Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 October 25, 2015 1 October 25, 2015October 25, 2015October 25, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.

Similar presentations


Presentation on theme: "1 October 25, 2015 1 October 25, 2015October 25, 2015October 25, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa."— Presentation transcript:

1 1 October 25, 2015 1 October 25, 2015October 25, 2015October 25, 2015 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 October 25, 2015 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 October 25, 2015 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 Names and Scope –S–Symbol tables for scope –R–Runtime allocation –H–Hierarchical symbol tables SDD/SDT and grammar –A–Attributed grammar –D–Denote semantics –D–Direct code generation SDT and Scope –D–Declaration in scope –S–Statements in scope –E–Expressions in scope

4 4 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 October 25, 2015 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 Symbol Tables for Scoping October 25, 2015 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 Symbol Tables for Scoping October 25, 2015 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 The symbol tables - repository of all information within a compiler. All parts of a compiler communicate thru these table and access the data-symbols. Symbol tables are also used to hold labels, constants, and types. Symbol tables map names ( constants, identifiers, label numbers) into sets of symbols. Different names have different attributes. - for identifiers, it could be its type, its location in a stack frame, its storage class etc.

7 7 Symbol Tables for Scoping October 25, 2015 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 Symbols are collected into symbol tables. The symbol-table module manages symbols and tables. Symbol Management should also deal with scope and visibility that is imposed by the language. Symbol Tables are usually lists of hash tables, one for each scope.

8 8 Symbol Tables for Scoping October 25, 2015 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 A typical table typedef struct table *Table; struct table { int level ; /* scope value */ table previous; struct entry { struct symbol sym; struct entry *link; } *buckets[256]; symbol all; } ;

9 9 Symbol Tables for Scoping October 25, 2015 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 The buckets field is an array of pointers to the hash chains. Previous field points to the table of the enclosing scope. In each table structure all heads a list of symbols in this and enclosing scopes. Symbol Table entries are (can be ) non-uniform (variable sized). Depending upon what the identifier represents. (We will see how a symbol is defined soon). Symbol Table look-up is performed in the context of a current scope, global, local, structured type.

10 10 Symbol Table Interface October 25, 2015 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 Create_scope(parent_scope) - return index for a new scope contained in a parent scope. Insert_scope(scope,name) - insert identifier into the specified scope of a symbol table. Lookup(scope,name) - look up identifier in specified scope. Delete_scope(scope) - delete specified scope and all symbol table entries that it contains

11 11 Offset and Width for Runtime Allocation October 25, 2015 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 Example October 25, 2015 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 Hierarchical Symbol Tables October 25, 2015 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 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 October 25, 2015 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 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 October 25, 2015 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 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 October 25, 2015 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 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) } October 25, 2015 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 Example October 25, 2015 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

19 19 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) } October 25, 2015 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

20 20 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] } October 25, 2015 20 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

21 21 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] } October 25, 2015 21 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

22 22 October 25, 2015 22 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 Names and Scope –S–Symbol tables for scope –R–Runtime allocation –H–Hierarchical symbol tables SDD/SDT and grammar –A–Attributed grammar –D–Denote semantics –D–Direct code generation SDT and Scope –D–Declaration in scope –S–Statements in scope –E–Expressions in scope

23 23 Thank you very much! Questions? October 25, 2015 23 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 October 25, 2015 1 October 25, 2015October 25, 2015October 25, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa."

Similar presentations


Ads by Google