@@ -56,6 +56,24 @@ static boundst map_bounds(
5656 return result;
5757}
5858
59+ // / changes the width of the given bitvector type
60+ bitvector_typet adjust_width (const typet &src, std::size_t new_width)
61+ {
62+ if (src.id () == ID_unsignedbv)
63+ return unsignedbv_typet (new_width);
64+ else if (src.id () == ID_signedbv)
65+ return signedbv_typet (new_width);
66+ else if (src.id () == ID_bv)
67+ return bv_typet (new_width);
68+ else if (src.id () == ID_c_enum) // we use the underlying type
69+ return adjust_width (to_c_enum_type (src).underlying_type (), new_width);
70+ else if (src.id () == ID_c_bit_field)
71+ return c_bit_field_typet (
72+ to_c_bit_field_type (src).underlying_type (), new_width);
73+ else
74+ PRECONDITION (false );
75+ }
76+
5977// / Convert a bitvector-typed expression \p bitvector_expr to a struct-typed
6078// / expression. See \ref bv_to_expr for an overview.
6179static struct_exprt bv_to_struct_expr (
@@ -89,7 +107,7 @@ static struct_exprt bv_to_struct_expr(
89107 endianness_map,
90108 member_offset_bits,
91109 member_offset_bits + component_bits - 1 );
92- bitvector_typet type{ bitvector_expr.type (). id () , component_bits} ;
110+ bitvector_typet type = adjust_width ( bitvector_expr.type (), component_bits) ;
93111 operands.push_back (bv_to_expr (
94112 extractbits_exprt{bitvector_expr, bounds.ub , bounds.lb , std::move (type)},
95113 comp.type (),
@@ -133,7 +151,7 @@ static union_exprt bv_to_union_expr(
133151 }
134152
135153 const auto bounds = map_bounds (endianness_map, 0 , component_bits - 1 );
136- bitvector_typet type{ bitvector_expr.type (). id () , component_bits} ;
154+ bitvector_typet type = adjust_width ( bitvector_expr.type (), component_bits) ;
137155 const irep_idt &component_name = widest_member.has_value ()
138156 ? widest_member->first .get_name ()
139157 : components.front ().get_name ();
@@ -185,7 +203,8 @@ static array_exprt bv_to_array_expr(
185203 numeric_cast_v<std::size_t >(*subtype_bits);
186204 const auto bounds = map_bounds (
187205 endianness_map, i * subtype_bits_int, ((i + 1 ) * subtype_bits_int) - 1 );
188- bitvector_typet type{bitvector_expr.type ().id (), subtype_bits_int};
206+ bitvector_typet type =
207+ adjust_width (bitvector_expr.type (), subtype_bits_int);
189208 operands.push_back (bv_to_expr (
190209 extractbits_exprt{
191210 bitvector_expr, bounds.ub , bounds.lb , std::move (type)},
@@ -230,7 +249,8 @@ static vector_exprt bv_to_vector_expr(
230249 numeric_cast_v<std::size_t >(*subtype_bits);
231250 const auto bounds = map_bounds (
232251 endianness_map, i * subtype_bits_int, ((i + 1 ) * subtype_bits_int) - 1 );
233- bitvector_typet type{bitvector_expr.type ().id (), subtype_bits_int};
252+ bitvector_typet type =
253+ adjust_width (bitvector_expr.type (), subtype_bits_int);
234254 operands.push_back (bv_to_expr (
235255 extractbits_exprt{
236256 bitvector_expr, bounds.ub , bounds.lb , std::move (type)},
@@ -274,7 +294,8 @@ static complex_exprt bv_to_complex_expr(
274294 const auto bounds_imag =
275295 map_bounds (endianness_map, subtype_bits, subtype_bits * 2 - 1 );
276296
277- const bitvector_typet type{bitvector_expr.type ().id (), subtype_bits};
297+ const bitvector_typet type =
298+ adjust_width (bitvector_expr.type (), subtype_bits);
278299
279300 return complex_exprt{
280301 bv_to_expr (
@@ -1293,7 +1314,7 @@ exprt lower_byte_extract(const byte_extract_exprt &src, const namespacet &ns)
12931314 else // width_bytes>=2
12941315 {
12951316 concatenation_exprt concatenation (
1296- std::move (op), bitvector_typet ( subtype-> id () , width_bytes * 8 ));
1317+ std::move (op), adjust_width (* subtype, width_bytes * 8 ));
12971318
12981319 endianness_mapt map (concatenation.type (), little_endian, ns);
12991320 return bv_to_expr (concatenation, src.type (), map, ns);
@@ -2304,12 +2325,11 @@ exprt lower_byte_update(const byte_update_exprt &src, const namespacet &ns)
23042325 extractbits_exprt extra_bits{
23052326 src.op (), bounds.ub , bounds.lb , bv_typet{n_extra_bits}};
23062327
2307- update_value =
2308- concatenation_exprt{typecast_exprt::conditional_cast (
2309- update_value, bv_typet{update_bits_int}),
2310- extra_bits,
2311- bitvector_typet{update_value.type ().id (),
2312- update_bits_int + n_extra_bits}};
2328+ update_value = concatenation_exprt{
2329+ typecast_exprt::conditional_cast (
2330+ update_value, bv_typet{update_bits_int}),
2331+ extra_bits,
2332+ adjust_width (update_value.type (), update_bits_int + n_extra_bits)};
23132333 }
23142334 else
23152335 {
0 commit comments