diff options
author | Gregory Szorc <gregory.szorc@gmail.com> | 2012-03-09 09:07:35 +0000 |
---|---|---|
committer | Gregory Szorc <gregory.szorc@gmail.com> | 2012-03-09 09:07:35 +0000 |
commit | 5ae04279e0ed16975e1eea012499e8d833aab3c5 (patch) | |
tree | 4a7b8e40d7732e081a2b88baf29103a930df8dc6 /bindings/python/llvm/core.py | |
parent | 1fabd9f85e8ac728c35cb63c70d8aac2c94c92a8 (diff) | |
download | external_llvm-5ae04279e0ed16975e1eea012499e8d833aab3c5.zip external_llvm-5ae04279e0ed16975e1eea012499e8d833aab3c5.tar.gz external_llvm-5ae04279e0ed16975e1eea012499e8d833aab3c5.tar.bz2 |
[llvm.py] Initial skeleton for Python LLVM bindings
This contains a semi-functional skeleton for the implementation of the
LLVM bindings for Python.
The API for the Object.h interface is roughly designed but not
implemented. MemoryBufferRef is implemented and actually appears to
work!
The ObjectFile unit test fails with a segmentation fault because the
LLVM library isn't being properly initialized. The build system doesn't
know about this code yet, so no alerts should fire.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@152397 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'bindings/python/llvm/core.py')
-rw-r--r-- | bindings/python/llvm/core.py | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/bindings/python/llvm/core.py b/bindings/python/llvm/core.py new file mode 100644 index 0000000..5a3bd51 --- /dev/null +++ b/bindings/python/llvm/core.py @@ -0,0 +1,61 @@ +#===- core.py - Python LLVM Bindings -------------------------*- python -*--===# +# +# The LLVM Compiler Infrastructure +# +# This file is distributed under the University of Illinois Open Source +# License. See LICENSE.TXT for details. +# +#===------------------------------------------------------------------------===# + +from .common import get_library + +from ctypes import POINTER +from ctypes import byref +from ctypes import c_char_p +from ctypes import c_void_p + +__all__ = [ + "lib", + "MemoryBufferRef", +] + +lib = get_library() + +class MemoryBuffer(object): + """Represents an opaque memory buffer.""" + + def __init__(self, filename=None): + """Create a new memory buffer. + + Currently, we support creating from the contents of a file at the + specified filename. + """ + if filename is None: + raise Exception("filename argument must be defined") + + memory = c_void_p(None) + out = c_char_p(None) + + result = lib.LLVMCreateMemoryBufferWithContentsOfFile(filename, + byref(memory), byref(out)) + + if result: + raise Exception("Could not create memory buffer: %s" % out.value) + + self._memory = memory + + def __del__(self): + lib.LLVMDisposeMemoryBuffer(self._memory) + + def from_param(self): + return self._memory + + +def register_library(library): + library.LLVMCreateMemoryBufferWithContentsOfFile.argtypes = [c_char_p, + POINTER(c_void_p), POINTER(c_char_p)] + library.LLVMCreateMemoryBufferWithContentsOfFile.restype = bool + + library.LLVMDisposeMemoryBuffer.argtypes = [c_void_p] + +register_library(lib) |