libTriton version 1.0 build 1592
Loading...
Searching...
No Matches
liftingToPython.cpp
Go to the documentation of this file.
1
2/*
3** Copyright (C) - Triton
4**
5** This program is under the terms of the Apache License 2.0.
6*/
7
8#include <algorithm>
9#include <map>
10#include <vector>
11
12#include <triton/astEnums.hpp>
15
16
17
18namespace triton {
19 namespace engines {
20 namespace lifters {
21
23 : astCtxt(astCtxt), symbolic(symbolic) {
24 }
25
26
27 void LiftingToPython::requiredFunctions(std::ostream& stream) {
28 stream << "def select(mem, index):" << std::endl;
29 stream << " return mem[index]" << std::endl;
30
31 stream << std::endl;
32 stream << "def store(mem, index, value):" << std::endl;
33 stream << " mem[index] = value" << std::endl;
34 stream << " return mem" << std::endl;
35
36 stream << std::endl;
37 stream << "def sx(bits, value):" << std::endl;
38 stream << " sign_bit = 1 << (bits - 1)" << std::endl;
39 stream << " return (value & (sign_bit - 1)) - (value & sign_bit)" << std::endl;
40
41 stream << std::endl;
42 stream << "def rol(value, rot, bits):" << std::endl;
43 stream << " return ((value << rot) | (value >> (bits - rot))) & ((0b1 << bits) - 1)" << std::endl;
44
45 stream << std::endl;
46 stream << "def ror(value, rot, bits):" << std::endl;
47 stream << " return ((value >> rot) | (value << (bits - rot))) & ((0b1 << bits) - 1)" << std::endl;
48
49 stream << std::endl;
50 stream << "def forall(variables, expr):" << std::endl;
51 stream << " return True" << std::endl;
52
53 stream << std::endl;
54 stream << "def bswap(value, size):" << std::endl;
55 stream << " v = value & 0xff" << std::endl;
56 stream << " for index in range(8, size, 8):" << std::endl;
57 stream << " v <<= 8" << std::endl;
58 stream << " v |= (value >> index) & 0xff" << std::endl;
59 stream << " return v" << std::endl;
60
61 stream << std::endl;
62 }
63
64
65 std::ostream& LiftingToPython::liftToPython(std::ostream& stream, const triton::engines::symbolic::SharedSymbolicExpression& expr, bool icomment) {
66 /* Save the AST representation mode */
67 triton::ast::representations::mode_e mode = this->astCtxt->getRepresentationMode();
68 this->astCtxt->setRepresentationMode(triton::ast::representations::PYTHON_REPRESENTATION);
69
70 /* Collect SSA form */
71 auto ssa = this->symbolic->sliceExpressions(expr);
72 std::vector<triton::usize> symExprs;
73 for (const auto& se : ssa) {
74 symExprs.push_back(se.first);
75 }
76
77 /* Collect used symbolic variables */
78 std::map<triton::usize, triton::engines::symbolic::SharedSymbolicVariable> symVars;
79 for (const auto& n : triton::ast::search(expr->getAst(), triton::ast::VARIABLE_NODE)) {
80 auto var = reinterpret_cast<triton::ast::VariableNode*>(n.get())->getSymbolicVariable();
81 symVars[var->getId()] = var;
82 }
83
84 /* Print required functions */
85 this->requiredFunctions(stream);
86
87 /* Declare arrays if exist */
88 for (const auto& array : triton::ast::search(expr->getAst(), triton::ast::ARRAY_NODE)) {
89 auto n = this->astCtxt->declare(array);
90 stream << n << std::endl;
91 }
92
93 /* Print symbolic variables */
94 for (const auto& var : symVars) {
95 auto n = this->astCtxt->declare(this->astCtxt->variable(var.second));
96 stream << n << std::endl;
97 }
98
99 /* Sort SSA */
100 std::sort(symExprs.begin(), symExprs.end());
101
102 /* Print symbolic expressions */
103 for (const auto& id : symExprs) {
104 auto& e = ssa[id];
105 stream << e->getFormattedExpression();
106 if (icomment && !e->getDisassembly().empty()) {
107 if (e->getComment().empty()) {
108 stream << " # ";
109 }
110 else {
111 stream << " - ";
112 }
113 stream << e->getDisassembly();
114 }
115 stream << std::endl;
116 }
117
118 /* Restore the AST representation mode */
119 this->astCtxt->setRepresentationMode(mode);
120
121 return stream;
122 }
123
124 }; /* lifters namespace */
125 }; /* engines namespace */
126}; /* triton namespace */
Variable node.
Definition ast.hpp:878
TRITON_EXPORT LiftingToPython(const triton::ast::SharedAstContext &astCtxt, triton::engines::symbolic::SymbolicEngine *symbolic)
Constructor.
TRITON_EXPORT std::ostream & liftToPython(std::ostream &stream, const triton::engines::symbolic::SharedSymbolicExpression &expr, bool icomment=false)
Lifts a symbolic expression and all its references to Python format. If icomment is true,...
TRITON_EXPORT std::unordered_map< triton::usize, SharedSymbolicExpression > sliceExpressions(const SharedSymbolicExpression &expr)
Slices all expressions from a given one.
std::shared_ptr< triton::ast::AstContext > SharedAstContext
Shared AST context.
Definition ast.hpp:65
std::deque< SharedAbstractNode > search(const SharedAbstractNode &node, triton::ast::ast_e match)
Returns a deque of collected matched nodes via a depth-first pre order traversal.
Definition ast.cpp:3710
mode_e
All types of representation mode.
Definition astEnums.hpp:98
std::shared_ptr< triton::engines::symbolic::SymbolicExpression > SharedSymbolicExpression
Shared Symbolic Expression.
Definition ast.hpp:40
The Triton namespace.