2009-03-23 17:52:36 +00:00
|
|
|
|
// -*- C++ -*-
|
2006-08-26 11:35:28 +00:00
|
|
|
|
//*************************************************************************
|
|
|
|
|
// DESCRIPTION: Verilator: Symbol table
|
|
|
|
|
//
|
2008-04-25 12:14:27 +00:00
|
|
|
|
// Code available from: http://www.veripool.org/verilator
|
2006-08-26 11:35:28 +00:00
|
|
|
|
//
|
|
|
|
|
// AUTHORS: Wilson Snyder with Paul Wasson, Duane Gabli
|
|
|
|
|
//
|
|
|
|
|
//*************************************************************************
|
|
|
|
|
//
|
2009-01-02 16:47:39 +00:00
|
|
|
|
// Copyright 2003-2009 by Wilson Snyder. This program is free software; you can
|
2006-08-26 11:35:28 +00:00
|
|
|
|
// redistribute it and/or modify it under the terms of either the GNU
|
2009-05-04 21:07:57 +00:00
|
|
|
|
// Lesser General Public License Version 3 or the Perl Artistic License
|
|
|
|
|
// Version 2.0.
|
2006-08-26 11:35:28 +00:00
|
|
|
|
//
|
|
|
|
|
// Verilator is distributed in the hope that it will be useful,
|
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
//
|
|
|
|
|
//*************************************************************************
|
|
|
|
|
|
|
|
|
|
#ifndef _V3LINKSYMTABLE_H_
|
|
|
|
|
#define _V3LINKSYMTABLE_H_ 1
|
|
|
|
|
|
2006-12-18 19:20:45 +00:00
|
|
|
|
#include "config_build.h"
|
|
|
|
|
#include "verilatedos.h"
|
2008-06-30 17:11:25 +00:00
|
|
|
|
#include <cstdio>
|
|
|
|
|
#include <cstdarg>
|
2006-08-26 11:35:28 +00:00
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <map>
|
2009-11-07 04:16:06 +00:00
|
|
|
|
#include <iomanip>
|
2006-08-26 11:35:28 +00:00
|
|
|
|
|
|
|
|
|
#include "V3Global.h"
|
|
|
|
|
|
|
|
|
|
//######################################################################
|
|
|
|
|
// Symbol table
|
|
|
|
|
|
|
|
|
|
class V3SymTable : public AstNUser {
|
|
|
|
|
// Symbol table that can have a "superior" table for resolving upper references
|
|
|
|
|
private:
|
|
|
|
|
// MEMBERS
|
|
|
|
|
typedef std::map<string,AstNode*> IdNameMap;
|
2009-10-31 14:14:04 +00:00
|
|
|
|
IdNameMap m_idNameMap; // Hash of variables by name
|
|
|
|
|
AstNode* m_ownerp; // Node that table belongs to
|
2006-08-26 11:35:28 +00:00
|
|
|
|
V3SymTable* m_upperp; // Table "above" this one in name scope
|
|
|
|
|
public:
|
|
|
|
|
// METHODS
|
2009-10-31 14:14:04 +00:00
|
|
|
|
V3SymTable(AstNode* ownerp, V3SymTable* upperTablep) {
|
|
|
|
|
m_ownerp = ownerp; m_upperp = upperTablep; }
|
|
|
|
|
V3SymTable() {
|
|
|
|
|
m_ownerp = NULL; m_upperp = NULL; }
|
2006-08-26 11:35:28 +00:00
|
|
|
|
~V3SymTable() {}
|
2009-10-31 14:14:04 +00:00
|
|
|
|
AstNode* ownerp() const { return m_ownerp; }
|
2006-08-26 11:35:28 +00:00
|
|
|
|
void insert(const string& name, AstNode* nodep) {
|
|
|
|
|
//UINFO(9, " SymInsert "<<this<<" '"<<name<<"' "<<nodep<<endl);
|
|
|
|
|
if (m_idNameMap.find(name) != m_idNameMap.end()) {
|
|
|
|
|
if (!V3Error::errorCount()) { // Else may have just reported warning
|
|
|
|
|
nodep->v3fatalSrc("Inserting two symbols with same name: "<<name<<endl);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
m_idNameMap.insert(make_pair(name, nodep));
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-10-31 14:14:04 +00:00
|
|
|
|
void reinsert(const string& name, AstNode* nodep) {
|
|
|
|
|
IdNameMap::iterator it = m_idNameMap.find(name);
|
|
|
|
|
if (it != m_idNameMap.end()) {
|
|
|
|
|
//UINFO(9, " SymReinsert "<<this<<" '"<<name<<"' "<<nodep<<endl);
|
|
|
|
|
it->second = nodep; // Replace
|
|
|
|
|
} else {
|
|
|
|
|
insert(name,nodep);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
AstNode* findIdFlat(const string& name) const {
|
2009-03-24 13:22:58 +00:00
|
|
|
|
// Find identifier without looking upward through symbol hierarchy
|
2006-08-26 11:35:28 +00:00
|
|
|
|
//UINFO(9, " SymFind "<<this<<" '"<<name<<"' "<<endl);
|
|
|
|
|
// First, scan this begin/end block or module for the name
|
2009-10-31 14:14:04 +00:00
|
|
|
|
IdNameMap::const_iterator iter = m_idNameMap.find(name);
|
2006-08-26 11:35:28 +00:00
|
|
|
|
if (iter != m_idNameMap.end()) return (iter->second);
|
2009-03-23 18:57:15 +00:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2009-10-31 14:14:04 +00:00
|
|
|
|
AstNode* findIdUpward(const string& name) const {
|
2009-03-24 13:22:58 +00:00
|
|
|
|
// Find identifier looking upward through symbol hierarchy
|
2009-03-23 18:57:15 +00:00
|
|
|
|
// First, scan this begin/end block or module for the name
|
2009-03-24 13:22:58 +00:00
|
|
|
|
if (AstNode* nodep = findIdFlat(name)) return nodep;
|
2006-08-26 11:35:28 +00:00
|
|
|
|
// Then scan the upper begin/end block or module for the name
|
2009-03-24 13:22:58 +00:00
|
|
|
|
if (m_upperp) return m_upperp->findIdUpward(name);
|
2006-08-26 11:35:28 +00:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2009-10-31 14:14:04 +00:00
|
|
|
|
void dump(ostream& os, const string& indent="", bool user4p_is_table=false) const {
|
2009-11-07 23:03:23 +00:00
|
|
|
|
if (user4p_is_table) { AstUser4InUse::check(); }
|
2009-10-31 14:14:04 +00:00
|
|
|
|
for (IdNameMap::const_iterator it=m_idNameMap.begin(); it!=m_idNameMap.end(); ++it) {
|
|
|
|
|
os<<indent<<it->first;
|
|
|
|
|
for (int i=it->first.length(); i<30; ++i) os<<" ";
|
|
|
|
|
if (user4p_is_table) {
|
|
|
|
|
V3SymTable* belowp = (it->second)->user4p()->castSymTable();
|
2009-11-07 04:16:06 +00:00
|
|
|
|
os<<setw(10)<<(void*)(belowp)<<setw(0)<<" "<<it->second<<endl;
|
|
|
|
|
if (belowp) belowp->dump(os, indent+"+ ", user4p_is_table);
|
|
|
|
|
} else {
|
|
|
|
|
os<<it->second<<endl;
|
2009-10-31 14:14:04 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2006-08-26 11:35:28 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif // guard
|