76d1b14e6312b877b9d4cd74ef294dd88f099d9f
[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
20 __all__ = [
21     "lib",
22     "MemoryBuffer",
23     "Context",
24     "PassRegistry"
25 ]
26
27 lib = get_library()
28
29 class OpCode(object):
30     """Represents an individual OpCode enumeration."""
31
32     _value_map = {}
33
34     def __init__(self, name, value):
35         self.name = name
36         self.value = value
37
38     def __repr__(self):
39         return 'OpCode.%s' % self.name
40
41     @staticmethod
42     def from_value(value):
43         """Obtain an OpCode instance from a numeric value."""
44         result = OpCode._value_map.get(value, None)
45
46         if result is None:
47             raise ValueError('Unknown OpCode: %d' % value)
48
49         return result
50
51     @staticmethod
52     def register(name, value):
53         """Registers a new OpCode enumeration.
54
55         This is called by this module for each enumeration defined in
56         enumerations. You should not need to call this outside this module.
57         """
58         if value in OpCode._value_map:
59             raise ValueError('OpCode value already registered: %d' % value)
60
61         opcode = OpCode(name, value)
62         OpCode._value_map[value] = opcode
63         setattr(OpCode, name, opcode)
64
65 class MemoryBuffer(LLVMObject):
66     """Represents an opaque memory buffer."""
67
68     def __init__(self, filename=None):
69         """Create a new memory buffer.
70
71         Currently, we support creating from the contents of a file at the
72         specified filename.
73         """
74         if filename is None:
75             raise Exception("filename argument must be defined")
76
77         memory = c_object_p()
78         out = c_char_p(None)
79
80         result = lib.LLVMCreateMemoryBufferWithContentsOfFile(filename,
81                 byref(memory), byref(out))
82
83         if result:
84             raise Exception("Could not create memory buffer: %s" % out.value)
85
86         LLVMObject.__init__(self, memory, disposer=lib.LLVMDisposeMemoryBuffer)
87
88     def __len__(self):
89         return lib.LLVMGetBufferSize(self)
90
91 class Context(LLVMObject):
92
93     def __init__(self, context=None):
94         if context is None:
95             context = lib.LLVMContextCreate()
96             LLVMObject.__init__(self, context, disposer=lib.LLVMContextDispose)
97         else:
98             LLVMObject.__init__(self, context)
99
100     @classmethod
101     def GetGlobalContext(cls):
102         return Context(lib.LLVMGetGlobalContext())
103
104 class PassRegistry(LLVMObject):
105     """Represents an opaque pass registry object."""
106
107     def __init__(self):
108         LLVMObject.__init__(self,
109                             lib.LLVMGetGlobalPassRegistry())
110
111 def register_library(library):
112     # Initialization/Shutdown declarations.
113     library.LLVMInitializeCore.argtypes = [PassRegistry]
114     library.LLVMInitializeCore.restype = None
115
116     library.LLVMInitializeTransformUtils.argtypes = [PassRegistry]
117     library.LLVMInitializeTransformUtils.restype = None
118
119     library.LLVMInitializeScalarOpts.argtypes = [PassRegistry]
120     library.LLVMInitializeScalarOpts.restype = None
121
122     library.LLVMInitializeObjCARCOpts.argtypes = [PassRegistry]
123     library.LLVMInitializeObjCARCOpts.restype = None
124
125     library.LLVMInitializeVectorization.argtypes = [PassRegistry]
126     library.LLVMInitializeVectorization.restype = None
127
128     library.LLVMInitializeInstCombine.argtypes = [PassRegistry]
129     library.LLVMInitializeInstCombine.restype = None
130
131     library.LLVMInitializeIPO.argtypes = [PassRegistry]
132     library.LLVMInitializeIPO.restype = None
133
134     library.LLVMInitializeInstrumentation.argtypes = [PassRegistry]
135     library.LLVMInitializeInstrumentation.restype = None
136
137     library.LLVMInitializeAnalysis.argtypes = [PassRegistry]
138     library.LLVMInitializeAnalysis.restype = None
139
140     library.LLVMInitializeIPA.argtypes = [PassRegistry]
141     library.LLVMInitializeIPA.restype = None
142
143     library.LLVMInitializeCodeGen.argtypes = [PassRegistry]
144     library.LLVMInitializeCodeGen.restype = None
145
146     library.LLVMInitializeTarget.argtypes = [PassRegistry]
147     library.LLVMInitializeTarget.restype = None
148
149     library.LLVMShutdown.argtypes = []
150     library.LLVMShutdown.restype = None
151
152     # Pass Registry declarations.
153     library.LLVMGetGlobalPassRegistry.argtypes = []
154     library.LLVMGetGlobalPassRegistry.restype = c_object_p
155
156     # Context declarations.
157     library.LLVMContextCreate.argtypes = []
158     library.LLVMContextCreate.restype = c_object_p
159
160     library.LLVMContextDispose.argtypes = [Context]
161     library.LLVMContextDispose.restype = None
162
163     library.LLVMGetGlobalContext.argtypes = []
164     library.LLVMGetGlobalContext.restype = c_object_p
165
166     # Memory buffer declarations
167     library.LLVMCreateMemoryBufferWithContentsOfFile.argtypes = [c_char_p,
168             POINTER(c_object_p), POINTER(c_char_p)]
169     library.LLVMCreateMemoryBufferWithContentsOfFile.restype = bool
170
171     library.LLVMGetBufferSize.argtypes = [MemoryBuffer]
172
173     library.LLVMDisposeMemoryBuffer.argtypes = [MemoryBuffer]
174
175 def register_enumerations():
176     for name, value in enumerations.OpCodes:
177         OpCode.register(name, value)
178
179 def initialize_llvm():
180     c = Context.GetGlobalContext()
181     p = PassRegistry()
182     lib.LLVMInitializeCore(p)
183     lib.LLVMInitializeTransformUtils(p)
184     lib.LLVMInitializeScalarOpts(p)
185     lib.LLVMInitializeObjCARCOpts(p)
186     lib.LLVMInitializeVectorization(p)
187     lib.LLVMInitializeInstCombine(p)
188     lib.LLVMInitializeIPO(p)
189     lib.LLVMInitializeInstrumentation(p)
190     lib.LLVMInitializeAnalysis(p)
191     lib.LLVMInitializeIPA(p)
192     lib.LLVMInitializeCodeGen(p)
193     lib.LLVMInitializeTarget(p)
194
195 register_library(lib)
196 register_enumerations()
197 initialize_llvm()