Added llvm.core.load_library_permanently() (Issue #12).

git-svn-id: http://llvm-py.googlecode.com/svn/trunk@46 8d1e9007-1d4e-0410-b67e-1979fd6579aa
This commit is contained in:
mdevan.foobar 2008-11-14 18:38:19 +00:00
commit b0a11dbbbc
6 changed files with 77 additions and 0 deletions

View file

@ -1,6 +1,7 @@
0.4, in progress:
* Added llvm.core.load_library_permanently() (Issue #12).
* Fix comparison using != (Issue #11).
* Instruction.is_terminator added.
* Fix Builder.select (Paulo Silva).

View file

@ -917,9 +917,37 @@ _wLLVMGenericValueToFloat(PyObject *self, PyObject *args)
_wrap_obj2none(LLVMDisposeGenericValue, LLVMGenericValueRef)
/*===----------------------------------------------------------------------===*/
/* Misc */
/*===----------------------------------------------------------------------===*/
_wrap_objintlist2obj(LLVMGetIntrinsic, LLVMModuleRef, LLVMTypeRef,
LLVMValueRef)
static PyObject *
_wLLVMLoadLibraryPermanently(PyObject *self, PyObject *args)
{
const char *filename;
char *outmsg;
PyObject *ret;
if (!PyArg_ParseTuple(args, "s", &filename))
return NULL;
outmsg = 0;
if (!LLVMLoadLibraryPermanently(filename, &outmsg)) {
if (outmsg) {
ret = PyString_FromString(outmsg);
LLVMDisposeMessage(outmsg);
return ret;
}
}
/* note: success => None, failure => string with error message */
Py_RETURN_NONE;
}
/*===----------------------------------------------------------------------===*/
/* Python member method table */
@ -1359,7 +1387,9 @@ static PyMethodDef core_methods[] = {
_method( LLVMGenericValueToFloat )
_method( LLVMDisposeGenericValue )
/* Misc */
_method( LLVMGetIntrinsic )
_method( LLVMLoadLibraryPermanently )
{ NULL }
};

View file

@ -2224,3 +2224,19 @@ class MemoryBuffer(object):
def __del__(self):
_core.LLVMDisposeMemoryBuffer(self.ptr)
#===----------------------------------------------------------------------===
# Misc
#===----------------------------------------------------------------------===
def load_library_permanently(filename):
"""Load a shared library.
Load the given shared library (filename argument specifies the full
path of the .so file) using LLVM. Symbols from these are available
from the execution engine thereafter."""
ret = _core.LLVMLoadLibraryPermanently(filename)
if isinstance(ret, str):
raise llvm.LLVMException, ret

View file

@ -21,6 +21,7 @@
#include "llvm/IntrinsicInst.h"
#include "llvm/Analysis/Verifier.h"
#include "llvm/Assembly/Parser.h"
#include "llvm/System/DynamicLibrary.h"
// +includes for passes
#include "llvm/PassManager.h"
#include "llvm/Analysis/LoopPass.h"
@ -198,6 +199,20 @@ unsigned char *LLVMGetBitcodeFromModule(LLVMModuleRef M, unsigned *Len)
return bytes;
}
/* Return 0 on failure (with ErrMsg filled in), 1 on success. */
unsigned LLVMLoadLibraryPermanently(const char* filename, char **ErrMsg)
{
std::string msg;
/* Note: the LLVM API returns true on failure. Don't ask why. */
if (sys::DynamicLibrary::LoadLibraryPermanently(filename, &msg)) {
*ErrMsg = strdup(msg.c_str());
return 0;
}
return 1;
}
/* passes */
#define define_pass(P) \

View file

@ -83,6 +83,8 @@ LLVMModuleRef LLVMGetModuleFromBitcode(const char *BC, unsigned Len,
char **OutMessage);
unsigned char *LLVMGetBitcodeFromModule(LLVMModuleRef M, unsigned *Len);
unsigned LLVMLoadLibraryPermanently(const char* filename, char **ErrMsg);
/* passes */
#define declare_pass(P) \

View file

@ -29,10 +29,23 @@ def do_ownable():
pass
def do_misc():
print " Testing miscellaneous functions"
try:
load_library_permanently("/usr/lib/libm.so")
except LLVMException:
pass
try:
load_library_permanently("no*such*so")
except LLVMException:
pass
def do_llvm():
print " Testing module llvm"
do_llvmexception()
do_ownable()
do_misc()
def do_module():