libTriton version 1.0 build 1590
Loading...
Searching...
No Matches
pyAstContext.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/astContext.hpp>
12#include <triton/config.hpp>
13#include <triton/exceptions.hpp>
14#include <triton/register.hpp>
15#ifdef TRITON_Z3_INTERFACE
16 #include <triton/tritonToZ3.hpp>
17 #include <triton/z3ToTriton.hpp>
18#endif
19
20#include <cstring>
21
22
23
24/* setup doctest context
25
26>>> from __future__ import print_function
27>>> from triton import REG, TritonContext, ARCH, Instruction, AST_REPRESENTATION
28>>> ctxt = TritonContext()
29>>> ctxt.setArchitecture(ARCH.X86_64)
30
31>>> opcode = b"\x48\x31\xD0"
32>>> inst = Instruction()
33
34>>> inst.setOpcode(opcode)
35>>> inst.setAddress(0x400000)
36>>> ctxt.setConcreteRegisterValue(ctxt.registers.rax, 12345)
37>>> ctxt.setConcreteRegisterValue(ctxt.registers.rdx, 67890)
38
39>>> ctxt.processing(inst)
40True
41>>> print(inst)
420x400000: xor rax, rdx
43
44*/
45
362namespace triton {
363 namespace bindings {
364 namespace python {
365
367 void AstContext_dealloc(PyObject* self) {
368 PyAstContext_AsAstContext(self) = nullptr; // decref the shared_ptr
369 Py_TYPE(self)->tp_free((PyObject*)self);
370 }
371
372
373 static PyObject* AstContext_array(PyObject* self, PyObject* op1) {
374 if (!PyLong_Check(op1) && !PyInt_Check(op1))
375 return PyErr_Format(PyExc_TypeError, "array(): expected an integer as first argument");
376
377 try {
378 return PyAstNode(PyAstContext_AsAstContext(self)->array(PyLong_AsUint32(op1)));
379 }
380 catch (const triton::exceptions::Exception& e) {
381 return PyErr_Format(PyExc_TypeError, "%s", e.what());
382 }
383 }
384
385
386 static PyObject* AstContext_assert(PyObject* self, PyObject* op1) {
387 if (!PyAstNode_Check(op1))
388 return PyErr_Format(PyExc_TypeError, "assert_(): expected a AstNode as first argument");
389
390 try {
391 return PyAstNode(PyAstContext_AsAstContext(self)->assert_(PyAstNode_AsAstNode(op1)));
392 }
393 catch (const triton::exceptions::Exception& e) {
394 return PyErr_Format(PyExc_TypeError, "%s", e.what());
395 }
396 }
397
398
399 static PyObject* AstContext_bswap(PyObject* self, PyObject* op1) {
400 if (!PyAstNode_Check(op1))
401 return PyErr_Format(PyExc_TypeError, "bswap(): expected a AstNode as first argument");
402
403 try {
405 }
406 catch (const triton::exceptions::Exception& e) {
407 return PyErr_Format(PyExc_TypeError, "%s", e.what());
408 }
409 }
410
411
412 static PyObject* AstContext_bv(PyObject* self, PyObject* args) {
413 PyObject* op1 = nullptr;
414 PyObject* op2 = nullptr;
415
416 /* Extract arguments */
417 if (PyArg_ParseTuple(args, "|OO", &op1, &op2) == false) {
418 return PyErr_Format(PyExc_TypeError, "bv(): Invalid number of arguments");
419 }
420
421 if (op1 == nullptr || (!PyLong_Check(op1) && !PyInt_Check(op1)))
422 return PyErr_Format(PyExc_TypeError, "bv(): expected an integer as first argument");
423
424 if (op2 == nullptr || (!PyLong_Check(op2) && !PyInt_Check(op2)))
425 return PyErr_Format(PyExc_TypeError, "bv(): expected an integer as second argument");
426
427 try {
429 }
430 catch (const triton::exceptions::Exception& e) {
431 return PyErr_Format(PyExc_TypeError, "%s", e.what());
432 }
433 }
434
435
436 static PyObject* AstContext_bvadd(PyObject* self, PyObject* args) {
437 PyObject* op1 = nullptr;
438 PyObject* op2 = nullptr;
439
440 /* Extract arguments */
441 if (PyArg_ParseTuple(args, "|OO", &op1, &op2) == false) {
442 return PyErr_Format(PyExc_TypeError, "bvadd(): Invalid number of arguments");
443 }
444
445 if (op1 == nullptr || !PyAstNode_Check(op1))
446 return PyErr_Format(PyExc_TypeError, "bvadd(): expected a AstNode as first argument");
447
448 if (op2 == nullptr || !PyAstNode_Check(op2))
449 return PyErr_Format(PyExc_TypeError, "bvadd(): expected a AstNode as second argument");
450
451 try {
453 }
454 catch (const triton::exceptions::Exception& e) {
455 return PyErr_Format(PyExc_TypeError, "%s", e.what());
456 }
457 }
458
459
460 static PyObject* AstContext_bvand(PyObject* self, PyObject* args) {
461 PyObject* op1 = nullptr;
462 PyObject* op2 = nullptr;
463
464 /* Extract arguments */
465 if (PyArg_ParseTuple(args, "|OO", &op1, &op2) == false) {
466 return PyErr_Format(PyExc_TypeError, "bvand(): Invalid number of arguments");
467 }
468
469 if (op1 == nullptr || !PyAstNode_Check(op1))
470 return PyErr_Format(PyExc_TypeError, "bvand(): expected a AstNode as first argument");
471
472 if (op2 == nullptr || !PyAstNode_Check(op2))
473 return PyErr_Format(PyExc_TypeError, "bvand(): expected a AstNode as second argument");
474
475 try {
477 }
478 catch (const triton::exceptions::Exception& e) {
479 return PyErr_Format(PyExc_TypeError, "%s", e.what());
480 }
481 }
482
483
484 static PyObject* AstContext_bvashr(PyObject* self, PyObject* args) {
485 PyObject* op1 = nullptr;
486 PyObject* op2 = nullptr;
487
488 /* Extract arguments */
489 if (PyArg_ParseTuple(args, "|OO", &op1, &op2) == false) {
490 return PyErr_Format(PyExc_TypeError, "bvashr(): Invalid number of arguments");
491 }
492
493 if (op1 == nullptr || !PyAstNode_Check(op1))
494 return PyErr_Format(PyExc_TypeError, "bvashr(): expected a AstNode as first argument");
495
496 if (op2 == nullptr || !PyAstNode_Check(op2))
497 return PyErr_Format(PyExc_TypeError, "bvashr(): expected a AstNode as second argument");
498
499 try {
501 }
502 catch (const triton::exceptions::Exception& e) {
503 return PyErr_Format(PyExc_TypeError, "%s", e.what());
504 }
505 }
506
507
508 static PyObject* AstContext_bvfalse(PyObject* self, PyObject* args) {
509 try {
510 return PyAstNode(PyAstContext_AsAstContext(self)->bvfalse());
511 }
512 catch (const triton::exceptions::Exception& e) {
513 return PyErr_Format(PyExc_TypeError, "%s", e.what());
514 }
515 }
516
517
518 static PyObject* AstContext_bvtrue(PyObject* self, PyObject* args) {
519 try {
520 return PyAstNode(PyAstContext_AsAstContext(self)->bvtrue());
521 }
522 catch (const triton::exceptions::Exception& e) {
523 return PyErr_Format(PyExc_TypeError, "%s", e.what());
524 }
525 }
526
527
528 static PyObject* AstContext_bvlshr(PyObject* self, PyObject* args) {
529 PyObject* op1 = nullptr;
530 PyObject* op2 = nullptr;
531
532 /* Extract arguments */
533 if (PyArg_ParseTuple(args, "|OO", &op1, &op2) == false) {
534 return PyErr_Format(PyExc_TypeError, "bvlshr(): Invalid number of arguments");
535 }
536
537 if (op1 == nullptr || !PyAstNode_Check(op1))
538 return PyErr_Format(PyExc_TypeError, "bvlshr(): expected a AstNode as first argument");
539
540 if (op2 == nullptr || !PyAstNode_Check(op2))
541 return PyErr_Format(PyExc_TypeError, "bvlshr(): expected a AstNode as second argument");
542
543 try {
545 }
546 catch (const triton::exceptions::Exception& e) {
547 return PyErr_Format(PyExc_TypeError, "%s", e.what());
548 }
549 }
550
551
552 static PyObject* AstContext_bvmul(PyObject* self, PyObject* args) {
553 PyObject* op1 = nullptr;
554 PyObject* op2 = nullptr;
555
556 /* Extract arguments */
557 if (PyArg_ParseTuple(args, "|OO", &op1, &op2) == false) {
558 return PyErr_Format(PyExc_TypeError, "bvmul(): Invalid number of arguments");
559 }
560
561 if (op1 == nullptr || !PyAstNode_Check(op1))
562 return PyErr_Format(PyExc_TypeError, "bvmul(): expected a AstNode as first argument");
563
564 if (op2 == nullptr || !PyAstNode_Check(op2))
565 return PyErr_Format(PyExc_TypeError, "bvmul(): expected a AstNode as second argument");
566
567 try {
569 }
570 catch (const triton::exceptions::Exception& e) {
571 return PyErr_Format(PyExc_TypeError, "%s", e.what());
572 }
573 }
574
575
576 static PyObject* AstContext_bvnand(PyObject* self, PyObject* args) {
577 PyObject* op1 = nullptr;
578 PyObject* op2 = nullptr;
579
580 /* Extract arguments */
581 if (PyArg_ParseTuple(args, "|OO", &op1, &op2) == false) {
582 return PyErr_Format(PyExc_TypeError, "bvnand(): Invalid number of arguments");
583 }
584
585 if (op1 == nullptr || !PyAstNode_Check(op1))
586 return PyErr_Format(PyExc_TypeError, "bvnand(): expected a AstNode as first argument");
587
588 if (op2 == nullptr || !PyAstNode_Check(op2))
589 return PyErr_Format(PyExc_TypeError, "bvnand(): expected a AstNode as second argument");
590
591 try {
593 }
594 catch (const triton::exceptions::Exception& e) {
595 return PyErr_Format(PyExc_TypeError, "%s", e.what());
596 }
597 }
598
599
600 static PyObject* AstContext_bvneg(PyObject* self, PyObject* op1) {
601 if (!PyAstNode_Check(op1))
602 return PyErr_Format(PyExc_TypeError, "bvneg(): expected a AstNode as first argument");
603
604 try {
606 }
607 catch (const triton::exceptions::Exception& e) {
608 return PyErr_Format(PyExc_TypeError, "%s", e.what());
609 }
610 }
611
612
613 static PyObject* AstContext_bvnor(PyObject* self, PyObject* args) {
614 PyObject* op1 = nullptr;
615 PyObject* op2 = nullptr;
616
617 /* Extract arguments */
618 if (PyArg_ParseTuple(args, "|OO", &op1, &op2) == false) {
619 return PyErr_Format(PyExc_TypeError, "bvnor(): Invalid number of arguments");
620 }
621
622 if (op1 == nullptr || !PyAstNode_Check(op1))
623 return PyErr_Format(PyExc_TypeError, "bvnor(): expected a AstNode as first argument");
624
625 if (op2 == nullptr || !PyAstNode_Check(op2))
626 return PyErr_Format(PyExc_TypeError, "bvnor(): expected a AstNode as second argument");
627
628 try {
630 }
631 catch (const triton::exceptions::Exception& e) {
632 return PyErr_Format(PyExc_TypeError, "%s", e.what());
633 }
634 }
635
636
637 static PyObject* AstContext_bvnot(PyObject* self, PyObject* op1) {
638 if (!PyAstNode_Check(op1))
639 return PyErr_Format(PyExc_TypeError, "bvnot(): expected a AstNode as first argument");
640
641 try {
643 }
644 catch (const triton::exceptions::Exception& e) {
645 return PyErr_Format(PyExc_TypeError, "%s", e.what());
646 }
647 }
648
649
650 static PyObject* AstContext_bvor(PyObject* self, PyObject* args) {
651 PyObject* op1 = nullptr;
652 PyObject* op2 = nullptr;
653
654 /* Extract arguments */
655 if (PyArg_ParseTuple(args, "|OO", &op1, &op2) == false) {
656 return PyErr_Format(PyExc_TypeError, "bvor(): Invalid number of arguments");
657 }
658
659 if (op1 == nullptr || !PyAstNode_Check(op1))
660 return PyErr_Format(PyExc_TypeError, "bvor(): expected a AstNode as first argument");
661
662 if (op2 == nullptr || !PyAstNode_Check(op2))
663 return PyErr_Format(PyExc_TypeError, "bvor(): expected a AstNode as second argument");
664
665 try {
667 }
668 catch (const triton::exceptions::Exception& e) {
669 return PyErr_Format(PyExc_TypeError, "%s", e.what());
670 }
671 }
672
673
674 static PyObject* AstContext_bvror(PyObject* self, PyObject* args) {
675 PyObject* op1 = nullptr;
676 PyObject* op2 = nullptr;
677
678 /* Extract arguments */
679 if (PyArg_ParseTuple(args, "|OO", &op1, &op2) == false) {
680 return PyErr_Format(PyExc_TypeError, "bvror(): Invalid number of arguments");
681 }
682
683 if (op1 == nullptr || !PyAstNode_Check(op1))
684 return PyErr_Format(PyExc_TypeError, "bvror(): expected a AstNode as first argument");
685
686 if (op2 == nullptr || !PyAstNode_Check(op2))
687 return PyErr_Format(PyExc_TypeError, "bvror(): expected a AstNode as second argument");
688
689 try {
691 }
692 catch (const triton::exceptions::Exception& e) {
693 return PyErr_Format(PyExc_TypeError, "%s", e.what());
694 }
695 }
696
697
698 static PyObject* AstContext_bvrol(PyObject* self, PyObject* args) {
699 PyObject* op1 = nullptr;
700 PyObject* op2 = nullptr;
701
702 /* Extract arguments */
703 if (PyArg_ParseTuple(args, "|OO", &op1, &op2) == false) {
704 return PyErr_Format(PyExc_TypeError, "bvrol(): Invalid number of arguments");
705 }
706
707 if (op1 == nullptr || !PyAstNode_Check(op1))
708 return PyErr_Format(PyExc_TypeError, "bvrol(): expected a AstNode as first argument");
709
710 if (op2 == nullptr || !PyAstNode_Check(op2))
711 return PyErr_Format(PyExc_TypeError, "bvrol(): expected a AstNode as second argument");
712
713 try {
715 }
716 catch (const triton::exceptions::Exception& e) {
717 return PyErr_Format(PyExc_TypeError, "%s", e.what());
718 }
719 }
720
721
722 static PyObject* AstContext_bvsdiv(PyObject* self, PyObject* args) {
723 PyObject* op1 = nullptr;
724 PyObject* op2 = nullptr;
725
726 /* Extract arguments */
727 if (PyArg_ParseTuple(args, "|OO", &op1, &op2) == false) {
728 return PyErr_Format(PyExc_TypeError, "bvsdiv(): Invalid number of arguments");
729 }
730
731 if (op1 == nullptr || !PyAstNode_Check(op1))
732 return PyErr_Format(PyExc_TypeError, "bvsdiv(): expected a AstNode as first argument");
733
734 if (op2 == nullptr || !PyAstNode_Check(op2))
735 return PyErr_Format(PyExc_TypeError, "bvsdiv(): expected a AstNode as second argument");
736
737 try {
739 }
740 catch (const triton::exceptions::Exception& e) {
741 return PyErr_Format(PyExc_TypeError, "%s", e.what());
742 }
743 }
744
745
746 static PyObject* AstContext_bvsge(PyObject* self, PyObject* args) {
747 PyObject* op1 = nullptr;
748 PyObject* op2 = nullptr;
749
750 /* Extract arguments */
751 if (PyArg_ParseTuple(args, "|OO", &op1, &op2) == false) {
752 return PyErr_Format(PyExc_TypeError, "bvsge(): Invalid number of arguments");
753 }
754
755 if (op1 == nullptr || !PyAstNode_Check(op1))
756 return PyErr_Format(PyExc_TypeError, "bvsge(): expected a AstNode as first argument");
757
758 if (op2 == nullptr || !PyAstNode_Check(op2))
759 return PyErr_Format(PyExc_TypeError, "bvsge(): expected a AstNode as second argument");
760
761 try {
763 }
764 catch (const triton::exceptions::Exception& e) {
765 return PyErr_Format(PyExc_TypeError, "%s", e.what());
766 }
767 }
768
769
770 static PyObject* AstContext_bvsgt(PyObject* self, PyObject* args) {
771 PyObject* op1 = nullptr;
772 PyObject* op2 = nullptr;
773
774 /* Extract arguments */
775 if (PyArg_ParseTuple(args, "|OO", &op1, &op2) == false) {
776 return PyErr_Format(PyExc_TypeError, "bvsgt(): Invalid number of arguments");
777 }
778
779 if (op1 == nullptr || !PyAstNode_Check(op1))
780 return PyErr_Format(PyExc_TypeError, "bvsgt(): expected a AstNode as first argument");
781
782 if (op2 == nullptr || !PyAstNode_Check(op2))
783 return PyErr_Format(PyExc_TypeError, "bvsgt(): expected a AstNode as second argument");
784
785 try {
787 }
788 catch (const triton::exceptions::Exception& e) {
789 return PyErr_Format(PyExc_TypeError, "%s", e.what());
790 }
791 }
792
793
794 static PyObject* AstContext_bvshl(PyObject* self, PyObject* args) {
795 PyObject* op1 = nullptr;
796 PyObject* op2 = nullptr;
797
798 /* Extract arguments */
799 if (PyArg_ParseTuple(args, "|OO", &op1, &op2) == false) {
800 return PyErr_Format(PyExc_TypeError, "bvshl(): Invalid number of arguments");
801 }
802
803 if (op1 == nullptr || !PyAstNode_Check(op1))
804 return PyErr_Format(PyExc_TypeError, "bvshl(): expected a AstNode as first argument");
805
806 if (op2 == nullptr || !PyAstNode_Check(op2))
807 return PyErr_Format(PyExc_TypeError, "bvshl(): expected a AstNode as second argument");
808
809 try {
811 }
812 catch (const triton::exceptions::Exception& e) {
813 return PyErr_Format(PyExc_TypeError, "%s", e.what());
814 }
815 }
816
817
818 static PyObject* AstContext_bvsle(PyObject* self, PyObject* args) {
819 PyObject* op1 = nullptr;
820 PyObject* op2 = nullptr;
821
822 /* Extract arguments */
823 if (PyArg_ParseTuple(args, "|OO", &op1, &op2) == false) {
824 return PyErr_Format(PyExc_TypeError, "bvsle(): Invalid number of arguments");
825 }
826
827 if (op1 == nullptr || !PyAstNode_Check(op1))
828 return PyErr_Format(PyExc_TypeError, "bvsle(): expected a AstNode as first argument");
829
830 if (op2 == nullptr || !PyAstNode_Check(op2))
831 return PyErr_Format(PyExc_TypeError, "bvsle(): expected a AstNode as second argument");
832
833 try {
835 }
836 catch (const triton::exceptions::Exception& e) {
837 return PyErr_Format(PyExc_TypeError, "%s", e.what());
838 }
839 }
840
841
842 static PyObject* AstContext_bvslt(PyObject* self, PyObject* args) {
843 PyObject* op1 = nullptr;
844 PyObject* op2 = nullptr;
845
846 /* Extract arguments */
847 if (PyArg_ParseTuple(args, "|OO", &op1, &op2) == false) {
848 return PyErr_Format(PyExc_TypeError, "bvslt(): Invalid number of arguments");
849 }
850
851 if (op1 == nullptr || !PyAstNode_Check(op1))
852 return PyErr_Format(PyExc_TypeError, "bvslt(): expected a AstNode as first argument");
853
854 if (op2 == nullptr || !PyAstNode_Check(op2))
855 return PyErr_Format(PyExc_TypeError, "bvslt(): expected a AstNode as second argument");
856
857 try {
859 }
860 catch (const triton::exceptions::Exception& e) {
861 return PyErr_Format(PyExc_TypeError, "%s", e.what());
862 }
863 }
864
865
866 static PyObject* AstContext_bvsmod(PyObject* self, PyObject* args) {
867 PyObject* op1 = nullptr;
868 PyObject* op2 = nullptr;
869
870 /* Extract arguments */
871 if (PyArg_ParseTuple(args, "|OO", &op1, &op2) == false) {
872 return PyErr_Format(PyExc_TypeError, "bvsmod(): Invalid number of arguments");
873 }
874
875 if (op1 == nullptr || !PyAstNode_Check(op1))
876 return PyErr_Format(PyExc_TypeError, "bvsmod(): expected a AstNode as first argument");
877
878 if (op2 == nullptr || !PyAstNode_Check(op2))
879 return PyErr_Format(PyExc_TypeError, "bvsmod(): expected a AstNode as second argument");
880
881 try {
883 }
884 catch (const triton::exceptions::Exception& e) {
885 return PyErr_Format(PyExc_TypeError, "%s", e.what());
886 }
887 }
888
889
890 static PyObject* AstContext_bvsrem(PyObject* self, PyObject* args) {
891 PyObject* op1 = nullptr;
892 PyObject* op2 = nullptr;
893
894 /* Extract arguments */
895 if (PyArg_ParseTuple(args, "|OO", &op1, &op2) == false) {
896 return PyErr_Format(PyExc_TypeError, "bvsrem(): Invalid number of arguments");
897 }
898
899 if (op1 == nullptr || !PyAstNode_Check(op1))
900 return PyErr_Format(PyExc_TypeError, "bvsrem(): expected a AstNode as first argument");
901
902 if (op2 == nullptr || !PyAstNode_Check(op2))
903 return PyErr_Format(PyExc_TypeError, "bvsrem(): expected a AstNode as second argument");
904
905 try {
907 }
908 catch (const triton::exceptions::Exception& e) {
909 return PyErr_Format(PyExc_TypeError, "%s", e.what());
910 }
911 }
912
913
914 static PyObject* AstContext_bvsub(PyObject* self, PyObject* args) {
915 PyObject* op1 = nullptr;
916 PyObject* op2 = nullptr;
917
918 /* Extract arguments */
919 if (PyArg_ParseTuple(args, "|OO", &op1, &op2) == false) {
920 return PyErr_Format(PyExc_TypeError, "bvsub(): Invalid number of arguments");
921 }
922
923 if (op1 == nullptr || !PyAstNode_Check(op1))
924 return PyErr_Format(PyExc_TypeError, "bvsub(): expected a AstNode as first argument");
925
926 if (op2 == nullptr || !PyAstNode_Check(op2))
927 return PyErr_Format(PyExc_TypeError, "bvsub(): expected a AstNode as second argument");
928
929 try {
931 }
932 catch (const triton::exceptions::Exception& e) {
933 return PyErr_Format(PyExc_TypeError, "%s", e.what());
934 }
935 }
936
937
938 static PyObject* AstContext_bvudiv(PyObject* self, PyObject* args) {
939 PyObject* op1 = nullptr;
940 PyObject* op2 = nullptr;
941
942 /* Extract arguments */
943 if (PyArg_ParseTuple(args, "|OO", &op1, &op2) == false) {
944 return PyErr_Format(PyExc_TypeError, "bvudiv(): Invalid number of arguments");
945 }
946
947 if (op1 == nullptr || !PyAstNode_Check(op1))
948 return PyErr_Format(PyExc_TypeError, "bvudiv(): expected a AstNode as first argument");
949
950 if (op2 == nullptr || !PyAstNode_Check(op2))
951 return PyErr_Format(PyExc_TypeError, "bvudiv(): expected a AstNode as second argument");
952
953 try {
955 }
956 catch (const triton::exceptions::Exception& e) {
957 return PyErr_Format(PyExc_TypeError, "%s", e.what());
958 }
959 }
960
961
962 static PyObject* AstContext_bvuge(PyObject* self, PyObject* args) {
963 PyObject* op1 = nullptr;
964 PyObject* op2 = nullptr;
965
966 /* Extract arguments */
967 if (PyArg_ParseTuple(args, "|OO", &op1, &op2) == false) {
968 return PyErr_Format(PyExc_TypeError, "bvuge(): Invalid number of arguments");
969 }
970
971 if (op1 == nullptr || !PyAstNode_Check(op1))
972 return PyErr_Format(PyExc_TypeError, "bvuge(): expected a AstNode as first argument");
973
974 if (op2 == nullptr || !PyAstNode_Check(op2))
975 return PyErr_Format(PyExc_TypeError, "bvuge(): expected a AstNode as second argument");
976
977 try {
979 }
980 catch (const triton::exceptions::Exception& e) {
981 return PyErr_Format(PyExc_TypeError, "%s", e.what());
982 }
983 }
984
985
986 static PyObject* AstContext_bvugt(PyObject* self, PyObject* args) {
987 PyObject* op1 = nullptr;
988 PyObject* op2 = nullptr;
989
990 /* Extract arguments */
991 if (PyArg_ParseTuple(args, "|OO", &op1, &op2) == false) {
992 return PyErr_Format(PyExc_TypeError, "bvugt(): Invalid number of arguments");
993 }
994
995 if (op1 == nullptr || !PyAstNode_Check(op1))
996 return PyErr_Format(PyExc_TypeError, "bvugt(): expected a AstNode as first argument");
997
998 if (op2 == nullptr || !PyAstNode_Check(op2))
999 return PyErr_Format(PyExc_TypeError, "bvugt(): expected a AstNode as second argument");
1000
1001 try {
1003 }
1004 catch (const triton::exceptions::Exception& e) {
1005 return PyErr_Format(PyExc_TypeError, "%s", e.what());
1006 }
1007 }
1008
1009
1010 static PyObject* AstContext_bvule(PyObject* self, PyObject* args) {
1011 PyObject* op1 = nullptr;
1012 PyObject* op2 = nullptr;
1013
1014 /* Extract arguments */
1015 if (PyArg_ParseTuple(args, "|OO", &op1, &op2) == false) {
1016 return PyErr_Format(PyExc_TypeError, "bvule(): Invalid number of arguments");
1017 }
1018
1019 if (op1 == nullptr || !PyAstNode_Check(op1))
1020 return PyErr_Format(PyExc_TypeError, "bvule(): expected a AstNode as first argument");
1021
1022 if (op2 == nullptr || !PyAstNode_Check(op2))
1023 return PyErr_Format(PyExc_TypeError, "bvule(): expected a AstNode as second argument");
1024
1025 try {
1027 }
1028 catch (const triton::exceptions::Exception& e) {
1029 return PyErr_Format(PyExc_TypeError, "%s", e.what());
1030 }
1031 }
1032
1033
1034 static PyObject* AstContext_bvult(PyObject* self, PyObject* args) {
1035 PyObject* op1 = nullptr;
1036 PyObject* op2 = nullptr;
1037
1038 /* Extract arguments */
1039 if (PyArg_ParseTuple(args, "|OO", &op1, &op2) == false) {
1040 return PyErr_Format(PyExc_TypeError, "bvult(): Invalid number of arguments");
1041 }
1042
1043 if (op1 == nullptr || !PyAstNode_Check(op1))
1044 return PyErr_Format(PyExc_TypeError, "bvult(): expected a AstNode as first argument");
1045
1046 if (op2 == nullptr || !PyAstNode_Check(op2))
1047 return PyErr_Format(PyExc_TypeError, "bvult(): expected a AstNode as second argument");
1048
1049 try {
1051 }
1052 catch (const triton::exceptions::Exception& e) {
1053 return PyErr_Format(PyExc_TypeError, "%s", e.what());
1054 }
1055 }
1056
1057
1058 static PyObject* AstContext_bvurem(PyObject* self, PyObject* args) {
1059 PyObject* op1 = nullptr;
1060 PyObject* op2 = nullptr;
1061
1062 /* Extract arguments */
1063 if (PyArg_ParseTuple(args, "|OO", &op1, &op2) == false) {
1064 return PyErr_Format(PyExc_TypeError, "bvurem(): Invalid number of arguments");
1065 }
1066
1067 if (op1 == nullptr || !PyAstNode_Check(op1))
1068 return PyErr_Format(PyExc_TypeError, "bvurem(): expected a AstNode as first argument");
1069
1070 if (op2 == nullptr || !PyAstNode_Check(op2))
1071 return PyErr_Format(PyExc_TypeError, "bvurem(): expected a AstNode as second argument");
1072
1073 try {
1075 }
1076 catch (const triton::exceptions::Exception& e) {
1077 return PyErr_Format(PyExc_TypeError, "%s", e.what());
1078 }
1079 }
1080
1081
1082 static PyObject* AstContext_bvxnor(PyObject* self, PyObject* args) {
1083 PyObject* op1 = nullptr;
1084 PyObject* op2 = nullptr;
1085
1086 /* Extract arguments */
1087 if (PyArg_ParseTuple(args, "|OO", &op1, &op2) == false) {
1088 return PyErr_Format(PyExc_TypeError, "bvxnor(): Invalid number of arguments");
1089 }
1090
1091 if (op1 == nullptr || !PyAstNode_Check(op1))
1092 return PyErr_Format(PyExc_TypeError, "bvxnor(): expected a AstNode as first argument");
1093
1094 if (op2 == nullptr || !PyAstNode_Check(op2))
1095 return PyErr_Format(PyExc_TypeError, "bvxnor(): expected a AstNode as second argument");
1096
1097 try {
1099 }
1100 catch (const triton::exceptions::Exception& e) {
1101 return PyErr_Format(PyExc_TypeError, "%s", e.what());
1102 }
1103 }
1104
1105
1106 static PyObject* AstContext_bvxor(PyObject* self, PyObject* args) {
1107 PyObject* op1 = nullptr;
1108 PyObject* op2 = nullptr;
1109
1110 /* Extract arguments */
1111 if (PyArg_ParseTuple(args, "|OO", &op1, &op2) == false) {
1112 return PyErr_Format(PyExc_TypeError, "bvxor(): Invalid number of arguments");
1113 }
1114
1115 if (op1 == nullptr || !PyAstNode_Check(op1))
1116 return PyErr_Format(PyExc_TypeError, "bvxor(): expected a AstNode as first argument");
1117
1118 if (op2 == nullptr || !PyAstNode_Check(op2))
1119 return PyErr_Format(PyExc_TypeError, "bvxor(): expected a AstNode as second argument");
1120
1121 try {
1123 }
1124 catch (const triton::exceptions::Exception& e) {
1125 return PyErr_Format(PyExc_TypeError, "%s", e.what());
1126 }
1127 }
1128
1129
1130 static PyObject* AstContext_declare(PyObject* self, PyObject* sort) {
1131 if (!PyAstNode_Check(sort))
1132 return PyErr_Format(PyExc_TypeError, "declare(): expected a AstNode as argument");
1133
1134 try {
1135 return PyAstNode(PyAstContext_AsAstContext(self)->declare(PyAstNode_AsAstNode(sort)));
1136 }
1137 catch (const triton::exceptions::Exception& e) {
1138 return PyErr_Format(PyExc_TypeError, "%s", e.what());
1139 }
1140 }
1141
1142
1143 static PyObject* AstContext_dereference(PyObject* self, PyObject* node) {
1144 if (!PyAstNode_Check(node))
1145 return PyErr_Format(PyExc_TypeError, "dereference(): Expects a AstNode as argument.");
1146
1147 try {
1149 }
1150 catch (const triton::exceptions::Exception& e) {
1151 return PyErr_Format(PyExc_TypeError, "%s", e.what());
1152 }
1153 }
1154
1155
1156 static PyObject* AstContext_distinct(PyObject* self, PyObject* args) {
1157 PyObject* op1 = nullptr;
1158 PyObject* op2 = nullptr;
1159
1160 /* Extract arguments */
1161 if (PyArg_ParseTuple(args, "|OO", &op1, &op2) == false) {
1162 return PyErr_Format(PyExc_TypeError, "distinct(): Invalid number of arguments");
1163 }
1164
1165 if (op1 == nullptr || !PyAstNode_Check(op1))
1166 return PyErr_Format(PyExc_TypeError, "distinct(): expected a AstNode as first argument");
1167
1168 if (op2 == nullptr || !PyAstNode_Check(op2))
1169 return PyErr_Format(PyExc_TypeError, "distinct(): expected a AstNode as second argument");
1170
1171 try {
1173 }
1174 catch (const triton::exceptions::Exception& e) {
1175 return PyErr_Format(PyExc_TypeError, "%s", e.what());
1176 }
1177 }
1178
1179
1180 static PyObject* AstContext_duplicate(PyObject* self, PyObject* node) {
1181 if (!PyAstNode_Check(node))
1182 return PyErr_Format(PyExc_TypeError, "duplicate(): expected a AstNode as argument");
1183
1184 try {
1185 return PyAstNode(triton::ast::newInstance(PyAstNode_AsAstNode(node).get(), true));
1186 }
1187
1188 catch (const triton::exceptions::Exception& e) {
1189 return PyErr_Format(PyExc_TypeError, "%s", e.what());
1190 }
1191 }
1192
1193
1194 static PyObject* AstContext_compound(PyObject* self, PyObject* exprsList) {
1195 std::vector<triton::ast::SharedAbstractNode> exprs;
1196
1197 if (exprsList == nullptr || !PyList_Check(exprsList))
1198 return PyErr_Format(PyExc_TypeError, "compound(): expected a list of AstNodes as first argument");
1199
1200 /* Check if the list contains only PyAstNode */
1201 for (Py_ssize_t i = 0; i < PyList_Size(exprsList); i++){
1202 PyObject* item = PyList_GetItem(exprsList, i);
1203
1204 if (!PyAstNode_Check(item))
1205 return PyErr_Format(PyExc_TypeError, "compound(): Each element from the list must be a AstNode");
1206
1207 exprs.push_back(PyAstNode_AsAstNode(item));
1208 }
1209
1210 try {
1211 return PyAstNode(PyAstContext_AsAstContext(self)->compound(exprs));
1212 }
1213 catch (const triton::exceptions::Exception& e) {
1214 return PyErr_Format(PyExc_TypeError, "%s", e.what());
1215 }
1216 }
1217
1218
1219 static PyObject* AstContext_concat(PyObject* self, PyObject* exprsList) {
1220 std::vector<triton::ast::SharedAbstractNode> exprs;
1221
1222 if (exprsList == nullptr || !PyList_Check(exprsList))
1223 return PyErr_Format(PyExc_TypeError, "concat(): expected a list of AstNodes as first argument");
1224
1225 /* Check if the list contains only PyAstNode */
1226 for (Py_ssize_t i = 0; i < PyList_Size(exprsList); i++){
1227 PyObject* item = PyList_GetItem(exprsList, i);
1228
1229 if (!PyAstNode_Check(item))
1230 return PyErr_Format(PyExc_TypeError, "concat(): Each element from the list must be a AstNode");
1231
1232 exprs.push_back(PyAstNode_AsAstNode(item));
1233 }
1234
1235 try {
1236 return PyAstNode(PyAstContext_AsAstContext(self)->concat(exprs));
1237 }
1238 catch (const triton::exceptions::Exception& e) {
1239 return PyErr_Format(PyExc_TypeError, "%s", e.what());
1240 }
1241 }
1242
1243
1244 static PyObject* AstContext_equal(PyObject* self, PyObject* args) {
1245 PyObject* op1 = nullptr;
1246 PyObject* op2 = nullptr;
1247
1248 /* Extract arguments */
1249 if (PyArg_ParseTuple(args, "|OO", &op1, &op2) == false) {
1250 return PyErr_Format(PyExc_TypeError, "equal(): Invalid number of arguments");
1251 }
1252
1253 if (op1 == nullptr || !PyAstNode_Check(op1))
1254 return PyErr_Format(PyExc_TypeError, "equal(): expected a AstNode as first argument");
1255
1256 if (op2 == nullptr || !PyAstNode_Check(op2))
1257 return PyErr_Format(PyExc_TypeError, "equal(): expected a AstNode as second argument");
1258
1259 try {
1261 }
1262 catch (const triton::exceptions::Exception& e) {
1263 return PyErr_Format(PyExc_TypeError, "%s", e.what());
1264 }
1265 }
1266
1267
1268 static PyObject* AstContext_extract(PyObject* self, PyObject* args) {
1269 PyObject* op1 = nullptr;
1270 PyObject* op2 = nullptr;
1271 PyObject* op3 = nullptr;
1272
1273 /* Extract arguments */
1274 if (PyArg_ParseTuple(args, "|OOO", &op1, &op2, &op3) == false) {
1275 return PyErr_Format(PyExc_TypeError, "extract(): Invalid number of arguments");
1276 }
1277
1278 if (op1 == nullptr || (!PyLong_Check(op1) && !PyInt_Check(op1)))
1279 return PyErr_Format(PyExc_TypeError, "extract(): expected an integer as first argument");
1280
1281 if (op2 == nullptr || (!PyLong_Check(op2) && !PyInt_Check(op2)))
1282 return PyErr_Format(PyExc_TypeError, "extract(): expected an integer as second argument");
1283
1284 if (op3 == nullptr || !PyAstNode_Check(op3))
1285 return PyErr_Format(PyExc_TypeError, "extract(): expected a AstNode as third argument");
1286
1287 try {
1289 }
1290 catch (const triton::exceptions::Exception& e) {
1291 return PyErr_Format(PyExc_TypeError, "%s", e.what());
1292 }
1293 }
1294
1295
1296 static PyObject* AstContext_forall(PyObject* self, PyObject* args) {
1297 std::vector<triton::ast::SharedAbstractNode> vars;
1298 PyObject* op1 = nullptr;
1299 PyObject* op2 = nullptr;
1300
1301 /* Extract arguments */
1302 if (PyArg_ParseTuple(args, "|OO", &op1, &op2) == false) {
1303 return PyErr_Format(PyExc_TypeError, "forall(): Invalid number of arguments");
1304 }
1305
1306 if (op1 == nullptr || !PyList_Check(op1))
1307 return PyErr_Format(PyExc_TypeError, "forall(): expected a list of AstNodes as first argument");
1308
1309 if (op2 == nullptr || !PyAstNode_Check(op2))
1310 return PyErr_Format(PyExc_TypeError, "forall(): expected a AstNode as second argument");
1311
1312 /* Check if the list contains only PyAstNode */
1313 for (Py_ssize_t i = 0; i < PyList_Size(op1); i++){
1314 PyObject* item = PyList_GetItem(op1, i);
1315
1316 if (!PyAstNode_Check(item))
1317 return PyErr_Format(PyExc_TypeError, "forall(): Each element from the list must be a AstNode");
1318
1319 vars.push_back(PyAstNode_AsAstNode(item));
1320 }
1321
1322 try {
1323 return PyAstNode(PyAstContext_AsAstContext(self)->forall(vars, PyAstNode_AsAstNode(op2)));
1324 }
1325 catch (const triton::exceptions::Exception& e) {
1326 return PyErr_Format(PyExc_TypeError, "%s", e.what());
1327 }
1328 }
1329
1330
1331 static PyObject* AstContext_iff(PyObject* self, PyObject* args) {
1332 PyObject* op1 = nullptr;
1333 PyObject* op2 = nullptr;
1334
1335 /* Extract arguments */
1336 if (PyArg_ParseTuple(args, "|OO", &op1, &op2) == false) {
1337 return PyErr_Format(PyExc_TypeError, "iff(): Invalid number of arguments");
1338 }
1339
1340 if (op1 == nullptr || !PyAstNode_Check(op1))
1341 return PyErr_Format(PyExc_TypeError, "iff(): expected a AstNode as first argument");
1342
1343 if (op2 == nullptr || !PyAstNode_Check(op2))
1344 return PyErr_Format(PyExc_TypeError, "iff(): expected a AstNode as second argument");
1345
1346 try {
1348 }
1349 catch (const triton::exceptions::Exception& e) {
1350 return PyErr_Format(PyExc_TypeError, "%s", e.what());
1351 }
1352 }
1353
1354
1355 static PyObject* AstContext_ite(PyObject* self, PyObject* args) {
1356 PyObject* op1 = nullptr;
1357 PyObject* op2 = nullptr;
1358 PyObject* op3 = nullptr;
1359
1360 /* Extract arguments */
1361 if (PyArg_ParseTuple(args, "|OOO", &op1, &op2, &op3) == false) {
1362 return PyErr_Format(PyExc_TypeError, "ite(): Invalid number of arguments");
1363 }
1364
1365 if (op1 == nullptr || !PyAstNode_Check(op1))
1366 return PyErr_Format(PyExc_TypeError, "ite(): expected a AstNode as first argument");
1367
1368 if (op2 == nullptr || !PyAstNode_Check(op2))
1369 return PyErr_Format(PyExc_TypeError, "ite(): expected a AstNode as second argument");
1370
1371 if (op3 == nullptr || !PyAstNode_Check(op3))
1372 return PyErr_Format(PyExc_TypeError, "ite(): expected a AstNode as third argument");
1373
1374 try {
1376 }
1377 catch (const triton::exceptions::Exception& e) {
1378 return PyErr_Format(PyExc_TypeError, "%s", e.what());
1379 }
1380 }
1381
1382
1383 static PyObject* AstContext_land(PyObject* self, PyObject* exprsList) {
1384 std::vector<triton::ast::SharedAbstractNode> exprs;
1385
1386 if (exprsList == nullptr || !PyList_Check(exprsList))
1387 return PyErr_Format(PyExc_TypeError, "land(): expected a list of AstNodes as first argument");
1388
1389 /* Check if the list contains only PyAstNode */
1390 for (Py_ssize_t i = 0; i < PyList_Size(exprsList); i++){
1391 PyObject* item = PyList_GetItem(exprsList, i);
1392
1393 if (!PyAstNode_Check(item))
1394 return PyErr_Format(PyExc_TypeError, "land(): Each element from the list must be a AstNode");
1395
1396 exprs.push_back(PyAstNode_AsAstNode(item));
1397 }
1398
1399 try {
1400 return PyAstNode(PyAstContext_AsAstContext(self)->land(exprs));
1401 }
1402 catch (const triton::exceptions::Exception& e) {
1403 return PyErr_Format(PyExc_TypeError, "%s", e.what());
1404 }
1405 }
1406
1407
1408 static PyObject* AstContext_let(PyObject* self, PyObject* args) {
1409 PyObject* op1 = nullptr;
1410 PyObject* op2 = nullptr;
1411 PyObject* op3 = nullptr;
1412
1413 /* Extract arguments */
1414 if (PyArg_ParseTuple(args, "|OOO", &op1, &op2, &op3) == false) {
1415 return PyErr_Format(PyExc_TypeError, "let(): Invalid number of arguments");
1416 }
1417
1418 if (op1 == nullptr || !PyStr_Check(op1))
1419 return PyErr_Format(PyExc_TypeError, "let(): expected a string as first argument");
1420
1421 if (op2 == nullptr || !PyAstNode_Check(op2))
1422 return PyErr_Format(PyExc_TypeError, "let(): expected a AstNode as second argument");
1423
1424 if (op3 == nullptr || !PyAstNode_Check(op3))
1425 return PyErr_Format(PyExc_TypeError, "let(): expected a AstNode as third argument");
1426
1427 try {
1428 return PyAstNode(PyAstContext_AsAstContext(self)->let(PyStr_AsString(op1), PyAstNode_AsAstNode(op2), PyAstNode_AsAstNode(op3)));
1429 }
1430 catch (const triton::exceptions::Exception& e) {
1431 return PyErr_Format(PyExc_TypeError, "%s", e.what());
1432 }
1433 }
1434
1435
1436 static PyObject* AstContext_lnot(PyObject* self, PyObject* expr) {
1437 if (expr == nullptr || !PyAstNode_Check(expr))
1438 return PyErr_Format(PyExc_TypeError, "lnot(): expected a AstNode as argument");
1439
1440 try {
1441 return PyAstNode(PyAstContext_AsAstContext(self)->lnot(PyAstNode_AsAstNode(expr)));
1442 }
1443 catch (const triton::exceptions::Exception& e) {
1444 return PyErr_Format(PyExc_TypeError, "%s", e.what());
1445 }
1446 }
1447
1448
1449 static PyObject* AstContext_search(PyObject* self, PyObject* args) {
1450 PyObject* ret = nullptr;
1451 PyObject* op1 = nullptr;
1452 PyObject* op2 = nullptr;
1453
1454 /* Extract arguments */
1455 if (PyArg_ParseTuple(args, "|OO", &op1, &op2) == false) {
1456 return PyErr_Format(PyExc_TypeError, "search(): Invalid number of arguments");
1457 }
1458
1459 if (op1 == nullptr || !PyAstNode_Check(op1))
1460 return PyErr_Format(PyExc_TypeError, "search(): expected a AstNode object as first argument");
1461
1462 if (op2 == nullptr || (!PyLong_Check(op2) && !PyInt_Check(op2)))
1463 return PyErr_Format(PyExc_TypeError, "search(): expected a AST_NODE enum as second argument");
1464
1465 try {
1466 auto nodes = triton::ast::search(PyAstNode_AsAstNode(op1), static_cast<triton::ast::ast_e>(PyLong_AsUint32(op2)));
1467 ret = xPyList_New(nodes.size());
1468
1469 triton::uint32 index = 0;
1470 for (auto&& node : nodes)
1471 PyList_SetItem(ret, index++, PyAstNode(node));
1472
1473 return ret;
1474 }
1475 catch (const triton::exceptions::Exception& e) {
1476 return PyErr_Format(PyExc_TypeError, "%s", e.what());
1477 }
1478 }
1479
1480
1481 static PyObject* AstContext_lor(PyObject* self, PyObject* exprsList) {
1482 std::vector<triton::ast::SharedAbstractNode> exprs;
1483
1484 if (exprsList == nullptr || !PyList_Check(exprsList))
1485 return PyErr_Format(PyExc_TypeError, "lor(): expected a list of AstNodes as first argument");
1486
1487 /* Check if the list contains only PyAstNode */
1488 for (Py_ssize_t i = 0; i < PyList_Size(exprsList); i++){
1489 PyObject* item = PyList_GetItem(exprsList, i);
1490
1491 if (!PyAstNode_Check(item))
1492 return PyErr_Format(PyExc_TypeError, "lor(): Each element from the list must be a AstNode");
1493
1494 exprs.push_back(PyAstNode_AsAstNode(item));
1495 }
1496
1497 try {
1498 return PyAstNode(PyAstContext_AsAstContext(self)->lor(exprs));
1499 }
1500 catch (const triton::exceptions::Exception& e) {
1501 return PyErr_Format(PyExc_TypeError, "%s", e.what());
1502 }
1503 }
1504
1505
1506 static PyObject* AstContext_lxor(PyObject* self, PyObject* exprsList) {
1507 std::vector<triton::ast::SharedAbstractNode> exprs;
1508
1509 if (exprsList == nullptr || !PyList_Check(exprsList))
1510 return PyErr_Format(PyExc_TypeError, "lxor(): expected a list of AstNodes as first argument");
1511
1512 /* Check if the list contains only PyAstNode */
1513 for (Py_ssize_t i = 0; i < PyList_Size(exprsList); i++){
1514 PyObject* item = PyList_GetItem(exprsList, i);
1515
1516 if (!PyAstNode_Check(item))
1517 return PyErr_Format(PyExc_TypeError, "lxor(): Each element from the list must be a AstNode");
1518
1519 exprs.push_back(PyAstNode_AsAstNode(item));
1520 }
1521
1522 try {
1523 return PyAstNode(PyAstContext_AsAstContext(self)->lxor(exprs));
1524 }
1525 catch (const triton::exceptions::Exception& e) {
1526 return PyErr_Format(PyExc_TypeError, "%s", e.what());
1527 }
1528 }
1529
1530
1531 static PyObject* AstContext_reference(PyObject* self, PyObject* symExpr) {
1532 if (!PySymbolicExpression_Check(symExpr))
1533 return PyErr_Format(PyExc_TypeError, "reference(): expected a symbolic expression as argument");
1534
1535 try {
1537 }
1538 catch (const triton::exceptions::Exception& e) {
1539 return PyErr_Format(PyExc_TypeError, "%s", e.what());
1540 }
1541 }
1542
1543
1544 static PyObject* AstContext_select(PyObject* self, PyObject* args) {
1545 PyObject* op1 = nullptr;
1546 PyObject* op2 = nullptr;
1547
1548 /* Extract arguments */
1549 PyArg_ParseTuple(args, "|OO", &op1, &op2);
1550
1551 if (op1 == nullptr || !PyAstNode_Check(op1))
1552 return PyErr_Format(PyExc_TypeError, "select(): expected a AstNode as first argument");
1553
1554 if (op2 == nullptr || (!PyAstNode_Check(op2) && !PyLong_Check(op2) && !PyInt_Check(op2)))
1555 return PyErr_Format(PyExc_TypeError, "select(): expected a AstNode or an integer as second argument");
1556
1557 try {
1558 if (PyAstNode_Check(op2))
1560 else
1562 }
1563 catch (const triton::exceptions::Exception& e) {
1564 return PyErr_Format(PyExc_TypeError, "%s", e.what());
1565 }
1566 }
1567
1568
1569 static PyObject* AstContext_store(PyObject* self, PyObject* args) {
1570 PyObject* op1 = nullptr;
1571 PyObject* op2 = nullptr;
1572 PyObject* op3 = nullptr;
1573
1574 /* Extract arguments */
1575 PyArg_ParseTuple(args, "|OOO", &op1, &op2, &op3);
1576
1577 if (op1 == nullptr || !PyAstNode_Check(op1))
1578 return PyErr_Format(PyExc_TypeError, "store(): expected a AstNode as first argument");
1579
1580 if (op2 == nullptr || (!PyAstNode_Check(op2) && !PyLong_Check(op2) && !PyInt_Check(op2)))
1581 return PyErr_Format(PyExc_TypeError, "select(): expected a AstNode or an integer as second argument");
1582
1583 if (op3 == nullptr || !PyAstNode_Check(op3))
1584 return PyErr_Format(PyExc_TypeError, "store(): expected a AstNode as third argument");
1585
1586 try {
1587 if (PyAstNode_Check(op2))
1589 else
1591 }
1592 catch (const triton::exceptions::Exception& e) {
1593 return PyErr_Format(PyExc_TypeError, "%s", e.what());
1594 }
1595 }
1596
1597
1598 static PyObject* AstContext_string(PyObject* self, PyObject* expr) {
1599 if (!PyStr_Check(expr))
1600 return PyErr_Format(PyExc_TypeError, "string(): expected a string as first argument");
1601
1602 try {
1603 return PyAstNode(PyAstContext_AsAstContext(self)->string(PyStr_AsString(expr)));
1604 }
1605 catch (const triton::exceptions::Exception& e) {
1606 return PyErr_Format(PyExc_TypeError, "%s", e.what());
1607 }
1608 }
1609
1610
1611 static PyObject* AstContext_sx(PyObject* self, PyObject* args) {
1612 PyObject* op1 = nullptr;
1613 PyObject* op2 = nullptr;
1614
1615 /* Extract arguments */
1616 if (PyArg_ParseTuple(args, "|OO", &op1, &op2) == false) {
1617 return PyErr_Format(PyExc_TypeError, "sx(): Invalid number of arguments");
1618 }
1619
1620 if (op1 == nullptr || (!PyLong_Check(op1) && !PyInt_Check(op1)))
1621 return PyErr_Format(PyExc_TypeError, "sx(): expected an integer as first argument");
1622
1623 if (op2 == nullptr || !PyAstNode_Check(op2))
1624 return PyErr_Format(PyExc_TypeError, "sx(): expected a AstNode as second argument");
1625
1626 try {
1628 }
1629 catch (const triton::exceptions::Exception& e) {
1630 return PyErr_Format(PyExc_TypeError, "%s", e.what());
1631 }
1632 }
1633
1634
1635 static PyObject* AstContext_unroll(PyObject* self, PyObject* node) {
1636 if (!PyAstNode_Check(node))
1637 return PyErr_Format(PyExc_TypeError, "unroll(): Expects a AstNode as argument.");
1638
1639 try {
1641 }
1642 catch (const triton::exceptions::Exception& e) {
1643 return PyErr_Format(PyExc_TypeError, "%s", e.what());
1644 }
1645 }
1646
1647
1648 static PyObject* AstContext_variable(PyObject* self, PyObject* symVar) {
1649 if (!PySymbolicVariable_Check(symVar))
1650 return PyErr_Format(PyExc_TypeError, "variable(): expected a SymbolicVariable as first argument");
1651
1652 try {
1654 }
1655 catch (const triton::exceptions::Exception& e) {
1656 return PyErr_Format(PyExc_TypeError, "%s", e.what());
1657 }
1658 }
1659
1660
1661 static PyObject* AstContext_zx(PyObject* self, PyObject* args) {
1662 PyObject* op1 = nullptr;
1663 PyObject* op2 = nullptr;
1664
1665 /* Extract arguments */
1666 if (PyArg_ParseTuple(args, "|OO", &op1, &op2) == false) {
1667 return PyErr_Format(PyExc_TypeError, "zx(): Invalid number of arguments");
1668 }
1669
1670 if (op1 == nullptr || (!PyLong_Check(op1) && !PyInt_Check(op1)))
1671 return PyErr_Format(PyExc_TypeError, "zx(): expected an integer as first argument");
1672
1673 if (op2 == nullptr || !PyAstNode_Check(op2))
1674 return PyErr_Format(PyExc_TypeError, "zx(): expected a AstNode as second argument");
1675
1676 try {
1678 }
1679 catch (const triton::exceptions::Exception& e) {
1680 return PyErr_Format(PyExc_TypeError, "%s", e.what());
1681 }
1682 }
1683
1684 // *********************************************************************
1685
1686 #ifdef TRITON_Z3_INTERFACE
1687 static PyObject* AstContext_tritonToZ3(PyObject* self, PyObject* node) {
1688 triton::ast::TritonToZ3 tritonToZ3{false};
1689
1690 if (node == nullptr || (!PyAstNode_Check(node)))
1691 return PyErr_Format(PyExc_TypeError, "tritonToZ3(): Expects a AstNode as argument.");
1692
1693 // import z3
1694 PyObject* z3mod = PyImport_ImportModule("z3");
1695 if (z3mod == nullptr) {
1696 return PyErr_Format(PyExc_TypeError, "tritonToZ3(): z3 module not found.");
1697 }
1698
1699 // z3.main_ctx().ctx.value
1700 PyObject* z3MainCtx = PyObject_CallObject(PyObject_GetAttrString(z3mod, "main_ctx"), nullptr);
1701 PyObject* z3CtxPtr = PyObject_GetAttrString(PyObject_GetAttrString(z3MainCtx, "ctx"), "value");
1702 Z3_context z3Ctx = reinterpret_cast<Z3_context>(PyLong_AsVoidPtr(z3CtxPtr));
1703 Py_DECREF(z3CtxPtr);
1704 Py_DECREF(z3MainCtx);
1705
1706 // Convert the node to a Z3++ expression and translate it into
1707 // python's z3 main context
1708 z3::expr expr = tritonToZ3.convert(PyAstNode_AsAstNode(node));
1709 Z3_ast ast = Z3_translate(expr.ctx(), expr, z3Ctx);
1710
1711 // Check that everything went fine
1712 if (Z3_get_error_code(z3Ctx) != Z3_OK) {
1713 Py_DECREF(z3mod);
1714 return PyErr_Format(PyExc_RuntimeError, "tritonToZ3(): Z3 AST translation failed.");
1715 }
1716
1717 // retAst = ctypes.c_void_p(ctx_ptr); retAst.__class__ = z3.Ast
1718 PyObject* pyArgs = Py_BuildValue("(O)", PyLong_FromVoidPtr(ast));
1719 PyObject* retAst = PyObject_CallObject(PyObject_GetAttrString(z3mod, "c_void_p"), pyArgs);
1720 PyObject_SetAttrString(retAst, "__class__", PyObject_GetAttrString(z3mod, "Ast"));
1721 Py_DECREF(pyArgs);
1722
1723 // return z3.ExprRef(ast)
1724 PyObject* z3ExprRef = PyObject_GetAttrString(z3mod, "ExprRef");
1725 pyArgs = Py_BuildValue("(O)", retAst);
1726 PyObject* retExpr = PyObject_CallObject(z3ExprRef, pyArgs);
1727 Py_DECREF(pyArgs);
1728 Py_DECREF(retAst);
1729 Py_DECREF(z3ExprRef);
1730
1731 // Cleanup
1732 Py_DECREF(z3mod);
1733
1734 return retExpr;
1735 }
1736
1737
1738 static PyObject* AstContext_z3ToTriton(PyObject* self, PyObject* expr) {
1740 z3::context z3Ctx;
1741
1742 if (std::strcmp(Py_TYPE(expr)->tp_name, "ExprRef") && std::strcmp(Py_TYPE(expr)->tp_name, "BitVecRef"))
1743 return PyErr_Format(PyExc_TypeError, "z3ToTriton(): expected an ExprRef as argument");
1744
1745 PyObject* z3AstPtr = PyObject_GetAttrString(expr, "ast");
1746 if (z3AstPtr == nullptr)
1747 return PyErr_Format(PyExc_TypeError, "z3ToTriton(): expected an ExprRef as argument");
1748
1749 PyObject* z3AstPtrValue = PyObject_GetAttrString(z3AstPtr, "value");
1750 if (z3AstPtrValue == nullptr)
1751 return PyErr_Format(PyExc_TypeError, "z3ToTriton(): expected an ExprRef as argument");
1752
1753 try {
1754 Z3_ast z3Ast = reinterpret_cast<Z3_ast>(PyLong_AsVoidPtr(z3AstPtrValue));
1755 z3::expr z3Expr = z3::to_expr(z3Ctx, z3Ast);
1756
1757 return PyAstNode(z3ToTritonAst.convert(z3Expr));
1758 }
1759 catch (const triton::exceptions::Exception& e) {
1760 return PyErr_Format(PyExc_TypeError, "%s", e.what());
1761 }
1762 }
1763 #endif
1764
1765
1766 static int AstContext_init(AstNode_Object *self, PyObject *args, PyObject *kwds) {
1767 return 0;
1768 }
1769
1770
1771 static PyObject* AstContext_new(PyTypeObject* type, PyObject* args, PyObject* kwds) {
1772 return type->tp_alloc(type, 0);
1773 }
1774
1775
1777 PyMethodDef AstContext_callbacks[] = {
1778 {"array", AstContext_array, METH_O, ""},
1779 {"assert_", AstContext_assert, METH_O, ""},
1780 {"bswap", AstContext_bswap, METH_O, ""},
1781 {"bv", AstContext_bv, METH_VARARGS, ""},
1782 {"bvadd", AstContext_bvadd, METH_VARARGS, ""},
1783 {"bvand", AstContext_bvand, METH_VARARGS, ""},
1784 {"bvashr", AstContext_bvashr, METH_VARARGS, ""},
1785 {"bvfalse", AstContext_bvfalse, METH_NOARGS, ""},
1786 {"bvlshr", AstContext_bvlshr, METH_VARARGS, ""},
1787 {"bvmul", AstContext_bvmul, METH_VARARGS, ""},
1788 {"bvnand", AstContext_bvnand, METH_VARARGS, ""},
1789 {"bvneg", AstContext_bvneg, METH_O, ""},
1790 {"bvnor", AstContext_bvnor, METH_VARARGS, ""},
1791 {"bvnot", AstContext_bvnot, METH_O, ""},
1792 {"bvor", AstContext_bvor, METH_VARARGS, ""},
1793 {"bvrol", AstContext_bvrol, METH_VARARGS, ""},
1794 {"bvror", AstContext_bvror, METH_VARARGS, ""},
1795 {"bvsdiv", AstContext_bvsdiv, METH_VARARGS, ""},
1796 {"bvsge", AstContext_bvsge, METH_VARARGS, ""},
1797 {"bvsgt", AstContext_bvsgt, METH_VARARGS, ""},
1798 {"bvshl", AstContext_bvshl, METH_VARARGS, ""},
1799 {"bvsle", AstContext_bvsle, METH_VARARGS, ""},
1800 {"bvslt", AstContext_bvslt, METH_VARARGS, ""},
1801 {"bvsmod", AstContext_bvsmod, METH_VARARGS, ""},
1802 {"bvsrem", AstContext_bvsrem, METH_VARARGS, ""},
1803 {"bvsub", AstContext_bvsub, METH_VARARGS, ""},
1804 {"bvtrue", AstContext_bvtrue, METH_NOARGS, ""},
1805 {"bvudiv", AstContext_bvudiv, METH_VARARGS, ""},
1806 {"bvuge", AstContext_bvuge, METH_VARARGS, ""},
1807 {"bvugt", AstContext_bvugt, METH_VARARGS, ""},
1808 {"bvule", AstContext_bvule, METH_VARARGS, ""},
1809 {"bvult", AstContext_bvult, METH_VARARGS, ""},
1810 {"bvurem", AstContext_bvurem, METH_VARARGS, ""},
1811 {"bvxnor", AstContext_bvxnor , METH_VARARGS, ""},
1812 {"bvxor", AstContext_bvxor, METH_VARARGS, ""},
1813 {"compound", AstContext_compound, METH_O, ""},
1814 {"concat", AstContext_concat, METH_O, ""},
1815 {"declare", AstContext_declare, METH_O, ""},
1816 {"dereference", AstContext_dereference, METH_O, ""},
1817 {"distinct", AstContext_distinct, METH_VARARGS, ""},
1818 {"duplicate", AstContext_duplicate, METH_O, ""},
1819 {"equal", AstContext_equal, METH_VARARGS, ""},
1820 {"extract", AstContext_extract, METH_VARARGS, ""},
1821 {"forall", AstContext_forall, METH_VARARGS, ""},
1822 {"iff", AstContext_iff, METH_VARARGS, ""},
1823 {"ite", AstContext_ite, METH_VARARGS, ""},
1824 {"land", AstContext_land, METH_O, ""},
1825 {"let", AstContext_let, METH_VARARGS, ""},
1826 {"lnot", AstContext_lnot, METH_O, ""},
1827 {"lor", AstContext_lor, METH_O, ""},
1828 {"lxor", AstContext_lxor, METH_O, ""},
1829 {"reference", AstContext_reference, METH_O, ""},
1830 {"select", AstContext_select, METH_VARARGS, ""},
1831 {"store", AstContext_store, METH_VARARGS, ""},
1832 {"search", AstContext_search, METH_VARARGS, ""},
1833 {"string", AstContext_string, METH_O, ""},
1834 {"sx", AstContext_sx, METH_VARARGS, ""},
1835 {"unroll", AstContext_unroll, METH_O, ""},
1836 {"variable", AstContext_variable, METH_O, ""},
1837 {"zx", AstContext_zx, METH_VARARGS, ""},
1838 #ifdef TRITON_Z3_INTERFACE
1839 {"tritonToZ3", AstContext_tritonToZ3, METH_O, ""},
1840 {"z3ToTriton", AstContext_z3ToTriton, METH_O, ""},
1841 #endif
1842 {nullptr, nullptr, 0, nullptr}
1843 };
1844
1845
1847 PyTypeObject AstContext_Type = {
1848 PyVarObject_HEAD_INIT(&PyType_Type, 0)
1849 "AstContext", /* tp_name */
1850 sizeof(AstContext_Object), /* tp_basicsize */
1851 0, /* tp_itemsize */
1852 (destructor)AstContext_dealloc, /* tp_dealloc */
1853 0, /* tp_print or tp_vectorcall_offset */
1854 0, /* tp_getattr */
1855 0, /* tp_setattr */
1856 0, /* tp_compare */
1857 0, /* tp_repr */
1858 0, /* tp_as_number */
1859 0, /* tp_as_sequence */
1860 0, /* tp_as_mapping */
1861 0, /* tp_hash */
1862 0, /* tp_call */
1863 0, /* tp_str */
1864 0, /* tp_getattro */
1865 0, /* tp_setattro */
1866 0, /* tp_as_buffer */
1867 Py_TPFLAGS_DEFAULT, /* tp_flags */
1868 "AstContext objects", /* tp_doc */
1869 0, /* tp_traverse */
1870 0, /* tp_clear */
1871 0, /* tp_richcompare */
1872 0, /* tp_weaklistoffset */
1873 0, /* tp_iter */
1874 0, /* tp_iternext */
1875 AstContext_callbacks, /* tp_methods */
1876 0, /* tp_members */
1877 0, /* tp_getset */
1878 0, /* tp_base */
1879 0, /* tp_dict */
1880 0, /* tp_descr_get */
1881 0, /* tp_descr_set */
1882 0, /* tp_dictoffset */
1883 (initproc)AstContext_init, /* tp_init */
1884 0, /* tp_alloc */
1885 (newfunc)AstContext_new, /* tp_new */
1886 0, /* tp_free */
1887 0, /* tp_is_gc */
1888 0, /* tp_bases */
1889 0, /* tp_mro */
1890 0, /* tp_cache */
1891 0, /* tp_subclasses */
1892 0, /* tp_weaklist */
1893 0, /* tp_del */
1894 #if IS_PY3
1895 0, /* tp_version_tag */
1896 0, /* tp_finalize */
1897 #if IS_PY3_8
1898 0, /* tp_vectorcall */
1899 #if !IS_PY3_9
1900 0, /* bpo-37250: kept for backwards compatibility in CPython 3.8 only */
1901 #endif
1902 #endif
1903 #else
1904 0 /* tp_version_tag */
1905 #endif
1906 };
1907
1908
1910 if (actx == nullptr) {
1911 Py_INCREF(Py_None);
1912 return Py_None;
1913 }
1914
1915 PyType_Ready(&AstContext_Type);
1916 auto* object = (triton::bindings::python::AstContext_Object*)PyObject_CallObject((PyObject*)&AstContext_Type, nullptr);
1917 if (object != NULL) {
1918 object->actx = actx;
1919 }
1920
1921 return (PyObject*)object;
1922 }
1923
1924 }; /* python namespace */
1925 }; /* bindings namespace */
1926}; /* triton namespace */
Converts a Triton's AST to Z3's AST.
Converts a Z3's AST to a Triton's AST.
The root class of all exceptions.
TRITON_EXPORT const char * what() const
Returns the exception message.
SharedAbstractNode unroll(const triton::ast::SharedAbstractNode &node)
AST C++ API - Unrolls the SSA form of a given AST.
Definition ast.cpp:3626
SharedAbstractNode dereference(const SharedAbstractNode &node)
Returns the first non referene node encountered.
Definition ast.cpp:3743
SharedAbstractNode newInstance(AbstractNode *node, bool unroll)
AST C++ API - Duplicates the AST.
Definition ast.cpp:3604
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
std::shared_ptr< triton::ast::AstContext > SharedAstContext
Shared AST context.
Definition ast.hpp:65
triton::uint512 PyLong_AsUint512(PyObject *vv)
Returns a triton::uint512 from a pyObject.
Definition utils.cpp:201
PyObject * PyAstContext(const triton::ast::SharedAstContext &actx)
Creates an AstContext python class.
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
triton::usize PyLong_AsUsize(PyObject *vv)
Returns a triton::usize from a pyObject.
Definition utils.cpp:56
PyObject * PyAstNode(const triton::ast::SharedAbstractNode &node)
Creates the AstNode python class.
std::uint32_t uint32
unisgned 32-bits
void AstContext_dealloc(PyObject *self)
AstContext destructor.
PyMethodDef AstContext_callbacks[]
AstContext methods.
PyTypeObject AstContext_Type
Python description for an ast context.
The Triton namespace.
#define PySymbolicVariable_Check(v)
#define PyAstContext_AsAstContext(v)
#define PyAstNode_Check(v)
#define PySymbolicExpression_Check(v)
#define PySymbolicVariable_AsSymbolicVariable(v)
#define PyAstNode_AsAstNode(v)
#define PySymbolicExpression_AsSymbolicExpression(v)