Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 June 3, 2016 1 June 3, 2016June 3, 2016June 3, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa Pacific University,

Similar presentations


Presentation on theme: "1 June 3, 2016 1 June 3, 2016June 3, 2016June 3, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa Pacific University,"— Presentation transcript:

1 1 June 3, 2016 1 June 3, 2016June 3, 2016June 3, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS400 Compiler Construction

2 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 endBack end Intermediate code Target machine code June 3, 2016 2 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction

3 3 June 3, 2016 3 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction Keep in mind following questions Intermediate Representations –W–Why? – Machine-independent Opt –M–Machine-independent Optimization –M–Machine-independent Checking High level IRs –G–Graphical Representations: –A–AST: abstract syntax tree –D–DAG: directed acyclic diagram Low-level IRs –P–Postfix notation (byte-code) –T–Three address code –T–Two address code

4 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 y which is the same as x := x op y June 3, 2016 4 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction

5 5 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) June 3, 2016 5 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction

6 6 Abstract Syntax Trees Pro:easy restructuring of code and/or expressions for intermediate code optimization Cons:memory intensive June 3, 2016 6 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction

7 7 Abstract Syntax Trees versus DAGs a := b * -c + b * -c June 3, 2016 7 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction DAG: directed acyclic graph uminus: unary minus

8 8 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 June 3, 2016 8 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction

9 9 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 June 3, 2016 9 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction

10 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, n return y June 3, 2016 10 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction

11 11 Syntax-Directed Translation into Three- Address Code Synthesized attributes: S.codethree-address code for S S.beginlabel to start of S or nil S.afterlabel to end of S or nil E.codethree-address code for E E.placea name holding the value of E Productions S  id := E | while E do S E  E + E | E * E | - E | ( E ) | id | num gen(E.place ‘:=’ E 1.place ‘+’ E 2.place) t3 := t1 + t2 Code generation June 3, 2016 11 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction

12 12 Syntax-Directed Translation into Three- Address Code (cont’d) Productions S  id := E S  while E do S 1 E  E 1 + E 2 E  E 1 * E 2 E  - E 1 E  ( E 1 ) 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 := E 1.code || E 2.code || gen(E.place ‘:=’ E 1.place ‘+’ E 2.place) E.place := newtemp(); E.code := E 1.code || E 2.code || gen(E.place ‘:=’ E 1.place ‘*’ E 2.place) E.place := newtemp(); E.code := E 1.code || gen(E.place ‘:=’ ‘uminus’ E 1.place) E.place := E 1.place E.code := E 1.code E.place := id.name E.code := ‘’ E.place := newtemp(); E.code := gen(E.place ‘:=’ num.value) June 3, 2016 12 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction

13 13 Syntax-Directed Translation into Three- Address Code (cont’d) Production S  while E do S 1 Semantic rule S.begin := newlabel() S.after := newlabel() S.code := gen(S.begin ‘:’) || E.code || gen(‘if’ E.place ‘=‘ ‘0’ ‘goto’ S.after) || S 1.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: June 3, 2016 13 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction

14 14 Example i := 2 * n + k while i do i := i - k t1 := 2 t2 := t1 * n t3 := t2 + k i := t3 L1: if i = 0 goto L2 t4 := i - k i := t4 goto L1 L2: June 3, 2016 14 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction

15 15 Implementation of Three-Address Statements: Quads #OpArg1Arg2Res (0)uminusct1 (1)*bt1t2 (2)uminusct3 (3)*bt3t4 (4)+t2t4t5 (5):=t5a Quads (quadruples) Pro:easy to rearrange code for global optimization Cons:lots of temporaries June 3, 2016 15 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction

16 16 Implementation of Three-Address Statements: Triples #OpArg1Arg2 (0)uminusc (1)*b(0) (2)uminusc (3)*b(2) (4)+(1)(3) (5):=a(4) Triples Pro:temporaries are implicit Cons:difficult to rearrange code June 3, 2016 16 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction

17 17 Implementation of Three-Address Stmts: Indirect Triples #OpArg1Arg2 (14)uminusc (15)*b(14) (16)uminusc (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 June 3, 2016 17 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction

18 18 June 3, 2016 18 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction Got it with following questions Intermediate Representations –W–Why? – Machine-independent Opt –M–Machine-independent Optimization –M–Machine-independent Checking High level IRs –G–Graphical Representations: –A–AST: abstract syntax tree –D–DAG: directed acyclic diagram Low-level IRs –P–Postfix notation (byte-code) –T–Three address code –T–Two address code

19 19 Thank you very much! Questions? June 3, 2016 19 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction


Download ppt "1 June 3, 2016 1 June 3, 2016June 3, 2016June 3, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa Pacific University,"

Similar presentations


Ads by Google