From e7adae6f8162f13537a6fdbe14f3d42de3e67fde Mon Sep 17 00:00:00 2001 From: Wilson Snyder Date: Sat, 26 Apr 2025 09:51:02 -0400 Subject: [PATCH] Internals: Cleanup some vlSelf code. No functional change intended. --- src/V3Ast.cpp | 8 +++++--- src/V3Ast.h | 1 + src/V3AstNodeOther.h | 6 ++++-- src/V3EmitCFunc.h | 10 ++++------ 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/V3Ast.cpp b/src/V3Ast.cpp index 46a4b1ca3..2fa02f3ff 100644 --- a/src/V3Ast.cpp +++ b/src/V3Ast.cpp @@ -64,10 +64,12 @@ std::ostream& operator<<(std::ostream& os, VNType rhs); const std::shared_ptr VSelfPointerText::s_emptyp = std::make_shared(""); const std::shared_ptr VSelfPointerText::s_thisp = std::make_shared("this"); +string VSelfPointerText::replaceThis(bool useSelfForThis, const string& text) { + return useSelfForThis ? VString::replaceWord(text, "this", "vlSelf") : text; +} + string VSelfPointerText::protect(bool useSelfForThis, bool protect) const { - const string& sp - = useSelfForThis ? VString::replaceWord(asString(), "this", "vlSelf") : asString(); - return VIdProtect::protectWordsIf(sp, protect); + return VIdProtect::protectWordsIf(replaceThis(useSelfForThis, asString()), protect); } //###################################################################### diff --git a/src/V3Ast.h b/src/V3Ast.h index 58d61d085..1795b683b 100644 --- a/src/V3Ast.h +++ b/src/V3Ast.h @@ -1686,6 +1686,7 @@ public: bool isVlSym() const { return m_strp->find("vlSymsp") != string::npos; } bool hasThis() const { return m_strp == s_thisp || VString::startsWith(*m_strp, "this"); } string protect(bool useSelfForThis, bool protect) const; + static string replaceThis(bool useSelfForThis, const string& text); const std::string& asString() const { return *m_strp; } bool operator==(const VSelfPointerText& other) const { return *m_strp == *other.m_strp; } }; diff --git a/src/V3AstNodeOther.h b/src/V3AstNodeOther.h index c66df472e..7d0180908 100644 --- a/src/V3AstNodeOther.h +++ b/src/V3AstNodeOther.h @@ -631,8 +631,10 @@ class AstCFunc final : public AstNode { bool m_isConstructor : 1; // Is C class constructor bool m_isDestructor : 1; // Is C class destructor bool m_isMethod : 1; // Is inside a class definition - bool m_isLoose : 1; // Semantically this is a method, but is implemented as a function - // with an explicitly passed 'self' pointer as the first argument + bool m_isLoose : 1; // Semantically this is a method, but is implemented as a function with + // an explicitly passed 'self' pointer as the first argument. This can + // be slightly faster due to __restrict, and we do not declare in header + // so adding/removing loose functions doesn't recompile everything. bool m_isInline : 1; // Inline function bool m_isVirtual : 1; // Virtual function bool m_entryPoint : 1; // User may call into this top level function diff --git a/src/V3EmitCFunc.h b/src/V3EmitCFunc.h index 67236a386..30075f281 100644 --- a/src/V3EmitCFunc.h +++ b/src/V3EmitCFunc.h @@ -356,8 +356,7 @@ public: // code to allow the compiler to generate load store after the // if condition (including short-circuit evaluation) // speculatively and also reduce the data cache pollution when - // executing in the wrong path to make verilator-generated code - // run faster. + // executing in the wrong path to make Verilated code faster. puts("auto& vlSelfRef = std::ref(*vlSelf).get();\n"); } @@ -1064,9 +1063,8 @@ public: putns(nodep, "vlSymsp->_vm_contextp__->timeprecision()"); } void visit(AstNodeSimpleText* nodep) override { - const string text = m_inUC && m_useSelfForThis - ? VString::replaceWord(nodep->text(), "this", "vlSelf") - : nodep->text(); + const string text + = VSelfPointerText::replaceThis(m_inUC && m_useSelfForThis, nodep->text()); if (nodep->tracking() || m_trackText) { puts(text); } else { @@ -1342,7 +1340,7 @@ public: void visit(AstThisRef* nodep) override { putnbs(nodep, nodep->dtypep()->cType("", false, false)); puts("{"); - puts(m_useSelfForThis ? "vlSelf" : "this"); + puts(VSelfPointerText::replaceThis(m_useSelfForThis, "this")); puts("}"); }