Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 23 Basic Blocks Topics Code Generation Readings: 9 April 17, 2006 CSCE 531 Compiler Construction.

Similar presentations


Presentation on theme: "Lecture 23 Basic Blocks Topics Code Generation Readings: 9 April 17, 2006 CSCE 531 Compiler Construction."— Presentation transcript:

1 Lecture 23 Basic Blocks Topics Code Generation Readings: 9 April 17, 2006 CSCE 531 Compiler Construction

2 – 2 – CSCE 531 Spring 2006 Overview Last Time – Lec22 slides 1-14, 15-16 Finishing touches on Arrays in expressions Project 5 Today’s Lecture Questions on Project 5 – Functions Code Generation References: Chapter 9

3 – 3 – CSCE 531 Spring 2006 Code Generation Chapter 9 Issues in Code Generation Input to code generator Input to code generator Target programs Target programs Memory management Memory management Instruction selection Instruction selection Register allocation Register allocation

4 – 4 – CSCE 531 Spring 2006 Input to code generator QuadruplesTriples Indirect triples Parse trees Syntax trees Mixtures – some trees some quads

5 – 5 – CSCE 531 Spring 2006 Target programs Target Architectures CISC CISC RISC RISC Target Architectures Object modules Object modules Assembly Assembly

6 – 6 – CSCE 531 Spring 2006 Target Machine Architecture RISC vs CISC Byte addressable, word addressable? Byte order? Big Endian vs little Address Modes supported by architecture Absolute Register Indexedd(R)d + contents(R) Indirect register *d(R)contents(d + contents(R)) Cost of address modes in references to memory Cost of address modes in references to memory

7 – 7 – CSCE 531 Spring 2006 Memory management

8 – 8 – CSCE 531 Spring 2006 Instruction selection

9 – 9 – CSCE 531 Spring 2006 Instruction Costs Floating point costs Floating point costs FPadd = 2 time units FPmultiply = 5 time units FPdivide=9 time units Page 521 – costs measured in instruction length (somewhat dated, but simple to understand) Page 521 – costs measured in instruction length (somewhat dated, but simple to understand) mov R0, R1 - cost = 1 mov R0, R1 - cost = 1 mov R5, mem- cost = 2 mov R5, mem- cost = 2 add $1, r3- cost = 2 add $1, r3- cost = 2 sub 4(R0), *12(R1) - cost =3 sub 4(R0), *12(R1) - cost =3

10 – 10 – CSCE 531 Spring 2006 Instruction Costs of Blocks: a = b + c mov b, R0 addc, R0 movR0, a

11 – 11 – CSCE 531 Spring 2006 Other Cost Metrics

12 – 12 – CSCE 531 Spring 2006 Run-Time Storage Managment

13 – 13 – CSCE 531 Spring 2006 Heap Really Operating System Problem not Compilers job System Calls System Calls ptr = malloc(size) free(ptr) Free space list What functionality the compiler needs to supply? Just pointers, function calls

14 – 14 – CSCE 531 Spring 2006 Basic Blocks and Flow Graphs To generate better code, we will need to analyze the structure of code written as list of quadruples A Basic Block is a sequence of consecutive statements in which flow of control enters at the beginning and leaves at the end without possibility of branching except at the end A Basic Block is a sequence of consecutive statements in which flow of control enters at the beginning and leaves at the end without possibility of branching except at the end Define/Use: a:= b + c Define/Use: a:= b + c This statement defines “a” It uses “b” and “c” A name (identifier) is said to be live at a given point if its value is after that point in the program A name (identifier) is said to be live at a given point if its value is after that point in the program

15 – 15 – CSCE 531 Spring 2006 Leaders of the Block A leader is the first statement of a basic block. The algorithm for Basic Blocks  Determine the leaders first.  The first statement is a leader.  Any statement that is the target of a branch is a leader.  Any statement immediately following a branch is a leader.  For each leader the block extends from the leader up to the statement just prior to the next leader.

16 – 16 – CSCE 531 Spring 2006 Example 9.3 – Dot Product

17 – 17 – CSCE 531 Spring 2006 Basic Blocks for Dot Product Code

18 – 18 – CSCE 531 Spring 2006 Transformations on Basic Blocks Common subexpression elimination Common subexpression elimination Dead-code elimination Dead-code elimination Renaming temporary variables Renaming temporary variables Reordering independent statements Reordering independent statements

19 – 19 – CSCE 531 Spring 2006 Flow Graphs Successor block Predecessor block Loops Strongly connected Inner loop

20 – 20 – CSCE 531 Spring 2006 Example

21 – 21 – CSCE 531 Spring 2006 Computing Next Uses Scan block backwards from last statement to leader For statement (i) x := y op z Attach to statement I, current info from symbol table on the liveness of x y and z In the symbol table Mark x “not live” = “no next use” In the symbol table change next uses of y and z to (i) “statement number i”

22 – 22 – CSCE 531 Spring 2006 A Simple Code Generation Algorithm

23 – 23 – CSCE 531 Spring 2006 GetReg


Download ppt "Lecture 23 Basic Blocks Topics Code Generation Readings: 9 April 17, 2006 CSCE 531 Compiler Construction."

Similar presentations


Ads by Google