From 119162912af9459654b450574eb16a4fc7e8dafb Mon Sep 17 00:00:00 2001 From: Wilson Snyder Date: Sat, 1 Feb 2020 16:45:11 -0500 Subject: [PATCH] Internals: Determine emit forward declaraions in new pass, towards classes. --- src/Makefile_obj.in | 1 + src/V3Ast.h | 30 ++++++++++++ src/V3AstNodes.cpp | 4 ++ src/V3AstNodes.h | 18 ++++++++ src/V3CUse.cpp | 109 ++++++++++++++++++++++++++++++++++++++++++++ src/V3CUse.h | 37 +++++++++++++++ src/V3EmitC.cpp | 40 ++++++++++------ src/Verilator.cpp | 4 ++ 8 files changed, 229 insertions(+), 14 deletions(-) create mode 100644 src/V3CUse.cpp create mode 100644 src/V3CUse.h diff --git a/src/Makefile_obj.in b/src/Makefile_obj.in index e9274cb1b..f847288bb 100644 --- a/src/Makefile_obj.in +++ b/src/Makefile_obj.in @@ -166,6 +166,7 @@ RAW_OBJS = \ V3Branch.o \ V3Broken.o \ V3CCtors.o \ + V3CUse.o \ V3Case.o \ V3Cast.o \ V3Cdc.o \ diff --git a/src/V3Ast.h b/src/V3Ast.h index 207b3e11e..c3c9cca4e 100644 --- a/src/V3Ast.h +++ b/src/V3Ast.h @@ -852,6 +852,36 @@ inline std::ostream& operator<<(std::ostream& os, const VNumRange& rhs) { rhs.du //###################################################################### +class VUseType { +public: + enum en { + IMP_INCLUDE, // Implementation (.cpp) needs an include + INT_INCLUDE, // Interface (.h) needs an include + IMP_FWD_CLASS, // Implementation (.cpp) needs a forward class declaration + INT_FWD_CLASS, // Interface (.h) needs a forward class declaration + }; + enum en m_e; + inline VUseType() : m_e(IMP_FWD_CLASS) {} + // cppcheck-suppress noExplicitConstructor + inline VUseType(en _e) : m_e(_e) {} + explicit inline VUseType(int _e) : m_e(static_cast(_e)) {} + bool isInclude() const { return m_e == IMP_INCLUDE || m_e == INT_INCLUDE; } + bool isFwdClass() const { return m_e == IMP_FWD_CLASS || m_e == INT_FWD_CLASS; } + operator en() const { return m_e; } + const char* ascii() const { + static const char* const names[] = {"IMP_INC", "INT_INC", "IMP_FWD", "INT_FWD"}; + return names[m_e]; + } +}; +inline bool operator==(VUseType lhs, VUseType rhs) { return (lhs.m_e == rhs.m_e); } +inline bool operator==(VUseType lhs, VUseType::en rhs) { return (lhs.m_e == rhs); } +inline bool operator==(VUseType::en lhs, VUseType rhs) { return (lhs == rhs.m_e); } +inline std::ostream& operator<<(std::ostream& os, const VUseType& rhs) { + return os << rhs.ascii(); +} + +//###################################################################### + class VBasicTypeKey { public: int m_width; // From AstNodeDType: Bit width of operation diff --git a/src/V3AstNodes.cpp b/src/V3AstNodes.cpp index fa85dd516..b8880c5df 100644 --- a/src/V3AstNodes.cpp +++ b/src/V3AstNodes.cpp @@ -1341,3 +1341,7 @@ void AstCFunc::dump(std::ostream& str) const { if (dpiExport()) str<<" [DPIX]"; if (dpiExportWrapper()) str<<" [DPIXWR]"; } +void AstCUse::dump(std::ostream& str) const { + this->AstNode::dump(str); + str << " [" << useType() << "]"; +} diff --git a/src/V3AstNodes.h b/src/V3AstNodes.h index f8b74b796..79dddf125 100644 --- a/src/V3AstNodes.h +++ b/src/V3AstNodes.h @@ -6881,6 +6881,24 @@ public: AstNode* bodysp() const { return op1p(); } // op1 = expressions to print }; +class AstCUse : public AstNode { + // C++ use of a class or #include; indicates need of forward declaration + // Parents: NODEMODULE +private: + VUseType m_useType; // What sort of use this is + string m_name; +public: + AstCUse(FileLine* fl, VUseType useType, const string& name) + : ASTGEN_SUPER(fl) + , m_useType(useType) + , m_name(name) {} + ASTNODE_NODE_FUNCS(CUse) + virtual string name() const { return m_name; } + virtual void dump(std::ostream& str = std::cout) const; + VUseType useType() const { return m_useType; } + void useType(VUseType useType) { m_useType = useType; } +}; + class AstMTaskBody : public AstNode { // Hold statements for each MTask private: diff --git a/src/V3CUse.cpp b/src/V3CUse.cpp new file mode 100644 index 000000000..a79525efe --- /dev/null +++ b/src/V3CUse.cpp @@ -0,0 +1,109 @@ +// -*- mode: C++; c-file-style: "cc-mode" -*- +//************************************************************************* +// DESCRIPTION: Verilator: Handle SV classes +// +// Code available from: https://verilator.org +// +//************************************************************************* +// +// Copyright 2003-2020 by Wilson Snyder. This program is free software; you can +// redistribute it and/or modify it under the terms of either the GNU +// Lesser General Public License Version 3 or the Perl Artistic License +// Version 2.0. +// +// 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. +// +//************************************************************************* +// V3Class's Transformations: +// +// Each class: +// Create string access functions +// +//************************************************************************* + +#include "config_build.h" +#include "verilatedos.h" + +#include "V3Global.h" +#include "V3CUse.h" +#include "V3Ast.h" +#include "V3EmitCBase.h" + +#include VL_INCLUDE_UNORDERED_MAP + +//###################################################################### + +class CUseVisitor : public AstNVisitor { +private: + // MEMBERS + AstNodeModule* m_modInsertp; // Current module to insert AstCUse under + typedef std::pair UseString; + std::map m_didUse; // What we already used + + // NODE STATE + // Entire netlist: + // AstClass::user() -> bool. True if class needs to_string dumper + AstUser1InUse m_inuser1; + + // METHODS + VL_DEBUG_FUNC; // Declare debug() + + AstCUse* newUse(AstNode* nodep, VUseType useType, const string& name) { + UseString key(useType, name); + if (m_didUse.find(key) == m_didUse.end()) { + AstCUse* newp = new AstCUse(nodep->fileline(), useType, name); + m_modInsertp->addStmtp(newp); + UINFO(8, "Insert " << newp << endl); + m_didUse[key] = newp; + } + return m_didUse[key]; + } + 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()); + } + } + } + + virtual void visit(AstNodeModule* nodep) VL_OVERRIDE { + if (v3Global.opt.trace()) { + AstCUse* usep = 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); } + +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; + } + } + virtual ~CUseVisitor() {} + VL_UNCOPYABLE(CUseVisitor); +}; + +//###################################################################### +// Class class functions + +void V3CUse::cUseAll(AstNetlist* nodep) { + UINFO(2, __FUNCTION__ << ": " << endl); + { CUseVisitor visitor(nodep); } // Destruct before checking + V3Global::dumpCheckGlobalTree("cuse", 0, v3Global.opt.dumpTreeLevel(__FILE__) >= 3); +} diff --git a/src/V3CUse.h b/src/V3CUse.h new file mode 100644 index 000000000..c79fc7ac4 --- /dev/null +++ b/src/V3CUse.h @@ -0,0 +1,37 @@ +// -*- mode: C++; c-file-style: "cc-mode" -*- +//************************************************************************* +// DESCRIPTION: Verilator: Pre C-Emit stage changes +// +// Code available from: https://verilator.org +// +//************************************************************************* +// +// Copyright 2003-2020 by Wilson Snyder. This program is free software; you can +// redistribute it and/or modify it under the terms of either the GNU +// Lesser General Public License Version 3 or the Perl Artistic License +// Version 2.0. +// +// 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 _V3CUSE_H_ +#define _V3CUSE_H_ 1 + +#include "config_build.h" +#include "verilatedos.h" + +#include "V3Error.h" +#include "V3Ast.h" + +//============================================================================ + +class V3CUse { +public: + static void cUseAll(AstNetlist* nodep); +}; + +#endif // Guard diff --git a/src/V3EmitC.cpp b/src/V3EmitC.cpp index b05c781b0..12d20fc49 100644 --- a/src/V3EmitC.cpp +++ b/src/V3EmitC.cpp @@ -984,6 +984,7 @@ public: virtual void visit(AstTraceInc*) VL_OVERRIDE {} // Handled outside the Visit class virtual void visit(AstCFile*) VL_OVERRIDE {} // Handled outside the Visit class virtual void visit(AstCellInline*) VL_OVERRIDE {} // Handled outside the Visit class (EmitCSyms) + virtual void visit(AstCUse*) VL_OVERRIDE {} // Handled outside the Visit class // Default virtual void visit(AstNode* nodep) VL_OVERRIDE { puts(string("\n???? // ")+nodep->prettyTypeName()+"\n"); @@ -1384,6 +1385,24 @@ class EmitCImp : EmitCStmts { // METHODS // Low level + void emitModCUse(AstNodeModule* modp, VUseType useType) { + string nl; + for (AstNode* itemp = modp->stmtsp(); itemp; itemp = itemp->nextp()) { + if (AstCUse* usep = VN_CAST(itemp, CUse)) { + if (usep->useType() == useType) { + if (usep->useType().isInclude()) { + puts("#include \"" + prefixNameProtect(usep) + ".h\"\n"); + } + if (usep->useType().isFwdClass()) { + puts("class " + prefixNameProtect(usep) + ";\n"); + } + nl = "\n"; + } + } + } + puts(nl); + } + void emitVarReset(AstVar* varp) { AstNodeDType* dtypep = varp->dtypep()->skipRefp(); if (varp->isIO() && m_modp->isTop() && optSystemC()) { @@ -2583,21 +2602,11 @@ void EmitCImp::emitInt(AstNodeModule* modp) { } puts("\n"); + emitModCUse(modp, VUseType::INT_INCLUDE); + // Declare foreign instances up front to make C++ happy - puts("class "+symClassName()+";\n"); - vl_unordered_set didClassName; - for (AstNode* nodep = modp->stmtsp(); nodep; nodep = nodep->nextp()) { - if (AstCell* cellp = VN_CAST(nodep, Cell)) { - string className = prefixNameProtect(cellp->modp()); - if (didClassName.find(className) == didClassName.end()) { - puts("class " + className + ";\n"); - didClassName.insert(className); - } - } - } - if (v3Global.opt.trace()) { - puts("class "+v3Global.opt.traceClassBase()+";\n"); - } + puts("class " + symClassName() + ";\n"); + emitModCUse(modp, VUseType::INT_FWD_CLASS); puts("\n//----------\n\n"); emitTextSection(AstType::atScHdr); @@ -2794,6 +2803,9 @@ void EmitCImp::emitImpTop(AstNodeModule* fileModp) { puts("#include \"verilated_dpi.h\"\n"); } + emitModCUse(fileModp, VUseType::IMP_INCLUDE); + emitModCUse(fileModp, VUseType::IMP_FWD_CLASS); + puts("\n"); emitTextSection(AstType::atScImpHdr); } diff --git a/src/Verilator.cpp b/src/Verilator.cpp index 4fc513b41..65b6a1218 100644 --- a/src/Verilator.cpp +++ b/src/Verilator.cpp @@ -37,6 +37,7 @@ #include "V3Coverage.h" #include "V3CoverageJoin.h" #include "V3CCtors.h" +#include "V3CUse.h" #include "V3Dead.h" #include "V3Delayed.h" #include "V3Depth.h" @@ -373,6 +374,9 @@ 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());