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
33 """Represents an individual OpCode enumeration."""
37 def __init__(self, name, value):
42 return 'OpCode.%s' % self.name
45 def from_value(value):
46 """Obtain an OpCode instance from a numeric value."""
47 result = OpCode._value_map.get(value, None)
50 raise ValueError('Unknown OpCode: %d' % value)
55 def register(name, value):
56 """Registers a new OpCode enumeration.
58 This is called by this module for each enumeration defined in
59 enumerations. You should not need to call this outside this module.
61 if value in OpCode._value_map:
62 raise ValueError('OpCode value already registered: %d' % value)
64 opcode = OpCode(name, value)
65 OpCode._value_map[value] = opcode
66 setattr(OpCode, name, opcode)
68 class MemoryBuffer(LLVMObject):
69 """Represents an opaque memory buffer."""
71 def __init__(self, filename=None):
72 """Create a new memory buffer.
74 Currently, we support creating from the contents of a file at the
78 raise Exception("filename argument must be defined")
83 result = lib.LLVMCreateMemoryBufferWithContentsOfFile(filename,
84 byref(memory), byref(out))
87 raise Exception("Could not create memory buffer: %s" % out.value)
89 LLVMObject.__init__(self, memory, disposer=lib.LLVMDisposeMemoryBuffer)
92 return lib.LLVMGetBufferSize(self)
94 class Module(LLVMObject):
95 """Represents the top-level structure of an llvm program in an opaque object."""
97 def __init__(self, module, name=None, context=None):
98 LLVMObject.__init__(self, module, disposer=lib.LLVMDisposeModule)
101 def CreateWithName(cls, module_id):
102 m = Module(lib.LLVMModuleCreateWithName(module_id))
103 c = Context.GetGlobalContext().take_ownership(m)
107 def datalayout(self):
108 return lib.LLVMGetDataLayout(self)
111 def datalayout(self, new_data_layout):
112 """new_data_layout is a string."""
113 lib.LLVMSetDataLayout(self, new_data_layout)
117 return lib.LLVMGetTarget(self)
120 def target(self, new_target):
121 """new_target is a string."""
122 lib.LLVMSetTarget(self, new_target)
125 lib.LLVMDumpModule(self)
127 def print_module_to_file(self, filename):
129 # Result is inverted so 0 means everything was ok.
130 result = lib.LLVMPrintModuleToFile(self, filename, byref(out))
132 raise RuntimeError("LLVM Error: %s" % out.value)
134 class Context(LLVMObject):
136 def __init__(self, context=None):
138 context = lib.LLVMContextCreate()
139 LLVMObject.__init__(self, context, disposer=lib.LLVMContextDispose)
141 LLVMObject.__init__(self, context)
144 def GetGlobalContext(cls):
145 return Context(lib.LLVMGetGlobalContext())
147 class PassRegistry(LLVMObject):
148 """Represents an opaque pass registry object."""
151 LLVMObject.__init__(self,
152 lib.LLVMGetGlobalPassRegistry())
154 def register_library(library):
155 # Initialization/Shutdown declarations.
156 library.LLVMInitializeCore.argtypes = [PassRegistry]
157 library.LLVMInitializeCore.restype = None
159 library.LLVMInitializeTransformUtils.argtypes = [PassRegistry]
160 library.LLVMInitializeTransformUtils.restype = None
162 library.LLVMInitializeScalarOpts.argtypes = [PassRegistry]
163 library.LLVMInitializeScalarOpts.restype = None
165 library.LLVMInitializeObjCARCOpts.argtypes = [PassRegistry]
166 library.LLVMInitializeObjCARCOpts.restype = None
168 library.LLVMInitializeVectorization.argtypes = [PassRegistry]
169 library.LLVMInitializeVectorization.restype = None
171 library.LLVMInitializeInstCombine.argtypes = [PassRegistry]
172 library.LLVMInitializeInstCombine.restype = None
174 library.LLVMInitializeIPO.argtypes = [PassRegistry]
175 library.LLVMInitializeIPO.restype = None
177 library.LLVMInitializeInstrumentation.argtypes = [PassRegistry]
178 library.LLVMInitializeInstrumentation.restype = None
180 library.LLVMInitializeAnalysis.argtypes = [PassRegistry]
181 library.LLVMInitializeAnalysis.restype = None
183 library.LLVMInitializeIPA.argtypes = [PassRegistry]
184 library.LLVMInitializeIPA.restype = None
186 library.LLVMInitializeCodeGen.argtypes = [PassRegistry]
187 library.LLVMInitializeCodeGen.restype = None
189 library.LLVMInitializeTarget.argtypes = [PassRegistry]
190 library.LLVMInitializeTarget.restype = None
192 library.LLVMShutdown.argtypes = []
193 library.LLVMShutdown.restype = None
195 # Pass Registry declarations.
196 library.LLVMGetGlobalPassRegistry.argtypes = []
197 library.LLVMGetGlobalPassRegistry.restype = c_object_p
199 # Context declarations.
200 library.LLVMContextCreate.argtypes = []
201 library.LLVMContextCreate.restype = c_object_p
203 library.LLVMContextDispose.argtypes = [Context]
204 library.LLVMContextDispose.restype = None
206 library.LLVMGetGlobalContext.argtypes = []
207 library.LLVMGetGlobalContext.restype = c_object_p
209 # Memory buffer declarations
210 library.LLVMCreateMemoryBufferWithContentsOfFile.argtypes = [c_char_p,
211 POINTER(c_object_p), POINTER(c_char_p)]
212 library.LLVMCreateMemoryBufferWithContentsOfFile.restype = bool
214 library.LLVMGetBufferSize.argtypes = [MemoryBuffer]
216 library.LLVMDisposeMemoryBuffer.argtypes = [MemoryBuffer]
218 # Module declarations
219 library.LLVMModuleCreateWithName.argtypes = [c_char_p]
220 library.LLVMModuleCreateWithName.restype = c_object_p
222 library.LLVMDisposeModule.argtypes = [Module]
223 library.LLVMDisposeModule.restype = None
225 library.LLVMGetDataLayout.argtypes = [Module]
226 library.LLVMGetDataLayout.restype = c_char_p
228 library.LLVMSetDataLayout.argtypes = [Module, c_char_p]
229 library.LLVMSetDataLayout.restype = None
231 library.LLVMGetTarget.argtypes = [Module]
232 library.LLVMGetTarget.restype = c_char_p
234 library.LLVMSetTarget.argtypes = [Module, c_char_p]
235 library.LLVMSetTarget.restype = None
237 library.LLVMDumpModule.argtypes = [Module]
238 library.LLVMDumpModule.restype = None
240 library.LLVMPrintModuleToFile.argtypes = [Module, c_char_p,
242 library.LLVMPrintModuleToFile.restype = bool
244 def register_enumerations():
245 for name, value in enumerations.OpCodes:
246 OpCode.register(name, value)
248 def initialize_llvm():
249 c = Context.GetGlobalContext()
251 lib.LLVMInitializeCore(p)
252 lib.LLVMInitializeTransformUtils(p)
253 lib.LLVMInitializeScalarOpts(p)
254 lib.LLVMInitializeObjCARCOpts(p)
255 lib.LLVMInitializeVectorization(p)
256 lib.LLVMInitializeInstCombine(p)
257 lib.LLVMInitializeIPO(p)
258 lib.LLVMInitializeInstrumentation(p)
259 lib.LLVMInitializeAnalysis(p)
260 lib.LLVMInitializeIPA(p)
261 lib.LLVMInitializeCodeGen(p)
262 lib.LLVMInitializeTarget(p)
264 register_library(lib)
265 register_enumerations()