Skip to content

Commit fad172f

Browse files
committed
byte-operator lowering: rename element_width to {element,component}_bits
This makes the unit of measurement explicit and thus should help avoid bits/bytes confusion.
1 parent 56dc239 commit fad172f

File tree

1 file changed

+28
-32
lines changed

1 file changed

+28
-32
lines changed

src/solvers/lowering/byte_operators.cpp

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ static exprt unpack_rec(
3030
/// \param src: array/vector to unpack
3131
/// \param src_size: array/vector size; if not a constant, \p max_bytes must be
3232
/// a constant value, otherwise we fail with an exception
33-
/// \param element_width: bit width of array/vector elements
33+
/// \param element_bits: bit width of array/vector elements
3434
/// \param little_endian: true, iff assumed endianness is little-endian
3535
/// \param offset_bytes: if not nil, bytes prior to this offset will be filled
3636
/// with nil values
@@ -41,7 +41,7 @@ static exprt unpack_rec(
4141
static array_exprt unpack_array_vector(
4242
const exprt &src,
4343
const exprt &src_size,
44-
const mp_integer &element_width,
44+
const mp_integer &element_bits,
4545
bool little_endian,
4646
const exprt &offset_bytes,
4747
const exprt &max_bytes,
@@ -61,9 +61,9 @@ static array_exprt unpack_array_vector(
6161
// refine the number of elements to extract in case the element width is known
6262
// and a multiple of bytes; otherwise we will expand the entire array/vector
6363
optionalt<mp_integer> max_elements;
64-
if(element_width > 0 && element_width % 8 == 0)
64+
if(element_bits > 0 && element_bits % 8 == 0)
6565
{
66-
mp_integer el_bytes = element_width / 8;
66+
mp_integer el_bytes = element_bits / 8;
6767

6868
// turn bytes into elements
6969
if(!num_elements)
@@ -85,7 +85,7 @@ static array_exprt unpack_array_vector(
8585
}
8686

8787
// the maximum number of bytes is an upper bound in case the size of the
88-
// array/vector is unknown; if the element_width was usable above this will
88+
// array/vector is unknown; if element_bits was usable above this will
8989
// have been turned into a number of elements already
9090
if(!num_elements)
9191
num_elements = *max_elements;
@@ -149,16 +149,16 @@ static exprt unpack_rec(
149149
const array_typet &array_type=to_array_type(src.type());
150150
const typet &subtype=array_type.subtype();
151151

152-
auto element_width = pointer_offset_bits(subtype, ns);
153-
CHECK_RETURN(element_width.has_value());
152+
auto element_bits = pointer_offset_bits(subtype, ns);
153+
CHECK_RETURN(element_bits.has_value());
154154

155-
if(!unpack_byte_array && *element_width == 8)
155+
if(!unpack_byte_array && *element_bits == 8)
156156
return src;
157157

158158
return unpack_array_vector(
159159
src,
160160
array_type.size(),
161-
*element_width,
161+
*element_bits,
162162
little_endian,
163163
offset_bytes,
164164
max_bytes,
@@ -169,16 +169,16 @@ static exprt unpack_rec(
169169
const vector_typet &vector_type = to_vector_type(src.type());
170170
const typet &subtype = vector_type.subtype();
171171

172-
auto element_width = pointer_offset_bits(subtype, ns);
173-
CHECK_RETURN(element_width.has_value());
172+
auto element_bits = pointer_offset_bits(subtype, ns);
173+
CHECK_RETURN(element_bits.has_value());
174174

175-
if(!unpack_byte_array && *element_width == 8)
175+
if(!unpack_byte_array && *element_bits == 8)
176176
return src;
177177

178178
return unpack_array_vector(
179179
src,
180180
vector_type.size(),
181-
*element_width,
181+
*element_bits,
182182
little_endian,
183183
offset_bytes,
184184
max_bytes,
@@ -194,14 +194,14 @@ static exprt unpack_rec(
194194
exprt::operandst byte_operands;
195195
for(const auto &comp : components)
196196
{
197-
auto element_width = pointer_offset_bits(comp.type(), ns);
197+
auto component_bits = pointer_offset_bits(comp.type(), ns);
198198

199199
// the next member would be misaligned, abort
200200
if(
201-
!element_width.has_value() || *element_width == 0 ||
202-
*element_width % 8 != 0)
201+
!component_bits.has_value() || *component_bits == 0 ||
202+
*component_bits % 8 != 0)
203203
{
204-
throw non_byte_alignedt(struct_type, comp, *element_width);
204+
throw non_byte_alignedt(struct_type, comp, *component_bits);
205205
}
206206

207207
exprt offset_in_member = nil_exprt();
@@ -240,7 +240,7 @@ static exprt unpack_rec(
240240
byte_operands.insert(
241241
byte_operands.end(), sub.operands().begin(), sub.operands().end());
242242

243-
member_offset_bits += *element_width;
243+
member_offset_bits += *component_bits;
244244
}
245245

246246
const std::size_t size = byte_operands.size();
@@ -372,25 +372,23 @@ exprt lower_byte_extract(const byte_extract_exprt &src, const namespacet &ns)
372372
const array_typet &array_type=to_array_type(src.type());
373373
const typet &subtype=array_type.subtype();
374374

375-
auto element_width = pointer_offset_bits(subtype, ns);
375+
auto element_bits = pointer_offset_bits(subtype, ns);
376376
auto num_elements = numeric_cast<mp_integer>(array_type.size());
377377
if(!num_elements.has_value())
378378
num_elements = mp_integer(unpacked.op().operands().size());
379379

380380
// consider ways of dealing with arrays of unknown subtype size or with a
381381
// subtype size that does not fit byte boundaries; currently we fall back to
382382
// stitching together consecutive elements down below
383-
if(
384-
element_width.has_value() && *element_width >= 1 &&
385-
*element_width % 8 == 0)
383+
if(element_bits.has_value() && *element_bits >= 1 && *element_bits % 8 == 0)
386384
{
387385
array_exprt array({}, array_type);
388386

389387
for(mp_integer i=0; i< *num_elements; ++i)
390388
{
391389
plus_exprt new_offset(
392390
unpacked.offset(),
393-
from_integer(i * (*element_width) / 8, unpacked.offset().type()));
391+
from_integer(i * (*element_bits) / 8, unpacked.offset().type()));
394392

395393
byte_extract_exprt tmp(unpacked);
396394
tmp.type()=subtype;
@@ -409,23 +407,21 @@ exprt lower_byte_extract(const byte_extract_exprt &src, const namespacet &ns)
409407

410408
mp_integer num_elements = numeric_cast_v<mp_integer>(vector_type.size());
411409

412-
auto element_width = pointer_offset_bits(subtype, ns);
413-
CHECK_RETURN(element_width.has_value());
410+
auto element_bits = pointer_offset_bits(subtype, ns);
411+
CHECK_RETURN(element_bits.has_value());
414412

415413
// consider ways of dealing with vectors of unknown subtype size or with a
416414
// subtype size that does not fit byte boundaries; currently we fall back to
417415
// stitching together consecutive elements down below
418-
if(
419-
element_width.has_value() && *element_width >= 1 &&
420-
*element_width % 8 == 0)
416+
if(element_bits.has_value() && *element_bits >= 1 && *element_bits % 8 == 0)
421417
{
422418
vector_exprt vector(vector_type);
423419

424420
for(mp_integer i = 0; i < num_elements; ++i)
425421
{
426422
plus_exprt new_offset(
427423
unpacked.offset(),
428-
from_integer(i * (*element_width) / 8, unpacked.offset().type()));
424+
from_integer(i * (*element_bits) / 8, unpacked.offset().type()));
429425

430426
byte_extract_exprt tmp(unpacked);
431427
tmp.type() = subtype;
@@ -447,12 +443,12 @@ exprt lower_byte_extract(const byte_extract_exprt &src, const namespacet &ns)
447443

448444
for(const auto &comp : components)
449445
{
450-
auto element_width = pointer_offset_bits(comp.type(), ns);
446+
auto component_bits = pointer_offset_bits(comp.type(), ns);
451447

452448
// the next member would be misaligned, abort
453449
if(
454-
!element_width.has_value() || *element_width == 0 ||
455-
*element_width % 8 != 0)
450+
!component_bits.has_value() || *component_bits == 0 ||
451+
*component_bits % 8 != 0)
456452
{
457453
failed=true;
458454
break;

0 commit comments

Comments
 (0)