Professional Documents
Culture Documents
Chapter 7
1
Control
Simple subprogram call-
return
Assumptions
Subprograms cannot be recursive.
Explicit call statements are required.
Not in exception handlers.
Subprograms must execute completely at each
call .
Not like a coroutine which continues execution
from the point of its last termination each time it is
called.
2
Immediate transfer of control at point of
call.
Not like a scheduled subprogram call,
execution of the subprogram may be deferred
until some later time.
Single execution sequence.
Not like tasks which may execute concurrently
so that several are in execution at once.
3
Functions calls
subprograms that return values directly.
procedure or subroutine calls
operate only through side effects on shared
data.
7
One important property of the copy-rule view
of subprograms: at most one activation of
any subprogram is in use at any point during
program execution.
FORTRAN, COBOL.
static allocation, code segment extension .
CIP is enough.
P.292, fig. 7.2.
8
Recursive Subprograms
9
The dynamic chain of pointers.
Ep values : a linked list , links the activation
records on the central stack in order of their
dynamic creation.
CEP: the top A.R.
ep value of CEPs return point: the 2nd A.R.
…
last ep: the main program.
10
7.2. Attributes of Data
Control
11
Names and Referencing
Environments
Two ways that a data object can be made available
as an operand for an operation:
Direct transmission.
Is used for data control within expressions.
Data object is allocated storage temporarily during its lifetime,
without any name.
Like 2*Z in X:=Y+2*Z.
Referencing through a named data object.
Most data controls outside of expressions .
A name ; or a name of a larger data object with a selection
operation.
12
Program elements that
may be named
Variable names. *
Formal parameter names. *
Subprogram names. *
Names for defined types.
Names for defined constants.
Statement labels (names for statements).
Exception names.
Names for primitive operations, e.g., + , * ,SQRT.
Names for literal constants, e.g., 17, 3.25.
13
Name:
simple name: designating an entire data
structure.
composite name: a name for a component of
a data structure. (a name and a selection)
a[3].class[2].room
14
Associations and
Referencing Environments
15
2. During execution of main program, it
invokes referencing operations, to
determine the particular data object or
subprogram associated with an identifier.
A:=B+FN(C)
17
Referencing Environments
18
Referencing environment
components
19
Visibility : an association for an identifier is
said to be visible within a subprogram if it is
part of the referencing environment for that
subprogram.
Hidden if redefined.
dynamic scope: dynamic scope of an
association consists of the set of subprogram
activations within which it is visible.
20
Referencing operation: an operation with
the signature
ref-op: id*referencing-environment -> data object or subprogram
Local, nonlocal, and global references.
If the association is local ...
21
Aliases for data objects
22
Dynamic Scope
23
Static Scope
24
You can make no use of static scope
rules.
LISP, SNOBOL4
The importance of static scope.
Static type checking: faster and more reliable.
Reading a program easier.
Ada, C, FORTRAN, Pascal
25
Block structure
26
Static scope rules
p.304, 305
1. Head of each block.
2. Immediately enclosed the first block. …up
too the predefined language env., and then an error.
3. Inner ones encapsulate dcl.s
4. Named block can be referenced.
Every reference to a name ,associated to a unique dcl.
27
Local data and local
referencing environment
28
P.310
some points about retention/deletion :
absence of recursion=> implementation is the
same
having both
static and automatic in PL/1
a subprogram name: retained
a formal parameter name: deleted
recursive subprogram calls=> deleted (often)
29
Deletion, Retention
Retention
history sensitive
more storage space
Deletion
less storage space
30
7.3. Shared Data in
Subprograms
31
Parameter and Parameter
Transmission
Parameters for
data
subprograms
statement labels
p.313, a table of different kinds of actual parameters
32
Establishing the correspondence
positional (pairing)
by explicit name
in Ada: Sub(Y=>B, X=>27);
33
Methods for Transmitting
Parameters
Call by name
call by reference
call by value
call by value-result
call by constant value
call by result
34
Call by name
35
Call by reference
36
Call by value
37
Call by value-result
38
Call by constant value
39
Call by result
40
FORTRAN: only call by ref.
Pascal : both (var keyword)
C: only call by value.
By pointers…
sub(&I)
...
sub(int *x)
41
Transmission Semantics
Rather than mode of transmission, role of the
parameter.
in, out, in out
In Ada:
elementary data types
in: constant value
out, in out: value-result
composite data types : by reference
42
Explicit function values
In C: return 2*x
In Pascal: fn:=2*x
43
Implementation of
parameter transmission
In activation record.
Each formal par. P is a local data object.
P
a local data object of type T (type of actual
par.).
a pointer to a data object of type T .
44
Various actions
at the point of call : actual par.s evaluated.
at the point of entry and exit:
prologue: complete the transmission.
epilogue: copy the result values (in transmissions by
result or value-result).
Compiler:
transmission of par.s.
static type checking.
45
Parameter-Transmission
examples
P.320
46