diff --git a/source/ast/builtins/QueryFuncs.cpp b/source/ast/builtins/QueryFuncs.cpp index 6addaa92c..5f67a16ac 100644 --- a/source/ast/builtins/QueryFuncs.cpp +++ b/source/ast/builtins/QueryFuncs.cpp @@ -61,7 +61,9 @@ class BitsFunction : public SystemSubroutine { width = cv.getBitstreamWidth(); } - // TODO: handle overflow of 32 bits + // Note: we convert the size down to a 32-bit result. This can result + // in (defined) overflow, which matches the behavior of all other + // tools that I tried. return SVInt(32, width, true); } }; diff --git a/tests/unittests/ast/SystemFuncTests.cpp b/tests/unittests/ast/SystemFuncTests.cpp index 59a1c3424..19ceb27b6 100644 --- a/tests/unittests/ast/SystemFuncTests.cpp +++ b/tests/unittests/ast/SystemFuncTests.cpp @@ -1303,3 +1303,17 @@ endmodule compilation.addSyntaxTree(tree); NO_COMPILATION_ERRORS; } + +TEST_CASE("$bits of >32bit sized type") { + auto tree = SyntaxTree::fromText(R"( +logic [7:0] a [2147483647]; +localparam p = $bits(a); +)"); + + Compilation compilation; + compilation.addSyntaxTree(tree); + NO_COMPILATION_ERRORS; + + auto& p = compilation.getCompilationUnits()[0]->find("p"); + CHECK(p.getValue().integer() == -8); +}