Static and Dynamic (Access) Links

Section 7.3.5 discusses the topic of Access Links building on the diagram Fig 7.5 which is repeated here: Figure 7.5 provides a General Activation Record
	-------------------------
	| Actual parameters     |      often use registers for this, but show space to add to discussion
	-------------------------
	| Returned values       |      space for returned values of a function
	-------------------------
	| Control link          |      points to AR of caller
	-------------------------
	| Access link           |      may be needed to locate data needed by called proc (say in another proc's AR)
	-------------------------
	|  Saved machine status |      status of machine just before proc call
	-------------------------
	|  Local Data           |      variable names declared in proc
	-------------------------
	|  Temporaries          |      evaluation of expressions (not using registers)
	-------------------------
This is a technique that is an alternative to using Displays to manage the Run Time Environment. It will use only one register, the Frame Pointer, fp below, and out TOS top_sp. This will allow unlimited nesting of procedures, in contrast to our Display example limited to nesting depth maximum of 3, an arbitrarily chosen value.

The access link points to the activiation record (AR) when function are nested. The access links form a chain from the AR at the top of the stack to a sequence of activations at progressively lower nesting depths. Along this chain are all the activiations whose data and procedures are accessible to the currently executing procedure.

We can draw the configuration using static link/dynamic link using textbook scheme from 7.2.3. We have a useful disgram on p. 437 on the division of tasks between the caller and the callee.




$s0 should point to global variables
$fp should point to the active activation record
$top_sp should point to the top of stack

The procedure's activation record holds

		/ \
		 |

	| local variables |
	-------------------
	| dynamic link    |
	-------------------
	| static link     |
	-------------------
	| return address  |
	-------------------

For the following code fragment referred to in 7.3.5 which a slight 
rearrangement of names.  In this example, procdure r is contained within 
procedure p.  (Text discussed proc p nested within proc q).

Package main is 
	Procedure p is
		Procedure r is
                     -- body of r
		end r;
        begin -- procedure p
	  r; -- invoke r
	end p;
	Procedure q is 
	begin -- procedure q
	  p; -- invoke p
	end q;
begin -- main
  q; -- invoke q
end main;

	
Lets example the run-time-stack after the following sequence of calls:

Main calls q
q calls p
p calls r
r calls r (called r')


with an Activation using dynamic/static links of the following form

                ^
                |
offset 12| local vars     |
         ------------------
offset 8 | dynamic link   |
         ------------------
offset 4 | static link    |
         ------------------
offset 0 | return address | AR(?)
         |----------------|


                                           
main calls q
q call p
p calls r
r calls r
executing in the body of proc r:

                                           While q is active, q
                                           can only see variables
$top_sp->                                  accessible through the
         | local vars     |                static chain - therefore
         ------------------                can see q's local vars
*--------|-dynamic link   |                and globals (either through
|        ------------------                $s0 or q's static link)
|        | static link  --|-----------*
|        ------------------           |    FYI, when q returns
|        | return address |           |    a) $fp <- q's dyn link
|        |----------------|           |
|        | AR (r')        |           |
|  $fp-->==================           |
|        | local vars     |           |
|        ------------------           |    When q calls p, proc p can see:
|  *-----|-dynamic link   |           |    a) p's local vars
|  |     |----------------|           |    b) globals using s0 or by following 
|  |     | static link  --|-------*   |       the static chain one link 
|  |     |----------------|       |   |       
|  |     | return address |       |   |    When p call r, proc r call see:  
|  |     |----------------|       |   |    a) r's local vars 
|  |     | AR (r )        |       |   |    b) p's local vars by following
*--|---->==================       |   |       the static chain one link
   |     | local vars     |       |   |    c) globals following static chain 2 links
   |     |----------------|       |   |      
   |  *--|-dynamic link   |       |   |    When r calls r (call this r'), 
   |  |  |----------------|       |   |    r' can see:
   |  |  | static link  --|----*  |   |    a) r' local vars
   |  |  |----------------|    |  |   |    b) p's local vars by following
   |  |  | return address |    |  |   |       the static chain 1 link 
   |  |  |----------------|    |  |   |    c) globals following static chain
   |  |  | AR (p )        |    |  |   |       2 links
   *--|->==================<---|--*---*
      |  | local vars     |    |       
      |  |----------------|    |       
 *-------|-dynamic link   |    |       
 |    |  |----------------|    |       
 |    |  | static link  --|--* |       
 |    |  |----------------|  | |       
 |    |  | return address |  | |       
 |    |  |----------------|  | |       
 |    |  | AR (q )        |  | |       
 |    *->==================  | |       
 |       | globals        |  | |       
 *->$s0->------------------<-*<*
         | static data    |
         ------------------
         |                |
         |   code         |
         ------------------