Activation Records - Frames
Moving towards Run-Time Organisation
Generic stack-based model & abstract machine code
Function calling conventions
Locals/Arguments in registers or in memory?
Handling nested scopes

Slide 2

Reminder
on
Stacks

Registers
For speed, keep as much as possible in registers
When f calls g and both require register r, who manages r?
Caller save – if r must be saved by f
Callee save – if r must be saved by g

Parameter passing
Also in registers where possible
Registers may be reused in subsequent calls
Caller reserves memory to save them
Callee saves as necessary
Return address and result often passed in a register as well

Frame resident variables… when?
Variable accessed in a function nested within this one
The variable has escaped
Register holding variable needed for something else
No other register available
Too many locals/temporaries/parameters to fit in registers
And so they spill out into the frame

Escaping variables – static links
Code of inner function can access variables in a lexically outer function
Frames matching lexical structure must be linked together
Each frame has a static link to the frame of the function statically or lexically enclosing it
Read up – P138

So what information is required?
Identify which values can be held in registers, which must be in memory
Escapes in memory
Note - at this stage, a property of the source language
Develop an abstraction for frame layout
Initially to record this information
Notion of levels required to model nesting depth

How to find escapes
Additional pass over the AST, before any type checking/ translation
Add escape? field to locals and formals in AST, initially set to false
Construct symbol table during pass
Entry for every local and formal, indicating lexical level and with a pointer to AST entry
On every access to local/formal
Lookup in table, check if declaration level is greater than current level – set escape? to true if so

Labels and Temporaries
Semantic analysis phase would like to
allocate machine registers to parameters and locals
allocate memory addresses for code bodies
Too early for this…
So use temporaries – abstract names – for registers, and
labels for static memory addresses

Packages & classes
Translate
Level
Translate.AccessList
Translate.Access
Frame
Frame    abstract class – realised for specific architectures
Frame.AccessList
Frame. Access
Temp
Label and Temp
Semant
Absyn

Finally
Read through Chapter 6
6.1 Stack Frames
Although note that the discussion on parameter passing is wider than we need
Be sure about static links
Work through the example if necessary
6.2 Frames in Tiger
Rather an opaque treatment
Read as much as you like, but concentrate on
Calculating escapes
Temporaries and labels
Managing static links
The rest is easier to understand with the code nearby
I’ll set up the code for download by Wednesday a.m.