1 Intermediate Code Generation Part I Chapter 8 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.

Slides:



Advertisements
Similar presentations
Chapter 6 Intermediate Code Generation
Advertisements

1 Lecture 10 Intermediate Representations. 2 front end »produces an intermediate representation (IR) for the program. optimizer »transforms the code in.
Intermediate Code Generation
Intermediate Code Generation. 2 Intermediate languages Declarations Expressions Statements.
8 Intermediate code generation
Chapter 8 Intermediate Code Generation. Intermediate languages: Syntax trees, three-address code, quadruples. Types of Three – Address Statements: x :=
1 Compiler Construction Intermediate Code Generation.
1 CMPSC 160 Translation of Programming Languages Fall 2002 Lecture-Modules 17 and 18 slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon.
Intermediate Code Generation Professor Yihjia Tsai Tamkang University.
1 Intermediate representation Goals: –encode knowledge about the program –facilitate analysis –facilitate retargeting –facilitate optimization scanning.
1 Intermediate Code generation. 2 Intermediate Code Generation l Intermediate languages l Declarations l Expressions l Statements l Reference: »Chapter.
CSC 8505 Compiler Construction Intermediate Representations.
Compiler Construction A Compulsory Module for Students in Computer Science Department Faculty of IT / Al – Al Bayt University Second Semester 2008/2009.
Presented by Dr Ioanna Dionysiou
CH4.1 CSE244 Intermediate Code Generation Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit.
7/15/2015\course\cpeg621-10F\Topic-1a.ppt1 Intermediate Code Generation Reading List: Aho-Sethi-Ullman: Chapter 2.3 Chapter 6.1 ~ 6.2 Chapter 6.3 ~ 6.10.
Static checking and symbol table Chapter 6, Chapter 7.6 and Chapter 8.2 Static checking: check whether the program follows both the syntactic and semantic.
CSc 453 Intermediate Code Generation Saumya Debray The University of Arizona Tucson.
CSc 453 Intermediate Code Generation Saumya Debray The University of Arizona Tucson.
1 October 1, October 1, 2015October 1, 2015October 1, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.
1 Structure of a Compiler Front end of a compiler is efficient and can be automated Back end is generally hard to automate and finding the optimum solution.
Compiler Chapter# 5 Intermediate code generation.
Chapter 8: Intermediate Code Generation
Review: –What is an activation record? –What are the typical fields in an activation record? –What are the storage allocation strategies? Which program.
1 October 25, October 25, 2015October 25, 2015October 25, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.
Intermediate Code Generation
1 June 3, June 3, 2016June 3, 2016June 3, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa Pacific University,
Introduction to Code Generation and Intermediate Representations
Chapter 5. Syntax-Directed Translation. 2 Fig Syntax-directed definition of a simple desk calculator ProductionSemantic Rules L  E n print ( E.val.
Topic #7: Intermediate Code EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
1 Intermediate Code Generation Abstraction at the source level identifiers, operators, expressions, statements, conditionals, iteration, functions (user.
Review: Syntax directed translation. –Translation is done according to the parse tree. Each production (when used in the parsing) is a sub- structure of.
1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
1 Syntax-Directed Translation Part I Chapter 5 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.
Chap. 4, Intermediate Code Generation
1 Structure of a Compiler Source Language Target Language Semantic Analyzer Syntax Analyzer Lexical Analyzer Front End Code Optimizer Target Code Generator.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 10 Ahmed Ezzat.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Road Map Regular Exprs, Context-Free Grammars Regular Exprs, Context-Free Grammars LR parsing algorithm LR parsing algorithm Building LR parse tables Building.
1 Syntax-Directed Translation Part II Chapter 5 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
CS 404 Introduction to Compiler Design
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Compiler Construction
Compiler Construction
Syntax-Directed Translation Part I
Subject Name:COMPILER DESIGN Subject Code:10CS63
Compiler Optimization and Code Generation
Syntax-Directed Translation Part II
Intermediate Code Generation Part I
Intermediate Code Generation
Syntax-Directed Translation Part I
Syntax-Directed Translation Part I
Intermediate Code Generation
Chapter 6 Intermediate-Code Generation
Intermediate Code Generation Part I
Syntax-Directed Translation Part II
Intermediate code generation
Three-address code A more common representation is THREE-ADDRESS CODE . Three address code is close to assembly language, making machine code generation.
Syntax-Directed Translation Part II
Compiler Design 21. Intermediate Code Generation
Intermediate Code Generation Part I
Syntax-Directed Translation Part I
Intermediate Code Generation
Syntax-Directed Translation Part II
Intermediate Code Generation Part I
Compiler Construction
Syntax-Directed Translation Part I
Compiler Design 21. Intermediate Code Generation
Review: What is an activation record?
Presentation transcript:

1 Intermediate Code Generation Part I Chapter 8 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007

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

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

4 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)

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

6 Abstract Syntax Trees versus DAGs := a+ * uminusb c * b c := a+ * uminusb c TreeDAG a := b * -c + b * -c

7 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

8 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

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, n return y

10 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

11 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)

12 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:

13 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:

14 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

15 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

16 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

17 Names and Scopes The three-address code generated by the syntax- directed definitions shown on the previous slides is somewhat simplistic, because it assumes that the names of variables can be easily resolved by the back end in global or local variables We need local symbol tables to record global declarations as well as local declarations in procedures, blocks, and structs to resolve names

18 Symbol Tables for Scoping struct S { int a; int b; } s; void swap(int& a, int& b) { int t; t = a; a = b; b = t; } void somefunc() { … swap(s.a, s.b); … } We need a symbol table for the fields of struct S Need symbol table for arguments and locals for each function Need symbol table for global variables and functions Check: s is global and has fields a and b Using symbol tables we can generate code to access s and its fields

19 Offset and Width for Runtime Allocation struct S { int a; int b; } s; void swap(int& a, int& b) { int t; t = a; a = b; b = t; } void somefunc() { … swap(s.a, s.b); … } Subroutine frame holds arguments a and b and local t at offsets 0, 4, and 8 a b (0) (4) a b t (0) (4) (8) Subroutine frame fp[0]= fp[4]= fp[8]= The fields a and b of struct S are located at offsets 0 and 4 from the start of S The width of S is 8 The width of the frame is 12

20 Example struct S { int a; int b; } s; void swap(int& a, int& b) { int t; t = a; a = b; b = t; } void foo() { … swap(s.a, s.b); … } a Trec S b s Tint Tfun swap a b t Tref prev=nil prev prev=nil prev Tfun foo swap foo globals (0) (4) (8) (0) (4) Table nodes type nodes (offset) [width] [12] [0] [8]

21 Hierarchical Symbol Table Operations mktable(previous) returns a pointer to a new table that is linked to a previous table in the outer scope enter(table, name, type, offset) creates a new entry in table addwidth(table, width) accumulates the total width of all entries in table enterproc(table, name, newtable) creates a new entry in table for procedure with local scope newtable lookup(table, name) returns a pointer to the entry in the table for name by following linked tables

22 Syntax-Directed Translation of Declarations in Scope Synthesized attributes: T.typepointer to type T.widthstorage width of type (bytes) E.placename of temp holding value of E Productions P  D ; S D  D ; D | id : T | proc id ; D ; S T  integer | real | array [ num ] of T | ^ T | record D end S  S ; S | id := E | call id ( A ) Global data to implement scoping: tblptrstack of pointers to tables offsetstack of offset values Productions (cont’d) E  E + E | E * E | - E | ( E ) | id | E ^ | & E | E. id A  A, E | E

23 Syntax-Directed Translation of Declarations in Scope (cont’d) P  { t := mktable(nil); push(t, tblptr); push(0, offset) } D ; S D  id : T { enter(top(tblptr), id.name, T.type, top(offset)); top(offset) := top(offset) + T.width } D  proc id ; { t := mktable(top(tblptr)); push(t, tblptr); push(0, offset) } D 1 ; S { t := top(tblptr); addwidth(t, top(offset)); pop(tblptr); pop(offset); enterproc(top(tblptr), id.name, t) } D  D 1 ; D 2

24 Syntax-Directed Translation of Declarations in Scope (cont’d) T  integer{ T.type := ‘integer’; T.width := 4 } T  real{ T.type := ‘real’; T.width := 8 } T  array [ num ] of T 1 { T.type := array(num.val, T 1.type); T.width := num.val * T 1.width } T  ^ T 1 { T.type := pointer(T 1.type); T.width := 4 } T  record { t := mktable(nil); push(t, tblptr); push(0, offset) } D end { T.type := record(top(tblptr)); T.width := top(offset); addwidth(top(tblptr), top(offset)); pop(tblptr); pop(offset) }

25 Example s: record a: integer; b: integer; end; proc swap; a: ^integer; b: ^integer; t: integer; t := a^; a^ := b^; b^ := t; proc foo; call swap(&s.a, &s.b); a Trec b s Tint Tfun swap a b t Tptr prev=nil prev prev=nil prev Tfun foo swap foo globals (0) (4) (8) (0) (4) Table nodes type nodes (offset) [width] [12] [0] [8]

26 Syntax-Directed Translation of Statements in Scope S  S ; S S  id := E { p := lookup(top(tblptr), id.name); if p = nil then error() else if p.level = 0 then // global variable emit(id.place ‘:=’ E.place) else // local variable in subroutine frame emit(fp[p.offset] ‘:=’ E.place) } s x y (0) (8) (12) Globals a b t (0) (4) (8) Subroutine frame fp[0]= fp[4]= fp[8]= …

27 Syntax-Directed Translation of Expressions in Scope E  E 1 + E 2 { E.place := newtemp(); emit(E.place ‘:=’ E 1.place ‘+’ E 2.place) } E  E 1 * E 2 { E.place := newtemp(); emit(E.place ‘:=’ E 1.place ‘*’ E 2.place) } E  - E 1 { E.place := newtemp(); emit(E.place ‘:=’ ‘uminus’ E 1.place) } E  ( E 1 ){ E.place := E 1.place } E  id { p := lookup(top(tblptr), id.name); if p = nil then error() else if p.level = 0 then // global variable E.place := id.place else // local variable in frame E.place := fp[p.offset] }

28 Syntax-Directed Translation of Expressions in Scope (cont’d) E  E 1 ^{ E.place := newtemp(); emit(E.place ‘:=’ ‘*’ E 1.place) } E  & E 1 { E.place := newtemp(); emit(E.place ‘:=’ ‘&’ E 1.place) } E  id 1. id 2 { p := lookup(top(tblptr), id 1.name); if p = nil or p.type != Trec then error() else q := lookup(p.type.table, id 2.name); if q = nil then error() else if p.level = 0 then // global variable E.place := id 1.place[q.offset] else // local variable in frame E.place := fp[p.offset+q.offset] }