From 2780d9fc1018cb330a14da3027150c3fa9099427 Mon Sep 17 00:00:00 2001 From: "mdevan.foobar" Date: Tue, 16 Sep 2008 17:03:21 +0000 Subject: [PATCH] Added pointer_size and get_or_insert_function to Module git-svn-id: http://llvm-py.googlecode.com/svn/trunk@43 8d1e9007-1d4e-0410-b67e-1979fd6579aa --- llvm/_core.c | 5 +++++ llvm/core.py | 21 ++++++++++++++++++++- llvm/extra.cpp | 18 ++++++++++++++++++ llvm/extra.h | 6 ++++++ test/testall.py | 4 ++++ 5 files changed, 53 insertions(+), 1 deletion(-) diff --git a/llvm/_core.c b/llvm/_core.c index dbee89c..1fdfaa7 100644 --- a/llvm/_core.c +++ b/llvm/_core.c @@ -38,6 +38,9 @@ _wrap_objstr2none(LLVMDeleteTypeName, LLVMModuleRef) _wrap_obj2none(LLVMDumpModule, LLVMModuleRef) _wrap_obj2none(LLVMDisposeModule, LLVMModuleRef) _wrap_dumper(LLVMDumpModuleToString, LLVMModuleRef) +_wrap_obj2obj(LLVMModuleGetPointerSize, LLVMModuleRef, int) +_wrap_objstrobj2obj(LLVMModuleGetOrInsertFunction, LLVMModuleRef, + LLVMTypeRef, LLVMValueRef) static PyObject * _wLLVMVerifyModule(PyObject *self, PyObject *args) @@ -942,6 +945,8 @@ static PyMethodDef core_methods[] = { _method( LLVMGetModuleFromAssembly ) _method( LLVMGetModuleFromBitcode ) _method( LLVMGetBitcodeFromModule ) + _method( LLVMModuleGetPointerSize ) + _method( LLVMModuleGetOrInsertFunction ) /* Types */ diff --git a/llvm/core.py b/llvm/core.py index 55c19c0..8b7e739 100644 --- a/llvm/core.py +++ b/llvm/core.py @@ -822,6 +822,14 @@ class Module(llvm.Ownable): """ ) + @property + def pointer_size(self): + """Pointer size of target platform. + + Can be 0, 32 or 64. Zero represents + llvm::Module::AnyPointerSize.""" + return _core.LLVMModuleGetPointerSize(self.ptr) + def add_type_name(self, name, ty): """Map a string to a type. @@ -868,6 +876,9 @@ class Module(llvm.Ownable): """Return a Function object representing function with given name.""" return Function.get(self, name) + def get_or_insert_function(self, ty, name): + return Function.get_or_insert(self, ty, name) + @property def functions(self): """All functions in this module. @@ -1603,7 +1614,15 @@ class Function(GlobalValue): def new(module, func_ty, name): check_is_module(module) check_is_type(func_ty) - return Function(_core.LLVMAddFunction(module.ptr, name, func_ty.ptr), module) + return Function(_core.LLVMAddFunction(module.ptr, name, \ + func_ty.ptr), module) + + @staticmethod + def get_or_insert(module, func_ty, name): + check_is_module(module) + check_is_type(func_ty) + return Function(_core.LLVMModuleGetOrInsertFunction(module.ptr, \ + name, func_ty.ptr), module) @staticmethod def get(module, name): diff --git a/llvm/extra.cpp b/llvm/extra.cpp index fecc8e6..26da903 100644 --- a/llvm/extra.cpp +++ b/llvm/extra.cpp @@ -84,6 +84,24 @@ LLVMValueRef LLVMBuildVFCmp(LLVMBuilderRef B, LLVMRealPredicate Op, } #endif +unsigned LLVMModuleGetPointerSize(LLVMModuleRef M) +{ + Module::PointerSize p = unwrap(M)->getPointerSize(); + if (p == Module::Pointer32) + return 32; + else if (p == Module::Pointer64) + return 64; + return 0; +} + +LLVMValueRef LLVMModuleGetOrInsertFunction(LLVMModuleRef M, + const char *Name, LLVMTypeRef FunctionTy) +{ + FunctionType *ft = unwrap(FunctionTy); + Constant *f = unwrap(M)->getOrInsertFunction(Name, ft); + return wrap(f); +} + #define inst_checkfn(ourfn, llvmfn) \ unsigned ourfn (LLVMValueRef I) { \ return unwrap(I)-> llvmfn () ? 1 : 0; \ diff --git a/llvm/extra.h b/llvm/extra.h index 74c8b1a..41f8fe1 100644 --- a/llvm/extra.h +++ b/llvm/extra.h @@ -51,6 +51,12 @@ LLVMValueRef LLVMBuildGetResult(LLVMBuilderRef, LLVMValueRef V, LLVMValueRef LLVMGetIntrinsic(LLVMModuleRef B, int ID, LLVMTypeRef *Types, unsigned Count); +/* module */ + +unsigned LLVMModuleGetPointerSize(LLVMModuleRef M); +LLVMValueRef LLVMModuleGetOrInsertFunction(LLVMModuleRef M, + const char *Name, LLVMTypeRef FunctionTy); + /* instruction */ unsigned LLVMInstIsTerminator(LLVMValueRef I); diff --git a/test/testall.py b/test/testall.py index fc576c4..a27b68b 100644 --- a/test/testall.py +++ b/test/testall.py @@ -52,6 +52,9 @@ def do_module(): ft = Type.function(ti, [ti]) m.add_function(ft, "func") m.get_function_named("func") + m.get_or_insert_function(ft, "func") + m.get_or_insert_function(Type.function(ti, []), "func") + m.get_or_insert_function(ft, "func2") fns = list(m.functions) try: m.verify() @@ -78,6 +81,7 @@ def do_module(): ss2.write(str(m)) m4 = Module.from_assembly(ss2) t = m4 == m + t = m4.pointer_size def do_type():