Docsity

OOPS - Activation Records and storage management, Study notes of Object Oriented Programming

Toggle sidebar
bg1
Activation Records and storage management
The General Semantics of Calls and Returns
The subprogram call and return operations of a language are together called its
subprogram linkage. Any implementation method for subprograms must be based on the
semantics of subprogram linkage.
A subprogram call in a typical language has numerous actions associated with it.
Implementing FORTRAN 77 Subprograms
Simplifying characteristic of FORTRAN 77 is that subprograms cannot be
recursive.
The semantics of a FORTRAN 77 subprogram call requires the following actions:
1. Save the execution status of the current program unit
2. Carry out the parameter-passing process.
3. Pass the return address to the callee.
4. Transfer control to the callee.
The semantics of a FORTRAN 77 subprogram return requires the following actions:
1. If pass-by-value-result parameters are used, the current values of those
parameters are moved to the corresponding actual parameters.
2. If the subprogram is a function, the functional value is moved to a place
accessible to the caller.
3. The execution status of the caller is restored.
4. Control is transferred back to the caller.
The call and return actions require storage for the following:
• Status information about the caller
• Parameters
• Return address
• Functional value for function subprograms
These, along with the local variables and the subprogram code, form the complete set of
information a subprogram needs to execute and then return control to the caller.
PAGE 16
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download OOPS - Activation Records and storage management and more Study notes Object Oriented Programming in PDF only on Docsity!

Activation Records and storage management

The General Semantics of Calls and Returns

The subprogram call and return operations of a language are together called its subprogram linkage. Any implementation method for subprograms must be based on the semantics of subprogram linkage. A subprogram call in a typical language has numerous actions associated with it.

Implementing FORTRAN 77 Subprograms Simplifying characteristic of FORTRAN 77 is that subprograms cannot be recursive. The semantics of a FORTRAN 77 subprogram call requires the following actions:

  1. Save the execution status of the current program unit
  2. Carry out the parameter-passing process.
  3. Pass the return address to the callee.
  4. (^) Transfer control to the callee. The semantics of a FORTRAN 77 subprogram return requires the following actions:
  5. If pass-by-value-result parameters are used, the current values of those parameters are moved to the corresponding actual parameters.
  6. If the subprogram is a function, the functional value is moved to a place accessible to the caller.
  7. The execution status of the caller is restored.
  8. Control is transferred back to the caller. The call and return actions require storage for the following:
  • Status information about the caller
  • Parameters
  • Return address
  • Functional value for function subprograms These, along with the local variables and the subprogram code, form the complete set of information a subprogram needs to execute and then return control to the caller.

A FORTRAN 77 subprogram consists of two separate parts: the actual code of the subprogram, which is constant and local variables and data listed previously, which can change when the subprogram is executed. Both of these parts have fixed sizes. The format, or layout, of the non code part of a subprogram is called an activation record, because the data it describes are only relevant during the activation of the subprogram. An activation record instance is a concrete example of an activation record, a collection of data in the form of an activation record FORTRAN 77 activation records is shown in below figure

Because an activation record instance for a FORTRAN 77 subprogram has fixed size, it can be statically allocated. Below figure shows a FORTRAN 77 program consisting of COMMON storage, a main program, and three subprograms: A, B and C

The construction of the complete FORTRAN 77 program shown in the above figure is not done entirely by the compiler. In fact, because of independent compilation, the four program units – MAIN, A, B and C – may have been compiled on different days, or even in different years. At the time each unit is compiled, the machine code for it, along with a list of references to external subprograms and COMMON variables within the code, is written to a file. The executable program shown in the above is put together by the linker, which is part of the operating system.

  • The static link, which is sometimes called a static scope pointer, points to the bottom of the activation record instance of an activation of the static parent. It is used for accesses to non local variables.
  • The dynamic link is a pointer to the top of an instance of the activation record of the caller.

Consider the following skeletal Pascal procedure.

procedure sub(var total: real; part : integer); var list : array [ 1..5] of integer; sum : real; begin … end;

The activation record for sub is shown in the Figure

A subprogram is active from the time it is called until the time that execution is completed. At the time it becomes inactive, its local scope ceases to exist and its referencing environment is no longer meaningful. So at that time, its activation record instance is destroyed. An Example without Recursion and Non local References

Consider the following skeletal example program

The sequence of procedure calls in this program is Main calls fun fun2 calls fun

fun1 calls fun

The stack contents for the points labeled 1, 2 and 3 are shown in the Figure

At point1, only the activation record instances for program MAIN_1 and procedure B are on the stack. When B calls A, an instance of A’s activation record is created on the stack. When A calls C, an instance of C’s activation record is created on the stack. When C’s execution ends, the instance of its activation record is removed from the stack, and the dynamic link is used to reset the stack top pointer. A similar process takes place when procedures A and B terminate. After the return from the call to B from MAIN_1, the stack has only the instance of the activation record of MAIN_ The collection of dynamic links present in the stack at a given time is called the dynamic chain, or call chain. References to local variables can be represented in the code as offsets from the beginning of the activation record of the local scope. Such an offset is called a local offset.

Recursion

Consider the following example C program, which uses recursion to compute the factorial function: int factorial (int n) { 1 if ( n <= 1) return 1;

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. That instance’s static link points, if there is one. 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. Let static_depth be an integer associated with a static scope that indicates how deeply it is nested in the outermost scope. A Pascal 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 2. The length of the static chain needed to reach the correct activation record instance for a non-local reference to a variable X is exactly the difference between 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. For example, consider the following skeletal program:

program A; procedure B; procedure C; … end; {C} … end; {B} … end; {A}

The static_depths of A, B and C are 0, 1 and 2 respectively. If procedure C references a variable declared in A, the chain_offset of that reference would be 2(static_depth of C minus the static_depth of A). if procedure C references a variable declared in B, the chain_offset of that reference would b 1. To illustrate the complete process of nonlocal accesses, consider the following skeletal program in Pascal. program MAIN_2; var X : integer; procedure BIGSUB; var A, B, C : integer; procedure SUB1; var A, D : integer; begin {SUB1} A := B+C; 1 … end ; {SUB1} procedure SUB2(X : integer); var B, E : integer; procedure SUB3; var C, E : integer; begin {SUB3} … SUB1; … E := B + A; 2 end ; {SUB3} begin {SUB2} … SUB3; … A :+ D + E; 3 end ; {SUB2}

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 SUB2 has one parameter. The reference to the variable A is to the A declared in BIGSUB, because neither SUB nor its static parent SUB2 has a declaration for a variable named A. it is referenced with the pair (2,3). 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). In summary, the references to the variable A at points 1, 2 and 3, would be represented by the following points (0,3) (local) (2,3) (two levels away) (1,3) (one level away)