Spring 2014Jim Hogg - UW - CSE - P501K-1 CSE P501 – Compiler Construction Mapping source code to x86 Basic statements and expressions Objects, method calls,

Slides:



Advertisements
Similar presentations
Goal: Write Programs in Assembly
Advertisements

Intermediate Code Generation
1/1/ / faculty of Electrical Engineering eindhoven university of technology Introduction Part 2: Data types and addressing modes dr.ir. A.C. Verschueren.
CSC 221 Computer Organization and Assembly Language Lecture 21: Conditional and Block Structures: Assembly Programs.
Machine Instructions Operations 1 ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-1.ppt Modification date: March 18, 2015.
Chapter 8 ICS 412. Code Generation Final phase of a compiler construction. It generates executable code for a target machine. A compiler may instead generate.
1 Compiler Construction Intermediate Code Generation.
PSUCS322 HM 1 Languages and Compiler Design II IR Code Generation I Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU.
C Programming and Assembly Language Janakiraman V – NITK Surathkal 2 nd August 2014.
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, Conditional Loop Instructions LOOPZ and LOOPE LOOPNZ.
Intermediate Representation I High-Level to Low-Level IR Translation EECS 483 – Lecture 17 University of Michigan Monday, November 6, 2006.
Chapter 14: Building a Runnable Program Chapter 14: Building a runnable program 14.1 Back-End Compiler Structure 14.2 Intermediate Forms 14.3 Code.
CS412/413 Introduction to Compilers Radu Rugina Lecture 16: Efficient Translation to Low IR 25 Feb 02.
Intermediate code generation. Code Generation Create linear representation of program Result can be machine code, assembly code, code for an abstract.
Peephole Optimization Final pass over generated code: examine a few consecutive instructions: 2 to 4 See if an obvious replacement is possible: store/load.
1 ICS 51 Introductory Computer Organization Fall 2006 updated: Oct. 2, 2006.
Assembly Language for Intel-Based Computers
4/23/09Prof. Hilfinger CS 164 Lecture 261 IL for Arrays & Local Optimizations Lecture 26 (Adapted from notes by R. Bodik and G. Necula)
CS 536 Spring Code generation I Lecture 20.
Accessing parameters from the stack and calling functions.
10/14/2002© 2002 Hal Perkins & UW CSEM-1 CSE 582 – Compilers Running JFlat Basic Code Generation and Bootstrapping Hal Perkins Autumn 2002.
4/6/08Prof. Hilfinger CS164 Lecture 291 Code Generation Lecture 29 (based on slides by R. Bodik)
Code Generation CS 480. Can be complex To do a good job of teaching about code generation I could easily spend ten weeks But, don’t have ten weeks, so.
Chapter 7Louden, Programming Languages1 Chapter 7 - Control I: Expressions and Statements "Control" is the general study of the semantics of execution.
8/9/2015© Hal Perkins & UW CSEK-1 CSE P 501 – Compilers Code Shape I – Basic Constructs Hal Perkins Winter 2008.
9/29: Lecture Topics Memory –Addressing (naming) –Address space sizing Data transfer instructions –load/store on arrays on arrays with variable indices.
Addressing Modes Chapter 11 S. Dandamudi To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer,  S.
CS412/413 Introduction to Compilers Radu Rugina Lecture 15: Translating High IR to Low IR 22 Feb 02.
Instruction Set Design by Kip R. Irvine (c) Kip Irvine, All rights reserved. You may modify and copy this slide show for your personal use,
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
Chapter 8 Intermediate Code Zhang Jing, Wang HaiLing College of Computer Science & Technology Harbin Engineering University.
Chapter 19 Number Systems. Irvine, Kip R. Assembly Language for Intel-Based Computers, Translating Languages English: Display the sum of A times.
Dr. José M. Reyes Álamo 1.  Review: ◦ of Comparisons ◦ of Set on Condition  Statement Labels  Unconditional Jumps  Conditional Jumps.
Assembly תרגול 5 תכנות באסמבלי. Assembly vs. Higher level languages There are NO variables’ type definitions.  All kinds of data are stored in the same.
Assembly Code Optimization Techniques for the AMD64 Athlon and Opteron Architectures David Phillips Robert Duckles Cse 520 Spring 2007 Term Project Presentation.
11/12/2015© Hal Perkins & UW CSEL-1 CSE P 501 – Compilers Code Shape II – Objects & Classes Hal Perkins Winter 2008.
1 ICS 51 Introductory Computer Organization Fall 2009.
Microprocessors The ia32 User Instruction Set Jan 31st, 2002.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
Addressing Modes Chapter 6 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
Chapter 7 Object Code Generation. Chapter 7 -- Object Code Generation2  Statements in 3AC are simple enough that it is usually no great problem to map.
Assembly Language. Symbol Table Variables.DATA var DW 0 sum DD 0 array TIMES 10 DW 0 message DB ’ Welcome ’,0 char1 DB ? Symbol Table Name Offset var.
Assembly - Arrays תרגול 7 מערכים.
Computer Systems – Machine & Assembly code. Objectives Machine Code Assembly Language Op-code Operand Instruction Set.
תרגול 5 תכנות באסמבלי, המשך
Intermediate code generation. Code Generation Create linear representation of program Result can be machine code, assembly code, code for an abstract.
Microprocessor & Assembly Language Arithmetic and logical Instructions.
Arrays. Outline 1.(Introduction) Arrays An array is a contiguous block of list of data in memory. Each element of the list must be the same type and use.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee Control Unit.
Machine-Level Programming II Control Flow Sept. 10, 1998 Topics Control Flow –Varieties of Loops –Switch Statements class06.ppt “The course that.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
CS 536 © CS 536 Spring Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 15.
Assembly Language Addressing Modes. Introduction CISC processors usually supports more addressing modes than RISC processors. –RISC processors use the.
7-Nov Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Oct lecture23-24-hll-interrupts 1 High Level Language vs. Assembly.
Bitwise Operations C includes operators that permit working with the bit-level representation of a value. You can: - shift the bits of a value to the left.
Data Transfers, Addressing, and Arithmetic
Introduction to Compilers Tim Teitelbaum
Basic Assembly Language
BIC 10503: COMPUTER ARCHITECTURE
Lecture 4: MIPS Instruction Set
Lecture 30 (based on slides by R. Bodik)
Fundamentals of Computer Organisation & Architecture
Code Shape I – Basic Constructs Hal Perkins Autumn 2011
Running MiniJava Basic Code Generation and Bootstrapping
Running MiniJava Basic Code Generation and Bootstrapping
X86 Assembly Review.
Running MiniJava Basic Code Generation and Bootstrapping
9/27: Lecture Topics Memory Data transfer instructions
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
Presentation transcript:

Spring 2014Jim Hogg - UW - CSE - P501K-1 CSE P501 – Compiler Construction Mapping source code to x86 Basic statements and expressions Objects, method calls, and dynamic dispatch

Spring 2014Jim Hogg - UW - CSE - P501K-2 Review: Variables For MiniJava, data is held in either A stack frame - for variables local to the method An object - for fields Local variables accessed via ebp ('above' ebp) eg: mov eax, [ebp - 12] Fields accessed via object address in a register Instead of ebp pointing to start of frame, use ecx pointing to start of object Details later

How to translate language constructs from MiniJava into assembly code? X = Y, X op Y, IF-THEN, IF-THEN-ELSE, WHILE, etc If you know assembler code for any chip, the translation templates should be obvious If you don't know assembler code, imagine translating MiniJava constructs into a primitive language whose only control flow is GOTO Spring 2014Jim Hogg - UW - CSE - P501K-3 Basic Statements and Expressions

Conventions in Examples Examples show code snippets in isolation Real code generator needs to deal with things like: Which registers are in use at which point in the program Which registers to spill into memory (pushed onto stack or stored in stack frame) when a new register is needed and no free ones are available Register eax used below as a generic example Rename as needed for more complex code Examples include a few peephole optimizations Spring 2014Jim Hogg - UW - CSE - P501K-4

Spring 2014Jim Hogg - UW - CSE - P501K-5 Code Generation for Constants Source 17 Idea Realize constant value in a register x86 mov eax, 17 Peephole Optimization if constant is 0, then: xor eax, eax

Spring 2014Jim Hogg - UW - CSE - P501K-6 Assignment Statement Source var = exp;// var is a local variable x86 mov [ebp-offset var ], eax

Spring 2014Jim Hogg - UW - CSE - P501K-7 Unary Minus Source -exp x86 neg eax Optimization Collapse -(-exp) to exp Unary plus is a no-op

Spring 2014Jim Hogg - UW - CSE - P501K-8 Binary + Source exp1 + exp2 x86 add eax, edx Optimizations If exp2 is a simple variable or constant: add eax, exp2 Change exp1 + (-exp2) into exp1 - exp2 If exp2 is 1 then: inc eax

Spring 2014Jim Hogg - UW - CSE - P501K-9 Binary -, * Same as + Use sub for – (but not commutative!) Use imul for * Optimizations Use left shift to multiply by powers of 2 If your multiplier is really slow or you’ve got free scalar units and multiplier is busy, consider: 10*x = (8*x)+(2*x) But this may perform worse on a different micro-architecture Use x+x instead of 2*x, etc (faster) Use dec for x-1

Spring 2014Jim Hogg - UW - CSE - P501K-10 Integer Division Slow on x86 Divides 64-bit integer in edx:eax by a 32-bit integer (eg, in ebx) ~50 clock latency Source exp1 / exp2 x86 cdq; extend eax into edx:eax, clobbers edx idiv ebx; => quotient in eax; remainder in edx

Spring 2014Jim Hogg - UW - CSE - P501K-11 Control Flow Basic idea: decompose higher level operation into jmps and conditional jmps In following slides, j false is used to mean jump when a condition is false No such instruction on x86! Will have to use appropriate instructions to set condition codes, followed by conditional jumps Normally wouldn’t actually generate the value “true” or “false” in a register

Spring 2014Jim Hogg - UW - CSE - P501K-12 While Source while (cond) stm x86 loop: j false done jmp loop done: Note In generated asm code we’ll need to create a unique label name for each loop, conditional statement, etc. eg: L123:

Spring 2014Jim Hogg - UW - CSE - P501K-13 Optimization for While Put the test at the end jmp test loop: test: j true loop Why bother? Pulls one jmp instruction out of the loop May avoid pipeline stall on jmp on each iteration But many chips assume backwards-branch is always taken Hardware branch-predictor will 'tune in' to the pattern Easy to do from AST or other IR; not so easy if generating code on- the-fly (eg, recursive-descent, 1-pass compiler)

Spring 2014Jim Hogg - UW - CSE - P501K-14 Do-While Source do stm while (cond); x86 loop: j true loop

Spring 2014Jim Hogg - UW - CSE - P501K-15 IF-THEN Source if (cond) stm x86 j false skip skip:

Spring 2014Jim Hogg - UW - CSE - P501K-16 IF-THEN-ELSE Source if (cond) stm1 else stm2 x86 j false else jmp done else: done:

Spring 2014Jim Hogg - UW - CSE - P501K-17 Jump Chaining Observation: naïve code gen can will produce jumps to jumps Optimization: if a jump targets an unconditional jump, change target of first jump to the target of the second Repeat until no further changes

Spring 2014Jim Hogg - UW - CSE - P501K-18 IF-ELSEIF-ELSE Source if (cond1) stm1 else if (cond2) stm2 else stm3 x86 j false elseif jmp done elseif: j false else jmp done else: jmp done done: Note: jump-to-next redundancy

Spring 2014Jim Hogg - UW - CSE - P501K-19 Boolean Expressions What do we do with this? x > y Expression evaluates to true or false Could generate the value in a register (eg: 0|1) But normally we don’t want/need the value; we’re only trying to decide whether to jump Eg: if (exp1 > exp2) stm cmpeax, edx jle L123 L123:

Spring 2014Jim Hogg - UW - CSE - P501K-20 Boolean Operators: ! Source ! exp eg: if (!exp) stm To compile: jnz L123, where L123 is label after stm

Spring 2014Jim Hogg - UW - CSE - P501K-21 Boolean Operators: && and || In C/C++/Java/C#, these are short-circuit operators Right operand is evaluated only if needed if (cond1 || cond2) if cond1 evaluates to true, then no need to evaluate cond2 if (cond1 && cond2) if cond1 evaluates to false, then no need to evaluate cond2 if (p != null && p.x == 2)... this check relies upon short-circuit evaluation! Basically, generate if statements that jump appropriately and only evaluate operands when needed

Spring 2014Jim Hogg - UW - CSE - P501K-22 Example: Code for && Source if (exp1 && exp2) stm x86 j false skip j false skip skip:

Spring 2014Jim Hogg - UW - CSE - P501K-23 Example: Code for || Source if (exp1 || exp2) stm x86 j true doit j false skip doit: skip:

Spring 2014Jim Hogg - UW - CSE - P501K-24 Realizing Boolean Values If a boolean value needs to be stored in a variable or method call parameter, generate code to create result Typical representations: 0 for false, +1 or -1 for true C specifies 0 and 1 if stored; we’ll use that Best choice can depend on machine instructions; normally some convention is established during the primeval history of the architecture (eg: VAX blbc)

Spring 2014Jim Hogg - UW - CSE - P501K-25 Boolean Values: Example Source var = bexp ; x86 j false genFalse mov eax, 1 jmp storeIt genFalse: mov eax, 0 storeIt:mov [ebp+offset var ], eax

Spring 2014Jim Hogg - UW - CSE - P501K-26 Better, If Enough Registers Source var = bexp ; x86 xor eax, eax j false storeIt inc eax storeIt:mov [ebp+offset var ], eax Or use conditional move instruction; avoids pipeline stalls mov eax, 1; assume bexp true cmovzeax, 0 mov [ebp+offset var ], eax

Spring 2014Jim Hogg - UW - CSE - P501K-27 Alternatively: setcc Source var = x < y; x86 mov eax, [ebp+offset x ] ; load x cmp eax, [ebp+offset y ] ; compare to y setl al; set AL to 0|1 if less than movzx eax,al ; zero-extend to 32 bits mov [ebp+offset var ],eax ; generated by asg stm Gnu mnemonic for movzx (byte->dbl word) is movzbl Or use conditional move (cmovcc) instruction for sequences like: x = y < z ? y : z

Spring 2014Jim Hogg - UW - CSE - P501K-28 Other Control Flow: switch Naïve: generate a chain of nested if-then-else’s Better: switch is intended to allow an O(1) selection, provided the set of switch values is reasonably compact Idea: create a 1-D array of jumps or labels and use the switch expression to select the right one Also need to generate an IF check that expression lies within bounds

Spring 2014 Jim Hogg - UW - CSE - P501K-29 Switch Source switch (exp) { case 0: stms0; case 1: stms1; case 2: stms2; } X86-ish “if (eax 2) jmp done” mov eax, swtab[eax * 4] jmp eax L0: jmp done L1: jmp done L2: jmp done done:.data swtabdd L0; switch table dd L1 dd L Assume no "fallthru"

Spring 2014Jim Hogg - UW - CSE - P501K-30 Arrays C/C++/Java have 1-dimensional arrays 0-origin array with n elements contains variables: a[0] … a[n-1] C/C++ n-dimensional arrays Multiple dimensions; row major order Java n-dimensional "jagged" arrays

Spring 2014Jim Hogg - UW - CSE - P501K-31 0-Origin 1-D Integer Arrays Source exp1[exp2] x86 address is [eax+4*edx]; 4 bytes per element contiguous cells in memory

Spring 2014Jim Hogg - UW - CSE - P501K-32 2-D Arrays C, etc (the curly-brace languages) use row-major order Fortran (and Excel) uses column-major order Exercises: What is the layout? How do you calculate location of a[i, j]? What happens when you pass array references between Fortran and C code? Java has "jagged" arrays - array of arrays 0,00,10,20,30,4 1,01,11,21,31, Java jagged arrays int[][] array = {{3, 4, 5}, {77, 50}}; int[][] a = { {1,5,-4,23}, {1,2,6,3}, {99} }

How to find A[row, col] Spring 2014Jim Hogg - UW - CSE - P501K-33 0,00,10,20,30,4 1,01,11,21,31,4 2,02,12,22,32,4 3,03,13,23,33,4 row column row-major layout 0,00,10,20,30,4 1,01,11,21,31,4 2,02,12,22,32,4 int A[row, col] + (row * numcols + col) * 4// 0-based A[row, col] + (row – rowmin) * (colmax – colmin + 1) * elemsize + (col – colmin) * elemsize

Spring 2014Jim Hogg - UW - CSE - P501K-34 Code Generation for Objects Representation Method calls Inheritance and overriding Strategies for implementing code generators Code improvement – optimization Next