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

Slides:



Advertisements
Similar presentations
Garbage collection David Walker CS 320. Where are we? Last time: A survey of common garbage collection techniques –Manual memory management –Reference.
Advertisements

Intermediate Code Generation
The University of Adelaide, School of Computer Science
Compilation /15a Lecture 13 Compiling Object-Oriented Programs Noam Rinetzky 1.
COMP 2003: Assembly Language and Digital Logic
Lecture 08a – Backpatching & Recap Eran Yahav 1 Reference: Dragon 6.2,6.3,6.4,6.6.
C Programming and Assembly Language Janakiraman V – NITK Surathkal 2 nd August 2014.
Compiler construction in4020 – lecture 12 Koen Langendoen Delft University of Technology The Netherlands.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
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 13 – Compiling Object-Oriented Programs Eran Yahav 1 Reference: MCD
Theory of Compilation Erez Petrank Lecture 9: Runtime (part 2); object oriented issues 1.
Lecture 11 – Code Generation Eran Yahav 1 Reference: Dragon 8. MCD
Compiler Construction Code Generation II Rina Zviel-Girshin and Ohad Shacham School of Computer Science Tel-Aviv University.
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.
1 ICS 51 Introductory Computer Organization Fall 2006 updated: Oct. 2, 2006.
Code Generation Professor Yihjia Tsai Tamkang University.
Run time vs. Compile time
Compiler Summary Mooly Sagiv html://
Assembly תרגול 8 פונקציות והתקפת buffer.. Procedures (Functions) A procedure call involves passing both data and control from one part of the code to.
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
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.
Addressing Modes Chapter 11 S. Dandamudi To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer,  S.
CEG 320/520: Computer Organization and Assembly Language ProgrammingIntel Assembly 1 Intel IA-32 vs Motorola
6.828: PC hardware and x86 Frans Kaashoek
Fast, Effective Code Generation in a Just-In-Time Java Compiler Rejin P. James & Roshan C. Subudhi CSE Department USC, Columbia.
Dr. José M. Reyes Álamo 1.  The 80x86 memory addressing modes provide flexible access to memory, allowing you to easily access ◦ Variables ◦ Arrays ◦
Today’s topics Parameter passing on the system stack Parameter passing on the system stack Register indirect and base-indexed addressing modes Register.
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.
Code Generation III. PAs PA4 5.1 – 7.2 PA5 (bonus) 24.1 –
The x86 Architecture Lecture 15 Fri, Mar 4, 2005.
Activation Records (in Tiger) CS 471 October 24, 2007.
Compilation (Semester A, 2013/14) Lecture 13b: Memory Management Noam Rinetzky Slides credit: Eran Yahav 1.
Execution of an instruction
Today’s topics Procedures Procedures Passing values to/from procedures Passing values to/from procedures Saving registers Saving registers Documenting.
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.
Microprocessors The ia32 User Instruction Set Jan 31st, 2002.
Addressing Modes Chapter 6 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
Code Generation Ⅰ CS308 Compiler Theory1. 2 Background The final phase in our compiler model Requirements imposed on a code generator –Preserving the.
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.
Compilers Modern Compiler Design
Compiler Construction Code Generation Activation Records
Compilation Lecture 13: Course summary: Advanced Topics Noam Rinetzky 1.
Computer Organization Instructions Language of The Computer (MIPS) 2.
Introduction to Intel IA-32 and IA-64 Instruction Set Architectures.
1 Assembly Language: Function Calls Jennifer Rexford.
Code Generation II. 2 Compiler IC Program ic x86 executable exe Lexical Analysis Syntax Analysis Parsing ASTSymbol Table etc. Inter. Rep. (IR) Code Generation.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee Control Unit.
Chapter 8 String Operations. 8.1 Using String Instructions.
1 Chapter10: Code generator. 2 Code Generator Source Program Target Program Semantic Analyzer Intermediate Code Generator Code Optimizer Code Generator.
Compilation (Semester A, 2013/14)
Assembly language.
Credits and Disclaimers
Compilers.
Introduction to Compilers Tim Teitelbaum
Assembly IA-32.
Unit IV Code Generation
Introduction to Intel IA-32 and IA-64 Instruction Set Architectures
MIPS Procedure Calls CSE 378 – Section 3.
Week 2: Buffer Overflow Part 1.
Optimization 薛智文 (textbook ch# 9) 薛智文 96 Spring.
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.
Credits and Disclaimers
Computer Architecture and System Programming Laboratory
Presentation transcript:

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

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

Last Week: Runtime Part II  Nested procedures  Object layout  Inheritance  Multiple inheritance 3

Today  Runtime checks  Garbage collection  Generating assembly code 4

5 Runtime checks  generate code for checking attempted illegal operations  Null pointer check  MoveField, MoveArray, ArrayLength, VirtualCall  Reference arguments to library functions should not be null  Array bounds check  Array allocation size check  Division by zero  …  If check fails jump to error handler code that prints a message and gracefully exists program

6 Null pointer check # null pointer check cmp $0,%eax je labelNPE labelNPE: push $strNPE# error message call __println push $1# error code call __exit Single generated handler for entire program

7 Array bounds check # array bounds check mov -4(%eax),%ebx # ebx = length mov $0,%ecx # ecx = index cmp %ecx,%ebx jle labelABE # ebx <= ecx ? cmp $0,%ecx jl labelABE # ecx < 0 ? labelABE: push $strABE # error message call __println push $1 # error code call __exit Single generated handler for entire program

8 Array allocation size check # array size check cmp $0,%eax# eax == array size jle labelASE # eax <= 0 ? labelASE: push $strASE # error message call __println push $1 # error code call __exit Single generated handler for entire program

Automatic Memory Management  automatically free memory when it is no longer needed  not limited to OO programs, we show it here because it is prevalent in OO languages such as Java  also in functional languages  approximate reasoning about object liveness  use reachability to approximate liveness  assume reachable objects are live  non-reachable objects are dead  Three classical garbage collection techniques  reference counting  mark and sweep  copying 9

GC using Reference Counting  add a reference-count field to every object  how many references point to it  when (rc==0) the object is non reachable  non reachable => dead  can be collected (deallocated) 10

Managing Reference Counts  Each object has a reference count o.RC  A newly allocated object o gets o.RC = 1  why?  write-barrier for reference updates update(x,old,new) { old.RC--; new.RC++; if (old.RC == 0) collect(old); }  collect(old) will decrement RC for all children and recursively collect objects whose RC reached 0. 11

Cycles!  cannot identify non-reachable cycles  reference counts for nodes on the cycle will never decrement to 0  several approaches for dealing with cycles  ignore  periodically invoke a tracing algorithm to collect cycles  specialized algorithms for collecting cycles 12

GC Using Mark & Sweep  Marking phase  mark roots  trace all objects transitively reachable from roots  mark every traversed object  Sweep phase  scan all objects in the heap  collect all unmarked objects 13

14 mark_sweep() { for Ptr in Roots mark(Ptr) sweep() } mark(Obj) { if mark_bit(Obj) == unmarked { mark_bit(Obj)=marked for C in Children(Obj) mark(C) } Sweep() { p = Heap_bottom while (p < Heap_top) if (mark_bit(p) == unmarked) then free(p) else mark_bit(p) = unmarked; p=p+size(p) } GC Using Mark & Sweep

Copying GC  partition the heap into two parts: old space, new space  GC  copy all reachable objects from old space to new space  swap roles of old/new space 15

Example 16 oldnew Roots A D C B E

Example 17 oldnew Roots A D C B E A C

Summary  How objects are organized in memory  Automatic management of memory  Coming up…  Generating assembly code 18

target languages 19 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 20

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) 21

AT&T vs. Intel Syntax AttributeAT&TIntel Parameter order Source comes before the destination Destination before Parameter Size Mnemonics are suffixed with a letter indicating the size of the operands (e.g., "q" for qword, "l" for dword, "w" for word, and "b" for byte) Derived from the name of the register that is used Immediate value signals Prefixed with a "$", and registers must be prefixed with a "%” The assembler automatically detects the type of symbols; i.e., if they are registers, constants or something else. Effective addresses General syntax DISP(BASE,INDEX,SCALE) Example: movl mem_location(%ebx,%ecx,4), %eax Use variables, and need to be in square brackets; additionally, size keywords like byte, word, or dword have to be used. [1] Example: mov eax, dword [ebx + ecx*4 + mem_location] 22

23 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)

24 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

25 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 26

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) 27

28 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 29

Example 30 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 31

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 32 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) 33 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 34 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 35

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 36

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 37 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 38 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 39 a = b + c b = a - d c = b + c d = a - d b0c0 + d0 - + a b,d c

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

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

coming up next  register allocation 42

The End 43