Title: Implementing Subprograms
1Implementing Subprograms
2Subprogram linkage refers to the subprogram call
and return operations of a programming language.
3- The execution of a called subprogram
- requires many associated actions.
- parameter passing
- storage allocation of local variables
- saving execution status of calling program
- transfer of control to the subprogram
- return to calling program
- access to non local variables
4Subprogram Activation
The subprogram definition is written by the
programmer. The definition is a static entity.
During the execution of the program, if the
subprogram is called, a subprogram activation is
created.
The definition serves as a template for the
subprogram activation.
5Subprogram Activation
A subprogram activation has a code segment and an
activation record.
The code segment is statically created and
invariant during execution. The code is shared
by all activations.
The activation record is dynamically created when
the subprogram is called. It is destroyed when
execution of the subprogram terminates.
6Subprogram Activation
In programming languages which do not allow
recursion, there can be only one activation of a
given subprogram at any time.
FORTRAN 77 Activation Record
7FORTRAN 77 Code and Activation Records
Common storage
Local variables
MAIN
Local variables
Data
A
Parameters
Return Address
Local variables
B
Parameters
Return Address
MAIN
A
Code
B
8FORTRAN 77 Code and Activation Records
Common storage
Code
MAIN
Local variables
Code
Local variables
A
Parameters
Return Address
Code
Local variables
B
Parameters
Return Address
9Implementing Subprograms in ALGOL-like Languages
In ALGOL-like programming languages (i.e.
Pascal, C) subprogram linkage is more complex
- Different parameter passing methods
- Local variables of subprograms are often
dynamically allocated
- Recursion makes multiple activations of a
single subprogram possible
- Implementation of static scoping
10ALGOL-like Subprogram Activation Record
In programming languages which do allow
recursion, there can be many activations of a
given subprogram at any time.
11Activation Record for Pascal Procedure
procedure sub(var total real part
integer) var list array 1..5 of integer
sum real begin end
12Static and Dynamic Links
The static link points to the bottom of the
activation record instance of an activation of
the static parent. It is used for references to
non-local variables.
The dynamic link points to the top of the
activation record instance of the caller. It is
used for destruction of the current activation
record instance.
13(No Transcript)
14(No Transcript)
15(No Transcript)
16(No Transcript)
17(No Transcript)
18(No Transcript)
19(No Transcript)
20(No Transcript)
21Implementing Nonlocal References
Nonlocal variable references occur in two
steps 1) find the activation record where the
nonlocal variable was allocated 2) use the
offset of the variable within that
activation record to access it
22Implementing Nonlocal References
Static Chain
Display
23Implementing Nonlocal References
Static Chain - a static chain is a chain of
static links that connects certain activation
record instances in the stack
24(No Transcript)
25main
A
C
B
Main calls B B calls A A calls C
26Implementing Nonlocal References
Display - the static links are collected in a
single array called a display
27Main - 0
A - 1
C - 2
B - 1
Main calls B B calls A A calls C
28Main - 0
A - 1
C - 2
B - 1
ARI for B
1
ARI for MAIN
Main calls B B calls A A calls C
0
stack
display
29Main - 0
A - 1
C - 2
B - 1
ARI for A
ARI for B
1
ARI for MAIN
Main calls B B calls A A calls C
0
stack
display
30Main - 0
A - 1
C - 2
ARI for C
B - 1
ARI for A
ARI for B
2
1
ARI for MAIN
Main calls B B calls A A calls C
0
stack
display