cmps445 Lecture10
cmps445 Lecture10
SUBPROGRAMS
CHAPTER 10 TOPICS
The General Semantics of Calls and Returns
Implementing “Simple” Subprograms
Implementing Subprograms with Stack-Dynamic Local Variables
Nested Subprograms
Blocks
Implementing Dynamic Scoping
THE GENERAL SEMANTICS OF CALLS AND
RETURNS
Def: The subprogram call and return operations of a language are together
called its subprogram linkage
Call Semantics:
1. Save the execution status of the caller
2. Carry out the parameter-passing process
3. Pass the return address to the callee
4. Transfer control to the callee
IMPLEMENTING “SIMPLE” SUBPROGRAMS
Return Semantics:
1. If pass-by-value-result parameters are used, move the current values of
those parameters to their corresponding actual parameters
2. If it is a function, move the functional value to a place the caller can get it
3. Restore the execution status of the caller
4. Transfer control back to the caller
IMPLEMENTING “SIMPLE” SUBPROGRAMS
Required Storage: Status information of the caller, parameters,
return address, and functional value (if it is a function)
The format, or layout, of the non-code part of an executing
subprogram is called an activation record
An activation record instance is a concrete example of an
activation record (the collection of data for a particular
subprogram activation)
AN ACTIVATION RECORD FOR “SIMPLE”
SUBPROGRAMS
Has fixed size ➔ can be sttically allocated
CODE AND ACTIVATION RECORDS OF A
PROGRAM WITH “SIMPLE” SUBPROGRAMS
IMPLEMENTING SUBPROGRAMS WITH STACK-
DYNAMIC LOCAL VARIABLES
More complicated because:
The compiler must generate code to cause implicit allocation and
deallocation of local variables
Recursion must be supported (adds the possibility of multiple
simultaneous activations of a subprogram)
TYPICAL ACTIVATION RECORD FOR A LANGUAGE
WITH STACK-DYNAMIC LOCAL VARIABLES
IMPLEMENTING SUBPROGRAMS WITH STACK-
DYNAMIC LOCAL VARIABLES
The activation record format is static, but its size may be dynamic
The dynamic link points to the top of the instance of the
activation record of the caller
An activation record instance is dynamically created when a
subprogram is called
AN EXAMPLE C FUNCTION
void sub(float total, int part)
[4]
{ [3]
int list[5];
[2]
[1]
…
}
AN EXAMPLE C PROGRAM WITHOUT RECURSION
void A(int x) {
int y;
...
C(y);
...
}
void B(float r) {
int s, t;
...
A(s);
...
}
void C(int q) {
...
}
void main() {
float p;
...
B(p);
...
}
STACK CONTENTS FOR PROGRAM
Note that:
main calls B
B calls A
A calls C
IMPLEMENTING SUBPROGRAMS WITH STACK-
DYNAMIC LOCAL VARIABLES
The collection of dynamic links in the stack at a given time is called the
dynamic chain, or call chain
Local variables can be accessed by their offset from the beginning of
the activation record. This offset is called the local_offset
The local_offset of a local variable can be determined by the compiler
Assuming all stack positions are the same size, the first local variable declared
has an offset of three plus the number of parameters, e.g., the local_offset of Y in
A is 4
RECURSION
RECURSION
RECURSION
NESTED SUBPROGRAMS
Some non-C-based static-scoped languages (e.g., Fortran 95+,
Ada, Python, JavaScript, Ruby, and Lua) use stack-dynamic local
variables and allow subprograms to be nested
Observation: All variables that can be nonlocally accessed reside
in some activation record instance in the stack
The process of locating a nonlocal reference:
1. Find the correct activation record instance
2. Determine the correct offset within that activation record instance
THE PROCESS OF LOCATING A NONLOCAL
REFERENCE
Finding the offset is easy
Finding the correct activation record instance:
Static semantic rules guarantee that all nonlocal variables that can be
referenced have been allocated in some activation record instance that is
on the stack when the reference is made
STATIC CHAINS
A static chain is a chain of static links that connects certain
activation record instances
The static link in an activation record instance for subprogram A
points to one of the activation record instances of A's static
parent
The static chain from an activation record instance connects it to
all of its static ancestors
STATIC CHAINS (CONTINUED)
To find the declaration for a reference to a nonlocal variable:
You could chase the static chain until the activation record instance (ari)
that has the variable is found, searching each ari as it is found, if variable
names were stored in the ari
Call sequence
STACK
CONTENTS At position 1 in SUB1:
a - (0, 3)
b - (1, 4)
c - (1, 5)
At position 2 in SUB3:
e - (0, 4)
b - (1, 4)
a - (2, 3)
At position 3 in SUB2:
a - (1, 3)
d - an error
e - (0, 5)
BLOCKS
Blocks are user-specified local scopes for variables
An example in C
{
int temp;
temp = list [upper];
list [upper] = list [lower];
list [lower] = temp
}
The lifetime of temp in the above example begins when control enters the
block
An advantage of using a local variable like temp is that it cannot interfere
with any other variable with the same name
BLOCKS
Two Methods:
1. Treat blocks as parameterless subprograms
Every block has an activation record; an instance is created every time
the block is executed
Uses stack chains.