0
Run-Time Environments Chapter 7 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007...
Procedure Activation and Lifetime <ul><li>A procedure is  activated  when called </li></ul><ul><li>The  lifetime  of an ac...
Procedure Activations: Example program sort(input, output)   var a : array [0..10] of integer;   procedure readarray;   va...
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...
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  ent...
Scope Rules <ul><li>Environment  determines name-to-object bindings: which objects are in  scope ? </li></ul>program prg; ...
Mapping Names to Values name storage value environment state var i; … i := 0; … i := i + 1;
Mapping Names to Values name storage value environment state var i; … i := 0; … i := i + 1; At compile time At run time
Static and Dynamic Notions of Bindings Static Notion Dynamic Notion Definition of a procedure Activations of the procedure...
Stack Allocation <ul><li>Activation records  (subroutine frames) on the run-time stack hold the state of a subroutine </li...
Activation Records (Subroutine Frames) Caller’s responsibility to initialize Callee’s responsibility to initialize fp (fra...
Control Links fp sp Control link Stack growth Callee’s activation record Caller’s activation record The control link is th...
Scope with Nested Procedures program sort(input, output)   var a : array [0..10] of integer;   x : integer;   procedure re...
Access Links (Static Links) The access link points to the activation record of the static parent procedure: s  is parent o...
Accessing Nonlocal Data <ul><li>To implement access to nonlocal data  a  in procedure  p , the compiler generates code to ...
Parameter Passing Modes <ul><li>Call-by-value : evaluate actual parameters and enter r-values in activation record </li></...
Upcoming SlideShare
Loading in...5
×

Ch7

822

Published on

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
822
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
35
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Ch7"

  1. 1. Run-Time Environments Chapter 7 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007-2009
  2. 2. Procedure Activation and Lifetime <ul><li>A procedure is activated when called </li></ul><ul><li>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 </li></ul><ul><li>A procedure is recursive if a new activation can begin before an earlier activation of the same procedure has ended </li></ul>
  3. 3. 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.
  4. 4. 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 program Note: also referred to as the dynamic call graph
  5. 5. 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) … Control stack: Activation tree: s q(1,9) q(1,3) q(2,3)
  6. 6. Scope Rules <ul><li>Environment determines name-to-object bindings: which objects are in scope ? </li></ul>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
  7. 7. Mapping Names to Values name storage value environment state var i; … i := 0; … i := i + 1;
  8. 8. Mapping Names to Values name storage value environment state var i; … i := 0; … i := i + 1; At compile time At run time
  9. 9. 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
  10. 10. Stack Allocation <ul><li>Activation records (subroutine frames) on the run-time stack hold the state of a subroutine </li></ul><ul><li>Calling sequences are code statements to create activations records on the stack and enter data in them </li></ul><ul><ul><li>Caller’s calling sequence enters actual arguments, control link, access link, and saved machine state </li></ul></ul><ul><ul><li>Callee’s calling sequence initializes local data </li></ul></ul><ul><ul><li>Callee’s return sequence enters return value </li></ul></ul><ul><ul><li>Caller’s return sequence removes activation record </li></ul></ul>
  11. 11. Activation Records (Subroutine Frames) Caller’s responsibility to initialize Callee’s responsibility to initialize fp (frame pointer) Returned value Actual parameters Optional control link Optional access link Save machine status Local data Temporaries
  12. 12. Control Links fp sp Control link Stack growth Callee’s activation record Caller’s activation record The control link is the old value of the fp
  13. 13. Scope with Nested Procedures program sort(input, output) var a : array [0..10] of integer; x : integer; procedure readarray; var i : integer; begin … end; procedure exchange(i, j : integer); begin x := a[i]; a[i] := a[j]; a[j] := x end; procedure quicksort(m, n : integer); var k, v : integer; function partition(y, z : integer) : integer var i, j : integer; begin … exchange(i, j) … end begin if (n > m) then begin i := partition(m, n); quicksort(m, i - 1); quicksort(i + 1, n) end end; begin … quicksort(1, 9) end.
  14. 14. Access Links (Static Links) 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 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
  15. 15. Accessing Nonlocal Data <ul><li>To implement access to nonlocal data a in procedure p , the compiler generates code to traverse n p - n a access links to reach the activation record where a resides </li></ul><ul><ul><li>n p is the nesting depth of procedure p </li></ul></ul><ul><ul><li>n a is the nesting depth of the procedure containing a </li></ul></ul>
  16. 16. Parameter Passing Modes <ul><li>Call-by-value : evaluate actual parameters and enter r-values in activation record </li></ul><ul><li>Call-by-reference : enter pointer to the storage of the actual parameter </li></ul><ul><li>Copy-restore (aka value-result ): evaluate actual parameters and enter r-values, after the call copy r-values of formal parameters into actuals </li></ul><ul><li>Call-by-name : use a form of in-line code expansion ( thunk ) to evaluate parameters </li></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×