24
Lecture # 22 Run-Time Environments Chapter 7 1

Lecture # 22

Embed Size (px)

DESCRIPTION

Lecture # 22. Run-Time Environments Chapter 7. 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. - PowerPoint PPT Presentation

Citation preview

Page 1: Lecture # 22

Lecture # 22

Run-Time Environments

Chapter 7

1

Page 2: Lecture # 22

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.

2

Page 3: Lecture # 22

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

3

Page 4: Lecture # 22

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

Page 5: Lecture # 22

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

5

Page 6: Lecture # 22

6

Procedure Activations: Exampleprogram 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.

Page 7: Lecture # 22

7

Activation Trees: Example

s

q(1,9)

q(1,3)

p(1,3) q(1,0) q(2,3)

q(2,1)p(2,3) q(3,3)

p(1,9)

r

q(5,9)

p(5,9) q(5,5) q(7,9)

q(7,7)p(7,9) q(9,9)

Activation tree for the sort programNote: also referred to as the dynamic call graph

Page 8: Lecture # 22

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

8

Page 9: Lecture # 22

Factorial Example discussed

public static int Factorial(int num)

{

if (num == 1)

{

fact=1;

}

else

{

fact=fact+num*Factorial(--num);

}

return fact;

}

9

Page 10: Lecture # 22

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;

• }

10

Page 11: Lecture # 22

11

Control Stack

s

q(1,9)

q(1,3)

p(1,3) q(1,0) q(2,3)

p(1,9)

r

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) …

Controlstack:

Activation tree:

s

q(1,9)

q(1,3)

q(2,3)

Page 12: Lecture # 22

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

Page 13: Lecture # 22

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

13

Page 14: Lecture # 22

14

Mapping Names to Values

name storage value

environment state

var i;…i := 0;…i := i + 1;

Page 15: Lecture # 22

15

Mapping Names to Values

name storage value

environment state

var i; i := 0; i := i + 1;

Environment and states are differentAn assignment changes the state but not the environment

At compile time At run time

Page 16: Lecture # 22

16

Static and Dynamic Notions of Bindings

Static Notion Dynamic Notion

Definition of a procedureActivations of the

procedure

Declaration of a name Bindings of the name

Scope of a declaration Lifetime of a binding

Page 17: Lecture # 22

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

17

Page 18: Lecture # 22

Typical subdivision of runtime memory

• A separate area of runtime memory called Heap holds all other runtime information

18

Code

Static Data

Stack

Heap

Page 19: Lecture # 22

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

Page 20: Lecture # 22

20

Activation Records(Subroutine Frames)

Returned value

Actual parameters

Optional control link

Optional access link

Save machine status

Local data

Temporaries

Caller’sresponsibilityto initialize

Callee’sresponsibilityto initialize

fp(frame pointer)

Page 21: Lecture # 22

21

Control Links

fp

spControl link

Stackgrowth

Callee’s activation record

Caller’s activation record

The control link is the old value of the fp

Page 22: Lecture # 22

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 qq is parent of p

Page 23: Lecture # 22

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

Page 24: Lecture # 22

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