libTriton version 1.0 build 1592
Loading...
Searching...
No Matches
pyBitsVector.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/bitsVector.hpp>
12#include <triton/coreUtils.hpp>
13#include <triton/exceptions.hpp>
14
15#include <iostream>
16
17
18
19/* setup doctest context
20
21>>> from triton import ARCH, TritonContext, REG
22>>> ctxt = TritonContext()
23>>> ctxt.setArchitecture(ARCH.X86_64)
24
25*/
26
70namespace triton {
71 namespace bindings {
72 namespace python {
73
75 void BitsVector_dealloc(PyObject* self) {
76 std::cout << std::flush;
77 delete PyBitsVector_AsBitsVector(self);
78 Py_TYPE(self)->tp_free((PyObject*)self);
79 }
80
81
82 static PyObject* BitsVector_getHigh(PyObject* self, PyObject* noarg) {
83 try {
84 return PyLong_FromUint32(PyBitsVector_AsBitsVector(self)->getHigh());
85 }
86 catch (const triton::exceptions::Exception& e) {
87 return PyErr_Format(PyExc_TypeError, "%s", e.what());
88 }
89 }
90
91
92 static PyObject* BitsVector_getLow(PyObject* self, PyObject* noarg) {
93 try {
94 return PyLong_FromUint32(PyBitsVector_AsBitsVector(self)->getLow());
95 }
96 catch (const triton::exceptions::Exception& e) {
97 return PyErr_Format(PyExc_TypeError, "%s", e.what());
98 }
99 }
100
101
102 static PyObject* BitsVector_getMaxValue(PyObject* self, PyObject* noarg) {
103 try {
104 return PyLong_FromUint512(PyBitsVector_AsBitsVector(self)->getMaxValue());
105 }
106 catch (const triton::exceptions::Exception& e) {
107 return PyErr_Format(PyExc_TypeError, "%s", e.what());
108 }
109 }
110
111
112 static PyObject* BitsVector_getVectorSize(PyObject* self, PyObject* noarg) {
113 try {
114 return PyLong_FromUint32(PyBitsVector_AsBitsVector(self)->getVectorSize());
115 }
116 catch (const triton::exceptions::Exception& e) {
117 return PyErr_Format(PyExc_TypeError, "%s", e.what());
118 }
119 }
120
121
122 #if !defined(IS_PY3_8) || !IS_PY3_8
123 static int BitsVector_print(PyObject* self, void* io, int s) {
124 std::cout << PyBitsVector_AsBitsVector(self);
125 return 0;
126 }
127 #endif
128
129
130 static PyObject* BitsVector_str(PyObject* self) {
131 try {
132 return PyStr_FromFormat("%s", triton::utils::toString(PyBitsVector_AsBitsVector(self)).c_str());
133 }
134 catch (const triton::exceptions::Exception& e) {
135 return PyErr_Format(PyExc_TypeError, "%s", e.what());
136 }
137 }
138
139
141 PyMethodDef BitsVector_callbacks[] = {
142 {"getHigh", BitsVector_getHigh, METH_NOARGS, ""},
143 {"getLow", BitsVector_getLow, METH_NOARGS, ""},
144 {"getMaxValue", BitsVector_getMaxValue, METH_NOARGS, ""},
145 {"getVectorSize", BitsVector_getVectorSize, METH_NOARGS, ""},
146 {nullptr, nullptr, 0, nullptr}
147 };
148
149
150 PyTypeObject BitsVector_Type = {
151 PyVarObject_HEAD_INIT(&PyType_Type, 0)
152 "BitsVector", /* tp_name */
153 sizeof(BitsVector_Object), /* tp_basicsize */
154 0, /* tp_itemsize */
155 (destructor)BitsVector_dealloc, /* tp_dealloc */
156 #if IS_PY3_8
157 0, /* tp_vectorcall_offset */
158 #else
159 (printfunc)BitsVector_print, /* tp_print */
160 #endif
161 0, /* tp_getattr */
162 0, /* tp_setattr */
163 0, /* tp_compare */
164 (reprfunc)BitsVector_str, /* tp_repr */
165 0, /* tp_as_number */
166 0, /* tp_as_sequence */
167 0, /* tp_as_mapping */
168 0, /* tp_hash */
169 0, /* tp_call */
170 (reprfunc)BitsVector_str, /* tp_str */
171 0, /* tp_getattro */
172 0, /* tp_setattro */
173 0, /* tp_as_buffer */
174 Py_TPFLAGS_DEFAULT, /* tp_flags */
175 "BitsVector objects", /* tp_doc */
176 0, /* tp_traverse */
177 0, /* tp_clear */
178 0, /* tp_richcompare */
179 0, /* tp_weaklistoffset */
180 0, /* tp_iter */
181 0, /* tp_iternext */
182 BitsVector_callbacks, /* tp_methods */
183 0, /* tp_members */
184 0, /* tp_getset */
185 0, /* tp_base */
186 0, /* tp_dict */
187 0, /* tp_descr_get */
188 0, /* tp_descr_set */
189 0, /* tp_dictoffset */
190 0, /* tp_init */
191 0, /* tp_alloc */
192 0, /* tp_new */
193 0, /* tp_free */
194 0, /* tp_is_gc */
195 0, /* tp_bases */
196 0, /* tp_mro */
197 0, /* tp_cache */
198 0, /* tp_subclasses */
199 0, /* tp_weaklist */
200 0, /* tp_del */
201 #if IS_PY3
202 0, /* tp_version_tag */
203 0, /* tp_finalize */
204 #if IS_PY3_8
205 0, /* tp_vectorcall */
206 #if !IS_PY3_9
207 0, /* bpo-37250: kept for backwards compatibility in CPython 3.8 only */
208 #endif
209 #endif
210 #else
211 0 /* tp_version_tag */
212 #endif
213 };
214
215
216 template PyObject* PyBitsVector(const triton::arch::Immediate& op);
217 template PyObject* PyBitsVector(const triton::arch::MemoryAccess& op);
218 template PyObject* PyBitsVector(const triton::arch::Register& op);
219 template <typename T>
220 PyObject* PyBitsVector(const T& op) {
221 BitsVector_Object* object;
222
223 PyType_Ready(&BitsVector_Type);
224 object = PyObject_NEW(BitsVector_Object, &BitsVector_Type);
225 if (object != NULL)
226 object->bv = new triton::arch::BitsVector(op);
227
228 return (PyObject*)object;
229 }
230
231 }; /* python namespace */
232 }; /* bindings namespace */
233}; /* triton namespace */
This class is used to deal with registers and memory as bits vector.
This class is used to represent an immediate.
Definition immediate.hpp:37
This class is used to represent a memory access.
This class is used when an instruction has a register operand.
Definition register.hpp:44
The root class of all exceptions.
TRITON_EXPORT const char * what() const
Returns the exception message.
PyObject * PyLong_FromUint512(triton::uint512 value)
Returns a pyObject from a triton::uint512.
Definition utils.cpp:418
PyTypeObject BitsVector_Type
pyBitsVector type.
PyObject * PyLong_FromUint32(triton::uint32 value)
Returns a pyObject from a triton::uint32.
Definition utils.cpp:322
std::string toString(const T &obj)
Converts an object to a string.
Definition coreUtils.hpp:38
PyMethodDef BitsVector_callbacks[]
BitsVector methods.
void BitsVector_dealloc(PyObject *self)
BitsVector destructor.
The Triton namespace.
#define PyBitsVector_AsBitsVector(v)