From cf86945db577ddbab2bc084d6a1a338380f46a5c Mon Sep 17 00:00:00 2001 From: angie Date: Fri, 10 Jun 2022 12:08:06 -0400 Subject: [PATCH] Expose Utils module --- rabbitizer/Utils.pyi | 11 ++++++ rabbitizer/__init__.pyi | 2 + rabbitizer/enums/rabbitizer_enum_Abi.c | 2 +- rabbitizer/rabbitizer_module.c | 1 + rabbitizer/rabbitizer_module.h | 2 + rabbitizer/rabbitizer_submodule_Utils.c | 49 +++++++++++++++++++++++++ setup.py | 2 +- 7 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 rabbitizer/Utils.pyi create mode 100644 rabbitizer/rabbitizer_submodule_Utils.c diff --git a/rabbitizer/Utils.pyi b/rabbitizer/Utils.pyi new file mode 100644 index 0000000..45b29f3 --- /dev/null +++ b/rabbitizer/Utils.pyi @@ -0,0 +1,11 @@ +#!/usr/bin/env python3 + +# SPDX-FileCopyrightText: © 2022 Decompollaborate +# SPDX-License-Identifier: MIT + +from __future__ import annotations + + +class Utils: + @staticmethod + def from2Complement(number: int, bits: int) -> int: ... diff --git a/rabbitizer/__init__.pyi b/rabbitizer/__init__.pyi index f1f7723..d00f5d7 100644 --- a/rabbitizer/__init__.pyi +++ b/rabbitizer/__init__.pyi @@ -5,6 +5,8 @@ from __future__ import annotations +from .Utils import * + from .Enum import * from .InstrCategory import * from .InstrId import * diff --git a/rabbitizer/enums/rabbitizer_enum_Abi.c b/rabbitizer/enums/rabbitizer_enum_Abi.c index 5cb027a..0c1f30b 100644 --- a/rabbitizer/enums/rabbitizer_enum_Abi.c +++ b/rabbitizer/enums/rabbitizer_enum_Abi.c @@ -19,7 +19,7 @@ RabbitizerEnumMetadata rabbitizer_enum_Abi_enumvalues[] = { static PyObject *rabbitizer_enum_Abi_fromStr(UNUSED PyObject *self, PyObject *args, PyObject *kwds) { - static char *kwlist[] = { "name", NULL} ; + static char *kwlist[] = { "name", NULL }; const char *name = NULL; RabbitizerAbi abi; PyObject *ret; diff --git a/rabbitizer/rabbitizer_module.c b/rabbitizer/rabbitizer_module.c index 9ada447..e29e7c0 100644 --- a/rabbitizer/rabbitizer_module.c +++ b/rabbitizer/rabbitizer_module.c @@ -33,6 +33,7 @@ typedef struct ModuleAttribute { #define MODULE_ATTRIBUTE_GLOBAL(name) { {.global = &rabbitizer_global_##name##_TypeObject}, MODULE_ATTRIBUTE_CAT_GLOBAL, #name, false, NULL } static ModuleAttributes rabbitizer_module_attributes[] = { + MODULE_ATTRIBUTE_INIT(Utils), MODULE_ATTRIBUTE_GLOBAL(config), MODULE_ATTRIBUTE_TYPE(Enum), MODULE_ATTRIBUTE_ENUM(Abi), diff --git a/rabbitizer/rabbitizer_module.h b/rabbitizer/rabbitizer_module.h index 04f0261..0777fd5 100644 --- a/rabbitizer/rabbitizer_module.h +++ b/rabbitizer/rabbitizer_module.h @@ -19,6 +19,8 @@ extern RabbitizerEnumMetadata rabbitizer_enum_InstrId_enumvalues[]; int rabbitizer_enum_Abi_Check(PyObject *o); +PyObject *rabbitizer_submodule_Utils_Init(void); + extern PyTypeObject rabbitizer_global_config_TypeObject; extern PyTypeObject rabbitizer_type_Enum_TypeObject; diff --git a/rabbitizer/rabbitizer_submodule_Utils.c b/rabbitizer/rabbitizer_submodule_Utils.c new file mode 100644 index 0000000..c9451ce --- /dev/null +++ b/rabbitizer/rabbitizer_submodule_Utils.c @@ -0,0 +1,49 @@ +/* SPDX-FileCopyrightText: © 2022 Decompollaborate */ +/* SPDX-License-Identifier: MIT */ + +#include "rabbitizer_module.h" + +#include "common/Utils.h" + + +static PyObject *rabbitizer_submodule_Utils_from2Complement(UNUSED PyObject *self, PyObject *args, PyObject *kwds) { + static char *kwlist[] = { "number", "bits", NULL }; + uint32_t number = 0; + int bits = 0; + + if (!PyArg_ParseTupleAndKeywords(args, kwds, "Ii", kwlist, &number, &bits)) { + return NULL; + } + + return PyLong_FromLong(RabbitizerUtils_From2Complement(number, bits)); +} + + +#define METHOD_NO_ARGS(name, docs) { #name, (PyCFunction) rabbitizer_submodule_Utils_##name, METH_NOARGS, PyDoc_STR(docs) } +#define METHOD_ARGS(name, docs) { #name, (PyCFunction) rabbitizer_submodule_Utils_##name, METH_VARARGS | METH_KEYWORDS, PyDoc_STR(docs) } + +static PyMethodDef rabbitizer_submodule_Utils_methods[] = { + METHOD_ARGS(from2Complement, ""), + + { 0 }, +}; + + +static PyModuleDef rabbitizer_submodule_Utils_module = { + PyModuleDef_HEAD_INIT, + .m_name = "rabbitizer.Utils", + .m_doc = "", + .m_size = -1, + .m_methods = rabbitizer_submodule_Utils_methods, +}; + +PyObject *rabbitizer_submodule_Utils_Init(void) { + PyObject *submodule; + + submodule = PyModule_Create(&rabbitizer_submodule_Utils_module); + if (submodule == NULL) { + return NULL; + } + + return submodule; +} diff --git a/setup.py b/setup.py index 61cd8ad..ab9ed21 100644 --- a/setup.py +++ b/setup.py @@ -8,7 +8,7 @@ setup( ext_modules=[ Extension( "rabbitizer", - ["rabbitizer/rabbitizer_module.c", "rabbitizer/rabbitizer_type_Instruction.c", "rabbitizer/rabbitizer_global_config.c", + ["rabbitizer/rabbitizer_module.c", "rabbitizer/rabbitizer_submodule_Utils.c", "rabbitizer/rabbitizer_type_Instruction.c", "rabbitizer/rabbitizer_global_config.c", "rabbitizer/enums/rabbitizer_type_Enum.c", "rabbitizer/enums/enums_utils.c", "rabbitizer/enums/rabbitizer_enum_InstrCategory.c", "rabbitizer/enums/rabbitizer_enum_InstrId.c", "rabbitizer/enums/rabbitizer_enum_Abi.c", "src/instructions/RabbitizerInstruction/RabbitizerInstruction_Disassemble.c", "src/instructions/RabbitizerInstruction/RabbitizerInstruction_ProcessUniqueId.c", "src/instructions/RabbitizerInstruction/RabbitizerInstruction.c", "src/instructions/RabbitizerInstruction/RabbitizerInstruction_Examination.c", "src/instructions/RabbitizerInstructionRsp/RabbitizerInstructionRsp.c", "src/instructions/RabbitizerInstructionRsp/RabbitizerInstructionRsp_ProcessUniqueId.c",