Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions regression/verilog/arrays/array_conversion3.desc
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ array_conversion3.sv
^\[main\.p13\] main\.array1\[2\] == 3: PROVED .*$
^\[main\.p14\] main\.array1\[3\] == 4: PROVED .*$
^\[main\.p15\] main\.array1 == 32'h1020304: PROVED .*$
^\[main\.p21\] main\.array2\[0\] == 4: REFUTED$
^\[main\.p22\] main\.array2\[1\] == 3: REFUTED$
^\[main\.p23\] main\.array2\[2\] == 2: REFUTED$
^\[main\.p24\] main\.array2\[3\] == 1: REFUTED$
^\[main\.p21\] main\.array2\[0\] == 4: PROVED .*$
^\[main\.p22\] main\.array2\[1\] == 3: PROVED .*$
^\[main\.p23\] main\.array2\[2\] == 2: PROVED .*$
^\[main\.p24\] main\.array2\[3\] == 1: PROVED .*$
^\[main\.p25\] main\.array2 == 32'h1020304: PROVED .*$
^EXIT=10$
^EXIT=0$
^SIGNAL=0$
--
--
3 changes: 1 addition & 2 deletions regression/verilog/arrays/packed_direction1.desc
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
KNOWNBUG
CORE
packed_direction1.sv
--module main
^EXIT=0$
^SIGNAL=0$
--
--
This gives the wrong answer.
6 changes: 4 additions & 2 deletions regression/verilog/arrays/unpacked_direction1.desc
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
KNOWNBUG
CORE
unpacked_direction1.sv
--module main
^\[main\.p0\] main\.my_array0\[0\] == 1: PROVED .*$
^\[main\.p1\] main\.my_array1\[0\] == 1: PROVED .*$
^\[main\.p2\] main\.my_array2\[0 \+ 5 - 3'b001 - 0\] == 5: PROVED .*$
^EXIT=0$
^SIGNAL=0$
--
--
This gives the wrong answer.
24 changes: 13 additions & 11 deletions regression/verilog/structs/array_in_struct1.sv
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@ module main;
bit [3:0] [31:0] array2;
} s;

initial s = '{ '{ 1, 2, 3, 4 }, '{ 1, 2, 3, 4 } };
initial begin
s = '{ '{ 1, 2, 3, 4 }, '{ 1, 2, 3, 4 } };

// Expected to pass.
p0: assert final ($bits(s) == 4 * 32 + 4 * 32);
p11: assert property (s.array1[0] == 1);
p12: assert property (s.array1[1] == 2);
p13: assert property (s.array1[2] == 3);
p14: assert property (s.array1[3] == 4);
p21: assert property (s.array2[0] == 1);
p22: assert property (s.array2[1] == 2);
p23: assert property (s.array2[2] == 3);
p24: assert property (s.array2[3] == 4);
// Expected to pass.
p0: assert($bits(s) == 4 * 32 + 4 * 32);
p11: assert(s.array1[0] == 1);
p12: assert(s.array1[1] == 2);
p13: assert(s.array1[2] == 3);
p14: assert(s.array1[3] == 4);
p21: assert(s.array2[0] == 4);
p22: assert(s.array2[1] == 3);
p23: assert(s.array2[2] == 2);
p24: assert(s.array2[3] == 1);
end

endmodule
1 change: 1 addition & 0 deletions src/verilog/verilog_elaborate_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ typet verilog_typecheck_exprt::convert_packed_array_type(

array_typet result{element_type, size};
result.set(ID_offset, from_integer(offset, integer_typet()));
result.set(ID_C_increasing, range.increasing());
result.set(ID_C_verilog_type, ID_verilog_packed_array);

return std::move(result).with_source_location(source_location);
Expand Down
2 changes: 1 addition & 1 deletion src/verilog/verilog_lowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ exprt to_bitvector(const exprt &src)
bool is_packed =
array_type.get(ID_C_verilog_type) == ID_verilog_packed_array;

if(is_packed && !array_type.get_bool(ID_C_increasing))
if(is_packed && array_type.get_bool(ID_C_increasing))
{
for(std::size_t index = 0; index < size_int; index++)
{
Expand Down
46 changes: 40 additions & 6 deletions src/verilog/verilog_typecheck_expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,18 @@ void verilog_typecheck_exprt::assignment_conversion(
assignment_conversion(rhs.operands()[i], element_type);
}

// For packed arrays with descending range (e.g., [3:0]),
// the assignment pattern lists elements from the left (highest)
// index to the right (lowest), but internally index 0 must
// correspond to Verilog index 0 (the lowest). Reverse the
// operands so that internal[0] = last element = Verilog index 0.
if(
array_type.get(ID_C_verilog_type) == ID_verilog_packed_array &&
!array_type.get_bool(ID_C_increasing))
{
std::reverse(rhs.operands().begin(), rhs.operands().end());
}

// turn into array expression
rhs.id(ID_array);
rhs.type() = lhs_type;
Expand Down Expand Up @@ -3000,13 +3012,35 @@ exprt verilog_typecheck_exprt::convert_bit_select_expr(binary_exprt expr)
typet _index_type = index_type(array_type);
op1 = typecast_exprt{op1, _index_type};

if(
array_type.get_bool(ID_C_increasing) &&
array_type.get(ID_C_verilog_type) == ID_verilog_packed_array)
// For unpacked arrays, the internal representation stores
// elements starting from the left index of the range.
// We need to adjust the Verilog index to the internal index.
if(array_type.get(ID_C_verilog_type) == ID_verilog_unpacked_array)
{
expr.op1() = minus_exprt{
minus_exprt{typecast_exprt{array_type.size(), _index_type}, expr.op1()},
from_integer(1, _index_type)};
auto offset_expr = static_cast<const exprt &>(array_type.find(ID_offset));

if(array_type.get_bool(ID_C_increasing))
{
// ascending range [l:r] with l<r, e.g., [0:4]
// internal index = verilog_index - offset
if(!offset_expr.is_zero())
{
expr.op1() =
minus_exprt{expr.op1(), typecast_exprt{offset_expr, _index_type}};
}
}
else
{
// descending range [l:r] with l>=r, e.g., [4:0]
// internal index = (offset + size - 1) - verilog_index
expr.op1() = minus_exprt{
minus_exprt{
plus_exprt{
typecast_exprt{offset_expr, _index_type},
typecast_exprt{array_type.size(), _index_type}},
from_integer(1, _index_type)},
expr.op1()};
}
}

expr.type() = array_type.element_type();
Expand Down
Loading