Cse322, Programming Languages and Compilers 1 6/21/2015 Lecture #15, May 22, 2007 Project 3 C calling convention The IA32 module Translating.

Slides:



Advertisements
Similar presentations
Calling sequence ESP.
Advertisements

University of Washington Procedures and Stacks II The Hardware/Software Interface CSE351 Winter 2013.
1 Homework / Exam Turn in mp2 at start of class today Reading –PAL, pp 3-6, Exam #1 next class –Open Book / Open Notes –NO calculators or other.
University of Washington Last Time For loops  for loop → while loop → do-while loop → goto version  for loop → while loop → goto “jump to middle” version.
Machine-Level Programming III: Procedures Apr. 17, 2006 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables CS213.
PC hardware and x86 3/3/08 Frans Kaashoek MIT
1 Function Calls Professor Jennifer Rexford COS 217 Reading: Chapter 4 of “Programming From the Ground Up” (available online from the course Web site)
Cse322, Programming Languages and Compilers 1 6/18/2015 Lecture #16, May 24, 2007 Runtime.c Running the code debugging assembler division strings for println.
Machine-Level Programming III: Procedures Sept. 17, 2007 IA32 stack discipline Register saving conventions Creating pointers to local variablesx86-64 Argument.
– 1 – , F’02 ICS05 Instructor: Peter A. Dinda TA: Bin Lin Recitation 4.
1 Homework Reading –PAL, pp , Machine Projects –Finish mp2warmup Questions? –Start mp2 as soon as possible Labs –Continue labs with your.
Machine-Level Programming III: Procedures Jan 30, 2003
Assembly תרגול 8 פונקציות והתקפת buffer.. Procedures (Functions) A procedure call involves passing both data and control from one part of the code to.
Machine-Level Programming III: Procedures Sept. 15, 2006 IA32 stack discipline Register saving conventions Creating pointers to local variablesx86-64 Argument.
September 22, 2014 Pengju (Jimmy) Jin Section E
Stack Activation Records Topics IA32 stack discipline Register saving conventions Creating pointers to local variables February 6, 2003 CSCE 212H Computer.
Stacks and Frames Demystified CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
Recitation 2: Assembly & gdb Andrew Faulring Section A 16 September 2002.
6.828: PC hardware and x86 Frans Kaashoek
Y86 Processor State Program Registers
ECE 273 – Computer Organization
Carnegie Mellon Introduction to Computer Systems /18-243, spring 2009 Recitation, Jan. 14 th.
1 Carnegie Mellon Stacks : Introduction to Computer Systems Recitation 5: September 24, 2012 Joon-Sup Han Section F.
Ithaca College Machine-Level Programming IV: IA32 Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2013 * Modified slides.
Machine-Level Programming III: Switch Statements and IA32 Procedures Seoul National University.
University of Washington Today More on procedures, stack etc. Lab 2 due today!  We hope it was fun! What is a stack?  And how about a stack frame? 1.
Fabián E. Bustamante, Spring 2007 Machine-Level Programming III - Procedures Today IA32 stack discipline Register saving conventions Creating pointers.
Recitation 2 – 2/11/02 Outline Stacks & Procedures Homogenous Data –Arrays –Nested Arrays Mengzhi Wang Office Hours: Thursday.
Assembly תרגול 5 תכנות באסמבלי. Assembly vs. Higher level languages There are NO variables’ type definitions.  All kinds of data are stored in the same.
Lecture #13, May 17, 2007 Analyzing the target language. The IA32 architecture Register, Addressing Modes, Instructions Flags, Jumps, the stack. RISC vs.
1 Carnegie Mellon Assembly and Bomb Lab : Introduction to Computer Systems Recitation 4, Sept. 17, 2012.
Low Level Programming Lecturer: Duncan Smeed The Interface Between High-Level and Low-Level Languages.
Machine-level Programming III: Procedures Topics –IA32 stack discipline –Register saving conventions –Creating pointers to local variables.
1 Procedure Call and Array. 2 Outline Data manipulation Control structure Suggested reading –Chap 3.7, 3.8.
Compiler Construction Code Generation Activation Records
University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture.
1 Assembly Language: Function Calls Jennifer Rexford.
IA32 Stack –Region of memory managed with stack discipline –Grows toward lower addresses –Register %esp indicates lowest stack address address of top element.
Section 5: Procedures & Stacks
Recitation 3: Procedures and the Stack
A job ad at a game programming company
Assembly function call convention
Reading Condition Codes (Cont.)
C function call conventions and the stack
IA32 Processors Evolutionary Design
Aaron Miller David Cohen Spring 2011
Homework In-line Assembly Code Machine Language
Introduction to Compilers Tim Teitelbaum
Recitation 2 – 2/4/01 Outline Machine Model
Assembly Language Programming V: In-line Assembly Code
Machine-Level Programming II: Arithmetic & Control
Carnegie Mellon Machine-Level Programming III: Switch Statements and IA32 Procedures / : Introduction to Computer Systems 7th Lecture, Sep.
Chapter 3 Machine-Level Representation of Programs
Machine-Level Programming 1 Introduction
asum.ys A Y86 Programming Example
Y86 Processor State Program Registers
Machine-Level Programming 4 Procedures
Instructor: David Ferry
Condition Codes Single Bit Registers
Machine-Level Programming 2 Control Flow
Assembly Language Programming II: C Compiler Calling Sequences
Machine-Level Programming 2 Control Flow
Machine-Level Programming III: Procedures Sept 18, 2001
MIPS Procedure Calls CSE 378 – Section 3.
Machine-Level Programming 2 Control Flow
Machine-Level Programming: Introduction
Chapter 3 Machine-Level Representation of Programs
“Way easier than when we were students”
Presentation transcript:

Cse322, Programming Languages and Compilers 1 6/21/2015 Lecture #15, May 22, 2007 Project 3 C calling convention The IA32 module Translating

Cse322, Programming Languages and Compilers 2 6/21/2015 Project #3 In class today we describe project #3 It is due Thursday, June 7, 2007 at 5:00 PM –this is in 17 days. –In order to get the course graded, there will be no extensions Final exam will be week of June 11 –Monday June 11

Cse322, Programming Languages and Compilers 3 6/21/2015 Properties of the X86 translation In project 3 we translate IR1 to x86 assembly. Because of the sparseness of the X86 register set we assume variables all live in memory. We have a number of types of variables –local variables (VAR n) –instance variables (MEMBER(address,n)) –parameters (PARAM n) –temporaries (TEMP n) How do we access these variables? Where in memory do they live?

Cse322, Programming Languages and Compilers 4 6/21/2015 C calling convention Callers Job –Before the call »Save any registers that might be needed later »push the n arguments in reverse order –call the function –after the call »remove the arguments from the stack »restore any saved registers –return value is in register %eax Callees Job –Set up the framepointer (%ebp) –allocate space for local variables on the stack –reset the old framepointer –put return value in %eax –return

Cse322, Programming Languages and Compilers 5 6/21/2015 The X86 stack Our translation is faithful to the calling convention of the GNU compiler. The stack on entry to a function looks as follows: return address arg 0 arg 1... arg n higher addresses → %esp

Cse322, Programming Languages and Compilers 6 6/21/2015 Allocating room for locals and temps Temporaries and locals are treated identically One exception locals may have some initialization code Everything is reachable via the frame pointer (%ebp) old %ebp return address arg 0... arg n higher addresses → var 0 var 1 temp 1 return address4(%ebp) arg0 8(%ebp) arg112(%ebp) var0-4(%ebp) var1-8(%ebp) %esp %ebp

Cse322, Programming Languages and Compilers 7 6/21/2015 Procedure Call Has 4 parts –Precall –Postreturn –Prolog –Epilog prolog precall postcall epilog prolog epilog Call Return

Cse322, Programming Languages and Compilers 8 6/21/2015 Caller - Precall Save registers that need it –write them to memory –save them on the stack Push arguments on the stack –pushL arg2 –pushL arg1 –pushL arg0 call the function –call _malloc

Cse322, Programming Languages and Compilers 9 6/21/2015 Caller - Postcall Remove n arguments from stack –addl$(4*n), %esp Pop saved registers if any

Cse322, Programming Languages and Compilers 10 6/21/2015 Callee -- Prolog Set up the framepointer (%ebx) –pushL %ebp # save the old framepointer –movl%esp,%ebp# initialize new framepointer allocate space for local variables on the stack –subl$(n*4),%esp#subtract from spackpointer return address arg 0 arg 1... arg n higher addresses → %esp

Cse322, Programming Languages and Compilers 11 6/21/2015 Callee -- Epilog put return value in %eax –movL ans,%eax reset the old framepointer –movl%ebp,%esp –popl%ebp return –return old %ebp return address arg 0... arg n higher addresses → var 1 var 2 temp 1 %esp %ebp

Cse322, Programming Languages and Compilers 12 6/21/2015 The IA32 module We build some data structures to represent IA32 code as SML data. Registers –represent the machine registers Modes –represent the addressing modes Instructions –represent instructions –every instruction has room for a label and a comment

Cse322, Programming Languages and Compilers 13 6/21/2015 Registers datatype Register = eax (* Accumulator *) | ebx (* Base *) | ecx (* Count *) | edx (* Data *) | esi (* Source index *) | edi (* Destination index *) | ebp (* Base pointer *) | esp (* Stack Pointer *) | eip (* Instruction pointer *) | eflag (* Flags *)

Cse322, Programming Languages and Compilers 14 6/21/2015 Modes type Label = string; datatype Mode = Mem of string (* top *) | % of Register (* %eax *) | $ of int (* $12 *) | & of (int * Mode); (& n(%eax) *)

Cse322, Programming Languages and Compilers 15 6/21/2015 Instructions datatype IA32 = Movl of (Label * Mode * Mode * string) | Xchgl of (Label * Mode * Mode * string) | Addl of (Label * Mode * Mode * string) | Subl of (Label * Mode * Mode * string) | Imull of (Label * Mode * Mode * string) | Andl of (Label * Mode * Mode * string) | Orl of (Label * Mode * Mode * string) | Xorl of (Label * Mode * Mode * string) | Cmpl of (Label * Mode * Mode * string) | Idivl of (Label * Mode * string) | Negl of (Label * Mode * string) | Notl of (Label * Mode * string) | Incl of (Label * Mode * string) | Decl of (Label * Mode * string) | Pushl of (Label * Mode * string) | Popl of (Label * Mode * string) | Jmp of (Label * Label * string) | Jz of (Label * Label * string) | Jnz of (Label * Label * string) | Jl of (Label * Label * string) | Jnl of (Label * Label * string) | Jg of (Label * Label * string) | Jng of (Label * Label * string)

Cse322, Programming Languages and Compilers 16 6/21/2015 lower case instuctions fun movl (m1,m2) = Movl("",m1,m2,""); fun xchgl(m1,m2) = Xchgl("",m1,m2,""); fun addl (m1,m2) = Addl("",m1,m2,""); fun subl (m1,m2) = Subl("",m1,m2,""); fun imull(m1,m2) = Imull("",m1,m2,""); fun andl (m1,m2) = Andl("",m1,m2,""); fun orl (m1,m2) = Orl("",m1,m2,""); fun xorl (m1,m2) = Xorl("",m1,m2,""); fun cmpl (m1,m2) = Cmpl("",m1,m2,""); fun idivl(m1) = Idivl("",m1,""); etc

Cse322, Programming Languages and Compilers 17 6/21/2015 Adding Labels fun addLabel l x = case x of Movl (_,m1,m2,s) => Movl (l,m1,m2,s) | Xchgl(_,m1,m2,s) => Xchgl(l,m1,m2,s) | Addl (_,m1,m2,s) => Addl (l,m1,m2,s) | Subl (_,m1,m2,s) => Subl (l,m1,m2,s) | Imull(_,m1,m2,s) => Imull(l,m1,m2,s) | Andl (_,m1,m2,s) => Andl (l,m1,m2,s) | Orl (_,m1,m2,s) => Orl (l,m1,m2,s) | Xorl (_,m1,m2,s) => Xorl (l,m1,m2,s) | Cmpl (_,m1,m2,s) => Cmpl (l,m1,m2,s)

Cse322, Programming Languages and Compilers 18 6/21/2015 Adding Comments fun addComment s x = case x of Movl (l,m1,m2,_) => Movl (l,m1,m2,s) | Xchgl(l,m1,m2,_) => Xchgl(l,m1,m2,s) | Addl (l,m1,m2,_) => Addl (l,m1,m2,s) | Subl (l,m1,m2,_) => Subl (l,m1,m2,s) | Imull(l,m1,m2,_) => Imull(l,m1,m2,s) | Andl (l,m1,m2,_) => Andl (l,m1,m2,s) | Orl (l,m1,m2,_) => Orl (l,m1,m2,s) | Xorl (l,m1,m2,_) => Xorl (l,m1,m2,s) | Cmpl (l,m1,m2,_) => Cmpl (l,m1,m2,s)

Cse322, Programming Languages and Compilers 19 6/21/2015 Translation scheme Translate every IR.Exp into a IA32 list Simple translation scheme Every translation of an Expression leaves the answer in the %eax register.

Cse322, Programming Languages and Compilers 20 6/21/2015 Getting started fun compileE exp = case exp of BINOP(ADD,x,y) => let val xCode = compileE x val yCode = compileE y in [ pushl(%eax) [ popl(%ebx), addl(%ebx,%eax) ] end | CONST(s,typ) => let val n = valOf(Int.fromString s) in [ movl($n,%eax) ] end | NAME s => [ movl(Mem s,%eax) ]

Cse322, Programming Languages and Compilers 21 6/21/2015 Translating function calls fun compileE exp = case exp of | CALL(NAME f,args) => let fun pushargs [] = [] | pushargs (x::xs) = (pushargs (compileE [ pushl (%eax) ] val n = length args in (pushargs [call [addl($(wdsize*n),%esp)] end

Cse322, Programming Languages and Compilers 22 6/21/2015 Translating Statements fun compileS x = case x of MOVE(dest,src) => (compileE [pushl (address [popl (%ebx),Movl("",%ebx,&(0,%eax), sSTMT x)] | JUMP n => [Jmp("",label32 n,"")]

Cse322, Programming Languages and Compilers 23 6/21/2015 Translating addresses fun address (VAR n) = [movl(%ebp,%eax), addl ($(~(n + wdsize)),%eax)] | address (PARAM n) = [movl(%ebp,%eax),addl ($( 2*wdsize + n),%eax) ] old %ebp return address arg 0... arg n higher addresses → var 1 var 2 temp 1 %esp %ebp

Cse322, Programming Languages and Compilers 24 6/21/2015 Translating Function definitions fun compileFunc (FUNC(nm,_,vs,ss)) = let fun size((typ),x) = ProgramTypes.typeSize typ + x val n = foldr size 0 vs in [ Pushl(nm,%ebp,"Entering "^nm), movl (%esp,%ebp), subl($ n, %esp) (compileSS [ Movl("",%ebp,%esp,"Default Epilog"), popl (%ebp), return() ] end; Prolog Epilog

Cse322, Programming Languages and Compilers 25 6/21/2015 Example class T { int instance2 = 0; public int f(int j) { int k = 1; return (j+k); }

Cse322, Programming Languages and Compilers 26 6/21/2015 IR1 code ================================= The Class Table with inherited instance variables: class Object has vars: class T has vars: 0: int instance2 := 0 ================================= T_f(int P1) int V0; V0 := 1 return MEM(P1) + MEM(V0)

Cse322, Programming Languages and Compilers 27 6/21/2015 IA32 code T_f: pushl %ebp # Entering T_f movl %esp,%ebp subl $4,%esp movl $1,%eax pushl %eax movl %ebp,%eax addl $-4,%eax popl %ebx movl %ebx,0(%eax) # V0 := 1 movl %ebp,%eax addl $12,%eax movl 0(%eax),%eax # P1 pushl %eax movl %ebp,%eax addl $-4,%eax movl 0(%eax),%eax # V0 popl %ebx addl %ebx,%eax movl %ebp,%esp # Epilog popl %ebp return movl %ebp,%esp # Default Epilog popl %ebp return

Cse322, Programming Languages and Compilers 28 6/21/2015 Using the assembler We will write some code in class.

Cse322, Programming Languages and Compilers 29 6/21/2015 What to turn in You should hand in the module Phase3.sml It should include a function compileFunc :: IR1.FUNC list -> IA32.IA32 list You should also write a function toplevel :: string -> string -> unit –toplevel src dest – parses and compiles src to IA32 list – Then prints it out as assembly code to file dest

Cse322, Programming Languages and Compilers 30 6/21/2015 The template I will supply a template. The template will provide drivers and a complete solution to projects 1 and 2. I will supply a file runtime.c –you will link your code with this file You may ask for the template by ing me. I have posted the IA32 code on the web page.