From 9049c9300975d321b989dfe72403833d2dc78515 Mon Sep 17 00:00:00 2001 From: Wilson Snyder Date: Tue, 17 Feb 2026 05:39:39 -0500 Subject: [PATCH] Internals: Call common V3NUmber function to zero data type. Cleanup chandle clearling. --- src/V3Ast.h | 1 + src/V3AstAttr.h | 1 + src/V3AstInlines.h | 3 +++ src/V3AstNodeDType.h | 1 + src/V3AstNodes.cpp | 3 +-- src/V3Number.cpp | 7 +++++-- src/V3Number.h | 9 ++++++--- src/V3Simulate.h | 2 +- src/V3Width.cpp | 2 +- 9 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/V3Ast.h b/src/V3Ast.h index 14c1bd3d4..1051a4b31 100644 --- a/src/V3Ast.h +++ b/src/V3Ast.h @@ -639,6 +639,7 @@ public: int widthWords() const { return VL_WORDS_I(width()); } bool isQuad() const VL_MT_STABLE { return (width() > VL_IDATASIZE && width() <= VL_QUADSIZE); } bool isWide() const VL_MT_STABLE { return (width() > VL_QUADSIZE); } + inline bool isCHandle() const VL_MT_STABLE; inline bool isDouble() const VL_MT_STABLE; inline bool isSigned() const VL_MT_STABLE; inline bool isString() const VL_MT_STABLE; diff --git a/src/V3AstAttr.h b/src/V3AstAttr.h index 93fd6249e..afb6220eb 100644 --- a/src/V3AstAttr.h +++ b/src/V3AstAttr.h @@ -591,6 +591,7 @@ public: || m_e == RANDOM_GENERATOR || m_e == RANDOM_STDGENERATOR || m_e == DOUBLE || m_e == UNTYPED); } + bool isCHandle() const VL_MT_SAFE { return m_e == CHANDLE; } bool isDouble() const VL_MT_SAFE { return m_e == DOUBLE; } bool isEvent() const { return m_e == EVENT; } bool isString() const VL_MT_SAFE { return m_e == STRING; } diff --git a/src/V3AstInlines.h b/src/V3AstInlines.h index 700e55887..1ef5bfd1a 100644 --- a/src/V3AstInlines.h +++ b/src/V3AstInlines.h @@ -33,6 +33,9 @@ bool AstNode::width1() const { // V3Const uses to know it can optimize int AstNode::widthInstrs() const { return (!dtypep() ? 1 : (dtypep()->isWide() ? dtypep()->widthWords() : 1)); } +bool AstNode::isCHandle() const VL_MT_STABLE { + return dtypep() && dtypep()->basicp() && dtypep()->basicp()->isCHandle(); +} bool AstNode::isDouble() const VL_MT_STABLE { return dtypep() && dtypep()->basicp() && dtypep()->basicp()->isDouble(); } diff --git a/src/V3AstNodeDType.h b/src/V3AstNodeDType.h index d55196555..4d5bd6046 100644 --- a/src/V3AstNodeDType.h +++ b/src/V3AstNodeDType.h @@ -458,6 +458,7 @@ public: return m.m_keyword; } bool isBitLogic() const { return keyword().isBitLogic(); } + bool isCHandle() const VL_MT_STABLE { return keyword().isCHandle(); } bool isDouble() const VL_MT_STABLE { return keyword().isDouble(); } bool isEvent() const VL_MT_STABLE { return keyword() == VBasicDTypeKwd::EVENT; } bool isForkSync() const VL_MT_SAFE { return keyword() == VBasicDTypeKwd::FORK_SYNC; } diff --git a/src/V3AstNodes.cpp b/src/V3AstNodes.cpp index 353e18d9b..afd4a978d 100644 --- a/src/V3AstNodes.cpp +++ b/src/V3AstNodes.cpp @@ -877,8 +877,7 @@ string AstVar::dpiTmpVarType(const string& varName) const { string primitive(const AstVar* varp) const override { string type = dpiTypesToStringConverter::primitive(varp); if (varp->isWritable() || VN_IS(varp->dtypep()->skipRefp(), UnpackArrayDType)) { - if (!varp->isWritable() && varp->basicp()->keyword() == VBasicDTypeKwd::CHANDLE) - type = "const " + type; + if (!varp->isWritable() && varp->basicp()->isCHandle()) type = "const " + type; } type += ' ' + m_name + arraySuffix(varp, 0); return type; diff --git a/src/V3Number.cpp b/src/V3Number.cpp index fff959d1a..616d7ac82 100644 --- a/src/V3Number.cpp +++ b/src/V3Number.cpp @@ -116,12 +116,15 @@ V3Number::V3Number(AstNode* nodep, VerilogStringLiteral, const string& str) { } V3Number::V3Number(AstNode* nodep, const AstNodeDType* nodedtypep) { - if (nodedtypep->isString()) { + if (nodedtypep->isCHandle()) { init(nodep); - setString(""); + setNull(); } else if (nodedtypep->isDouble()) { init(nodep, 64); setDouble(0.0); + } else if (nodedtypep->isString()) { + init(nodep); + setString(""); } else { init(nodep, nodedtypep->width(), nodedtypep->widthSized()); } diff --git a/src/V3Number.h b/src/V3Number.h index 236dd26a9..8c84252d1 100644 --- a/src/V3Number.h +++ b/src/V3Number.h @@ -340,6 +340,11 @@ class V3Number final { FileLine* m_fileline = nullptr; // Source location - if no parent node is reasonable // METHODS + void setNull() { + m_data.setLogic(); + m_data.m_isNull = true; + m_data.m_autoExtend = true; + } V3Number& setSingleBits(char value); V3Number& setString(const string& str) { m_data.setString(str); @@ -512,9 +517,7 @@ public: class Null {}; V3Number(AstNode* nodep, Null) { init(nodep); - m_data.setLogic(); - m_data.m_isNull = true; - m_data.m_autoExtend = true; + setNull(); } explicit V3Number(const V3Number* nump, int width = 1) { init(nullptr, width); diff --git a/src/V3Simulate.h b/src/V3Simulate.h index 506f3e0df..0fd8b97ea 100644 --- a/src/V3Simulate.h +++ b/src/V3Simulate.h @@ -458,7 +458,7 @@ private: if (const AstBasicDType* const basicp = nodep->dtypeSkipRefp()->basicp()) { AstConst cnst{nodep->fileline(), AstConst::WidthedValue{}, basicp->widthMin(), 0}; if (basicp->isZeroInit()) { - cnst.num().setAllBits0(); + cnst.num() = V3Number{nodep, basicp}; } else { cnst.num().setAllBitsX(); } diff --git a/src/V3Width.cpp b/src/V3Width.cpp index e138b3869..50f0cb2dd 100644 --- a/src/V3Width.cpp +++ b/src/V3Width.cpp @@ -8264,7 +8264,7 @@ class WidthVisitor final : public VNVisitor { linker.relink(newp); } else if (VN_IS(underVDTypep, ClassRefDType) || VN_IS(underVDTypep, IfaceRefDType) || (VN_IS(underVDTypep, BasicDType) - && VN_AS(underVDTypep, BasicDType)->keyword() == VBasicDTypeKwd::CHANDLE)) { + && VN_AS(underVDTypep, BasicDType)->isCHandle())) { // Allow warning-free "if (handle)" VL_DO_DANGLING(fixWidthReduce(VN_AS(underp, NodeExpr)), underp); // Changed } else if (!underVDTypep->basicp()) {