libTriton version 1.0 build 1590
Loading...
Searching...
No Matches
pyAstNode.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
11#include <triton/ast.hpp>
12#include <triton/astContext.hpp>
13#include <triton/coreUtils.hpp>
14#include <triton/exceptions.hpp>
15
16#include <iostream>
17
18
19
20/* setup doctest context
21
22>>> from triton import TritonContext, ARCH
23>>> ctxt = TritonContext()
24>>> ctxt.setArchitecture(ARCH.X86_64)
25
26*/
27
145namespace triton {
146 namespace bindings {
147 namespace python {
148
150 void AstNode_dealloc(PyObject* self) {
151 std::cout << std::flush;
152 PyAstNode_AsAstNode(self) = nullptr; // decref the shared_ptr
153 Py_TYPE(self)->tp_free((PyObject*)self);
154 }
155
156
157 static PyObject* AstNode_equalTo(PyObject* self, PyObject* other) {
158 try {
159 if (other == nullptr || !PyAstNode_Check(other))
160 return PyErr_Format(PyExc_TypeError, "AstNode::equalTo(): Expected a AstNode as argument.");
161
162 if (PyAstNode_AsAstNode(self)->equalTo(PyAstNode_AsAstNode(other)))
163 Py_RETURN_TRUE;
164
165 Py_RETURN_FALSE;
166 }
167 catch (const triton::exceptions::Exception& e) {
168 return PyErr_Format(PyExc_TypeError, "%s", e.what());
169 }
170 }
171
172
173 static PyObject* AstNode_evaluate(PyObject* self, PyObject* noarg) {
174 try {
175 return PyLong_FromUint512(PyAstNode_AsAstNode(self)->evaluate());
176 }
177 catch (const triton::exceptions::Exception& e) {
178 return PyErr_Format(PyExc_TypeError, "%s", e.what());
179 }
180 }
181
182
183 static PyObject* AstNode_getBitvectorMask(PyObject* self, PyObject* noarg) {
184 try {
185 return PyLong_FromUint512(PyAstNode_AsAstNode(self)->getBitvectorMask());
186 }
187 catch (const triton::exceptions::Exception& e) {
188 return PyErr_Format(PyExc_TypeError, "%s", e.what());
189 }
190 }
191
192
193 static PyObject* AstNode_getBitvectorSize(PyObject* self, PyObject* noarg) {
194 try {
195 return PyLong_FromUint32(PyAstNode_AsAstNode(self)->getBitvectorSize());
196 }
197 catch (const triton::exceptions::Exception& e) {
198 return PyErr_Format(PyExc_TypeError, "%s", e.what());
199 }
200 }
201
202
203 static PyObject* AstNode_getChildren(PyObject* self, PyObject* noarg) {
204 try {
205 PyObject* children;
207
208 triton::usize size = node->getChildren().size();
209 children = xPyList_New(size);
210 for (triton::usize index = 0; index < size; index++)
211 PyList_SetItem(children, index, PyAstNode(node->getChildren()[index]));
212
213 return children;
214 }
215 catch (const triton::exceptions::Exception& e) {
216 return PyErr_Format(PyExc_TypeError, "%s", e.what());
217 }
218 }
219
220
221 static PyObject* AstNode_getHash(PyObject* self, PyObject* noarg) {
222 try {
223 return PyLong_FromUint512(PyAstNode_AsAstNode(self)->getHash());
224 }
225 catch (const triton::exceptions::Exception& e) {
226 return PyErr_Format(PyExc_TypeError, "%s", e.what());
227 }
228 }
229
230
231 static PyObject* AstNode_getInteger(PyObject* self, PyObject* noarg) {
233
234 if (node->getType() != triton::ast::INTEGER_NODE)
235 return PyErr_Format(PyExc_TypeError, "AstNode::getInteger(): Only available on INTEGER_NODE type.");
236
237 try {
238 return PyLong_FromUint512(triton::ast::getInteger<triton::uint512>(node));
239 }
240 catch (const triton::exceptions::Exception& e) {
241 return PyErr_Format(PyExc_TypeError, "%s", e.what());
242 }
243 }
244
245
246 static PyObject* AstNode_getLevel(PyObject* self, PyObject* noarg) {
247 try {
248 return PyLong_FromUint32(PyAstNode_AsAstNode(self)->getLevel());
249 }
250 catch (const triton::exceptions::Exception& e) {
251 return PyErr_Format(PyExc_TypeError, "%s", e.what());
252 }
253 }
254
255
256 static PyObject* AstNode_getParents(PyObject* self, PyObject* noarg) {
257 try {
258 PyObject* ret = nullptr;
259 auto parents = PyAstNode_AsAstNode(self)->getParents();
260 ret = xPyList_New(parents.size());
261 triton::uint32 index = 0;
262 for (auto& sp : parents)
263 PyList_SetItem(ret, index++, PyAstNode(sp));
264 return ret;
265 }
266 catch (const triton::exceptions::Exception& e) {
267 return PyErr_Format(PyExc_TypeError, "%s", e.what());
268 }
269 }
270
271
272 static PyObject* AstNode_getString(PyObject* self, PyObject* noarg) {
274
275 if (node->getType() != triton::ast::STRING_NODE)
276 return PyErr_Format(PyExc_TypeError, "AstNode::getString(): Only available on STRING_NODE type.");
277
278 try {
279 return Py_BuildValue("s", reinterpret_cast<triton::ast::StringNode*>(node.get())->getString().c_str());
280 }
281 catch (const triton::exceptions::Exception& e) {
282 return PyErr_Format(PyExc_TypeError, "%s", e.what());
283 }
284 }
285
286
287 static PyObject* AstNode_getSymbolicExpression(PyObject* self, PyObject* noarg) {
289
290 if (node->getType() != triton::ast::REFERENCE_NODE)
291 return PyErr_Format(PyExc_TypeError, "AstNode::getSymbolicExpression(): Only available on REFERENCE_NODE type.");
292
293 try {
294 return PySymbolicExpression(reinterpret_cast<triton::ast::ReferenceNode*>(node.get())->getSymbolicExpression());
295 }
296 catch (const triton::exceptions::Exception& e) {
297 return PyErr_Format(PyExc_TypeError, "%s", e.what());
298 }
299 }
300
301
302 static PyObject* AstNode_getSymbolicVariable(PyObject* self, PyObject* noarg) {
304
305 if (node->getType() != triton::ast::VARIABLE_NODE)
306 return PyErr_Format(PyExc_TypeError, "AstNode::getSymbolicVariable(): Only available on VARIABLE_NODE type.");
307
308 try {
309 return PySymbolicVariable(reinterpret_cast<triton::ast::VariableNode*>(node.get())->getSymbolicVariable());
310 }
311 catch (const triton::exceptions::Exception& e) {
312 return PyErr_Format(PyExc_TypeError, "%s", e.what());
313 }
314 }
315
316
317 static PyObject* AstNode_getType(PyObject* self, PyObject* noarg) {
318 try {
319 return PyLong_FromUint32(PyAstNode_AsAstNode(self)->getType());
320 }
321 catch (const triton::exceptions::Exception& e) {
322 return PyErr_Format(PyExc_TypeError, "%s", e.what());
323 }
324 }
325
326
327 static PyObject* AstNode_isArray(PyObject* self, PyObject* noarg) {
328 try {
329 if (PyAstNode_AsAstNode(self)->isArray())
330 Py_RETURN_TRUE;
331 Py_RETURN_FALSE;
332 }
333 catch (const triton::exceptions::Exception& e) {
334 return PyErr_Format(PyExc_TypeError, "%s", e.what());
335 }
336 }
337
338
339 static PyObject* AstNode_isLogical(PyObject* self, PyObject* noarg) {
340 try {
341 if (PyAstNode_AsAstNode(self)->isLogical())
342 Py_RETURN_TRUE;
343 Py_RETURN_FALSE;
344 }
345 catch (const triton::exceptions::Exception& e) {
346 return PyErr_Format(PyExc_TypeError, "%s", e.what());
347 }
348 }
349
350
351 static PyObject* AstNode_isSigned(PyObject* self, PyObject* noarg) {
352 try {
353 if (PyAstNode_AsAstNode(self)->isSigned())
354 Py_RETURN_TRUE;
355 Py_RETURN_FALSE;
356 }
357 catch (const triton::exceptions::Exception& e) {
358 return PyErr_Format(PyExc_TypeError, "%s", e.what());
359 }
360 }
361
362
363 static PyObject* AstNode_isSymbolized(PyObject* self, PyObject* noarg) {
364 try {
365 if (PyAstNode_AsAstNode(self)->isSymbolized())
366 Py_RETURN_TRUE;
367 Py_RETURN_FALSE;
368 }
369 catch (const triton::exceptions::Exception& e) {
370 return PyErr_Format(PyExc_TypeError, "%s", e.what());
371 }
372 }
373
374
375 static PyObject* AstNode_setChild(PyObject* self, PyObject* args) {
376 try {
377 PyObject* index = nullptr;
378 PyObject* node = nullptr;
379 triton::uint32 idx;
381
382 if (PyArg_ParseTuple(args, "|OO", &index, &node) == false) {
383 return PyErr_Format(PyExc_TypeError, "AstNode::setChild(): Invalid number of arguments");
384 }
385
386 if (index == nullptr || (!PyLong_Check(index) && !PyInt_Check(index)))
387 return PyErr_Format(PyExc_TypeError, "AstNode::setChild(): Expected an index (integer) as first argument.");
388
389 if (node == nullptr || !PyAstNode_Check(node))
390 return PyErr_Format(PyExc_TypeError, "AstNode::setChild(): Expected a AstNode as second argument.");
391
392 idx = PyLong_AsUint32(index);
393 src = PyAstNode_AsAstNode(node);
394 dst = PyAstNode_AsAstNode(self);
395 dst->setChild(idx, src);
396
397 Py_RETURN_TRUE;
398 }
399 catch (const triton::exceptions::Exception& e) {
400 return PyErr_Format(PyExc_TypeError, "%s", e.what());
401 }
402 }
403
404
405 #if !defined(IS_PY3_8) || !IS_PY3_8
406 static int AstNode_print(PyObject* self, void* io, int s) {
407 std::cout << PyAstNode_AsAstNode(self);
408 return 0;
409 }
410 #endif
411
412
413 #if !defined(IS_PY3) || !IS_PY3
414 static int AstNode_cmp(PyObject* a, PyObject* b) {
415 if (!PyAstNode_Check(a) || !PyAstNode_Check(b)) {
416 PyErr_Format(
417 PyExc_TypeError,
418 "__cmp__ not supported between instances of '%.100s' and '%.100s'",
419 a->ob_type->tp_name,
420 b->ob_type->tp_name);
421 return -1;
422 }
423 auto ha = PyAstNode_AsAstNode(a)->getHash();
424 auto hb = PyAstNode_AsAstNode(b)->getHash();
425 return (ha == hb ? 0 : (ha > hb ? 1 : -1));
426 }
427 #endif
428
429
430 static long AstNode_hash(PyObject* self) {
431 return static_cast<long>(reinterpret_cast<intptr_t>(PyAstNode_AsAstNode(self).get()) & 0xffffffff);
432 }
433
434
435 static PyObject* AstNode_str(PyObject* self) {
436 try {
437 return PyStr_FromFormat("%s", triton::utils::toString(PyAstNode_AsAstNode(self)).c_str());
438 }
439 catch (const triton::exceptions::Exception& e) {
440 return PyErr_Format(PyExc_TypeError, "%s", e.what());
441 }
442 }
443
444
445 static PyObject* AstNode_operatorAdd(PyObject* self, PyObject* other) {
446 try {
447 #if IS_PY3
448 if ((PyInt_Check(self) || PyLong_Check(self)) && PyAstNode_Check(other)) {
449 auto& nother = PyAstNode_AsAstNode(other);
450 auto ast = nother->getContext();
451 auto nself = ast->bv(PyLong_AsUint512(self), nother->getBitvectorSize());
452 return PyAstNode(ast->bvadd(nself, nother));
453 }
454
455 if (PyAstNode_Check(self) && (PyInt_Check(other) || PyLong_Check(other))) {
456 auto& nself = PyAstNode_AsAstNode(self);
457 auto ast = nself->getContext();
458 auto nother = ast->bv(PyLong_AsUint512(other), nself->getBitvectorSize());
459 return PyAstNode(ast->bvadd(nself, nother));
460 }
461 #endif
462
463 if (PyAstNode_Check(self) && PyAstNode_Check(other))
464 return PyAstNode(PyAstNode_AsAstNode(self)->getContext()->bvadd(PyAstNode_AsAstNode(self), PyAstNode_AsAstNode(other)));
465
466 return PyErr_Format(PyExc_TypeError, "AstNode::operatorAdd(): Expected a AstNode as arguments.");
467 }
468 catch (const triton::exceptions::Exception& e) {
469 return PyErr_Format(PyExc_TypeError, "%s", e.what());
470 }
471 }
472
473
474 static PyObject* AstNode_operatorSub(PyObject* self, PyObject* other) {
475 try {
476 #if IS_PY3
477 if ((PyInt_Check(self) || PyLong_Check(self)) && PyAstNode_Check(other)) {
478 auto& nother = PyAstNode_AsAstNode(other);
479 auto ast = nother->getContext();
480 auto nself = ast->bv(PyLong_AsUint512(self), nother->getBitvectorSize());
481 return PyAstNode(ast->bvsub(nself, nother));
482 }
483
484 if (PyAstNode_Check(self) && (PyInt_Check(other) || PyLong_Check(other))) {
485 auto& nself = PyAstNode_AsAstNode(self);
486 auto ast = nself->getContext();
487 auto nother = ast->bv(PyLong_AsUint512(other), nself->getBitvectorSize());
488 return PyAstNode(ast->bvsub(nself, nother));
489 }
490 #endif
491
492 if (PyAstNode_Check(self) && PyAstNode_Check(other))
493 return PyAstNode(PyAstNode_AsAstNode(self)->getContext()->bvsub(PyAstNode_AsAstNode(self), PyAstNode_AsAstNode(other)));
494
495 return PyErr_Format(PyExc_TypeError, "AstNode::operatorSub(): Expected a AstNode as arguments.");
496 }
497 catch (const triton::exceptions::Exception& e) {
498 return PyErr_Format(PyExc_TypeError, "%s", e.what());
499 }
500 }
501
502
503 static PyObject* AstNode_operatorMul(PyObject* self, PyObject* other) {
504 try {
505 #if IS_PY3
506 if ((PyInt_Check(self) || PyLong_Check(self)) && PyAstNode_Check(other)) {
507 auto& nother = PyAstNode_AsAstNode(other);
508 auto ast = nother->getContext();
509 auto nself = ast->bv(PyLong_AsUint512(self), nother->getBitvectorSize());
510 return PyAstNode(ast->bvmul(nself, nother));
511 }
512
513 if (PyAstNode_Check(self) && (PyInt_Check(other) || PyLong_Check(other))) {
514 auto& nself = PyAstNode_AsAstNode(self);
515 auto ast = nself->getContext();
516 auto nother = ast->bv(PyLong_AsUint512(other), nself->getBitvectorSize());
517 return PyAstNode(ast->bvmul(nself, nother));
518 }
519 #endif
520
521 if (PyAstNode_Check(self) && PyAstNode_Check(other))
522 return PyAstNode(PyAstNode_AsAstNode(self)->getContext()->bvmul(PyAstNode_AsAstNode(self), PyAstNode_AsAstNode(other)));
523
524 return PyErr_Format(PyExc_TypeError, "AstNode::operatorMul(): Expected a AstNode as arguments.");
525 }
526 catch (const triton::exceptions::Exception& e) {
527 return PyErr_Format(PyExc_TypeError, "%s", e.what());
528 }
529 }
530
531
532 static PyObject* AstNode_operatorDiv(PyObject* self, PyObject* other) {
533 try {
534 #if IS_PY3
535 if ((PyInt_Check(self) || PyLong_Check(self)) && PyAstNode_Check(other)) {
536 auto& nother = PyAstNode_AsAstNode(other);
537 auto ast = nother->getContext();
538 auto nself = ast->bv(PyLong_AsUint512(self), nother->getBitvectorSize());
539 return PyAstNode(ast->bvudiv(nself, nother));
540 }
541
542 if (PyAstNode_Check(self) && (PyInt_Check(other) || PyLong_Check(other))) {
543 auto& nself = PyAstNode_AsAstNode(self);
544 auto ast = nself->getContext();
545 auto nother = ast->bv(PyLong_AsUint512(other), nself->getBitvectorSize());
546 return PyAstNode(ast->bvudiv(nself, nother));
547 }
548 #endif
549
550 if (PyAstNode_Check(self) && PyAstNode_Check(other))
551 return PyAstNode(PyAstNode_AsAstNode(self)->getContext()->bvudiv(PyAstNode_AsAstNode(self), PyAstNode_AsAstNode(other)));
552
553 return PyErr_Format(PyExc_TypeError, "AstNode::operatorDiv(): Expected a AstNode as arguments.");
554 }
555 catch (const triton::exceptions::Exception& e) {
556 return PyErr_Format(PyExc_TypeError, "%s", e.what());
557 }
558 }
559
560
561 static PyObject* AstNode_operatorRem(PyObject* self, PyObject* other) {
562 try {
563 #if IS_PY3
564 if ((PyInt_Check(self) || PyLong_Check(self)) && PyAstNode_Check(other)) {
565 auto& nother = PyAstNode_AsAstNode(other);
566 auto ast = nother->getContext();
567 auto nself = ast->bv(PyLong_AsUint512(self), nother->getBitvectorSize());
568 return PyAstNode(ast->bvurem(nself, nother));
569 }
570
571 if (PyAstNode_Check(self) && (PyInt_Check(other) || PyLong_Check(other))) {
572 auto& nself = PyAstNode_AsAstNode(self);
573 auto ast = nself->getContext();
574 auto nother = ast->bv(PyLong_AsUint512(other), nself->getBitvectorSize());
575 return PyAstNode(ast->bvurem(nself, nother));
576 }
577 #endif
578
579 if (PyAstNode_Check(self) && PyAstNode_Check(other))
580 return PyAstNode(PyAstNode_AsAstNode(self)->getContext()->bvurem(PyAstNode_AsAstNode(self), PyAstNode_AsAstNode(other)));
581
582 return PyErr_Format(PyExc_TypeError, "AstNode::operatorMod(): Expected a AstNode as arguments.");
583 }
584 catch (const triton::exceptions::Exception& e) {
585 return PyErr_Format(PyExc_TypeError, "%s", e.what());
586 }
587 }
588
589
590 static PyObject* AstNode_operatorNeg(PyObject* node) {
591 try {
592 if (!PyAstNode_Check(node))
593 return PyErr_Format(PyExc_TypeError, "AstNode::operatorNeg(): Expected a AstNode as argument.");
594 return PyAstNode(PyAstNode_AsAstNode(node)->getContext()->bvneg(PyAstNode_AsAstNode(node)));
595 }
596 catch (const triton::exceptions::Exception& e) {
597 return PyErr_Format(PyExc_TypeError, "%s", e.what());
598 }
599 }
600
601
602 static PyObject* AstNode_operatorNot(PyObject* node) {
603 try {
604 if (!PyAstNode_Check(node))
605 return PyErr_Format(PyExc_TypeError, "AstNode::operatorNot(): Expected a AstNode as argument.");
606 return PyAstNode(PyAstNode_AsAstNode(node)->getContext()->bvnot(PyAstNode_AsAstNode(node)));
607 }
608 catch (const triton::exceptions::Exception& e) {
609 return PyErr_Format(PyExc_TypeError, "%s", e.what());
610 }
611 }
612
613
614 static PyObject* AstNode_operatorShl(PyObject* self, PyObject* other) {
615 try {
616 #if IS_PY3
617 if ((PyInt_Check(self) || PyLong_Check(self)) && PyAstNode_Check(other)) {
618 auto& nother = PyAstNode_AsAstNode(other);
619 auto ast = nother->getContext();
620 auto nself = ast->bv(PyLong_AsUint512(self), nother->getBitvectorSize());
621 return PyAstNode(ast->bvshl(nself, nother));
622 }
623
624 if (PyAstNode_Check(self) && (PyInt_Check(other) || PyLong_Check(other))) {
625 auto& nself = PyAstNode_AsAstNode(self);
626 auto ast = nself->getContext();
627 auto nother = ast->bv(PyLong_AsUint512(other), nself->getBitvectorSize());
628 return PyAstNode(ast->bvshl(nself, nother));
629 }
630 #endif
631
632 if (PyAstNode_Check(self) && PyAstNode_Check(other))
633 return PyAstNode(PyAstNode_AsAstNode(self)->getContext()->bvshl(PyAstNode_AsAstNode(self), PyAstNode_AsAstNode(other)));
634
635 return PyErr_Format(PyExc_TypeError, "AstNode::operatorShl(): Expected a AstNode as arguments.");
636 }
637 catch (const triton::exceptions::Exception& e) {
638 return PyErr_Format(PyExc_TypeError, "%s", e.what());
639 }
640 }
641
642
643 static PyObject* AstNode_operatorShr(PyObject* self, PyObject* other) {
644 try {
645 #if IS_PY3
646 if ((PyInt_Check(self) || PyLong_Check(self)) && PyAstNode_Check(other)) {
647 auto& nother = PyAstNode_AsAstNode(other);
648 auto ast = nother->getContext();
649 auto nself = ast->bv(PyLong_AsUint512(self), nother->getBitvectorSize());
650 return PyAstNode(ast->bvlshr(nself, nother));
651 }
652
653 if (PyAstNode_Check(self) && (PyInt_Check(other) || PyLong_Check(other))) {
654 auto& nself = PyAstNode_AsAstNode(self);
655 auto ast = nself->getContext();
656 auto nother = ast->bv(PyLong_AsUint512(other), nself->getBitvectorSize());
657 return PyAstNode(ast->bvlshr(nself, nother));
658 }
659 #endif
660
661 if (PyAstNode_Check(self) && PyAstNode_Check(other))
662 return PyAstNode(PyAstNode_AsAstNode(self)->getContext()->bvlshr(PyAstNode_AsAstNode(self), PyAstNode_AsAstNode(other)));
663
664 return PyErr_Format(PyExc_TypeError, "AstNode::operatorShr(): Expected a AstNode as arguments.");
665 }
666 catch (const triton::exceptions::Exception& e) {
667 return PyErr_Format(PyExc_TypeError, "%s", e.what());
668 }
669 }
670
671
672 static PyObject* AstNode_operatorAnd(PyObject* self, PyObject* other) {
673 try {
674 #if IS_PY3
675 if ((PyInt_Check(self) || PyLong_Check(self)) && PyAstNode_Check(other)) {
676 auto& nother = PyAstNode_AsAstNode(other);
677 auto ast = nother->getContext();
678 auto nself = ast->bv(PyLong_AsUint512(self), nother->getBitvectorSize());
679 return PyAstNode(ast->bvand(nself, nother));
680 }
681
682 if (PyAstNode_Check(self) && (PyInt_Check(other) || PyLong_Check(other))) {
683 auto& nself = PyAstNode_AsAstNode(self);
684 auto ast = nself->getContext();
685 auto nother = ast->bv(PyLong_AsUint512(other), nself->getBitvectorSize());
686 return PyAstNode(ast->bvand(nself, nother));
687 }
688 #endif
689
690 if (PyAstNode_Check(self) && PyAstNode_Check(other))
691 return PyAstNode(PyAstNode_AsAstNode(self)->getContext()->bvand(PyAstNode_AsAstNode(self), PyAstNode_AsAstNode(other)));
692
693 return PyErr_Format(PyExc_TypeError, "AstNode::operatorAnd(): Expected a AstNode as arguments.");
694 }
695 catch (const triton::exceptions::Exception& e) {
696 return PyErr_Format(PyExc_TypeError, "%s", e.what());
697 }
698 }
699
700
701 static PyObject* AstNode_operatorXor(PyObject* self, PyObject* other) {
702 try {
703 #if IS_PY3
704 if ((PyInt_Check(self) || PyLong_Check(self)) && PyAstNode_Check(other)) {
705 auto& nother = PyAstNode_AsAstNode(other);
706 auto ast = nother->getContext();
707 auto nself = ast->bv(PyLong_AsUint512(self), nother->getBitvectorSize());
708 return PyAstNode(ast->bvxor(nself, nother));
709 }
710
711 if (PyAstNode_Check(self) && (PyInt_Check(other) || PyLong_Check(other))) {
712 auto& nself = PyAstNode_AsAstNode(self);
713 auto ast = nself->getContext();
714 auto nother = ast->bv(PyLong_AsUint512(other), nself->getBitvectorSize());
715 return PyAstNode(ast->bvxor(nself, nother));
716 }
717 #endif
718
719 if (PyAstNode_Check(self) && PyAstNode_Check(other))
720 return PyAstNode(PyAstNode_AsAstNode(self)->getContext()->bvxor(PyAstNode_AsAstNode(self), PyAstNode_AsAstNode(other)));
721
722 return PyErr_Format(PyExc_TypeError, "AstNode::operatorXor(): Expected a AstNode as arguments.");
723 }
724 catch (const triton::exceptions::Exception& e) {
725 return PyErr_Format(PyExc_TypeError, "%s", e.what());
726 }
727 }
728
729
730 static PyObject* AstNode_operatorOr(PyObject* self, PyObject* other) {
731 try {
732 #if IS_PY3
733 if ((PyInt_Check(self) || PyLong_Check(self)) && PyAstNode_Check(other)) {
734 auto& nother = PyAstNode_AsAstNode(other);
735 auto ast = nother->getContext();
736 auto nself = ast->bv(PyLong_AsUint512(self), nother->getBitvectorSize());
737 return PyAstNode(ast->bvor(nself, nother));
738 }
739
740 if (PyAstNode_Check(self) && (PyInt_Check(other) || PyLong_Check(other))) {
741 auto& nself = PyAstNode_AsAstNode(self);
742 auto ast = nself->getContext();
743 auto nother = ast->bv(PyLong_AsUint512(other), nself->getBitvectorSize());
744 return PyAstNode(ast->bvor(nself, nother));
745 }
746 #endif
747
748 if (PyAstNode_Check(self) && PyAstNode_Check(other))
749 return PyAstNode(PyAstNode_AsAstNode(self)->getContext()->bvor(PyAstNode_AsAstNode(self), PyAstNode_AsAstNode(other)));
750
751 return PyErr_Format(PyExc_TypeError, "AstNode::operatorOr(): Expected a AstNode as arguments.");
752 }
753 catch (const triton::exceptions::Exception& e) {
754 return PyErr_Format(PyExc_TypeError, "%s", e.what());
755 }
756 }
757
758
759 #if !defined(IS_PY3) || !IS_PY3
760 static int AstNode_coerce(PyObject** self, PyObject** other) {
761 if (PyLong_Check(*other) || PyInt_Check(*other)) {
762 triton::uint512 value = PyLong_AsUint512(*other);
763 triton::uint32 size = PyAstNode_AsAstNode(*self)->getBitvectorSize();
764 if (size) {
765 *other = PyAstNode(PyAstNode_AsAstNode(*self)->getContext()->bv(value, size));
766 Py_INCREF(*self);
767 return 0;
768 }
769 }
770 return 1;
771 }
772 #endif
773
774
775 static PyObject* AstNode_richcompare(PyObject* self, PyObject* other, int op) {
777 triton::ast::SharedAbstractNode node2 = nullptr;
778 PyObject* result = nullptr;
779
780 if (PyLong_Check(other) || PyInt_Check(other)) {
781 triton::uint512 value = PyLong_AsUint512(other);
782 triton::uint32 size = PyAstNode_AsAstNode(self)->getBitvectorSize();
783 if (size) {
784 node2 = PyAstNode_AsAstNode(self)->getContext()->bv(value, size);
785 }
786 }
787
788 else if (PyAstNode_Check(other)) {
789 node2 = PyAstNode_AsAstNode(other);
790 }
791
792 else {
793 result = Py_NotImplemented;
794 Py_INCREF(result);
795 return result;
796 }
797
798 switch (op) {
799 case Py_LT:
800 result = PyAstNode(node1->getContext()->bvult(node1, node2));
801 break;
802 case Py_LE:
803 result = PyAstNode(node1->getContext()->bvule(node1, node2));
804 break;
805 case Py_EQ:
806 result = PyAstNode(node1->getContext()->equal(node1, node2));
807 break;
808 case Py_NE:
809 result = PyAstNode(node1->getContext()->lnot(node1->getContext()->equal(node1, node2)));
810 break;
811 case Py_GT:
812 result = PyAstNode(node1->getContext()->bvugt(node1, node2));
813 break;
814 case Py_GE:
815 result = PyAstNode(node1->getContext()->bvuge(node1, node2));
816 break;
817 default:
818 result = Py_NotImplemented;
819 Py_INCREF(result);
820 Py_DECREF(other);
821 return result;
822 }
823
824 return result;
825 }
826
827
828 static int AstNode_init(AstNode_Object* self, PyObject* args, PyObject* kwds) {
829 return 0;
830 }
831
832
833 static PyObject* AstNode_new(PyTypeObject* type, PyObject* args, PyObject* kwds) {
834 return type->tp_alloc(type, 0);
835 }
836
837
839 PyMethodDef AstNode_callbacks[] = {
840 {"equalTo", AstNode_equalTo, METH_O, ""},
841 {"evaluate", AstNode_evaluate, METH_NOARGS, ""},
842 {"getBitvectorMask", AstNode_getBitvectorMask, METH_NOARGS, ""},
843 {"getBitvectorSize", AstNode_getBitvectorSize, METH_NOARGS, ""},
844 {"getChildren", AstNode_getChildren, METH_NOARGS, ""},
845 {"getHash", AstNode_getHash, METH_NOARGS, ""},
846 {"getInteger", AstNode_getInteger, METH_NOARGS, ""},
847 {"getLevel", AstNode_getLevel, METH_NOARGS, ""},
848 {"getParents", AstNode_getParents, METH_NOARGS, ""},
849 {"getString", AstNode_getString, METH_NOARGS, ""},
850 {"getSymbolicExpression", AstNode_getSymbolicExpression, METH_NOARGS, ""},
851 {"getSymbolicVariable", AstNode_getSymbolicVariable, METH_NOARGS, ""},
852 {"getType", AstNode_getType, METH_NOARGS, ""},
853 {"isArray", AstNode_isArray, METH_NOARGS, ""},
854 {"isLogical", AstNode_isLogical, METH_NOARGS, ""},
855 {"isSigned", AstNode_isSigned, METH_NOARGS, ""},
856 {"isSymbolized", AstNode_isSymbolized, METH_NOARGS, ""},
857 {"setChild", AstNode_setChild, METH_VARARGS, ""},
858 {nullptr, nullptr, 0, nullptr}
859 };
860
861
863 PyNumberMethods AstNode_NumberMethods = {
864 AstNode_operatorAdd, /* nb_add */
865 AstNode_operatorSub, /* nb_subtract */
866 AstNode_operatorMul, /* nb_multiply */
867 #if !defined(IS_PY3) || !IS_PY3
868 AstNode_operatorDiv, /* nb_divide */
869 #endif
870 AstNode_operatorRem, /* nb_remainder */
871 0, /* nb_divmod */
872 0, /* nb_power */
873 AstNode_operatorNeg, /* nb_negative */
874 0, /* nb_positive */
875 0, /* nb_absolute */
876 0, /* nb_nonzero/nb_bool */
877 AstNode_operatorNot, /* nb_invert */
878 AstNode_operatorShl, /* nb_lshift */
879 AstNode_operatorShr, /* nb_rshift */
880 AstNode_operatorAnd, /* nb_and */
881 AstNode_operatorXor, /* nb_xor */
882 AstNode_operatorOr, /* nb_or */
883 #if !defined(IS_PY3) || !IS_PY3
884 AstNode_coerce, /* nb_coerce */
885 #endif
886 0, /* nb_int */
887 0, /* nb_long/nb_reserved */
888 0, /* nb_float */
889 #if !defined(IS_PY3) || !IS_PY3
890 0, /* nb_oct */
891 0, /* nb_hex */
892 #endif
893 AstNode_operatorAdd, /* nb_inplace_add */
894 AstNode_operatorSub, /* nb_inplace_subtract */
895 AstNode_operatorMul, /* nb_inplace_multiply */
896 #if !defined(IS_PY3) || !IS_PY3
897 AstNode_operatorDiv, /* nb_inplace_divide */
898 #endif
899 AstNode_operatorRem, /* nb_inplace_remainder */
900 0, /* nb_inplace_power */
901 AstNode_operatorShl, /* nb_inplace_lshift */
902 AstNode_operatorShr, /* nb_inplace_rshift */
903 AstNode_operatorAnd, /* nb_inplace_and */
904 AstNode_operatorXor, /* nb_inplace_xor */
905 AstNode_operatorOr, /* nb_inplace_or */
906 AstNode_operatorDiv, /* nb_floor_divide */
907 AstNode_operatorDiv, /* nb_true_divide */
908 AstNode_operatorDiv, /* nb_inplace_floor_divide */
909 AstNode_operatorDiv, /* nb_inplace_true_divide */
910 0, /* nb_index */
911 #if defined(IS_PY3) && IS_PY3
912 0, /* nb_matrix_multiply */
913 0, /* nb_inplace_matrix_multiply */
914 #endif
915 };
916
917
918 PyTypeObject AstNode_Type = {
919 PyVarObject_HEAD_INIT(&PyType_Type, 0)
920 "AstNode", /* tp_name */
921 sizeof(AstNode_Object), /* tp_basicsize */
922 0, /* tp_itemsize */
923 (destructor)AstNode_dealloc, /* tp_dealloc */
924 #if IS_PY3_8
925 0, /* tp_vectorcall_offset */
926 #else
927 (printfunc)AstNode_print, /* tp_print */
928 #endif
929 0, /* tp_getattr */
930 0, /* tp_setattr */
931 #if defined(IS_PY3) && IS_PY3
932 0, /* tp_as_async */
933 #else
934 (cmpfunc)AstNode_cmp, /* tp_compare */
935 #endif
936 (reprfunc)AstNode_str, /* tp_repr */
937 &AstNode_NumberMethods, /* tp_as_number */
938 0, /* tp_as_sequence */
939 0, /* tp_as_mapping */
940 (hashfunc)AstNode_hash, /* tp_hash */
941 0, /* tp_call*/
942 (reprfunc)AstNode_str, /* tp_str */
943 0, /* tp_getattro */
944 0, /* tp_setattro */
945 0, /* tp_as_buffer */
946 Py_TPFLAGS_DEFAULT, /* tp_flags */
947 "AstNode objects", /* tp_doc */
948 0, /* tp_traverse */
949 0, /* tp_clear */
950 (richcmpfunc)AstNode_richcompare, /* tp_richcompare */
951 0, /* tp_weaklistoffset */
952 0, /* tp_iter */
953 0, /* tp_iternext */
954 AstNode_callbacks, /* tp_methods */
955 0, /* tp_members */
956 0, /* tp_getset */
957 0, /* tp_base */
958 0, /* tp_dict */
959 0, /* tp_descr_get */
960 0, /* tp_descr_set */
961 0, /* tp_dictoffset */
962 (initproc)AstNode_init, /* tp_init */
963 0, /* tp_alloc */
964 (newfunc)AstNode_new, /* tp_new */
965 0, /* tp_free */
966 0, /* tp_is_gc */
967 0, /* tp_bases */
968 0, /* tp_mro */
969 0, /* tp_cache */
970 0, /* tp_subclasses */
971 0, /* tp_weaklist */
972 0, /* tp_del */
973 #if IS_PY3
974 0, /* tp_version_tag */
975 0, /* tp_finalize */
976 #if IS_PY3_8
977 0, /* tp_vectorcall */
978 #if !IS_PY3_9
979 0, /* bpo-37250: kept for backwards compatibility in CPython 3.8 only */
980 #endif
981 #endif
982 #else
983 0 /* tp_version_tag */
984 #endif
985 };
986
987
989 if (node == nullptr) {
990 Py_INCREF(Py_None);
991 return Py_None;
992 }
993
994 PyType_Ready(&AstNode_Type);
995 // Build the new object the python way (calling operator() on the type) as
996 // it crash otherwise (certainly due to incorrect shared_ptr initialization).
997 auto* object = (triton::bindings::python::AstNode_Object*)PyObject_CallObject((PyObject*) &AstNode_Type, nullptr);
998 if (object != NULL) {
999 object->node = node;
1000 }
1001
1002 return (PyObject*)object;
1003 }
1004
1005 }; /* python namespace */
1006 }; /* bindings namespace */
1007}; /* triton namespace */
Reference node.
Definition ast.hpp:789
String node.
Definition ast.hpp:852
Variable node.
Definition ast.hpp:878
The root class of all exceptions.
TRITON_EXPORT const char * what() const
Returns the exception message.
std::shared_ptr< triton::ast::AbstractNode > SharedAbstractNode
Shared Abstract Node.
Definition ast.hpp:59
PyTypeObject AstNode_Type
pyAstNode type.
PyObject * PySymbolicVariable(const triton::engines::symbolic::SharedSymbolicVariable &symVar)
Creates the SymbolicVariable python class.
PyObject * PyLong_FromUint512(triton::uint512 value)
Returns a pyObject from a triton::uint512.
Definition utils.cpp:410
triton::uint512 PyLong_AsUint512(PyObject *vv)
Returns a triton::uint512 from a pyObject.
Definition utils.cpp:201
PyObject * xPyList_New(Py_ssize_t len)
Creates a PyList and raises an exception if it fails.
triton::uint32 PyLong_AsUint32(PyObject *vv)
Returns a triton::uint32 from a pyObject.
Definition utils.cpp:85
PyObject * PySymbolicExpression(const triton::engines::symbolic::SharedSymbolicExpression &symExpr)
Creates the SymbolicExpression python class.
PyObject * PyLong_FromUint32(triton::uint32 value)
Returns a pyObject from a triton::uint32.
Definition utils.cpp:305
PyObject * PyAstNode(const triton::ast::SharedAbstractNode &node)
Creates the AstNode python class.
std::size_t usize
unsigned MAX_INT 32 or 64 bits according to the CPU.
math::wide_integer::uint512_t uint512
unsigned 512-bits
std::uint32_t uint32
unisgned 32-bits
std::string toString(const T &obj)
Converts an object to a string.
Definition coreUtils.hpp:38
PyMethodDef AstNode_callbacks[]
AstNode methods.
PyNumberMethods AstNode_NumberMethods
AstNode operator methods.
void AstNode_dealloc(PyObject *self)
AstNode destructor.
The Triton namespace.
#define PyAstNode_Check(v)
#define PyAstNode_AsAstNode(v)