[python-bindings] Fixed 3 test failures caused by typos.
[oota-llvm.git] / bindings / python / llvm / core.py
1 #===- core.py - Python LLVM Bindings -------------------------*- python -*--===#
2 #
3 #                     The LLVM Compiler Infrastructure
4 #
5 # This file is distributed under the University of Illinois Open Source
6 # License. See LICENSE.TXT for details.
7 #
8 #===------------------------------------------------------------------------===#
9
10 from .common import LLVMObject
11 from .common import c_object_p
12 from .common import get_library
13
14 from . import enumerations
15
16 from ctypes import POINTER
17 from ctypes import byref
18 from ctypes import c_char_p
19 from ctypes import c_uint
20
21 __all__ = [
22     "lib",
23     "MemoryBuffer",
24     "Module",
25     "Context",
26     "PassRegistry"
27 ]
28
29 lib = get_library()
30
31 class OpCode(object):
32     """Represents an individual OpCode enumeration."""
33
34     _value_map = {}
35
36     def __init__(self, name, value):
37         self.name = name
38         self.value = value
39
40     def __repr__(self):
41         return 'OpCode.%s' % self.name
42
43     @staticmethod
44     def from_value(value):
45         """Obtain an OpCode instance from a numeric value."""
46         result = OpCode._value_map.get(value, None)
47
48         if result is None:
49             raise ValueError('Unknown OpCode: %d' % value)
50
51         return result
52
53     @staticmethod
54     def register(name, value):
55         """Registers a new OpCode enumeration.
56
57         This is called by this module for each enumeration defined in
58         enumerations. You should not need to call this outside this module.
59         """
60         if value in OpCode._value_map:
61             raise ValueError('OpCode value already registered: %d' % value)
62
63         opcode = OpCode(name, value)
64         OpCode._value_map[value] = opcode
65         setattr(OpCode, name, opcode)
66
67 class MemoryBuffer(LLVMObject):
68     """Represents an opaque memory buffer."""
69
70     def __init__(self, filename=None):
71         """Create a new memory buffer.
72
73         Currently, we support creating from the contents of a file at the
74         specified filename.
75         """
76         if filename is None:
77             raise Exception("filename argument must be defined")
78
79         memory = c_object_p()
80         out = c_char_p(None)
81
82         result = lib.LLVMCreateMemoryBufferWithContentsOfFile(filename,
83                 byref(memory), byref(out))
84
85         if result:
86             raise Exception("Could not create memory buffer: %s" % out.value)
87
88         LLVMObject.__init__(self, memory, disposer=lib.LLVMDisposeMemoryBuffer)
89
90     def __len__(self):
91         return lib.LLVMGetBufferSize(self)
92
93 class Module(LLVMObject):
94     """Represents the top-level structure of an llvm program in an opaque object."""
95
96     def __init__(self, module, name=None, context=None):
97         LLVMObject.__init__(self, module, disposer=lib.LLVMDisposeModule)
98
99     @classmethod
100     def CreateWithName(cls, module_id):
101         m = Module(lib.LLVMModuleCreateWithName(module_id))
102         c = Context.GetGlobalContext().take_ownership(m)
103         return m
104
105     @property
106     def datalayout(self):
107         return lib.LLVMGetDataLayout(self)
108
109     @datalayout.setter
110     def datalayout(self, new_data_layout):
111         """new_data_layout is a string."""
112         lib.LLVMSetDataLayout(self, new_data_layout)
113
114     @property
115     def target(self):
116         return lib.LLVMGetTarget(self)
117
118     @target.setter
119     def target(self, new_target):
120         """new_target is a string."""
121         lib.LLVMSetTarget(self, new_target)
122
123     def dump(self):
124         lib.LLVMDumpModule(self)
125
126     def print_module_to_file(self, filename):
127         out = c_char_p(None)
128         # Result is inverted so 0 means everything was ok.
129         result = lib.LLVMPrintModuleToFile(self, filename, byref(out))        
130         if result:
131             raise RuntimeError("LLVM Error: %s" % out.value)
132
133 class Context(LLVMObject):
134
135     def __init__(self, context=None):
136         if context is None:
137             context = lib.LLVMContextCreate()
138             LLVMObject.__init__(self, context, disposer=lib.LLVMContextDispose)
139         else:
140             LLVMObject.__init__(self, context)
141
142     @classmethod
143     def GetGlobalContext(cls):
144         return Context(lib.LLVMGetGlobalContext())
145
146 class PassRegistry(LLVMObject):
147     """Represents an opaque pass registry object."""
148
149     def __init__(self):
150         LLVMObject.__init__(self,
151                             lib.LLVMGetGlobalPassRegistry())
152
153 def register_library(library):
154     # Initialization/Shutdown declarations.
155     library.LLVMInitializeCore.argtypes = [PassRegistry]
156     library.LLVMInitializeCore.restype = None
157
158     library.LLVMInitializeTransformUtils.argtypes = [PassRegistry]
159     library.LLVMInitializeTransformUtils.restype = None
160
161     library.LLVMInitializeScalarOpts.argtypes = [PassRegistry]
162     library.LLVMInitializeScalarOpts.restype = None
163
164     library.LLVMInitializeObjCARCOpts.argtypes = [PassRegistry]
165     library.LLVMInitializeObjCARCOpts.restype = None
166
167     library.LLVMInitializeVectorization.argtypes = [PassRegistry]
168     library.LLVMInitializeVectorization.restype = None
169
170     library.LLVMInitializeInstCombine.argtypes = [PassRegistry]
171     library.LLVMInitializeInstCombine.restype = None
172
173     library.LLVMInitializeIPO.argtypes = [PassRegistry]
174     library.LLVMInitializeIPO.restype = None
175
176     library.LLVMInitializeInstrumentation.argtypes = [PassRegistry]
177     library.LLVMInitializeInstrumentation.restype = None
178
179     library.LLVMInitializeAnalysis.argtypes = [PassRegistry]
180     library.LLVMInitializeAnalysis.restype = None
181
182     library.LLVMInitializeIPA.argtypes = [PassRegistry]
183     library.LLVMInitializeIPA.restype = None
184
185     library.LLVMInitializeCodeGen.argtypes = [PassRegistry]
186     library.LLVMInitializeCodeGen.restype = None
187
188     library.LLVMInitializeTarget.argtypes = [PassRegistry]
189     library.LLVMInitializeTarget.restype = None
190
191     library.LLVMShutdown.argtypes = []
192     library.LLVMShutdown.restype = None
193
194     # Pass Registry declarations.
195     library.LLVMGetGlobalPassRegistry.argtypes = []
196     library.LLVMGetGlobalPassRegistry.restype = c_object_p
197
198     # Context declarations.
199     library.LLVMContextCreate.argtypes = []
200     library.LLVMContextCreate.restype = c_object_p
201
202     library.LLVMContextDispose.argtypes = [Context]
203     library.LLVMContextDispose.restype = None
204
205     library.LLVMGetGlobalContext.argtypes = []
206     library.LLVMGetGlobalContext.restype = c_object_p
207
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
212
213     library.LLVMGetBufferSize.argtypes = [MemoryBuffer]
214
215     library.LLVMDisposeMemoryBuffer.argtypes = [MemoryBuffer]
216
217     # Module declarations
218     library.LLVMModuleCreateWithName.argtypes = [c_char_p]
219     library.LLVMModuleCreateWithName.restype = c_object_p
220
221     library.LLVMDisposeModule.argtypes = [Module]
222     library.LLVMDisposeModule.restype = None
223
224     library.LLVMGetDataLayout.argtypes = [Module]
225     library.LLVMGetDataLayout.restype = c_char_p
226
227     library.LLVMSetDataLayout.argtypes = [Module, c_char_p]
228     library.LLVMSetDataLayout.restype = None
229
230     library.LLVMGetTarget.argtypes = [Module]
231     library.LLVMGetTarget.restype = c_char_p
232
233     library.LLVMSetTarget.argtypes = [Module, c_char_p]
234     library.LLVMSetTarget.restype = None
235
236     library.LLVMDumpModule.argtypes = [Module]
237     library.LLVMDumpModule.restype = None
238
239     library.LLVMPrintModuleToFile.argtypes = [Module, c_char_p,
240                                               POINTER(c_char_p)]
241     library.LLVMPrintModuleToFile.restype = bool
242
243 def register_enumerations():
244     for name, value in enumerations.OpCodes:
245         OpCode.register(name, value)
246
247 def initialize_llvm():
248     c = Context.GetGlobalContext()
249     p = PassRegistry()
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)
262
263 register_library(lib)
264 register_enumerations()
265 initialize_llvm()