september 16, 2014
DESCRIPTION
Computer Science at Azusa Pacific University. CS400 Compiler Construction. Sheldon X. Liang Ph. D. September 16, 2014. Azusa, CA. 1. September 16, 2014. Azusa Pacific University, Azusa, CA 91702, Tel: (800) 8 25-5278 - PowerPoint PPT PresentationTRANSCRIPT
1
April 21, 20231
April 21, 2023April 21, 2023 Azusa, CAAzusa, CA
Sheldon X. Liang Ph. D.
Computer Science at Computer Science at Azusa Pacific UniversityAzusa Pacific University
Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/
CS400 Compiler ConstructionCS400 Compiler Construction
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 endIntermediate
code
Targetmachine
code
April 21, 20232
Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction
3
April 21, 20233
Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction
Keep in mind following questionsKeep in mind following questions
• Intermediate Representations – Why? – Machine-independent Opt– Machine-independent Optimization– Machine-independent Checking
• High level IRs– Graphical Representations:– AST: abstract syntax tree– DAG: directed acyclic diagram
• Low-level IRs– Postfix notation (byte-code)– Three address code– Two address code
4
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
April 21, 20234
Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction
5
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)
April 21, 20235
Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction
6
Abstract Syntax Trees
Pro: easy restructuring of codeand/or expressions forintermediate code optimization
Cons: memory intensive
April 21, 20236
Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction
7
Abstract Syntax Trees versus DAGs
a := b * -c + b * -c
April 21, 20237
Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction
DAG: directed acyclic graph uminus: unary minus
8
Postfix Notationa := b * -c + b * -c
a b c uminus * b c uminus * + assigniload 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
April 21, 20238
Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction
9
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
April 21, 20239
Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction
10
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
April 21, 202310
Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction
11
Syntax-Directed Translation into Three-Address CodeSynthesized 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
April 21, 202311
Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction
12
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) April 21, 2023
12Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction
13
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:
April 21, 202313
Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction
14
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:
April 21, 202314
Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction
15
Implementation of Three-Address Statements: Quads
# Op Arg1 Arg2 Res
(0) uminus c t1
(1) * b t1 t2
(2) uminus c t3
(3) * b t3 t4
(4) + t2 t4 t5
(5) := t5 a
Quads (quadruples)
Pro: easy to rearrange code for global optimizationCons: lots of temporaries
April 21, 202315
Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction
16
Implementation of Three-Address Statements: Triples
# Op Arg1 Arg2
(0) uminus c
(1) * b (0)
(2) uminus c
(3) * b (2)
(4) + (1) (3)
(5) := a (4)
Triples
Pro: temporaries are implicitCons: difficult to rearrange code
April 21, 202316
Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction
17
Implementation of Three-Address Stmts: Indirect Triples
# Op Arg1 Arg2
(14) uminus c
(15) * b (14)
(16) uminus c
(17) * b (16)
(18) + (15) (17)
(19) := a (18)
Triple container
Pro: temporaries are implicit & easier to rearrange code
# Stmt
(0) (14)
(1) (15)
(2) (16)
(3) (17)
(4) (18)
(5) (19)
Program
April 21, 202317
Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction
18
April 21, 202318
Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction
Got it with following questionsGot it with following questions
• Intermediate Representations – Why? – Machine-independent Opt– Machine-independent Optimization– Machine-independent Checking
• High level IRs– Graphical Representations:– AST: abstract syntax tree– DAG: directed acyclic diagram
• Low-level IRs– Postfix notation (byte-code)– Three address code– Two address code
19
Thank you very much!
Questions?
April 21, 202319
Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction