

































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


































© Harry H. Porter, 2006
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
Virtual Machine (Interpreter) Final Code Generation Optimization Type Checking Parser Lexer Front-End Back-End Intermediate Code Generation Symbol Table Information
© Harry H. Porter, 2006
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
© Harry H. Porter, 2006 Requirements
© 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 #
© Harry H. Porter, 2006 Code Generation Algorithm #
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
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!!!
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
A “ Cost Model ”
© Harry H. Porter, 2006 Evaluating A Potential Code Sequence
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
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
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
© 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
CS-322 Target Generation, Part 1 © Harry H. Porter, 2006
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.
© 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
-! The first instruction of each routine is a leader. -! Any statement that is the target of a branch / goto is a 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