1#ifndef BACKENDS_P4TOOLS_COMMON_CORE_Z3_SOLVER_H_
2#define BACKENDS_P4TOOLS_COMMON_CORE_Z3_SOLVER_H_
13#include "ir/json_generator.h"
15#include "lib/cstring.h"
16#include "lib/ordered_map.h"
18#include "lib/safe_vector.h"
29 friend class Z3Translator;
31 friend class Z3SolverAccessor;
34 ~Z3Solver()
override =
default;
36 explicit Z3Solver(
bool isIncremental =
true,
37 std::optional<std::istream *> inOpt = std::nullopt);
43 void timeout(
unsigned tm)
override;
45 std::optional<bool>
checkSat(
const std::vector<const Constraint *> &asserts)
override;
49 std::optional<bool>
checkSat(
const z3::expr_vector &asserts);
62 [[nodiscard]]
const z3::solver &
getZ3Solver()
const;
65 [[nodiscard]]
const z3::context &
getZ3Ctx()
const;
85 void asrt(
const z3::expr &assert);
92 z3::sort toSort(
const IR::Type *type);
95 [[nodiscard]] z3::context &ctx()
const;
100 z3::expr declareVar(
const IR::SymbolicVariable &var);
103 [[nodiscard]]
static std::string generateName(
const IR::SymbolicVariable &var);
106 static const IR::Literal *toLiteral(
const z3::expr &e,
const IR::Type *type);
114 void addZ3Pushes(
size_t &chkIndex,
size_t asrtIndex);
117 static std::optional<bool> interpretSolverResult(z3::check_result result);
135 std::vector<size_t> checkpoints;
138 z3::expr_vector z3Assertions;
141 std::optional<unsigned> seed_;
144 std::optional<unsigned> timeout_;
160 bool preorder(
const IR::Cast *cast)
override;
163 bool preorder(
const IR::Constant *constant)
override;
164 bool preorder(
const IR::BoolLiteral *boolLiteral)
override;
165 bool preorder(
const IR::StringLiteral *stringLiteral)
override;
168 bool preorder(
const IR::SymbolicVariable *var)
override;
171 bool preorder(
const IR::Neg *op)
override;
172 bool preorder(
const IR::Cmpl *op)
override;
173 bool preorder(
const IR::LNot *op)
override;
176 bool preorder(
const IR::Equ *op)
override;
177 bool preorder(
const IR::Neq *op)
override;
178 bool preorder(
const IR::Lss *op)
override;
179 bool preorder(
const IR::Leq *op)
override;
180 bool preorder(
const IR::Grt *op)
override;
181 bool preorder(
const IR::Geq *op)
override;
182 bool preorder(
const IR::Mod *op)
override;
183 bool preorder(
const IR::Add *op)
override;
184 bool preorder(
const IR::Sub *op)
override;
185 bool preorder(
const IR::Mul *op)
override;
186 bool preorder(
const IR::Div *op)
override;
187 bool preorder(
const IR::Shl *op)
override;
188 bool preorder(
const IR::Shr *op)
override;
189 bool preorder(
const IR::BAnd *op)
override;
190 bool preorder(
const IR::BOr *op)
override;
191 bool preorder(
const IR::BXor *op)
override;
192 bool preorder(
const IR::LAnd *op)
override;
193 bool preorder(
const IR::LOr *op)
override;
194 bool preorder(
const IR::Concat *op)
override;
197 bool preorder(
const IR::Mux *op)
override;
198 bool preorder(
const IR::Slice *op)
override;
204 z3::expr
translate(
const IR::Expression *expression);
208 using Z3UnaryOp = z3::expr (*)(
const z3::expr &);
211 using Z3BinaryOp = z3::expr (*)(
const z3::expr &,
const z3::expr &);
214 using Z3TernaryOp = z3::expr (*)(
const z3::expr &,
const z3::expr &,
const z3::expr &);
219 bool recurseUnary(
const IR::Operation_Unary *unary, Z3UnaryOp f);
224 bool recurseBinary(
const IR::Operation_Binary *binary, Z3BinaryOp f);
229 bool recurseTernary(
const IR::Operation_Ternary *ternary, Z3TernaryOp f);
237 template <
class ShiftType>
238 const ShiftType *rewriteShift(
const ShiftType *shift)
const;
244 std::reference_wrapper<Z3Solver> solver;
Provides a higher-level interface for an SMT solver.
Definition solver.h:24
Definition json_generator.h:36
Definition safe_vector.h:27
IR::Expression Constraint
Represents a constraint that can be shipped to and asserted within a solver.
Definition solver.h:17
absl::btree_map< const IR::SymbolicVariable *, const IR::Expression *, IR::SymbolicVariableLess > SymbolicMapping
This type maps symbolic variables to their value assigned by the solver.
Definition solver.h:20