From 81d822562a490051d93fed4557b4e841476259c8 Mon Sep 17 00:00:00 2001 From: Zachary Snow Date: Thu, 7 Dec 2023 23:43:19 -0500 Subject: [PATCH] fix stringToInteger byte order --- CHANGELOG.md | 1 + src/Convert/ExprUtils.hs | 4 +--- test/core/constexpr.sv | 12 ++++++++---- test/core/string_byte_order.sv | 8 ++++++++ test/core/string_byte_order.v | 8 ++++++++ test/core/string_byte_order.vh | 7 +++++++ 6 files changed, 33 insertions(+), 7 deletions(-) create mode 100644 test/core/string_byte_order.sv create mode 100644 test/core/string_byte_order.v create mode 100644 test/core/string_byte_order.vh diff --git a/CHANGELOG.md b/CHANGELOG.md index adb272e..c2421e5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ * Fixed an issue that prevented parsing tasks and functions with `inout` ports * Fixed conflicting genvar names when inlining interfaces and modules that use them; all genvars are now given a design-wide unique name +* Fixed byte order of strings in size casts * Fixed unconverted structs within explicit type casts * Fixed unconverted multidimensional struct fields within dimension queries * Fixed non-typenames (e.g., from packages or subsequent declarations) diff --git a/src/Convert/ExprUtils.hs b/src/Convert/ExprUtils.hs index a57b876..3612d2e 100644 --- a/src/Convert/ExprUtils.hs +++ b/src/Convert/ExprUtils.hs @@ -288,9 +288,7 @@ stringToNumber str = -- convert a string to big integer stringToInteger :: String -> Integer -stringToInteger [] = 0 -stringToInteger (x : xs) = - fromIntegral (ord x) + (256 :: Integer) * stringToInteger xs +stringToInteger = foldl ((+) . (256 *)) 0 . map (fromIntegral . ord) -- cast string to number at least as big as the width of the given number sizeStringAs :: String -> Number -> Expr diff --git a/test/core/constexpr.sv b/test/core/constexpr.sv index 94d4d65..f429c59 100644 --- a/test/core/constexpr.sv +++ b/test/core/constexpr.sv @@ -1,8 +1,12 @@ // This verifies that sv2v can evaluate certain constant expressions by // producing iverilog-incompatible code if the expression cannot be simplified // or is evaluated incorrectly. -`define ASSERT_TRUE(expr) if (expr) begin end else begin shortreal x; end -`define ASSERT_FALSE(expr) if (expr) begin shortreal x; end +`define ASSERT_FALSE(expr) \ + if (expr) begin \ + initial $display("fail"); \ + shortreal x; \ + end +`define ASSERT_TRUE(expr) `ASSERT_FALSE(!(expr)) module top; `ASSERT_TRUE(1) @@ -19,8 +23,8 @@ module top; `ASSERT_TRUE("invv" != "inv") `ASSERT_TRUE("0inv" != "inv") - `ASSERT_TRUE(24'("inv0") == "inv") - `ASSERT_TRUE(24'("0inv") != "inv") + `ASSERT_TRUE(24'("inv0") != "inv") + `ASSERT_TRUE(24'("0inv") == "inv") `ASSERT_FALSE("inv" == 0) `ASSERT_FALSE("inv" == '0) `ASSERT_FALSE('0 == "inv") diff --git a/test/core/string_byte_order.sv b/test/core/string_byte_order.sv new file mode 100644 index 0000000..16a9407 --- /dev/null +++ b/test/core/string_byte_order.sv @@ -0,0 +1,8 @@ +module top; + localparam a = "abcd"; + localparam b = 64'("abcd"); + logic [3:0][7:0] c = "abcd"; + integer d = b; // truncate + localparam e = 32'("abcd"); + `include "string_byte_order.vh" +endmodule diff --git a/test/core/string_byte_order.v b/test/core/string_byte_order.v new file mode 100644 index 0000000..0a539f9 --- /dev/null +++ b/test/core/string_byte_order.v @@ -0,0 +1,8 @@ +module top; + localparam a = "abcd"; + localparam [63:0] b = "abcd"; + reg [3:0][7:0] c = "abcd"; + integer d = b; // truncate + localparam [31:0] e = "abcd"; + `include "string_byte_order.vh" +endmodule diff --git a/test/core/string_byte_order.vh b/test/core/string_byte_order.vh new file mode 100644 index 0000000..48ac3c2 --- /dev/null +++ b/test/core/string_byte_order.vh @@ -0,0 +1,7 @@ +`define TEST(str) initial \ + $display("%s %b %c %c", str, str, str[0], str[3]); +`TEST(a) +`TEST(b) +`TEST(c) +`TEST(d) +`TEST(e)