LOCAL DATA & LOCAL REFERENCING ENVIRONMENTS Local environment of a subprogram subprogram consists of identifiers, variable variable names, subprogram subprogram names . Example: The local environment environment of the t he subprogram Q contains contains the identifiers, variable variable names n ames formal parameters.
EX : FOR LOCAL REFERENCING ENVIROMENT
procedure R; end; procedure Q; var X; integer=30; begin write (X) R; X:=X+1; end; procedure P; Q; end;
For local environments, there are static scope rules & dynamic scope rules. Static scope rules: Specifies
that a reference to an identifier X in the body of the subprogram Q is related to the local declaration for X in the head of the program. Dynamic scope rule:
Specifies
that a reference to X during execution of Q refer to the association for X in the current activation of Q.
IMPLEMENTATION OF STATIC SCOPE RULES The compiler maintains a table of the local declaration for the identifiers that appear in the head of Q On compiling the body of Q, compiler refers to local declaration table whenever the declaration of an identifier is required. Local environment table consists of pairs . Each pair containing an identifier and associated data objects Each data object is represented as a type and its location in the memory.
procedure SUB(X:integer) var Y:real; Z:array [1..3] of real; procedure SUB2; Local environment table in compile time begin NAME TYPE LVALUE CONTENT X integer value parameter end{sub2}; Y real local value begin Z real local array ----------------------descriptor[1,3] end{sub} SUB2
procedure
pointer to code segment
IMPLEMENTATION OF DYNAMIC SCOPE RULES Consider the subprogram P, Q & R. The local variable X is declared in Q. The sequence is: 1. When P is execution, X is not visible to P because x is local to Q. 2. When P calls Q, X becomes visible with initial value 30.Q executes X & prints value 30. 3. When Q calls R, the association of X becomes hidden. 4. R returns control to Q, association of X becomes visible. 5. Q resumes its execution, X is incremented & its new value is printed ie, 31. 6. Q returns its control to P. The association of X is hidden.
EX : FOR LOCAL REFERENCING ENVIROMENT
procedure R; end; procedure Q; var X; integer=30; begin write (X) R; X:=X+1; end; procedure P; Q; end;
We have two different meaning here, Retention: the association of X, is retained until Q is called again. If the association is retained then when Q is called second time, it have the value 31. if it is called third time it will be 32 & so on. Deletion: the association of X is deleted. When Q is called again, a new data object is created and assigned a initial value 30 and association is recreated . Q prints 30 every time Q is executed.
IMPLEMENTATION OF RETENTION A single local environment table containing the retained variables is allocated as a part of the code segment. Code segment is allocated statically and remain throughout the execution. For example-If the initial value of the variable Y is 30, it is stored in the storage allocated. If the local variable is declared at the start of the sub program definition, the compiler can determine the size of the variables & compute the offset of the variable from the start of the code segment
procedure SUB(X:integer) var Y:real; Z:array [1..3] of real; procedure SUB2; begin end{sub2}; begin end{sub}
ALLOCATION AND REFERENCING OF RETAINED LOCAL VARIABLES
EXECUTABLE CODE FOR sub
constants retained value for X retained value for Y retained value for Z
EXECUTABLE CODE FOR sub2
IMPLEMENTATION OF DELETION If the local environment of sub is to be deleted between calls, the local environment table containing the deleted variables is allocated as activation record for sub. If each deleted local variable is declared at the start of the definition of sub, the compiler can determine the number of variables & its size in the local environment table & its offset A current-environment pointer is maintained which points to the base address of the activation record in the stack for the subprogram that is executing currently.
procedure SUB(X:integer) var Y:real; Z:array [1..3] of real; procedure SUB2; begin end{sub2}; begin end{sub}
ALLOCATION AND REFERENCING OF DELETED LOCAL VARIABLES
activation record for main
return point in calling activation record r- value for X r- value for Y r- value for Z
Activation record for subprograms called from sub
CEP
ADVANTAGE 1. Retention approach allows the programmer to write subprograms that are history-sensitive ie, their results are partially determined by the i/p & local data values computed during previous activations. DISADVANTAGE 1. In retention Local environment tables for all subprograms exist throughout execution . wastage of storage space. 2. A variable that must be retained should be declared nonlocal to the program.
ADVANTAGE 1. It provides saving of storage space in that local environment table for those subprograms which are in execution or are suspended. 2. It does not allow local data to be carried over from one call to next .
APPLICATIONS RETENTION : C, java, pascal, Ada, LISP, APL. DELETION : COBOL, some version of FORTRAN.