CS5113: Advanced Programming Languages
Week 11
Subprograms
look at their interactions
how they manage to pass data among themselves
Overview
Sequence control
call and return
activation records / IP and EP
how recursion is implemented
-
- Static and dynamic scope (referencing environments)
- Shared data in subprograms
actual and formal parameters
correspondence
methods of transmission
aliasing and parameters
SEQUENCE CONTROL
Start with simple mechanism – uses call and return statement
Programs can be viewed as hierarchies
single main program which may during execution call various subprograms which in turn call each other
each subprogram expected to terminate its execution and return control to where it was called
temporarily halts execution of calling program until it is resumed
Run-time implementation
difference between subprogram definition and subprogram activation
an activation consists of two parts
code segment (invariant)
activation record containing local data, parameters, current instruction pointer (CIP) and current environment pointer (CEP)
created when subprogram is called “activated”
destroyed when subprogram returns “deactivated”
SEE FIGURE FROM PAGE 290 from HANDOUT
– example of static allocation, such as in FORTRAN
SEE FIGURE FROM PAGE 292 from HANDOUT
I5
I6
Example of Dynamic allocation (how recursion works) program main; integer x, y; procedure A (…) integer i; begin
…
x := x + i;
…
I7
I8
I9 end A procedure B (…) boolean x, y; begin
...
A (…)
… end B; I10
I1 begin
I2
….
A(….);
I3 B(…);
….
I4 end main;
Trace utilizing run-time stacks done on board!!
Question: When procedure A is executing and I refer to x – is it the global from main or nonlocal from B?
Answer: This is the difference between static scope and dynamic scope
Out-of-block GOTOs a: begin
…
b: begin
…
goto exit;
…
end;
…
exit: …
end exit visible to block b
if exit b normally, activation record for b is deleted from run-time stack
must do if exit by a jump. So goto’s implementation complicated / number of levels to be considered
#recursions -> #of records discarded/ compiler can’t know beforehand
This is another example of a feature interaction problem : GOTOs with visibility rules
Data Control
accessibility of data at different points during program execution
EXAMPLE: x ;= y + z * z
could be y declared in this subprogram
could be parameter of subprogram
could be declared in main and used globally
Look at scope rules
Referencing environments
data control is concerned with binding of ids to particular objects (data or subprograms)
1.
declarations/ association made ids(names) – bound to variables (memory locations) names – bound to subprogram definitions
2.
referencing operation determine the particular object associated with id during execution
3.
when new subprogram called, new associations made
4.
when it is invoked, it performs referencing operations
5.
when it terminates, associations are destroyed
6.
back in main, execution continues using associations in step 1.
Referencing environments
set of id associatiations that are available for use is termed referencing environment of subprogram
usually reference environment is invariant during execution of subprogram
1.
local environment
formal parms, local vars, and local subprograms
meaning of associations can be determined w/o going outside subprogram activation
2.
non-local environment
set of associations for id used inside a subprogram not created on it’s entry
3.
global
associations created at start of main’s execution used in a subprogram, global part of nonlocal environment
4.
predefined environment
determined directly by language definition (such as predefined constants, built-in procedures, etc.)
Scope and Extent
Scope (of a name) – that part of program text where all uses of the name are the same
Extent – time during execution that the storage used to hold a value is bound to its name
Simplest scope (basic and assembly) – given id always denotes the same variable and all names of variables are known in all program units
Better – restricted range of text, frees programmer from remembering all names (SUPPORTS
REGULARITY AND INFORMATION HIDING PRINCIPLES)
Free variable – use of variable not defined in block
The question of which declaration is used is resolved in a variety of ways:
1) static scoping
2) dynamic scoping
3) and others….
Compiled languages usually use static scoping and check next textually containing block
Static scoping – references during execution related to declarations within text (defined at compile time)
(Examine diagrams in text) dynamic scoping (LISP, APL, SNOBOL)
look at procedures/blocks not yet terminated, i.e., activation records still on run-time stack
scope of association created during execution
examine in reverse order of invocation / or top to bottom on run-time stack
Example: begin
const boolean b := true;
procedure P ( );
begin
write (b); static -> prints true dynamic -> prints false
end;
procedure R ( );
begin
const boolean b := false;
P ( );
end;
R ( ); end; revisit runtime allocation/activation records