Add ExecutionEngine.add_global_mapping
This commit is contained in:
parent
457cd24986
commit
9571aefbe0
3 changed files with 53 additions and 0 deletions
|
|
@ -1342,6 +1342,30 @@ _wLLVMGetPointerToGlobal(PyObject *self, PyObject *args)
|
||||||
LLVMPY_CATCH_ALL
|
LLVMPY_CATCH_ALL
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static PyObject *
|
||||||
|
_wLLVMAddGlobalMapping(PyObject *self, PyObject *args)
|
||||||
|
{
|
||||||
|
LLVMPY_TRY
|
||||||
|
PyObject *obj_ee;
|
||||||
|
PyObject *obj_val;
|
||||||
|
long int address;
|
||||||
|
|
||||||
|
if (!PyArg_ParseTuple(args, "OOl", &obj_ee, &obj_val, &address))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
const LLVMExecutionEngineRef ee = pycap_get<LLVMExecutionEngineRef>( obj_ee );
|
||||||
|
const LLVMValueRef val = pycap_get<LLVMValueRef>( obj_val );
|
||||||
|
|
||||||
|
if (address == (unsigned long long)-1)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
LLVMAddGlobalMapping(ee, val, (void*) address);
|
||||||
|
|
||||||
|
Py_RETURN_NONE;
|
||||||
|
|
||||||
|
LLVMPY_CATCH_ALL
|
||||||
|
}
|
||||||
|
|
||||||
/* the args should have been ptr, num */
|
/* the args should have been ptr, num */
|
||||||
LLVMGenericValueRef LLVMRunFunction2(LLVMExecutionEngineRef EE,
|
LLVMGenericValueRef LLVMRunFunction2(LLVMExecutionEngineRef EE,
|
||||||
LLVMValueRef F, LLVMGenericValueRef *Args, unsigned NumArgs)
|
LLVMValueRef F, LLVMGenericValueRef *Args, unsigned NumArgs)
|
||||||
|
|
@ -2096,6 +2120,7 @@ static PyMethodDef core_methods[] = {
|
||||||
_method( LLVMRunFunction2 )
|
_method( LLVMRunFunction2 )
|
||||||
_method( LLVMGetPointerToFunction )
|
_method( LLVMGetPointerToFunction )
|
||||||
_method( LLVMGetPointerToGlobal )
|
_method( LLVMGetPointerToGlobal )
|
||||||
|
_method( LLVMAddGlobalMapping )
|
||||||
_method( LLVMGetExecutionEngineTargetData )
|
_method( LLVMGetExecutionEngineTargetData )
|
||||||
_method( LLVMRunStaticConstructors )
|
_method( LLVMRunStaticConstructors )
|
||||||
_method( LLVMRunStaticDestructors )
|
_method( LLVMRunStaticDestructors )
|
||||||
|
|
|
||||||
|
|
@ -230,6 +230,10 @@ class ExecutionEngine(llvm.Handle):
|
||||||
core.check_is_global_value(val)
|
core.check_is_global_value(val)
|
||||||
return _core.LLVMGetPointerToGlobal(self.ptr, val.ptr)
|
return _core.LLVMGetPointerToGlobal(self.ptr, val.ptr)
|
||||||
|
|
||||||
|
def add_global_mapping(self, gvar, addr):
|
||||||
|
assert addr >= 0, "Address cannot not be negative"
|
||||||
|
_core.LLVMAddGlobalMapping(self.ptr, gvar.ptr, addr)
|
||||||
|
|
||||||
def run_static_ctors(self):
|
def run_static_ctors(self):
|
||||||
_core.LLVMRunStaticConstructors(self.ptr)
|
_core.LLVMRunStaticConstructors(self.ptr)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -464,6 +464,30 @@ class TestExecutionEngine(unittest.TestCase):
|
||||||
casted = cast(c_void_p(ptr), POINTER(c_int))
|
casted = cast(c_void_p(ptr), POINTER(c_int))
|
||||||
self.assertEqual(X, casted[0])
|
self.assertEqual(X, casted[0])
|
||||||
|
|
||||||
|
def test_add_global_mapping(self):
|
||||||
|
module = lc.Module.new(str(self))
|
||||||
|
gvar = module.add_global_variable(Type.int(), 'hello')
|
||||||
|
|
||||||
|
fnty = lc.Type.function(Type.int(), [])
|
||||||
|
foo = module.add_function(fnty, name='foo')
|
||||||
|
bldr = lc.Builder.new(foo.append_basic_block('entry'))
|
||||||
|
bldr.ret(bldr.load(gvar))
|
||||||
|
|
||||||
|
ee = le.ExecutionEngine.new(module)
|
||||||
|
from ctypes import c_int, addressof, CFUNCTYPE
|
||||||
|
value = 0xABCD
|
||||||
|
value_ctype = c_int(value)
|
||||||
|
value_pointer = addressof(value_ctype)
|
||||||
|
|
||||||
|
ee.add_global_mapping(gvar, value_pointer)
|
||||||
|
|
||||||
|
foo_addr = ee.get_pointer_to_function(foo)
|
||||||
|
prototype = CFUNCTYPE(c_int)
|
||||||
|
foo_callable = prototype(foo_addr)
|
||||||
|
self.assertEqual(foo_callable(), value)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
tests.append(TestExecutionEngine)
|
tests.append(TestExecutionEngine)
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue