Skip to content

Commit bd5891b

Browse files
authored
Merge pull request #560 from diffblue/unions
SystemVerilog Unions
2 parents c9fe05c + e88774a commit bd5891b

12 files changed

+153
-7
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
KNOWNBUG
2+
unions1.sv
3+
--bound 0
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
--
7+
--
8+
cast bitvector to union missing

regression/verilog/unions/unions1.sv

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module main;
2+
3+
union packed {
4+
bit [6:0] field1;
5+
bit [6:0] field2;
6+
} u;
7+
8+
// bit-vectors can be converted without cast to packed unions
9+
initial u = 7'b1010101;
10+
11+
// Expected to pass.
12+
p0: assert property ($bits(u) == 7);
13+
p1: assert property (u.field1 == 7'b1010101);
14+
p2: assert property (u.field2 == 7'b1010101);
15+
16+
endmodule
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CORE
2+
unions2.sv
3+
--bound 0
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
--
7+
--

regression/verilog/unions/unions2.sv

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module main;
2+
3+
union {
4+
bit field1;
5+
bit [6:0] field2;
6+
} u;
7+
8+
initial begin
9+
u.field2 = 0;
10+
u.field1 = 1;
11+
end
12+
13+
// Expected to pass.
14+
p1: assert property (u.field1 == 1);
15+
p2: assert property (u.field2 == 1);
16+
p3: assert property ($bits(u) == 7);
17+
18+
endmodule
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CORE
2+
unions3.sv
3+
--bound 0
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
--
7+
--

regression/verilog/unions/unions3.sv

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module main;
2+
3+
union packed {
4+
// a struct inside a union
5+
struct packed {
6+
bit [7:0] field1;
7+
} field1;
8+
9+
bit [7:0] field2;
10+
} u;
11+
12+
initial begin
13+
u.field1.field1 = 123;
14+
end
15+
16+
// Expected to pass.
17+
p1: assert property (u.field2 == 123);
18+
19+
endmodule

src/hw_cbmc_irep_ids.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ IREP_ID_ONE(automatic)
150150
IREP_ID_TWO(C_verilog_type, #verilog_type)
151151
IREP_ID_TWO(C_verilog_aval_bval, #verilog_aval_bval)
152152
IREP_ID_ONE(verilog_enum)
153+
IREP_ID_ONE(verilog_packed)
153154
IREP_ID_ONE(verilog_packed_array)
154155
IREP_ID_ONE(verilog_type_reference)
155156
IREP_ID_ONE(verilog_unpacked_array)

src/verilog/verilog_synthesis.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -857,7 +857,9 @@ void verilog_synthesist::assignment_rec(
857857
const exprt &lhs_compound = member_expr.struct_op();
858858
auto component_name = member_expr.get_component_name();
859859

860-
if(lhs_compound.type().id() == ID_struct)
860+
if(
861+
lhs_compound.type().id() == ID_struct ||
862+
lhs_compound.type().id() == ID_union)
861863
{
862864
// turn
863865
// s.m=e

src/verilog/verilog_typecheck_base.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,17 @@ Author: Daniel Kroening, [email protected]
66
77
\*******************************************************************/
88

9-
#include <cassert>
9+
#include "verilog_typecheck_base.h"
1010

1111
#include <util/ebmc_util.h>
1212
#include <util/expr_util.h>
1313
#include <util/prefix.h>
1414
#include <util/std_types.h>
1515

1616
#include "expr2verilog.h"
17-
#include "verilog_typecheck_base.h"
17+
#include "verilog_types.h"
18+
19+
#include <cassert>
1820

1921
/*******************************************************************\
2022
@@ -200,6 +202,15 @@ verilog_typecheck_baset::get_width_opt(const typet &type)
200202
return sum;
201203
}
202204

205+
if(type.id() == ID_union)
206+
{
207+
// find the biggest
208+
mp_integer max = 0;
209+
for(auto &component : to_verilog_union_type(type).components())
210+
max = std::max(max, get_width(component.type()));
211+
return max;
212+
}
213+
203214
if(type.id() == ID_verilog_shortint)
204215
return 16;
205216
else if(type.id() == ID_verilog_int)

src/verilog/verilog_typecheck_base.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Author: Daniel Kroening, [email protected]
99
#ifndef CPROVER_VERILOG_TYPECHEK_BASE_H
1010
#define CPROVER_VERILOG_TYPECHEK_BASE_H
1111

12+
#include <util/expr.h>
1213
#include <util/mp_arith.h>
1314
#include <util/namespace.h>
1415
#include <util/typecheck.h>

0 commit comments

Comments
 (0)