Python3 ready. Py2.7 and Py3 working. Drop PyCObject for all.
Pre Py2.7 support should be possible by including "capsulethunk.h". (See llvm/wrap.h)
This commit is contained in:
parent
0ca676eb8c
commit
9aadd489c3
14 changed files with 163 additions and 1142 deletions
|
|
@ -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)
|
||||
|
||||
|
|
|
|||
212
llvm/_core.cpp
212
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,
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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<n; i++) {
|
||||
PyObject *e = PyList_GetItem(list, i);
|
||||
#ifdef LLVM_PY_USE_PYCAPSULE
|
||||
arr[i] = PyCapsule_GetPointer(e, NULL);
|
||||
#else
|
||||
arr[i] = PyCObject_AsVoidPtr(e);
|
||||
#endif
|
||||
}
|
||||
|
||||
return arr;
|
||||
|
|
|
|||
954
llvm/wrap.h
954
llvm/wrap.h
File diff suppressed because it is too large
Load diff
|
|
@ -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,
|
||||
|
|
|
|||
20
test/asm.py
20
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 '<string>'.
|
||||
m2.id = m.id # Copy the name from `m`
|
||||
|
||||
self.assertEqual(str(m2).strip(), asm.strip())
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
|
|
|||
|
|
@ -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 ]
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue