31
1 Chapter 7: Runtime Environments

Chapter 7: Runtime Environments

  • Upload
    nat

  • View
    123

  • Download
    5

Embed Size (px)

DESCRIPTION

Chapter 7: Runtime Environments . Each execution of a procedure is referred to as an activation of the procedure . If the procedure is recursive, several of its activation may be alive at the same time. Activation Record - PowerPoint PPT Presentation

Citation preview

Page 1: Chapter 7: Runtime Environments

1

Chapter 7: Runtime Environments

Page 2: Chapter 7: Runtime Environments

2

Activation Record - a contiguous block of storage recording

the state of an activation of a function, not all the compilers set the same fields.

Each execution of a procedure is referred to as an activation of the procedure. If the procedure is recursive, several of its activation may be alive at the same time.

Page 3: Chapter 7: Runtime Environments

3

What is the contents of the activation record?

temporary values local data saved machine status optional access link to refer to non-local data held in other activation records optional control link to refer to the activation actual parameters returned value

Page 4: Chapter 7: Runtime Environments

General Organization of a Activation Record

Local & temp. data Old SP Return value Return address Argument count Parameter tn

................. Parameter t2

Parameter t1

0

2n-1

..

..

..

SP

Directionof Growthcaller’s

responsibility

callee’s duty

Page 5: Chapter 7: Runtime Environments

5

So, local data are accessed by negative offsets from SP (stack pointer, a register holding a particular position of currently activated procedure). A local name X can be referenced by X[SP] ([SP] means the address of SP), where X is the (negative) offset for the name from the location pointed to by SP. Parameter can be referred to by an positive offset from SP. That is, the ith parameter can be referenced by (4+n-i)[SP] (assume each entry takes one unit of space)

Page 6: Chapter 7: Runtime Environments

Suppose the Memory structure for C program is organized as below, where function P calls Q and Q calls R

0 .. Static area for programs and global (static) data

 

  ..

2n-1

Top

Extra storage for R Activation for R

Direction

Extra storage for Q

Activation for Q

SP

of growthExtra storage for P

Activation for P

(fp)

(sp)

Page 7: Chapter 7: Runtime Environments

TOP is a register holding the top address of the stack. SP is also a permanently allocated register (stack pointer) holding a specific address in the activation record of the currently active procedure. We use SP to indirectly refer to the local variables, parameters, etc.

Extra storage is used for storing pointers on the display, variable-length data such as dynamically allocated arrays, etc.

Page 8: Chapter 7: Runtime Environments

8

Page 9: Chapter 7: Runtime Environments

9

Page 10: Chapter 7: Runtime Environments

10

Page 11: Chapter 7: Runtime Environments

At run-timee.g. p (T1, T2, ..., Tn) ==> param T1 => push (T1) param T2 => push (T2) ... .. ... .. param Tn => push (Tn) call p, n => push (n) => push (l1)

=> push () => push (SP)

=> goto l2

l1 denotes the return address; l2 denotes the address of the first statement of the called procedure.

Page 12: Chapter 7: Runtime Environments

12

e.g., p (A+B*C, D) ==> T1 = B * C T2 = A + T1 Param T2 Param D Call p, 2

Temporary variable

Page 13: Chapter 7: Runtime Environments

At run-time * Assume TOP points to the lowest-numbered used location on the

stack and the memory locations are counted by words. 'param T' is translated into 'push(T)' which stands for TOP = TOP - 1; /* now TOP points to an available entry */ *TOP = T; /* save T into the memory 'call p, n' is translated into the following instructions: push (n) /*store the argument count */push (l1) /* l1 is the return address */push () /* leave one space for the return value */push (SP) /* store the old stack pointer */goto l2 /* l2 is the first statement of the called procedure p */

Page 14: Chapter 7: Runtime Environments

The first statement of the called procedure must be a special three-address code 'procbegin', which (1) sets the stack pointer to the place holding the old SP and (2) sets TOP to the top of the activation record (or the stack), so 'procbegin' stands for:

SP = TOP; // now SP points to old SP value TOP = SP + size_of_p; /* size_of_p is the size of p, i.e., the number of words taken by the local data for p */

The first statement of the called procedure

Page 15: Chapter 7: Runtime Environments

The ‘return’ statement The return statement in c can have the form 'return (expression);' This statement can be implemented by three-address code to evaluate the expression into a temporary T followed by:   1[SP] = T /* 1 is the offset for the location of the return value */ TOP = SP + 2 /* TOP now point to the return address */ SP = *SP /* restore SP, SP now points to old location */ L = *TOP /* the value of L is now the return address */ TOP = TOP + 1 /* TOP points to the argument count */ TOP = TOP + 1 + *TOP /* *TOP is the number of parameter of P. We restore TOP to the top of extra storage for the activation record below */ go to *L

Page 16: Chapter 7: Runtime Environments

16

Do not expect a procedure to do anything that requires it to know the size of the activation record of another procedure.

Page 17: Chapter 7: Runtime Environments

17

Control Link & Access Link

Control (Dynamic) Link - link the caller’s activation record and thus

allows us to restore the state (activation record) of the caller upon procedure exit.

Access (Static) Link   - a link used to access non-local variables

Page 18: Chapter 7: Runtime Environments

B

P

.

.

.P

Q

B

P

.

.

.PP

Q

Control Links Access and Control Links

growthdirection

P

Two procedures P and Q are defined in procedure B; P callsitself recursively several times before it calls Q.

Procedure B procedure P { P….. Q….. } procedure Q {…… …… }…P….

Page 19: Chapter 7: Runtime Environments

Nested Block Structures in Ce.g. Int test( ) { int x,y,m; ….. { int m=1, n=2; x = m + n; …. } ….. { int w=2, k =4; m = w * k; … } ….. }

Page 20: Chapter 7: Runtime Environments

Access variables for activation record with access links For local variables e.g., fetch (-1)[SP] // assume (-1) is the offset of a local variable. For non-local variables with static distance 1 e.g., EP = (1)[SP] //EP is a register; assume access link is stored in (1)[SP]. EP = (-1)[EP] // EP now points to what the SP should point to. fetch (-3)[EP] // (-3) is the offset of the non-local variable. For non-local variables with static distance 2 e.g., EP = (1)[SP] EP = *EP // the content of EP is saved in EP EP = (-1)[EP] // EP now points to what the SP should point to. fetch (-5)[EP] // assume (-5) is the offset of the non-local

Page 21: Chapter 7: Runtime Environments

21

Steps to access a non-local variable at run-time

1. Skip down the access links given by the static distance to get the activation record in which the variable resides. The static distance between the use and the declaration of the variable is a constant computed by the compiler at compile-time.

2. The address of the variable is obtained by adding the fixed offset of the variable to the address obtained in step 1. The offset is also a constant computed by the compiler at compile-time.

Page 22: Chapter 7: Runtime Environments

22

Variable-Length Data

A common strategy for handling variable-length data is shown below, where procedure p has three local arrays. The storage for these arrays is not part of the activation record for p; only a pointer to the beginning of each array appears in the activation record. The relative addresses of these pointers are known at compile-time, so the target code can access array elements through pointers.

Page 23: Chapter 7: Runtime Environments

control linkPointer to APointer to B

array Aarray B

activation record for P

arraysof P

control link

TOP

Activation recordof Q called by P

array of q

SP

Access to dynamically allocated arrays

int x[10];

int A[w];Int B[m];……

read w;m= w * 10;

w*2

Page 24: Chapter 7: Runtime Environments

24

Passing function as a parameter

Page 25: Chapter 7: Runtime Environments

Program HP

proc p (func h)

h

proc q

func f

p(g)

p(f)

func g

q

HP

q

p

f

Stack configurationafter the call to p(f)and (dashed) afterthe call to h.

call to h

After the callto p(f)

access links

Page 26: Chapter 7: Runtime Environments

Program HP

proc p (func h)

h

proc q

func f

p(g)

p(f)

func g

q

HP

q

p

f

Stack configurationafter the call to p(g)and (dashed) afterthe call to h.

call to h

p

g

after callto p(g)

Page 27: Chapter 7: Runtime Environments

27

Display

An array data structure (array of pointers to entries of activation record, e.g., d[ ]) used to store the specific location of (1) currently activated function’s activation record and (2) the activation records of each level of declared nested functions. Therefore, storage for a non-local variable a at nesting depth i is in the activation record pointed to by display element d[i].

Page 28: Chapter 7: Runtime Environments

e.g. program s (input, output); var a: array[0..10] of integer; x: integer; procedure r; var i: integer; begin ... a ... end; /* r */ procedure e (i,j: integer); begin x := a[i]; a[i] = a[j]; a[j] := x end; /* e */ procedure q (m,n: integer); var k,v: integer; function p (y,z: integer):integer; var i,j: integer; begin ... a ... ... v ... e (i,j); end; /* p */ begin ...... end; /* q */ begin ... end. /* s */

Page 29: Chapter 7: Runtime Environments

d [1]d [2]

q (1,9)saved d [2]

S

q (1,9)saved d [2]

S

d [1]d [2]

q(1,3)saved d [2]

growthdirection

(a)

(b)

Page 30: Chapter 7: Runtime Environments

q (1,9)saved d [2]

S

d [1]d [2]

q(1,3)

saved d [3]

growthdirection

d [3]

p(1,3)

saved d [2]

( c )

Page 31: Chapter 7: Runtime Environments

q (1,9)saved d [2]

S

d [1]d [2]

q(1,3)

saved d [3]

growthdirection

d [3]

p(1,3)

saved d [2]

( d )

e(1,3)saved d[2]