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
use super::{
Assignment, BVOperator, BitVector, Formula, FormulaVisitor, Solver, SolverError, SymbolId,
};
use std::{
collections::HashMap,
fs::File,
io::{stdout, BufWriter, Write},
path::Path,
sync::{Arc, Mutex},
};
pub struct ExternalSolver {
output: Arc<Mutex<dyn Write + Send>>,
}
impl ExternalSolver {
pub fn new<P>(path: P) -> Result<Self, SolverError>
where
P: AsRef<Path>,
{
let file = File::open(path)?;
let mut writer = BufWriter::new(file);
write_init(&mut writer)?;
let output = Arc::new(Mutex::new(writer));
Ok(Self { output })
}
}
fn write_init<W: Write>(writer: &mut W) -> Result<(), SolverError> {
writeln!(writer, "(set-logic QF_BV)").map_err(SolverError::from)
}
impl Default for ExternalSolver {
fn default() -> Self {
let mut file = BufWriter::new(stdout());
write_init(&mut file).expect("stdout should not fail");
Self {
output: Arc::new(Mutex::new(file)),
}
}
}
impl Solver for ExternalSolver {
fn name() -> &'static str {
"External"
}
fn solve_impl<F: Formula>(&self, formula: &F) -> Result<Option<Assignment>, SolverError> {
{
let mut output = self.output.lock().expect("no other thread should fail");
writeln!(output, "(push 1)")?;
}
let mut printer = SmtPrinter {
output: self.output.clone(),
};
let mut visited = HashMap::<SymbolId, Result<SymbolId, SolverError>>::new();
formula.traverse(formula.root(), &mut visited, &mut printer)?;
let mut output = self.output.lock().expect("no other thread should fail");
writeln!(output, "(check-sat)\n(get-model)\n(pop 1)")?;
Err(SolverError::SatUnknown)
}
}
struct SmtPrinter {
output: Arc<Mutex<dyn Write>>,
}
impl FormulaVisitor<Result<SymbolId, SolverError>> for SmtPrinter {
fn input(&mut self, idx: SymbolId, name: &str) -> Result<SymbolId, SolverError> {
let mut o = self.output.lock().expect("no other thread should fail");
writeln!(o, "(declare-fun x{} () (_ BitVec 64)); {:?}", idx, name)?;
Ok(idx)
}
fn constant(&mut self, idx: SymbolId, v: BitVector) -> Result<SymbolId, SolverError> {
let mut o = self.output.lock().expect("no other thread should fail");
writeln!(
o,
"(declare-fun x{} () (_ BitVec 64))\n(assert (= x{} (_ bv{} 64)))",
idx, idx, v.0
)?;
Ok(idx)
}
fn unary(
&mut self,
idx: SymbolId,
op: BVOperator,
v: Result<SymbolId, SolverError>,
) -> Result<SymbolId, SolverError> {
let mut o = self.output.lock().expect("no other thread should fail");
writeln!(
o,
"(declare-fun x{} () (_ BitVec 64))\n(assert (= x{} ({} x{})))",
idx,
idx,
to_smt(op),
v?
)?;
Ok(idx)
}
fn binary(
&mut self,
idx: SymbolId,
op: BVOperator,
lhs: Result<SymbolId, SolverError>,
rhs: Result<SymbolId, SolverError>,
) -> Result<SymbolId, SolverError> {
let mut o = self.output.lock().expect("no other thread should fail");
writeln!(
o,
"(declare-fun x{} () (_ BitVec 64))\n(assert (= x{} ({} x{} x{})))",
idx,
idx,
to_smt(op),
lhs?,
rhs?
)?;
Ok(idx)
}
}
fn to_smt(op: BVOperator) -> &'static str {
match op {
BVOperator::Add => "bvadd",
BVOperator::Sub => "bvsub",
BVOperator::Not => "not",
BVOperator::Mul => "bvmul",
BVOperator::Divu => "bvudiv",
BVOperator::Remu => "bvurem",
BVOperator::Equals => "=",
BVOperator::BitwiseAnd => "bvand",
BVOperator::Sltu => "bvult",
}
}