diff options
| author | William Bruneau <william.bruneau@epfedu.fr> | 2019-05-15 11:29:37 +0200 |
|---|---|---|
| committer | William Bruneau <william.bruneau@epfedu.fr> | 2019-05-15 12:32:32 +0200 |
| commit | df7fc844a15de590331fbc13408ff89506b9efa2 (patch) | |
| tree | 78ff1287dd22422d3d1082d1c4a029a0b7e4f636 | |
| parent | bba990f43b73cbae980c70f6dc3a82511fcac3be (diff) | |
| download | miasm-df7fc844a15de590331fbc13408ff89506b9efa2.tar.gz miasm-df7fc844a15de590331fbc13408ff89506b9efa2.zip | |
bn_to_PyLong
| -rw-r--r-- | miasm/jitter/JitCore.h | 54 | ||||
| -rw-r--r-- | miasm/jitter/vm_mngr_py.c | 27 | ||||
| -rw-r--r-- | miasm/jitter/vm_mngr_py.h | 1 |
3 files changed, 31 insertions, 51 deletions
diff --git a/miasm/jitter/JitCore.h b/miasm/jitter/JitCore.h index ecd1efb1..798b360e 100644 --- a/miasm/jitter/JitCore.h +++ b/miasm/jitter/JitCore.h @@ -25,26 +25,10 @@ static PyObject *JitCpu_get_ ## regname (JitCpu *self, void *closure) \ { \ bn_t bn; \ - int j; \ PyObject* py_long; \ - PyObject* py_long_new; \ - PyObject* py_tmp; \ - PyObject* cst_32; \ - uint64_t tmp; \ - py_long = PyLong_FromLong(0); \ - cst_32 = PyLong_FromLong(32); \ bn = (self->cpu)->regname; \ bn = bignum_mask(bn, (size)); \ - for (j = BN_BYTE_SIZE - 4; j >= 0 ; j -= 4) { \ - tmp = bignum_to_uint64(bignum_mask(bignum_rshift(bn, 8 * j), 32)); \ - py_tmp = PyLong_FromUnsignedLong((unsigned long)tmp); \ - py_long_new = PyObject_CallMethod(py_long, "__lshift__", "O", cst_32); \ - Py_DECREF(py_long); \ - py_long = PyObject_CallMethod(py_long_new, "__add__", "O", py_tmp); \ - Py_DECREF(py_long_new); \ - Py_DECREF(py_tmp); \ - } \ - Py_DECREF(cst_32); \ + py_long = bn_to_PyLong(bn); \ return py_long; \ } \ \ @@ -70,26 +54,10 @@ static PyObject *JitCpu_get_ ## regname (JitCpu *self, void *closure) \ { \ bn_t bn; \ - int j; \ PyObject* py_long; \ - PyObject* py_long_new; \ - PyObject* py_tmp; \ - PyObject* cst_32; \ - uint64_t tmp; \ - py_long = PyLong_FromLong(0); \ - cst_32 = PyLong_FromLong(32); \ bn = (self->cpu)->regname; \ bn = bignum_mask(bn, (size)); \ - for (j = BN_BYTE_SIZE - 4; j >= 0 ; j -= 4) { \ - tmp = bignum_to_uint64(bignum_mask(bignum_rshift(bn, 8 * j), 32)); \ - py_tmp = PyLong_FromUnsignedLong((unsigned long)tmp); \ - py_long_new = PyObject_CallMethod(py_long, "__lshift__", "O", cst_32); \ - Py_DECREF(py_long); \ - py_long = PyObject_CallMethod(py_long_new, "__add__", "O", py_tmp); \ - Py_DECREF(py_long_new); \ - Py_DECREF(py_tmp); \ - } \ - Py_DECREF(cst_32); \ + py_long = bn_to_PyLong(bn); \ return py_long; \ } \ \ @@ -190,28 +158,12 @@ #define get_reg_bn(reg, size) do { \ bn_t bn; \ - int j; \ PyObject* py_long; \ - PyObject* py_long_new; \ - PyObject* py_tmp; \ - PyObject* cst_32; \ - uint64_t tmp; \ - py_long = PyLong_FromLong(0); \ - cst_32 = PyLong_FromLong(32); \ bn = self->cpu->reg; \ bn = bignum_mask(bn, size); \ - for (j = BN_BYTE_SIZE - 4; j >= 0 ; j -= 4) { \ - tmp = bignum_to_uint64(bignum_mask(bignum_rshift(bn, 8 * j), 32)); \ - py_tmp = PyLong_FromUnsignedLong((unsigned long)tmp); \ - py_long_new = PyObject_CallMethod(py_long, "__lshift__", "O", cst_32); \ - Py_DECREF(py_long); \ - py_long = PyObject_CallMethod(py_long_new, "__add__", "O", py_tmp); \ - Py_DECREF(py_long_new); \ - Py_DECREF(py_tmp); \ - } \ + py_long = bn_to_PyLong(bn); \ PyDict_SetItemString(dict, #reg, py_long); \ Py_DECREF(py_long); \ - Py_DECREF(cst_32); \ } while(0); diff --git a/miasm/jitter/vm_mngr_py.c b/miasm/jitter/vm_mngr_py.c index 4684f8f8..69e62fef 100644 --- a/miasm/jitter/vm_mngr_py.c +++ b/miasm/jitter/vm_mngr_py.c @@ -1041,3 +1041,30 @@ bn_t PyLong_to_bn(PyObject* py_long) return bn; } + +PyObject* bn_to_PyLong(bn_t bn) +{ + int j; + PyObject* py_long; + PyObject* py_long_new; + PyObject* py_tmp; + PyObject* cst_32; + uint64_t tmp; + + py_long = PyLong_FromLong(0); + cst_32 = PyLong_FromLong(32); + + for (j = BN_BYTE_SIZE - 4; j >= 0 ; j -= 4) { + tmp = bignum_to_uint64(bignum_mask(bignum_rshift(bn, 8 * j), 32)); + py_tmp = PyLong_FromUnsignedLong((unsigned long)tmp); + py_long_new = PyObject_CallMethod(py_long, "__lshift__", "O", cst_32); + Py_DECREF(py_long); + py_long = PyObject_CallMethod(py_long_new, "__add__", "O", py_tmp); + Py_DECREF(py_long_new); + Py_DECREF(py_tmp); + } + + Py_DECREF(cst_32); + + return py_long; +} diff --git a/miasm/jitter/vm_mngr_py.h b/miasm/jitter/vm_mngr_py.h index 4e10803b..a8f7dcd0 100644 --- a/miasm/jitter/vm_mngr_py.h +++ b/miasm/jitter/vm_mngr_py.h @@ -14,3 +14,4 @@ typedef struct { #endif// VM_MNGR_PY_H bn_t PyLong_to_bn(PyObject* py_long); +PyObject* bn_to_PyLong(bn_t bn); |