libTriton version 1.0 build 1590
Loading...
Searching...
No Matches
pyImmediate.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/immediate.hpp>
14
15#include <iostream>
16
17
18
19/* setup doctest context
20
21>>> from __future__ import print_function
22>>> from triton import TritonContext, ARCH, Instruction, Immediate, CPUSIZE
23
24>>> ctxt = TritonContext()
25>>> ctxt.setArchitecture(ARCH.X86_64)
26
27>>> inst = Instruction()
28>>> inst.setOpcode(b"\xB8\x14\x00\x00\x00")
29
30*/
31
116namespace triton {
117 namespace bindings {
118 namespace python {
119
121 void Immediate_dealloc(PyObject* self) {
122 std::cout << std::flush;
123 delete PyImmediate_AsImmediate(self);
124 Py_TYPE(self)->tp_free((PyObject*)self);
125 }
126
127
128 static PyObject* Immediate_getBitvector(PyObject* self, PyObject* noarg) {
129 try {
130 return PyBitsVector(*PyImmediate_AsImmediate(self));
131 }
132 catch (const triton::exceptions::Exception& e) {
133 return PyErr_Format(PyExc_TypeError, "%s", e.what());
134 }
135 }
136
137
138 static PyObject* Immediate_getBitSize(PyObject* self, PyObject* noarg) {
139 try {
140 return PyLong_FromUint32(PyImmediate_AsImmediate(self)->getBitSize());
141 }
142 catch (const triton::exceptions::Exception& e) {
143 return PyErr_Format(PyExc_TypeError, "%s", e.what());
144 }
145 }
146
147
148 static PyObject* Immediate_getShiftType(PyObject* self, PyObject* noarg) {
149 try {
150 return PyLong_FromUint32(PyImmediate_AsImmediate(self)->getShiftType());
151 }
152 catch (const triton::exceptions::Exception& e) {
153 return PyErr_Format(PyExc_TypeError, "%s", e.what());
154 }
155 }
156
157
158 static PyObject* Immediate_getShiftImmediate(PyObject* self, PyObject* noarg) {
159 try {
160 return PyLong_FromUint64(PyImmediate_AsImmediate(self)->getShiftImmediate());
161 }
162 catch (const triton::exceptions::Exception& e) {
163 return PyErr_Format(PyExc_TypeError, "%s", e.what());
164 }
165 }
166
167
168 static PyObject* Immediate_getShiftRegister(PyObject* self, PyObject* noarg) {
169 try {
170 return PyLong_FromUint64(PyImmediate_AsImmediate(self)->getShiftRegister());
171 }
172 catch (const triton::exceptions::Exception& e) {
173 return PyErr_Format(PyExc_TypeError, "%s", e.what());
174 }
175 }
176
177
178 static PyObject* Immediate_getSize(PyObject* self, PyObject* noarg) {
179 try {
180 return PyLong_FromUint32(PyImmediate_AsImmediate(self)->getSize());
181 }
182 catch (const triton::exceptions::Exception& e) {
183 return PyErr_Format(PyExc_TypeError, "%s", e.what());
184 }
185 }
186
187
188 static PyObject* Immediate_getType(PyObject* self, PyObject* noarg) {
189 try {
190 return PyLong_FromUint32(PyImmediate_AsImmediate(self)->getType());
191 }
192 catch (const triton::exceptions::Exception& e) {
193 return PyErr_Format(PyExc_TypeError, "%s", e.what());
194 }
195 }
196
197
198 static PyObject* Immediate_getValue(PyObject* self, PyObject* noarg) {
199 try {
200 return PyLong_FromUint64(PyImmediate_AsImmediate(self)->getValue());
201 }
202 catch (const triton::exceptions::Exception& e) {
203 return PyErr_Format(PyExc_TypeError, "%s", e.what());
204 }
205 }
206
207
208 static PyObject* Immediate_setValue(PyObject* self, PyObject* args) {
209 PyObject* value = nullptr;
210 PyObject* size = nullptr;
211
212 /* Extract arguments */
213 if (PyArg_ParseTuple(args, "|OO", &value, &size) == false) {
214 return PyErr_Format(PyExc_TypeError, "Immediate::setValue(): Invalid number of arguments");
215 }
216
217 try {
218 if (!PyLong_Check(value) && !PyInt_Check(value))
219 return PyErr_Format(PyExc_TypeError, "Immediate::setValue(): expected an integer as first argument");
220
221 if (!PyLong_Check(size) && !PyInt_Check(size))
222 return PyErr_Format(PyExc_TypeError, "Immediate::setValue(): expected an integer as second argument");
223
224 PyImmediate_AsImmediate(self)->setValue(PyLong_AsUint64(value), PyLong_AsUint32(size));
225 Py_INCREF(Py_None);
226 return Py_None;
227 }
228 catch (const triton::exceptions::Exception& e) {
229 return PyErr_Format(PyExc_TypeError, "%s", e.what());
230 }
231 }
232
233
234 #if !defined(IS_PY3_8) || !IS_PY3_8
235 static int Immediate_print(PyObject* self, void* io, int s) {
236 std::cout << PyImmediate_AsImmediate(self);
237 return 0;
238 }
239 #endif
240
241
242 static PyObject* Immediate_str(PyObject* self) {
243 try {
244 return PyStr_FromFormat("%s", triton::utils::toString(PyImmediate_AsImmediate(self)).c_str());
245 }
246 catch (const triton::exceptions::Exception& e) {
247 return PyErr_Format(PyExc_TypeError, "%s", e.what());
248 }
249 }
250
251
253 PyMethodDef Immediate_callbacks[] = {
254 {"getBitSize", Immediate_getBitSize, METH_NOARGS, ""},
255 {"getBitvector", Immediate_getBitvector, METH_NOARGS, ""},
256 {"getShiftType", Immediate_getShiftType, METH_NOARGS, ""},
257 {"getShiftImmediate", Immediate_getShiftImmediate, METH_NOARGS, ""},
258 {"getShiftRegister", Immediate_getShiftRegister, METH_NOARGS, ""},
259 {"getSize", Immediate_getSize, METH_NOARGS, ""},
260 {"getType", Immediate_getType, METH_NOARGS, ""},
261 {"getValue", Immediate_getValue, METH_NOARGS, ""},
262 {"setValue", Immediate_setValue, METH_VARARGS, ""},
263 {nullptr, nullptr, 0, nullptr}
264 };
265
266
267 PyTypeObject Immediate_Type = {
268 PyVarObject_HEAD_INIT(&PyType_Type, 0)
269 "Immediate", /* tp_name */
270 sizeof(Immediate_Object), /* tp_basicsize */
271 0, /* tp_itemsize */
272 (destructor)Immediate_dealloc, /* tp_dealloc */
273 #if IS_PY3_8
274 0, /* tp_vectorcall_offset */
275 #else
276 (printfunc)Immediate_print, /* tp_print */
277 #endif
278 0, /* tp_getattr */
279 0, /* tp_setattr */
280 0, /* tp_compare */
281 (reprfunc)Immediate_str, /* tp_repr */
282 0, /* tp_as_number */
283 0, /* tp_as_sequence */
284 0, /* tp_as_mapping */
285 0, /* tp_hash */
286 0, /* tp_call */
287 (reprfunc)Immediate_str, /* tp_str */
288 0, /* tp_getattro */
289 0, /* tp_setattro */
290 0, /* tp_as_buffer */
291 Py_TPFLAGS_DEFAULT, /* tp_flags */
292 "Immediate objects", /* tp_doc */
293 0, /* tp_traverse */
294 0, /* tp_clear */
295 0, /* tp_richcompare */
296 0, /* tp_weaklistoffset */
297 0, /* tp_iter */
298 0, /* tp_iternext */
299 Immediate_callbacks, /* tp_methods */
300 0, /* tp_members */
301 0, /* tp_getset */
302 0, /* tp_base */
303 0, /* tp_dict */
304 0, /* tp_descr_get */
305 0, /* tp_descr_set */
306 0, /* tp_dictoffset */
307 0, /* tp_init */
308 0, /* tp_alloc */
309 0, /* tp_new */
310 0, /* tp_free */
311 0, /* tp_is_gc */
312 0, /* tp_bases */
313 0, /* tp_mro */
314 0, /* tp_cache */
315 0, /* tp_subclasses */
316 0, /* tp_weaklist */
317 0, /* tp_del */
318 #if IS_PY3
319 0, /* tp_version_tag */
320 0, /* tp_finalize */
321 #if IS_PY3_8
322 0, /* tp_vectorcall */
323 #if !IS_PY3_9
324 0, /* bpo-37250: kept for backwards compatibility in CPython 3.8 only */
325 #endif
326 #endif
327 #else
328 0 /* tp_version_tag */
329 #endif
330 };
331
332
333 PyObject* PyImmediate(const triton::arch::Immediate& imm) {
334 Immediate_Object* object;
335
336 PyType_Ready(&Immediate_Type);
337 object = PyObject_NEW(Immediate_Object, &Immediate_Type);
338 if (object != NULL)
339 object->imm = new triton::arch::Immediate(imm);
340
341 return (PyObject*)object;
342 }
343
344 }; /* python namespace */
345 }; /* bindings namespace */
346}; /* triton namespace */
This class is used to represent an immediate.
Definition immediate.hpp:37
The root class of all exceptions.
TRITON_EXPORT const char * what() const
Returns the exception message.
triton::uint64 PyLong_AsUint64(PyObject *vv)
Returns a triton::uint64 from a pyObject.
Definition utils.cpp:114
PyObject * PyImmediate(const triton::arch::Immediate &imm)
Creates the Immediate python class.
PyObject * PyLong_FromUint64(triton::uint64 value)
Returns a pyObject from a triton::uint64.
Definition utils.cpp:311
triton::uint32 PyLong_AsUint32(PyObject *vv)
Returns a triton::uint32 from a pyObject.
Definition utils.cpp:85
PyTypeObject Immediate_Type
pyImmediate type.
PyObject * PyLong_FromUint32(triton::uint32 value)
Returns a pyObject from a triton::uint32.
Definition utils.cpp:305
std::string toString(const T &obj)
Converts an object to a string.
Definition coreUtils.hpp:38
PyMethodDef Immediate_callbacks[]
Immediate methods.
void Immediate_dealloc(PyObject *self)
Immediate destructor.
The Triton namespace.
#define PyImmediate_AsImmediate(v)