Docsity

OOPS - Activation records and storage management (Static chains), Study notes of Object Oriented Programming

Partial preview of the text

Download OOPS - Activation records and storage management (Static chains) and more Study notes Object Oriented Programming in PDF only on Docsity!

Activation records and storage

management (Static chains)

Static chains

 A static chain is a chain of static links that connect

certain activation record instances in the stack.

 During the execution of a procedure P, the static link

of its activation record instance points to an

activation record instance of P’s static parent

program unit.

 So the static chain links all the static ancestors of an

executing subprogram, in order of static parent first.

 This chain can obviously be used to implement the

accesses to non-local variables in static-scoped

languages.

Static depth

 Main program has a static_depth of 0.

 If procedure A is the only procedure defined

in a main program, its static_depth is 1.

 If procedure A contains the definition of a

nested procedure B, then B’s static_depth is

Chain offset

 (^) The length of the static chain needed to reach the correct

activation record instance for a nonlocal reference to a variable X is exactly the difference between the static_depth of the procedure containing the reference of X and the static_depth of the procedure containing the declaration for X.

 (^) This difference is called the nesting_depth or chain_offset, of

the reference.

 (^) The actual reference can be represented by an ordered pair of

integers (chain_offset, local_offset) where chain_offset is the number of links to the correct activation record instance.

 (^) To illustrate the complete process of non-

local accesses, consider the following

skeletal program in C

code.doc

Local offset for each procedure in the diagram

RA –Return address, SL – Static link, DL – Dynamic link

Reference to variables in expression Decl – Declaration, Ref – Refrence, SD – Static depth, proc - procedure

When control is at Position 2 in program

 (^) After SUB1 completes its execution, the activation record

instance for SUB1 is removed from the stack, and control returns to SUB3.  (^) The reference to the variable E at position 2 in SUB3 is local

and uses the pair (0,4) for access.

 (^) The reference to the variable B is to the one declared in SUB2,

because that is the nearest static ancestor that contains such a declaration. It is accessed with the pair(1,4). The local_offset is 4 because B is the first variable declared in SUB1, and SUB has one parameter.

 (^) The reference to the variable A is to the A declared in BIGSUB,

because neither SUB3 nor its static parent SUB2 has a declaration for a variable named A. it is referenced with the pair (2,3).

When control is at Position 3 in program

 (^) After SUB3 completes its execution, the activation record

instance for SUB3 is removed from the stack, leaving only the activation record instances for MAIN_2, BIGSUB, and SUB2.  (^) At position 3 in SUB2, the reference to the variable A is to the A

in BIGSUB, which has the only declaration of A among the active routines. This access is made with the pair(1,3).  (^) At this position, there is no visible scope containing a

declaration for the variable D, so this reference to D is a static semantics error. The error would be detected when the compiler attempted to compute the chain_offset/local_offset pair.

 (^) The reference to E is to the local E in SUB2, which can be

accessed with the pair (0,5).