|
12 | 12 |
|
13 | 13 | #include <solvers/smt2_incremental/construct_value_expr_from_smt.h> |
14 | 14 | #include <solvers/smt2_incremental/convert_expr_to_smt.h> |
| 15 | +#include <solvers/smt2_incremental/smt_array_theory.h> |
15 | 16 | #include <solvers/smt2_incremental/smt_commands.h> |
16 | 17 | #include <solvers/smt2_incremental/smt_core_theory.h> |
17 | 18 | #include <solvers/smt2_incremental/smt_responses.h> |
@@ -64,14 +65,42 @@ static std::vector<exprt> gather_dependent_expressions(const exprt &expr) |
64 | 65 | { |
65 | 66 | std::vector<exprt> dependent_expressions; |
66 | 67 | expr.visit_pre([&](const exprt &expr_node) { |
67 | | - if(can_cast_expr<symbol_exprt>(expr_node)) |
| 68 | + if( |
| 69 | + can_cast_expr<symbol_exprt>(expr_node) || |
| 70 | + can_cast_expr<array_exprt>(expr_node)) |
68 | 71 | { |
69 | 72 | dependent_expressions.push_back(expr_node); |
70 | 73 | } |
71 | 74 | }); |
72 | 75 | return dependent_expressions; |
73 | 76 | } |
74 | 77 |
|
| 78 | +void smt2_incremental_decision_proceduret::define_array_function( |
| 79 | + const array_exprt &array) |
| 80 | +{ |
| 81 | + const auto array_sort = |
| 82 | + convert_type_to_smt_sort(array.type()).cast<smt_array_sortt>(); |
| 83 | + INVARIANT( |
| 84 | + array_sort, |
| 85 | + "Converting array typed expression to SMT should result in a term of array " |
| 86 | + "sort."); |
| 87 | + const smt_identifier_termt array_identifier = smt_identifier_termt{ |
| 88 | + "array_" + std::to_string(array_sequence()), *array_sort}; |
| 89 | + solver_process->send(smt_declare_function_commandt{array_identifier, {}}); |
| 90 | + const std::vector<exprt> &elements = array.operands(); |
| 91 | + const std::size_t index_width = |
| 92 | + array_sort->index_sort().cast<smt_bit_vector_sortt>()->bit_width(); |
| 93 | + for(std::size_t i = 0; i < elements.size(); ++i) |
| 94 | + { |
| 95 | + const smt_assert_commandt element_definition{smt_core_theoryt::equal( |
| 96 | + smt_array_theoryt::select( |
| 97 | + array_identifier, smt_bit_vector_constant_termt{i, index_width}), |
| 98 | + convert_expr_to_smt(elements.at(i)))}; |
| 99 | + solver_process->send(element_definition); |
| 100 | + } |
| 101 | + expression_identifiers.emplace(array, array_identifier); |
| 102 | +} |
| 103 | + |
75 | 104 | /// \brief Defines any functions which \p expr depends on, which have not yet |
76 | 105 | /// been defined, along with their dependencies in turn. |
77 | 106 | void smt2_incremental_decision_proceduret::define_dependent_functions( |
@@ -123,10 +152,29 @@ void smt2_incremental_decision_proceduret::define_dependent_functions( |
123 | 152 | solver_process->send(function); |
124 | 153 | } |
125 | 154 | } |
| 155 | + if(const auto array_expr = expr_try_dynamic_cast<array_exprt>(current)) |
| 156 | + define_array_function(*array_expr); |
126 | 157 | to_be_defined.pop(); |
127 | 158 | } |
128 | 159 | } |
129 | 160 |
|
| 161 | +/// Replaces the sub expressions of \p expr which have been defined as separate |
| 162 | +/// functions in the smt solver, using the \p expression_identifiers map. |
| 163 | +static exprt substitute_identifiers( |
| 164 | + exprt expr, |
| 165 | + const std::unordered_map<exprt, smt_identifier_termt, irep_hash> |
| 166 | + &expression_identifiers) |
| 167 | +{ |
| 168 | + expr.visit_pre([&](exprt &node) -> void { |
| 169 | + auto find_result = expression_identifiers.find(node); |
| 170 | + if(find_result == expression_identifiers.cend()) |
| 171 | + return; |
| 172 | + const auto type = find_result->first.type(); |
| 173 | + node = symbol_exprt{find_result->second.identifier(), type}; |
| 174 | + }); |
| 175 | + return expr; |
| 176 | +} |
| 177 | + |
130 | 178 | smt2_incremental_decision_proceduret::smt2_incremental_decision_proceduret( |
131 | 179 | const namespacet &_ns, |
132 | 180 | std::unique_ptr<smt_base_solver_processt> _solver_process, |
@@ -164,15 +212,20 @@ void smt2_incremental_decision_proceduret::ensure_handle_for_expr_defined( |
164 | 212 | smt_termt |
165 | 213 | smt2_incremental_decision_proceduret::convert_expr_to_smt(const exprt &expr) |
166 | 214 | { |
167 | | - track_expression_objects(expr, ns, object_map); |
| 215 | + const exprt substituted = |
| 216 | + substitute_identifiers(expr, expression_identifiers); |
| 217 | + track_expression_objects(substituted, ns, object_map); |
168 | 218 | associate_pointer_sizes( |
169 | | - expr, |
| 219 | + substituted, |
170 | 220 | ns, |
171 | 221 | pointer_sizes_map, |
172 | 222 | object_map, |
173 | 223 | object_size_function.make_application); |
174 | 224 | return ::convert_expr_to_smt( |
175 | | - expr, object_map, pointer_sizes_map, object_size_function.make_application); |
| 225 | + substituted, |
| 226 | + object_map, |
| 227 | + pointer_sizes_map, |
| 228 | + object_size_function.make_application); |
176 | 229 | } |
177 | 230 |
|
178 | 231 | exprt smt2_incremental_decision_proceduret::handle(const exprt &expr) |
|
0 commit comments