1 cs 201 compiler construction machine code generation

16
1 CS 201 Compiler Construction Machine Code Generation

Upload: deonte-brafford

Post on 14-Dec-2015

213 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 1 CS 201 Compiler Construction Machine Code Generation

1

CS 201Compiler Construction

Machine Code Generation

Page 2: 1 CS 201 Compiler Construction Machine Code Generation

Code Generation – Naïve Approach

Macro expand each intermediate code statement into a sequence of target machine instructions.

2

A=B+C

M=A+B

Load A, R1ADD C, R1Store R1, A

Load A, R1 -- redundantADD B, R1Store R1, M

Page 3: 1 CS 201 Compiler Construction Machine Code Generation

Naïve Approach Contd..

Assumption: All registers are free immediately preceding and following an intermediate code statement.+ each intermediate code statement can be translated independently- code is of poor quality – too many loads and stores.To obtain good quality code multiple intermediate code statements should be examined together and register allocation and assignment should be performed to minimize loads and stores.

3

Page 4: 1 CS 201 Compiler Construction Machine Code Generation

Register Allocation & Assignment

Allocation: What variables should be put into registers? Assignment: What specific registers should be assigned at each program point?

4

Local Register Allocation: Examine each basic block independently, i.e. all register are free at basic block boundaries.Global Register Allocation: Examine the code for each function independently, i.e. all registers are free at function boundaries.

Page 5: 1 CS 201 Compiler Construction Machine Code Generation

Register Allocation & Assignment

5

Generating code for A=B+C assuming B is in R11. ADD C,R1 – neither B or C available in register2. Load C, R2 – C available in R2; B not available ADD R2, R13. Load C, R2 – C not available; B available in R1 ADD R1, R2Usage of B and C after A=B+C determines the preferable choice.

Page 6: 1 CS 201 Compiler Construction Machine Code Generation

Order of Statements Matters

(A+B) – (C – X*Z)

6

1.T1 = A + B2.T2 = X * Z3.T3 = C - T24.T4 = T1 – T3

1.Load A, R11.Add B, R12.Load X, R22.Mult Z, R2Store R1, T13.Load C, R13.Sub R2, R1Load T1, R24.Sub R1, R24.Store R2, T4

1.T2 = X * Z2.T3 = C - T23.T1 = A + B4.T4 = T1 – T3

1.Load X, R11.Mult Z, R12.Load C, R22.Sub R1, R23.Load A, R13.Add B, R14.Sub R2, R14.Store R1, T4

Spill Code

Page 7: 1 CS 201 Compiler Construction Machine Code Generation

Order of Statements Contd…

The ordering of statements effects maximum number of simultaneously live variables and hence the minimum number of registers needed to avoid spill code.

Rest of the discussion:+ Code reordering within basic blocks+ Global register allocation

7

Page 8: 1 CS 201 Compiler Construction Machine Code Generation

Reordering Code Statements

DAG – Directed Acyclic GraphA representation to assist in code reordering.+ Nodes are operations+ Edges represent dependencesNodes are labeled as follows:1. Leaves with variables or constants – subscript 0 is used to distinguish initial value of the variable from other values.2. Interior nodes with operators and list of variables whose values are computed by the node.

8

Page 9: 1 CS 201 Compiler Construction Machine Code Generation

DAGs Contd..

DAGs are useful for:1. Removing common local sub-expressions. 2. Renaming temporaries.3. Finding names used inside the block but evaluated outside.4. Finding statements in the block that could have their computed values used outside the block.5. Statements that can be reordered (or executed in parallel).

9

Page 10: 1 CS 201 Compiler Construction Machine Code Generation

DAG Construction

Given a function node(identifier) – it returns the node currently associated with the identifier.Process one statement at a time as follows: Statement types: x = y op z; x = op y; x = y;1. If node(y) is undefined then create a leaf labeled yo and this is now node(y) in case of x = y op z; do the same for z.2. Determine if there is a node labeled “op” with node(y) & node(z) as the left and right children in case of x = y op z.

10

Page 11: 1 CS 201 Compiler Construction Machine Code Generation

DAG Construction Contd..

2 Contd. In case of x = op y, check if there is a node labeled “op” with single child node(y).

If not, create such a node. Let the node found or created by n.3. Delete x from the list of identifiers attached to

node(x) & append x to the list of identifiers attached to node n.

Set node(x) to n.

11

Page 12: 1 CS 201 Compiler Construction Machine Code Generation

Example of DAG Construction

12

T1 = J * 2T2 = X[T1]T3 = T2 + NT4 = J * 2T5 = Y[T4]T6 = T5 + T3ANS = T6T7 = J+ 1J = T7ANS = JIf J<10 goto L

Page 13: 1 CS 201 Compiler Construction Machine Code Generation

Code Reordering

DAG captures all legal re-orderings. Which ordering should we select?

Try to place computation & use of a value next to each other so that the register used by the value is freed immediately.

Next we will look at a heuristic that attempts, as far as possible, to make the evaluation of a node immediately follow the evaluation of the leftmost argument.

13

Page 14: 1 CS 201 Compiler Construction Machine Code Generation

Code Reordering Contd..

Algorithm: List nodes in reverse order.

While unlisted interior nodes remain Doselect an unlisted node n, all of whose parents have been listedlist nWhile leftmost child m of n has no unlisted parents and is not a leaf Do

list mn = mEnd while

End while

14

Page 15: 1 CS 201 Compiler Construction Machine Code Generation

Example

15

Page 16: 1 CS 201 Compiler Construction Machine Code Generation

Sample Problem

16