Behavioral Design Patterns October 19, 2015October 19, 2015October 19, 2015.
1 October 25, 2015 1 October 25, 2015October 25, 2015October 25, 2015 Azusa, CA Sheldon X. Liang Ph....
-
Upload
myra-norton -
Category
Documents
-
view
218 -
download
0
Transcript of 1 October 25, 2015 1 October 25, 2015October 25, 2015October 25, 2015 Azusa, CA Sheldon X. Liang Ph....
1
April 20, 20231
April 20, 2023April 20, 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 20, 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 20, 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
• Names and Scope – Symbol tables for scope– Runtime allocation– Hierarchical symbol tables
• SDD/SDT and grammar– Attributed grammar– Denote semantics– Direct code generation
• SDT and Scope– Declaration in scope– Statements in scope– Expressions in scope
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
April 20, 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
Symbol Tables for Scoping
April 20, 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
Symbol Tables for Scoping
April 20, 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
• 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
Symbol Tables for Scoping
April 20, 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
• 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
Symbol Tables for Scoping
April 20, 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
• 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
Symbol Tables for Scoping
April 20, 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
• 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
Symbol Table Interface
April 20, 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
• 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
Offset and Width for Runtime Allocation
April 20, 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
Example
April 20, 202312
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
13
Hierarchical Symbol Tables
April 20, 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
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
April 20, 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
Syntax-Directed Translation of Declarations in Scope
Synthesized attributes:T.type pointer to typeT.width storage width of type (bytes)E.place name of temp holding value of E
ProductionsP D ; SD D ; D | id : T | proc id ; D ; ST integer | real | array [ num ] of T | ^ T | record D endS S ; S | id := E | call id ( A )
Global data to implement scoping:tblptr stack of pointers to tablesoffset stack of offset values
Productions (cont’d)E E + E | E * E | - E | ( E ) | id | E ^ | & E | E . idA A , E | E
April 20, 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
Syntax-Directed Translation of Declarations in Scope (cont’d)
P { t := mktable(nil); push(t, tblptr); push(0, offset) } D ; SD 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) }
D1 ; S{ t := top(tblptr); addwidth(t, top(offset)); pop(tblptr); pop(offset); enterproc(top(tblptr), id.name, t) }
D D1 ; D2
April 20, 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
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 T1
{ T.type := array(num.val, T1.type); T.width := num.val * T1.width }
T ^ T1
{ T.type := pointer(T1.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) }
April 20, 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
Example
April 20, 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
19
Syntax-Directed Translation of Statements in Scope
S S ; SS 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) }
April 20, 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
20
Syntax-Directed Translation of Expressions in Scope
E E1 + E2 { E.place := newtemp(); emit(E.place ‘:=’ E1.place ‘+’ E2.place) }
E E1 * E2 { E.place := newtemp(); emit(E.place ‘:=’ E1.place ‘*’ E2.place) }
E - E1 { E.place := newtemp(); emit(E.place ‘:=’ ‘uminus’ E1.place) }
E ( E1 ) { E.place := E1.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] }
April 20, 202320
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
21
Syntax-Directed Translation of Expressions in Scope (cont’d)
E E1 ^ { E.place := newtemp(); emit(E.place ‘:=’ ‘*’ E1.place) }
E & E1{ E.place := newtemp(); emit(E.place ‘:=’ ‘&’ E1.place) }
E id1 . id2 { p := lookup(top(tblptr), id1.name); if p = nil or p.type != Trec then error() else q := lookup(p.type.table, id2.name); if q = nil then error() else if p.level = 0 then // global variable
E.place := id1.place[q.offset] else // local variable in frame
E.place := fp[p.offset+q.offset] } April 20, 2023
21Azusa 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
22
April 20, 202322
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
• Names and Scope – Symbol tables for scope– Runtime allocation– Hierarchical symbol tables
• SDD/SDT and grammar– Attributed grammar– Denote semantics– Direct code generation
• SDT and Scope– Declaration in scope– Statements in scope– Expressions in scope
23
Thank you very much!
Questions?
April 20, 202323
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