From f5caa4b7dc83d4918d1977989da3aa5d9b341603 Mon Sep 17 00:00:00 2001 From: Krzysztof Bieganski Date: Mon, 22 Jul 2024 14:41:12 +0200 Subject: [PATCH] Fix randomizing current object with `rand` class instance member (#5292) Signed-off-by: Krzysztof Bieganski --- src/V3Randomize.cpp | 22 +++++++++---------- ...randomize_small.pl => t_randomize_this.pl} | 0 ...t_randomize_small.v => t_randomize_this.v} | 22 +++++++++++++++++-- 3 files changed, 31 insertions(+), 13 deletions(-) rename test_regress/t/{t_randomize_small.pl => t_randomize_this.pl} (100%) rename test_regress/t/{t_randomize_small.v => t_randomize_this.v} (51%) diff --git a/src/V3Randomize.cpp b/src/V3Randomize.cpp index fe1d00169..531c90df9 100644 --- a/src/V3Randomize.cpp +++ b/src/V3Randomize.cpp @@ -119,20 +119,20 @@ class RandomizeMarkVisitor final : public VNVisitorConst { m_baseToDerivedMap[basep].insert(nodep); } } - void visit(AstMethodCall* nodep) override { - if (nodep->name() != "randomize") return; - if (const AstClassRefDType* const classRefp - = VN_CAST(nodep->fromp()->dtypep()->skipRefp(), ClassRefDType)) { - AstClass* const classp = classRefp->classp(); - classp->user1(true); - markMembers(classp); - } - iterateChildrenConst(nodep); - } void visit(AstNodeFTaskRef* nodep) override { iterateChildrenConst(nodep); if (nodep->name() != "randomize") return; - if (m_classp) m_classp->user1(true); + AstClass* classp = m_classp; + if (const AstMethodCall* const methodCallp = VN_CAST(nodep, MethodCall)) { + if (const AstClassRefDType* const classRefp + = VN_CAST(methodCallp->fromp()->dtypep()->skipRefp(), ClassRefDType)) { + classp = classRefp->classp(); + } + } + if (classp) { + classp->user1(true); + markMembers(classp); + } } void visit(AstConstraintExpr* nodep) override { VL_RESTORER(m_constraintExprp); diff --git a/test_regress/t/t_randomize_small.pl b/test_regress/t/t_randomize_this.pl similarity index 100% rename from test_regress/t/t_randomize_small.pl rename to test_regress/t/t_randomize_this.pl diff --git a/test_regress/t/t_randomize_small.v b/test_regress/t/t_randomize_this.v similarity index 51% rename from test_regress/t/t_randomize_small.v rename to test_regress/t/t_randomize_this.v index 1b91feab4..8ce0869af 100644 --- a/test_regress/t/t_randomize_small.v +++ b/test_regress/t/t_randomize_this.v @@ -4,14 +4,32 @@ // any use, without warranty, 2023 by Wilson Snyder. // SPDX-License-Identifier: CC0-1.0 +class Member; + rand int m_val; +endclass + class Cls; rand int m_val; + rand Member m_member; function void test; automatic int rand_result; + logic ok1 = 0, ok2 = 0; - rand_result = randomize(); - if (rand_result != 1) $stop; + m_val = 256; + m_member.m_val = 65535; + for (int i = 0; i < 20; i++) begin + rand_result = randomize(); + if (rand_result != 1) $stop; + if (m_val != 256) ok1 = 1; + if (m_member.m_val != 65535) ok2 = 1; + end + if (!ok1) $stop; + if (!ok2) $stop; + endfunction + + function new; + m_member = new; endfunction endclass