









































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
A lecture note from ecs 142 - computer organization course taught by dr. Sean peisert during spring 2009. It covers the concept of activation records and their role in code generation during compiler development. The lecture explains how the activation record is designed together with the code generator, the stack discipline that guarantees the same pointer value on function exit, and the use of a frame pointer to find variables. The document also discusses the advantages and disadvantages of stack machines and proposes a better way to keep temporaries in the activation record.
Typology: Study Guides, Projects, Research
1 / 49
This page cannot be seen from the preview
Don't miss anything!










































Lecture 21 Dr. Sean Peisert – ECS 142 – Spring 2009 1
1 week to go on project 3
Read Sec. 7.1 through 7.4 by Monday, May 18 2
The stack discipline guarantees that on function exit $sp is the same as it was on function entry
No need to save $sp
We need the return address
It’s handy to have a pointer to start of the current activation
This pointer lives in register $fp (frame pointer)
Reason for frame pointer will be clear shortly 4
Summary: for this language, an AR with the caller’s frame pointer, the actual parameters, and the return address suffices.
Picture: consider a call to f(x,y), the AR will be: 5 old FP y x High Addresses
AR of f Low Addresses
Calling Sequence for f(x,y) 7
old FP y x
old FP y x return Before call On entry Before exit After call
Code Generation for Variables 8
Variable references are the last construct
The “variables” of a function are just its parameters
They are all in the AR
Pushed by the caller
Problem: Because the stack grows when intermediate results are saved, the variables are not at a fixed offset from $sp
Code Generation for Variables 10
Example: For a function def f(x 1 ,x 2 ) = e the activation and frame pointer are set up as follows: FP SP old FP x 2 x 1 return x 1 is at fp + 4 x 2 is at fp + 8 Thus: where z = 4 * i cgen(xi) = lw $a0 z($fp)
The activation record must be designed together with the code generator
Code generation can be done by recursive traversal of the AST
We recommend you use a stack machine for your Cool compiler (it’s simple) 11
Stack Machine Pros & Cons
The stack machine has activation records and intermediate results interleaved on the stack
Advantage: very simple code generation
Disadvantage: Very slow code
Storing/loading temporaries requires a store/load and $sp adjustment 13 AR Intermediates AR Intermediates
Keep Temporaries in the AR
Code generator must assign a location in the AR for each temporary. 14
How Many Temporaries?
Space used for temporaries in e 1 can be reused for temporaries in e 2 16 NT(e 1 + e 2 ) = max(NT(e 1 ), 1 + NT(e 2 )) NT(e 1 - e 2 ) = max(NT(e 1 ), 1 + NT(e 2 )) NT(if e 1 = e 2 then e 3 else e 4 ) = max(NT(e 1 ), 1 + NT(e 2 ), NT(e 3 ), NT(e 4 )) NT(id(e 1 ,…,en) = max(NT(e 1 ),…,NT(en)) NT(int) = 0 NT(id) = 0 Exercise: Is this bottom-up or top-down? Exercise: What is NT(…code for fib…)? 17
19 Old FP xn ... x 1 Return Addr. Temp NT(e) ... Temp 1
Revised Code Generation 20