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.

Slides:



Advertisements
Similar presentations
SYMBOL TABLES &CODE GENERATION FOR EXECUTABLES. SYMBOL TABLES Compilers that produce an executable (or the representation of an executable in object module.
Advertisements

Symbol Table.
Chapter 6 Intermediate Code Generation
Code Generation.
Intermediate Code Generation
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
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.
Compiler Construction
Lesson 12 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
1 CMPSC 160 Translation of Programming Languages Fall 2002 Lecture-Modules 17 and 18 slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon.
1 Chapter 7: Runtime Environments. int * larger (int a, int b) { if (a > b) return &a; //wrong else return &b; //wrong } int * larger (int *a, int *b)
Honors Compilers Semantic Analysis and Attribute Grammars Mar 5th 2002.
1 Intermediate Code generation. 2 Intermediate Code Generation l Intermediate languages l Declarations l Expressions l Statements l Reference: »Chapter.
Semantic analysis Enforce context-dependent language rules that are not reflected in the BNF, e.g.a function must have a return statement. Decorate AST.
CH4.1 CSE244 Intermediate Code Generation Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit.
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.
September 7, September 7, 2015September 7, 2015September 7, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
Semantic Analysis Legality checks –Check that program obey all rules of the language that are not described by a context-free grammar Disambiguation –Name.
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 October 2, October 2, 2015October 2, 2015October 2, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.
COMPILERS Semantic Analysis hussein suleman uct csc3005h 2006.
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
Attribute Grammar Examples and Symbol Tables Compiler Design Lecture (02/23/98) Computer Science Rensselaer Polytechnic.
Names and Scope. Scope Suppose that a name is used many times for different entities in text of the program, or in the course of execution. When the name.
1 October 14, October 14, 2015October 14, 2015October 14, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.
Lesson 11 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
1 October 16, October 16, 2015October 16, 2015October 16, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.
1 Intermediate Code Generation Part I Chapter 8 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.
Chapter 8: Intermediate Code Generation
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 1- 1 October 20, October 20, 2015October 20, 2015October 20,
Review: –What is an activation record? –What are the typical fields in an activation record? –What are the storage allocation strategies? Which program.
COMPILERS Symbol Tables hussein suleman uct csc3003s 2007.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Intermediate Code Generation
CS 153: Concepts of Compiler Design October 5 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
May 31, May 31, 2016May 31, 2016May 31, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa Pacific University,
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,
1 November 19, November 19, 2015November 19, 2015November 19, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
Introduction to Code Generation and Intermediate Representations
CS 153: Concepts of Compiler Design October 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
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 January 18, January 18, 2016January 18, 2016January 18, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.
1 February 17, February 17, 2016February 17, 2016February 17, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
1 February 23, February 23, 2016February 23, 2016February 23, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
Chapter 8: Semantic Analyzer1 Compiler Designs and Constructions Chapter 8: Semantic Analyzer Objectives: Syntax-Directed Translation Type Checking Dr.
1 February 28, February 28, 2016February 28, 2016February 28, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
1 March 16, March 16, 2016March 16, 2016March 16, 2016 Azusa, CA Sheldon X. Liang Ph. D. Azusa Pacific University, Azusa, CA 91702, Tel: (800)
March 19, March 19, 2016March 19, 2016March 19, 2016 Azusa, CA Sheldon X. Liang Ph. D. Software Engineering in CS at APU Azusa Pacific University,
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
©2004 Joel Jones 1 CS 403: Programming Languages Lecture 3 Fall 2004 Department of Computer Science University of Alabama Joel Jones.
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Lecture 9 Symbol Table and Attributed Grammars
Context-Sensitive Analysis
Compiler Construction
Subject Name:COMPILER DESIGN Subject Code:10CS63
Intermediate Code Generation Part I
Intermediate Code Generation
CMPE 152: Compiler Design October 4 Class Meeting
Chapter 6 Intermediate-Code Generation
Intermediate Code Generation Part I
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.
Intermediate Code Generation Part I
Intermediate Code Generation
COMPILERS Semantic Analysis
Intermediate Code Generation Part I
SYMBOL TABLE Chuen-Liang Chen Department of Computer Science
Presentation transcript:

1 October 25, 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) Department of Computer Science, CS400 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 endBack end Intermediate code Target machine code October 25, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

3 October 25, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, 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 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, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

5 Symbol Tables for Scoping October 25, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

6 Symbol Tables for Scoping October 25, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, 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 October 25, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, 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 October 25, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, 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 October 25, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, 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 October 25, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, 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 October 25, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

12 Example October 25, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

13 Hierarchical Symbol Tables October 25, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, 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 October 25, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

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, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

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, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, 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 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, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

18 Example October 25, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

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, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

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, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

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, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction

22 October 25, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, 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 Thank you very much! Questions? October 25, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction