Generating Stack Codes and Code for Complex Expressions | CMSC 430, Study notes of Computer Science

Material Type: Notes; Class: INTRO TO COMPILERS; Subject: Computer Science; University: University of Maryland; Term: Spring 2006;

Typology: Study notes

Pre 2010

Uploaded on 07/30/2009

koofers-user-05o
koofers-user-05o 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Code generation
Generating stack code
simple expressions
boolean expressions
control flow
Code for complex expressions
procedures
function calls
mixed type expressions
array references
Storage
local variables
stack limit
CMSC 430 Lecture 10, Page 1
Generating stack code
Simple expressions
make use of implicit stack
still need variable addresses
expr( node )
switch( kind of node )
case PLUS:
expr( left child );
expr( right child );
if (node.type() == INT)
emit( iadd );
break;
case ID:
int offset = addr( node.str );
if (node.type() == INT)
emit( iload, offset );
break;
case NUM:
emit( ldc int, node.val );
break;
return result;
CMSC 430 Lecture 10, Page 2
Simple expressions
PLUS
ID
xPLUS
NUM
4ID
y
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 Lecture 10, Page 3
Boolean expressions
Algorithm
evaluate expression
leave 0 on top of stack if false, 1 if true
E1== E2E1
E2
if icmpeq L1
iconst 0
goto L2
L1:
iconst 1
L2:
E1<E2E1
E2
if icmplt L1
iconst 0
goto L2
L1:
iconst 1
L2:
CMSC 430 Lecture 10, Page 4
pf3
pf4

Partial preview of the text

Download Generating Stack Codes and Code for Complex Expressions | CMSC 430 and more Study notes Computer Science in PDF only on Docsity!

-^ Generating stack codeCode generation

  • (^) control flow • (^) boolean expressions (^) simple expressions -^ Code for complex expressions
  • (^) array references • (^) mixed type expressions • (^) function calls (^) procedures -^ Storage
  • (^) stack limit (^) local variables CMSC 430

Lecture 10, Page 1

-^ Simple expressions^ Generating stack code

  • (^) still need variable addresses (^) make use of implicit stack

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

4 NUM

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

  • (^) leave 0 on top of stack if false, 1 if true (^) evaluate expression 1 E (^) == E

(^2)

L2:iconst 1L1:goto L2iconst 0 if icmpeq L1 2 E 1 E

1 E

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

1 E

E

(^2)

L1: 2 Epopifne L1 dup 1 E

! 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

, RA

(^) 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

  • (^) get the return value into a register • (^) generate the call and return sequence (^) set up the arguments -^ Cautions Example:^ •^ register save-restore covers intermediate values^ •^ evaluation order is suddenly important^ function may have side effects

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

A[12]

(^) 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

  • (^) virtual machine (^) code generated -^ Local storage -^ For each procedure
  • (^) assign offset to all local variables on stack (^) calculate total size of local variables

CMSC 430

Lecture 10, Page 15