In this set of notes we will consider how three different kinds of variables: local, global, and non-local, are accessed at runtime. For the purposes of this discussion, we will define these three categories as follows:
Local variables and parameters are stored in the activation record of the method in which they are declared. They are accessed at runtime using an offset relative to the frame pointer (FP). Since we're assuming that "up" in the stack means a lower address, these offsets will be (i) positive numbers for parameters, and (ii) negative numbers for local variables of the procedure. For example, given this code:
void P(int x) { int a, b; ... }and assuming that activation records do not include an access link field or space for saved registers, P's activation records will be organized as follows:
_______________ | | <--- Stack Pointer |_______________| b: | | |_______________| a: | | // locals have negative offsets relative to the frame pointer |_______________| | Control Link | |_______________| | Return Addr | <--- Frame Pointer |_______________| x: | | // parameters have positive offsets relative to the frame pointer |_______________|We will assume that each address and each integer takes up 4 bytes. Our memory-organization invariant will be that (i) the stack pointer points to the next 4-byte slot to use, and (ii) the frame pointer points to the 4-byte slot that holds the return address. (This invariant is only one possible convention that one could choose. For instance, you could choose to have the stack pointer point to the last item pushed -- i.e., the slot for b in the example above -- and you could have the frame pointer point to the control link. It is necessary to pick some convention and stick to it.)
For the example above, here are the offsets for each of P's parameters and locals:
lw $t1, -8($fp) # load a lw $t2, -12($fp) # load bTo be able to generate this code at compile time (e.g., to process a statement like x = a + b), the offset of each local variable must be known by the compiler. Therefore, the offset of each local variable from the Frame Pointer should be stored as an attribute of the variable in the symbol table. This can be done as follows:
Note as well that the formal parameters are traversed in left-to-right order, and are given successively greater and greater positive offsets (with respect to the callee's frame pointer). Thus, we must arrange for the caller to evaluate the actuals from right to left, so that the resulting values are pushed on the stack in an order that is consistent with the numbering scheme given above.
For MIPS code, each double variable requires 8 bytes. For example, given this code:
void P(double x) { double a, b; ... }variable x will be stored in the bytes at offsets +4 to +11 from the frame pointer, and the offset that should be stored in x's symboltable entry (and used to access x at runtime) is +4. Here are the offsets for all of P's locals:
l.d $f0, -12($fp) # load a l.d $f2, -20($fp) # load b
Assume that both an address and an integer require 4 bytes of storage, and that a double requires 8 bytes. Also assume that each activation record includes parameters, a return address, a control link, and space for local variables as illustrated above. What are the offsets (in bytes) for each of the parameters and local variables in the following functions?
void P1(int x, int y) { int a, b, c; ... while (...) { double a, w; } } void P2() { int x, y; ... if (...) { double a; ... } else { int b, c; ... } }
As noted above, global variables are stored in the static data area. Using MIPS code, each global is stored in space labeled with the name of the variable, and the variable is accessed at runtime using its name. (Since we will be using the SPIM simulator, and since SPIM has some reserved words that can also be used as Simple variable names, you will need to add an underscore to global variable names to prevent clashes with SPIM reserved words.)
For example, if the source code includes:
// global variables int g; double d;The following code would be generated to reserve space in the static data area for variables g and d:
.data # put the following in the static data area .align 2 # align on a word boundary _g: .space 4 # set aside 4 bytes .data .align 2 _d: .space 8And the following code would be generated to load the value of variable g into register t0, and to load the value of variable d into register f0:
lw $t0, _g # load contents of g into t0 l.d $f0, _dd # load contents of dd into f0Non-Local Variables
Recall that we are using the term "non-local variable" to refer to two situations:
First, let's consider an example (Pascal) program that includes accesses to non-local variables (the nesting levels of the procedures are given for later reference):
+ program MAIN; | var x: integer; | | + procedure P; | (2) write(x); | + | | + procedure Q; | | var y: integer = x; | | | | + procedure R; | | | x = x + 1; | | | y = y + x; (1) (2) (3) if y<6 call R; | | | call P | | + | | | | call R; | | call P; | | if x < 5 call Q; | + | | x = 2; | call Q; +
Trace the execution of this program, drawing the activation records for the main program and for each called procedure (include only local variables and control links in the activation records). Each time a non-local variable is accessed, determine which activation record it is stored in. Notice that the relative nesting levels of the variable's declaration and its use does not tell you how far down the stack to look for the activation record that contains the non-local variable. In fact, this number changes for different calls (e.g., due to recursion).
The idea behind the use of access links is as follows:
We will put the access-link field as the first field in the AR (proper)—that is, register FP points to the access-link field. Thus, the layout of an AR with one parameter and two locals is as follows:
_______________ | | <--- Stack Pointer |_______________| local2: | | |_______________| local1: | | // locals have negative offsets relative to the frame pointer |_______________| | Control Link | |_______________| | Return Addr | |_______________| | Access Link | <--- Frame Pointer |_______________| param1: | | // parameters have positive offsets relative to the frame pointer |_______________|A good way of thinking about the ARs is in terms of linked lists: each AR is an element that can participate in two kinds of lists:
Returning to the example program given earlier, here's a snapshot of the runtime stack after the first call from R to P (where we show only the access links and the local variables in the ARs):
____________ P | |------------+ <-- FP +==========+ | R | |----------+ | +==========+ | | R | |------+ | | +==========+ | | | y:| 9 | | | | Q +----------| | | | | |--+ <-+ <-+ | +==========+ | | x:| 4 | | | MAIN |----------| | | | null |<-+ <--+ |__________|To access the value of x from procedure R, access links must be followed. The number of links that must be followed is:
move $t0, FP // no links followed yet; t0 holds ptr to R's access link field lw $t0, ($t0) // 1 link: t0 holds ptr to Q's AR's access link field +-> lw $t0, ($t0) // 2 links: t0 holds ptr to main's AR's access link field | lw $t0, -12($t0) // t0 holds value of x | sw $t0, -12(FP) // y = x | This code would be repeated if we needed to follow more links.How to set up access links:
In this case, the calling procedure sets up the called procedure's access link by pushing the value of the FP just before the call, since the FP points to its access link. For example, when Q is calling R, here is a picture of the stack just after R's access link has been set up by Q:
<-- SP |----------| | |------+ |==========| | y:| | | Q |----------| | | |--+ <-+ <-- FP |==========| | x:| | | MAIN |----------| | | null |<-+ |__________|Case 2: The calling procedure's level is greater than or equal to the called procedure's level. In this case, the called procedure's access link should point to an AR that is already on the stack. The value for the new access link is found by starting with the value of the calling procedure's access link field (which is pointed to by the FP), and following X-Y access links, where:
lw $t0, 0(FP)If X == Y (i.e., no links should be followed to find the value of the new access link), then the value of t0 should simply be pushed onto the stack. If X is greater than Y, then the code:
lw $t0, 0($t0)should be generated X - Y times, before pushing the value of t0.
To illustrate this situation, consider two cases from the example code: R calls P, and Q calls itself. Recall that the nesting structure of the example program is:
+-- | | +-- | | | P| | | | +-- | MAIN | +-- | | | | +-- | | | | Q| R| | | | | | +-- | | | +-- | +--When R is about to call P, the stack looks like this:
+==========+ R | |----------+ +==========+ | R | |------+ | +==========+ | | y:| 9 | | | Q +----------| | | | |--+ <-+ <-+ +==========+ | x:| 4 | | MAIN |----------| | | null |<-+ |__________|Since P is nested inside MAIN, P's access link should point to MAIN's AR (i.e., the bottom AR on the stack). R's nesting level is 3 and P's nesting level is 2. Therefore, we start with the value of R's access link (the pointer to Q's AR) and follow one link. This retrieves the values of Q's access link, which is (as desired) a pointer to MAIN's AR. This is the value that will be pushed onto the stack (copied into P's AR as its access link).
When Q is about to call itself, the stack looks like this:
|==========| y:| | Q |----------| | |--+ |==========| | x:| | | MAIN |----------| | | null |<-+ |__________|The access link in the new AR for Q should be the same as the access link in the current AR for Q; namely, it should be a pointer to the AR for MAIN. This value is found by starting with the value of Q's access link (a pointer to MAIN's AR) and following zero links (because X = Y = 1).
Trace the execution of the example program again. Each time a procedure is called, determine which case applies in terms of how to set up the called procedure's access link. Then use the appropriate algorithm to find the value of the new access link, and draw the new AR with its access link.
Each time a non-local variable x is accessed, make sure that you find its activation record by following i - j access links (where i is the nesting level of the procedure that uses x and j is the nesting level of the procedure that declares x).
To use an access link:
The motivation for using a display is to avoid the runtime overhead of following multiple access links to find the activation record that contains a non-local variable. The idea is to maintain a global "array" called the display. Ideally, the display is actually implemented using registers (one for each array element) rather than an actual array; however, it can also be implemented using an array in the static data area.
The size of the display is the maximum nesting depth of a procedure in the program (which is known at compile time). The display is used as follows:
+-- |int x; | | +-- | | | P|...x... | | | +-- | MAIN | +-- | | int y; | | | | +-- | | |...x...y... | Q| R|call R | | |call P | | +-- | | | | call R | | call P | | if (...) call Q | +-- | | call Q +--Below are two pictures comparing the use of access links with the use of a display. Both show the same moment at runtime.
USING ACCESS LINKS USING A DISPLAY ____________ ---------- P | |------------+ P | |<--+ |==========| | |========| | +--+ R | |----------+ | R | |<------| | [2] |==========| | | |========| | +--+ y:| | | | y:| | +---| | [1] Q |----------| | | Q |--------| +--+ | |------+ <-+ | | | +---| | [0] |==========| | | |========| | +--+ x:| | | | x:| | | DISPLAY MAIN |----------| | | MAIN |--------| | | null | <-+ <--+ | |<--+ |__________| ---------- STACK STACK
To maintain the display, a new field (called the "save-display" field) is added to each activation record. The display is maintained as follows:
Before R calls P: ---------------- ________ ____________ [2] | |--\ R | | |______| -------------->| | [1] | |--\ |==========| |______| \ y:| | [0] | |--\ \ Q |----------| |______| \ ------------>| | \ |==========| \ x:| | \ MAIN |----------| ---------->| | |==========| After R calls P: --------------- ________ [2] | |--\ ------------ |______| \ P | |---+ [1] | |----\------------>| | | |______| \ |==========| | [0] | |--\ \ R | | | |______| \ ---------->| | | \ |==========| | \ x:| | | \ Q |----------| | \ | |<--+ \ |==========| \ y:| | \ MAIN |----------| ------>| | |----------|
Trace the execution of the running example program, assuming that a display is used instead of access links. Each time a non-local variable x is accessed, make sure that you understand how to find its AR using the display.
Comparison: Access Links vs The Display
Recall that under dynamic scoping, a use of a non-local variable corresponds to the declaration in the "most recently called, still active" method. So the question of which non-local variable to use can't be determined at compile time. It can only be determined at run-time. There are two ways to implement access to non-locals under dynamic scope: "deep access" and "shallow access", described below.
Using this approach, given a use of a non-local variable, control links are used to search back in the stack for the most recent AR that contains space for that variable. Note that this requires that it be possible to tell which variables are stored in each AR; this is more natural for languages that are interpreted rather than being compiled (which was indeed the case for languages that used dynamic scope). Note also that the number of control links that must be followed cannot be determined at compile time; in fact, a different number of links may be followed at different times during execution, as illustrated by the following example program:
void P() { write x; } void Q() { x = x + 1; if (x < 23) Q(); else P(); } void R() { int x = 20; Q(); P(): } void main() { int x = 10; R(); P(); }
Trace the execution of the program given above. Note that method P includes a use of non-local variable x. How many control links must be followed to find the AR with space for x each time P is called?
Using this approach, space is allocated (in registers or in the static data area) for every variable name that is in the program (i.e., one space for variable x even if there are several declarations of x in different methods). For every reference to x, the generated code refers to the same location.
When a method is called, it saves, in its own AR, the current values of all of the variables that it declares itself (i.e., if it declares x and y, then it saves the values of x and y that are currently in the space for x and y). It restores those values when it finishes.
Note that this means that when a method accesses a non-local variable x, the value of x from the most-recently-called, still-active method is stored in the (single) location for x. There is no need to go searching down the stack!
Question 1: Trace the execution of the program given above again, this time assuming that shallow access is used.
Question 2: What are the advantages and disadvantages of shallow access compared with deep access? (Consider both time and space requirements.)