1 #===- core.py - Python LLVM Bindings -------------------------*- python -*--===#
3 # The LLVM Compiler Infrastructure
5 # This file is distributed under the University of Illinois Open Source
6 # License. See LICENSE.TXT for details.
8 #===------------------------------------------------------------------------===#
10 from .common import LLVMObject
11 from .common import c_object_p
12 from .common import get_library
14 from . import enumerations
16 from ctypes import POINTER
17 from ctypes import byref
18 from ctypes import c_char_p
28 """Represents an individual OpCode enumeration."""
32 def __init__(self, name, value):
37 return 'OpCode.%s' % self.name
40 def from_value(value):
41 """Obtain an OpCode instance from a numeric value."""
42 result = OpCode._value_map.get(value, None)
45 raise ValueError('Unknown OpCode: %d' % value)
50 def register(name, value):
51 """Registers a new OpCode enumeration.
53 This is called by this module for each enumeration defined in
54 enumerations. You should not need to call this outside this module.
56 if value in OpCode._value_map:
57 raise ValueError('OpCode value already registered: %d' % value)
59 opcode = OpCode(name, value)
60 OpCode._value_map[value] = opcode
61 setattr(OpCode, name, opcode)
63 class MemoryBuffer(LLVMObject):
64 """Represents an opaque memory buffer."""
66 def __init__(self, filename=None):
67 """Create a new memory buffer.
69 Currently, we support creating from the contents of a file at the
73 raise Exception("filename argument must be defined")
78 result = lib.LLVMCreateMemoryBufferWithContentsOfFile(filename,
79 byref(memory), byref(out))
82 raise Exception("Could not create memory buffer: %s" % out.value)
84 LLVMObject.__init__(self, memory, disposer=lib.LLVMDisposeMemoryBuffer)
87 return lib.LLVMGetBufferSize(self)
89 def register_library(library):
90 # Memory buffer declarations
91 library.LLVMCreateMemoryBufferWithContentsOfFile.argtypes = [c_char_p,
92 POINTER(c_object_p), POINTER(c_char_p)]
93 library.LLVMCreateMemoryBufferWithContentsOfFile.restype = bool
95 library.LLVMGetBufferSize.argtypes = [MemoryBuffer]
97 library.LLVMDisposeMemoryBuffer.argtypes = [MemoryBuffer]
99 def register_enumerations():
100 for name, value in enumerations.OpCodes:
101 OpCode.register(name, value)
103 register_library(lib)
104 register_enumerations()