From 9121a81a74e748b2e659393e0023986a8ea5301b Mon Sep 17 00:00:00 2001 From: Todd Strader Date: Wed, 8 Feb 2023 18:50:27 -0500 Subject: [PATCH] Fix constant enum methods (#3621) --- src/V3Simulate.h | 3 +- test_regress/t/t_enum_const_methods.pl | 21 ++++++++++++ test_regress/t/t_enum_const_methods.v | 47 ++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 1 deletion(-) create mode 100755 test_regress/t/t_enum_const_methods.pl create mode 100644 test_regress/t/t_enum_const_methods.v diff --git a/src/V3Simulate.h b/src/V3Simulate.h index 116596dad..90f846cfb 100644 --- a/src/V3Simulate.h +++ b/src/V3Simulate.h @@ -442,7 +442,8 @@ private: clearOptimizable(nodep, "Var write & read"); } vscp->user1(vscp->user1() | VU_RV); - const bool isConst = nodep->varp()->isParam() && nodep->varp()->valuep(); + const bool isConst = (nodep->varp()->isConst() || nodep->varp()->isParam()) + && nodep->varp()->valuep(); AstNodeExpr* const valuep = isConst ? fetchValueNull(nodep->varp()->valuep()) : nullptr; // Propagate PARAM constants for constant function analysis diff --git a/test_regress/t/t_enum_const_methods.pl b/test_regress/t/t_enum_const_methods.pl new file mode 100755 index 000000000..b46d46042 --- /dev/null +++ b/test_regress/t/t_enum_const_methods.pl @@ -0,0 +1,21 @@ +#!/usr/bin/env perl +if (!$::Driver) { use FindBin; exec("$FindBin::Bin/bootstrap.pl", @ARGV, $0); die; } +# DESCRIPTION: Verilator: Verilog Test driver/expect definition +# +# Copyright 2003 by Wilson Snyder. This program is free software; you +# can redistribute it and/or modify it under the terms of either the GNU +# Lesser General Public License Version 3 or the Perl Artistic License +# Version 2.0. +# SPDX-License-Identifier: LGPL-3.0-only OR Artistic-2.0 + +scenarios(simulator => 1); + +compile( + ); + +execute( + check_finished => 1, + ); + +ok(1); +1; diff --git a/test_regress/t/t_enum_const_methods.v b/test_regress/t/t_enum_const_methods.v new file mode 100644 index 000000000..e5ce9e979 --- /dev/null +++ b/test_regress/t/t_enum_const_methods.v @@ -0,0 +1,47 @@ +// DESCRIPTION: Verilator: constant enum methods +// +// This file ONLY is placed into the Public Domain, for any use, +// without warranty, 2022 by Todd Strader +// SPDX-License-Identifier: CC0-1.0 + +module t (); + + typedef enum [1:0] {E0, E1, E2} enm_t; + + function automatic enm_t get_first(); + enm_t enm; + return enm.first; + endfunction + + localparam enm_t enum_first = get_first(); + + function automatic enm_t get_last(); + enm_t enm; + return enm.last; + endfunction + + localparam enm_t enum_last = get_last(); + + function automatic enm_t get_second(); + enm_t enm; + enm = enm.first; + return enm.next; + endfunction + + localparam enm_t enum_second = get_second(); + + function automatic string get_name(enm_t enm); + return enm.name; + endfunction + + localparam string e0_name = get_name(E0); + + initial begin + if (enum_first != E0) $stop; + if (enum_last != E2) $stop; + if (enum_second != E1) $stop; + if (e0_name != "E0") $stop; + $write("*-* All Finished *-*\n"); + $finish; + end +endmodule