CBMC
Loading...
Searching...
No Matches
lower_byte_operators.cpp
Go to the documentation of this file.
1/*******************************************************************\
2
3Module:
4
5Author: Daniel Kroening, kroening@kroening.com
6
7\*******************************************************************/
8
9#include "arith_tools.h"
10#include "bitvector_expr.h"
11#include "byte_operators.h"
12#include "c_types.h"
13#include "endianness_map.h"
14#include "expr_util.h"
15#include "namespace.h"
16#include "narrow.h"
17#include "pointer_offset_size.h"
18#include "simplify_expr.h"
19#include "string_constant.h"
20
21#include <algorithm>
22
23static exprt bv_to_expr(
24 const exprt &bitvector_expr,
25 const typet &target_type,
26 const endianness_mapt &endianness_map,
27 const namespacet &ns);
28
29struct boundst
30{
31 std::size_t lb;
32 std::size_t ub;
33};
34
37 const endianness_mapt &endianness_map,
38 std::size_t lower_bound,
39 std::size_t upper_bound)
40{
41 boundst result;
42 result.lb = lower_bound;
43 result.ub = upper_bound;
44
45 if(result.ub < endianness_map.number_of_bits())
46 {
47 result.lb = endianness_map.map_bit(result.lb);
48 result.ub = endianness_map.map_bit(result.ub);
49
50 // big-endian bounds need swapping
51 if(result.ub < result.lb)
52 std::swap(result.lb, result.ub);
53 }
54
55 return result;
56}
57
59bitvector_typet adjust_width(const typet &src, std::size_t new_width)
60{
61 if(src.id() == ID_unsignedbv)
62 return unsignedbv_typet(new_width);
63 else if(src.id() == ID_signedbv)
64 return signedbv_typet(new_width);
65 else if(src.id() == ID_bv)
66 return bv_typet(new_width);
67 else if(src.id() == ID_c_enum) // we use the underlying type
68 return adjust_width(to_c_enum_type(src).underlying_type(), new_width);
69 else if(src.id() == ID_c_bit_field)
70 return c_bit_field_typet(
71 to_c_bit_field_type(src).underlying_type(), new_width);
72 else
73 PRECONDITION(false);
74}
75
79 const exprt &bitvector_expr,
80 const struct_typet &struct_type,
81 const endianness_mapt &endianness_map,
82 const namespacet &ns)
83{
84 const struct_typet::componentst &components = struct_type.components();
85
86 exprt::operandst operands;
87 operands.reserve(components.size());
88 std::size_t member_offset_bits = 0;
89 for(const auto &comp : components)
90 {
91 const auto component_bits_opt = pointer_offset_bits(comp.type(), ns);
92 std::size_t component_bits;
93 if(component_bits_opt.has_value())
94 component_bits = numeric_cast_v<std::size_t>(*component_bits_opt);
95 else
96 component_bits = to_bitvector_type(bitvector_expr.type()).get_width() -
98
99 if(component_bits == 0)
100 {
101 operands.push_back(
102 bv_to_expr(bitvector_expr, comp.type(), endianness_map, ns));
103 continue;
104 }
105
106 const auto bounds = map_bounds(
107 endianness_map,
109 member_offset_bits + component_bits - 1);
110 bitvector_typet type = adjust_width(bitvector_expr.type(), component_bits);
111 PRECONDITION(pointer_offset_bits(bitvector_expr.type(), ns).has_value());
112 operands.push_back(bv_to_expr(
113 extractbits_exprt{bitvector_expr, bounds.lb, std::move(type)},
114 comp.type(),
115 endianness_map,
116 ns));
117
118 if(component_bits_opt.has_value())
119 member_offset_bits += component_bits;
120 }
121
122 return struct_exprt{std::move(operands), struct_type};
123}
124
128 const exprt &bitvector_expr,
129 const union_typet &union_type,
130 const endianness_mapt &endianness_map,
131 const namespacet &ns)
132{
133 const union_typet::componentst &components = union_type.components();
134
135 if(components.empty())
136 return empty_union_exprt{union_type};
137
138 const auto widest_member = union_type.find_widest_union_component(ns);
139
140 std::size_t component_bits;
141 if(widest_member.has_value())
142 component_bits = numeric_cast_v<std::size_t>(widest_member->second);
143 else
144 component_bits = to_bitvector_type(bitvector_expr.type()).get_width();
145
146 if(component_bits == 0)
147 {
148 return union_exprt{
149 components.front().get_name(),
150 bv_to_expr(bitvector_expr, components.front().type(), endianness_map, ns),
151 union_type};
152 }
153
154 const auto bounds = map_bounds(endianness_map, 0, component_bits - 1);
155 bitvector_typet type = adjust_width(bitvector_expr.type(), component_bits);
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();
162 PRECONDITION(pointer_offset_bits(bitvector_expr.type(), ns).has_value());
163 return union_exprt{
164 component_name,
166 extractbits_exprt{bitvector_expr, bounds.lb, std::move(type)},
167 component_type,
168 endianness_map,
169 ns),
170 union_type};
171}
172
176 const exprt &bitvector_expr,
177 const array_typet &array_type,
178 const endianness_mapt &endianness_map,
179 const namespacet &ns)
180{
181 auto num_elements = numeric_cast<std::size_t>(array_type.size());
182 auto subtype_bits = pointer_offset_bits(array_type.element_type(), ns);
183
184 const std::size_t total_bits =
185 to_bitvector_type(bitvector_expr.type()).get_width();
186 if(!num_elements.has_value())
187 {
188 if(!subtype_bits.has_value() || *subtype_bits == bitst{0})
189 num_elements = total_bits;
190 else
191 num_elements = total_bits / numeric_cast_v<std::size_t>(*subtype_bits);
192 }
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");
197
198 exprt::operandst operands;
199 operands.reserve(*num_elements);
200 for(std::size_t i = 0; i < *num_elements; ++i)
201 {
202 if(subtype_bits.has_value())
203 {
204 const std::size_t subtype_bits_int =
205 numeric_cast_v<std::size_t>(*subtype_bits);
206 const auto bounds = map_bounds(
207 endianness_map, i * subtype_bits_int, ((i + 1) * subtype_bits_int) - 1);
208 bitvector_typet type =
209 adjust_width(bitvector_expr.type(), subtype_bits_int);
210 PRECONDITION(pointer_offset_bits(bitvector_expr.type(), ns).has_value());
211 operands.push_back(bv_to_expr(
212 extractbits_exprt{bitvector_expr, bounds.lb, std::move(type)},
213 array_type.element_type(),
214 endianness_map,
215 ns));
216 }
217 else
218 {
219 operands.push_back(bv_to_expr(
220 bitvector_expr, array_type.element_type(), endianness_map, ns));
221 }
222 }
223
224 return array_exprt{std::move(operands), array_type};
225}
226
230 const exprt &bitvector_expr,
231 const vector_typet &vector_type,
232 const endianness_mapt &endianness_map,
233 const namespacet &ns)
234{
235 const std::size_t num_elements =
236 numeric_cast_v<std::size_t>(vector_type.size());
237 auto subtype_bits = pointer_offset_bits(vector_type.element_type(), ns);
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");
243
244 exprt::operandst operands;
245 operands.reserve(num_elements);
246 for(std::size_t i = 0; i < num_elements; ++i)
247 {
248 if(subtype_bits.has_value())
249 {
250 const std::size_t subtype_bits_int =
251 numeric_cast_v<std::size_t>(*subtype_bits);
252 const auto bounds = map_bounds(
253 endianness_map, i * subtype_bits_int, ((i + 1) * subtype_bits_int) - 1);
254 bitvector_typet type =
255 adjust_width(bitvector_expr.type(), subtype_bits_int);
256 PRECONDITION(pointer_offset_bits(bitvector_expr.type(), ns).has_value());
257 operands.push_back(bv_to_expr(
258 extractbits_exprt{bitvector_expr, bounds.lb, std::move(type)},
259 vector_type.element_type(),
260 endianness_map,
261 ns));
262 }
263 else
264 {
265 operands.push_back(bv_to_expr(
266 bitvector_expr, vector_type.element_type(), endianness_map, ns));
267 }
268 }
269
270 return vector_exprt{std::move(operands), vector_type};
271}
272
276 const exprt &bitvector_expr,
277 const complex_typet &complex_type,
278 const endianness_mapt &endianness_map,
279 const namespacet &ns)
280{
281 const std::size_t total_bits =
282 to_bitvector_type(bitvector_expr.type()).get_width();
283 const auto subtype_bits_opt = pointer_offset_bits(complex_type.subtype(), ns);
284 std::size_t subtype_bits;
285 if(subtype_bits_opt.has_value())
286 {
287 subtype_bits = numeric_cast_v<std::size_t>(*subtype_bits_opt);
289 subtype_bits * 2 == total_bits,
290 "subtype width should be half of the total bitvector width");
291 }
292 else
293 subtype_bits = total_bits / 2;
294
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);
298
299 const bitvector_typet type =
300 adjust_width(bitvector_expr.type(), subtype_bits);
301
302 PRECONDITION(pointer_offset_bits(bitvector_expr.type(), ns).has_value());
303 return complex_exprt{
305 extractbits_exprt{bitvector_expr, bounds_real.lb, type},
306 complex_type.subtype(),
307 endianness_map,
308 ns),
310 extractbits_exprt{bitvector_expr, bounds_imag.lb, type},
311 complex_type.subtype(),
312 endianness_map,
313 ns),
314 complex_type};
315}
316
331 const exprt &bitvector_expr,
332 const typet &target_type,
333 const endianness_mapt &endianness_map,
334 const namespacet &ns)
335{
337
338 if(target_type.id() == ID_floatbv)
339 {
340 std::size_t width = to_bitvector_type(bitvector_expr.type()).get_width();
341 exprt bv_expr =
342 typecast_exprt::conditional_cast(bitvector_expr, bv_typet{width});
343 return simplify_expr(
344 typecast_exprt::conditional_cast(bv_expr, target_type), ns);
345 }
346 else if(
347 can_cast_type<bitvector_typet>(target_type) ||
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 &&
351 to_bitvector_type(bitvector_expr.type()).get_width() == 1))
352 {
353 return simplify_expr(
354 typecast_exprt::conditional_cast(bitvector_expr, target_type), ns);
355 }
356 else if(target_type.id() == ID_struct)
357 {
358 return bv_to_struct_expr(
359 bitvector_expr, to_struct_type(target_type), endianness_map, ns);
360 }
361 else if(target_type.id() == ID_struct_tag)
362 {
364 bitvector_expr,
365 ns.follow_tag(to_struct_tag_type(target_type)),
366 endianness_map,
367 ns);
368 result.type() = target_type;
369 return std::move(result);
370 }
371 else if(target_type.id() == ID_union)
372 {
373 return bv_to_union_expr(
374 bitvector_expr, to_union_type(target_type), endianness_map, ns);
375 }
376 else if(target_type.id() == ID_union_tag)
377 {
378 exprt result = bv_to_union_expr(
379 bitvector_expr,
380 ns.follow_tag(to_union_tag_type(target_type)),
381 endianness_map,
382 ns);
383 result.type() = target_type;
384 return result;
385 }
386 else if(target_type.id() == ID_array)
387 {
388 return bv_to_array_expr(
389 bitvector_expr, to_array_type(target_type), endianness_map, ns);
390 }
391 else if(target_type.id() == ID_vector)
392 {
393 return bv_to_vector_expr(
394 bitvector_expr, to_vector_type(target_type), endianness_map, ns);
395 }
396 else if(target_type.id() == ID_complex)
397 {
398 return bv_to_complex_expr(
399 bitvector_expr, to_complex_type(target_type), endianness_map, ns);
400 }
401 else
402 {
404 false, "bv_to_expr does not yet support ", target_type.id_string());
405 }
406}
407
408static exprt unpack_rec(
409 const exprt &src,
410 bool little_endian,
411 const std::optional<bytest> &offset_bytes,
412 const std::optional<bytest> &max_bytes,
413 const std::size_t bits_per_byte,
414 const namespacet &ns,
415 bool unpack_byte_array = false);
416
425 const exprt &src,
426 std::size_t lower_bound,
427 std::size_t upper_bound,
428 const std::size_t bits_per_byte,
429 const namespacet &ns)
430{
431 PRECONDITION(lower_bound <= upper_bound);
432
433 if(src.id() == ID_array)
434 {
435 PRECONDITION(upper_bound <= src.operands().size());
436 return exprt::operandst{
437 src.operands().begin() + narrow_cast<std::ptrdiff_t>(lower_bound),
438 src.operands().begin() + narrow_cast<std::ptrdiff_t>(upper_bound)};
439 }
440
441 const typet &element_type = src.type().id() == ID_array
444 const typet index_type = src.type().id() == ID_array
445 ? to_array_type(src.type()).index_type()
446 : to_vector_type(src.type()).index_type();
448 can_cast_type<bitvector_typet>(element_type) &&
449 to_bitvector_type(element_type).get_width() == bits_per_byte);
450 exprt::operandst bytes;
451 bytes.reserve(upper_bound - lower_bound);
452 for(std::size_t i = lower_bound; i < upper_bound; ++i)
453 {
454 const index_exprt idx{src, from_integer(i, index_type)};
455 bytes.push_back(simplify_expr(idx, ns));
456 }
457 return bytes;
458}
459
469 const exprt &src,
470 std::size_t el_bytes,
471 bool little_endian,
472 const std::size_t bits_per_byte,
473 const namespacet &ns)
474{
475 const typet index_type = src.type().id() == ID_array
476 ? to_array_type(src.type()).index_type()
477 : to_vector_type(src.type()).index_type();
478
479 // TODO we either need a symbol table here or make array comprehensions
480 // introduce a scope
481 static std::size_t array_comprehension_index_counter = 0;
482 ++array_comprehension_index_counter;
483 symbol_exprt array_comprehension_index{
484 "$array_comprehension_index_a_v" +
485 std::to_string(array_comprehension_index_counter),
486 index_type};
487
488 index_exprt element{
489 src,
490 div_exprt{array_comprehension_index, from_integer(el_bytes, index_type)}};
491
492 exprt sub =
493 unpack_rec(element, little_endian, {}, {}, bits_per_byte, ns, false);
494 exprt::operandst sub_operands =
495 instantiate_byte_array(sub, 0, el_bytes, bits_per_byte, ns);
496
497 exprt body = sub_operands.front();
498 const mod_exprt offset{
499 array_comprehension_index,
500 from_integer(el_bytes, array_comprehension_index.type())};
501 for(std::size_t i = 1; i < el_bytes; ++i)
502 {
503 body = if_exprt{
504 equal_exprt{offset, from_integer(i, array_comprehension_index.type())},
505 sub_operands[i],
506 body};
507 }
508
509 const exprt array_vector_size = src.type().id() == ID_vector
510 ? to_vector_type(src.type()).size()
511 : to_array_type(src.type()).size();
512
513 if(array_vector_size.is_nil())
514 {
515 // The source array/vector has no statically known size (an incomplete
516 // type whose extent is genuinely unknown here, e.g. an extern array
517 // declared without a bound). Represent it as a zero-length comprehension;
518 // any byte access into it is then out of bounds and becomes nondet/fails,
519 // which is the behaviour the extern6 regression exercises.
521 std::move(array_comprehension_index),
522 std::move(body),
523 array_typet{bv_typet{bits_per_byte}, from_integer(0, size_type())}};
524 }
525
527 std::move(array_comprehension_index),
528 std::move(body),
530 bv_typet{bits_per_byte},
532 array_vector_size, from_integer(el_bytes, array_vector_size.type())}}};
533}
534
551 const exprt &src,
552 const std::optional<mp_integer> &src_size,
553 const bitst &element_bits,
554 bool little_endian,
555 const std::optional<bytest> &offset_bytes,
556 const std::optional<bytest> &max_bytes,
557 const std::size_t bits_per_byte,
558 const namespacet &ns)
559{
560 const bytest el_bytes = bits_to_bytes_ceil(element_bits, bits_per_byte);
561
562 if(!src_size.has_value() && !max_bytes.has_value())
563 {
565 el_bytes > bytest{0} && element_bits % bitst{bits_per_byte} == bitst{0},
566 "unpacking of arrays with non-byte-aligned elements is not supported");
568 src,
570 little_endian,
571 bits_per_byte,
572 ns);
573 }
574
575 exprt::operandst byte_operands;
576 mp_integer first_element = 0;
577
578 // refine the number of elements to extract in case the element width is known
579 // and a multiple of bytes; otherwise we will expand the entire array/vector
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})
584 {
585 if(!num_elements.has_value())
586 {
587 // turn bytes into elements, rounding up
588 num_elements = (*max_bytes + el_bytes - bytest{1}) / el_bytes;
589 }
590
591 if(offset_bytes.has_value())
592 {
593 // compute offset as number of elements
594 first_element = *offset_bytes / el_bytes;
595 // insert offset_bytes-many nil bytes into the output array
596 byte_operands.resize(
598 *offset_bytes - (*offset_bytes % el_bytes),
599 *num_elements * el_bytes)),
600 bv_typet{bits_per_byte}.all_zeros_expr());
601 }
602 }
603
604 // the maximum number of bytes is an upper bound in case the size of the
605 // array/vector is unknown; if element_bits was usable above this will
606 // have been turned into a number of elements already
607 if(!num_elements)
608 num_elements = max_bytes->get();
609
610 const exprt src_simp = simplify_expr(src, ns);
611 const typet index_type = src_simp.type().id() == ID_array
612 ? to_array_type(src_simp.type()).index_type()
613 : to_vector_type(src_simp.type()).index_type();
614
615 for(mp_integer i = first_element; i < *num_elements; ++i)
616 {
617 exprt element;
618
619 if(
620 (src_simp.id() == ID_array || src_simp.id() == ID_vector) &&
621 i < src_simp.operands().size())
622 {
623 const std::size_t index_int = numeric_cast_v<std::size_t>(i);
624 element = src_simp.operands()[index_int];
625 }
626 else
627 {
628 element = index_exprt(src_simp, from_integer(i, index_type));
629 }
630
631 // recursively unpack each element so that we eventually just have an array
632 // of bytes left
633
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>{};
637 const std::size_t element_max_bytes_int = numeric_cast_v<std::size_t>(
638 element_max_bytes ? *element_max_bytes : el_bytes);
639
640 exprt sub = unpack_rec(
641 element, little_endian, {}, element_max_bytes, bits_per_byte, ns, true);
642 exprt::operandst sub_operands =
643 instantiate_byte_array(sub, 0, element_max_bytes_int, bits_per_byte, ns);
644 byte_operands.insert(
645 byte_operands.end(), sub_operands.begin(), sub_operands.end());
646
647 if(max_bytes && bytest{byte_operands.size()} >= *max_bytes)
648 break;
649 }
650
651 const std::size_t size = byte_operands.size();
652 return array_exprt(
653 std::move(byte_operands),
654 array_typet{bv_typet{bits_per_byte}, from_integer(size, size_type())});
655}
656
669 exprt::operandst &&bit_fields,
670 std::size_t total_bits,
671 exprt::operandst &dest,
672 bool little_endian,
673 const std::optional<bytest> &offset_bytes,
674 const std::optional<bytest> &max_bytes,
675 const std::size_t bits_per_byte,
676 const namespacet &ns)
677{
678 const concatenation_exprt concatenation{
679 std::move(bit_fields), bv_typet{total_bits}};
680
681 exprt sub = unpack_rec(
682 concatenation,
683 little_endian,
684 offset_bytes,
685 max_bytes,
686 bits_per_byte,
687 ns,
688 true);
689
690 dest.insert(
691 dest.end(),
692 std::make_move_iterator(sub.operands().begin()),
693 std::make_move_iterator(sub.operands().end()));
694}
695
707 const exprt &src,
708 bool little_endian,
709 const std::optional<bytest> &offset_bytes,
710 const std::optional<bytest> &max_bytes,
711 const std::size_t bits_per_byte,
712 const namespacet &ns)
713{
714 const struct_typet &struct_type =
715 src.type().id() == ID_struct_tag
717 : to_struct_type(src.type());
718 const struct_typet::componentst &components = struct_type.components();
719
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;
723
725 exprt::operandst byte_operands;
726 for(auto it = components.begin(); it != components.end(); ++it)
727 {
728 const auto &comp = *it;
729 auto component_bits = pointer_offset_bits(comp.type(), ns);
730
731 // We can only handle a member of unknown width when it is the last member
732 // and is byte-aligned. Members of unknown width in the middle would leave
733 // us with unknown alignment of subsequent members, and queuing them up as
734 // bit fields is not possible either as the total width of the concatenation
735 // could not be determined.
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");
740
741 member_exprt member(src, comp.get_name(), comp.type());
742 if(src.id() == ID_struct)
743 simplify(member, ns);
744
745 // Is it a byte-aligned member?
746 if(member_offset_bits % bitst{bits_per_byte} == bitst{0})
747 {
748 if(bit_fields.has_value())
749 {
751 std::move(bit_fields->first),
752 bit_fields->second,
753 byte_operands,
754 little_endian,
755 offset_in_member,
756 max_bytes_left,
757 bits_per_byte,
758 ns);
759 bit_fields.reset();
760 }
761
762 if(offset_bytes.has_value())
763 {
764 offset_in_member =
765 *offset_bytes - bytest{member_offset_bits / bitst{bits_per_byte}};
766 // if the offset is negative, offset_in_member remains unset, which has
767 // the same effect as setting it to zero
768 if(*offset_in_member < bytest{0})
769 offset_in_member.reset();
770 }
771
772 if(max_bytes.has_value())
773 {
774 max_bytes_left =
775 *max_bytes - bytest{member_offset_bits / bitst{bits_per_byte}};
776 if(*max_bytes_left < bytest{0})
777 break;
778 }
779 }
780
781 if(
782 member_offset_bits % bitst{bits_per_byte} != bitst{0} ||
783 (component_bits.has_value() &&
784 *component_bits % bitst{bits_per_byte} != bitst{0}))
785 {
786 if(!bit_fields.has_value())
787 bit_fields = std::make_pair(exprt::operandst{}, std::size_t{0});
788
789 const std::size_t bits_int = numeric_cast_v<std::size_t>(*component_bits);
790 bit_fields->first.insert(
791 little_endian ? bit_fields->first.begin() : bit_fields->first.end(),
792 typecast_exprt::conditional_cast(member, bv_typet{bits_int}));
793 bit_fields->second += bits_int;
794
795 member_offset_bits += *component_bits;
796
797 continue;
798 }
799
800 INVARIANT(
801 !bit_fields.has_value(),
802 "all preceding members should have been processed");
803
804 if(
805 component_bits.has_value() && offset_in_member.has_value() &&
806 bytes_to_bits(*offset_in_member, bits_per_byte) >= *component_bits)
807 {
808 // we won't actually need this component, fill in zeros instead of
809 // computing an unpacking
810 byte_operands.resize(
812 *component_bits, bits_per_byte)),
813 bv_typet{bits_per_byte}.all_zeros_expr());
814 }
815 else
816 {
817 exprt sub = unpack_rec(
818 member,
819 little_endian,
820 offset_in_member,
821 max_bytes_left,
822 bits_per_byte,
823 ns,
824 true);
825
826 byte_operands.insert(
827 byte_operands.end(),
828 std::make_move_iterator(sub.operands().begin()),
829 std::make_move_iterator(sub.operands().end()));
830 }
831
832 if(component_bits.has_value())
833 member_offset_bits += *component_bits;
834 }
835
836 // any remaining bit fields?
837 if(bit_fields.has_value())
838 {
840 std::move(bit_fields->first),
841 bit_fields->second,
842 byte_operands,
843 little_endian,
844 offset_in_member,
845 max_bytes_left,
846 bits_per_byte,
847 ns);
848 }
849
850 const std::size_t size = byte_operands.size();
851 return array_exprt{
852 std::move(byte_operands),
853 array_typet{bv_typet{bits_per_byte}, from_integer(size, size_type())}};
854}
855
863 const exprt &src,
864 bool little_endian,
865 const std::size_t bits_per_byte,
866 const namespacet &ns)
867{
868 const complex_typet &complex_type = to_complex_type(src.type());
869 const typet &subtype = complex_type.subtype();
870
871 auto subtype_bits = pointer_offset_bits(subtype, ns);
872 CHECK_RETURN(subtype_bits.has_value());
873 CHECK_RETURN(*subtype_bits % bitst{bits_per_byte} == bitst{0});
874
875 exprt sub_real = unpack_rec(
877 little_endian,
878 bytest{0},
879 bytest{*subtype_bits / bitst{bits_per_byte}},
880 bits_per_byte,
881 ns,
882 true);
883 exprt::operandst byte_operands = std::move(sub_real.operands());
884
885 exprt sub_imag = unpack_rec(
887 little_endian,
888 bytest{0},
889 bytest{*subtype_bits / bitst{bits_per_byte}},
890 bits_per_byte,
891 ns,
892 true);
893 byte_operands.insert(
894 byte_operands.end(),
895 std::make_move_iterator(sub_imag.operands().begin()),
896 std::make_move_iterator(sub_imag.operands().end()));
897
898 const std::size_t size = byte_operands.size();
899 return array_exprt{
900 std::move(byte_operands),
901 array_typet{bv_typet{bits_per_byte}, from_integer(size, size_type())}};
902}
903
914// array of bytes
917 const exprt &src,
918 bool little_endian,
919 const std::optional<bytest> &offset_bytes,
920 const std::optional<bytest> &max_bytes,
921 const std::size_t bits_per_byte,
922 const namespacet &ns,
923 bool unpack_byte_array)
924{
925 if(src.type().id() == ID_array)
926 {
927 const array_typet &array_type = to_array_type(src.type());
928 const typet &subtype = array_type.element_type();
929
930 auto element_bits = pointer_offset_bits(subtype, ns);
931 CHECK_RETURN(element_bits.has_value());
932
933 if(
934 !unpack_byte_array && *element_bits == bitst{bits_per_byte} &&
936 {
937 return src;
938 }
939
940 const auto constant_size_opt = numeric_cast<mp_integer>(array_type.size());
941 return unpack_array_vector(
942 src,
943 constant_size_opt,
944 *element_bits,
945 little_endian,
946 offset_bytes,
947 max_bytes,
948 bits_per_byte,
949 ns);
950 }
951 else if(src.type().id() == ID_vector)
952 {
953 const vector_typet &vector_type = to_vector_type(src.type());
954 const typet &subtype = vector_type.element_type();
955
956 auto element_bits = pointer_offset_bits(subtype, ns);
957 CHECK_RETURN(element_bits.has_value());
958
959 if(
960 !unpack_byte_array && *element_bits == bitst{bits_per_byte} &&
962 {
963 return src;
964 }
965
966 return unpack_array_vector(
967 src,
968 numeric_cast_v<mp_integer>(vector_type.size()),
969 *element_bits,
970 little_endian,
971 offset_bytes,
972 max_bytes,
973 bits_per_byte,
974 ns);
975 }
976 else if(src.type().id() == ID_complex)
977 {
978 return unpack_complex(src, little_endian, bits_per_byte, ns);
979 }
980 else if(src.type().id() == ID_struct || src.type().id() == ID_struct_tag)
981 {
982 return unpack_struct(
983 src, little_endian, offset_bytes, max_bytes, bits_per_byte, ns);
984 }
985 else if(src.type().id() == ID_union || src.type().id() == ID_union_tag)
986 {
987 const union_typet &union_type =
988 src.type().id() == ID_union_tag
990 : to_union_type(src.type());
991
992 const auto widest_member = union_type.find_widest_union_component(ns);
993
994 if(widest_member.has_value())
995 {
996 member_exprt member{
997 src, widest_member->first.get_name(), widest_member->first.type()};
998 return unpack_rec(
999 member,
1000 little_endian,
1001 offset_bytes,
1002 bits_to_bytes_ceil(widest_member->second, bits_per_byte),
1003 bits_per_byte,
1004 ns,
1005 true);
1006 }
1007 else if(!union_type.components().empty())
1008 {
1009 member_exprt member{src, union_type.components().front()};
1010 return unpack_rec(
1011 member,
1012 little_endian,
1013 offset_bytes,
1014 max_bytes,
1015 bits_per_byte,
1016 ns,
1017 true);
1018 }
1019 }
1020 else if(src.type().id() == ID_pointer)
1021 {
1022 return unpack_rec(
1024 little_endian,
1025 offset_bytes,
1026 max_bytes,
1027 bits_per_byte,
1028 ns,
1029 unpack_byte_array);
1030 }
1031 else if(src.id() == ID_string_constant)
1032 {
1033 return unpack_rec(
1035 little_endian,
1036 offset_bytes,
1037 max_bytes,
1038 bits_per_byte,
1039 ns,
1040 unpack_byte_array);
1041 }
1042 else if(src.is_constant() && src.type().id() == ID_string)
1043 {
1044 return unpack_rec(
1046 little_endian,
1047 offset_bytes,
1048 max_bytes,
1049 bits_per_byte,
1050 ns,
1051 unpack_byte_array);
1052 }
1053 else if(src.type().id() != ID_empty)
1054 {
1055 // a basic type; we turn that into extractbits while considering
1056 // endianness
1057 auto bits_opt = pointer_offset_bits(src.type(), ns);
1058 DATA_INVARIANT(bits_opt.has_value(), "basic type should have a fixed size");
1059
1060 const bitst total_bits = *bits_opt;
1061 bitst last_bit = total_bits;
1062 bitst bit_offset{0};
1063
1064 if(max_bytes.has_value())
1065 {
1066 const auto max_bits = bytes_to_bits(*max_bytes, bits_per_byte);
1067 if(little_endian)
1068 {
1069 last_bit = std::min(last_bit, max_bits);
1070 }
1071 else
1072 {
1073 bit_offset = std::max(bitst{0}, last_bit - max_bits);
1074 }
1075 }
1076
1077 // When the source width is not a multiple of the byte size, round up
1078 // to the next byte boundary and zero-extend the source so that all
1079 // byte-sized extractbits operations remain within bounds. The padding
1080 // bits (the high bits of the final partial byte) are unspecified and are
1081 // filled with zero.
1082 const bitst padded_bits = bytes_to_bits(
1083 bits_to_bytes_ceil(total_bits, bits_per_byte), bits_per_byte);
1084 exprt src_as_bitvector = typecast_exprt::conditional_cast(
1085 src, bv_typet{numeric_cast_v<std::size_t>(total_bits)});
1086 if(padded_bits > total_bits)
1087 {
1088 src_as_bitvector = zero_extend_exprt{
1089 src_as_bitvector, bv_typet{numeric_cast_v<std::size_t>(padded_bits)}};
1090 }
1091 auto const byte_type = bv_typet{bits_per_byte};
1092 exprt::operandst byte_operands;
1093 array_typet array_type{
1094 bv_typet{bits_per_byte}, from_integer(0, size_type())};
1095
1096 for(; bit_offset < last_bit; bit_offset += bitst{bits_per_byte})
1097 {
1099 pointer_offset_bits(src_as_bitvector.type(), ns).has_value());
1100 extractbits_exprt extractbits(
1101 src_as_bitvector,
1102 from_integer(bit_offset, array_type.index_type()),
1103 byte_type);
1104
1105 // endianness_mapt should be the point of reference for mapping out
1106 // endianness, but we need to work on elements here instead of
1107 // individual bits
1108 if(little_endian)
1109 byte_operands.push_back(extractbits);
1110 else
1111 byte_operands.insert(byte_operands.begin(), extractbits);
1112 }
1113
1114 const std::size_t size = byte_operands.size();
1115 array_type.size() = from_integer(size, size_type());
1116 return array_exprt{std::move(byte_operands), std::move(array_type)};
1117 }
1118
1119 return array_exprt(
1120 {}, array_typet{bv_typet{bits_per_byte}, from_integer(0, size_type())});
1121}
1122
1134 const byte_extract_exprt &src,
1135 const byte_extract_exprt &unpacked,
1136 const typet &subtype,
1137 const bitst &element_bits,
1138 const namespacet &ns)
1139{
1140 std::optional<std::size_t> num_elements;
1141 if(src.type().id() == ID_array)
1142 num_elements = numeric_cast<std::size_t>(to_array_type(src.type()).size());
1143 else
1144 num_elements = numeric_cast<std::size_t>(to_vector_type(src.type()).size());
1145
1146 if(num_elements.has_value())
1147 {
1148 exprt::operandst operands;
1149 operands.reserve(*num_elements);
1150 for(std::size_t i = 0; i < *num_elements; ++i)
1151 {
1152 plus_exprt new_offset(
1153 unpacked.offset(),
1155 i * element_bits / bitst{src.get_bits_per_byte()},
1156 unpacked.offset().type()));
1157
1158 byte_extract_exprt tmp(unpacked);
1159 tmp.type() = subtype;
1160 tmp.offset() = new_offset;
1161
1162 operands.push_back(lower_byte_extract(tmp, ns));
1163 }
1164
1165 exprt result;
1166 if(src.type().id() == ID_array)
1167 result = array_exprt{std::move(operands), to_array_type(src.type())};
1168 else
1169 result = vector_exprt{std::move(operands), to_vector_type(src.type())};
1170
1171 return simplify_expr(result, ns);
1172 }
1173
1174 DATA_INVARIANT(src.type().id() == ID_array, "vectors have constant size");
1175 const array_typet &array_type = to_array_type(src.type());
1176
1177 // TODO we either need a symbol table here or make array comprehensions
1178 // introduce a scope
1179 static std::size_t array_comprehension_index_counter = 0;
1180 ++array_comprehension_index_counter;
1181 symbol_exprt array_comprehension_index{
1182 "$array_comprehension_index_a" +
1183 std::to_string(array_comprehension_index_counter),
1184 array_type.index_type()};
1185
1186 plus_exprt new_offset{
1187 unpacked.offset(),
1189 mult_exprt{
1190 array_comprehension_index,
1192 element_bits / bitst{src.get_bits_per_byte()},
1193 array_comprehension_index.type())},
1194 unpacked.offset().type())};
1195
1196 byte_extract_exprt body(unpacked);
1197 body.type() = subtype;
1198 body.offset() = std::move(new_offset);
1199
1201 std::move(array_comprehension_index),
1202 lower_byte_extract(body, ns),
1203 array_type};
1204}
1205
1214static std::optional<exprt> lower_byte_extract_complex(
1215 const byte_extract_exprt &src,
1216 const byte_extract_exprt &unpacked,
1217 const namespacet &ns)
1218{
1219 const complex_typet &complex_type = to_complex_type(src.type());
1220 const typet &subtype = complex_type.subtype();
1221
1222 auto subtype_bits = pointer_offset_bits(subtype, ns);
1223 if(
1224 !subtype_bits.has_value() ||
1225 *subtype_bits % bitst{src.get_bits_per_byte()} != bitst{0})
1226 return {};
1227
1228 // offset remains unchanged
1229 byte_extract_exprt real{unpacked};
1230 real.type() = subtype;
1231
1232 const plus_exprt new_offset{
1233 unpacked.offset(),
1235 *subtype_bits / bitst{src.get_bits_per_byte()},
1236 unpacked.offset().type())};
1237 byte_extract_exprt imag{unpacked};
1238 imag.type() = subtype;
1239 imag.offset() = simplify_expr(new_offset, ns);
1240
1241 return simplify_expr(
1243 lower_byte_extract(real, ns), lower_byte_extract(imag, ns), complex_type},
1244 ns);
1245}
1246
1250{
1251 // General notes about endianness and the bit-vector conversion:
1252 // A single byte with value 0b10001000 is stored (in irept) as
1253 // exactly this string literal, and its bit-vector encoding will be
1254 // bvt bv={0,0,0,1,0,0,0,1}, i.e., bv[0]==0 and bv[7]==1
1255 //
1256 // A multi-byte value like x=256 would be:
1257 // - little-endian storage: ((char*)&x)[0]==0, ((char*)&x)[1]==1
1258 // - big-endian storage: ((char*)&x)[0]==1, ((char*)&x)[1]==0
1259 // - irept representation: 0000000100000000
1260 // - bvt: {0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0}
1261 // <... 8bits ...> <... 8bits ...>
1262 //
1263 // An array {0, 1} will be encoded as bvt bv={0,1}, i.e., bv[1]==1
1264 // concatenation(0, 1) will yield a bvt bv={1,0}, i.e., bv[1]==0
1265 //
1266 // The semantics of byte_extract(endianness, op, offset, T) is:
1267 // interpret ((char*)&op)+offset as the endianness-ordered storage
1268 // of an object of type T at address ((char*)&op)+offset
1269 // For some T x, byte_extract(endianness, x, 0, T) must yield x.
1270 //
1271 // byte_extract for a composite type T or an array will interpret
1272 // the individual subtypes with suitable endianness; the overall
1273 // order of components is not affected by endianness.
1274 //
1275 // Examples:
1276 // unsigned char A[4];
1277 // byte_extract_little_endian(A, 0, unsigned short) requests that
1278 // A[0],A[1] be interpreted as the storage of an unsigned short with
1279 // A[1] being the most-significant byte; byte_extract_big_endian for
1280 // the same operands will select A[0] as the most-significant byte.
1281 //
1282 // int A[2] = {0x01020304,0xDEADBEEF}
1283 // byte_extract_big_endian(A, 0, short) should yield 0x0102
1284 // byte_extract_little_endian(A, 0, short) should yield 0x0304
1285 // To obtain this we first compute byte arrays while taking into
1286 // account endianness:
1287 // big-endian byte representation: {01,02,03,04,DE,AB,BE,EF}
1288 // little-endian byte representation: {04,03,02,01,EF,BE,AB,DE}
1289 // We extract the relevant bytes starting from ((char*)A)+0:
1290 // big-endian: {01,02}; little-endian: {04,03}
1291 // Finally we place them in the appropriate byte order as indicated
1292 // by endianness:
1293 // big-endian: (short)concatenation(01,02)=0x0102
1294 // little-endian: (short)concatenation(03,04)=0x0304
1295
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;
1300
1301 // determine an upper bound of the last byte we might need
1302 auto upper_bound_opt = size_of_expr(src.type(), ns);
1303 if(upper_bound_opt.has_value())
1304 {
1305 upper_bound_opt = simplify_expr(
1306 plus_exprt(
1307 upper_bound_opt.value(),
1309 src.offset(), upper_bound_opt.value().type())),
1310 ns);
1311 }
1312 else if(src.type().id() == ID_empty)
1313 upper_bound_opt = from_integer(0, size_type());
1314
1315 const auto lower_bound_int_opt = numeric_cast<bytest>(src.offset());
1316 const auto upper_bound_int_opt =
1317 numeric_cast<bytest>(upper_bound_opt.value_or(nil_exprt()));
1318
1319 byte_extract_exprt unpacked(src);
1320 unpacked.op() = unpack_rec(
1321 src.op(),
1322 little_endian,
1323 lower_bound_int_opt,
1324 upper_bound_int_opt,
1325 src.get_bits_per_byte(),
1326 ns);
1329 .get_width() == src.get_bits_per_byte());
1330
1331 if(src.type().id() == ID_array || src.type().id() == ID_vector)
1332 {
1333 const typet &subtype = to_type_with_subtype(src.type()).subtype();
1334
1335 // consider ways of dealing with arrays of unknown subtype size or with a
1336 // subtype size that does not fit byte boundaries; currently we fall back to
1337 // stitching together consecutive elements down below
1338 auto element_bits = pointer_offset_bits(subtype, ns);
1339 if(
1340 element_bits.has_value() && *element_bits >= bitst{1} &&
1341 *element_bits % bitst{src.get_bits_per_byte()} == bitst{0})
1342 {
1344 src, unpacked, subtype, *element_bits, ns);
1345 }
1346 }
1347 else if(src.type().id() == ID_complex)
1348 {
1349 auto result = lower_byte_extract_complex(src, unpacked, ns);
1350 if(result.has_value())
1351 return std::move(*result);
1352
1353 // else fall back to generic lowering that uses bit masks, below
1354 }
1355 else if(src.type().id() == ID_struct || src.type().id() == ID_struct_tag)
1356 {
1357 const struct_typet &struct_type =
1358 src.type().id() == ID_struct_tag
1360 : to_struct_type(src.type());
1361 const struct_typet::componentst &components = struct_type.components();
1362
1363 bool failed = false;
1364 struct_exprt s({}, src.type());
1365
1366 for(const auto &comp : components)
1367 {
1368 auto component_bits = pointer_offset_bits(comp.type(), ns);
1369
1370 // the next member would be misaligned, abort
1371 if(
1372 !component_bits.has_value() ||
1373 *component_bits % bitst{src.get_bits_per_byte()} != bitst{0})
1374 {
1375 failed = true;
1376 break;
1377 }
1378
1379 auto member_offset_opt =
1380 member_offset_expr(struct_type, comp.get_name(), ns);
1381
1382 if(!member_offset_opt.has_value())
1383 {
1384 failed = true;
1385 break;
1386 }
1387
1388 plus_exprt new_offset(
1389 unpacked.offset(),
1391 member_offset_opt.value(), unpacked.offset().type()));
1392
1393 byte_extract_exprt tmp(unpacked);
1394 tmp.type() = comp.type();
1395 tmp.offset() = new_offset;
1396
1398 }
1399
1400 if(!failed)
1401 return simplify_expr(std::move(s), ns);
1402 }
1403 else if(src.type().id() == ID_union || src.type().id() == ID_union_tag)
1404 {
1405 const union_typet &union_type =
1406 src.type().id() == ID_union_tag
1407 ? ns.follow_tag(to_union_tag_type(src.type()))
1408 : to_union_type(src.type());
1409
1410 const auto widest_member = union_type.find_widest_union_component(ns);
1411
1412 if(widest_member.has_value())
1413 {
1414 byte_extract_exprt tmp(unpacked);
1415 tmp.type() = widest_member->first.type();
1416
1417 return union_exprt(
1418 widest_member->first.get_name(),
1419 lower_byte_extract(tmp, ns),
1420 src.type());
1421 }
1422 }
1423
1424 const exprt &root = unpacked.op();
1425 const exprt &offset = unpacked.offset();
1426
1427 std::optional<typet> subtype;
1428 std::optional<typet> index_type;
1429 if(root.type().id() == ID_vector)
1430 {
1431 subtype = to_vector_type(root.type()).element_type();
1432 index_type = to_vector_type(root.type()).index_type();
1433 }
1434 else
1435 {
1436 subtype = to_array_type(root.type()).element_type();
1437 index_type = to_array_type(root.type()).index_type();
1438 }
1439
1440 auto subtype_bits = pointer_offset_bits(*subtype, ns);
1441
1443 subtype_bits.has_value() && *subtype_bits == bitst{src.get_bits_per_byte()},
1444 "offset bits are byte aligned");
1445
1446 auto size_bits = pointer_offset_bits(unpacked.type(), ns);
1447 if(!size_bits.has_value())
1448 {
1449 auto op0_bits = pointer_offset_bits(unpacked.op().type(), ns);
1450 // all cases with non-constant width should have been handled above
1452 op0_bits.has_value(),
1453 "the extracted width or the source width must be known");
1454 size_bits = op0_bits;
1455 }
1456
1457 bytest num_bytes =
1458 bytest{(*size_bits) / bitst{src.get_bits_per_byte()}} +
1459 bytest{
1460 (((*size_bits) % bitst{src.get_bits_per_byte()} == bitst{0}) ? 0 : 1)};
1461
1462 // get 'width'-many bytes, and concatenate
1463 const std::size_t width_bytes = numeric_cast_v<std::size_t>(num_bytes);
1465 op.reserve(width_bytes);
1466
1467 for(std::size_t i = 0; i < width_bytes; i++)
1468 {
1469 // the most significant byte comes first in the concatenation!
1470 std::size_t offset_int = little_endian ? (width_bytes - i - 1) : i;
1471
1472 plus_exprt offset_i{
1473 from_integer(offset_int, *index_type),
1474 typecast_exprt::conditional_cast(offset, *index_type)};
1475 simplify(offset_i, ns);
1476
1477 mp_integer index = 0;
1478 if(
1479 offset_i.is_constant() &&
1480 (root.id() == ID_array || root.id() == ID_vector) &&
1481 !to_integer(to_constant_expr(offset_i), index) &&
1482 index < root.operands().size() && index >= 0)
1483 {
1484 // offset is constant and in range
1485 op.push_back(root.operands()[numeric_cast_v<std::size_t>(index)]);
1486 }
1487 else
1488 {
1489 op.push_back(index_exprt(root, offset_i));
1490 }
1491 }
1492
1493 if(width_bytes == 1)
1494 {
1495 const typet &result_type = src.type();
1496 if(
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)
1501 {
1502 // A composite result type cannot be produced by a plain typecast (the
1503 // SMT2 back-end has no bit-vector -> struct/union/array typecast); build
1504 // the aggregate from the single byte instead.
1505 endianness_mapt map{op.front().type(), little_endian, ns};
1506 return bv_to_expr(op.front(), result_type, map, ns);
1507 }
1508 return simplify_expr(
1509 typecast_exprt::conditional_cast(op.front(), result_type), ns);
1510 }
1511 else // width_bytes>=2
1512 {
1513 concatenation_exprt concatenation(
1514 std::move(op),
1515 adjust_width(*subtype, width_bytes * src.get_bits_per_byte()));
1516
1517 endianness_mapt map(concatenation.type(), little_endian, ns);
1518 return bv_to_expr(concatenation, src.type(), map, ns);
1519 }
1520}
1521
1523 const byte_update_exprt &src,
1524 const exprt &value_as_byte_array,
1525 const std::optional<exprt> &non_const_update_bound,
1526 const namespacet &ns);
1527
1538 const byte_update_exprt &src,
1539 const typet &subtype,
1540 const exprt &value_as_byte_array,
1541 const exprt &non_const_update_bound,
1542 const namespacet &ns)
1543{
1544 // TODO we either need a symbol table here or make array comprehensions
1545 // introduce a scope
1546 static std::size_t array_comprehension_index_counter = 0;
1547 ++array_comprehension_index_counter;
1548 symbol_exprt array_comprehension_index{
1549 "$array_comprehension_index_u_a_v" +
1550 std::to_string(array_comprehension_index_counter),
1551 to_array_type(src.type()).index_type()};
1552
1553 binary_predicate_exprt lower_bound{
1555 array_comprehension_index, src.offset().type()),
1556 ID_lt,
1557 src.offset()};
1558 binary_predicate_exprt upper_bound{
1560 array_comprehension_index, non_const_update_bound.type()),
1561 ID_ge,
1562 plus_exprt{
1564 src.offset(), non_const_update_bound.type()),
1565 non_const_update_bound}};
1566
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;
1571 endianness_mapt map(
1572 to_array_type(value_as_byte_array.type()).element_type(),
1573 little_endian,
1574 ns);
1575 if_exprt array_comprehension_body{
1576 or_exprt{std::move(lower_bound), std::move(upper_bound)},
1577 index_exprt{src.op(), array_comprehension_index},
1578 bv_to_expr(
1580 value_as_byte_array,
1582 array_comprehension_index,
1584 src.offset(), array_comprehension_index.type())}},
1585 subtype,
1586 map,
1587 ns)};
1588
1589 return simplify_expr(
1591 array_comprehension_index,
1592 std::move(array_comprehension_body),
1593 to_array_type(src.type())},
1594 ns);
1595}
1596
1607 const byte_update_exprt &src,
1608 const typet &subtype,
1609 const array_exprt &value_as_byte_array,
1610 const std::optional<exprt> &non_const_update_bound,
1611 const namespacet &ns)
1612{
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;
1617
1618 const typet index_type = src.type().id() == ID_array
1619 ? to_array_type(src.type()).index_type()
1620 : to_vector_type(src.type()).index_type();
1621
1622 // apply 'array-update-with' num_elements times
1623 exprt result = src.op();
1624
1625 for(std::size_t i = 0; i < value_as_byte_array.operands().size(); ++i)
1626 {
1627 const exprt &element = value_as_byte_array.operands()[i];
1628
1629 exprt where = simplify_expr(
1630 plus_exprt{
1631 typecast_exprt::conditional_cast(src.offset(), index_type),
1632 from_integer(i, index_type)},
1633 ns);
1634
1635 // skip elements that wouldn't change (skip over typecasts as we might have
1636 // some signed/unsigned char conversions)
1637 const exprt &e = skip_typecast(element);
1638 if(e.id() == ID_index)
1639 {
1640 const index_exprt &index_expr = to_index_expr(e);
1641 if(index_expr.array() == src.op() && index_expr.index() == where)
1642 continue;
1643 }
1644
1645 endianness_mapt map(element.type(), little_endian, ns);
1646 exprt update_value = bv_to_expr(element, subtype, map, ns);
1647 if(non_const_update_bound.has_value())
1648 {
1649 update_value = if_exprt{
1651 from_integer(i, non_const_update_bound->type()),
1652 ID_lt,
1653 *non_const_update_bound},
1654 update_value,
1655 index_exprt{src.op(), where}};
1656 }
1657
1658 result = with_exprt{result, std::move(where), std::move(update_value)};
1659 }
1660
1661 return simplify_expr(std::move(result), ns);
1662}
1663
1680 const byte_update_exprt &src,
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,
1688 const namespacet &ns)
1689{
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;
1693
1694 // TODO we either need a symbol table here or make array comprehensions
1695 // introduce a scope
1696 static std::size_t array_comprehension_index_counter = 0;
1697 ++array_comprehension_index_counter;
1698 symbol_exprt array_comprehension_index{
1699 "$array_comprehension_index_u_a_v_u" +
1700 std::to_string(array_comprehension_index_counter),
1701 to_array_type(src.type()).index_type()};
1702
1703 // all arithmetic uses offset/index types
1704 PRECONDITION(array_comprehension_index.type() == src.offset().type());
1705 PRECONDITION(subtype_size.type() == src.offset().type());
1706 PRECONDITION(initial_bytes.type() == src.offset().type());
1707 PRECONDITION(first_index.type() == src.offset().type());
1708
1709 // the bound from where updates start
1710 binary_predicate_exprt lower_bound{
1712 array_comprehension_index, first_index.type()),
1713 ID_lt,
1714 first_index};
1715
1716 // The actual value of updates other than at the start or end
1717 plus_exprt offset_expr{
1718 initial_bytes,
1719 mult_exprt{
1720 subtype_size,
1723 array_comprehension_index, first_index.type()),
1724 plus_exprt{first_index, from_integer(1, first_index.type())}}}};
1725 exprt update_value = lower_byte_extract(
1727 extract_opcode,
1728 value_as_byte_array,
1729 std::move(offset_expr),
1730 src.get_bits_per_byte(),
1731 subtype},
1732 ns);
1733
1734 // The number of target array/vector elements being replaced, not including
1735 // a possible partial update at the end of the updated range, which is handled
1736 // below: (non_const_update_bound + (subtype_size - 1)) / subtype_size to
1737 // round up to the nearest multiple of subtype_size.
1738 div_exprt updated_elements{
1739 plus_exprt{
1741 non_const_update_bound, subtype_size.type()),
1742 minus_exprt{subtype_size, from_integer(1, subtype_size.type())}},
1743 subtype_size};
1744
1745 // The last element to be updated: first_index + updated_elements - 1
1746 plus_exprt last_index{
1747 first_index,
1749 std::move(updated_elements), from_integer(1, first_index.type())}};
1750
1751 // The size of a partial update at the end of the updated range, may be zero.
1752 mod_exprt tail_size{
1756 non_const_update_bound, initial_bytes.type()),
1757 initial_bytes},
1758 subtype_size.type()),
1759 subtype_size};
1760
1761 // The bound where updates end, which is conditional on the partial update
1762 // being empty.
1763 binary_predicate_exprt upper_bound{
1765 array_comprehension_index, last_index.type()),
1766 ID_ge,
1767 if_exprt{
1768 equal_exprt{tail_size, from_integer(0, tail_size.type())},
1769 last_index,
1770 plus_exprt{last_index, from_integer(1, last_index.type())}}};
1771
1772 // The actual value of a partial update at the end.
1773 exprt last_update_value = lower_byte_update(
1775 src.id(),
1776 index_exprt{src.op(), last_index},
1778 typecast_exprt::conditional_cast(last_index, subtype_size.type()),
1779 subtype_size}},
1780 value_as_byte_array,
1781 src.get_bits_per_byte()},
1782 ns);
1783
1784 if_exprt array_comprehension_body{
1785 or_exprt{std::move(lower_bound), std::move(upper_bound)},
1786 index_exprt{src.op(), array_comprehension_index},
1787 if_exprt{
1790 array_comprehension_index, first_index.type()),
1791 first_index},
1792 first_update_value,
1793 if_exprt{
1796 array_comprehension_index, last_index.type()),
1797 last_index},
1798 std::move(last_update_value),
1799 std::move(update_value)}}};
1800
1801 return simplify_expr(
1803 array_comprehension_index,
1804 std::move(array_comprehension_body),
1805 to_array_type(src.type())},
1806 ns);
1807}
1808
1820 const byte_update_exprt &src,
1821 const typet &subtype,
1822 const exprt &value_as_byte_array,
1823 const std::optional<exprt> &non_const_update_bound,
1824 const namespacet &ns)
1825{
1826 // do all arithmetic below using index/offset types - the array theory
1827 // back-end is really picky about indices having the same type
1828 auto subtype_size_opt = size_of_expr(subtype, ns);
1829 CHECK_RETURN(subtype_size_opt.has_value());
1830
1831 const exprt subtype_size = simplify_expr(
1833 subtype_size_opt.value(), src.offset().type()),
1834 ns);
1835
1836 // compute the index of the first element of the array/vector that may be
1837 // updated
1839 src.offset().type() == to_array_type(src.op().type()).index_type());
1840 exprt first_index = div_exprt{src.offset(), subtype_size};
1841 simplify(first_index, ns);
1842
1843 // compute the offset within that first element
1844 const exprt update_offset = mod_exprt{src.offset(), subtype_size};
1845
1846 // compute the number of bytes (from the update value) that are going to be
1847 // consumed for updating the first element
1848 const exprt update_size =
1849 from_integer(value_as_byte_array.operands().size(), subtype_size.type());
1850 exprt initial_bytes = minus_exprt{subtype_size, update_offset};
1851 exprt update_bound;
1852 if(non_const_update_bound.has_value())
1853 {
1854 update_bound = typecast_exprt::conditional_cast(
1855 *non_const_update_bound, subtype_size.type());
1856 }
1857 else
1858 {
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;
1863 }
1864 initial_bytes = if_exprt{
1865 binary_predicate_exprt{initial_bytes, ID_lt, update_bound},
1866 initial_bytes,
1867 update_bound};
1868 simplify(initial_bytes, ns);
1869
1870 // encode the first update: update the original element at first_index (the
1871 // actual update will only be initial_bytes-many bytes from
1872 // value_as_byte_array)
1873 exprt first_update_value = lower_byte_update(
1875 src.id(),
1876 index_exprt{src.op(), first_index},
1877 update_offset,
1878 value_as_byte_array,
1879 src.get_bits_per_byte()},
1880 ns);
1881
1882 if(value_as_byte_array.id() != ID_array)
1883 {
1885 src,
1886 subtype,
1887 subtype_size,
1888 value_as_byte_array,
1889 *non_const_update_bound,
1890 initial_bytes,
1891 first_index,
1892 first_update_value,
1893 ns);
1894 }
1895
1896 // We will update one array/vector element at a time - compute the number of
1897 // update values that will be consumed in each step. If we cannot determine a
1898 // constant value at this time we assume it's at least one byte. The
1899 // byte_extract_exprt within the loop uses the symbolic value (subtype_size),
1900 // we just need to make sure we make progress for the loop to terminate.
1901 std::size_t step_size = 1;
1902 if(subtype_size.is_constant())
1903 step_size = numeric_cast_v<std::size_t>(to_constant_expr(subtype_size));
1904 // Given the first update already encoded, keep track of the offset into the
1905 // update value. Again, the expressions within the loop use the symbolic
1906 // value, this is just an optimization in case we can determine a constant
1907 // offset.
1908 std::size_t offset = 0;
1909 if(initial_bytes.is_constant())
1910 offset = numeric_cast_v<std::size_t>(to_constant_expr(initial_bytes));
1911
1912 with_exprt result{src.op(), first_index, first_update_value};
1913
1914 auto lower_byte_update_array_vector_non_const_one_element =
1915 [&src,
1916 &first_index,
1917 &initial_bytes,
1918 &subtype_size,
1919 &value_as_byte_array,
1920 &ns,
1921 &result](std::size_t i) -> void {
1922 exprt where = simplify_expr(
1923 plus_exprt{first_index, from_integer(i, first_index.type())}, ns);
1924
1925 exprt neg_offset_expr = simplify_expr(
1927 initial_bytes,
1928 mult_exprt{subtype_size, from_integer(i - 1, subtype_size.type())}}},
1929 ns);
1930
1931 exprt element = lower_byte_update(
1933 src.id(),
1934 index_exprt{src.op(), where},
1935 neg_offset_expr,
1936 value_as_byte_array,
1937 src.get_bits_per_byte()},
1938 ns);
1939
1940 result = with_exprt{result, std::move(where), std::move(element)};
1941 };
1942
1943 std::size_t i = 1;
1944 for(; offset + step_size <= value_as_byte_array.operands().size();
1945 offset += step_size, ++i)
1946 {
1947 lower_byte_update_array_vector_non_const_one_element(i);
1948 }
1949
1950 // do the tail
1951 if(offset < value_as_byte_array.operands().size())
1952 lower_byte_update_array_vector_non_const_one_element(i);
1953
1954 return simplify_expr(std::move(result), ns);
1955}
1956
1970 const byte_update_exprt &src,
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,
1977 const namespacet &ns)
1978{
1979 // We need to take one or more bytes from value_as_byte_array to modify the
1980 // target element. We need to compute:
1981 // - The position in value_as_byte_array to take bytes from: If subtype_bits
1982 // is less than the size of a byte, then multiple struct/array/vector elements
1983 // will need to be updated using the same byte.
1984 std::size_t first = 0;
1985 // - An upper bound on the number of bytes required from value_as_byte_array.
1986 bytest update_elements =
1987 bits_to_bytes_ceil(subtype_bits, src.get_bits_per_byte());
1988 // - The offset into the target element: If subtype_bits is greater or equal
1989 // to the size of a byte, then there may be an offset into the target element
1990 // that needs to be taken into account, and multiple bytes will be required.
1991 bitst offset_bits_in_target_element = offset_bits - bits_already_set;
1992
1993 if(offset_bits_in_target_element < bitst{0})
1994 {
1995 INVARIANT(
1996 value_as_byte_array.id() != ID_array ||
1998 bytest{value_as_byte_array.operands().size()},
1999 src.get_bits_per_byte()) > -1 * offset_bits_in_target_element,
2000 "indices past the update should be handled below");
2003 offset_bits_in_target_element, src.get_bits_per_byte()));
2004 offset_bits_in_target_element += bytes_to_bits(
2006 offset_bits_in_target_element, src.get_bits_per_byte()),
2007 src.get_bits_per_byte());
2008 if(offset_bits_in_target_element < bitst{0})
2009 update_elements += bytest{1};
2010 }
2011 else
2012 {
2013 update_elements -= bits_to_bytes_trunc(
2014 offset_bits_in_target_element, src.get_bits_per_byte());
2015 INVARIANT(
2016 update_elements > bytest{0},
2017 "indices before the update should be handled above");
2018 }
2019
2020 std::size_t end = first + numeric_cast_v<std::size_t>(update_elements);
2021 if(value_as_byte_array.id() == ID_array)
2022 end = std::min(end, value_as_byte_array.operands().size());
2024 value_as_byte_array, first, end, src.get_bits_per_byte(), ns);
2025 const std::size_t update_size = update_values.size();
2026 const exprt update_size_expr = from_integer(update_size, size_type());
2027 const array_typet update_type{
2028 bv_typet{src.get_bits_per_byte()}, update_size_expr};
2029
2030 // each case below will set "new_value" as appropriate
2031 exprt new_value;
2032
2033 if(
2034 offset_bits_in_target_element >= bitst{0} &&
2035 offset_bits_in_target_element % bitst{src.get_bits_per_byte()} == bitst{0})
2036 {
2037 new_value = array_exprt{update_values, update_type};
2038 }
2039 else
2040 {
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})
2044 {
2045 if(src.id() == ID_byte_update_little_endian)
2046 {
2047 new_value = lshr_exprt{
2049 update_values, bv_typet{update_size * src.get_bits_per_byte()}},
2050 numeric_cast_v<std::size_t>(-1 * offset_bits_in_target_element)};
2051 }
2052 else
2053 {
2054 new_value = shl_exprt{
2056 update_values, bv_typet{update_size * src.get_bits_per_byte()}},
2057 numeric_cast_v<std::size_t>(-1 * offset_bits_in_target_element)};
2058 }
2059 }
2060 else
2061 {
2062 const std::size_t offset_bits_int =
2063 numeric_cast_v<std::size_t>(offset_bits_in_target_element);
2064 const std::size_t subtype_bits_int =
2065 numeric_cast_v<std::size_t>(subtype_bits);
2066
2067 extractbits_exprt bits_to_keep{
2068 element_to_update,
2069 subtype_bits_int - offset_bits_int,
2070 bv_typet{offset_bits_int}};
2071 new_value = concatenation_exprt{
2072 bits_to_keep,
2075 update_values, bv_typet{update_size * src.get_bits_per_byte()}},
2076 offset_bits_int,
2077 bv_typet{update_size * src.get_bits_per_byte() - offset_bits_int}},
2078 bv_typet{update_size * src.get_bits_per_byte()}};
2079 }
2080
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;
2084
2085 const byte_extract_exprt byte_extract_expr{
2086 extract_opcode,
2087 new_value,
2088 from_integer(0, src.offset().type()),
2089 src.get_bits_per_byte(),
2090 update_type};
2091
2092 new_value = lower_byte_extract(byte_extract_expr, ns);
2093
2094 offset_bits_in_target_element = bitst{0};
2095 }
2096
2097 // With an upper bound on the size of the update established, construct the
2098 // actual update expression. If the exact size of the update is unknown,
2099 // make the size expression conditional.
2100 const byte_update_exprt bu{
2101 src.id(),
2102 element_to_update,
2105 offset_bits_in_target_element, src.get_bits_per_byte()),
2106 src.offset().type()),
2107 new_value,
2108 src.get_bits_per_byte()};
2109
2110 std::optional<exprt> update_bound;
2111 if(non_const_update_bound.has_value())
2112 {
2113 // The size of the update is not constant, and thus is to be symbolically
2114 // bound; first see how many bytes we have left in the update:
2115 // non_const_update_bound > first ? non_const_update_bound - first: 0
2116 const exprt remaining_update_bytes = typecast_exprt::conditional_cast(
2117 if_exprt{
2119 *non_const_update_bound,
2120 ID_gt,
2121 from_integer(first, non_const_update_bound->type())},
2123 *non_const_update_bound,
2124 from_integer(first, non_const_update_bound->type())},
2125 from_integer(0, non_const_update_bound->type())},
2126 size_type());
2127 // Now take the minimum of update-bytes-left and the previously computed
2128 // size of the element to be updated:
2129 update_bound = simplify_expr(
2130 if_exprt{
2131 binary_predicate_exprt{update_size_expr, ID_lt, remaining_update_bytes},
2132 update_size_expr,
2133 remaining_update_bytes},
2134 ns);
2135 }
2136
2137 return lower_byte_update(bu, new_value, update_bound, ns);
2138}
2139
2151 const byte_update_exprt &src,
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,
2156 const namespacet &ns)
2157{
2158 const bool is_array = src.type().id() == ID_array;
2159 exprt size;
2160 typet index_type;
2161 if(is_array)
2162 {
2163 size = to_array_type(src.type()).size();
2164 index_type = to_array_type(src.type()).index_type();
2165 }
2166 else
2167 {
2168 size = to_vector_type(src.type()).size();
2169 index_type = to_vector_type(src.type()).index_type();
2170 }
2171
2172 // fall back to bytewise updates in all non-constant or dubious cases
2173 if(
2174 !size.is_constant() || !src.offset().is_constant() ||
2175 !subtype_bits.has_value() || value_as_byte_array.id() != ID_array)
2176 {
2178 src, subtype, value_as_byte_array, non_const_update_bound, ns);
2179 }
2180
2181 std::size_t num_elements =
2183 bitst offset_bits = bytes_to_bits(
2185 src.get_bits_per_byte());
2186
2187 exprt::operandst elements;
2188 elements.reserve(num_elements);
2189
2190 std::size_t i = 0;
2191 // copy the prefix not affected by the update
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)});
2194
2195 // the modified elements
2196 for(; i < num_elements &&
2197 i * *subtype_bits <
2198 offset_bits + bytes_to_bits(
2199 bytest{value_as_byte_array.operands().size()},
2200 src.get_bits_per_byte());
2201 ++i)
2202 {
2203 elements.push_back(lower_byte_update_single_element(
2204 src,
2205 offset_bits,
2206 index_exprt{src.op(), from_integer(i, index_type)},
2207 *subtype_bits,
2208 i * *subtype_bits,
2209 value_as_byte_array,
2210 non_const_update_bound,
2211 ns));
2212 }
2213
2214 // copy the tail not affected by the update
2215 for(; i < num_elements; ++i)
2216 elements.push_back(index_exprt{src.op(), from_integer(i, index_type)});
2217
2218 if(is_array)
2219 return simplify_expr(
2220 array_exprt{std::move(elements), to_array_type(src.type())}, ns);
2221 else
2222 return simplify_expr(
2223 vector_exprt{std::move(elements), to_vector_type(src.type())}, ns);
2224}
2225
2236 const byte_update_exprt &src,
2237 const struct_typet &struct_type,
2238 const exprt &value_as_byte_array,
2239 const std::optional<exprt> &non_const_update_bound,
2240 const namespacet &ns)
2241{
2242 exprt::operandst elements;
2243 elements.reserve(struct_type.components().size());
2244
2246 for(const auto &comp : struct_type.components())
2247 {
2248 auto element_width = pointer_offset_bits(comp.type(), ns);
2249
2250 // compute the update offset relative to this struct member - will be
2251 // negative if we are already in the middle of the update or beyond it
2252 exprt offset = simplify_expr(
2254 mult_exprt{
2255 src.offset(),
2256 from_integer(src.get_bits_per_byte(), src.offset().type())},
2258 ns);
2259 auto offset_bits = numeric_cast<bitst>(offset);
2260 if(!offset_bits.has_value() || !element_width.has_value())
2261 {
2262 // The offset to update is not constant, either because the original
2263 // offset in src never was, or because a struct member has an unknown
2264 // offset. Abort the attempt to update individual struct members and
2265 // instead turn the operand-to-be-updated into a byte array, which we know
2266 // how to update even if the offset is non-constant.
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;
2270
2271 auto src_size_opt = size_of_expr(src.type(), ns);
2272 CHECK_RETURN(src_size_opt.has_value());
2273
2274 const byte_extract_exprt byte_extract_expr{
2275 extract_opcode,
2276 src.op(),
2277 from_integer(0, src.offset().type()),
2278 src.get_bits_per_byte(),
2279 array_typet{bv_typet{src.get_bits_per_byte()}, src_size_opt.value()}};
2280
2281 byte_update_exprt bu = src;
2282 bu.set_op(lower_byte_extract(byte_extract_expr, ns));
2283 bu.type() = bu.op().type();
2284
2285 return lower_byte_extract(
2287 extract_opcode,
2289 bu, value_as_byte_array, non_const_update_bound, ns),
2290 from_integer(0, src.offset().type()),
2291 src.get_bits_per_byte(),
2292 src.type()},
2293 ns);
2294 }
2295
2296 exprt member = member_exprt{src.op(), comp.get_name(), comp.type()};
2297
2298 // we don't need to update anything when
2299 // 1) the update offset is greater than the end of this member (thus the
2300 // relative offset is greater than this element) or
2301 // 2) the update offset plus the size of the update is less than the member
2302 // offset
2303 if(
2304 *offset_bits >= *element_width ||
2305 (value_as_byte_array.id() == ID_array && *offset_bits < bitst{0} &&
2306 -1 * *offset_bits >= bytes_to_bits(
2307 bytest{value_as_byte_array.operands().size()},
2308 src.get_bits_per_byte())))
2309 {
2310 elements.push_back(member);
2311 member_offset_bits += *element_width;
2312 continue;
2313 }
2314
2315 elements.push_back(lower_byte_update_single_element(
2316 src,
2317 *offset_bits + member_offset_bits,
2318 member,
2319 *element_width,
2321 value_as_byte_array,
2322 non_const_update_bound,
2323 ns));
2324
2325 member_offset_bits += *element_width;
2326 }
2327
2328 return simplify_expr(struct_exprt{std::move(elements), struct_type}, ns);
2329}
2330
2341 const byte_update_exprt &src,
2342 const union_typet &union_type,
2343 const exprt &value_as_byte_array,
2344 const std::optional<exprt> &non_const_update_bound,
2345 const namespacet &ns)
2346{
2347 const auto widest_member = union_type.find_widest_union_component(ns);
2348
2350 widest_member.has_value(),
2351 "lower_byte_update of union of unknown size is not supported");
2352
2353 byte_update_exprt bu = src;
2355 src.op(), widest_member->first.get_name(), widest_member->first.type()});
2356 bu.type() = widest_member->first.type();
2357
2358 return union_exprt{
2359 widest_member->first.get_name(),
2360 lower_byte_update(bu, value_as_byte_array, non_const_update_bound, ns),
2361 src.type()};
2362}
2363
2373 const byte_update_exprt &src,
2374 const exprt &value_as_byte_array,
2375 const std::optional<exprt> &non_const_update_bound,
2376 const namespacet &ns)
2377{
2378 if(src.type().id() == ID_array || src.type().id() == ID_vector)
2379 {
2380 std::optional<typet> subtype;
2381 if(src.type().id() == ID_vector)
2382 subtype = to_vector_type(src.type()).element_type();
2383 else
2384 subtype = to_array_type(src.type()).element_type();
2385
2386 auto element_width = pointer_offset_bits(*subtype, ns);
2387
2388 if(
2389 element_width.has_value() &&
2390 *element_width == bitst{src.get_bits_per_byte()})
2391 {
2392 if(value_as_byte_array.id() != ID_array)
2393 {
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);
2399 }
2400
2402 src,
2403 *subtype,
2404 to_array_expr(value_as_byte_array),
2405 non_const_update_bound,
2406 ns);
2407 }
2408 else
2409 {
2411 src,
2412 *subtype,
2413 element_width,
2414 value_as_byte_array,
2415 non_const_update_bound,
2416 ns);
2417 }
2418 }
2419 else if(src.type().id() == ID_struct || src.type().id() == ID_struct_tag)
2420 {
2421 const struct_typet &struct_type =
2422 src.type().id() == ID_struct_tag
2424 : to_struct_type(src.type());
2426 src, struct_type, value_as_byte_array, non_const_update_bound, ns);
2427 result.type() = src.type();
2428 return result;
2429 }
2430 else if(src.type().id() == ID_union || src.type().id() == ID_union_tag)
2431 {
2432 const union_typet &union_type =
2433 src.type().id() == ID_union_tag
2434 ? ns.follow_tag(to_union_tag_type(src.type()))
2435 : to_union_type(src.type());
2437 src, union_type, value_as_byte_array, non_const_update_bound, ns);
2438 result.type() = src.type();
2439 return result;
2440 }
2441 else if(
2443 src.type().id() == ID_c_enum || src.type().id() == ID_c_enum_tag)
2444 {
2445 // mask out the bits to be updated, shift the value according to the
2446 // offset and bit-or
2447 const auto type_width = pointer_offset_bits(src.type(), ns);
2448 CHECK_RETURN(type_width.has_value() && *type_width > bitst{0});
2449 const std::size_t type_bits = numeric_cast_v<std::size_t>(*type_width);
2450
2451 exprt::operandst update_bytes;
2452 if(value_as_byte_array.id() == ID_array)
2453 update_bytes = value_as_byte_array.operands();
2454 else
2455 {
2456 update_bytes = instantiate_byte_array(
2457 value_as_byte_array,
2458 0,
2459 (type_bits + src.get_bits_per_byte() - 1) / src.get_bits_per_byte(),
2460 src.get_bits_per_byte(),
2461 ns);
2462 }
2463
2464 const std::size_t update_size_bits =
2465 update_bytes.size() * src.get_bits_per_byte();
2466 const std::size_t bit_width = std::max(type_bits, update_size_bits);
2467
2468 const bool is_little_endian = src.id() == ID_byte_update_little_endian;
2469
2470 exprt val_before =
2471 typecast_exprt::conditional_cast(src.op(), bv_typet{type_bits});
2472 if(bit_width > type_bits)
2473 {
2474 val_before = concatenation_exprt{
2475 bv_typet{bit_width - type_bits}.all_zeros_expr(),
2476 val_before,
2477 bv_typet{bit_width}};
2478
2479 if(!is_little_endian)
2480 to_concatenation_expr(val_before)
2481 .op0()
2482 .swap(to_concatenation_expr(val_before).op1());
2483 }
2484
2485 if(non_const_update_bound.has_value())
2486 {
2487 const exprt src_as_bytes = unpack_rec(
2488 val_before,
2489 src.id() == ID_byte_update_little_endian,
2490 bytest{0},
2491 bytest{update_bytes.size()},
2492 src.get_bits_per_byte(),
2493 ns);
2494 CHECK_RETURN(src_as_bytes.id() == ID_array);
2495 CHECK_RETURN(src_as_bytes.operands().size() == update_bytes.size());
2496 for(std::size_t i = 0; i < update_bytes.size(); ++i)
2497 {
2498 update_bytes[i] = if_exprt{
2500 from_integer(i, non_const_update_bound->type()),
2501 ID_lt,
2502 *non_const_update_bound},
2503 update_bytes[i],
2504 src_as_bytes.operands()[i]};
2505 }
2506 }
2507
2508 // build mask
2509 exprt mask;
2510 if(is_little_endian)
2511 mask = from_integer(power(2, update_size_bits) - 1, bv_typet{bit_width});
2512 else
2513 {
2515 power(2, bit_width) - power(2, bit_width - update_size_bits),
2516 bv_typet{bit_width});
2517 }
2518
2519 const typet &offset_type = src.offset().type();
2520 mult_exprt offset_in_bits{
2521 src.offset(), from_integer(src.get_bits_per_byte(), offset_type)};
2522
2523 const binary_predicate_exprt offset_ge_zero{
2524 offset_in_bits, ID_ge, from_integer(0, offset_type)};
2525
2526 if_exprt mask_shifted{
2527 offset_ge_zero,
2528 shl_exprt{mask, offset_in_bits},
2529 lshr_exprt{mask, unary_minus_exprt{offset_in_bits}}};
2530 if(!is_little_endian)
2531 {
2532 mask_shifted.true_case().swap(mask_shifted.false_case());
2533 to_shift_expr(mask_shifted.true_case())
2534 .distance()
2535 .swap(to_shift_expr(mask_shifted.false_case()).distance());
2536 }
2537
2538 // original_bits &= ~mask
2539 bitand_exprt bitand_expr{val_before, bitnot_exprt{mask_shifted}};
2540
2541 // concatenate and zero-extend the value
2542 concatenation_exprt value{update_bytes, bv_typet{update_size_bits}};
2543 if(is_little_endian)
2544 std::reverse(value.operands().begin(), value.operands().end());
2545
2546 exprt zero_extended;
2547 if(bit_width > update_size_bits)
2548 {
2549 if(is_little_endian)
2550 zero_extended = zero_extend_exprt{value, bv_typet{bit_width}};
2551 else
2552 {
2553 // Big endian -- the zero is added as LSB.
2554 zero_extended = concatenation_exprt{
2555 value,
2556 bv_typet{bit_width - update_size_bits}.all_zeros_expr(),
2557 bv_typet{bit_width}};
2558 }
2559 }
2560 else
2561 zero_extended = value;
2562
2563 // shift the value
2564 if_exprt value_shifted{
2565 offset_ge_zero,
2566 shl_exprt{zero_extended, offset_in_bits},
2567 lshr_exprt{zero_extended, unary_minus_exprt{offset_in_bits}}};
2568 if(!is_little_endian)
2569 {
2570 value_shifted.true_case().swap(value_shifted.false_case());
2571 to_shift_expr(value_shifted.true_case())
2572 .distance()
2573 .swap(to_shift_expr(value_shifted.false_case()).distance());
2574 }
2575
2576 // original_bits |= newvalue
2577 bitor_exprt bitor_expr{bitand_expr, value_shifted};
2578
2579 if(bit_width > type_bits)
2580 {
2581 endianness_mapt endianness_map(
2582 bitor_expr.type(), src.id() == ID_byte_update_little_endian, ns);
2583 const auto bounds = map_bounds(endianness_map, 0, type_bits - 1);
2584
2585 PRECONDITION(pointer_offset_bits(bitor_expr.type(), ns).has_value());
2586 return simplify_expr(
2588 extractbits_exprt{bitor_expr, bounds.lb, bv_typet{type_bits}},
2589 src.type()),
2590 ns);
2591 }
2592
2593 return simplify_expr(
2594 typecast_exprt::conditional_cast(bitor_expr, src.type()), ns);
2595 }
2596 else
2597 {
2599 false, "lower_byte_update does not yet support ", src.type().id_string());
2600 }
2601}
2602
2604{
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");
2609
2610 // An update of a void-typed object or update by a void-typed value is the
2611 // source operand (this is questionable, but may arise when dereferencing
2612 // invalid pointers).
2613 if(src.type().id() == ID_empty || src.value().type().id() == ID_empty)
2614 return src.op();
2615
2616 // byte_update lowering proceeds as follows:
2617 // 1) Determine the size of the update, with the size of the object to be
2618 // updated as an upper bound. We fail if neither can be determined.
2619 // 2) Turn the update value into a byte array of the size determined above.
2620 // 3) If the offset is not constant, turn the object into a byte array, and
2621 // use a "with" expression to encode the update; else update the values in
2622 // place.
2623 // 4) Construct a new object.
2624 std::optional<exprt> non_const_update_bound;
2625 // update value, may require extension to full bytes
2626 exprt update_value = src.value();
2627 auto update_size_expr_opt = size_of_expr(update_value.type(), ns);
2628 CHECK_RETURN(update_size_expr_opt.has_value());
2629 simplify(update_size_expr_opt.value(), ns);
2630
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;
2634 const std::size_t bits_per_byte = src.get_bits_per_byte();
2635
2636 if(!update_size_expr_opt.value().is_constant())
2637 non_const_update_bound = *update_size_expr_opt;
2638 else
2639 {
2640 auto update_bits = pointer_offset_bits(update_value.type(), ns);
2641 // If the following invariant fails, then the type was only found to be
2642 // constant via simplification. Such instances should be fixed at the place
2643 // introducing these constant-but-not-constant_exprt type sizes.
2645 update_bits.has_value(), "constant size-of should imply fixed bit width");
2646 const size_t update_bits_int = numeric_cast_v<size_t>(*update_bits);
2647
2648 if(update_bits_int % bits_per_byte != 0)
2649 {
2651 can_cast_type<bitvector_typet>(update_value.type()),
2652 "non-byte aligned type expected to be a bitvector type");
2653 const byte_extract_exprt overlapping_byte_extract{
2654 extract_opcode,
2655 src.op(),
2657 plus_exprt{
2658 src.offset(),
2659 from_integer(update_bits_int / bits_per_byte, src.offset().type())},
2660 ns),
2661 src.get_bits_per_byte(),
2662 bv_typet{bits_per_byte}};
2663 const exprt overlapping_byte =
2664 lower_byte_extract(overlapping_byte_extract, ns);
2665
2666 size_t n_extra_bits = bits_per_byte - update_bits_int % bits_per_byte;
2667 extractbits_exprt extra_bits{overlapping_byte, 0, bv_typet{n_extra_bits}};
2668
2669 update_value = concatenation_exprt{
2671 update_value, bv_typet{update_bits_int}),
2672 extra_bits,
2673 adjust_width(update_value.type(), update_bits_int + n_extra_bits)};
2674 }
2675 else
2676 {
2677 update_size_expr_opt = from_integer(
2678 update_bits_int / bits_per_byte, update_size_expr_opt->type());
2679 }
2680 }
2681
2682 const byte_extract_exprt byte_extract_expr{
2683 extract_opcode,
2684 update_value,
2685 from_integer(0, src.offset().type()),
2686 src.get_bits_per_byte(),
2687 array_typet{bv_typet{bits_per_byte}, *update_size_expr_opt}};
2688
2689 const exprt value_as_byte_array = lower_byte_extract(byte_extract_expr, ns);
2690
2691 exprt result =
2692 lower_byte_update(src, value_as_byte_array, non_const_update_bound, ns);
2693 return result;
2694}
2695
2697{
2698 exprt tmp = src;
2699
2700 // destroys any sharing, should use hash table
2701 Forall_operands(it, tmp)
2702 {
2703 *it = lower_byte_operators(*it, ns);
2704 }
2705
2706 if(
2707 src.id() == ID_byte_update_little_endian ||
2708 src.id() == ID_byte_update_big_endian)
2709 {
2710 return lower_byte_update(to_byte_update_expr(tmp), ns);
2711 }
2712 else if(
2713 src.id() == ID_byte_extract_little_endian ||
2714 src.id() == ID_byte_extract_big_endian)
2715 {
2717 }
2718 else
2719 return tmp;
2720}
constant_exprt from_integer(const mp_integer &int_value, const typet &type)
bool to_integer(const constant_exprt &expr, mp_integer &int_value)
Convert a constant expression expr to an arbitrary-precision integer.
mp_integer power(const mp_integer &base, const mp_integer &exponent)
A multi-precision implementation of the power operator.
Target numeric_cast_v(const mp_integer &arg)
Convert an mp_integer to integral type Target An invariant will fail if the conversion is not possibl...
std::optional< Target > numeric_cast(const exprt &arg)
Converts an expression to any integral type.
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.
Definition c_types.h:80
const c_enum_typet & to_c_enum_type(const typet &type)
Cast a typet to a c_enum_typet.
Definition c_types.h:335
const union_typet & to_union_type(const typet &type)
Cast a typet to a union_typet.
Definition c_types.h:184
const union_tag_typet & to_union_tag_type(const typet &type)
Cast a typet to a union_tag_typet.
Definition c_types.h:224
Expression to define a mapping from an argument (index) to elements.
Definition std_expr.h:3467
Array constructor from list of elements.
Definition std_expr.h:1570
Arrays with given size.
Definition std_types.h:806
typet index_type() const
The type of the index expressions into any instance of this type.
Definition std_types.cpp:34
const exprt & size() const
Definition std_types.h:839
const typet & element_type() const
The type of the elements of the array.
Definition std_types.h:826
A base class for expressions that are predicates, i.e., Boolean-typed, and that take exactly two argu...
Definition std_expr.h:737
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 of type type extracted from some object op starting at position offset (given in number of...
std::size_t get_bits_per_byte() const
Expression corresponding to op() where the bytes starting at position offset (given in number of byte...
const exprt & offset() const
const exprt & op() const
void set_op(exprt e)
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' (...
Definition c_types.h:20
Complex constructor from a pair of numbers.
Definition std_expr.h:1859
Imaginary part of the expression describing a complex number.
Definition std_expr.h:1957
Real part of the expression describing a complex number.
Definition std_expr.h:1920
Complex numbers made of pair of given subtype.
Definition std_types.h:1023
Concatenation of bit-vector operands.
Division.
Definition std_expr.h:1152
Union constructor to support unions without any member (a GCC/Clang feature).
Definition std_expr.h:1783
Maps a big-endian offset to a little-endian offset.
size_t number_of_bits() const
size_t map_bit(size_t bit) const
Equality.
Definition std_expr.h:1339
Base class for all expressions.
Definition expr.h:57
std::vector< exprt > operandst
Definition expr.h:59
bool is_constant() const
Return whether the expression is a constant.
Definition expr.h:213
typet & type()
Return the type of the expression.
Definition expr.h:85
operandst & operands()
Definition expr.h:95
void add_to_operands(const exprt &expr)
Add the given argument to the end of exprt's operands.
Definition expr.h:171
Extracts a sub-range of a bit-vector operand.
The trinary if-then-else operator.
Definition std_expr.h:2426
exprt & false_case()
Definition std_expr.h:2463
exprt & true_case()
Definition std_expr.h:2453
Array index operator.
Definition std_expr.h:1431
exprt & index()
Definition std_expr.h:1471
exprt & array()
Definition std_expr.h:1461
const std::string & id_string() const
Definition irep.h:393
void swap(irept &irep)
Definition irep.h:436
const irep_idt & id() const
Definition irep.h:390
bool is_nil() const
Definition irep.h:370
Logical right shift.
Extract member of struct or union.
Definition std_expr.h:2866
Binary minus.
Definition std_expr.h:1065
Modulo defined as lhs-(rhs * truncate(lhs/rhs)).
Definition std_expr.h:1216
Binary multiplication Associativity is not specified.
Definition std_expr.h:1104
exprt & op0()
Definition std_expr.h:936
const union_typet & follow_tag(const union_tag_typet &) const
Follow type tag of union type.
Definition namespace.cpp:49
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition namespace.h:91
The NIL expression.
Definition std_expr.h:3144
Boolean OR All operands must be boolean, and the result is always boolean.
Definition std_expr.h:2193
The plus expression Associativity is not specified.
Definition std_expr.h:1006
exprt & distance()
Left shift.
Fixed-width bit-vector with two's complement interpretation.
Struct constructor from list of elements.
Definition std_expr.h:1820
Structure type, corresponds to C style structs.
Definition std_types.h:230
const componentst & components() const
Definition std_types.h:146
std::vector< componentt > componentst
Definition std_types.h:139
Expression to hold a symbol (variable).
Definition std_expr.h:132
const typet & subtype() const
Definition type.h:187
Semantic type conversion.
Definition std_expr.h:1995
static exprt conditional_cast(const exprt &expr, const typet &type)
Definition std_expr.h:2003
The type of an expression, extends irept.
Definition type.h:29
The unary minus expression.
Definition std_expr.h:477
Union constructor from single element.
Definition std_expr.h:1724
The union type.
Definition c_types.h:147
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.
Definition c_types.cpp:300
Fixed-width bit-vector with unsigned binary interpretation.
Vector constructor from list of elements.
Definition std_expr.h:1686
The vector type.
Definition std_types.h:954
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.
Definition std_types.h:970
Operator to update elements in structs and arrays.
Definition std_expr.h:2520
zero extension The operand is converted to the given type by either a) truncating if the new type is ...
#define Forall_operands(it, expr)
Definition expr.h:28
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.
Definition mp_arith.h:188
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.
Definition mp_arith.h:205
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.
Definition mp_arith.h:196
named_mp_integert< bits_tagt > bitst
Definition mp_arith.h:179
named_mp_integert< bytes_tagt > bytest
Definition mp_arith.h:184
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...
Definition narrow.h:19
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)
Pointer Logic.
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)
BigInt mp_integer
Definition smt_terms.h:17
#define PRECONDITION_WITH_DIAGNOSTICS(CONDITION,...)
Definition invariant.h:464
#define CHECK_RETURN(CONDITION)
Definition invariant.h:495
#define DATA_INVARIANT(CONDITION, REASON)
This condition should be used to document that assumptions that are made on goto_functions,...
Definition invariant.h:534
#define PRECONDITION(CONDITION)
Definition invariant.h:463
#define INVARIANT(CONDITION, REASON)
This macro uses the wrapper function 'invariant_violated_string'.
Definition invariant.h:423
const index_exprt & to_index_expr(const exprt &expr)
Cast an exprt to an index_exprt.
Definition std_expr.h:1494
const array_exprt & to_array_expr(const exprt &expr)
Cast an exprt to an array_exprt.
Definition std_expr.h:1614
const constant_exprt & to_constant_expr(const exprt &expr)
Cast an exprt to a constant_exprt.
Definition std_expr.h:3078
const vector_typet & to_vector_type(const typet &type)
Cast a typet to a vector_typet.
Definition std_types.h:1006
const struct_typet & to_struct_type(const typet &type)
Cast a typet to a struct_typet.
Definition std_types.h:307
const struct_tag_typet & to_struct_tag_type(const typet &type)
Cast a typet to a struct_tag_typet.
Definition std_types.h:517
const complex_typet & to_complex_type(const typet &type)
Cast a typet to a complex_typet.
Definition std_types.h:1048
const array_typet & to_array_type(const typet &type)
Cast a typet to an array_typet.
Definition std_types.h:887
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)
Definition type.h:208
#define size_type
Definition unistd.c:186
dstringt irep_idt