0% found this document useful (0 votes)
941 views11 pages

Nested Subprograms

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 11

Nested Subprograms

• Some non-C-based static-scoped languages (e.g.,


Fortran, Ada, Python, JavaScript, Ruby) use stack-
dynamic local variables and allow subprograms to be
nested
• All variables that can be non-locally accessed reside in
some activation record instance in the stack
The process of locating a non-local reference:
1. Find the correct activation record instance
2. Determine the correct offset within that activation record
instance

BITS Pilani, Hyderabad


Pilani Campus
Campus
Locating a Non-local Reference
Finding the offset is easy
Finding the correct activation record instance
– Static semantic rules guarantee that all non-local variables that can be
referenced have been allocated in some activation record instance that is on the
stack when the reference is made

BITS Pilani, Hyderabad


Pilani Campus
Campus
Static Scoping
• A static chain is a chain of static links that connects certain activation record
instances

• The static link (access link) in an activation record instance for subprogram
A points to the activation record instance of A's static parent.

• The static chain from an activation record instance connects it to all of its
static ancestors.

• We can find the correct ARI for a non-local variable using static links: When
a reference to a non-local variable is made, the ARI containing the variable
can be found by searching the static chain until a static ancestor ARI is
found that contains the variable.
• Also, since the scoping is static, the compiler can identify the non-local
variables and the length of the static chain to be followed to reach the ARI of
the referenced non-local variable.

BITS Pilani, Hyderabad


Pilani Campus
Campus
Static Scoping (continued)
• Static_depth is an integer associated with a static scope whose
value is the depth of nesting of that scope

• The length of the static chain needed to reach the correct ARI of the
non-local variable is given by the chain_offset or nesting_depth. It is
defined as the difference between the static_depth of the
subprogram containing the reference and the static depth of the
subprogram containing the declaration of that non-local variable.

• A reference to a variable can be represented by the pair:


(chain_offset, local_offset),
where local_offset is the offset in the activation record of the variable
being referenced and chain_offset is the no. of links to the correct
ARI.

BITS Pilani, Hyderabad


Pilani Campus
Campus
Example Ada Program

BITS Pilani, Hyderabad


Pilani Campus
Campus
Example Ada Program (continued)
Call sequence for Main_2

Main_2 calls Bigsub


Bigsub calls Sub2
Sub2 calls Sub3
Sub3 calls Sub1

BITS Pilani, Hyderabad


Pilani Campus
Campus
Stack Contents

BITS Pilani, Hyderabad


Pilani Campus
Campus
(Contd..)

Position 1 in sub1:
A: (0,3) [A is local variable]
B: (1,4)
C: (1,5)
Position 2 in sub3:
E: (0,4)
B: (1,4)
A: (2,3)
Position 3 in sub2:
A: (1,3)
D: Static semantic error
E: (0, 5)

BITS Pilani, Hyderabad


Pilani Campus
Campus
Example: Static & Dynamic
Scoping
main()
{
P()
{
int x;
x=1;
Q()
{ Output:
x=x+5; Static scoping: 6 11 17
printf(“%d”, x);
} Dynamic scoping: 6 17 22
R()
{
int x;
x=2;
x=x+10;
Q();
x=x+5;
printf(“%d”, x);
}
Q();
R();
}
P();
}
BITS Pilani, Hyderabad
Pilani Campus
Campus
Implementing Dynamic Scoping
non-local references are found by searching the activation
record instances on the dynamic chain
- Length of the chain cannot be statically
determined

BITS Pilani, Hyderabad


Pilani Campus
Campus
Summary
• Subprograms with stack-dynamic local variables and
nested subprograms have two components
– actual code
– activation record
• Activation record instances contain formal parameters
and local variables among other things
• Static chains are the primary method of implementing
accesses to non-local variables in static-scoped
languages with nested subprograms
• Access to non-local variables in dynamic-scoped
languages can be implemented by use of the dynamic
chain.

BITS Pilani, Hyderabad


Pilani Campus
Campus

You might also like