28 this->translatedNodes.clear();
29 this->variables.clear();
30 this->symbols.clear();
35 return this->variables;
47 for (
auto&& n : nodes) {
48 this->translatedNodes[n] = translate(n, bzla);
51 return this->translatedNodes.at(node);
55 BitwuzlaTerm TritonToBitwuzla::translate(
const SharedAbstractNode& node, Bitwuzla* bzla) {
59 std::vector<BitwuzlaTerm> children;
60 for (
auto&& n : node->getChildren()) {
61 children.emplace_back(this->translatedNodes.at(n));
64 BitwuzlaTermManager* tm = bitwuzla_get_term_mgr(bzla);
65 switch (node->getType()) {
68 auto size = triton::ast::getInteger<triton::uint32>(node->getChildren()[0]);
69 auto isort = bitwuzla_mk_bv_sort(tm, size);
70 auto vsort = bitwuzla_mk_bv_sort(tm, 8);
71 auto asort = bitwuzla_mk_array_sort(tm, isort, vsort);
72 auto value = bitwuzla_mk_bv_value_uint64(tm, vsort, 0);
73 return bitwuzla_mk_const_array(tm, asort, value);
77 auto bvsize = node->getBitvectorSize();
78 auto bvsort = bitwuzla_mk_bv_sort(tm, bvsize);
79 auto retval = bitwuzla_mk_term2(tm, BITWUZLA_KIND_BV_AND, children[0], bitwuzla_mk_bv_value_uint64(tm, bvsort, 0xff));
81 retval = bitwuzla_mk_term2(tm, BITWUZLA_KIND_BV_SHL, retval, bitwuzla_mk_bv_value_uint64(tm, bvsort, 8));
82 retval = bitwuzla_mk_term2(tm, BITWUZLA_KIND_BV_OR, retval,
83 bitwuzla_mk_term2(tm, BITWUZLA_KIND_BV_AND,
84 bitwuzla_mk_term2(tm, BITWUZLA_KIND_BV_SHR, children[0], bitwuzla_mk_bv_value_uint64(tm, bvsort, index)),
85 bitwuzla_mk_bv_value_uint64(tm, bvsort, 0xff)
93 return bitwuzla_mk_term2(tm, BITWUZLA_KIND_BV_ADD, children[0], children[1]);
96 return bitwuzla_mk_term2(tm, BITWUZLA_KIND_BV_AND, children[0], children[1]);
99 return bitwuzla_mk_term2(tm, BITWUZLA_KIND_BV_ASHR, children[0], children[1]);
102 return bitwuzla_mk_term2(tm, BITWUZLA_KIND_BV_SHR, children[0], children[1]);
105 return bitwuzla_mk_term2(tm, BITWUZLA_KIND_BV_MUL, children[0], children[1]);
108 return bitwuzla_mk_term2(tm, BITWUZLA_KIND_BV_NAND, children[0], children[1]);
111 return bitwuzla_mk_term1(tm, BITWUZLA_KIND_BV_NEG, children[0]);
114 return bitwuzla_mk_term2(tm, BITWUZLA_KIND_BV_NOR, children[0], children[1]);
117 return bitwuzla_mk_term1(tm, BITWUZLA_KIND_BV_NOT, children[0]);
120 return bitwuzla_mk_term2(tm, BITWUZLA_KIND_BV_OR, children[0], children[1]);
123 auto childNodes = node->getChildren();
124 auto idx = triton::ast::getInteger<triton::usize>(childNodes[1]);
125 return bitwuzla_mk_term1_indexed1(tm, BITWUZLA_KIND_BV_ROLI, children[0], idx);
129 auto childNodes = node->getChildren();
130 auto idx = triton::ast::getInteger<triton::usize>(childNodes[1]);
131 return bitwuzla_mk_term1_indexed1(tm, BITWUZLA_KIND_BV_RORI, children[0], idx);
135 return bitwuzla_mk_term2(tm, BITWUZLA_KIND_BV_SDIV, children[0], children[1]);
138 return bitwuzla_mk_term2(tm, BITWUZLA_KIND_BV_SGE, children[0], children[1]);
141 return bitwuzla_mk_term2(tm, BITWUZLA_KIND_BV_SGT, children[0], children[1]);
144 return bitwuzla_mk_term2(tm, BITWUZLA_KIND_BV_SHL, children[0], children[1]);
147 return bitwuzla_mk_term2(tm, BITWUZLA_KIND_BV_SLE, children[0], children[1]);
150 return bitwuzla_mk_term2(tm, BITWUZLA_KIND_BV_SLT, children[0], children[1]);
153 return bitwuzla_mk_term2(tm, BITWUZLA_KIND_BV_SMOD, children[0], children[1]);
156 return bitwuzla_mk_term2(tm, BITWUZLA_KIND_BV_SREM, children[0], children[1]);
159 return bitwuzla_mk_term2(tm, BITWUZLA_KIND_BV_SUB, children[0], children[1]);
162 return bitwuzla_mk_term2(tm, BITWUZLA_KIND_BV_UDIV, children[0], children[1]);
165 return bitwuzla_mk_term2(tm, BITWUZLA_KIND_BV_UGE, children[0], children[1]);
168 return bitwuzla_mk_term2(tm, BITWUZLA_KIND_BV_UGT, children[0], children[1]);
171 return bitwuzla_mk_term2(tm, BITWUZLA_KIND_BV_ULE, children[0], children[1]);
174 return bitwuzla_mk_term2(tm, BITWUZLA_KIND_BV_ULT, children[0], children[1]);
177 return bitwuzla_mk_term2(tm, BITWUZLA_KIND_BV_UREM, children[0], children[1]);
180 return bitwuzla_mk_term2(tm, BITWUZLA_KIND_BV_XNOR, children[0], children[1]);
183 return bitwuzla_mk_term2(tm, BITWUZLA_KIND_BV_XOR, children[0], children[1]);
186 auto childNodes = node->getChildren();
187 auto bv_size = triton::ast::getInteger<triton::usize>(childNodes[1]);
188 auto sort = this->bvSorts.find(bv_size);
189 if (sort == this->bvSorts.end()) {
190 sort = this->bvSorts.insert({bv_size, bitwuzla_mk_bv_sort(tm, bv_size)}).first;
194 if (bv_size <=
sizeof(uint64_t) * 8) {
195 auto bv_value = triton::ast::getInteger<triton::uint64>(childNodes[0]);
196 return bitwuzla_mk_bv_value_uint64(tm, sort->second, bv_value);
199 auto bv_value = triton::ast::getInteger<std::string>(childNodes[0]);
200 return bitwuzla_mk_bv_value(tm, sort->second, bv_value.c_str(), 10);
204 return bitwuzla_mk_term(tm, BITWUZLA_KIND_BV_CONCAT, children.size(), children.data());
207 return bitwuzla_mk_term2(tm, BITWUZLA_KIND_DISTINCT, children[0], children[1]);
210 return bitwuzla_mk_term2(tm, BITWUZLA_KIND_EQUAL, children[0], children[1]);
213 auto childNodes = node->getChildren();
214 auto high = triton::ast::getInteger<triton::usize>(childNodes[0]);
215 auto low = triton::ast::getInteger<triton::usize>(childNodes[1]);
216 return bitwuzla_mk_term1_indexed2(tm, BITWUZLA_KIND_BV_EXTRACT, children[2], high, low);
223 return bitwuzla_mk_term2(tm, BITWUZLA_KIND_IFF, children[0], children[1]);
226 return (BitwuzlaTerm) 0;
229 return bitwuzla_mk_term3(tm, BITWUZLA_KIND_ITE, children[0], children[1], children[2]);
232 return bitwuzla_mk_term(tm, BITWUZLA_KIND_AND, children.size(), children.data());
235 auto childNodes = node->getChildren();
241 return bitwuzla_mk_term1(tm, BITWUZLA_KIND_NOT, children[0]);
244 return bitwuzla_mk_term(tm, BITWUZLA_KIND_OR, children.size(), children.data());
247 return bitwuzla_mk_term(tm, BITWUZLA_KIND_XOR, children.size(), children.data());
250 auto ref =
reinterpret_cast<ReferenceNode*
>(node.get())->getSymbolicExpression()->getAst();
251 return this->translatedNodes.at(ref);
255 return bitwuzla_mk_term2(tm, BITWUZLA_KIND_ARRAY_SELECT, children[0], children[1]);
258 return bitwuzla_mk_term3(tm, BITWUZLA_KIND_ARRAY_STORE, children[0], children[1], children[2]);
263 auto it = symbols.find(value);
264 if (it == symbols.end())
267 return this->translatedNodes.at(it->second);
271 auto childNodes = node->getChildren();
272 auto ext = triton::ast::getInteger<triton::usize>(childNodes[0]);
273 return bitwuzla_mk_term1_indexed1(tm, BITWUZLA_KIND_BV_SIGN_EXTEND, children[1], ext);
277 const auto& symVar =
reinterpret_cast<VariableNode*
>(node.get())->getSymbolicVariable();
278 auto size = symVar->getSize();
279 auto sort = this->bvSorts.find(size);
280 if (sort == this->bvSorts.end()) {
281 sort = this->bvSorts.insert({size, bitwuzla_mk_bv_sort(tm, size)}).first;
287 if (size <=
sizeof(uint64_t) * 8) {
288 return bitwuzla_mk_bv_value_uint64(tm, sort->second,
static_cast<uint64_t
>(value));
293 auto n = (BitwuzlaTerm) 0;
295 for (
auto it = variables.begin(); it != variables.end(); ++it) {
296 if (it->second == symVar) {
303 n = bitwuzla_mk_const(tm, sort->second, symVar->getName().c_str());
304 variables[n] = symVar;
311 auto childNodes = node->getChildren();
312 auto ext = triton::ast::getInteger<triton::usize>(childNodes[0]);
313 return bitwuzla_mk_term1_indexed1(tm, BITWUZLA_KIND_BV_ZERO_EXTEND,children[1], ext);
TRITON_EXPORT BitwuzlaTerm convert(const SharedAbstractNode &node, Bitwuzla *bzla)
Converts to Bitwuzla's AST.
TRITON_EXPORT const std::map< size_t, BitwuzlaSort > & getBitvectorSorts(void) const
Returns bitvector sorts.
TRITON_EXPORT ~TritonToBitwuzla()
Destructor.
TRITON_EXPORT const std::unordered_map< BitwuzlaTerm, triton::engines::symbolic::SharedSymbolicVariable > & getVariables(void) const
Returns symbolic variables and its assosiated Bitwuzla terms to process the solver model.
TRITON_EXPORT TritonToBitwuzla(bool eval=false)
Constructor.
The exception class used by all AST lifting (e.g z3 <-> triton).
std::vector< SharedAbstractNode > childrenExtraction(const SharedAbstractNode &node, bool unroll, bool revert)
Returns node and all its children of an AST sorted topologically. If unroll is true,...
std::shared_ptr< triton::ast::AbstractNode > SharedAbstractNode
Shared Abstract Node.
constexpr triton::uint32 byte
byte size in bit
std::uint32_t uint32
unisgned 32-bits
std::string toString(const T &obj)
Converts an object to a string.