1 / 13

Lecture # 23

Lecture # 23. Chapter 8: Intermediate Code Generation. Intermediate Code Generation. Facilitates retargeting : enables attaching a back end for the new machine to an existing front end Enables machine-independent code optimization. Intermediate code. Target machine code. Front end.

Download Presentation

Lecture # 23

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture # 23 Chapter 8: Intermediate Code Generation

  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 Intermediatecode Targetmachinecode Front end Back end

  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 ywhich is the same as x := x op y

  4. Syntax-Directed Translation of Abstract Syntax Trees ProductionS  id :=EE E1+E2E E1*E2E -E1E( E1 ) E id Semantic RuleS.nptr := mknode(‘:=’, mkleaf(id, id.entry), E.nptr)E.nptr := mknode(‘+’, E1.nptr, E2.nptr)E.nptr := mknode(‘*’, E1.nptr, E2.nptr)E.nptr := mknode(‘uminus’, E1.nptr)E.nptr := E1.nptr E.nptr := mkleaf(id, id.entry)

  5. Abstract Syntax Trees E.nptr E.nptr * a * (b + c) E.nptr E.nptr a ( ) E.nptr + E.nptr b c * Pro: easy restructuring of code and/or expressions for intermediate code optimization Cons: memory intensive a + b c

  6. Abstract Syntax Trees versus DAGs a := b * -c + b * -c := := a + a + * * * b uminus b uminus b uminus c c c Tree DAG

  7. Postfix Notation a := b * -c + b * -c a b c uminus * b c uminus * + assign Bytecode (for example) iload 2 // push biload 3 // push cineg // uminusimul // *iload 2 // push biload 3 // push cineg // uminusimul // *iadd // +istore 1 // store a Postfix notation represents operations on a stack Pro: easy to generate Cons: stack operations are more difficult to optimize

  8. Three-Address Code a := b * -c + b * -c t1 := - ct2 := b * t1t3 := - ct4 := b * t3t5 := t2 + t4a := t5 t1 := - ct2 := b * t1t5 := t2 + t2a := t5 Linearized representationof a syntax tree Linearized representationof a syntax DAG

  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, nreturn y

  10. Syntax-Directed Translation into Three-Address Code ProductionsS id := E | whileEdoSE E+E | E*E | -E | (E ) | id | num Synthesized attributes:S.code three-address code for S S.begin label to start of S or nilS.after label to end of S or nilE.code three-address code for E E.place a name holding the value of E gen(E.place ‘:=’ E1.place ‘+’ E2.place) Code generation t3 := t1 + t2

  11. Syntax-Directed Translation into Three-Address Code (cont’d) ProductionsS id := E S whileEdoS1E E1+E2E E1*E2E -E1E (E1 )E id E  num Semantic rules S.code := E.code || gen(id.place ‘:=’ E.place); S.begin := S.after := nil(see next slide) E.place := newtemp();E.code := E1.code || E2.code || gen(E.place ‘:=’ E1.place ‘+’ E2.place)E.place := newtemp();E.code := E1.code || E2.code || gen(E.place ‘:=’ E1.place ‘*’ E2.place) E.place := newtemp();E.code := E1.code || gen(E.place ‘:=’ ‘uminus’ E1.place) E.place := E1.placeE.code := E1.code E.place := id.nameE.code := ‘’E.place := newtemp();E.code := gen(E.place ‘:=’ num.value)

  12. Syntax-Directed Translation into Three-Address Code (cont’d) ProductionS whileEdoS1 S.begin: E.code if E.place=0goto S.after S.code Semantic ruleS.begin := newlabel()S.after := newlabel() S.code := gen(S.begin ‘:’) ||E.code ||gen(‘if’ E.place ‘=‘ ‘0’ ‘goto’ S.after) ||S1.code ||gen(‘goto’ S.begin) ||gen(S.after ‘:’) goto S.begin S.after: …

  13. Example i := 2 * n + kwhile i do i := i - k t1 := 2 t2 := t1 * n t3 := t2 + k i := t3L1: if i = 0 goto L2 t4 := i - k i := t4 goto L1L2:

More Related