{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Ch8a - 1 Intermediate Code Generation Part I Chapter...

Info icon This preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Intermediate Code Generation Part I Chapter 6 (formerly 1 st ed Chapter 8) COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007-2011
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
2 Intermediate Code Generation • Facilitates retargeting : enables attaching a back end for the new machine to an existing front end Enables machine-independent code optimization Front end Back end Intermediate code Target machine code
Image of page 2
3 Intermediate Representations Graphical representations (e.g. AST) Postfix notation : operations on values stored on operand stack (similar to JVM bytecode) Three-address code : (e.g. triples and quads ) x := y op z Two-address code : x := op y which is the same as x := x op y
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
4 Syntax-Directed Translation of Abstract Syntax Trees Production S ! id := E E ! E 1 + E 2 E ! E 1 * E 2 E ! - E 1 E ! ( E 1 ) E ! id Semantic Rule S .nptr := mknode (‘:=’, mkleaf ( id , id .entry), E .nptr) E .nptr := mknode (‘+’, E 1 .nptr, E 2 .nptr) E .nptr := mknode (‘*’, E 1 .nptr, E 2 .nptr) E .nptr := mknode (‘uminus’, E 1 .nptr) E .nptr := E 1 .nptr E .nptr := mkleaf ( id , id .entry)
Image of page 4
5 Abstract Syntax Trees E .nptr * E .nptr E .nptr a b + E .nptr * a + b c E .nptr c E .nptr ( ) a * (b + c) Pro: easy restructuring of code and/or expressions for intermediate code optimization Cons: memory intensive
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
6 Abstract Syntax Trees versus DAGs := a + * uminus b c * uminus b c := a + * uminus b c Tree DAG a := b * -c + b * -c
Image of page 6
7 Postfix Notation a := b * -c + b * -c a b c uminus * b c uminus * + assign iload 2 // push b iload 3 // push c ineg // uminus imul // * iload 2 // push b iload 3 // push c ineg // uminus imul // * iadd // + istore 1 // store a Bytecode (for example) Postfix notation represents operations on a stack Pro: easy to generate Cons: stack operations are more difficult to optimize
Image of page 7

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
8 Three-Address Code a := b * -c + b * -c t1 := - c t2 := b * t1 t3 := - c t4 := b * t3 t5 := t2 + t4 a := t5 Linearized representation of a syntax tree t1 := - c t2 := b * t1 t5 := t2 + t2 a := t5 Linearized representation of a syntax DAG
Image of page 8
9 Three-Address Statements Assignment statements: x := y op z , x := op y Indexed assignments: x := y [ i ], x [ i ] := y Pointer assignments: x := & y , x := * y , * x := y Copy statements: x := y Unconditional jumps: goto lab Conditional jumps: if x relop y goto lab Function calls: param x… call p, n return y
Image of page 9

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
10 Syntax-Directed Translation into Three-Address Code Synthesized attributes: S .code three-address code for S S .begin
Image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}