libTriton version 1.0 build 1592
Loading...
Searching...
No Matches
pyRegister.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>
13#include <triton/register.hpp>
14
15#include <iostream>
16
17
18
19/* setup doctest context
20
21>>> from __future__ import print_function
22>>> from triton import ARCH, TritonContext, Instruction, REG
23>>> ctxt = TritonContext()
24>>> ctxt.setArchitecture(ARCH.X86_64)
25
26>>> inst = Instruction(b"\x8A\xA4\x4A\x00\x01\x00\x00")
27>>> inst.setAddress(0x40000)
28
29*/
30
142namespace triton {
143 namespace bindings {
144 namespace python {
145
147 void Register_dealloc(PyObject* self) {
148 std::cout << std::flush;
149 delete PyRegister_AsRegister(self);
150 Py_TYPE(self)->tp_free((PyObject*)self);
151 }
152
153
154 static PyObject* Register_getBitSize(PyObject* self, PyObject* noarg) {
155 try {
156 return PyLong_FromUint32(PyRegister_AsRegister(self)->getBitSize());
157 }
158 catch (const triton::exceptions::Exception& e) {
159 return PyErr_Format(PyExc_TypeError, "%s", e.what());
160 }
161 }
162
163
164 static PyObject* Register_getBitvector(PyObject* self, PyObject* noarg) {
165 try {
166 return PyBitsVector(*PyRegister_AsRegister(self));
167 }
168 catch (const triton::exceptions::Exception& e) {
169 return PyErr_Format(PyExc_TypeError, "%s", e.what());
170 }
171 }
172
173
174 static PyObject* Register_getExtendSize(PyObject* self, PyObject* noarg) {
175 try {
176 return PyLong_FromUint32(PyRegister_AsRegister(self)->getExtendSize());
177 }
178 catch (const triton::exceptions::Exception& e) {
179 return PyErr_Format(PyExc_TypeError, "%s", e.what());
180 }
181 }
182
183
184 static PyObject* Register_getExtendType(PyObject* self, PyObject* noarg) {
185 try {
186 return PyLong_FromUint32(PyRegister_AsRegister(self)->getExtendType());
187 }
188 catch (const triton::exceptions::Exception& e) {
189 return PyErr_Format(PyExc_TypeError, "%s", e.what());
190 }
191 }
192
193
194 static PyObject* Register_getId(PyObject* self, PyObject* noarg) {
195 try {
196 return PyLong_FromUint32(PyRegister_AsRegister(self)->getId());
197 }
198 catch (const triton::exceptions::Exception& e) {
199 return PyErr_Format(PyExc_TypeError, "%s", e.what());
200 }
201 }
202
203
204 static PyObject* Register_getName(PyObject* self, PyObject* noarg) {
205 try {
206 return Py_BuildValue("s", PyRegister_AsRegister(self)->getName().c_str());
207 }
208 catch (const triton::exceptions::Exception& e) {
209 return PyErr_Format(PyExc_TypeError, "%s", e.what());
210 }
211 }
212
213
214 static PyObject* Register_getShiftType(PyObject* self, PyObject* noarg) {
215 try {
216 return PyLong_FromUint32(PyRegister_AsRegister(self)->getShiftType());
217 }
218 catch (const triton::exceptions::Exception& e) {
219 return PyErr_Format(PyExc_TypeError, "%s", e.what());
220 }
221 }
222
223
224 static PyObject* Register_getShiftImmediate(PyObject* self, PyObject* noarg) {
225 try {
226 return PyLong_FromUint64(PyRegister_AsRegister(self)->getShiftImmediate());
227 }
228 catch (const triton::exceptions::Exception& e) {
229 return PyErr_Format(PyExc_TypeError, "%s", e.what());
230 }
231 }
232
233
234 static PyObject* Register_getShiftRegister(PyObject* self, PyObject* noarg) {
235 try {
236 return PyLong_FromUint64(PyRegister_AsRegister(self)->getShiftRegister());
237 }
238 catch (const triton::exceptions::Exception& e) {
239 return PyErr_Format(PyExc_TypeError, "%s", e.what());
240 }
241 }
242
243
244 static PyObject* Register_getSize(PyObject* self, PyObject* noarg) {
245 try {
246 return PyLong_FromUint32(PyRegister_AsRegister(self)->getSize());
247 }
248 catch (const triton::exceptions::Exception& e) {
249 return PyErr_Format(PyExc_TypeError, "%s", e.what());
250 }
251 }
252
253
254 static PyObject* Register_getType(PyObject* self, PyObject* noarg) {
255 try {
256 return PyLong_FromUint32(PyRegister_AsRegister(self)->getType());
257 }
258 catch (const triton::exceptions::Exception& e) {
259 return PyErr_Format(PyExc_TypeError, "%s", e.what());
260 }
261 }
262
263
264 static PyObject* Register_getVASType(PyObject* self, PyObject* noarg) {
265 try {
266 return PyLong_FromUint32(PyRegister_AsRegister(self)->getVASType());
267 }
268 catch (const triton::exceptions::Exception& e) {
269 return PyErr_Format(PyExc_TypeError, "%s", e.what());
270 }
271 }
272
273
274 static PyObject* Register_isMutable(PyObject* self, PyObject* noarg) {
275 try {
276 if (PyRegister_AsRegister(self)->isMutable())
277 Py_RETURN_TRUE;
278 Py_RETURN_FALSE;
279 }
280 catch (const triton::exceptions::Exception& e) {
281 return PyErr_Format(PyExc_TypeError, "%s", e.what());
282 }
283 }
284
285
286 static PyObject* Register_isOverlapWith(PyObject* self, PyObject* reg2) {
287 try {
289
290 if (!PyRegister_Check(reg2))
291 return PyErr_Format(PyExc_TypeError, "Register::isOverlapWith(): Expected a Register as argument.");
292
293 reg1 = PyRegister_AsRegister(self);
294 if (reg1->isOverlapWith(*PyRegister_AsRegister(reg2)))
295 Py_RETURN_TRUE;
296 Py_RETURN_FALSE;
297 }
298 catch (const triton::exceptions::Exception& e) {
299 return PyErr_Format(PyExc_TypeError, "%s", e.what());
300 }
301 }
302
303
304 #if !defined(IS_PY3_8) || !IS_PY3_8
305 static int Register_print(PyObject* self, void* io, int s) {
306 std::cout << PyRegister_AsRegister(self);
307 return 0;
308 }
309 #endif
310
311
312 static long Register_hash(PyObject* self) {
313 return static_cast<long>(PyRegister_AsRegister(self)->getId());
314 }
315
316
317 static PyObject* Register_str(PyObject* self) {
318 try {
319 return PyStr_FromFormat("%s", triton::utils::toString(PyRegister_AsRegister(self)).c_str());
320 }
321 catch (const triton::exceptions::Exception& e) {
322 return PyErr_Format(PyExc_TypeError, "%s", e.what());
323 }
324 }
325
326
327 static PyObject* Register_richcompare(PyObject* self, PyObject* other, int op) {
328 PyObject* result = nullptr;
329 triton::uint32 id1 = 0;
330 triton::uint32 id2 = 0;
331
332 if (!PyRegister_Check(other)) {
333 result = Py_NotImplemented;
334 }
335
336 else {
337 id1 = PyRegister_AsRegister(self)->getId();
338 id2 = PyRegister_AsRegister(other)->getId();
339
340 switch (op) {
341 case Py_LT:
342 result = (id1 < id2) ? Py_True : Py_False;
343 break;
344 case Py_LE:
345 result = (id1 <= id2) ? Py_True : Py_False;
346 break;
347 case Py_EQ:
348 result = (id1 == id2) ? Py_True : Py_False;
349 break;
350 case Py_NE:
351 result = (id1 != id2) ? Py_True : Py_False;
352 break;
353 case Py_GT:
354 result = (id1 > id2) ? Py_True : Py_False;
355 break;
356 case Py_GE:
357 result = (id1 >= id2) ? Py_True : Py_False;
358 break;
359 }
360 }
361
362 Py_INCREF(result);
363 return result;
364 }
365
366
368 PyMethodDef Register_callbacks[] = {
369 {"getBitSize", Register_getBitSize, METH_NOARGS, ""},
370 {"getBitvector", Register_getBitvector, METH_NOARGS, ""},
371 {"getExtendSize", Register_getExtendSize, METH_NOARGS, ""},
372 {"getExtendType", Register_getExtendType, METH_NOARGS, ""},
373 {"getId", Register_getId, METH_NOARGS, ""},
374 {"getName", Register_getName, METH_NOARGS, ""},
375 {"getShiftImmediate", Register_getShiftImmediate, METH_NOARGS, ""},
376 {"getShiftRegister", Register_getShiftRegister, METH_NOARGS, ""},
377 {"getShiftType", Register_getShiftType, METH_NOARGS, ""},
378 {"getSize", Register_getSize, METH_NOARGS, ""},
379 {"getType", Register_getType, METH_NOARGS, ""},
380 {"getVASType", Register_getVASType, METH_NOARGS, ""},
381 {"isMutable", Register_isMutable, METH_NOARGS, ""},
382 {"isOverlapWith", Register_isOverlapWith, METH_O, ""},
383 {nullptr, nullptr, 0, nullptr}
384 };
385
386
387 PyTypeObject Register_Type = {
388 PyVarObject_HEAD_INIT(&PyType_Type, 0)
389 "Register", /* tp_name */
390 sizeof(Register_Object), /* tp_basicsize */
391 0, /* tp_itemsize */
392 (destructor)Register_dealloc, /* tp_dealloc */
393 #if IS_PY3_8
394 0, /* tp_vectorcall_offset */
395 #else
396 (printfunc)Register_print, /* tp_print */
397 #endif
398 0, /* tp_getattr */
399 0, /* tp_setattr */
400 0, /* tp_compare */
401 (reprfunc)Register_str, /* tp_repr */
402 0, /* tp_as_number */
403 0, /* tp_as_sequence */
404 0, /* tp_as_mapping */
405 (hashfunc)Register_hash, /* tp_hash */
406 0, /* tp_call */
407 (reprfunc)Register_str, /* tp_str */
408 0, /* tp_getattro */
409 0, /* tp_setattro */
410 0, /* tp_as_buffer */
411 Py_TPFLAGS_DEFAULT, /* tp_flags */
412 "Register objects", /* tp_doc */
413 0, /* tp_traverse */
414 0, /* tp_clear */
415 (richcmpfunc)Register_richcompare, /* tp_richcompare */
416 0, /* tp_weaklistoffset */
417 0, /* tp_iter */
418 0, /* tp_iternext */
419 Register_callbacks, /* tp_methods */
420 0, /* tp_members */
421 0, /* tp_getset */
422 0, /* tp_base */
423 0, /* tp_dict */
424 0, /* tp_descr_get */
425 0, /* tp_descr_set */
426 0, /* tp_dictoffset */
427 0, /* tp_init */
428 0, /* tp_alloc */
429 0, /* tp_new */
430 0, /* tp_free */
431 0, /* tp_is_gc */
432 0, /* tp_bases */
433 0, /* tp_mro */
434 0, /* tp_cache */
435 0, /* tp_subclasses */
436 0, /* tp_weaklist */
437 0, /* tp_del */
438 #if IS_PY3
439 0, /* tp_version_tag */
440 0, /* tp_finalize */
441 #if IS_PY3_8
442 0, /* tp_vectorcall */
443 #if !IS_PY3_9
444 0, /* bpo-37250: kept for backwards compatibility in CPython 3.8 only */
445 #endif
446 #endif
447 #else
448 0 /* tp_version_tag */
449 #endif
450 };
451
452
453 PyObject* PyRegister(const triton::arch::Register& reg) {
454 Register_Object* object;
455
456 PyType_Ready(&Register_Type);
457 object = PyObject_NEW(Register_Object, &Register_Type);
458 if (object != NULL)
459 object->reg = new triton::arch::Register(reg);
460
461 return (PyObject*)object;
462 }
463
464 }; /* python namespace */
465 }; /* bindings namespace */
466}; /* triton namespace */
This class is used when an instruction has a register operand.
Definition register.hpp:44
TRITON_EXPORT bool isOverlapWith(const Register &other) const
Returns true if other and self overlap.
Definition register.cpp:86
The root class of all exceptions.
TRITON_EXPORT const char * what() const
Returns the exception message.
PyTypeObject Register_Type
pyRegister type.
PyObject * PyLong_FromUint64(triton::uint64 value)
Returns a pyObject from a triton::uint64.
Definition utils.cpp:328
PyObject * PyRegister(const triton::arch::Register &reg)
Creates the Register python class.
PyObject * PyLong_FromUint32(triton::uint32 value)
Returns a pyObject from a triton::uint32.
Definition utils.cpp:322
std::uint32_t uint32
unisgned 32-bits
std::string toString(const T &obj)
Converts an object to a string.
Definition coreUtils.hpp:38
PyMethodDef Register_callbacks[]
Register methods.
void Register_dealloc(PyObject *self)
Register destructor.
The Triton namespace.
#define PyRegister_AsRegister(v)
#define PyRegister_Check(v)