Implementing Subprograms with Dynamic Local Variables cont.Stack-• The compiler generates – A prologue : Entering a subprogram Get a segment of free stack storage and move the stack po
Trang 1Chapter 10
Implementing
Subprograms
Trang 2Chapter 10 Topics
• The General Semantics of Calls and Returns
• Implementing “Simple” Subprograms
• Implementing Subprograms with
Stack-Dynamic Local Variables
• Nested Subprograms
• Blocks
• Implementing Dynamic Scoping
Trang 3The General Semantics of Calls and Returns
• The subprogram call and return operations of a language are together called its subprogram
linkage
• Any implementation method for subprograms must be based on the semantics of subprogram linkage
• Subprogram calls are strictly nested: the caller
waits until the callee has terminated
Trang 4The General Semantics … (cont.)
call B
A
call C B
call D
Trang 5The General Semantics … (cont.)
• A subprogram call has two semantic actions
associated with it
– Call semantics
– Return semantics
Trang 6Call Semantics
• Save the execution status of the caller
• Carry out the parameter-passing process
• Pass the return address to the callee
• Transfer control to the callee
• The call must cause some mechanism to be
created to provide access to nonlocal variables that are visible to the callee
Trang 7Return Semantics
• If the subprogram has parameters that are out
mode or inout mode, the first action is to move the local values of the associated formal
parameters to the actual parameters
• If it is a function, move the functional value to a
place the caller can get it
• Deallocate the storage used for local variables
• Restore the execution status of the caller
• Transfer control back to the caller
Trang 8Implementing “Simple” Subprograms: Parts
• Required Storage: Status information of the
caller, parameters, return address, and
functional value (if it is a function)
• Two separate parts: the actual code and the
noncode part (local variables and data that can
change)
• The format, or layout, of the noncode part of an executing subprogram is called an activation
record (AR)
Trang 9Parts (cont.)
• An activation record instance (ARI) is a concrete example of an AR
• The AR of the callee is placed at the top of the
run-time stack, directly above the AR of the
Trang 10An AR for “Simple” Subprograms
[Functional value]
Local variables Parameters Return address
Trang 11Code & ARs parts for
FORTRAN 77 program
Common storage Local variables Local variables Parameters Return address Local variables Parameters Return address Local variables Parameters Return address
Code
Trang 12Implementing Subprograms with
Stack-Dynamic Local Variables
• More complex activation record
– The compiler must generate code to cause
implicit allocation and deallocation of local
variables
– Recursion must be supported (adds the possibility
of multiple simultaneous ARIs of a subprogram)
– The AR format is static, but its size may be
dynamic
– An ARI is dynamically created when a subprogram
is called
Trang 13Typical AR for a Language with
Stack-Dynamic Local Variables
Stack top
Local variables Parameters Dynamic link Static link Return address
Trang 14Information About the Caller
• The return address consists of:
– A pointer to the code segment of the caller
– An offset address in that code segment of the
instruction following the call statement
• Static link: a pointer to the ARI of the enclosing block
– It is used for accesses to nonlocal variables (static scope)
Trang 15Information About the Callee
• Dynamic link: A pointer to the top of the caller’s ARI (the next record on the stack)
– This pointer is necessary to handle varying record sizes
– This gives us access to the chain of previous
callers up to the main program
• Formal parameters (values or only pointers)
• Local variables and constants
• Temporary storage (to evaluate expressions)
Trang 16Local Local Local
total part a[1] a[2] a[3] a[4] a[5] sum
Trang 17Implementing Subprograms with Dynamic Local Variables (cont.)
Stack-• The compiler generates
– A prologue : Entering a subprogram
Get a segment of free stack storage and move the stack pointer up
Put in the new stack frame all data about the caller and the callee
– An epilogue : Exiting a subprogram
Return a value (if the subprogram is a function)
Remove the segment from the stack, move the stack pointer down
Jump to the return address (continue the statements in the caller's body)
Trang 18An Example Without Recursion
A(S);
… end;
begin
… B(P);
… end.
Trang 19An Example Without Recursion (cont.)
Local Local Parameter Dynamic link Static link Return to MAIN
R S T
Trang 20An Example Without Recursion (cont.)
Local Local Parameter Dynamic link Static link Return to MAIN
R S T
R S T
Local
Top
ARI for B
ARI for MAIN
P
Local Parameter Dynamic link Static link Return to B
X
Y
ARI for A
MAIN
A
B
C
Trang 21An Example Without Recursion (cont.)
Local Local Parameter Dynamic link Static link Return to MAIN
R S T
R S T
Local
Top
ARI for B
ARI for MAIN
P
Local Parameter Dynamic link Static link Return to B
X
Y
ARI for A
Local Local Parameter Dynamic link Static link Return to MAIN
R S T
Local
Top
ARI for B
ARI for MAIN
P
Local Parameter Dynamic link Static link Return to B
X
Y
ARI for A
Parameter Dynamic link Static link Return to A
Q ARI
for C
MAIN
A
B
C
Trang 22Dynamic Chain and Local Offset
• The collection of dynamic links in the stack at a given time is called the dynamic chain
• Local variables can be accessed by their offset from the beginning of the ARI This offset is
called the local_offset
– The local_offset can be determined by the
compiler at compile time
Trang 23An Example With Recursion
• The AR used in the previous example supports recursion, e.g
int factorial (int n) {
Trang 24Functional value Parameter Dynamic link Static link Return (to main)
? First call
Trang 25Copyright © 2006 Addison-Wesley All rights reserved 1-25
Functional value Parameter Dynamic link Static link Return (to main)
? First call
Functional value Parameter Dynamic link Static link Return (to main)
n
Local
First ARI for factorial
ARI for main
? 3
?
Functional value Parameter Dynamic link Static link Return (to factorial)
n
Top
Second ARI
for factorial
? 2
Trang 26Copyright © 2006 Addison-Wesley All rights reserved 1-26
Functional value Parameter Dynamic link Static link Return (to main)
? First call
Functional value Parameter Dynamic link Static link Return (to main)
n
Local
First ARI for factorial
ARI for main
? 3
?
Functional value Parameter Dynamic link Static link Return (to factorial)
n
Top
Second ARI
for factorial
? 2
Functional value Parameter Dynamic link Static link Return (to main)
n
Local
First ARI for factoria l ARI for
? 3
? Third call
Functional value Parameter Dynamic link Static link Return (to factorial)
n Second ARI
for factorial
? 2
Functional value Parameter Dynamic link Static link Return (to factorial)
n Top
Third ARI for factorial
? 1
Trang 27Functional value Parameter Dynamic link Static link Return (to main)
?
At position 2 in factorial third call completed
Functional value Parameter Dynamic link Static link Return (to factorial)
n Second ARI for
factorial
? 2
Functional value Parameter Dynamic link Static link Return (to factorial)
Trang 28Functional value Parameter Dynamic link Static link Return (to main)
?
At position 2 in factorial third call completed
Functional value Parameter Dynamic link Static link Return (to factorial)
n Second ARI for
factorial
? 2
Functional value Parameter Dynamic link Static link Return (to factorial)
value
Functional value Parameter Dynamic link Static link Return (to main)
? 3
?
At position 2 in factorial second call completed
Functional value Parameter Dynamic link Static link Return (to factorial)
Trang 29Functional value Parameter Dynamic link Static link Return (to main)
?
At position 2 in factorial third call completed
Functional value Parameter Dynamic link Static link Return (to factorial)
n Second ARI for
factorial
? 2
Functional value Parameter Dynamic link Static link Return (to factorial)
value
Functional value Parameter Dynamic link Static link Return (to main)
? 3
?
At position 2 in factorial second call completed
Functional value Parameter Dynamic link Static link Return (to factorial)
Functional value Parameter Dynamic link Static link Return (to main)
?
At position 2 in factorial
Trang 30Functional value Parameter Dynamic link Static link Return (to main)
?
At position 2 in factorial third call completed
Functional value Parameter Dynamic link Static link Return (to factorial)
n Second ARI for
factorial
? 2
Functional value Parameter Dynamic link Static link Return (to factorial)
value
Functional value Parameter Dynamic link Static link Return (to main)
? 3
?
At position 2 in factorial second call completed
Functional value Parameter Dynamic link Static link Return (to factorial)
Functional value Parameter Dynamic link Static link Return (to main)
?
At position 2 in factorial
value Local
ARI for
At position 3 in main final results
Top
Trang 31Nested Subprograms
• Some non-C-based static-scoped languages
(e.g., Fortran 95, Ada, JavaScript) use
stack-dynamic local variables and allow subprograms
to be nested
• All variables that can be non-locally accessed
reside in some ARI in the stack
• The process of locating a nonlocal reference:
1 Find the correct ARI
2 Determine the correct offset within that ARI
Trang 32Static Scoping
• There are two major implementation techniques for creating accesses to nonlocal variables in a static-scoped language: static chain and display
• Finding the offset is easy
• Finding the correct ARI
– Static semantic rules guarantee that all nonlocal variables that can be referenced have been
allocated in some ARI that is on the stack when
the reference is made
Trang 33Static Scoping - Concepts
• The static link in an ARI for subprogram A
points to the ARI of A's static parent (enclosing scope)
• A static chain is a chain of static links that
connects certain ARIs
• The static chain from an ARI connects it to all of its static ancestors
Trang 34Static Scoping – Concepts (cont.)
• To find the declaration for a reference to a
nonlocal variable:
– You could chase the static chain until the ARI that has the variable is found
– Searching each ARI as it is found, if variable
names were stored in the ARI
• Static_depth is an integer associated with a
static scope whose value is the depth of nesting
of that scope
Trang 35… end;
procedure C ; static_depth = 1
… end;
…
end.
Trang 36Static Scoping – Concepts (cont.)
• The nesting_depth of a (nonlocal) reference is
the difference between:
– The static_depth of the subprogram containing
the reference to nonlocal
– The static_depth of the subprogram containing
the declaration for nonlocal
• A reference can be represented by the pair:
(nesting_depth, local_offset)
where local_offset is the offset in the ARI of the
variable being referenced and nesting_depth is the number of static links to the correct ARI
Trang 37procedure A
procedure B
procedure C
… referencing a variable declared in A; 1
… referencing a variable declared in B; 2
… referencing a variable declared in C; 3
end C;
end B;
end A;
• Position 1: nesting_depth of this reference would be 2
• Position 2: nesting_depth of this reference would be 1
• Position 3: nesting_depth of this reference would be 0
Trang 38Example Pascal Program
procedure SUB2 (X : integer);
var B, E : integer;
procedure SUB3 ; var C, E : integer;
begin SUB1; E := B + A: < 2 end;
begin
SUB3; A := D + E; < 3 end;
begin SUB2(7); end;
begin BIGSUB; end;
Trang 39Stack Contents
Local Local Dynamic link Static link Return (to MAIN)
A
Local
ARI for BIGSUB
Local
B
C Top
1 0
Trang 40Local
ARI for BIGSUB
Local Parameter Dynamic link Static link Return (to BIGSUB)
X
ARI for SUB2
Local
B C
Local
B E
1 0
1 0 Top
Stack Contents
Trang 41Local Local Dynamic link Static link Return (to MAIN)
A
Local
ARI for BIGSUB
Local Parameter Dynamic link Static link Return (to BIGSUB)
X
ARI for SUB2
Local Local Dynamic link Static link Return (to SUB2)
C ARI for
SUB3
Local
B C
Local
B E
E Top
1 0
1 0
1 0
Trang 42Local Local Dynamic link Static link Return (to MAIN)
A
Local
ARI for BIGSUB
Local Parameter Dynamic link Static link Return (to BIGSUB)
X
ARI for SUB2
Local Local Dynamic link Static link Return (to SUB2)
C
Top
ARI for SUB3
Local
B C
Local
B E E
Local Local Dynamic link Static link Return (to SUB2)
A ARI for
SUB1
D
1 0
1 0
1 0
1 0
Trang 43Example Pascal Program (cont.)
Trang 44Static Chain Maintenance
• Assume there are no parameters that are
subprograms and no pass-by-name parameters
• At the call
– The ARI must be built
– The dynamic link is just the old stack top pointer – The static link must point to the most recent ARI
of the static parent
• Two methods
1 Search the dynamic chain until the first ARI for the static parent is found (easy, but slow)
Trang 45Static Chain Maintenance (cont.)
2 When the compiler encounters a procedure call:
It determines the procedure that declared the callee , which must be a static ancestor of the caller
It then computes the nesting_depth
This information is stored and can be accessed by
the procedure call during execution
• At the time of the call, the static link of the
callee’s ARI is determined by moving down the static chain of the caller the number of links
equal to the nesting_depth
Trang 46Static Chain Maintenance (cont.)
• At the return
– Subprogram’s ARI is removed from the stack
– The new top ARI is that of the unit that called the subprogram whose execution just terminated
– Because the static chain from this ARI was never changed, it works correctly just as it did before
the call to the other subprogram
Trang 47Evaluation of the Static Chain Method
• This method works for all procedure linkage,
except when parameters that are subprogram
names are involved
• A nonlocal reference is slow if the number of
scopes between the reference and the
declaration of the referenced variable is large
• It is difficult to estimate the costs of nonlocal
references, because the costs of references are not equal, and can change with code upgrades and fixes
Trang 48• An alternative to static chains
• Static links are stored in a single array called a
display
• The contents of the display at any given time is
a list of addresses of the accessible ARIs
– The entries in the display are pointers to the ARIs that have the variables in the referencing
environment
Trang 49Mechanics of references
• Represent references as
(display_offset, local_offset) where display_offset plays the role of nesting_depth
• Accesses to nonlocals using a display require
exactly two steps:
– Use the display_offset to get the pointer in the
display into the ARI with the variable
– Use the local_offset to get to the variable within the ARI
Trang 50Display maintenance
• Assuming no parameters that are subprograms and no pass-by-name parameters
• Note that display_offset depends only on the
static_depth of the procedure whose ARI is
being built
• There are k+1 entries in the display, where k is the static_depth of the currently executing unit (k = 0 is for the main program)
Trang 51Display maintenance (cont.)
• At the call: For a call to procedure P with a
static_depth of k
– Save, in the new ARI, a copy of the pointer at
position k in the display
– Put the link to the ARI for P at position k in the
display
• At the return
– Move the saved display pointer from the ARI back into the display at position k