10.3 Implementing Subprograms with Stack-Dynamic Local Variables 445
memory with the code for MAIN. Then, it had to patch in the target addresses for
all calls to A, B, C, and any library subprograms in A, B, C, and MAIN.
10.3 Implementing Subprograms with Stack-Dynamic Local Variables
We now examine the implementation of the subprogram linkage in languages in
which locals are stack dynamic, again focusing on the call and return operations.
One of the most important advantages of stack-dynamic local variables
is support for recursion. Therefore, languages that use stack-dynamic local
variables also support recursion.
A discussion of the additional complexity required when subprograms can
be nested is postponed until Section 10.4.
10.3.1 More Complex Activation Records
Subprogram linkage in languages that use stack-dynamic local variables are
more complex than the linkage of simple subprograms for the following reasons:
- The compiler must generate code to cause the implicit allocation and deal-
location of local variables.
Figure 10.2
The code and
activation records of
a program with simple
subprograms
MAIN
Data
Code
A
B
C
MAIN
A
B
C
Local variables
Local variables
Parameters
Return address
Local variables
Parameters
Return address
Local variables
Parameters
Return address