libTriton version 1.0 build 1592
Loading...
Searching...
No Matches
pySymbolicVariable.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/coreUtils.hpp>
12#include <triton/exceptions.hpp>
14
15#include <iostream>
16
17
18
19/* setup doctest context
20
21>>> from triton import TritonContext, REG, ARCH
22>>> ctxt = TritonContext()
23>>> ctxt.setArchitecture(ARCH.X86_64)
24
25*/
26
84namespace triton {
85 namespace bindings {
86 namespace python {
87
89 void SymbolicVariable_dealloc(PyObject* self) {
90 std::cout << std::flush;
91 PySymbolicVariable_AsSymbolicVariable(self) = nullptr; // decref the shared_ptr
92 Py_TYPE(self)->tp_free((PyObject*)self);
93 }
94
95
96 static PyObject* SymbolicVariable_getAlias(PyObject* self, PyObject* noarg) {
97 try {
98 return Py_BuildValue("s", PySymbolicVariable_AsSymbolicVariable(self)->getAlias().c_str());
99 }
100 catch (const triton::exceptions::Exception& e) {
101 return PyErr_Format(PyExc_TypeError, "%s", e.what());
102 }
103 }
104
105
106 static PyObject* SymbolicVariable_getName(PyObject* self, PyObject* noarg) {
107 try {
108 return Py_BuildValue("s", PySymbolicVariable_AsSymbolicVariable(self)->getName().c_str());
109 }
110 catch (const triton::exceptions::Exception& e) {
111 return PyErr_Format(PyExc_TypeError, "%s", e.what());
112 }
113 }
114
115
116 static PyObject* SymbolicVariable_getId(PyObject* self, PyObject* noarg) {
117 try {
119 }
120 catch (const triton::exceptions::Exception& e) {
121 return PyErr_Format(PyExc_TypeError, "%s", e.what());
122 }
123 }
124
125
126 static PyObject* SymbolicVariable_getType(PyObject* self, PyObject* noarg) {
127 try {
129 }
130 catch (const triton::exceptions::Exception& e) {
131 return PyErr_Format(PyExc_TypeError, "%s", e.what());
132 }
133 }
134
135
136 static PyObject* SymbolicVariable_getOrigin(PyObject* self, PyObject* noarg) {
137 try {
139 }
140 catch (const triton::exceptions::Exception& e) {
141 return PyErr_Format(PyExc_TypeError, "%s", e.what());
142 }
143 }
144
145
146 static PyObject* SymbolicVariable_getBitSize(PyObject* self, PyObject* noarg) {
147 try {
149 }
150 catch (const triton::exceptions::Exception& e) {
151 return PyErr_Format(PyExc_TypeError, "%s", e.what());
152 }
153 }
154
155
156 static PyObject* SymbolicVariable_getComment(PyObject* self, PyObject* noarg) {
157 try {
158 return Py_BuildValue("s", PySymbolicVariable_AsSymbolicVariable(self)->getComment().c_str());
159 }
160 catch (const triton::exceptions::Exception& e) {
161 return PyErr_Format(PyExc_TypeError, "%s", e.what());
162 }
163 }
164
165
166 static PyObject* SymbolicVariable_setAlias(PyObject* self, PyObject* alias) {
167 try {
168 if (!PyStr_Check(alias))
169 return PyErr_Format(PyExc_TypeError, "SymbolicVariable::setAlias(): Expected a string as argument.");
170 PySymbolicVariable_AsSymbolicVariable(self)->setAlias(PyStr_AsString(alias));
171 Py_INCREF(Py_None);
172 return Py_None;
173 }
174 catch (const triton::exceptions::Exception& e) {
175 return PyErr_Format(PyExc_TypeError, "%s", e.what());
176 }
177 }
178
179
180 static PyObject* SymbolicVariable_setComment(PyObject* self, PyObject* comment) {
181 try {
182 if (!PyStr_Check(comment))
183 return PyErr_Format(PyExc_TypeError, "SymbolicVariable::setComment(): Expected a string as argument.");
184 PySymbolicVariable_AsSymbolicVariable(self)->setComment(PyStr_AsString(comment));
185 Py_INCREF(Py_None);
186 return Py_None;
187 }
188 catch (const triton::exceptions::Exception& e) {
189 return PyErr_Format(PyExc_TypeError, "%s", e.what());
190 }
191 }
192
193
194 #if !defined(IS_PY3_8) || !IS_PY3_8
195 static int SymbolicVariable_print(PyObject* self, void* io, int s) {
196 std::cout << PySymbolicVariable_AsSymbolicVariable(self);
197 return 0;
198 }
199 #endif
200
201
202 static PyObject* SymbolicVariable_str(PyObject* self) {
203 try {
204 return PyStr_FromFormat("%s", triton::utils::toString(PySymbolicVariable_AsSymbolicVariable(self)).c_str());
205 }
206 catch (const triton::exceptions::Exception& e) {
207 return PyErr_Format(PyExc_TypeError, "%s", e.what());
208 }
209 }
210
211
212 static int SymbolicVariable_init(AstNode_Object* self, PyObject* args, PyObject* kwds) {
213 return 0;
214 }
215
216
217 static PyObject* SymbolicVariable_new(PyTypeObject* type, PyObject* args, PyObject* kwds) {
218 return type->tp_alloc(type, 0);
219 }
220
221
222 static long SymbolicVariable_hash(PyObject* self) {
223 /* NOTE: Should be a problem if there is more than 0xffffffff variables (casting from usize to long). */
224 return static_cast<long>(PySymbolicVariable_AsSymbolicVariable(self)->getId());
225 }
226
227
228 static PyObject* SymbolicVariable_richcompare(PyObject* self, PyObject* other, int op) {
229 PyObject* result = nullptr;
230 triton::usize id1 = 0;
231 triton::usize id2 = 0;
232
233 if (!PySymbolicVariable_Check(other)) {
234 result = Py_NotImplemented;
235 }
236 else {
237 id1 = PySymbolicVariable_AsSymbolicVariable(self)->getId();
238 id2 = PySymbolicVariable_AsSymbolicVariable(other)->getId();
239
240 switch (op) {
241 case Py_LT:
242 result = (id1 < id2) ? Py_True : Py_False;
243 break;
244 case Py_LE:
245 result = (id1 <= id2) ? Py_True : Py_False;
246 break;
247 case Py_EQ:
248 result = (id1 == id2) ? Py_True : Py_False;
249 break;
250 case Py_NE:
251 result = (id1 != id2) ? Py_True : Py_False;
252 break;
253 case Py_GT:
254 result = (id1 > id2) ? Py_True : Py_False;
255 break;
256 case Py_GE:
257 result = (id1 >= id2) ? Py_True : Py_False;
258 break;
259 }
260 }
261
262 Py_INCREF(result);
263 return result;
264 }
265
268 {"getAlias", SymbolicVariable_getAlias, METH_NOARGS, ""},
269 {"getBitSize", SymbolicVariable_getBitSize, METH_NOARGS, ""},
270 {"getComment", SymbolicVariable_getComment, METH_NOARGS, ""},
271 {"getId", SymbolicVariable_getId, METH_NOARGS, ""},
272 {"getName", SymbolicVariable_getName, METH_NOARGS, ""},
273 {"getOrigin", SymbolicVariable_getOrigin, METH_NOARGS, ""},
274 {"getType", SymbolicVariable_getType, METH_NOARGS, ""},
275 {"setAlias", SymbolicVariable_setAlias, METH_O, ""},
276 {"setComment", SymbolicVariable_setComment, METH_O, ""},
277 {nullptr, nullptr, 0, nullptr}
278 };
279
280
281 PyTypeObject SymbolicVariable_Type = {
282 PyVarObject_HEAD_INIT(&PyType_Type, 0)
283 "SymbolicVariable", /* tp_name */
284 sizeof(SymbolicVariable_Object), /* tp_basicsize */
285 0, /* tp_itemsize */
286 (destructor)SymbolicVariable_dealloc, /* tp_dealloc */
287 #if IS_PY3_8
288 0, /* tp_vectorcall_offset */
289 #else
290 (printfunc)SymbolicVariable_print, /* tp_print */
291 #endif
292 0, /* tp_getattr */
293 0, /* tp_setattr */
294 0, /* tp_compare */
295 (reprfunc)SymbolicVariable_str, /* tp_repr */
296 0, /* tp_as_number */
297 0, /* tp_as_sequence */
298 0, /* tp_as_mapping */
299 (hashfunc)SymbolicVariable_hash, /* tp_hash */
300 0, /* tp_call */
301 (reprfunc)SymbolicVariable_str, /* tp_str */
302 0, /* tp_getattro */
303 0, /* tp_setattro */
304 0, /* tp_as_buffer */
305 Py_TPFLAGS_DEFAULT, /* tp_flags */
306 "SymbolicVariable objects", /* tp_doc */
307 0, /* tp_traverse */
308 0, /* tp_clear */
309 SymbolicVariable_richcompare, /* tp_richcompare */
310 0, /* tp_weaklistoffset */
311 0, /* tp_iter */
312 0, /* tp_iternext */
313 SymbolicVariable_callbacks, /* tp_methods */
314 0, /* tp_members */
315 0, /* tp_getset */
316 0, /* tp_base */
317 0, /* tp_dict */
318 0, /* tp_descr_get */
319 0, /* tp_descr_set */
320 0, /* tp_dictoffset */
321 (initproc)SymbolicVariable_init, /* tp_init */
322 0, /* tp_alloc */
323 (newfunc)SymbolicVariable_new, /* tp_new */
324 0, /* tp_free */
325 0, /* tp_is_gc */
326 0, /* tp_bases */
327 0, /* tp_mro */
328 0, /* tp_cache */
329 0, /* tp_subclasses */
330 0, /* tp_weaklist */
331 0, /* tp_del */
332 #if IS_PY3
333 0, /* tp_version_tag */
334 0, /* tp_finalize */
335 #if IS_PY3_8
336 0, /* tp_vectorcall */
337 #if !IS_PY3_9
338 0, /* bpo-37250: kept for backwards compatibility in CPython 3.8 only */
339 #endif
340 #endif
341 #else
342 0 /* tp_version_tag */
343 #endif
344 };
345
346
348 if (symVar == nullptr) {
349 Py_INCREF(Py_None);
350 return Py_None;
351 }
352
353 PyType_Ready(&SymbolicVariable_Type);
354 // Build the new object the python way (calling operator() on the type) as
355 // it crash otherwise (certainly due to incorrect shared_ptr initialization).
356 auto* object = (triton::bindings::python::SymbolicVariable_Object*)PyObject_CallObject((PyObject*)&SymbolicVariable_Type, nullptr);
357 if (object != NULL) {
358 object->symVar = symVar;
359 }
360
361 return (PyObject*)object;
362 }
363
364 }; /* python namespace */
365 }; /* bindings namespace */
366}; /* triton namespace */
The root class of all exceptions.
TRITON_EXPORT const char * what() const
Returns the exception message.
PyTypeObject SymbolicVariable_Type
pySymbolicVariable type.
PyObject * PySymbolicVariable(const triton::engines::symbolic::SharedSymbolicVariable &symVar)
Creates the SymbolicVariable python class.
PyObject * PyLong_FromUsize(triton::usize value)
Returns a pyObject from a triton::usize.
Definition utils.cpp:288
PyObject * PyLong_FromUint64(triton::uint64 value)
Returns a pyObject from a triton::uint64.
Definition utils.cpp:328
PyObject * PyLong_FromUint32(triton::uint32 value)
Returns a pyObject from a triton::uint32.
Definition utils.cpp:322
std::shared_ptr< triton::engines::symbolic::SymbolicVariable > SharedSymbolicVariable
Shared Symbolic variable.
Definition ast.hpp:43
std::size_t usize
unsigned MAX_INT 32 or 64 bits according to the CPU.
std::string toString(const T &obj)
Converts an object to a string.
Definition coreUtils.hpp:38
PyMethodDef SymbolicVariable_callbacks[]
SymbolicVariable methods.
void SymbolicVariable_dealloc(PyObject *self)
SymbolicVariable destructor.
The Triton namespace.
#define PySymbolicVariable_Check(v)
#define PySymbolicVariable_AsSymbolicVariable(v)