Topic 3-a. Calling Convention 2/29/2008 1

Similar documents
Subroutines. int main() { int i, j; i = 5; j = celtokel(i); i = j; return 0;}

An Overview to Compiler Design. 2008/2/14 \course\cpeg421-08s\topic-1a.ppt 1

MIPS Procedure Calls - Review

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

Memory Usage 0x7fffffff. stack. dynamic data. static data 0x Code Reserved 0x x A software convention

Course Administration

Programming Languages

Instruction Set Architectures (4)

ECE260: Fundamentals of Computer Engineering

Chap. 8 :: Subroutines and Control Abstraction

Do-While Example. In C++ In assembly language. do { z--; while (a == b); z = b; loop: addi $s2, $s2, -1 beq $s0, $s1, loop or $s2, $s1, $zero

Lecture 5. Announcements: Today: Finish up functions in MIPS

ECE232: Hardware Organization and Design

Chapter 9 :: Subroutines and Control Abstraction

Module 27 Switch-case statements and Run-time storage management

Functions in MIPS. Functions in MIPS 1

CA Compiler Construction

comp 180 Lecture 10 Outline of Lecture Procedure calls Saving and restoring registers Summary of MIPS instructions

Code Generation. The Main Idea of Today s Lecture. We can emit stack-machine-style code for expressions via recursion. Lecture Outline.

We can emit stack-machine-style code for expressions via recursion

Lectures 5. Announcements: Today: Oops in Strings/pointers (example from last time) Functions in MIPS

Today. Putting it all together

Chapter 8 :: Subroutines and Control Abstraction. Final Test. Final Test Review Tomorrow

Functions and Procedures

ECE260: Fundamentals of Computer Engineering. Supporting Procedures in Computer Hardware

Implementing Procedure Calls

2/16/2018. Procedures, the basic idea. MIPS Procedure convention. Example: compute multiplication. Re-write it as a MIPS procedure

Compilers and computer architecture: A realistic compiler to MIPS

EE 361 University of Hawaii Fall

Calling Conventions. Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University. See P&H 2.8 and 2.12

SPIM Procedure Calls

MIPS Programming. A basic rule is: try to be mechanical (that is, don't be "tricky") when you translate high-level code into assembler code.

Compiling Code, Procedures and Stacks

Code Generation. Lecture 19

Lecture 5: Procedure Calls

Lecture 9: Procedures & Functions. CS 540 George Mason University

CPS311 Lecture: Procedures Last revised 9/9/13. Objectives:

The plot thickens. Some MIPS instructions you can write cannot be translated to a 32-bit number

EN164: Design of Computing Systems Lecture 11: Processor / ISA 4

Code Generation. Lecture 12

Procedure Call and Return Procedure call

G Programming Languages - Fall 2012

The plot thickens. Some MIPS instructions you can write cannot be translated to a 32-bit number

Lecture 5: Procedure Calls

Concepts Introduced in Chapter 7

Review of Activation Frames. FP of caller Y X Return value A B C

MIPS Procedure Calls. Lecture 6 CS301

Procedures and Stacks

Control Abstraction. Hwansoo Han

CENG3420 Computer Organization and Design Lab 1-2: System calls and recursions

Special Topics: Programming Languages

Instruction Set Architecture

System Software Assignment 1 Runtime Support for Procedures

MIPS Assembly (Functions)

Branch Addressing. Jump Addressing. Target Addressing Example. The University of Adelaide, School of Computer Science 28 September 2015

CSE Lecture In Class Example Handout

Procedure Calls Main Procedure. MIPS Calling Convention. MIPS-specific info. Procedure Calls. MIPS-specific info who cares? Chapter 2.7 Appendix A.

Lecture Outline. Topic 1: Basic Code Generation. Code Generation. Lecture 12. Topic 2: Code Generation for Objects. Simulating a Stack Machine

Topic 7: Activation Records

CS 316: Procedure Calls/Pipelining

Subroutines and Control Abstraction. CSE 307 Principles of Programming Languages Stony Brook University

Calling Conventions. See P&H 2.8 and Hakim Weatherspoon CS 3410, Spring 2013 Computer Science Cornell University

CSC 2400: Computer Systems. Using the Stack for Function Calls

Run-Time Environments

The Activation Record (AR)

Runtime management. CS Compiler Design. The procedure abstraction. The procedure abstraction. Runtime management. V.

Function Calls. 1 Administrivia. Tom Kelliher, CS 240. Feb. 13, Announcements. Collect homework. Assignment. Read

Q1: /20 Q2: /30 Q3: /24 Q4: /26. Total: /100

Stack Frames. September 2, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 September 2, / 15

Chapter 2. Computer Abstractions and Technology. Lesson 4: MIPS (cont )

CS64 Week 5 Lecture 1. Kyle Dewey

Run Time Environment. Procedure Abstraction. The Procedure as a Control Abstraction. The Procedure as a Control Abstraction

CS 61c: Great Ideas in Computer Architecture

More C functions and Big Picture [MIPSc Notes]

Run Time Environments

CSC 8400: Computer Systems. Using the Stack for Function Calls

Implementing Subroutines. Outline [1]

MIPS function continued

Function Calls. Tom Kelliher, CS 220. Oct. 24, SPIM programs due Wednesday. Refer to homework handout for what to turn in, and how.

Control Instructions. Computer Organization Architectures for Embedded Computing. Thursday, 26 September Summary

Control Instructions

Mark Redekopp, All rights reserved. EE 352 Unit 6. Stack Frames Recursive Routines

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

CSE 504. Expression evaluation. Expression Evaluation, Runtime Environments. One possible semantics: Problem:

Run-time Environment

Wednesday, October 15, 14. Functions

ECE331: Hardware Organization and Design

! Those values must be stored somewhere! Therefore, variables must somehow be bound. ! How?

Separate compilation. Topic 6: Runtime Environments p.1/21. CS 526 Topic 6: Runtime Environments The linkage convention

ECE 30 Introduction to Computer Engineering

Function Calling Conventions 2 CS 64: Computer Organization and Design Logic Lecture #10

CS153: Compilers Lecture 8: Compiling Calls

COMP2611: Computer Organization MIPS function and recursion

MIPS Assembly (FuncDons)

Computer Systems and Networks

Mechanisms in Procedures. CS429: Computer Organization and Architecture. x86-64 Stack. x86-64 Stack Pushing

Stack Frames. Compilers use the stack: to store the to to a subroutine for storage of declared in the subroutine and a place to

CSC 2400: Computing Systems. X86 Assembly: Function Calls"

CSCE 5610: Computer Architecture

CSC 2400: Computer Systems. Using the Stack for Function Calls

Transcription:

Topic 3-a Calling Convention 2/29/2008 1

Calling Convention Calling convention is a standardized method for a program to pass parameters to a procedure and receive result values back from it. 2/29/2008 \course\cpeg421-08s\topic-3-a.ppt 2

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. 2/29/2008 \course\cpeg421-08s\topic-3-a.ppt 3

Issues in Calling Convention Register usage convention Calling sequence Parameter passing Local data layout 2/29/2008 \course\cpeg421-08s\topic-3-a.ppt 4

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) 2/29/2008 \course\cpeg421-08s\topic-3-a.ppt 5

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 2/29/2008 \course\cpeg421-08s\topic-3-a.ppt 6

Caller save Register Saving (Con t) 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. 2/29/2008 \course\cpeg421-08s\topic-3-a.ppt 7

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! 2/29/2008 \course\cpeg421-08s\topic-3-a.ppt 8

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 2/29/2008 \course\cpeg421-08s\topic-3-a.ppt 9

Caller Save/Callee Save Trade-Off caller save temporary registers callee save saved registers 2/29/2008 \course\cpeg421-08s\topic-3-a.ppt 10

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

Callee Save example Function A Add $10, $11,$12 Jal B Add $11, $2, $12 Mul $12, $10, $13 Function B 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 2/29/2008 \course\cpeg421-08s\topic-3-a.ppt 12

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

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. 2/29/2008 \course\cpeg421-08s\topic-3-a.ppt 14

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. 2/29/2008 \course\cpeg421-08s\topic-3-a.ppt 15

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 2/29/2008 \course\cpeg421-08s\topic-3-a.ppt 16

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 2/29/2008 \course\cpeg421-08s\topic-3-a.ppt 17

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

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

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

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 2/29/2008 \course\cpeg421-08s\topic-3-a.ppt 21

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. 2/29/2008 \course\cpeg421-08s\topic-3-a.ppt 22

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. 2/29/2008 \course\cpeg421-08s\topic-3-a.ppt 23

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 2/29/2008 \course\cpeg421-08s\topic-3-a.ppt 24

Parameter Passing 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 2/29/2008 \course\cpeg421-08s\topic-3-a.ppt 25

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. 2/29/2008 \course\cpeg421-08s\topic-3-a.ppt 26

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. 2/29/2008 \course\cpeg421-08s\topic-3-a.ppt 27

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. 2/29/2008 \course\cpeg421-08s\topic-3-a.ppt 28

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. 2/29/2008 \course\cpeg421-08s\topic-3-a.ppt 29

Parameter Passing: An Example main() { int i =10; fun( i ); printf( i=%d\n, i); } fun( x) int x { x = 20; Why? Call-by value: print: i = 10; Call-by reference: Print: i = 10 or 20? Print: i = 10 How can i = 20? } 2/29/2008 \course\cpeg421-08s\topic-3-a.ppt 30

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. 2/29/2008 \course\cpeg421-08s\topic-3-a.ppt 31

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

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. 2/29/2008 \course\cpeg421-08s\topic-3-a.ppt 33