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

Slides:



Advertisements
Similar presentations
CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
Advertisements

©UCB CS 161 Lecture 4 Prof. L.N. Bhuyan
ECE 232 L6.Assemb.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 6 MIPS Assembly.
Procedures in more detail. CMPE12cGabriel Hugh Elkaim 2 Why use procedures? –Code reuse –More readable code –Less code Microprocessors (and assembly languages)
Lecture 6: MIPS Instruction Set Today’s topic –Control instructions –Procedure call/return 1.
Computer Architecture CSCE 350
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
The University of Adelaide, School of Computer Science
Procedure call frame: Hold values passed to a procedure as arguments
Implementing Subprograms
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison- Wesley. All rights reserved. 1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
ISBN Chapter 10 Implementing Subprograms.
1 Storage Registers vs. memory Access to registers is much faster than access to memory Goal: store as much data as possible in registers Limitations/considerations:
Procedures in more detail. CMPE12cCyrus Bazeghi 2 Procedures Why use procedures? Reuse of code More readable Less code Microprocessors (and assembly languages)
ISBN Chapter 10 Implementing Subprograms.
ISBN Chapter 10 Implementing Subprograms.
Honors Compilers Addressing of Local Variables Mar 19 th, 2002.
Run time vs. Compile time
Semantics of Calls and Returns
The environment of the computation Declarations introduce names that denote entities. At execution-time, entities are bound to values or to locations:
ISBN Chapter 10 Implementing Subprograms –Semantics of Calls and Returns –Implementing “Simple” Subprograms –Implementing Subprograms with.
1 CSCI 360 Survey Of Programming Languages 9 – Implementing Subprograms Spring, 2008 Doug L Hoffman, PhD.
Chapter 10 Implementing Subprograms. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Semantics of Call and Return The subprogram call and return.
Chapter 8 :: Subroutines and Control Abstraction
ISBN Chapter 10 Implementing Subprograms.
Programming Language Principles Lecture 24 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Subroutines.
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 9 Def: The subprogram call and return operations of a language are together called its subprogram.
Functions and Procedures. Function or Procedure u A separate piece of code u Possibly separately compiled u Located at some address in the memory used.
Copyright © 2005 Elsevier Chapter 8 :: Subroutines and Control Abstraction Programming Language Pragmatics Michael L. Scott.
Lecture 18: 11/5/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
1 Control Abstraction (Section ) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 7: MIPS Instructions III
Activation Records (in Tiger) CS 471 October 24, 2007.
CSC3315 (Spring 2008)1 CSC 3315 Subprograms Hamid Harroud School of Science and Engineering, Akhawayn University
ISBN Chapter 10 Implementing Subprograms.
Implementing Subprograms What actions must take place when subprograms are called and when they terminate? –calling a subprogram has several associated.
A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University A.Alzubair Hassan Abdullah Dept. Computer Sciences Kassala University NESTED SUBPROGRAMS.
CSC 8505 Compiler Construction Runtime Environments.
1 Chapter 10 © 2002 by Addison Wesley Longman, Inc The General Semantics of Calls and Returns - Def: The subprogram call and return operations of.
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
10-1 Chapter 10: Implementing Subprograms The General Semantics of Calls and Returns Implementing “Simple” Subprograms Implementing Subprograms with Stack-Dynamic.
ISBN Chapter 10 Implementing Subprograms.
Implementing Subprograms
MIPS Lab CMPE 142 – Operating Systems. MIPS Simulator First Time Startup Setting Options.
Subprograms - implementation. Calling a subprogram  transferring control to a subprogram: save conditions in calling program pass parameters allocate.
Function Calling. Mips Assembly Call and Return Steps for procedure calling –Save the return address –Jump to the procedure (function) –Execute the procedure.
ISBN Chapter 10 Implementing Subprograms.
ISBN Chapter 10 Implementing Subprograms.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
Chapter 10 : Implementing Subprograms
Implementing Subprograms Chapter 10
Implementing Subprograms
Implementing Subprograms
Procedures (Functions)
Procedures (Functions)
Functions and Procedures
Implementing Subprograms
Chapter 10: Implementing Subprograms Sangho Ha
Implementing Subprograms
Implementing Subprograms
Program and memory layout
Where is all the knowledge we lost with information? T. S. Eliot
Program and memory layout
Implementing Subprograms
Chapter 10 Def: The subprogram call and return operations of
Presentation transcript:

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

© 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

© 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

© 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

© 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

© 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

© 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

© 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

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

© 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

© 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

© 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

© 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

© 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?

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

© 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

© 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

© 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

© 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

© 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

© 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

© 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)?

© 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

© 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

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

© 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

© 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