1#ifndef BACKENDS_P4TOOLS_COMMON_CORE_Z3_SOLVER_H_
2#define BACKENDS_P4TOOLS_COMMON_CORE_Z3_SOLVER_H_
14#include "ir/json_generator.h"
16#include "lib/cstring.h"
17#include "lib/ordered_map.h"
19#include "lib/safe_vector.h"
37 explicit Z3Solver(
bool isIncremental =
true,
38 std::optional<std::istream *> inOpt = std::nullopt);
44 void timeout(
unsigned tm)
override;
46 std::optional<bool>
checkSat(
const std::vector<const Constraint *> &asserts)
override;
50 std::optional<bool>
checkSat(
const z3::expr_vector &asserts);
63 [[nodiscard]]
const z3::solver &
getZ3Solver()
const;
66 [[nodiscard]]
const z3::context &
getZ3Ctx()
const;
86 void asrt(
const z3::expr &assert);
93 z3::sort toSort(
const IR::Type *type);
96 [[nodiscard]] z3::context &ctx()
const;
101 z3::expr declareVar(
const IR::SymbolicVariable &var);
104 [[nodiscard]]
static std::string generateName(
const IR::SymbolicVariable &var);
107 static const IR::Literal *toLiteral(
const z3::expr &e,
const IR::Type *type);
115 void addZ3Pushes(
size_t &chkIndex,
size_t asrtIndex);
118 static std::optional<bool> interpretSolverResult(z3::check_result result);
136 std::vector<size_t> checkpoints;
139 z3::expr_vector z3Assertions;
142 std::optional<unsigned> seed_;
145 std::optional<unsigned> timeout_;
161 bool preorder(
const IR::Cast *cast)
override;
164 bool preorder(
const IR::Constant *constant)
override;
165 bool preorder(
const IR::BoolLiteral *boolLiteral)
override;
166 bool preorder(
const IR::StringLiteral *stringLiteral)
override;
169 bool preorder(
const IR::SymbolicVariable *var)
override;
172 bool preorder(
const IR::Neg *op)
override;
173 bool preorder(
const IR::Cmpl *op)
override;
174 bool preorder(
const IR::LNot *op)
override;
177 bool preorder(
const IR::Equ *op)
override;
178 bool preorder(
const IR::Neq *op)
override;
179 bool preorder(
const IR::Lss *op)
override;
180 bool preorder(
const IR::Leq *op)
override;
181 bool preorder(
const IR::Grt *op)
override;
182 bool preorder(
const IR::Geq *op)
override;
183 bool preorder(
const IR::Mod *op)
override;
184 bool preorder(
const IR::Add *op)
override;
185 bool preorder(
const IR::Sub *op)
override;
186 bool preorder(
const IR::Mul *op)
override;
187 bool preorder(
const IR::Div *op)
override;
188 bool preorder(
const IR::Shl *op)
override;
189 bool preorder(
const IR::Shr *op)
override;
190 bool preorder(
const IR::BAnd *op)
override;
191 bool preorder(
const IR::BOr *op)
override;
192 bool preorder(
const IR::BXor *op)
override;
193 bool preorder(
const IR::LAnd *op)
override;
194 bool preorder(
const IR::LOr *op)
override;
195 bool preorder(
const IR::Concat *op)
override;
198 bool preorder(
const IR::Mux *op)
override;
199 bool preorder(
const IR::Slice *op)
override;
205 z3::expr
translate(
const IR::Expression *expression);
209 using Z3UnaryOp = z3::expr (*)(
const z3::expr &);
212 using Z3BinaryOp = z3::expr (*)(
const z3::expr &,
const z3::expr &);
215 using Z3TernaryOp = z3::expr (*)(
const z3::expr &,
const z3::expr &,
const z3::expr &);
220 bool recurseUnary(
const IR::Operation_Unary *unary, Z3UnaryOp f);
225 bool recurseBinary(
const IR::Operation_Binary *binary, Z3BinaryOp f);
230 bool recurseTernary(
const IR::Operation_Ternary *ternary, Z3TernaryOp f);
238 template <
class ShiftType>
239 const ShiftType *rewriteShift(
const ShiftType *shift)
const;
245 std::reference_wrapper<Z3Solver>
solver;
Provides a higher-level interface for an SMT solver.
Definition solver.h:24
Definition json_generator.h:37
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
Definition phv/solver/action_constraint_solver.cpp:33