You are on page 1of 16

Semantics of Subprogram Calls

and Returns

Implementing Subprograms &


Blocks

CS516 1 CS516 2

Subprogram Calls Subprogram Returns

• Pass parameters using parameter passing • Copy back using parameter passing methods if
methods. needed.
• Allocate storage space for local variables. • Deallocate the storage used for locals.
• Arrange to access nonlocal variables. • Restore the execution status of the caller.
• Save the execution status of the caller. • Return control to the caller.
• Save the return address.
• Transfer control to the callee.

CS516 3 CS516 4

Info Needed for Subprogram Calls Info Needed for Subprogram Calls
and Returns and Returns

• Certain information must be available: • The code for the subprogram


– The code for the subprogram – Fixed
– The state while the body of the subprogram is • The state while the body of the subprogram is
executing. executing.
• Instruction part
– Changing
– A pointer to the instruction to be executed after the subprogram
returns (Return address) – Different calls to the same subprogram will have
• Environment part different states!
– The values of locals, nonlocals and parameters.

CS516 5 CS516 6

1
Subprogram, Call, Activation &
Activation Record (AR)
Activation Record

• The state info need for a subprogram call and • A subprogram


return is stored in an activation record (AR). • A call to the subprogram
• In an activation record: • An activation of the call
– Instruction part • An activation record for the activation
• A pointer to the instruction to be executed after the
subprogram return (Return address)
– Environment part
• The values of locals, nonlocals and parameters.

CS516 7 CS516 8

Storage for Activation Records Two Types of Languages

• Where do we allocate storage for the activation • FORTRAN-like languages


records? – No recursive subprograms
– It depends! Static – Static local variables
allocate
– No nonlocal variables (Flat block structure)
lifetime Stack • Algol-like languages
deallocate – Recursive subprograms
– Stack-dynamic local variables
– Nonlocal variables (Nested block structure)
Heap
CS516 9 CS516 10

Implementing Subprogram Calls


Storage for Activation Records
and Returns

• FORTRAN-like languages • It depends on the type of the language!


– From static storage
• Algol-like languages
– From stack storage

CS516 11 CS516 12

2
Implementing Subprogram Calls
Two Types of Languages
and Returns

• FORTRAN-like languages • FORTRAN-like languages


– No recursive subprograms – Relatively simple!
– Static local variables • Algol-like languages
– No nonlocal variables (Flat block structure) – More difficult!
• Algol-like languages
– Recursive subprograms
– Stack-dynamic local variables
– Nonlocal variables (Nested block structure)
CS516 13 CS516 14

1. Implementing FORTRAN77-
Call Semantics
like Subprograms

1. Save the execution status of the caller.


2. Carry out the parameter-passing process.
3. Pass the return address.
4. Transfer control to the callee.

CS516 15 CS516 16

Return Semantics Required Storage

1. If pass-by-value-result parameters are used, • Status information of the caller


move the current values of those parameters • Parameters
to their corresponding actual parameters. • Return address
2. If it is a function, move the functional value to • Functional value (if it is a function)
a place the caller can get it.
• Local variables
3. Restore the execution status of the caller. • The subprogram code
4. Transfer control back to the caller.

CS516 17 CS516 18

3
Activation Record Activation Record Instance

• The format or layout of the noncode part is • An activation record instance is


called an activation record. – A concrete example of an activation record.
– The collection of data for a particular subprogram
Functional Value activation (call).
Local variables
Parameters A subprogram call An activation record instance
Return Address

CS516 19 CS516 20

Static Allocation for Activation Static Allocation for Activation


Record Record

• A FORTRAN 77 subprogram can have only • Statically allocate storage for Activation
one activation record instance at any given Record.
time! • Use it for each activation record instance.
• Why?
Functional Value
– No recursive subprogram!
Local variables
Static Storage
Parameters
Return Address
CS516 21 CS516 22

Example: Implementing A 2. Implementing ALGOL-like


FORTRAN 77 Subprogram Subprograms
• A main program MAIN • This is more complicated than implementing
• Three subprograms A, B & C FORTRAN 77-like subprograms.
• The code and activation records: • Why?
– See Figure 10.2 (p. 400) – Local variables are often dynamically allocated.
– Recursion must be supported.
– Static scoping must be supported.
– More parameter passing methods

CS516 23 CS516 24

4
Activation Record Activation Record

• A typical activation record for an ALGOL- • The activation record format is static, but its
like language: size may be dynamic.
• An activation record instance must be created
Local variables
dynamically when a subprogram is called.
Parameters
Dynamic Links
Static Links
Return Address
CS516 25 CS516 26

Dynamic and Static Links Activation Record

• The dynamic link (DL) • An Algol-like subprogram can have more than
– points to the top of an instance of the activation one activation record instance at any given
record of the caller. time!
• The static link (SL) • Why?
– points to the bottom of the activation record – Recursive subprogram!
instance of an activation of the static parent (to be
used for access to nonlocal variables).

CS516 27 CS516 28

Dynamic Allocation for Activation


Example: Activation Record
Record

• Dynamically allocate storage for Activation


Record. procedure sub(var total: real; part: integer);
var list: array[1..2] of integer;
SP (Stack Pointer) sum: real;
Local variables begin
Parameters …
end
Dynamic Links Stack Storage

Static Links
Return Address
FP (Frame Pointer)
CS516 29 CS516 30

5
(1) Without Recursion and
Example: Activation Record
Nonlocal References
sum Local
list[3] Local
list[2] Local
list[1] Local
part Parameter
total Parameter
DL
SL
CS516 RA 31 CS516 32

Example Dynamic Chain


void fun1(int x) {
int y;
... <----------------------------2 • A dynamic link is a pointer to the AR of the
fun3(y);
... Call sequence: caller.
main calls fun2
}
void fun2(float r) { fun2 calls fun1 – Why?
int s, t; fun1 calls fun3
... <--------------------------1 • A dynamic chain is a sequence of dynamic
fun1(s);
...
links.
} Stack contents: • The dynamic chain is a list of all AR’s on the
void fun3(int q) {
... <--------------------------3
...
stack, i.e., all active subprograms.
}
Void main() {
float p; See FIGURE 10.5 (p. 405)
fun2(p);
}
CS516 33 CS516 34

Local Variables (2) With Recursion

• 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 at compile time by the compiler.

CS516 35 CS516 36

6
Example: Recursive Functions (3) With Nonlocal References
int factorial(int n) {
<-----------------------------1
if (n <= 1)
return 1;
else return (n * factorial(n - 1));
<-----------------------------2
}
void main() {
int value;
value = factorial(3);
<-----------------------------3
}

Stack contents:

CS516 See FIGUREs 10.7 and 10.8 (p. 407 and p. 408) 37 CS516 38

Rules for Nonlocal References The Scope Rule

1. Static scoping rule • The scope rule of a programming language


2. Dynamic scoping rule determines
– How a particular occurrence of a name (variable) is
associated with a variable.
• Given an applied (use, reference) occurrence of
a variable x, what is the binding (defining,
declaration) occurrence of the variable x?

CS516 39 CS516 40

The Static Scoping Rule The Static Scoping Rule

• Based on program text. • Search declarations, first locally, then in


• Just by examining the program text, we can increasingly larger enclosing scopes, until one
is found for the given name.
determine which binding occurrence
correspond to a given applied occurrence. • Find the innermost enclosing block containing
the applied occurrence and a binding
• The binding between applied occurrences and occurrence.
binding occurrences is FIXED, not changing
throughout the program’s execution.

CS516 41 CS516 42

7
Nonlocal References with Static
Static-Scoped Languages
Scoping Rule

• A subprogram is callable only when all of its • Observation:


static ancestor program units are active! – All variables that can be nonlocally accessed
• In a given subprogram, only variables declared reside in some activation record instance in the
in the static ancestor scopes are visible and can stack.
be accessed. • The process of locating a nonlocal reference:
• Activation record instances of all of the static 1. Find the correct activation record instance in
which the variable is allocated.
ancestors are guaranteed to exist on the stack.
2. Use the local offset within that activation
record instance to access it.
CS516 43 CS516 44

How to Find the Correct Implementing Nonlocal References


Activation Record Instance? with Static Scoping Rule

• Find the innermost enclosing block containing • Using static chains


the applied occurrence and a binding • Using display
occurrence.

CS516 45 CS516 46

1. Static Chain Static Chain

• The static link in an activation record instance • A static chain is a chain of static links.
for a subprogram S points to an activation • The static chain from an activation record
record instances of S's static parent (enclosing instance for a subprogram S links all the static
subprogram). ancestors of S.
– The most recent ARI of the static parent!

CS516 47 CS516 48

8
How to Find the Correct Activation
Static Depth of A Subprogram
Record Instance Using Static Chain?

• To find the declaration for a reference to a • Given a subprogram S,


nonlocal variable? • The static_depth of S is an integer associated
– Search the static chain until the activation record with the subprogram:
instance that contains the variable (as a local – How deeply it is nested in the outmost program!
variable) is found!
– 0 (the outmost), 1, 2, …
• How many static links to be followed? – Also called SNL (Static Nesting Level)
– Can be determined at compile time!

CS516 49 CS516 50

Nesting Depth of A Nonlocal


Example: Static Depth
Reference
program A;
var x: int;
procedure B; • Given a nonlocal reference to a variable X,
procedure C;
… A ----- static_depth = 0
• The nesting_depth or chain_offset of the
x:=x+1; nonlocal reference is
… B ----- static_depth = 1
end;{C} (The static_depth of the the subprogram containing
C ----- static_depth = 2
… the reference to X)
x:=x+1; MINUS

end;{B} (The static_depth of the the subprogram containing
… the declaration for X)
x:=x+1;

– Also called SD (Static Distance)
CS516
end;{A} 51 CS516 52

How to Access Nonlocal Variables


Example: Nesting Depth
Using Static Chain?
program A;
A ----- static_depth = 0
var x: int; B ----- static_depth = 1
procedure B; C ----- static_depth = 2
• A reference to a nonlocal variable X can be
procedure C; represented by the pair (chain_offset,

x:=x+1; local_offset) where
… SD Nesting depth of X in C: 2
– chain_offset = The number of static links to the
end;{C}
… SD Nesting depth of X in B: 1
correct ARI.
x:=x+1; – local_offset = The offset from the beginning of the

end;{B}
SD Nesting depth of X in A: 0 AR of the subprogram containing the declaration
… for X.
x:=x+1;

CS516
end;{A} 53 CS516 54

9
Example: Nonlocal Variable Example
Access Using Static Chain program MAIN_2;
var X : integer;
procedure BIGSUB;
program A; var A, B, C : integer;
A ----- static_depth = 0 procedure SUB1;
var x: int; B ----- static_depth = 1 var A, D : integer;
procedure B; C ----- static_depth = 2 begin { SUB1 }
procedure C; A := B + C; <-----------------------1 Call sequence:
end; { SUB1 }
… Nesting depth of X in C: 2 procedure SUB2(X : integer);
x:=x+1; var B, E : integer; MAIN_2 calls BIGSUB
Nesting depth of X in B: 1
procedure SUB3; BIGSUB calls SUB2
… Nesting depth of X in A: 0 var C, E : integer; SUB2 calls SUB3
end;{C} begin { SUB3 }
SUB3 calls SUB1
SUB1;
… Reference to X in C: (2, local-offset) E := B + A: <--------------------2
x:=x+1; end; { SUB3 }
begin { SUB2 }
… Reference to X in B: (1, local-offset) SUB3;
end;{B} A := D + E; <-----------------------3
end; { SUB2 }
… Reference to X in A: (0, local-offset) begin { BIGSUB }
x:=x+1; SUB2(7);
end; { BIGSUB }
… begin
CS516
end;{A} 55 CS516
BIGSUB; 56
end. { MAIN_2 }

Example Example
program MAIN_2; program MAIN_2;
var X : integer; var X : integer;
procedure BIGSUB; procedure BIGSUB;
var A, B, C : integer; var A, B, C : integer; Nonlocal references:
procedure SUB1; procedure SUB1;
var A, D : integer; var A, D : integer;
begin { SUB1 } begin { SUB1 } At position 1 in SUB1:
A := B + C; <-----------------------1 Stack contents A := B + C; <-----------------------1 A - (0, 3)
end; { SUB1 } end; { SUB1 }
procedure SUB2(X : integer);
at position 1: procedure SUB2(X : integer);
B - (1, 4)
var B, E : integer; var B, E : integer; C - (1, 5)
procedure SUB3; procedure SUB3;
var C, E : integer; var C, E : integer;
begin { SUB3 } begin { SUB3 } At position 2 in SUB3:
SUB1; SUB1; E - (0, 4)
E := B + A: <--------------------2 E := B + A: <--------------------2 B - (1, 4)
end; { SUB3 } end; { SUB3 }
begin { SUB2 } begin { SUB2 } A - (2, 3)
SUB3; See FIGURE 10.9 (p. 414) SUB3;
A := D + E; <-----------------------3 A := D + E; <-----------------------3
end; { SUB2 } end; { SUB2 }
At position 3 in SUB2:
begin { BIGSUB } begin { BIGSUB } A - (1, 3)
SUB2(7); SUB2(7); D - an error
end; { BIGSUB } end; { BIGSUB }
begin begin E - (0, 5)
CS516
BIGSUB; 57 CS516
BIGSUB; 58
end. { MAIN_2 } end. { MAIN_2 }

QUIZ: Static Chain How to Maintain the Static Chain?


program MAIN_2;
var X : integer;
procedure BIGSUB;
var A, B, C : integer;
procedure SUB1;
var A, D : integer;
begin { SUB1 }
• During program execution!
A := B + C; <-----------------------1
end; { SUB1 }
Stack contents?
• At a subprogram call:
procedure SUB2(X : integer);
var B, E : integer;
procedure SUB3;
(1) At position 2 – The static link (SL) must point to the most
var C, E : integer; (2) At position 3 recent ARI of the static parent.
begin { SUB3 }
SUB1;
E := B + A: <--------------------2
end; { SUB3 }
begin { SUB2 }
SUB3;
A := D + E; <-----------------------3
end; { SUB2 }
begin { BIGSUB }
SUB2(7);
end; { BIGSUB }
begin
CS516
BIGSUB; 59 CS516 60
end. { MAIN_2 }

10
Static Chain - Maintenance Static Chain - Maintenance

• Method 1: • Method 2:
– Search the dynamic chain until the first ARI for the – Treat subprogram declarations and calls like
static parent is found. variable declarations and references.
– Easy, but slow.

CS516 61 CS516 62

Example: Static Chain -


Static Chain - Maintenance program MAIN_2;
var X : integer;
Maintenance
procedure BIGSUB;
var A, B, C : integer; • At the call to SUB1
procedure SUB1; in SUB3, this
• Given a subprogram call to S: var A, D : integer;
begin { SUB1 } nesting-depth is 2,
A := B + C; <-----------------------1 which is sent to
– Have the compiler compute the nesting depth end; { SUB1 }
procedure SUB2(X : integer); SUB1 with the call.
between the caller and the subprogram that declared var B, E : integer;
procedure SUB3; • The static link in
S. var C, E : integer; the new ARI for
begin { SUB3 }
– Store this nesting depth and send it with the call. SUB1; SUB1 is set to point
E := B + A: <--------------------2 to the ARI that is
– The SL of the S’ ARI is determined by moving end; { SUB3 }
begin { SUB2 } pointed to by the
down the static chain of the caller the number of SUB3;
A := D + E; <-----------------------3
second static link in
static links equal to the nesting depth. end; { SUB2 } the static chain
begin { BIGSUB } from the ARI for
SUB2(7);
end; { BIGSUB } SUB3.
begin
CS516 63 CS516
BIGSUB; 64
end. { MAIN_2 }

Static Chain - Evaluation 2. Display

• A nonlocal reference is slow. • The idea:


– (Nesting-Depth or SD + 1) memory references! – Put the static links in an array called a display.
• It is difficult to estimate the costs of nonlocal – Rather than being stored in the activation records.
references for time-critical (real-time)
programs.

CS516 65 CS516 66

11
Display program MAIN_3; Example: Display
procedure BIGSUB;
procedure SUB1;

end; {SUB1}
• The display contains a list of pointers to ARIs procedure SUB2;
procedure SUB3;
in the stack. …
end; {SUB3}

– One for each active static depth (static nesting end; {SUB2}
level)! SUB2;
end; {BIGSUB}
BIGSUB;
– Display[i] = The most recent ARI of a subprogram end. {MAIN_3}
with static depth (SNL) i
ARI for SUB2
• There are k+1 entries in the display where k is the static
depth of the currently executing subprogram units. ARI for BIGSUB 2
1
• k=0 is for the main program unit. ARI for MAIN_3 0

Stack Display
CS516 67 CS516 68

How to Access Nonlocal Variables How to Access Nonlocal Variables


Using Display Using Display

• A reference to a nonlocal variable X can be • Use the display_offset to get the pointer to the
represented by the pair (display_offset, correct ARI with the variable.
local_offset) where – Display[display-offset]
– display_offset = The same as chain_offset. • Use the local_offset to get to the variable
– local_offset = The offset from the beginning of the within the ARI.
AR of the subprogram containing the declaration – Two memory references for any nonlocal
for X. reference!

CS516 69 CS516 70

How to Maintain the Display? program MAIN_3; Example: Display


procedure BIGSUB;
procedure SUB1;

end; {SUB1}
• During program execution! procedure SUB2;
procedure SUB3;
MAIN3 calls BIGSUB calls SUB2
• At a subprogram call: …
end; {SUB3}
SUB1;
– Maintain the display condition: end; {SUB2}
– Display[i] = The most recent ARI of a subprogram SUB2;
end; {BIGSUB} Calls SUB1?
with static depth (SNL) i BIGSUB;
end. {MAIN_3}
• At a subprogram return:
ARI for SUB2
– Maintain the display condition:
ARI for BIGSUB 2
– Display[i] = The most recent ARI of a subprogram 1
ARI for MAIN_3 0
with static depth (SNL) i
Stack Display
CS516 71 CS516 72

12
program MAIN_3; program MAIN_3;
procedure BIGSUB;
procedure SUB1;
Example: Display procedure BIGSUB;
procedure SUB1;
Example: Display
… …
end; {SUB1} end; {SUB1}
procedure SUB2; procedure SUB2;
procedure SUB3; procedure SUB3;
… … MAIN3 calls BIGSUB calls SUB2
end; {SUB3} end; {SUB3}
SUB1; SUB1; calls SUB1
end; {SUB2} MAIN3 calls BIGSUB calls SUB2 end; {SUB2}
SUB2; SUB2;
end; {BIGSUB} calls SUB1 end; {BIGSUB}
BIGSUB; BIGSUB; Returns from SUB1?
end. {MAIN_3} end. {MAIN_3}

ARI for SUB1 ARI for SUB1

ARI for SUB2 ARI for SUB2

ARI for BIGSUB 2 ARI for BIGSUB 2


1 1
ARI for MAIN_3 0 ARI for MAIN_3 0

Stack Display Stack Display


CS516 73 CS516 74

Display - Maintenance QUIZ: Display?


program A;
procedure B;
• At a call to subprogram P with static_depth k: procedure C;
B;
– Save in the new ARI for P a copy of the pointer end; {C}
stored at position k in the display. C; A calls B calls C
end; {B}
– Put the link to the ARI for P at position k in the B;
end. {A}
display.
• At an exit:
ARI for C
– Move the saved display pointer from the ARI for P
ARI for B 2
back into the display at position k. 1
ARI for A 0

Stack Display
CS516 75 CS516 76

QUIZ: Display? QUIZ: Display?


program A; program A;
procedure B; procedure B;
procedure C; A calls B calls C calls B procedure C; A calls B calls C calls B
B; B;
end; {C} end; {C}
C; C;
end; {B} end; {B}
B; B;
end. {A} end. {A} Returns from B?

ARI for B

ARI for C ARI for C

ARI for B 2 ARI for B 2


1 1
ARI for A 0 ARI for A 0

Stack Display Stack Display


CS516 77 CS516 78

13
Display Static Chain vs Display Methods

• The display can also be kept in registers if there • References to locals


are enough. – Not much difference
– It speeds up access and maintenance. • References to nonlocals
– If it is one level away, they are equal.
– If it is farther away, the display is faster.
– Display is better for time-critical code, because all
nonlocal references cost the same.

CS516 79 CS516 80

Static Chain vs Display Methods Static Chain vs Display Methods

• Procedure calls • Static chain is better:


– For one or two levels of depth, static chain is faster. – If …
– Otherwise, the display is faster. • Display is better:
• Procedure returns – If …
– Both have fixed time, but the static chain is slightly
faster.

CS516 81 CS516 82

QUIZ: Static Chain vs Display Implementing Dynamic Scoping


program MAIN_2;
var X : integer;
procedure BIGSUB;
var A, B, C : integer;
procedure SUB1;
Call sequence:
var A, D : integer;
begin { SUB1 } MAIN_2 calls BIGSUB
A := B + C; <-----------------------1 BIGSUB calls SUB2
end; { SUB1 }
SUB2 calls SUB3
procedure SUB2(X : integer);
var B, E : integer; SUB3 calls SUB1
procedure SUB3;
var C, E : integer;
begin { SUB3 }
SUB1;
E := B + A:
end; { SUB3 } (1) Static chain?
begin { SUB2 } (2) Display?
SUB3;
A := D + E;
end; { SUB2 }
begin { BIGSUB }
SUB2(7);
end; { BIGSUB }
begin
CS516
BIGSUB; 83 CS516 84
end. { MAIN_2 }

14
The Dynamic Scoping Rule The Dynamic Scoping Rule

• Based on calling sequences of program units. • References to variables are connected to


(The program’s dynamic flow of control) declarations by searching back through the
• Not based on their textual layout (temporal chain of subprogram calls that forced execution
versus spatial). to this point.
• Find the most recently active block containing
the applied occurrence and a binding
occurrence.

CS516 85 CS516 86

Implementing Dynamic Scoping Deep Access

1. Using Deep Access • Follow the dynamic chain!


2. Using Shallow Access – No need to maintain static links.
• Nonlocal references are found by searching all
the activation record instances on the stack
using the dynamic chain.

CS516 87 CS516 88

Deep Access vs Static Chain 2. Shallow Access

• The deep access method: • Put locals in a central place


– The length of chain cannot be statically • Method:
determined. – One stack for each variable name.
– Every activation record instance must store the – See Figure 10.12 (p. 422).
names of variables.

CS516 89 CS516 90

15
Deep Access vs Shallow Access Implementing Blocks

• Deep access:
– Slow access
– Fast calls and returns
• Shallow access:
– Fast access
– Slow calls and returns

CS516 91 CS516 92

Blocks Implementing Blocks

• Blocks are entered and exited in strictly textual 1. Treat blocks as parameterless subprograms
order. – Use activation records and static chains or display.
– No calls to blocks! 2. Allocate locals on top of the ARI of the
• Blocks can be treated as parameterless subprogram that contains the block.
subprograms that are always called from the – Must use a different method to access locals.
same place in the program. – A little more work for the compiler writer.

CS516 93 CS516 94

Example: Blocks
main() {
int x, y, z; e
while (…) { d
int a, b, c;
… b&g
while (…) { a&f
int d, e;
… z
} y
}
while (…) { x
int f, g;
… ARI for main
}

CS516
} 95

16

You might also like