CmSc 315 DM Programming Languages
Chapter 9: Subprogram Control
Subprogram control: interaction among subprograms and how subprograms manage to pass data among themselves in a structured and efficient manner.
Terminology:
Simple subprogram call return
Copy rule view of subprograms: the effect of a call statement is the same as ifImplicit assumptions present in this view :
Execution of subprograms
Outline of the method:
The definition is translated into a template, used to
create an activation
each time a subprogram is called.
Execution is implemented by the use of two system-defined pointers:

On call instruction:
On return
Restrictions of the model: at most one activation of any subprogram
The simplest implementation: to allocate storage for each activation as an
extension of the code segment. Used in FORTRAN and COBOL.
The activation record is not destroyed - only reinitialized for each subprogram execution.
Hardware support - CIP is the program counter, CEP is not used, simple jump executed on return.
Stack-based implementation - the simplest run-time storage management technique
call statements : push CIP and CEP
return statements : pop CIP and CEP off of the stack.
Used in most C implementations
LISP: uses the stack as an environment.
Specification
Syntactically - no difference
Semantically - multiple activations of the same subprogram exist
simultaneously at some point in the execution.
Implementation
Stack-based - CIP and CEP are stored in stack, forming a dynamic chain of links.
Some language compilers (C, Pascal) always assume recursive structure
of subprograms,
while in others non-recursive subprograms
are implemented in the simple way.
Data control features: determine the accessibility of data at different points during program execution.
Central problem: the meaning of variable names,
i.e. the correspondence between names and memory locations.
Two ways to make a data object available as an operand for an operation.
Example: x = y + 2*z;
The result of multiplication is transmitted directly as an operand of the addition operation.
1. 1. Program elements that may be named
1. Variables
2. Formal parameters
3. Subprograms
4. Defined types
5. Defined constants
6. Labels
7. Exception names
8. Primitive operations
9. Literal constants
Names from 4 thru 9 - resolved at translation time.
Names 1 thru 3 - discussed below.
Simple names: identifiers, e.g. var1.
Composite names: names for data structure components, e.g.
student[4].last_name.
1. 2. Associations and Referencing Environments
Association: binding identifiers to particular data objects and
subprograms
Referencing environment: the set of identifier associations for a
given subprogram.
Referencing operations during program execution: determine
the particular data object
or subprogram associated
with an identifier.
Local referencing environment:
The set of associations created on entry to a subprogram that represent formal parameters, local variables, and subprograms defined only within that subprogram
Nonlocal referencing environment:
Global referencing environment: associations created at the
start of execution
of the main program, available to be used in a
subprogram,
Predefined referencing environments: predefined association in the language definition.
Visibility of associations
Dynamic scope of associations
1. 3. Aliases for data objects: Multiple names of a data object
Problems with aliasing
The dynamic scope of an association for an identifier is that set of subprogram activations in which the association is visible during execution.
Dynamic scope rules
The static scope of a declaration is that part of the program text where a use of the identifier is a reference to that particular declaration of the identifier.
Static scope rules
Importance of static scope rules - recording information about a variable during translation.
Block-structured languages (Pascal):
Static scope rules for block-structured programs

Local environment of a subprogram: various identifiers declared in
the subprogram -
variable names, parameters, subprogram names.
Static scope rules: implemented by means of a
table of the local declarations
Dynamic scope rules: two methods:
Implementation of dynamic scope rules in local referencing environments:
by means of a local environment table to associate names, types and
values.
Exam-like questions