From 1d063642840c45de2ccb47e56e9e0d2a84f044cc Mon Sep 17 00:00:00 2001 From: Krzysztof Starecki Date: Tue, 12 Nov 2024 17:28:39 +0100 Subject: [PATCH] Support vpiDefName (#5572) --- docs/CONTRIBUTORS | 1 + include/verilated.cpp | 3 ++- include/verilated.h | 5 ++++- include/verilated_vpi.cpp | 3 +++ src/V3EmitCSyms.cpp | 31 +++++++++++++++++++------------ test_regress/t/t_vpi_escape.cpp | 2 +- test_regress/t/t_vpi_var.cpp | 2 +- 7 files changed, 31 insertions(+), 16 deletions(-) diff --git a/docs/CONTRIBUTORS b/docs/CONTRIBUTORS index dae6f4082..f3913b510 100644 --- a/docs/CONTRIBUTORS +++ b/docs/CONTRIBUTORS @@ -126,6 +126,7 @@ Krzysztof Bieganski Krzysztof Boronski Krzysztof Boroński Krzysztof Obłonczek +Krzysztof Starecki Kuba Ober Larry Doolittle Liam Braun diff --git a/include/verilated.cpp b/include/verilated.cpp index 9bb99166c..a4968ccd7 100644 --- a/include/verilated.cpp +++ b/include/verilated.cpp @@ -3326,7 +3326,7 @@ VerilatedScope::~VerilatedScope() { } void VerilatedScope::configure(VerilatedSyms* symsp, const char* prefixp, const char* suffixp, - const char* identifier, int8_t timeunit, + const char* identifier, const char* defnamep, int8_t timeunit, const Type& type) VL_MT_UNSAFE { // Slowpath - called once/scope at construction // We don't want the space and reference-count access overhead of strings. @@ -3343,6 +3343,7 @@ void VerilatedScope::configure(VerilatedSyms* symsp, const char* prefixp, const m_namep = namep; } m_identifierp = identifier; + m_defnamep = defnamep; Verilated::threadContextp()->impp()->scopeInsert(this); } diff --git a/include/verilated.h b/include/verilated.h index 65ff30c95..aec79542b 100644 --- a/include/verilated.h +++ b/include/verilated.h @@ -714,6 +714,7 @@ private: VerilatedVarNameMap* m_varsp = nullptr; // Variable map const char* m_namep = nullptr; // Scope name (Slowpath) const char* m_identifierp = nullptr; // Identifier of scope (with escapes removed) + const char* m_defnamep = nullptr; // Definition name (SCOPE_MODULE only) int8_t m_timeunit = 0; // Timeunit in negative power-of-10 Type m_type = SCOPE_OTHER; // Type of the scope @@ -721,13 +722,15 @@ public: // But internals only - called from VerilatedModule's VerilatedScope() = default; ~VerilatedScope(); void configure(VerilatedSyms* symsp, const char* prefixp, const char* suffixp, - const char* identifier, int8_t timeunit, const Type& type) VL_MT_UNSAFE; + const char* identifier, const char* defnamep, int8_t timeunit, + const Type& type) VL_MT_UNSAFE; void exportInsert(int finalize, const char* namep, void* cb) VL_MT_UNSAFE; void varInsert(int finalize, const char* namep, void* datap, bool isParam, VerilatedVarType vltype, int vlflags, int dims, ...) VL_MT_UNSAFE; // ACCESSORS const char* name() const VL_MT_SAFE_POSTINIT { return m_namep; } const char* identifier() const VL_MT_SAFE_POSTINIT { return m_identifierp; } + const char* defname() const VL_MT_SAFE_POSTINIT { return m_defnamep; } int8_t timeunit() const VL_MT_SAFE_POSTINIT { return m_timeunit; } VerilatedSyms* symsp() const VL_MT_SAFE_POSTINIT { return m_symsp; } VerilatedVar* varFind(const char* namep) const VL_MT_SAFE_POSTINIT; diff --git a/include/verilated_vpi.cpp b/include/verilated_vpi.cpp index c066ae8a7..2f8556530 100644 --- a/include/verilated_vpi.cpp +++ b/include/verilated_vpi.cpp @@ -268,6 +268,7 @@ protected: bool m_toplevel = false; const char* m_name; const char* m_fullname; + const char* m_defname; public: explicit VerilatedVpioScope(const VerilatedScope* scopep) @@ -275,6 +276,7 @@ public: m_fullname = m_scopep->name(); if (std::strncmp(m_fullname, "TOP.", 4) == 0) m_fullname += 4; m_name = m_scopep->identifier(); + m_defname = m_scopep->defname(); } ~VerilatedVpioScope() override = default; static VerilatedVpioScope* castp(vpiHandle h) { @@ -284,6 +286,7 @@ public: const VerilatedScope* scopep() const { return m_scopep; } const char* name() const override { return m_name; } const char* fullname() const override { return m_fullname; } + const char* defname() const override { return m_defname; } bool toplevel() const { return m_toplevel; } }; diff --git a/src/V3EmitCSyms.cpp b/src/V3EmitCSyms.cpp index e2e020f7f..633d2ba4a 100644 --- a/src/V3EmitCSyms.cpp +++ b/src/V3EmitCSyms.cpp @@ -43,13 +43,15 @@ class EmitCSyms final : EmitCBaseVisitorConst { const AstNode* m_nodep; const string m_symName; const string m_prettyName; + const string m_defName; const int m_timeunit; string m_type; ScopeData(const AstNode* nodep, const string& symName, const string& prettyName, - int timeunit, const string& type) + const string& defName, int timeunit, const string& type) : m_nodep{nodep} , m_symName{symName} , m_prettyName{prettyName} + , m_defName{defName} , m_timeunit{timeunit} , m_type{type} {} }; @@ -242,8 +244,8 @@ class EmitCSyms final : EmitCBaseVisitorConst { if (v3Global.opt.vpi()) varHierarchyScopes(scpName); if (m_scopeNames.find(scpSym) == m_scopeNames.end()) { // cppcheck-suppress stlFindInsert - m_scopeNames.emplace(scpSym, - ScopeData{varp, scpSym, scpPretty, 0, "SCOPE_OTHER"}); + m_scopeNames.emplace(scpSym, ScopeData{varp, scpSym, scpPretty, "", + 0, "SCOPE_OTHER"}); } m_scopeVars.emplace(scpSym + " " + varp->name(), ScopeVarData{scpSym, varBasePretty, varp, modp, scopep}); @@ -312,7 +314,9 @@ class EmitCSyms final : EmitCBaseVisitorConst { const int timeunit = m_modp->timeunit().powerOfTen(); m_vpiScopeCandidates.emplace( scopeSymString(name), - ScopeData{nodep, scopeSymString(name), name_pretty, timeunit, type}); + ScopeData{nodep, scopeSymString(name), name_pretty, + type == "SCOPE_MODULE" ? nodep->origModName() : "", timeunit, + type}); } } void visit(AstScope* nodep) override { @@ -325,9 +329,9 @@ class EmitCSyms final : EmitCBaseVisitorConst { const string type = VN_IS(nodep->modp(), Package) ? "SCOPE_PACKAGE" : "SCOPE_MODULE"; const string name_pretty = AstNode::vpiName(nodep->shortName()); const int timeunit = m_modp->timeunit().powerOfTen(); - m_vpiScopeCandidates.emplace( - scopeSymString(nodep->name()), - ScopeData{nodep, scopeSymString(nodep->name()), name_pretty, timeunit, type}); + m_vpiScopeCandidates.emplace(scopeSymString(nodep->name()), + ScopeData{nodep, scopeSymString(nodep->name()), + name_pretty, "", timeunit, type}); } iterateChildrenConst(nodep); } @@ -336,8 +340,8 @@ class EmitCSyms final : EmitCBaseVisitorConst { // UINFO(9, "scnameins sp " << nodep->name() << " sp " << nodep->scopePrettySymName() // << " ss" << name << endl); const int timeunit = m_modp ? m_modp->timeunit().powerOfTen() : 0; - m_scopeNames.emplace( - name, ScopeData{nodep, name, nodep->scopePrettySymName(), timeunit, "SCOPE_OTHER"}); + m_scopeNames.emplace(name, ScopeData{nodep, name, nodep->scopePrettySymName(), "", + timeunit, "SCOPE_OTHER"}); if (nodep->dpiExport()) { UASSERT_OBJ(m_cfuncp, nodep, "ScopeName not under DPI function"); m_scopeFuncs.emplace(name + " " + m_cfuncp->name(), @@ -345,9 +349,10 @@ class EmitCSyms final : EmitCBaseVisitorConst { } else { if (m_scopeNames.find(nodep->scopeDpiName()) == m_scopeNames.end()) { // cppcheck-suppress stlFindInsert - m_scopeNames.emplace(nodep->scopeDpiName(), ScopeData{nodep, nodep->scopeDpiName(), - nodep->scopePrettyDpiName(), - timeunit, "SCOPE_OTHER"}); + m_scopeNames.emplace(nodep->scopeDpiName(), + ScopeData{nodep, nodep->scopeDpiName(), + nodep->scopePrettyDpiName(), "", timeunit, + "SCOPE_OTHER"}); } } } @@ -885,6 +890,8 @@ void EmitCSyms::emitSymImp() { puts(", "); putsQuoted(protect(scopeDecodeIdentifier(it->second.m_prettyName))); puts(", "); + putsQuoted(it->second.m_defName); + puts(", "); puts(cvtToStr(it->second.m_timeunit)); puts(", VerilatedScope::" + it->second.m_type + ");\n"); ++m_numStmts; diff --git a/test_regress/t/t_vpi_escape.cpp b/test_regress/t/t_vpi_escape.cpp index 7fed0d2e6..73b7875fd 100644 --- a/test_regress/t/t_vpi_escape.cpp +++ b/test_regress/t/t_vpi_escape.cpp @@ -132,7 +132,7 @@ int _mon_check_iter() { TEST_CHECK_CSTR(p, "\\mod.with_dot "); if (TestSimulator::is_verilator()) { p = vpi_get_str(vpiDefName, vh2); - TEST_CHECK_CSTR(p, ""); // Unsupported + TEST_CHECK_CSTR(p, "sub"); } TestVpiHandle vh_null_name = MY_VPI_HANDLE("___0_"); diff --git a/test_regress/t/t_vpi_var.cpp b/test_regress/t/t_vpi_var.cpp index 7ed632ecc..82c98783d 100644 --- a/test_regress/t/t_vpi_var.cpp +++ b/test_regress/t/t_vpi_var.cpp @@ -443,7 +443,7 @@ int _mon_check_varlist() { CHECK_RESULT_CSTR(p, "sub"); if (TestSimulator::is_verilator()) { p = vpi_get_str(vpiDefName, vh2); - CHECK_RESULT_CSTR(p, ""); // Unsupported + CHECK_RESULT_CSTR(p, "sub"); } TestVpiHandle vh10 = vpi_iterate(vpiReg, vh2);