Docsity

Understanding Intermediate Representation & Code Generation Algorithms in CS-322 - Prof. H, Study notes of Computer Science

Toggle sidebar
bg1
1
CS-322 Target Generation, Part 1
© Harry H. Porter, 2006
Intermediate Representation
Intermediate Representation
Target Code (e.g., SPARC “.s” file)
Executable (e.g., an a.out file)
Assembler / Linker
Final Code Generation
Optimization
Type Checking
Parser
Lexer
Front-End
Back-End
Intermediate Code Generation Symbol
Table
Information
2
CS-322 Target Generation, Part 1
© Harry H. Porter, 2006
Intermediate Representation
Intermediate Representation
Byte Codes
Virtual Machine (Interpreter)
Final Code Generation
Optimization
Type Checking
Parser
Lexer
Front-End
Back-End
Intermediate Code Generation Symbol
Table
Information
Java Approach
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29

Partial preview of the text

Download Understanding Intermediate Representation & Code Generation Algorithms in CS-322 - Prof. H and more Study notes Computer Science in PDF only on Docsity!

© Harry H. Porter, 2006

Intermediate Representation

Intermediate Representation

Target Code (e.g., SPARC “.s” file)

Executable (e.g., an a.out file)

Assembler / Linker Final Code Generation Optimization Type Checking Parser Lexer Front-End Back-End Intermediate Code Generation Symbol Table Information CS-322 Target Generation, Part 1 © Harry H. Porter, 2006

Intermediate Representation

Intermediate Representation

Byte Codes

Virtual Machine (Interpreter) Final Code Generation Optimization Type Checking Parser Lexer Front-End Back-End Intermediate Code Generation Symbol Table Information

Java Approach

© Harry H. Porter, 2006

Intermediate Representation

Intermediate Representation

Machine Code

CPU

Final Code Generation Optimization Type Checking Parser Lexer Front-End Back-End Intermediate Code Generation Symbol Table Information CS-322 Target Generation, Part 1 © Harry H. Porter, 2006 Output to Assembly Code (vs. machine code) Breaks code generation task into 2 phases

  • !Compiler back-end
  • !Assembler Easier to debug compiler output! Slightly slower (?)

© Harry H. Porter, 2006 Requirements

  • !Target code must be correct.
  • !Target code should be efficient.
  • !Back-end should run quickly. Want optimal code sequences? NP-Complete Generate all correct code sequences ... and see which is best Optimal? The target program... ... executes faster ... takes less memory CS-322 Target Generation, Part 1 © Harry H. Porter, 2006 Code Generation Algorithms Algorithm #1! Easiest; We’ll use for PCAT Algorithm # Algorithm #3! Most complex

© Harry H. Porter, 2006 Code Generation Algorithms Algorithm #1! Easiest; We’ll use for PCAT Algorithm # Algorithm #3! Most complex Example Target Machine 2-Address Architecture mov x,r add y,r mov r0,z source destination r0 := r0 + y store back into memory CS-322 Target Generation, Part 1 © Harry H. Porter, 2006 Code Generation Algorithm #

Statement-by-statement generation

Code for each IR instruction is

generated independently of all other IR instructions.

IR Code:

a := b + c

d := a + e

© Harry H. Porter, 2006 Code Generation Algorithm #

Statement-by-statement generation

Code for each IR instruction is

generated independently of all other IR instructions.

IR Code:

a := b + c

d := a + e

Target Code:

mov b,r

add c,r

mov r0,a

mov a,r

add e,r

mov r0,d

a := b + c

d := a + e

ALSO: Registers are not

used effectively.

CS-322 Target Generation, Part 1 © Harry H. Porter, 2006 Machine Idioms IR Code: x := x + 5 Target Code: mov x,r add 5,r mov r0,x

© Harry H. Porter, 2006 Machine Idioms IR Code: x := x + 5 Target Code: mov x,r add 5,r mov r0,x IR Code: x := x + 1 Target Code: mov x,r add 1,r mov r0,x CS-322 Target Generation, Part 1 © Harry H. Porter, 2006 Machine Idioms IR Code: x := x + 5 Target Code: mov x,r add 5,r mov r0,x IR Code: x := x + 1 Target Code: mov x,r add 1,r mov r0,x Target Code: mov x,r inc r mov r0,x

© Harry H. Porter, 2006 Assume

Multiply Instruction

mul y,r

Multiply Instruction

div y,r

SRDA: Shift Right Double Arithmetic

srda 32,r

Must specify an even numbered register

r5 " y # [r4,r5]

Must specify an even numbered register

[r4,r5] ÷ y $ [r4,r5]

1010110010 xxxxxxxxxx ssssssssss 1010110010 r6 r7 r6 r CS-322 Target Generation, Part 1 © Harry H. Porter, 2006 IR Code: t := a + b t := t * c t := t / d Target Code: mov a,r add b,r mul c,r div d,r mov r1,t

© Harry H. Porter, 2006 IR Code: t := a + b t := t * c t := t / d Target Code: mov a,r add b,r mul c,r div d,r mov r1,t IR Code: t := a + b t := t + c t := t / d Target Code: mov a,r add b,r add c,r srda 32,r div d,r mov r1,t CS-322 Target Generation, Part 1 © Harry H. Porter, 2006 Conclusion: Where you put the result of t:=a+b (either r0 or r1) depends on how it will be used later!!!

[A “chicken-and-egg” problem]

IR Code: t := a + b t := t * c t := t / d Target Code: mov a,r add b,r mul c,r div d,r mov r1,t IR Code: t := a + b t := t + c t := t / d Target Code: mov a,r add b,r add c,r srda 32,r div d,r mov r1,t

© Harry H. Porter, 2006 An Example Target Machine A 2-address Architecture op source,destination Address Modes: Absolute Memory Address mov x,y sub x,y Register mov r0,r sub r2,r Literal mov 39,r sub 47,r Indirect Register mov r0,[r1] Indirect plus Index mov r0,[r1+48] Double Indirect mov r0,[[r1+48]] 2 operands, at most x # y y–x # y r3–r2 # r Data is included in the instruction directly Register contains an address. Moves data in to word pointed to by r Use r1+48 as an address. Go to memory and fetch a second address, “p”. “p” points to the word. Op-Codes: mov add sub mul ... CS-322 Target Generation, Part 1 © Harry H. Porter, 2006 Evaluating A Potential Code Sequence

Each instruction has a “ cost”

Cost = Execution Time

Execution Time is difficult to predict.

Pipelining, Branches, Delay Slots, etc.

Goal: Approximate the real cost

A “ Cost Model ”

© Harry H. Porter, 2006 Evaluating A Potential Code Sequence

Each instruction has a “ cost”

Cost = Execution Time

Execution Time is difficult to predict.

Pipelining, Branches, Delay Slots, etc.

Goal: Approximate the real cost

A “ Cost Model ” Simplest Cost Model: Code Length % Execution Time Just count the instructions! CS-322 Target Generation, Part 1 © Harry H. Porter, 2006 A Better Cost Model

Look at each instruction.

Compute a cost (in “units”).

Count the number of memory accesses.

Cost = 1 + Cost-of-operand-1 + Cost-of-operand-2 + Cost-of-result

Example: sub 97,r5 r5 – 97 # r Cost = 1 + 0 + 0 + 0 = 1 Example: sub 97,[r5] [r5] – 97 # [r5] Cost = 1 + 1 + 0 + 1 = 3 Example: sub [r1],[[r5+48]] [[r5+48]] – [r1] # [[r5+48]] Cost = 1 + 2 + 1 + 2 = 6 example cost Absolute Memory Address x 1 Register r0 0 Literal 39 0 Indirect Register [r1] 1 Indirect plus Index [r1+48] 1 Double Indirect [[r1+48]] 2

© Harry H. Porter, 2006 Code Generation Example IR Code: x := y + z Translation #1: mov y,x 3 add z,x 4 Translation #2: mov y,r1 2 add z,r1 2 mov r1,x 2 Translation #3: Assume “y” is in r1 and “z” is in r Assume “y” will not be needed again add r2,r1 1 mov r1,x 2 Cost = 7 Cost = 6 Cost = 3

Lesson #1:

Use Registers

Lesson #2:

Keep variables in registers

CS-322 Target Generation, Part 1 © Harry H. Porter, 2006 Code Generation Example IR Code: x := y + z Translation #1: mov y,x 3 add z,x 4 Translation #2: mov y,r1 2 add z,r1 2 mov r1,x 2 Translation #3: Assume “y” is in r1 and “z” is in r Assume “y” will not be needed again add r2,r1 1 mov r1,x 2 Translation #4: Assume “y” is in r1 and “z” is in r Assume “y” will not be needed again. Assume we can keep “x” in a register. add r2,r1 1 Cost = 7 Cost = 6 Cost = 3 Cost = 1

Lesson #1:

Use Registers

Lesson #2:

Keep variables in registers

Lesson #3:

Avoid or delay storing

into memory.

© Harry H. Porter, 2006 Code Generation Example IR Code: x := y + z Translation #1: mov y,x 3 add z,x 4 Translation #2: mov y,r1 2 add z,r1 2 mov r1,x 2 Translation #3: Assume “y” is in r1 and “z” is in r Assume “y” will not be needed again add r2,r1 1 mov r1,x 2 Translation #4: Assume “y” is in r1 and “z” is in r Assume “y” will not be needed again. Assume we can keep “x” in a register. add r2,r1 1 Cost = 7 Cost = 6 Cost = 3 Cost = 1

Lesson #1:

Use Registers

Lesson #2:

Keep variables in registers

Lesson #3:

Avoid or delay storing

into memory.

Lesson #4: (not illustrated)

Use different addressing

modes effectively.

CS-322 Target Generation, Part 1 © Harry H. Porter, 2006

Basic Blocks

Break IR code into blocks such that... The block contains NO transfer-of-control instructions ... except as the last instruction

-! A sequence of consecutive statements.

  • Control enters only at the beginning.
  • Control leaves only at the end.

© Harry H. Porter, 2006 Control Flow Graph t3 := t4 + 7 t5 := t3 - 8 if t5 < 9 goto B 3 t6 := 1 (^) t6 := 0 t7 := t6 + 3 t8 := y + z x := t8 - y := t8 + x z := w + x t9 := z - (^5) •

- - B 1 B 2 B 3 B 4 B 5 CS-322 Target Generation, Part 1 © Harry H. Porter, 2006 Algorithm to Partition Instructions into Basic Blocks

Concept: “Leader”

The first instruction in a basic block

Idea:

Identify “leaders”

-! The first instruction of each routine is a leader. -! Any statement that is the target of a branch / goto is a leader.

  • Any statement that immediately follows

a branch / goto

a call instruction

... is a leader

A Basic Block consists of

A leader and all statements that follow it

... up to, but not including, the next leader

© Harry H. Porter, 2006 Identify Leaders Label_43: t3 := t4 + 7 t5 := t3 - 8 if t5 < 9 goto Label_ t6 := 1 goto Label_ Label_44: t6 := 0 Label_45: t7 := t6 + 3 t8 := y + z x := t8 - y := t8 + x Label_46: z := w + x t9 := z - 5

**-

-**

CS-322 Target Generation, Part 1 © Harry H. Porter, 2006 Identify Leaders Label_43: t3 := t4 + 7 t5 := t3 - 8 if t5 < 9 goto Label_ t6 := 1 goto Label_ Label_44: t6 := 0 Label_45: t7 := t6 + 3 t8 := y + z x := t8 - y := t8 + x Label_46: z := w + x t9 := z - 5

**-

-**

Targets of GOTOs