Presentation is loading. Please wait.

Presentation is loading. Please wait.

© G. Drew Kessler and William M. Pottenger1 Subroutines, Part 2 CSE 262, Spring 2003.

Similar presentations


Presentation on theme: "© G. Drew Kessler and William M. Pottenger1 Subroutines, Part 2 CSE 262, Spring 2003."— Presentation transcript:

1 © G. Drew Kessler and William M. Pottenger1 Subroutines, Part 2 CSE 262, Spring 2003

2 © G. Drew Kessler and William M. Pottenger2 Activation Records and Instances An activation record is the data used by the subroutine while it is active that is not the subroutine’s code An activation record instance is the data for a particular call to a subroutine An activation record typically contains, at least: Local variable storage Parameter storage Return address

3 © G. Drew Kessler and William M. Pottenger3 Languages with Static Local Variables (that do not support recursion) For static variable languages, there is only one activation record instance per subroutine. Can be organized at compile time and allocation at load time. Procedure A(int x, real y)... end Procedure B(int a) real z = 5.0 A(a, 5.0) end Main int n = 6 B(n) end Local variables Parameters Return address Local variables Parameters Return address Main A A B B Data Code

4 © G. Drew Kessler and William M. Pottenger4 Using the Stack for Stack-Dynamic Local Variables For Stack-Dynamic Local Variables, need a new activation record instance for each time a subroutine is called. Allows recursion Use run-time stack Main B B A B Procedure A(int x, real y)... end Procedure B(int a) real z = 5.0 A(a, 5.0) end Main int n = 6 B(n) end Call Sequence

5 © G. Drew Kessler and William M. Pottenger5 Activation Record (AR) for Stack- Dynamic Variables Local variables and parameters are located by (address of activation record, local_offset) pair Local variables Parameters Dynamic Link Static Link Return Address Local variables Parameters... Stack built upward Memory Addresses 0 Top of previous AR Bottom of AR of parent scope

6 © G. Drew Kessler and William M. Pottenger6 Simple Example int n = 6 Procedure A(int x, real y)... end Procedure B(int a) real z = 5.0 A(a, 5.0) end Main B(n) end n Main ret a z B y A x Dynamic Static Dynamic Static

7 © G. Drew Kessler and William M. Pottenger7 Nested Subroutine Example int y Procedure A() int x, y procedure B(real z) int x begin y = 6 end procedure C(real y) begin B(y) x = 4 end Begin C(2.0) end Begin A() end

8 © G. Drew Kessler and William M. Pottenger8 Example with Recursion Procedure Merge(int A[], int L, int M, int R) /* Merge L to M with M+1 to R ranges of A */ end Procedure MergeSort(int A[], int L, int R) int M = L+R/2 if (L<R) MergeSort(A, L, M) MergeSort(A, M+1, R) Merge(A, L, M, R) end Main Num[5] = {4, 5, 2, 1, 3} MergeSort(Num, 0, 4) end

9 © G. Drew Kessler and William M. Pottenger9 Referencing Environments in Statically Scoped Languages

10 © G. Drew Kessler and William M. Pottenger10 Non-local Variables, Statically Scoped How to find non-local variable using static scoping Static Chain or Displays Static Chain Use static link in AR Determine distance to AR of parent scope at compile time  Calculate static_depth for each subroutine  Calculate nesting_depth from caller to callee’s parent scope  Static link is this number of hops along static chain from caller

11 © G. Drew Kessler and William M. Pottenger11 Tracking Referencing Environments using a Display Displays less common in modern architectures Essentially a collection of static links stored in an array

12 © G. Drew Kessler and William M. Pottenger12 ret Dynamic Scoping - Deep Access (Not the same as deep binding!) Follow the dynamic links until variable is found If run-time stack is tall, may be very slow int y, n Procedure A int x x = n end Procedure B int x = 2 int n = 4 A() y = x end n Main n x B y A x Dynamic Main n = 5 y = 3 B(n) end

13 © G. Drew Kessler and William M. Pottenger13 Dynamic Scoping - Shallow Access Provides same semantics as deep access One method: maintain stack for each variable name (separate from AR’s) Costly to enter and exit subprograms due to stack maintenance int y, n Procedure A int x x = n end Procedure B int x = 2 int n = 4 A() y = x end Main n = 5 y = 3 B(n) end MainB A ynx B

14 © G. Drew Kessler and William M. Pottenger14 The Binding of Referencing Environments Recall that the referencing environment of a statement or expression is the set of active bindings during execution When a procedure is passed as an argument, the referencing environment and the procedure itself are packaged together and called a closure If a language supports passing subroutines as parameters, should the referencing environment be the one in which the subroutine is passed or the environment in which it is finally executed?

15 © G. Drew Kessler and William M. Pottenger15  Morgan Kaufmann (Figure reproduced by permission of author/publisher)

16 © G. Drew Kessler and William M. Pottenger16 Shallow vs. Deep Binding Shallow binding refers to when the (non-local) referencing environment of a procedure instance is the referencing environment in force at the time the procedure is invoked (i.e., the one in which the procedure is invoked) Deep binding refers to when the (non-local) referencing environment of a procedure instance is the referencing environment in force at the time the procedure's declaration is elaborated (i.e., the one in which the procedure was passed as an argument) It ’ s as if the procedure were called in the caller Both of these binding methods can be applied using either static or dynamic scope rules

17 © G. Drew Kessler and William M. Pottenger17 Notes on Subroutine Bindings The difference between deep and shallow binding is not apparent unless you pass procedures as parameters The difference will only be noticeable for non- local references, that is, references which are neither local nor global E.g., binding rules aren’t relevant in languages such as C which have no nested subroutines To the best of our knowledge, no language with static scope rules has shallow binding

18 © G. Drew Kessler and William M. Pottenger18 General Overview: Making the Call The steps followed when calling and returning from a subroutine is knows as subprogram linkage Steps needed when calling a subroutine: Allocate memory for parameters as needed Copy values to pass-by-value and pass-by-value- result parameters Allocate memory for non-static local variables Save execution status of calling program Provide “return address” Set up mechanism for non-local variable access Transfer control to subroutine code

19 © G. Drew Kessler and William M. Pottenger19 General Overview: Handling the Return Steps need on the return from a subroutine: Copy values for pass-by-result and pass-by-value- result parameters Deallocate storage for called subroutine local variables, parameters, and run-time system info Restore state of calling subroutine, introducing result of called subroutine, if applicable Restore mechanism for accessing non-local variables from calling subroutine Return control to calling subroutine

20 © G. Drew Kessler and William M. Pottenger20 Memory Stacks Useful for stacked environments/subroutine call & return even if operand stack not part of architecture Stacks that Grow Down vs. Stacks that Grow Up: c b a 0 Little Inf. Big Inf. Big Memory Addresses $sp Grows Down POP: Load from Increment $sp PUSH: Decrement $sp Store to offset($sp) grows up grows down Grows Up POP: PUSH: Increment $sp 0 Little Store to Load from Decrement $sp up down offset($sp) Portions  UCB Fall 1997 Dave Patterson

21 © G. Drew Kessler and William M. Pottenger21 0zero constant 0 1atreserved for assembler 2v0expression evaluation & 3v1function results 4a0arguments 5a1 6a2 7a3 8t0temporary: caller saves...(callee can clobber) 15t7 MIPS: Software conventions for Registers 16s0callee saves... (caller can clobber) 23s7 24t8 temporary (cont’d) 25t9 26k0reserved for OS kernel 27k1 28gpPointer to global area 29spStack pointer 30fpframe pointer 31raReturn Address (HW)  UCB Fall 1997 Dave Patterson

22 © G. Drew Kessler and William M. Pottenger22 Call-Return Linkage: MIPS Stack Frame °Block structured languages contain link to lexically enclosing frame °Compilers normally keep scalar variables in registers, not memory! $fp ‘Extra’ Args Saved Registers Callee preserves caller’s $ra, $sp, $fp, $gp, $s0-$s7 as necessary Local Variables $sp Reference args and local variables at fixed offset from $fp $sp grows and shrinks during expression eval High Mem Low Mem Stack grows down Portions  UCB Fall 1997 Dave Patterson °Note that $sp = $sp - frame size; $fp = $sp + (frame size - 4) - gcc initializes $fp to $sp at procedure entry: what is sign of the gcc offset? °Some compilers don’t use $fp as frame pointer - can you think how (why)?

23 © G. Drew Kessler and William M. Pottenger23 Procedure Call Case Study put parameters in $a0-$a3++ and on stack jal procedure allocate local storage needed by procedure execute procedure code put results in $v0/$v1 (plus on ‘heap’) jr $ra

24 © G. Drew Kessler and William M. Pottenger24 Procedure Call: Details Caller: –Pass args in registers $a0-$a3. Additional args may be passed in other registers or on stack –Save caller-saved registers ($a0-$a3 and $t0- $t9) –Execute jal to callee’s first instruction: this saves caller’s return address in $ra

25 © G. Drew Kessler and William M. Pottenger25 Procedure Call: Details Callee (prior to execution): Allocate ‘local’ stack memory needed to store local variables, save registers, etc.  $sp = $sp - size of stack frame Save callee-saved registers (e.g., $s0-$s7, $fp, $ra) Establish frame pointer  $fp = $sp + (size of stack frame - 4)

26 © G. Drew Kessler and William M. Pottenger26 Procedure Call: Details Callee return (after execution): –If return value, place in $v0/$v1 –Restore all callee-saved registers (e.g., $fp, $ra) –Restore $sp to original value $sp = $sp + size of callee stack frame –Return control to caller jr $ra

27 © G. Drew Kessler and William M. Pottenger27 Saving $ra and $fp during procedure call $fp $sp $ra $fp $sp $ra $fp $sp low address high address Portions  UCB Fall 1997 Dave Patterson $ra


Download ppt "© G. Drew Kessler and William M. Pottenger1 Subroutines, Part 2 CSE 262, Spring 2003."

Similar presentations


Ads by Google