Nested Subprograms
Nested Subprograms
Nested Subprograms
• 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.
• 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.
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)