From 820df28ad910fa12f84740c157e35a4c32cba69d Mon Sep 17 00:00:00 2001 From: Wilson Snyder Date: Sat, 1 Feb 2020 19:11:19 -0500 Subject: [PATCH] Internals: V3CUse state refactoring. No functional change intended. --- src/V3CUse.cpp | 59 ++++++++++++++++++++++++++++++----------------- src/Verilator.cpp | 6 ++--- 2 files changed, 41 insertions(+), 24 deletions(-) diff --git a/src/V3CUse.cpp b/src/V3CUse.cpp index a79525efe..e19a77d24 100644 --- a/src/V3CUse.cpp +++ b/src/V3CUse.cpp @@ -19,8 +19,9 @@ //************************************************************************* // V3Class's Transformations: // -// Each class: -// Create string access functions +// Each module: +// Each cell: +// Create CUse for cell forward declaration // //************************************************************************* @@ -36,7 +37,7 @@ //###################################################################### -class CUseVisitor : public AstNVisitor { +class CUseState { private: // MEMBERS AstNodeModule* m_modInsertp; // Current module to insert AstCUse under @@ -45,12 +46,13 @@ private: // NODE STATE // 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; // METHODS VL_DEBUG_FUNC; // Declare debug() +public: AstCUse* newUse(AstNode* nodep, VUseType useType, const string& name) { UseString key(useType, name); if (m_didUse.find(key) == m_didUse.end()) { @@ -61,39 +63,48 @@ private: } 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) { for (AstNode* itemp = nodep->stmtsp(); itemp; itemp = itemp->nextp()) { if (AstCell* cellp = VN_CAST(itemp, Cell)) { // 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 { 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); } makeUseCells(nodep); } - virtual void visit(AstNodeMath* nodep) VL_OVERRIDE {} // Short circuit - virtual void visit(AstNodeStmt* nodep) VL_OVERRIDE {} // Short circuit - virtual void visit(AstNode* nodep) VL_OVERRIDE { iterateChildren(nodep); } + virtual void visit(AstNode* nodep) VL_OVERRIDE {} // All in AstNodeModule public: // CONSTRUCTORS - explicit CUseVisitor(AstNetlist* nodep) - : m_modInsertp(NULL) { - 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); - m_modInsertp = NULL; - } + explicit CUseVisitor(AstNodeModule* nodep) + : m_state(nodep) { + iterate(nodep); } virtual ~CUseVisitor() {} VL_UNCOPYABLE(CUseVisitor); @@ -104,6 +115,12 @@ public: void V3CUse::cUseAll(AstNetlist* nodep) { 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); } diff --git a/src/Verilator.cpp b/src/Verilator.cpp index 65b6a1218..0a89f2f0e 100644 --- a/src/Verilator.cpp +++ b/src/Verilator.cpp @@ -374,9 +374,6 @@ static void process() { //--MODULE OPTIMIZATIONS-------------- 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. if (!v3Global.opt.lintOnly() && v3Global.opt.compLimitBlocks()) { V3DepthBlock::depthBlockAll(v3Global.rootp()); @@ -468,6 +465,9 @@ static void process() { if (!v3Global.opt.lintOnly() && !v3Global.opt.xmlOnly() && !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 V3EmitC::emitcInlines(); V3EmitC::emitcSyms();