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
30 """Represents an individual OpCode enumeration."""
34 def __init__(self, name, value):
39 return 'OpCode.%s' % self.name
42 def from_value(value):
43 """Obtain an OpCode instance from a numeric value."""
44 result = OpCode._value_map.get(value, None)
47 raise ValueError('Unknown OpCode: %d' % value)
52 def register(name, value):
53 """Registers a new OpCode enumeration.
55 This is called by this module for each enumeration defined in
56 enumerations. You should not need to call this outside this module.
58 if value in OpCode._value_map:
59 raise ValueError('OpCode value already registered: %d' % value)
61 opcode = OpCode(name, value)
62 OpCode._value_map[value] = opcode
63 setattr(OpCode, name, opcode)
65 class MemoryBuffer(LLVMObject):
66 """Represents an opaque memory buffer."""
68 def __init__(self, filename=None):
69 """Create a new memory buffer.
71 Currently, we support creating from the contents of a file at the
75 raise Exception("filename argument must be defined")
80 result = lib.LLVMCreateMemoryBufferWithContentsOfFile(filename,
81 byref(memory), byref(out))
84 raise Exception("Could not create memory buffer: %s" % out.value)
86 LLVMObject.__init__(self, memory, disposer=lib.LLVMDisposeMemoryBuffer)
89 return lib.LLVMGetBufferSize(self)
91 class Context(LLVMObject):
93 def __init__(self, context=None):
95 context = lib.LLVMContextCreate()
96 LLVMObject.__init__(self, context, disposer=lib.LLVMContextDispose)
98 LLVMObject.__init__(self, context)
101 def GetGlobalContext(cls):
102 return Context(lib.LLVMGetGlobalContext())
104 class PassRegistry(LLVMObject):
105 """Represents an opaque pass registry object."""
108 LLVMObject.__init__(self,
109 lib.LLVMGetGlobalPassRegistry())
111 def register_library(library):
112 # Initialization/Shutdown declarations.
113 library.LLVMInitializeCore.argtypes = [PassRegistry]
114 library.LLVMInitializeCore.restype = None
116 library.LLVMInitializeTransformUtils.argtypes = [PassRegistry]
117 library.LLVMInitializeTransformUtils.restype = None
119 library.LLVMInitializeScalarOpts.argtypes = [PassRegistry]
120 library.LLVMInitializeScalarOpts.restype = None
122 library.LLVMInitializeObjCARCOpts.argtypes = [PassRegistry]
123 library.LLVMInitializeObjCARCOpts.restype = None
125 library.LLVMInitializeVectorization.argtypes = [PassRegistry]
126 library.LLVMInitializeVectorization.restype = None
128 library.LLVMInitializeInstCombine.argtypes = [PassRegistry]
129 library.LLVMInitializeInstCombine.restype = None
131 library.LLVMInitializeIPO.argtypes = [PassRegistry]
132 library.LLVMInitializeIPO.restype = None
134 library.LLVMInitializeInstrumentation.argtypes = [PassRegistry]
135 library.LLVMInitializeInstrumentation.restype = None
137 library.LLVMInitializeAnalysis.argtypes = [PassRegistry]
138 library.LLVMInitializeAnalysis.restype = None
140 library.LLVMInitializeIPA.argtypes = [PassRegistry]
141 library.LLVMInitializeIPA.restype = None
143 library.LLVMInitializeCodeGen.argtypes = [PassRegistry]
144 library.LLVMInitializeCodeGen.restype = None
146 library.LLVMInitializeTarget.argtypes = [PassRegistry]
147 library.LLVMInitializeTarget.restype = None
149 library.LLVMShutdown.argtypes = []
150 library.LLVMShutdown.restype = None
152 # Pass Registry declarations.
153 library.LLVMGetGlobalPassRegistry.argtypes = []
154 library.LLVMGetGlobalPassRegistry.restype = c_object_p
156 # Context declarations.
157 library.LLVMContextCreate.argtypes = []
158 library.LLVMContextCreate.restype = c_object_p
160 library.LLVMContextDispose.argtypes = [Context]
161 library.LLVMContextDispose.restype = None
163 library.LLVMGetGlobalContext.argtypes = []
164 library.LLVMGetGlobalContext.restype = c_object_p
166 # Memory buffer declarations
167 library.LLVMCreateMemoryBufferWithContentsOfFile.argtypes = [c_char_p,
168 POINTER(c_object_p), POINTER(c_char_p)]
169 library.LLVMCreateMemoryBufferWithContentsOfFile.restype = bool
171 library.LLVMGetBufferSize.argtypes = [MemoryBuffer]
173 library.LLVMDisposeMemoryBuffer.argtypes = [MemoryBuffer]
175 def register_enumerations():
176 for name, value in enumerations.OpCodes:
177 OpCode.register(name, value)
179 def initialize_llvm():
180 c = Context.GetGlobalContext()
182 lib.LLVMInitializeCore(p)
183 lib.LLVMInitializeTransformUtils(p)
184 lib.LLVMInitializeScalarOpts(p)
185 lib.LLVMInitializeObjCARCOpts(p)
186 lib.LLVMInitializeVectorization(p)
187 lib.LLVMInitializeInstCombine(p)
188 lib.LLVMInitializeIPO(p)
189 lib.LLVMInitializeInstrumentation(p)
190 lib.LLVMInitializeAnalysis(p)
191 lib.LLVMInitializeIPA(p)
192 lib.LLVMInitializeCodeGen(p)
193 lib.LLVMInitializeTarget(p)
195 register_library(lib)
196 register_enumerations()