Topic 3-a Calling Convention 1/10/2019.

Slides:



Advertisements
Similar presentations
The University of Adelaide, School of Computer Science
Advertisements

10/6: Lecture Topics Procedure call Calling conventions The stack
Procedures in more detail. CMPE12cGabriel Hugh Elkaim 2 Why use procedures? –Code reuse –More readable code –Less code Microprocessors (and assembly languages)
Computer Architecture CSCE 350
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Ch. 8 Functions.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
Apr. 12, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 6: Branching and Procedures in MIPS* Jeremy R. Johnson Wed. Apr. 12, 2000.
The University of Adelaide, School of Computer Science
Procedure call frame: Hold values passed to a procedure as arguments
Procedures in more detail. CMPE12cCyrus Bazeghi 2 Procedures Why use procedures? Reuse of code More readable Less code Microprocessors (and assembly languages)
Runtime Environments Source language issues Storage organization
CS 536 Spring Code generation I Lecture 20.
1 Pertemuan 20 Run-Time Environment Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
Run time vs. Compile time
28/06/2015CMPUT Functions (2)  Function calling convention  Various conventions available  One is specified by CMPUT229  Recursive functions.
Chapter 8 :: Subroutines and Control Abstraction
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Programming Language Principles Lecture 24 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Subroutines.
Copyright © 2005 Elsevier Chapter 8 :: Subroutines and Control Abstraction Programming Language Pragmatics Michael L. Scott.
1 Control Abstraction (Section ) CSCI 431 Programming Languages Fall 2003 A compilation of material developed by Felix Hernandez-Campos and Michael.
Procedure Calls and the Stack (Lectures #18) ECE 445 – Computer Organization The slides included herein were taken from the materials accompanying Computer.
Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard form for.
COP4020 Programming Languages Subroutines and Parameter Passing Prof. Xin Yuan.
Runtime Environments. Support of Execution  Activation Tree  Control Stack  Scope  Binding of Names –Data object (values in storage) –Environment.
Lecture 19: 11/7/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Procedure Basics Computer Organization I 1 October 2009 © McQuain, Feng & Ribbens Procedure Support From previous study of high-level languages,
Runtime Stack Computer Organization I 1 November 2009 © McQuain, Feng & Ribbens MIPS Memory Organization In addition to memory for static.
MAL 3 - Procedures Lecture 13. MAL procedure call The use of procedures facilitates modular programming. Four steps to transfer to and return from a procedure:
7. Runtime Environments Zhang Zhizheng
Pushing the Return Address To return to the caller a subroutine must have the correct return address in $ra when the jr instruction is performed. But this.
Function Calling. Mips Assembly Call and Return Steps for procedure calling –Save the return address –Jump to the procedure (function) –Execute the procedure.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 12 Procedure Calling.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
Run-Time Environments Presented By: Seema Gupta 09MCA102.
Computer Architecture & Operations I
Storage Classes There are three places in memory where data may be placed: In Data section declared with .data in assembly language in C - Static) On the.
Run-Time Environments Chapter 7
Computer Science 210 Computer Organization
Computer structure: Procedure Calls
Lecture 5: Procedure Calls
© Craig Zilles (adapted from slides by Howard Huang)
Run-Time Storage Organization
Run-Time Storage Organization
Procedures (Functions)
Procedures (Functions)
Functions and Procedures
Run-Time Environments
Chapter 9 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
Stack Frame Linkage.
MIPS Instructions.
The University of Adelaide, School of Computer Science
Run-Time Environments
Lecture 5: Procedure Calls
Topic 3-b Run-Time Environment
10/4: Lecture Topics Overflow and underflow Logical operations
UNIT V Run Time Environments.
Procedures and Calling Conventions
Program and memory layout
Run Time Environments 薛智文
Computer Architecture
Program and memory layout
Where is all the knowledge we lost with information? T. S. Eliot
Program and memory layout
© Craig Zilles (adapted from slides by Howard Huang)
Topic 2b ISA Support for High-Level Languages
Presentation transcript:

Topic 3-a Calling Convention 1/10/2019

Calling Convention Calling convention is a standardized method for a program to pass parameters to a procedure and receive result values back from it. 1/10/2019 \course\cpeg421-08s\Topic-3-a.ppt

Consideration of Call Convention • Where to place parameters and return values (in registers, on the activation stack, or a mix of both) • The order in which parameters are passed • Responsibility for setting up and cleaning up a function call - distributed between the calling and the called code. • Different platforms use different call conventions, and so can different programming languages, even on the same platform. 1/10/2019 \course\cpeg421-08s\Topic-3-a.ppt

Issues in Calling Convention • Register usage convention • Calling sequence • Parameter passing • Local data layout 1/10/2019 \course\cpeg421-08s\Topic-3-a.ppt

Register Usage Convention • Special registers -- stack register, frame register, return address register, return value registers) • Reserved registers • Temporary registers (not preserved across a procedure call) • Saved Registers (preserved across a procedure call) 1/10/2019 \course\cpeg421-08s\Topic-3-a.ppt

Context Switching –Register Saving Register contents are saved before context switching into another procedure. • Caller-save versus Callee-save disciplines • Contents saved in frame of saver • Often choose to keep values in registers for efficiency 1/10/2019 \course\cpeg421-08s\Topic-3-a.ppt

Register Saving (Con’t) • Caller save The calling procedure (caller) is responsible for saving and restoring any registers that must be preserved across the call. The called procedure (callee) can then modify any register without constraint. • Callee save The callee is responsible for saving and restoring any registers that it might use. The caller uses registers without worrying about restoring them after a call. 1/10/2019 \course\cpeg421-08s\Topic-3-a.ppt

Without Register Saving Assume register set: saved registers + temporary registers main (){ …; subA; } can use saved registers, temporary registers subA(){ …; subB;…;} can’t use saved registers but can use temporary registers subB(){} can’t use saved registers but can use temporary registers can’t save any value in saved registers! 1/10/2019 \course\cpeg421-08s\Topic-3-a.ppt

Caller Save/Callee Save Trade-Off • If all caller save ? Even callee doesn’t kill any of the saved registers – waste of cycles and memory resource • If all callee save ? Callee has to save all the register (which will be used by callee), even caller doesn’t use them 1/10/2019 \course\cpeg421-08s\Topic-3-a.ppt

Caller Save/Callee Save Trade-Off temporary registers • callee save saved registers 1/10/2019 \course\cpeg421-08s\Topic-3-a.ppt

Caller save example Function A Function B How to save ? Add $10, $11,$12 Save $10, $12, $13 Jal B Restore $10, $12, $13 Sub $11, $2, $12 Mul $12, $10, $13 Add $2, $4, $5 Br $31 How to save ? 1/10/2019 \course\cpeg421-08s\Topic-3-a.ppt

Callee Save example Function A Function B Add $10, $11,$12 Jal B Add $11, $2, $12 Mul $12, $10, $13 Save $10, $11, $12 (if they are used in B) Lw $10, 4(sp) Sub $11, $8, $9 Sub $12, $11, $10 Sub $2, $12, $11 Restore $10, $11, $12 Br $31 1/10/2019 \course\cpeg421-08s\Topic-3-a.ppt

Callee Save example Function A Function B No save! Add $10, $11,$12 Jal B Sub $11, $2, $12 Mul $12, $10, $13 Add $2, $4, $5 Br $31 No save! 1/10/2019 \course\cpeg421-08s\Topic-3-a.ppt

Calling Sequences A calling sequence is code statements to create activation records on the stack and enter data in them. It consists of call sequence and return sequence. 1/10/2019 \course\cpeg421-08s\Topic-3-a.ppt

Calling Sequences (Con’t) • A call sequence allocates an activation record and enters information into its fields. -- parameters, return address, old stack top, saving registers, local data, etc. • A return sequence restores the state of the machine -- return value, restore registers, restore old stack top, branch to return address • The code in calling sequence is often divided between the caller and the callee. 1/10/2019 \course\cpeg421-08s\Topic-3-a.ppt

Calling Sequence Implementation: Approach I More responsibilities are in caller side. Call sequence: • Caller evaluates actual parameters • Caller stores a return address and the old stack top into callee’s AR. • Caller pushes the AR (i.e. increment top_sp) • Callee saves register values and other status information • Callee initializes local data and begin execution 1/10/2019 \course\cpeg421-08s\Topic-3-a.ppt

Calling Sequence Implementation: Approach I (Con’t) Return sequence: • Callee places a return value next to the AR of the caller. • Callee restores top_sp and other registers • Callee branches to the return address • Caller can copy return value into its own AR 1/10/2019 \course\cpeg421-08s\Topic-3-a.ppt

Calling Sequence Implementation: Approach I (Con’t) Actual parameters Return values Optional control link Optional access link Save machine status Local Data Temporaries Caller’s AR top_sp Caller’s responsibility Caller put actual parameters Control link access link Return address top_sp 1/10/2019 \course\cpeg421-08s\Topic-3-a.ppt

Calling Sequence Implementation: Approach I (Con’t) Actual parameters Return values Optional control link Optional access link Save machine status Local Data Temporaries Caller’s AR Caller’s responsibility Caller put actual parameters Control link access link Return address Callee’s AR top_sp top_sp Local Data Callee’s responsibility 1/10/2019 \course\cpeg421-08s\Topic-3-a.ppt Temporaries

Calling Sequence Implementation: Approach I (Con’t) Actual parameters Return values Optional control link Optional access link Save machine status Local Data Temporaries Caller’s AR Caller put actual parameters Return values Control link access link Return address Callee’s AR top_sp top_sp Local Data 1/10/2019 \course\cpeg421-08s\Topic-3-a.ppt Temporaries

Calling Sequence Implementation: Approach II (MIPS) More responsibilities are in callee side. Call sequence: 1. Push onto the register save field for any temporary registers ($t0-$t9) that contain values that must be saved. The callee procedure might change these registers. 2. Put first 4 argument values into $a0-$a3 and others into incoming argument field. 3. Call the subroutine 1/10/2019 \course\cpeg421-08s\Topic-3-a.ppt

Calling Sequence Implementation: Approach II (MIPS) In callee: Prolog (done by the callee at its beginning): 1. If this procedure might call other procedures, save return address onto the stack. 2. Push onto the stack any saved registers ($s0-$s7) that this procedure might alter. Callee Body: 1. The procedure may alter any register that it saved in the prolog. 2. If the procedure calls another procedure, then it does so by following above rules. 1/10/2019 \course\cpeg421-08s\Topic-3-a.ppt

Calling Sequence Implementation: Approach II (MIPS) Return sequence: Epilog (done by the procedure just before it returns to the caller): 1. Put returned values in registers($v0-$v1) 2. Pop from the stack (in reverse order) any registers ($s0-$s7) that were pushed in the prolog. 3. If it was pushed in the prolog, pop the return address from the stack into register $ra. 4. Return to the caller using jr $ra. 1/10/2019 \course\cpeg421-08s\Topic-3-a.ppt

Calling Sequence Implementation: Approach II (MIPS) A frame of Implementation code sequence: Procedure Call Regain control from procedure Prolog of procedure Procedure body Epilog of procedure 1/10/2019 \course\cpeg421-08s\Topic-3-a.ppt

Parameter passing modes Parameters Names that appear in the declaration of a procedure are formal parameters. Variables and expressions that are passed to a procedure are actual parameters (or arguments) Parameter passing modes Call by value Call by reference Copy-restore Call by name 1/10/2019 \course\cpeg421-08s\Topic-3-a.ppt

Call-by-Value An evaluation strategy where arguments are evaluated before the procedure is entered. Only the values of the arguments are passed and changes to the arguments within the called procedure have no effect on the actual arguments as seen by the caller. 1/10/2019 \course\cpeg421-08s\Topic-3-a.ppt

Features of Call-by-Value 1. The actual parameters are evaluated and their r-values are passed to the called procedure. 2. A procedure called by value can affect its caller either through nonlocal names or through pointers. 3. Parameters in C are always passed by value. Array is unusual, what is passed by value is a pointer. 4. Pascal uses pass by value by default, but var parameters are passed by reference. 1/10/2019 \course\cpeg421-08s\Topic-3-a.ppt

Call-by-Reference An argument passing convention where the address of an argument variable is passed to a procedure. Execution of the procedure may have side-effect on the actual argument as seen by the caller. The C language's "&" (address of) and "*" (dereference) operators allow the programmer to code explicit call-by-reference. Other languages provide special syntax to declare reference arguments. 1/10/2019 \course\cpeg421-08s\Topic-3-a.ppt

Features of Call-by-Reference 1. Also known as call-by-address or call-by-location. The caller passes to the called procedure the l-value of the parameter. 2. If the parameter is an expression, then the expression is evaluated in a new location, and the address of the new location is passed. 3. Parameters in Fortran are passed by reference. 1/10/2019 \course\cpeg421-08s\Topic-3-a.ppt

Parameter Passing: An Example main() { int i =10; fun( ); printf(“i=%d\n”, i); } fun( ) int { = 20; Call-by–value: print: i = 10; i Call-by–reference: Print: i = 10 or 20 ? x Print: i = 10 x How can i = 20? Why? x 1/10/2019 \course\cpeg421-08s\Topic-3-a.ppt *x &i *x

Copy-Restore 1. A hybrid between call-by-value and call-by reference. 2. The actual parameters are evaluated and their r-values are passed as in call-by-value. In addition, l-values are determined before the call. 3. When control returns, the current r-values of the formal parameters are copied back into the l-values of the actual parameters. 1/10/2019 \course\cpeg421-08s\Topic-3-a.ppt

Why Copy-Restore Copy-Restore avoids the problem when a procedure call has more than one way to access a variable. In this example, call-by-reference will print 0, copy-restore will print 2. program A int a; program f(var x) { x:=2; a:=0; } { a:=1; f(a); print(a) } 1/10/2019 \course\cpeg421-08s\Topic-3-a.ppt

Call-by-Name 1. The actual parameters literally substituted for the formals. This is like a macro-expansion or in-line expansion. 2. Call-by-name is not used in practice. However, the conceptually related technique of in-line expansion is commonly used. 3. In-lining may be one of the most effective optimization transformations if they are guided by execution profiles. 1/10/2019 \course\cpeg421-08s\Topic-3-a.ppt