refactor: Use V3Simulate to handle all array init

This commit is contained in:
Greg Davill 2026-04-12 18:24:44 +09:30
parent cc2b83c04d
commit 2994d2cd70
No known key found for this signature in database
GPG Key ID: 80EE31ACCEBEA6DA
1 changed files with 4 additions and 40 deletions

View File

@ -932,7 +932,6 @@ class ConstVisitor final : public VNVisitor {
bool m_hasLoopTest = false; // Contains AstLoopTest bool m_hasLoopTest = false; // Contains AstLoopTest
bool m_underRecFunc = false; // Under a recursive function bool m_underRecFunc = false; // Under a recursive function
AstNodeModule* m_modp = nullptr; // Current module AstNodeModule* m_modp = nullptr; // Current module
const AstArraySel* m_selp = nullptr; // Current select
const AstScope* m_scopep = nullptr; // Current scope const AstScope* m_scopep = nullptr; // Current scope
const AstAttrOf* m_attrp = nullptr; // Current attribute const AstAttrOf* m_attrp = nullptr; // Current attribute
VDouble0 m_statBitOpReduction; // Ops reduced in ConstBitOpTreeVisitor VDouble0 m_statBitOpReduction; // Ops reduced in ConstBitOpTreeVisitor
@ -3029,34 +3028,12 @@ class ConstVisitor final : public VNVisitor {
void visit(AstArraySel* nodep) override { void visit(AstArraySel* nodep) override {
iterateAndNextNull(nodep->bitp()); iterateAndNextNull(nodep->bitp());
if (VN_IS(nodep->bitp(), Const)
&& VN_IS(nodep->fromp(), VarRef)
// Need to make sure it's an array object so don't mis-allow a constant (bug509.)
&& VN_AS(nodep->fromp(), VarRef)->varp()
&& VN_IS(VN_AS(nodep->fromp(), VarRef)->varp()->valuep(), InitArray)) {
m_selp = nodep; // Ask visit(AstVarRef) to replace varref with const
}
iterateAndNextNull(nodep->fromp()); iterateAndNextNull(nodep->fromp());
if (VN_IS(nodep->fromp(), Const)) { // It did. if (VN_IS(nodep->fromp(), Const)) {
if (!m_selp) { nodep->v3error("Illegal assignment of constant to unpacked array");
nodep->v3error("Illegal assignment of constant to unpacked array"); } else if (m_required) {
} else { replaceWithSimulation(nodep);
AstNode* const fromp = nodep->fromp()->unlinkFrBack();
nodep->replaceWithKeepDType(fromp);
VL_DO_DANGLING(pushDeletep(nodep), nodep);
}
} }
// Handle ARRAYSEL directly on InitArray (not through VarRef)
else if (VN_IS(nodep->bitp(), Const) && VN_IS(nodep->fromp(), InitArray)) {
const AstInitArray* const initarp = VN_AS(nodep->fromp(), InitArray);
const uint32_t bit = VN_AS(nodep->bitp(), Const)->toUInt();
const AstNode* const itemp = initarp->getIndexDefaultedValuep(bit);
if (VN_IS(itemp, Const)) {
const V3Number& num = VN_AS(itemp, Const)->num();
VL_DO_DANGLING(replaceNum(nodep, num), nodep);
}
}
m_selp = nullptr;
} }
// Evaluate a slice of an unpacked array. If constantification is // Evaluate a slice of an unpacked array. If constantification is
@ -3118,21 +3095,8 @@ class ConstVisitor final : public VNVisitor {
= nodep->varp()->isParam() ? nodep->varp()->name() : ""; = nodep->varp()->isParam() ? nodep->varp()->name() : "";
VL_DO_DANGLING(replaceNum(nodep, num, origParamName), nodep); VL_DO_DANGLING(replaceNum(nodep, num, origParamName), nodep);
did = true; did = true;
} else if (m_selp && VN_IS(valuep, InitArray)) {
const AstInitArray* const initarp = VN_AS(valuep, InitArray);
const uint32_t bit = m_selp->bitConst();
const AstNode* const itemp = initarp->getIndexDefaultedValuep(bit);
if (VN_IS(itemp, Const)) {
const V3Number& num = VN_AS(itemp, Const)->num();
// UINFO(2, "constVisit " << cvtToHex(valuep) << " " << num);
VL_DO_DANGLING(replaceNum(nodep, num), nodep);
did = true;
}
} else if (m_params && VN_IS(valuep, InitArray)) { } else if (m_params && VN_IS(valuep, InitArray)) {
// Allow parameters to pass arrays // Allow parameters to pass arrays
// Earlier recursion of InitArray made sure each array value is constant
// This exception is fairly fragile, i.e. doesn't
// support arrays of arrays or other stuff
AstNode* const newp = valuep->cloneTree(false); AstNode* const newp = valuep->cloneTree(false);
nodep->replaceWithKeepDType(newp); nodep->replaceWithKeepDType(newp);
VL_DO_DANGLING(pushDeletep(nodep), nodep); VL_DO_DANGLING(pushDeletep(nodep), nodep);