Professional Documents
Culture Documents
CS 540
Spring 2009
Compiler Architecture
Intermediate
Code
tokens Syntactic
Scanner Parser Semantic Code
Source structure Target
language
(lexical (syntax Analysis Generator
language
analysis) analysis) (IC generator)
Intermediate
Code
Intermediate
Code Code
Optimizer
Symbol
Table
c c c
b
t0
b
t0
t1
c
t0
b - (uni)
t1
t1
c
t0
b - (uni)
t1
t1
c
t1
c
t1
c c
t2
li $v0,1
lw $a0,i
syscall # print I
li $v0,10
syscall # exit
inner loop
CS 540 GMU Spring 2009 36
can run by providing an input file
EE+E
E id E E
E E
E0 E1
Each number a := b + c + d + e
corresponds to a
temporary variable.
Each number a := b + c + d + e
corresponds to a
temporary variable.
• Boundary issues
• Field names – address will be offset from record
address
CS 540 GMU Spring 2009 54
Records in Spim
• Allocate enough space to hold all of the elements.
• Multiple ways to do this
• Record holding 3 (uninitialized) four-byte integers
named a,b,c:
record: .space 12
OR
record_a: .word 0
record_b: .word 0
record_c: .word 0 convert to scalar
x z
y Data
Data
The Concept:
Code Data Code
Code
f1 b: f1 b:
code code
c: c:
f2 z f2 z
code f1 code f1
f2 f2
f1
b: b:
code
c: c:
f2 z z
code f1 f1
f2 f2
N: 2 b: b:
Class A
d: c: c:
f1 z z
f2
f2 f1
code code
Object Variables
• Object storage is heap allocated at object creation
– Fields at fixed offsets from start of object storage
• Methods
– Code for methods is stored with the class
– Methods accessed by offsets from code vector
• Allows method references inline
– Method local storage in object (no calls) or on stack
Multiple inheritance??
CS 540 GMU Spring 2009 75
Processing Control Structures
• Constructs:
– If
– While
– Repeat
– For
– case
• Label generation – all labels must be unique
• Nested control structures – need a stack
CS 540 GMU Spring 2009 76
Conditional Examples
lw $t0,y
li $t1,0
if (y > 0) then begin sgt $t2,$t0,$t1 # = 1 if true
beqz $t2,L2
…body…
…body… L2:
end
Control Flow
…body-2 …
end
Control Flow
end
Control Flow
CS 540 GMU Spring 2009 79
Generating Conditionals
if_stmt IF expr THEN
{ code to eval expr ($2) already done
get two new label names
output conditional ($2=false) branch to first
label}
stmts ELSE
{ output unconditional branch to second
label
output first label }
stmts ENDIF
{ output second label }
stmts END
{ increment id (and save)
unconditional branch to label1
output label2 }