1 / 31

Chapter 7: Runtime Environments

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

nat
Télécharger la présentation

Chapter 7: Runtime Environments

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 7: Runtime Environments

  2. 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 - a contiguous block of storage recording the state of an activation of a function, not all the compilers set the same fields.

  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

  4. General Organization of a Activation Record 0 .. callee’s duty SP Local & temp. data Old SP Return value Return address Argument count Parameter tn ................. Parameter t2 Parameter t1 .. Direction of Growth caller’s responsibility .. 2n-1

  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 byX[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)

  6. (sp) Top • 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 Extra storage for R Activation for R SP Extra storage for Q (fp) Activation for Q Direction Extra storage for P of growth Activation for P .. 2n-1

  7. 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.

  8. At run-time e.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; l2denotes the address of the first statement of the called procedure.

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

  10. 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 */

  11. The first statement of the called procedure • 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 */

  12. 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

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

  14. 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

  15. Procedure B procedure P { P….. Q….. } procedure Q {…… …… } … P …. Q Q P P . P . . growth direction . . . P P P B B Access and Control Links Control Links Two procedures P and Q are defined in procedure B; P calls itself recursively several times before it calls Q.

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

  17. 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

  18. Steps to access a non-local variable at run-time • 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. • 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.

  19. 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.

  20. TOP array of q SP Activation record of Q called by P control link int x[10]; int A[w]; Int B[m]; …… read w; m= w * 10; array B arrays of P array A Pointer to B Pointer to A activation record for P control link w*2 Access to dynamically allocated arrays

  21. Passing function as a parameter

  22. Program HP proc p (func h) h proc q func f p(g) p(f) func g q access links HP call to h q p After the call to p(f) f Stack configuration after the call to p(f) and (dashed) after the call to h.

  23. Program HP proc p (func h) h proc q func f p(g) p(f) func g q HP call to h q p f after call to p(g) Stack configuration after the call to p(g) and (dashed) after the call to h. p g

  24. 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].

  25. 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 */

  26. S d [1] growth direction q (1,9) d [2] saved d [2] (a) S d [1] q (1,9) d [2] saved d [2] q(1,3) (b) saved d [2]

  27. S growth direction d [1] q (1,9) d [2] saved d [2] d [3] q(1,3) saved d [2] p(1,3) saved d [3] ( c )

  28. S growth direction d [1] q (1,9) d [2] saved d [2] d [3] q(1,3) saved d [2] p(1,3) saved d [3] ( d ) e(1,3) saved d[2]

More Related