sv: fix size cast clipping expression width
authorZachary Snow <zach@zachjs.com>
Thu, 30 Dec 2021 07:01:30 +0000 (00:01 -0700)
committerZachary Snow <zachary.j.snow@gmail.com>
Mon, 3 Jan 2022 15:17:35 +0000 (08:17 -0700)
CHANGELOG
frontends/ast/genrtlil.cc
tests/simple/lesser_size_cast.sv [new file with mode: 0644]

index ab1632a09b6983cec1d8f2189abffa106eb79584..9402f9bbb1ef60e271ad0d60d3f9e343b4ce820e 100644 (file)
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -16,6 +16,8 @@ Yosys 0.11 .. Yosys 0.12
     - Support parameters using struct as a wiretype
     - Fixed regression preventing the use array querying functions in case
       expressions and case item expressions
+    - Fixed static size casts inadvertently limiting the result width of binary
+      operations
 
  * New commands and options
     - Added "-genlib" option to "abc" pass
index 1fe74bb729a3406b5a22f7fa3d1a598e71555ed0..2788a850f8f9231b42e10d3b4c4edabd9e3d4dfa 100644 (file)
@@ -932,7 +932,8 @@ void AstNode::detectSignWidthWorker(int &width_hint, bool &sign_hint, bool *foun
                if (children.at(0)->type != AST_CONSTANT)
                        log_file_error(filename, location.first_line, "Static cast with non constant expression!\n");
                children.at(1)->detectSignWidthWorker(width_hint, sign_hint);
-               width_hint = children.at(0)->bitsAsConst().as_int();
+               this_width = children.at(0)->bitsAsConst().as_int();
+               width_hint = max(width_hint, this_width);
                if (width_hint <= 0)
                        log_file_error(filename, location.first_line, "Static cast with zero or negative size!\n");
                break;
diff --git a/tests/simple/lesser_size_cast.sv b/tests/simple/lesser_size_cast.sv
new file mode 100644 (file)
index 0000000..8c0bc98
--- /dev/null
@@ -0,0 +1,7 @@
+module top (
+    input signed [1:0] a,
+    input signed [2:0] b,
+    output signed [4:0] c
+);
+    assign c = 2'(a) * b;
+endmodule