From 0322e9da7ed52bca9b8fa5d8d7170f3fea7f8c56 Mon Sep 17 00:00:00 2001 From: Wilson Snyder Date: Fri, 18 Nov 2022 21:30:24 -0500 Subject: [PATCH] Fix randcase inside function. --- src/V3Randomize.cpp | 11 ++++++++++- test_regress/t/t_randcase.v | 10 ++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/V3Randomize.cpp b/src/V3Randomize.cpp index 89ddac9d2..e912f5e90 100644 --- a/src/V3Randomize.cpp +++ b/src/V3Randomize.cpp @@ -126,6 +126,7 @@ private: // STATE AstNodeModule* m_modp = nullptr; // Current module + AstNodeFTask* m_ftaskp = nullptr; // Current function/task size_t m_enumValueTabCount = 0; // Number of tables with enum values created int m_randCaseNum = 0; // Randcase number within a module for var naming @@ -209,12 +210,19 @@ private: // VISITORS void visit(AstNodeModule* nodep) override { + VL_RESTORER(m_modp); VL_RESTORER(m_randCaseNum); m_modp = nodep; m_randCaseNum = 0; iterateChildren(nodep); } + void visit(AstNodeFTask* nodep) override { + VL_RESTORER(m_ftaskp); + m_ftaskp = nodep; + iterateChildren(nodep); + } void visit(AstClass* nodep) override { + VL_RESTORER(m_modp); VL_RESTORER(m_randCaseNum); m_modp = nodep; m_randCaseNum = 0; @@ -278,8 +286,9 @@ private: FileLine* const fl = nodep->fileline(); const std::string name = "__Vrandcase" + cvtToStr(m_randCaseNum++); - AstVar* const randVarp = new AstVar{fl, VVarType::STMTTEMP, name, sumDTypep}; + AstVar* const randVarp = new AstVar{fl, VVarType::BLOCKTEMP, name, sumDTypep}; randVarp->noSubst(true); + if (m_ftaskp) randVarp->funcLocal(true); AstNodeExpr* sump = new AstConst{fl, AstConst::WidthedValue{}, 64, 0}; AstNodeIf* firstIfsp = new AstIf{fl, new AstConst{fl, AstConst::BitFalse{}}, nullptr, nullptr}; diff --git a/test_regress/t/t_randcase.v b/test_regress/t/t_randcase.v index 331a53aa0..0d3ff6465 100644 --- a/test_regress/t/t_randcase.v +++ b/test_regress/t/t_randcase.v @@ -13,7 +13,17 @@ module t (/*AUTOARG*/); int v; int counts[8]; + function int randfunc(); + int i; + randcase + 0 : i = 50; // Never + 1 : i = 100; + endcase + return i; + endfunction + initial begin; + if (randfunc() != 100) $stop; // for (int i = 0; i < 8; ++i) counts[i] = 0;