2017-07-13 16:22:51 +00:00
|
|
|
/*
|
|
|
|
This file is part of solidity.
|
|
|
|
|
|
|
|
solidity is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
solidity is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2017-07-13 16:22:51 +00:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2020-05-19 12:52:31 +00:00
|
|
|
#include <libsmtutil/Exceptions.h>
|
2020-05-18 15:42:24 +00:00
|
|
|
#include <libsmtutil/Sorts.h>
|
2020-03-24 19:51:52 +00:00
|
|
|
|
2020-01-06 10:52:23 +00:00
|
|
|
#include <libsolutil/Common.h>
|
2021-09-16 14:33:28 +00:00
|
|
|
#include <libsolutil/Numeric.h>
|
2021-10-06 09:45:15 +00:00
|
|
|
#include <libsolutil/CommonData.h>
|
2017-07-13 16:22:51 +00:00
|
|
|
|
2021-10-06 09:45:15 +00:00
|
|
|
#include <range/v3/algorithm/all_of.hpp>
|
2021-05-19 15:35:19 +00:00
|
|
|
#include <range/v3/view.hpp>
|
|
|
|
|
2018-12-17 17:26:10 +00:00
|
|
|
#include <cstdio>
|
2017-07-13 16:22:51 +00:00
|
|
|
#include <map>
|
2020-04-14 09:09:38 +00:00
|
|
|
#include <memory>
|
2020-11-16 16:42:51 +00:00
|
|
|
#include <optional>
|
2021-05-19 15:35:19 +00:00
|
|
|
#include <set>
|
2017-07-13 16:22:51 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2020-05-19 12:14:46 +00:00
|
|
|
namespace solidity::smtutil
|
2017-07-13 16:22:51 +00:00
|
|
|
{
|
|
|
|
|
2019-12-03 13:39:47 +00:00
|
|
|
struct SMTSolverChoice
|
|
|
|
{
|
|
|
|
bool cvc4 = false;
|
2021-05-19 15:35:19 +00:00
|
|
|
bool smtlib2 = false;
|
2019-12-03 13:39:47 +00:00
|
|
|
bool z3 = false;
|
|
|
|
|
2021-09-13 10:15:18 +00:00
|
|
|
static constexpr SMTSolverChoice All() noexcept { return {true, true, true}; }
|
|
|
|
static constexpr SMTSolverChoice CVC4() noexcept { return {true, false, false}; }
|
|
|
|
static constexpr SMTSolverChoice SMTLIB2() noexcept { return {false, true, false}; }
|
|
|
|
static constexpr SMTSolverChoice Z3() noexcept { return {false, false, true}; }
|
|
|
|
static constexpr SMTSolverChoice None() noexcept { return {false, false, false}; }
|
2021-05-19 15:35:19 +00:00
|
|
|
|
|
|
|
static std::optional<SMTSolverChoice> fromString(std::string const& _solvers)
|
|
|
|
{
|
|
|
|
SMTSolverChoice solvers;
|
|
|
|
if (_solvers == "all")
|
|
|
|
{
|
|
|
|
smtAssert(solvers.setSolver("cvc4"), "");
|
|
|
|
smtAssert(solvers.setSolver("smtlib2"), "");
|
|
|
|
smtAssert(solvers.setSolver("z3"), "");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
for (auto&& s: _solvers | ranges::views::split(',') | ranges::to<std::vector<std::string>>())
|
|
|
|
if (!solvers.setSolver(s))
|
|
|
|
return {};
|
|
|
|
|
|
|
|
return solvers;
|
|
|
|
}
|
|
|
|
|
2021-09-17 17:41:34 +00:00
|
|
|
SMTSolverChoice& operator&=(SMTSolverChoice const& _other)
|
2021-05-19 15:35:19 +00:00
|
|
|
{
|
|
|
|
cvc4 &= _other.cvc4;
|
|
|
|
smtlib2 &= _other.smtlib2;
|
|
|
|
z3 &= _other.z3;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-09-17 17:41:34 +00:00
|
|
|
SMTSolverChoice operator&(SMTSolverChoice _other) const noexcept
|
2021-05-19 15:35:19 +00:00
|
|
|
{
|
2021-09-17 17:41:34 +00:00
|
|
|
_other &= *this;
|
|
|
|
return _other;
|
2021-05-19 15:35:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(SMTSolverChoice const& _other) const noexcept { return !(*this == _other); }
|
|
|
|
|
|
|
|
bool operator==(SMTSolverChoice const& _other) const noexcept
|
|
|
|
{
|
|
|
|
return cvc4 == _other.cvc4 &&
|
|
|
|
smtlib2 == _other.smtlib2 &&
|
|
|
|
z3 == _other.z3;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool setSolver(std::string const& _solver)
|
|
|
|
{
|
|
|
|
static std::set<std::string> const solvers{"cvc4", "smtlib2", "z3"};
|
|
|
|
if (!solvers.count(_solver))
|
|
|
|
return false;
|
|
|
|
if (_solver == "cvc4")
|
|
|
|
cvc4 = true;
|
|
|
|
else if (_solver == "smtlib2")
|
|
|
|
smtlib2 = true;
|
|
|
|
else if (_solver == "z3")
|
|
|
|
z3 = true;
|
|
|
|
return true;
|
|
|
|
}
|
2019-12-03 13:39:47 +00:00
|
|
|
|
2021-09-13 10:15:18 +00:00
|
|
|
bool none() const noexcept { return !some(); }
|
|
|
|
bool some() const noexcept { return cvc4 || smtlib2 || z3; }
|
|
|
|
bool all() const noexcept { return cvc4 && smtlib2 && z3; }
|
2019-12-03 13:39:47 +00:00
|
|
|
};
|
|
|
|
|
2017-07-13 16:22:51 +00:00
|
|
|
enum class CheckResult
|
|
|
|
{
|
2018-06-26 10:41:26 +00:00
|
|
|
SATISFIABLE, UNSATISFIABLE, UNKNOWN, CONFLICTING, ERROR
|
2017-07-13 16:22:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// C++ representation of an SMTLIB2 expression.
|
|
|
|
class Expression
|
|
|
|
{
|
|
|
|
friend class SolverInterface;
|
|
|
|
public:
|
2018-11-21 14:13:50 +00:00
|
|
|
explicit Expression(bool _v): Expression(_v ? "true" : "false", Kind::Bool) {}
|
2020-05-19 12:23:41 +00:00
|
|
|
explicit Expression(std::shared_ptr<SortSort> _sort, std::string _name = ""): Expression(std::move(_name), {}, _sort) {}
|
2020-10-17 18:38:54 +00:00
|
|
|
explicit Expression(std::string _name, std::vector<Expression> _arguments, SortPointer _sort):
|
|
|
|
name(std::move(_name)), arguments(std::move(_arguments)), sort(std::move(_sort)) {}
|
2020-07-15 16:10:14 +00:00
|
|
|
Expression(size_t _number): Expression(std::to_string(_number), {}, SortProvider::sintSort) {}
|
|
|
|
Expression(u256 const& _number): Expression(_number.str(), {}, SortProvider::sintSort) {}
|
2021-05-20 16:07:40 +00:00
|
|
|
Expression(s256 const& _number): Expression(
|
|
|
|
_number >= 0 ? _number.str() : "-",
|
|
|
|
_number >= 0 ?
|
|
|
|
std::vector<Expression>{} :
|
|
|
|
std::vector<Expression>{Expression(size_t(0)), bigint(-_number)},
|
|
|
|
SortProvider::sintSort
|
|
|
|
) {}
|
|
|
|
Expression(bigint const& _number): Expression(
|
|
|
|
_number >= 0 ? _number.str() : "-",
|
|
|
|
_number >= 0 ?
|
|
|
|
std::vector<Expression>{} :
|
|
|
|
std::vector<Expression>{Expression(size_t(0)), bigint(-_number)},
|
|
|
|
SortProvider::sintSort
|
|
|
|
) {}
|
2017-07-13 16:22:51 +00:00
|
|
|
|
2017-08-31 10:16:41 +00:00
|
|
|
Expression(Expression const&) = default;
|
|
|
|
Expression(Expression&&) = default;
|
2018-10-17 14:46:18 +00:00
|
|
|
Expression& operator=(Expression const&) = default;
|
|
|
|
Expression& operator=(Expression&&) = default;
|
2017-07-13 16:22:51 +00:00
|
|
|
|
2018-04-06 16:01:40 +00:00
|
|
|
bool hasCorrectArity() const
|
|
|
|
{
|
2020-04-14 09:09:38 +00:00
|
|
|
if (name == "tuple_constructor")
|
|
|
|
{
|
|
|
|
auto tupleSort = std::dynamic_pointer_cast<TupleSort>(sort);
|
2020-05-19 12:52:31 +00:00
|
|
|
smtAssert(tupleSort, "");
|
2020-04-14 09:09:38 +00:00
|
|
|
return arguments.size() == tupleSort->components.size();
|
|
|
|
}
|
|
|
|
|
2018-04-06 16:01:40 +00:00
|
|
|
static std::map<std::string, unsigned> const operatorsArity{
|
|
|
|
{"ite", 3},
|
|
|
|
{"not", 1},
|
|
|
|
{"and", 2},
|
|
|
|
{"or", 2},
|
2021-05-18 17:15:08 +00:00
|
|
|
{"=>", 2},
|
2018-04-06 16:01:40 +00:00
|
|
|
{"=", 2},
|
|
|
|
{"<", 2},
|
|
|
|
{"<=", 2},
|
|
|
|
{">", 2},
|
|
|
|
{">=", 2},
|
|
|
|
{"+", 2},
|
|
|
|
{"-", 2},
|
|
|
|
{"*", 2},
|
2021-05-18 17:47:00 +00:00
|
|
|
{"div", 2},
|
2019-01-22 13:34:29 +00:00
|
|
|
{"mod", 2},
|
2020-08-23 19:21:45 +00:00
|
|
|
{"bvnot", 1},
|
2020-05-13 11:08:48 +00:00
|
|
|
{"bvand", 2},
|
2020-08-23 19:21:45 +00:00
|
|
|
{"bvor", 2},
|
|
|
|
{"bvxor", 2},
|
2020-09-04 14:55:45 +00:00
|
|
|
{"bvshl", 2},
|
|
|
|
{"bvlshr", 2},
|
|
|
|
{"bvashr", 2},
|
2020-05-13 11:08:48 +00:00
|
|
|
{"int2bv", 2},
|
|
|
|
{"bv2int", 1},
|
2018-11-22 10:24:12 +00:00
|
|
|
{"select", 2},
|
2019-08-22 13:44:30 +00:00
|
|
|
{"store", 3},
|
2020-04-03 13:10:16 +00:00
|
|
|
{"const_array", 2},
|
|
|
|
{"tuple_get", 2}
|
2018-04-06 16:01:40 +00:00
|
|
|
};
|
|
|
|
return operatorsArity.count(name) && operatorsArity.at(name) == arguments.size();
|
|
|
|
}
|
|
|
|
|
2017-07-14 09:49:27 +00:00
|
|
|
static Expression ite(Expression _condition, Expression _trueValue, Expression _falseValue)
|
|
|
|
{
|
2020-05-19 12:52:31 +00:00
|
|
|
smtAssert(*_trueValue.sort == *_falseValue.sort, "");
|
2018-11-29 14:32:38 +00:00
|
|
|
SortPointer sort = _trueValue.sort;
|
2017-07-14 09:49:27 +00:00
|
|
|
return Expression("ite", std::vector<Expression>{
|
|
|
|
std::move(_condition), std::move(_trueValue), std::move(_falseValue)
|
2018-11-29 14:32:38 +00:00
|
|
|
}, std::move(sort));
|
2017-07-14 09:49:27 +00:00
|
|
|
}
|
|
|
|
|
2017-12-11 19:09:17 +00:00
|
|
|
static Expression implies(Expression _a, Expression _b)
|
|
|
|
{
|
2019-06-04 12:23:44 +00:00
|
|
|
return Expression(
|
2021-05-18 17:15:08 +00:00
|
|
|
"=>",
|
2019-06-04 12:23:44 +00:00
|
|
|
std::move(_a),
|
|
|
|
std::move(_b),
|
|
|
|
Kind::Bool
|
|
|
|
);
|
2017-12-11 19:09:17 +00:00
|
|
|
}
|
|
|
|
|
2018-11-22 10:24:12 +00:00
|
|
|
/// select is the SMT representation of an array index access.
|
|
|
|
static Expression select(Expression _array, Expression _index)
|
|
|
|
{
|
2020-05-19 12:52:31 +00:00
|
|
|
smtAssert(_array.sort->kind == Kind::Array, "");
|
2018-11-09 16:06:30 +00:00
|
|
|
std::shared_ptr<ArraySort> arraySort = std::dynamic_pointer_cast<ArraySort>(_array.sort);
|
2020-05-19 12:52:31 +00:00
|
|
|
smtAssert(arraySort, "");
|
|
|
|
smtAssert(_index.sort, "");
|
|
|
|
smtAssert(*arraySort->domain == *_index.sort, "");
|
2018-11-22 10:24:12 +00:00
|
|
|
return Expression(
|
|
|
|
"select",
|
|
|
|
std::vector<Expression>{std::move(_array), std::move(_index)},
|
|
|
|
arraySort->range
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// store is the SMT representation of an assignment to array index.
|
|
|
|
/// The function is pure and returns the modified array.
|
|
|
|
static Expression store(Expression _array, Expression _index, Expression _element)
|
|
|
|
{
|
2020-04-14 09:09:38 +00:00
|
|
|
auto arraySort = std::dynamic_pointer_cast<ArraySort>(_array.sort);
|
2020-05-19 12:52:31 +00:00
|
|
|
smtAssert(arraySort, "");
|
|
|
|
smtAssert(_index.sort, "");
|
|
|
|
smtAssert(_element.sort, "");
|
|
|
|
smtAssert(*arraySort->domain == *_index.sort, "");
|
|
|
|
smtAssert(*arraySort->range == *_element.sort, "");
|
2018-11-22 10:24:12 +00:00
|
|
|
return Expression(
|
|
|
|
"store",
|
|
|
|
std::vector<Expression>{std::move(_array), std::move(_index), std::move(_element)},
|
2018-11-09 16:06:30 +00:00
|
|
|
arraySort
|
2018-11-22 10:24:12 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-08-22 13:44:30 +00:00
|
|
|
static Expression const_array(Expression _sort, Expression _value)
|
|
|
|
{
|
2020-05-19 12:52:31 +00:00
|
|
|
smtAssert(_sort.sort->kind == Kind::Sort, "");
|
2019-08-22 13:44:30 +00:00
|
|
|
auto sortSort = std::dynamic_pointer_cast<SortSort>(_sort.sort);
|
|
|
|
auto arraySort = std::dynamic_pointer_cast<ArraySort>(sortSort->inner);
|
2020-05-19 12:52:31 +00:00
|
|
|
smtAssert(sortSort && arraySort, "");
|
|
|
|
smtAssert(_value.sort, "");
|
|
|
|
smtAssert(*arraySort->range == *_value.sort, "");
|
2019-08-22 13:44:30 +00:00
|
|
|
return Expression(
|
|
|
|
"const_array",
|
|
|
|
std::vector<Expression>{std::move(_sort), std::move(_value)},
|
|
|
|
arraySort
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-04-03 13:10:16 +00:00
|
|
|
static Expression tuple_get(Expression _tuple, size_t _index)
|
|
|
|
{
|
2020-05-19 12:52:31 +00:00
|
|
|
smtAssert(_tuple.sort->kind == Kind::Tuple, "");
|
2020-04-03 13:10:16 +00:00
|
|
|
std::shared_ptr<TupleSort> tupleSort = std::dynamic_pointer_cast<TupleSort>(_tuple.sort);
|
2020-05-19 12:52:31 +00:00
|
|
|
smtAssert(tupleSort, "");
|
|
|
|
smtAssert(_index < tupleSort->components.size(), "");
|
2020-04-03 13:10:16 +00:00
|
|
|
return Expression(
|
|
|
|
"tuple_get",
|
|
|
|
std::vector<Expression>{std::move(_tuple), Expression(_index)},
|
|
|
|
tupleSort->components.at(_index)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-04-14 09:09:38 +00:00
|
|
|
static Expression tuple_constructor(Expression _tuple, std::vector<Expression> _arguments)
|
|
|
|
{
|
2020-05-19 12:52:31 +00:00
|
|
|
smtAssert(_tuple.sort->kind == Kind::Sort, "");
|
2020-04-14 09:09:38 +00:00
|
|
|
auto sortSort = std::dynamic_pointer_cast<SortSort>(_tuple.sort);
|
|
|
|
auto tupleSort = std::dynamic_pointer_cast<TupleSort>(sortSort->inner);
|
2020-05-19 12:52:31 +00:00
|
|
|
smtAssert(tupleSort, "");
|
|
|
|
smtAssert(_arguments.size() == tupleSort->components.size(), "");
|
2020-04-14 09:09:38 +00:00
|
|
|
return Expression(
|
|
|
|
"tuple_constructor",
|
|
|
|
std::move(_arguments),
|
|
|
|
tupleSort
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-05-13 11:08:48 +00:00
|
|
|
static Expression int2bv(Expression _n, size_t _size)
|
|
|
|
{
|
|
|
|
smtAssert(_n.sort->kind == Kind::Int, "");
|
|
|
|
std::shared_ptr<IntSort> intSort = std::dynamic_pointer_cast<IntSort>(_n.sort);
|
|
|
|
smtAssert(intSort, "");
|
|
|
|
smtAssert(_size <= 256, "");
|
|
|
|
return Expression(
|
|
|
|
"int2bv",
|
|
|
|
std::vector<Expression>{std::move(_n), Expression(_size)},
|
|
|
|
std::make_shared<BitVectorSort>(_size)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Expression bv2int(Expression _bv, bool _signed = false)
|
|
|
|
{
|
|
|
|
smtAssert(_bv.sort->kind == Kind::BitVector, "");
|
|
|
|
std::shared_ptr<BitVectorSort> bvSort = std::dynamic_pointer_cast<BitVectorSort>(_bv.sort);
|
|
|
|
smtAssert(bvSort, "");
|
|
|
|
smtAssert(bvSort->size <= 256, "");
|
|
|
|
return Expression(
|
|
|
|
"bv2int",
|
|
|
|
std::vector<Expression>{std::move(_bv)},
|
|
|
|
SortProvider::intSort(_signed)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-10-06 09:45:15 +00:00
|
|
|
static bool sameSort(std::vector<Expression> const& _args)
|
|
|
|
{
|
|
|
|
if (_args.empty())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
auto sort = _args.front().sort;
|
|
|
|
return ranges::all_of(
|
|
|
|
_args,
|
|
|
|
[&](auto const& _expr){ return _expr.sort->kind == sort->kind; }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Expression mkAnd(std::vector<Expression> _args)
|
|
|
|
{
|
|
|
|
smtAssert(!_args.empty(), "");
|
|
|
|
smtAssert(sameSort(_args), "");
|
|
|
|
|
|
|
|
auto sort = _args.front().sort;
|
|
|
|
if (sort->kind == Kind::BitVector)
|
|
|
|
return Expression("bvand", std::move(_args), sort);
|
|
|
|
|
|
|
|
smtAssert(sort->kind == Kind::Bool, "");
|
|
|
|
return Expression("and", std::move(_args), Kind::Bool);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Expression mkOr(std::vector<Expression> _args)
|
|
|
|
{
|
|
|
|
smtAssert(!_args.empty(), "");
|
|
|
|
smtAssert(sameSort(_args), "");
|
|
|
|
|
|
|
|
auto sort = _args.front().sort;
|
|
|
|
if (sort->kind == Kind::BitVector)
|
|
|
|
return Expression("bvor", std::move(_args), sort);
|
|
|
|
|
|
|
|
smtAssert(sort->kind == Kind::Bool, "");
|
|
|
|
return Expression("or", std::move(_args), Kind::Bool);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Expression mkPlus(std::vector<Expression> _args)
|
|
|
|
{
|
|
|
|
smtAssert(!_args.empty(), "");
|
|
|
|
smtAssert(sameSort(_args), "");
|
|
|
|
|
|
|
|
auto sort = _args.front().sort;
|
|
|
|
smtAssert(sort->kind == Kind::BitVector || sort->kind == Kind::Int, "");
|
|
|
|
return Expression("+", std::move(_args), sort);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Expression mkMul(std::vector<Expression> _args)
|
|
|
|
{
|
|
|
|
smtAssert(!_args.empty(), "");
|
|
|
|
smtAssert(sameSort(_args), "");
|
|
|
|
|
|
|
|
auto sort = _args.front().sort;
|
|
|
|
smtAssert(sort->kind == Kind::BitVector || sort->kind == Kind::Int, "");
|
|
|
|
return Expression("*", std::move(_args), sort);
|
|
|
|
}
|
|
|
|
|
2017-07-13 16:22:51 +00:00
|
|
|
friend Expression operator!(Expression _a)
|
|
|
|
{
|
2020-10-17 18:38:54 +00:00
|
|
|
if (_a.sort->kind == Kind::BitVector)
|
|
|
|
return ~_a;
|
2018-11-21 14:13:50 +00:00
|
|
|
return Expression("not", std::move(_a), Kind::Bool);
|
2017-07-13 16:22:51 +00:00
|
|
|
}
|
|
|
|
friend Expression operator&&(Expression _a, Expression _b)
|
|
|
|
{
|
2020-10-17 18:38:54 +00:00
|
|
|
if (_a.sort->kind == Kind::BitVector)
|
|
|
|
{
|
|
|
|
smtAssert(_b.sort->kind == Kind::BitVector, "");
|
|
|
|
return _a & _b;
|
|
|
|
}
|
2018-11-21 14:13:50 +00:00
|
|
|
return Expression("and", std::move(_a), std::move(_b), Kind::Bool);
|
2017-07-13 16:22:51 +00:00
|
|
|
}
|
|
|
|
friend Expression operator||(Expression _a, Expression _b)
|
|
|
|
{
|
2020-10-17 18:38:54 +00:00
|
|
|
if (_a.sort->kind == Kind::BitVector)
|
|
|
|
{
|
|
|
|
smtAssert(_b.sort->kind == Kind::BitVector, "");
|
|
|
|
return _a | _b;
|
|
|
|
}
|
2018-11-21 14:13:50 +00:00
|
|
|
return Expression("or", std::move(_a), std::move(_b), Kind::Bool);
|
2017-07-13 16:22:51 +00:00
|
|
|
}
|
|
|
|
friend Expression operator==(Expression _a, Expression _b)
|
|
|
|
{
|
2021-03-03 15:48:32 +00:00
|
|
|
smtAssert(_a.sort->kind == _b.sort->kind, "Trying to create an 'equal' expression with different sorts");
|
2018-11-21 14:13:50 +00:00
|
|
|
return Expression("=", std::move(_a), std::move(_b), Kind::Bool);
|
2017-07-13 16:22:51 +00:00
|
|
|
}
|
|
|
|
friend Expression operator!=(Expression _a, Expression _b)
|
|
|
|
{
|
|
|
|
return !(std::move(_a) == std::move(_b));
|
|
|
|
}
|
|
|
|
friend Expression operator<(Expression _a, Expression _b)
|
|
|
|
{
|
2018-11-21 14:13:50 +00:00
|
|
|
return Expression("<", std::move(_a), std::move(_b), Kind::Bool);
|
2017-07-13 16:22:51 +00:00
|
|
|
}
|
|
|
|
friend Expression operator<=(Expression _a, Expression _b)
|
|
|
|
{
|
2018-11-21 14:13:50 +00:00
|
|
|
return Expression("<=", std::move(_a), std::move(_b), Kind::Bool);
|
2017-07-13 16:22:51 +00:00
|
|
|
}
|
|
|
|
friend Expression operator>(Expression _a, Expression _b)
|
|
|
|
{
|
2018-11-21 14:13:50 +00:00
|
|
|
return Expression(">", std::move(_a), std::move(_b), Kind::Bool);
|
2017-07-13 16:22:51 +00:00
|
|
|
}
|
|
|
|
friend Expression operator>=(Expression _a, Expression _b)
|
|
|
|
{
|
2018-11-21 14:13:50 +00:00
|
|
|
return Expression(">=", std::move(_a), std::move(_b), Kind::Bool);
|
2017-07-13 16:22:51 +00:00
|
|
|
}
|
|
|
|
friend Expression operator+(Expression _a, Expression _b)
|
|
|
|
{
|
2020-07-15 16:10:14 +00:00
|
|
|
auto intSort = _a.sort;
|
|
|
|
return Expression("+", {std::move(_a), std::move(_b)}, intSort);
|
2017-07-13 16:22:51 +00:00
|
|
|
}
|
|
|
|
friend Expression operator-(Expression _a, Expression _b)
|
|
|
|
{
|
2020-07-15 16:10:14 +00:00
|
|
|
auto intSort = _a.sort;
|
|
|
|
return Expression("-", {std::move(_a), std::move(_b)}, intSort);
|
2017-07-13 16:22:51 +00:00
|
|
|
}
|
|
|
|
friend Expression operator*(Expression _a, Expression _b)
|
|
|
|
{
|
2020-07-15 16:10:14 +00:00
|
|
|
auto intSort = _a.sort;
|
|
|
|
return Expression("*", {std::move(_a), std::move(_b)}, intSort);
|
2017-10-05 13:23:25 +00:00
|
|
|
}
|
|
|
|
friend Expression operator/(Expression _a, Expression _b)
|
|
|
|
{
|
2020-07-15 16:10:14 +00:00
|
|
|
auto intSort = _a.sort;
|
2021-05-18 17:47:00 +00:00
|
|
|
return Expression("div", {std::move(_a), std::move(_b)}, intSort);
|
2017-07-13 16:22:51 +00:00
|
|
|
}
|
2019-01-22 13:34:29 +00:00
|
|
|
friend Expression operator%(Expression _a, Expression _b)
|
|
|
|
{
|
2020-07-15 16:10:14 +00:00
|
|
|
auto intSort = _a.sort;
|
|
|
|
return Expression("mod", {std::move(_a), std::move(_b)}, intSort);
|
2019-01-22 13:34:29 +00:00
|
|
|
}
|
2020-08-23 19:21:45 +00:00
|
|
|
friend Expression operator~(Expression _a)
|
|
|
|
{
|
|
|
|
auto bvSort = _a.sort;
|
|
|
|
return Expression("bvnot", {std::move(_a)}, bvSort);
|
|
|
|
}
|
2020-05-13 11:08:48 +00:00
|
|
|
friend Expression operator&(Expression _a, Expression _b)
|
|
|
|
{
|
|
|
|
auto bvSort = _a.sort;
|
|
|
|
return Expression("bvand", {std::move(_a), std::move(_b)}, bvSort);
|
|
|
|
}
|
2020-09-04 14:55:45 +00:00
|
|
|
friend Expression operator|(Expression _a, Expression _b)
|
|
|
|
{
|
|
|
|
auto bvSort = _a.sort;
|
|
|
|
return Expression("bvor", {std::move(_a), std::move(_b)}, bvSort);
|
|
|
|
}
|
2020-08-23 19:21:45 +00:00
|
|
|
friend Expression operator^(Expression _a, Expression _b)
|
|
|
|
{
|
|
|
|
auto bvSort = _a.sort;
|
|
|
|
return Expression("bvxor", {std::move(_a), std::move(_b)}, bvSort);
|
|
|
|
}
|
2020-09-04 14:55:45 +00:00
|
|
|
friend Expression operator<<(Expression _a, Expression _b)
|
2020-08-23 19:21:45 +00:00
|
|
|
{
|
|
|
|
auto bvSort = _a.sort;
|
2020-09-04 14:55:45 +00:00
|
|
|
return Expression("bvshl", {std::move(_a), std::move(_b)}, bvSort);
|
|
|
|
}
|
|
|
|
friend Expression operator>>(Expression _a, Expression _b)
|
|
|
|
{
|
|
|
|
auto bvSort = _a.sort;
|
|
|
|
return Expression("bvlshr", {std::move(_a), std::move(_b)}, bvSort);
|
|
|
|
}
|
|
|
|
static Expression ashr(Expression _a, Expression _b)
|
|
|
|
{
|
|
|
|
auto bvSort = _a.sort;
|
|
|
|
return Expression("bvashr", {std::move(_a), std::move(_b)}, bvSort);
|
2020-08-23 19:21:45 +00:00
|
|
|
}
|
2018-10-25 14:00:09 +00:00
|
|
|
Expression operator()(std::vector<Expression> _arguments) const
|
2017-07-13 16:22:51 +00:00
|
|
|
{
|
2020-05-19 12:52:31 +00:00
|
|
|
smtAssert(
|
2018-11-21 15:57:02 +00:00
|
|
|
sort->kind == Kind::Function,
|
2017-11-22 16:12:22 +00:00
|
|
|
"Attempted function application to non-function."
|
|
|
|
);
|
2018-11-21 15:57:02 +00:00
|
|
|
auto fSort = dynamic_cast<FunctionSort const*>(sort.get());
|
2020-05-19 12:52:31 +00:00
|
|
|
smtAssert(fSort, "");
|
2018-11-21 15:57:02 +00:00
|
|
|
return Expression(name, std::move(_arguments), fSort->codomain);
|
2017-07-13 16:22:51 +00:00
|
|
|
}
|
|
|
|
|
2018-10-17 14:46:18 +00:00
|
|
|
std::string name;
|
|
|
|
std::vector<Expression> arguments;
|
2018-11-21 14:13:50 +00:00
|
|
|
SortPointer sort;
|
2017-07-13 16:22:51 +00:00
|
|
|
|
|
|
|
private:
|
2018-11-21 14:13:50 +00:00
|
|
|
/// Manual constructors, should only be used by SolverInterface and this class itself.
|
|
|
|
Expression(std::string _name, std::vector<Expression> _arguments, Kind _kind):
|
|
|
|
Expression(std::move(_name), std::move(_arguments), std::make_shared<Sort>(_kind)) {}
|
|
|
|
|
|
|
|
explicit Expression(std::string _name, Kind _kind):
|
|
|
|
Expression(std::move(_name), std::vector<Expression>{}, _kind) {}
|
|
|
|
Expression(std::string _name, Expression _arg, Kind _kind):
|
|
|
|
Expression(std::move(_name), std::vector<Expression>{std::move(_arg)}, _kind) {}
|
|
|
|
Expression(std::string _name, Expression _arg1, Expression _arg2, Kind _kind):
|
|
|
|
Expression(std::move(_name), std::vector<Expression>{std::move(_arg1), std::move(_arg2)}, _kind) {}
|
2017-07-13 16:22:51 +00:00
|
|
|
};
|
|
|
|
|
2017-07-13 19:06:29 +00:00
|
|
|
DEV_SIMPLE_EXCEPTION(SolverError);
|
|
|
|
|
2017-07-13 16:22:51 +00:00
|
|
|
class SolverInterface
|
|
|
|
{
|
|
|
|
public:
|
2020-11-02 20:20:20 +00:00
|
|
|
SolverInterface(std::optional<unsigned> _queryTimeout = {}): m_queryTimeout(_queryTimeout) {}
|
|
|
|
|
2018-05-02 11:29:16 +00:00
|
|
|
virtual ~SolverInterface() = default;
|
2017-07-13 16:22:51 +00:00
|
|
|
virtual void reset() = 0;
|
|
|
|
|
|
|
|
virtual void push() = 0;
|
|
|
|
virtual void pop() = 0;
|
|
|
|
|
2019-09-24 15:35:31 +00:00
|
|
|
virtual void declareVariable(std::string const& _name, SortPointer const& _sort) = 0;
|
|
|
|
Expression newVariable(std::string _name, SortPointer const& _sort)
|
2017-07-13 16:22:51 +00:00
|
|
|
{
|
|
|
|
// Subclasses should do something here
|
2020-05-19 12:52:31 +00:00
|
|
|
smtAssert(_sort, "");
|
2019-09-24 15:35:31 +00:00
|
|
|
declareVariable(_name, _sort);
|
|
|
|
return Expression(std::move(_name), {}, _sort);
|
2017-07-13 16:22:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void addAssertion(Expression const& _expr) = 0;
|
|
|
|
|
2017-07-13 19:06:29 +00:00
|
|
|
/// Checks for satisfiability, evaluates the expressions if a model
|
|
|
|
/// is available. Throws SMTSolverError on error.
|
2017-07-13 16:22:51 +00:00
|
|
|
virtual std::pair<CheckResult, std::vector<std::string>>
|
|
|
|
check(std::vector<Expression> const& _expressionsToEvaluate) = 0;
|
|
|
|
|
2017-10-13 13:19:53 +00:00
|
|
|
/// @returns a list of queries that the system was not able to respond to.
|
|
|
|
virtual std::vector<std::string> unhandledQueries() { return {}; }
|
|
|
|
|
2019-01-23 10:22:38 +00:00
|
|
|
/// @returns how many SMT solvers this interface has.
|
2020-07-13 20:40:33 +00:00
|
|
|
virtual size_t solvers() { return 1; }
|
2020-11-02 20:20:20 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
std::optional<unsigned> m_queryTimeout;
|
2018-07-27 07:14:50 +00:00
|
|
|
};
|
2017-07-13 16:22:51 +00:00
|
|
|
|
|
|
|
}
|