User Guide
nmodl::symtab::ModelSymbolTable Class Reference

Hold top level (i.e. More...

Detailed Description

Hold top level (i.e.

global) symbol table for mod file

symtab::SymbolTable is sufficient to hold information about all symbols in the mod file. It might be sufficient to keep track of global symbol tables and local symbol tables. But we construct symbol table using visitor pass. In this case we visit ast and recursively create symbol table for each block scope. In this case, ModelSymbolTable provides high level interface to build symbol table as part of visitor.

Note
  • For included mod file it's not clear yet whether we need to maintain separate ModelSymbolTable.
  • See command project in compiler teaching course for details
Todo:
Unique name should be based on location. Use ModToken to get position.

Definition at line 239 of file symbol_table.hpp.

#include <symbol_table.hpp>

Public Member Functions

SymbolTableenter_scope (const std::string &name, ast::Ast *node, bool global, SymbolTable *node_symtab)
 entering into new nmodl block More...
 
void leave_scope ()
 leaving current nmodl block More...
 
std::shared_ptr< Symbolinsert (const std::shared_ptr< Symbol > &symbol)
 insert new symbol into current table More...
 
std::shared_ptr< Symbollookup (const std::string &name)
 lookup for symbol into current as well as all parent tables More...
 
void set_mode (bool update_mode)
 re-initialize members to throw away old symbol tables this is required as symtab visitor pass runs multiple time More...
 
void print (std::ostream &ostr) const
 pretty print More...
 

Private Member Functions

std::string get_unique_name (const std::string &name, ast::Ast *node, bool is_global)
 return unique name by appending some counter value More...
 
std::shared_ptr< Symbolupdate_mode_insert (const std::shared_ptr< Symbol > &symbol)
 insert symbol table in update mode More...
 
void emit_message (const std::shared_ptr< Symbol > &first, const std::shared_ptr< Symbol > &second, bool redefinition)
 Emit warning message for shadowing definition or throw an exception if variable is being redefined in the same block. More...
 
void update_order (const std::shared_ptr< Symbol > &present_symbol, const std::shared_ptr< Symbol > &new_symbol)
 

Private Attributes

std::shared_ptr< SymbolTablesymtab
 symbol table for mod file (always top level symbol table) More...
 
SymbolTablecurrent_symtab = nullptr
 current symbol table being constructed More...
 
const std::string GLOBAL_SYMTAB_NAME = "NMODL_GLOBAL"
 name of top level global symbol table More...
 
bool update_table = false
 default mode of symbol table: if update is true then we update exisiting symbols otherwise we throw away old table and construct new one More...
 
int definition_order = 0
 current order of variable being defined More...
 

Member Function Documentation

◆ emit_message()

void nmodl::symtab::ModelSymbolTable::emit_message ( const std::shared_ptr< Symbol > &  first,
const std::shared_ptr< Symbol > &  second,
bool  redefinition 
)
private

Emit warning message for shadowing definition or throw an exception if variable is being redefined in the same block.

Definition at line 199 of file symbol_table.cpp.

◆ enter_scope()

SymbolTable * nmodl::symtab::ModelSymbolTable::enter_scope ( const std::string &  name,
ast::Ast node,
bool  global,
SymbolTable node_symtab 
)

entering into new nmodl block

Function callback at the entry of every block in nmodl file Every block starts a new scope and hence new symbol table is created.

The same symbol table is returned so that visitor can store pointer to symbol table within a node.

All global blocks in mod file have same global symbol table. If there is already symbol table setup in global scope, return the same.

statement block within global scope is part of global block itself

Definition at line 377 of file symbol_table.cpp.

◆ get_unique_name()

std::string nmodl::symtab::ModelSymbolTable::get_unique_name ( const std::string &  name,
ast::Ast node,
bool  is_global 
)
private

return unique name by appending some counter value

Some blocks can appear multiple times in the nmodl file.

In order to distinguish them we simply append counter.

Todo:
We should add position information to make name unique

Definition at line 358 of file symbol_table.cpp.

◆ insert()

std::shared_ptr< Symbol > nmodl::symtab::ModelSymbolTable::insert ( const std::shared_ptr< Symbol > &  symbol)

insert new symbol into current table

handle update mode insertion

For global symbol tables, same variable can appear in multiple nmodl "global" blocks. It's an error if it appears multiple times in the same nmodl block. To check this we compare symbol properties which are bit flags. If they are not same that means, we have to add new properties to existing symbol. If the properties are same that means symbol are duplicate.

For non-global scopes, check if symbol that we found has same scope name as symbol table. This means variable is being re-declared within the same scope. Otherwise, there is variable with same name in parent scopes and it will shadow the definition. In this case just emit the warning and insert the symbol.

Note: We suppress the warning for the voltage since in most cases it is extern as well as argument and it is fine like that.

Suppress warning for voltage since it is often extern and argument.

Definition at line 286 of file symbol_table.cpp.

◆ leave_scope()

void nmodl::symtab::ModelSymbolTable::leave_scope ( )

leaving current nmodl block

Callback at the exit of every block in nmodl file.

When we reach program node (top most level), there is no parent block and hence use top level symbol table. Otherwise traverse back to parent.

Definition at line 420 of file symbol_table.cpp.

◆ lookup()

std::shared_ptr< Symbol > nmodl::symtab::ModelSymbolTable::lookup ( const std::string &  name)

lookup for symbol into current as well as all parent tables

lookup in current symtab as well as all parent symbol tables

Definition at line 173 of file symbol_table.cpp.

◆ print()

void nmodl::symtab::ModelSymbolTable::print ( std::ostream &  ostr) const
inline

pretty print

Definition at line 290 of file symbol_table.hpp.

◆ set_mode()

void nmodl::symtab::ModelSymbolTable::set_mode ( bool  update_mode)

re-initialize members to throw away old symbol tables this is required as symtab visitor pass runs multiple time

Update mode is true if we want to re-use exisiting symbol table.

If there is no symbol table constructed then we toggle mode.

Definition at line 435 of file symbol_table.cpp.

◆ update_mode_insert()

std::shared_ptr< Symbol > nmodl::symtab::ModelSymbolTable::update_mode_insert ( const std::shared_ptr< Symbol > &  symbol)
private

insert symbol table in update mode

Insert symbol in the update mode i.e.

symbol table is previously created and we are adding new symbol table.

We set status as "created" because missing symbol means the variable is added by some intermediate passes.

Consider inlining pass which creates a block like:

DERIVATIVE states() { LOCAL xx { LOCAL xx } }

Second xx is not added into symbol table (and hence not visible to other passes like local renamer). In this case we have to do local lookup in the current symtab and insert if doesn't exist.

if no symbol found then safe to insert

for global scope just combine properties

insert into current block's symbol table

Definition at line 244 of file symbol_table.cpp.

◆ update_order()

void nmodl::symtab::ModelSymbolTable::update_order ( const std::shared_ptr< Symbol > &  present_symbol,
const std::shared_ptr< Symbol > &  new_symbol 
)
private

Definition at line 271 of file symbol_table.cpp.

Member Data Documentation

◆ current_symtab

SymbolTable* nmodl::symtab::ModelSymbolTable::current_symtab = nullptr
private

current symbol table being constructed

Definition at line 244 of file symbol_table.hpp.

◆ definition_order

int nmodl::symtab::ModelSymbolTable::definition_order = 0
private

current order of variable being defined

Definition at line 257 of file symbol_table.hpp.

◆ GLOBAL_SYMTAB_NAME

const std::string nmodl::symtab::ModelSymbolTable::GLOBAL_SYMTAB_NAME = "NMODL_GLOBAL"
private

name of top level global symbol table

Definition at line 250 of file symbol_table.hpp.

◆ symtab

std::shared_ptr<SymbolTable> nmodl::symtab::ModelSymbolTable::symtab
private

symbol table for mod file (always top level symbol table)

Definition at line 241 of file symbol_table.hpp.

◆ update_table

bool nmodl::symtab::ModelSymbolTable::update_table = false
private

default mode of symbol table: if update is true then we update exisiting symbols otherwise we throw away old table and construct new one

Definition at line 254 of file symbol_table.hpp.


The documentation for this class was generated from the following files: