diff --git a/src/V3AstNodeExpr.h b/src/V3AstNodeExpr.h index d306f0171..5b68e2914 100644 --- a/src/V3AstNodeExpr.h +++ b/src/V3AstNodeExpr.h @@ -4543,7 +4543,7 @@ public: dtypeSetString(); } ASTGEN_MEMBERS_AstCvtPackString; - void numberOperate(V3Number& out, const V3Number& lhs) override { V3ERROR_NA; } + void numberOperate(V3Number& out, const V3Number& lhs) override { out.opAssign(lhs); } string emitVerilog() override { return "%f$_CAST(%l)"; } string emitC() override { return "VL_CVT_PACK_STR_N%lq(%lW, %li)"; } bool cleanOut() const override { return true; } diff --git a/src/V3Number.cpp b/src/V3Number.cpp index 54342a561..4772120f9 100644 --- a/src/V3Number.cpp +++ b/src/V3Number.cpp @@ -2201,8 +2201,8 @@ V3Number& V3Number::opAssignNonXZ(const V3Number& lhs, bool ignoreXZ) { m_data.m_isNull = true; } else if (isString()) { if (VL_UNLIKELY(!lhs.isString())) { - // Non-compatible types, erase value. - m_data.str() = ""; + // Numbers can still be strings + m_data.str() = lhs.toString(); } else { m_data.str() = lhs.m_data.str(); } diff --git a/test_regress/t/t_const_string_func.pl b/test_regress/t/t_const_string_func.pl new file mode 100755 index 000000000..859050d63 --- /dev/null +++ b/test_regress/t/t_const_string_func.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 2023 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_const_string_func.v b/test_regress/t/t_const_string_func.v new file mode 100644 index 000000000..c3ed6d12b --- /dev/null +++ b/test_regress/t/t_const_string_func.v @@ -0,0 +1,20 @@ +// DESCRIPTION: Verilator: constant string functions +// +// This file ONLY is placed into the Public Domain, for any use, +// without warranty, 2023 by Wilson Snyder +// SPDX-License-Identifier: CC0-1.0 + +module t (); + + function automatic string foo_func(); + foo_func = "FOO"; + endfunction + + localparam string the_foo = foo_func(); + + initial begin + if (the_foo != "FOO") $stop; + $write("*-* All Finished *-*\n"); + $finish; + end +endmodule