
CS 476, Prog. Lang. Design/ page 1
Ch.9: Implementing Subprograms
I. General Semantics of Calls and Returns
A. What the caller of a subprogram must do:
1. Include the mechanism for the parameter-passing method used
2. Allocate storage on the stack for non-static local variables
3. Save the execution status of the calling program unit
4. Transfer control to called program and back again at end of subprogram
5. Provide a mechanismto access non-local variables if static scoping is used
B. What the called subprogram must do:
1. Copy values of out-mode parameters on the return
2. Deallocate storage used for local variables (move the return address and adjust stack pointer)
3. Return control to the calling program
II. Implementing FORTRAN 77 Subprograms
A. Simplifying characteristics:
1. Non-local variables accessed through COMMON
2. No recursion
3. Subprogram variables statically declared
B. Semantics of a subprogram call
1. Save the execution status of the current program unit (Registers)
2. Carry out the parameter-passing process
3. Push the return address on the run-time stack
4. Transfer control to the callee (Load subprogram starting address into PC)
C. Semantics of a subprogram return:
1. Copy parameter values back to caller if pass-by-value-result parameters are used
2. If the subprogram is a function, make the resulting functional value accessible to caller
3. Restore callerÕs execution status (Registers)
4. Transfer control back to the caller (move the return address into the PC)
D. To implement subroutine calls & returns, storage on the stack is required for caller status (reg-
isters), parameters, return address, function result value. The layout for these items is called
an activation record, since the data items are relevant during the activation of a subprogram.
E. When a program runs, there could be multiple instances of the activation record for a subpro-
gram (e.g. recursion), though not in FORTRAN 77
F. FORTRAN 77 example: [See overhead]
1. Since there can only be one active version of a subprogram at a time, the activation records for
all the functions can be allocated in memory statically.
2. Pieces can be compiled separately and put together by a linker/loader in order to run it.
III. Implementing Subprograms in ALGOL-like Languages
A. More complex than in FORTRAN 77 because:
1. Parameters can be passed by reference, not just value
2. Subprogram local variables may be dynamically allocated
3. Recursion introduces the possibility of multiple activation records for a single subprogramat one
time.
4. Static scoping used to access nonlocal variables
B. A typical activation record now includes:
1. static link: points to the bottom of the activation record instance of an activation of the static par-
ent. It is used to access non-local variables
2. dynamic link: points to the top of an instance of the activation record of thecaller. This is used in
the destruction of the current activation record instance when the subprogram is completed, as
there may have been other allocations on the stack.