miasm
Reverse engineering framework
compat_py23.h
Go to the documentation of this file.
1 #ifndef __COMPAT_PY23_H__
2 #define __COMPAT_PY23_H__
3 
4 
5 #include "bn.h"
6 
7 #if PY_MAJOR_VERSION >= 3
8 #define PyGetInt_uint_t(size_type, item, value) \
9  if (PyLong_Check(item)) { \
10  Py_INCREF(item); \
11  PyObject* py_long = item; \
12  PyObject* py_long_new; \
13  bn_t bn; \
14  uint64_t tmp; \
15  int neg = 0; \
16  \
17  if (Py_SIZE(py_long) < 0) { \
18  neg = 1; \
19  py_long_new = PyObject_CallMethod(py_long, "__neg__", NULL); \
20  Py_DECREF(py_long); \
21  py_long = py_long_new; \
22  } \
23  \
24  bn = PyLong_to_bn(py_long); \
25  \
26  bn_t mask_bn = bignum_lshift(bignum_from_int(1), sizeof(size_type)*8); \
27  if (bignum_is_inf_equal_unsigned(mask_bn, bn)) { \
28  RAISE(PyExc_TypeError, "Arg too big for " #size_type ""); \
29  } \
30  if (neg) { \
31  bn = bignum_sub(mask_bn, bn); \
32  } \
33  tmp = bignum_to_uint64(bn); \
34  value = (size_type) tmp; \
35  } \
36  else{ \
37  RAISE(PyExc_TypeError, "Arg must be int"); \
38  }
39 
40 
41 #define PyGetInt_uint_t_retneg(size_type, item, value) \
42  if (PyLong_Check(item)) { \
43  Py_INCREF(item); \
44  PyObject* py_long = item; \
45  PyObject* py_long_new; \
46  bn_t bn; \
47  uint64_t tmp; \
48  int neg = 0; \
49  \
50  if (Py_SIZE(py_long) < 0) { \
51  neg = 1; \
52  py_long_new = PyObject_CallMethod(py_long, "__neg__", NULL); \
53  Py_DECREF(py_long); \
54  py_long = py_long_new; \
55  } \
56  \
57  bn = PyLong_to_bn(py_long); \
58  \
59  bn_t mask_bn = bignum_lshift(bignum_from_int(1), sizeof(size_type)*8); \
60  if (bignum_is_inf_equal_unsigned(mask_bn, bn)) { \
61  PyErr_SetString(PyExc_TypeError, "Arg too big for " #size_type ""); \
62  return -1; \
63  } \
64  if (neg) { \
65  bn = bignum_sub(mask_bn, bn); \
66  } \
67  tmp = bignum_to_uint64(bn); \
68  value = (size_type) tmp; \
69  } \
70  else{ \
71  PyErr_SetString(PyExc_TypeError, "Arg must be int"); \
72  return -1; \
73  }
74 
75 #define PyGetStr(dest, name) \
76  if (!PyUnicode_Check((name))) \
77  RAISE(PyExc_TypeError,"Page name must be bytes"); \
78  (dest) = PyUnicode_AsUTF8((name))
79 
80 
81 
82 #else
83 #define PyGetInt_uint_t(size_type, item, value) \
84  if (PyInt_Check(item)) { \
85  long tmp; \
86  tmp = PyInt_AsLong(item); \
87  \
88  if (Py_SIZE(item) < 0) { \
89  if (-tmp > ((size_type) -1)) { \
90  RAISE(PyExc_TypeError, "Arg too big for " #size_type ""); \
91  } \
92  } \
93  else if (tmp > (size_type) -1) { \
94  RAISE(PyExc_TypeError, "Arg too big for " #size_type ""); \
95  } \
96  value = (size_type) tmp; \
97  } \
98  else if (PyLong_Check(item)){ \
99  Py_INCREF(item); \
100  PyObject* py_long = item; \
101  PyObject* py_long_new; \
102  bn_t bn; \
103  uint64_t tmp; \
104  int neg = 0; \
105  \
106  if (Py_SIZE(py_long) < 0) { \
107  neg = 1; \
108  py_long_new = PyObject_CallMethod(py_long, "__neg__", NULL); \
109  Py_DECREF(py_long); \
110  py_long = py_long_new; \
111  } \
112  \
113  bn = PyLong_to_bn(py_long); \
114  \
115  bn_t mask_bn = bignum_lshift(bignum_from_int(1), sizeof(size_type)*8); \
116  if (bignum_is_inf_equal_unsigned(mask_bn, bn)) { \
117  RAISE(PyExc_TypeError, "Arg too big for " #size_type ""); \
118  } \
119  if (neg) { \
120  bn = bignum_sub(mask_bn, bn); \
121  } \
122  tmp = bignum_to_uint64(bn); \
123  value = (size_type) tmp; \
124  } \
125  else{ \
126  RAISE(PyExc_TypeError, "Arg must be int"); \
127  }
128 
129 
130 #define PyGetInt_uint_t_retneg(size_type, item, value) \
131  if (PyInt_Check(item)) { \
132  long tmp; \
133  tmp = PyInt_AsLong(item); \
134  \
135  if (Py_SIZE(item) < 0) { \
136  if (-tmp > ((size_type) -1)) { \
137  PyErr_SetString(PyExc_TypeError, "Arg too big for " #size_type ""); \
138  return -1; \
139  } \
140  } \
141  else if (tmp > (size_type) -1) { \
142  PyErr_SetString(PyExc_TypeError, "Arg too big for " #size_type ""); \
143  return -1; \
144  } \
145  value = (size_type) tmp; \
146  } \
147  else if (PyLong_Check(item)){ \
148  Py_INCREF(item); \
149  PyObject* py_long = item; \
150  PyObject* py_long_new; \
151  bn_t bn; \
152  uint64_t tmp; \
153  int neg = 0; \
154  \
155  if (Py_SIZE(py_long) < 0) { \
156  neg = 1; \
157  py_long_new = PyObject_CallMethod(py_long, "__neg__", NULL); \
158  Py_DECREF(py_long); \
159  py_long = py_long_new; \
160  } \
161  \
162  bn = PyLong_to_bn(py_long); \
163  \
164  bn_t mask_bn = bignum_lshift(bignum_from_int(1), sizeof(size_type)*8); \
165  if (bignum_is_inf_equal_unsigned(mask_bn, bn)) { \
166  PyErr_SetString(PyExc_TypeError, "Arg too big for " #size_type ""); \
167  return -1; \
168  } \
169  if (neg) { \
170  bn = bignum_sub(mask_bn, bn); \
171  } \
172  tmp = bignum_to_uint64(bn); \
173  value = (size_type) tmp; \
174  } \
175  else{ \
176  PyErr_SetString(PyExc_TypeError, "Arg must be int"); \
177  return -1; \
178  } \
179 
180 
181 #define PyGetStr(dest, name) \
182  if (!PyString_Check((name))) \
183  RAISE(PyExc_TypeError,"Page name must be bytes"); \
184  (dest) = PyString_AsString((name))
185 
186 #endif
187 
188 
189 
190 #define PyGetInt_size_t(item, value) PyGetInt_uint_t(size_t, item, value)
191 
192 #define PyGetInt_uint8_t(item, value) PyGetInt_uint_t(uint8_t, item, value)
193 #define PyGetInt_uint16_t(item, value) PyGetInt_uint_t(uint16_t, item, value)
194 #define PyGetInt_uint32_t(item, value) PyGetInt_uint_t(uint32_t, item, value)
195 #define PyGetInt_uint64_t(item, value) PyGetInt_uint_t(uint64_t, item, value)
196 
197 #define PyGetInt_uint8_t_retneg(item, value) PyGetInt_uint_t_retneg(uint8_t, item, value)
198 #define PyGetInt_uint16_t_retneg(item, value) PyGetInt_uint_t_retneg(uint16_t, item, value)
199 #define PyGetInt_uint32_t_retneg(item, value) PyGetInt_uint_t_retneg(uint32_t, item, value)
200 #define PyGetInt_uint64_t_retneg(item, value) PyGetInt_uint_t_retneg(uint64_t, item, value)
201 
202 
203 
204 #if PY_MAJOR_VERSION >= 3
205 
206 #define MOD_INIT(name) PyMODINIT_FUNC PyInit_##name(void)
207 
208 #define MOD_DEF(ob, name, doc, methods) \
209  static struct PyModuleDef moduledef = { \
210  PyModuleDef_HEAD_INIT, name, doc, -1, methods, }; \
211  ob = PyModule_Create(&moduledef);
212 #define RET_MODULE return module
213 
214 #else
215 
216 #define MOD_INIT(name) PyMODINIT_FUNC init##name(void)
217 
218 #define MOD_DEF(ob, name, doc, methods) \
219  ob = Py_InitModule3(name, methods, doc);
220 
221 #define RET_MODULE return
222 #endif
223 
224 
225 
226 
227 
228 #endif
miasm.jitter.codegen.CGen.traverse_expr_dst
def traverse_expr_dst(self, expr, dst2index)
Definition: codegen.py:325
bn.h
miasm.jitter.codegen.CGen.dst_to_c
def dst_to_c(self, src)
Definition: codegen.py:124
miasm.jitter.codegen.Attributes.__init__
def __init__(self, log_mn=False, log_regs=False)
Definition: codegen.py:33
miasm.jitter.codegen.CGen.id_to_c
def id_to_c(self, expr)
Definition: codegen.py:134
miasm.jitter.codegen.CGen.gen_finalize
def gen_finalize(self, block)
Definition: codegen.py:615
miasm.jitter.codegen.CGen.gen_c_assignments
def gen_c_assignments(self, assignblk)
Definition: codegen.py:229
miasm.expression.expression
Definition: expression.py:1
miasm.jitter.codegen.CGen.translator
translator
Definition: codegen.py:113
miasm.jitter.codegen.CGen.CODE_CPU_EXCEPTION_POST_INSTR
string CODE_CPU_EXCEPTION_POST_INSTR
Definition: codegen.py:79
miasm.jitter.codegen.CGen.gen_dst_goto
def gen_dst_goto(self, attrib, instr_offsets, dst2index)
Definition: codegen.py:442
miasm.jitter.codegen.CGen.gen_goto_code
def gen_goto_code(self, attrib, instr_offsets, dst)
Definition: codegen.py:409
miasm.jitter.codegen.CGen.gen_post_instr_checks
def gen_post_instr_checks(self, attrib)
Definition: codegen.py:370
miasm.jitter.codegen.CGen.gen_check_cpu_exception
def gen_check_cpu_exception(self, address)
Definition: codegen.py:319
miasm.expression.expression.ExprMem
Definition: expression.py:1103
miasm.jitter.codegen.Attributes.log_regs
log_regs
Definition: codegen.py:38
miasm.jitter.codegen.CGen.id_to_c_id
id_to_c_id
Definition: codegen.py:118
miasm.expression.expression.ExprId
Definition: expression.py:816
miasm.core.asmblock
Definition: asmblock.py:1
miasm.jitter.codegen.CGen.get_attributes
def get_attributes(self, instr, irblocks, log_mn=False, log_regs=False)
Definition: codegen.py:536
miasm.jitter.codegen.Attributes
Definition: codegen.py:27
miasm.jitter.codegen.CGen.gen_assignblk_dst
def gen_assignblk_dst(self, dst)
Definition: codegen.py:359
miasm.jitter.codegen.CGen.CODE_VM_EXCEPTION_POST_INSTR
string CODE_VM_EXCEPTION_POST_INSTR
Definition: codegen.py:87
miasm.jitter.codegen.CGen.CODE_RETURN_NO_EXCEPTION
string CODE_RETURN_NO_EXCEPTION
Definition: codegen.py:72
miasm.jitter.codegen.CGen.patch_c_id
def patch_c_id(self, expr)
Definition: codegen.py:130
miasm.jitter.codegen.CGen.add_local_var
def add_local_var(self, dst_var, dst_index, expr)
Definition: codegen.py:188
miasm.jitter.codegen.CGen.gen_post_code
def gen_post_code(self, attrib, pc_value)
Definition: codegen.py:399
miasm.jitter.codegen.CGen.PC
PC
Definition: codegen.py:112
miasm.jitter.codegen.CGen.gen_c_code
def gen_c_code(self, attrib, c_dst, c_assignmnts)
Definition: codegen.py:472
miasm.jitter.codegen.CGen.C_PC
C_PC
Definition: codegen.py:122
miasm.jitter.codegen.CGen.gen_irblock
def gen_irblock(self, instr_attrib, attributes, instr_offsets, irblock)
Definition: codegen.py:590
miasm.jitter.codegen.CGen.CODE_EXCEPTION_MEM_AT_INSTR
string CODE_EXCEPTION_MEM_AT_INSTR
Definition: codegen.py:51
miasm.jitter.codegen.CGen.gen_pre_code
def gen_pre_code(self, instr_attrib)
Definition: codegen.py:384
miasm.jitter.codegen.CGen.__init__
def __init__(self, ir_arch)
Definition: codegen.py:110
miasm.jitter.codegen.Attributes.mem_read
mem_read
Definition: codegen.py:34
miasm.ir.ir.AssignBlock
Definition: ir.py:67
miasm.expression.expression.is_expr
def is_expr(expr)
Definition: expression.py:141
miasm.expression.simplifications
Definition: simplifications.py:1
miasm.expression.expression.ExprInt
Definition: expression.py:742
miasm.jitter.codegen.Attributes.set_exception
set_exception
Definition: codegen.py:36
miasm.jitter.codegen.CGen.gen_bad_block
def gen_bad_block(self)
Definition: codegen.py:563
miasm.jitter.codegen.CGen.init_arch_C
def init_arch_C(self)
Definition: codegen.py:116
miasm.jitter.codegen.Attributes.log_mn
log_mn
Definition: codegen.py:37
miasm.jitter.codegen.CGen.block2assignblks
def block2assignblks(self, block)
Definition: codegen.py:161
miasm.jitter.codegen.CGen.CODE_BAD_BLOCK
string CODE_BAD_BLOCK
Definition: codegen.py:105
miasm.jitter.codegen.CGen.ir_arch
ir_arch
Definition: codegen.py:111
miasm.jitter.codegen.CGen.get_mem_prefetch
def get_mem_prefetch(self, assignblk)
Definition: codegen.py:206
miasm.ir.translators.C.TranslatorC
Definition: C.py:50
miasm.jitter.codegen.CGen
Definition: codegen.py:42
miasm.jitter.codegen.CGen.gen_check_memory_exception
def gen_check_memory_exception(self, address)
Definition: codegen.py:313
miasm.jitter.codegen.CGen.CODE_EXCEPTION_AT_INSTR
string CODE_EXCEPTION_AT_INSTR
Definition: codegen.py:60
miasm.ir.ir.IRBlock
Definition: ir.py:313
miasm.ir.ir
Definition: ir.py:1
miasm.jitter.codegen.CGen.assignblk_to_irbloc
def assignblk_to_irbloc(self, instr, assignblk)
Definition: codegen.py:146
miasm.jitter.codegen.CGen.get_caracteristics
def get_caracteristics(self, assignblk, attrib)
Definition: codegen.py:518
miasm.jitter.codegen.CGen.gen_init
def gen_init(self, block)
Definition: codegen.py:577
miasm.jitter.codegen.CGen.add_label_index
def add_label_index(self, dst2index, loc_key)
Definition: codegen.py:138
miasm.jitter.codegen.Attributes.mem_write
mem_write
Definition: codegen.py:35
miasm.jitter.codegen.CGen.CODE_INIT
string CODE_INIT
Definition: codegen.py:97
miasm.expression.expression.ExprLoc
Definition: expression.py:879
miasm.ir.translators.C
Definition: C.py:1
miasm.jitter.codegen.Attributes.instr
instr
Definition: codegen.py:39
miasm.jitter.codegen.CGen.gen_c
def gen_c(self, block, log_mn=False, log_regs=False)
Definition: codegen.py:626
miasm.jitter.codegen.CGen.get_block_post_label
def get_block_post_label(self, block)
Definition: codegen.py:569