


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Class: INTRO TO COMPILERS; Subject: Computer Science; University: University of Maryland; Term: Spring 2006;
Typology: Study notes
1 / 4
This page cannot be seen from the preview
Don't miss anything!



-^ Generating stack codeCode generation
Lecture 10, Page 1
-^ Simple expressions^ Generating stack code
expr( node ) switch(
(^) kind of
node )
case PLUS: if (node.type() == INT)expr( right child );expr( left child ); emit( iadd );
break;
case ID: if (node.type() == INT)int offset = addr( node.str ); emit( iload, offset );
break;
case NUM: break;emit( ldc int, node.val );
return result;
CMSC 430
Lecture 10, Page 2
Simple expressions
PLUS
x ID
PLUS
yID
iload 1
$ push addr(x)
x
ldc int, 4
$ push constant 4
x, 4
iload 2
$ push addr(y)
x, 4, y
iadd
$ add 4 & y
x, 4+y
iadd
$ add x & (4 + y)
x+(4+y)
CMSC 430
-^ Algorithm^ Boolean expressions
(^2)
L2:iconst 1L1:goto L2iconst 0 if icmpeq L1 2 E 1 E
(^2)
L2:iconst 1L1:goto L2iconst 0 if icmplt L1 2 E 1 E
CMSC 430
1 E Boolean expressions (^) && E
(^2)
L1: 2 Epopifeq L1 dup 1 E
(^2)
L1: 2 Epopifne L1 dup 1 E
L2:iconst 1L1:goto L2iconst 0ifeq L1E
CMSC 430
Lecture 10, Page 5
x = E Control structures
istore addr(x)E
if ( E ) S
L:Sifeq LE
if ( E ) S
(^1) else S
(^2)
L2: 2 S L1: goto L2 1 S ifeq L1E
while ( E ) S
L2:goto L1S ifeq L2EL1:
CMSC 430
Lecture 10, Page 6
Code for procedure calls ( Procedure calls
FP (^) is (^) frame pointer
(^) is (^) return address
save registers
prolog code
extend basic frame
for local data
find static data area
if needed
allocate child’s frame... initialize locals
start of a
(^) call
storeevaluate & store params.
(^) FP (^) and (^) RA
in child’s frame
set (^) FP (^) for child
jump to child
may handle
(^) RA
RA : copy return value
post- call (^) code
restore params.
if needed
store return value... free child’s frame
epilog code
restore parent’srestore registers unextend basic frame
(^) FP
jump to
(^) RA
hardware
(^) ret
CMSC 430 How do we handle a function call in an expression? Function calls Example:
a + foo(1)
-^ Treat it like a function call
a + foo(a,b) + b
CMSC 430
What about arrays as actual parameters?Array parameters Example:
int A[100]; foo( A );
Call-by-reference (
c-b-r ) — address of variable
Call-by-value (
c-b-v ) — value of variable
-^ Whole arrays (^) need dimension information —
(^) dope vectors
stuff in all the values in calling sequence
pass the address of dope vector as parameter
generate the complete address polynomial
-^ Some improvement is possible. (^) save (^) n i (^) and (^) low i
pre-compute terms on entry to procedure (
if used
(^) )
CMSC 430^ Restricting the language can eliminate this problem
Lecture 10, Page 13
What does Array parameters
(^) mean as an actual parameter?
Example:
foo( A[12] )
If the corresponding formal is a scalar, it’s easy Example:
foo( int X )
simply pass the value or the address
must know about arguments on both sides
language must force this interpretation
What if the corresponding formal is an array? Example:
foo( int X[100] )
requires knowledge on both sides of call
meaning must be well-defined and understood
cross-procedural checking of conformability
CMSC 430
Lecture 10, Page 14
-^ Stack limit^ Memory management -^ •^ requires detailed knowledge of^ •^ trace possible flows of control^ maximum height of stack during evaluation of an expression
CMSC 430
Lecture 10, Page 15