@@ -11,6 +11,7 @@ Author: Daniel Kroening, kroening@kroening.com
1111
1212#include < util/bitvector_expr.h>
1313#include < util/bitvector_types.h>
14+ #include < util/c_types.h>
1415#include < util/ebmc_util.h>
1516#include < util/expr_util.h>
1617#include < util/identifier.h>
@@ -113,6 +114,21 @@ exprt verilog_synthesist::from_bitvector(
113114
114115 return struct_exprt{std::move (field_values), struct_type};
115116 }
117+ else if (dest.id () == ID_union)
118+ {
119+ // We use the first field of the union.
120+ // All fields of a SystemVerilog packed union must have the same width.
121+ const auto &union_type = to_union_type (dest);
122+ DATA_INVARIANT (
123+ !union_type.components ().empty (),
124+ " union type must have at least one field" );
125+ auto &field = union_type.components ().front ();
126+
127+ // rec. call
128+ auto field_value = from_bitvector (src, offset, field.type ());
129+
130+ return union_exprt{field.get_name (), std::move (field_value), union_type};
131+ }
116132 else
117133 {
118134 return extract (src, offset, dest);
@@ -152,6 +168,16 @@ exprt verilog_synthesist::to_bitvector(const exprt &src)
152168 auto width_int = numeric_cast_v<std::size_t >(get_width (src));
153169 return concatenation_exprt{std::move (field_values), bv_typet{width_int}};
154170 }
171+ else if (src_type.id () == ID_union)
172+ {
173+ const auto &union_type = to_union_type (src_type);
174+ DATA_INVARIANT (
175+ !union_type.components ().empty (),
176+ " union type must have at least one field" );
177+ auto &field = union_type.components ().front ();
178+ auto member = member_exprt{src, field};
179+ return to_bitvector (member); // rec. call
180+ }
155181 else
156182 {
157183 return src;
@@ -302,13 +328,13 @@ exprt verilog_synthesist::synth_expr(exprt expr, symbol_statet symbol_state)
302328 auto aval_bval_type = lower_to_aval_bval (dest_type);
303329 return aval_bval_conversion (typecast_expr.op (), aval_bval_type);
304330 }
305- else if (dest_type.id () == ID_struct)
331+ else if (dest_type.id () == ID_struct || dest_type. id () == ID_union )
306332 {
307333 return from_bitvector (typecast_expr.op (), 0 , dest_type);
308334 }
309335 else
310336 {
311- if (src_type.id () == ID_struct)
337+ if (src_type.id () == ID_struct || src_type. id () == ID_union )
312338 {
313339 return extract (to_bitvector (typecast_expr.op ()), 0 , dest_type);
314340 }
0 commit comments