cprover
boolbv.h
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module:
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
9 
10 #ifndef CPROVER_SOLVERS_FLATTENING_BOOLBV_H
11 #define CPROVER_SOLVERS_FLATTENING_BOOLBV_H
12 
13 //
14 // convert expression to boolean formula
15 //
16 
17 #include <util/byte_operators.h>
18 #include <util/endianness_map.h>
19 #include <util/expr.h>
20 #include <util/mp_arith.h>
21 #include <util/optional.h>
22 
24 
25 #include "bv_utils.h"
26 #include "boolbv_width.h"
27 #include "boolbv_map.h"
28 #include "arrays.h"
29 
31 class bswap_exprt;
33 class extractbit_exprt;
34 class extractbits_exprt;
37 class member_exprt;
38 class replication_exprt;
39 
40 class boolbvt:public arrayst
41 {
42 public:
44  const namespacet &_ns,
45  propt &_prop,
47  bool get_array_constraints = false)
50  bv_width(_ns),
51  bv_utils(_prop),
52  functions(*this),
53  map(_prop)
54  {
55  }
56 
57  virtual const bvt &convert_bv( // check cache
58  const exprt &expr,
59  const optionalt<std::size_t> expected_width = nullopt);
60 
61  virtual bvt convert_bitvector(const exprt &expr); // no cache
62 
63  // overloading
64  exprt get(const exprt &expr) const override;
65  void set_to(const exprt &expr, bool value) override;
66  void print_assignment(std::ostream &out) const override;
67 
68  void clear_cache() override
69  {
71  bv_cache.clear();
72  }
73 
74  void post_process() override
75  {
79  }
80 
81  enum class unbounded_arrayt { U_NONE, U_ALL, U_AUTO };
83 
85  {
86  return get_value(bv, 0, bv.size());
87  }
88 
89  mp_integer get_value(const bvt &bv, std::size_t offset, std::size_t width);
90 
91  const boolbv_mapt &get_map() const
92  {
93  return map;
94  }
95 
96  virtual std::size_t boolbv_width(const typet &type) const
97  {
98  return bv_width(type);
99  }
100 
101  virtual endianness_mapt
102  endianness_map(const typet &type, bool little_endian) const
103  {
104  return endianness_mapt{type, little_endian, ns};
105  }
106 
107 protected:
110 
111  // uninterpreted functions
113 
114  // the mapping from identifiers to literals
116 
117  // overloading
118  literalt convert_rest(const exprt &expr) override;
119  virtual bool boolbv_set_equality_to_true(const equal_exprt &expr);
120 
121  // NOLINTNEXTLINE(readability/identifiers)
122  typedef arrayst SUB;
123 
124  void conversion_failed(const exprt &expr, bvt &bv)
125  {
126  bv=conversion_failed(expr);
127  }
128 
129  bvt conversion_failed(const exprt &expr);
130 
131  typedef std::unordered_map<const exprt, bvt, irep_hash> bv_cachet;
133 
134  bool type_conversion(
135  const typet &src_type, const bvt &src,
136  const typet &dest_type, bvt &dest);
137 
139  virtual literalt convert_typecast(const typecast_exprt &expr);
140  virtual literalt convert_reduction(const unary_exprt &expr);
141  virtual literalt convert_onehot(const unary_exprt &expr);
142  virtual literalt convert_extractbit(const extractbit_exprt &expr);
143  virtual literalt convert_overflow(const exprt &expr);
144  virtual literalt convert_equality(const equal_exprt &expr);
146  const binary_relation_exprt &expr);
148  virtual literalt convert_quantifier(const quantifier_exprt &expr);
149 
150  virtual bvt convert_index(const exprt &array, const mp_integer &index);
151  virtual bvt convert_index(const index_exprt &expr);
152  virtual bvt convert_bswap(const bswap_exprt &expr);
153  virtual bvt convert_byte_extract(const byte_extract_exprt &expr);
154  virtual bvt convert_byte_update(const byte_update_exprt &expr);
155  virtual bvt convert_constraint_select_one(const exprt &expr);
156  virtual bvt convert_if(const if_exprt &expr);
157  virtual bvt convert_struct(const struct_exprt &expr);
158  virtual bvt convert_array(const exprt &expr);
159  virtual bvt convert_vector(const vector_exprt &expr);
160  virtual bvt convert_complex(const complex_exprt &expr);
161  virtual bvt convert_complex_real(const complex_real_exprt &expr);
162  virtual bvt convert_complex_imag(const complex_imag_exprt &expr);
164  virtual bvt convert_let(const let_exprt &);
165  virtual bvt convert_array_of(const array_of_exprt &expr);
166  virtual bvt convert_union(const union_exprt &expr);
167  virtual bvt convert_bv_typecast(const typecast_exprt &expr);
168  virtual bvt convert_add_sub(const exprt &expr);
169  virtual bvt convert_mult(const mult_exprt &expr);
170  virtual bvt convert_div(const div_exprt &expr);
171  virtual bvt convert_mod(const mod_exprt &expr);
172  virtual bvt convert_floatbv_op(const ieee_float_op_exprt &);
174  virtual bvt convert_member(const member_exprt &expr);
175  virtual bvt convert_with(const with_exprt &expr);
176  virtual bvt convert_update(const update_exprt &);
177  virtual bvt convert_case(const exprt &expr);
178  virtual bvt convert_cond(const cond_exprt &);
179  virtual bvt convert_shift(const binary_exprt &expr);
180  virtual bvt convert_bitwise(const exprt &expr);
181  virtual bvt convert_unary_minus(const unary_minus_exprt &expr);
182  virtual bvt convert_abs(const abs_exprt &expr);
183  virtual bvt convert_concatenation(const concatenation_exprt &expr);
184  virtual bvt convert_replication(const replication_exprt &expr);
185  virtual bvt convert_bv_literals(const exprt &expr);
186  virtual bvt convert_constant(const constant_exprt &expr);
187  virtual bvt convert_extractbits(const extractbits_exprt &expr);
188  virtual bvt convert_symbol(const exprt &expr);
189  virtual bvt convert_bv_reduction(const unary_exprt &expr);
190  virtual bvt convert_not(const not_exprt &expr);
191  virtual bvt convert_power(const binary_exprt &expr);
193  const function_application_exprt &expr);
194 
195  virtual exprt make_bv_expr(const typet &type, const bvt &bv);
196  virtual exprt make_free_bv_expr(const typet &type);
197 
198  void convert_with(
199  const typet &type,
200  const exprt &op1,
201  const exprt &op2,
202  const bvt &prev_bv,
203  bvt &next_bv);
204 
205  void convert_with_bv(
206  const exprt &op1,
207  const exprt &op2,
208  const bvt &prev_bv,
209  bvt &next_bv);
210 
211  void convert_with_array(
212  const array_typet &type,
213  const exprt &op1,
214  const exprt &op2,
215  const bvt &prev_bv,
216  bvt &next_bv);
217 
218  void convert_with_union(
219  const union_typet &type,
220  const exprt &op2,
221  const bvt &prev_bv,
222  bvt &next_bv);
223 
224  void convert_with_struct(
225  const struct_typet &type,
226  const exprt &op1,
227  const exprt &op2,
228  const bvt &prev_bv,
229  bvt &next_bv);
230 
231  void convert_update_rec(
232  const exprt::operandst &designator,
233  std::size_t d,
234  const typet &type,
235  std::size_t offset,
236  const exprt &new_value,
237  bvt &bv);
238 
239  virtual exprt bv_get_unbounded_array(const exprt &) const;
240 
241  virtual exprt bv_get_rec(
242  const exprt &expr,
243  const bvt &bv,
244  std::size_t offset,
245  const typet &type) const;
246 
247  exprt bv_get(const bvt &bv, const typet &type) const;
248  exprt bv_get_cache(const exprt &expr) const;
249 
250  // unbounded arrays
251  bool is_unbounded_array(const typet &type) const override;
252 
253  // quantifier instantiations
255  {
256  public:
258  : expr(std::move(_expr)), l(std::move(_l))
259  {
260  }
261 
264  };
265 
266  typedef std::list<quantifiert> quantifier_listt;
268 
270 
271  typedef std::vector<std::size_t> offset_mapt;
273 
274  // strings
276 };
277 
278 #endif // CPROVER_SOLVERS_FLATTENING_BOOLBV_H
Theory of Arrays with Extensionality.
Expression classes for byte-level operators.
Absolute value.
Definition: std_expr.h:347
Expression to define a mapping from an argument (index) to elements.
Definition: std_expr.h:3025
Array constructor from single element.
Definition: std_expr.h:1317
Arrays with given size.
Definition: std_types.h:968
Definition: arrays.h:31
void post_process() override
Definition: arrays.h:39
const namespacet & ns
Definition: arrays.h:54
message_handlert & message_handler
Definition: arrays.h:56
bool get_array_constraints
Definition: arrays.h:111
A base class for binary expressions.
Definition: std_expr.h:551
A base class for relations, i.e., binary predicates whose two operands have the same type.
Definition: std_expr.h:675
quantifiert(exprt _expr, literalt _l)
Definition: boolbv.h:257
Definition: boolbv.h:41
virtual bvt convert_bv_literals(const exprt &expr)
Definition: boolbv.cpp:277
virtual bvt convert_with(const with_exprt &expr)
Definition: boolbv_with.cpp:16
virtual bvt convert_array(const exprt &expr)
Flatten array.
void convert_with_bv(const exprt &op1, const exprt &op2, const bvt &prev_bv, bvt &next_bv)
virtual literalt convert_bv_rel(const binary_relation_exprt &)
Flatten <, >, <= and >= expressions.
bv_cachet bv_cache
Definition: boolbv.h:132
virtual literalt convert_onehot(const unary_exprt &expr)
virtual bvt convert_index(const exprt &array, const mp_integer &index)
index operator with constant index
virtual bvt convert_constraint_select_one(const exprt &expr)
void convert_update_rec(const exprt::operandst &designator, std::size_t d, const typet &type, std::size_t offset, const exprt &new_value, bvt &bv)
virtual exprt make_free_bv_expr(const typet &type)
Definition: boolbv.cpp:533
virtual bvt convert_complex_imag(const complex_imag_exprt &expr)
virtual literalt convert_reduction(const unary_exprt &expr)
virtual literalt convert_ieee_float_rel(const binary_relation_exprt &)
std::unordered_map< const exprt, bvt, irep_hash > bv_cachet
Definition: boolbv.h:131
virtual bvt convert_complex_real(const complex_real_exprt &expr)
virtual bvt convert_complex(const complex_exprt &expr)
virtual bvt convert_let(const let_exprt &)
Definition: boolbv_let.cpp:15
virtual bvt convert_byte_extract(const byte_extract_exprt &expr)
offset_mapt build_offset_map(const struct_typet &src)
Definition: boolbv.cpp:570
virtual bvt convert_bitvector(const exprt &expr)
Converts an expression into its gate-level representation and returns a vector of literals correspond...
Definition: boolbv.cpp:91
const boolbv_mapt & get_map() const
Definition: boolbv.h:91
unbounded_arrayt
Definition: boolbv.h:81
virtual literalt convert_typecast(const typecast_exprt &expr)
conversion from bitvector types to boolean
virtual bvt convert_bswap(const bswap_exprt &expr)
bool is_unbounded_array(const typet &type) const override
Definition: boolbv.cpp:543
virtual const bvt & convert_bv(const exprt &expr, const optionalt< std::size_t > expected_width=nullopt)
Convert expression to vector of literalts, using an internal cache to speed up conversion if availabl...
Definition: boolbv.cpp:38
exprt bv_get(const bvt &bv, const typet &type) const
Definition: boolbv_get.cpp:263
virtual bvt convert_mod(const mod_exprt &expr)
Definition: boolbv_mod.cpp:12
virtual bvt convert_add_sub(const exprt &expr)
virtual bvt convert_constant(const constant_exprt &expr)
numberingt< irep_idt > string_numbering
Definition: boolbv.h:275
void conversion_failed(const exprt &expr, bvt &bv)
Definition: boolbv.h:124
virtual literalt convert_quantifier(const quantifier_exprt &expr)
void post_process() override
Definition: boolbv.h:74
boolbvt(const namespacet &_ns, propt &_prop, message_handlert &message_handler, bool get_array_constraints=false)
Definition: boolbv.h:43
virtual bool boolbv_set_equality_to_true(const equal_exprt &expr)
Definition: boolbv.cpp:481
virtual exprt bv_get_unbounded_array(const exprt &) const
Definition: boolbv_get.cpp:280
void clear_cache() override
Definition: boolbv.h:68
virtual bvt convert_mult(const mult_exprt &expr)
Definition: boolbv_mult.cpp:13
virtual bvt convert_member(const member_exprt &expr)
virtual literalt convert_verilog_case_equality(const binary_relation_exprt &expr)
virtual bvt convert_function_application(const function_application_exprt &expr)
Definition: boolbv.cpp:322
virtual bvt convert_cond(const cond_exprt &)
Definition: boolbv_cond.cpp:13
void convert_with_struct(const struct_typet &type, const exprt &op1, const exprt &op2, const bvt &prev_bv, bvt &next_bv)
virtual bvt convert_if(const if_exprt &expr)
Definition: boolbv_if.cpp:12
virtual bvt convert_union(const union_exprt &expr)
void set_to(const exprt &expr, bool value) override
For a Boolean expression expr, add the constraint 'expr' if value is true, otherwise add 'not expr'.
Definition: boolbv.cpp:512
virtual bvt convert_byte_update(const byte_update_exprt &expr)
boolbv_widtht bv_width
Definition: boolbv.h:108
virtual literalt convert_extractbit(const extractbit_exprt &expr)
std::list< quantifiert > quantifier_listt
Definition: boolbv.h:266
unbounded_arrayt unbounded_array
Definition: boolbv.h:82
exprt get(const exprt &expr) const override
Return expr with variables replaced by values from satisfying assignment if available.
Definition: boolbv_get.cpp:20
virtual bvt convert_floatbv_op(const ieee_float_op_exprt &)
void print_assignment(std::ostream &out) const override
Print satisfying assignment to out.
Definition: boolbv.cpp:564
virtual bvt convert_not(const not_exprt &expr)
Definition: boolbv_not.cpp:12
exprt bv_get_cache(const exprt &expr) const
Definition: boolbv_get.cpp:268
virtual bvt convert_extractbits(const extractbits_exprt &expr)
virtual bvt convert_shift(const binary_exprt &expr)
virtual bvt convert_update(const update_exprt &)
virtual bvt convert_unary_minus(const unary_minus_exprt &expr)
virtual bvt convert_div(const div_exprt &expr)
Definition: boolbv_div.cpp:13
virtual bvt convert_array_of(const array_of_exprt &expr)
Flatten arrays constructed from a single element.
bv_utilst bv_utils
Definition: boolbv.h:109
virtual bvt convert_bitwise(const exprt &expr)
virtual bvt convert_array_comprehension(const array_comprehension_exprt &)
Definition: boolbv.cpp:235
void convert_with_union(const union_typet &type, const exprt &op2, const bvt &prev_bv, bvt &next_bv)
virtual bvt convert_bv_reduction(const unary_exprt &expr)
virtual literalt convert_equality(const equal_exprt &expr)
functionst functions
Definition: boolbv.h:112
virtual bvt convert_vector(const vector_exprt &expr)
quantifier_listt quantifier_list
Definition: boolbv.h:267
virtual endianness_mapt endianness_map(const typet &type, bool little_endian) const
Definition: boolbv.h:102
virtual std::size_t boolbv_width(const typet &type) const
Definition: boolbv.h:96
virtual bvt convert_case(const exprt &expr)
Definition: boolbv_case.cpp:13
virtual exprt make_bv_expr(const typet &type, const bvt &bv)
Definition: boolbv.cpp:522
virtual bvt convert_symbol(const exprt &expr)
Definition: boolbv.cpp:298
virtual literalt convert_overflow(const exprt &expr)
void convert_with_array(const array_typet &type, const exprt &op1, const exprt &op2, const bvt &prev_bv, bvt &next_bv)
Definition: boolbv_with.cpp:84
virtual bvt convert_floatbv_typecast(const floatbv_typecast_exprt &expr)
arrayst SUB
Definition: boolbv.h:122
virtual bvt convert_power(const binary_exprt &expr)
virtual exprt bv_get_rec(const exprt &expr, const bvt &bv, std::size_t offset, const typet &type) const
Definition: boolbv_get.cpp:45
virtual bvt convert_bv_typecast(const typecast_exprt &expr)
mp_integer get_value(const bvt &bv)
Definition: boolbv.h:84
bool type_conversion(const typet &src_type, const bvt &src, const typet &dest_type, bvt &dest)
virtual bvt convert_concatenation(const concatenation_exprt &expr)
virtual bvt convert_struct(const struct_exprt &expr)
void post_process_quantifiers()
virtual bvt convert_replication(const replication_exprt &expr)
literalt convert_rest(const exprt &expr) override
Definition: boolbv.cpp:333
std::vector< std::size_t > offset_mapt
Definition: boolbv.h:271
virtual bvt convert_abs(const abs_exprt &expr)
Definition: boolbv_abs.cpp:17
boolbv_mapt map
Definition: boolbv.h:115
The byte swap expression.
Expression of type type extracted from some object op starting at position offset (given in number of...
Expression corresponding to op() where the bytes starting at position offset (given in number of byte...
Complex constructor from a pair of numbers.
Definition: std_expr.h:1622
Imaginary part of the expression describing a complex number.
Definition: std_expr.h:1735
Real part of the expression describing a complex number.
Definition: std_expr.h:1690
Concatenation of bit-vector operands.
this is a parametric version of an if-expression: it returns the value of the first case (using the o...
Definition: std_expr.h:2962
A constant literal expression.
Definition: std_expr.h:2668
Division.
Definition: std_expr.h:981
Maps a big-endian offset to a little-endian offset.
Equality.
Definition: std_expr.h:1140
Base class for all expressions.
Definition: expr.h:54
std::vector< exprt > operandst
Definition: expr.h:56
Extracts a single bit of a bit-vector operand.
Extracts a sub-range of a bit-vector operand.
Semantic type conversion from/to floating-point formats.
Definition: floatbv_expr.h:19
Application of (mathematical) function.
virtual void post_process()
Definition: functions.h:36
IEEE floating-point operations These have two data operands (op0 and op1) and one rounding mode (op2)...
Definition: floatbv_expr.h:364
The trinary if-then-else operator.
Definition: std_expr.h:2087
Array index operator.
Definition: std_expr.h:1243
A let expression.
Definition: std_expr.h:2816
Extract member of struct or union.
Definition: std_expr.h:2528
Modulo.
Definition: std_expr.h:1050
Binary multiplication Associativity is not specified.
Definition: std_expr.h:936
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:92
Boolean negation.
Definition: std_expr.h:2042
virtual void clear_cache()
TO_BE_DOCUMENTED.
Definition: prop.h:25
A base class for quantifier expressions.
Bit-vector replication.
Struct constructor from list of elements.
Definition: std_expr.h:1583
Structure type, corresponds to C style structs.
Definition: std_types.h:226
Semantic type conversion.
Definition: std_expr.h:1781
The type of an expression, extends irept.
Definition: type.h:28
Generic base class for unary expressions.
Definition: std_expr.h:282
The unary minus expression.
Definition: std_expr.h:391
Union constructor from single element.
Definition: std_expr.h:1517
The union type.
Definition: std_types.h:393
Operator to update elements in structs and arrays.
Definition: std_expr.h:2357
Vector constructor from list of elements.
Definition: std_expr.h:1481
Operator to update elements in structs and arrays.
Definition: std_expr.h:2173
Uninterpreted Functions.
std::vector< literalt > bvt
Definition: literal.h:201
BigInt mp_integer
Definition: mp_arith.h:19
nonstd::optional< T > optionalt
Definition: optional.h:35