|
Illustration of what happens with our compiler:
Grammar (subset of Tiny):
M --> S { S } '$'
S --> A | P
A --> lower-case '=' E ';'
P --> '<' ( E ';' | 'N' ';')
E --> T {('+' | '-') T}
T --> F {('*' | '/') F}
F --> '(' E ')' | lower-case | digit
Example Tiny program:
ex0.t: Tiny program (should print 7 or 7.0):
< 7; $
Parser works on virtual parse tree:
The address of '7' is 7*8 = 56 bytes past start of M.
This address is calculated inside F.
It is returned from F to T to E to A.
M
| \
S \
| '$'
P
/ | \
/ | \
'<' E ';'
| | 56
T ^
| | 56
F ^
| | 56
'7'
Your parsing program: Calls M at the top level.
M():
Output first part of framework
Call S()
Since next == '$', Tiny program done
Output second part of framework
S():
Because next is '<', call A()
P():
Scan past '<'
Get return value from E: res = E()
56 is returned
Output 3 MIPS instructions:
" li $v0, 3\n"
" l.d $f12, 56($s1)\n"
" syscall\n"
(A alway prints the same, except for
the "56" which comes from the call to E)
Execute Tiny compiler:
Redirect Tiny source in; redirect MIPS output out:
% python tiny.py < ex0.t > ex0.s
ex0.s: my final MIPS program:
Final MIPS output from compiler:
### compiled by Neal Wagner on
# Tue Oct 28 20:29:57 2014
main: move $s7, $ra
la $s1, M # data addr
# Print your name
li $v0, 4
la $a0, Name
syscall
### Compiled code starts here
# Output M[7] --+
li $v0, 3 |
l.d $f12, 56($s1) +--- generated code
syscall |
--+
# Stuff at end
move $ra, $s7
jr $ra # ret to sys
# data declarations
.data
.align 3
M: .double 0.,1.,2.,3.,4.,5.
.double 6.,7.,8.,9. # cons
.space 208 # a to z
.space 1000 # 125 temps
Blank: .asciiz " "
NewL: .asciiz "\n"
Tab: .asciiz "\t"
Name: .asciiz "Executed by Neal Wagner\n"
(Execute MIPS program. '%' = Linux prompt, red = MIPS output)
% spim ex0.s
SPIM Version 7.4 of January 1, 2009
Copyright 1990-2004 by James R. Larus (larus@cs.wisc.edu).
All Rights Reserved.
See the file README for a full copyright notice.
Loaded: /usr/lib/spim/exceptions.s
Executed by Neal Wagner
7%
(final note: if we wanted to print a newline at the end,
we would have included another Tiny statement: < N; )
|