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?

Similar documents
Scope: Global and Local. Concept of Scope of Variable

Chapter 14 Functions

Chapter 17 Recursion

Chapter 17 Recursion

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

Copyright The McGraw-Hill Companies, Inc. Persion required for reproduction or display. What is Recursion?

What is Recursion? Chapter 17 Recursion. Executing RunningSum. High-Level Example: Binary Search

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

Recursion. Chapter 17 CMPE13. Cyrus Bazeghi

Implementing Functions at the Machine Level

G Programming Languages - Fall 2012

CIT Week13 Lecture

Functions. CS10001: Programming & Data Structures. Sudeshna Sarkar Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

LECTURE 19. Subroutines and Parameter Passing

Midterm 2 Review Chapters 4-16 LC-3

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

Wednesday, October 15, 14. Functions

Implementing Procedure Calls

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

Anne Bracy CS 3410 Computer Science Cornell University

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

EECE.3170: Microprocessor Systems Design I Summer 2017 Homework 4 Solution

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

CSE Lecture In Class Example Handout

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

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

Assembly Language: Function Calls" Goals of this Lecture"

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

Assembly Language: Function Calls" Goals of this Lecture"

THEORY OF COMPILATION

Assembly Language: Function Calls

CA Compiler Construction

Topic 7: Activation Records

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

Code Generation. Lecture 12

Course Administration

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

Assembly Language: Function Calls. Goals of this Lecture. Function Call Problems

MIPS Procedure Calls. Lecture 6 CS301

Anne Bracy CS 3410 Computer Science Cornell University

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

CSC 2400: Computing Systems. X86 Assembly: Function Calls

CS61C : Machine Structures

Run-time Environment

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

Procedure-Calling Conventions October 30

System Software Assignment 1 Runtime Support for Procedures

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

Project Compiler. CS031 TA Help Session November 28, 2011

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

Anne Bracy CS 3410 Computer Science Cornell University

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

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

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

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

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

Assembly Language: Function Calls

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

Chapter 10 Memory Model for Program Execution. Problem

CS 31: Intro to Systems Functions and the Stack. Martin Gagne Swarthmore College February 23, 2016

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.

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

Winter Compiler Construction T11 Activation records + Introduction to x86 assembly. Today. Tips for PA4. Today:

Programming Languages

CS61C : Machine Structures

Procedure Call and Return Procedure call

Lecture 7: Binding Time and Storage

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

The Activation Record (AR)

Compilation /15a Lecture 7. Activation Records Noam Rinetzky

Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004

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

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

Lecture 7: Procedures and Program Execution Preview

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

CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11

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

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

CPSC 213. Introduction to Computer Systems. Procedures and the Stack. Unit 1e Feb 11, 13, 15, and 25. Winter Session 2018, Term 2

EE 3170 Microcontroller Applications

Procedure Calls. Young W. Lim Mon. Young W. Lim Procedure Calls Mon 1 / 29

Lecture #16: Introduction to Runtime Organization. Last modified: Fri Mar 19 00:17: CS164: Lecture #16 1

Subprograms: Local Variables

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

Hakim Weatherspoon CS 3410 Computer Science Cornell University

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

Language of the Machine Recursive functions

Procedure Calls. Young W. Lim Sat. Young W. Lim Procedure Calls Sat 1 / 27

Chap. 8 :: Subroutines and Control Abstraction

Subprograms, Subroutines, and Functions

CS 61c: Great Ideas in Computer Architecture

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

The code generator must statically assign a location in the AR for each temporary add $a0 $t1 $a0 ; $a0 = e 1 + e 2 addiu $sp $sp 4 ; adjust $sp (!

Procedures and Stacks

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

Storage in Programs. largest. address. address

Run Time Environments

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

MIPS Functions and Instruction Formats

Chapter 9 :: Subroutines and Control Abstraction

Transcription:

Memory Allocation in C Functions in C Global data pointer: R4 Global and static variables Specify positive offsets Frame pointer: Points to current code block Negative offset Stack Pointer: Top of stack x0000 R4 Code Constant Data Alterable Data Heap Return address: R7 Stack xffff C to LC3 Code generation Next.. Complete and submit C to LC3 code generation Linked from Lectures ebpage Ho to handle function calls? Caller save and callee save concepts again! Where to store the data? Example C code translated to LC3 Finally: hat can go rong? 1

Functions in C Implementing Functions Functions in C Declaration (also called prototype) int Factorial(int n); Implementing Functions in C Run-time Stack, Getting It All to Work, Tying it All Together type of return value name of function types of all arguments Recursion ho? Function call -- used in expression a = x + Factorial(f + g); 1. evaluate arguments 2, execute function 3. use return value in expression Function Definition State type, name, types of arguments must match function declaration give name to each argument (doesn't have to match declaration) int Factorial(int n) int i; int result = 1; for (i = 1; i <= n; i++) result *= i; return result; Local variables gives control back to calling function and returns value Functions calling functions int mult(int a, int b) int c=0 ; hile (b > 0) c=c+a ; b=b-1 ; We ve ritten our on er and multiplication functions We ll trace these through the stack in this lecture int (int a, int p) int c ; for (c = 1; p > 0; p--) c = mult(c, a) ; // performs: c=c*a int () int a=2,b=3,c=0; c = (a, b) ; // performs: c=a^b CS 2461 2

Input Parameters int mult(int a, int b) int c=0 ; hile (b > 0) c=c+a ; b=b-1 ; int (int a, int p) int c ; for (c = 1; p > 0; p--) c = mult(c, a) ; int () int a=2,b=3,c=0; c = (a, b) ; // performs: 2^3 CS 2461 Input Parameters In MULT: a and b are input params In POW: a and p are input params a is not the same in both functions Local Variables int mult(int a, int b) int c=0 ; hile (b > 0) c=c+a ; b=b-1 ; int (int a, int p) int c ; for (c = 1; p > 0; p--) c = mult(c, a) ; int () int a=2,b=3,c=0; c = (a, b) ; // performs: 2^3 CS 2461 Local Variables In MULT: c local variable In POW: c (a different one) is local var In MAIN: c (also different) is local var Return Values int mult(int a, int b) int c=0 ; hile (b > 0) mult and return values of type int c=c+a ; b=b-1 ; int (int a, int p) int c ; for (c = 1; p > 0; p--) c = mult(c, a) ; int () int a=2,b=3,c=0; c = (a, b) ; // performs: 2^3 CS 2461 Function Calls, Arguments, and Return Values int mult(int a, int b) int c=0 ; hile (b > 0) c=c+a ; b=b-1 ; int (int a, int p) int c ; for (c = 1; p > 0; p--) c = mult(c, a) ; int () int a=2,b=3,c=0; c = (a, b) ; // performs: 2^3 CS 2461 calls mult ith arguments c and a mult returns final value of c to 3

Passing Parameters By Value int mult(int a, int b) int c=0 ; hile (b > 0) c=c+a ; b=b-1 ; int (int a, int p) int c ; for (c = 1; p > 0; p--) c = mult(c, a) ; int () int a=2,b=3,c=0; c = (a, b) ; // performs: 2^3 CS 2461 passes c and a to mult by value Value of s a is bound to local name b in mult In mult, b is a local variable and can be modified (b = b-1) When mult returns, a in is unaffected Locals, Parameters, Arguments, and Return Values int mult(int a, int b) int c=0 ; hile (b > 0) c=c+a ; b=b-1 ; int (int a, int p) int c ; for (c = 1; p > 0; p--) c = mult(c, a) ; int () int a=2,b=3,c=0; c = (a, b) ; // performs: 2^3 CS 2461 One function s local variable is another s parameter One function s return value is another s local variable Ho do e organize all of these to tain order? Ho do e keep locals vars, parameters, arguments, RVs straight? Function calls.. What needs to be done? Caller can pass parameters to the function Function returns a value Function needs to return to caller PC needs to be stored pointer to variables used by caller needs to be restored Function uses local variables, so allocate space for these variables Ne scope (i.e., ne frame pointer) Function can be called from another function model this behaviour and capture all this information in an Activation Record 4

Activation Record/Stack Frame Function call results in activation record pushed on stack Function return results in activation record popped off stack Allos for recursion Place to keep Parameters Local (auto) variables Register spillage Return address Return value Run-Time Stack Recall that local variables are stored on the run-time stack in an activation record Frame pointer () points to the beginning of a region of activation record that stores local variables for the current function When a ne function is called, its activation record is pushed on the stack; hen it returns, its activation record is popped off of the stack. Old frame pointer Run-Time Stack Stack Frames (Activation Records) Memory Memory Memory Watt Before call During call After call Stack generally managed in function-sized chunks Called frames or activation records Function call pushes frame of called function onto stack Function return pops frame off of stack This all happens in run-time mult mult calls calls mult mult mult returns calls returns mult In C, top-level user function is alays 5

Frame Pointer () and Stack Pointer () LC3 uses to more registers as part of calling convention is the stack pointer (SP), points to current top of stack is the frame pointer (FP), points to bottom of current frame Sometimes called base pointer (BP) Activation Record Bookkeeping Return value space for value returned by function allocated even if function does not return a value calls calls mult mult mult returns Return address save pointer to next instruction in calling function convenient location to store R7 in case another function (JSR) is called Dynamic link caller s frame pointer used to pop this activation record from stack In x86, esp and ebp are the stack and base pointer registers Finally Stack Frame Layout (Activation Records) In caller s stack frame: addresses > Caller s saved frame pointer return address/value arguments In running function s stack frame: addresses <= Local variables temporaries arguments to running function s callees temporaries, arguments to callees local variables caller s frame pointer return address return value arguments Activation Record int NoName(int a, int b) int, x, y;... return y; bookkeeping Name Type Offset Scope a b x y int int int int int 4 5 0-1 -2 NoName NoName NoName NoName NoName y x dynamic link return address return value a b locals args 6

Calling Convention Caller and Callee: Who does hat? Compilers typically compile functions separately Generate assembly for (), mult(), and () independently Why? They may be in different files, mult may be in a library, etc. This necessitates use of calling convention Some standard format for arguments and return values Allos separately compiled functions to call each other properly In LC-3, e ve seen part of its calling convention: R7=return address Calling convention is function of HLL, ISA, and compiler Why code compiled by different compilers may not inter-operate Caller Puts arguments onto stack (R L) Does a JSR (or JSRR) to function Callee Makes space for Return Value and Return Address (and saves Return address) makes space for and saves old FP (Frame Pointer) Why? Makes FP point to next space Moves SP enough for all local variables Starts execution of "ork" of function Who does hat? Who does hat? Callee (continued) As registers are needed their current contents can be spilled onto stack When computation done Bring SP back to base Restore FP (adjust SP) Restore RA (adjust SP) Leave SP pointing at return value RET Caller Grabs return value and does the right thing ith it! 7

Implementing Functions: Overvie Activation record information about each function, including arguments and local variables stored on run-time stack Calling function push ne activation record copy values into arguments call function get result from stack Called function Push return address and Dynamic link, return val execute code put result in activation record pop activation record return addr ess return int int a,b; b=foo(a); b=bar(a,b); int bar(int q, int r) int k; int m; return k; int foo(int a) int ; = bar(,); return ; Example : Function Call Sho contents of stack/memory at each time: Time 1 Time 4 Time 3 Time 2 =#3000 (for ) Example : Addresses on stack At each time frame pointer for = 3000 int int a,b; b=foo(a); b=bar(a,b); int bar(int q, int r) int k; int m; return k; Example: Calling the Function int foo(int a) int,x,y; = bar(x,y); return ; 8

Step 2: Calling the Function- LC3 code = bar(x,y); ; push arguments ; call subroutine JSR bar ne xfd00 Note: Caller needs to kno number and type of arguments, doesn't kno about local variables. It needs to push the arguments and then JSR q r x y = bar(, ); ; push second arg AND R0, R0, #0 ADD R0, R0, # ADD,, #-1 STR R0,, #0 ; push first argument LDR R0,, #0 ADD,, #-1 STR R0,, #0 ; call subroutine JSR bar Calling the Function ne xfd00 Note: Caller needs to kno number and type of arguments, doesn't kno about local variables. It needs to push the arguments and then JSR q r a Next steps: starting Callee function Question 4: Starting the Callee Function Create space for return value Store return address Store frame pointer Set ne frame pointer Set space for local variables ; leave space for return value ne ne ; push return address. ; push (caller s frame ptr) ; set ne frame pointer. ; allocate space for locals. X??? x30 m k q r x y int bar(int q, int r) int k; int m; return k; xfd00 9

Starting the Callee Function Returning from function steps ; leave space for return valuene ADD,, #-1 ne ; push return address ADD,, #-1 STR R7,, #0 ; push (caller s frame ptr) ADD,, #-1 STR,, #0 ; set ne frame pointer ADD,, #-1 ; allocate space for locals ADD,, #-2 int bar(int q, int r) int k; int m; return k; xfd00 xfcfb x30 m k q r a Write return value return address into R7 Restore old frame pointer Pop local variables Where should top of stack point to after RET? Ending the Callee Function Ending the Callee Function return k; ; copy k into return value ; pop local variables ; pop dynamic link (into ) ; pop return addr (into R7) ; return control to caller RET ne ne No e understand hy sap xfd00 Did not ork! In C arguments are passed by value -43 217 xfcfb x30 217 m k q r return k; ; copy k into return value LDR R0,, #0 STR R0,, #3 ; pop local variables ADD,, #1 ; pop dynamic link (into ) LDR,, #0 ADD,, #1 ; pop return addr (into R7) LDR R7,, #0 ADD,, #1 ; return control to caller RET ne ne xfd00-43 217 xfcfb x30 217 m k q r a

Back to caller steps What should caller do no? Resuming the Caller Function = bar(,); Get return value Clear arguments JSR bar ; load return value (top of stack) LDR R0,, #0 ne ; perform assignment STR R0,, #0 ; pop return value ADD,, #1 ; pop arguments ADD,, #2 xfd00 217 217 q r a And We re Done at least ith that call/return sequence Stack frame back to here it as A lot of extra instructions involved in function call Many compilers try to inline functions Expand function body at call site Removes most of call overhead Introduces other overheads Multiple static copies of same function Prologue, Body, Epilogue Steps at start of function that e sa are called function prologue Setup code compiler generates automatically One of the (fe) abstractions C provides over assembly More sophisticated compilers can generate tighter prologues Code that follos is translation of function body lcc does this statement-by-statement Results in many inefficiencies More sophisticated compilers vie entire function (at least) When explicit body finishes, need function epilogue Cleanup code compiler generates automatically epilogue (uninding/popping of the stack) 11

Things to notice Caller and Callee Saved Registers 1) Arguments are pushed onto stack right-to-left So that first argument from left is closest to callee This is called C convention (left-to-right is called PASCAL) Needed for functions ith variable argument counts (e.g., printf) 2) C is pass-by-value (not pass-by-reference) Functions receive copies of local variables Recall, arguments to functions ere copies of local vars Protects local variables from being modified accidentally 3) We see hy variables must be declared at start of function Size of static/automatic variables are knon at compile time: ADD,, #-1 ; allocate space for local vars Also, compiler may compile line-by-line, hence right up front!,, and R7 actively participate in call/return sequence What happens to R0 R4 across call? Callee saved : callee saves/restores if it ants to use them Caller saved : caller saves/restores if it cares about values Turns out LCC doesn t have a convention for these??? Doesn t have to (because it compiles statement-by-statement) At the end of every statement, all local variables are on stack R0 R4 are used just as temporary storage ithin expressions Highly inefficient Register allocation: assign locals to registers too Avoid many unnecessary loads and stores to stack All real compilers do this Summary of LC-3 Function Call Implementation 1. Caller pushes arguments (last to first). 2. Caller invokes subroutine (JSR). 3. Callee allocates return value, pushes R7 and. 4. Callee allocates space for local variables. 5. Callee executes function code. 6. Callee stores result into return value slot. 7. Callee pops local vars, pops, pops R7. 8. Callee returns (JMP R7). 9. Caller loads return value and pops arguments.. Caller resumes computation For details.. Read Chapter 14 section 14.3, Figure 14.8 for full implementation of the function call process Check out the lcc cross compiler 12

Question What can go rong? What if the return address as overritten Where does program return to? Recall privilege level hat if program as operating as root? Will level change? Buffer overflo attack/stack smashing attack n i Example: Running sum ( ) 1 Mathematical Definition: RunningSum(1) = 1 RunningSum(n) = n + RunningSum(n-1) What is Recursion? A recursive function is one that solves its task by calling itself on smaller pieces of data. Similar to recurrence function in mathematics. Question: Ho do you prove correctness of recurrence function?? Like iteration -- can be used interchangeably; sometimes recursion results in a simpler solution. Recursive Function: int RunningSum(int n) if (n == 1) return 1; else return n + RunningSum(n-1); Executing RunningSum Example: Fibonacci Numbers res = RunningSum(4); return value = RunningSum(4) return 4 + RunningSum(3); return value = 6 RunningSum(3) return 3 + RunningSum(2); return value = 3 RunningSum(2) return 2 + RunningSum(1); return value = 1 RunningSum(1) return 1; Mathematical Definition: f ( n) f ( n 1) f ( n 2) f (1) 1 f (0) 1 In other ords, the n-th Fibonacci number is the sum of the previous to Fibonacci numbers. To compute value of F(n) e need values of F(n-1) and F(n- 2) 13

Ho is recursion implemented? Do e need to do anything different from ho e handled function calls? No! Activation record for each instance/call of Fibonacci! Fibonacci: C Code int Fibonacci(int n) if ((n == 0) (n == 1)) return 1; else return Fibonacci(n-1) + Fibonacci(n-2); Activation Records Activation Records (cont.) Whenever Fibonacci is invoked, a ne activation record is pushed onto the stack. calls Fibonacci(3) Fibonacci(3) calls Fibonacci(2) Fib(2) Fibonacci(2) calls Fibonacci(1) Fib(1) Fib(2) Fibonacci(1) returns, Fibonacci(2) calls Fibonacci(0) Fib(0) Fib(2) Fibonacci(2) returns, Fibonacci(3) calls Fibonacci(1) Fib(1) Fibonacci(3) returns Fib(3) Fib(3) Fib(3) Fib(3) Fib(3) 14

Tracing the Function Calls If e are debugging this program, e might ant to trace all the calls of Fibonacci. Note: A trace ill also contain the arguments passed into the function. For Fibonacci(3), a trace looks like: Fibonacci(3) Fibonacci(2) Fibonacci(1) Fibonacci(0) Fibonacci(1) Fibonacci: LC-3 Code Activation Record read Chapter 17 Fibonacci example bookkeeping temp dynamic link return address return value n local arg Compiler generates temporary variable to hold result of first Fibonacci call. What ould trace of Fibonacci(4) look like? Function Calls -- Summary Activation records keep track of caller and callee variables Stack structure What happens if e accidentally overrite the return address? Next: Pointers, Arrays, Dynamic data structures and the heap 15