1
//! \file
2
/*
3
**  Copyright (C) - Triton
4
**
5
**  This program is under the terms of the Apache License 2.0.
6
*/
7

8
#ifndef TRITON_AST_CONTEXT_H
9
#define TRITON_AST_CONTEXT_H
10

11
#include <deque>
12
#include <list>
13
#include <memory>
14
#include <unordered_map>
15
#include <vector>
16

17
#include <triton/ast.hpp>
18
#include <triton/astRepresentation.hpp>
19
#include <triton/dllexport.hpp>
20
#include <triton/exceptions.hpp>
21
#include <triton/modes.hpp>
22

23

24

25
//! The Triton namespace
26
namespace triton {
27
/*!
28
 *  \addtogroup triton
29
 *  @{
30
 */
31

32
  /* Forward declarations */
33
  namespace engines {
34
    namespace symbolic {
35
      class SymbolicExpression;
36
      using SharedSymbolicExpression = std::shared_ptr<triton::engines::symbolic::SymbolicExpression>;
37

38
      class SymbolicVariable;
39
      using SharedSymbolicVariable = std::shared_ptr<triton::engines::symbolic::SymbolicVariable>;
40
    };
41
  };
42

43
  //! The AST namespace
44
  namespace ast {
45
  /*!
46
   *  \ingroup triton
47
   *  \addtogroup ast
48
   *  @{
49
   */
50

51
    //! \class AstContext
52
    /*! \brief AST Context - Used as AST builder. */
53
    class AstContext : public std::enable_shared_from_this<AstContext> {
54
      private:
55
        //! Modes API
56
        triton::modes::SharedModes modes;
57

58
        //! String formater for ast
59
        triton::ast::representations::AstRepresentation astRepresentation;
60

61
        //! Maps a concrete value and ast node for a variable name.
62
        std::unordered_map<std::string, std::pair<triton::ast::WeakAbstractNode, triton::uint512>> valueMapping;
63

64
        //! The list of nodes
65
        std::deque<SharedAbstractNode> nodes;
66

67
      public:
68
        //! Constructor
69
        TRITON_EXPORT AstContext(const triton::modes::SharedModes& modes);
70

71
        //! Destructor
72
        TRITON_EXPORT ~AstContext();
73

74
        //! Operator
75
        TRITON_EXPORT AstContext& operator=(const AstContext& other);
76

77
        //! Collect new nodes
78
        TRITON_EXPORT SharedAbstractNode collect(const SharedAbstractNode& node);
79

80
        //! Garbage unused nodes.
81
        TRITON_EXPORT void garbage(void);
82

83
        //! AST C++ API - assert node builder
84
        TRITON_EXPORT SharedAbstractNode assert_(const SharedAbstractNode& expr);
85

86
        //! AST C++ API - bv node builder
87
        TRITON_EXPORT SharedAbstractNode bv(const triton::uint512& value, triton::uint32 size);
88

89
        //! AST C++ API - bvadd node builder
90
        TRITON_EXPORT SharedAbstractNode bvadd(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
91

92
        //! AST C++ API - bvand node builder
93
        TRITON_EXPORT SharedAbstractNode bvand(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
94

95
        //! AST C++ API - bvashr node builder
96
        TRITON_EXPORT SharedAbstractNode bvashr(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
97

98
        //! AST C++ API - bvfalse node builder
99
        TRITON_EXPORT SharedAbstractNode bvfalse(void);
100

101
        //! AST C++ API - bvlshr node builder
102
        TRITON_EXPORT SharedAbstractNode bvlshr(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
103

104
        //! AST C++ API - bvmul node builder
105
        TRITON_EXPORT SharedAbstractNode bvmul(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
106

107
        //! AST C++ API - bvnand node builder
108
        TRITON_EXPORT SharedAbstractNode bvnand(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
109

110
        //! AST C++ API - bvneg node builder
111
        TRITON_EXPORT SharedAbstractNode bvneg(const SharedAbstractNode& expr);
112

113
        //! AST C++ API - bvnor node builder
114
        TRITON_EXPORT SharedAbstractNode bvnor(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
115

116
        //! AST C++ API - bvnot node builder
117
        TRITON_EXPORT SharedAbstractNode bvnot(const SharedAbstractNode& expr);
118

119
        //! AST C++ API - bvor node builder
120
        TRITON_EXPORT SharedAbstractNode bvor(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
121

122
        //! AST C++ API - bvrol node builder
123
        TRITON_EXPORT SharedAbstractNode bvrol(const SharedAbstractNode& expr, triton::uint32 rot);
124

125
        //! AST C++ API - bvrol node builder
126
        TRITON_EXPORT SharedAbstractNode bvrol(const SharedAbstractNode& expr, const SharedAbstractNode& rot);
127

128
        //! AST C++ API - bvror node builder
129
        TRITON_EXPORT SharedAbstractNode bvror(const SharedAbstractNode& expr, triton::uint32 rot);
130

131
        //! AST C++ API - bvror node builder
132
        TRITON_EXPORT SharedAbstractNode bvror(const SharedAbstractNode& expr, const SharedAbstractNode& rot);
133

134
        //! AST C++ API - bvsdiv node builder
135
        TRITON_EXPORT SharedAbstractNode bvsdiv(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
136

137
        //! AST C++ API - bvsge node builder
138
        TRITON_EXPORT SharedAbstractNode bvsge(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
139

140
        //! AST C++ API - bvsgt node builder
141
        TRITON_EXPORT SharedAbstractNode bvsgt(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
142

143
        //! AST C++ API - bvshl node builder
144
        TRITON_EXPORT SharedAbstractNode bvshl(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
145

146
        //! AST C++ API - bvsle node builder
147
        TRITON_EXPORT SharedAbstractNode bvsle(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
148

149
        //! AST C++ API - bvslt node builder
150
        TRITON_EXPORT SharedAbstractNode bvslt(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
151

152
        //! AST C++ API - bvsmod node builder
153
        TRITON_EXPORT SharedAbstractNode bvsmod(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
154

155
        //! AST C++ API - bvsrem node builder
156
        TRITON_EXPORT SharedAbstractNode bvsrem(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
157

158
        //! AST C++ API - bvsub node builder
159
        TRITON_EXPORT SharedAbstractNode bvsub(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
160

161
        //! AST C++ API - bvtrue node builder
162
        TRITON_EXPORT SharedAbstractNode bvtrue(void);
163

164
        //! AST C++ API - bvudiv node builder
165
        TRITON_EXPORT SharedAbstractNode bvudiv(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
166

167
        //! AST C++ API - bvuge node builder
168
        TRITON_EXPORT SharedAbstractNode bvuge(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
169

170
        //! AST C++ API - bvugt node builder
171
        TRITON_EXPORT SharedAbstractNode bvugt(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
172

173
        //! AST C++ API - bvule node builder
174
        TRITON_EXPORT SharedAbstractNode bvule(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
175

176
        //! AST C++ API - bvult node builder
177
        TRITON_EXPORT SharedAbstractNode bvult(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
178

179
        //! AST C++ API - bvurem node builder
180
        TRITON_EXPORT SharedAbstractNode bvurem(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
181

182
        //! AST C++ API - bvxnor node builder
183
        TRITON_EXPORT SharedAbstractNode bvxnor(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
184

185
        //! AST C++ API - bvxor node builder
186
        TRITON_EXPORT SharedAbstractNode bvxor(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
187

188
        //! AST C++ API - compound node builder
189 1
        template <typename T> SharedAbstractNode compound(const T& exprs) {
190 1
          SharedAbstractNode node = std::make_shared<CompoundNode>(exprs, this->shared_from_this());
191 1
          if (node == nullptr)
192 0
            throw triton::exceptions::Ast("Node builders - Not enough memory");
193 1
          node->init();
194 1
          return this->collect(node);
195
        }
196

197
        //! AST C++ API - concat node builder
198
        TRITON_EXPORT SharedAbstractNode concat(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
199

200
        //! AST C++ API - concat node builder
201 1
        template <typename T> SharedAbstractNode concat(const T& exprs) {
202 1
          SharedAbstractNode node = std::make_shared<ConcatNode>(exprs, this->shared_from_this());
203 1
          if (node == nullptr)
204 0
            throw triton::exceptions::Ast("Node builders - Not enough memory");
205 1
          node->init();
206

207 1
          if (this->modes->isModeEnabled(triton::modes::CONSTANT_FOLDING)) {
208 1
            if (node->isSymbolized() == false) {
209 1
              return this->bv(node->evaluate(), node->getBitvectorSize());
210
            }
211
          }
212

213 1
          return this->collect(node);
214
        }
215

216
        //! AST C++ API - declare node builder
217
        TRITON_EXPORT SharedAbstractNode declare(const SharedAbstractNode& var);
218

219
        //! AST C++ API - distinct node builder
220
        TRITON_EXPORT SharedAbstractNode distinct(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
221

222
        //! AST C++ API - equal node builder
223
        TRITON_EXPORT SharedAbstractNode equal(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
224

225
        //! AST C++ API - extract node builder
226
        TRITON_EXPORT SharedAbstractNode extract(triton::uint32 high, triton::uint32 low, const SharedAbstractNode& expr);
227

228
        //! AST C++ API - forall node builder
229 1
        template <typename T> SharedAbstractNode forall(const T& vars, const SharedAbstractNode& body) {
230 1
          SharedAbstractNode node = std::make_shared<ForallNode>(vars, body);
231 1
          if (node == nullptr)
232 0
            throw triton::exceptions::Ast("Node builders - Not enough memory");
233 1
          node->init();
234 1
          return this->collect(node);
235
        }
236

237
        //! AST C++ API - iff node builder
238
        TRITON_EXPORT SharedAbstractNode iff(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
239

240
        //! AST C++ API - integer node builder
241
        TRITON_EXPORT SharedAbstractNode integer(const triton::uint512& value);
242

243
        //! AST C++ API - ite node builder
244
        TRITON_EXPORT SharedAbstractNode ite(const SharedAbstractNode& ifExpr, const SharedAbstractNode& thenExpr, const SharedAbstractNode& elseExpr);
245

246
        //! AST C++ API - land node builder
247
        TRITON_EXPORT SharedAbstractNode land(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
248

249
        //! AST C++ API - land node builder
250 1
        template <typename T> SharedAbstractNode land(const T& exprs) {
251 1
          SharedAbstractNode node = std::make_shared<LandNode>(exprs, this->shared_from_this());
252 1
          if (node == nullptr)
253 0
            throw triton::exceptions::Ast("Node builders - Not enough memory");
254 1
          node->init();
255 1
          return this->collect(node);
256
        }
257

258
        //! AST C++ API - let node builder
259
        TRITON_EXPORT SharedAbstractNode let(std::string alias, const SharedAbstractNode& expr2, const SharedAbstractNode& expr3);
260

261
        //! AST C++ API - lnot node builder
262
        TRITON_EXPORT SharedAbstractNode lnot(const SharedAbstractNode& expr);
263

264
        //! AST C++ API - lor node builder
265
        TRITON_EXPORT SharedAbstractNode lor(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
266

267
        //! AST C++ API - lor node builder
268 1
        template <typename T> SharedAbstractNode lor(const T& exprs) {
269 1
          SharedAbstractNode node = std::make_shared<LorNode>(exprs, this->shared_from_this());
270 1
          if (node == nullptr)
271 0
            throw triton::exceptions::Ast("Node builders - Not enough memory");
272 1
          node->init();
273 1
          return this->collect(node);
274
        }
275

276
        //! AST C++ API - lxor node builder
277
        TRITON_EXPORT SharedAbstractNode lxor(const SharedAbstractNode& expr1, const SharedAbstractNode& expr2);
278

279
        //! AST C++ API - lxor node builder
280 1
        template <typename T> SharedAbstractNode lxor(const T& exprs) {
281 1
          SharedAbstractNode node = std::make_shared<LxorNode>(exprs, this->shared_from_this());
282 1
          if (node == nullptr)
283 0
            throw triton::exceptions::Ast("Node builders - Not enough memory");
284 1
          node->init();
285 1
          return this->collect(node);
286
        }
287

288
        //! AST C++ API - reference node builder
289
        TRITON_EXPORT SharedAbstractNode reference(const triton::engines::symbolic::SharedSymbolicExpression& expr);
290

291
        //! AST C++ API - string node builder
292
        TRITON_EXPORT SharedAbstractNode string(std::string value);
293

294
        //! AST C++ API - sx node builder
295
        TRITON_EXPORT SharedAbstractNode sx(triton::uint32 sizeExt, const SharedAbstractNode& expr);
296

297
        //! AST C++ API - variable node builder
298
        TRITON_EXPORT SharedAbstractNode variable(const triton::engines::symbolic::SharedSymbolicVariable& symVar);
299

300
        //! AST C++ API - zx node builder
301
        TRITON_EXPORT SharedAbstractNode zx(triton::uint32 sizeExt, const SharedAbstractNode& expr);
302

303
        //! Initializes a variable in the context
304
        TRITON_EXPORT void initVariable(const std::string& name, const triton::uint512& value, const SharedAbstractNode& node);
305

306
        //! Updates a variable value in this context
307
        TRITON_EXPORT void updateVariable(const std::string& name, const triton::uint512& value);
308

309
        //! Gets a variable node from its name.
310
        SharedAbstractNode getVariableNode(const std::string& name);
311

312
        //! Gets a variable value from its name.
313
        TRITON_EXPORT const triton::uint512& getVariableValue(const std::string& name) const;
314

315
        //! Sets the representation mode for this astContext
316
        TRITON_EXPORT void setRepresentationMode(triton::uint32 mode);
317

318
        //! Gets the representations mode of this astContext
319
        TRITON_EXPORT triton::uint32 getRepresentationMode(void) const;
320

321
        //! Prints the given node with this context representation
322
        TRITON_EXPORT std::ostream& print(std::ostream& stream, AbstractNode* node);
323

324
      private:
325
        //! Return simplified extraction.
326
        SharedAbstractNode simplify_extract(triton::uint32 high, triton::uint32 low, const SharedAbstractNode& expr);
327
    };
328

329
    //! Shared AST context
330
    using SharedAstContext = std::shared_ptr<triton::ast::AstContext>;
331

332
  /*! @} End of ast namespace */
333
  };
334
/*! @} End of triton namespace */
335
};
336

337
#endif /* TRITON_AST_CONTEXT_H */

Read our documentation on viewing source code .

Loading