CBMC
smt2_conv.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_SMT2_SMT2_CONV_H
11 #define CPROVER_SOLVERS_SMT2_SMT2_CONV_H
12 
13 #include <util/pointer_expr.h>
14 #include <util/std_expr.h>
15 #include <util/threeval.h>
16 
17 #include <map>
18 #include <set>
19 #include <sstream>
20 
21 #if !HASH_CODE
23 #endif
24 
27 #include <solvers/prop/literal.h>
29 
30 #include "letify.h"
31 
34 class union_typet;
35 class update_bit_exprt;
36 class update_bits_exprt;
37 
39 {
40 public:
41  enum class solvert
42  {
43  GENERIC,
44  BITWUZLA,
45  BOOLECTOR,
47  CVC3,
48  CVC4,
49  CVC5,
50  MATHSAT,
51  YICES,
52  Z3
53  };
54 
55  smt2_convt(
56  const namespacet &_ns,
57  const std::string &_benchmark,
58  const std::string &_notes,
59  const std::string &_logic,
60  solvert _solver,
61  std::ostream &_out);
62 
63  ~smt2_convt() override = default;
64 
72 
73  exprt handle(const exprt &expr) override;
74  void set_to(const exprt &expr, bool value) override;
75  exprt get(const exprt &expr) const override;
76  std::string decision_procedure_text() const override;
77  void print_assignment(std::ostream &out) const override;
78 
80  void push() override;
81 
83  void push(const std::vector<exprt> &_assumptions) override;
84 
86  void pop() override;
87 
88  std::size_t get_number_of_solver_calls() const override;
89 
90  static std::string convert_identifier(const irep_idt &identifier);
91 
92  void set_converter(irep_idt id, std::function<void(const exprt &)> converter)
93  {
94  converters[id] = std::move(converter);
95  }
96 
97 protected:
98  const namespacet &ns;
99  std::ostream &out;
100  std::string benchmark, notes, logic;
102  using converterst = std::
103  unordered_map<irep_idt, std::function<void(const exprt &)>, irep_id_hash>;
105 
106  std::vector<literalt> assumptions;
108 
109  std::size_t number_of_solver_calls = 0;
110 
111  resultt dec_solve(const exprt &) override;
112 
113  void write_header();
123  void write_footer();
124 
125  // tweaks for arrays
126  bool use_array_theory(const exprt &);
127  void flatten_array(const exprt &);
128 
129  // specific expressions go here
130  void convert_typecast(const typecast_exprt &expr);
132  void convert_struct(const struct_exprt &expr);
133  void convert_union(const union_exprt &expr);
134  void convert_constant(const constant_exprt &expr);
137  void convert_plus(const plus_exprt &expr);
138  void convert_minus(const minus_exprt &expr);
139  void convert_div(const div_exprt &expr);
140  void convert_mult(const mult_exprt &expr);
141  void convert_rounding_mode_FPA(const exprt &expr);
142  void convert_floatbv_plus(const ieee_float_op_exprt &expr);
143  void convert_floatbv_minus(const ieee_float_op_exprt &expr);
144  void convert_floatbv_div(const ieee_float_op_exprt &expr);
145  void convert_floatbv_mult(const ieee_float_op_exprt &expr);
146  void convert_floatbv_rem(const binary_exprt &expr);
147  void convert_mod(const mod_exprt &expr);
148  void convert_euclidean_mod(const euclidean_mod_exprt &expr);
149  void convert_index(const index_exprt &expr);
150  void convert_member(const member_exprt &expr);
151 
152  void convert_with(const with_exprt &expr);
153  void convert_update(const update_exprt &);
154  void convert_update_bit(const update_bit_exprt &);
156 
157  void convert_expr(const exprt &);
158  void convert_type(const typet &);
159  void convert_literal(const literalt);
160  void convert_string_literal(const std::string &);
161 
162  literalt convert(const exprt &expr);
163  tvt l_get(literalt l) const;
164 
165  // auxiliary methods
166  exprt prepare_for_convert_expr(const exprt &expr);
167  exprt lower_byte_operators(const exprt &expr);
168  void find_symbols(const exprt &expr);
169  void find_symbols(const typet &type);
170  void find_symbols_rec(const typet &type, std::set<irep_idt> &recstack);
171 
172  // letification
174 
175  // Parsing solver responses
176  constant_exprt parse_literal(const irept &, const typet &type);
177  struct_exprt parse_struct(const irept &s, const struct_typet &type);
178  exprt parse_union(const irept &s, const union_typet &type);
184  exprt parse_array(const irept &s, const array_typet &type);
185  exprt parse_rec(const irept &s, const typet &type);
192  void walk_array_tree(
193  std::unordered_map<int64_t, exprt> *operands_map,
194  const irept &src,
195  const array_typet &type);
196  std::unordered_map<irep_idt, irept> current_bindings;
197 
198  // we use this to build a bit-vector encoding of the FPA theory
199  void convert_floatbv(const exprt &expr);
200  std::string type2id(const typet &) const;
201  std::string floatbv_suffix(const exprt &) const;
202  std::set<irep_idt> bvfp_set; // already converted
203 
204  // conversion for ID_evaluate, ID_update_state,
205  // ID_object_address, ID_field_address, ID_element_address
206  std::set<irep_idt> state_fkt_declared;
207 
209  {
210  public:
211  smt2_symbolt(const irep_idt &_identifier, const typet &_type)
212  : nullary_exprt(ID_smt2_symbol, _type)
213  { set(ID_identifier, _identifier); }
214 
215  const irep_idt &get_identifier() const
216  {
217  return get(ID_identifier);
218  }
219  };
220 
221  const smt2_symbolt &to_smt2_symbol(const exprt &expr)
222  {
223  PRECONDITION(expr.id() == ID_smt2_symbol && !expr.has_operands());
224  return static_cast<const smt2_symbolt&>(expr);
225  }
226 
227  // flattens any non-bitvector type into a bitvector,
228  // e.g., booleans, vectors, structs, arrays but also
229  // floats when using the FPA theory.
230  // unflatten() does the opposite.
231  enum class wheret { BEGIN, END };
232  void flatten2bv(const exprt &);
233  void unflatten(wheret, const typet &, unsigned nesting=0);
234 
235  // pointers
238  const exprt &expr, const pointer_typet &result_type);
239 
240  void define_object_size(const irep_idt &id, const object_size_exprt &expr);
241 
242  // keeps track of all non-Boolean symbols and their value
243  struct identifiert
244  {
245  bool is_bound;
248 
250  {
251  type.make_nil();
252  value.make_nil();
253  }
254  };
255 
256  typedef std::unordered_map<irep_idt, identifiert> identifier_mapt;
257 
259 
260  // for modeling structs as SMT datatype when use_datatype is set
261  //
262  // it maintains a map of `struct_typet` or `struct_tag_typet`
263  // to datatype names declared in SMT
264  typedef std::map<typet, std::string> datatype_mapt;
266 
267  // for replacing various defined expressions:
268  //
269  // ID_array_of
270  // ID_array
271  // ID_string_constant
272 
273  typedef std::map<exprt, irep_idt> defined_expressionst;
278  std::unordered_map<irep_idt, bool> set_values;
279 
280  std::map<object_size_exprt, irep_idt> object_sizes;
281 
282  typedef std::set<std::string> smt2_identifierst;
284 
285  // Boolean part
286  std::size_t no_boolean_variables;
287  std::vector<bool> boolean_assignment;
288 };
289 
290 #endif // CPROVER_SOLVERS_SMT2_SMT2_CONV_H
Arrays with given size.
Definition: std_types.h:807
A base class for binary expressions.
Definition: std_expr.h:638
A base class for relations, i.e., binary predicates whose two operands have the same type.
Definition: std_expr.h:762
A constant literal expression.
Definition: std_expr.h:2987
resultt
Result of running the decision procedure.
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
Boute's Euclidean definition of Modulo – to match SMT-LIB2.
Definition: std_expr.h:1291
Base class for all expressions.
Definition: expr.h:56
bool has_operands() const
Return true if there is at least one operand.
Definition: expr.h:91
Semantic type conversion from/to floating-point formats.
Definition: floatbv_expr.h:19
IEEE floating-point operations These have two data operands (op0 and op1) and one rounding mode (op2)...
Definition: floatbv_expr.h:364
Array index operator.
Definition: std_expr.h:1465
There are a large number of kinds of tree structured or tree-like data in CPROVER.
Definition: irep.h:360
const irep_idt & get(const irep_idt &name) const
Definition: irep.cpp:44
void set(const irep_idt &name, const irep_idt &value)
Definition: irep.h:408
const irep_idt & id() const
Definition: irep.h:384
void make_nil()
Definition: irep.h:442
Introduce LET for common subexpressions.
Definition: letify.h:16
Extract member of struct or union.
Definition: std_expr.h:2841
Binary minus.
Definition: std_expr.h:1061
Modulo defined as lhs-(rhs * truncate(lhs/rhs)).
Definition: std_expr.h:1223
Binary multiplication Associativity is not specified.
Definition: std_expr.h:1107
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:94
An expression without operands.
Definition: std_expr.h:22
Expression for finding the size (in bytes) of the object a pointer points to.
The plus expression Associativity is not specified.
Definition: std_expr.h:1002
The pointer type These are both 'bitvector_typet' (they have a width) and 'type_with_subtypet' (they ...
Definition: pointer_expr.h:24
smt2_symbolt(const irep_idt &_identifier, const typet &_type)
Definition: smt2_conv.h:211
const irep_idt & get_identifier() const
Definition: smt2_conv.h:215
void convert_relation(const binary_relation_exprt &)
Definition: smt2_conv.cpp:3521
bool use_lambda_for_array
Definition: smt2_conv.h:70
void convert_type(const typet &)
Definition: smt2_conv.cpp:5463
void unflatten(wheret, const typet &, unsigned nesting=0)
Definition: smt2_conv.cpp:4551
bool use_array_theory(const exprt &)
Definition: smt2_conv.cpp:5451
void find_symbols(const exprt &expr)
Definition: smt2_conv.cpp:4896
std::map< exprt, irep_idt > defined_expressionst
Definition: smt2_conv.h:273
std::size_t number_of_solver_calls
Definition: smt2_conv.h:109
void convert_typecast(const typecast_exprt &expr)
Definition: smt2_conv.cpp:2491
~smt2_convt() override=default
void write_footer()
Writes the end of the SMT file to the smt_convt::out stream.
Definition: smt2_conv.cpp:205
tvt l_get(literalt l) const
Definition: smt2_conv.cpp:160
std::string benchmark
Definition: smt2_conv.h:100
void convert_floatbv_rem(const binary_exprt &expr)
Definition: smt2_conv.cpp:4084
std::map< typet, std::string > datatype_mapt
Definition: smt2_conv.h:264
std::unordered_map< irep_idt, irept > current_bindings
Definition: smt2_conv.h:196
bool use_FPA_theory
Definition: smt2_conv.h:65
resultt dec_solve(const exprt &) override
Implementation of the decision procedure.
Definition: smt2_conv.cpp:320
std::set< irep_idt > bvfp_set
Definition: smt2_conv.h:202
void convert_address_of_rec(const exprt &expr, const pointer_typet &result_type)
Definition: smt2_conv.cpp:783
void push() override
Unimplemented.
Definition: smt2_conv.cpp:981
void convert_is_dynamic_object(const unary_exprt &)
Definition: smt2_conv.cpp:3484
void convert_literal(const literalt)
Definition: smt2_conv.cpp:958
void convert_floatbv_div(const ieee_float_op_exprt &expr)
Definition: smt2_conv.cpp:3969
void convert_string_literal(const std::string &)
Definition: smt2_conv.cpp:1149
std::size_t get_number_of_solver_calls() const override
Return the number of incremental solver calls.
Definition: smt2_conv.cpp:5794
const namespacet & ns
Definition: smt2_conv.h:98
void convert_floatbv_mult(const ieee_float_op_exprt &expr)
Definition: smt2_conv.cpp:4064
boolbv_widtht boolbv_width
Definition: smt2_conv.h:107
void convert_constant(const constant_exprt &expr)
Definition: smt2_conv.cpp:3300
std::string floatbv_suffix(const exprt &) const
Definition: smt2_conv.cpp:1108
std::unordered_map< irep_idt, identifiert > identifier_mapt
Definition: smt2_conv.h:256
void flatten2bv(const exprt &)
Definition: smt2_conv.cpp:4462
std::string notes
Definition: smt2_conv.h:100
void convert_div(const div_exprt &expr)
Definition: smt2_conv.cpp:3925
std::ostream & out
Definition: smt2_conv.h:99
exprt lower_byte_operators(const exprt &expr)
Lower byte_update and byte_extract operations within expr.
Definition: smt2_conv.cpp:4825
std::string type2id(const typet &) const
Definition: smt2_conv.cpp:1052
bool emit_set_logic
Definition: smt2_conv.h:71
void convert_rounding_mode_FPA(const exprt &expr)
Converting a constant or symbolic rounding mode to SMT-LIB.
Definition: smt2_conv.cpp:3734
void convert_floatbv_typecast(const floatbv_typecast_exprt &expr)
Definition: smt2_conv.cpp:3024
struct_exprt parse_struct(const irept &s, const struct_typet &type)
Definition: smt2_conv.cpp:629
std::string logic
Definition: smt2_conv.h:100
void convert_mult(const mult_exprt &expr)
Definition: smt2_conv.cpp:3989
void convert_update_bit(const update_bit_exprt &)
Definition: smt2_conv.cpp:4321
exprt prepare_for_convert_expr(const exprt &expr)
Perform steps necessary before an expression is passed to convert_expr.
Definition: smt2_conv.cpp:4857
exprt get(const exprt &expr) const override
Return expr with variables replaced by values from satisfying assignment if available.
Definition: smt2_conv.cpp:335
std::string decision_procedure_text() const override
Return a textual description of the decision procedure.
Definition: smt2_conv.cpp:145
void convert_floatbv_minus(const ieee_float_op_exprt &expr)
Definition: smt2_conv.cpp:3905
bool use_check_sat_assuming
Definition: smt2_conv.h:68
std::map< object_size_exprt, irep_idt > object_sizes
Definition: smt2_conv.h:280
void define_object_size(const irep_idt &id, const object_size_exprt &expr)
Definition: smt2_conv.cpp:284
bool use_datatypes
Definition: smt2_conv.h:69
datatype_mapt datatype_map
Definition: smt2_conv.h:265
void convert_mod(const mod_exprt &expr)
Definition: smt2_conv.cpp:3465
static std::string convert_identifier(const irep_idt &identifier)
Definition: smt2_conv.cpp:1017
void convert_floatbv_plus(const ieee_float_op_exprt &expr)
Definition: smt2_conv.cpp:3791
void convert_struct(const struct_exprt &expr)
Definition: smt2_conv.cpp:3168
std::unordered_map< irep_idt, bool > set_values
The values which boolean identifiers have been smt2_convt::set_to or in other words those which are a...
Definition: smt2_conv.h:278
smt2_convt(const namespacet &_ns, const std::string &_benchmark, const std::string &_notes, const std::string &_logic, solvert _solver, std::ostream &_out)
Definition: smt2_conv.cpp:56
void convert_member(const member_exprt &expr)
Definition: smt2_conv.cpp:4401
void convert_euclidean_mod(const euclidean_mod_exprt &expr)
Definition: smt2_conv.cpp:3450
void convert_index(const index_exprt &expr)
Definition: smt2_conv.cpp:4331
converterst converters
Definition: smt2_conv.h:104
pointer_logict pointer_logic
Definition: smt2_conv.h:236
exprt handle(const exprt &expr) override
Generate a handle, which is an expression that has the same value as the argument in any model that i...
Definition: smt2_conv.cpp:949
void print_assignment(std::ostream &out) const override
Print satisfying assignment to out.
Definition: smt2_conv.cpp:150
void walk_array_tree(std::unordered_map< int64_t, exprt > *operands_map, const irept &src, const array_typet &type)
This function walks the SMT output and populates a map with index/value pairs for the array.
Definition: smt2_conv.cpp:575
std::set< std::string > smt2_identifierst
Definition: smt2_conv.h:282
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: smt2_conv.cpp:4673
bool use_as_const
Definition: smt2_conv.h:67
exprt parse_rec(const irept &s, const typet &type)
Definition: smt2_conv.cpp:698
void convert_union(const union_exprt &expr)
Definition: smt2_conv.cpp:3271
exprt parse_union(const irept &s, const union_typet &type)
Definition: smt2_conv.cpp:613
void set_converter(irep_idt id, std::function< void(const exprt &)> converter)
Definition: smt2_conv.h:92
exprt parse_array(const irept &s, const array_typet &type)
This function is for parsing array output from SMT solvers when "(get-value |???|)" returns an array ...
Definition: smt2_conv.cpp:531
std::vector< bool > boolean_assignment
Definition: smt2_conv.h:287
void flatten_array(const exprt &)
produce a flat bit-vector for a given array of fixed size
Definition: smt2_conv.cpp:3240
void convert_with(const with_exprt &expr)
Definition: smt2_conv.cpp:4107
letifyt letify
Definition: smt2_conv.h:173
bool use_array_of_bool
Definition: smt2_conv.h:66
std::vector< literalt > assumptions
Definition: smt2_conv.h:106
void convert_plus(const plus_exprt &expr)
Definition: smt2_conv.cpp:3628
defined_expressionst defined_expressions
Definition: smt2_conv.h:274
void pop() override
Currently, only implements a single stack element (no nested contexts)
Definition: smt2_conv.cpp:995
void convert_update_bits(const update_bits_exprt &)
Definition: smt2_conv.cpp:4326
void find_symbols_rec(const typet &type, std::set< irep_idt > &recstack)
Definition: smt2_conv.cpp:5593
void convert_update(const update_exprt &)
Definition: smt2_conv.cpp:4314
void write_header()
Definition: smt2_conv.cpp:173
std::set< irep_idt > state_fkt_declared
Definition: smt2_conv.h:206
solvert solver
Definition: smt2_conv.h:101
identifier_mapt identifier_map
Definition: smt2_conv.h:258
void convert_minus(const minus_exprt &expr)
Definition: smt2_conv.cpp:3826
void convert_expr(const exprt &)
Definition: smt2_conv.cpp:1162
constant_exprt parse_literal(const irept &, const typet &type)
Definition: smt2_conv.cpp:394
std::size_t no_boolean_variables
Definition: smt2_conv.h:286
smt2_identifierst smt2_identifiers
Definition: smt2_conv.h:283
void convert_floatbv(const exprt &expr)
Definition: smt2_conv.cpp:1115
literalt convert(const exprt &expr)
Definition: smt2_conv.cpp:893
const smt2_symbolt & to_smt2_symbol(const exprt &expr)
Definition: smt2_conv.h:221
std::unordered_map< irep_idt, std::function< void(const exprt &)>, irep_id_hash > converterst
Definition: smt2_conv.h:103
Struct constructor from list of elements.
Definition: std_expr.h:1872
Structure type, corresponds to C style structs.
Definition: std_types.h:231
Definition: threeval.h:20
Semantic type conversion.
Definition: std_expr.h:2068
The type of an expression, extends irept.
Definition: type.h:29
Generic base class for unary expressions.
Definition: std_expr.h:361
Union constructor from single element.
Definition: std_expr.h:1765
The union type.
Definition: c_types.h:147
Replaces a sub-range of a bit-vector operand.
Replaces a sub-range of a bit-vector operand.
Operator to update elements in structs and arrays.
Definition: std_expr.h:2655
Operator to update elements in structs and arrays.
Definition: std_expr.h:2471
IREP Hash Container.
API to expression classes for Pointers.
Pointer Logic.
#define PRECONDITION(CONDITION)
Definition: invariant.h:463
Decision procedure with incremental solving with contexts and assumptions.
API to expression classes.
dstringt irep_idt