From f0afcede10609eb855f375a3dc0535a06e13d271 Mon Sep 17 00:00:00 2001 From: Geza Lore Date: Sun, 1 Feb 2026 05:09:19 +0000 Subject: [PATCH] Internals: Use pure expressions in V3Randomize (#6974) --- include/verilated_random.cpp | 2 +- include/verilated_random.h | 2 +- src/V3Randomize.cpp | 17 +++++++++-------- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/include/verilated_random.cpp b/include/verilated_random.cpp index 1bea2458d..a70a81ed7 100644 --- a/include/verilated_random.cpp +++ b/include/verilated_random.cpp @@ -595,7 +595,7 @@ bool VlRandomizer::parseSolution(std::iostream& os, bool log) { return true; } -void VlRandomizer::hard(std::string&& constraint, const char* filename, int linenum, +void VlRandomizer::hard(std::string&& constraint, const char* filename, uint32_t linenum, const char* source) { m_constraints.emplace_back(std::move(constraint)); // Format constraint location: "filename:linenum source" diff --git a/include/verilated_random.h b/include/verilated_random.h index fc03ab1b5..0d4c84fbd 100644 --- a/include/verilated_random.h +++ b/include/verilated_random.h @@ -581,7 +581,7 @@ public: + std::to_string(idx); } - void hard(std::string&& constraint, const char* filename = "", int linenum = 0, + void hard(std::string&& constraint, const char* filename = "", uint32_t linenum = 0, const char* source = ""); void clearConstraints(); void clearAll(); // Clear both constraints and variables diff --git a/src/V3Randomize.cpp b/src/V3Randomize.cpp index 658b451ab..2a4dd3d8a 100644 --- a/src/V3Randomize.cpp +++ b/src/V3Randomize.cpp @@ -901,8 +901,7 @@ class ConstraintExprVisitor final : public VNVisitor { AstNodeExpr* exprp; if (randMode.usesMode) { // Use string literal to avoid double formatting - exprp = new AstCExpr{nodep->fileline(), "std::string(\"" + smtName + "\")", 1}; - exprp->dtypeSetString(); + exprp = new AstConst{nodep->fileline(), AstConst::String{}, smtName}; // Get const format, using membersel if available for correct width/value AstNodeExpr* constFormatp @@ -1003,8 +1002,8 @@ class ConstraintExprVisitor final : public VNVisitor { const size_t width = tmpDtypep->width(); methodp->addPinsp( new AstConst{varp->dtypep()->fileline(), AstConst::Unsized64{}, width}); - AstNodeExpr* const varnamep - = new AstCExpr{varp->fileline(), "\"" + smtName + "\"", varp->width()}; + AstNodeExpr* const varnamep = new AstCExpr{varp->fileline(), AstCExpr::Pure{}, + "\"" + smtName + "\"", varp->width()}; varnamep->dtypep(varp->dtypep()); methodp->addPinsp(varnamep); methodp->addPinsp( @@ -1434,10 +1433,11 @@ class ConstraintExprVisitor final : public VNVisitor { // Pass filename, lineno, and source as separate arguments // This allows EmitC to call protect() on filename, similar to VL_STOP // Add filename parameter - callp->addPinsp( - new AstCExpr{nodep->fileline(), "\"" + nodep->fileline()->filename() + "\""}); + callp->addPinsp(new AstCExpr{nodep->fileline(), AstCExpr::Pure{}, + "\"" + nodep->fileline()->filename() + "\""}); // Add line number parameter - callp->addPinsp(new AstCExpr{nodep->fileline(), cvtToStr(nodep->fileline()->lineno())}); + const uint32_t lineno = static_cast(nodep->fileline()->lineno()); + callp->addPinsp(new AstConst{nodep->fileline(), lineno}); // Add source text parameter (empty if --protect-ids to avoid source leakage) std::string prettyText; if (!v3Global.opt.protectIds()) { @@ -1448,7 +1448,8 @@ class ConstraintExprVisitor final : public VNVisitor { pos += std::strlen("\\\""); } } - callp->addPinsp(new AstCExpr{nodep->fileline(), "\"" + prettyText + "\""}); + callp->addPinsp( + new AstCExpr{nodep->fileline(), AstCExpr::Pure{}, "\"" + prettyText + "\""}); nodep->replaceWith(callp->makeStmt()); VL_DO_DANGLING(nodep->deleteTree(), nodep); }