LECTURE 19. Subroutines and Parameter Passing

Similar documents
G Programming Languages - Fall 2012

Programming Languages

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

Chap. 8 :: Subroutines and Control Abstraction

Control Abstraction. Hwansoo Han

Functions and Procedures

Chapter 9 :: Subroutines and Control Abstraction

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

Lecture 7: Binding Time and Storage

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

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

Compilation /15a Lecture 7. Activation Records Noam Rinetzky

CS153: Compilers Lecture 8: Compiling Calls

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

CS A331 Programming Language Concepts

CS356: Discussion #6 Assembly Procedures and Arrays. Marco Paolieri

Functions in MIPS. Functions in MIPS 1

Procedure and Object- Oriented Abstraction

CSE Lecture In Class Example Handout

CA Compiler Construction

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

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

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

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

Lecture 4: Instruction Set Design/Pipelining

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

Run-Time Environments

Today. Putting it all together

Functions in C. Memory Allocation in C. C to LC3 Code generation. Next.. Complete and submit C to LC3 code generation. How to handle function calls?

Run-time Environment

A Unix process s address space appears to be three regions of memory: a read-only text region (containing executable code); a read-write region

Chapter 9 Subroutines and Control Abstraction. June 22, 2016

EE 361 University of Hawaii Fall

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

Principles of Programming Languages

CS 480 Fall Runtime Environments. Mike Lam, Professor. (a.k.a. procedure calls and heap management)

CS 61c: Great Ideas in Computer Architecture

SPIM Procedure Calls

Lecture 7: Procedures and Program Execution Preview

Course Administration

Instruction Set Architectures (4)

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

System Software Assignment 1 Runtime Support for Procedures

See P&H 2.8 and 2.12, and A.5-6. Prof. Hakim Weatherspoon CS 3410, Spring 2015 Computer Science Cornell University

2/12/2018. Recall Why ISAs Define Calling Conventions. ECE 220: Computer Systems & Programming. Recall the Structure of the LC-3 Stack Frame

Principles of Programming Languages

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

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

CSCE 5610: Computer Architecture

Fast Interrupts. Krste Asanovic, UC Berkeley / SiFive Inc. (Chair) Kevin Chen, Andes (Vice-Chair)

Compilers and Code Optimization EDOARDO FUSELLA

MIPS Functions and Instruction Formats

Chapter 14 Functions. Function. Example of High-Level Structure. Functions in C

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

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

143A: Principles of Operating Systems. Lecture 4: Calling conventions. Anton Burtsev October, 2017

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

Implementing Procedure Calls

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

Lecture 5: Procedure Calls

Chapter 2A Instructions: Language of the Computer

Prof. Kavita Bala and Prof. Hakim Weatherspoon CS 3410, Spring 2014 Computer Science Cornell University. See P&H 2.8 and 2.12, and A.

238P: Operating Systems. Lecture 3: Calling conventions. Anton Burtsev October, 2018

ELEC / Computer Architecture and Design Fall 2013 Instruction Set Architecture (Chapter 2)

CS558 Programming Languages

CS 61C: Great Ideas in Computer Architecture More MIPS, MIPS Functions

Programming Languages

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

MODULE 4 INSTRUCTIONS: LANGUAGE OF THE MACHINE

143A: Principles of Operating Systems. Lecture 5: Calling conventions. Anton Burtsev January, 2017

CSE Lecture In Class Example Handout

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

Run-Time Data Structures

CS 110 Computer Architecture Lecture 6: More MIPS, MIPS Functions

CIT Week13 Lecture

CS61C : Machine Structures

Lab 4 : MIPS Function Calls

ECE232: Hardware Organization and Design

Computer Systems Lecture 9

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

Scope, Functions, and Storage Management

Instruction Set Principles and Examples. Appendix B

Contents. Slide Set 2. Outline of Slide Set 2. More about Pseudoinstructions. Avoid using pseudoinstructions in ENCM 369 labs

SISTEMI EMBEDDED. Stack, Subroutine, Parameter Passing C Storage Classes and Scope. Federico Baronti Last version:

The Activation Record (AR)

COL728 Minor2 Exam Compiler Design Sem II, Answer all 5 questions Max. Marks: 20

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

Lecture 5: Procedure Calls

Anne Bracy CS 3410 Computer Science Cornell University

CS 61C: Great Ideas in Computer Architecture. More MIPS, MIPS Functions

Run Time Environments

What the CPU Sees Basic Flow Control Conditional Flow Control Structured Flow Control Functions and Scope. C Flow Control.

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

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

COMP3221: Microprocessors and. and Embedded Systems. Overview. Variable Types and Memory Sections. Types of Variables in C

Topic 7: Activation Records

Compiler Design Spring 2017

Virtual Machines and Dynamic Translation: Implementing ISAs in Software

2.7 Supporting Procedures in hardware. Why procedures or functions? Procedure calls

Compiling Code, Procedures and Stacks

Transcription:

LECTURE 19 Subroutines and Parameter Passing

ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments behind a simple name. Data abstraction: hide data representation details. Control abstraction: create a well-defined operation by hiding execution details.

SUBROUTINES Subroutines are the principle method of control abstraction in most languages. Subroutines are executed on behalf of a caller to whom the subroutine returns when it is finished. Parameterized: caller provides subroutine with arguments which influence execution of subroutine. Actual parameters: actual arguments passed in a subroutine call instance. Formal parameters: aliases for actual parameters within the subroutine. Functions: subroutines that return values (typically). Procedures: subroutines that do not return values.

SUBROUTINE FRAME An activation record (subroutine frame) is used to store all related information for the execution of a subroutine. Before a subroutine is executed, the frame must be set up and some fields in the frame must be initialized. Formal arguments must be replaced with actual arguments. This is done in the calling sequence, a sequence of instructions before and after a subroutine, to set-up the frame. Temporary storage (e.g. for expression evaluation) Local variables Bookkeeping (e.g. saved CPU registers) Return address Subroutine arguments and returns

SUBROUTINE FRAME With stack allocation, the subroutine frame for the current frame is on top of the stack. Stack pointer (sp): top of the stack. Frame pointer (fp): an address within the top frame. To access non-local variables, a static link (for static scoping) or a dynamic link (dynamic scoping) is maintained in the frame.

CALLING SEQUENCES Maintenance of the subroutine call stack is the responsibility of the calling sequence. In general, calling sequences have three components: The code executed by the caller immediately before and after a subroutine call. Prologue: code executed at the beginning of the subroutine itself. Epilogue: code executed at the end of the subroutine itself. F(a, b); Calling Sequence Code Call F Calling Sequence Code Prologue Body of F Epilogue

CALLING SEQUENCES What needs to be done in the calling sequence? Before the subroutine code can be executed, we need to set up the subroutine frame (calling code before the subroutine call + prologue). Generally speaking, the steps are: Compute the parameters and pass the parameters. Save the return address. Change the program counter. Change sp to allocate space for new frame. Save registers, including fp, which may be overwritten by subroutine. Change fp to allocate new frame. Execute initialization code when needed for objects in new frame, if needed.

CALLING SEQUENCES What needs to be done in the calling sequence? After the subroutine code is executed, remove the subroutine frame (calling code after the subroutine + epilogue). Generally speaking, the steps are: Pass return parameters or function value. Finalization code for local objects. Deallocate the stack frame (restore fp and sp to their previous values). Restore saved registers and program counter.

CALLING SEQUENCES Some of the operations must be performed by the caller, while others can either be done by the caller or callee. For example, passing parameters must be done by the caller because the parameters passed will differ every time a call is made. In general, it s better to have set-up and tear-down operations performed by the callee as much as possible. This cuts down on code generated for subroutine calling as it only needs to appear once in the target program. On the other hand, any tasks that the caller is responsible for appear at every call site.

SAVING AND RESTORING REGISTERS Some registers such as sp and fp are clearly different in and out of a subroutine. For general purpose registers used in the caller and/or callee: main(){ foo(){ addi $s0,$0,0 add $s0,$s1,$s2 } call foo(); mult $s0,$s5 } To execute correctly, $s0 needs to be saved before foo() and restored after the subroutine returns.

SAVING AND RESTORING REGISTERS The compiler should generate code only to save and restore registers that matter. If a subroutine does not use $s0, $s0 does not need to be saved in the calling sequence. Ideally, we should only save registers that are used in both the caller and the callee. Difficult due to separate compilation: no information about callee when compiling caller, and vice versa. Simple solution (with unnecessary save/restore): Option 1: caller saves/restores all registers it uses. Option 2: callee saves/restores all registers it uses. Compromise: partition registers into two sets, one for the caller to save and one for the callee to save.

SAVING AND RESTORING REGISTERS MIPS and x86 processors implement the following compromise: non-special-purpose registers are divided into two sets of roughly equal size. One set is the caller s responsibility, the other set is the callee s responsibility. A callee assumes there is nothing of value in the caller-saved set. A caller assumes the callee will not destroy the contents of the callee-saved set. The compiler will use callee-saved set for long-term values and will use the callersaved set for transient values (unlikely to need to be saved across calls). Result: caller-saved set is rarely saved by either party.

A TYPICAL CALLING SEQUENCE Caller before the call: 1. Save any caller-saved registers whose values will be needed after the call. 2. Compute the values of arguments and move them into the stack or registers. 3. Compute the static link, and pass it as an extra hidden argument. 4. Use a special subroutine call instruction to jump to the subroutine, simultaneously passing the return address on the stack or in a register. Prologue in the callee: 1. Allocate a frame (sp = sp offset). 2. Save old fp on the the stack, update the fp. 3. Save callee-saved registers that may be overwritten in the routine. Epilogue in the callee: 1. Move the return value into a register or a location on the stack. 2. Restore callee-saved registers. 3. Restore fp and sp. 4. Jump back to the return address. Caller after the call: 1. Move the return value to wherever it is needed. 2. Restore caller-saved registers.

QUESTION Why do local variables typically not have a default value (while globals do)? int i; int main(){ } int j; cout << i << j;

ANSWER Global variables (and statics) are allocated a fixed section of memory at compile time so initializing them to zero incurs no real runtime overhead it s a one-time deal when the program is loaded into memory. Local variables, however, may have a different address on the stack every single time they are called which would require a new initialization step every single time space was made for them. It s easier to just leave it up to the programmer to explicitly request some value.

REGISTER WINDOWS An example of hardware support for efficient subroutine execution is Register Windows. Introduced in Berkeley RISC machines. Also used in Sun SPARC and Intel Itanium processors. Maintain multiple sets (a window) of ISA registers using a much larger collection of physical registers. Use a new mapping (set) of registers when making subroutine calls. Setting and resetting a mapping is cheaper than saving and restoring registers. New and old mapping registers overlaps to allow parameter passing.

IN-LINE FUNCTIONS An example of language support for efficient subroutine execution is in-line functions. In C/C++: inline int max(int a, int b) {return a> b? a: b;} Such functions are not real functions, the routine body is expanded in-line at the point of call. A copy of the routine body becomes a part of the caller. No actual routine call occurs. Will inline function always improve performance? Maybe, maybe not. Many other factors: e.g. code size affecting cache/memory behavior. Most compilers decide on their own whether to use in-line or call conventionally.