CBMC
boolbv_byte_update.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module:
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
9 #include "boolbv.h"
10 
11 #include <util/arith_tools.h>
12 #include <util/byte_operators.h>
13 #include <util/invariant.h>
14 
16 {
17  // if we update (from) an unbounded array, lower the expression as the array
18  // logic does not handle byte operators
19  if(
20  is_unbounded_array(expr.op().type()) ||
21  is_unbounded_array(expr.value().type()))
22  {
23  return convert_bv(lower_byte_update(expr, ns));
24  }
25 
26  const exprt &op = expr.op();
27  const exprt &offset_expr=expr.offset();
28  const exprt &value=expr.value();
29 
31  expr.id() == ID_byte_update_little_endian ||
32  expr.id() == ID_byte_update_big_endian);
33  const bool little_endian = expr.id() == ID_byte_update_little_endian;
34 
35  bvt bv=convert_bv(op);
36 
37  const bvt &value_bv=convert_bv(value);
38  std::size_t update_width=value_bv.size();
39  std::size_t byte_width = expr.get_bits_per_byte();
40 
41  if(update_width>bv.size())
42  update_width=bv.size();
43 
44  // see if the byte number is constant
45 
46  const auto index = numeric_cast<mp_integer>(offset_expr);
47  if(index.has_value())
48  {
49  // yes!
50  const mp_integer offset = *index * byte_width;
51 
52  if(offset+update_width>mp_integer(bv.size()) || offset<0)
53  {
54  // out of bounds
55  }
56  else
57  {
58  endianness_mapt map_op = endianness_map(op.type(), little_endian);
59  endianness_mapt map_value = endianness_map(value.type(), little_endian);
60 
61  const std::size_t offset_i = numeric_cast_v<std::size_t>(offset);
62 
63  for(std::size_t i = 0; i < update_width; i++)
64  {
65  size_t index_op = map_op.map_bit(offset_i + i);
66  size_t index_value = map_value.map_bit(i);
67 
68  INVARIANT(
69  index_op < bv.size(), "bit vector index shall be within bounds");
70  INVARIANT(
71  index_value < value_bv.size(),
72  "bit vector index shall be within bounds");
73 
74  bv[index_op] = value_bv[index_value];
75  }
76  }
77 
78  return bv;
79  }
80 
81  // byte_update with variable index
82  for(std::size_t offset=0; offset<bv.size(); offset+=byte_width)
83  {
84  // index condition
86  offset_expr, from_integer(offset / byte_width, offset_expr.type()));
87  literalt equal=convert(equality);
88 
89  endianness_mapt map_op = endianness_map(op.type(), little_endian);
90  endianness_mapt map_value = endianness_map(value.type(), little_endian);
91 
92  for(std::size_t bit=0; bit<update_width; bit++)
93  if(offset+bit<bv.size())
94  {
95  std::size_t bv_o=map_op.map_bit(offset+bit);
96  std::size_t value_bv_o=map_value.map_bit(bit);
97 
98  bv[bv_o]=prop.lselect(equal, value_bv[value_bv_o], bv[bv_o]);
99  }
100  }
101 
102  return bv;
103 }
constant_exprt from_integer(const mp_integer &int_value, const typet &type)
Expression classes for byte-level operators.
exprt lower_byte_update(const byte_update_exprt &src, const namespacet &ns)
Rewrite a byte update expression to more fundamental operations.
const namespacet & ns
Definition: arrays.h:56
virtual const bvt & convert_bv(const exprt &expr, const std::optional< std::size_t > expected_width={})
Convert expression to vector of literalts, using an internal cache to speed up conversion if availabl...
Definition: boolbv.cpp:39
bool is_unbounded_array(const typet &type) const override
Definition: boolbv.cpp:533
virtual bvt convert_byte_update(const byte_update_exprt &expr)
virtual endianness_mapt endianness_map(const typet &type, bool little_endian) const
Definition: boolbv.h:108
Expression corresponding to op() where the bytes starting at position offset (given in number of byte...
const exprt & value() const
const exprt & op() const
const exprt & offset() const
std::size_t get_bits_per_byte() const
Maps a big-endian offset to a little-endian offset.
size_t map_bit(size_t bit) const
Equality.
Definition: std_expr.h:1361
virtual literalt equality(const exprt &e1, const exprt &e2)
Definition: equality.cpp:17
Base class for all expressions.
Definition: expr.h:56
typet & type()
Return the type of the expression.
Definition: expr.h:84
const irep_idt & id() const
Definition: irep.h:388
literalt convert(const exprt &expr) override
Convert a Boolean expression and return the corresponding literal.
virtual literalt lselect(literalt a, literalt b, literalt c)=0
std::vector< literalt > bvt
Definition: literal.h:201
BigInt mp_integer
Definition: smt_terms.h:17
#define PRECONDITION(CONDITION)
Definition: invariant.h:463