Symbol table lookup & install

Slides:



Advertisements
Similar presentations
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
Advertisements

The Symbol Table Lecture 13 Wed, Feb 23, The Symbol Table When identifiers are found, they will be entered into a symbol table, which will hold.
Dynamic Memory Allocation in C++. Memory Segments in C++ Memory is divided in certain segments – Code Segment Stores application code – Data Segment Holds.
CS 536 Spring Run-time organization Lecture 19.
Unix Process Environment. main Function A C program starts execution with a function called main. The prototype for the main function is: int main (int.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Scope Accessibility of Names. Review We’ve seen that C++ permits a programmer to declare names and then use those names in a manner consistent with their.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
1 Scope Scope describes the region where an identifier is known, and semantic rules for this.
C Programming Day 4. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 More on Pointers Constant Pointers Two ways to.
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
C++ Lecture 5 Monday, 18 July Chapter 7 Classes, continued l const objects and const member functions l Composition: objects as members of classes.
Functions. Flow of Control Review while for do while goto break & continue switch Relational operators short circuits.
Chapter 2. Variable and Data type
Language Implementation Overview John Keyser Spring 2016.
CS412/413 Introduction to Compilers Radu Rugina Lecture 11: Symbol Tables 13 Feb 02.
 constant represented by a name, just like a variable, but whose value cannot be changed  The const keyword precedes the type, name, and initialization.
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Windows Programming Lecture 03. Pointers and Arrays.
Advanced Programming in C
Generic Programming in C
Name Spaces: ALL versus OOL
Classes C++ representation of an object
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Lectures linked lists Chapter 6 of textbook
C++ Standard Library.
Run-time organization
Functions.
Student Book An Introduction
Names.
Semantic Analysis Chapter 6.
Lecture 6 C++ Programming
A Doubly Linked List There’s the need to access a list in reverse order prev next data dnode header 1.
#include "std_lib_facilities
Compiler Design 18. Object Oriented Semantic Analysis (Symbol Tables, Type Checking) Kanat Bolazar March 30, 2010.
CSE 3302 Programming Languages
Pointers, Dynamic Data, and Reference Types
Structs and Classes Static Class Members Recall the following
Govt. Polytechnic,Dhangar
CSC 253 Lecture 7.
PZ09A - Activation records
Topic 3-b Run-Time Environment
Introduction to Programming
C-to-LC3 Compiler Over the course of the next two weeks, you will build a program that will compile C code to LC-3 assembly language Don't panic! You.
Namespaces How Shall I Name Thee?.
Semantic Analysis Chapter 6.
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Pointers Pointers point to memory locations
Not referenced literal
C Programming Lecture-8 Pointers and Memory Management
inline substitution algorithm
Type compatibility and pointer operation
CSE 3302 Programming Languages
Chapter 9: Pointers and String
Classes C++ representation of an object
COMPILERS Semantic Analysis
Interface between frontend and backend
Programming Languages and Paradigms
Tag at C++ Compiler Kei Hasegawa.
nested-name-specifier
Design of tacsim - Execute part
Topics to cover Instance variables vs. local variables.
Storage classes in C In C language, each variable has a storage class which decides the following things: scope i.e where the value of the variable would.
Templates Generic Programming.
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Functions.
Temporary type Kei Hasegawa
Default argument Kei Hasegawa.
Presentation transcript:

Symbol table lookup & install Kei Hasegawa 2018.09.25

Variables and literals in program text struct scope { ... map<string, vector<usr*> > usrs; // Accessible symbol infomation form name };

map<string, vector<usr*> > // Outside of function int i = 1; int i; // ok extern int i; // Of cause ok That's because chosing map<string, vector<usr*> > not map<string, usr*>

How backend deal with symbol table? int i = 1; // (1) int i; // (2) extern int i; // (3) For these delarations, backend shall allocate 4 (=sizeof(int)) bytes with initial value `1' for `i'. For (2) declaration, it is not correct to allocate 4 bytes for `i' because multiple definition of `i'.

frontend function lookup() // For string, return lexical kind by searching symbol table lex::kind_t lookup(string name, scope* ptr) { const map<string, vector<usr*> >& usrs = ptr->usrs; map<string, vector<usr*> >::const_iterator p = usrs.find(name); if ( p != usrs.end() ) { const vector<usr*>& v = p->second; usr* u = v.back(); // POINT : Reference to the last one ... } int i = 1; // (1) int i; // (2) extern int i; // (3) void f(void){ ... use i ... } // Reference to (3) declaration

How backend deal with symbol table?(2) In function `gen' For (1), generate `i' space code with initial value For (2), generate `i' space code with no initial value For (3), just skip because of `extern' These are not correct because of multipe definition of `i' find_if(v.begin(), v.end(), gen); may works roughly. int i = 1; // (1) int i; // (2) extern int i; // (3) Implementation of backend const map<string, vector<usr*> >& usrs = ... for_each(usrs.begin(), usrs.end(), usr); void usr(const pair<string vector<usr*> >& p) { const vector<usr*>& v = p.second; for_each(v.begin(), v.end(), gen); }

How backend deal with symbol table?(3) int i = 1; // (1) int i; // (2) void f(void){ ... use i ... } // Reference to not (1) but to (2). So backend have to decide address descriptor of (2) `i' declaration extern int i; void g(void){ ... use i ... } // Reference to (3) neither (1) or (2). Instad of code generation for some declarations, backend has to decide address descriptor for the last one. i.e: const vector<usr*>& v = p.second; find_if(v.begin(), v.end(), gen); decide_address_descriptor(v.back());

Install to the symbol table void install(usr* curr, scope* ptr) { string name = curr->m_name; map<string, vector<usr*> >& usrs = ptr->m_usrs; map<string, vector<usr*> >::iterator p = usrs.find(name); if ( p != usrs.end() ) { vector<usr*>& v = p->second; usr* prev = v.back(); // Just compare with the last one if (!prev->m_type->compaitble(curr->m_type) || ... ) { // Eror. redeclaration. } else { curr->m_type = prev->m_type->composite(curr->m_type); usrs[name].push_back(curr);

Example void f(); // Register as void (...) void g() { } f(1,2,3); // ok } void f(int); // ok. void (...) is compatible with void (int) // Register as composite type void (int) void h() { f(3); f(1,2,3); // error. void f(); // Once again! void (int) is compatible with void (...) // Register as composite type void (int) not void (...) void i() { f(1,2,3); // error