Skip to content

Commit cd2fab7

Browse files
committed
Fix empty foreach in constraints
Signed-off-by: Krzysztof Bieganski <kbieganski@antmicro.com>
1 parent f623db7 commit cd2fab7

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

src/V3Randomize.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -695,11 +695,13 @@ class ConstraintExprVisitor final : public VNVisitor {
695695
AstNode* const cstmtp = new AstText{fl, "ret += \" \" + "};
696696
cstmtp->addNext(itemp);
697697
cstmtp->addNext(new AstText{fl, ";"});
698-
AstNode* const exprsp = new AstText{fl, "([&]{ std::string ret = \"(bvand\";"};
698+
AstNode* const exprsp = new AstText{fl, "([&]{ std::string ret;"};
699699
exprsp->addNext(new AstBegin{
700700
fl, "",
701701
new AstForeach{fl, nodep->arrayp()->unlinkFrBack(), new AstCStmt{fl, cstmtp}},
702702
false, true});
703+
exprsp->addNext(
704+
new AstText{fl, "return ret.empty() ? \"#b1\" : \"(bvand \" + ret + \")\";"});
703705
exprsp->addNext(new AstText{fl, "return ret + \")\"; })()"});
704706
AstNodeExpr* const newp = new AstCExpr{fl, exprsp};
705707
newp->dtypeSetString();

test_regress/t/t_constraint_foreach.v

+5
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,13 @@ endclass
3232
class D;
3333
rand bit posit;
3434
rand int x;
35+
int p[$]; // empty
3536
int q[$] = {0, 0, 0, 0, 0};
3637
constraint fore {
38+
foreach(p[i]) x == i;
39+
if (posit == 1) {
40+
foreach(p[i]) p[i] > 0;
41+
}
3742
if (posit == 1) {
3843
x < 7;
3944
foreach(q[i])

0 commit comments

Comments
 (0)