diff --git a/llvm/_core.c b/llvm/_core.c index bf5e0f8..0ca080c 100644 --- a/llvm/_core.c +++ b/llvm/_core.c @@ -683,32 +683,103 @@ _wLLVMCreateExecutionEngine(PyObject *self, PyObject *args) return ret; } -_wrap_obj2none(LLVMDisposeExecutionEngine, LLVMExecutionEngineRef) - -static PyObject * -_wLLVMRunFunction(PyObject *self, PyObject *args) +/* the args should have been ptr, num */ +LLVMGenericValueRef LLVMRunFunction2(LLVMExecutionEngineRef EE, + LLVMValueRef F, LLVMGenericValueRef *Args, unsigned NumArgs) { - PyObject *obj1, *obj2, *obj3; - LLVMExecutionEngineRef ee; - LLVMValueRef fn; - - if (!PyArg_ParseTuple(args, "OOO", &obj1, &obj2, &obj3)) - return NULL; - - /* obj3 is a list of args to the function, ignored currently */ - - ee = (LLVMExecutionEngineRef) PyCObject_AsVoidPtr(obj1); - fn = (LLVMValueRef) PyCObject_AsVoidPtr(obj2); - - LLVMRunFunction(ee, fn, 0, NULL); - - /* fn return value ignore currently */ - - Py_RETURN_NONE; + return LLVMRunFunction(EE, F, NumArgs, Args); } +_wrap_obj2none(LLVMDisposeExecutionEngine, LLVMExecutionEngineRef) +_wrap_objobjlist2obj(LLVMRunFunction2, LLVMExecutionEngineRef, + LLVMValueRef, LLVMGenericValueRef, LLVMGenericValueRef) _wrap_obj2obj(LLVMGetExecutionEngineTargetData, LLVMExecutionEngineRef, LLVMTargetDataRef) +_wrap_obj2none(LLVMRunStaticConstructors, LLVMExecutionEngineRef) +_wrap_obj2none(LLVMRunStaticDestructors, LLVMExecutionEngineRef) +_wrap_objobj2none(LLVMFreeMachineCodeForFunction, LLVMExecutionEngineRef, + LLVMValueRef) +_wrap_objobj2none(LLVMAddModuleProvider, LLVMExecutionEngineRef, + LLVMModuleProviderRef) + + +/*===----------------------------------------------------------------------===*/ +/* Generic Value */ +/*===----------------------------------------------------------------------===*/ + +static PyObject * +_wLLVMCreateGenericValueOfInt(PyObject *self, PyObject *args) +{ + PyObject *obj1; + LLVMTypeRef ty; + unsigned long long n; + int is_signed; + LLVMGenericValueRef gv; + + if (!PyArg_ParseTuple(args, "OLi", &obj1, &n, &is_signed)) + return NULL; + + ty = (LLVMTypeRef) PyCObject_AsVoidPtr(obj1); + + gv = LLVMCreateGenericValueOfInt(ty, n, is_signed); + return ctor_LLVMGenericValueRef(gv); +} + +static PyObject * +_wLLVMCreateGenericValueOfFloat(PyObject *self, PyObject *args) +{ + PyObject *obj1; + LLVMTypeRef ty; + double d; + LLVMGenericValueRef gv; + + if (!PyArg_ParseTuple(args, "Od", &obj1, &d)) + return NULL; + + ty = (LLVMTypeRef) PyCObject_AsVoidPtr(obj1); + + gv = LLVMCreateGenericValueOfFloat(ty, d); + return ctor_LLVMGenericValueRef(gv); +} + +static PyObject * +_wLLVMGenericValueToInt(PyObject *self, PyObject *args) +{ + PyObject *obj1; + int is_signed; + LLVMGenericValueRef gv; + unsigned long long val; + + if (!PyArg_ParseTuple(args, "Oi", &obj1, &is_signed)) + return NULL; + + gv = (LLVMGenericValueRef) PyCObject_AsVoidPtr(obj1); + + val = LLVMGenericValueToInt(gv, is_signed); + return is_signed ? + PyLong_FromLongLong((long long)val) : + PyLong_FromUnsignedLongLong(val); +} + +static PyObject * +_wLLVMGenericValueToFloat(PyObject *self, PyObject *args) +{ + PyObject *obj1, *obj2; + LLVMTypeRef ty; + LLVMGenericValueRef gv; + double val; + + if (!PyArg_ParseTuple(args, "OO", &obj1, &obj2)) + return NULL; + + ty = (LLVMTypeRef) PyCObject_AsVoidPtr(obj1); + gv = (LLVMGenericValueRef) PyCObject_AsVoidPtr(obj2); + + val = LLVMGenericValueToFloat(ty, gv); + return PyFloat_FromDouble(val); +} + +_wrap_obj2none(LLVMDisposeGenericValue, LLVMGenericValueRef) /*===----------------------------------------------------------------------===*/ @@ -1060,8 +1131,19 @@ static PyMethodDef core_methods[] = { /* Execution Engine */ _method( LLVMCreateExecutionEngine ) _method( LLVMDisposeExecutionEngine ) - _method( LLVMRunFunction ) + _method( LLVMRunFunction2 ) _method( LLVMGetExecutionEngineTargetData ) + _method( LLVMRunStaticConstructors ) + _method( LLVMRunStaticDestructors ) + _method( LLVMFreeMachineCodeForFunction ) + _method( LLVMAddModuleProvider ) + + /* Generic Value */ + _method( LLVMCreateGenericValueOfInt ) + _method( LLVMCreateGenericValueOfFloat ) + _method( LLVMGenericValueToInt ) + _method( LLVMGenericValueToFloat ) + _method( LLVMDisposeGenericValue ) { NULL } }; diff --git a/llvm/_util.py b/llvm/_util.py index db6576e..faab301 100644 --- a/llvm/_util.py +++ b/llvm/_util.py @@ -11,20 +11,20 @@ import llvm # failures. #===----------------------------------------------------------------------=== -def _check_gen(obj, type): +def check_gen(obj, type): if not isinstance(obj, type): type_str = type.__name__ msg = "argument not an instance of llvm.core.%s" % type_str raise TypeError, msg -def check_is_type(obj): _check_gen(obj, core.Type) -def check_is_value(obj): _check_gen(obj, core.Value) -def check_is_pointer(obj): _check_gen(obj, core.Pointer) -def check_is_constant(obj): _check_gen(obj, core.Constant) -def check_is_function(obj): _check_gen(obj, core.Function) -def check_is_basic_block(obj): _check_gen(obj, core.BasicBlock) -def check_is_module(obj): _check_gen(obj, core.Module) -def check_is_module_provider(obj): _check_gen(obj, core.ModuleProvider) +def check_is_type(obj): check_gen(obj, core.Type) +def check_is_value(obj): check_gen(obj, core.Value) +def check_is_pointer(obj): check_gen(obj, core.Pointer) +def check_is_constant(obj): check_gen(obj, core.Constant) +def check_is_function(obj): check_gen(obj, core.Function) +def check_is_basic_block(obj): check_gen(obj, core.BasicBlock) +def check_is_module(obj): check_gen(obj, core.Module) +def check_is_module_provider(obj): check_gen(obj, core.ModuleProvider) def check_is_unowned(ownable): if ownable.owner: @@ -37,13 +37,13 @@ def check_is_unowned(ownable): # the way. #===----------------------------------------------------------------------=== -def _unpack_gen(objlist, check_fn): +def unpack_gen(objlist, check_fn): for obj in objlist: check_fn(obj) return [ obj.ptr for obj in objlist ] -def unpack_types(objlist): return _unpack_gen(objlist, check_is_type) -def unpack_values(objlist): return _unpack_gen(objlist, check_is_value) -def unpack_constants(objlist): return _unpack_gen(objlist, check_is_constant) +def unpack_types(objlist): return unpack_gen(objlist, check_is_type) +def unpack_values(objlist): return unpack_gen(objlist, check_is_value) +def unpack_constants(objlist): return unpack_gen(objlist, check_is_constant) #===----------------------------------------------------------------------=== diff --git a/llvm/ee.py b/llvm/ee.py index dc56077..3165173 100644 --- a/llvm/ee.py +++ b/llvm/ee.py @@ -25,6 +25,53 @@ class TargetData(llvm.Ownable): return _core.LLVMTargetDataAsString(self.ptr) +#===----------------------------------------------------------------------=== +# Generic value +#===----------------------------------------------------------------------=== + +class GenericValue(object): + + @staticmethod + def int(ty, intval): + check_is_type(ty) + ptr = _core.LLVMCreateGenericValueOfInt(ty.ptr, intval, 0) + return GenericValue(ptr) + + @staticmethod + def int_signed(ty, intval): + check_is_type(ty) + ptr = _core.LLVMCreateGenericValueOfInt(ty.ptr, intval, 1) + return GenericValue(ptr) + + @staticmethod + def real(ty, floatval): + check_is_type(ty) # only float or double + ptr = _core.LLVMCreateGenericValueOfFloat(ty.ptr, floatval) + return GenericValue(ptr) + + def __init__(self, ptr): + self.ptr = ptr + + def __del__(self): + _core.LLVMDisposeGenericValue(self.ptr) + + def as_int(self): + return _core.LLVMGenericValueToInt(self.ptr, 0) + + def as_int_signed(self): + return _core.LLVMGenericValueToInt(self.ptr, 1) + + def as_real(self, ty): + check_is_type(ty) # only float or double + return _core.LLVMGenericValueToFloat(ty.ptr, self.ptr) + + +# helper functions for generic value objects +def _check_is_generic_value(obj): check_gen(obj, GenericValue) +def _unpack_generic_values(objlist): + return unpack_gen(objlist, _check_is_generic_value) + + #===----------------------------------------------------------------------=== # Execution engine #===----------------------------------------------------------------------=== @@ -49,7 +96,23 @@ class ExecutionEngine(object): def run_function(self, fn, args): check_is_function(fn) - return _core.LLVMRunFunction(self.ptr, fn.ptr, args) + ptrs = _unpack_generic_values(args) + gvptr = _core.LLVMRunFunction2(self.ptr, fn.ptr, ptrs) + return GenericValue(gvptr) + + def run_static_ctors(self): + _core.LLVMRunStaticConstructors(self.ptr) + + def run_static_dtors(self): + _core.LLVMRunStaticDestructors(self.ptr) + + def free_machine_code_for(self, fn): + check_is_function(fn) + _core.LLVMFreeMachineCodeForFunction(self.ptr, fn.ptr) + + def add_module_provider(self, mp): + check_is_module_provider(mp) + _core.LLVMAddModuleProvider(self.ptr, mp.ptr) @property def target_data(self): diff --git a/llvm/wrap.c b/llvm/wrap.c index 062eef2..2279ac2 100644 --- a/llvm/wrap.c +++ b/llvm/wrap.c @@ -1,127 +1,86 @@ -#include "wrap.h" - - -/*===----------------------------------------------------------------------===*/ -/* Type ctor/dtor */ -/*===----------------------------------------------------------------------===*/ - -PyObject *ctor_LLVMModuleRef(LLVMModuleRef p) -{ - if (p) - return PyCObject_FromVoidPtr(p, NULL); - Py_RETURN_NONE; /* avoiding the `else' since we don't (want to) know what - the macro Py_RETURN_NONE expands to */ -} - -PyObject *ctor_LLVMTypeRef(LLVMTypeRef p) -{ - if (p) - return PyCObject_FromVoidPtr(p, NULL); - Py_RETURN_NONE; -} - -PyObject *ctor_LLVMValueRef(LLVMValueRef p) -{ - if (p) - return PyCObject_FromVoidPtr(p, NULL); - Py_RETURN_NONE; -} - -PyObject *ctor_LLVMTypeHandleRef(LLVMTypeHandleRef p) -{ - if (p) - return PyCObject_FromVoidPtr(p, NULL); - Py_RETURN_NONE; -} - -PyObject *ctor_LLVMBasicBlockRef(LLVMBasicBlockRef p) -{ - if (p) - return PyCObject_FromVoidPtr(p, NULL); - Py_RETURN_NONE; -} - -PyObject *ctor_LLVMBuilderRef(LLVMBuilderRef p) -{ - if (p) - return PyCObject_FromVoidPtr(p, NULL); - Py_RETURN_NONE; -} - -PyObject *ctor_LLVMModuleProviderRef(LLVMModuleProviderRef p) -{ - if (p) - return PyCObject_FromVoidPtr(p, NULL); - Py_RETURN_NONE; -} - -PyObject *ctor_LLVMMemoryBufferRef(LLVMMemoryBufferRef p) -{ - if (p) - return PyCObject_FromVoidPtr(p, NULL); - Py_RETURN_NONE; -} - -PyObject *ctor_LLVMPassManagerRef(LLVMPassManagerRef p) -{ - if (p) - return PyCObject_FromVoidPtr(p, NULL); - Py_RETURN_NONE; -} - -PyObject *ctor_int(int i) -{ - return PyInt_FromLong(i); -} - -_define_std_ctor(LLVMExecutionEngineRef) -_define_std_ctor(LLVMTargetDataRef) - - -/*===----------------------------------------------------------------------===*/ -/* Helper functions */ -/*===----------------------------------------------------------------------===*/ - -void *get_object_arg(PyObject *args) -{ - PyObject *o; - - if (!PyArg_ParseTuple(args, "O", &o)) - return NULL; - - return PyCObject_AsVoidPtr(o); -} - -void **make_array_from_list(PyObject *list, int n) -{ - int i; - void **arr; - - arr = (void **)malloc(sizeof(void *) * n); - if (!arr) - return NULL; - - for (i=0; i -#include - -/* llvm includes */ -#include "llvm-c/Core.h" -#include "llvm-c/Analysis.h" -#include "llvm-c/ExecutionEngine.h" -#include "llvm-c/Target.h" - - -/*===----------------------------------------------------------------------===*/ -/* Type ctor/dtor */ -/*===----------------------------------------------------------------------===*/ - -/* These are functions that can construct a PyCObject (a Python object that - * holds an opaque pointer) from any time. The naming convention is significant, - * this is assumed by the wrapper macros below. These are the equivalent of - * what would in C++ have been specializations of a template function ctor - * for various types. - */ - -/* LLVMModuleRef */ -PyObject *ctor_LLVMModuleRef(LLVMModuleRef p); - -/* LLVMTypeRef */ -PyObject *ctor_LLVMTypeRef(LLVMTypeRef p); - -/* LLVMValueRef */ -PyObject *ctor_LLVMValueRef(LLVMValueRef p); - -/* LLVMTypeHandleRef */ -PyObject *ctor_LLVMTypeHandleRef(LLVMTypeHandleRef p); - -/* LLVMBasicBlockRef */ -PyObject *ctor_LLVMBasicBlockRef(LLVMBasicBlockRef p); - -/* LLVMBuilderRef */ -PyObject *ctor_LLVMBuilderRef(LLVMBuilderRef p); - -/* LLVMModuleProviderRef */ -PyObject *ctor_LLVMModuleProviderRef(LLVMModuleProviderRef p); - -/* LLVMMemoryBufferRef */ -PyObject *ctor_LLVMMemoryBufferRef(LLVMMemoryBufferRef p); - -/* LLVMPassManagerRef */ -PyObject *ctor_LLVMPassManagerRef(LLVMPassManagerRef p); - -/* standard types */ -PyObject *ctor_int(int i); - -#define _declare_std_ctor(typ) \ -PyObject * ctor_ ## typ ( typ p); - -#define _define_std_ctor(typ) \ -PyObject * ctor_ ## typ ( typ p) \ -{ \ - if (p) \ - return PyCObject_FromVoidPtr(p, NULL); \ - Py_RETURN_NONE; \ -} - -_declare_std_ctor(LLVMExecutionEngineRef) -_declare_std_ctor(LLVMTargetDataRef) - - -/*===----------------------------------------------------------------------===*/ -/* Helper methods */ -/*===----------------------------------------------------------------------===*/ - -/** - * Accept a single object argument of type PyCObject and return the - * opaque pointer contained in it. - */ -void *get_object_arg(PyObject *args); - -/** - * Given a PyList object (list) having n (= PyList_Size(list)) elements, - * each list object being a PyCObject, return a malloc()-ed array of - * opaque pointers from the PyCObjects. The 'n' is passed explicitly - * since the caller will have to query it once anyway, and we can avoid - * a second call internally. - */ -void **make_array_from_list(PyObject *list, int n); - -/** - * Given an array of LLVMTypeRef's, create a PyList object. Note that - * currently such an action is required only for LLVMTypeRef's, when - * it is required another type, this method has to be generalized. - */ -PyObject *make_list_from_LLVMTypeRef_array(LLVMTypeRef *p, unsigned n); - - -/*===----------------------------------------------------------------------===*/ -/* Wrapper macros */ -/*===----------------------------------------------------------------------===*/ - -/* The following wrapper macros define functions that wrap over LLVM-C APIs - * of various signatures. Though they look hairy, they all follow some - * conventions. - */ - -/** - * Wrap LLVM functions of the type - * outtype func(intype1 arg1) - */ -#define _wrap_obj2obj(func, intype1, outtype) \ -static PyObject * \ -_w ## func (PyObject *self, PyObject *args) \ -{ \ - intype1 arg1; \ - \ - if (!(arg1 = ( intype1 )get_object_arg(args))) \ - return NULL; \ - \ - return ctor_ ## outtype ( func (arg1)); \ -} - -/** - * Wrap LLVM functions of the type - * outtype func(int arg1) - */ -#define _wrap_int2obj(func, outtype) \ -static PyObject * \ -_w ## func (PyObject *self, PyObject *args) \ -{ \ - int arg1; \ - \ - if (!PyArg_ParseTuple(args, "i", &arg1)) \ - return NULL; \ - \ - return ctor_ ## outtype ( func (arg1)); \ -} - -/** - * Wrap LLVM functions of the type - * outtype func(intype1 arg1, intype2 arg2) - */ -#define _wrap_objobj2obj(func, intype1, intype2, outtype) \ -static PyObject * \ -_w ## func (PyObject *self, PyObject *args) \ -{ \ - PyObject *obj1, *obj2; \ - intype1 arg1; \ - intype2 arg2; \ - \ - if (!PyArg_ParseTuple(args, "OO", &obj1, &obj2)) \ - return NULL; \ - \ - arg1 = ( intype1 ) PyCObject_AsVoidPtr(obj1); \ - arg2 = ( intype2 ) PyCObject_AsVoidPtr(obj2); \ - \ - return ctor_ ## outtype ( func (arg1, arg2)); \ -} - -/** - * Wrap LLVM functions of the type - * void func(intype1 arg1, intype2 arg2) - */ -#define _wrap_objobj2none(func, intype1, intype2) \ -static PyObject * \ -_w ## func (PyObject *self, PyObject *args) \ -{ \ - PyObject *obj1, *obj2; \ - intype1 arg1; \ - intype2 arg2; \ - \ - if (!PyArg_ParseTuple(args, "OO", &obj1, &obj2)) \ - return NULL; \ - \ - arg1 = ( intype1 ) PyCObject_AsVoidPtr(obj1); \ - arg2 = ( intype2 ) PyCObject_AsVoidPtr(obj2); \ - \ - func (arg1, arg2); \ - Py_RETURN_NONE; \ -} - -/** - * Wrap LLVM functions of the type - * outtype func(intype1 arg1, arg2) - */ -#define _wrap_objint2obj(func, intype1, outtype) \ -static PyObject * \ -_w ## func (PyObject *self, PyObject *args) \ -{ \ - PyObject *obj1; \ - intype1 arg1; \ - unsigned int arg2; \ - \ - if (!PyArg_ParseTuple(args, "OI", &obj1, &arg2)) \ - return NULL; \ - \ - arg1 = ( intype1 ) PyCObject_AsVoidPtr(obj1); \ - \ - return ctor_ ## outtype ( func (arg1, arg2)); \ -} - -/** - * Wrap LLVM functions of the type - * outtype func(intype1 arg1, intype2 arg2, intype3 arg3) - */ -#define _wrap_objobjobj2obj(func, intype1, intype2, intype3, outtype) \ -static PyObject * \ -_w ## func (PyObject *self, PyObject *args) \ -{ \ - PyObject *obj1, *obj2, *obj3; \ - intype1 arg1; \ - intype2 arg2; \ - intype3 arg3; \ - \ - if (!PyArg_ParseTuple(args, "OOO", &obj1, &obj2, &obj3)) \ - return NULL; \ - \ - arg1 = ( intype1 ) PyCObject_AsVoidPtr(obj1); \ - arg2 = ( intype2 ) PyCObject_AsVoidPtr(obj2); \ - arg3 = ( intype3 ) PyCObject_AsVoidPtr(obj3); \ - \ - return ctor_ ## outtype ( func (arg1, arg2, arg3)); \ -} - -/** - * Wrap LLVM functions of the type - * void func(intype1 arg1, intype2 arg2, intype3 arg3) - */ -#define _wrap_objobjobj2none(func, intype1, intype2, intype3) \ -static PyObject * \ -_w ## func (PyObject *self, PyObject *args) \ -{ \ - PyObject *obj1, *obj2, *obj3; \ - intype1 arg1; \ - intype2 arg2; \ - intype3 arg3; \ - \ - if (!PyArg_ParseTuple(args, "OOO", &obj1, &obj2, &obj3)) \ - return NULL; \ - \ - arg1 = ( intype1 ) PyCObject_AsVoidPtr(obj1); \ - arg2 = ( intype2 ) PyCObject_AsVoidPtr(obj2); \ - arg3 = ( intype3 ) PyCObject_AsVoidPtr(obj3); \ - \ - func (arg1, arg2, arg3); \ - Py_RETURN_NONE; \ -} - -/** - * Wrap LLVM functions of the type - * outtype func( arg1, intype2 arg2, intype3 arg3) - */ -#define _wrap_intobjobj2obj(func, intype2, intype3, outtype) \ -static PyObject * \ -_w ## func (PyObject *self, PyObject *args) \ -{ \ - unsigned int arg1; \ - PyObject *obj2, *obj3; \ - intype2 arg2; \ - intype3 arg3; \ - \ - if (!PyArg_ParseTuple(args, "IOO", &arg1, &obj2, &obj3)) \ - return NULL; \ - \ - arg2 = ( intype2 ) PyCObject_AsVoidPtr(obj2); \ - arg3 = ( intype3 ) PyCObject_AsVoidPtr(obj3); \ - \ - return ctor_ ## outtype ( func (arg1, arg2, arg3)); \ -} - -/** - * Wrap LLVM functions of the type - * outtype func(const char *s) - */ -#define _wrap_str2obj(func, outtype) \ -static PyObject * \ -_w ## func (PyObject *self, PyObject *args) \ -{ \ - const char *arg1; \ - \ - if (!PyArg_ParseTuple(args, "s", &arg1)) \ - return NULL; \ - \ - return ctor_ ## outtype ( func (arg1)); \ -} - -/** - * Wrap LLVM functions of the type - * outtype func() - */ -#define _wrap_none2obj(func, outtype) \ -static PyObject * \ -_w ## func (PyObject *self, PyObject *args) \ -{ \ - if (!PyArg_ParseTuple(args, "")) \ - return NULL; \ - \ - return ctor_ ## outtype ( func ()); \ -} - -/** - * Wrap LLVM functions of the type - * const char *func(intype1 arg1) - */ -#define _wrap_obj2str(func, intype1) \ -static PyObject * \ -_w ## func (PyObject *self, PyObject *args) \ -{ \ - intype1 arg1; \ - \ - if (!(arg1 = ( intype1 )get_object_arg(args))) \ - return NULL; \ - \ - return PyString_FromString( func (arg1)); \ -} - -/** - * Wrap LLVM functions of the type - * void func(intype1 arg1) - */ -#define _wrap_obj2none(func, intype1) \ -static PyObject * \ -_w ## func (PyObject *self, PyObject *args) \ -{ \ - intype1 arg1; \ - \ - if (!(arg1 = ( intype1 )get_object_arg(args))) \ - return NULL; \ - \ - func (arg1); \ - Py_RETURN_NONE; \ -} - -/** - * Wrap LLVM functions of the type - * void func(intype1 arg1, const char *arg2) - */ -#define _wrap_objstr2none(func, intype1) \ -static PyObject * \ -_w ## func (PyObject *self, PyObject *args) \ -{ \ - PyObject *obj1; \ - const char *arg2; \ - intype1 arg1; \ - \ - if (!PyArg_ParseTuple(args, "Os", &obj1, &arg2)) \ - return NULL; \ - \ - arg1 = ( intype1 ) PyCObject_AsVoidPtr(obj1); \ - \ - func (arg1, arg2); \ - Py_RETURN_NONE; \ -} - -/** - * Wrap LLVM functions of the type - * outtype func(intype1 arg1, const char *arg2) - */ -#define _wrap_objstr2obj(func, intype1, outtype) \ -static PyObject * \ -_w ## func (PyObject *self, PyObject *args) \ -{ \ - PyObject *obj1; \ - const char *arg2; \ - intype1 arg1; \ - \ - if (!PyArg_ParseTuple(args, "Os", &obj1, &arg2)) \ - return NULL; \ - \ - arg1 = ( intype1 ) PyCObject_AsVoidPtr(obj1); \ - \ - return ctor_ ## outtype ( func (arg1, arg2)); \ -} - -/** - * Wrap LLVM functions of the type - * void func(intype1 arg1, arg2) - */ -#define _wrap_objint2none(func, intype1) \ -static PyObject * \ -_w ## func (PyObject *self, PyObject *args) \ -{ \ - PyObject *obj1; \ - int arg2; \ - intype1 arg1; \ - \ - if (!PyArg_ParseTuple(args, "Oi", &obj1, &arg2)) \ - return NULL; \ - \ - arg1 = ( intype1 ) PyCObject_AsVoidPtr(obj1); \ - \ - func (arg1, arg2); \ - Py_RETURN_NONE; \ -} - -/** - * Wrap LLVM functions of the type - * outtype func(intype1 arg1, intype2 arg2, const char *arg3) - */ -#define _wrap_objobjstr2obj(func, intype1, intype2, outtype) \ -static PyObject * \ -_w ## func (PyObject *self, PyObject *args) \ -{ \ - PyObject *obj1, *obj2; \ - intype1 arg1; \ - intype2 arg2; \ - const char *arg3; \ - \ - if (!PyArg_ParseTuple(args, "OOs", &obj1, &obj2, &arg3)) \ - return NULL; \ - \ - arg1 = ( intype1 ) PyCObject_AsVoidPtr(obj1); \ - arg2 = ( intype2 ) PyCObject_AsVoidPtr(obj2); \ - \ - return ctor_ ## outtype ( func (arg1, arg2, arg3)) ;\ -} - -/** - * Wrap LLVM functions of the type - * void func(intype1 arg1, arg2, arg3) - */ -#define _wrap_objintint2none(func, intype1) \ -static PyObject * \ -_w ## func (PyObject *self, PyObject *args) \ -{ \ - PyObject *obj1; \ - intype1 arg1; \ - int arg2, arg3; \ - \ - if (!PyArg_ParseTuple(args, "Oii", &obj1, &arg2, &arg3)) \ - return NULL; \ - \ - arg1 = ( intype1 ) PyCObject_AsVoidPtr(obj1); \ - \ - func (arg1, arg2, arg3); \ - Py_RETURN_NONE; \ -} - -/** - * Wrap LLVM functions of the type - * outtype func(intype1 arg1, const char *arg2, intype3 arg3) - */ -#define _wrap_objstrobj2obj(func, intype1, intype3, outtype) \ -static PyObject * \ -_w ## func (PyObject *self, PyObject *args) \ -{ \ - PyObject *obj1, *obj3; \ - intype1 arg1; \ - const char *arg2; \ - intype3 arg3; \ - \ - if (!PyArg_ParseTuple(args, "OsO", &obj1, &arg2, &obj3)) \ - return NULL; \ - \ - arg1 = ( intype1 ) PyCObject_AsVoidPtr(obj1); \ - arg3 = ( intype3 ) PyCObject_AsVoidPtr(obj3); \ - \ - return ctor_ ## outtype ( func (arg1, arg2, arg3)); \ -} - - -/** - * Wrap LLVM functions of the type - * outtype func(intype1 arg1, intype2 arg2, int arg3, const char *arg4) - */ -#define _wrap_objobjintstr2obj(func, intype1, intype2, outtype) \ -static PyObject * \ -_w ## func (PyObject *self, PyObject *args) \ -{ \ - PyObject *obj1, *obj2; \ - intype1 arg1; \ - intype2 arg2; \ - int arg3; \ - const char *arg4; \ - \ - if (!PyArg_ParseTuple(args, "OOis", &obj1, &obj2, &arg3, &arg4)) \ - return NULL; \ - \ - arg1 = ( intype1 ) PyCObject_AsVoidPtr(obj1); \ - arg2 = ( intype2 ) PyCObject_AsVoidPtr(obj2); \ - \ - return ctor_ ## outtype ( func (arg1, arg2, arg3, arg4)) ;\ -} - -/** - * Wrap LLVM functions of the type - * outtype func(intype1 arg1, intype2 arg2, intype3 arg3, const char *arg4) - */ -#define _wrap_objobjobjstr2obj(func, intype1, intype2, intype3, outtype) \ -static PyObject * \ -_w ## func (PyObject *self, PyObject *args) \ -{ \ - PyObject *obj1, *obj2, *obj3; \ - intype1 arg1; \ - intype2 arg2; \ - intype3 arg3; \ - const char *arg4; \ - \ - if (!PyArg_ParseTuple(args, "OOOs", &obj1, &obj2, &obj3, &arg4)) \ - return NULL; \ - \ - arg1 = ( intype1 ) PyCObject_AsVoidPtr(obj1); \ - arg2 = ( intype2 ) PyCObject_AsVoidPtr(obj2); \ - arg3 = ( intype3 ) PyCObject_AsVoidPtr(obj3); \ - \ - return ctor_ ## outtype ( func (arg1, arg2, arg3, arg4)); \ -} - -/** - * Wrap LLVM functions of the type - * outtype func(intype1 arg1, intype2 arg2, intype3 arg3, arg4) - */ -#define _wrap_objobjobjint2obj(func, intype1, intype2, intype3, outtype) \ -static PyObject * \ -_w ## func (PyObject *self, PyObject *args) \ -{ \ - PyObject *obj1, *obj2, *obj3; \ - intype1 arg1; \ - intype2 arg2; \ - intype3 arg3; \ - int arg4; \ - \ - if (!PyArg_ParseTuple(args, "OOOi", &obj1, &obj2, &obj3, &arg4)) \ - return NULL; \ - \ - arg1 = ( intype1 ) PyCObject_AsVoidPtr(obj1); \ - arg2 = ( intype2 ) PyCObject_AsVoidPtr(obj2); \ - arg3 = ( intype3 ) PyCObject_AsVoidPtr(obj3); \ - \ - return ctor_ ## outtype ( func (arg1, arg2, arg3, arg4)); \ -} - -/** - * Wrap LLVM functions of the type - * outtype func(intype1 arg1, intype2 arg2, intype3 arg3, intype arg4) - */ -#define _wrap_objobjobjobj2obj(func, intype1, intype2, intype3, intype4, outtype) \ -static PyObject * \ -_w ## func (PyObject *self, PyObject *args) \ -{ \ - PyObject *obj1, *obj2, *obj3, *obj4; \ - intype1 arg1; \ - intype2 arg2; \ - intype3 arg3; \ - intype4 arg4; \ - \ - if (!PyArg_ParseTuple(args, "OOOO", &obj1, &obj2, &obj3, &obj4)) \ - return NULL; \ - \ - arg1 = ( intype1 ) PyCObject_AsVoidPtr(obj1); \ - arg2 = ( intype2 ) PyCObject_AsVoidPtr(obj2); \ - arg3 = ( intype3 ) PyCObject_AsVoidPtr(obj3); \ - arg4 = ( intype4 ) PyCObject_AsVoidPtr(obj4); \ - \ - return ctor_ ## outtype ( func (arg1, arg2, arg3, arg4)); \ -} - -/** - * Wrap LLVM functions of the type - * outtype func(intype1 arg1, intype2 arg2, intype3 arg3, intype arg4, const char *arg5) - */ -#define _wrap_objobjobjobjstr2obj(func, intype1, intype2, intype3, intype4, outtype) \ -static PyObject * \ -_w ## func (PyObject *self, PyObject *args) \ -{ \ - PyObject *obj1, *obj2, *obj3, *obj4; \ - intype1 arg1; \ - intype2 arg2; \ - intype3 arg3; \ - intype4 arg4; \ - const char *arg5; \ - \ - if (!PyArg_ParseTuple(args, "OOOOs", &obj1, &obj2, &obj3, &obj4, &arg5)) \ - return NULL; \ - \ - arg1 = ( intype1 ) PyCObject_AsVoidPtr(obj1); \ - arg2 = ( intype2 ) PyCObject_AsVoidPtr(obj2); \ - arg3 = ( intype3 ) PyCObject_AsVoidPtr(obj3); \ - arg4 = ( intype4 ) PyCObject_AsVoidPtr(obj4); \ - \ - return ctor_ ## outtype ( func (arg1, arg2, arg3, arg4, arg5)); \ -} - -/** - * Wrap LLVM functions of the type - * outtype func(intype1 arg1, int arg2, intype3 arg3, intype4 arg4, const char *arg5) - */ -#define _wrap_objintobjobjstr2obj(func, intype1, intype3, intype4, outtype) \ -static PyObject * \ -_w ## func (PyObject *self, PyObject *args) \ -{ \ - PyObject *obj1, *obj3, *obj4; \ - intype1 arg1; \ - int arg2; \ - intype3 arg3; \ - intype4 arg4; \ - const char *arg5; \ - \ - if (!PyArg_ParseTuple(args, "OiOOs", &obj1, &arg2, &obj3, &obj4, &arg5)) \ - return NULL; \ - \ - arg1 = ( intype1 ) PyCObject_AsVoidPtr(obj1); \ - arg3 = ( intype3 ) PyCObject_AsVoidPtr(obj3); \ - arg4 = ( intype4 ) PyCObject_AsVoidPtr(obj4); \ - \ - return ctor_ ## outtype ( func (arg1, arg2, arg3, arg4, arg5)); \ -} - -/** - * Wrap LLVM functions of the type - * outtype func(intype1 arg1, intype2 *arg2v, unsigned arg2n) - * where arg2v is an array of intype2 elements, arg2n in length. - */ -#define _wrap_objlist2obj(func, intype1, intype2, outtype) \ -static PyObject * \ -_w ## func (PyObject *self, PyObject *args) \ -{ \ - PyObject *obj1, *obj2; \ - intype1 arg1; \ - intype2 *arg2v; \ - unsigned arg2n; \ - outtype ret; \ - \ - if (!PyArg_ParseTuple(args, "OO", &obj1, &obj2)) \ - return NULL; \ - \ - arg1 = ( intype1 ) PyCObject_AsVoidPtr(obj1); \ - arg2n = (unsigned) PyList_Size(obj2); \ - if (!(arg2v = ( intype2 *)make_array_from_list(obj2, arg2n))) \ - return PyErr_NoMemory(); \ - \ - ret = func (arg1, arg2v, arg2n); \ - free(arg2v); \ - return ctor_ ## outtype (ret); \ -} - -/** - * Wrap LLVM functions of the type - * outtype func(intype1 *arg1v, unsigned arg1n, int arg2) - * where arg1v is an array of intype1 elements, arg1n in length. - */ -#define _wrap_listint2obj(func, intype1, outtype) \ -static PyObject * \ -_w ## func (PyObject *self, PyObject *args) \ -{ \ - PyObject *obj1; \ - int arg2; \ - intype1 *arg1v; \ - unsigned arg1n; \ - outtype ret; \ - \ - if (!PyArg_ParseTuple(args, "Oi", &obj1, &arg2)) \ - return NULL; \ - \ - arg1n = (unsigned) PyList_Size(obj1); \ - if (!(arg1v = ( intype1 *)make_array_from_list(obj1, arg1n))) \ - return PyErr_NoMemory(); \ - \ - ret = func (arg1v, arg1n, arg2); \ - free(arg1v); \ - return ctor_ ## outtype (ret); \ -} - -/** - * Wrap LLVM functions of the type - * outtype func(intype1 *arg1v, unsigned arg1n) - * where arg1v is an array of intype1 elements, arg1n in length. - */ -#define _wrap_list2obj(func, intype1, outtype) \ -static PyObject * \ -_w ## func (PyObject *self, PyObject *args) \ -{ \ - PyObject *obj1; \ - intype1 *arg1v; \ - unsigned arg1n; \ - outtype ret; \ - \ - if (!PyArg_ParseTuple(args, "O", &obj1)) \ - return NULL; \ - \ - arg1n = (unsigned) PyList_Size(obj1); \ - if (!(arg1v = ( intype1 *)make_array_from_list(obj1, arg1n))) \ - return PyErr_NoMemory(); \ - \ - ret = func (arg1v, arg1n); \ - free(arg1v); \ - return ctor_ ## outtype (ret); \ -} - -/** - * Wrap LLVM functions of the type - * outtype func(intype1 arg1, intype2 *arg2v, unsigned arg2n, int arg3) - * where arg2v is an array of intype2 elements, arg2n in length. - */ -#define _wrap_objlistint2obj(func, intype1, intype2, outtype) \ -static PyObject * \ -_w ## func (PyObject *self, PyObject *args) \ -{ \ - PyObject *obj1, *obj2; \ - intype1 arg1; \ - intype2 *arg2v; \ - unsigned arg2n; \ - int arg3; \ - outtype ret; \ - \ - if (!PyArg_ParseTuple(args, "OOi", &obj1, &obj2, &arg3)) \ - return NULL; \ - \ - arg1 = ( intype1 ) PyCObject_AsVoidPtr(obj1); \ - arg2n = (unsigned) PyList_Size(obj2); \ - if (!(arg2v = ( intype2 *)make_array_from_list(obj2, arg2n))) \ - return PyErr_NoMemory(); \ - \ - ret = func (arg1, arg2v, arg2n, arg3); \ - free(arg2v); \ - return ctor_ ## outtype (ret); \ -} - -/** - * Wrap LLVM functions of the type - * outtype func(intype1 arg1, intype2 arg2, intype3 *arg3v, unsigned arg3n, const char *arg4) - * where arg3v is an array of intype3 elements, arg3n in length. - */ -#define _wrap_objobjliststr2obj(func, intype1, intype2, intype3, outtype) \ -static PyObject * \ -_w ## func (PyObject *self, PyObject *args) \ -{ \ - PyObject *obj1, *obj2, *obj3; \ - const char *arg4; \ - intype1 arg1; \ - intype2 arg2; \ - intype3 *arg3v; \ - unsigned arg3n; \ - outtype ret; \ - \ - if (!PyArg_ParseTuple(args, "OOOs", &obj1, &obj2, &obj3, &arg4))\ - return NULL; \ - \ - arg1 = ( intype1 ) PyCObject_AsVoidPtr(obj1); \ - arg2 = ( intype2 ) PyCObject_AsVoidPtr(obj2); \ - arg3n = ( unsigned ) PyList_Size(obj3); \ - if (!(arg3v = ( intype3 *)make_array_from_list(obj3, arg3n))) \ - return PyErr_NoMemory(); \ - \ - ret = func (arg1, arg2, arg3v, arg3n, arg4); \ - free(arg3v); \ - return ctor_ ## outtype (ret); \ -} - -/** - * Wrap LLVM dump-to-string functions of the type - * char *func(intype1) - * where the return value has to be disposed after use by calling - * LLVMDisposeMessage. - */ -#define _wrap_dumper(func, intype1) \ -static PyObject * \ -_w ## func (PyObject *self, PyObject *args) \ -{ \ - intype1 arg1; \ - char *val; \ - PyObject *ret; \ - \ - if (!(arg1= ( intype1 ) get_object_arg(args))) \ - return NULL; \ - \ - val = func (arg1); \ - ret = PyString_FromString(val); \ - LLVMDisposeMessage(val); \ - return ret; \ -} - -#endif /* LLVM_PY_WRAP_H */ - +/** + * Functions and macros to aid in wrapping. + */ + +#ifndef LLVM_PY_WRAP_H +#define LLVM_PY_WRAP_H + + +/* python includes */ +#include +#include + +/* llvm includes */ +#include "llvm-c/Core.h" +#include "llvm-c/Analysis.h" +#include "llvm-c/ExecutionEngine.h" +#include "llvm-c/Target.h" + + +/*===----------------------------------------------------------------------===*/ +/* Type ctor/dtor */ +/*===----------------------------------------------------------------------===*/ + +/* These are functions that can construct a PyCObject (a Python object that + * holds an opaque pointer) from any time. The naming convention is significant, + * this is assumed by the wrapper macros below. These are the equivalent of + * what would in C++ have been specializations of a template function ctor + * for various types. + */ + +#define _declare_std_ctor(typ) \ +PyObject * ctor_ ## typ ( typ p); + +_declare_std_ctor(LLVMModuleRef) +_declare_std_ctor(LLVMTypeRef) +_declare_std_ctor(LLVMValueRef) +_declare_std_ctor(LLVMTypeHandleRef) +_declare_std_ctor(LLVMBasicBlockRef) +_declare_std_ctor(LLVMBuilderRef) +_declare_std_ctor(LLVMModuleProviderRef) +_declare_std_ctor(LLVMMemoryBufferRef) +_declare_std_ctor(LLVMPassManagerRef) +_declare_std_ctor(LLVMExecutionEngineRef) +_declare_std_ctor(LLVMTargetDataRef) +_declare_std_ctor(LLVMGenericValueRef) + +/* standard types */ +PyObject *ctor_int(int i); + + +/*===----------------------------------------------------------------------===*/ +/* Helper methods */ +/*===----------------------------------------------------------------------===*/ + +/** + * Accept a single object argument of type PyCObject and return the + * opaque pointer contained in it. + */ +void *get_object_arg(PyObject *args); + +/** + * Given a PyList object (list) having n (= PyList_Size(list)) elements, + * each list object being a PyCObject, return a malloc()-ed array of + * opaque pointers from the PyCObjects. The 'n' is passed explicitly + * since the caller will have to query it once anyway, and we can avoid + * a second call internally. + */ +void **make_array_from_list(PyObject *list, int n); + +/** + * Given an array of LLVMTypeRef's, create a PyList object. Note that + * currently such an action is required only for LLVMTypeRef's, when + * it is required another type, this method has to be generalized. + */ +PyObject *make_list_from_LLVMTypeRef_array(LLVMTypeRef *p, unsigned n); + + +/*===----------------------------------------------------------------------===*/ +/* Wrapper macros */ +/*===----------------------------------------------------------------------===*/ + +/* The following wrapper macros define functions that wrap over LLVM-C APIs + * of various signatures. Though they look hairy, they all follow some + * conventions. + */ + +/** + * Wrap LLVM functions of the type + * outtype func(intype1 arg1) + */ +#define _wrap_obj2obj(func, intype1, outtype) \ +static PyObject * \ +_w ## func (PyObject *self, PyObject *args) \ +{ \ + intype1 arg1; \ + \ + if (!(arg1 = ( intype1 )get_object_arg(args))) \ + return NULL; \ + \ + return ctor_ ## outtype ( func (arg1)); \ +} + +/** + * Wrap LLVM functions of the type + * outtype func(int arg1) + */ +#define _wrap_int2obj(func, outtype) \ +static PyObject * \ +_w ## func (PyObject *self, PyObject *args) \ +{ \ + int arg1; \ + \ + if (!PyArg_ParseTuple(args, "i", &arg1)) \ + return NULL; \ + \ + return ctor_ ## outtype ( func (arg1)); \ +} + +/** + * Wrap LLVM functions of the type + * outtype func(intype1 arg1, intype2 arg2) + */ +#define _wrap_objobj2obj(func, intype1, intype2, outtype) \ +static PyObject * \ +_w ## func (PyObject *self, PyObject *args) \ +{ \ + PyObject *obj1, *obj2; \ + intype1 arg1; \ + intype2 arg2; \ + \ + if (!PyArg_ParseTuple(args, "OO", &obj1, &obj2)) \ + return NULL; \ + \ + arg1 = ( intype1 ) PyCObject_AsVoidPtr(obj1); \ + arg2 = ( intype2 ) PyCObject_AsVoidPtr(obj2); \ + \ + return ctor_ ## outtype ( func (arg1, arg2)); \ +} + +/** + * Wrap LLVM functions of the type + * void func(intype1 arg1, intype2 arg2) + */ +#define _wrap_objobj2none(func, intype1, intype2) \ +static PyObject * \ +_w ## func (PyObject *self, PyObject *args) \ +{ \ + PyObject *obj1, *obj2; \ + intype1 arg1; \ + intype2 arg2; \ + \ + if (!PyArg_ParseTuple(args, "OO", &obj1, &obj2)) \ + return NULL; \ + \ + arg1 = ( intype1 ) PyCObject_AsVoidPtr(obj1); \ + arg2 = ( intype2 ) PyCObject_AsVoidPtr(obj2); \ + \ + func (arg1, arg2); \ + Py_RETURN_NONE; \ +} + +/** + * Wrap LLVM functions of the type + * outtype func(intype1 arg1, arg2) + */ +#define _wrap_objint2obj(func, intype1, outtype) \ +static PyObject * \ +_w ## func (PyObject *self, PyObject *args) \ +{ \ + PyObject *obj1; \ + intype1 arg1; \ + unsigned int arg2; \ + \ + if (!PyArg_ParseTuple(args, "OI", &obj1, &arg2)) \ + return NULL; \ + \ + arg1 = ( intype1 ) PyCObject_AsVoidPtr(obj1); \ + \ + return ctor_ ## outtype ( func (arg1, arg2)); \ +} + +/** + * Wrap LLVM functions of the type + * outtype func(intype1 arg1, intype2 arg2, intype3 arg3) + */ +#define _wrap_objobjobj2obj(func, intype1, intype2, intype3, outtype) \ +static PyObject * \ +_w ## func (PyObject *self, PyObject *args) \ +{ \ + PyObject *obj1, *obj2, *obj3; \ + intype1 arg1; \ + intype2 arg2; \ + intype3 arg3; \ + \ + if (!PyArg_ParseTuple(args, "OOO", &obj1, &obj2, &obj3)) \ + return NULL; \ + \ + arg1 = ( intype1 ) PyCObject_AsVoidPtr(obj1); \ + arg2 = ( intype2 ) PyCObject_AsVoidPtr(obj2); \ + arg3 = ( intype3 ) PyCObject_AsVoidPtr(obj3); \ + \ + return ctor_ ## outtype ( func (arg1, arg2, arg3)); \ +} + +/** + * Wrap LLVM functions of the type + * void func(intype1 arg1, intype2 arg2, intype3 arg3) + */ +#define _wrap_objobjobj2none(func, intype1, intype2, intype3) \ +static PyObject * \ +_w ## func (PyObject *self, PyObject *args) \ +{ \ + PyObject *obj1, *obj2, *obj3; \ + intype1 arg1; \ + intype2 arg2; \ + intype3 arg3; \ + \ + if (!PyArg_ParseTuple(args, "OOO", &obj1, &obj2, &obj3)) \ + return NULL; \ + \ + arg1 = ( intype1 ) PyCObject_AsVoidPtr(obj1); \ + arg2 = ( intype2 ) PyCObject_AsVoidPtr(obj2); \ + arg3 = ( intype3 ) PyCObject_AsVoidPtr(obj3); \ + \ + func (arg1, arg2, arg3); \ + Py_RETURN_NONE; \ +} + +/** + * Wrap LLVM functions of the type + * outtype func( arg1, intype2 arg2, intype3 arg3) + */ +#define _wrap_intobjobj2obj(func, intype2, intype3, outtype) \ +static PyObject * \ +_w ## func (PyObject *self, PyObject *args) \ +{ \ + unsigned int arg1; \ + PyObject *obj2, *obj3; \ + intype2 arg2; \ + intype3 arg3; \ + \ + if (!PyArg_ParseTuple(args, "IOO", &arg1, &obj2, &obj3)) \ + return NULL; \ + \ + arg2 = ( intype2 ) PyCObject_AsVoidPtr(obj2); \ + arg3 = ( intype3 ) PyCObject_AsVoidPtr(obj3); \ + \ + return ctor_ ## outtype ( func (arg1, arg2, arg3)); \ +} + +/** + * Wrap LLVM functions of the type + * outtype func(const char *s) + */ +#define _wrap_str2obj(func, outtype) \ +static PyObject * \ +_w ## func (PyObject *self, PyObject *args) \ +{ \ + const char *arg1; \ + \ + if (!PyArg_ParseTuple(args, "s", &arg1)) \ + return NULL; \ + \ + return ctor_ ## outtype ( func (arg1)); \ +} + +/** + * Wrap LLVM functions of the type + * outtype func() + */ +#define _wrap_none2obj(func, outtype) \ +static PyObject * \ +_w ## func (PyObject *self, PyObject *args) \ +{ \ + if (!PyArg_ParseTuple(args, "")) \ + return NULL; \ + \ + return ctor_ ## outtype ( func ()); \ +} + +/** + * Wrap LLVM functions of the type + * const char *func(intype1 arg1) + */ +#define _wrap_obj2str(func, intype1) \ +static PyObject * \ +_w ## func (PyObject *self, PyObject *args) \ +{ \ + intype1 arg1; \ + \ + if (!(arg1 = ( intype1 )get_object_arg(args))) \ + return NULL; \ + \ + return PyString_FromString( func (arg1)); \ +} + +/** + * Wrap LLVM functions of the type + * void func(intype1 arg1) + */ +#define _wrap_obj2none(func, intype1) \ +static PyObject * \ +_w ## func (PyObject *self, PyObject *args) \ +{ \ + intype1 arg1; \ + \ + if (!(arg1 = ( intype1 )get_object_arg(args))) \ + return NULL; \ + \ + func (arg1); \ + Py_RETURN_NONE; \ +} + +/** + * Wrap LLVM functions of the type + * void func(intype1 arg1, const char *arg2) + */ +#define _wrap_objstr2none(func, intype1) \ +static PyObject * \ +_w ## func (PyObject *self, PyObject *args) \ +{ \ + PyObject *obj1; \ + const char *arg2; \ + intype1 arg1; \ + \ + if (!PyArg_ParseTuple(args, "Os", &obj1, &arg2)) \ + return NULL; \ + \ + arg1 = ( intype1 ) PyCObject_AsVoidPtr(obj1); \ + \ + func (arg1, arg2); \ + Py_RETURN_NONE; \ +} + +/** + * Wrap LLVM functions of the type + * outtype func(intype1 arg1, const char *arg2) + */ +#define _wrap_objstr2obj(func, intype1, outtype) \ +static PyObject * \ +_w ## func (PyObject *self, PyObject *args) \ +{ \ + PyObject *obj1; \ + const char *arg2; \ + intype1 arg1; \ + \ + if (!PyArg_ParseTuple(args, "Os", &obj1, &arg2)) \ + return NULL; \ + \ + arg1 = ( intype1 ) PyCObject_AsVoidPtr(obj1); \ + \ + return ctor_ ## outtype ( func (arg1, arg2)); \ +} + +/** + * Wrap LLVM functions of the type + * void func(intype1 arg1, arg2) + */ +#define _wrap_objint2none(func, intype1) \ +static PyObject * \ +_w ## func (PyObject *self, PyObject *args) \ +{ \ + PyObject *obj1; \ + int arg2; \ + intype1 arg1; \ + \ + if (!PyArg_ParseTuple(args, "Oi", &obj1, &arg2)) \ + return NULL; \ + \ + arg1 = ( intype1 ) PyCObject_AsVoidPtr(obj1); \ + \ + func (arg1, arg2); \ + Py_RETURN_NONE; \ +} + +/** + * Wrap LLVM functions of the type + * outtype func(intype1 arg1, intype2 arg2, const char *arg3) + */ +#define _wrap_objobjstr2obj(func, intype1, intype2, outtype) \ +static PyObject * \ +_w ## func (PyObject *self, PyObject *args) \ +{ \ + PyObject *obj1, *obj2; \ + intype1 arg1; \ + intype2 arg2; \ + const char *arg3; \ + \ + if (!PyArg_ParseTuple(args, "OOs", &obj1, &obj2, &arg3)) \ + return NULL; \ + \ + arg1 = ( intype1 ) PyCObject_AsVoidPtr(obj1); \ + arg2 = ( intype2 ) PyCObject_AsVoidPtr(obj2); \ + \ + return ctor_ ## outtype ( func (arg1, arg2, arg3)) ;\ +} + +/** + * Wrap LLVM functions of the type + * void func(intype1 arg1, arg2, arg3) + */ +#define _wrap_objintint2none(func, intype1) \ +static PyObject * \ +_w ## func (PyObject *self, PyObject *args) \ +{ \ + PyObject *obj1; \ + intype1 arg1; \ + int arg2, arg3; \ + \ + if (!PyArg_ParseTuple(args, "Oii", &obj1, &arg2, &arg3)) \ + return NULL; \ + \ + arg1 = ( intype1 ) PyCObject_AsVoidPtr(obj1); \ + \ + func (arg1, arg2, arg3); \ + Py_RETURN_NONE; \ +} + +/** + * Wrap LLVM functions of the type + * outtype func(intype1 arg1, const char *arg2, intype3 arg3) + */ +#define _wrap_objstrobj2obj(func, intype1, intype3, outtype) \ +static PyObject * \ +_w ## func (PyObject *self, PyObject *args) \ +{ \ + PyObject *obj1, *obj3; \ + intype1 arg1; \ + const char *arg2; \ + intype3 arg3; \ + \ + if (!PyArg_ParseTuple(args, "OsO", &obj1, &arg2, &obj3)) \ + return NULL; \ + \ + arg1 = ( intype1 ) PyCObject_AsVoidPtr(obj1); \ + arg3 = ( intype3 ) PyCObject_AsVoidPtr(obj3); \ + \ + return ctor_ ## outtype ( func (arg1, arg2, arg3)); \ +} + + +/** + * Wrap LLVM functions of the type + * outtype func(intype1 arg1, intype2 arg2, int arg3, const char *arg4) + */ +#define _wrap_objobjintstr2obj(func, intype1, intype2, outtype) \ +static PyObject * \ +_w ## func (PyObject *self, PyObject *args) \ +{ \ + PyObject *obj1, *obj2; \ + intype1 arg1; \ + intype2 arg2; \ + int arg3; \ + const char *arg4; \ + \ + if (!PyArg_ParseTuple(args, "OOis", &obj1, &obj2, &arg3, &arg4)) \ + return NULL; \ + \ + arg1 = ( intype1 ) PyCObject_AsVoidPtr(obj1); \ + arg2 = ( intype2 ) PyCObject_AsVoidPtr(obj2); \ + \ + return ctor_ ## outtype ( func (arg1, arg2, arg3, arg4)) ;\ +} + +/** + * Wrap LLVM functions of the type + * outtype func(intype1 arg1, intype2 arg2, intype3 arg3, const char *arg4) + */ +#define _wrap_objobjobjstr2obj(func, intype1, intype2, intype3, outtype) \ +static PyObject * \ +_w ## func (PyObject *self, PyObject *args) \ +{ \ + PyObject *obj1, *obj2, *obj3; \ + intype1 arg1; \ + intype2 arg2; \ + intype3 arg3; \ + const char *arg4; \ + \ + if (!PyArg_ParseTuple(args, "OOOs", &obj1, &obj2, &obj3, &arg4)) \ + return NULL; \ + \ + arg1 = ( intype1 ) PyCObject_AsVoidPtr(obj1); \ + arg2 = ( intype2 ) PyCObject_AsVoidPtr(obj2); \ + arg3 = ( intype3 ) PyCObject_AsVoidPtr(obj3); \ + \ + return ctor_ ## outtype ( func (arg1, arg2, arg3, arg4)); \ +} + +/** + * Wrap LLVM functions of the type + * outtype func(intype1 arg1, intype2 arg2, intype3 arg3, arg4) + */ +#define _wrap_objobjobjint2obj(func, intype1, intype2, intype3, outtype) \ +static PyObject * \ +_w ## func (PyObject *self, PyObject *args) \ +{ \ + PyObject *obj1, *obj2, *obj3; \ + intype1 arg1; \ + intype2 arg2; \ + intype3 arg3; \ + int arg4; \ + \ + if (!PyArg_ParseTuple(args, "OOOi", &obj1, &obj2, &obj3, &arg4)) \ + return NULL; \ + \ + arg1 = ( intype1 ) PyCObject_AsVoidPtr(obj1); \ + arg2 = ( intype2 ) PyCObject_AsVoidPtr(obj2); \ + arg3 = ( intype3 ) PyCObject_AsVoidPtr(obj3); \ + \ + return ctor_ ## outtype ( func (arg1, arg2, arg3, arg4)); \ +} + +/** + * Wrap LLVM functions of the type + * outtype func(intype1 arg1, intype2 arg2, intype3 arg3, intype arg4) + */ +#define _wrap_objobjobjobj2obj(func, intype1, intype2, intype3, intype4, outtype) \ +static PyObject * \ +_w ## func (PyObject *self, PyObject *args) \ +{ \ + PyObject *obj1, *obj2, *obj3, *obj4; \ + intype1 arg1; \ + intype2 arg2; \ + intype3 arg3; \ + intype4 arg4; \ + \ + if (!PyArg_ParseTuple(args, "OOOO", &obj1, &obj2, &obj3, &obj4)) \ + return NULL; \ + \ + arg1 = ( intype1 ) PyCObject_AsVoidPtr(obj1); \ + arg2 = ( intype2 ) PyCObject_AsVoidPtr(obj2); \ + arg3 = ( intype3 ) PyCObject_AsVoidPtr(obj3); \ + arg4 = ( intype4 ) PyCObject_AsVoidPtr(obj4); \ + \ + return ctor_ ## outtype ( func (arg1, arg2, arg3, arg4)); \ +} + +/** + * Wrap LLVM functions of the type + * outtype func(intype1 arg1, intype2 arg2, intype3 arg3, intype arg4, const char *arg5) + */ +#define _wrap_objobjobjobjstr2obj(func, intype1, intype2, intype3, intype4, outtype) \ +static PyObject * \ +_w ## func (PyObject *self, PyObject *args) \ +{ \ + PyObject *obj1, *obj2, *obj3, *obj4; \ + intype1 arg1; \ + intype2 arg2; \ + intype3 arg3; \ + intype4 arg4; \ + const char *arg5; \ + \ + if (!PyArg_ParseTuple(args, "OOOOs", &obj1, &obj2, &obj3, &obj4, &arg5)) \ + return NULL; \ + \ + arg1 = ( intype1 ) PyCObject_AsVoidPtr(obj1); \ + arg2 = ( intype2 ) PyCObject_AsVoidPtr(obj2); \ + arg3 = ( intype3 ) PyCObject_AsVoidPtr(obj3); \ + arg4 = ( intype4 ) PyCObject_AsVoidPtr(obj4); \ + \ + return ctor_ ## outtype ( func (arg1, arg2, arg3, arg4, arg5)); \ +} + +/** + * Wrap LLVM functions of the type + * outtype func(intype1 arg1, int arg2, intype3 arg3, intype4 arg4, const char *arg5) + */ +#define _wrap_objintobjobjstr2obj(func, intype1, intype3, intype4, outtype) \ +static PyObject * \ +_w ## func (PyObject *self, PyObject *args) \ +{ \ + PyObject *obj1, *obj3, *obj4; \ + intype1 arg1; \ + int arg2; \ + intype3 arg3; \ + intype4 arg4; \ + const char *arg5; \ + \ + if (!PyArg_ParseTuple(args, "OiOOs", &obj1, &arg2, &obj3, &obj4, &arg5)) \ + return NULL; \ + \ + arg1 = ( intype1 ) PyCObject_AsVoidPtr(obj1); \ + arg3 = ( intype3 ) PyCObject_AsVoidPtr(obj3); \ + arg4 = ( intype4 ) PyCObject_AsVoidPtr(obj4); \ + \ + return ctor_ ## outtype ( func (arg1, arg2, arg3, arg4, arg5)); \ +} + +/** + * Wrap LLVM functions of the type + * outtype func(intype1 arg1, intype2 *arg2v, unsigned arg2n) + * where arg2v is an array of intype2 elements, arg2n in length. + */ +#define _wrap_objlist2obj(func, intype1, intype2, outtype) \ +static PyObject * \ +_w ## func (PyObject *self, PyObject *args) \ +{ \ + PyObject *obj1, *obj2; \ + intype1 arg1; \ + intype2 *arg2v; \ + unsigned arg2n; \ + outtype ret; \ + \ + if (!PyArg_ParseTuple(args, "OO", &obj1, &obj2)) \ + return NULL; \ + \ + arg1 = ( intype1 ) PyCObject_AsVoidPtr(obj1); \ + arg2n = (unsigned) PyList_Size(obj2); \ + if (!(arg2v = ( intype2 *)make_array_from_list(obj2, arg2n))) \ + return PyErr_NoMemory(); \ + \ + ret = func (arg1, arg2v, arg2n); \ + free(arg2v); \ + return ctor_ ## outtype (ret); \ +} + +/** + * Wrap LLVM functions of the type + * outtype func(intype1 *arg1v, unsigned arg1n, int arg2) + * where arg1v is an array of intype1 elements, arg1n in length. + */ +#define _wrap_listint2obj(func, intype1, outtype) \ +static PyObject * \ +_w ## func (PyObject *self, PyObject *args) \ +{ \ + PyObject *obj1; \ + int arg2; \ + intype1 *arg1v; \ + unsigned arg1n; \ + outtype ret; \ + \ + if (!PyArg_ParseTuple(args, "Oi", &obj1, &arg2)) \ + return NULL; \ + \ + arg1n = (unsigned) PyList_Size(obj1); \ + if (!(arg1v = ( intype1 *)make_array_from_list(obj1, arg1n))) \ + return PyErr_NoMemory(); \ + \ + ret = func (arg1v, arg1n, arg2); \ + free(arg1v); \ + return ctor_ ## outtype (ret); \ +} + +/** + * Wrap LLVM functions of the type + * outtype func(intype1 *arg1v, unsigned arg1n) + * where arg1v is an array of intype1 elements, arg1n in length. + */ +#define _wrap_list2obj(func, intype1, outtype) \ +static PyObject * \ +_w ## func (PyObject *self, PyObject *args) \ +{ \ + PyObject *obj1; \ + intype1 *arg1v; \ + unsigned arg1n; \ + outtype ret; \ + \ + if (!PyArg_ParseTuple(args, "O", &obj1)) \ + return NULL; \ + \ + arg1n = (unsigned) PyList_Size(obj1); \ + if (!(arg1v = ( intype1 *)make_array_from_list(obj1, arg1n))) \ + return PyErr_NoMemory(); \ + \ + ret = func (arg1v, arg1n); \ + free(arg1v); \ + return ctor_ ## outtype (ret); \ +} + +/** + * Wrap LLVM functions of the type + * outtype func(intype1 arg1, intype2 *arg2v, unsigned arg2n, int arg3) + * where arg2v is an array of intype2 elements, arg2n in length. + */ +#define _wrap_objlistint2obj(func, intype1, intype2, outtype) \ +static PyObject * \ +_w ## func (PyObject *self, PyObject *args) \ +{ \ + PyObject *obj1, *obj2; \ + intype1 arg1; \ + intype2 *arg2v; \ + unsigned arg2n; \ + int arg3; \ + outtype ret; \ + \ + if (!PyArg_ParseTuple(args, "OOi", &obj1, &obj2, &arg3)) \ + return NULL; \ + \ + arg1 = ( intype1 ) PyCObject_AsVoidPtr(obj1); \ + arg2n = (unsigned) PyList_Size(obj2); \ + if (!(arg2v = ( intype2 *)make_array_from_list(obj2, arg2n))) \ + return PyErr_NoMemory(); \ + \ + ret = func (arg1, arg2v, arg2n, arg3); \ + free(arg2v); \ + return ctor_ ## outtype (ret); \ +} + +/** + * Wrap LLVM functions of the type + * outtype func(intype1 arg1, intype2 arg2, intype3 *arg3v, unsigned arg3n) + * where arg3v is an array of intype3 elements, arg3n in length. + */ +#define _wrap_objobjlist2obj(func, intype1, intype2, intype3, outtype) \ +static PyObject * \ +_w ## func (PyObject *self, PyObject *args) \ +{ \ + PyObject *obj1, *obj2, *obj3; \ + intype1 arg1; \ + intype2 arg2; \ + intype3 *arg3v; \ + unsigned arg3n; \ + outtype ret; \ + \ + if (!PyArg_ParseTuple(args, "OOO", &obj1, &obj2, &obj3)) \ + return NULL; \ + \ + arg1 = ( intype1 ) PyCObject_AsVoidPtr(obj1); \ + arg2 = ( intype2 ) PyCObject_AsVoidPtr(obj2); \ + arg3n = (unsigned) PyList_Size(obj3); \ + if (!(arg3v = ( intype3 *)make_array_from_list(obj3, arg3n))) \ + return PyErr_NoMemory(); \ + \ + ret = func (arg1, arg2, arg3v, arg3n); \ + free(arg3v); \ + return ctor_ ## outtype (ret); \ +} + +/** + * Wrap LLVM functions of the type + * outtype func(intype1 arg1, intype2 arg2, intype3 *arg3v, unsigned arg3n, const char *arg4) + * where arg3v is an array of intype3 elements, arg3n in length. + */ +#define _wrap_objobjliststr2obj(func, intype1, intype2, intype3, outtype) \ +static PyObject * \ +_w ## func (PyObject *self, PyObject *args) \ +{ \ + PyObject *obj1, *obj2, *obj3; \ + const char *arg4; \ + intype1 arg1; \ + intype2 arg2; \ + intype3 *arg3v; \ + unsigned arg3n; \ + outtype ret; \ + \ + if (!PyArg_ParseTuple(args, "OOOs", &obj1, &obj2, &obj3, &arg4))\ + return NULL; \ + \ + arg1 = ( intype1 ) PyCObject_AsVoidPtr(obj1); \ + arg2 = ( intype2 ) PyCObject_AsVoidPtr(obj2); \ + arg3n = ( unsigned ) PyList_Size(obj3); \ + if (!(arg3v = ( intype3 *)make_array_from_list(obj3, arg3n))) \ + return PyErr_NoMemory(); \ + \ + ret = func (arg1, arg2, arg3v, arg3n, arg4); \ + free(arg3v); \ + return ctor_ ## outtype (ret); \ +} + +/** + * Wrap LLVM dump-to-string functions of the type + * char *func(intype1) + * where the return value has to be disposed after use by calling + * LLVMDisposeMessage. + */ +#define _wrap_dumper(func, intype1) \ +static PyObject * \ +_w ## func (PyObject *self, PyObject *args) \ +{ \ + intype1 arg1; \ + char *val; \ + PyObject *ret; \ + \ + if (!(arg1= ( intype1 ) get_object_arg(args))) \ + return NULL; \ + \ + val = func (arg1); \ + ret = PyString_FromString(val); \ + LLVMDisposeMessage(val); \ + return ret; \ +} + +#endif /* LLVM_PY_WRAP_H */ +