From c6ecd6099306bcd37ff93128578ba288e5401ab7 Mon Sep 17 00:00:00 2001 From: Wilson Snyder Date: Sun, 13 Nov 2022 11:59:40 -0500 Subject: [PATCH] Support pre_randomize and post_randomize. --- Changes | 1 + src/V3AstNodeExpr.h | 2 +- src/V3LinkDot.cpp | 2 +- src/V3LinkResolve.cpp | 4 +- src/V3Randomize.cpp | 10 ++++ test_regress/t/t_randomize_method_unsup.out | 8 ---- test_regress/t/t_randomize_method_unsup.v | 16 ------- ...method_unsup.pl => t_randomize_prepost.pl} | 6 ++- test_regress/t/t_randomize_prepost.v | 47 +++++++++++++++++++ 9 files changed, 65 insertions(+), 31 deletions(-) delete mode 100644 test_regress/t/t_randomize_method_unsup.out delete mode 100644 test_regress/t/t_randomize_method_unsup.v rename test_regress/t/{t_randomize_method_unsup.pl => t_randomize_prepost.pl} (86%) create mode 100644 test_regress/t/t_randomize_prepost.v diff --git a/Changes b/Changes index 96b17667d..449c8a3ae 100644 --- a/Changes +++ b/Changes @@ -16,6 +16,7 @@ Verilator 5.003 devel * Deprecate --no-threads; use --threads 1 for single threaded (#3703). [Kamil Rakoczy, Antmicro Ltd] * Support named properties (#3667). [Ryszard Rozak, Antmicro Ltd] * Support randcase. +* Support pre_randomize and post_randomize. * Add ENUMVALUE warning when value misused for enum (#726). * Internal AST improvements, also affect XML format (#3721). [Geza Lore] * Change ENDLABEL from warning into an error. diff --git a/src/V3AstNodeExpr.h b/src/V3AstNodeExpr.h index 93e59a566..880d757d9 100644 --- a/src/V3AstNodeExpr.h +++ b/src/V3AstNodeExpr.h @@ -1585,7 +1585,7 @@ class AstThisRef final : public AstNodeExpr { // Reference to 'this'. // @astgen op1 := childDTypep : Optional[AstClassRefDType] // dtype of the node public: - explicit AstThisRef(FileLine* fl, AstClassRefDType* dtypep) + AstThisRef(FileLine* fl, VFlagChildDType, AstClassRefDType* dtypep) : ASTGEN_SUPER_ThisRef(fl) { childDTypep(dtypep); } diff --git a/src/V3LinkDot.cpp b/src/V3LinkDot.cpp index 32a02e295..693acbc38 100644 --- a/src/V3LinkDot.cpp +++ b/src/V3LinkDot.cpp @@ -2342,7 +2342,7 @@ private: AstClass* const classp = VN_AS(classSymp->nodep(), Class); AstClassRefDType* const dtypep = new AstClassRefDType{nodep->fileline(), classp, nullptr}; - AstThisRef* const newp = new AstThisRef{nodep->fileline(), dtypep}; + AstThisRef* const newp = new AstThisRef{nodep->fileline(), VFlagChildDType{}, dtypep}; nodep->replaceWith(newp); VL_DO_DANGLING(pushDeletep(nodep), nodep); return; diff --git a/src/V3LinkResolve.cpp b/src/V3LinkResolve.cpp index ca4352e2b..9b6729e72 100644 --- a/src/V3LinkResolve.cpp +++ b/src/V3LinkResolve.cpp @@ -115,9 +115,7 @@ private: if (m_underGenerate) nodep->underGenerate(true); // Remember the existing symbol table scope if (m_classp) { - if (nodep->name() == "pre_randomize" || nodep->name() == "post_randomize") { - nodep->v3warn(E_UNSUPPORTED, "Unsupported: " << nodep->prettyNameQ()); - } else if (nodep->name() == "randomize") { + if (nodep->name() == "randomize") { nodep->v3error(nodep->prettyNameQ() << " is a predefined class method; redefinition not allowed" " (IEEE 1800-2017 18.6.3)"); diff --git a/src/V3Randomize.cpp b/src/V3Randomize.cpp index 0552865df..89ddac9d2 100644 --- a/src/V3Randomize.cpp +++ b/src/V3Randomize.cpp @@ -198,6 +198,14 @@ private: valp}; } } + void addPrePostCall(AstClass* classp, AstFunc* funcp, const string& name) { + if (AstTask* userFuncp = VN_CAST(classp->findMember(name), Task)) { + AstTaskRef* const callp + = new AstTaskRef{userFuncp->fileline(), userFuncp->name(), nullptr}; + callp->taskp(userFuncp); + funcp->addStmtsp(callp->makeStmt()); + } + } // VISITORS void visit(AstNodeModule* nodep) override { @@ -215,6 +223,7 @@ private: UINFO(9, "Define randomize() for " << nodep << endl); AstFunc* const funcp = V3Randomize::newRandomizeFunc(nodep); AstVar* const fvarp = VN_AS(funcp->fvarp(), Var); + addPrePostCall(nodep, funcp, "pre_randomize"); funcp->addStmtsp(new AstAssign{ nodep->fileline(), new AstVarRef{nodep->fileline(), fvarp, VAccess::WRITE}, new AstConst{nodep->fileline(), AstConst::WidthedValue{}, 32, 1}}); @@ -250,6 +259,7 @@ private: } } } + addPrePostCall(nodep, funcp, "post_randomize"); nodep->user1(false); } void visit(AstRandCase* nodep) override { diff --git a/test_regress/t/t_randomize_method_unsup.out b/test_regress/t/t_randomize_method_unsup.out deleted file mode 100644 index 42f6c65fa..000000000 --- a/test_regress/t/t_randomize_method_unsup.out +++ /dev/null @@ -1,8 +0,0 @@ -%Error-UNSUPPORTED: t/t_randomize_method_unsup.v:8:18: Unsupported: 'pre_randomize' - 8 | function void pre_randomize; - | ^~~~~~~~~~~~~ - ... For error description see https://verilator.org/warn/UNSUPPORTED?v=latest -%Error-UNSUPPORTED: t/t_randomize_method_unsup.v:11:18: Unsupported: 'post_randomize' - 11 | function void post_randomize; - | ^~~~~~~~~~~~~~ -%Error: Exiting due to diff --git a/test_regress/t/t_randomize_method_unsup.v b/test_regress/t/t_randomize_method_unsup.v deleted file mode 100644 index 898bf9fff..000000000 --- a/test_regress/t/t_randomize_method_unsup.v +++ /dev/null @@ -1,16 +0,0 @@ -// DESCRIPTION: Verilator: Verilog Test module -// -// This file ONLY is placed under the Creative Commons Public Domain, for -// any use, without warranty, 2020 by Wilson Snyder. -// SPDX-License-Identifier: CC0-1.0 - -class Cls; - function void pre_randomize; - endfunction; - - function void post_randomize; - endfunction; -endclass - -module t (/*AUTOARG*/); -endmodule diff --git a/test_regress/t/t_randomize_method_unsup.pl b/test_regress/t/t_randomize_prepost.pl similarity index 86% rename from test_regress/t/t_randomize_method_unsup.pl rename to test_regress/t/t_randomize_prepost.pl index 6ad7137de..aabcde63e 100755 --- a/test_regress/t/t_randomize_method_unsup.pl +++ b/test_regress/t/t_randomize_prepost.pl @@ -11,8 +11,10 @@ if (!$::Driver) { use FindBin; exec("$FindBin::Bin/bootstrap.pl", @ARGV, $0); di scenarios(simulator => 1); compile( - fails => $Self->{vlt_all}, - expect_filename => $Self->{golden_filename}, + ); + +execute( + check_finished => 1, ); ok(1); diff --git a/test_regress/t/t_randomize_prepost.v b/test_regress/t/t_randomize_prepost.v new file mode 100644 index 000000000..734bb3c31 --- /dev/null +++ b/test_regress/t/t_randomize_prepost.v @@ -0,0 +1,47 @@ +// DESCRIPTION: Verilator: Verilog Test module +// +// This file ONLY is placed under the Creative Commons Public Domain, for +// any use, without warranty, 2022 by Wilson Snyder. +// SPDX-License-Identifier: CC0-1.0 + +typedef enum int { + RANDOMIZED = 20 +} enumed_t; + +class Cls; + int pre; + rand enumed_t r; + int post; + + function void pre_randomize; + if (pre != 0) $stop; + $display("%d", r); + if (r != enumed_t'(0)) $stop; + if (post != 0) $stop; + pre = 10; + endfunction + + function void post_randomize; + if (pre != 10) $stop; + if (r != RANDOMIZED) $stop; + if (post != 0) $stop; + post = 30; + endfunction +endclass + +module t (/*AUTOARG*/); + + initial begin + Cls c; + int rand_result; + + c = new; + rand_result = c.randomize(); + if (rand_result != 1) $stop; + if (c.pre != 10) $stop; + if (c.r != RANDOMIZED) $stop; + if (c.post != 30) $stop; + $write("*-* All Finished *-*\n"); + $finish; + end +endmodule