Download presentation

Presentation is loading. Please wait.

Published byDeonte Brafford Modified over 2 years ago

1
1 CS 201 Compiler Construction Machine Code Generation

2
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, R1 ADD C, R1 Store R1, A Load A, R1 -- redundant ADD B, R1 Store R1, M

3
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

4
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.

5
Register Allocation & Assignment 5 Generating code for A=B+C assuming B is in R1 1. ADD C,R1 – neither B or C available in register 2. Load C, R2 – C available in R2; B not available ADD R2, R1 3. Load C, R2 – C not available; B available in R1 ADD R1, R2 Usage of B and C after A=B+C determines the preferable choice.

6
Order of Statements Matters (A+B) – (C – X*Z) 6 1. T1 = A + B 2. T2 = X * Z 3. T3 = C - T2 4. T4 = T1 – T3 1. Load A, R1 1. Add B, R1 2. Load X, R2 2. Mult Z, R2 Store R1, T1 3. Load C, R1 3. Sub R2, R1 Load T1, R2 4. Sub R1, R2 4. Store R2, T4 1. T2 = X * Z 2. T3 = C - T2 3. T1 = A + B 4. T4 = T1 – T3 1. Load X, R1 1. Mult Z, R1 2. Load C, R2 2. Sub R1, R2 3. Load A, R1 3. Add B, R1 4. Sub R2, R1 4. Store R1, T4 Spill Code

7
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

8
Reordering Code Statements DAG – Directed Acyclic Graph A representation to assist in code reordering. + Nodes are operations + Edges represent dependences Nodes 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

9
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

10
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 y o 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

11
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

12
Example of DAG Construction 12 T1 = J * 2 T2 = X[T1] T3 = T2 + N T4 = J * 2 T5 = Y[T4] T6 = T5 + T3 ANS = T6 T7 = J+ 1 J = T7 ANS = J If J<10 goto L

13
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

14
Code Reordering Contd.. Algorithm: List nodes in reverse order. While unlisted interior nodes remain Do select an unlisted node n, all of whose parents have been listed list n While leftmost child m of n has no unlisted parents and is not a leaf Do list m n = m End while 14

15
Example 15

16
Sample Problem 16

Similar presentations

OK

Code Generation for Basic Blocks Introduction Mooly Sagiv html://www.cs.tau.ac.il/~msagiv/courses/wcc03.html Chapter 4.2.5.

Code Generation for Basic Blocks Introduction Mooly Sagiv html://www.cs.tau.ac.il/~msagiv/courses/wcc03.html Chapter 4.2.5.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google