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