diff --git a/llvm/__init__.py b/llvm/__init__.py index a537015..b540270 100644 --- a/llvm/__init__.py +++ b/llvm/__init__.py @@ -137,8 +137,18 @@ class _ObjectCache(type): # Cacheables #===----------------------------------------------------------------------=== -class Cacheable(object): - __metaclass__ = _ObjectCache +# version 2/3 compatibility help +# version 2 metaclass +# class Cacheable(object): +# __metaclass__ = _ObjectCache # Doing nothing for version 3 +# +# version 3 metaclass +# class Cacheable(metaclass=_ObjectCache): +# +# Reference: http://mikewatkins.ca/2008/11/29/python-2-and-3-metaclasses/#using-the-metaclass-in-python-3-x +ObjectCache = _ObjectCache('ObjectCache', (object, ), {}) + +class Cacheable(ObjectCache): """Objects that can be cached. Objects that wrap a PyCObject are cached to avoid "aliasing", i.e., @@ -146,5 +156,5 @@ class Cacheable(object): to the same C pointer.""" def forget(self): - _ObjectCache.forget(self) + ObjectCache.forget(self) diff --git a/llvm/_core.cpp b/llvm/_core.cpp index bccd959..52310f1 100644 --- a/llvm/_core.cpp +++ b/llvm/_core.cpp @@ -52,11 +52,6 @@ typedef int Py_ssize_t; #endif -/* Project-wide setting */ -#if (PY_MAJOR_VERSION >= 3) -#define LLVM_PY_USE_PYCAPSULE -#endif - /*===----------------------------------------------------------------------===*/ /* Modules */ /*===----------------------------------------------------------------------===*/ @@ -67,18 +62,10 @@ _wLLVMModuleCreateWithName(PyObject *self, PyObject *args) const char *s; LLVMModuleRef module; -#ifdef LLVM_PY_USE_PYCAPSULE - PyObject *s_; - if (!PyArg_ParseTuple(args, "U:LLVMModuleCreateWithName", &s_)) { + if (!PyArg_ParseTuple(args, "s:LLVMModuleCreateWithName", &s)) { return NULL; } - s = PyUnicode_AS_DATA(s_); -#else - if (!PyArg_ParseTuple(args, "s", &s)) - return NULL; -#endif - module = LLVMModuleCreateWithName(s); return ctor_LLVMModuleRef(module); } @@ -112,37 +99,62 @@ _wLLVMVerifyModule(PyObject *self, PyObject *args) (void) LLVMVerifyModule(m, LLVMReturnStatusAction, &outmsg); if (outmsg) { - ret = PyBytes_FromString(outmsg); + ret = PyUnicode_FromString(outmsg); LLVMDisposeMessage(outmsg); } else { - ret = PyBytes_FromString(""); + ret = PyUnicode_FromString(""); } return ret; } -typedef LLVMModuleRef (*asm_or_bc_fn_t)(const char *A, unsigned Len, - char **OutMessage); +///// unused +// typedef LLVMModuleRef (*asm_or_bc_fn_t)(const char *A, unsigned Len, char **OutMessage); + + +static PyObject* +_wLLVMGetModuleFromAssembly(PyObject *self, PyObject *args) +{ + char * str; + if ( !PyArg_ParseTuple(args, "s", &str) ){ + return NULL; + } + + char * outmsg = 0; + LLVMModuleRef mod = LLVMGetModuleFromAssembly(str, &outmsg); + + if ( !mod ){ + if ( outmsg ){ + PyObject * ret = PyUnicode_FromString(outmsg); + LLVMDisposeMessage(outmsg); + return ret; + } else { + Py_RETURN_NONE; + } + } + + return ctor_LLVMModuleRef(mod); +} static PyObject * -_get_asm_or_bc(asm_or_bc_fn_t fn, PyObject *self, PyObject *args) +_wLLVMGetModuleFromBitcode(PyObject *self, PyObject *args) { PyObject *obj, *ret; Py_ssize_t len; char *start, *outmsg; LLVMModuleRef m; - if (!PyArg_ParseTuple(args, "O", &obj)) + if (!PyArg_ParseTuple(args, "S", &obj)) return NULL; start = PyBytes_AsString(obj); len = PyBytes_Size(obj); outmsg = 0; - m = fn(start, len, &outmsg); + m = LLVMGetModuleFromBitcode(start, len, &outmsg); if (!m) { if (outmsg) { - ret = PyBytes_FromString(outmsg); + ret = PyUnicode_FromString(outmsg); LLVMDisposeMessage(outmsg); return ret; } else { @@ -153,18 +165,6 @@ _get_asm_or_bc(asm_or_bc_fn_t fn, PyObject *self, PyObject *args) return ctor_LLVMModuleRef(m); } -static PyObject * -_wLLVMGetModuleFromAssembly(PyObject *self, PyObject *args) -{ - return _get_asm_or_bc(LLVMGetModuleFromAssembly, self, args); -} - -static PyObject * -_wLLVMGetModuleFromBitcode(PyObject *self, PyObject *args) -{ - return _get_asm_or_bc(LLVMGetModuleFromBitcode, self, args); -} - static PyObject * _wLLVMGetBitcodeFromModule(PyObject *self, PyObject *args) { @@ -195,20 +195,15 @@ _wLLVMLinkModules(PyObject *self, PyObject *args) if (!PyArg_ParseTuple(args, "OO|I", &dest_obj, &src_obj, &mode)) return NULL; -#ifdef LLVM_PY_USE_PYCAPSULE dest = (LLVMModuleRef) PyCapsule_GetPointer(dest_obj, NULL); src = (LLVMModuleRef) PyCapsule_GetPointer(src_obj, NULL); -#else - dest = (LLVMModuleRef) PyCObject_AsVoidPtr(dest_obj); - src = (LLVMModuleRef) PyCObject_AsVoidPtr(src_obj); -#endif if (!LLVMLinkModules(dest, src, mode, &errmsg)) { if (errmsg) { - ret = PyBytes_FromString(errmsg); + ret = PyUnicode_FromString(errmsg); LLVMDisposeMessage(errmsg); } else { - ret = PyBytes_FromString("Link error"); + ret = PyUnicode_FromString("Link error"); } return ret; } @@ -408,11 +403,7 @@ _wLLVMConstInt(PyObject *self, PyObject *args) if (!PyArg_ParseTuple(args, "OKi", &obj, &n, &sign_extend)) return NULL; -#ifdef LLVM_PY_USE_PYCAPSULE ty = (LLVMTypeRef) PyCapsule_GetPointer(obj, NULL); -#else - ty = (LLVMTypeRef)(PyCObject_AsVoidPtr(obj)); -#endif val = LLVMConstInt(ty, n, sign_extend); return ctor_LLVMValueRef(val); @@ -429,11 +420,7 @@ _wLLVMConstReal(PyObject *self, PyObject *args) if (!PyArg_ParseTuple(args, "Od", &obj, &d)) return NULL; -#ifdef LLVM_PY_USE_PYCAPSULE ty = (LLVMTypeRef) PyCapsule_GetPointer(obj, NULL); -#else - ty = (LLVMTypeRef)(PyCObject_AsVoidPtr(obj)); -#endif val = LLVMConstReal(ty, d); return ctor_LLVMValueRef(val); @@ -450,18 +437,10 @@ _wLLVMConstString(PyObject *self, PyObject *args) int dont_null_terminate; LLVMValueRef val; -#ifdef LLVM_PY_USE_PYCAPSULE - PyObject *s_; - if (!PyArg_ParseTuple(args, "Ui:LLVMConstString", &s_, &dont_null_terminate)) { + if (!PyArg_ParseTuple(args, "si:LLVMConstString", &s, &dont_null_terminate)) { return NULL; } - s = PyUnicode_AS_DATA(s_); -#else - if (!PyArg_ParseTuple(args, "si", &s, &dont_null_terminate)) - return NULL; -#endif - val = LLVMConstString(s, strlen(s), dont_null_terminate); return ctor_LLVMValueRef(val); } @@ -676,38 +655,22 @@ _wLLVMBuildInvoke(PyObject *self, PyObject *args) LLVMBasicBlockRef then_blk, catch_blk; LLVMValueRef inst; -#ifdef LLVM_PY_USE_PYCAPSULE - PyObject *name_; - if (!PyArg_ParseTuple(args, "OOOOOU:LLVMBuildInvoke", &obj1, &obj2, &obj3, &obj4, &obj5, &name_)) { + if (!PyArg_ParseTuple(args, "OOOOOs:LLVMBuildInvoke", &obj1, &obj2, &obj3, &obj4, &obj5, &name)) { return NULL; } - name = PyUnicode_AS_DATA(name_); -#else - if (!PyArg_ParseTuple(args, "OOOOOs", &obj1, &obj2, &obj3, &obj4, &obj5, &name)) - return NULL; -#endif - -#ifdef LLVM_PY_USE_PYCAPSULE builder = (LLVMBuilderRef) PyCapsule_GetPointer(obj1, NULL); func = (LLVMValueRef) PyCapsule_GetPointer(obj2, NULL); -#else - builder = (LLVMBuilderRef)(PyCObject_AsVoidPtr(obj1)); - func = (LLVMValueRef)(PyCObject_AsVoidPtr(obj2)); -#endif + fnarg_count = (unsigned) PyList_Size(obj3); fnargs = (LLVMValueRef *)make_array_from_list(obj3, fnarg_count); if (!fnargs) return PyErr_NoMemory(); -#ifdef LLVM_PY_USE_PYCAPSULE then_blk = (LLVMBasicBlockRef) PyCapsule_GetPointer(obj4, NULL); catch_blk = (LLVMBasicBlockRef) PyCapsule_GetPointer(obj5, NULL); -#else - then_blk = (LLVMBasicBlockRef)(PyCObject_AsVoidPtr(obj4)); - catch_blk = (LLVMBasicBlockRef)(PyCObject_AsVoidPtr(obj5)); -#endif + inst = LLVMBuildInvoke(builder, func, fnargs, fnarg_count, then_blk, catch_blk, name); @@ -800,22 +763,14 @@ _wLLVMCreateMemoryBufferWithContentsOfFile(PyObject *self, PyObject *args) char *outmsg; PyObject *ret; -#ifdef LLVM_PY_USE_PYCAPSULE - PyObject *path_; - if (!PyArg_ParseTuple(args, "U:LLVMCreateMemoryBufferWithContentsOfFile", &path_)) { + if (!PyArg_ParseTuple(args, "s:LLVMCreateMemoryBufferWithContentsOfFile", &path)) { return NULL; } - path = PyUnicode_AS_DATA(path_); -#else - if (!PyArg_ParseTuple(args, "s", &path)) - return NULL; -#endif - if (!LLVMCreateMemoryBufferWithContentsOfFile(path, &ref, &outmsg)) { ret = ctor_LLVMMemoryBufferRef(ref); } else { - ret = PyBytes_FromString(outmsg); + ret = PyUnicode_FromString(outmsg); LLVMDisposeMessage(outmsg); } @@ -832,7 +787,7 @@ _wLLVMCreateMemoryBufferWithSTDIN(PyObject *self, PyObject *args) if (!LLVMCreateMemoryBufferWithSTDIN(&ref, &outmsg)) { ret = ctor_LLVMMemoryBufferRef(ref); } else { - ret = PyBytes_FromString(outmsg); + ret = PyUnicode_FromString(outmsg); LLVMDisposeMessage(outmsg); } @@ -1021,7 +976,7 @@ _wLLVMTargetDataAsString(PyObject *self, PyObject *args) return NULL; tdrep = LLVMCopyStringRepOfTargetData(td); - ret = PyBytes_FromString(tdrep); + ret = PyUnicode_FromString(tdrep); LLVMDisposeMessage(tdrep); return ret; } @@ -1066,15 +1021,13 @@ _wLLVMEngineBuilderCreate(PyObject *self, PyObject *args) if (!(obj = (LLVMEngineBuilderRef)get_object_arg(args))) return NULL; - char * outmsg = 0; - - LLVMExecutionEngineRef ee = LLVMEngineBuilderCreate(obj, &outmsg); + std::string outmsg; + LLVMExecutionEngineRef ee = LLVMEngineBuilderCreate(obj, outmsg); PyObject * ret; - if( outmsg ){ // check if error message is set. - ret = PyBytes_FromString(outmsg); - free(outmsg); + if( !ee ){ // check if error message is set. + ret = PyUnicode_FromString(outmsg.c_str()); }else{ ret = ctor_LLVMExecutionEngineRef(ee); } @@ -1100,11 +1053,7 @@ _wLLVMCreateExecutionEngine(PyObject *self, PyObject *args) if (!PyArg_ParseTuple(args, "Oi", &obj, &force_interpreter)) return NULL; -#ifdef LLVM_PY_USE_PYCAPSULE mod = (LLVMModuleRef) PyCapsule_GetPointer(obj, NULL); -#else - mod = (LLVMModuleRef) PyCObject_AsVoidPtr(obj); -#endif if (force_interpreter) error = LLVMCreateInterpreterForModule(&ee, mod, &outmsg); @@ -1112,7 +1061,7 @@ _wLLVMCreateExecutionEngine(PyObject *self, PyObject *args) error = LLVMCreateJITCompilerForModule(&ee, mod, 1 /*fast*/, &outmsg); if (error) { - ret = PyBytes_FromString(outmsg); + ret = PyUnicode_FromString(outmsg); LLVMDisposeMessage(outmsg); } else { ret = ctor_LLVMExecutionEngineRef(ee); @@ -1132,13 +1081,8 @@ _wLLVMGetPointerToFunction(PyObject *self, PyObject *args) if (!PyArg_ParseTuple(args, "OO", &obj_ee, &obj_fn)) return NULL; -#ifdef LLVM_PY_USE_PYCAPSULE ee = (LLVMExecutionEngineRef) PyCapsule_GetPointer(obj_ee, NULL); fn = (LLVMValueRef) PyCapsule_GetPointer(obj_fn, NULL); -#else - ee = (LLVMExecutionEngineRef) PyCObject_AsVoidPtr(obj_ee); - fn = (LLVMValueRef) PyCObject_AsVoidPtr(obj_fn); -#endif return PyLong_FromVoidPtr(LLVMGetPointerToFunction(ee,fn)); } @@ -1163,23 +1107,18 @@ _wLLVMRemoveModule2(PyObject *self, PyObject *args) if (!PyArg_ParseTuple(args, "OO", &obj_ee, &obj_mod)) return NULL; -#ifdef LLVM_PY_USE_PYCAPSULE ee = (LLVMExecutionEngineRef) PyCapsule_GetPointer(obj_ee, NULL); mod = (LLVMModuleRef) PyCapsule_GetPointer(obj_mod, NULL); -#else - ee = (LLVMExecutionEngineRef) PyCObject_AsVoidPtr(obj_ee); - mod = (LLVMModuleRef) PyCObject_AsVoidPtr(obj_mod); -#endif LLVMRemoveModule(ee, mod, &mod_new, &outmsg); if (mod_new) { ret = ctor_LLVMModuleRef(mod_new); } else { if (outmsg) { - ret = PyBytes_FromString(outmsg); + ret = PyUnicode_FromString(outmsg); LLVMDisposeMessage(outmsg); } else { - ret = PyBytes_FromString("error"); + ret = PyUnicode_FromString("error"); } } @@ -1215,11 +1154,7 @@ _wLLVMCreateGenericValueOfInt(PyObject *self, PyObject *args) if (!PyArg_ParseTuple(args, "OLi", &obj1, &n, &is_signed)) return NULL; -#ifdef LLVM_PY_USE_PYCAPSULE ty = (LLVMTypeRef) PyCapsule_GetPointer(obj1, NULL); -#else - ty = (LLVMTypeRef) PyCObject_AsVoidPtr(obj1); -#endif gv = LLVMCreateGenericValueOfInt(ty, n, is_signed); return ctor_LLVMGenericValueRef(gv); @@ -1236,11 +1171,7 @@ _wLLVMCreateGenericValueOfFloat(PyObject *self, PyObject *args) if (!PyArg_ParseTuple(args, "Od", &obj1, &d)) return NULL; -#ifdef LLVM_PY_USE_PYCAPSULE ty = (LLVMTypeRef) PyCapsule_GetPointer(obj1, NULL); -#else - ty = (LLVMTypeRef) PyCObject_AsVoidPtr(obj1); -#endif gv = LLVMCreateGenericValueOfFloat(ty, d); return ctor_LLVMGenericValueRef(gv); @@ -1260,11 +1191,7 @@ _wLLVMCreateGenericValueOfPointer(PyObject *self, PyObject *args) n=n_; -#ifdef LLVM_PY_USE_PYCAPSULE ty = (LLVMTypeRef) PyCapsule_GetPointer(obj1, NULL); -#else - ty = (LLVMTypeRef) PyCObject_AsVoidPtr(obj1); -#endif gv = LLVMCreateGenericValueOfPointer((void*)n); return ctor_LLVMGenericValueRef(gv); @@ -1281,11 +1208,7 @@ _wLLVMGenericValueToInt(PyObject *self, PyObject *args) if (!PyArg_ParseTuple(args, "Oi", &obj1, &is_signed)) return NULL; -#ifdef LLVM_PY_USE_PYCAPSULE gv = (LLVMGenericValueRef) PyCapsule_GetPointer(obj1, NULL); -#else - gv = (LLVMGenericValueRef) PyCObject_AsVoidPtr(obj1); -#endif val = LLVMGenericValueToInt(gv, is_signed); return is_signed ? @@ -1304,13 +1227,8 @@ _wLLVMGenericValueToFloat(PyObject *self, PyObject *args) if (!PyArg_ParseTuple(args, "OO", &obj1, &obj2)) return NULL; -#ifdef LLVM_PY_USE_PYCAPSULE ty = (LLVMTypeRef) PyCapsule_GetPointer(obj1, NULL); gv = (LLVMGenericValueRef) PyCapsule_GetPointer(obj2, NULL); -#else - ty = (LLVMTypeRef) PyCObject_AsVoidPtr(obj1); - gv = (LLVMGenericValueRef) PyCObject_AsVoidPtr(obj2); -#endif val = LLVMGenericValueToFloat(ty, gv); return PyFloat_FromDouble(val); @@ -1326,11 +1244,7 @@ _wLLVMGenericValueToPointer(PyObject *self, PyObject *args) if (!PyArg_ParseTuple(args, "O", &obj1)) return NULL; -#ifdef LLVM_PY_USE_PYCAPSULE gv = (LLVMGenericValueRef) PyCapsule_GetPointer(obj1, NULL); -#else - gv = (LLVMGenericValueRef) PyCObject_AsVoidPtr(obj1); -#endif val = LLVMGenericValueToPointer(gv); return PyLong_FromVoidPtr(val); @@ -1353,23 +1267,14 @@ _wLLVMLoadLibraryPermanently(PyObject *self, PyObject *args) char *outmsg; PyObject *ret; -#ifdef LLVM_PY_USE_PYCAPSULE - PyObject *filename_; - if (!PyArg_ParseTuple(args, "U:LLVMLoadLibraryPermanently", &filename_)) { + if (!PyArg_ParseTuple(args, "s:LLVMLoadLibraryPermanently", &filename)) { return NULL; } - filename = PyUnicode_AS_DATA(filename_); -#else - if (!PyArg_ParseTuple(args, "s", &filename)) { - return NULL; - } -#endif - outmsg = 0; if (!LLVMLoadLibraryPermanently(filename, &outmsg)) { if (outmsg) { - ret = PyBytes_FromString(outmsg); + ret = PyUnicode_FromString(outmsg); LLVMDisposeMessage(outmsg); return ret; } @@ -1399,18 +1304,9 @@ _wPyCObjectVoidPtrToPyLong(PyObject *self, PyObject *args) /* Python member method table */ /*===----------------------------------------------------------------------===*/ -#ifdef LLVM_PY_USE_PYCAPSULE - #define _method( func ) { # func , ( PyCFunction )_w ## func , METH_VARARGS , NULL }, #define _pass( P ) _method( LLVMAdd ## P ## Pass ) -#else - -#define _method( func ) { # func , _w ## func , METH_VARARGS }, -#define _pass( P ) _method( LLVMAdd ## P ## Pass ) - -#endif - static PyMethodDef core_methods[] = { /* Modules */ @@ -1954,7 +1850,7 @@ static PyMethodDef core_methods[] = { // Module main function, hairy because of py3k port extern "C" { - + #if (PY_MAJOR_VERSION >= 3) struct PyModuleDef module_def = { PyModuleDef_HEAD_INIT, diff --git a/llvm/_util.py b/llvm/_util.py index 1ff4cc9..1766d6f 100644 --- a/llvm/_util.py +++ b/llvm/_util.py @@ -77,3 +77,22 @@ def wrapiter(first, next, container, wrapper): ptr = next(ptr) return ret + +#===----------------------------------------------------------------------=== +# Py2/3 compatibility string check +#===----------------------------------------------------------------------=== +def _isstring_py2(x): + return isinstance(x, basestring) + +def _isstring_py3(x): + return isinstance(x, str) + +def _isstring_choose(): + try: + basestring + return _isstring_py2 + except: + return _isstring_py3 + +isstring = _isstring_choose() + diff --git a/llvm/core.py b/llvm/core.py index 1658846..f4c75bf 100644 --- a/llvm/core.py +++ b/llvm/core.py @@ -348,7 +348,7 @@ class Module(llvm.Ownable, llvm.Cacheable): ret = _core.LLVMGetModuleFromBitcode(data) if not ret: raise llvm.LLVMException("Unable to create module from bitcode") - elif isinstance(ret, str): + elif _util.isstring(ret): raise llvm.LLVMException(ret) else: return Module(ret) diff --git a/llvm/ee.py b/llvm/ee.py index 24b9325..62795a0 100644 --- a/llvm/ee.py +++ b/llvm/ee.py @@ -209,7 +209,7 @@ class EngineBuilder(object): def create(self): ret = _core.LLVMEngineBuilderCreate(self.ptr) - if isinstance(ret, basestring): + if isinstance(ret, str): raise llvm.LLVMException(ret) return ExecutionEngine(ret, self._module) diff --git a/llvm/extra.cpp b/llvm/extra.cpp index 0b5a874..858fbcf 100644 --- a/llvm/extra.cpp +++ b/llvm/extra.cpp @@ -148,17 +148,15 @@ void LLVMEngineBuilderSetOptLevel(LLVMEngineBuilderRef eb, int level) } -LLVMExecutionEngineRef LLVMEngineBuilderCreate(LLVMEngineBuilderRef eb, char **err) +LLVMExecutionEngineRef LLVMEngineBuilderCreate(LLVMEngineBuilderRef eb, std::string & error) { using namespace llvm; - std::string errstring; LLVMExecutionEngineRef ret; - ret = wrap(unwrap(eb)->setErrorStr(&errstring).create()); + ret = wrap(unwrap(eb)->setErrorStr(&error).create()); - if ( !errstring.empty() ) { - *err = strdup(errstring.c_str()); - return 0; + if ( !error.empty() ) { // error string is set + return NULL; } else { return ret; } @@ -534,8 +532,7 @@ LLVMValueRef LLVMGetIntrinsic(LLVMModuleRef module, int id, return wrap(intfunc); } -LLVMModuleRef LLVMGetModuleFromAssembly(const char *asmtext, unsigned txtlen, - char **out) +LLVMModuleRef LLVMGetModuleFromAssembly(const char *asmtext, char **out) { assert(asmtext); assert(out); diff --git a/llvm/extra.h b/llvm/extra.h index 9c73098..b0bacc8 100644 --- a/llvm/extra.h +++ b/llvm/extra.h @@ -75,7 +75,8 @@ void LLVMEngineBuilderSetOptLevel(LLVMEngineBuilderRef eb, int level); /* * Wraps EngineBuilder::setErrorStr and EngineBuilder::create */ -LLVMExecutionEngineRef LLVMEngineBuilderCreate(LLVMEngineBuilderRef eb, char **err); +LLVMExecutionEngineRef LLVMEngineBuilderCreate(LLVMEngineBuilderRef eb, + std::string &error); /* * Wraps PassManagerBuilder::OptLevel @@ -272,8 +273,7 @@ unsigned LLVMCmpInstGetPredicate(LLVMValueRef cmpinst); /* Wraps llvm::ParseAssemblyString(). Returns a module reference or NULL (with * `out' pointing to an error message). Dispose error message after use, via * LLVMDisposeMessage(). */ -LLVMModuleRef LLVMGetModuleFromAssembly(const char *asmtxt, unsigned txten, - char **out); +LLVMModuleRef LLVMGetModuleFromAssembly(const char *asmtxt, char **out); /* Wraps llvm::ParseBitcodeFile(). Returns a module reference or NULL (with * `out' pointing to an error message). Dispose error message after use, via diff --git a/llvm/passes.py b/llvm/passes.py index 0b64c11..fb5dc7c 100644 --- a/llvm/passes.py +++ b/llvm/passes.py @@ -39,7 +39,7 @@ import llvm # top-level, for common stuff import llvm.ee as ee # target data import llvm.core as core # module, function etc. import llvm._core as _core # C wrappers - +import llvm._util as _util # Utility functions #===----------------------------------------------------------------------=== # Pass manager builder @@ -142,10 +142,10 @@ class PassManager(object): def add(self, tgt_data_or_pass_name): if isinstance(tgt_data_or_pass_name, ee.TargetData): self._add_target_data(tgt_data_or_pass_name) - elif isinstance(tgt_data_or_pass_name, basestring): + elif _util.isstring(tgt_data_or_pass_name): self._add_pass(tgt_data_or_pass_name) else: - raise llvm.LLVMException("invalid pass_id (%s)" % str(tgt_data_or_pass_name)) + raise llvm.LLVMException("invalid pass_id (%s)" % tgt_data_or_pass_name) def _add_target_data(self, tgt): _core.LLVMAddTargetData(tgt.ptr, self.ptr) diff --git a/llvm/wrap.cpp b/llvm/wrap.cpp index 25ea30c..4527912 100644 --- a/llvm/wrap.cpp +++ b/llvm/wrap.cpp @@ -30,37 +30,19 @@ #include "wrap.h" -/* Project-wide setting */ -#if (PY_MAJOR_VERSION >= 3) -#define LLVM_PY_USE_PYCAPSULE -#endif - /*===----------------------------------------------------------------------===*/ /* Helper functions/macros */ /*===----------------------------------------------------------------------===*/ -#ifdef LLVM_PY_USE_PYCAPSULE - #define _define_std_ctor(typ) \ PyObject * ctor_ ## typ ( typ p) \ { \ - if (p) \ + if (p){ \ return PyCapsule_New(p, NULL, NULL); \ + } \ Py_RETURN_NONE; \ } -#else - -#define _define_std_ctor(typ) \ -PyObject * ctor_ ## typ ( typ p) \ -{ \ - if (p) \ - return PyCObject_FromVoidPtr(p, NULL); \ - Py_RETURN_NONE; \ -} - -#endif - /*===----------------------------------------------------------------------===*/ /* Type ctor/dtor */ /*===----------------------------------------------------------------------===*/ @@ -100,11 +82,8 @@ void *get_object_arg(PyObject *args) if (!PyArg_ParseTuple(args, "O", &o)) return NULL; -#ifdef LLVM_PY_USE_PYCAPSULE + return PyCapsule_GetPointer(o, NULL); -#else - return PyCObject_AsVoidPtr(o); -#endif } void **make_array_from_list(PyObject *list, int n) @@ -118,11 +97,7 @@ void **make_array_from_list(PyObject *list, int n) for (i=0; i -#include +// #include //unused? + +//// For pre-2.7 compatbility, use the following include, which provides +//// alias for PyCapsule. +//// See http://docs.python.org/py3k/howto/cporting.html +// #include // pre-2.7 compatibility for PyCapsule + /* llvm includes */ #include "llvm-c/Core.h" @@ -53,19 +59,12 @@ #include "llvm_c_extra.h" -/* Project-wide setting */ -#if PY_MAJOR_VERSION >= 3 -#define LLVM_PY_USE_PYCAPSULE -#endif - /*===----------------------------------------------------------------------===*/ /* Typedefs */ /*===----------------------------------------------------------------------===*/ typedef unsigned long long llvmwrap_ull; - - /*===----------------------------------------------------------------------===*/ /* Type ctor/dtor */ /*===----------------------------------------------------------------------===*/ @@ -140,8 +139,6 @@ PyObject *make_list_from_LLVMValueRef_array(LLVMValueRef *p, unsigned n); */ - -#ifdef LLVM_PY_USE_PYCAPSULE /****************************************************************************** ****************************************************************************** *** PyCapsule Calls @@ -342,14 +339,11 @@ _w ## func (PyObject *self, PyObject *args) \ static PyObject * \ _w ## func (PyObject *self, PyObject *args) \ { \ - PyObject *arg1_; \ const char *arg1; \ \ - if (!PyArg_ParseTuple(args, "U", &arg1_)) \ + if (!PyArg_ParseTuple(args, "s", &arg1)) \ return NULL; \ \ - arg1 = PyUnicode_AS_DATA(arg1_); \ - \ return ctor_ ## outtype ( func (arg1)); \ } @@ -378,7 +372,7 @@ _w ## func (PyObject *self, PyObject *args) \ { \ if (!PyArg_ParseTuple(args, "")) \ return NULL; \ - return PyBytes_FromString(func()); \ + return PyUnicode_FromString(func()); \ } @@ -395,7 +389,7 @@ _w ## func (PyObject *self, PyObject *args) \ if (!(arg1 = ( intype1 )get_object_arg(args))) \ return NULL; \ \ - return PyBytes_FromString( func (arg1)); \ + return PyUnicode_FromString( func (arg1)); \ } /** @@ -423,15 +417,14 @@ _w ## func (PyObject *self, PyObject *args) \ static PyObject * \ _w ## func (PyObject *self, PyObject *args) \ { \ - PyObject *obj1, *arg2_; \ + PyObject *obj1; \ const char *arg2; \ intype1 arg1; \ \ - if (!PyArg_ParseTuple(args, "OU", &obj1, &arg2_)) \ + if (!PyArg_ParseTuple(args, "Os", &obj1, &arg2)) \ return NULL; \ \ arg1 = ( intype1 ) PyCapsule_GetPointer(obj1, NULL);\ - arg2 = PyUnicode_AS_DATA(arg2_); \ \ func (arg1, arg2); \ Py_RETURN_NONE; \ @@ -445,15 +438,14 @@ _w ## func (PyObject *self, PyObject *args) \ static PyObject * \ _w ## func (PyObject *self, PyObject *args) \ { \ - PyObject *obj1, *arg2_; \ + PyObject *obj1; \ const char *arg2; \ intype1 arg1; \ \ - if (!PyArg_ParseTuple(args, "OU", &obj1, &arg2_)) \ + if (!PyArg_ParseTuple(args, "Os", &obj1, &arg2)) \ return NULL; \ \ arg1 = ( intype1 ) PyCapsule_GetPointer(obj1, NULL);\ - arg2 = PyUnicode_AS_DATA(arg2_); \ \ return ctor_ ## outtype ( func (arg1, arg2)); \ } @@ -508,17 +500,16 @@ _w ## func (PyObject *self, PyObject *args) \ static PyObject * \ _w ## func (PyObject *self, PyObject *args) \ { \ - PyObject *obj1, *obj2, *arg3_; \ + PyObject *obj1, *obj2; \ intype1 arg1; \ intype2 arg2; \ const char *arg3; \ \ - if (!PyArg_ParseTuple(args, "OOU", &obj1, &obj2, &arg3_)) \ + if (!PyArg_ParseTuple(args, "OOs", &obj1, &obj2, &arg3)) \ return NULL; \ \ arg1 = ( intype1 ) PyCapsule_GetPointer(obj1, NULL); \ arg2 = ( intype2 ) PyCapsule_GetPointer(obj2, NULL); \ - arg3 = PyUnicode_AS_DATA(arg3_); \ \ return ctor_ ## outtype ( func (arg1, arg2, arg3)); \ } @@ -618,16 +609,15 @@ _w ## func (PyObject *self, PyObject *args) \ static PyObject * \ _w ## func (PyObject *self, PyObject *args) \ { \ - PyObject *obj1, *obj3, *arg2_; \ + PyObject *obj1, *obj3; \ intype1 arg1; \ const char *arg2; \ intype3 arg3; \ \ - if (!PyArg_ParseTuple(args, "OUO", &obj1, &arg2_, &obj3)) \ + if (!PyArg_ParseTuple(args, "OsO", &obj1, &arg2, &obj3)) \ return NULL; \ \ arg1 = ( intype1 ) PyCapsule_GetPointer(obj1, NULL); \ - arg2 = PyUnicode_AS_DATA(arg2_); \ arg3 = ( intype3 ) PyCapsule_GetPointer(obj3, NULL); \ \ return ctor_ ## outtype ( func (arg1, arg2, arg3)); \ @@ -642,18 +632,17 @@ _w ## func (PyObject *self, PyObject *args) \ static PyObject * \ _w ## func (PyObject *self, PyObject *args) \ { \ - PyObject *obj1, *obj2, *arg4_; \ + PyObject *obj1, *obj2; \ intype1 arg1; \ intype2 arg2; \ int arg3; \ const char *arg4; \ \ - if (!PyArg_ParseTuple(args, "OOiU", &obj1, &obj2, &arg3, &arg4_)) \ + if (!PyArg_ParseTuple(args, "OOis", &obj1, &obj2, &arg3, &arg4)) \ return NULL; \ \ arg1 = ( intype1 ) PyCapsule_GetPointer(obj1, NULL); \ arg2 = ( intype2 ) PyCapsule_GetPointer(obj2, NULL); \ - arg4 = PyUnicode_AS_DATA(arg4_); \ \ return ctor_ ## outtype ( func (arg1, arg2, arg3, arg4)) ; \ } @@ -666,19 +655,18 @@ _w ## func (PyObject *self, PyObject *args) \ static PyObject * \ _w ## func (PyObject *self, PyObject *args) \ { \ - PyObject *obj1, *obj2, *obj3, *arg4_; \ + PyObject *obj1, *obj2, *obj3; \ intype1 arg1; \ intype2 arg2; \ intype3 arg3; \ const char *arg4; \ \ - if (!PyArg_ParseTuple(args, "OOOU", &obj1, &obj2, &obj3, &arg4_)) \ + if (!PyArg_ParseTuple(args, "OOOs", &obj1, &obj2, &obj3, &arg4)) \ return NULL; \ \ arg1 = ( intype1 ) PyCapsule_GetPointer(obj1, NULL); \ arg2 = ( intype2 ) PyCapsule_GetPointer(obj2, NULL); \ arg3 = ( intype3 ) PyCapsule_GetPointer(obj3, NULL); \ - arg4 = PyUnicode_AS_DATA(arg4_); \ \ return ctor_ ## outtype ( func (arg1, arg2, arg3, arg4)); \ } @@ -740,21 +728,20 @@ _w ## func (PyObject *self, PyObject *args) static PyObject * \ _w ## func (PyObject *self, PyObject *args) \ { \ - PyObject *obj1, *obj2, *obj3, *obj4, *arg5_; \ + PyObject *obj1, *obj2, *obj3, *obj4; \ intype1 arg1; \ intype2 arg2; \ intype3 arg3; \ intype4 arg4; \ const char *arg5; \ \ - if (!PyArg_ParseTuple(args, "OOOOU", &obj1, &obj2, &obj3, &obj4, &arg5_)) \ + if (!PyArg_ParseTuple(args, "OOOOs", &obj1, &obj2, &obj3, &obj4, &arg5)) \ return NULL; \ \ arg1 = ( intype1 ) PyCapsule_GetPointer(obj1, NULL); \ arg2 = ( intype2 ) PyCapsule_GetPointer(obj2, NULL); \ arg3 = ( intype3 ) PyCapsule_GetPointer(obj3, NULL); \ arg4 = ( intype4 ) PyCapsule_GetPointer(obj4, NULL); \ - arg5 = PyUnicode_AS_DATA(arg5_); \ \ return ctor_ ## outtype ( func (arg1, arg2, arg3, arg4, arg5)); \ } @@ -767,20 +754,19 @@ _w ## func (PyObject *self, PyObject *args) static PyObject * \ _w ## func (PyObject *self, PyObject *args) \ { \ - PyObject *obj1, *obj3, *obj4, *arg5_; \ + PyObject *obj1, *obj3, *obj4; \ intype1 arg1; \ intype2 arg2; \ intype3 arg3; \ intype4 arg4; \ const char *arg5; \ \ - if (!PyArg_ParseTuple(args, "OiOOU", &obj1, &arg2, &obj3, &obj4, &arg5_)) \ + if (!PyArg_ParseTuple(args, "OiOOs", &obj1, &arg2, &obj3, &obj4, &arg5)) \ return NULL; \ \ arg1 = ( intype1 ) PyCapsule_GetPointer(obj1, NULL); \ arg3 = ( intype3 ) PyCapsule_GetPointer(obj3, NULL); \ arg4 = ( intype4 ) PyCapsule_GetPointer(obj4, NULL); \ - arg5 = PyUnicode_AS_DATA(arg5_); \ \ return ctor_ ## outtype ( func (arg1, arg2, arg3, arg4, arg5)); \ } @@ -992,7 +978,7 @@ _w ## func (PyObject *self, PyObject *args) \ static PyObject * \ _w ## func (PyObject *self, PyObject *args) \ { \ - PyObject *obj1, *obj2, *obj3, *arg4_; \ + PyObject *obj1, *obj2, *obj3; \ const char *arg4; \ intype1 arg1; \ intype2 arg2; \ @@ -1000,13 +986,12 @@ _w ## func (PyObject *self, PyObject *args) \ unsigned arg3n; \ outtype ret; \ \ - if (!PyArg_ParseTuple(args, "OOOU", &obj1, &obj2, &obj3, &arg4_)) \ + if (!PyArg_ParseTuple(args, "OOOs", &obj1, &obj2, &obj3, &arg4)) \ return NULL; \ \ arg1 = ( intype1 ) PyCapsule_GetPointer(obj1, NULL); \ arg2 = ( intype2 ) PyCapsule_GetPointer(obj2, NULL); \ arg3n = ( unsigned ) PyList_Size(obj3); \ - arg4 = PyUnicode_AS_DATA(arg4_); \ if (!(arg3v = ( intype3 *)make_array_from_list(obj3, arg3n))) \ return PyErr_NoMemory(); \ \ @@ -1033,892 +1018,11 @@ _w ## func (PyObject *self, PyObject *args) \ return NULL; \ \ val = func (arg1); \ - ret = PyBytes_FromString(val); \ + ret = PyUnicode_FromString(val); \ LLVMDisposeMessage(val); \ return ret; \ } -#else -/****************************************************************************** - ****************************************************************************** - *** PyCObject calls - ****************************************************************************** - *****************************************************************************/ -/** - * 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(enum_intype1 arg1, intype2 arg2, intype3 arg3) - */ -#define _wrap_enumobjobj2obj(func, intype1, intype2, intype3, outtype) \ -static PyObject * \ -_w ## func (PyObject *self, PyObject *args) \ -{ \ - intype1 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() - */ - #define _wrap_none2str(func) \ -static PyObject * \ -_w ## func (PyObject *self, PyObject *args) \ -{ \ - if (!PyArg_ParseTuple(args, "")) \ - return NULL; \ - return PyBytes_FromString(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 PyBytes_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 - * void func(intype1 arg1, enum_intype2 arg2) - */ -#define _wrap_objenum2none(func, intype1, intype2) \ -static PyObject * \ -_w ## func (PyObject *self, PyObject *args) \ -{ \ - PyObject *obj1; \ - intype1 arg1; \ - intype2 arg2; \ - \ - 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 - * outtype func(intype1 arg1, intype2 arg2, int arg3) - */ -#define _wrap_objobjint2obj(func, intype1, intype2, outtype) \ -static PyObject * \ -_w ## func (PyObject *self, PyObject *args) \ -{ \ - PyObject *obj1, *obj2; \ - intype1 arg1; \ - intype2 arg2; \ - int arg3; \ - \ - if (!PyArg_ParseTuple(args, "OOi", &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 - * outtype func(intype1 arg1, intype2 arg2, unsigned long long arg3) - */ -#define _wrap_objobjull2obj(func, intype1, intype2, outtype) \ -static PyObject * \ -_w ## func (PyObject *self, PyObject *args) \ -{ \ - PyObject *obj1, *obj2; \ - intype1 arg1; \ - intype2 arg2; \ - unsigned long long arg3; \ - \ - if (!PyArg_ParseTuple(args, "OOK", &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 - * void func(intype1 arg1, arg2, enum_intype3 arg3) - */ -#define _wrap_objintenum2none(func, intype1, intype3) \ -static PyObject * \ -_w ## func (PyObject *self, PyObject *args) \ -{ \ - PyObject *obj1; \ - intype1 arg1; \ - int arg2; \ - intype3 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, enum_intype2 arg2, intype3 arg3, intype4 arg4, const char *arg5) - */ -#define _wrap_objenumobjobjstr2obj(func, intype1, intype2, intype3, intype4, outtype) \ -static PyObject * \ -_w ## func (PyObject *self, PyObject *args) \ -{ \ - PyObject *obj1, *obj3, *obj4; \ - intype1 arg1; \ - intype2 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 - * void func(intype1 arg1, intype2 *arg2v, unsigned arg2n, int arg3) - * where arg2v is an array of intype2 elements, arg2n in length. - */ -#define _wrap_objlistint2none(func, intype1, intype2) \ -static PyObject * \ -_w ## func (PyObject *self, PyObject *args) \ -{ \ - PyObject *obj1, *obj2; \ - intype1 arg1; \ - intype2 *arg2v; \ - unsigned arg2n; \ - int arg3; \ - \ - 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(); \ - \ - func (arg1, arg2v, arg2n, arg3); \ - free(arg2v); \ - Py_RETURN_NONE; \ -} - - -/** - * Wrap LLVM functions of the type - * outtype func(intype1 arg1, int arg2, intype3 *arg3v, unsigned arg3n) - * where arg3v is an array of intype3 elements, arg3n in length. - */ -#define _wrap_objintlist2obj(func, intype1, intype3, outtype) \ -static PyObject * \ -_w ## func (PyObject *self, PyObject *args) \ -{ \ - PyObject *obj1, *obj3; \ - intype1 arg1; \ - int arg2; \ - intype3 *arg3v; \ - unsigned arg3n; \ - outtype ret; \ - \ - if (!PyArg_ParseTuple(args, "OiO", &obj1, &arg2, &obj3)) \ - return NULL; \ - \ - arg1 = ( intype1 ) PyCObject_AsVoidPtr(obj1); \ - 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) - * 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 = PyBytes_FromString(val); \ - LLVMDisposeMessage(val); \ - return ret; \ -} - -#endif /* LLVM_PY_USE_PYCAPSULE */ #endif /* LLVM_PY_WRAP_H */ diff --git a/setup-win32.py b/setup-win32.py index 46cc69c..fc066d8 100644 --- a/setup-win32.py +++ b/setup-win32.py @@ -1,21 +1,21 @@ #!/usr/bin/env python -# +# # Copyright (c) 2008, Mahadevan R All rights reserved. -# +# # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: -# +# # * Redistributions of source code must retain the above copyright notice, # this list of conditions and the following disclaimer. -# +# # * Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. -# -# * Neither the name of this software, nor the names of its +# +# * Neither the name of this software, nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. -# +# # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR @@ -123,7 +123,7 @@ def call_setup(llvm_dir, llvm_build_dir): ext_core = Extension( 'llvm._core', - ['llvm/_core.c', 'llvm/wrap.c', 'llvm/extra.cpp'], + ['llvm/_core.cpp', 'llvm/wrap.cpp', 'llvm/extra.cpp'], include_dirs = incdirs, library_dirs = [libdir], libraries = std_libs + libs_core, diff --git a/test/asm.py b/test/asm.py index dd564ae..ed003c1 100755 --- a/test/asm.py +++ b/test/asm.py @@ -25,5 +25,25 @@ class TestAsm(unittest.TestCase): self.assertEqual(str(m2).strip(), asm.strip()) + + def test_bitcode(self): + # create a module + m = Module.new('module1') + m.add_global_variable(Type.int(), 'i') + + # write it's assembly representation to a file + asm = str(m) + + with open("/tmp/testasm.bc", "wb") as fout: + m.to_bitcode(fout) + + # read it back into a module + with open("/tmp/testasm.bc", "rb") as fin: + m2 = Module.from_bitcode(fin) + # The default `m.id` is ''. + m2.id = m.id # Copy the name from `m` + + self.assertEqual(str(m2).strip(), asm.strip()) + if __name__ == '__main__': unittest.main() diff --git a/test/constants.py b/test/constants.py index 4a2e543..c8e4000 100755 --- a/test/constants.py +++ b/test/constants.py @@ -193,7 +193,7 @@ class TestConstants(unittest.TestCase): # check result for result, golden in zip(results, values): - self.assertEqual(result, golden) + self.assertEqual(result.decode(), golden) def test_const_struct(self): from random import randint, random @@ -201,7 +201,7 @@ class TestConstants(unittest.TestCase): (0, 0., 0.), (-1, -1., -1.), (1, 1., 1.), - ] + [(randint(0, 0xffffffff), random(), random()) for _ in xrange(100)] + ] + [(randint(0, 0xffffffff), random(), random()) for _ in range(100)] struct_type = Type.struct([Type.int(), Type.float(), Type.double()]) @@ -260,7 +260,7 @@ class TestConstants(unittest.TestCase): (0, 0, 0, 0), (1, 1, 1, 1), (-1, -1, -1, -1), - ] + [ (randgen(), randgen(), randgen(), randgen()) for _ in xrange(100) ] + ] + [ (randgen(), randgen(), randgen(), randgen()) for _ in range(100) ] def map_constant(packed_values): consts = [ Constant.int(Type.int(), i) for i in packed_values ] diff --git a/test/testattrs.py b/test/testattrs.py index 09deced..8801f62 100755 --- a/test/testattrs.py +++ b/test/testattrs.py @@ -10,7 +10,7 @@ except ImportError: import unittest def make_module(): - test_module = u""" + test_module = """ define i32 @sum(i32, i32) { entry: %2 = add i32 %0, %1