1
//! \file
2
/*
3
**  Copyright (C) - Triton
4
**
5
**  This program is under the terms of the Apache License 2.0.
6
*/
7

8
#include <triton/pythonObjects.hpp>
9
#include <triton/pythonUtils.hpp>
10
#include <triton/pythonXFunctions.hpp>
11
#include <triton/exceptions.hpp>
12
#include <triton/immediate.hpp>
13

14

15

16
/* setup doctest context
17

18
>>> from __future__ import print_function
19
>>> from triton import TritonContext, ARCH, Instruction, Immediate, CPUSIZE
20

21
>>> ctxt = TritonContext()
22
>>> ctxt.setArchitecture(ARCH.X86_64)
23

24
>>> inst = Instruction()
25
>>> inst.setOpcode(b"\xB8\x14\x00\x00\x00")
26

27
*/
28

29
/*! \page py_Immediate_page Immediate
30
    \brief [**python api**] All information about the Immediate Python object.
31

32
\tableofcontents
33

34
\section py_Immediate_description Description
35
<hr>
36

37
This object is used to represent an immediate.
38

39
\subsection py_Immediate_example Example
40

41
~~~~~~~~~~~~~{.py}
42
>>> ctxt.processing(inst)
43
True
44
>>> print(inst)
45
0x0: mov eax, 0x14
46

47
>>> op1 = inst.getOperands()[1]
48
>>> print(op1)
49
0x14:32 bv[31..0]
50

51
>>> print(hex(op1.getValue()))
52
0x14
53

54
>>> print(op1.getBitSize())
55
32
56

57
~~~~~~~~~~~~~
58

59
\subsection py_Immediate_constructor Constructor
60

61
~~~~~~~~~~~~~{.py}
62
>>> imm = Immediate(0x1234, CPUSIZE.WORD)
63
>>> print(imm)
64
0x1234:16 bv[15..0]
65
>>> imm.getValue()
66
4660
67
>>> imm.getSize()
68
2
69
>>> imm.getBitSize()
70
16
71

72
~~~~~~~~~~~~~
73

74
\section Immediate_py_api Python API - Methods of the Immediate class
75
<hr>
76

77
- <b>integer getBitSize(void)</b><br>
78
Returns the size (in bits) of the immediate.<br>
79
e.g: `64`
80

81
- <b>\ref py_BitsVector_page getBitvector(void)</b><br>
82
Returns the bit vector.
83

84
- <b>\ref py_SHIFT_page getShiftType(void)</b><br>
85
Returns the shift type of the operand. Mainly used for AArch64.<br>
86
e.g: `SHIFT.ARM.LSL`
87

88
- <b>integer getShiftImmediate(void)</b><br>
89
Returns the shift immediate value of the operand. Mainly used for AArch64 and ARM32.<br>
90
e.g: `2`
91

92
- <b>\ref py_REG_page getShiftRegister(void)</b><br>
93
Returns the shift register of the operand. Mainly used for ARM32.<br>
94
e.g: `REG.ARM32.R0`
95

96
- <b>integer getSize(void)</b><br>
97
Returns the size (in bytes) of the immediate.<br>
98
e.g: `8`
99

100
- <b>\ref py_OPERAND_page getType(void)</b><br>
101
Returns the type of the immediate. In this case this function returns `OPERAND.IMM`.
102

103
- <b>integer getValue(void)</b><br>
104
Returns the immediate value.
105

106
- <b>void setValue(integer value, integer size)</b><br>
107
Sets the immediate value.
108

109
*/
110

111

112

113
namespace triton {
114
  namespace bindings {
115
    namespace python {
116

117
      //! Immediate destructor.
118 1
      void Immediate_dealloc(PyObject* self) {
119 1
        std::cout << std::flush;
120 1
        delete PyImmediate_AsImmediate(self);
121 1
        Py_TYPE(self)->tp_free((PyObject*)self);
122
      }
123

124

125 0
      static PyObject* Immediate_getBitvector(PyObject* self, PyObject* noarg) {
126
        try {
127 0
          return PyBitsVector(*PyImmediate_AsImmediate(self));
128
        }
129 0
        catch (const triton::exceptions::Exception& e) {
130 0
          return PyErr_Format(PyExc_TypeError, "%s", e.what());
131
        }
132
      }
133

134

135 1
      static PyObject* Immediate_getBitSize(PyObject* self, PyObject* noarg) {
136
        try {
137 1
          return PyLong_FromUint32(PyImmediate_AsImmediate(self)->getBitSize());
138
        }
139 0
        catch (const triton::exceptions::Exception& e) {
140 0
          return PyErr_Format(PyExc_TypeError, "%s", e.what());
141
        }
142
      }
143

144

145 1
      static PyObject* Immediate_getShiftType(PyObject* self, PyObject* noarg) {
146
        try {
147 1
          return PyLong_FromUint32(PyImmediate_AsImmediate(self)->getShiftType());
148
        }
149 0
        catch (const triton::exceptions::Exception& e) {
150 0
          return PyErr_Format(PyExc_TypeError, "%s", e.what());
151
        }
152
      }
153

154

155 1
      static PyObject* Immediate_getShiftImmediate(PyObject* self, PyObject* noarg) {
156
        try {
157 1
          return PyLong_FromUint64(PyImmediate_AsImmediate(self)->getShiftImmediate());
158
        }
159 0
        catch (const triton::exceptions::Exception& e) {
160 0
          return PyErr_Format(PyExc_TypeError, "%s", e.what());
161
        }
162
      }
163

164

165 0
      static PyObject* Immediate_getShiftRegister(PyObject* self, PyObject* noarg) {
166
        try {
167 0
          return PyLong_FromUint64(PyImmediate_AsImmediate(self)->getShiftRegister());
168
        }
169 0
        catch (const triton::exceptions::Exception& e) {
170 0
          return PyErr_Format(PyExc_TypeError, "%s", e.what());
171
        }
172
      }
173

174

175 1
      static PyObject* Immediate_getSize(PyObject* self, PyObject* noarg) {
176
        try {
177 1
          return PyLong_FromUint32(PyImmediate_AsImmediate(self)->getSize());
178
        }
179 0
        catch (const triton::exceptions::Exception& e) {
180 0
          return PyErr_Format(PyExc_TypeError, "%s", e.what());
181
        }
182
      }
183

184

185 1
      static PyObject* Immediate_getType(PyObject* self, PyObject* noarg) {
186
        try {
187 1
          return PyLong_FromUint32(PyImmediate_AsImmediate(self)->getType());
188
        }
189 0
        catch (const triton::exceptions::Exception& e) {
190 0
          return PyErr_Format(PyExc_TypeError, "%s", e.what());
191
        }
192
      }
193

194

195 1
      static PyObject* Immediate_getValue(PyObject* self, PyObject* noarg) {
196
        try {
197 1
          return PyLong_FromUint64(PyImmediate_AsImmediate(self)->getValue());
198
        }
199 0
        catch (const triton::exceptions::Exception& e) {
200 0
          return PyErr_Format(PyExc_TypeError, "%s", e.what());
201
        }
202
      }
203

204

205 0
      static PyObject* Immediate_setValue(PyObject* self, PyObject* args) {
206 0
        PyObject* value = nullptr;
207 0
        PyObject* size  = nullptr;
208

209
        /* Extract arguments */
210 0
        if (PyArg_ParseTuple(args, "|OO", &value, &size) == false) {
211 0
          return PyErr_Format(PyExc_TypeError, "Immediate::setValue(): Invalid number of arguments");
212
        }
213

214
        try {
215 0
          if (!PyLong_Check(value) && !PyInt_Check(value))
216 0
            return PyErr_Format(PyExc_TypeError, "Immediate::setValue(): expected an integer as first argument");
217

218 0
          if (!PyLong_Check(size) && !PyInt_Check(size))
219 0
            return PyErr_Format(PyExc_TypeError, "Immediate::setValue(): expected an integer as second argument");
220

221 0
          PyImmediate_AsImmediate(self)->setValue(PyLong_AsUint64(value), PyLong_AsUint32(size));
222 0
          Py_INCREF(Py_None);
223 0
          return Py_None;
224
        }
225 0
        catch (const triton::exceptions::Exception& e) {
226 0
          return PyErr_Format(PyExc_TypeError, "%s", e.what());
227
        }
228
      }
229

230

231
      #if !defined(IS_PY3_8) || !IS_PY3_8
232 0
      static int Immediate_print(PyObject* self, void* io, int s) {
233 0
        std::cout << PyImmediate_AsImmediate(self);
234 0
        return 0;
235
      }
236
      #endif
237

238

239 1
      static PyObject* Immediate_str(PyObject* self) {
240
        try {
241 1
          std::stringstream str;
242 1
          str << PyImmediate_AsImmediate(self);
243 1
          return PyStr_FromFormat("%s", str.str().c_str());
244
        }
245 0
        catch (const triton::exceptions::Exception& e) {
246 0
          return PyErr_Format(PyExc_TypeError, "%s", e.what());
247
        }
248
      }
249

250

251
      //! Immediate methods.
252
      PyMethodDef Immediate_callbacks[] = {
253
        {"getBitSize",        Immediate_getBitSize,        METH_NOARGS,  ""},
254
        {"getBitvector",      Immediate_getBitvector,      METH_NOARGS,  ""},
255
        {"getShiftType",      Immediate_getShiftType,      METH_NOARGS,  ""},
256
        {"getShiftImmediate", Immediate_getShiftImmediate, METH_NOARGS,  ""},
257
        {"getShiftRegister",  Immediate_getShiftRegister,  METH_NOARGS,  ""},
258
        {"getSize",           Immediate_getSize,           METH_NOARGS,  ""},
259
        {"getType",           Immediate_getType,           METH_NOARGS,  ""},
260
        {"getValue",          Immediate_getValue,          METH_NOARGS,  ""},
261
        {"setValue",          Immediate_setValue,          METH_VARARGS, ""},
262
        {nullptr,             nullptr,                     0,            nullptr}
263
      };
264

265

266
      PyTypeObject Immediate_Type = {
267
        PyVarObject_HEAD_INIT(&PyType_Type, 0)
268
        "Immediate",                                /* tp_name */
269
        sizeof(Immediate_Object),                   /* tp_basicsize */
270
        0,                                          /* tp_itemsize */
271
        (destructor)Immediate_dealloc,              /* tp_dealloc */
272
        #if IS_PY3_8
273
        0,                                          /* tp_vectorcall_offset */
274
        #else
275
        (printfunc)Immediate_print,                 /* tp_print */
276
        #endif
277
        0,                                          /* tp_getattr */
278
        0,                                          /* tp_setattr */
279
        0,                                          /* tp_compare */
280
        (reprfunc)Immediate_str,                    /* tp_repr */
281
        0,                                          /* tp_as_number */
282
        0,                                          /* tp_as_sequence */
283
        0,                                          /* tp_as_mapping */
284
        0,                                          /* tp_hash */
285
        0,                                          /* tp_call */
286
        (reprfunc)Immediate_str,                    /* tp_str */
287
        0,                                          /* tp_getattro */
288
        0,                                          /* tp_setattro */
289
        0,                                          /* tp_as_buffer */
290
        Py_TPFLAGS_DEFAULT,                         /* tp_flags */
291
        "Immediate objects",                        /* tp_doc */
292
        0,                                          /* tp_traverse */
293
        0,                                          /* tp_clear */
294
        0,                                          /* tp_richcompare */
295
        0,                                          /* tp_weaklistoffset */
296
        0,                                          /* tp_iter */
297
        0,                                          /* tp_iternext */
298
        Immediate_callbacks,                        /* tp_methods */
299
        0,                                          /* tp_members */
300
        0,                                          /* tp_getset */
301
        0,                                          /* tp_base */
302
        0,                                          /* tp_dict */
303
        0,                                          /* tp_descr_get */
304
        0,                                          /* tp_descr_set */
305
        0,                                          /* tp_dictoffset */
306
        0,                                          /* tp_init */
307
        0,                                          /* tp_alloc */
308
        0,                                          /* tp_new */
309
        0,                                          /* tp_free */
310
        0,                                          /* tp_is_gc */
311
        0,                                          /* tp_bases */
312
        0,                                          /* tp_mro */
313
        0,                                          /* tp_cache */
314
        0,                                          /* tp_subclasses */
315
        0,                                          /* tp_weaklist */
316
        0,                                          /* tp_del */
317
        #if IS_PY3
318
        0,                                          /* tp_version_tag */
319
        0,                                          /* tp_finalize */
320
        #if IS_PY3_8
321
        0,                                          /* tp_vectorcall */
322
        0,                                          /* bpo-37250: kept for backwards compatibility in CPython 3.8 only */
323
        #endif
324
        #else
325
        0                                           /* tp_version_tag */
326
        #endif
327
      };
328

329

330 1
      PyObject* PyImmediate(const triton::arch::Immediate& imm) {
331
        Immediate_Object* object;
332

333 1
        PyType_Ready(&Immediate_Type);
334 1
        object = PyObject_NEW(Immediate_Object, &Immediate_Type);
335 1
        if (object != NULL)
336 1
          object->imm = new triton::arch::Immediate(imm);
337

338 1
        return (PyObject*)object;
339
      }
340

341
    }; /* python namespace */
342
  }; /* bindings namespace */
343 1
}; /* triton namespace */

Read our documentation on viewing source code .

Loading