From 6ee55406ec1b463c4163bc1695f383a15af7d80d Mon Sep 17 00:00:00 2001 From: Todd Strader Date: Fri, 24 Oct 2025 17:33:10 -0400 Subject: [PATCH] Intermediate variables for system calls during expression coverage --- src/V3Ast.h | 2 + src/V3AstNodeExpr.h | 53 +++++++++++++++++++ src/V3Coverage.cpp | 7 +-- .../t/t_sys_file_basic_cover_expr.out | 1 + test_regress/t/t_sys_file_basic_cover_expr.py | 26 +++++++++ 5 files changed, 86 insertions(+), 3 deletions(-) create mode 120000 test_regress/t/t_sys_file_basic_cover_expr.out create mode 100755 test_regress/t/t_sys_file_basic_cover_expr.py diff --git a/src/V3Ast.h b/src/V3Ast.h index a64ead1a1..18183a421 100644 --- a/src/V3Ast.h +++ b/src/V3Ast.h @@ -901,6 +901,8 @@ public: // isUnlikely handles $stop or similar statement which means an above IF // statement is unlikely to be taken virtual bool isUnlikely() const { return false; } + // Is an IEEE system function (versus internally-generated) + virtual bool isSystemFunc() const { return false; } virtual int instrCount() const { return 0; } // Iff node is identical to another node virtual bool isSame(const AstNode* samep) const { diff --git a/src/V3AstNodeExpr.h b/src/V3AstNodeExpr.h index 066e2be69..e8448a0bf 100644 --- a/src/V3AstNodeExpr.h +++ b/src/V3AstNodeExpr.h @@ -134,6 +134,7 @@ public: bool cleanRhs() const override { return false; } bool sizeMattersLhs() const override { return false; } bool sizeMattersRhs() const override { return false; } + bool isSystemFunc() const override { return true; } int instrCount() const override { return INSTR_COUNT_DBL_TRIG; } void numberOperate(V3Number&, const V3Number&, const V3Number&) override { V3ERROR_NA; } }; @@ -172,6 +173,7 @@ public: bool cleanRhs() const override { return false; } bool sizeMattersLhs() const override { return false; } bool sizeMattersRhs() const override { return false; } + bool isSystemFunc() const override { return true; } int instrCount() const override { return INSTR_COUNT_DBL_TRIG; } bool doubleFlavor() const override { return true; } }; @@ -347,6 +349,7 @@ protected: public: ASTGEN_MEMBERS_AstNodeTermop; + bool isSystemFunc() const override { return true; } }; class AstNodeTriop VL_NOT_FINAL : public AstNodeExpr { // Ternary expression @@ -403,6 +406,7 @@ public: bool sizeMattersLhs() const override { return false; } bool sizeMattersRhs() const override { return false; } bool sizeMattersThs() const override { return false; } + bool isSystemFunc() const override { return true; } int instrCount() const override { return INSTR_COUNT_DBL_TRIG; } void numberOperate(V3Number& out, const V3Number& lhs, const V3Number& rhs, const V3Number& ths) override { @@ -449,6 +453,7 @@ public: bool cleanOut() const override { return true; } bool cleanLhs() const override { return false; } bool sizeMattersLhs() const override { return false; } + bool isSystemFunc() const override { return true; } int instrCount() const override { return INSTR_COUNT_DBL_TRIG; } bool doubleFlavor() const override { return true; } }; @@ -1350,6 +1355,7 @@ public: int instrCount() const override { return widthInstrs() * 64; } bool isPredictOptimizable() const override { return false; } bool isPure() override { return false; } // SPECIAL: $display has 'visual' ordering + bool isSystemFunc() const override { return true; } bool sameNode(const AstNode* /*samep*/) const override { return true; } }; class AstFOpen final : public AstNodeExpr { @@ -1372,6 +1378,7 @@ public: bool isOutputter() override { return true; } bool isUnlikely() const override { return true; } bool sameNode(const AstNode* /*samep*/) const override { return true; } + bool isSystemFunc() const override { return true; } }; class AstFOpenMcd final : public AstNodeExpr { // @astgen op2 := filenamep : AstNodeExpr @@ -1415,6 +1422,7 @@ public: bool isOutputter() override { return true; } // SPECIAL: makes output bool cleanOut() const override { return false; } bool sameNode(const AstNode* /*samep*/) const override { return true; } + bool isSystemFunc() const override { return true; } }; class AstFRewind final : public AstNodeExpr { // @astgen op1 := filep : Optional[AstNode] @@ -1462,6 +1470,7 @@ public: } string text() const { return m_text; } // * = Text to display void text(const string& text) { m_text = text; } + bool isSystemFunc() const override { return true; } }; class AstFSeek final : public AstNodeExpr { // @astgen op1 := filep : AstNode // file (must be a VarRef) @@ -1484,6 +1493,7 @@ public: bool isOutputter() override { return true; } // SPECIAL: makes output bool cleanOut() const override { return false; } bool sameNode(const AstNode* /*samep*/) const override { return true; } + bool isSystemFunc() const override { return true; } }; class AstFTell final : public AstNodeExpr { // @astgen op1 := filep : AstNode // file (must be a VarRef) @@ -1503,6 +1513,7 @@ public: bool isUnlikely() const override { return true; } bool cleanOut() const override { return false; } bool sameNode(const AstNode* /*samep*/) const override { return true; } + bool isSystemFunc() const override { return true; } }; class AstFalling final : public AstNodeExpr { // Verilog $falling_gclk @@ -1519,6 +1530,7 @@ public: bool cleanOut() const override { V3ERROR_NA_RETURN(""); } int instrCount() const override { return widthInstrs(); } bool sameNode(const AstNode* /*samep*/) const override { return true; } + bool isSystemFunc() const override { return true; } }; class AstFell final : public AstNodeExpr { // Verilog $fell @@ -1537,6 +1549,7 @@ public: bool cleanOut() const override { V3ERROR_NA_RETURN(""); } int instrCount() const override { return widthInstrs(); } bool sameNode(const AstNode* /*samep*/) const override { return true; } + bool isSystemFunc() const override { return true; } }; class AstFuture final : public AstNodeExpr { // Verilog $future_gclk @@ -1555,6 +1568,7 @@ public: bool cleanOut() const override { V3ERROR_NA_RETURN(""); } int instrCount() const override { return widthInstrs(); } bool sameNode(const AstNode* /*samep*/) const override { return true; } + bool isSystemFunc() const override { return true; } }; class AstGatePin final : public AstNodeExpr { // Possibly expand a gate primitive input pin value to match the range of the gate primitive @@ -1835,6 +1849,7 @@ public: bool cleanOut() const override { V3ERROR_NA_RETURN(""); } int instrCount() const override { return widthInstrs(); } bool sameNode(const AstNode* /*samep*/) const override { return true; } + bool isSystemFunc() const override { return true; } }; class AstPatMember final : public AstNodeExpr { // Verilog '{a} or '{a{b}} @@ -1923,6 +1938,7 @@ public: bool isGateOptimizable() const override { return false; } bool isPredictOptimizable() const override { return false; } bool isPure() override { return !seedp(); } + bool isSystemFunc() const override { return true; } int instrCount() const override { return INSTR_COUNT_PLI; } bool sameNode(const AstNode* /*samep*/) const override { return true; } bool combinable(const AstRand* samep) const { @@ -1947,6 +1963,7 @@ public: bool cleanOut() const override { return false; } bool isGateOptimizable() const override { return false; } bool isPredictOptimizable() const override { return false; } + bool isSystemFunc() const override { return true; } int instrCount() const override { return INSTR_COUNT_PLI; } bool sameNode(const AstNode* /*samep*/) const override { return true; } }; @@ -1965,6 +1982,7 @@ public: bool cleanOut() const override { V3ERROR_NA_RETURN(""); } int instrCount() const override { return widthInstrs(); } bool sameNode(const AstNode* /*samep*/) const override { return true; } + bool isSystemFunc() const override { return true; } }; class AstRose final : public AstNodeExpr { // Verilog $rose @@ -1983,6 +2001,7 @@ public: bool cleanOut() const override { V3ERROR_NA_RETURN(""); } int instrCount() const override { return widthInstrs(); } bool sameNode(const AstNode* /*samep*/) const override { return true; } + bool isSystemFunc() const override { return true; } }; class AstSExpr final : public AstNodeExpr { // Sequence expression @@ -2055,6 +2074,7 @@ public: string emitVerilog() override { V3ERROR_NA_RETURN(""); } string emitC() override { V3ERROR_NA_RETURN(""); } bool cleanOut() const override { V3ERROR_NA_RETURN(true); } + bool isSystemFunc() const override { return true; } }; class AstSScanF final : public AstNodeExpr { // @astgen op1 := exprsp : List[AstNode] // VarRefs for results @@ -2086,6 +2106,7 @@ public: string text() const { return m_text; } // * = Text to display VTimescale timeunit() const { return m_timeunit; } void timeunit(const VTimescale& flag) { m_timeunit = flag; } + bool isSystemFunc() const override { return true; } }; class AstSampled final : public AstNodeExpr { // Verilog $sampled @@ -2102,6 +2123,7 @@ public: bool cleanOut() const override { V3ERROR_NA_RETURN(""); } int instrCount() const override { return 0; } bool sameNode(const AstNode* /*samep*/) const override { return true; } + bool isSystemFunc() const override { return true; } }; class AstScopeName final : public AstNodeExpr { // For display %m and DPI context imports @@ -2228,6 +2250,7 @@ public: bool cleanOut() const override { V3ERROR_NA_RETURN(""); } int instrCount() const override { return widthInstrs(); } bool sameNode(const AstNode* /*samep*/) const override { return true; } + bool isSystemFunc() const override { return true; } }; class AstStackTraceF final : public AstNodeExpr { // $stacktrace used as function @@ -2263,6 +2286,7 @@ public: bool cleanOut() const override { V3ERROR_NA_RETURN(""); } int instrCount() const override { return widthInstrs(); } bool sameNode(const AstNode* /*samep*/) const override { return true; } + bool isSystemFunc() const override { return true; } }; class AstStructSel final : public AstNodeExpr { // Unpacked struct/union member access @@ -2330,6 +2354,7 @@ public: bool isUnlikely() const override { return true; } bool cleanOut() const override { return true; } bool sameNode(const AstNode* /*samep*/) const override { return true; } + bool isSystemFunc() const override { return true; } }; class AstTestPlusArgs final : public AstNodeExpr { // Search expression. If nullptr then this is a $test$plusargs instead of $value$plusargs. @@ -2346,6 +2371,7 @@ public: bool isGateOptimizable() const override { return false; } bool isPredictOptimizable() const override { return false; } // but isPure() true + bool isSystemFunc() const override { return true; } bool cleanOut() const override { return true; } bool sameNode(const AstNode* /*samep*/) const override { return true; } }; @@ -2380,6 +2406,7 @@ public: string emitC() override { V3ERROR_NA_RETURN(""); } string emitSimpleOperator() override { V3ERROR_NA_RETURN(""); } bool cleanOut() const override { return true; } + bool isSystemFunc() const override { return true; } int instrCount() const override { return widthInstrs(); } bool sameNode(const AstNode* /*samep*/) const override { return true; } }; @@ -2396,6 +2423,7 @@ public: string emitC() override { V3ERROR_NA_RETURN(""); } string emitSimpleOperator() override { V3ERROR_NA_RETURN(""); } bool cleanOut() const override { return true; } + bool isSystemFunc() const override { return true; } int instrCount() const override { return widthInstrs(); } bool sameNode(const AstNode* /*samep*/) const override { return true; } VTimescale timeunit() const { return m_timeunit; } @@ -2450,6 +2478,7 @@ public: bool isGateOptimizable() const override { return false; } bool isPredictOptimizable() const override { return false; } bool isPure() override { return !outp(); } + bool isSystemFunc() const override { return true; } bool cleanOut() const override { return true; } bool sameNode(const AstNode* /*samep*/) const override { return true; } }; @@ -2553,6 +2582,7 @@ public: bool cleanRhs() const override { return true; } bool sizeMattersLhs() const override { return false; } bool sizeMattersRhs() const override { return false; } + bool isSystemFunc() const override { return true; } int instrCount() const override { return widthInstrs() * 20; } bool isPure() override { return false; } }; @@ -2731,6 +2761,7 @@ public: bool cleanRhs() const override { return true; } bool sizeMattersLhs() const override { return false; } bool sizeMattersRhs() const override { return false; } + bool isSystemFunc() const override { return true; } int instrCount() const override { return widthInstrs() * 64; } AstNode* strgp() const { return lhsp(); } AstNode* filep() const { return rhsp(); } @@ -2751,6 +2782,7 @@ public: bool cleanRhs() const override { return true; } bool sizeMattersLhs() const override { return false; } bool sizeMattersRhs() const override { return false; } + bool isSystemFunc() const override { return true; } int instrCount() const override { return widthInstrs() * 64; } bool isPredictOptimizable() const override { return false; } bool isPure() override { return false; } // SPECIAL: $display has 'visual' ordering @@ -3667,6 +3699,7 @@ public: bool isGateOptimizable() const override { return false; } bool isPredictOptimizable() const override { return false; } bool isPure() override { return false; } + bool isSystemFunc() const override { return true; } int instrCount() const override { return INSTR_COUNT_PLI; } }; @@ -4452,6 +4485,7 @@ public: bool sizeMattersRhs() const override { return false; } bool sizeMattersThs() const override { return false; } bool sizeMattersFhs() const override { return false; } + bool isSystemFunc() const override { return true; } int instrCount() const override { return widthInstrs() * 16; } }; @@ -4466,6 +4500,7 @@ public: string emitVerilog() override { return "%f$inferred_disable"; } string emitC() override { V3ERROR_NA_RETURN(""); } bool cleanOut() const override { return true; } + bool isSystemFunc() const override { return true; } bool sameNode(const AstNode* /*samep*/) const override { return true; } }; class AstTime final : public AstNodeTermop { @@ -4482,6 +4517,7 @@ public: bool cleanOut() const override { return true; } bool isGateOptimizable() const override { return false; } bool isPredictOptimizable() const override { return false; } + bool isSystemFunc() const override { return true; } int instrCount() const override { return INSTR_COUNT_TIME; } bool sameNode(const AstNode* /*samep*/) const override { return true; } void dump(std::ostream& str = std::cout) const override; @@ -4503,6 +4539,7 @@ public: bool cleanOut() const override { return true; } bool isGateOptimizable() const override { return false; } bool isPredictOptimizable() const override { return false; } + bool isSystemFunc() const override { return true; } int instrCount() const override { return INSTR_COUNT_TIME; } bool sameNode(const AstNode* /*samep*/) const override { return true; } void dump(std::ostream& str = std::cout) const override; @@ -4795,6 +4832,7 @@ public: bool cleanLhs() const override { return false; } // Eliminated before matters bool sizeMattersLhs() const override { return false; } // Eliminated before matters int instrCount() const override { return INSTR_COUNT_DBL; } + bool isSystemFunc() const override { return true; } }; class AstCAwait final : public AstNodeUniop { // Emit C++'s co_await expression @@ -4867,6 +4905,7 @@ public: bool cleanLhs() const override { return true; } bool sizeMattersLhs() const override { return false; } int instrCount() const override { return widthInstrs() * 16; } + bool isSystemFunc() const override { return true; } }; class AstCastWrap final : public AstNodeUniop { // A cast which has been expanded and the LHSP does all the lifting @@ -4896,6 +4935,7 @@ public: bool cleanLhs() const override { return true; } bool sizeMattersLhs() const override { return false; } int instrCount() const override { return widthInstrs() * 16; } + bool isSystemFunc() const override { return true; } }; class AstCvtPackString final : public AstNodeUniop { // Convert to Verilator Packed String (aka verilog "string") @@ -4977,6 +5017,7 @@ public: int instrCount() const override { return widthInstrs() * 16; } bool isPredictOptimizable() const override { return false; } bool isPure() override { return false; } // SPECIAL: $display has 'visual' ordering + bool isSystemFunc() const override { return true; } AstNode* filep() const { return lhsp(); } }; class AstFGetC final : public AstNodeUniop { @@ -4995,6 +5036,7 @@ public: bool isPredictOptimizable() const override { return false; } bool isPure() override { return false; } // SPECIAL: $display has 'visual' ordering AstNode* filep() const { return lhsp(); } + bool isSystemFunc() const override { return true; } }; class AstISToRD final : public AstNodeUniop { // $itor where lhs is signed @@ -5012,6 +5054,7 @@ public: bool cleanLhs() const override { return true; } bool sizeMattersLhs() const override { return false; } int instrCount() const override { return INSTR_COUNT_DBL; } + bool isSystemFunc() const override { return true; } }; class AstIToRD final : public AstNodeUniop { // $itor where lhs is unsigned @@ -5028,6 +5071,7 @@ public: bool cleanLhs() const override { return true; } bool sizeMattersLhs() const override { return false; } int instrCount() const override { return INSTR_COUNT_DBL; } + bool isSystemFunc() const override { return true; } }; class AstIsUnbounded final : public AstNodeUniop { // True if is unbounded ($) @@ -5046,6 +5090,7 @@ public: bool cleanOut() const override { return false; } bool cleanLhs() const override { return false; } bool sizeMattersLhs() const override { return false; } + bool isSystemFunc() const override { return true; } }; class AstIsUnknown final : public AstNodeUniop { // True if any unknown bits @@ -5061,6 +5106,7 @@ public: bool cleanOut() const override { return false; } bool cleanLhs() const override { return false; } bool sizeMattersLhs() const override { return false; } + bool isSystemFunc() const override { return true; } }; class AstLenN final : public AstNodeUniop { // Length of a string @@ -5191,6 +5237,7 @@ public: bool cleanLhs() const override { return true; } bool sizeMattersLhs() const override { return false; } int instrCount() const override { return widthInstrs() * 4; } + bool isSystemFunc() const override { return true; } }; class AstOneHot0 final : public AstNodeUniop { // True if only single bit, or no bits set in vector @@ -5207,6 +5254,7 @@ public: bool cleanLhs() const override { return true; } bool sizeMattersLhs() const override { return false; } int instrCount() const override { return widthInstrs() * 3; } + bool isSystemFunc() const override { return true; } }; class AstRToIRoundS final : public AstNodeUniop { // Convert real to integer, with arbitrary sized output (not just "integer" format) @@ -5225,6 +5273,7 @@ public: bool cleanLhs() const override { return false; } bool sizeMattersLhs() const override { return false; } int instrCount() const override { return INSTR_COUNT_DBL; } + bool isSystemFunc() const override { return true; } }; class AstRToIS final : public AstNodeUniop { // $rtoi(lhs) @@ -5241,6 +5290,7 @@ public: bool cleanLhs() const override { return false; } // Eliminated before matters bool sizeMattersLhs() const override { return false; } // Eliminated before matters int instrCount() const override { return INSTR_COUNT_DBL; } + bool isSystemFunc() const override { return true; } }; class AstRealToBits final : public AstNodeUniop { public: @@ -5256,6 +5306,7 @@ public: bool cleanLhs() const override { return false; } // Eliminated before matters bool sizeMattersLhs() const override { return false; } // Eliminated before matters int instrCount() const override { return INSTR_COUNT_DBL; } + bool isSystemFunc() const override { return true; } }; class AstRedAnd final : public AstNodeUniop { public: @@ -5337,6 +5388,7 @@ public: bool cleanLhs() const override { return false; } // Eliminated before matters bool sizeMattersLhs() const override { return true; } // Eliminated before matters int instrCount() const override { return 0; } + bool isSystemFunc() const override { return true; } }; class AstTimeImport final : public AstNodeUniop { // Take a constant that represents a time and needs conversion based on time units @@ -5419,6 +5471,7 @@ public: bool cleanLhs() const override { return false; } // Eliminated before matters bool sizeMattersLhs() const override { return true; } // Eliminated before matters int instrCount() const override { return 0; } + bool isSystemFunc() const override { return true; } }; // === AstNodeSystemUniopD === diff --git a/src/V3Coverage.cpp b/src/V3Coverage.cpp index 6a72acd00..809774657 100644 --- a/src/V3Coverage.cpp +++ b/src/V3Coverage.cpp @@ -141,7 +141,7 @@ class CoverageVisitor final : public VNVisitor { const VNUser2InUse m_inuser2; V3UniqueNames m_exprTempNames; // For generating unique temporary variable names used by // expression coverage - std::unordered_map, AstVar*> m_funcTemps; + std::unordered_map, AstVar*> m_funcTemps; // STATE - across all visitors int m_nextHandle = 0; @@ -784,8 +784,9 @@ class CoverageVisitor final : public VNVisitor { comment += (first ? "" : " && ") + term.m_emitV + "==" + (term.m_objective ? "1" : "0"); AstNodeExpr* covExprp = nullptr; - if (AstFuncRef* const frefp = VN_CAST(term.m_exprp, FuncRef)) { - AstNodeDType* const dtypep = frefp->taskp()->fvarp()->dtypep(); + if (VN_IS(term.m_exprp, FuncRef) || term.m_exprp->isSystemFunc()) { + AstNodeExpr* const frefp = term.m_exprp; + AstNodeDType* const dtypep = frefp->dtypep(); const auto pair = m_funcTemps.emplace(*frefp, nullptr); AstVar* varp = pair.first->second; if (pair.second) { diff --git a/test_regress/t/t_sys_file_basic_cover_expr.out b/test_regress/t/t_sys_file_basic_cover_expr.out new file mode 120000 index 000000000..36fc2f5aa --- /dev/null +++ b/test_regress/t/t_sys_file_basic_cover_expr.out @@ -0,0 +1 @@ +t_sys_file_basic.out \ No newline at end of file diff --git a/test_regress/t/t_sys_file_basic_cover_expr.py b/test_regress/t/t_sys_file_basic_cover_expr.py new file mode 100755 index 000000000..0c86310dc --- /dev/null +++ b/test_regress/t/t_sys_file_basic_cover_expr.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python3 +# DESCRIPTION: Verilator: Verilog Test driver/expect definition +# +# Copyright 2024 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 + +import vltest_bootstrap + +test.scenarios("simulator") +test.top_filename = "t/t_sys_file_basic.v" + +test.unlink_ok(test.obj_dir + "/t_sys_file_basic_test.log") + +test.compile( + verilator_flags2=["--coverage-expr"], + # Build without cached objects, see bug363 + make_flags=["VM_PARALLEL_BUILDS=0"], +) + +test.execute() +test.files_identical(test.obj_dir + "/t_sys_file_basic_test.log", test.golden_filename) + +test.passes()