CBMC: /home/runner/work/cbmc/cbmc/src/util/lower_byte_operators.cpp Source File

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
29 struct 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
60{
61 if(src.id() == ID_unsignedbv)
63 else if(src.id() == ID_signedbv)
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,
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())
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,
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())
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())
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())
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);
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{
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 == 0)
190 else
192 }
194 !num_elements.has_value() || !subtype_bits.has_value() ||
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 =
206 const auto bounds = map_bounds(
207 endianness_map, i * subtype_bits_int, ((i + 1) * subtype_bits_int) - 1);
208 bitvector_typet type =
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,
232 const endianness_mapt &endianness_map,
233 const namespacet &ns)
234{
235 const std::size_t num_elements =
237 auto subtype_bits = pointer_offset_bits(vector_type.element_type(), ns);
239 !subtype_bits.has_value() ||
241 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 =
252 const auto bounds = map_bounds(
253 endianness_map, i * subtype_bits_int, ((i + 1) * subtype_bits_int) - 1);
254 bitvector_typet type =
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,
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 {
290 "subtype width should be half of the total bitvector width");
291 }
292 else
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 =
301
302 PRECONDITION(pointer_offset_bits(bitvector_expr.type(), ns).has_value());
303 return complex_exprt{
306 complex_type.subtype(),
307 endianness_map,
308 ns),
311 complex_type.subtype(),
312 endianness_map,
313 ns),
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 =
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(
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 {
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(
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<mp_integer> &offset_bytes,
412 const std::optional<mp_integer> &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
442 ? to_array_type(src.type()).element_type()
443 : to_vector_type(src.type()).element_type();
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;
484 "$array_comprehension_index_a_v" +
485 std::to_string(array_comprehension_index_counter),
486 index_type};
487
488 index_exprt element{
489 src,
491
492 exprt sub =
493 unpack_rec(element, little_endian, {}, {}, bits_per_byte, ns, false);
496
497 exprt body = sub_operands.front();
498 const mod_exprt offset{
501 for(std::size_t i = 1; i < el_bytes; ++i)
502 {
503 body = if_exprt{
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),
524 }
525
527 std::move(array_comprehension_index),
528 std::move(body),
533}
534
551 const exprt &src,
552 const std::optional<mp_integer> &src_size,
554 bool little_endian,
555 const std::optional<mp_integer> &offset_bytes,
556 const std::optional<mp_integer> &max_bytes,
557 const std::size_t bits_per_byte,
558 const namespacet &ns)
559{
560 const std::size_t el_bytes = numeric_cast_v<std::size_t>(
562
563 if(!src_size.has_value() && !max_bytes.has_value())
564 {
566 el_bytes > 0 && element_bits % bits_per_byte == 0,
567 "unpacking of arrays with non-byte-aligned elements is not supported");
569 src, el_bytes, little_endian, bits_per_byte, ns);
570 }
571
574
575 // refine the number of elements to extract in case the element width is known
576 // and a multiple of bytes; otherwise we will expand the entire array/vector
577 std::optional<mp_integer> num_elements = src_size;
578 if(element_bits > 0 && element_bits % bits_per_byte == 0)
579 {
580 if(!num_elements.has_value())
581 {
582 // turn bytes into elements, rounding up
584 }
585
586 if(offset_bytes.has_value())
587 {
588 // compute offset as number of elements
590 // insert offset_bytes-many nil bytes into the output array
591 byte_operands.resize(
595 bv_typet{bits_per_byte}.all_zeros_expr());
596 }
597 }
598
599 // the maximum number of bytes is an upper bound in case the size of the
600 // array/vector is unknown; if element_bits was usable above this will
601 // have been turned into a number of elements already
602 if(!num_elements)
604
605 const exprt src_simp = simplify_expr(src, ns);
606 const typet index_type = src_simp.type().id() == ID_array
607 ? to_array_type(src_simp.type()).index_type()
608 : to_vector_type(src_simp.type()).index_type();
609
610 for(mp_integer i = first_element; i < *num_elements; ++i)
611 {
612 exprt element;
613
614 if(
615 (src_simp.id() == ID_array || src_simp.id() == ID_vector) &&
616 i < src_simp.operands().size())
617 {
618 const std::size_t index_int = numeric_cast_v<std::size_t>(i);
619 element = src_simp.operands()[index_int];
620 }
621 else
622 {
623 element = index_exprt(src_simp, from_integer(i, index_type));
624 }
625
626 // recursively unpack each element so that we eventually just have an array
627 // of bytes left
628
629 const std::optional<mp_integer> element_max_bytes =
630 max_bytes
631 ? std::min(mp_integer{el_bytes}, *max_bytes - byte_operands.size())
632 : std::optional<mp_integer>{};
633 const std::size_t element_max_bytes_int =
635 : el_bytes;
636
637 exprt sub = unpack_rec(
638 element, little_endian, {}, element_max_bytes, bits_per_byte, ns, true);
641 byte_operands.insert(
642 byte_operands.end(), sub_operands.begin(), sub_operands.end());
643
644 if(max_bytes && byte_operands.size() >= *max_bytes)
645 break;
646 }
647
648 const std::size_t size = byte_operands.size();
649 return array_exprt(
650 std::move(byte_operands),
652}
653
667 std::size_t total_bits,
668 exprt::operandst &dest,
669 bool little_endian,
670 const std::optional<mp_integer> &offset_bytes,
671 const std::optional<mp_integer> &max_bytes,
672 const std::size_t bits_per_byte,
673 const namespacet &ns)
674{
676 std::move(bit_fields), bv_typet{total_bits}};
677
678 exprt sub = unpack_rec(
680 little_endian,
682 max_bytes,
684 ns,
685 true);
686
687 dest.insert(
688 dest.end(),
689 std::make_move_iterator(sub.operands().begin()),
690 std::make_move_iterator(sub.operands().end()));
691}
692
704 const exprt &src,
705 bool little_endian,
706 const std::optional<mp_integer> &offset_bytes,
707 const std::optional<mp_integer> &max_bytes,
708 const std::size_t bits_per_byte,
709 const namespacet &ns)
710{
712 src.type().id() == ID_struct_tag
714 : to_struct_type(src.type());
715 const struct_typet::componentst &components = struct_type.components();
716
717 std::optional<mp_integer> offset_in_member;
718 std::optional<mp_integer> max_bytes_left;
719 std::optional<std::pair<exprt::operandst, std::size_t>> bit_fields;
720
723 for(auto it = components.begin(); it != components.end(); ++it)
724 {
725 const auto &comp = *it;
726 auto component_bits = pointer_offset_bits(comp.type(), ns);
727
728 // We can only handle a member of unknown width when it is the last member
729 // and is byte-aligned. Members of unknown width in the middle would leave
730 // us with unknown alignment of subsequent members, and queuing them up as
731 // bit fields is not possible either as the total width of the concatenation
732 // could not be determined.
734 component_bits.has_value() ||
735 (std::next(it) == components.end() && !bit_fields.has_value()),
736 "members of non-constant width should come last in a struct");
737
738 member_exprt member(src, comp.get_name(), comp.type());
739 if(src.id() == ID_struct)
740 simplify(member, ns);
741
742 // Is it a byte-aligned member?
744 {
745 if(bit_fields.has_value())
746 {
748 std::move(bit_fields->first),
749 bit_fields->second,
751 little_endian,
755 ns);
756 bit_fields.reset();
757 }
758
759 if(offset_bytes.has_value())
760 {
762 // if the offset is negative, offset_in_member remains unset, which has
763 // the same effect as setting it to zero
764 if(*offset_in_member < 0)
765 offset_in_member.reset();
766 }
767
768 if(max_bytes.has_value())
769 {
771 if(*max_bytes_left < 0)
772 break;
773 }
774 }
775
776 if(
778 (component_bits.has_value() && *component_bits % bits_per_byte != 0))
779 {
780 if(!bit_fields.has_value())
781 bit_fields = std::make_pair(exprt::operandst{}, std::size_t{0});
782
784 bit_fields->first.insert(
785 little_endian ? bit_fields->first.begin() : bit_fields->first.end(),
787 bit_fields->second += bits_int;
788
790
791 continue;
792 }
793
794 INVARIANT(
795 !bit_fields.has_value(),
796 "all preceding members should have been processed");
797
798 if(
799 component_bits.has_value() && offset_in_member.has_value() &&
801 {
802 // we won't actually need this component, fill in zeros instead of
803 // computing an unpacking
804 byte_operands.resize(
805 byte_operands.size() +
807 bv_typet{bits_per_byte}.all_zeros_expr());
808 }
809 else
810 {
811 exprt sub = unpack_rec(
812 member,
813 little_endian,
817 ns,
818 true);
819
820 byte_operands.insert(
821 byte_operands.end(),
822 std::make_move_iterator(sub.operands().begin()),
823 std::make_move_iterator(sub.operands().end()));
824 }
825
826 if(component_bits.has_value())
828 }
829
830 // any remaining bit fields?
831 if(bit_fields.has_value())
832 {
834 std::move(bit_fields->first),
835 bit_fields->second,
837 little_endian,
841 ns);
842 }
843
844 const std::size_t size = byte_operands.size();
845 return array_exprt{
846 std::move(byte_operands),
848}
849
857 const exprt &src,
858 bool little_endian,
859 const std::size_t bits_per_byte,
860 const namespacet &ns)
861{
863 const typet &subtype = complex_type.subtype();
864
865 auto subtype_bits = pointer_offset_bits(subtype, ns);
866 CHECK_RETURN(subtype_bits.has_value());
868
871 little_endian,
872 mp_integer{0},
875 ns,
876 true);
877 exprt::operandst byte_operands = std::move(sub_real.operands());
878
881 little_endian,
882 mp_integer{0},
885 ns,
886 true);
887 byte_operands.insert(
888 byte_operands.end(),
889 std::make_move_iterator(sub_imag.operands().begin()),
890 std::make_move_iterator(sub_imag.operands().end()));
891
892 const std::size_t size = byte_operands.size();
893 return array_exprt{
894 std::move(byte_operands),
896}
897
908// array of bytes
911 const exprt &src,
912 bool little_endian,
913 const std::optional<mp_integer> &offset_bytes,
914 const std::optional<mp_integer> &max_bytes,
915 const std::size_t bits_per_byte,
916 const namespacet &ns,
918{
919 if(src.type().id() == ID_array)
920 {
922 const typet &subtype = array_type.element_type();
923
924 auto element_bits = pointer_offset_bits(subtype, ns);
925 CHECK_RETURN(element_bits.has_value());
926
927 if(
930 {
931 return src;
932 }
933
935 return unpack_array_vector(
936 src,
939 little_endian,
941 max_bytes,
943 ns);
944 }
945 else if(src.type().id() == ID_vector)
946 {
948 const typet &subtype = vector_type.element_type();
949
950 auto element_bits = pointer_offset_bits(subtype, ns);
951 CHECK_RETURN(element_bits.has_value());
952
953 if(
956 {
957 return src;
958 }
959
960 return unpack_array_vector(
961 src,
964 little_endian,
966 max_bytes,
968 ns);
969 }
970 else if(src.type().id() == ID_complex)
971 {
972 return unpack_complex(src, little_endian, bits_per_byte, ns);
973 }
974 else if(src.type().id() == ID_struct || src.type().id() == ID_struct_tag)
975 {
976 return unpack_struct(
977 src, little_endian, offset_bytes, max_bytes, bits_per_byte, ns);
978 }
979 else if(src.type().id() == ID_union || src.type().id() == ID_union_tag)
980 {
981 const union_typet &union_type =
982 src.type().id() == ID_union_tag
984 : to_union_type(src.type());
985
986 const auto widest_member = union_type.find_widest_union_component(ns);
987
988 if(widest_member.has_value())
989 {
990 member_exprt member{
991 src, widest_member->first.get_name(), widest_member->first.type()};
992 return unpack_rec(
993 member,
994 little_endian,
996 widest_member->second,
998 ns,
999 true);
1000 }
1001 else if(!union_type.components().empty())
1002 {
1003 member_exprt member{src, union_type.components().front()};
1004 return unpack_rec(
1005 member,
1006 little_endian,
1008 max_bytes,
1010 ns,
1011 true);
1012 }
1013 }
1014 else if(src.type().id() == ID_pointer)
1015 {
1016 return unpack_rec(
1017 typecast_exprt{src, bv_typet{to_pointer_type(src.type()).get_width()}},
1018 little_endian,
1020 max_bytes,
1022 ns,
1024 }
1025 else if(src.id() == ID_string_constant)
1026 {
1027 return unpack_rec(
1029 little_endian,
1031 max_bytes,
1033 ns,
1035 }
1036 else if(src.is_constant() && src.type().id() == ID_string)
1037 {
1038 return unpack_rec(
1040 little_endian,
1042 max_bytes,
1044 ns,
1046 }
1047 else if(src.type().id() != ID_empty)
1048 {
1049 // a basic type; we turn that into extractbits while considering
1050 // endianness
1051 auto bits_opt = pointer_offset_bits(src.type(), ns);
1052 DATA_INVARIANT(bits_opt.has_value(), "basic type should have a fixed size");
1053
1057
1058 if(max_bytes.has_value())
1059 {
1060 const auto max_bits = *max_bytes * bits_per_byte;
1061 if(little_endian)
1062 {
1063 last_bit = std::min(last_bit, max_bits);
1064 }
1065 else
1066 {
1067 bit_offset = std::max(mp_integer{0}, last_bit - max_bits);
1068 }
1069 }
1070
1071 // When the source width is not a multiple of the byte size, round up
1072 // to the next byte boundary and zero-extend the source so that all
1073 // byte-sized extractbits operations remain within bounds. The padding
1074 // bits (the high bits of the final partial byte) are unspecified and are
1075 // filled with zero.
1076 const mp_integer padded_bits =
1078 ? total_bits
1083 {
1086 }
1087 auto const byte_type = bv_typet{bits_per_byte};
1091
1093 {
1095 pointer_offset_bits(src_as_bitvector.type(), ns).has_value());
1098 from_integer(bit_offset, array_type.index_type()),
1099 byte_type);
1100
1101 // endianness_mapt should be the point of reference for mapping out
1102 // endianness, but we need to work on elements here instead of
1103 // individual bits
1104 if(little_endian)
1105 byte_operands.push_back(extractbits);
1106 else
1107 byte_operands.insert(byte_operands.begin(), extractbits);
1108 }
1109
1110 const std::size_t size = byte_operands.size();
1111 array_type.size() = from_integer(size, size_type());
1112 return array_exprt{std::move(byte_operands), std::move(array_type)};
1113 }
1114
1115 return array_exprt(
1117}
1118
1130 const byte_extract_exprt &src,
1132 const typet &subtype,
1133 const mp_integer &element_bits,
1134 const namespacet &ns)
1135{
1136 std::optional<std::size_t> num_elements;
1137 if(src.type().id() == ID_array)
1139 else
1141
1142 if(num_elements.has_value())
1143 {
1144 exprt::operandst operands;
1145 operands.reserve(*num_elements);
1146 for(std::size_t i = 0; i < *num_elements; ++i)
1147 {
1149 unpacked.offset(),
1151 i * element_bits / src.get_bits_per_byte(),
1152 unpacked.offset().type()));
1153
1155 tmp.type() = subtype;
1156 tmp.offset() = new_offset;
1157
1158 operands.push_back(lower_byte_extract(tmp, ns));
1159 }
1160
1161 exprt result;
1162 if(src.type().id() == ID_array)
1163 result = array_exprt{std::move(operands), to_array_type(src.type())};
1164 else
1165 result = vector_exprt{std::move(operands), to_vector_type(src.type())};
1166
1167 return simplify_expr(result, ns);
1168 }
1169
1170 DATA_INVARIANT(src.type().id() == ID_array, "vectors have constant size");
1171 const array_typet &array_type = to_array_type(src.type());
1172
1173 // TODO we either need a symbol table here or make array comprehensions
1174 // introduce a scope
1175 static std::size_t array_comprehension_index_counter = 0;
1178 "$array_comprehension_index_a" +
1179 std::to_string(array_comprehension_index_counter),
1180 array_type.index_type()};
1181
1183 unpacked.offset(),
1185 mult_exprt{
1190 unpacked.offset().type())};
1191
1193 body.type() = subtype;
1194 body.offset() = std::move(new_offset);
1195
1197 std::move(array_comprehension_index),
1198 lower_byte_extract(body, ns),
1199 array_type};
1200}
1201
1210 static std::optional<exprt> lower_byte_extract_complex(
1211 const byte_extract_exprt &src,
1213 const namespacet &ns)
1214{
1216 const typet &subtype = complex_type.subtype();
1217
1218 auto subtype_bits = pointer_offset_bits(subtype, ns);
1219 if(!subtype_bits.has_value() || *subtype_bits % src.get_bits_per_byte() != 0)
1220 return {};
1221
1222 // offset remains unchanged
1224 real.type() = subtype;
1225
1226 const plus_exprt new_offset{
1227 unpacked.offset(),
1229 *subtype_bits / src.get_bits_per_byte(), unpacked.offset().type())};
1231 imag.type() = subtype;
1232 imag.offset() = simplify_expr(new_offset, ns);
1233
1234 return simplify_expr(
1236 lower_byte_extract(real, ns), lower_byte_extract(imag, ns), complex_type},
1237 ns);
1238}
1239
1243{
1244 // General notes about endianness and the bit-vector conversion:
1245 // A single byte with value 0b10001000 is stored (in irept) as
1246 // exactly this string literal, and its bit-vector encoding will be
1247 // bvt bv={0,0,0,1,0,0,0,1}, i.e., bv[0]==0 and bv[7]==1
1248 //
1249 // A multi-byte value like x=256 would be:
1250 // - little-endian storage: ((char*)&x)[0]==0, ((char*)&x)[1]==1
1251 // - big-endian storage: ((char*)&x)[0]==1, ((char*)&x)[1]==0
1252 // - irept representation: 0000000100000000
1253 // - bvt: {0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0}
1254 // <... 8bits ...> <... 8bits ...>
1255 //
1256 // An array {0, 1} will be encoded as bvt bv={0,1}, i.e., bv[1]==1
1257 // concatenation(0, 1) will yield a bvt bv={1,0}, i.e., bv[1]==0
1258 //
1259 // The semantics of byte_extract(endianness, op, offset, T) is:
1260 // interpret ((char*)&op)+offset as the endianness-ordered storage
1261 // of an object of type T at address ((char*)&op)+offset
1262 // For some T x, byte_extract(endianness, x, 0, T) must yield x.
1263 //
1264 // byte_extract for a composite type T or an array will interpret
1265 // the individual subtypes with suitable endianness; the overall
1266 // order of components is not affected by endianness.
1267 //
1268 // Examples:
1269 // unsigned char A[4];
1270 // byte_extract_little_endian(A, 0, unsigned short) requests that
1271 // A[0],A[1] be interpreted as the storage of an unsigned short with
1272 // A[1] being the most-significant byte; byte_extract_big_endian for
1273 // the same operands will select A[0] as the most-significant byte.
1274 //
1275 // int A[2] = {0x01020304,0xDEADBEEF}
1276 // byte_extract_big_endian(A, 0, short) should yield 0x0102
1277 // byte_extract_little_endian(A, 0, short) should yield 0x0304
1278 // To obtain this we first compute byte arrays while taking into
1279 // account endianness:
1280 // big-endian byte representation: {01,02,03,04,DE,AB,BE,EF}
1281 // little-endian byte representation: {04,03,02,01,EF,BE,AB,DE}
1282 // We extract the relevant bytes starting from ((char*)A)+0:
1283 // big-endian: {01,02}; little-endian: {04,03}
1284 // Finally we place them in the appropriate byte order as indicated
1285 // by endianness:
1286 // big-endian: (short)concatenation(01,02)=0x0102
1287 // little-endian: (short)concatenation(03,04)=0x0304
1288
1292 const bool little_endian = src.id() == ID_byte_extract_little_endian;
1293
1294 // determine an upper bound of the last byte we might need
1295 auto upper_bound_opt = size_of_expr(src.type(), ns);
1296 if(upper_bound_opt.has_value())
1297 {
1299 plus_exprt(
1300 upper_bound_opt.value(),
1302 src.offset(), upper_bound_opt.value().type())),
1303 ns);
1304 }
1305 else if(src.type().id() == ID_empty)
1307
1309 const auto upper_bound_int_opt =
1311
1313 unpacked.op() = unpack_rec(
1314 src.op(),
1315 little_endian,
1318 src.get_bits_per_byte(),
1319 ns);
1321 to_bitvector_type(to_type_with_subtype(unpacked.op().type()).subtype())
1322 .get_width() == src.get_bits_per_byte());
1323
1324 if(src.type().id() == ID_array || src.type().id() == ID_vector)
1325 {
1326 const typet &subtype = to_type_with_subtype(src.type()).subtype();
1327
1328 // consider ways of dealing with arrays of unknown subtype size or with a
1329 // subtype size that does not fit byte boundaries; currently we fall back to
1330 // stitching together consecutive elements down below
1331 auto element_bits = pointer_offset_bits(subtype, ns);
1332 if(
1333 element_bits.has_value() && *element_bits >= 1 &&
1334 *element_bits % src.get_bits_per_byte() == 0)
1335 {
1337 src, unpacked, subtype, *element_bits, ns);
1338 }
1339 }
1340 else if(src.type().id() == ID_complex)
1341 {
1342 auto result = lower_byte_extract_complex(src, unpacked, ns);
1343 if(result.has_value())
1344 return std::move(*result);
1345
1346 // else fall back to generic lowering that uses bit masks, below
1347 }
1348 else if(src.type().id() == ID_struct || src.type().id() == ID_struct_tag)
1349 {
1350 const struct_typet &struct_type =
1351 src.type().id() == ID_struct_tag
1353 : to_struct_type(src.type());
1354 const struct_typet::componentst &components = struct_type.components();
1355
1356 bool failed = false;
1357 struct_exprt s({}, src.type());
1358
1359 for(const auto &comp : components)
1360 {
1361 auto component_bits = pointer_offset_bits(comp.type(), ns);
1362
1363 // the next member would be misaligned, abort
1364 if(
1365 !component_bits.has_value() ||
1366 *component_bits % src.get_bits_per_byte() != 0)
1367 {
1368 failed = true;
1369 break;
1370 }
1371
1372 auto member_offset_opt =
1373 member_offset_expr(struct_type, comp.get_name(), ns);
1374
1375 if(!member_offset_opt.has_value())
1376 {
1377 failed = true;
1378 break;
1379 }
1380
1382 unpacked.offset(),
1384 member_offset_opt.value(), unpacked.offset().type()));
1385
1387 tmp.type() = comp.type();
1388 tmp.offset() = new_offset;
1389
1391 }
1392
1393 if(!failed)
1394 return simplify_expr(std::move(s), ns);
1395 }
1396 else if(src.type().id() == ID_union || src.type().id() == ID_union_tag)
1397 {
1398 const union_typet &union_type =
1399 src.type().id() == ID_union_tag
1400 ? ns.follow_tag(to_union_tag_type(src.type()))
1401 : to_union_type(src.type());
1402
1403 const auto widest_member = union_type.find_widest_union_component(ns);
1404
1405 if(widest_member.has_value())
1406 {
1408 tmp.type() = widest_member->first.type();
1409
1410 return union_exprt(
1411 widest_member->first.get_name(),
1413 src.type());
1414 }
1415 }
1416
1417 const exprt &root = unpacked.op();
1418 const exprt &offset = unpacked.offset();
1419
1420 std::optional<typet> subtype;
1421 std::optional<typet> index_type;
1422 if(root.type().id() == ID_vector)
1423 {
1424 subtype = to_vector_type(root.type()).element_type();
1425 index_type = to_vector_type(root.type()).index_type();
1426 }
1427 else
1428 {
1429 subtype = to_array_type(root.type()).element_type();
1430 index_type = to_array_type(root.type()).index_type();
1431 }
1432
1433 auto subtype_bits = pointer_offset_bits(*subtype, ns);
1434
1436 subtype_bits.has_value() && *subtype_bits == src.get_bits_per_byte(),
1437 "offset bits are byte aligned");
1438
1439 auto size_bits = pointer_offset_bits(unpacked.type(), ns);
1440 if(!size_bits.has_value())
1441 {
1442 auto op0_bits = pointer_offset_bits(unpacked.op().type(), ns);
1443 // all cases with non-constant width should have been handled above
1445 op0_bits.has_value(),
1446 "the extracted width or the source width must be known");
1448 }
1449
1451 (*size_bits) / src.get_bits_per_byte() +
1452 (((*size_bits) % src.get_bits_per_byte() == 0) ? 0 : 1);
1453
1454 // get 'width'-many bytes, and concatenate
1457 op.reserve(width_bytes);
1458
1459 for(std::size_t i = 0; i < width_bytes; i++)
1460 {
1461 // the most significant byte comes first in the concatenation!
1462 std::size_t offset_int = little_endian ? (width_bytes - i - 1) : i;
1463
1465 from_integer(offset_int, *index_type),
1466 typecast_exprt::conditional_cast(offset, *index_type)};
1467 simplify(offset_i, ns);
1468
1469 mp_integer index = 0;
1470 if(
1471 offset_i.is_constant() &&
1472 (root.id() == ID_array || root.id() == ID_vector) &&
1474 index < root.operands().size() && index >= 0)
1475 {
1476 // offset is constant and in range
1477 op.push_back(root.operands()[numeric_cast_v<std::size_t>(index)]);
1478 }
1479 else
1480 {
1481 op.push_back(index_exprt(root, offset_i));
1482 }
1483 }
1484
1485 if(width_bytes == 1)
1486 {
1487 return simplify_expr(
1488 typecast_exprt::conditional_cast(op.front(), src.type()), ns);
1489 }
1490 else // width_bytes>=2
1491 {
1493 std::move(op),
1494 adjust_width(*subtype, width_bytes * src.get_bits_per_byte()));
1495
1496 endianness_mapt map(concatenation.type(), little_endian, ns);
1497 return bv_to_expr(concatenation, src.type(), map, ns);
1498 }
1499}
1500
1501static exprt lower_byte_update(
1502 const byte_update_exprt &src,
1503 const exprt &value_as_byte_array,
1504 const std::optional<exprt> &non_const_update_bound,
1505 const namespacet &ns);
1506
1517 const byte_update_exprt &src,
1518 const typet &subtype,
1521 const namespacet &ns)
1522{
1523 // TODO we either need a symbol table here or make array comprehensions
1524 // introduce a scope
1525 static std::size_t array_comprehension_index_counter = 0;
1528 "$array_comprehension_index_u_a_v" +
1529 std::to_string(array_comprehension_index_counter),
1530 to_array_type(src.type()).index_type()};
1531
1532 binary_predicate_exprt lower_bound{
1534 array_comprehension_index, src.offset().type()),
1535 ID_lt,
1536 src.offset()};
1537 binary_predicate_exprt upper_bound{
1540 ID_ge,
1541 plus_exprt{
1543 src.offset(), non_const_update_bound.type()),
1545
1548 src.id() == ID_byte_update_big_endian);
1549 const bool little_endian = src.id() == ID_byte_update_little_endian;
1550 endianness_mapt map(
1551 to_array_type(value_as_byte_array.type()).element_type(),
1552 little_endian,
1553 ns);
1555 or_exprt{std::move(lower_bound), std::move(upper_bound)},
1557 bv_to_expr(
1563 src.offset(), array_comprehension_index.type())}},
1564 subtype,
1565 map,
1566 ns)};
1567
1568 return simplify_expr(
1571 std::move(array_comprehension_body),
1572 to_array_type(src.type())},
1573 ns);
1574}
1575
1586 const byte_update_exprt &src,
1587 const typet &subtype,
1589 const std::optional<exprt> &non_const_update_bound,
1590 const namespacet &ns)
1591{
1594 src.id() == ID_byte_update_big_endian);
1595 const bool little_endian = src.id() == ID_byte_update_little_endian;
1596
1597 const typet index_type = src.type().id() == ID_array
1598 ? to_array_type(src.type()).index_type()
1599 : to_vector_type(src.type()).index_type();
1600
1601 // apply 'array-update-with' num_elements times
1602 exprt result = src.op();
1603
1604 for(std::size_t i = 0; i < value_as_byte_array.operands().size(); ++i)
1605 {
1606 const exprt &element = value_as_byte_array.operands()[i];
1607
1608 exprt where = simplify_expr(
1609 plus_exprt{
1610 typecast_exprt::conditional_cast(src.offset(), index_type),
1611 from_integer(i, index_type)},
1612 ns);
1613
1614 // skip elements that wouldn't change (skip over typecasts as we might have
1615 // some signed/unsigned char conversions)
1616 const exprt &e = skip_typecast(element);
1617 if(e.id() == ID_index)
1618 {
1620 if(index_expr.array() == src.op() && index_expr.index() == where)
1621 continue;
1622 }
1623
1624 endianness_mapt map(element.type(), little_endian, ns);
1625 exprt update_value = bv_to_expr(element, subtype, map, ns);
1626 if(non_const_update_bound.has_value())
1627 {
1631 ID_lt,
1634 index_exprt{src.op(), where}};
1635 }
1636
1637 result = with_exprt{result, std::move(where), std::move(update_value)};
1638 }
1639
1640 return simplify_expr(std::move(result), ns);
1641}
1642
1659 const byte_update_exprt &src,
1660 const typet &subtype,
1661 const exprt &subtype_size,
1664 const exprt &initial_bytes,
1665 const exprt &first_index,
1667 const namespacet &ns)
1668{
1672
1673 // TODO we either need a symbol table here or make array comprehensions
1674 // introduce a scope
1675 static std::size_t array_comprehension_index_counter = 0;
1678 "$array_comprehension_index_u_a_v_u" +
1679 std::to_string(array_comprehension_index_counter),
1680 to_array_type(src.type()).index_type()};
1681
1682 // all arithmetic uses offset/index types
1683 PRECONDITION(array_comprehension_index.type() == src.offset().type());
1684 PRECONDITION(subtype_size.type() == src.offset().type());
1685 PRECONDITION(initial_bytes.type() == src.offset().type());
1686 PRECONDITION(first_index.type() == src.offset().type());
1687
1688 // the bound from where updates start
1689 binary_predicate_exprt lower_bound{
1692 ID_lt,
1693 first_index};
1694
1695 // The actual value of updates other than at the start or end
1698 mult_exprt{
1708 std::move(offset_expr),
1709 src.get_bits_per_byte(),
1710 subtype},
1711 ns);
1712
1713 // The number of target array/vector elements being replaced, not including
1714 // a possible partial update at the end of the updated range, which is handled
1715 // below: (non_const_update_bound + (subtype_size - 1)) / subtype_size to
1716 // round up to the nearest multiple of subtype_size.
1718 plus_exprt{
1722 subtype_size};
1723
1724 // The last element to be updated: first_index + updated_elements - 1
1728 std::move(updated_elements), from_integer(1, first_index.type())}};
1729
1730 // The size of a partial update at the end of the updated range, may be zero.
1737 subtype_size.type()),
1738 subtype_size};
1739
1740 // The bound where updates end, which is conditional on the partial update
1741 // being empty.
1742 binary_predicate_exprt upper_bound{
1745 ID_ge,
1746 if_exprt{
1748 last_index,
1750
1751 // The actual value of a partial update at the end.
1754 src.id(),
1755 index_exprt{src.op(), last_index},
1758 subtype_size}},
1760 src.get_bits_per_byte()},
1761 ns);
1762
1764 or_exprt{std::move(lower_bound), std::move(upper_bound)},
1766 if_exprt{
1770 first_index},
1772 if_exprt{
1776 last_index},
1777 std::move(last_update_value),
1778 std::move(update_value)}}};
1779
1780 return simplify_expr(
1783 std::move(array_comprehension_body),
1784 to_array_type(src.type())},
1785 ns);
1786}
1787
1799 const byte_update_exprt &src,
1800 const typet &subtype,
1802 const std::optional<exprt> &non_const_update_bound,
1803 const namespacet &ns)
1804{
1805 // do all arithmetic below using index/offset types - the array theory
1806 // back-end is really picky about indices having the same type
1807 auto subtype_size_opt = size_of_expr(subtype, ns);
1808 CHECK_RETURN(subtype_size_opt.has_value());
1809
1812 subtype_size_opt.value(), src.offset().type()),
1813 ns);
1814
1815 // compute the index of the first element of the array/vector that may be
1816 // updated
1818 src.offset().type() == to_array_type(src.op().type()).index_type());
1820 simplify(first_index, ns);
1821
1822 // compute the offset within that first element
1824
1825 // compute the number of bytes (from the update value) that are going to be
1826 // consumed for updating the first element
1827 const exprt update_size =
1828 from_integer(value_as_byte_array.operands().size(), subtype_size.type());
1831 if(non_const_update_bound.has_value())
1832 {
1835 }
1836 else
1837 {
1840 "value should be an array expression if the update bound is constant");
1842 }
1846 update_bound};
1848
1849 // encode the first update: update the original element at first_index (the
1850 // actual update will only be initial_bytes-many bytes from
1851 // value_as_byte_array)
1854 src.id(),
1855 index_exprt{src.op(), first_index},
1858 src.get_bits_per_byte()},
1859 ns);
1860
1861 if(value_as_byte_array.id() != ID_array)
1862 {
1864 src,
1865 subtype,
1872 ns);
1873 }
1874
1875 // We will update one array/vector element at a time - compute the number of
1876 // update values that will be consumed in each step. If we cannot determine a
1877 // constant value at this time we assume it's at least one byte. The
1878 // byte_extract_exprt within the loop uses the symbolic value (subtype_size),
1879 // we just need to make sure we make progress for the loop to terminate.
1880 std::size_t step_size = 1;
1881 if(subtype_size.is_constant())
1883 // Given the first update already encoded, keep track of the offset into the
1884 // update value. Again, the expressions within the loop use the symbolic
1885 // value, this is just an optimization in case we can determine a constant
1886 // offset.
1887 std::size_t offset = 0;
1888 if(initial_bytes.is_constant())
1890
1892
1894 [&src,
1895 &first_index,
1897 &subtype_size,
1899 &ns,
1900 &result](std::size_t i) -> void {
1901 exprt where = simplify_expr(
1903
1908 ns);
1909
1910 exprt element = lower_byte_update(
1912 src.id(),
1913 index_exprt{src.op(), where},
1916 src.get_bits_per_byte()},
1917 ns);
1918
1919 result = with_exprt{result, std::move(where), std::move(element)};
1920 };
1921
1922 std::size_t i = 1;
1923 for(; offset + step_size <= value_as_byte_array.operands().size();
1924 offset += step_size, ++i)
1925 {
1927 }
1928
1929 // do the tail
1930 if(offset < value_as_byte_array.operands().size())
1932
1933 return simplify_expr(std::move(result), ns);
1934}
1935
1949 const byte_update_exprt &src,
1950 const mp_integer &offset_bits,
1951 const exprt &element_to_update,
1952 const mp_integer &subtype_bits,
1955 const std::optional<exprt> &non_const_update_bound,
1956 const namespacet &ns)
1957{
1958 // We need to take one or more bytes from value_as_byte_array to modify the
1959 // target element. We need to compute:
1960 // - The position in value_as_byte_array to take bytes from: If subtype_bits
1961 // is less than the size of a byte, then multiple struct/array/vector elements
1962 // will need to be updated using the same byte.
1963 std::size_t first = 0;
1964 // - An upper bound on the number of bytes required from value_as_byte_array.
1966 (subtype_bits + src.get_bits_per_byte() - 1) / src.get_bits_per_byte();
1967 // - The offset into the target element: If subtype_bits is greater or equal
1968 // to the size of a byte, then there may be an offset into the target element
1969 // that needs to be taken into account, and multiple bytes will be required.
1971
1973 {
1974 INVARIANT(
1975 value_as_byte_array.id() != ID_array ||
1976 value_as_byte_array.operands().size() * src.get_bits_per_byte() >
1978 "indices past the update should be handled below");
1983 src.get_bits_per_byte();
1986 }
1987 else
1988 {
1990 INVARIANT(
1991 update_elements > 0, "indices before the update should be handled above");
1992 }
1993
1994 std::size_t end = first + numeric_cast_v<std::size_t>(update_elements);
1995 if(value_as_byte_array.id() == ID_array)
1996 end = std::min(end, value_as_byte_array.operands().size());
1998 value_as_byte_array, first, end, src.get_bits_per_byte(), ns);
1999 const std::size_t update_size = update_values.size();
2001 const array_typet update_type{
2003
2004 // each case below will set "new_value" as appropriate
2005 exprt new_value;
2006
2007 if(
2010 {
2011 new_value = array_exprt{update_values, update_type};
2012 }
2013 else
2014 {
2015 if(src.id() == ID_byte_update_little_endian)
2016 std::reverse(update_values.begin(), update_values.end());
2018 {
2019 if(src.id() == ID_byte_update_little_endian)
2020 {
2021 new_value = lshr_exprt{
2025 }
2026 else
2027 {
2028 new_value = shl_exprt{
2032 }
2033 }
2034 else
2035 {
2036 const std::size_t offset_bits_int =
2038 const std::size_t subtype_bits_int =
2040
2045 new_value = concatenation_exprt{
2053 }
2054
2058
2061 new_value,
2062 from_integer(0, src.offset().type()),
2063 src.get_bits_per_byte(),
2064 update_type};
2065
2066 new_value = lower_byte_extract(byte_extract_expr, ns);
2067
2069 }
2070
2071 // With an upper bound on the size of the update established, construct the
2072 // actual update expression. If the exact size of the update is unknown,
2073 // make the size expression conditional.
2074 const byte_update_exprt bu{
2075 src.id(),
2079 src.offset().type()),
2080 new_value,
2081 src.get_bits_per_byte()};
2082
2083 std::optional<exprt> update_bound;
2084 if(non_const_update_bound.has_value())
2085 {
2086 // The size of the update is not constant, and thus is to be symbolically
2087 // bound; first see how many bytes we have left in the update:
2088 // non_const_update_bound > first ? non_const_update_bound - first: 0
2090 if_exprt{
2093 ID_gt,
2094 from_integer(first, non_const_update_bound->type())},
2097 from_integer(first, non_const_update_bound->type())},
2099 size_type());
2100 // Now take the minimum of update-bytes-left and the previously computed
2101 // size of the element to be updated:
2103 if_exprt{
2107 ns);
2108 }
2109
2110 return lower_byte_update(bu, new_value, update_bound, ns);
2111}
2112
2124 const byte_update_exprt &src,
2125 const typet &subtype,
2126 const std::optional<mp_integer> &subtype_bits,
2128 const std::optional<exprt> &non_const_update_bound,
2129 const namespacet &ns)
2130{
2131 const bool is_array = src.type().id() == ID_array;
2132 exprt size;
2133 typet index_type;
2134 if(is_array)
2135 {
2136 size = to_array_type(src.type()).size();
2137 index_type = to_array_type(src.type()).index_type();
2138 }
2139 else
2140 {
2141 size = to_vector_type(src.type()).size();
2142 index_type = to_vector_type(src.type()).index_type();
2143 }
2144
2145 // fall back to bytewise updates in all non-constant or dubious cases
2146 if(
2147 !size.is_constant() || !src.offset().is_constant() ||
2148 !subtype_bits.has_value() || value_as_byte_array.id() != ID_array)
2149 {
2151 src, subtype, value_as_byte_array, non_const_update_bound, ns);
2152 }
2153
2154 std::size_t num_elements =
2158 src.get_bits_per_byte();
2159
2160 exprt::operandst elements;
2161 elements.reserve(num_elements);
2162
2163 std::size_t i = 0;
2164 // copy the prefix not affected by the update
2165 for(; i < num_elements && (i + 1) * *subtype_bits <= offset_bits; ++i)
2166 elements.push_back(index_exprt{src.op(), from_integer(i, index_type)});
2167
2168 // the modified elements
2169 for(;
2170 i < num_elements &&
2171 i * *subtype_bits < offset_bits + value_as_byte_array.operands().size() *
2172 src.get_bits_per_byte();
2173 ++i)
2174 {
2175 elements.push_back(lower_byte_update_single_element(
2176 src,
2178 index_exprt{src.op(), from_integer(i, index_type)},
2179 *subtype_bits,
2180 i * *subtype_bits,
2183 ns));
2184 }
2185
2186 // copy the tail not affected by the update
2187 for(; i < num_elements; ++i)
2188 elements.push_back(index_exprt{src.op(), from_integer(i, index_type)});
2189
2190 if(is_array)
2191 return simplify_expr(
2192 array_exprt{std::move(elements), to_array_type(src.type())}, ns);
2193 else
2194 return simplify_expr(
2195 vector_exprt{std::move(elements), to_vector_type(src.type())}, ns);
2196}
2197
2208 const byte_update_exprt &src,
2211 const std::optional<exprt> &non_const_update_bound,
2212 const namespacet &ns)
2213{
2214 exprt::operandst elements;
2215 elements.reserve(struct_type.components().size());
2216
2218 for(const auto &comp : struct_type.components())
2219 {
2220 auto element_width = pointer_offset_bits(comp.type(), ns);
2221
2222 // compute the update offset relative to this struct member - will be
2223 // negative if we are already in the middle of the update or beyond it
2224 exprt offset = simplify_expr(
2226 mult_exprt{
2227 src.offset(),
2228 from_integer(src.get_bits_per_byte(), src.offset().type())},
2229 from_integer(member_offset_bits, src.offset().type())},
2230 ns);
2232 if(!offset_bits.has_value() || !element_width.has_value())
2233 {
2234 // The offset to update is not constant, either because the original
2235 // offset in src never was, or because a struct member has an unknown
2236 // offset. Abort the attempt to update individual struct members and
2237 // instead turn the operand-to-be-updated into a byte array, which we know
2238 // how to update even if the offset is non-constant.
2242
2243 auto src_size_opt = size_of_expr(src.type(), ns);
2244 CHECK_RETURN(src_size_opt.has_value());
2245
2248 src.op(),
2249 from_integer(0, src.offset().type()),
2250 src.get_bits_per_byte(),
2252
2253 byte_update_exprt bu = src;
2255 bu.type() = bu.op().type();
2256
2257 return lower_byte_extract(
2262 from_integer(0, src.offset().type()),
2263 src.get_bits_per_byte(),
2264 src.type()},
2265 ns);
2266 }
2267
2268 exprt member = member_exprt{src.op(), comp.get_name(), comp.type()};
2269
2270 // we don't need to update anything when
2271 // 1) the update offset is greater than the end of this member (thus the
2272 // relative offset is greater than this element) or
2273 // 2) the update offset plus the size of the update is less than the member
2274 // offset
2275 if(
2277 (value_as_byte_array.id() == ID_array && *offset_bits < 0 &&
2278 -*offset_bits >=
2279 value_as_byte_array.operands().size() * src.get_bits_per_byte()))
2280 {
2281 elements.push_back(member);
2283 continue;
2284 }
2285
2286 elements.push_back(lower_byte_update_single_element(
2287 src,
2289 member,
2294 ns));
2295
2297 }
2298
2299 return simplify_expr(struct_exprt{std::move(elements), struct_type}, ns);
2300}
2301
2312 const byte_update_exprt &src,
2313 const union_typet &union_type,
2315 const std::optional<exprt> &non_const_update_bound,
2316 const namespacet &ns)
2317{
2318 const auto widest_member = union_type.find_widest_union_component(ns);
2319
2321 widest_member.has_value(),
2322 "lower_byte_update of union of unknown size is not supported");
2323
2324 byte_update_exprt bu = src;
2325 bu.set_op(member_exprt{
2326 src.op(), widest_member->first.get_name(), widest_member->first.type()});
2327 bu.type() = widest_member->first.type();
2328
2329 return union_exprt{
2330 widest_member->first.get_name(),
2332 src.type()};
2333}
2334
2344 const byte_update_exprt &src,
2346 const std::optional<exprt> &non_const_update_bound,
2347 const namespacet &ns)
2348{
2349 if(src.type().id() == ID_array || src.type().id() == ID_vector)
2350 {
2351 std::optional<typet> subtype;
2352 if(src.type().id() == ID_vector)
2353 subtype = to_vector_type(src.type()).element_type();
2354 else
2355 subtype = to_array_type(src.type()).element_type();
2356
2357 auto element_width = pointer_offset_bits(*subtype, ns);
2358
2359 if(element_width.has_value() && *element_width == src.get_bits_per_byte())
2360 {
2361 if(value_as_byte_array.id() != ID_array)
2362 {
2364 non_const_update_bound.has_value(),
2365 "constant update bound should yield an array expression");
2367 src, *subtype, value_as_byte_array, *non_const_update_bound, ns);
2368 }
2369
2371 src,
2372 *subtype,
2375 ns);
2376 }
2377 else
2378 {
2380 src,
2381 *subtype,
2385 ns);
2386 }
2387 }
2388 else if(src.type().id() == ID_struct || src.type().id() == ID_struct_tag)
2389 {
2390 const struct_typet &struct_type =
2391 src.type().id() == ID_struct_tag
2393 : to_struct_type(src.type());
2396 result.type() = src.type();
2397 return result;
2398 }
2399 else if(src.type().id() == ID_union || src.type().id() == ID_union_tag)
2400 {
2401 const union_typet &union_type =
2402 src.type().id() == ID_union_tag
2403 ? ns.follow_tag(to_union_tag_type(src.type()))
2404 : to_union_type(src.type());
2407 result.type() = src.type();
2408 return result;
2409 }
2410 else if(
2412 src.type().id() == ID_c_enum || src.type().id() == ID_c_enum_tag)
2413 {
2414 // mask out the bits to be updated, shift the value according to the
2415 // offset and bit-or
2416 const auto type_width = pointer_offset_bits(src.type(), ns);
2417 CHECK_RETURN(type_width.has_value() && *type_width > 0);
2418 const std::size_t type_bits = numeric_cast_v<std::size_t>(*type_width);
2419
2421 if(value_as_byte_array.id() == ID_array)
2422 update_bytes = value_as_byte_array.operands();
2423 else
2424 {
2427 0,
2428 (type_bits + src.get_bits_per_byte() - 1) / src.get_bits_per_byte(),
2429 src.get_bits_per_byte(),
2430 ns);
2431 }
2432
2433 const std::size_t update_size_bits =
2434 update_bytes.size() * src.get_bits_per_byte();
2435 const std::size_t bit_width = std::max(type_bits, update_size_bits);
2436
2437 const bool is_little_endian = src.id() == ID_byte_update_little_endian;
2438
2440 typecast_exprt::conditional_cast(src.op(), bv_typet{type_bits});
2441 if(bit_width > type_bits)
2442 {
2444 bv_typet{bit_width - type_bits}.all_zeros_expr(),
2445 val_before,
2446 bv_typet{bit_width}};
2447
2448 if(!is_little_endian)
2450 .op0()
2451 .swap(to_concatenation_expr(val_before).op1());
2452 }
2453
2454 if(non_const_update_bound.has_value())
2455 {
2457 val_before,
2459 mp_integer{0},
2460 mp_integer{update_bytes.size()},
2461 src.get_bits_per_byte(),
2462 ns);
2464 CHECK_RETURN(src_as_bytes.operands().size() == update_bytes.size());
2465 for(std::size_t i = 0; i < update_bytes.size(); ++i)
2466 {
2470 ID_lt,
2472 update_bytes[i],
2473 src_as_bytes.operands()[i]};
2474 }
2475 }
2476
2477 // build mask
2478 exprt mask;
2480 mask = from_integer(power(2, update_size_bits) - 1, bv_typet{bit_width});
2481 else
2482 {
2483 mask = from_integer(
2484 power(2, bit_width) - power(2, bit_width - update_size_bits),
2485 bv_typet{bit_width});
2486 }
2487
2488 const typet &offset_type = src.offset().type();
2491
2494
2499 if(!is_little_endian)
2500 {
2501 mask_shifted.true_case().swap(mask_shifted.false_case());
2502 to_shift_expr(mask_shifted.true_case())
2503 .distance()
2504 .swap(to_shift_expr(mask_shifted.false_case()).distance());
2505 }
2506
2507 // original_bits &= ~mask
2509
2510 // concatenate and zero-extend the value
2513 std::reverse(value.operands().begin(), value.operands().end());
2514
2516 if(bit_width > update_size_bits)
2517 {
2519 zero_extended = zero_extend_exprt{value, bv_typet{bit_width}};
2520 else
2521 {
2522 // Big endian -- the zero is added as LSB.
2524 value,
2525 bv_typet{bit_width - update_size_bits}.all_zeros_expr(),
2526 bv_typet{bit_width}};
2527 }
2528 }
2529 else
2530 zero_extended = value;
2531
2532 // shift the value
2537 if(!is_little_endian)
2538 {
2539 value_shifted.true_case().swap(value_shifted.false_case());
2540 to_shift_expr(value_shifted.true_case())
2541 .distance()
2542 .swap(to_shift_expr(value_shifted.false_case()).distance());
2543 }
2544
2545 // original_bits |= newvalue
2547
2548 if(bit_width > type_bits)
2549 {
2550 endianness_mapt endianness_map(
2551 bitor_expr.type(), src.id() == ID_byte_update_little_endian, ns);
2552 const auto bounds = map_bounds(endianness_map, 0, type_bits - 1);
2553
2554 PRECONDITION(pointer_offset_bits(bitor_expr.type(), ns).has_value());
2555 return simplify_expr(
2558 src.type()),
2559 ns);
2560 }
2561
2562 return simplify_expr(
2564 }
2565 else
2566 {
2568 false, "lower_byte_update does not yet support ", src.type().id_string());
2569 }
2570}
2571
2573{
2577 "byte update expression should either be little or big endian");
2578
2579 // An update of a void-typed object or update by a void-typed value is the
2580 // source operand (this is questionable, but may arise when dereferencing
2581 // invalid pointers).
2582 if(src.type().id() == ID_empty || src.value().type().id() == ID_empty)
2583 return src.op();
2584
2585 // byte_update lowering proceeds as follows:
2586 // 1) Determine the size of the update, with the size of the object to be
2587 // updated as an upper bound. We fail if neither can be determined.
2588 // 2) Turn the update value into a byte array of the size determined above.
2589 // 3) If the offset is not constant, turn the object into a byte array, and
2590 // use a "with" expression to encode the update; else update the values in
2591 // place.
2592 // 4) Construct a new object.
2593 std::optional<exprt> non_const_update_bound;
2594 // update value, may require extension to full bytes
2595 exprt update_value = src.value();
2598 simplify(update_size_expr_opt.value(), ns);
2599
2603 const std::size_t bits_per_byte = src.get_bits_per_byte();
2604
2605 if(!update_size_expr_opt.value().is_constant())
2607 else
2608 {
2609 auto update_bits = pointer_offset_bits(update_value.type(), ns);
2610 // If the following invariant fails, then the type was only found to be
2611 // constant via simplification. Such instances should be fixed at the place
2612 // introducing these constant-but-not-constant_exprt type sizes.
2614 update_bits.has_value(), "constant size-of should imply fixed bit width");
2616
2618 {
2621 "non-byte aligned type expected to be a bitvector type");
2624 src.op(),
2626 plus_exprt{
2627 src.offset(),
2629 ns),
2630 src.get_bits_per_byte(),
2632 const exprt overlapping_byte =
2634
2637
2641 extra_bits,
2643 }
2644 else
2645 {
2648 }
2649 }
2650
2654 from_integer(0, src.offset().type()),
2655 src.get_bits_per_byte(),
2657
2659
2660 exprt result =
2662 return result;
2663}
2664
2666{
2667 exprt tmp = src;
2668
2669 // destroys any sharing, should use hash table
2670 Forall_operands(it, tmp)
2671 {
2672 *it = lower_byte_operators(*it, ns);
2673 }
2674
2675 if(
2678 {
2680 }
2681 else if(
2684 {
2686 }
2687 else
2688 return tmp;
2689}
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.
Definition arith_tools.cpp:21
mp_integer power(const mp_integer &base, const mp_integer &exponent)
A multi-precision implementation of the power operator.
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.
Expression classes for byte-level operators.
const byte_update_exprt & to_byte_update_expr(const exprt &expr)
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
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Definition ai.h:566
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
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
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
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
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition dstring.h:38
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
Array index operator.
Definition std_expr.h:1431
const std::string & id_string() const
Definition irep.h:391
const irep_idt & id() const
Definition irep.h:388
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
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
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
std::vector< componentt > componentst
Definition std_types.h:139
Expression to hold a symbol (variable)
Definition std_expr.h:132
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
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
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
const exprt & skip_typecast(const exprt &expr)
find the expression nested inside typecasts, if any
Definition expr_util.cpp:188
Deprecated expression utility functions.
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 array_exprt unpack_struct(const exprt &src, bool little_endian, const std::optional< mp_integer > &offset_bytes, const std::optional< mp_integer > &max_bytes, const std::size_t bits_per_byte, const namespacet &ns)
Rewrite a struct-typed expression 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...
bitvector_typet adjust_width(const typet &src, std::size_t new_width)
changes the width of the given bitvector type
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 void process_bit_fields(exprt::operandst &&bit_fields, std::size_t total_bits, exprt::operandst &dest, bool little_endian, const std::optional< mp_integer > &offset_bytes, const std::optional< mp_integer > &max_bytes, const std::size_t bits_per_byte, const namespacet &ns)
Extract bytes from a sequence of bitvector-typed elements.
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(const byte_update_exprt &src, const typet &subtype, const std::optional< mp_integer > &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_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 exprt lower_byte_extract_array_vector(const byte_extract_exprt &src, const byte_extract_exprt &unpacked, const typet &subtype, const mp_integer &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(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 unpack_array_vector(const exprt &src, const std::optional< mp_integer > &src_size, const mp_integer &element_bits, bool little_endian, const std::optional< mp_integer > &offset_bytes, const std::optional< mp_integer > &max_bytes, const std::size_t bits_per_byte, const namespacet &ns)
Rewrite an array or vector into its individual bytes.
static exprt unpack_rec(const exprt &src, bool little_endian, const std::optional< mp_integer > &offset_bytes, const std::optional< mp_integer > &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 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 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_single_element(const byte_update_exprt &src, const mp_integer &offset_bits, const exprt &element_to_update, const mp_integer &subtype_bits, const mp_integer &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.
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...
const pointer_typet & to_pointer_type(const typet &type)
Cast a typet to a pointer_typet.
Definition pointer_expr.h:93
std::optional< mp_integer > pointer_offset_bits(const typet &type, const namespacet &ns)
std::optional< mp_integer > 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.
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)
std::size_t lb
std::size_t ub
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

AltStyle によって変換されたページ (->オリジナル) /