Skip to content

Commit

Permalink
Extract code from class visitor to addRandomizeBody
Browse files Browse the repository at this point in the history
Signed-off-by: Ryszard Rozak <[email protected]>
  • Loading branch information
RRozak committed Nov 28, 2024
1 parent 11071da commit 160ee51
Showing 1 changed file with 49 additions and 43 deletions.
92 changes: 49 additions & 43 deletions src/V3Randomize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1896,6 +1896,54 @@ class RandomizeVisitor final : public VNVisitor {
relinker.relink(new AstBegin{nodep->fileline(), "", stmtsp, false, true});
}
}
void addRandomizeBody(AstClass* const classp, AstFunc* const randomizep, AstVar* const genp) {
FileLine* fl = randomizep->fileline();
AstNodeExpr* beginValp = nullptr;
AstVar* const fvarp = VN_AS(randomizep->fvarp(), Var);
addPrePostCall(classp, randomizep, "pre_randomize");

if (genp) {
randomizep->addStmtsp(implementConstraintsClear(fl, genp));
AstTask* setupAllTaskp = getCreateConstraintSetupFunc(classp);
AstTaskRef* const setupTaskRefp = new AstTaskRef{fl, setupAllTaskp->name(), nullptr};
setupTaskRefp->taskp(setupAllTaskp);
randomizep->addStmtsp(setupTaskRefp->makeStmt());

AstNodeModule* const genModp = VN_AS(genp->user2p(), NodeModule);
AstVarRef* const genRefp = new AstVarRef{fl, genModp, genp, VAccess::READWRITE};
AstNode* const argsp = genRefp;
argsp->addNext(new AstText{fl, ".next(__Vm_rng)"});

AstNodeExpr* const solverCallp = new AstCExpr{fl, argsp};
solverCallp->dtypeSetBit();
beginValp = solverCallp;
} else {
beginValp = new AstConst{fl, AstConst::WidthedValue{}, 32, 1};
}

AstVarRef* const fvarRefp = new AstVarRef{fl, fvarp, VAccess::WRITE};
randomizep->addStmtsp(new AstAssign{fl, fvarRefp, beginValp});

if (AstTask* const resizeAllTaskp
= VN_AS(m_memberMap.findMember(classp, "__Vresize_constrained_arrays"), Task)) {
AstTaskRef* const resizeTaskRefp = new AstTaskRef{fl, resizeAllTaskp->name(), nullptr};
resizeTaskRefp->taskp(resizeAllTaskp);
randomizep->addStmtsp(resizeTaskRefp->makeStmt());
}

AstFunc* const basicRandomizep
= V3Randomize::newRandomizeFunc(m_memberMap, classp, "__Vbasic_randomize");
addBasicRandomizeBody(basicRandomizep, classp, 1);
AstFuncRef* const basicRandomizeCallp = new AstFuncRef{fl, "__Vbasic_randomize", nullptr};
basicRandomizeCallp->taskp(basicRandomizep);
basicRandomizeCallp->dtypep(basicRandomizep->dtypep());
AstVarRef* const fvarRefReadp = fvarRefp->cloneTree(false);
fvarRefReadp->access(VAccess::READ);

randomizep->addStmtsp(new AstAssign{fl, fvarRefp->cloneTree(false),
new AstAnd{fl, fvarRefReadp, basicRandomizeCallp}});
addPrePostCall(classp, randomizep, "post_randomize");
}

// VISITORS
void visit(AstNodeModule* nodep) override {
Expand All @@ -1921,12 +1969,9 @@ class RandomizeVisitor final : public VNVisitor {
UINFO(9, "Define randomize() for " << nodep << endl);
nodep->baseMostClassp()->needRNG(true);
AstFunc* const randomizep = V3Randomize::newRandomizeFunc(m_memberMap, nodep);
AstVar* const fvarp = VN_AS(randomizep->fvarp(), Var);
addPrePostCall(nodep, randomizep, "pre_randomize");
FileLine* fl = nodep->fileline();

AstVar* const randModeVarp = getRandModeVar(nodep);
AstNodeExpr* beginValp = nullptr;
AstVar* genp = getRandomGenerator(nodep);
if (genp) {
nodep->foreachMember([&](AstClass* const classp, AstConstraint* const constrp) {
Expand Down Expand Up @@ -1960,55 +2005,16 @@ class RandomizeVisitor final : public VNVisitor {
nodep, constrp, constrp->itemsp()->unlinkFrBackWithNext()));
}
});
randomizep->addStmtsp(implementConstraintsClear(fl, genp));
AstTask* setupAllTaskp = getCreateConstraintSetupFunc(nodep);
AstTaskRef* const setupTaskRefp = new AstTaskRef{fl, setupAllTaskp->name(), nullptr};
setupTaskRefp->taskp(setupAllTaskp);
randomizep->addStmtsp(setupTaskRefp->makeStmt());

AstNodeModule* const genModp = VN_AS(genp->user2p(), NodeModule);
AstVarRef* const genRefp = new AstVarRef{fl, genModp, genp, VAccess::READWRITE};
AstNode* const argsp = genRefp;
argsp->addNext(new AstText{fl, ".next(__Vm_rng)"});

AstNodeExpr* const solverCallp = new AstCExpr{fl, argsp};
solverCallp->dtypeSetBit();
beginValp = solverCallp;

if (randModeVarp) {
AstNodeModule* const randModeClassp = VN_AS(randModeVarp->user2p(), Class);
AstNodeFTask* const newp
= VN_AS(m_memberMap.findMember(randModeClassp, "new"), NodeFTask);
UASSERT_OBJ(newp, randModeClassp, "No new() in class");
addSetRandMode(newp, genp, randModeVarp);
}
} else {
beginValp = new AstConst{fl, AstConst::WidthedValue{}, 32, 1};
}

AstVarRef* const fvarRefp = new AstVarRef{fl, fvarp, VAccess::WRITE};
randomizep->addStmtsp(new AstAssign{fl, fvarRefp, beginValp});

if (AstTask* const resizeAllTaskp
= VN_AS(m_memberMap.findMember(nodep, "__Vresize_constrained_arrays"), Task)) {
AstTaskRef* const resizeTaskRefp = new AstTaskRef{fl, resizeAllTaskp->name(), nullptr};
resizeTaskRefp->taskp(resizeAllTaskp);
randomizep->addStmtsp(resizeTaskRefp->makeStmt());
}

AstFunc* const basicRandomizep
= V3Randomize::newRandomizeFunc(m_memberMap, nodep, "__Vbasic_randomize");
handleRandMode(nodep, randModeVarp);
addBasicRandomizeBody(basicRandomizep, nodep, 1);
AstFuncRef* const basicRandomizeCallp = new AstFuncRef{fl, "__Vbasic_randomize", nullptr};
basicRandomizeCallp->taskp(basicRandomizep);
basicRandomizeCallp->dtypep(basicRandomizep->dtypep());
AstVarRef* const fvarRefReadp = fvarRefp->cloneTree(false);
fvarRefReadp->access(VAccess::READ);

randomizep->addStmtsp(new AstAssign{fl, fvarRefp->cloneTree(false),
new AstAnd{fl, fvarRefReadp, basicRandomizeCallp}});
addPrePostCall(nodep, randomizep, "post_randomize");
addRandomizeBody(nodep, randomizep, genp);
nodep->user1(false);
}
void visit(AstRandCase* nodep) override {
Expand Down

0 comments on commit 160ee51

Please sign in to comment.