2012-04-13 01:08:20 +00:00
|
|
|
|
// -*- mode: C++; c-file-style: "cc-mode" -*-
|
2006-08-26 11:35:28 +00:00
|
|
|
|
//*************************************************************************
|
|
|
|
|
// DESCRIPTION: Verilator: Emit C++ for tree
|
|
|
|
|
//
|
2008-04-25 12:14:27 +00:00
|
|
|
|
// Code available from: http://www.veripool.org/verilator
|
2006-08-26 11:35:28 +00:00
|
|
|
|
//
|
|
|
|
|
//*************************************************************************
|
|
|
|
|
//
|
2013-01-01 14:42:59 +00:00
|
|
|
|
// Copyright 2003-2013 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.
|
|
|
|
|
//
|
|
|
|
|
//*************************************************************************
|
|
|
|
|
|
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>
|
2008-06-30 17:11:25 +00:00
|
|
|
|
#include <cmath>
|
2006-08-26 11:35:28 +00:00
|
|
|
|
#include <map>
|
|
|
|
|
#include <set>
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
|
|
#include "V3Global.h"
|
|
|
|
|
#include "V3EmitC.h"
|
|
|
|
|
#include "V3EmitCBase.h"
|
|
|
|
|
#include "V3LanguageWords.h"
|
|
|
|
|
|
|
|
|
|
//######################################################################
|
|
|
|
|
// Symbol table emitting
|
|
|
|
|
|
|
|
|
|
class EmitCSyms : EmitCBaseVisitor {
|
2008-12-05 15:54:14 +00:00
|
|
|
|
// NODE STATE
|
|
|
|
|
// Cleared on Netlist
|
2009-11-07 11:20:20 +00:00
|
|
|
|
// AstNodeModule::user1() -> bool. Set true __Vconfigure called
|
2008-12-05 15:54:14 +00:00
|
|
|
|
AstUser1InUse m_inuser1;
|
|
|
|
|
|
2009-12-09 03:12:59 +00:00
|
|
|
|
// TYPES
|
2010-03-17 12:22:49 +00:00
|
|
|
|
struct ScopeNameData { string m_symName; string m_prettyName;
|
|
|
|
|
ScopeNameData(const string& symName, const string& prettyName)
|
|
|
|
|
: m_symName(symName), m_prettyName(prettyName) {}
|
|
|
|
|
};
|
2009-12-20 13:27:00 +00:00
|
|
|
|
struct ScopeFuncData { AstScopeName* m_scopep; AstCFunc* m_funcp; AstNodeModule* m_modp;
|
|
|
|
|
ScopeFuncData(AstScopeName* scopep, AstCFunc* funcp, AstNodeModule* modp)
|
|
|
|
|
: m_scopep(scopep), m_funcp(funcp), m_modp(modp) {}
|
|
|
|
|
};
|
2010-03-17 12:22:49 +00:00
|
|
|
|
struct ScopeVarData { string m_scopeName; string m_varBasePretty; AstVar* m_varp;
|
|
|
|
|
AstNodeModule* m_modp; AstScope* m_scopep;
|
|
|
|
|
ScopeVarData(const string& scopeName, const string& varBasePretty, AstVar* varp, AstNodeModule* modp, AstScope* scopep)
|
|
|
|
|
: m_scopeName(scopeName), m_varBasePretty(varBasePretty), m_varp(varp), m_modp(modp), m_scopep(scopep) {}
|
|
|
|
|
};
|
2009-12-20 13:27:00 +00:00
|
|
|
|
typedef map<string,ScopeFuncData> ScopeFuncs;
|
2010-03-17 12:22:49 +00:00
|
|
|
|
typedef map<string,ScopeVarData> ScopeVars;
|
|
|
|
|
typedef map<string,ScopeNameData> ScopeNames;
|
2009-12-09 03:12:59 +00:00
|
|
|
|
typedef pair<AstScope*,AstNodeModule*> ScopeModPair;
|
2010-03-17 12:22:49 +00:00
|
|
|
|
typedef pair<AstNodeModule*,AstVar*> ModVarPair;
|
2009-12-09 03:12:59 +00:00
|
|
|
|
struct CmpName {
|
|
|
|
|
inline bool operator () (const ScopeModPair& lhsp, const ScopeModPair& rhsp) const {
|
|
|
|
|
return lhsp.first->name() < rhsp.first->name();
|
|
|
|
|
}
|
|
|
|
|
};
|
2009-12-20 13:27:00 +00:00
|
|
|
|
struct CmpDpi {
|
|
|
|
|
inline bool operator () (const AstCFunc* lhsp, const AstCFunc* rhsp) const {
|
|
|
|
|
if (lhsp->dpiImport() != rhsp->dpiImport()) {
|
|
|
|
|
return lhsp->dpiImport() < rhsp->dpiImport();
|
|
|
|
|
}
|
|
|
|
|
return lhsp->name() < rhsp->name();
|
|
|
|
|
}
|
|
|
|
|
};
|
2009-12-05 15:38:49 +00:00
|
|
|
|
|
2006-08-26 11:35:28 +00:00
|
|
|
|
// STATE
|
2009-12-20 13:27:00 +00:00
|
|
|
|
AstCFunc* m_funcp; // Current function
|
2009-11-07 11:20:20 +00:00
|
|
|
|
AstNodeModule* m_modp; // Current module
|
2006-08-26 11:35:28 +00:00
|
|
|
|
vector<ScopeModPair> m_scopes; // Every scope by module
|
2009-12-09 03:12:59 +00:00
|
|
|
|
vector<AstCFunc*> m_dpis; // DPI functions
|
2010-03-17 12:22:49 +00:00
|
|
|
|
vector<ModVarPair> m_modVars; // Each public {mod,var}
|
2009-12-05 15:38:49 +00:00
|
|
|
|
ScopeNames m_scopeNames; // Each unique AstScopeName
|
2010-03-17 12:22:49 +00:00
|
|
|
|
ScopeFuncs m_scopeFuncs; // Each {scope,dpi-export-func}
|
|
|
|
|
ScopeVars m_scopeVars; // Each {scope,public-var}
|
2006-08-26 11:35:28 +00:00
|
|
|
|
V3LanguageWords m_words; // Reserved word detector
|
2008-12-05 15:54:14 +00:00
|
|
|
|
int m_coverBins; // Coverage bin number
|
2010-02-14 15:01:21 +00:00
|
|
|
|
int m_labelNum; // Next label number
|
2006-08-26 11:35:28 +00:00
|
|
|
|
|
|
|
|
|
// METHODS
|
2009-12-09 03:12:59 +00:00
|
|
|
|
void emitSymHdr();
|
|
|
|
|
void emitSymImp();
|
|
|
|
|
void emitDpiHdr();
|
|
|
|
|
void emitDpiImp();
|
2006-08-26 11:35:28 +00:00
|
|
|
|
|
|
|
|
|
void nameCheck(AstNode* nodep) {
|
|
|
|
|
// Prevent GCC compile time error; name check all things that reach C++ code
|
|
|
|
|
if (nodep->name() != "") {
|
|
|
|
|
string rsvd = m_words.isKeyword(nodep->name());
|
|
|
|
|
if (rsvd != "") {
|
2009-07-22 19:21:41 +00:00
|
|
|
|
// Generally V3Name should find all of these and throw SYMRSVDWORD.
|
|
|
|
|
// We'll still check here because the compiler errors resulting if we miss this warning are SO nasty
|
2013-03-12 11:27:17 +00:00
|
|
|
|
nodep->v3error("Symbol matching "+rsvd+" reserved word reached emitter, should have hit SYMRSVDWORD: '"<<nodep->prettyName()<<"'");
|
2006-08-26 11:35:28 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-17 12:22:49 +00:00
|
|
|
|
void varsExpand() {
|
|
|
|
|
// We didn'e have all m_scopes loaded when we encountered variables, so expand them now
|
|
|
|
|
// It would be less code if each module inserted its own variables.
|
|
|
|
|
// Someday. For now public isn't common.
|
|
|
|
|
for (vector<ScopeModPair>::iterator it = m_scopes.begin(); it != m_scopes.end(); ++it) {
|
|
|
|
|
AstScope* scopep = it->first; AstNodeModule* smodp = it->second;
|
|
|
|
|
for (vector<ModVarPair>::iterator it = m_modVars.begin(); it != m_modVars.end(); ++it) {
|
|
|
|
|
AstNodeModule* modp = it->first;
|
2012-10-30 07:02:35 +00:00
|
|
|
|
AstVar* varp = it->second;
|
2010-03-17 12:22:49 +00:00
|
|
|
|
if (modp == smodp) {
|
|
|
|
|
// Need to split the module + var name into the original-ish full scope and variable name under that scope.
|
|
|
|
|
// The module instance name is included later, when we know the scopes this module is under
|
|
|
|
|
string whole = scopep->name()+"__DOT__"+varp->name();
|
|
|
|
|
string scpName;
|
|
|
|
|
string varBase;
|
|
|
|
|
if (whole.substr(0,10) == "__DOT__TOP") whole.replace(0,10,"");
|
|
|
|
|
string::size_type pos = whole.rfind("__DOT__");
|
|
|
|
|
if (pos != string::npos) {
|
|
|
|
|
scpName = whole.substr(0,pos);
|
|
|
|
|
varBase = whole.substr(pos+strlen("__DOT__"));
|
|
|
|
|
} else {
|
|
|
|
|
varBase = whole;
|
|
|
|
|
}
|
|
|
|
|
//UINFO(9,"For "<<scopep->name()<<" - "<<varp->name()<<" Scp "<<scpName<<" Var "<<varBase<<endl);
|
|
|
|
|
string varBasePretty = AstNode::prettyName(varBase);
|
|
|
|
|
string scpPretty = AstNode::prettyName(scpName);
|
|
|
|
|
string scpSym;
|
|
|
|
|
{
|
|
|
|
|
string out = scpName;
|
|
|
|
|
string::size_type pos;
|
|
|
|
|
while ((pos=out.find("__PVT__")) != string::npos) {
|
|
|
|
|
out.replace(pos, 7, "");
|
|
|
|
|
}
|
|
|
|
|
if (out.substr(0,10) == "TOP__DOT__") out.replace(0,10,"");
|
|
|
|
|
if (out.substr(0,4) == "TOP.") out.replace(0,4,"");
|
|
|
|
|
while ((pos=out.find(".")) != string::npos) {
|
|
|
|
|
out.replace(pos, 1, "__");
|
|
|
|
|
}
|
|
|
|
|
while ((pos=out.find("__DOT__")) != string::npos) {
|
|
|
|
|
out.replace(pos, 7, "__");
|
|
|
|
|
}
|
|
|
|
|
scpSym = out;
|
|
|
|
|
}
|
|
|
|
|
//UINFO(9," scnameins sp "<<scpName<<" sp "<<scpPretty<<" ss "<<scpSym<<endl);
|
|
|
|
|
if (m_scopeNames.find(scpSym) == m_scopeNames.end()) {
|
|
|
|
|
m_scopeNames.insert(make_pair(scpSym, ScopeNameData(scpSym, scpPretty)));
|
|
|
|
|
}
|
|
|
|
|
m_scopeVars.insert(make_pair(scpSym + " " + varp->name(),
|
|
|
|
|
ScopeVarData(scpSym, varBasePretty, varp, modp, scopep)));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2006-08-26 11:35:28 +00:00
|
|
|
|
// VISITORS
|
|
|
|
|
virtual void visit(AstNetlist* nodep, AstNUser*) {
|
|
|
|
|
// Collect list of scopes
|
|
|
|
|
nodep->iterateChildren(*this);
|
2010-03-17 12:22:49 +00:00
|
|
|
|
varsExpand();
|
2008-06-10 01:25:10 +00:00
|
|
|
|
|
2009-12-20 13:27:00 +00:00
|
|
|
|
// Sort by names, so line/process order matters less
|
2006-08-26 11:35:28 +00:00
|
|
|
|
sort(m_scopes.begin(), m_scopes.end(), CmpName());
|
2009-12-20 13:27:00 +00:00
|
|
|
|
sort(m_dpis.begin(), m_dpis.end(), CmpDpi());
|
2008-12-05 15:54:14 +00:00
|
|
|
|
|
2006-08-26 11:35:28 +00:00
|
|
|
|
// Output
|
2009-12-09 03:12:59 +00:00
|
|
|
|
emitSymHdr();
|
|
|
|
|
emitSymImp();
|
|
|
|
|
if (v3Global.dpi()) {
|
|
|
|
|
emitDpiHdr();
|
2009-12-20 13:27:00 +00:00
|
|
|
|
emitDpiImp();
|
2009-12-09 03:12:59 +00:00
|
|
|
|
}
|
2006-08-26 11:35:28 +00:00
|
|
|
|
}
|
2009-11-07 11:20:20 +00:00
|
|
|
|
virtual void visit(AstNodeModule* nodep, AstNUser*) {
|
2006-08-26 11:35:28 +00:00
|
|
|
|
nameCheck(nodep);
|
|
|
|
|
m_modp = nodep;
|
2010-02-14 15:01:21 +00:00
|
|
|
|
m_labelNum = 0;
|
2006-08-26 11:35:28 +00:00
|
|
|
|
nodep->iterateChildren(*this);
|
2008-11-12 20:32:09 +00:00
|
|
|
|
m_modp = NULL;
|
2006-08-26 11:35:28 +00:00
|
|
|
|
}
|
|
|
|
|
virtual void visit(AstScope* nodep, AstNUser*) {
|
|
|
|
|
nameCheck(nodep);
|
|
|
|
|
m_scopes.push_back(make_pair(nodep, m_modp));
|
|
|
|
|
}
|
2009-12-05 15:38:49 +00:00
|
|
|
|
virtual void visit(AstScopeName* nodep, AstNUser*) {
|
|
|
|
|
string name = nodep->scopeSymName();
|
2010-03-17 12:22:49 +00:00
|
|
|
|
//UINFO(9,"scnameins sp "<<nodep->name()<<" sp "<<nodep->scopePrettyName()<<" ss "<<name<<endl);
|
2009-12-05 15:38:49 +00:00
|
|
|
|
if (m_scopeNames.find(name) == m_scopeNames.end()) {
|
2010-03-17 12:22:49 +00:00
|
|
|
|
m_scopeNames.insert(make_pair(name, ScopeNameData(name, nodep->scopePrettyName())));
|
2009-12-05 15:38:49 +00:00
|
|
|
|
}
|
2009-12-20 13:27:00 +00:00
|
|
|
|
if (nodep->dpiExport()) {
|
|
|
|
|
if (!m_funcp) nodep->v3fatalSrc("ScopeName not under DPI function");
|
|
|
|
|
m_scopeFuncs.insert(make_pair(name + " " + m_funcp->name(),
|
|
|
|
|
ScopeFuncData(nodep, m_funcp, m_modp)));
|
|
|
|
|
}
|
2009-12-05 15:38:49 +00:00
|
|
|
|
}
|
2010-03-17 12:22:49 +00:00
|
|
|
|
virtual void visit(AstVar* nodep, AstNUser*) {
|
|
|
|
|
nodep->iterateChildren(*this);
|
2012-10-30 07:02:35 +00:00
|
|
|
|
if (nodep->isSigUserRdPublic()
|
|
|
|
|
&& !nodep->isParam()) { // The VPI functions require a pointer to allow modification, but parameters are constants
|
2010-03-17 12:22:49 +00:00
|
|
|
|
m_modVars.push_back(make_pair(m_modp, nodep));
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-12-05 15:54:14 +00:00
|
|
|
|
virtual void visit(AstCoverDecl* nodep, AstNUser*) {
|
|
|
|
|
// Assign numbers to all bins, so we know how big of an array to use
|
2008-12-12 20:34:02 +00:00
|
|
|
|
if (!nodep->dataDeclNullp()) { // else duplicate we don't need code for
|
|
|
|
|
nodep->binNum(m_coverBins++);
|
|
|
|
|
}
|
2008-12-05 15:54:14 +00:00
|
|
|
|
}
|
2010-02-14 15:01:21 +00:00
|
|
|
|
virtual void visit(AstJumpLabel* nodep, AstNUser*) {
|
|
|
|
|
nodep->labelNum(++m_labelNum);
|
|
|
|
|
nodep->iterateChildren(*this);
|
|
|
|
|
}
|
2009-12-09 03:12:59 +00:00
|
|
|
|
virtual void visit(AstCFunc* nodep, AstNUser*) {
|
2009-12-20 13:27:00 +00:00
|
|
|
|
if (nodep->dpiImport() || nodep->dpiExportWrapper()) {
|
2009-12-09 03:12:59 +00:00
|
|
|
|
m_dpis.push_back(nodep);
|
|
|
|
|
}
|
2009-12-20 13:27:00 +00:00
|
|
|
|
m_funcp = nodep;
|
|
|
|
|
nodep->iterateChildren(*this);
|
|
|
|
|
m_funcp = NULL;
|
2009-12-09 03:12:59 +00:00
|
|
|
|
}
|
2006-08-26 11:35:28 +00:00
|
|
|
|
// NOPs
|
|
|
|
|
virtual void visit(AstConst*, AstNUser*) {}
|
|
|
|
|
// Default
|
|
|
|
|
virtual void visit(AstNode* nodep, AstNUser*) {
|
|
|
|
|
nameCheck(nodep);
|
|
|
|
|
nodep->iterateChildren(*this);
|
|
|
|
|
}
|
|
|
|
|
//---------------------------------------
|
|
|
|
|
// ACCESSORS
|
|
|
|
|
public:
|
|
|
|
|
EmitCSyms(AstNetlist* nodep) {
|
2009-12-20 13:27:00 +00:00
|
|
|
|
m_funcp = NULL;
|
2006-08-26 11:35:28 +00:00
|
|
|
|
m_modp = NULL;
|
2008-12-05 15:54:14 +00:00
|
|
|
|
m_coverBins = 0;
|
2010-02-14 15:01:21 +00:00
|
|
|
|
m_labelNum = 0;
|
2006-08-26 11:35:28 +00:00
|
|
|
|
nodep->accept(*this);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2009-12-09 03:12:59 +00:00
|
|
|
|
void EmitCSyms::emitSymHdr() {
|
|
|
|
|
UINFO(6,__FUNCTION__<<": "<<endl);
|
2006-08-26 11:35:28 +00:00
|
|
|
|
string filename = v3Global.opt.makeDir()+"/"+symClassName()+".h";
|
|
|
|
|
newCFile(filename, true/*slow*/, false/*source*/);
|
|
|
|
|
V3OutCFile hf (filename);
|
|
|
|
|
m_ofp = &hf;
|
|
|
|
|
|
|
|
|
|
ofp()->putsHeader();
|
2009-12-03 01:09:13 +00:00
|
|
|
|
puts("// DESCR" "IPTION: Verilator output: Symbol table internal header\n");
|
|
|
|
|
puts("//\n");
|
|
|
|
|
puts("// Internal details; most calling programs do not need this header\n");
|
|
|
|
|
puts("\n");
|
|
|
|
|
|
2006-08-26 11:35:28 +00:00
|
|
|
|
puts("#ifndef _"+symClassName()+"_H_\n");
|
|
|
|
|
puts("#define _"+symClassName()+"_H_\n");
|
|
|
|
|
puts("\n");
|
|
|
|
|
|
2010-02-05 02:46:48 +00:00
|
|
|
|
if (optSystemPerl()) puts("#include \"systemperl.h\"\n");
|
|
|
|
|
else if (optSystemC()) puts("#include \"systemc.h\"\n");
|
|
|
|
|
|
|
|
|
|
if (optSystemPerl() || optSystemC()) {
|
|
|
|
|
puts("#include \"verilated_sc.h\"\n");
|
|
|
|
|
}
|
2010-01-17 20:10:37 +00:00
|
|
|
|
if (v3Global.needHeavy()) {
|
2010-01-24 13:38:17 +00:00
|
|
|
|
puts("#include \"verilated_heavy.h\"\n");
|
2010-01-17 20:10:37 +00:00
|
|
|
|
} else {
|
|
|
|
|
puts("#include \"verilated.h\"\n");
|
|
|
|
|
}
|
|
|
|
|
|
2006-08-26 11:35:28 +00:00
|
|
|
|
// for
|
|
|
|
|
puts("\n// INCLUDE MODULE CLASSES\n");
|
2009-11-07 11:20:20 +00:00
|
|
|
|
for (AstNodeModule* nodep = v3Global.rootp()->modulesp(); nodep; nodep=nodep->nextp()->castNodeModule()) {
|
2006-08-26 11:35:28 +00:00
|
|
|
|
puts("#include \""+modClassName(nodep)+".h\"\n");
|
|
|
|
|
}
|
|
|
|
|
|
2009-12-20 13:27:00 +00:00
|
|
|
|
if (v3Global.dpi()) {
|
|
|
|
|
puts ("\n// DPI TYPES for DPI Export callbacks (Internal use)\n");
|
|
|
|
|
map<string,int> types; // Remove duplicates and sort
|
|
|
|
|
for (ScopeFuncs::iterator it = m_scopeFuncs.begin(); it != m_scopeFuncs.end(); ++it) {
|
|
|
|
|
AstCFunc* funcp = it->second.m_funcp;
|
|
|
|
|
if (funcp->dpiExport()) {
|
|
|
|
|
string cbtype = v3Global.opt.prefix()+"__Vcb_"+funcp->cname()+"_t";
|
|
|
|
|
types["typedef void (*"+cbtype+") ("+cFuncArgs(funcp)+");\n"] = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for (map<string,int>::iterator it = types.begin(); it != types.end(); ++it) {
|
|
|
|
|
puts(it->first);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2006-08-26 11:35:28 +00:00
|
|
|
|
puts("\n// SYMS CLASS\n");
|
2009-12-20 13:27:00 +00:00
|
|
|
|
puts((string)"class "+symClassName()+" : public VerilatedSyms {\n");
|
2006-08-30 21:07:55 +00:00
|
|
|
|
ofp()->putsPrivate(false); // public:
|
2006-08-26 11:35:28 +00:00
|
|
|
|
|
2006-08-30 01:14:29 +00:00
|
|
|
|
puts("\n// LOCAL STATE\n");
|
2006-09-19 15:27:15 +00:00
|
|
|
|
ofp()->putAlign(V3OutFile::AL_AUTO, sizeof(vluint64_t));
|
2006-08-30 01:14:29 +00:00
|
|
|
|
puts("const char* __Vm_namep;\n"); // Must be before subcells, as constructor order needed before _vlCoverInsert.
|
2006-08-30 21:07:55 +00:00
|
|
|
|
ofp()->putAlign(V3OutFile::AL_AUTO, sizeof(bool));
|
|
|
|
|
puts("bool\t__Vm_activity;\t\t///< Used by trace routines to determine change occurred\n");
|
|
|
|
|
ofp()->putAlign(V3OutFile::AL_AUTO, sizeof(bool));
|
|
|
|
|
puts("bool\t__Vm_didInit;\n");
|
2006-08-30 01:14:29 +00:00
|
|
|
|
|
2006-09-19 15:27:15 +00:00
|
|
|
|
ofp()->putAlign(V3OutFile::AL_AUTO, sizeof(vluint64_t));
|
2006-08-30 01:14:29 +00:00
|
|
|
|
puts("\n// SUBCELL STATE\n");
|
2006-08-26 11:35:28 +00:00
|
|
|
|
for (vector<ScopeModPair>::iterator it = m_scopes.begin(); it != m_scopes.end(); ++it) {
|
2009-11-07 11:20:20 +00:00
|
|
|
|
AstScope* scopep = it->first; AstNodeModule* modp = it->second;
|
2006-08-26 11:35:28 +00:00
|
|
|
|
if (modp->isTop()) {
|
|
|
|
|
ofp()->printf("%-30s ", (modClassName(modp)+"*").c_str());
|
|
|
|
|
puts(scopep->nameDotless()+"p;\n");
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
ofp()->printf("%-30s ", (modClassName(modp)+"").c_str());
|
|
|
|
|
puts(scopep->nameDotless()+";\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-05 15:54:14 +00:00
|
|
|
|
puts("\n// COVERAGE\n");
|
|
|
|
|
if (m_coverBins) {
|
|
|
|
|
ofp()->putAlign(V3OutFile::AL_AUTO, sizeof(uint32_t));
|
|
|
|
|
puts("uint32_t\t__Vcoverage["); puts(cvtToStr(m_coverBins)); puts("];\n");
|
|
|
|
|
}
|
|
|
|
|
|
2009-12-05 15:38:49 +00:00
|
|
|
|
puts("\n// SCOPE NAMES\n");
|
|
|
|
|
for (ScopeNames::iterator it = m_scopeNames.begin(); it != m_scopeNames.end(); ++it) {
|
2010-03-17 12:22:49 +00:00
|
|
|
|
puts("VerilatedScope __Vscope_"+it->second.m_symName+";\n");
|
2009-12-05 15:38:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-08-26 11:35:28 +00:00
|
|
|
|
puts("\n// CREATORS\n");
|
2006-08-30 01:14:29 +00:00
|
|
|
|
puts(symClassName()+"("+topClassName()+"* topp, const char* namep);\n");
|
2006-08-26 11:35:28 +00:00
|
|
|
|
puts((string)"~"+symClassName()+"() {};\n");
|
2006-08-30 21:07:55 +00:00
|
|
|
|
|
2006-08-30 01:14:29 +00:00
|
|
|
|
puts("\n// METHODS\n");
|
|
|
|
|
puts("inline const char* name() { return __Vm_namep; }\n");
|
2006-08-30 21:07:55 +00:00
|
|
|
|
puts("inline bool getClearActivity() { bool r=__Vm_activity; __Vm_activity=false; return r;}\n");
|
2012-08-27 01:13:47 +00:00
|
|
|
|
if (v3Global.opt.savable() ) {
|
|
|
|
|
puts("void __Vserialize(VerilatedSerialize& os);\n");
|
|
|
|
|
puts("void __Vdeserialize(VerilatedDeserialize& os);\n");
|
|
|
|
|
}
|
2006-08-26 11:35:28 +00:00
|
|
|
|
puts("\n");
|
2006-08-30 22:00:55 +00:00
|
|
|
|
puts("} VL_ATTR_ALIGNED(64);\n");
|
2012-08-27 01:13:47 +00:00
|
|
|
|
puts("\n");
|
2006-08-26 11:35:28 +00:00
|
|
|
|
puts("#endif /*guard*/\n");
|
|
|
|
|
}
|
|
|
|
|
|
2009-12-09 03:12:59 +00:00
|
|
|
|
void EmitCSyms::emitSymImp() {
|
|
|
|
|
UINFO(6,__FUNCTION__<<": "<<endl);
|
2006-08-26 11:35:28 +00:00
|
|
|
|
string filename = v3Global.opt.makeDir()+"/"+symClassName()+".cpp";
|
|
|
|
|
AstCFile* cfilep = newCFile(filename, true/*slow*/, true/*source*/);
|
|
|
|
|
cfilep->support(true);
|
|
|
|
|
V3OutCFile cf (filename);
|
|
|
|
|
m_ofp = &cf;
|
|
|
|
|
ofp()->putsHeader();
|
2009-12-03 01:09:13 +00:00
|
|
|
|
puts("// DESCR" "IPTION: Verilator output: Symbol table implementation internals\n");
|
2006-08-26 11:35:28 +00:00
|
|
|
|
puts("\n");
|
|
|
|
|
|
|
|
|
|
// Includes
|
|
|
|
|
puts("#include \""+symClassName()+".h\"\n");
|
2009-11-07 11:20:20 +00:00
|
|
|
|
for (AstNodeModule* nodep = v3Global.rootp()->modulesp(); nodep; nodep=nodep->nextp()->castNodeModule()) {
|
2006-08-26 11:35:28 +00:00
|
|
|
|
puts("#include \""+modClassName(nodep)+".h\"\n");
|
|
|
|
|
}
|
|
|
|
|
|
2006-08-30 21:07:55 +00:00
|
|
|
|
//puts("\n// GLOBALS\n");
|
2006-08-26 11:35:28 +00:00
|
|
|
|
|
|
|
|
|
puts("\n// FUNCTIONS\n");
|
2006-08-30 01:14:29 +00:00
|
|
|
|
puts(symClassName()+"::"+symClassName()+"("+topClassName()+"* topp, const char* namep)\n");
|
|
|
|
|
puts("\t// Setup locals\n");
|
|
|
|
|
puts("\t: __Vm_namep(namep)\n"); // No leak, as we get destroyed when the top is destroyed
|
2006-08-30 21:07:55 +00:00
|
|
|
|
puts("\t, __Vm_activity(false)\n");
|
|
|
|
|
puts("\t, __Vm_didInit(false)\n");
|
2006-08-26 11:35:28 +00:00
|
|
|
|
puts("\t// Setup submodule names\n");
|
2006-08-30 01:14:29 +00:00
|
|
|
|
char comma=',';
|
2006-08-26 11:35:28 +00:00
|
|
|
|
for (vector<ScopeModPair>::iterator it = m_scopes.begin(); it != m_scopes.end(); ++it) {
|
2009-11-07 11:20:20 +00:00
|
|
|
|
AstScope* scopep = it->first; AstNodeModule* modp = it->second;
|
2006-08-26 11:35:28 +00:00
|
|
|
|
if (modp->isTop()) {
|
|
|
|
|
} else {
|
|
|
|
|
ofp()->printf("\t%c %-30s ", comma, scopep->nameDotless().c_str());
|
2009-05-08 17:16:19 +00:00
|
|
|
|
puts("(Verilated::catName(topp->name(),");
|
2010-02-04 00:19:18 +00:00
|
|
|
|
// The "." is added by catName
|
|
|
|
|
putsQuoted(scopep->prettyName());
|
2009-05-08 17:16:19 +00:00
|
|
|
|
puts("))\n");
|
2006-08-26 11:35:28 +00:00
|
|
|
|
comma=',';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
puts("{\n");
|
|
|
|
|
|
|
|
|
|
puts("// Pointer to top level\n");
|
|
|
|
|
puts("TOPp = topp;\n");
|
|
|
|
|
puts("// Setup each module's pointers to their submodules\n");
|
|
|
|
|
for (vector<ScopeModPair>::iterator it = m_scopes.begin(); it != m_scopes.end(); ++it) {
|
2009-11-07 11:20:20 +00:00
|
|
|
|
AstScope* scopep = it->first; AstNodeModule* modp = it->second;
|
2006-08-26 11:35:28 +00:00
|
|
|
|
if (!modp->isTop()) {
|
|
|
|
|
string arrow = scopep->name();
|
|
|
|
|
string::size_type pos;
|
|
|
|
|
while ((pos=arrow.find(".")) != string::npos) {
|
|
|
|
|
arrow.replace(pos, 1, "->");
|
|
|
|
|
}
|
|
|
|
|
if (arrow.substr(0,5) == "TOP->") arrow.replace(0,5,"TOPp->");
|
|
|
|
|
ofp()->printf("%-30s ", arrow.c_str());
|
|
|
|
|
puts(" = &");
|
|
|
|
|
puts(scopep->nameDotless()+";\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-12-05 15:38:49 +00:00
|
|
|
|
|
2006-08-26 11:35:28 +00:00
|
|
|
|
puts("// Setup each module's pointer back to symbol table (for public functions)\n");
|
2008-12-05 15:54:14 +00:00
|
|
|
|
puts("TOPp->__Vconfigure(this, true);\n");
|
2006-08-26 11:35:28 +00:00
|
|
|
|
for (vector<ScopeModPair>::iterator it = m_scopes.begin(); it != m_scopes.end(); ++it) {
|
2009-11-07 11:20:20 +00:00
|
|
|
|
AstScope* scopep = it->first; AstNodeModule* modp = it->second;
|
2006-08-26 11:35:28 +00:00
|
|
|
|
if (!modp->isTop()) {
|
2008-12-05 15:54:14 +00:00
|
|
|
|
// first is used by AstCoverDecl's call to __vlCoverInsert
|
|
|
|
|
bool first = !modp->user1();
|
|
|
|
|
modp->user1(true);
|
|
|
|
|
puts(scopep->nameDotless()+".__Vconfigure(this, "
|
|
|
|
|
+(first?"true":"false")
|
|
|
|
|
+");\n");
|
2006-08-26 11:35:28 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-12-05 15:38:49 +00:00
|
|
|
|
puts("// Setup scope names\n");
|
|
|
|
|
for (ScopeNames::iterator it = m_scopeNames.begin(); it != m_scopeNames.end(); ++it) {
|
2010-03-17 12:22:49 +00:00
|
|
|
|
puts("__Vscope_"+it->second.m_symName+".configure(this,name(),");
|
|
|
|
|
putsQuoted(it->second.m_prettyName);
|
2009-12-05 15:38:49 +00:00
|
|
|
|
puts(");\n");
|
|
|
|
|
}
|
|
|
|
|
|
2009-12-20 13:27:00 +00:00
|
|
|
|
if (v3Global.dpi()) {
|
|
|
|
|
puts("// Setup export functions\n");
|
|
|
|
|
puts("for (int __Vfinal=0; __Vfinal<2; __Vfinal++) {\n");
|
|
|
|
|
for (ScopeFuncs::iterator it = m_scopeFuncs.begin(); it != m_scopeFuncs.end(); ++it) {
|
|
|
|
|
AstScopeName* scopep = it->second.m_scopep;
|
|
|
|
|
AstCFunc* funcp = it->second.m_funcp;
|
|
|
|
|
AstNodeModule* modp = it->second.m_modp;
|
|
|
|
|
if (funcp->dpiExport()) {
|
|
|
|
|
puts("__Vscope_"+scopep->scopeSymName()+".exportInsert(__Vfinal,");
|
|
|
|
|
putsQuoted(funcp->cname());
|
|
|
|
|
puts(", (void*)(&");
|
|
|
|
|
puts(modClassName(modp));
|
|
|
|
|
puts("::");
|
|
|
|
|
puts(funcp->name());
|
|
|
|
|
puts("));\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-03-17 12:22:49 +00:00
|
|
|
|
// It would be less code if each module inserted its own variables.
|
|
|
|
|
// Someday. For now public isn't common.
|
|
|
|
|
for (ScopeVars::iterator it = m_scopeVars.begin(); it != m_scopeVars.end(); ++it) {
|
|
|
|
|
AstNodeModule* modp = it->second.m_modp;
|
|
|
|
|
AstScope* scopep = it->second.m_scopep;
|
|
|
|
|
AstVar* varp = it->second.m_varp;
|
|
|
|
|
//
|
2013-01-15 02:49:22 +00:00
|
|
|
|
int pdim=0;
|
|
|
|
|
int udim=0;
|
2010-03-17 12:22:49 +00:00
|
|
|
|
string bounds;
|
|
|
|
|
if (AstBasicDType* basicp = varp->basicp()) {
|
|
|
|
|
// Range is always first, it's not in "C" order
|
2012-02-20 16:48:31 +00:00
|
|
|
|
if (basicp->isRanged()) {
|
|
|
|
|
bounds += " ,"; bounds += cvtToStr(basicp->msb());
|
|
|
|
|
bounds += ","; bounds += cvtToStr(basicp->lsb());
|
2013-01-15 02:49:22 +00:00
|
|
|
|
pdim++;
|
2010-03-17 12:22:49 +00:00
|
|
|
|
}
|
|
|
|
|
for (AstNodeDType* dtypep=varp->dtypep(); dtypep; ) {
|
|
|
|
|
dtypep = dtypep->skipRefp(); // Skip AstRefDType/AstTypedef, or return same node
|
2013-01-12 21:19:25 +00:00
|
|
|
|
if (AstNodeArrayDType* adtypep = dtypep->castNodeArrayDType()) {
|
2012-04-22 23:18:51 +00:00
|
|
|
|
bounds += " ,"; bounds += cvtToStr(adtypep->msb());
|
|
|
|
|
bounds += ","; bounds += cvtToStr(adtypep->lsb());
|
2013-01-15 02:49:22 +00:00
|
|
|
|
if (dtypep->castPackArrayDType()) pdim++; else udim++;
|
2012-04-14 16:43:03 +00:00
|
|
|
|
dtypep = adtypep->subDTypep();
|
2010-03-17 12:22:49 +00:00
|
|
|
|
}
|
|
|
|
|
else break; // AstBasicDType - nothing below, 1
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//
|
2013-01-15 02:49:22 +00:00
|
|
|
|
if (pdim>1 || udim>1) {
|
|
|
|
|
puts("//UNSUP "); // VerilatedImp can't deal with >2d or packed arrays
|
2010-03-17 12:22:49 +00:00
|
|
|
|
}
|
|
|
|
|
puts("__Vscope_"+it->second.m_scopeName+".varInsert(__Vfinal,");
|
|
|
|
|
putsQuoted(it->second.m_varBasePretty);
|
|
|
|
|
puts(", &(");
|
|
|
|
|
if (modp->isTop()) {
|
|
|
|
|
puts(scopep->nameDotless());
|
|
|
|
|
puts("p->");
|
|
|
|
|
} else {
|
|
|
|
|
puts(scopep->nameDotless());
|
|
|
|
|
puts(".");
|
|
|
|
|
}
|
|
|
|
|
puts(varp->name());
|
|
|
|
|
puts("), ");
|
|
|
|
|
puts(varp->vlEnumType()); // VLVT_UINT32 etc
|
|
|
|
|
puts(",");
|
2010-04-06 00:01:17 +00:00
|
|
|
|
puts(varp->vlEnumDir()); // VLVD_IN etc
|
|
|
|
|
if (varp->isSigUserRWPublic()) puts("|VLVF_PUB_RW");
|
|
|
|
|
else if (varp->isSigUserRdPublic()) puts("|VLVF_PUB_RD");
|
|
|
|
|
puts(",");
|
2013-01-15 02:49:22 +00:00
|
|
|
|
puts(cvtToStr(pdim+udim));
|
2010-03-17 12:22:49 +00:00
|
|
|
|
puts(bounds);
|
|
|
|
|
puts(");\n");
|
|
|
|
|
}
|
2009-12-20 13:27:00 +00:00
|
|
|
|
puts("}\n");
|
|
|
|
|
}
|
|
|
|
|
|
2006-08-26 11:35:28 +00:00
|
|
|
|
puts("}\n");
|
2012-08-27 01:13:47 +00:00
|
|
|
|
|
|
|
|
|
if (v3Global.opt.savable() ) {
|
|
|
|
|
puts("\n");
|
|
|
|
|
for (int de=0; de<2; ++de) {
|
|
|
|
|
string classname = de ? "VerilatedDeserialize" : "VerilatedSerialize";
|
|
|
|
|
string funcname = de ? "__Vdeserialize" : "__Vserialize";
|
|
|
|
|
string op = de ? ">>" : "<<";
|
|
|
|
|
puts("void "+symClassName()+"::"+funcname+"("+classname+"& os) {\n");
|
|
|
|
|
puts( "// LOCAL STATE\n");
|
|
|
|
|
// __Vm_namep presumably already correct
|
|
|
|
|
puts( "os"+op+"__Vm_activity;\n");
|
|
|
|
|
puts( "os"+op+"__Vm_didInit;\n");
|
|
|
|
|
puts( "// SUBCELL STATE\n");
|
|
|
|
|
for (vector<ScopeModPair>::iterator it = m_scopes.begin(); it != m_scopes.end(); ++it) {
|
|
|
|
|
AstScope* scopep = it->first; AstNodeModule* modp = it->second;
|
|
|
|
|
if (!modp->isTop()) {
|
|
|
|
|
puts( scopep->nameDotless()+"."+funcname+"(os);\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
puts("}\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
2006-08-26 11:35:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-12-09 03:12:59 +00:00
|
|
|
|
//######################################################################
|
|
|
|
|
|
|
|
|
|
void EmitCSyms::emitDpiHdr() {
|
|
|
|
|
UINFO(6,__FUNCTION__<<": "<<endl);
|
|
|
|
|
string filename = v3Global.opt.makeDir()+"/"+topClassName()+"__Dpi.h";
|
|
|
|
|
AstCFile* cfilep = newCFile(filename, false/*slow*/, false/*source*/);
|
|
|
|
|
cfilep->support(true);
|
|
|
|
|
V3OutCFile hf (filename);
|
|
|
|
|
m_ofp = &hf;
|
|
|
|
|
|
|
|
|
|
m_ofp->putsHeader();
|
|
|
|
|
puts("// DESCR" "IPTION: Verilator output: Prototypes for DPI import and export functions.\n");
|
|
|
|
|
puts("//\n");
|
|
|
|
|
puts("// Verilator includes this file in all generated .cpp files that use DPI functions.\n");
|
|
|
|
|
puts("// Manually include this file where DPI .c import functions are declared to insure\n");
|
|
|
|
|
puts("// the C functions match the expectations of the DPI imports.\n");
|
|
|
|
|
puts("\n");
|
2011-05-12 10:32:29 +00:00
|
|
|
|
puts("#include \"svdpi.h\"\n");
|
|
|
|
|
puts("\n");
|
2009-12-09 03:12:59 +00:00
|
|
|
|
puts("#ifdef __cplusplus\n");
|
|
|
|
|
puts("extern \"C\" {\n");
|
|
|
|
|
puts("#endif\n");
|
|
|
|
|
puts("\n");
|
2012-03-20 20:01:53 +00:00
|
|
|
|
|
2011-08-05 01:58:45 +00:00
|
|
|
|
int firstExp = 0;
|
|
|
|
|
int firstImp = 0;
|
2009-12-09 03:12:59 +00:00
|
|
|
|
for (vector<AstCFunc*>::iterator it = m_dpis.begin(); it != m_dpis.end(); ++it) {
|
|
|
|
|
AstCFunc* nodep = *it;
|
2009-12-20 13:27:00 +00:00
|
|
|
|
if (nodep->dpiExportWrapper()) {
|
|
|
|
|
if (!firstExp++) puts("\n// DPI EXPORTS\n");
|
|
|
|
|
puts("// DPI Export at "+nodep->fileline()->ascii()+"\n");
|
|
|
|
|
puts("extern "+nodep->rtnTypeVoid()+" "+nodep->name()+" ("+cFuncArgs(nodep)+");\n");
|
|
|
|
|
}
|
|
|
|
|
else if (nodep->dpiImport()) {
|
|
|
|
|
if (!firstImp++) puts("\n// DPI IMPORTS\n");
|
|
|
|
|
puts("// DPI Import at "+nodep->fileline()->ascii()+"\n");
|
2009-12-09 03:12:59 +00:00
|
|
|
|
puts("extern "+nodep->rtnTypeVoid()+" "+nodep->name()+" ("+cFuncArgs(nodep)+");\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
puts("\n");
|
|
|
|
|
puts("#ifdef __cplusplus\n");
|
|
|
|
|
puts("}\n");
|
|
|
|
|
puts("#endif\n");
|
|
|
|
|
}
|
|
|
|
|
|
2009-12-20 13:27:00 +00:00
|
|
|
|
//######################################################################
|
|
|
|
|
|
|
|
|
|
void EmitCSyms::emitDpiImp() {
|
|
|
|
|
UINFO(6,__FUNCTION__<<": "<<endl);
|
|
|
|
|
string filename = v3Global.opt.makeDir()+"/"+topClassName()+"__Dpi.cpp";
|
|
|
|
|
AstCFile* cfilep = newCFile(filename, false/*slow*/, true/*source*/);
|
|
|
|
|
cfilep->support(true);
|
|
|
|
|
V3OutCFile hf (filename);
|
|
|
|
|
m_ofp = &hf;
|
|
|
|
|
|
|
|
|
|
m_ofp->putsHeader();
|
|
|
|
|
puts("// DESCR" "IPTION: Verilator output: Implementation of DPI export functions.\n");
|
|
|
|
|
puts("//\n");
|
|
|
|
|
puts("// Verilator compiles this file in when DPI functions are used.\n");
|
|
|
|
|
puts("// If you have multiple Verilated designs with the same DPI exported\n");
|
|
|
|
|
puts("// function names, you will get multiple definition link errors from here.\n");
|
|
|
|
|
puts("// This is an unfortunate result of the DPI specification.\n");
|
|
|
|
|
puts("// To solve this, either\n");
|
|
|
|
|
puts("// 1. Call "+topClassName()+"::{export_function} instead,\n");
|
|
|
|
|
puts("// and do not even bother to compile this file\n");
|
|
|
|
|
puts("// or 2. Compile all __Dpi.cpp files in the same compiler run,\n");
|
|
|
|
|
puts("// and #ifdefs already inserted here will sort everything out.\n");
|
|
|
|
|
puts("\n");
|
2012-03-20 20:01:53 +00:00
|
|
|
|
|
2009-12-20 13:27:00 +00:00
|
|
|
|
puts("#include \""+topClassName()+"__Dpi.h\"\n");
|
|
|
|
|
puts("#include \""+topClassName()+".h\"\n");
|
|
|
|
|
puts("\n");
|
|
|
|
|
|
|
|
|
|
for (vector<AstCFunc*>::iterator it = m_dpis.begin(); it != m_dpis.end(); ++it) {
|
|
|
|
|
AstCFunc* nodep = *it;
|
|
|
|
|
if (nodep->dpiExportWrapper()) {
|
|
|
|
|
puts("#ifndef _VL_DPIDECL_"+nodep->name()+"\n");
|
|
|
|
|
puts("#define _VL_DPIDECL_"+nodep->name()+"\n");
|
|
|
|
|
puts(nodep->rtnTypeVoid()+" "+nodep->name()+" ("+cFuncArgs(nodep)+") {\n");
|
|
|
|
|
puts("// DPI Export at "+nodep->fileline()->ascii()+"\n");
|
|
|
|
|
puts("return "+topClassName()+"::"+nodep->name()+"(");
|
|
|
|
|
string args;
|
|
|
|
|
for (AstNode* stmtp = nodep->argsp(); stmtp; stmtp=stmtp->nextp()) {
|
|
|
|
|
if (AstVar* portp = stmtp->castVar()) {
|
|
|
|
|
if (portp->isIO() && !portp->isFuncReturn()) {
|
|
|
|
|
if (args != "") args+= ", ";
|
|
|
|
|
args += portp->name();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
puts(args+");\n");
|
|
|
|
|
puts("}\n");
|
|
|
|
|
puts("#endif\n");
|
|
|
|
|
puts("\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2006-08-26 11:35:28 +00:00
|
|
|
|
//######################################################################
|
|
|
|
|
// EmitC class functions
|
|
|
|
|
|
|
|
|
|
void V3EmitC::emitcSyms() {
|
|
|
|
|
UINFO(2,__FUNCTION__<<": "<<endl);
|
|
|
|
|
EmitCSyms syms (v3Global.rootp());
|
|
|
|
|
}
|