lecture # 23 chapter 8: intermediate code generation
TRANSCRIPT
![Page 1: Lecture # 23 Chapter 8: Intermediate Code Generation](https://reader035.vdocuments.net/reader035/viewer/2022072015/56649ed05503460f94bdf175/html5/thumbnails/1.jpg)
Lecture # 23
Chapter 8: Intermediate Code Generation
![Page 2: Lecture # 23 Chapter 8: Intermediate Code Generation](https://reader035.vdocuments.net/reader035/viewer/2022072015/56649ed05503460f94bdf175/html5/thumbnails/2.jpg)
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
Intermediatecode
Targetmachine
code
![Page 3: Lecture # 23 Chapter 8: Intermediate Code Generation](https://reader035.vdocuments.net/reader035/viewer/2022072015/56649ed05503460f94bdf175/html5/thumbnails/3.jpg)
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
![Page 4: Lecture # 23 Chapter 8: Intermediate Code Generation](https://reader035.vdocuments.net/reader035/viewer/2022072015/56649ed05503460f94bdf175/html5/thumbnails/4.jpg)
4
Syntax-Directed Translation of Abstract Syntax Trees
ProductionS id := EE E1 + E2
E E1 * E2
E - E1
E ( 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.nptrE.nptr := mkleaf(id, id.entry)
![Page 5: Lecture # 23 Chapter 8: Intermediate Code Generation](https://reader035.vdocuments.net/reader035/viewer/2022072015/56649ed05503460f94bdf175/html5/thumbnails/5.jpg)
5
Abstract Syntax TreesE.nptr
*E.nptr
E.nptra
b
+E.nptr
*
a +
b c
E.nptr
c
E.nptr
( )
a * (b + c)
Pro: easy restructuring of codeand/or expressions forintermediate code optimization
Cons: memory intensive
![Page 6: Lecture # 23 Chapter 8: Intermediate Code Generation](https://reader035.vdocuments.net/reader035/viewer/2022072015/56649ed05503460f94bdf175/html5/thumbnails/6.jpg)
6
Abstract Syntax Trees versus DAGs
:=
a +
*
uminusb
c
*
uminusb
c
:=
a +
*
uminusb
c
Tree DAG
a := b * -c + b * -c
![Page 7: Lecture # 23 Chapter 8: Intermediate Code Generation](https://reader035.vdocuments.net/reader035/viewer/2022072015/56649ed05503460f94bdf175/html5/thumbnails/7.jpg)
7
Postfix Notation
a := b * -c + b * -c
a b c uminus * b c uminus * + assign
iload 2 // push biload 3 // push cineg // uminusimul // *iload 2 // push biload 3 // push cineg // uminusimul // *iadd // +istore 1 // store a
Bytecode (for example)
Postfix notation representsoperations on a stack
Pro: easy to generateCons: stack operations are more
difficult to optimize
![Page 8: Lecture # 23 Chapter 8: Intermediate Code Generation](https://reader035.vdocuments.net/reader035/viewer/2022072015/56649ed05503460f94bdf175/html5/thumbnails/8.jpg)
8
Three-Address Code
a := b * -c + b * -c
t1 := - ct2 := b * t1t3 := - ct4 := b * t3t5 := t2 + t4a := t5
Linearized representationof a syntax tree
t1 := - ct2 := b * t1t5 := t2 + t2a := t5
Linearized representationof a syntax DAG
![Page 9: Lecture # 23 Chapter 8: Intermediate Code Generation](https://reader035.vdocuments.net/reader035/viewer/2022072015/56649ed05503460f94bdf175/html5/thumbnails/9.jpg)
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
![Page 10: Lecture # 23 Chapter 8: Intermediate Code Generation](https://reader035.vdocuments.net/reader035/viewer/2022072015/56649ed05503460f94bdf175/html5/thumbnails/10.jpg)
10
Syntax-Directed Translation into Three-Address Code
Synthesized attributes:S.code three-address code for SS.begin label to start of S or nilS.after label to end of S or nilE.code three-address code for EE.place a name holding the value of E
ProductionsS id := E | while E do SE E + E | E * E | - E | ( E ) | id | num
gen(E.place ‘:=’ E1.place ‘+’ E2.place)
t3 := t1 + t2Code generation
![Page 11: Lecture # 23 Chapter 8: Intermediate Code Generation](https://reader035.vdocuments.net/reader035/viewer/2022072015/56649ed05503460f94bdf175/html5/thumbnails/11.jpg)
11
Syntax-Directed Translation into Three-Address Code (cont’d)
ProductionsS id := ES while E do S1
E E1 + E2
E E1 * E2
E - E1
E ( E1 )
E id
E num
Semantic rulesS.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.codeE.place := id.nameE.code := ‘’E.place := newtemp();E.code := gen(E.place ‘:=’ num.value)
![Page 12: Lecture # 23 Chapter 8: Intermediate Code Generation](https://reader035.vdocuments.net/reader035/viewer/2022072015/56649ed05503460f94bdf175/html5/thumbnails/12.jpg)
12
Syntax-Directed Translation into Three-Address Code (cont’d)
ProductionS while E do S1
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 ‘:’)
…
if E.place = 0 goto S.after
S.code
E.code
goto S.begin
S.begin:
S.after:
![Page 13: Lecture # 23 Chapter 8: Intermediate Code Generation](https://reader035.vdocuments.net/reader035/viewer/2022072015/56649ed05503460f94bdf175/html5/thumbnails/13.jpg)
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: