Assignment 11: functions, calling conventions, and the stack

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

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

Assembly Language: Function Calls

Assembly Language: Function Calls" Goals of this Lecture"

Question 4.2 2: (Solution, p 5) Suppose that the HYMN CPU begins with the following in memory. addr data (translation) LOAD 11110

Assembly Language: Function Calls" Goals of this Lecture"

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

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

CS61 Section Solutions 3

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

AS08-C++ and Assembly Calling and Returning. CS220 Logic Design AS08-C++ and Assembly. AS08-C++ and Assembly Calling Conventions

administrivia today start assembly probably won t finish all these slides Assignment 4 due tomorrow any questions?

x86 assembly CS449 Fall 2017

CIT Week13 Lecture

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

Introduction to Compilers Professor Jeremy Siek

Compiler Construction D7011E

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

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

Digital Forensics Lecture 3 - Reverse Engineering

ASSEMBLY III: PROCEDURES. Jo, Heeseung

Integers and variables

Assembly III: Procedures. Jo, Heeseung

Integers and variables

CMSC 313 Lecture 12. Project 3 Questions. How C functions pass parameters. UMBC, CMSC313, Richard Chang

What is a Compiler? Compiler Construction SMD163. Why Translation is Needed: Know your Target: Lecture 8: Introduction to code generation

CSE351 Autumn 2012 Midterm Exam (5 Nov 2012)

Assembly III: Procedures. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

System Programming and Computer Architecture (Fall 2009)

COMP 210 Example Question Exam 2 (Solutions at the bottom)

Code Generation. Lecture 30

4) C = 96 * B 5) 1 and 3 only 6) 2 and 4 only

Machine-level Programming (3)

Machine Programming 3: Procedures

Second Part of the Course

CSC 2400: Computing Systems. X86 Assembly: Function Calls

CS 33: Week 3 Discussion. x86 Assembly (v1.0) Section 1G

CPSC W Term 2 Problem Set #3 - Solution

Lab 10: Introduction to x86 Assembly

The course that gives CMU its Zip! Machine-Level Programming III: Procedures Sept. 17, 2002

CPEG421/621 Tutorial

Machine Program: Procedure. Zhaoguo Wang

CS241 Computer Organization Spring Loops & Arrays

CS213. Machine-Level Programming III: Procedures

CS241 Computer Organization Spring 2015 IA

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

Intel assembly language using gcc

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

Lecture Outline. Code Generation. Lecture 30. Example of a Stack Machine Program. Stack Machines

Assembly Language: IA-32 Instructions

CPS104 Recitation: Assembly Programming

Low-Level Essentials for Understanding Security Problems Aurélien Francillon

Sungkyunkwan University

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2016 Lecture 12

Instructor: Alvin R. Lebeck

Instruction Set Architecture

Instruction Set Architecture

Implementing Threads. Operating Systems In Depth II 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Machine-Level Programming II: Control and Arithmetic

CMSC 313 Lecture 12 [draft] How C functions pass parameters

CS 31: Intro to Systems ISAs and Assembly. Kevin Webb Swarthmore College February 9, 2016

CS 31: Intro to Systems ISAs and Assembly. Kevin Webb Swarthmore College September 25, 2018

Instruction Set Architecture

Summary: Direct Code Generation

Process Layout, Function Calls, and the Heap

X86 Assembly -Procedure II:1

Assembly Programmer s View Lecture 4A Machine-Level Programming I: Introduction

University of Washington

Instruction Set Architecture

CISC 360 Instruction Set Architecture

X86 Review Process Layout, ISA, etc. CS642: Computer Security. Drew Davidson

You may work with a partner on this quiz; both of you must submit your answers.

An Elegant Weapon for a More Civilized Age

Assignment 7: functions and closure conversion (part 1)

CSE2421 FINAL EXAM SPRING Name KEY. Instructions: Signature

The Hardware/Software Interface CSE351 Spring 2013

Machine-Level Programming III: Procedures

Register Allocation, iii. Bringing in functions & using spilling & coalescing

Y86 Processor State. Instruction Example. Encoding Registers. Lecture 7A. Computer Architecture I Instruction Set Architecture Assembly Language View

x86 assembly CS449 Spring 2016

CSE 351: Week 4. Tom Bergan, TA

Process Layout and Function Calls

Introduction to Computer Systems. Exam 1. February 22, Model Solution fp

X86 Stack Calling Function POV

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 21: Generating Pentium Code 10 March 08

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

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

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

Code Generation. Lecture 12

IA32 Stack. Stack BoDom. Region of memory managed with stack discipline Grows toward lower addresses. Register %esp contains lowest stack address

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 6

Lecture 15 Intel Manual, Vol. 1, Chapter 3. Fri, Mar 6, Hampden-Sydney College. The x86 Architecture. Robb T. Koether. Overview of the x86

Assignment 7: functions and closure conversion

CSE351 Autumn 2014 Midterm Exam (29 October 2014)

CS 31: Intro to Systems ISAs and Assembly. Martin Gagné Swarthmore College February 7, 2017

Object-oriented features

The x86 Architecture

UW CSE 351, Winter 2013 Midterm Exam

CSE351 Autumn 2014 Midterm Exam (29 October 2014)

Transcription:

Assignment 11: functions, calling conventions, and the stack ECEN 4553 & 5013, CSCI 4555 & 5525 Prof. Jeremy G. Siek December 5, 2008 The goal of this week s assignment is to remove function definitions and function calls, replacing them with operations that manipulate a stack, where each element of the stack is a chunk of data about a function invocation. These chunks are called activation records (or frames). They contain the arguments to a function, its local variables, the return address, and other information needed to execute the function. With the removal of function definitions, our IR (intermediate representation) will nearly be at the same abstraction-level as x86 assembly, so we will be able to emit assembly code to test this pass. After this week, the structure of the compiler will match what is shown in Figure 1. 1 Background Before diving into the details of how a stack is used to implement functions, let us recall why a stack is needed in the first place and why it is the right data-structure for the job. Consider the following recursive function that computes the nth Fibonacci number. int fib(int n) { if (n == 0 n == 1) return n; else return fib(n - 2) + fib(n - 1); } The calls made to the fib function can be visualized as a tree. Figure 2 shows the tree that results from calling fib(6). The execution of fib(6) can be thought of as a depth-first search through this tree that visits the subtree on the left before visiting the subtree on the right. So fib(6) invokes fib(4), which invokes fib(2), which calls fib(0). Once execution reaches 1

Python Text Lex & Parse Python AST Class and Object Lowering Variable Heapification Closure Conversion Convert to C-level AST Type Analysis & insert casts Insert Variable Declarations Convert to SSA Form C-level AST Remove Complex Opera* Remove Structured Control Remove 3-operand Instructions Remove Functions Emit x86 Lower Complex Assignments x86 Assembly Figure 1: Structure of the compiler. fib(0) fib(1) fib(0) fib(1) fib(0) fib(1) fib(0) fib(1) fib(1) fib(2) fib(0) fib(1) fib(1) fib(2) fib(1) fib(2) fib(2) fib(3) fib(2) fib(3) fib(3) fib(4) fib(4) fib(5) fib(6) Figure 2: The call tree for fib(6). 2

a leaf in the tree, it backtracks to the previous node and continues with the subtree on the right, so from fib(0) we backtrack to fib(2) and invoke fib(1). Once that is complete we again backtrack, but this time fib(2) is complete so we backtrack further to fib(4). Now, consider what is needed to implement this backtracking. We need to keep track of where we came from, that is, the sequence of nodes that led to the current node. Furthermore, for each node in the sequence, we need to keep track of what remains to be done and the values of the intermediate results. For example, when we return to fib(4) after computing fib(3), we need to remember the result of fib(2) so that we can add it to the result of fib(3). Also, note that as execution proceeds, we only add and remove nodes from the front of this sequence. Therefore, a stack data-structure is the appropriate choice, where each item in the stack contains information about intermediate results and what is left to be done. When the program is about to be run, the operating system creates a process and dedicates a chunk of memory to it. In Linux, this memory is laid out as shown The Stack in Figure 3. Memory addresses increase from bottom to Free Memory top. So the heap, which is the memory dedicated to object allocated with malloc, grows up, and the stack, used The Heap Static Data Program Code Figure 3: Memory Layout for a Linux Process. for activation frames, grows down. Having an upsidedown stack is confusing because tradition refers to the top of the stack as the place where items are pushed and popped. However, because this stack is upside-down, the top is lower than the bottom. To avoid further confusion, we refer to the front of the stack as the place where items are pushed and popped. The machine code for the program itself is stored at the bottom. Static data, such as string constants, is stored directly above the program code. The way in which a function should manipulate the stack is governed by calling conventions. By following an agreed-to set of conventions, different functions can work together, even if they are compiled by different compilers (so long as the compilers all follow the same calling convention). Figure 4 is a diagram of the stack focusing on the current activation record at the front of the stack. The %esp register is the stack pointer and it should always point to the front of the stack. The instruction pushes the contents of a 32-bit (double-word or 4-byte) register onto the stack, e.g., %eax pushes the value of the %eax register. The instruction does this by decrementing the %esp by 4 bytes so that it 3

points to an empty memory location and then writes the contents of the register to the address in %esp. The instruction %eax is equivalent to the following. subl $4, %esp movl %eax, (%esp) The popl instruction does just the opposite. It removes an item from the front of the stack. The popl instruction accomplishes this by loading the value at the %esp address into a register and then incrementing the stack pointer by 4 bytes. The instruction popl %eax is equivalent to the following. movl (%esp), %eax addl $4, %esp Previous Activation Records { The Current Activation Record{ The Stack arg 2 arg 1 arg 0 old instr. pointer old base pointer callee-save 0 callee-save 1 local 0 local 1 local 2 Free Memory higher addresses %ebp %esp lower addresses Figure 4: An activation record following the Linux x86 calling conventions. To call a function, you push the arguments for the function on the stack in reverse order and invoke the call instruction. Once the function is complete, control will be returned to the instruction following the call, which should pop the arguments from the stack by incrementing the stack pointer the appropriate number of bytes. For example, the call fib(6) corresponds to the following assembly code. $6 call fib addl $4, %esp The call instruction does two things: it pushes return address on the top of the stack and it jumps to the address indicated by the label, in this case fib. In Figure 4, the return address is in the slot labeled old instr. pointer. Figure 5 shows the assembly code produced by the GNU C compiler for the fib function. The base pointer (register %ebp) is used to keep track of where the function arguments and local variables are located on the stack. The arguments start at eight bytes above the base pointer and continue up. 4

For example, 8(%ebp) refers to argument 0 of the function and 12(%ebp) refers to argument 1 (assuming that argument 0 takes up 4 bytes). The base pointer is also used by the leave instruction (more about this later). The first thing a function needs to do is save the base pointer and then change it to point to the same place as the current stack pointer. fib: %ebp movl %esp, %ebp %ebx subl $4, %esp cmpl $0, 8(%ebp) je.l3 cmpl $1, 8(%ebp) je.l3 jmp.l2.l3: movl 8(%ebp), %eax movl %eax, -8(%ebp) jmp.l1.l2: movl 8(%ebp), %eax subl $2, %eax %eax call fib addl $4, %esp movl %eax, %ebx movl 8(%ebp), %eax decl %eax %eax call fib addl $4, %esp addl %eax, %ebx movl %ebx, -8(%ebp).L1: movl -8(%ebp), %eax movl -4(%ebp), %ebx leave ret Figure 5: x86 assembly code for the fib function. The next thing a function needs to do is to save the values of any callee-save registers that the function plans to use. The GNU C convention is that %ebx, %esi, %edi, and %ebp are designated as callee-save. The rest of the register are caller-save: %eax, %ecx, and %edx. In Figure 5 we see the %ebx register being pushed onto the stack just after the base pointer. The value for %ebx is retrieved from the stack at the very end of the fib function. After the callee-save registers are saved, the function makes room for any local variables that need to be stored on the stack. For the fib function, there is just one local variable stored on the stack; it is used to store the result of the function. To make room for this variable, 4 bytes are subtracted from the stack pointer. Later in the function, this variable is referred to as -8(%ebp). Scanning towards the middle of the fib function we see two calls to fib. In each case the argument is pushed on the stack, the call to fib is made, and then the argument is removed by adding 4 to the stack pointer. At the end of the function are two instructions: leave followed by ret. The leave instruction copies the contents of the %ebp register into the %esp register, so %esp points to the old base pointer. It then pops the old base pointer into the %ebp register. The ret instruction pops the return address from the stack and jumps back to the calling function. Exercise 1.1. Read chapter 6 through 6.3.4 of the Intel 64 and IA-32 Architectures Software Developer s Manual Volume 5

1. The advice given in this chapter is quite general, whereas the above description is specific to GNU C calling conventions. Passing Large Objects If your pyobj type is larger than 32 bits, then special care is needed for passing pyobjs as function arguments and returning then from a function. Let s see how the GNU C compiler handles the following program. #include <stdio.h> struct pyobj { int tag; union { int i; double f; } u; }; struct pyobj add(struct pyobj a, struct pyobj b) { struct pyobj r; r.tag = 0; r.u.i = a.u.i + b.u.i; return r; } int main() { struct pyobj x, y, z; y.tag = 0; y.u.i = 40; z.tag = 0; z.u.i = 2; x = add(y, z); printf("%d\n", x.u.i); return 0; } Here s the interesting portion of the assembly code for the main function. The object x is stored at -16(%ebp) through -24(%ebp), y is stored at -32(%ebp) through -40(%ebp), and z is stored at -48(%ebp) through -56(%ebp). The sequences of pushes leading up to call to the add function push z, then y, then the address of x. The leal instruction put the address in the eax register and the final before the call put the address onto the stack. main:... leal -24(%ebp), %eax -48(%ebp) -52(%ebp) -56(%ebp) -32(%ebp) -36(%ebp) -40(%ebp) 6

call addl subl call... %eax add $28, %esp $8, %esp -20(%ebp) $.LC0 printf The assembly code for the add function is shown below. add: %ebp movl %esp, %ebp subl $24, %esp movl 8(%ebp), %edx movl $0, -24(%ebp) movl 28(%ebp), %eax addl 16(%ebp), %eax movl %eax, -20(%ebp) movl -24(%ebp), %eax movl %eax, (%edx) movl -20(%ebp), %eax movl %eax, 4(%edx) movl -16(%ebp), %eax movl %eax, 8(%edx) movl %edx, %eax leave ret $4 The parameter b is stored in 24(%ebp) through 32(%ebp) and a is stored in 12(%ebp) through 20(%ebp) (which is where main put them when it pushed the arguments z and y). The address for the return value is in 8(%ebp). The local variable r is store in -24(%ebp) through -16(%ebp). Starting with the instruction movl -24(%ebp), %eax, the assembly code is copying r into the return value. This is a memory-to-memory copy and x86 does not have a general purpose instruction that copies memory to memory. Instead one 32-bit chunk at a time of r is copied into the %eax register and then copied into the return value, where the %edx register holds the address of the return value. The instruction movl %edx,%eax moves the address of the return value into the eax register. This move seems to be unnecessary and is not generated when optimization is turned on in gcc. 7

2 Remove Functions In this pass we remove the CallFunc and Return AST nodes and add the following new kinds of nodes. class Call: def init (self, label): self.label = label class Push: def init (self, arg): self.arg = arg class FPush: def init (self, arg): self.arg = arg class FPop: def init (self, arg): self.arg = arg class Register: def init (self, name): self.name = name class Address: def init (self, arg): self.arg = arg class Deref: def init (self, arg): self.arg = arg class Offset: def init (self, arg, offset): self.arg = arg self.offset = offset class Leave: pass class Ret: pass The Push AST node corresponds to the x86 instruction that pushes an item on the stack. The Call AST node corresponds to the x86 call instruction. 8

2.1 Translating CallFunc For the simple case when all of the arguments and the return value are small (32 bits), the CallFunc node is translated as follows, where m is the sum of the sizes of the arguments. CallFunc(Name(f), [arg 1,..., arg n ]) Push(arg n )... Push(arg 1 ) Call(f) Register( esp ) += m If an argument is large, then instead of a single Push you ll take the address of the argument and then use several Pushs. Suppose that arg i is 12 bytes. Then you ll need three Pushs to copy it onto the stack. Push(Offset(Name(arg i ), 8)) Push(Offset(Name(arg i ), 4)) Push(Offset(Name(arg i ), 0)) If the return type of the function is larger than 32 bits, then you need to push the address of the return value onto the stack just before the Call. Assign([AssName(x, OP_ASSIGN )], CallFunc(Name(f), [arg 1,..., arg n ])) Push(arg n )... Push(arg 1 ) tmp = Address(x) Push(tmp) Call(f) Register( esp ) += m If the CallFunc did not appear on the RHS of an assignment, then you must first create a new temporary variable and an assignment statement where the temporary is the LHS and the CallFunc is on the RHS. Then proceed as shown above. The temporary variable will need a VarDecl at the beginning of the function. If the return value of the function is a floating point number, then it is returned on the floating point stack. The FPop AST node corresponds to the fstpl instruction. Assign([AssName(x, OP_ASSIGN )], CallFunc(Name(f), [arg 1,..., arg n ])) Push(arg n )... Push(arg 1 ) 9

Call(f) Register( esp ) += m FPop(x) 2.2 Translating Return If the argument to return is small, then simple assign the argument to the %eax register and then issue the Leave and Ret instructions. Return(e) Register( eax ) = e Leave() Ret() If the argument is large, then copy the value into the return value. We ll introduce a special variable named ret to represent the address of the return value. Return(x) Deref( ret) = x Leave() Ret() If the argument is a floating point number, then load into onto the floating point stack. The FPush AST node corresponds to the fldl instruction. Return(x) FPush(x) Leave() Ret() 2.3 Assign Stack Locations to Parameters and Locals We assign a location on the stack to each parameter and local variable of a function. The location will be specified by an offset relative to the base pointer. The parameters get positive offsets and the local variables get negative offsets. The first parameter gets offset 8. Each parameter after the first gets an offset that is equal to the offset for the previous parameter plus the size of the previous parameter. So if the first parameter is 4 bytes, the second parameter gets offset 12. The first local variable is at offset -4 and each local variable after the first gets an even lower offset according to the size of the preceding local. 10

If the return type of a function is large, then the special variable ret has been introduced and it should be assigned an offset of 8, which bumps the first parameter up to offset 12. When we emit the x86 code, we ll need to access the assigned stack location when we come upon a Name AST node. There are several ways one could accomplish this. One way is to attached a dictionary to every function that maps variable names to their assigned location. When you emit the assembly code, pass the dictionary as a parameter to the emit function. 3 Lower Complex Assignments If an assignment statement is between large objects (greater than 32 bits, but not including floating point numbers) then you have to expand the assignment into a sequence of assignments. Suppose that x and y are 12 bytes. Then an assignment of x to y translates to three assignments statements. y = x; Offset(y, 0) = Offset(x, 0) Offset(y, 4) = Offset(x, 4) Offset(y, 8) = Offset(x, 8) If there is a Deref on the right or left-hand side of the assignment, some special handling is needed. The Offset nodes need to be inserted inside the Deref nodes (not on the outside). Deref(y) = x; Deref(Offset(y, 0)) = Offset(x, 0) Deref(Offset(y, 4)) = Offset(x, 4) Deref(Offset(y, 8)) = Offset(x, 8) 4 Emit x86 Assembly Code To emit x86 assembly from the AST, we write a recursive function that traverses the AST and produces a string containing the x86 instructions. Several cases are discussed below. Assignment x = e; Recursively apply the emit function to get x and e. Then emit two movl instructions as shown below. 11

x = e; movl e, %eax movl %eax, x For floating point numbers, use the movsd instructions and the xmm0 register instead. x = e; movsd e, %xmm0 movsd %xmm0, x However, the case where e is a floating point constant requires some special treatment. You need to represent e as two 32-bit integers. The easiest way to do this (in C) is to cast your double to an array of two integers. Call the two integers i 0 and i 1. Then you can use two movl instructions to write the two integers into the 8-bytes of memory at x. x = e; movl $i 0, 0(x ) movl $i 1, 4(x ) There s also some special cases for assignments concerning pointers. If there s a dereference on the left-hand side, then the address must be moved into a register before it can be used as the target of a move. We can then move e into the location pointed to by the register. The parenthesis around %eax in the second movl is the assembly language way to say dereference. Deref(x) = e movl x, %eax movl e, (%eax) If there s an Offset node inside the Deref node, then use the offset amount to adjust the dereference. Deref(Offset(x, n)) = e movl x, %eax movl e, n(%eax) If there s a dereference on the right-hand side, then we move the value of the right-hand side (which is an address) into a register, then move the value pointed to by the register into the register, and finally move the result to x. x = Deref(e) 12

movl e, %eax movl (%eax), %eax movl %eax, x The case where there s a dereference on both sides of the assignment is left to the reader to figure out. If the assignment has an Address node on the right-hand side, use the leal instruction to put the address of the argument in a register and then move the address to its destination. x = Address(e) leal e, %eax movl %eax, x Add x += e; Recursively apply the emit function to get x and e. For integer addition, emit a movl instruction to load e into a register then emit an addl instruction. x += e movl e, %eax addl %eax, x For floating point addition, we recommend using the SSE2 instructions. The SSE2 instructions use a special set of 8 registers for floating point, called the XMM registers. For now we ll only use one of these registers: xmm0. The instruction sequence for floating point is a bit more complicated than for integers because the SSE2 arithmetic instruction require the destination to be a register. Thus a second move is necessary to move the result back to the appropriate memory location. So in the following, we first move x to the xmm0 register, using the movsd instruction. The s stands for scalar (as opposed to vector, i.e. we re only dealing with a single floating point number) and the d stands for double precision. We then add e to the contents of xmm0 using the addsd instruction, and finally move the result back to x. x += e movsd x, %xmm0 addsd e, %xmm0 movsd %xmm0, x 13

Conditional Goto if (x == 0) goto label; Recursively apply the emit function to get x. Then emit a cmp instruction followed by a jne instruction. if (x == 0) goto label; cmpl $0, x je label Variable x Look up the offset n that you ve assigned to x and then emit n(%ebp) Constant c If the constant is an integer (or Boolean represented as 0 or 1) then emit $c Offset (x, m) Look up the offset n that you ve assigned to x and let n = n + m. Emit n (%ebp) Exercise 4.1. Add the three passes described above so that your compiler outputs x86 assembly code. 14