Added API for reading assembly (.ll) files

git-svn-id: http://llvm-py.googlecode.com/svn/trunk@35 8d1e9007-1d4e-0410-b67e-1979fd6579aa
This commit is contained in:
mdevan.foobar 2008-09-08 13:09:43 +00:00
commit c10b1b58c3
10 changed files with 82 additions and 18 deletions

View file

@ -62,8 +62,11 @@ _wLLVMVerifyModule(PyObject *self, PyObject *args)
return ret;
}
typedef LLVMModuleRef (*asm_or_bc_fn_t)(const char *A, unsigned Len,
char **OutMessage);
static PyObject *
_wLLVMGetModuleFromBitcode(PyObject *self, PyObject *args)
_get_asm_or_bc(asm_or_bc_fn_t fn, PyObject *self, PyObject *args)
{
PyObject *obj, *ret;
Py_ssize_t len;
@ -77,7 +80,7 @@ _wLLVMGetModuleFromBitcode(PyObject *self, PyObject *args)
len = PyString_Size(obj);
outmsg = 0;
m = LLVMGetModuleFromBitcode(start, len, &outmsg);
m = fn(start, len, &outmsg);
if (!m) {
if (outmsg) {
ret = PyString_FromString(outmsg);
@ -91,6 +94,18 @@ _wLLVMGetModuleFromBitcode(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)
{
@ -910,6 +925,7 @@ static PyMethodDef core_methods[] = {
_method( LLVMDisposeModule )
_method( LLVMDumpModuleToString )
_method( LLVMVerifyModule )
_method( LLVMGetModuleFromAssembly )
_method( LLVMGetModuleFromBitcode )
_method( LLVMGetBitcodeFromModule )

View file

@ -751,6 +751,21 @@ class Module(llvm.Ownable):
else:
return Module(ret)
@staticmethod
def from_assembly(fileobj):
"""Create a Module instance from the contents of an LLVM
assembly (.ll) file."""
data = fileobj.read()
ret = _core.LLVMGetModuleFromAssembly(data)
if not ret:
raise llvm.LLVMException, \
"Unable to create module from assembly"
elif isinstance(ret, str):
raise llvm.LLVMException, ret
else:
return Module(ret)
def __init__(self, ptr):
"""DO NOT CALL DIRECTLY.

View file

@ -20,6 +20,7 @@
#include "llvm/Support/CallSite.h"
#include "llvm/IntrinsicInst.h"
#include "llvm/Analysis/Verifier.h"
#include "llvm/Assembly/Parser.h"
// +includes for passes
#include "llvm/PassManager.h"
#include "llvm/Analysis/LoopPass.h"
@ -110,6 +111,20 @@ LLVMValueRef LLVMGetIntrinsic(LLVMModuleRef M, int ID,
return wrap(intfunc);
}
LLVMModuleRef LLVMGetModuleFromAssembly(const char *A, unsigned Len,
char **OutMessage)
{
ParseError e;
Module *MP = ParseAssemblyString(A, NULL /*unused within!*/, &e);
if (!MP) {
*OutMessage = strdup(e.getMessage().c_str());
return NULL;
}
return wrap(MP);
}
LLVMModuleRef LLVMGetModuleFromBitcode(const char *BC, unsigned Len,
char **OutMessage)
{

View file

@ -51,6 +51,11 @@ LLVMValueRef LLVMBuildGetResult(LLVMBuilderRef, LLVMValueRef V,
LLVMValueRef LLVMGetIntrinsic(LLVMModuleRef B, int ID,
LLVMTypeRef *Types, unsigned Count);
/* reading llvm "assembly" */
LLVMModuleRef LLVMGetModuleFromAssembly(const char *A, unsigned Len,
char **OutMessage);
/* bitcode related */
LLVMModuleRef LLVMGetModuleFromBitcode(const char *BC, unsigned Len,