Wednesday, October 15, 14. Functions

Similar documents
System Software Assignment 1 Runtime Support for Procedures

G Programming Languages - Fall 2012

Code Generation & Parameter Passing

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

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

2/6/2018. Let s Act Like Compilers! ECE 220: Computer Systems & Programming. Decompose Finding Absolute Value

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

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

CS558 Programming Languages

CS558 Programming Languages Winter 2018 Lecture 4a. Andrew Tolmach Portland State University

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

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

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

Course Administration

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?

CIT Week13 Lecture

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

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

Functions in MIPS. Functions in MIPS 1

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

MIPS Procedure Calls. Lecture 6 CS301

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

Project Compiler. CS031 TA Help Session November 28, 2011

Implementing Functions at the Machine Level

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

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

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

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

SPIM Procedure Calls

Today. Putting it all together

Protection Levels and Constructors The 'const' Keyword

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

MIPS Functions and Instruction Formats

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

ECE 190 Midterm Exam 3 Spring 2011

Run-time Environment

The role of semantic analysis in a compiler

CA Compiler Construction

Announcements. Class 7: Intro to SRC Simulator Procedure Calls HLL -> Assembly. Agenda. SRC Procedure Calls. SRC Memory Layout. High Level Program

Scope: Global and Local. Concept of Scope of Variable

! What do we care about? n Fast program execution. n Efficient memory usage. n Avoid memory fragmentation. n Maintain data locality

Compilation /15a Lecture 7. Activation Records Noam Rinetzky

Implementing Procedure Calls

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

CS 314 Principles of Programming Languages. Lecture 13

CSE Lecture In Class Example Handout

Stacks and Frames Demystified. CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han

Anne Bracy CS 3410 Computer Science Cornell University

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

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

Arguments and Return Values. EE 109 Unit 16 Stack Frames. Assembly & HLL s. Arguments and Return Values

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

Systems I. Machine-Level Programming V: Procedures

EE 361 University of Hawaii Fall

Chapter 14 Functions

11/29/17. Outline. Subprograms. Subroutine. Subroutine. Parameters. Characteristics of Subroutines/ Subprograms

CSE 220: System Fundamentals I Unit 14: MIPS Assembly: Multi-dimensional Arrays. Kevin McDonnell Stony Brook University CSE 220

Instruction Set Architectures (4)

Stacks and Function Calls

Principles of Programming Languages

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

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.

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.

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

CS61C : Machine Structures

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

Lecture 4: Instruction Set Design/Pipelining

Interprocedural Analysis. Motivation. Interprocedural Analysis. Function Calls and Pointers

Summer 2003 Lecture 14 07/02/03

Compiling Code, Procedures and Stacks

Plan. Regression testing: Demo of how to use the regress.sh script.

CS 316: Procedure Calls/Pipelining

Programming Studio #9 ECE 190

Assembly Language: Function Calls

CSC 2400: Computing Systems. X86 Assembly: Function Calls

Princeton University Computer Science 217: Introduction to Programming Systems. Assembly Language: Function Calls

This section provides some reminders and some terminology with which you might not be familiar.

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

Lecture 7: Procedures and Program Execution Preview

Function Calls COS 217. Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site)

Chapter 8: Virtual Machine II: Program Control

Calvin Lin The University of Texas at Austin

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

Code Generation II. Code generation for OO languages. Object layout Dynamic dispatch. Parameter-passing mechanisms Allocating temporaries in the AR

Lecture 5: Procedure Calls

Problem Solving in C. Stepwise Refinement. ...but can stop refining at a higher level of abstraction. Same basic constructs. as covered in Chapter 6

Chapter 10 Memory Model for Program Execution. Problem

Machine Language Instructions Introduction. Instructions Words of a language understood by machine. Instruction set Vocabulary of the machine

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

Weeks 6&7: Procedures and Parameter Passing

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

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

Control Instructions

Machine Programming 3: Procedures

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

Anne Bracy CS 3410 Computer Science Cornell University

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

Virtual Machine (Part II)

MPATE-GE 2618: C Programming for Music Technology. Unit 4.1

IA32 Stack. Lecture 5 Machine-Level Programming III: Procedures. IA32 Stack Popping. IA32 Stack Pushing. Topics. Pushing. Popping

Transcription:

Functions

Terms void foo() { int a, b;... bar(a, b); void bar(int x, int y) {... foo is the caller bar is the callee a, b are the actual parameters to bar x, y are the formal parameters of bar Shorthand: argument = actual parameter parameter = formal parameter

Different kinds of parameters Value parameters Reference parameters Result parameters Value-result parameters Read-only parameters

Value parameters Call-by-value Used in C, Java, default in C++ Passes the value of an argument to the function Makes a copy of argument when function is called Advantages? Disadvantages?

Value parameters int x = 1; void main () { foo(x, x); void foo(int y, int z) { y = 2; z = 3;

Value parameters int x = 1; void main () { foo(x, x); What do the print statements print? void foo(int y, int z) { y = 2; z = 3;

Value parameters int x = 1; void main () { foo(x, x); void foo(int y, int z) { y = 2; z = 3; What do the print statements print? Answer: //prints 1 //prints 1

Reference parameters Call-by-reference Optional in Pascal (use var keyword) and C++ (use & ) Pass the address of the argument to the function If an argument is an expression, evaluate it, place it in memory and then pass the address of the memory location Advantages? Disadvantages?

Reference parameters int x = 1; void main () { foo(x, x); void foo(int &y, int &z) { y = 2; z = 3; print(y);

Reference parameters int x = 1; void main () { foo(x, x); What do the print statements print? void foo(int &y, int &z) { y = 2; z = 3; print(y);

Reference parameters int x = 1; void main () { foo(x, x); void foo(int &y, int &z) { y = 2; z = 3; print(y); What do the print statements print? Answer: //prints 3 //prints 3 print(y); //prints 3!

Result parameters Return values of a function Some languages let you specify other parameters as result parameters these are un-initialized at the beginning of the function Copied at the end of function into the arguments of the caller C++ supports return references int& foo(... ) compute return values, store in memory, return address of return value

Result parameters int x = 1; void main () { foo(x, x); void foo(int y, result int z) { y = 2; z = 3;

Result parameters int x = 1; void main () { foo(x, x); What do the print statements print? void foo(int y, result int z) { y = 2; z = 3;

Result parameters int x = 1; void main () { foo(x, x); void foo(int y, result int z) { y = 2; z = 3; What do the print statements print? Answer: //prints 3 //prints 1

Value-result parameters Copy-in copy-out Evaluate argument expression, copy to parameters After subroutine is done, copy values of parameters back into arguments Results are often similar to pass-by-reference, but there are some subtle situations where they are different

Value-result parameters int x = 1; int w = 1; void main () { foo(w, x); print(w); void foo(int& y, value result int z) { y = 2; z = 3; print(w);

Value-result parameters int x = 1; int w = 1; void main () { foo(w, x); print(w); What do the print statements print? void foo(int& y, value result int z) { y = 2; z = 3; print(w);

Value-result parameters int x = 1; int w = 1; void main () { foo(w, x); print(w); void foo(int& y, value result int z) { y = 2; z = 3; print(w); What do the print statements print? Answer: print(x) //prints 3 print(w) //prints 2 print(x) //prints 1 print(w) //prints 2

What about this? int x = 1; void main () { foo(x, x); void foo(value result int y, value result int z) { y = 2; z = 3;

What about this? int x = 1; void main () { foo(x, x); What do the print statements print? void foo(value result int y, value result int z) { y = 2; z = 3;

What about this? int x = 1; void main () { foo(x, x); void foo(value result int y, value result int z) { y = 2; z = 3; What do the print statements print? Answer: //undefined! //prints 1

Read only parameters Used when callee will not change value of parameters Read-only restriction must be enforced by compiler This can be tricky when in the presence of aliasing and control flow void foo(const int x, int y) { int * p; if (...) p = &x else p = &y *p = 4 Is this legal? Hard to tell! gcc will not let the assignment happen

Esoteric: name parameters Call-by-name Usually, we evaluate the arguments before passing them to the function. In call-by-name, the arguments are passed to the function before evaluation Not used in many languages, but Haskell uses a variant int x = 2; void main () { foo(x + 2); int x = 2; void main () { foo(x + 2); void foo(int y) { z = y + 2; print(z); void foo(int y) { z = x + 2 + 2; print(z);

Why is this useful? int x = 2; void main () { foo(bar()); void foo(int y) { if (... ) { z = y; else { z = 3; print(z); Consider the code on the left Normally, we must evaluate bar() before calling foo() But what if bar() runs for a long time? In call by name, we only evaluate bar() if we need to use it

Other considerations Scalars For call by value, can pass the address of the actual parameter and copy the value into local storage within the procedure Reduces size of caller code (why is this good?) For machines with a lot of registers (e.g., MIPS), compilers will save a few registers for arguments and return types Less need to manipulate stack

Other considerations Arrays For efficiency reasons, arrays should be passed by reference (why?) Java, C, C++ pass arrays by reference by default (technically, they pass a pointer to the array by value) Pass in a fixed size dope vector as the actual parameter (not the whole array!) Callee can copy array into local storage as needed

Dope vectors Remember: store additional information about an array Where it is in memory Size of array # of dimensions Storage order Can sometimes eliminate dope vectors with compile-time analysis

Strings Requires a descriptor Like a dope vector, provides information about string May just need to pass a pointer (if string contains information about its length) May also need to pass information about length

Calling a function What should happen when a function is called? Set the frame pointer (sets the base of the activation record) Allocate space for local variables (use the function s symbol table for this) What about registers? Callee might want to use registers that the caller is using

Saving registers Two options: caller saves and callee saves Caller saves Caller pushes all the registers it is using on to the stack before calling function, restores the registers after the function returns Callee saves Callee pushes all the registers it is going to use on the stack immediately after being called, restores the registers just before it returns Why use one vs. the other? Simple optimizations are good here: don t save registers if the caller/callee doesn t use any

Activation records Caller's responsibility Callee's responsibility Stack Growth Return value Actual parameters Caller's return address Caller's frame pointer Static links (other FPs) Register save area Local variables FP register Is this record generated for calleesaves or caller-saves? How would the other record look?

The frame pointer Manipulate with instructions like link and unlink Link: push current value of FP on to stack, set FP to top of stack Unlink: read value at current address pointed to by FP, set FP to point to that value In other words: link pushes a new frame onto the stack, unlink pops it off

Example Subroutine Call and Stack Frame Lower addr z = SubOne(x,2*y); int SubOne(int a, int b) { int l1, l2; I1 = a; l2 = b; return l1+l2; ; 24

Example Subroutine Call and Stack Frame R6 return value x 2*y return address saved frame ptr l1 l2... stack Lower addr z = SubOne(x,2*y); int SubOne(int a, int b) { int l1, l2; I1 = a; l2 = b; return l1+l2; ; 24

Example Subroutine Call and Stack Frame R6 return value x 2*y return address saved frame ptr l1 l2... stack Lower addr 3-address code: push push x mul 2 y t1 push t1 jsr SubOne pop pop pop z z = SubOne(x,2*y); int SubOne(int a, int b) { int l1, l2; I1 = a; l2 = b; return l1+l2; ; link 3 move $P1 $L1 move $P2 $L2 add $L1 $L2 t2 move t2 $R unlink ret 24

Example Subroutine Call and Stack Frame R6 return value x 2*y return address saved frame ptr l1 l2... stack Lower addr 3-address code: push push x mul 2 y t1 push t1 jsr SubOne pop pop pop z assembly code: push push x load y R1 muli 2 R1 push R1 jsr SubOne pop pop pop R1 store R1 z z = SubOne(x,2*y); int SubOne(int a, int b) { int l1, l2; I1 = a; l2 = b; return l1+l2; ; link 3 move $P1 $L1 move $P2 $L2 add $L1 $L2 t2 move t2 $R unlink ret link R6 3 load 3(R6) R1 store R1-1(R6) load 2(R6) R2 store R2-2(R6) load -1(R6) R1 add -2(R6) R1 store R1 4(R6) unlink ret 24