libTriton version 1.0 build 1592
Loading...
Searching...
No Matches
pyBasicBlock.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/basicBlock.hpp>
14
15#include <iostream>
16
17
18/* setup doctest context
19
20>>> from __future__ import print_function
21>>> from triton import *
22>>> ctx = TritonContext(ARCH.X86_64)
23
24*/
25
130namespace triton {
131 namespace bindings {
132 namespace python {
133
135 void BasicBlock_dealloc(PyObject* self) {
136 std::cout << std::flush;
137 delete PyBasicBlock_AsBasicBlock(self);
138 Py_TYPE(self)->tp_free((PyObject*)self);
139 }
140
141
142 static PyObject* BasicBlock_add(PyObject* self, PyObject* instruction) {
143 try {
144 if (!PyInstruction_Check(instruction))
145 return PyErr_Format(PyExc_TypeError, "BasicBlock::add(): Expected an Instruction as argument.");
147 Py_INCREF(Py_None);
148 return Py_None;
149 }
150 catch (const triton::exceptions::Exception& e) {
151 return PyErr_Format(PyExc_TypeError, "%s", e.what());
152 }
153 }
154
155
156 static PyObject* BasicBlock_getFirstAddress(PyObject* self, PyObject* noarg) {
157 try {
158 return PyLong_FromUint64(PyBasicBlock_AsBasicBlock(self)->getFirstAddress());
159 }
160 catch (const triton::exceptions::Exception& e) {
161 return PyErr_Format(PyExc_TypeError, "%s", e.what());
162 }
163 }
164
165
166 static PyObject* BasicBlock_getInstructions(PyObject* self, PyObject* noarg) {
167 PyObject* ret = nullptr;
168 triton::usize index = 0;
169
170 try {
171 auto insts = PyBasicBlock_AsBasicBlock(self)->getInstructions();
172 ret = xPyList_New(insts.size());
173 for (auto& inst : insts)
174 PyList_SetItem(ret, index++, PyInstruction(inst));
175 return ret;
176 }
177 catch (const triton::exceptions::Exception& e) {
178 return PyErr_Format(PyExc_TypeError, "%s", e.what());
179 }
180 }
181
182
183 static PyObject* BasicBlock_getLastAddress(PyObject* self, PyObject* noarg) {
184 try {
185 return PyLong_FromUint64(PyBasicBlock_AsBasicBlock(self)->getLastAddress());
186 }
187 catch (const triton::exceptions::Exception& e) {
188 return PyErr_Format(PyExc_TypeError, "%s", e.what());
189 }
190 }
191
192
193 static PyObject* BasicBlock_getSize(PyObject* self, PyObject* noarg) {
194 try {
195 return PyLong_FromUint64(PyBasicBlock_AsBasicBlock(self)->getSize());
196 }
197 catch (const triton::exceptions::Exception& e) {
198 return PyErr_Format(PyExc_TypeError, "%s", e.what());
199 }
200 }
201
202
203 static PyObject* BasicBlock_remove(PyObject* self, PyObject* position) {
204 try {
205 if (!PyLong_Check(position) && !PyInt_Check(position))
206 return PyErr_Format(PyExc_TypeError, "BasicBlock::remove(): Expected an integer as argument.");
207 if (PyBasicBlock_AsBasicBlock(self)->remove(PyLong_AsUint64(position)) == true)
208 Py_RETURN_TRUE;
209 Py_RETURN_FALSE;
210 }
211 catch (const triton::exceptions::Exception& e) {
212 return PyErr_Format(PyExc_TypeError, "%s", e.what());
213 }
214 }
215
216
217 #if !defined(IS_PY3_8) || !IS_PY3_8
218 static int BasicBlock_print(PyObject* self, void* io, int s) {
219 std::cout << PyBasicBlock_AsBasicBlock(self);
220 return 0;
221 }
222 #endif
223
224
225 static PyObject* BasicBlock_str(PyObject* self) {
226 try {
227 return PyStr_FromFormat("%s", triton::utils::toString(PyBasicBlock_AsBasicBlock(self)).c_str());
228 }
229 catch (const triton::exceptions::Exception& e) {
230 return PyErr_Format(PyExc_TypeError, "%s", e.what());
231 }
232 }
233
234
236 PyMethodDef BasicBlock_callbacks[] = {
237 {"add", BasicBlock_add, METH_O, ""},
238 {"getFirstAddress", BasicBlock_getFirstAddress, METH_NOARGS, ""},
239 {"getInstructions", BasicBlock_getInstructions, METH_NOARGS, ""},
240 {"getLastAddress", BasicBlock_getLastAddress, METH_NOARGS, ""},
241 {"getSize", BasicBlock_getSize, METH_NOARGS, ""},
242 {"remove", BasicBlock_remove, METH_O, ""},
243 {nullptr, nullptr, 0, nullptr}
244 };
245
246
247 PyTypeObject BasicBlock_Type = {
248 PyVarObject_HEAD_INIT(&PyType_Type, 0)
249 "BasicBlock", /* tp_name */
250 sizeof(BasicBlock_Object), /* tp_basicsize */
251 0, /* tp_itemsize */
252 (destructor)BasicBlock_dealloc, /* tp_dealloc */
253 #if IS_PY3_8
254 0, /* tp_vectorcall_offset */
255 #else
256 (printfunc)BasicBlock_print, /* tp_print */
257 #endif
258 0, /* tp_getattr */
259 0, /* tp_setattr */
260 0, /* tp_compare */
261 (reprfunc)BasicBlock_str, /* tp_repr */
262 0, /* tp_as_number */
263 0, /* tp_as_sequence */
264 0, /* tp_as_mapping */
265 0, /* tp_hash */
266 0, /* tp_call */
267 (reprfunc)BasicBlock_str, /* tp_str */
268 0, /* tp_getattro */
269 0, /* tp_setattro */
270 0, /* tp_as_buffer */
271 Py_TPFLAGS_DEFAULT, /* tp_flags */
272 "BasicBlock objects", /* tp_doc */
273 0, /* tp_traverse */
274 0, /* tp_clear */
275 0, /* tp_richcompare */
276 0, /* tp_weaklistoffset */
277 0, /* tp_iter */
278 0, /* tp_iternext */
279 BasicBlock_callbacks, /* tp_methods */
280 0, /* tp_members */
281 0, /* tp_getset */
282 0, /* tp_base */
283 0, /* tp_dict */
284 0, /* tp_descr_get */
285 0, /* tp_descr_set */
286 0, /* tp_dictoffset */
287 0, /* tp_init */
288 0, /* tp_alloc */
289 0, /* tp_new */
290 0, /* tp_free */
291 0, /* tp_is_gc */
292 0, /* tp_bases */
293 0, /* tp_mro */
294 0, /* tp_cache */
295 0, /* tp_subclasses */
296 0, /* tp_weaklist */
297 0, /* tp_del */
298 #if IS_PY3
299 0, /* tp_version_tag */
300 0, /* tp_finalize */
301 #if IS_PY3_8
302 0, /* tp_vectorcall */
303 #if !IS_PY3_9
304 0, /* bpo-37250: kept for backwards compatibility in CPython 3.8 only */
305 #endif
306 #endif
307 #else
308 0 /* tp_version_tag */
309 #endif
310 };
311
312
313 PyObject* PyBasicBlock(const triton::arch::BasicBlock& block) {
314 BasicBlock_Object* object;
315
316 PyType_Ready(&BasicBlock_Type);
317 object = PyObject_NEW(BasicBlock_Object, &BasicBlock_Type);
318 if (object != NULL)
319 object->block = new triton::arch::BasicBlock(block);
320
321 return (PyObject*)object;
322 }
323
324
325 PyObject* PyBasicBlock(std::vector<triton::arch::Instruction>& insts) {
326 BasicBlock_Object* object;
327
328 PyType_Ready(&BasicBlock_Type);
329 object = PyObject_NEW(BasicBlock_Object, &BasicBlock_Type);
330 if (object != NULL)
331 object->block = new triton::arch::BasicBlock(insts);
332
333 return (PyObject*)object;
334 }
335
336
337 PyObject* PyBasicBlock(void) {
338 BasicBlock_Object* object;
339
340 PyType_Ready(&BasicBlock_Type);
341 object = PyObject_NEW(BasicBlock_Object, &BasicBlock_Type);
342 if (object != NULL)
343 object->block = new triton::arch::BasicBlock();
344
345 return (PyObject*)object;
346 }
347
348 }; /* python namespace */
349 }; /* bindings namespace */
350}; /* triton namespace */
This class is used to represent a basic block.
The root class of all exceptions.
TRITON_EXPORT const char * what() const
Returns the exception message.
PyTypeObject BasicBlock_Type
pyBasicBlock type.
triton::uint64 PyLong_AsUint64(PyObject *vv)
Returns a triton::uint64 from a pyObject.
Definition utils.cpp:137
PyObject * PyBasicBlock(void)
Creates the BasicBlock python class.
PyObject * PyInstruction(void)
Creates the Instruction python class.
PyObject * xPyList_New(Py_ssize_t len)
Creates a PyList and raises an exception if it fails.
PyObject * PyLong_FromUint64(triton::uint64 value)
Returns a pyObject from a triton::uint64.
Definition utils.cpp:328
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 BasicBlock_callbacks[]
BasicBlock methods.
void BasicBlock_dealloc(PyObject *self)
BasicBlock destructor.
The Triton namespace.
#define PyInstruction_AsInstruction(v)
#define PyInstruction_Check(v)
#define PyBasicBlock_AsBasicBlock(v)