@@ -56,6 +56,35 @@ static boundst map_bounds(
56
56
return result;
57
57
}
58
58
59
+ // / determines the underlying type of a given bitvector type
60
+ typet underlying_type (const typet &src)
61
+ {
62
+ if (src.id () == ID_c_enum)
63
+ return to_c_enum_type (src).underlying_type ();
64
+ else if (src.id () == ID_c_bit_field)
65
+ return to_c_bit_field_type (src).underlying_type ();
66
+ else
67
+ return src;
68
+ }
69
+
70
+ // / changes the width of the given bitvector type
71
+ bitvector_typet adjust_width (const typet &src, std::size_t new_width)
72
+ {
73
+ if (src.id () == ID_unsignedbv)
74
+ return unsignedbv_typet (new_width);
75
+ else if (src.id () == ID_signedbv)
76
+ return signedbv_typet (new_width);
77
+ else if (src.id () == ID_bv)
78
+ return bv_typet (new_width);
79
+ else if (src.id () == ID_c_enum) // we use the underlying type
80
+ return adjust_width (to_c_enum_type (src).underlying_type (), new_width);
81
+ else if (src.id () == ID_c_bit_field)
82
+ return c_bit_field_typet (
83
+ to_c_bit_field_type (src).underlying_type (), new_width);
84
+ else
85
+ PRECONDITION (false );
86
+ }
87
+
59
88
// / Convert a bitvector-typed expression \p bitvector_expr to a struct-typed
60
89
// / expression. See \ref bv_to_expr for an overview.
61
90
static struct_exprt bv_to_struct_expr (
@@ -89,7 +118,7 @@ static struct_exprt bv_to_struct_expr(
89
118
endianness_map,
90
119
member_offset_bits,
91
120
member_offset_bits + component_bits - 1 );
92
- bitvector_typet type{ bitvector_expr.type (). id () , component_bits} ;
121
+ bitvector_typet type = adjust_width ( bitvector_expr.type (), component_bits) ;
93
122
operands.push_back (bv_to_expr (
94
123
extractbits_exprt{bitvector_expr, bounds.ub , bounds.lb , std::move (type)},
95
124
comp.type (),
@@ -133,7 +162,7 @@ static union_exprt bv_to_union_expr(
133
162
}
134
163
135
164
const auto bounds = map_bounds (endianness_map, 0 , component_bits - 1 );
136
- bitvector_typet type{ bitvector_expr.type (). id () , component_bits} ;
165
+ bitvector_typet type = adjust_width ( bitvector_expr.type (), component_bits) ;
137
166
const irep_idt &component_name = widest_member.has_value ()
138
167
? widest_member->first .get_name ()
139
168
: components.front ().get_name ();
@@ -185,7 +214,8 @@ static array_exprt bv_to_array_expr(
185
214
numeric_cast_v<std::size_t >(*subtype_bits);
186
215
const auto bounds = map_bounds (
187
216
endianness_map, i * subtype_bits_int, ((i + 1 ) * subtype_bits_int) - 1 );
188
- bitvector_typet type{bitvector_expr.type ().id (), subtype_bits_int};
217
+ bitvector_typet type =
218
+ adjust_width (bitvector_expr.type (), subtype_bits_int);
189
219
operands.push_back (bv_to_expr (
190
220
extractbits_exprt{
191
221
bitvector_expr, bounds.ub , bounds.lb , std::move (type)},
@@ -230,7 +260,8 @@ static vector_exprt bv_to_vector_expr(
230
260
numeric_cast_v<std::size_t >(*subtype_bits);
231
261
const auto bounds = map_bounds (
232
262
endianness_map, i * subtype_bits_int, ((i + 1 ) * subtype_bits_int) - 1 );
233
- bitvector_typet type{bitvector_expr.type ().id (), subtype_bits_int};
263
+ bitvector_typet type =
264
+ adjust_width (bitvector_expr.type (), subtype_bits_int);
234
265
operands.push_back (bv_to_expr (
235
266
extractbits_exprt{
236
267
bitvector_expr, bounds.ub , bounds.lb , std::move (type)},
@@ -274,7 +305,8 @@ static complex_exprt bv_to_complex_expr(
274
305
const auto bounds_imag =
275
306
map_bounds (endianness_map, subtype_bits, subtype_bits * 2 - 1 );
276
307
277
- const bitvector_typet type{bitvector_expr.type ().id (), subtype_bits};
308
+ const bitvector_typet type =
309
+ adjust_width (bitvector_expr.type (), subtype_bits);
278
310
279
311
return complex_exprt{
280
312
bv_to_expr (
@@ -1293,7 +1325,7 @@ exprt lower_byte_extract(const byte_extract_exprt &src, const namespacet &ns)
1293
1325
else // width_bytes>=2
1294
1326
{
1295
1327
concatenation_exprt concatenation (
1296
- std::move (op), bitvector_typet ( subtype-> id () , width_bytes * 8 ));
1328
+ std::move (op), adjust_width (* subtype, width_bytes * 8 ));
1297
1329
1298
1330
endianness_mapt map (concatenation.type (), little_endian, ns);
1299
1331
return bv_to_expr (concatenation, src.type (), map, ns);
@@ -2304,12 +2336,11 @@ exprt lower_byte_update(const byte_update_exprt &src, const namespacet &ns)
2304
2336
extractbits_exprt extra_bits{
2305
2337
src.op (), bounds.ub , bounds.lb , bv_typet{n_extra_bits}};
2306
2338
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}};
2339
+ update_value = concatenation_exprt{
2340
+ typecast_exprt::conditional_cast (
2341
+ update_value, bv_typet{update_bits_int}),
2342
+ extra_bits,
2343
+ adjust_width (update_value.type (), update_bits_int + n_extra_bits)};
2313
2344
}
2314
2345
else
2315
2346
{
0 commit comments