2012-03-09 09:07:35 +00:00
|
|
|
#===- 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.
|
|
|
|
#
|
|
|
|
#===------------------------------------------------------------------------===#
|
|
|
|
|
2012-03-09 18:56:33 +00:00
|
|
|
from .common import LLVMObject
|
2012-03-10 04:41:24 +00:00
|
|
|
from .common import c_object_p
|
2012-03-09 09:07:35 +00:00
|
|
|
from .common import get_library
|
|
|
|
|
|
|
|
from ctypes import POINTER
|
|
|
|
from ctypes import byref
|
|
|
|
from ctypes import c_char_p
|
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
"lib",
|
2012-03-10 04:41:24 +00:00
|
|
|
"MemoryBuffer",
|
2012-03-09 09:07:35 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
lib = get_library()
|
|
|
|
|
2012-03-10 04:41:24 +00:00
|
|
|
class MemoryBuffer(LLVMObject):
|
2012-03-09 09:07:35 +00:00
|
|
|
"""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")
|
|
|
|
|
2012-03-10 04:41:24 +00:00
|
|
|
memory = c_object_p()
|
2012-03-09 09:07:35 +00:00
|
|
|
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)
|
|
|
|
|
2012-03-10 04:41:24 +00:00
|
|
|
LLVMObject.__init__(self, memory, disposer=lib.LLVMDisposeMemoryBuffer)
|
2012-03-09 09:07:35 +00:00
|
|
|
|
|
|
|
def register_library(library):
|
|
|
|
library.LLVMCreateMemoryBufferWithContentsOfFile.argtypes = [c_char_p,
|
2012-03-10 04:41:24 +00:00
|
|
|
POINTER(c_object_p), POINTER(c_char_p)]
|
2012-03-09 09:07:35 +00:00
|
|
|
library.LLVMCreateMemoryBufferWithContentsOfFile.restype = bool
|
|
|
|
|
2012-03-10 04:41:24 +00:00
|
|
|
library.LLVMDisposeMemoryBuffer.argtypes = [MemoryBuffer]
|
2012-03-09 09:07:35 +00:00
|
|
|
|
|
|
|
register_library(lib)
|