-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathsmt2_dec.cpp
249 lines (209 loc) · 7.17 KB
/
smt2_dec.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*******************************************************************\
Module:
Author: Daniel Kroening, [email protected]
\*******************************************************************/
#include "smt2_dec.h"
#include <util/invariant.h>
#include <util/message.h>
#include <util/run.h>
#include <util/tempfile.h>
#include "smt2irep.h"
std::string smt2_dect::decision_procedure_text() const
{
// clang-format off
return "SMT2 " + logic + (use_FPA_theory ? " (with FPA)" : "") + " using " +
(solver==solvert::GENERIC?"Generic":
solver==solvert::BITWUZLA?"Bitwuzla":
solver==solvert::BOOLECTOR?"Boolector":
solver==solvert::CPROVER_SMT2?"CPROVER SMT2":
solver==solvert::CVC3?"CVC3":
solver==solvert::CVC4?"CVC4":
solver==solvert::CVC5?"CVC5":
solver==solvert::MATHSAT?"MathSAT":
solver==solvert::YICES?"Yices":
solver==solvert::Z3?"Z3":
"(unknown)");
// clang-format on
}
decision_proceduret::resultt smt2_dect::dec_solve(const exprt &assumption)
{
++number_of_solver_calls;
temporary_filet temp_file_problem("smt2_dec_problem_", ""),
temp_file_stdout("smt2_dec_stdout_", ""),
temp_file_stderr("smt2_dec_stderr_", "");
const auto write_problem_to_file = [&](std::ofstream problem_out) {
cached_output << stringstream.str();
stringstream.str(std::string{});
write_footer();
problem_out << cached_output.str() << stringstream.str();
stringstream.str(std::string{});
};
write_problem_to_file(std::ofstream(
temp_file_problem(), std::ios_base::out | std::ios_base::trunc));
std::vector<std::string> argv;
std::string stdin_filename;
switch(solver)
{
case solvert::BITWUZLA:
argv = {"bitwuzla", temp_file_problem()};
break;
case solvert::BOOLECTOR:
argv = {"boolector", "--smt2", temp_file_problem(), "-m"};
break;
case solvert::CPROVER_SMT2:
argv = {"smt2_solver"};
stdin_filename = temp_file_problem();
break;
case solvert::CVC3:
argv = {"cvc3",
"+model",
"-lang",
"smtlib",
"-output-lang",
"smtlib",
temp_file_problem()};
break;
case solvert::CVC4:
// The flags --bitblast=eager --bv-div-zero-const help but only
// work for pure bit-vector formulas.
argv = {"cvc4", "-L", "smt2", temp_file_problem()};
break;
case solvert::CVC5:
argv = {"cvc5", "--lang", "smtlib", temp_file_problem()};
break;
case solvert::MATHSAT:
// The options below were recommended by Alberto Griggio
// on 10 July 2013
argv = {"mathsat",
"-input=smt2",
"-preprocessor.toplevel_propagation=true",
"-preprocessor.simplification=7",
"-dpll.branching_random_frequency=0.01",
"-dpll.branching_random_invalidate_phase_cache=true",
"-dpll.restart_strategy=3",
"-dpll.glucose_var_activity=true",
"-dpll.glucose_learnt_minimization=true",
"-theory.bv.eager=true",
"-theory.bv.bit_blast_mode=1",
"-theory.bv.delay_propagated_eqs=true",
"-theory.fp.mode=1",
"-theory.fp.bit_blast_mode=2",
"-theory.arr.mode=1"};
stdin_filename = temp_file_problem();
break;
case solvert::YICES:
// command = "yices -smt -e " // Calling convention for older versions
// Convention for 2.2.1
argv = {"yices-smt2", temp_file_problem()};
break;
case solvert::Z3:
argv = {"z3", "-smt2", temp_file_problem()};
break;
case solvert::GENERIC:
UNREACHABLE;
}
int res =
run(argv[0], argv, stdin_filename, temp_file_stdout(), temp_file_stderr());
if(res<0)
{
messaget log{message_handler};
log.error() << "error running SMT2 solver" << messaget::eom;
return decision_proceduret::resultt::D_ERROR;
}
std::ifstream in(temp_file_stdout());
return read_result(in);
}
static std::string drop_quotes(std::string src)
{
if(src.size() >= 2 && src.front() == '|' && src.back() == '|')
return std::string(src, 1, src.size() - 2);
else
return src;
}
decision_proceduret::resultt smt2_dect::read_result(std::istream &in)
{
std::string line;
decision_proceduret::resultt res=resultt::D_ERROR;
boolean_assignment.clear();
boolean_assignment.resize(no_boolean_variables, false);
typedef std::unordered_map<irep_idt, irept> valuest;
valuest parsed_values;
while(in)
{
auto parsed_opt = smt2irep(in, message_handler);
if(!parsed_opt.has_value())
break;
const auto &parsed = parsed_opt.value();
if(parsed.id()=="sat")
res=resultt::D_SATISFIABLE;
else if(parsed.id()=="unsat")
res=resultt::D_UNSATISFIABLE;
else if(parsed.id() == "unknown")
{
messaget log{message_handler};
log.error() << "SMT2 solver returned \"unknown\"" << messaget::eom;
return decision_proceduret::resultt::D_ERROR;
}
else if(
parsed.id().empty() && parsed.get_sub().size() == 1 &&
parsed.get_sub().front().get_sub().size() == 2)
{
const irept &s0=parsed.get_sub().front().get_sub()[0];
const irept &s1=parsed.get_sub().front().get_sub()[1];
// Examples:
// ( (B0 true) )
// ( (|__CPROVER_pipe_count#1| (_ bv0 32)) )
// ( (|some_integer| 0) )
// ( (|some_integer| (- 10)) )
parsed_values[s0.id()] = s1;
}
else if(
parsed.id().empty() && parsed.get_sub().size() == 2 &&
parsed.get_sub().front().id() == "error")
{
// We ignore errors after UNSAT because get-value after check-sat
// returns unsat will give an error.
if(res != resultt::D_UNSATISFIABLE)
{
const auto &message = id2string(parsed.get_sub()[1].id());
messaget log{message_handler};
log.error() << "SMT2 solver returned error message:\n"
<< "\t\"" << message << "\"" << messaget::eom;
return decision_proceduret::resultt::D_ERROR;
}
}
}
// If the result is not satisfiable don't bother updating the assignments and
// values (since we didn't get any), just return.
if(res != resultt::D_SATISFIABLE)
return res;
for(auto &assignment : identifier_map)
{
std::string conv_id = drop_quotes(convert_identifier(assignment.first));
const irept &value = parsed_values[conv_id];
assignment.second.value = parse_rec(value, assignment.second.type);
}
// Booleans
for(unsigned v=0; v<no_boolean_variables; v++)
{
const std::string boolean_identifier =
convert_identifier("B" + std::to_string(v));
boolean_assignment[v] = [&]() {
const auto found_parsed_value =
parsed_values.find(drop_quotes(boolean_identifier));
if(found_parsed_value != parsed_values.end())
{
return found_parsed_value->second.id() == ID_true;
}
// Work out the value based on what set_to was called with.
const auto found_set_value = set_values.find(boolean_identifier);
if(found_set_value != set_values.end())
return found_set_value->second;
// Old code used the computation
// const irept &value=values["B"+std::to_string(v)];
// boolean_assignment[v]=(value.id()==ID_true);
return parsed_values[boolean_identifier].id() == ID_true;
}();
}
return res;
}