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
32 """Represents an individual OpCode enumeration."""
36 def __init__(self, name, value):
41 return 'OpCode.%s' % self.name
44 def from_value(value):
45 """Obtain an OpCode instance from a numeric value."""
46 result = OpCode._value_map.get(value, None)
49 raise ValueError('Unknown OpCode: %d' % value)
54 def register(name, value):
55 """Registers a new OpCode enumeration.
57 This is called by this module for each enumeration defined in
58 enumerations. You should not need to call this outside this module.
60 if value in OpCode._value_map:
61 raise ValueError('OpCode value already registered: %d' % value)
63 opcode = OpCode(name, value)
64 OpCode._value_map[value] = opcode
65 setattr(OpCode, name, opcode)
67 class MemoryBuffer(LLVMObject):
68 """Represents an opaque memory buffer."""
70 def __init__(self, filename=None):
71 """Create a new memory buffer.
73 Currently, we support creating from the contents of a file at the
77 raise Exception("filename argument must be defined")
82 result = lib.LLVMCreateMemoryBufferWithContentsOfFile(filename,
83 byref(memory), byref(out))
86 raise Exception("Could not create memory buffer: %s" % out.value)
88 LLVMObject.__init__(self, memory, disposer=lib.LLVMDisposeMemoryBuffer)
91 return lib.LLVMGetBufferSize(self)
93 class Module(LLVMObject):
94 """Represents the top-level structure of an llvm program in an opaque object."""
96 def __init__(self, module, name=None, context=None):
97 LLVMObject.__init__(self, module, disposer=lib.LLVMDisposeModule)
100 def CreateWithName(cls, module_id):
101 m = Module(lib.LLVMModuleCreateWithName(module_id))
102 c = Context.GetGlobalContext().take_ownership(m)
106 def datalayout(self):
107 return lib.LLVMGetDataLayout(self)
110 def datalayout(self, new_data_layout):
111 """new_data_layout is a string."""
112 lib.LLVMSetDataLayout(self, new_data_layout)
116 return lib.LLVMGetTarget(self)
119 def target(self, new_target):
120 """new_target is a string."""
121 lib.LLVMSetTarget(self, new_target)
124 lib.LLVMDumpModule(self)
126 def print_module_to_file(self, filename):
128 # Result is inverted so 0 means everything was ok.
129 result = lib.LLVMPrintModuleToFile(self, filename, byref(out))
131 raise RuntimeError("LLVM Error: %s" % out.value)
133 class Context(LLVMObject):
135 def __init__(self, context=None):
137 context = lib.LLVMContextCreate()
138 LLVMObject.__init__(self, context, disposer=lib.LLVMContextDispose)
140 LLVMObject.__init__(self, context)
143 def GetGlobalContext(cls):
144 return Context(lib.LLVMGetGlobalContext())
146 class PassRegistry(LLVMObject):
147 """Represents an opaque pass registry object."""
150 LLVMObject.__init__(self,
151 lib.LLVMGetGlobalPassRegistry())
153 def register_library(library):
154 # Initialization/Shutdown declarations.
155 library.LLVMInitializeCore.argtypes = [PassRegistry]
156 library.LLVMInitializeCore.restype = None
158 library.LLVMInitializeTransformUtils.argtypes = [PassRegistry]
159 library.LLVMInitializeTransformUtils.restype = None
161 library.LLVMInitializeScalarOpts.argtypes = [PassRegistry]
162 library.LLVMInitializeScalarOpts.restype = None
164 library.LLVMInitializeObjCARCOpts.argtypes = [PassRegistry]
165 library.LLVMInitializeObjCARCOpts.restype = None
167 library.LLVMInitializeVectorization.argtypes = [PassRegistry]
168 library.LLVMInitializeVectorization.restype = None
170 library.LLVMInitializeInstCombine.argtypes = [PassRegistry]
171 library.LLVMInitializeInstCombine.restype = None
173 library.LLVMInitializeIPO.argtypes = [PassRegistry]
174 library.LLVMInitializeIPO.restype = None
176 library.LLVMInitializeInstrumentation.argtypes = [PassRegistry]
177 library.LLVMInitializeInstrumentation.restype = None
179 library.LLVMInitializeAnalysis.argtypes = [PassRegistry]
180 library.LLVMInitializeAnalysis.restype = None
182 library.LLVMInitializeIPA.argtypes = [PassRegistry]
183 library.LLVMInitializeIPA.restype = None
185 library.LLVMInitializeCodeGen.argtypes = [PassRegistry]
186 library.LLVMInitializeCodeGen.restype = None
188 library.LLVMInitializeTarget.argtypes = [PassRegistry]
189 library.LLVMInitializeTarget.restype = None
191 library.LLVMShutdown.argtypes = []
192 library.LLVMShutdown.restype = None
194 # Pass Registry declarations.
195 library.LLVMGetGlobalPassRegistry.argtypes = []
196 library.LLVMGetGlobalPassRegistry.restype = c_object_p
198 # Context declarations.
199 library.LLVMContextCreate.argtypes = []
200 library.LLVMContextCreate.restype = c_object_p
202 library.LLVMContextDispose.argtypes = [Context]
203 library.LLVMContextDispose.restype = None
205 library.LLVMGetGlobalContext.argtypes = []
206 library.LLVMGetGlobalContext.restype = c_object_p
208 # Memory buffer declarations
209 library.LLVMCreateMemoryBufferWithContentsOfFile.argtypes = [c_char_p,
210 POINTER(c_object_p), POINTER(c_char_p)]
211 library.LLVMCreateMemoryBufferWithContentsOfFile.restype = bool
213 library.LLVMGetBufferSize.argtypes = [MemoryBuffer]
215 library.LLVMDisposeMemoryBuffer.argtypes = [MemoryBuffer]
217 # Module declarations
218 library.LLVMModuleCreateWithName.argtypes = [c_char_p]
219 library.LLVMModuleCreateWithName.restype = c_object_p
221 library.LLVMDisposeModule.argtypes = [Module]
222 library.LLVMDisposeModule.restype = None
224 library.LLVMGetDataLayout.argtypes = [Module]
225 library.LLVMGetDataLayout.restype = c_char_p
227 library.LLVMSetDataLayout.argtypes = [Module, c_char_p]
228 library.LLVMSetDataLayout.restype = None
230 library.LLVMGetTarget.argtypes = [Module]
231 library.LLVMGetTarget.restype = c_char_p
233 library.LLVMSetTarget.argtypes = [Module, c_char_p]
234 library.LLVMSetTarget.restype = None
236 library.LLVMDumpModule.argtypes = [Module]
237 library.LLVMDumpModule.restype = None
239 library.LLVMPrintModuleToFile.argtypes = [Module, c_char_p,
241 library.LLVMPrintModuleToFile.restype = bool
243 def register_enumerations():
244 for name, value in enumerations.OpCodes:
245 OpCode.register(name, value)
247 def initialize_llvm():
248 c = Context.GetGlobalContext()
250 lib.LLVMInitializeCore(p)
251 lib.LLVMInitializeTransformUtils(p)
252 lib.LLVMInitializeScalarOpts(p)
253 lib.LLVMInitializeObjCARCOpts(p)
254 lib.LLVMInitializeVectorization(p)
255 lib.LLVMInitializeInstCombine(p)
256 lib.LLVMInitializeIPO(p)
257 lib.LLVMInitializeInstrumentation(p)
258 lib.LLVMInitializeAnalysis(p)
259 lib.LLVMInitializeIPA(p)
260 lib.LLVMInitializeCodeGen(p)
261 lib.LLVMInitializeTarget(p)
263 register_library(lib)
264 register_enumerations()