24 const exprt &bitvector_expr,
25 const typet &target_type,
38 std::size_t lower_bound,
39 std::size_t upper_bound)
42 result.
lb = lower_bound;
43 result.
ub = upper_bound;
51 if(result.
ub < result.
lb)
52 std::swap(result.
lb, result.
ub);
61 if(src.
id() == ID_unsignedbv)
63 else if(src.
id() == ID_signedbv)
65 else if(src.
id() == ID_bv)
67 else if(src.
id() == ID_c_enum)
69 else if(src.
id() == ID_c_bit_field)
79 const exprt &bitvector_expr,
87 operands.reserve(components.size());
89 for(
const auto &comp : components)
92 std::size_t component_bits;
93 if(component_bits_opt.has_value())
99 if(component_bits == 0)
118 if(component_bits_opt.has_value())
128 const exprt &bitvector_expr,
135 if(components.empty())
140 std::size_t component_bits;
141 if(widest_member.has_value())
146 if(component_bits == 0)
149 components.front().get_name(),
150 bv_to_expr(bitvector_expr, components.front().type(), endianness_map, ns),
154 const auto bounds =
map_bounds(endianness_map, 0, component_bits - 1);
156 const irep_idt &component_name = widest_member.has_value()
157 ? widest_member->first.get_name()
158 : components.front().get_name();
159 const typet &component_type = widest_member.has_value()
160 ? widest_member->first.type()
161 : components.front().type();
176 const exprt &bitvector_expr,
184 const std::size_t total_bits =
186 if(!num_elements.has_value())
188 if(!subtype_bits.has_value() || *subtype_bits ==
bitst{0})
189 num_elements = total_bits;
194 !num_elements.has_value() || !subtype_bits.has_value() ||
195 *num_elements == 0 || *subtype_bits * *num_elements ==
bitst{total_bits},
196 "subtype width times array size should be total bitvector width");
199 operands.reserve(*num_elements);
200 for(std::size_t i = 0; i < *num_elements; ++i)
202 if(subtype_bits.has_value())
204 const std::size_t subtype_bits_int =
207 endianness_map, i * subtype_bits_int, ((i + 1) * subtype_bits_int) - 1);
220 bitvector_expr, array_type.
element_type(), endianness_map, ns));
224 return array_exprt{std::move(operands), array_type};
230 const exprt &bitvector_expr,
235 const std::size_t num_elements =
239 !subtype_bits.has_value() ||
240 *subtype_bits * num_elements ==
241 bitst{to_bitvector_type(bitvector_expr.type()).get_width()},
242 "subtype width times vector size should be total bitvector width");
245 operands.reserve(num_elements);
246 for(std::size_t i = 0; i < num_elements; ++i)
248 if(subtype_bits.has_value())
250 const std::size_t subtype_bits_int =
253 endianness_map, i * subtype_bits_int, ((i + 1) * subtype_bits_int) - 1);
266 bitvector_expr, vector_type.
element_type(), endianness_map, ns));
276 const exprt &bitvector_expr,
281 const std::size_t total_bits =
284 std::size_t subtype_bits;
285 if(subtype_bits_opt.has_value())
289 subtype_bits * 2 == total_bits,
290 "subtype width should be half of the total bitvector width");
293 subtype_bits = total_bits / 2;
295 const auto bounds_real =
map_bounds(endianness_map, 0, subtype_bits - 1);
296 const auto bounds_imag =
297 map_bounds(endianness_map, subtype_bits, subtype_bits * 2 - 1);
331 const exprt &bitvector_expr,
332 const typet &target_type,
338 if(target_type.
id() == ID_floatbv)
348 target_type.
id() == ID_c_enum || target_type.
id() == ID_c_enum_tag ||
349 target_type.
id() == ID_string ||
350 (target_type.
id() == ID_bool &&
356 else if(target_type.
id() == ID_struct)
361 else if(target_type.
id() == ID_struct_tag)
368 result.
type() = target_type;
369 return std::move(result);
371 else if(target_type.
id() == ID_union)
374 bitvector_expr,
to_union_type(target_type), endianness_map, ns);
376 else if(target_type.
id() == ID_union_tag)
383 result.
type() = target_type;
386 else if(target_type.
id() == ID_array)
389 bitvector_expr,
to_array_type(target_type), endianness_map, ns);
391 else if(target_type.
id() == ID_vector)
396 else if(target_type.
id() == ID_complex)
404 false,
"bv_to_expr does not yet support ", target_type.
id_string());
411 const std::optional<bytest> &offset_bytes,
412 const std::optional<bytest> &max_bytes,
413 const std::size_t bits_per_byte,
415 bool unpack_byte_array =
false);
426 std::size_t lower_bound,
427 std::size_t upper_bound,
428 const std::size_t bits_per_byte,
433 if(src.
id() == ID_array)
441 const typet &element_type = src.
type().
id() == ID_array
444 const typet index_type = src.
type().
id() == ID_array
451 bytes.reserve(upper_bound - lower_bound);
452 for(std::size_t i = lower_bound; i < upper_bound; ++i)
470 std::size_t el_bytes,
472 const std::size_t bits_per_byte,
475 const typet index_type = src.
type().
id() == ID_array
481 static std::size_t array_comprehension_index_counter = 0;
482 ++array_comprehension_index_counter;
484 "$array_comprehension_index_a_v" +
485 std::to_string(array_comprehension_index_counter),
493 unpack_rec(element, little_endian, {}, {}, bits_per_byte, ns,
false);
497 exprt body = sub_operands.front();
499 array_comprehension_index,
501 for(std::size_t i = 1; i < el_bytes; ++i)
509 const exprt array_vector_size = src.
type().
id() == ID_vector
513 if(array_vector_size.
is_nil())
521 std::move(array_comprehension_index),
527 std::move(array_comprehension_index),
552 const std::optional<mp_integer> &src_size,
553 const bitst &element_bits,
555 const std::optional<bytest> &offset_bytes,
556 const std::optional<bytest> &max_bytes,
557 const std::size_t bits_per_byte,
562 if(!src_size.has_value() && !max_bytes.has_value())
566 "unpacking of arrays with non-byte-aligned elements is not supported");
580 std::optional<mp_integer> num_elements;
581 if(src_size.has_value())
582 num_elements = *src_size;
583 if(element_bits >
bitst{0} && element_bits %
bitst{bits_per_byte} ==
bitst{0})
585 if(!num_elements.has_value())
588 num_elements = (*max_bytes + el_bytes -
bytest{1}) / el_bytes;
591 if(offset_bytes.has_value())
594 first_element = *offset_bytes / el_bytes;
596 byte_operands.resize(
598 *offset_bytes - (*offset_bytes % el_bytes),
599 *num_elements * el_bytes)),
608 num_elements = max_bytes->get();
611 const typet index_type = src_simp.
type().
id() == ID_array
615 for(
mp_integer i = first_element; i < *num_elements; ++i)
620 (src_simp.
id() == ID_array || src_simp.
id() == ID_vector) &&
624 element = src_simp.
operands()[index_int];
634 const std::optional<bytest> element_max_bytes =
635 max_bytes ? std::min(el_bytes, *max_bytes -
bytest{byte_operands.size()})
636 : std::optional<bytest>{};
638 element_max_bytes ? *element_max_bytes : el_bytes);
641 element, little_endian, {}, element_max_bytes, bits_per_byte, ns,
true);
644 byte_operands.insert(
645 byte_operands.end(), sub_operands.begin(), sub_operands.end());
647 if(max_bytes &&
bytest{byte_operands.size()} >= *max_bytes)
651 const std::size_t size = byte_operands.size();
653 std::move(byte_operands),
670 std::size_t total_bits,
673 const std::optional<bytest> &offset_bytes,
674 const std::optional<bytest> &max_bytes,
675 const std::size_t bits_per_byte,
679 std::move(bit_fields),
bv_typet{total_bits}};
692 std::make_move_iterator(sub.
operands().begin()),
693 std::make_move_iterator(sub.
operands().end()));
709 const std::optional<bytest> &offset_bytes,
710 const std::optional<bytest> &max_bytes,
711 const std::size_t bits_per_byte,
715 src.
type().
id() == ID_struct_tag
720 std::optional<bytest> offset_in_member;
721 std::optional<bytest> max_bytes_left;
722 std::optional<std::pair<exprt::operandst, std::size_t>> bit_fields;
726 for(
auto it = components.begin(); it != components.end(); ++it)
728 const auto &comp = *it;
737 component_bits.has_value() ||
738 (std::next(it) == components.end() && !bit_fields.has_value()),
739 "members of non-constant width should come last in a struct");
742 if(src.
id() == ID_struct)
748 if(bit_fields.has_value())
751 std::move(bit_fields->first),
762 if(offset_bytes.has_value())
768 if(*offset_in_member <
bytest{0})
769 offset_in_member.reset();
772 if(max_bytes.has_value())
776 if(*max_bytes_left <
bytest{0})
783 (component_bits.has_value() &&
784 *component_bits %
bitst{bits_per_byte} !=
bitst{0}))
786 if(!bit_fields.has_value())
790 bit_fields->first.insert(
791 little_endian ? bit_fields->first.begin() : bit_fields->first.end(),
793 bit_fields->second += bits_int;
801 !bit_fields.has_value(),
802 "all preceding members should have been processed");
805 component_bits.has_value() && offset_in_member.has_value() &&
806 bytes_to_bits(*offset_in_member, bits_per_byte) >= *component_bits)
810 byte_operands.resize(
812 *component_bits, bits_per_byte)),
826 byte_operands.insert(
828 std::make_move_iterator(sub.
operands().begin()),
829 std::make_move_iterator(sub.
operands().end()));
832 if(component_bits.has_value())
837 if(bit_fields.has_value())
840 std::move(bit_fields->first),
850 const std::size_t size = byte_operands.size();
852 std::move(byte_operands),
865 const std::size_t bits_per_byte,
893 byte_operands.insert(
895 std::make_move_iterator(sub_imag.
operands().begin()),
896 std::make_move_iterator(sub_imag.
operands().end()));
898 const std::size_t size = byte_operands.size();
900 std::move(byte_operands),
919 const std::optional<bytest> &offset_bytes,
920 const std::optional<bytest> &max_bytes,
921 const std::size_t bits_per_byte,
923 bool unpack_byte_array)
925 if(src.
type().
id() == ID_array)
934 !unpack_byte_array && *element_bits ==
bitst{bits_per_byte} &&
951 else if(src.
type().
id() == ID_vector)
960 !unpack_byte_array && *element_bits ==
bitst{bits_per_byte} &&
976 else if(src.
type().
id() == ID_complex)
980 else if(src.
type().
id() == ID_struct || src.
type().
id() == ID_struct_tag)
983 src, little_endian, offset_bytes, max_bytes, bits_per_byte, ns);
985 else if(src.
type().
id() == ID_union || src.
type().
id() == ID_union_tag)
988 src.
type().
id() == ID_union_tag
994 if(widest_member.has_value())
997 src, widest_member->first.get_name(), widest_member->first.
type()};
1020 else if(src.
type().
id() == ID_pointer)
1031 else if(src.
id() == ID_string_constant)
1053 else if(src.
type().
id() != ID_empty)
1058 DATA_INVARIANT(bits_opt.has_value(),
"basic type should have a fixed size");
1060 const bitst total_bits = *bits_opt;
1061 bitst last_bit = total_bits;
1062 bitst bit_offset{0};
1064 if(max_bytes.has_value())
1066 const auto max_bits =
bytes_to_bits(*max_bytes, bits_per_byte);
1069 last_bit = std::min(last_bit, max_bits);
1073 bit_offset = std::max(
bitst{0}, last_bit - max_bits);
1086 if(padded_bits > total_bits)
1091 auto const byte_type =
bv_typet{bits_per_byte};
1096 for(; bit_offset < last_bit; bit_offset +=
bitst{bits_per_byte})
1109 byte_operands.push_back(extractbits);
1111 byte_operands.insert(byte_operands.begin(), extractbits);
1114 const std::size_t size = byte_operands.size();
1116 return array_exprt{std::move(byte_operands), std::move(array_type)};
1136 const typet &subtype,
1137 const bitst &element_bits,
1140 std::optional<std::size_t> num_elements;
1141 if(src.
type().
id() == ID_array)
1146 if(num_elements.has_value())
1149 operands.reserve(*num_elements);
1150 for(std::size_t i = 0; i < *num_elements; ++i)
1156 unpacked.
offset().type()));
1159 tmp.
type() = subtype;
1160 tmp.
offset() = new_offset;
1166 if(src.
type().
id() == ID_array)
1179 static std::size_t array_comprehension_index_counter = 0;
1180 ++array_comprehension_index_counter;
1182 "$array_comprehension_index_a" +
1183 std::to_string(array_comprehension_index_counter),
1190 array_comprehension_index,
1193 array_comprehension_index.
type())},
1194 unpacked.
offset().type())};
1197 body.
type() = subtype;
1198 body.
offset() = std::move(new_offset);
1201 std::move(array_comprehension_index),
1224 !subtype_bits.has_value() ||
1225 *subtype_bits %
bitst{src.get_bits_per_byte()} !=
bitst{0})
1230 real.
type() = subtype;
1236 unpacked.
offset().type())};
1238 imag.
type() = subtype;
1297 src.
id() == ID_byte_extract_little_endian ||
1298 src.
id() == ID_byte_extract_big_endian);
1299 const bool little_endian = src.
id() == ID_byte_extract_little_endian;
1303 if(upper_bound_opt.has_value())
1307 upper_bound_opt.value(),
1309 src.
offset(), upper_bound_opt.value().
type())),
1312 else if(src.
type().
id() == ID_empty)
1316 const auto upper_bound_int_opt =
1323 lower_bound_int_opt,
1324 upper_bound_int_opt,
1331 if(src.
type().
id() == ID_array || src.
type().
id() == ID_vector)
1340 element_bits.has_value() && *element_bits >=
bitst{1} &&
1341 *element_bits %
bitst{src.get_bits_per_byte()} ==
bitst{0})
1344 src, unpacked, subtype, *element_bits, ns);
1347 else if(src.
type().
id() == ID_complex)
1350 if(result.has_value())
1351 return std::move(*result);
1355 else if(src.
type().
id() == ID_struct || src.
type().
id() == ID_struct_tag)
1358 src.
type().
id() == ID_struct_tag
1366 for(
const auto &comp : components)
1372 !component_bits.has_value() ||
1373 *component_bits %
bitst{src.get_bits_per_byte()} !=
bitst{0})
1379 auto member_offset_opt =
1382 if(!member_offset_opt.has_value())
1391 member_offset_opt.value(), unpacked.
offset().
type()));
1394 tmp.
type() = comp.type();
1395 tmp.
offset() = new_offset;
1403 else if(src.
type().
id() == ID_union || src.
type().
id() == ID_union_tag)
1406 src.
type().
id() == ID_union_tag
1412 if(widest_member.has_value())
1415 tmp.
type() = widest_member->first.type();
1418 widest_member->first.get_name(),
1424 const exprt &root = unpacked.
op();
1427 std::optional<typet> subtype;
1428 std::optional<typet> index_type;
1429 if(root.
type().
id() == ID_vector)
1443 subtype_bits.has_value() && *subtype_bits ==
bitst{src.get_bits_per_byte()},
1444 "offset bits are byte aligned");
1447 if(!size_bits.has_value())
1452 op0_bits.has_value(),
1453 "the extracted width or the source width must be known");
1454 size_bits = op0_bits;
1465 op.reserve(width_bytes);
1467 for(std::size_t i = 0; i < width_bytes; i++)
1470 std::size_t offset_int = little_endian ? (width_bytes - i - 1) : i;
1480 (root.
id() == ID_array || root.
id() == ID_vector) &&
1482 index < root.
operands().size() && index >= 0)
1493 if(width_bytes == 1)
1497 result_type.
id() == ID_struct || result_type.
id() == ID_struct_tag ||
1498 result_type.
id() == ID_union || result_type.
id() == ID_union_tag ||
1499 result_type.
id() == ID_array || result_type.
id() == ID_vector ||
1500 result_type.
id() == ID_complex)
1506 return bv_to_expr(op.front(), result_type, map, ns);
1524 const exprt &value_as_byte_array,
1525 const std::optional<exprt> &non_const_update_bound,
1539 const typet &subtype,
1540 const exprt &value_as_byte_array,
1541 const exprt &non_const_update_bound,
1546 static std::size_t array_comprehension_index_counter = 0;
1547 ++array_comprehension_index_counter;
1549 "$array_comprehension_index_u_a_v" +
1550 std::to_string(array_comprehension_index_counter),
1555 array_comprehension_index, src.
offset().
type()),
1560 array_comprehension_index, non_const_update_bound.
type()),
1564 src.
offset(), non_const_update_bound.
type()),
1565 non_const_update_bound}};
1568 src.
id() == ID_byte_update_little_endian ||
1569 src.
id() == ID_byte_update_big_endian);
1570 const bool little_endian = src.
id() == ID_byte_update_little_endian;
1576 or_exprt{std::move(lower_bound), std::move(upper_bound)},
1580 value_as_byte_array,
1582 array_comprehension_index,
1584 src.
offset(), array_comprehension_index.
type())}},
1591 array_comprehension_index,
1592 std::move(array_comprehension_body),
1608 const typet &subtype,
1610 const std::optional<exprt> &non_const_update_bound,
1614 src.
id() == ID_byte_update_little_endian ||
1615 src.
id() == ID_byte_update_big_endian);
1616 const bool little_endian = src.
id() == ID_byte_update_little_endian;
1618 const typet index_type = src.
type().
id() == ID_array
1625 for(std::size_t i = 0; i < value_as_byte_array.
operands().size(); ++i)
1638 if(e.id() == ID_index)
1641 if(index_expr.
array() == src.
op() && index_expr.
index() == where)
1647 if(non_const_update_bound.has_value())
1653 *non_const_update_bound},
1658 result =
with_exprt{result, std::move(where), std::move(update_value)};
1681 const typet &subtype,
1682 const exprt &subtype_size,
1683 const exprt &value_as_byte_array,
1684 const exprt &non_const_update_bound,
1685 const exprt &initial_bytes,
1686 const exprt &first_index,
1687 const exprt &first_update_value,
1690 const irep_idt extract_opcode = src.
id() == ID_byte_update_little_endian
1691 ? ID_byte_extract_little_endian
1692 : ID_byte_extract_big_endian;
1696 static std::size_t array_comprehension_index_counter = 0;
1697 ++array_comprehension_index_counter;
1699 "$array_comprehension_index_u_a_v_u" +
1700 std::to_string(array_comprehension_index_counter),
1712 array_comprehension_index, first_index.
type()),
1723 array_comprehension_index, first_index.
type()),
1728 value_as_byte_array,
1729 std::move(offset_expr),
1741 non_const_update_bound, subtype_size.
type()),
1756 non_const_update_bound, initial_bytes.
type()),
1758 subtype_size.
type()),
1765 array_comprehension_index, last_index.
type()),
1780 value_as_byte_array,
1785 or_exprt{std::move(lower_bound), std::move(upper_bound)},
1790 array_comprehension_index, first_index.
type()),
1796 array_comprehension_index, last_index.
type()),
1798 std::move(last_update_value),
1799 std::move(update_value)}}};
1803 array_comprehension_index,
1804 std::move(array_comprehension_body),
1821 const typet &subtype,
1822 const exprt &value_as_byte_array,
1823 const std::optional<exprt> &non_const_update_bound,
1833 subtype_size_opt.value(), src.
offset().
type()),
1848 const exprt update_size =
1852 if(non_const_update_bound.has_value())
1855 *non_const_update_bound, subtype_size.
type());
1860 value_as_byte_array.
id() == ID_array,
1861 "value should be an array expression if the update bound is constant");
1862 update_bound = update_size;
1878 value_as_byte_array,
1882 if(value_as_byte_array.
id() != ID_array)
1888 value_as_byte_array,
1889 *non_const_update_bound,
1901 std::size_t step_size = 1;
1908 std::size_t offset = 0;
1912 with_exprt result{src.
op(), first_index, first_update_value};
1914 auto lower_byte_update_array_vector_non_const_one_element =
1919 &value_as_byte_array,
1921 &result](std::size_t i) ->
void {
1936 value_as_byte_array,
1940 result =
with_exprt{result, std::move(where), std::move(element)};
1944 for(; offset + step_size <= value_as_byte_array.
operands().size();
1945 offset += step_size, ++i)
1947 lower_byte_update_array_vector_non_const_one_element(i);
1951 if(offset < value_as_byte_array.
operands().size())
1952 lower_byte_update_array_vector_non_const_one_element(i);
1971 const bitst &offset_bits,
1972 const exprt &element_to_update,
1973 const bitst &subtype_bits,
1974 const bitst &bits_already_set,
1975 const exprt &value_as_byte_array,
1976 const std::optional<exprt> &non_const_update_bound,
1984 std::size_t first = 0;
1991 bitst offset_bits_in_target_element = offset_bits - bits_already_set;
1993 if(offset_bits_in_target_element <
bitst{0})
1996 value_as_byte_array.
id() != ID_array ||
2000 "indices past the update should be handled below");
2008 if(offset_bits_in_target_element <
bitst{0})
2009 update_elements +=
bytest{1};
2016 update_elements >
bytest{0},
2017 "indices before the update should be handled above");
2021 if(value_as_byte_array.
id() == ID_array)
2022 end = std::min(end, value_as_byte_array.
operands().size());
2025 const std::size_t update_size = update_values.size();
2034 offset_bits_in_target_element >=
bitst{0} &&
2037 new_value =
array_exprt{update_values, update_type};
2041 if(src.
id() == ID_byte_update_little_endian)
2042 std::reverse(update_values.begin(), update_values.end());
2043 if(offset_bits_in_target_element <
bitst{0})
2045 if(src.
id() == ID_byte_update_little_endian)
2062 const std::size_t offset_bits_int =
2064 const std::size_t subtype_bits_int =
2069 subtype_bits_int - offset_bits_int,
2081 const irep_idt extract_opcode = src.
id() == ID_byte_update_little_endian
2082 ? ID_byte_extract_little_endian
2083 : ID_byte_extract_big_endian;
2094 offset_bits_in_target_element =
bitst{0};
2110 std::optional<exprt> update_bound;
2111 if(non_const_update_bound.has_value())
2119 *non_const_update_bound,
2123 *non_const_update_bound,
2133 remaining_update_bytes},
2152 const typet &subtype,
2153 const std::optional<bitst> &subtype_bits,
2154 const exprt &value_as_byte_array,
2155 const std::optional<exprt> &non_const_update_bound,
2158 const bool is_array = src.
type().
id() == ID_array;
2175 !subtype_bits.has_value() || value_as_byte_array.
id() != ID_array)
2178 src, subtype, value_as_byte_array, non_const_update_bound, ns);
2181 std::size_t num_elements =
2188 elements.reserve(num_elements);
2192 for(; i < num_elements && (i + 1) * *subtype_bits <= offset_bits; ++i)
2193 elements.push_back(
index_exprt{src.op(), from_integer(i, index_type)});
2196 for(; i < num_elements &&
2209 value_as_byte_array,
2210 non_const_update_bound,
2215 for(; i < num_elements; ++i)
2216 elements.push_back(
index_exprt{src.op(), from_integer(i, index_type)});
2238 const exprt &value_as_byte_array,
2239 const std::optional<exprt> &non_const_update_bound,
2243 elements.reserve(struct_type.
components().size());
2246 for(
const auto &comp : struct_type.
components())
2260 if(!offset_bits.has_value() || !element_width.has_value())
2267 const irep_idt extract_opcode = src.
id() == ID_byte_update_little_endian
2268 ? ID_byte_extract_little_endian
2269 : ID_byte_extract_big_endian;
2289 bu, value_as_byte_array, non_const_update_bound, ns),
2304 *offset_bits >= *element_width ||
2305 (value_as_byte_array.
id() == ID_array && *offset_bits <
bitst{0} &&
2310 elements.push_back(member);
2321 value_as_byte_array,
2322 non_const_update_bound,
2343 const exprt &value_as_byte_array,
2344 const std::optional<exprt> &non_const_update_bound,
2350 widest_member.has_value(),
2351 "lower_byte_update of union of unknown size is not supported");
2355 src.
op(), widest_member->first.get_name(), widest_member->first.
type()});
2356 bu.
type() = widest_member->first.type();
2359 widest_member->first.get_name(),
2374 const exprt &value_as_byte_array,
2375 const std::optional<exprt> &non_const_update_bound,
2378 if(src.
type().
id() == ID_array || src.
type().
id() == ID_vector)
2380 std::optional<typet> subtype;
2381 if(src.
type().
id() == ID_vector)
2389 element_width.has_value() &&
2390 *element_width ==
bitst{src.get_bits_per_byte()})
2392 if(value_as_byte_array.
id() != ID_array)
2395 non_const_update_bound.has_value(),
2396 "constant update bound should yield an array expression");
2398 src, *subtype, value_as_byte_array, *non_const_update_bound, ns);
2405 non_const_update_bound,
2414 value_as_byte_array,
2415 non_const_update_bound,
2419 else if(src.
type().
id() == ID_struct || src.
type().
id() == ID_struct_tag)
2422 src.
type().
id() == ID_struct_tag
2426 src, struct_type, value_as_byte_array, non_const_update_bound, ns);
2430 else if(src.
type().
id() == ID_union || src.
type().
id() == ID_union_tag)
2433 src.
type().
id() == ID_union_tag
2437 src, union_type, value_as_byte_array, non_const_update_bound, ns);
2443 src.
type().
id() == ID_c_enum || src.
type().
id() == ID_c_enum_tag)
2452 if(value_as_byte_array.
id() == ID_array)
2453 update_bytes = value_as_byte_array.
operands();
2457 value_as_byte_array,
2464 const std::size_t update_size_bits =
2466 const std::size_t bit_width = std::max(type_bits, update_size_bits);
2468 const bool is_little_endian = src.
id() == ID_byte_update_little_endian;
2472 if(bit_width > type_bits)
2479 if(!is_little_endian)
2485 if(non_const_update_bound.has_value())
2489 src.
id() == ID_byte_update_little_endian,
2491 bytest{update_bytes.size()},
2496 for(std::size_t i = 0; i < update_bytes.size(); ++i)
2502 *non_const_update_bound},
2510 if(is_little_endian)
2515 power(2, bit_width) -
power(2, bit_width - update_size_bits),
2530 if(!is_little_endian)
2543 if(is_little_endian)
2546 exprt zero_extended;
2547 if(bit_width > update_size_bits)
2549 if(is_little_endian)
2561 zero_extended = value;
2566 shl_exprt{zero_extended, offset_in_bits},
2568 if(!is_little_endian)
2577 bitor_exprt bitor_expr{bitand_expr, value_shifted};
2579 if(bit_width > type_bits)
2582 bitor_expr.
type(), src.
id() == ID_byte_update_little_endian, ns);
2583 const auto bounds =
map_bounds(endianness_map, 0, type_bits - 1);
2599 false,
"lower_byte_update does not yet support ", src.
type().
id_string());
2606 src.
id() == ID_byte_update_little_endian ||
2607 src.
id() == ID_byte_update_big_endian,
2608 "byte update expression should either be little or big endian");
2624 std::optional<exprt> non_const_update_bound;
2629 simplify(update_size_expr_opt.value(), ns);
2631 const irep_idt extract_opcode = src.
id() == ID_byte_update_little_endian
2632 ? ID_byte_extract_little_endian
2633 : ID_byte_extract_big_endian;
2636 if(!update_size_expr_opt.value().is_constant())
2637 non_const_update_bound = *update_size_expr_opt;
2645 update_bits.has_value(),
"constant size-of should imply fixed bit width");
2648 if(update_bits_int % bits_per_byte != 0)
2652 "non-byte aligned type expected to be a bitvector type");
2663 const exprt overlapping_byte =
2666 size_t n_extra_bits = bits_per_byte - update_bits_int % bits_per_byte;
2671 update_value,
bv_typet{update_bits_int}),
2678 update_bits_int / bits_per_byte, update_size_expr_opt->type());
2707 src.
id() == ID_byte_update_little_endian ||
2708 src.
id() == ID_byte_update_big_endian)
2713 src.
id() == ID_byte_extract_little_endian ||
2714 src.
id() == ID_byte_extract_big_endian)
API to expression classes for bitvectors.
const shift_exprt & to_shift_expr(const exprt &expr)
Cast an exprt to a shift_exprt.
const concatenation_exprt & to_concatenation_expr(const exprt &expr)
Cast an exprt to a concatenation_exprt.
const bitvector_typet & to_bitvector_type(const typet &type)
Cast a typet to a bitvector_typet.
bool can_cast_type< bitvector_typet >(const typet &type)
Check whether a reference to a typet is a bitvector_typet.
Expression classes for byte-level operators.
const byte_update_exprt & to_byte_update_expr(const exprt &expr)
exprt lower_byte_extract(const byte_extract_exprt &src, const namespacet &ns)
Rewrite a byte extract expression to more fundamental operations.
const byte_extract_exprt & to_byte_extract_expr(const exprt &expr)
const c_bit_field_typet & to_c_bit_field_type(const typet &type)
Cast a typet to a c_bit_field_typet.
const c_enum_typet & to_c_enum_type(const typet &type)
Cast a typet to a c_enum_typet.
const union_typet & to_union_type(const typet &type)
Cast a typet to a union_typet.
const union_tag_typet & to_union_tag_type(const typet &type)
Cast a typet to a union_tag_typet.
Expression to define a mapping from an argument (index) to elements.
Array constructor from list of elements.
typet index_type() const
The type of the index expressions into any instance of this type.
const exprt & size() const
const typet & element_type() const
The type of the elements of the array.
A base class for expressions that are predicates, i.e., Boolean-typed, and that take exactly two argu...
Bit-wise AND Any number of operands that is greater or equal one.
Bit-wise negation of bit-vectors.
Bit-wise OR Any number of operands that is greater or equal one.
Base class of fixed-width bit-vector types.
constant_exprt all_zeros_expr() const
std::size_t get_width() const
Fixed-width bit-vector without numerical interpretation.
Expression corresponding to op() where the bytes starting at position offset (given in number of byte...
const exprt & offset() const
std::size_t get_bits_per_byte() const
const exprt & value() const
Type for C bit fields These are both 'bitvector_typet' (they have a width) and 'type_with_subtypet' (...
Complex constructor from a pair of numbers.
Imaginary part of the expression describing a complex number.
Real part of the expression describing a complex number.
Complex numbers made of pair of given subtype.
Concatenation of bit-vector operands.
Union constructor to support unions without any member (a GCC/Clang feature).
Maps a big-endian offset to a little-endian offset.
size_t number_of_bits() const
size_t map_bit(size_t bit) const
Base class for all expressions.
std::vector< exprt > operandst
bool is_constant() const
Return whether the expression is a constant.
typet & type()
Return the type of the expression.
void add_to_operands(const exprt &expr)
Add the given argument to the end of exprt's operands.
The trinary if-then-else operator.
const std::string & id_string() const
const irep_idt & id() const
Extract member of struct or union.
Modulo defined as lhs-(rhs * truncate(lhs/rhs)).
Binary multiplication Associativity is not specified.
const union_typet & follow_tag(const union_tag_typet &) const
Follow type tag of union type.
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Boolean OR All operands must be boolean, and the result is always boolean.
The plus expression Associativity is not specified.
Fixed-width bit-vector with two's complement interpretation.
Struct constructor from list of elements.
Structure type, corresponds to C style structs.
const componentst & components() const
std::vector< componentt > componentst
Expression to hold a symbol (variable).
const typet & subtype() const
Semantic type conversion.
static exprt conditional_cast(const exprt &expr, const typet &type)
The type of an expression, extends irept.
The unary minus expression.
Union constructor from single element.
std::optional< std::pair< struct_union_typet::componentt, bitst > > find_widest_union_component(const namespacet &ns) const
Determine the member of maximum bit width in a union type.
Fixed-width bit-vector with unsigned binary interpretation.
Vector constructor from list of elements.
const constant_exprt & size() const
typet index_type() const
The type of any index expression into an instance of this type.
const typet & element_type() const
The type of the elements of the vector.
Operator to update elements in structs and arrays.
zero extension The operand is converted to the given type by either a) truncating if the new type is ...
#define Forall_operands(it, expr)
bool can_cast_type(const typet &base)
Check whether a reference to a generic typet is of a specific derived class.
const exprt & skip_typecast(const exprt &expr)
find the expression nested inside typecasts, if any
Deprecated expression utility functions.
static exprt lower_byte_update_array_vector(const byte_update_exprt &src, const typet &subtype, const std::optional< bitst > &subtype_bits, const exprt &value_as_byte_array, const std::optional< exprt > &non_const_update_bound, const namespacet &ns)
Apply a byte update src to an array/vector typed operand using the byte array value_as_byte_array as ...
static exprt lower_byte_extract_array_vector(const byte_extract_exprt &src, const byte_extract_exprt &unpacked, const typet &subtype, const bitst &element_bits, const namespacet &ns)
Rewrite a byte extraction of an array/vector-typed result to byte extraction of the individual compon...
static exprt lower_byte_update_union(const byte_update_exprt &src, const union_typet &union_type, const exprt &value_as_byte_array, const std::optional< exprt > &non_const_update_bound, const namespacet &ns)
Apply a byte update src to a union typed operand using the byte array value_as_byte_array as update v...
static exprt lower_byte_update_byte_array_vector(const byte_update_exprt &src, const typet &subtype, const array_exprt &value_as_byte_array, const std::optional< exprt > &non_const_update_bound, const namespacet &ns)
Apply a byte update src to an array/vector of bytes using the byte array value_as_byte_array as updat...
static exprt bv_to_expr(const exprt &bitvector_expr, const typet &target_type, const endianness_mapt &endianness_map, const namespacet &ns)
Convert a bitvector-typed expression bitvector_expr to an expression of type target_type.
static exprt bv_to_union_expr(const exprt &bitvector_expr, const union_typet &union_type, const endianness_mapt &endianness_map, const namespacet &ns)
Convert a bitvector-typed expression bitvector_expr to a union-typed expression.
static complex_exprt bv_to_complex_expr(const exprt &bitvector_expr, const complex_typet &complex_type, const endianness_mapt &endianness_map, const namespacet &ns)
Convert a bitvector-typed expression bitvector_expr to a complex-typed expression.
static exprt unpack_array_vector(const exprt &src, const std::optional< mp_integer > &src_size, const bitst &element_bits, bool little_endian, const std::optional< bytest > &offset_bytes, const std::optional< bytest > &max_bytes, const std::size_t bits_per_byte, const namespacet &ns)
Rewrite an array or vector into its individual bytes.
static exprt::operandst instantiate_byte_array(const exprt &src, std::size_t lower_bound, std::size_t upper_bound, const std::size_t bits_per_byte, const namespacet &ns)
Build the individual bytes to be used in an update.
exprt lower_byte_extract(const byte_extract_exprt &src, const namespacet &ns)
rewrite byte extraction from an array to byte extraction from a concatenation of array index expressi...
static exprt lower_byte_update_single_element(const byte_update_exprt &src, const bitst &offset_bits, const exprt &element_to_update, const bitst &subtype_bits, const bitst &bits_already_set, const exprt &value_as_byte_array, const std::optional< exprt > &non_const_update_bound, const namespacet &ns)
Byte update a struct/array/vector element.
bitvector_typet adjust_width(const typet &src, std::size_t new_width)
changes the width of the given bitvector type
static exprt unpack_rec(const exprt &src, bool little_endian, const std::optional< bytest > &offset_bytes, const std::optional< bytest > &max_bytes, const std::size_t bits_per_byte, const namespacet &ns, bool unpack_byte_array=false)
Rewrite an object into its individual bytes.
static vector_exprt bv_to_vector_expr(const exprt &bitvector_expr, const vector_typet &vector_type, const endianness_mapt &endianness_map, const namespacet &ns)
Convert a bitvector-typed expression bitvector_expr to a vector-typed expression.
static struct_exprt bv_to_struct_expr(const exprt &bitvector_expr, const struct_typet &struct_type, const endianness_mapt &endianness_map, const namespacet &ns)
Convert a bitvector-typed expression bitvector_expr to a struct-typed expression.
static array_exprt unpack_complex(const exprt &src, bool little_endian, const std::size_t bits_per_byte, const namespacet &ns)
Rewrite a complex_exprt into its individual bytes.
static exprt unpack_array_vector_no_known_bounds(const exprt &src, std::size_t el_bytes, bool little_endian, const std::size_t bits_per_byte, const namespacet &ns)
Rewrite an array or vector into its individual bytes when no maximum number of bytes is known.
static exprt lower_byte_update_array_vector_non_const(const byte_update_exprt &src, const typet &subtype, const exprt &value_as_byte_array, const std::optional< exprt > &non_const_update_bound, const namespacet &ns)
Apply a byte update src to an array/vector typed operand, using the byte array value_as_byte_array as...
static array_exprt bv_to_array_expr(const exprt &bitvector_expr, const array_typet &array_type, const endianness_mapt &endianness_map, const namespacet &ns)
Convert a bitvector-typed expression bitvector_expr to an array-typed expression.
static boundst map_bounds(const endianness_mapt &endianness_map, std::size_t lower_bound, std::size_t upper_bound)
Map bit boundaries according to endianness.
exprt lower_byte_operators(const exprt &src, const namespacet &ns)
Rewrite an expression possibly containing byte-extract or -update expressions to more fundamental ope...
static void process_bit_fields(exprt::operandst &&bit_fields, std::size_t total_bits, exprt::operandst &dest, bool little_endian, const std::optional< bytest > &offset_bytes, const std::optional< bytest > &max_bytes, const std::size_t bits_per_byte, const namespacet &ns)
Extract bytes from a sequence of bitvector-typed elements.
static exprt lower_byte_update(const byte_update_exprt &src, const exprt &value_as_byte_array, const std::optional< exprt > &non_const_update_bound, const namespacet &ns)
Apply a byte update src using the byte array value_as_byte_array as update value.
static exprt lower_byte_update_struct(const byte_update_exprt &src, const struct_typet &struct_type, const exprt &value_as_byte_array, const std::optional< exprt > &non_const_update_bound, const namespacet &ns)
Apply a byte update src to a struct typed operand using the byte array value_as_byte_array as update ...
static array_exprt unpack_struct(const exprt &src, bool little_endian, const std::optional< bytest > &offset_bytes, const std::optional< bytest > &max_bytes, const std::size_t bits_per_byte, const namespacet &ns)
Rewrite a struct-typed expression into its individual bytes.
static std::optional< exprt > lower_byte_extract_complex(const byte_extract_exprt &src, const byte_extract_exprt &unpacked, const namespacet &ns)
Rewrite a byte extraction of a complex-typed result to byte extraction of the individual components t...
static exprt lower_byte_update_byte_array_vector_non_const(const byte_update_exprt &src, const typet &subtype, const exprt &value_as_byte_array, const exprt &non_const_update_bound, const namespacet &ns)
Apply a byte update src to an array/vector of bytes using the byte-array typed expression value_as_by...
static exprt lower_byte_update_array_vector_unbounded(const byte_update_exprt &src, const typet &subtype, const exprt &subtype_size, const exprt &value_as_byte_array, const exprt &non_const_update_bound, const exprt &initial_bytes, const exprt &first_index, const exprt &first_update_value, const namespacet &ns)
Apply a byte update src to an array/vector typed operand, using the byte array value_as_byte_array as...
bitst bytes_to_bits(const bytest &bytes, const mp_integer &bits_per_byte)
Convert bytes to bits assuming bits_per_byte number of bits in one byte.
bytest bits_to_bytes_ceil(const bitst &bits, const mp_integer &bits_per_byte)
Convert bits to bytes assuming bits_per_byte number of bits in one byte, rounding upwards.
bytest bits_to_bytes_trunc(const bitst &bits, const mp_integer &bits_per_byte)
Convert bits to bytes assuming bits_per_byte number of bits in one byte, rounding downwards.
named_mp_integert< bits_tagt > bitst
named_mp_integert< bytes_tagt > bytest
output_type narrow_cast(input_type value)
Alias for static_cast intended to be used for numeric casting Rationale: Easier to grep than static_c...
const pointer_typet & to_pointer_type(const typet &type)
Cast a typet to a pointer_typet.
std::optional< bitst > pointer_offset_bits(const typet &type, const namespacet &ns)
std::optional< bitst > member_offset_bits(const struct_typet &type, const irep_idt &member, const namespacet &ns)
std::optional< exprt > size_of_expr(const typet &type, const namespacet &ns)
std::optional< exprt > member_offset_expr(const member_exprt &member_expr, const namespacet &ns)
const std::size_t sharing_mapt< keyT, valueT, fail_if_equal, hashT, equalT >::mask
bool simplify(exprt &expr, const namespacet &ns)
exprt simplify_expr(exprt src, const namespacet &ns)
#define PRECONDITION_WITH_DIAGNOSTICS(CONDITION,...)
#define CHECK_RETURN(CONDITION)
#define DATA_INVARIANT(CONDITION, REASON)
This condition should be used to document that assumptions that are made on goto_functions,...
#define PRECONDITION(CONDITION)
#define INVARIANT(CONDITION, REASON)
This macro uses the wrapper function 'invariant_violated_string'.
const index_exprt & to_index_expr(const exprt &expr)
Cast an exprt to an index_exprt.
const array_exprt & to_array_expr(const exprt &expr)
Cast an exprt to an array_exprt.
const constant_exprt & to_constant_expr(const exprt &expr)
Cast an exprt to a constant_exprt.
const vector_typet & to_vector_type(const typet &type)
Cast a typet to a vector_typet.
const struct_typet & to_struct_type(const typet &type)
Cast a typet to a struct_typet.
const struct_tag_typet & to_struct_tag_type(const typet &type)
Cast a typet to a struct_tag_typet.
const complex_typet & to_complex_type(const typet &type)
Cast a typet to a complex_typet.
const array_typet & to_array_type(const typet &type)
Cast a typet to an array_typet.
const string_constantt & to_string_constant(const exprt &expr)
static bool failed(bool error_indicator)
const type_with_subtypet & to_type_with_subtype(const typet &type)