Lecture 11 – Code Generation Eran Yahav 1 Reference: Dragon 8. MCD 4.2.4 www.cs.technion.ac.il/~yahave/tocs2011/compilers-lec11.pptx.

Slides:



Advertisements
Similar presentations
Target Code Generation
Advertisements

Intermediate Code Generation
1 Compiler Construction Intermediate Code Generation.
Lecture 26 Epilogue: Or Everything else you Wanted to Know about Compilers (more accurately Everything else I wanted you to Know) Topics Getreg – Error.
Lecture 10: Code Generation 1. You are here 2 Executable code exe Source text txt Compiler Lexical Analysis Syntax Analysis Semantic Analysis Inter. Rep.
Lecture 12 – Code Generation Eran Yahav 1 Reference: Dragon 8. MCD
Representing programs Goals. Representing programs Primary goals –analysis is easy and effective just a few cases to handle directly link related things.
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
PC hardware and x86 3/3/08 Frans Kaashoek MIT
1 Intermediate representation Goals: –encode knowledge about the program –facilitate analysis –facilitate retargeting –facilitate optimization scanning.
Lecture 23 Basic Blocks Topics Code Generation Readings: 9 April 17, 2006 CSCE 531 Compiler Construction.
1 ICS 51 Introductory Computer Organization Fall 2006 updated: Oct. 2, 2006.
Code Generation Professor Yihjia Tsai Tamkang University.
Lecture 25 Generating Code for Basic Blocks Topics Code Generation Readings: April 19, 2006 CSCE 531 Compiler Construction.
Improving Code Generation Honors Compilers April 16 th 2002.
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
Compiler Construction A Compulsory Module for Students in Computer Science Department Faculty of IT / Al – Al Bayt University Second Semester 2008/2009.
Compiler Construction Activation records Code Generation Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.
Compiler Construction Code Generation I Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University.
Data Transfer & Decisions I (1) Fall 2005 Lecture 3: MIPS Assembly language Decisions I.
CEG 320/520: Computer Organization and Assembly Language ProgrammingIntel Assembly 1 Intel IA-32 vs Motorola
6.828: PC hardware and x86 Frans Kaashoek
Dr. José M. Reyes Álamo 1.  The 80x86 memory addressing modes provide flexible access to memory, allowing you to easily access ◦ Variables ◦ Arrays ◦
Lecture 10 – Code Generation Eran Yahav 1 Reference: Dragon 8. MCD
1 Code Generation Part II Chapter 8 (1 st ed. Ch.9) COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
Fall 2012 Chapter 2: x86 Processor Architecture. Irvine, Kip R. Assembly Language for x86 Processors 6/e, Chapter Overview General Concepts IA-32.
CSc 453 Final Code Generation Saumya Debray The University of Arizona Tucson.
1 Code Generation Part II Chapter 9 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
The x86 Architecture Lecture 15 Fri, Mar 4, 2005.
Execution of an instruction
1 Lecture 10: Code Generation. You are here 2 Executable code exe Source text txt Compiler Lexical Analysis Syntax Analysis Semantic Analysis Inter. Rep.
Winter Compiler Construction T11 – Activation records + Introduction to x86 assembly Mooly Sagiv and Roman Manevich School of Computer Science.
1 ICS 51 Introductory Computer Organization Fall 2009.
Code Generation Ⅰ CS308 Compiler Theory1. 2 Background The final phase in our compiler model Requirements imposed on a code generator –Preserving the.
Chapter# 6 Code generation.  The final phase in our compiler model is the code generator.  It takes as input the intermediate representation(IR) produced.
Other Processors. Having learnt MIPS, we can learn other major processors. Not going to be able to cover everything; will pick on the interesting aspects.
Compilers Modern Compiler Design
Compiler Construction Code Generation Activation Records
Computer Organization Instructions Language of The Computer (MIPS) 2.
Introduction to Intel IA-32 and IA-64 Instruction Set Architectures.
CC410: System Programming Dr. Manal Helal – Fall 2014 – Lecture 4 - Assembler 1.
Road Map Regular Exprs, Context-Free Grammars Regular Exprs, Context-Free Grammars LR parsing algorithm LR parsing algorithm Building LR parse tables Building.
Topic #9: Target Code EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
1 Chapter10: Code generator. 2 Code Generator Source Program Target Program Semantic Analyzer Intermediate Code Generator Code Optimizer Code Generator.
Code Generation Part I Chapter 8 (1st ed. Ch.9)
Chapter 8 Code Generation
Compilation (Semester A, 2013/14)
Assembly language.
Credits and Disclaimers
Other Processors.
Introduction to Compilers Tim Teitelbaum
BIC 10503: COMPUTER ARCHITECTURE
Code Generation Part I Chapter 9
Code Generation.
Unit IV Code Generation
Code Generation Part I Chapter 8 (1st ed. Ch.9)
Introduction to Intel IA-32 and IA-64 Instruction Set Architectures
ECE232: Hardware Organization and Design
Code Generation Part I Chapter 9
MIPS Procedure Calls CSE 378 – Section 3.
Week 2: Buffer Overflow Part 1.
TARGET CODE -Next Usage
8 Code Generation Topics A simple code generator algorithm
Optimization 薛智文 (textbook ch# 9) 薛智文 96 Spring.
Compiler Construction
Code Generation Part II
Other Processors Having learnt MIPS, we can learn other major processors. Not going to be able to cover everything; will pick on the interesting aspects.
Target Code Generation
Credits and Disclaimers
Computer Architecture and System Programming Laboratory
Presentation transcript:

Lecture 11 – Code Generation Eran Yahav 1 Reference: Dragon 8. MCD

2 You are here Executable code exe Source text txt Compiler Lexical Analysis Syntax Analysis Parsing Semantic Analysis Inter. Rep. (IR) Code Gen.

target languages 3 Absolute machine code Code Gen. Relative machine code Assembly IR + Symbol Table

From IR to ASM: Challenges  mapping IR to ASM operations  what instruction(s) should be used to implement an IR operation?  how do we translate code sequences  call/return of routines  managing activation records  memory allocation  register allocation  optimizations 4

Intel IA-32 Assembly  Going from Assembly to Binary…  Assembling  Linking  AT&T syntax vs. Intel syntax  We will use AT&T syntax  matches GNU assembler (GAS) 5

6 IA-32 Registers  Eight 32-bit general-purpose registers  EAX – accumulator for operands and result data. Used to return value from function calls.  EBX – pointer to data. Often use as array-base address  ECX – counter for string and loop operations  EDX – I/O pointer (GP for us)  ESI – GP and source pointer for string operations  EDI – GP and destination pointer for string operations  EBP – stack frame (base) pointer  ESP – stack pointer  EFLAGS register  EIP (instruction pointer) register  Six 16-bit segment registers  … (ignore the rest for our purposes)

7 Not all registers are born equal  EAX  Required operand of MUL,IMUL,DIV and IDIV instructions  Contains the result of these operations  EDX  Stores remainder of a DIV or IDIV instruction (EAX stores quotient)  ESI, EDI  ESI – required source pointer for string instructions  EDI – required destination pointer for string instructions  Destination Registers of Arithmetic operations  EAX, EBX, ECX, EDX  EBP – stack frame (base) pointer  ESP – stack pointer

8 IA-32 Addressing Modes  Machine-instructions take zero or more operands  Source operand  Immediate  Register  Memory location  (I/O port)  Destination operand  Register  Memory location  (I/O port)

Immediate and Register Operands  Immediate  Value specified in the instruction itself  GAS syntax – immediate values preceded by $  add $4, %esp  Register  Register name is used  GAS syntax – register names preceded with %  mov %esp,%ebp 9

Memory and Base Displacement Operands  Memory operands  Value at given address  GAS syntax - parentheses  mov (%eax), %eax  Base displacement  Value at computed address  Address computed out of  base register, index register, scale factor, displacement  offset = base + (index*scale) + displacement  Syntax: disp(base,index,scale)  movl $42, $2(%eax)  movl $42, $1(%eax,%ecx,4) 10

11 Base Displacement Addressing Mov (%ecx,%ebx,4), %eax 7 Array Base Reference %ecx = base %ebx = 3 offset = base + (index*scale) + displacement offset = base + (3*4) + 0 = base + 12 (%ecx,%ebx,4)

How do we generate the code?  break the IR into basic blocks  basic block is a sequence of instructions with  single entry (to first instruction), no jumps to the middle of the block  single exit (last instruction)  code execute as a sequence from first instruction to last instruction without any jumps  edge from one basic block B1 to another block B2 when the last statement of B1 may jump to B2 12

Example 13 False B1B1 B2B2 B3B3 B4B4 True t 1 := 4 * i t 2 := a [ t 1 ] if t 2 <= 20 goto B 3 t 5 := t 2 * t 4 t 6 := prod + t 5 prod := t 6 goto B 4 t 7 := i + 1 i := t 2 Goto B 5 t 3 := 4 * i t 4 := b [ t 3 ] goto B 4

creating basic blocks  Input: A sequence of three-address statements  Output: A list of basic blocks with each three- address statement in exactly one block  Method  Determine the set of leaders (first statement of a block)  The first statement is a leader  Any statement that is the target of a conditional or unconditional jump is a leader  Any statement that immediately follows a goto or conditional jump statement is a leader  For each leader, its basic block consists of the leader and all statements up to but not including the next leader or the end of the program 14

control flow graph  A directed graph G=(V,E)  nodes V = basic blocks  edges E = control flow  (B1,B2)  E when control from B1 flows to B2 15 B1B1 B2B2 t 1 := 4 * i t 2 := a [ t 1 ] t 3 := 4 * i t 4 := b [ t 3 ] t 5 := t 2 * t 4 t 6 := prod + t 5 prod := t 6 t 7 := i + 1 i := t 7 if i <= 20 goto B 2 prod := 0 i := 1

example 1) i = 1 2) j =1 3) t1 = 10*I 4) t2 = t1 + j 5) t3 = 8*t2 6) t4 = t3-88 7) a[t4] = 0.0 8) j = j + 1 9) if j <= 10 goto (3) 10) i=i+1 11) if i <= 10 goto (2) 12) i=1 13) t5=i-1 14) t6=88*t5 15) a[t6]=1.0 16) i=i+1 17) if I <=10 goto (13) 16 i = 1 j = 1 t1 = 10*I t2 = t1 + j t3 = 8*t2 t4 = t3-88 a[t4] = 0.0 j = j + 1 if j <= 10 goto B3 i=i+1 if i <= 10 goto B2 i = 1 t5=i-1 t6=88*t5 a[t6]=1.0 i=i+1 if I <=10 goto B6 B1B1 B2B2 B3B3 B4B4 B5B5 B6B6 for i from 1 to 10 do for j from 1 to 10 do a[i, j] = 0.0; for i from 1 to 10 do a[i, i] = 1.0; sourceIR CFG

Variable Liveness  A statement x = y + z  defines x  uses y and z  A variable x is live at a program point if its value is used at a later point 17 y = 42 z = 73 x = y + z print(x); x is live, y dead, z dead x undef, y live, z live x undef, y live, z undef x is dead, y dead, z dead (showing state after the statement)

Computing Liveness Information  between basic blocks – dataflow analysis (next lecture)  within a single basic block?  idea  use symbol table to record next-use information  scan basic block backwards  update next-use for each variable 18

Computing Liveness Information  INPUT: A basic block B of three-address statements. symbol table initially shows all non-temporary variables in B as being live on exit.  OUTPUT: At each statement i: x = y + z in B, liveness and next-use information of x, y, and z at i.  Start at the last statement in B and scan backwards  At each statement i: x = y + z in B, we do the following: 1. Attach to i the information currently found in the symbol table regarding the next use and liveness of x, y, and z. 2. In the symbol table, set x to "not live" and "no next use.“ 3. In the symbol table, set y and z to "live" and the next uses of y and z to i 19

Computing Liveness Information  Start at the last statement in B and scan backwards  At each statement i: x = y + z in B, we do the following: 1. Attach to i the information currently found in the symbol table regarding the next use and liveness of x, y, and z. 2. In the symbol table, set x to "not live" and "no next use.“ 3. In the symbol table, set y and z to "live" and the next uses of y and z to i 20 can we change the order between 2 and 3? x = 1 y = x + 3 z = x * 3 x = x * z

common-subexpression elimination  common-subexpression elimination 21 a = b + c b = a – d c = b + c d = a - d a = b + c b = a – d c = b + c d = b

DAG Representation of Basic Blocks 22 a = b + c b = a - d c = b + c d = a - d b0c0 + d0 - + a b,d c

DAG Representation of Basic Blocks 23 a = b + c b = b - d c = c + d e = b + c b0c0 + d0 - + a bc + e

algebraic identities 24 a = x^2 b = x*2 c = x/2 d = 1*x a = x*x b = x+x c = x*0.5 d = x

Simple code generation  registers  used as operands of instructions  can be used to store temporary results  can (should) be used as loop indexes due to frequent arithmetic operation  used to manage administrative info (e.g., runtime stack)  number of registers is limited  need to allocate them in a clever way 25

simple code generation  assume machine instructions of the form  LD reg, mem  ST mem, reg  OP reg,reg,reg  further assume that we have all registers available for our use  ignore registers allocated for stack management 26

simple code generation  translate each 3AC instruction separately  A register descriptor keeps track of the variable names whose current value is in that register.  we use only those registers that are available for local use within a basic block, we assume that initially, all register descriptors are empty.  As code generation progresses, each register will hold the value of zero or more names.  For each program variable, an address descriptor keeps track of the location or locations where the current value of that variable can be found.  The location may be a register, a memory address, a stack location, or some set of more than one of these  Information can be stored in the symbol-table entry for that variable 27

simple code generation For each three-address statement x := y op z, 1. Invoke getreg (x := y op z) to select registers R x, R y, and R z. 2. If Ry does not contain y, issue: “LD R y, y’ ”, for a location y’ of y. 3. If Rz does not contain z, issue: “LD R z, z’ ”, for a location z’ of z. 4. Issue the instruction “OP R x,R y,R z ” 5. Update the address descriptors of x, y, z, if necessary.  R x is the only location of x now, and R x contains only x (remove R x from other address descriptors). 28

updating descriptors  1. For the instruction LD R, x a) Change the register descriptor for register R so it holds only x. b) Change the address descriptor for x by adding register R as an additional location.  2. For the instruction ST x, R, change the address descriptor for x to include its own memory location.  3. For an operation such as ADD Rx, Ry, Rz, implementing a 3AC instruction x = y + z a) Change the register descriptor for Rx so that it holds only x. b) Change the address descriptor for x so that its only location is Rx. Note that the memory location for x is not now in the address descriptor for x. c) Remove Rx from the address descriptor of any variable other than x.  4. When we process a copy statement x = y, after generating the load for y into register Ry, if needed, and after managing descriptors as for all load statements (rule 1): a) Add x to the register descriptor for Ry. b) Change the address descriptor for x so that its only location is Ry. 29

example 30 t= A – B u = A- C v = t + u A = D D = v + u A B C D = live outside the block t,u,v = temporaries in local storate R1 R2R3 ABC A BC D D tuv t = A – B LD R1,A LD R2,B SUB R2,R1,R2 At R1 R2R3 A,R1 BC A BC DR2 D tuv u = A – C LD R3,C SUB R1,R1,R3 v = t + u ADD R3,R2,R1 utC R1 R2R3 AB C,R3 A BC DR2R1 D tuv utv R2R3 ABC A BC DR2R1 D tu R3 v

example 31 t= A – B u = A- C v = t + u A = D D = v + u A B C D = live outside the block t,u,v = temporaries in local storate A = D LD R2, D u A,D v R1 R2R3 R2 BC A BC D,R2 R1 D tu R3 v D = v + u ADD R1,R3,R1 exit ST A, R2 ST D, R1 DAv R1 R2R3 R2BC A BC R1 D tu R3 v utv R1 R2R3 ABC A BC DR2R1 D tu R3 v DAv R1 R2R3 A,R2 BC A BC D,R1 D tu R3 v

coming up next  register allocation 32

The End 33