Local Data & Local Referencing Environments
Local Data & Local Referencing Environments
ENVIRONMENTS
• Local environment of a subprogram consists of
identifiers, variable names, subprogram
names .
• Example:
The local environment of the subprogram Q
contains the identifiers, variable names 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
• 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) ALLOCATION AND REFERENCING
OF RETAINED LOCAL VARIABLES
var Y:real;
Z:array [1..3] of real; EXECUTABLE CODE FOR
sub
procedure SUB2;
begin constants
…
retained value for X
end{sub2};
begin retained value for Y