137a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman
237a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesmanfrom .common import LLVMObject
337a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesmanfrom .common import c_object_p
437a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesmanfrom .common import get_library
537a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesmanfrom . import enumerations
637a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesmanfrom .core import MemoryBuffer
737a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesmanfrom .core import Module
837a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesmanfrom .core import OpCode
937a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesmanfrom ctypes import POINTER
1037a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesmanfrom ctypes import byref
1137a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesmanfrom ctypes import c_char_p
1237a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesmanfrom ctypes import cast
1337a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman__all__ = ['parse_bitcode']
1437a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesmanlib = get_library()
1537a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman
1637a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesmandef parse_bitcode(mem_buffer):
1737a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman    """Input is .core.MemoryBuffer"""
1837a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman    module = c_object_p()
1937a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman    out = c_char_p(None)
2037a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman    result = lib.LLVMParseBitcode(mem_buffer, byref(module), byref(out))
2137a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman    if result:
2237a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman        raise RuntimeError('LLVM Error: %s' % out.value)
2337a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman    m = Module(module)
2437a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman    m.take_ownership(mem_buffer)
2537a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman    return m
2637a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman
2737a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesmandef register_library(library):
2837a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman    library.LLVMParseBitcode.argtypes = [MemoryBuffer, POINTER(c_object_p), POINTER(c_char_p)]
2937a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman    library.LLVMParseBitcode.restype = bool
3037a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesman
3137a8807323672ab0bb366272e5a798b8e63752a4Michael Gottesmanregister_library(lib)
32