Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Storage Registers vs. memory Access to registers is much faster than access to memory Goal: store as much data as possible in registers Limitations/considerations:

Similar presentations


Presentation on theme: "1 Storage Registers vs. memory Access to registers is much faster than access to memory Goal: store as much data as possible in registers Limitations/considerations:"— Presentation transcript:

1 1 Storage 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

2 2 Storage 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.

3 3 Storage 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?

4 4 Storage 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.

5 5 Storage organization Typical stack layout. Assume function f calls g, and g is about to call h parameter 5 parameter 6... temporary values return value saved fp return address local variables other saved registers parameters saved fp return address return value f's frame g's frame current fp current sp 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: fp+increment The value stored here is f's frame pointer. It will be restored on return from g. This is essentially the dynamic link Temporary variables are created by the compiler during code generation. g is responsible for saving the contents of certain registers (possibly used by f), and restoring them on exit. These are h's parameters Often stored in a register Program counter. So that g knows what the next instruction is after the return from h.

6 6 Handling 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 proc A() { proc B () { call C() } proc C() { proc D() { proc E() { call B() } call E() } call D() } call B() } call sequence: A C B B E D B can call C B cannot call D or E E can access C's locals C cannot access B's locals

7 7 Handling 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

8 8 Handling 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.

9 9 Handling 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 D[i] in the new stack frame (to be restored on exit) set D[i] to the new stack frame

10 10 Handling 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

11 11 Parameter 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

12 12 Stack 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

13 13 Stack maintenance A typical calling sequence : 1. 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

14 14 Stack maintenance A typical calling sequence : 2. Callee saves info on entry allocate memory for stack frame, update stack pointer save callee-saved registers save old frame pointer update frame pointer 3. Callee executes

15 15 Stack maintenance A typical calling sequence : 4. 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 5. Caller restores info restore caller-saved registers


Download ppt "1 Storage Registers vs. memory Access to registers is much faster than access to memory Goal: store as much data as possible in registers Limitations/considerations:"

Similar presentations


Ads by Google