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
19 from ctypes import c_uint
35 """Represents an individual OpCode enumeration."""
39 def __init__(self, name, value):
44 return 'OpCode.%s' % self.name
47 def from_value(value):
48 """Obtain an OpCode instance from a numeric value."""
49 result = OpCode._value_map.get(value, None)
52 raise ValueError('Unknown OpCode: %d' % value)
57 def register(name, value):
58 """Registers a new OpCode enumeration.
60 This is called by this module for each enumeration defined in
61 enumerations. You should not need to call this outside this module.
63 if value in OpCode._value_map:
64 raise ValueError('OpCode value already registered: %d' % value)
66 opcode = OpCode(name, value)
67 OpCode._value_map[value] = opcode
68 setattr(OpCode, name, opcode)
70 class MemoryBuffer(LLVMObject):
71 """Represents an opaque memory buffer."""
73 def __init__(self, filename=None):
74 """Create a new memory buffer.
76 Currently, we support creating from the contents of a file at the
80 raise Exception("filename argument must be defined")
85 result = lib.LLVMCreateMemoryBufferWithContentsOfFile(filename,
86 byref(memory), byref(out))
89 raise Exception("Could not create memory buffer: %s" % out.value)
91 LLVMObject.__init__(self, memory, disposer=lib.LLVMDisposeMemoryBuffer)
94 return lib.LLVMGetBufferSize(self)
96 class Value(LLVMObject):
98 def __init__(self, value):
99 LLVMObject.__init__(self, value)
103 return lib.LLVMGetValueName(self)
106 lib.LLVMDumpValue(self)
108 class Module(LLVMObject):
109 """Represents the top-level structure of an llvm program in an opaque object."""
111 def __init__(self, module, name=None, context=None):
112 LLVMObject.__init__(self, module, disposer=lib.LLVMDisposeModule)
115 def CreateWithName(cls, module_id):
116 m = Module(lib.LLVMModuleCreateWithName(module_id))
117 c = Context.GetGlobalContext().take_ownership(m)
121 def datalayout(self):
122 return lib.LLVMGetDataLayout(self)
125 def datalayout(self, new_data_layout):
126 """new_data_layout is a string."""
127 lib.LLVMSetDataLayout(self, new_data_layout)
131 return lib.LLVMGetTarget(self)
134 def target(self, new_target):
135 """new_target is a string."""
136 lib.LLVMSetTarget(self, new_target)
139 lib.LLVMDumpModule(self)
141 class __function_iterator(object):
142 def __init__(self, module, reverse=False):
144 self.reverse = reverse
146 self.function = self.module.last
148 self.function = self.module.first
154 if not isinstance(self.function, Function):
155 raise StopIteration("")
156 result = self.function
158 self.function = self.function.prev
160 self.function = self.function.next
164 return Module.__function_iterator(self)
166 def __reversed__(self):
167 return Module.__function_iterator(self, reverse=True)
171 return Function(lib.LLVMGetFirstFunction(self))
175 return Function(lib.LLVMGetLastFunction(self))
177 def print_module_to_file(self, filename):
179 # Result is inverted so 0 means everything was ok.
180 result = lib.LLVMPrintModuleToFile(self, filename, byref(out))
182 raise RuntimeError("LLVM Error: %s" % out.value)
184 class Function(Value):
186 def __init__(self, value):
187 Value.__init__(self, value)
191 f = lib.LLVMGetNextFunction(self)
192 return f and Function(f)
196 f = lib.LLVMGetPreviousFunction(self)
197 return f and Function(f)
199 class Context(LLVMObject):
201 def __init__(self, context=None):
203 context = lib.LLVMContextCreate()
204 LLVMObject.__init__(self, context, disposer=lib.LLVMContextDispose)
206 LLVMObject.__init__(self, context)
209 def GetGlobalContext(cls):
210 return Context(lib.LLVMGetGlobalContext())
212 class PassRegistry(LLVMObject):
213 """Represents an opaque pass registry object."""
216 LLVMObject.__init__(self,
217 lib.LLVMGetGlobalPassRegistry())
219 def register_library(library):
220 # Initialization/Shutdown declarations.
221 library.LLVMInitializeCore.argtypes = [PassRegistry]
222 library.LLVMInitializeCore.restype = None
224 library.LLVMInitializeTransformUtils.argtypes = [PassRegistry]
225 library.LLVMInitializeTransformUtils.restype = None
227 library.LLVMInitializeScalarOpts.argtypes = [PassRegistry]
228 library.LLVMInitializeScalarOpts.restype = None
230 library.LLVMInitializeObjCARCOpts.argtypes = [PassRegistry]
231 library.LLVMInitializeObjCARCOpts.restype = None
233 library.LLVMInitializeVectorization.argtypes = [PassRegistry]
234 library.LLVMInitializeVectorization.restype = None
236 library.LLVMInitializeInstCombine.argtypes = [PassRegistry]
237 library.LLVMInitializeInstCombine.restype = None
239 library.LLVMInitializeIPO.argtypes = [PassRegistry]
240 library.LLVMInitializeIPO.restype = None
242 library.LLVMInitializeInstrumentation.argtypes = [PassRegistry]
243 library.LLVMInitializeInstrumentation.restype = None
245 library.LLVMInitializeAnalysis.argtypes = [PassRegistry]
246 library.LLVMInitializeAnalysis.restype = None
248 library.LLVMInitializeIPA.argtypes = [PassRegistry]
249 library.LLVMInitializeIPA.restype = None
251 library.LLVMInitializeCodeGen.argtypes = [PassRegistry]
252 library.LLVMInitializeCodeGen.restype = None
254 library.LLVMInitializeTarget.argtypes = [PassRegistry]
255 library.LLVMInitializeTarget.restype = None
257 library.LLVMShutdown.argtypes = []
258 library.LLVMShutdown.restype = None
260 # Pass Registry declarations.
261 library.LLVMGetGlobalPassRegistry.argtypes = []
262 library.LLVMGetGlobalPassRegistry.restype = c_object_p
264 # Context declarations.
265 library.LLVMContextCreate.argtypes = []
266 library.LLVMContextCreate.restype = c_object_p
268 library.LLVMContextDispose.argtypes = [Context]
269 library.LLVMContextDispose.restype = None
271 library.LLVMGetGlobalContext.argtypes = []
272 library.LLVMGetGlobalContext.restype = c_object_p
274 # Memory buffer declarations
275 library.LLVMCreateMemoryBufferWithContentsOfFile.argtypes = [c_char_p,
276 POINTER(c_object_p), POINTER(c_char_p)]
277 library.LLVMCreateMemoryBufferWithContentsOfFile.restype = bool
279 library.LLVMGetBufferSize.argtypes = [MemoryBuffer]
281 library.LLVMDisposeMemoryBuffer.argtypes = [MemoryBuffer]
283 # Module declarations
284 library.LLVMModuleCreateWithName.argtypes = [c_char_p]
285 library.LLVMModuleCreateWithName.restype = c_object_p
287 library.LLVMDisposeModule.argtypes = [Module]
288 library.LLVMDisposeModule.restype = None
290 library.LLVMGetDataLayout.argtypes = [Module]
291 library.LLVMGetDataLayout.restype = c_char_p
293 library.LLVMSetDataLayout.argtypes = [Module, c_char_p]
294 library.LLVMSetDataLayout.restype = None
296 library.LLVMGetTarget.argtypes = [Module]
297 library.LLVMGetTarget.restype = c_char_p
299 library.LLVMSetTarget.argtypes = [Module, c_char_p]
300 library.LLVMSetTarget.restype = None
302 library.LLVMDumpModule.argtypes = [Module]
303 library.LLVMDumpModule.restype = None
305 library.LLVMPrintModuleToFile.argtypes = [Module, c_char_p,
307 library.LLVMPrintModuleToFile.restype = bool
309 library.LLVMGetFirstFunction.argtypes = [Module]
310 library.LLVMGetFirstFunction.restype = c_object_p
312 library.LLVMGetLastFunction.argtypes = [Module]
313 library.LLVMGetLastFunction.restype = c_object_p
315 library.LLVMGetNextFunction.argtypes = [Function]
316 library.LLVMGetNextFunction.restype = c_object_p
318 library.LLVMGetPreviousFunction.argtypes = [Function]
319 library.LLVMGetPreviousFunction.restype = c_object_p
321 # Value declarations.
322 library.LLVMGetValueName.argtypes = [Value]
323 library.LLVMGetValueName.restype = c_char_p
325 library.LLVMDumpValue.argtypes = [Value]
326 library.LLVMDumpValue.restype = None
328 def register_enumerations():
329 for name, value in enumerations.OpCodes:
330 OpCode.register(name, value)
332 def initialize_llvm():
333 c = Context.GetGlobalContext()
335 lib.LLVMInitializeCore(p)
336 lib.LLVMInitializeTransformUtils(p)
337 lib.LLVMInitializeScalarOpts(p)
338 lib.LLVMInitializeObjCARCOpts(p)
339 lib.LLVMInitializeVectorization(p)
340 lib.LLVMInitializeInstCombine(p)
341 lib.LLVMInitializeIPO(p)
342 lib.LLVMInitializeInstrumentation(p)
343 lib.LLVMInitializeAnalysis(p)
344 lib.LLVMInitializeIPA(p)
345 lib.LLVMInitializeCodeGen(p)
346 lib.LLVMInitializeTarget(p)
348 register_library(lib)
349 register_enumerations()