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
2 changes: 1 addition & 1 deletion regression/verilog/structs/structs4.sv
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module main;
s.field3 = 'b1110011;
end

// structs can be converted without cast to bit-vectors
// packed structs can be converted without cast to bit-vectors
wire [8:0] w = s;

// Expected to pass.
Expand Down
7 changes: 7 additions & 0 deletions regression/verilog/structs/unpacked_struct1.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CORE
unpacked_struct1.sv

^file .* line 9: failed to convert `struct' to `\[8:0\]'$
^EXIT=2$
^SIGNAL=0$
--
11 changes: 11 additions & 0 deletions regression/verilog/structs/unpacked_struct1.sv
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module main;

// an unpacked struct
struct {
bit field;
} s;

// unpacked structs cannot be converted to bit-vectors
wire [8:0] w = s;

endmodule
5 changes: 4 additions & 1 deletion src/verilog/expr2verilog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2149,7 +2149,10 @@ std::string expr2verilogt::convert(const typet &type)
}
else if(type.id() == ID_struct)
{
return "struct";
if(type.get_bool(ID_packed))
return "struct packed";
else
return "struct";
}
else if(type.id() == ID_union)
{
Expand Down
2 changes: 2 additions & 0 deletions src/verilog/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -1552,6 +1552,8 @@ data_type:
| struct_union packed_opt signing_opt
'{' struct_union_member_brace '}' packed_dimension_brace
{ $$=$1;
if(stack_expr($2).id() == ID_packed)
stack_type($1).set(ID_packed, true);
addswap($$, ID_declaration_list, $5); }
| TOK_ENUM enum_base_type_opt '{' enum_name_declaration_list '}'
{ // Like in C, these do _not_ create a scope.
Expand Down
10 changes: 8 additions & 2 deletions src/verilog/verilog_elaborate_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -425,8 +425,14 @@ typet verilog_typecheck_exprt::elaborate_type(const typet &src)
}
}

return struct_union_typet{src.id(), std::move(components)}
.with_source_location(src.source_location());
auto result =
struct_union_typet{src.id(), std::move(components)}.with_source_location(
src.source_location());

if(src.get_bool(ID_packed))
result.set(ID_packed, true);

return result;
}
else if(src.id() == ID_verilog_string)
{
Expand Down
2 changes: 1 addition & 1 deletion src/verilog/verilog_typecheck_expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2481,7 +2481,7 @@ void verilog_typecheck_exprt::struct_decay(exprt &expr) const
// Verilog packed struct types decay to a vector type [$bits(t)-1:0]
// when used in relational or arithmetic expressions.
auto &type = expr.type();
if(type.id() == ID_struct)
if(type.id() == ID_struct && type.get_bool(ID_packed))
{
auto new_type =
unsignedbv_typet{numeric_cast_v<std::size_t>(get_width(type))};
Expand Down
Loading