libTriton  version 0.7 build 1409
astContext.hpp
Go to the documentation of this file.
1 /*
3 ** Copyright (C) - Triton
4 **
5 ** This program is under the terms of the BSD License.
6 */
7 
8 #ifndef TRITON_AST_CONTEXT_H
9 #define TRITON_AST_CONTEXT_H
10 
11 #include <map>
12 #include <memory>
13 #include <vector>
14 
15 #include <triton/ast.hpp>
17 #include <triton/dllexport.hpp>
18 #include <triton/exceptions.hpp>
19 #include <triton/modes.hpp>
20 
21 
22 
24 namespace triton {
30  /* Forward declarations */
31  namespace engines {
32  namespace symbolic {
33  class SymbolicExpression;
34  using SharedSymbolicExpression = std::shared_ptr<triton::engines::symbolic::SymbolicExpression>;
35 
36  class SymbolicVariable;
37  using SharedSymbolicVariable = std::shared_ptr<triton::engines::symbolic::SymbolicVariable>;
38  };
39  };
40 
42  namespace ast {
49 
51  class AstContext : public std::enable_shared_from_this<AstContext> {
52  private:
55 
58 
60  std::map<std::string, std::pair<triton::ast::SharedAbstractNode, triton::uint512>> valueMapping;
61 
62  public:
64  TRITON_EXPORT AstContext(const triton::modes::SharedModes& modes);
65 
67  TRITON_EXPORT AstContext(const AstContext& other);
68 
70  TRITON_EXPORT ~AstContext();
71 
73  TRITON_EXPORT AstContext& operator=(const AstContext& other);
74 
76  TRITON_EXPORT SharedAbstractNode assert_(const SharedAbstractNode& expr);
77 
79  TRITON_EXPORT SharedAbstractNode bv(triton::uint512 value, triton::uint32 size);
80 
82  TRITON_EXPORT SharedAbstractNode bvadd(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
83 
85  TRITON_EXPORT SharedAbstractNode bvand(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
86 
88  TRITON_EXPORT SharedAbstractNode bvashr(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
89 
91  TRITON_EXPORT SharedAbstractNode bvfalse(void);
92 
94  TRITON_EXPORT SharedAbstractNode bvlshr(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
95 
97  TRITON_EXPORT SharedAbstractNode bvmul(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
98 
100  TRITON_EXPORT SharedAbstractNode bvnand(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
101 
103  TRITON_EXPORT SharedAbstractNode bvneg(const SharedAbstractNode& expr);
104 
106  TRITON_EXPORT SharedAbstractNode bvnor(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
107 
109  TRITON_EXPORT SharedAbstractNode bvnot(const SharedAbstractNode& expr);
110 
112  TRITON_EXPORT SharedAbstractNode bvor(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
113 
115  TRITON_EXPORT SharedAbstractNode bvrol(const SharedAbstractNode& expr, triton::uint32 rot);
116 
118  TRITON_EXPORT SharedAbstractNode bvrol(const SharedAbstractNode& expr, const SharedAbstractNode& rot);
119 
121  TRITON_EXPORT SharedAbstractNode bvror(const SharedAbstractNode& expr, triton::uint32 rot);
122 
124  TRITON_EXPORT SharedAbstractNode bvror(const SharedAbstractNode& expr, const SharedAbstractNode& rot);
125 
127  TRITON_EXPORT SharedAbstractNode bvsdiv(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
128 
130  TRITON_EXPORT SharedAbstractNode bvsge(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
131 
133  TRITON_EXPORT SharedAbstractNode bvsgt(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
134 
136  TRITON_EXPORT SharedAbstractNode bvshl(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
137 
139  TRITON_EXPORT SharedAbstractNode bvsle(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
140 
142  TRITON_EXPORT SharedAbstractNode bvslt(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
143 
145  TRITON_EXPORT SharedAbstractNode bvsmod(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
146 
148  TRITON_EXPORT SharedAbstractNode bvsrem(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
149 
151  TRITON_EXPORT SharedAbstractNode bvsub(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
152 
154  TRITON_EXPORT SharedAbstractNode bvtrue(void);
155 
157  TRITON_EXPORT SharedAbstractNode bvudiv(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
158 
160  TRITON_EXPORT SharedAbstractNode bvuge(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
161 
163  TRITON_EXPORT SharedAbstractNode bvugt(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
164 
166  TRITON_EXPORT SharedAbstractNode bvule(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
167 
169  TRITON_EXPORT SharedAbstractNode bvult(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
170 
172  TRITON_EXPORT SharedAbstractNode bvurem(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
173 
175  TRITON_EXPORT SharedAbstractNode bvxnor(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
176 
178  TRITON_EXPORT SharedAbstractNode bvxor(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
179 
181  template <typename T> SharedAbstractNode compound(const T& exprs) {
182  SharedAbstractNode node = std::make_shared<CompoundNode>(exprs, this->shared_from_this());
183  if (node == nullptr)
184  throw triton::exceptions::Ast("Node builders - Not enough memory");
185  node->init();
186  return node;
187  }
188 
190  TRITON_EXPORT SharedAbstractNode concat(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
191 
193  template <typename T> SharedAbstractNode concat(const T& exprs) {
194  SharedAbstractNode node = std::make_shared<ConcatNode>(exprs, this->shared_from_this());
195  if (node == nullptr)
196  throw triton::exceptions::Ast("Node builders - Not enough memory");
197  node->init();
198  return node;
199  }
200 
202  TRITON_EXPORT SharedAbstractNode declare(const SharedAbstractNode& var);
203 
205  TRITON_EXPORT SharedAbstractNode distinct(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
206 
208  TRITON_EXPORT SharedAbstractNode equal(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
209 
211  TRITON_EXPORT SharedAbstractNode extract(triton::uint32 high, triton::uint32 low, const SharedAbstractNode& expr);
212 
214  TRITON_EXPORT SharedAbstractNode iff(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
215 
217  TRITON_EXPORT SharedAbstractNode integer(triton::uint512 value);
218 
220  TRITON_EXPORT SharedAbstractNode ite(const SharedAbstractNode& ifExpr, const SharedAbstractNode& thenExpr, const SharedAbstractNode& elseExpr);
221 
223  TRITON_EXPORT SharedAbstractNode land(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
224 
226  template <typename T> SharedAbstractNode land(const T& exprs) {
227  SharedAbstractNode node = std::make_shared<LandNode>(exprs, this->shared_from_this());
228  if (node == nullptr)
229  throw triton::exceptions::Ast("Node builders - Not enough memory");
230  node->init();
231  return node;
232  }
233 
235  TRITON_EXPORT SharedAbstractNode let(std::string alias, const SharedAbstractNode& expr2, const SharedAbstractNode& expr3);
236 
238  TRITON_EXPORT SharedAbstractNode lnot(const SharedAbstractNode& expr);
239 
241  TRITON_EXPORT SharedAbstractNode lor(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
242 
244  template <typename T> SharedAbstractNode lor(const T& exprs) {
245  SharedAbstractNode node = std::make_shared<LorNode>(exprs, this->shared_from_this());
246  if (node == nullptr)
247  throw triton::exceptions::Ast("Node builders - Not enough memory");
248  node->init();
249  return node;
250  }
251 
254 
256  TRITON_EXPORT SharedAbstractNode string(std::string value);
257 
259  TRITON_EXPORT SharedAbstractNode sx(triton::uint32 sizeExt, const SharedAbstractNode& expr);
260 
263 
265  TRITON_EXPORT SharedAbstractNode zx(triton::uint32 sizeExt, const SharedAbstractNode& expr);
266 
268  TRITON_EXPORT void initVariable(const std::string& name, const triton::uint512& value, const SharedAbstractNode& node);
269 
271  TRITON_EXPORT void updateVariable(const std::string& name, const triton::uint512& value);
272 
274  SharedAbstractNode getVariableNode(const std::string& name);
275 
277  TRITON_EXPORT const triton::uint512& getVariableValue(const std::string& varName) const;
278 
280  TRITON_EXPORT void setRepresentationMode(triton::uint32 mode);
281 
283  TRITON_EXPORT triton::uint32 getRepresentationMode(void) const;
284 
286  TRITON_EXPORT std::ostream& print(std::ostream& stream, AbstractNode* node);
287  };
288 
290  using SharedAstContext = std::shared_ptr<triton::ast::AstContext>;
291 
293  };
295 };
296 
297 #endif /* TRITON_AST_CONTEXT_H */
TRITON_EXPORT SharedAbstractNode assert_(const SharedAbstractNode &expr)
AST C++ API - assert node builder.
Definition: astContext.cpp:48
std::shared_ptr< triton::ast::AstContext > SharedAstContext
Shared AST context.
Definition: ast.hpp:64
TRITON_EXPORT SharedAbstractNode iff(const SharedAbstractNode &expr1, const SharedAbstractNode &expr2)
AST C++ API - iff node builder.
Definition: astContext.cpp:613
TRITON_EXPORT SharedAbstractNode bvnor(const SharedAbstractNode &expr1, const SharedAbstractNode &expr2)
AST C++ API - bvnor node builder.
Definition: astContext.cpp:209
TRITON_EXPORT void setRepresentationMode(triton::uint32 mode)
Sets the representation mode for this astContext.
Definition: astContext.cpp:791
TRITON_EXPORT SharedAbstractNode string(std::string value)
AST C++ API - string node builder.
Definition: astContext.cpp:693
TRITON_EXPORT SharedAbstractNode distinct(const SharedAbstractNode &expr1, const SharedAbstractNode &expr2)
AST C++ API - distinct node builder.
Definition: astContext.cpp:580
TRITON_EXPORT const triton::uint512 & getVariableValue(const std::string &varName) const
Gets a variable value from its name.
Definition: astContext.cpp:782
TRITON_EXPORT SharedAbstractNode land(const SharedAbstractNode &expr1, const SharedAbstractNode &expr2)
AST C++ API - land node builder.
Definition: astContext.cpp:640
Abstract node.
Definition: ast.hpp:67
TRITON_EXPORT SharedAbstractNode ite(const SharedAbstractNode &ifExpr, const SharedAbstractNode &thenExpr, const SharedAbstractNode &elseExpr)
AST C++ API - ite node builder.
Definition: astContext.cpp:631
TRITON_EXPORT SharedAbstractNode let(std::string alias, const SharedAbstractNode &expr2, const SharedAbstractNode &expr3)
AST C++ API - let node builder.
Definition: astContext.cpp:653
AST Context - Used as AST builder.
Definition: astContext.hpp:51
TRITON_EXPORT SharedAbstractNode bvrol(const SharedAbstractNode &expr, triton::uint32 rot)
AST C++ API - bvrol node builder.
Definition: astContext.cpp:259
TRITON_EXPORT SharedAbstractNode declare(const SharedAbstractNode &var)
AST C++ API - declare node builder.
Definition: astContext.cpp:571
TRITON_EXPORT SharedAbstractNode bvsdiv(const SharedAbstractNode &expr1, const SharedAbstractNode &expr2)
AST C++ API - bvsdiv node builder.
Definition: astContext.cpp:333
SharedAbstractNode compound(const T &exprs)
AST C++ API - compound node builder.
Definition: astContext.hpp:181
The Triton namespace.
Definition: api.cpp:226
TRITON_EXPORT SharedAbstractNode lnot(const SharedAbstractNode &expr)
AST C++ API - lnot node builder.
Definition: astContext.cpp:662
TRITON_EXPORT SharedAbstractNode integer(triton::uint512 value)
AST C++ API - integer node builder.
Definition: astContext.cpp:622
The exception class used by all AST nodes.
Definition: exceptions.hpp:341
TRITON_EXPORT SharedAbstractNode bvslt(const SharedAbstractNode &expr1, const SharedAbstractNode &expr2)
AST C++ API - bvslt node builder.
Definition: astContext.cpp:400
TRITON_EXPORT SharedAbstractNode bvand(const SharedAbstractNode &expr1, const SharedAbstractNode &expr2)
AST C++ API - bvand node builder.
Definition: astContext.cpp:86
TRITON_EXPORT SharedAbstractNode bvsrem(const SharedAbstractNode &expr1, const SharedAbstractNode &expr2)
AST C++ API - bvsrem node builder.
Definition: astContext.cpp:418
TRITON_EXPORT SharedAbstractNode reference(const triton::engines::symbolic::SharedSymbolicExpression &expr)
AST C++ API - reference node builder.
Definition: astContext.cpp:684
TRITON_EXPORT SharedAbstractNode bvurem(const SharedAbstractNode &expr1, const SharedAbstractNode &expr2)
AST C++ API - bvurem node builder.
Definition: astContext.cpp:512
std::shared_ptr< triton::ast::AbstractNode > SharedAbstractNode
Shared Abstract Node.
Definition: ast.hpp:58
TRITON_EXPORT SharedAbstractNode bvuge(const SharedAbstractNode &expr1, const SharedAbstractNode &expr2)
AST C++ API - bvuge node builder.
Definition: astContext.cpp:476
TRITON_EXPORT SharedAbstractNode bvnot(const SharedAbstractNode &expr)
AST C++ API - bvnot node builder.
Definition: astContext.cpp:218
TRITON_EXPORT SharedAbstractNode extract(triton::uint32 high, triton::uint32 low, const SharedAbstractNode &expr)
AST C++ API - extract node builder.
Definition: astContext.cpp:598
TRITON_EXPORT SharedAbstractNode bvxnor(const SharedAbstractNode &expr1, const SharedAbstractNode &expr2)
AST C++ API - bvxnor node builder.
Definition: astContext.cpp:521
TRITON_EXPORT SharedAbstractNode sx(triton::uint32 sizeExt, const SharedAbstractNode &expr)
AST C++ API - sx node builder.
Definition: astContext.cpp:702
TRITON_EXPORT ~AstContext()
Destructor.
Definition: astContext.cpp:34
TRITON_EXPORT SharedAbstractNode bvor(const SharedAbstractNode &expr1, const SharedAbstractNode &expr2)
AST C++ API - bvor node builder.
Definition: astContext.cpp:227
TRITON_EXPORT SharedAbstractNode bvadd(const SharedAbstractNode &expr1, const SharedAbstractNode &expr2)
AST C++ API - bvadd node builder.
Definition: astContext.cpp:66
std::shared_ptr< triton::modes::Modes > SharedModes
Shared Modes.
Definition: modes.hpp:63
SharedAbstractNode land(const T &exprs)
AST C++ API - land node builder.
Definition: astContext.hpp:226
SharedAbstractNode concat(const T &exprs)
AST C++ API - concat node builder.
Definition: astContext.hpp:193
TRITON_EXPORT SharedAbstractNode bvror(const SharedAbstractNode &expr, triton::uint32 rot)
AST C++ API - bvror node builder.
Definition: astContext.cpp:296
TRITON_EXPORT SharedAbstractNode bvashr(const SharedAbstractNode &expr1, const SharedAbstractNode &expr2)
AST C++ API - bvashr node builder.
Definition: astContext.cpp:118
TRITON_EXPORT void updateVariable(const std::string &name, const triton::uint512 &value)
Updates a variable value in this context.
Definition: astContext.cpp:766
TRITON_EXPORT SharedAbstractNode bv(triton::uint512 value, triton::uint32 size)
AST C++ API - bv node builder.
Definition: astContext.cpp:57
TRITON_EXPORT void initVariable(const std::string &name, const triton::uint512 &value, const SharedAbstractNode &node)
Initializes a variable in the context.
Definition: astContext.cpp:757
TRITON_EXPORT SharedAbstractNode bvmul(const SharedAbstractNode &expr1, const SharedAbstractNode &expr2)
AST C++ API - bvmul node builder.
Definition: astContext.cpp:171
TRITON_EXPORT SharedAbstractNode bvsmod(const SharedAbstractNode &expr1, const SharedAbstractNode &expr2)
AST C++ API - bvsmod node builder.
Definition: astContext.cpp:409
TRITON_EXPORT SharedAbstractNode bvneg(const SharedAbstractNode &expr)
AST C++ API - bvneg node builder.
Definition: astContext.cpp:200
TRITON_EXPORT SharedAbstractNode concat(const SharedAbstractNode &expr1, const SharedAbstractNode &expr2)
AST C++ API - concat node builder.
Definition: astContext.cpp:558
std::shared_ptr< triton::engines::symbolic::SymbolicExpression > SharedSymbolicExpression
Shared Symbolic Expression.
Definition: ast.hpp:39
TRITON_EXPORT SharedAbstractNode zx(triton::uint32 sizeExt, const SharedAbstractNode &expr)
AST C++ API - zx node builder.
Definition: astContext.cpp:742
TRITON_EXPORT SharedAbstractNode bvxor(const SharedAbstractNode &expr1, const SharedAbstractNode &expr2)
AST C++ API - bvxor node builder.
Definition: astContext.cpp:530
std::uint32_t uint32
unisgned 32-bits
Definition: tritonTypes.hpp:31
SharedAbstractNode lor(const T &exprs)
AST C++ API - lor node builder.
Definition: astContext.hpp:244
TRITON_EXPORT SharedAbstractNode bvlshr(const SharedAbstractNode &expr1, const SharedAbstractNode &expr2)
AST C++ API - bvlshr node builder.
Definition: astContext.cpp:147
TRITON_EXPORT SharedAbstractNode bvnand(const SharedAbstractNode &expr1, const SharedAbstractNode &expr2)
AST C++ API - bvnand node builder.
Definition: astContext.cpp:191
TRITON_EXPORT SharedAbstractNode equal(const SharedAbstractNode &expr1, const SharedAbstractNode &expr2)
AST C++ API - equal node builder.
Definition: astContext.cpp:589
TRITON_EXPORT SharedAbstractNode bvsge(const SharedAbstractNode &expr1, const SharedAbstractNode &expr2)
AST C++ API - bvsge node builder.
Definition: astContext.cpp:349
TRITON_EXPORT AstContext(const triton::modes::SharedModes &modes)
Constructor.
Definition: astContext.cpp:21
TRITON_EXPORT triton::uint32 getRepresentationMode(void) const
Gets the representations mode of this astContext.
Definition: astContext.cpp:796
TRITON_EXPORT SharedAbstractNode bvtrue(void)
AST C++ API - bvtrue node builder.
Definition: astContext.cpp:451
std::shared_ptr< triton::engines::symbolic::SymbolicVariable > SharedSymbolicVariable
Shared Symbolic variable.
Definition: ast.hpp:42
TRITON_EXPORT SharedAbstractNode bvudiv(const SharedAbstractNode &expr1, const SharedAbstractNode &expr2)
AST C++ API - bvudiv node builder.
Definition: astContext.cpp:460
TRITON_EXPORT SharedAbstractNode bvult(const SharedAbstractNode &expr1, const SharedAbstractNode &expr2)
AST C++ API - bvult node builder.
Definition: astContext.cpp:503
TRITON_EXPORT SharedAbstractNode bvshl(const SharedAbstractNode &expr1, const SharedAbstractNode &expr2)
AST C++ API - bvshl node builder.
Definition: astContext.cpp:367
TRITON_EXPORT SharedAbstractNode bvule(const SharedAbstractNode &expr1, const SharedAbstractNode &expr2)
AST C++ API - bvule node builder.
Definition: astContext.cpp:494
TRITON_EXPORT SharedAbstractNode variable(const triton::engines::symbolic::SharedSymbolicVariable &symVar)
AST C++ API - variable node builder.
Definition: astContext.cpp:717
TRITON_EXPORT SharedAbstractNode bvugt(const SharedAbstractNode &expr1, const SharedAbstractNode &expr2)
AST C++ API - bvugt node builder.
Definition: astContext.cpp:485
TRITON_EXPORT AstContext & operator=(const AstContext &other)
Operator.
Definition: astContext.cpp:39
TRITON_EXPORT SharedAbstractNode bvsgt(const SharedAbstractNode &expr1, const SharedAbstractNode &expr2)
AST C++ API - bvsgt node builder.
Definition: astContext.cpp:358
boost::multiprecision::uint512_t uint512
unsigned 512-bits
Definition: tritonTypes.hpp:43
TRITON_EXPORT std::ostream & print(std::ostream &stream, AbstractNode *node)
Print the given node with this context representation.
Definition: astContext.cpp:801
TRITON_EXPORT SharedAbstractNode bvsub(const SharedAbstractNode &expr1, const SharedAbstractNode &expr2)
AST C++ API - bvsub node builder.
Definition: astContext.cpp:427
TRITON_EXPORT SharedAbstractNode bvfalse(void)
AST C++ API - bvfalse node builder.
Definition: astContext.cpp:138
TRITON_EXPORT SharedAbstractNode bvsle(const SharedAbstractNode &expr1, const SharedAbstractNode &expr2)
AST C++ API - bvsle node builder.
Definition: astContext.cpp:391
SharedAbstractNode getVariableNode(const std::string &name)
Gets a variable node from its name.
Definition: astContext.cpp:773
TRITON_EXPORT SharedAbstractNode lor(const SharedAbstractNode &expr1, const SharedAbstractNode &expr2)
AST C++ API - lor node builder.
Definition: astContext.cpp:671