7#ifndef BACKENDS_P4TOOLS_COMMON_CORE_Z3_SOLVER_H_
8#define BACKENDS_P4TOOLS_COMMON_CORE_Z3_SOLVER_H_
18#include <unordered_map>
22#include "ir/json_generator.h"
24#include "lib/cstring.h"
25#include "lib/ordered_map.h"
27#include "lib/safe_vector.h"
38 friend class Z3Translator;
40 friend class Z3SolverAccessor;
43 ~Z3Solver()
override =
default;
45 explicit Z3Solver(
bool isIncremental =
true,
46 std::optional<std::istream *> inOpt = std::nullopt);
52 void timeout(
unsigned tm)
override;
54 std::optional<bool>
checkSat(
const std::vector<const Constraint *> &asserts)
override;
58 std::optional<bool>
checkSat(
const z3::expr_vector &asserts);
71 [[nodiscard]]
const z3::solver &
getZ3Solver()
const;
77 [[nodiscard]]
const z3::context &
context()
const;
100 void asrt(
const z3::expr &assert);
107 z3::sort toSort(
const IR::Type *type);
112 z3::expr declareVar(
const IR::SymbolicVariable &var);
115 [[nodiscard]]
static std::string generateName(
const IR::SymbolicVariable &var);
118 static const IR::Literal *toLiteral(
const z3::expr &e,
const IR::Type *type);
126 void addZ3Pushes(
size_t &chkIndex,
size_t asrtIndex);
129 static std::optional<bool> interpretSolverResult(z3::check_result result);
132 std::unique_ptr<z3::context> ownedContext;
150 std::vector<size_t> checkpoints;
153 z3::expr_vector z3Assertions;
156 std::optional<unsigned> seed_;
159 std::optional<unsigned> timeout_;
175 bool preorder(
const IR::Cast *cast)
override;
178 bool preorder(
const IR::Constant *constant)
override;
179 bool preorder(
const IR::BoolLiteral *boolLiteral)
override;
180 bool preorder(
const IR::StringLiteral *stringLiteral)
override;
183 bool preorder(
const IR::SymbolicVariable *var)
override;
186 bool preorder(
const IR::Neg *op)
override;
187 bool preorder(
const IR::Cmpl *op)
override;
188 bool preorder(
const IR::LNot *op)
override;
191 bool preorder(
const IR::Equ *op)
override;
192 bool preorder(
const IR::Neq *op)
override;
193 bool preorder(
const IR::Lss *op)
override;
194 bool preorder(
const IR::Leq *op)
override;
195 bool preorder(
const IR::Grt *op)
override;
196 bool preorder(
const IR::Geq *op)
override;
197 bool preorder(
const IR::Mod *op)
override;
198 bool preorder(
const IR::Add *op)
override;
199 bool preorder(
const IR::Sub *op)
override;
200 bool preorder(
const IR::Mul *op)
override;
201 bool preorder(
const IR::Div *op)
override;
202 bool preorder(
const IR::Shl *op)
override;
203 bool preorder(
const IR::Shr *op)
override;
204 bool preorder(
const IR::BAnd *op)
override;
205 bool preorder(
const IR::BOr *op)
override;
206 bool preorder(
const IR::BXor *op)
override;
207 bool preorder(
const IR::LAnd *op)
override;
208 bool preorder(
const IR::LOr *op)
override;
209 bool preorder(
const IR::Concat *op)
override;
212 bool preorder(
const IR::Mux *op)
override;
213 bool preorder(
const IR::Slice *op)
override;
219 z3::expr
translate(
const IR::Expression *expression);
224 std::unordered_map<const IR::Expression *, z3::expr> translations;
227 void end_apply()
override;
228 z3::expr translateChild(
const IR::Expression *expression);
231 using Z3UnaryOp = z3::expr (*)(
const z3::expr &);
234 using Z3BinaryOp = z3::expr (*)(
const z3::expr &,
const z3::expr &);
237 using Z3TernaryOp = z3::expr (*)(
const z3::expr &,
const z3::expr &,
const z3::expr &);
242 bool recurseUnary(
const IR::Operation_Unary *unary, Z3UnaryOp f);
247 bool recurseBinary(
const IR::Operation_Binary *binary, Z3BinaryOp f);
252 bool recurseTernary(
const IR::Operation_Ternary *ternary, Z3TernaryOp f);
260 template <
class ShiftType>
261 const ShiftType *rewriteShift(
const ShiftType *shift)
const;
267 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:34