8/9/2015© 2002-08 Hal Perkins & UW CSEK-1 CSE P 501 – Compilers Code Shape I – Basic Constructs Hal Perkins Winter 2008.

Slides:



Advertisements
Similar presentations
Goal: Write Programs in Assembly
Advertisements

Optimizing Compilers for Modern Architectures Allen and Kennedy, Chapter 13 Compiling Array Assignments.
Intermediate Code Generation
Programming Languages and Paradigms
1/1/ / faculty of Electrical Engineering eindhoven university of technology Introduction Part 2: Data types and addressing modes dr.ir. A.C. Verschueren.
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.
Deeper Assembly: Addressing, Conditions, Branching, and Loops
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.
Chapter 9 Imperative and object-oriented languages 1.
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.
Chapter 7Louden, Programming Languages1 Chapter 7 - Control I: Expressions and Statements "Control" is the general study of the semantics of execution.
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.
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.
Run time vs. Compile time
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.
Addressing Modes Chapter 11 S. Dandamudi To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer,  S.
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
Programmer's view on Computer Architecture by Istvan Haller.
Chapter 8 Intermediate Code Zhang Jing, Wang HaiLing College of Computer Science & Technology Harbin Engineering University.
Compiler Chapter# 5 Intermediate code generation.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Spring 2014Jim Hogg - UW - CSE - P501K-1 CSE P501 – Compiler Construction Mapping source code to x86 Basic statements and expressions Objects, method calls,
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.
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,
M. Mateen Yaqoob The University of Lahore Spring 2014.
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.
12/18/2015© Hal Perkins & UW CSEG-1 CSE P 501 – Compilers Intermediate Representations Hal Perkins Winter 2008.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Decision Making and Branching (cont.)
Assembly - Arrays תרגול 7 מערכים.
3/2/2016© Hal Perkins & UW CSES-1 CSE P 501 – Compilers Optimizing Transformations Hal Perkins Autumn 2009.
Calling Procedures C calling conventions. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
Today… Preparation for doing Assignment 1. Invoking methods overview. Conditionals and Loops. Winter 2016CMPE212 - Prof. McLeod1.
CS 536 © CS 536 Spring Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 15.
7-Nov Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Oct lecture23-24-hll-interrupts 1 High Level Language vs. Assembly.
Flow Control. Comments u Comments: /* This is a comment */ –Use them! –Comments should explain: v special cases v the use of functions (parameters, return.
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
CS2100 Computer Organisation
Introduction to Compilers Tim Teitelbaum
Code Shape II – Objects & Classes Hal Perkins Autumn 2011
Lecture 30 (based on slides by R. Bodik)
Machine-Level Programming III: Procedures Sept 18, 2001
Code Shape I – Basic Constructs Hal Perkins Autumn 2011
Running MiniJava Basic Code Generation and Bootstrapping
Multi-modules programming
Running MiniJava Basic Code Generation and Bootstrapping
Code Shape II – Objects & Classes Hal Perkins Autumn 2005
Lecture 15: Code Generation - Instruction Selection
Code Shape II – Objects & Classes Hal Perkins Summer 2004
LL and Recursive-Descent Parsing Hal Perkins Winter 2008
Running MiniJava Basic Code Generation and Bootstrapping
Procedure Linkages Standard procedure linkage Procedure has
Code Shape II – Objects & Classes Hal Perkins Autumn 2009
Presentation transcript:

8/9/2015© Hal Perkins & UW CSEK-1 CSE P 501 – Compilers Code Shape I – Basic Constructs Hal Perkins Winter 2008

8/9/2015© Hal Perkins & UW CSEK-2 Agenda Mapping source code to x86 Mapping for other common architectures follows same basic pattern Now: basic statements and expressions Next: Object representation, method calls, and dynamic dispatch

8/9/2015© Hal Perkins & UW CSEK-3 Review: Variables For us, all data will be in either: A stack frame for method local variables An object for instance variables Local variables accessed via ebp mov eax,[ebp+12] Instance variables accessed via an object address in a register Details later

8/9/2015© Hal Perkins & UW CSEK-4 Conventions for Examples Examples show code snippets in isolation Real code generator needs to worry about things like Which registers are busy at which point in the program Which registers to spill into memory when a new register is needed and no free ones are available (x86: temporaries are often pushed on the stack, but can also be stored in a stack frame) Register eax used below as a generic example Rename as needed for more complex code involving multiple registers A few peephole optimizations shown

8/9/2015© Hal Perkins & UW CSEK-5 Constants Source 17 x86 mov eax,17 Idea: realize constant value in a register Optimization: if constant is 0 xor eax,eax Machine instructions from a compiler writer’s perspective: “I don’t care what it was designed to do, I care what it can do!”

8/9/2015© Hal Perkins & UW CSEK-6 Assignment Statement Source var = exp; x86 mov [ebp+offset var ],eax

8/9/2015© Hal Perkins & UW CSEK-7 Unary Minus Source -exp x86 neg eax Optimization Collapse -(-exp) to exp Unary plus is a no-op

8/9/2015© Hal Perkins & UW CSEK-8 Binary + Source exp1 + exp2 x86 add eax,edx

8/9/2015© Hal Perkins & UW CSEK-9 Binary + Optimizations If exp2 is a simple variable or constant add eax,exp2 Change exp1 + -exp2 into exp1-exp2 If exp2 is 1 inc eax

8/9/2015© Hal Perkins & UW CSEK-10 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, 10*x = (8*x)+(2*x) Use x+x instead of 2*x, etc. (faster) Use dec for x-1

8/9/2015© Hal Perkins & UW CSEK-11 Integer Division Ghastly on x86 Only works on 64 bit int divided by 32-bit int Requires use of specific registers Source exp1 / exp2 x86 cdq; extend to edx:eax, clobbers edx idiv ebx; quotient in eax; remainder in edx

8/9/2015© Hal Perkins & UW CSEK-12 Control Flow Basic idea: decompose higher level operation into conditional and unconditional gotos In the following, j false is used to mean jump when a condition is false No such instruction on x86 Will have to realize with appropriate sequence of instructions to set condition codes followed by conditional jumps Normally won’t actually generate the value “true” or “false” in a register

8/9/2015© Hal Perkins & UW CSEK-13 While Source while (cond) stmt x86 test: j false done jmp test done: Note: In generated asm code we’ll need to generate unique label for each loop, conditional statement, etc.

8/9/2015© Hal Perkins & UW CSEK-14 Optimization for While Put the test at the end jmp test loop: test: j true loop Why bother? Pulls one instruction (jmp) out of the loop Avoids a pipeline stall on jmp on each iteration Although modern processors will often predict control flow and avoid the stall Easy to do from AST or other IR; not so easy if generating code on the fly (e.g., recursive descent 1- pass compiler)

8/9/2015© Hal Perkins & UW CSEK-15 Do-While Source do stmt while(cond); x86 loop: j true loop

8/9/2015© Hal Perkins & UW CSEK-16 If Source if (cond) stmt x86 j false skip skip:

8/9/2015© Hal Perkins & UW CSEK-17 If-Else Source if (cond) stmt1 else stmt2 x86 j false else jmp done else: done:

8/9/2015© Hal Perkins & UW CSEK-18 Jump Chaining Observation: naïve implementation can produce jumps to jumps Optimization: if a jump has as its target an unconditional jump, change the target of the first jump to the target of the second Repeat until no further changes

8/9/2015© Hal Perkins & UW CSEK-19 Boolean Expressions What do we do with this? x > y It is an expression that evaluates to true or false Could generate the value (0/1 or whatever the local convention is) But normally we don’t want/need the value; we’re only trying to decide whether to jump

8/9/2015© Hal Perkins & UW CSEK-20 Code for exp1 > exp2 Basic idea: designate jump target, and whether to jump if the condition is true or if it is false Example: exp1 > exp2, target L123, jump on false cmpeax,edx jng L123

8/9/2015© Hal Perkins & UW CSEK-21 Boolean Operators: ! Source ! exp Context: evaluate exp and jump to L123 if false (or true) To compile !, reverse the sense of the test: evaluate exp and jump to L123 if true (or false)

8/9/2015© Hal Perkins & UW CSEK-22 Boolean Operators: && and || In C/C++/Java/C#, these are short- circuit operators Right operand is evaluated only if needed Basically, generate the if statements that jump appropriately and only evaluate operands when needed

8/9/2015© Hal Perkins & UW CSEK-23 Example: Code for && Source if (exp1 && exp2) stmt x86 j false skip j false skip skip:

8/9/2015© Hal Perkins & UW CSEK-24 Example: Code for || Source if (exp1 || exp2) stmt x86 j true doit j false skip doit: skip:

8/9/2015© Hal Perkins & UW CSEK-25 Realizing Boolean Values If a boolean value needs to be stored in a variable or method call parameter, generate code needed to actually produce it Typical representations: 0 for false, +1 or -1 for true C uses 0 and 1; we’ll use that Best choice can depend on machine architecture; normally some convention is established during the primeval history of the architecture

8/9/2015© Hal Perkins & UW CSEK-26 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 ; generated by asg stmt

8/9/2015© Hal Perkins & UW CSEK-27 Faster, If Enough Registers Source var = bexp ; x86 xor eax,eax j false storeIt inc eax storeIt:mov [ebp+offset var ],eax ; generated by asg stmt Or use conditional move (movecc) instruction if available – avoids pipeline stalls due to conditional jumps

8/9/2015© Hal Perkins & UW CSEK-28 Other Control Flow: switch Naïve: generate a chain of nested if-else if statements Better: switch is designed 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 Need to generate the equivalent of an if statement to ensure that expression value is within bounds

8/9/2015© Hal Perkins & UW CSEK-29 Switch Source switch (exp) { case 0: stmts0; case 1: stmts1; case 2: stmts2; } X86 “if (eax 2) jmp defaultLabel” mov eax,swtab[eax*4] jmp eax.data swtabdd L0 dd L1 dd L2.code L0: L1: L2:

8/9/2015© Hal Perkins & UW CSEK-30 Arrays Several variations C/C++/Java 0-origin; an array with n elements contains variables a[0]…a[n-1] 1 or more dimensions; row major order Key step is to evaluate a subscript expression and calculate the location of the corresponding element

8/9/2015© Hal Perkins & UW CSEK-31 0-Origin 1-D Integer Arrays Source exp1[exp2] x86 address is [eax+4*edx]; 4 bytes per element

8/9/2015© Hal Perkins & UW CSEK-32 2-D Arrays Subscripts start with 1 (default) C, etc. use row-major order E.g., an array with 3 rows and 2 columns is stored in this sequence: a(1,1), a(1,2), a(2,1), a(2,2), a(3,1), a(3,2) Fortran 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/etc. code? Java does not have “real” 2-D arrays. A 2-D array is a pointer to a vector of pointers to the array rows

8/9/2015© Hal Perkins & UW CSEK-33 a(i,j) in C/C++/etc. To find a(i,j), we need to know Values of i and j How many columns the array has Location of a(i,j) is Location of a + (i-1)*(#of columns) + (j-1) Can factor to pull out load-time constant part and evaluate that at load time – no recalculating at runtime

8/9/2015© Hal Perkins & UW CSEK-34 Coming Attractions Code Generation for Objects Representation Method calls Inheritance and overriding Strategies for implementing code generators Code improvement – optimization