Title: Storage
1Storage
- Registers vs. memory
- Access to registers is much faster than access to
memory - Goal store as much data as possible in registers
- Limitations/considerations
- Small number of registers
- E.g. if there are no registers available or their
contents need to be spilled, we must have a place
in memory where this data can be saved. - Some data cannot be stored in registers
- E.g. variables whose address needs to be
accessed, or variables that are too large to fit
in a register. - Things we'd like to store in registers
- Frequently used variables
- Function parameters
- Function return values
2Storage
- Registers vs. memory from the point of view of a
function - Some variables can be stored in registers
- Exceptions
- Variables whose address is being used
- Variables that will be passed by reference to
another called function. - Variables such as arrays (due to indexing issues)
- Parameters and return values can be stored in
registers - Useful fact most functions have at most 4
parameters. - There must be an area in memory, associated with
the function, so that the debugger can provide a
backtrace. - Items that need to be stored in memory, are all
grouped together and placed in a stack frame.
3Storage organization
- Stack layout
- Each called procedure is given a stack frame at
the top of the stack - Stack typically grows towards lower addresses
- At any time, the stack pointer points to the
current top of the stack (first available
location) - At any time, the frame pointer points to the
beginning of the current frame - The stack frame size and the offsets are
determined at compile time! - Stack frame contents are accessed as offsets
relative to the stack pointer or frame pointer. - Why would we use the frame pointer? Can we do
without it?
4Storage organization
- Stack frames. What's typically in them?
- parameters
- local variables
- temporary values
- return value
- stack pointer
- frame pointer
- return address
- (saved pc)
- dynamic link
- Reference to the frame of the caller, used to
restore the stack after the call. - static link
- Reference to the nearest frame of the lexically
enclosing procedure. Used for non-local accesses
in languages that support statically nested
scopes.
5Storage organization
- Typical stack layout. Assume function f calls g,
and g is about to call h
...
The first 4 parameters for g are typically in
registers. The rest are stored here in reverse
order, so that they are accessed in the "right"
order by g fpincrement
parameter 6
parameter 5
f's frame
return value
return address
current fp
The value stored here is f's frame pointer. It
will be restored on return from g. This is
essentially the dynamic link
saved fp
local variables
temporary values
Temporary variables are created by the compiler
during code generation.
g's frame
other savedregisters
g is responsible for saving the contents of
certain registers (possibly used by f), and
restoring them on exit.
parameters
These are h's parameters
return value
Often stored in a register
return address
current sp
saved fp
Program counter. So that g knows what the next
instruction is after the return from h.
6Handling nested procedures
- Some languages allow nested procedures
- Example
- In order to implement the "closest nested scope"
rule we need access to the frame of the lexically
enclosing procedure
call sequence
proc A() proc B () call C()
proc C() proc D()
proc E() call B()
call E() call
D() call B()
A
B
B can call C B cannot call D or E E can access
C's locals C cannot access B's locals
C
D
E
B
7Handling nested procedures
- Some languages allow nested procedures
- In order to implement the "closest nested scope"
rule we need access to the frame of the lexically
enclosing procedure - Method 1 static links (a.k.a. access links)
- Reference to the frame of the lexically enclosing
procedure - Static chains of such links are created.
- How do we use them to access non-locals?
- The compiler knows the scope s of a variable
- The compiler knows the current scope t
- Follow s-t links
8Handling nested procedures
- Method 1 static links
- Setting the links
- if the callee is nested directly within the
caller, set its static link to point to the
caller's frame pointer (or stack pointer) - if the callee has the same nesting level as the
caller, set its static link to point to wherever
the caller's static link points.
9Handling nested procedures
- Method 2 Displays
- A Display encodes the static link info in an
array. - The display may be stored in registers or in
memory - The ith element of the array points to the frame
of the most recent procedure at scope level i - How?
- When a new stack frame is created for a procedure
at nesting level i, - save the current value of Di in the new stack
frame (to be restored on exit) - set Di to the new stack frame
10Handling nested procedures
- Displays vs. Static links
- criteria
- added overhead
- space
- nesting depth
- frequency of non-local accesses
- storage in registers vs. memory
- memory storage requires extra loads
- register storage ties up registers
11Parameter passing
- By value
- actual parameter is copied
- By reference
- address of actual parameter is stored
- By value-result
- call by value, AND
- the values of the formal parameters are copied
back into the actual parameters. - Example
int a void test(int x) x 2 a
0 int main () a 1 test(a)
a is 2 here
12Stack maintenance
- Calling sequence
- code executed by the caller before and after a
call - code executed by the callee at the beginning
- code executed by the callee at the end
13Stack maintenance
- A typical calling sequence
- Caller assembles arguments and transfers control
- evaluate arguments
- place arguments in stack frame and/or registers
- save caller-saved registers
- save return address
- jump to callee's first instruction
14Stack maintenance
- A typical calling sequence
- Callee saves info on entry
- allocate memory for stack frame, update stack
pointer - save callee-saved registers
- save old frame pointer
- update frame pointer
- Callee executes
15Stack maintenance
- A typical calling sequence
- Callee restores info on exit and returns control
- place return value in appropriate location
- restore callee-saved registers
- restore frame pointer
- pop the stack frame
- jump to return address
- Caller restores info
- restore caller-saved registers