7. runtime environments zhang zhizheng
DESCRIPTION
Notes: An activation may manipulate data objects allocated for its use.TRANSCRIPT
7. Runtime Environments
Zhang [email protected]
7. 0 Overview1.Program vs program execution • A program consists of several procedures (functions)• An execution of a program is called as a process• An execution of a program would cause the activation of the related procedures• A name (e.g, a variable name)in a procedure would be related to different data objects
Notes: An activation may manipulate data objects allocated for its use.
2. Allocation and de-allocation of data objects– Managed by the run-time support
package– Allocation of a data object is just to
allocate storage space to the data object relating to the name in the procedure
Notes: The representation of a data object at run time is determined by its type.
3.Activation Trees A tree to depict the control flow enters and
leaves activation of a procedure f(4) f(3) f(2) f(2) f(1) f(1) f(0) f(1) f(0)
Enter f(4)Enter f(3)Enter f(2)Enter f(1)Leave f(1)Enter f(0)Leave f(0)Leave f(2)Enter f(1)Leave f(1)Leave f(3)
Enter f(2)Enter f(1)Leave f(1)Enter f(0)Leave f(0)Leave f(2)Leave f(4)
• Each node represents an activation of a procedure
• The root represents the activation of the main program
• The node for a is the parent of the node for b if and only if the control flows from activation of a to b, and
• The node for a is to the left of the node for b if and only if the lifetime of a occurs before the lifetime of b.
So, the flow of control in a program corresponds to a depth-first traversal of the activation tree that starts at the root.Printing enter when the node for an activation is reached for the first time, andPrinting leave after the entire subtree of the node has been visited during the traversal.
4.Control stacks A stack to keep track of live
procedure activations:Push the node for an activation onto
the control stack as the activation begins and to pop the node when the activation ends.
Actually, PUSH=ENTER; POP=LEAVE
5.Bindings of Names When an environment associates
storage location s with a name x, we say that x is bound to s; the association itself is referred to as a binding of x.
Notes: 1)Even each name is declared once in a program, the same name may denote different object at run time2)The “ data object” corresponds to a storage location that can hold values
3)In programming language semantics, the term “environment” refers to a function that maps a name to a storage location, and term “state” refers to a function that maps a storage location to the value held there.
environment state
name storage value
4)Environments and states are different; an assignment changes the state, but not the environment
5)If x is not of a basic type, the storage s for x may be a collection of memory words
6)A binding is the dynamic counterpart of a declaration
6.Factors that determines the run time environment of a program– Recursive definition– Local name storage space
allocation strategy– global name storage space
allocation strategy– Parameter passing mechanism– A function is whether allowed to be
a parameter of or return value of a function
7. 1 Storage organization1.Subdivision of Runtime Memory
Typical subdivision of runtime memory into code and data areas
codeStatic datastack
heap
The generated target code;Data objects;A counterpart of the control stack to keep track of procedure activations
2.Activation Records/Frames1) Definition A contiguous block of storage
that stores information needed by a single execution of a procedure
2) A general activation record
Returned valueActual parameters
Optional control link
Optional access linkSaved machine status
Local data
temporaries
Such as those values in the evaluation of expressions
Data that is local to the execution of a procedure
Information about the state of the machine just before
the procedure is called
Refer to non-local data held in other activation
records/fixed place
Point to the activation record of the caller
Used by the caller to supply parameters to the callee
The value that the callee return to caller
7. 2 Storage allocation strategies1.Storage allocation strategies
– Static allocation •Lays out storage for all data objects at compile time.
– Stack allocation•Manages the run-time storage as a stack.
– Heap allocation•Allocates and de-allocates storage as needed at run time from a heap.
2.Static allocationNames are bound to storage as the program is compiled,
so there is no need for a run-time support package. Since the bindings do not change at run time, every time a procedure is activated, its names are bound to the same storage locations.
Some Limitations– The size of a data object and
constraints on its position in memory must be known at compile time.
– Recursive procedures are restricted, because all activations of a procedure use the same bindings for local name.
– Data structures cannot be created dynamically.
3.Heap allocation– A separate area of run-time
memory, called a heap.– Heap allocation parcels out pieces
of contiguous storage, as needed for activation records or other objects. Pieces may be de-allocated in any order.
I -11 J -11 X 0 A -21
Heap allocation is useful if either of the following is possible.
1. The value of local names must be retained when an activation ends.
2. A called activation outlives the caller.
4. Stack allocation1)Main idea
– Based on the idea of a control stack; storage is organized as a stack, and activation records are pushed and popped as activations begin and end, respectively.
– Storage for the locals in each call of a procedure is contained in the activation record for that call.
– Locals are bound to fresh storage in each activation
Position in AT AR on the stack Remark
sS
a: array
S
a: arrayr
i: integer
s
r
s
r q(1, 9)
S
a: arrayq(1 9)
i: integer
s
r q(1, 9)
S
a: arrayq(1 9)
i: integerq(1 3)
i: integer
p(1, 9) q(1, 3)
p(1, 3) q(1, 0)
2)Calling sequences– A call sequence allocates an
activation record and enters information into its fields
– A return sequence restores the state of the machine so the calling procedure can continue execution.
Parameters & returned value
Control link links and saved status
Temporaries and local data
Parameters & returned value
Control link links and saved status
Temporaries and local dataTop-sp
Caller’s Responsibility
Callee’s Responsibility
Caller’s activation record
Callee’s activation record
Division of tasks between caller and callee
The register top-sp points to the end of the machine status field
Steps: (1)The caller evaluates actual parameters.(2)The caller stores a return address and the old value of top-sp into the callee’s activation record.(3)The callee saves register values and other status information(4)the callee initializes its local data and begins execution.
A possible return sequence :– (1)The callee places a return value next to the activation record of the caller– (2)Using the information in the status field, the callee restores top-sp and other registers and branches to a return address in the caller’s code.– (3)Although top-sp has been decremented, the caller can copy the returned value into its own activation record and use it to evaluate an expression.
ACCESS TO NONLOCAL NAMES
• P411-415• Algol(Alan J.Perlis)
3)Stack allocation for non-nested procedure--------for C
In C, a procedure definition cannot appear within another. If there is a nonlocal reference to a name a in some function, then a must be declared outside any function.
– Stack allocation for C – When entering a procedure, the
activation record of the procedure is set up on the top of the stack
– Initially, put the global(static) variables and the activation record of the Main function
e.g. the calling sequence is like the following:main () {…… q( ); } p( ) {……} q( ) { …… p( ); …… }
Activation record for main
Activation record for q
Activation record for P
TOP
Top-SP
Main AR
Q AR
P ARTOP
SP
global Var
Caller’s SPCalling Return addressAmount of parameters
Formal parametersLocal data
Array data
Temporaries
Returned value
#include <stdio.h>int x,y;int main(){ x=5; y=f(x);}
int f(int n){ if (n<=1) return 1;else { int t1,t2,t; t1=f(n-1); t2=f(n-2);t=t1+t2; return t} }
…..Activation Record of the function called by Main functionActivation Record of Main functionGlobal Variable Data Area
Storage Organization of C Language
The Form of Activation Record of any a function in C
Unit for Returned Value of functionInternal Temporary Work UnitsLocal Variable Data Storage UnitsFormal parameter Storage UnitsNumber of Formal ParametersReturned Address Caller’s SP (Start Address of caller’s activation record)
TOP
SP
1) Here we assume that the caller’s sp of Main function is the start address of global variable data area, and the returned address in the activation record of a function (including Main function) is filled by the operating system automatically, you might not care it. 2) The start address of stack used in the program is K.
How about the stack map of the above c program?
4)Stack allocation for nested procedures(1) the nesting of procedure definitions in the
Pascal programP0
P1
P2
Call P2
Call P1
P0
P1
P2
Call P2
Call P1
(2) nesting depth
i - 1
P0
……
Pi-1
Pii
0
(3) access links
P0
P1
P2
Call P2
Call P1
TOP
Top-SPP2
P1
P0
Access link
Access link
Access link
TOP
Top-SPP2
P1
P0
Access link
Access link
Access link
P0
P1
P2
Call P2
Call P1
(4) displays– Faster access to global than with
access links can be obtained using an array d of pointers to activation records, called a display.
j - 1
Sp for p0
……
Sp for pj-1
Sp for pjj
0
d[j+1]
Access non-local name by display.Suppose control is in an activation of a procedure p at nesting depth j. Then, the first j-1 elements of the display point to the most recent activations of the procedure that lexically enclose procedure p, and d[j] points to the activation of p.
– When a new activation record for a procedure at nesting depth i is setup, we• A) save the value of d[i] in the new activation record;
• B) set d[i] to point to the new activation record.
Example:Program M; var A,B:integer function F(N:integer):integer; if N<=2 then return(N) else return (N*F(N-1)); begin read(A); /*Let A=5*/ B:=F(A); write (B); end.Please write down the display of the above program
MF(5)
Saved d[1]F(4)
Saved d[1]F(3)
Saved d[1]F(2)
Saved d[1]
d[1]d[2]
7. 3 Parameter passing1.Parameter-passing methods• Call-by-value• Call-by-reference• Copy-restore• Call-by-name
2.Call-by-value1) in C and Pascal2)implementation
– A formal parameter is treated just like a local name, so the storage for the formals is in the activation record of the called procedure.
– The caller evaluates the actual parameters and places their r-values in the storage for the formals.
3.Call-by-Reference1) also known as call-by-address or call-by-
location.2)implementation
– If an actual parameter is a name or an expression having an l-value, then that l-value itself is passed.
– If the actual parameter is an expression that has no l-value, then the expression is evaluated in a new location, and the address of that location is passed.
4.Copy-Restore1)a hybrid between call-by-value and call-by-reference is copy-restore linkage.2)implementation– Before control flows to the called procedure, the actual parameters are evaluated.(copy-in)– When control returns,the current r-value of the formal parameters are copied back into the l-values of the actuals, using the l-values computed before the call(copy-out)Notes: Only actuals having l-values are copied.
5.Call-by-NameTraditionally defined by the copy-rule of Algol It is just as macro-expansion or in-line expansion
7. 4 Symbol Tables1.Functions of symbol tables• Keep track of scope and binding
information about names• Allow us to add new entries and find
existing entries efficiently
Notes: 1)Changes to the table occur if a
new name or new information about an existing name is discovered
2)The table may be a linear list or a hash table
3)It is useful for a compiler to be able to grow the symbol table dynamically, if necessary, at compile time
2.Symbol-table entries• Each entry in the symbol table is for
the declaration of a name• Each entry can be implemented as a
record consisting of a sequence of consecutive words of memory
Notes: 1)Information may be entered into the symbol table at various times 2)Attributes of a name are entered in response to declarations
Exercises7.6Addition 1:We assume that the storage organization and the form of activation record used in C language program run-time stack storage allocation are as following. Please construct the run-time stack map (detailed map) for the following C program
• Storage Organization of C Language
…..
Activation Record of the function called by Main functionActivation Record of Main function
Global Variable Data Area
The Form of Activation Record of any a function in C
Unit for Returned Value of functionInternal Temporary Work UnitsLocal Variable Data Storage UnitsFormal parameter Storage UnitsNumber of Formal ParametersReturned Address Caller’s SP (Start Address of caller’s activation record)
TOP
SP
The C program is as the following:
#include <stdio.h>int x,y,z;int main(){ x=3; y=9; z=f(x,y);}
int f(int m, int n){ if (m>=0 && n>=0) {if (m==0) return n; else if (n==0) return m; else { int t1,t2,t; if (m>=n) { t1=m-n; t2=n } else {t1=n-m; t2=m} t=f(t1,t2); return t } }
Notes: 1) Here we assume that the caller’s
sp of Main function is the start address of global variable data area, and the returned address in the activation record of a function (including Main function) is filled by the operating system automatically, you might not care it.
2) The start address of stack used in the program is K.