Professional Documents
Culture Documents
and Returns
CS516 1 CS516 2
• 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
CS516 5 CS516 6
1
Subprogram, Call, Activation &
Activation Record (AR)
Activation Record
CS516 7 CS516 8
CS516 11 CS516 12
2
Implementing Subprogram Calls
Two Types of Languages
and Returns
1. Implementing FORTRAN77-
Call Semantics
like Subprograms
CS516 15 CS516 16
CS516 17 CS516 18
3
Activation Record Activation Record Instance
CS516 19 CS516 20
• 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
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
• 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
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
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
CS516 39 CS516 40
CS516 41 CS516 42
7
Nonlocal References with Static
Static-Scoped Languages
Scoping Rule
CS516 45 CS516 46
• 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?
CS516 49 CS516 50
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 }
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
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
• 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
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}
Stack Display
CS516 75 CS516 76
ARI for B
13
Display Static Chain vs Display Methods
CS516 79 CS516 80
CS516 81 CS516 82
14
The Dynamic Scoping Rule The Dynamic Scoping Rule
CS516 85 CS516 86
CS516 87 CS516 88
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 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