Internals: Refactor to create VCMethod (#3715). No functional change intended.

This commit is contained in:
Wilson Snyder
2025-09-27 08:22:17 -04:00
parent 89cd5417ef
commit 3b623dc12e
20 changed files with 467 additions and 244 deletions
+33 -27
View File
@@ -233,7 +233,8 @@ class RandomizeMarkVisitor final : public VNVisitor {
nodep->v3error("Cannot call 'rand_mode()' on packed array element");
valid = false;
} else if (AstCMethodHard* const methodHardp = VN_CAST(fromp, CMethodHard)) {
if (methodHardp->name() == "at" || methodHardp->name() == "atWrite") {
if (methodHardp->method() == VCMethod::ARRAY_AT
|| methodHardp->method() == VCMethod::ARRAY_AT_WRITE) {
nodep->v3warn(E_UNSUPPORTED,
"Unsupported: 'rand_mode()' on dynamic array element");
valid = false;
@@ -622,7 +623,7 @@ class ConstraintExprVisitor final : public VNVisitor {
AstNodeDType* const arrDtp = arrayp->unlinkFrBack()->dtypep();
AstNodeExpr* selp = nullptr;
if (VN_IS(arrDtp, QueueDType) || VN_IS(arrDtp, DynArrayDType))
selp = new AstCMethodHard{fl, arrayp, "at", idxp};
selp = new AstCMethodHard{fl, arrayp, VCMethod::ARRAY_AT, idxp};
else if (VN_IS(arrDtp, UnpackArrayDType))
selp = new AstArraySel{fl, arrayp, idxp};
else if (VN_IS(arrDtp, AssocArrayDType))
@@ -662,7 +663,7 @@ class ConstraintExprVisitor final : public VNVisitor {
nodep->fileline(),
new AstVarRef{varp->fileline(), VN_AS(m_randModeVarp->user2p(), NodeModule),
m_randModeVarp, VAccess::READ},
"at", new AstConst{nodep->fileline(), randMode.index}};
VCMethod::ARRAY_AT, new AstConst{nodep->fileline(), randMode.index}};
atp->dtypeSetUInt32();
exprp = new AstCond{varp->fileline(), atp, exprp, constFormatp};
} else {
@@ -675,7 +676,7 @@ class ConstraintExprVisitor final : public VNVisitor {
varp->fileline(),
new AstVarRef{varp->fileline(), VN_AS(m_genp->user2p(), NodeModule), m_genp,
VAccess::READWRITE},
"write_var"};
VCMethod::RANDOMIZER_WRITE_VAR};
uint32_t dimension = 0;
if (VN_IS(varp->dtypep(), UnpackArrayDType) || VN_IS(varp->dtypep(), DynArrayDType)
|| VN_IS(varp->dtypep(), QueueDType) || VN_IS(varp->dtypep(), AssocArrayDType)) {
@@ -991,7 +992,7 @@ class ConstraintExprVisitor final : public VNVisitor {
nodep->fileline(),
new AstVarRef{nodep->fileline(), VN_AS(m_genp->user2p(), NodeModule), m_genp,
VAccess::READWRITE},
"hard", nodep->exprp()->unlinkFrBack()};
VCMethod::RANDOMIZER_HARD, nodep->exprp()->unlinkFrBack()};
callp->dtypeSetVoid();
nodep->replaceWith(callp->makeStmt());
VL_DO_DANGLING(nodep->deleteTree(), nodep);
@@ -1000,7 +1001,7 @@ class ConstraintExprVisitor final : public VNVisitor {
if (editFormat(nodep)) return;
FileLine* const fl = nodep->fileline();
if (nodep->name() == "at" && nodep->fromp()->user1()) {
if (nodep->method() == VCMethod::ARRAY_AT && nodep->fromp()->user1()) {
iterateChildren(nodep);
AstNodeExpr* pinp = nodep->pinsp()->unlinkFrBack();
if (VN_IS(pinp, SFormatF) && m_structSel) VN_AS(pinp, SFormatF)->name("%x");
@@ -1015,7 +1016,7 @@ class ConstraintExprVisitor final : public VNVisitor {
return;
}
if (nodep->name() == "inside") {
if (nodep->method() == VCMethod::ARRAY_INSIDE) {
bool randArr = nodep->fromp()->user1();
AstVar* const newVarp
@@ -1455,7 +1456,7 @@ class RandomizeVisitor final : public VNVisitor {
FileLine* const fl = ftaskp->fileline();
AstCMethodHard* const setRandModep = new AstCMethodHard{
fl, new AstVarRef{fl, VN_AS(genp->user2p(), NodeModule), genp, VAccess::WRITE},
"set_randmode",
VCMethod::CLASS_SET_RANDMODE,
new AstVarRef{fl, VN_AS(randModeVarp->user2p(), NodeModule), randModeVarp,
VAccess::READ}};
setRandModep->dtypeSetVoid();
@@ -1506,7 +1507,7 @@ class RandomizeVisitor final : public VNVisitor {
FileLine* fl = modeVarp->fileline();
AstCMethodHard* const dynarrayNewp
= new AstCMethodHard{fl, new AstVarRef{fl, modeVarModp, modeVarp, VAccess::WRITE},
"resize", new AstConst{fl, modeCount}};
VCMethod::DYN_RESIZE, new AstConst{fl, modeCount}};
dynarrayNewp->dtypeSetVoid();
AstNodeFTask* const newp = VN_AS(m_memberMap.findMember(classp, "new"), NodeFTask);
UASSERT_OBJ(newp, classp, "No new() in class");
@@ -1520,10 +1521,11 @@ class RandomizeVisitor final : public VNVisitor {
AstVar* const iterVarp = new AstVar{fl, VVarType::BLOCKTEMP, "i", lhsp->findUInt32DType()};
iterVarp->funcLocal(inTask);
iterVarp->lifetime(VLifetime::AUTOMATIC_EXPLICIT);
AstCMethodHard* const sizep = new AstCMethodHard{fl, lhsp, "size", nullptr};
AstCMethodHard* const sizep = new AstCMethodHard{fl, lhsp, VCMethod::DYN_SIZE, nullptr};
sizep->dtypeSetUInt32();
AstCMethodHard* const setp = new AstCMethodHard{
fl, lhsp->cloneTree(false), "atWrite", new AstVarRef{fl, iterVarp, VAccess::READ}};
AstCMethodHard* const setp
= new AstCMethodHard{fl, lhsp->cloneTree(false), VCMethod::ARRAY_AT_WRITE,
new AstVarRef{fl, iterVarp, VAccess::READ}};
setp->dtypeSetUInt32();
AstNode* const stmtsp = iterVarp;
stmtsp->addNext(
@@ -1550,7 +1552,7 @@ class RandomizeVisitor final : public VNVisitor {
if (mode.usesMode) {
AstCMethodHard* const atp = new AstCMethodHard{
fl, new AstVarRef{fl, VN_AS(modeVarp->user2p(), Class), modeVarp, VAccess::READ},
"at", new AstConst{fl, mode.index}};
VCMethod::ARRAY_AT, new AstConst{fl, mode.index}};
atp->dtypeSetUInt32();
return new AstIf{fl, atp, stmtp};
}
@@ -1651,7 +1653,8 @@ class RandomizeVisitor final : public VNVisitor {
AstNodeExpr* const tempExprp = tempElementp ? tempElementp : exprp;
AstVarRef* const tempRefp = new AstVarRef{fl, newRandLoopIndxp, VAccess::READ};
if (VN_IS(tempDTypep, DynArrayDType)) {
tempElementp = new AstCMethodHard{fl, tempExprp, "atWrite", tempRefp};
tempElementp
= new AstCMethodHard{fl, tempExprp, VCMethod::ARRAY_AT_WRITE, tempRefp};
} else if (VN_IS(tempDTypep, UnpackArrayDType)) {
AstNodeArrayDType* const aryDTypep = VN_CAST(tempDTypep, NodeArrayDType);
// Adjust the bitp to ensure it covers all possible indices
@@ -1665,7 +1668,8 @@ class RandomizeVisitor final : public VNVisitor {
} else if (VN_IS(tempDTypep, AssocArrayDType)) {
tempElementp = new AstAssocSel{fl, tempExprp, tempRefp};
} else if (VN_IS(tempDTypep, QueueDType)) {
tempElementp = new AstCMethodHard{fl, tempExprp, "atWriteAppend", tempRefp};
tempElementp
= new AstCMethodHard{fl, tempExprp, VCMethod::DYN_AT_WRITE_APPEND, tempRefp};
}
tempElementp->dtypep(tempDTypep->subDTypep());
tempDTypep = tempDTypep->virtRefDTypep();
@@ -1797,7 +1801,7 @@ class RandomizeVisitor final : public VNVisitor {
AstCMethodHard* const clearp = new AstCMethodHard{
fileline,
new AstVarRef{fileline, VN_AS(genp->user2p(), NodeModule), genp, VAccess::READWRITE},
"clear"};
VCMethod::RANDOMIZER_CLEAR};
clearp->dtypeSetVoid();
return clearp->makeStmt();
}
@@ -1909,7 +1913,7 @@ class RandomizeVisitor final : public VNVisitor {
nodep->fileline(),
new AstVarRef{fl, VN_AS(randModeVarp->user2p(), NodeModule), randModeVarp,
VAccess::WRITE},
"atWrite", new AstConst{nodep->fileline(), randMode.index}};
VCMethod::ARRAY_AT_WRITE, new AstConst{nodep->fileline(), randMode.index}};
setp->dtypeSetUInt32();
newp->addStmtsp(new AstAssign{fl, setp, new AstConst{fl, 0}});
}
@@ -1973,8 +1977,8 @@ class RandomizeVisitor final : public VNVisitor {
// mode
const RandomizeMode rmode = {.asInt = receiverp->user1()};
UASSERT_OBJ(rmode.usesMode, ftaskRefp, "Failed to set usesMode");
AstCMethodHard* const setp
= new AstCMethodHard{fl, lhsp, "atWrite", new AstConst{fl, rmode.index}};
AstCMethodHard* const setp = new AstCMethodHard{fl, lhsp, VCMethod::ARRAY_AT_WRITE,
new AstConst{fl, rmode.index}};
setp->dtypeSetUInt32();
m_stmtp->replaceWith(new AstAssign{fl, setp, rhsp});
} else {
@@ -1988,8 +1992,8 @@ class RandomizeVisitor final : public VNVisitor {
UASSERT_OBJ(receiverp, ftaskRefp, "Should have receiver");
const RandomizeMode rmode = {.asInt = receiverp->user1()};
UASSERT_OBJ(rmode.usesMode, ftaskRefp, "Failed to set usesMode");
AstCMethodHard* const setp
= new AstCMethodHard{fl, lhsp, "atWrite", new AstConst{fl, rmode.index}};
AstCMethodHard* const setp = new AstCMethodHard{fl, lhsp, VCMethod::ARRAY_AT_WRITE,
new AstConst{fl, rmode.index}};
setp->dtypeSetUInt32();
ftaskRefp->replaceWith(setp);
VL_DO_DANGLING(pushDeletep(ftaskRefp), ftaskRefp);
@@ -2057,9 +2061,9 @@ class RandomizeVisitor final : public VNVisitor {
tmpVarps = AstNode::addNext(tmpVarps, randModeTmpVarp);
}
const RandomizeMode randMode = {.asInt = randVarp->user1()};
AstCMethodHard* setp
= new AstCMethodHard{fl, makeSiblingRefp(exprp, randModeVarp, VAccess::WRITE),
"atWrite", new AstConst{fl, randMode.index}};
AstCMethodHard* setp = new AstCMethodHard{
fl, makeSiblingRefp(exprp, randModeVarp, VAccess::WRITE),
VCMethod::ARRAY_AT_WRITE, new AstConst{fl, randMode.index}};
setp->dtypeSetUInt32();
setStmtsp
= AstNode::addNext(setStmtsp, new AstAssign{fl, setp, new AstConst{fl, 1}});
@@ -2306,7 +2310,7 @@ class RandomizeVisitor final : public VNVisitor {
AstCMethodHard* const basicMethodp = new AstCMethodHard{
nodep->fileline(),
new AstVarRef{nodep->fileline(), stdrand, VAccess::READWRITE},
"basicStdRandomization"};
VCMethod::RANDOMIZER_BASIC_STD_RANDOMIZATION};
AstVar* const refvarp
= new AstVar{exprp->fileline(), VVarType::MEMBER,
"__Varg"s + std::to_string(++argn), exprp->dtypep()};
@@ -2456,7 +2460,9 @@ class RandomizeVisitor final : public VNVisitor {
void visit(AstCMethodHard* nodep) override {
iterateChildren(nodep);
FileLine* const fl = nodep->fileline();
if (m_constraintp && nodep->fromp()->user1() && nodep->name() == "size") {
if (m_constraintp && nodep->fromp()->user1()
&& (nodep->method() == VCMethod::ASSOC_SIZE
|| nodep->method() == VCMethod::DYN_SIZE)) {
AstClass* const classp = VN_AS(m_modp, Class);
AstVarRef* const queueVarRefp = VN_CAST(nodep->fromp(), VarRef);
if (!queueVarRefp) {
@@ -2480,7 +2486,7 @@ class RandomizeVisitor final : public VNVisitor {
m_constraintp->user3p(resizerTaskp);
}
AstCMethodHard* const resizep
= new AstCMethodHard{fl, nodep->fromp()->unlinkFrBack(), "resize",
= new AstCMethodHard{fl, nodep->fromp()->unlinkFrBack(), VCMethod::DYN_RESIZE,
new AstVarRef{fl, sizeVarp, VAccess::READ}};
resizep->dtypep(nodep->findVoidDType());
resizerTaskp->addStmtsp(new AstStmtExpr{fl, resizep});