Enable BasicBlock.function API

git-svn-id: http://llvm-py.googlecode.com/svn/trunk@63 8d1e9007-1d4e-0410-b67e-1979fd6579aa
This commit is contained in:
mdevan.foobar 2009-02-21 16:40:01 +00:00
commit a339b69193
3 changed files with 36 additions and 6 deletions

View file

@ -1001,6 +1001,19 @@ _wLLVMLoadLibraryPermanently(PyObject *self, PyObject *args)
Py_RETURN_NONE; Py_RETURN_NONE;
} }
/* Expose the void* inside a PyCObject as a PyLong. This allows us to
* use it as a unique ID. */
static PyObject *
_wPyCObjectVoidPtrToPyLong(PyObject *self, PyObject *args)
{
void *p;
if (!(p = get_object_arg(args)))
return NULL;
return PyLong_FromVoidPtr(p);
}
/*===----------------------------------------------------------------------===*/ /*===----------------------------------------------------------------------===*/
/* Python member method table */ /* Python member method table */
@ -1441,6 +1454,7 @@ static PyMethodDef core_methods[] = {
/* Misc */ /* Misc */
_method( LLVMGetIntrinsic ) _method( LLVMGetIntrinsic )
_method( LLVMLoadLibraryPermanently ) _method( LLVMLoadLibraryPermanently )
_method( PyCObjectVoidPtrToPyLong )
{ NULL } { NULL }
}; };

View file

@ -37,6 +37,7 @@ in-memory intermediate representation (IR) data structures."""
import llvm # top-level, for common stuff import llvm # top-level, for common stuff
import llvm._core as _core # C wrappers import llvm._core as _core # C wrappers
from llvm._util import * # utility functions from llvm._util import * # utility functions
import weakref # global list of modules
#===----------------------------------------------------------------------=== #===----------------------------------------------------------------------===
@ -802,6 +803,17 @@ def _to_int(v):
else: else:
return 0 return 0
__all_modules = weakref.WeakValueDictionary()
def _report_new_module(ptr, obj):
__all_modules[_core.PyCObjectVoidPtrToPyLong(ptr)] = obj
def _module_from_ptr(ptr):
i = _core.PyCObjectVoidPtrToPyLong(ptr)
if i not in __all_modules:
raise llvm.LLVMException, "module not found in internal list"
return __all_modules[i]
#===----------------------------------------------------------------------=== #===----------------------------------------------------------------------===
# Module # Module
@ -865,6 +877,7 @@ class Module(llvm.Ownable):
Use the static method `Module.new' instead. Use the static method `Module.new' instead.
""" """
llvm.Ownable.__init__(self, ptr, _core.LLVMDisposeModule) llvm.Ownable.__init__(self, ptr, _core.LLVMDisposeModule)
_report_new_module(ptr, self)
def __str__(self): def __str__(self):
"""Text representation of a module. """Text representation of a module.
@ -1937,10 +1950,12 @@ class BasicBlock(Value):
_core.LLVMDeleteBasicBlock(self.ptr) _core.LLVMDeleteBasicBlock(self.ptr)
self.ptr = None self.ptr = None
#-- disabled till we find a proper way to do it -- @property
#@property def function(self):
#def function(self): func_ptr = _core.LLVMGetBasicBlockParent(self.ptr)
# return Function(_core.LLVMGetBasicBlockParent(self.ptr)) module_ptr = _core.LLVMGetGlobalParent(func_ptr)
module_obj = _module_from_ptr(module_ptr)
return Function(func_ptr, module_obj)
@property @property
def instructions(self): def instructions(self):

View file

@ -349,8 +349,9 @@ def do_basicblock():
s = list(b.instructions) s = list(b.instructions)
b2 = b.insert_before('before') b2 = b.insert_before('before')
b2.delete() b2.delete()
# ff = b.function ff = b.function
# ^ not working yet! m2 = ff.module
t = m == m2
def _do_builder_mrv(): def _do_builder_mrv():