Added GenericValue. EE.run() uses GVs now. Converted line endings in wrap.{c,h}.
git-svn-id: http://llvm-py.googlecode.com/svn/trunk@19 8d1e9007-1d4e-0410-b67e-1979fd6579aa
This commit is contained in:
parent
0a8dc6a115
commit
01eb4a7133
5 changed files with 1051 additions and 942 deletions
126
llvm/_core.c
126
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 }
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
||||
|
||||
#===----------------------------------------------------------------------===
|
||||
|
|
|
|||
65
llvm/ee.py
65
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):
|
||||
|
|
|
|||
213
llvm/wrap.c
213
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<n; i++) {
|
||||
PyObject *e = PyList_GetItem(list, i);
|
||||
arr[i] = PyCObject_AsVoidPtr(e);
|
||||
}
|
||||
|
||||
return arr;
|
||||
}
|
||||
|
||||
PyObject *make_list_from_LLVMTypeRef_array(LLVMTypeRef *p, unsigned n)
|
||||
{
|
||||
int i;
|
||||
PyObject *list = PyList_New(n);
|
||||
|
||||
if (!list)
|
||||
return NULL;
|
||||
|
||||
for (i=0; i<n; i++) {
|
||||
PyObject *elem = ctor_LLVMTypeRef(p[i]);
|
||||
PyList_SetItem(list, i, elem);
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
#include "wrap.h"
|
||||
|
||||
|
||||
/*===----------------------------------------------------------------------===*/
|
||||
/* Helper functions/macros */
|
||||
/*===----------------------------------------------------------------------===*/
|
||||
|
||||
#define _define_std_ctor(typ) \
|
||||
PyObject * ctor_ ## typ ( typ p) \
|
||||
{ \
|
||||
if (p) \
|
||||
return PyCObject_FromVoidPtr(p, NULL); \
|
||||
Py_RETURN_NONE; \
|
||||
}
|
||||
|
||||
|
||||
/*===----------------------------------------------------------------------===*/
|
||||
/* Type ctor/dtor */
|
||||
/*===----------------------------------------------------------------------===*/
|
||||
|
||||
_define_std_ctor(LLVMModuleRef)
|
||||
_define_std_ctor(LLVMTypeRef)
|
||||
_define_std_ctor(LLVMValueRef)
|
||||
_define_std_ctor(LLVMTypeHandleRef)
|
||||
_define_std_ctor(LLVMBasicBlockRef)
|
||||
_define_std_ctor(LLVMBuilderRef)
|
||||
_define_std_ctor(LLVMModuleProviderRef)
|
||||
_define_std_ctor(LLVMMemoryBufferRef)
|
||||
_define_std_ctor(LLVMPassManagerRef)
|
||||
_define_std_ctor(LLVMExecutionEngineRef)
|
||||
_define_std_ctor(LLVMTargetDataRef)
|
||||
_define_std_ctor(LLVMGenericValueRef)
|
||||
|
||||
PyObject *ctor_int(int i)
|
||||
{
|
||||
return PyInt_FromLong(i);
|
||||
}
|
||||
|
||||
|
||||
/*===----------------------------------------------------------------------===*/
|
||||
/* 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<n; i++) {
|
||||
PyObject *e = PyList_GetItem(list, i);
|
||||
arr[i] = PyCObject_AsVoidPtr(e);
|
||||
}
|
||||
|
||||
return arr;
|
||||
}
|
||||
|
||||
PyObject *make_list_from_LLVMTypeRef_array(LLVMTypeRef *p, unsigned n)
|
||||
{
|
||||
int i;
|
||||
PyObject *list = PyList_New(n);
|
||||
|
||||
if (!list)
|
||||
return NULL;
|
||||
|
||||
for (i=0; i<n; i++) {
|
||||
PyObject *elem = ctor_LLVMTypeRef(p[i]);
|
||||
PyList_SetItem(list, i, elem);
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
|
|
|
|||
1563
llvm/wrap.h
1563
llvm/wrap.h
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue