Internals: V3CUse state refactoring. No functional change intended.

This commit is contained in:
Wilson Snyder 2020-02-01 19:11:19 -05:00
parent 50fb7fc8b4
commit 820df28ad9
2 changed files with 41 additions and 24 deletions

View File

@ -19,8 +19,9 @@
//************************************************************************* //*************************************************************************
// V3Class's Transformations: // V3Class's Transformations:
// //
// Each class: // Each module:
// Create string access functions // Each cell:
// Create CUse for cell forward declaration
// //
//************************************************************************* //*************************************************************************
@ -36,7 +37,7 @@
//###################################################################### //######################################################################
class CUseVisitor : public AstNVisitor { class CUseState {
private: private:
// MEMBERS // MEMBERS
AstNodeModule* m_modInsertp; // Current module to insert AstCUse under AstNodeModule* m_modInsertp; // Current module to insert AstCUse under
@ -45,12 +46,13 @@ private:
// NODE STATE // NODE STATE
// Entire netlist: // Entire netlist:
// AstClass::user() -> bool. True if class needs to_string dumper // AstClass::user1() -> bool. True if class needs to_string dumper
AstUser1InUse m_inuser1; AstUser1InUse m_inuser1;
// METHODS // METHODS
VL_DEBUG_FUNC; // Declare debug() VL_DEBUG_FUNC; // Declare debug()
public:
AstCUse* newUse(AstNode* nodep, VUseType useType, const string& name) { AstCUse* newUse(AstNode* nodep, VUseType useType, const string& name) {
UseString key(useType, name); UseString key(useType, name);
if (m_didUse.find(key) == m_didUse.end()) { if (m_didUse.find(key) == m_didUse.end()) {
@ -61,39 +63,48 @@ private:
} }
return m_didUse[key]; return m_didUse[key];
} }
// CONSTRUCTORS
explicit CUseState(AstNodeModule* nodep)
: m_modInsertp(nodep) {}
virtual ~CUseState() {}
VL_UNCOPYABLE(CUseState);
};
class CUseVisitor : public AstNVisitor {
// MEMBERS
CUseState m_state; // Inserter state
// METHODS
VL_DEBUG_FUNC; // Declare debug()
// Module use builders
void makeUseCells(AstNodeModule* nodep) { void makeUseCells(AstNodeModule* nodep) {
for (AstNode* itemp = nodep->stmtsp(); itemp; itemp = itemp->nextp()) { for (AstNode* itemp = nodep->stmtsp(); itemp; itemp = itemp->nextp()) {
if (AstCell* cellp = VN_CAST(itemp, Cell)) { if (AstCell* cellp = VN_CAST(itemp, Cell)) {
// Currently no include because we include __Syms which has them all // Currently no include because we include __Syms which has them all
AstCUse* usep = newUse(nodep, VUseType::INT_FWD_CLASS, cellp->modp()->name()); AstCUse* usep
= m_state.newUse(nodep, VUseType::INT_FWD_CLASS, cellp->modp()->name());
} }
} }
} }
// VISITORS
virtual void visit(AstNodeModule* nodep) VL_OVERRIDE { virtual void visit(AstNodeModule* nodep) VL_OVERRIDE {
if (v3Global.opt.trace()) { if (v3Global.opt.trace()) {
AstCUse* usep = newUse(nodep, VUseType::INT_FWD_CLASS, v3Global.opt.traceClassBase()); AstCUse* usep
= m_state.newUse(nodep, VUseType::INT_FWD_CLASS, v3Global.opt.traceClassBase());
usep->protect(false); usep->protect(false);
} }
makeUseCells(nodep); makeUseCells(nodep);
} }
virtual void visit(AstNodeMath* nodep) VL_OVERRIDE {} // Short circuit virtual void visit(AstNode* nodep) VL_OVERRIDE {} // All in AstNodeModule
virtual void visit(AstNodeStmt* nodep) VL_OVERRIDE {} // Short circuit
virtual void visit(AstNode* nodep) VL_OVERRIDE { iterateChildren(nodep); }
public: public:
// CONSTRUCTORS // CONSTRUCTORS
explicit CUseVisitor(AstNetlist* nodep) explicit CUseVisitor(AstNodeModule* nodep)
: m_modInsertp(NULL) { : m_state(nodep) {
for (AstNodeModule* nodep = v3Global.rootp()->modulesp(); nodep;
nodep = VN_CAST(nodep->nextp(), NodeModule)) {
// Insert under this module; someday we should e.g. make Ast
// for each output file and put under that
m_modInsertp = nodep;
m_didUse.clear();
iterate(nodep); iterate(nodep);
m_modInsertp = NULL;
}
} }
virtual ~CUseVisitor() {} virtual ~CUseVisitor() {}
VL_UNCOPYABLE(CUseVisitor); VL_UNCOPYABLE(CUseVisitor);
@ -104,6 +115,12 @@ public:
void V3CUse::cUseAll(AstNetlist* nodep) { void V3CUse::cUseAll(AstNetlist* nodep) {
UINFO(2, __FUNCTION__ << ": " << endl); UINFO(2, __FUNCTION__ << ": " << endl);
{ CUseVisitor visitor(nodep); } // Destruct before checking // Call visitor separately for each module, so visitor state is cleared
for (AstNodeModule* nodep = v3Global.rootp()->modulesp(); nodep;
nodep = VN_CAST(nodep->nextp(), NodeModule)) {
// Insert under this module; someday we should e.g. make Ast
// for each output file and put under that
CUseVisitor visitor(nodep);
}
V3Global::dumpCheckGlobalTree("cuse", 0, v3Global.opt.dumpTreeLevel(__FILE__) >= 3); V3Global::dumpCheckGlobalTree("cuse", 0, v3Global.opt.dumpTreeLevel(__FILE__) >= 3);
} }

View File

@ -374,9 +374,6 @@ static void process() {
//--MODULE OPTIMIZATIONS-------------- //--MODULE OPTIMIZATIONS--------------
if (!v3Global.opt.xmlOnly()) { if (!v3Global.opt.xmlOnly()) {
// Create AstCUse to determine what class forward declarations/#includes needed in C
V3CUse::cUseAll(v3Global.rootp());
// Split deep blocks to appease MSVC++. Must be before Localize. // Split deep blocks to appease MSVC++. Must be before Localize.
if (!v3Global.opt.lintOnly() && v3Global.opt.compLimitBlocks()) { if (!v3Global.opt.lintOnly() && v3Global.opt.compLimitBlocks()) {
V3DepthBlock::depthBlockAll(v3Global.rootp()); V3DepthBlock::depthBlockAll(v3Global.rootp());
@ -468,6 +465,9 @@ static void process() {
if (!v3Global.opt.lintOnly() if (!v3Global.opt.lintOnly()
&& !v3Global.opt.xmlOnly() && !v3Global.opt.xmlOnly()
&& !v3Global.opt.dpiHdrOnly()) { && !v3Global.opt.dpiHdrOnly()) {
// Create AstCUse to determine what class forward declarations/#includes needed in C
V3CUse::cUseAll(v3Global.rootp());
// emitcInlines is first, as it may set needHInlines which other emitters read // emitcInlines is first, as it may set needHInlines which other emitters read
V3EmitC::emitcInlines(); V3EmitC::emitcInlines();
V3EmitC::emitcSyms(); V3EmitC::emitcSyms();