Docsity

Basic Code Generation, DAG Representations - Compiler Construction - Lecture Slides, Slides of Compiler Construction

Partial preview of the text

Download Basic Code Generation, DAG Representations - Compiler Construction - Lecture Slides and more Slides Compiler Construction in PDF only on Docsity!

Compiler

Construction

Lecture 45

2

Liveness information allows us to keep values in registers if they will be used later (efficiency)

Basic Code Generation

4

 Why do we need to save live variables at the end?

 We might have to reload them in the next block.

 Global Data-Flow Analysis

Basic Code Generation

5

DAG Representations

A dag (directed acyclic graph)

for a basic block has the

following labels for the nodes:

7

DAG Representations

BB DAG for x = y op z

op x

y z

8

Generating DAGs

For statement i: x = y op z  if y op z node exists, add x to the label for that node. else add node for y op z.  If y or z exist in the dag, point to existing locations else add leaves for y and/or z and have the op node point to them.

10

Example 1

a = b + c t1 = a * a b = t1 + a c = t1 * b t2 = c + b a = t2 + t

11

Example 1

a = b + c t1 = a * a b = t1 + a c = t1 * b t2 = c + b a = t2 + t2 (^) b c

a +

13

Example 1

a = b + c t1 = a * a b = t1 + a c = t1 * b t2 = c + b a = t2 + t2 (^) b’

t

c

b +

a

14

Example 1

a = b + c t1 = a * a b = t1 + a c = t1 * b t2 = c + b a = t2 + t2 (^) b’

t

c’

b +

  • c

a

16

Example 1

a = b + c t1 = a * a b = t1 + a c = t1 * b t2 = c + b a = t2 + t2 (^) b’

t

c’

t

b +

c

a

17

Example 2

t1 = 4 * i t2 = a[t1] t3 = 4 * i t4 = b[t3] t5 = t2 * t t6 = p + t p = t t7 = i + 1 i = t if i <20 goto (1)

a b 4 i’ 1

[ ] (^) [ ] <

p’

t6,p t t2 t t1,t3 (^) t7,i

(1) 20

19

Order Matters

Order 1 t1 = a + b t2 = c + d t3 = e – t t4 = t1 – t

Order 2 t2 = c + d t3 = e – t t1 = a + b t4 = t1 – t

a (^) b e + c (^) d

t

t

t t

20

Order #1 with 2 registers

t1 = a + b t2 = c + d t3 = e – t t4 = t1 – t

MOV a, R ADD b,R MOV c, R ADD D, R MOV R0,t MOV e, R SUB R1,R MOV t1,R SUB R0, R MOV R1,t ; 10 instructions