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