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
29 """Represents an individual OpCode enumeration."""
33 def __init__(self, name, value):
38 return 'OpCode.%s' % self.name
41 def from_value(value):
42 """Obtain an OpCode instance from a numeric value."""
43 result = OpCode._value_map.get(value, None)
46 raise ValueError('Unknown OpCode: %d' % value)
51 def register(name, value):
52 """Registers a new OpCode enumeration.
54 This is called by this module for each enumeration defined in
55 enumerations. You should not need to call this outside this module.
57 if value in OpCode._value_map:
58 raise ValueError('OpCode value already registered: %d' % value)
60 opcode = OpCode(name, value)
61 OpCode._value_map[value] = opcode
62 setattr(OpCode, name, opcode)
64 class MemoryBuffer(LLVMObject):
65 """Represents an opaque memory buffer."""
67 def __init__(self, filename=None):
68 """Create a new memory buffer.
70 Currently, we support creating from the contents of a file at the
74 raise Exception("filename argument must be defined")
79 result = lib.LLVMCreateMemoryBufferWithContentsOfFile(filename,
80 byref(memory), byref(out))
83 raise Exception("Could not create memory buffer: %s" % out.value)
85 LLVMObject.__init__(self, memory, disposer=lib.LLVMDisposeMemoryBuffer)
88 return lib.LLVMGetBufferSize(self)
90 class PassRegistry(LLVMObject):
91 """Represents an opaque pass registry object."""
94 LLVMObject.__init__(self,
95 lib.LLVMGetGlobalPassRegistry())
97 def register_library(library):
98 # Initialization/Shutdown declarations.
99 library.LLVMInitializeCore.argtypes = [PassRegistry]
100 library.LLVMInitializeCore.restype = None
102 library.LLVMInitializeTransformUtils.argtypes = [PassRegistry]
103 library.LLVMInitializeTransformUtils.restype = None
105 library.LLVMInitializeScalarOpts.argtypes = [PassRegistry]
106 library.LLVMInitializeScalarOpts.restype = None
108 library.LLVMInitializeObjCARCOpts.argtypes = [PassRegistry]
109 library.LLVMInitializeObjCARCOpts.restype = None
111 library.LLVMInitializeVectorization.argtypes = [PassRegistry]
112 library.LLVMInitializeVectorization.restype = None
114 library.LLVMInitializeInstCombine.argtypes = [PassRegistry]
115 library.LLVMInitializeInstCombine.restype = None
117 library.LLVMInitializeIPO.argtypes = [PassRegistry]
118 library.LLVMInitializeIPO.restype = None
120 library.LLVMInitializeInstrumentation.argtypes = [PassRegistry]
121 library.LLVMInitializeInstrumentation.restype = None
123 library.LLVMInitializeAnalysis.argtypes = [PassRegistry]
124 library.LLVMInitializeAnalysis.restype = None
126 library.LLVMInitializeIPA.argtypes = [PassRegistry]
127 library.LLVMInitializeIPA.restype = None
129 library.LLVMInitializeCodeGen.argtypes = [PassRegistry]
130 library.LLVMInitializeCodeGen.restype = None
132 library.LLVMInitializeTarget.argtypes = [PassRegistry]
133 library.LLVMInitializeTarget.restype = None
135 library.LLVMShutdown.argtypes = []
136 library.LLVMShutdown.restype = None
138 # Pass Registry declarations.
139 library.LLVMGetGlobalPassRegistry.argtypes = []
140 library.LLVMGetGlobalPassRegistry.restype = c_object_p
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
147 library.LLVMGetBufferSize.argtypes = [MemoryBuffer]
149 library.LLVMDisposeMemoryBuffer.argtypes = [MemoryBuffer]
151 def register_enumerations():
152 for name, value in enumerations.OpCodes:
153 OpCode.register(name, value)
155 def initialize_llvm():
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)
170 register_library(lib)
171 register_enumerations()