From 37a8807323672ab0bb366272e5a798b8e63752a4 Mon Sep 17 00:00:00 2001 From: Michael Gottesman Date: Wed, 11 Sep 2013 00:23:14 +0000 Subject: [PATCH] [python-bindings] Added code for loading a module from bitcode, getset its datalayout, getset its target, dump it, print it to a file. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@190458 91177308-0d34-0410-b5e6-96231b3b80d8 --- bindings/python/llvm/bit_reader.py | 31 +++++++++++ bindings/python/llvm/core.py | 67 ++++++++++++++++++++++++ bindings/python/llvm/tests/base.py | 3 ++ bindings/python/llvm/tests/test.bc | Bin 0 -> 1344 bytes bindings/python/llvm/tests/test_core.py | 27 ++++++++++ 5 files changed, 128 insertions(+) create mode 100644 bindings/python/llvm/bit_reader.py create mode 100644 bindings/python/llvm/tests/test.bc diff --git a/bindings/python/llvm/bit_reader.py b/bindings/python/llvm/bit_reader.py new file mode 100644 index 00000000000..5bf5e22025a --- /dev/null +++ b/bindings/python/llvm/bit_reader.py @@ -0,0 +1,31 @@ + +from .common import LLVMObject +from .common import c_object_p +from .common import get_library +from . import enumerations +from .core import MemoryBuffer +from .core import Module +from .core import OpCode +from ctypes import POINTER +from ctypes import byref +from ctypes import c_char_p +from ctypes import cast +__all__ = ['parse_bitcode'] +lib = get_library() + +def parse_bitcode(mem_buffer): + """Input is .core.MemoryBuffer""" + module = c_object_p() + out = c_char_p(None) + result = lib.LLVMParseBitcode(mem_buffer, byref(module), byref(out)) + if result: + raise RuntimeError('LLVM Error: %s' % out.value) + m = Module(module) + m.take_ownership(mem_buffer) + return m + +def register_library(library): + library.LLVMParseBitcode.argtypes = [MemoryBuffer, POINTER(c_object_p), POINTER(c_char_p)] + library.LLVMParseBitcode.restype = bool + +register_library(lib) diff --git a/bindings/python/llvm/core.py b/bindings/python/llvm/core.py index 76d1b14e631..2072d2f18dc 100644 --- a/bindings/python/llvm/core.py +++ b/bindings/python/llvm/core.py @@ -16,10 +16,12 @@ from . import enumerations from ctypes import POINTER from ctypes import byref from ctypes import c_char_p +from ctypes import c_uint __all__ = [ "lib", "MemoryBuffer", + "Module", "Context", "PassRegistry" ] @@ -88,6 +90,45 @@ class MemoryBuffer(LLVMObject): def __len__(self): return lib.LLVMGetBufferSize(self) +class Module(LLVMObject): + """Represents the top-level structure of an llvm program in an opaque object.""" + + def __init__(self, module, name=None, context=None): + LLVMObject.__init__(self, module, disposer=lib.LLVMDisposeModule) + + @classmethod + def CreateWithName(cls, module_id): + m = Module(lib.LLVMModuleCreateWithName(module_id)) + c = Context.GetGlobalContext().take_ownership(m) + return m + + @property + def data_layout(self): + return lib.LLVMGetDataLayout(self) + + @data_layout.setter + def data_layout(self, new_data_layout): + """new_data_layout is a string.""" + lib.LLVMSetDataLayout(self, new_data_layout) + + @property + def target(self): + return lib.LLVMGetTarget(self) + + @target.setter + def target(self, new_target): + """new_target is a string.""" + lib.LLVMSetTarget(self, new_target) + + def dump(self): + lib.LLVMDumpModule(self) + + def print_module_to_file(self, filename): + out = c_char_p(None) + result = lib.LLVMPrintModuleToFile(self, filename, byref(out)) + if not result: + raise RuntimeError("LLVM Error: %s" % out.value) + class Context(LLVMObject): def __init__(self, context=None): @@ -172,6 +213,32 @@ def register_library(library): library.LLVMDisposeMemoryBuffer.argtypes = [MemoryBuffer] + # Module declarations + library.LLVMModuleCreateWithName.argtypes = [c_char_p] + library.LLVMModuleCreateWithName.restype = c_object_p + + library.LLVMDisposeModule.argtypes = [Module] + library.LLVMDisposeModule.restype = None + + library.LLVMGetDataLayout.argtypes = [Module] + library.LLVMGetDataLayout.restype = c_char_p + + library.LLVMSetDataLayout.argtypes = [Module, c_char_p] + library.LLVMSetDataLayout.restype = None + + library.LLVMGetTarget.argtypes = [Module] + library.LLVMGetTarget.restype = c_char_p + + library.LLVMSetTarget.argtypes = [Module, c_char_p] + library.LLVMSetTarget.restype = None + + library.LLVMDumpModule.argtypes = [Module] + library.LLVMDumpModule.restype = None + + library.LLVMPrintModuleToFile.argtypes = [Module, c_char_p, + POINTER(c_char_p)] + library.LLVMPrintModuleToFile.restype = bool + def register_enumerations(): for name, value in enumerations.OpCodes: OpCode.register(name, value) diff --git a/bindings/python/llvm/tests/base.py b/bindings/python/llvm/tests/base.py index 22da5fdbdd0..194f1a41192 100644 --- a/bindings/python/llvm/tests/base.py +++ b/bindings/python/llvm/tests/base.py @@ -33,3 +33,6 @@ class TestBase(unittest.TestCase): def get_test_file(self): return os.path.join(os.path.dirname(os.path.abspath(__file__)), "test_file") + + def get_test_bc(self): + return os.path.join(os.path.dirname(os.path.abspath(__file__)), "test.bc") diff --git a/bindings/python/llvm/tests/test.bc b/bindings/python/llvm/tests/test.bc new file mode 100644 index 0000000000000000000000000000000000000000..8d3d28fbc0db75966d653101c54c4e4702649b0d GIT binary patch literal 1344 zcmbtTO=weD6h1HS<=vO`HN9z{G{LAhO$8Sfo>gm#29lRlqjph$K$vlnlv0b}kp56! zBf@JNO=j$(I*V}}7m2AuVK!=Mbs2wfx+p^h(Skovl!6PzMRh&r=2@A_;Kswd_k6kM zobQ}-@5!r5V-28axR?__MQy|@uYw3V1b~_`1`a%{$3;d%mtynML7BF;UlwxeR;S|M zEM4rjv<`=HxyG2Hty);|9dx%Qnv?FyVCdmdOZ5i_n#p|9+dW;Jjz0Q;l<+eH*n+yX zDd}Q8=cQkprgFHw&Xp#$`p#b_YI0MZ)C|F(l+q7a%~rUaxdw$vy59?>qXpe49+P#) zjV3Ghmzq*fpXf=vSyHwo%ph$M&Cxf0zqfrjH+G^BjTe8AqvFfG?!R5u{C=a@0l%+m zl|T$rLqkh8n$Px;{+s!|ed8I6n5q0g-}sQ3ADI^SSon?Cu6B9OKwVZR14S~(2nUPu zTS~Il8bbH~_cr}g3iRsE zs@wR+1u-%ok4%V>1#@C3L(VE>B$>}-NH!x5oB0eQ$lcFy_E7)H0(XMSfinBq6mQZMt(Zc;APqHh{wDC!0&*1|RKN;_1VMg?V;s9%jrAz6lf z!j4p!+a+9cI$TP7BuhAw>~`t5-O`oz=48nG`1tcxAyBy;b}$}g=jH$31}Xl9>&Yq1s~U8nUJ*Bl zg702I5TMiM