7#ifndef BACKENDS_P4TOOLS_COMMON_CORE_Z3_SOLVER_H_
8#define BACKENDS_P4TOOLS_COMMON_CORE_Z3_SOLVER_H_
20#include "ir/json_generator.h"
22#include "lib/cstring.h"
23#include "lib/ordered_map.h"
25#include "lib/safe_vector.h"
36 friend class Z3Translator;
38 friend class Z3SolverAccessor;
41 ~Z3Solver()
override =
default;
43 explicit Z3Solver(
bool isIncremental =
true,
44 std::optional<std::istream *> inOpt = std::nullopt);
50 void timeout(
unsigned tm)
override;
52 std::optional<bool>
checkSat(
const std::vector<const Constraint *> &asserts)
override;
56 std::optional<bool>
checkSat(
const z3::expr_vector &asserts);
69 [[nodiscard]]
const z3::solver &
getZ3Solver()
const;
72 [[nodiscard]]
const z3::context &
getZ3Ctx()
const;
92 void asrt(
const z3::expr &assert);
99 z3::sort toSort(
const IR::Type *type);
102 [[nodiscard]] z3::context &ctx()
const;
107 z3::expr declareVar(
const IR::SymbolicVariable &var);
110 [[nodiscard]]
static std::string generateName(
const IR::SymbolicVariable &var);
113 static const IR::Literal *toLiteral(
const z3::expr &e,
const IR::Type *type);
121 void addZ3Pushes(
size_t &chkIndex,
size_t asrtIndex);
124 static std::optional<bool> interpretSolverResult(z3::check_result result);
142 std::vector<size_t> checkpoints;
145 z3::expr_vector z3Assertions;
148 std::optional<unsigned> seed_;
151 std::optional<unsigned> timeout_;
167 bool preorder(
const IR::Cast *cast)
override;
170 bool preorder(
const IR::Constant *constant)
override;
171 bool preorder(
const IR::BoolLiteral *boolLiteral)
override;
172 bool preorder(
const IR::StringLiteral *stringLiteral)
override;
175 bool preorder(
const IR::SymbolicVariable *var)
override;
178 bool preorder(
const IR::Neg *op)
override;
179 bool preorder(
const IR::Cmpl *op)
override;
180 bool preorder(
const IR::LNot *op)
override;
183 bool preorder(
const IR::Equ *op)
override;
184 bool preorder(
const IR::Neq *op)
override;
185 bool preorder(
const IR::Lss *op)
override;
186 bool preorder(
const IR::Leq *op)
override;
187 bool preorder(
const IR::Grt *op)
override;
188 bool preorder(
const IR::Geq *op)
override;
189 bool preorder(
const IR::Mod *op)
override;
190 bool preorder(
const IR::Add *op)
override;
191 bool preorder(
const IR::Sub *op)
override;
192 bool preorder(
const IR::Mul *op)
override;
193 bool preorder(
const IR::Div *op)
override;
194 bool preorder(
const IR::Shl *op)
override;
195 bool preorder(
const IR::Shr *op)
override;
196 bool preorder(
const IR::BAnd *op)
override;
197 bool preorder(
const IR::BOr *op)
override;
198 bool preorder(
const IR::BXor *op)
override;
199 bool preorder(
const IR::LAnd *op)
override;
200 bool preorder(
const IR::LOr *op)
override;
201 bool preorder(
const IR::Concat *op)
override;
204 bool preorder(
const IR::Mux *op)
override;
205 bool preorder(
const IR::Slice *op)
override;
211 z3::expr
translate(
const IR::Expression *expression);
215 using Z3UnaryOp = z3::expr (*)(
const z3::expr &);
218 using Z3BinaryOp = z3::expr (*)(
const z3::expr &,
const z3::expr &);
221 using Z3TernaryOp = z3::expr (*)(
const z3::expr &,
const z3::expr &,
const z3::expr &);
226 bool recurseUnary(
const IR::Operation_Unary *unary, Z3UnaryOp f);
231 bool recurseBinary(
const IR::Operation_Binary *binary, Z3BinaryOp f);
236 bool recurseTernary(
const IR::Operation_Ternary *ternary, Z3TernaryOp f);
244 template <
class ShiftType>
245 const ShiftType *rewriteShift(
const ShiftType *shift)
const;
251 std::reference_wrapper<Z3Solver>
solver;
Provides a higher-level interface for an SMT solver.
Definition solver.h:30
Definition json_generator.h:30
Definition safe_vector.h:18
IR::Expression Constraint
Represents a constraint that can be shipped to and asserted within a solver.
Definition solver.h:23
P4::flat_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:26
Definition phv/solver/action_constraint_solver.cpp:33