Download presentation
Presentation is loading. Please wait.
1
Run-Time Environments Chapter 7
Lecture # 22 Run-Time Environments Chapter 7
2
Runtime Environments Before code generation static source text of a program needs to be related to the actions that must occur at runtime to implement the program. As execution proceeds same name in the source text can denote different data objects in the target machine. The allocation and de allocation of data objects is managed by the runtime support package.
3
7.1 Source Language Issues
Each execution of a function is referred to as an activation of the procedure/function If the function is recursive several of its activations may be alive at the same time
4
Procedure Activation and Lifetime
A procedure is activated when called The lifetime of an activation of a procedure is the sequence of steps between the first and last steps in the execution of the procedure body A procedure is recursive if a new activation can begin before an earlier activation of the same procedure has ended
5
Activation Tree We use an activation tree to depict the way control enters and leaves activations. In an activation tree: Each node represents activation of a procedure The root represents activation of main program The node for a is the parent of the node for b if the control flows from activation a to b The node for a is to the left of node for b if the lifetime of a occurs before the lifetime of b
6
Procedure Activations: Example
program sort(input, output) var a : array [0..10] of integer; procedure readarray; var i : integer; begin for i := 1 to 9 do read(a[i]) end; function partition(y, z : integer) : integer var i, j, x, v : integer; begin … end procedure quicksort(m, n : integer); var i : integer; begin if (n > m) then begin i := partition(m, n); quicksort(m, i - 1); quicksort(i + 1, n) end end; begin a[0] := -9999; a[10] := 9999; readarray; quicksort(1, 9) end. Activations: begin sort enter readarray leave readarray enter quicksort(1,9) enter partition(1,9) leave partition(1,9) enter quicksort(1,3) … leave quicksort(1,3) enter quicksort(5,9) … leave quicksort(5,9) leave quicksort(1,9) end sort.
7
Activation Trees: Example
q(1,9) p(1,9) q(1,3) q(5,9) p(1,3) q(1,0) q(2,3) p(5,9) q(5,5) q(7,9) p(2,3) q(2,1) q(3,3) p(7,9) q(7,7) q(9,9) Activation tree for the sort program Note: also referred to as the dynamic call graph
8
Control Stack The flow of control of a program corresponds to depth first traversal of the activation tree Control stack is used to keep track of the live procedure activations. The idea is to push the node when activation begins and pop when it ends
9
Factorial Example discussed
public static int Factorial(int num) { if (num == 1) fact=1; } else fact=fact+num*Factorial(--num); return fact;
10
Addition of Elements of Array recursively
public static int Add(int index) { if (index == 0) sum = arr[index]; } else sum = sum + Add(arr[index]); return sum;
11
Control Stack Activation tree: Control stack:
Activations: begin sort enter readarray leave readarray enter quicksort(1,9) enter partition(1,9) leave partition(1,9) enter quicksort(1,3) enter partition(1,3) leave partition(1,3) enter quicksort(1,0) leave quicksort(1,0) enter quicksort(2,3) … s s q(1,9) q(1,3) q(2,3) r q(1,9) p(1,9) q(1,3) p(1,3) q(1,0) q(2,3)
12
Scope Rules Environment determines name-to-object bindings: which objects are in scope? program prg; var y : real; function x(a : real) : real; begin … end; procedure p; var x : integer; begin x := 1; … end; begin y := x(0.0); … end. Variable x locally declared in p A function x
13
Binding of Names If a name is declared once in a program it can hold different values at runtime The term environment refers to a function that maps name to a storage location The term state is referred to a function that maps a storage location to the value held there
14
Mapping Names to Values
environment state name storage value var i; … i := 0; … i := i + 1;
15
Mapping Names to Values
At compile time At run time environment state name storage value var i; i := 0; i := i + 1; Environment and states are different An assignment changes the state but not the environment
16
Static and Dynamic Notions of Bindings
Static Notion Dynamic Notion Definition of a procedure Activations of the procedure Declaration of a name Bindings of the name Scope of a declaration Lifetime of a binding
17
Storage Organization Runtime storage may be subdivided as:
The generated target code Data objects Control stack to keep track of procedure activations The size of target code is fixed at compile time Similarly size of data objects may be known
18
Typical subdivision of runtime memory
A separate area of runtime memory called Heap holds all other runtime information Code Static Data Stack Heap
19
Stack Allocation Activation records (subroutine frames) on the run-time stack hold the state of a subroutine Calling sequences are code statements to create activations records on the stack and enter data in them Caller’s calling sequence enters actual arguments, control link, access link, and saved machine state Callee’s calling sequence initializes local data Callee’s return sequence enters return value Caller’s return sequence removes activation record
20
Activation Records (Subroutine Frames)
fp (frame pointer) Returned value Actual parameters Optional control link Optional access link Save machine status Local data Temporaries Caller’s responsibility to initialize Callee’s responsibility to initialize
21
Control Links fp sp Stack growth
The control link is the old value of the fp Caller’s activation record fp Callee’s activation record Control link sp Stack growth
22
Access Links (Static Links)
s a x q(1,9) access k v s a x q(1,9) access k v q(1,3) access k v s a x q(1,9) access k v q(1,3) access k v p(1,3) access i j s a x q(1,9) access k v q(1,3) access k v p(1,3) access i j e(1,3) access The access link points to the activation record of the static parent procedure: s is parent of r, e, and q q is parent of p
23
Accessing Nonlocal Data
To implement access to nonlocal data a in procedure p, the compiler generates code to traverse np - na access links to reach the activation record where a resides np is the nesting depth of procedure p na is the nesting depth of the procedure containing a
24
Parameter Passing Modes
Call-by-value: evaluate actual parameters and enter r-values in activation record Call-by-reference: enter pointer to the storage of the actual parameter
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.