190 likes | 329 Views
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
E N D
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) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler Construction Intermediate Code Generation • Facilitates retargeting: enables attaching a back end for the new machine to an existing front end • Enables machine-independent code optimization Targetmachinecode Front end Back end Intermediatecode 2 September 16, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler Construction Keep 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 3 September 16, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler Construction 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 September 16, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler Construction 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 September 16, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler Construction Abstract Syntax Trees Pro: easy restructuring of code and/or expressions for intermediate code optimization Cons: memory intensive 6 September 16, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler Construction Abstract Syntax Trees versus DAGs uminus: unary minus DAG: directed acyclic graph a := b * -c + b * -c 7 September 16, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler Construction 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 September 16, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler Construction 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 September 16, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler Construction 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 September 16, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler Construction 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 September 16, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler Construction 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 September 16, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler Construction Syntax-Directed Translation into Three-Address Code (cont’d) ProductionS whileEdoS1 S.begin: E.code if E.place=0goto S.after 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 ‘:’) S.code goto S.begin S.after: … 13 September 16, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler Construction 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: 14 September 16, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler Construction Implementation of Three-Address Statements: Quads Quads (quadruples) Pro: easy to rearrange code for global optimizationCons: lots of temporaries 15 September 16, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler Construction Implementation of Three-Address Statements: Triples Triples Pro: temporaries are implicitCons: difficult to rearrange code 16 September 16, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler Construction Implementation of Three-Address Stmts: Indirect Triples Program Triple container Pro: temporaries are implicit & easier to rearrange code 17 September 16, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler Construction Got 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 18 September 16, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler Construction Thank you very much! Questions? 19 September 16, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/