llvmlite 0.46.0b1__cp311-cp311-macosx_11_0_universal2.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of llvmlite might be problematic. Click here for more details.

Files changed (45) hide show
  1. llvmlite/__init__.py +11 -0
  2. llvmlite/_version.py +11 -0
  3. llvmlite/binding/__init__.py +18 -0
  4. llvmlite/binding/analysis.py +69 -0
  5. llvmlite/binding/common.py +34 -0
  6. llvmlite/binding/config.py +143 -0
  7. llvmlite/binding/context.py +31 -0
  8. llvmlite/binding/dylib.py +45 -0
  9. llvmlite/binding/executionengine.py +330 -0
  10. llvmlite/binding/ffi.py +395 -0
  11. llvmlite/binding/initfini.py +85 -0
  12. llvmlite/binding/libllvmlite.dylib +0 -0
  13. llvmlite/binding/linker.py +20 -0
  14. llvmlite/binding/module.py +349 -0
  15. llvmlite/binding/newpassmanagers.py +1049 -0
  16. llvmlite/binding/object_file.py +82 -0
  17. llvmlite/binding/options.py +17 -0
  18. llvmlite/binding/orcjit.py +342 -0
  19. llvmlite/binding/targets.py +462 -0
  20. llvmlite/binding/typeref.py +267 -0
  21. llvmlite/binding/value.py +632 -0
  22. llvmlite/ir/__init__.py +11 -0
  23. llvmlite/ir/_utils.py +80 -0
  24. llvmlite/ir/builder.py +1120 -0
  25. llvmlite/ir/context.py +20 -0
  26. llvmlite/ir/instructions.py +920 -0
  27. llvmlite/ir/module.py +256 -0
  28. llvmlite/ir/transforms.py +64 -0
  29. llvmlite/ir/types.py +730 -0
  30. llvmlite/ir/values.py +1217 -0
  31. llvmlite/tests/__init__.py +57 -0
  32. llvmlite/tests/__main__.py +3 -0
  33. llvmlite/tests/customize.py +407 -0
  34. llvmlite/tests/refprune_proto.py +330 -0
  35. llvmlite/tests/test_binding.py +3155 -0
  36. llvmlite/tests/test_ir.py +3095 -0
  37. llvmlite/tests/test_refprune.py +574 -0
  38. llvmlite/tests/test_valuerepr.py +60 -0
  39. llvmlite/utils.py +29 -0
  40. llvmlite-0.46.0b1.dist-info/LICENSE +24 -0
  41. llvmlite-0.46.0b1.dist-info/LICENSE.thirdparty +225 -0
  42. llvmlite-0.46.0b1.dist-info/METADATA +137 -0
  43. llvmlite-0.46.0b1.dist-info/RECORD +45 -0
  44. llvmlite-0.46.0b1.dist-info/WHEEL +5 -0
  45. llvmlite-0.46.0b1.dist-info/top_level.txt +1 -0
@@ -0,0 +1,349 @@
1
+ from ctypes import (c_char_p, byref, POINTER, c_bool, create_string_buffer,
2
+ c_size_t, string_at)
3
+
4
+ from llvmlite.binding import ffi
5
+ from llvmlite.binding.linker import link_modules
6
+ from llvmlite.binding.common import _decode_string, _encode_string
7
+ from llvmlite.binding.value import ValueRef, TypeRef
8
+ from llvmlite.binding.context import get_global_context
9
+
10
+
11
+ def parse_assembly(llvmir, context=None):
12
+ """
13
+ Create Module from a LLVM IR string
14
+ """
15
+ if context is None:
16
+ context = get_global_context()
17
+ llvmir = _encode_string(llvmir)
18
+ strbuf = c_char_p(llvmir)
19
+ with ffi.OutputString() as errmsg:
20
+ mod = ModuleRef(
21
+ ffi.lib.LLVMPY_ParseAssembly(context, strbuf, errmsg),
22
+ context)
23
+ if errmsg:
24
+ mod.close()
25
+ raise RuntimeError("LLVM IR parsing error\n{0}".format(errmsg))
26
+ return mod
27
+
28
+
29
+ def parse_bitcode(bitcode, context=None):
30
+ """
31
+ Create Module from a LLVM *bitcode* (a bytes object).
32
+ """
33
+ if context is None:
34
+ context = get_global_context()
35
+ buf = c_char_p(bitcode)
36
+ bufsize = len(bitcode)
37
+ with ffi.OutputString() as errmsg:
38
+ mod = ModuleRef(ffi.lib.LLVMPY_ParseBitcode(
39
+ context, buf, bufsize, errmsg), context)
40
+ if errmsg:
41
+ mod.close()
42
+ raise RuntimeError(
43
+ "LLVM bitcode parsing error\n{0}".format(errmsg))
44
+ return mod
45
+
46
+
47
+ class ModuleRef(ffi.ObjectRef):
48
+ """
49
+ A reference to a LLVM module.
50
+ """
51
+
52
+ def __init__(self, module_ptr, context):
53
+ super(ModuleRef, self).__init__(module_ptr)
54
+ self._context = context
55
+
56
+ def __str__(self):
57
+ with ffi.OutputString() as outstr:
58
+ ffi.lib.LLVMPY_PrintModuleToString(self, outstr)
59
+ return str(outstr)
60
+
61
+ def as_bitcode(self):
62
+ """
63
+ Return the module's LLVM bitcode, as a bytes object.
64
+ """
65
+ ptr = c_char_p(None)
66
+ size = c_size_t(-1)
67
+ ffi.lib.LLVMPY_WriteBitcodeToString(self, byref(ptr), byref(size))
68
+ if not ptr:
69
+ raise MemoryError
70
+ try:
71
+ assert size.value >= 0
72
+ return string_at(ptr, size.value)
73
+ finally:
74
+ ffi.lib.LLVMPY_DisposeString(ptr)
75
+
76
+ def _dispose(self):
77
+ self._capi.LLVMPY_DisposeModule(self)
78
+
79
+ def get_function(self, name):
80
+ """
81
+ Get a ValueRef pointing to the function named *name*.
82
+ NameError is raised if the symbol isn't found.
83
+ """
84
+ p = ffi.lib.LLVMPY_GetNamedFunction(self, _encode_string(name))
85
+ if not p:
86
+ raise NameError(name)
87
+ return ValueRef(p, 'function', dict(module=self))
88
+
89
+ def get_global_variable(self, name):
90
+ """
91
+ Get a ValueRef pointing to the global variable named *name*.
92
+ NameError is raised if the symbol isn't found.
93
+ """
94
+ p = ffi.lib.LLVMPY_GetNamedGlobalVariable(self, _encode_string(name))
95
+ if not p:
96
+ raise NameError(name)
97
+ return ValueRef(p, 'global', dict(module=self))
98
+
99
+ def get_struct_type(self, name):
100
+ """
101
+ Get a TypeRef pointing to a structure type named *name*.
102
+ NameError is raised if the struct type isn't found.
103
+ """
104
+ p = ffi.lib.LLVMPY_GetNamedStructType(self, _encode_string(name))
105
+ if not p:
106
+ raise NameError(name)
107
+ return TypeRef(p)
108
+
109
+ def verify(self):
110
+ """
111
+ Verify the module IR's correctness. RuntimeError is raised on error.
112
+ """
113
+ with ffi.OutputString() as outmsg:
114
+ if ffi.lib.LLVMPY_VerifyModule(self, outmsg):
115
+ raise RuntimeError(str(outmsg))
116
+
117
+ @property
118
+ def name(self):
119
+ """
120
+ The module's identifier.
121
+ """
122
+ return _decode_string(ffi.lib.LLVMPY_GetModuleName(self))
123
+
124
+ @name.setter
125
+ def name(self, value):
126
+ ffi.lib.LLVMPY_SetModuleName(self, _encode_string(value))
127
+
128
+ @property
129
+ def source_file(self):
130
+ """
131
+ The module's original source file name
132
+ """
133
+ return _decode_string(ffi.lib.LLVMPY_GetModuleSourceFileName(self))
134
+
135
+ @property
136
+ def data_layout(self):
137
+ """
138
+ This module's data layout specification, as a string.
139
+ """
140
+ # LLVMGetDataLayout() points inside a std::string managed by LLVM.
141
+ with ffi.OutputString(owned=False) as outmsg:
142
+ ffi.lib.LLVMPY_GetDataLayout(self, outmsg)
143
+ return str(outmsg)
144
+
145
+ @data_layout.setter
146
+ def data_layout(self, strrep):
147
+ ffi.lib.LLVMPY_SetDataLayout(self,
148
+ create_string_buffer(
149
+ strrep.encode('utf8')))
150
+
151
+ @property
152
+ def triple(self):
153
+ """
154
+ This module's target "triple" specification, as a string.
155
+ """
156
+ # LLVMGetTarget() points inside a std::string managed by LLVM.
157
+ with ffi.OutputString(owned=False) as outmsg:
158
+ ffi.lib.LLVMPY_GetTarget(self, outmsg)
159
+ return str(outmsg)
160
+
161
+ @triple.setter
162
+ def triple(self, strrep):
163
+ ffi.lib.LLVMPY_SetTarget(self,
164
+ create_string_buffer(
165
+ strrep.encode('utf8')))
166
+
167
+ def link_in(self, other, preserve=False):
168
+ """
169
+ Link the *other* module into this one. The *other* module will
170
+ be destroyed unless *preserve* is true.
171
+ """
172
+ if preserve:
173
+ other = other.clone()
174
+ link_modules(self, other)
175
+
176
+ @property
177
+ def global_variables(self):
178
+ """
179
+ Return an iterator over this module's global variables.
180
+ The iterator will yield a ValueRef for each global variable.
181
+
182
+ Note that global variables don't include functions
183
+ (a function is a "global value" but not a "global variable" in
184
+ LLVM parlance)
185
+ """
186
+ it = ffi.lib.LLVMPY_ModuleGlobalsIter(self)
187
+ return _GlobalsIterator(it, dict(module=self))
188
+
189
+ @property
190
+ def functions(self):
191
+ """
192
+ Return an iterator over this module's functions.
193
+ The iterator will yield a ValueRef for each function.
194
+ """
195
+ it = ffi.lib.LLVMPY_ModuleFunctionsIter(self)
196
+ return _FunctionsIterator(it, dict(module=self))
197
+
198
+ @property
199
+ def struct_types(self):
200
+ """
201
+ Return an iterator over the struct types defined in
202
+ the module. The iterator will yield a TypeRef.
203
+ """
204
+ it = ffi.lib.LLVMPY_ModuleTypesIter(self)
205
+ return _TypesIterator(it, dict(module=self))
206
+
207
+ def clone(self):
208
+ return ModuleRef(ffi.lib.LLVMPY_CloneModule(self), self._context)
209
+
210
+
211
+ class _Iterator(ffi.ObjectRef):
212
+
213
+ kind = None
214
+
215
+ def __init__(self, ptr, parents):
216
+ ffi.ObjectRef.__init__(self, ptr)
217
+ self._parents = parents
218
+ assert self.kind is not None
219
+
220
+ def __next__(self):
221
+ vp = self._next()
222
+ if vp:
223
+ return ValueRef(vp, self.kind, self._parents)
224
+ else:
225
+ raise StopIteration
226
+
227
+ next = __next__
228
+
229
+ def __iter__(self):
230
+ return self
231
+
232
+
233
+ class _GlobalsIterator(_Iterator):
234
+
235
+ kind = 'global'
236
+
237
+ def _dispose(self):
238
+ self._capi.LLVMPY_DisposeGlobalsIter(self)
239
+
240
+ def _next(self):
241
+ return ffi.lib.LLVMPY_GlobalsIterNext(self)
242
+
243
+
244
+ class _FunctionsIterator(_Iterator):
245
+
246
+ kind = 'function'
247
+
248
+ def _dispose(self):
249
+ self._capi.LLVMPY_DisposeFunctionsIter(self)
250
+
251
+ def _next(self):
252
+ return ffi.lib.LLVMPY_FunctionsIterNext(self)
253
+
254
+
255
+ class _TypesIterator(_Iterator):
256
+
257
+ kind = 'type'
258
+
259
+ def _dispose(self):
260
+ self._capi.LLVMPY_DisposeTypesIter(self)
261
+
262
+ def __next__(self):
263
+ vp = self._next()
264
+ if vp:
265
+ return TypeRef(vp)
266
+ else:
267
+ raise StopIteration
268
+
269
+ def _next(self):
270
+ return ffi.lib.LLVMPY_TypesIterNext(self)
271
+
272
+ next = __next__
273
+
274
+
275
+ # =============================================================================
276
+ # Set function FFI
277
+
278
+ ffi.lib.LLVMPY_ParseAssembly.argtypes = [ffi.LLVMContextRef,
279
+ c_char_p,
280
+ POINTER(c_char_p)]
281
+ ffi.lib.LLVMPY_ParseAssembly.restype = ffi.LLVMModuleRef
282
+
283
+ ffi.lib.LLVMPY_ParseBitcode.argtypes = [ffi.LLVMContextRef,
284
+ c_char_p, c_size_t,
285
+ POINTER(c_char_p)]
286
+ ffi.lib.LLVMPY_ParseBitcode.restype = ffi.LLVMModuleRef
287
+
288
+ ffi.lib.LLVMPY_DisposeModule.argtypes = [ffi.LLVMModuleRef]
289
+
290
+ ffi.lib.LLVMPY_PrintModuleToString.argtypes = [ffi.LLVMModuleRef,
291
+ POINTER(c_char_p)]
292
+ ffi.lib.LLVMPY_WriteBitcodeToString.argtypes = [ffi.LLVMModuleRef,
293
+ POINTER(c_char_p),
294
+ POINTER(c_size_t)]
295
+
296
+ ffi.lib.LLVMPY_GetNamedFunction.argtypes = [ffi.LLVMModuleRef,
297
+ c_char_p]
298
+ ffi.lib.LLVMPY_GetNamedFunction.restype = ffi.LLVMValueRef
299
+
300
+ ffi.lib.LLVMPY_VerifyModule.argtypes = [ffi.LLVMModuleRef,
301
+ POINTER(c_char_p)]
302
+ ffi.lib.LLVMPY_VerifyModule.restype = c_bool
303
+
304
+ ffi.lib.LLVMPY_GetDataLayout.argtypes = [ffi.LLVMModuleRef, POINTER(c_char_p)]
305
+ ffi.lib.LLVMPY_SetDataLayout.argtypes = [ffi.LLVMModuleRef, c_char_p]
306
+
307
+ ffi.lib.LLVMPY_GetTarget.argtypes = [ffi.LLVMModuleRef, POINTER(c_char_p)]
308
+ ffi.lib.LLVMPY_SetTarget.argtypes = [ffi.LLVMModuleRef, c_char_p]
309
+
310
+ ffi.lib.LLVMPY_GetNamedGlobalVariable.argtypes = [ffi.LLVMModuleRef, c_char_p]
311
+ ffi.lib.LLVMPY_GetNamedGlobalVariable.restype = ffi.LLVMValueRef
312
+
313
+ ffi.lib.LLVMPY_GetNamedStructType.argtypes = [ffi.LLVMModuleRef, c_char_p]
314
+ ffi.lib.LLVMPY_GetNamedStructType.restype = ffi.LLVMTypeRef
315
+
316
+ ffi.lib.LLVMPY_ModuleGlobalsIter.argtypes = [ffi.LLVMModuleRef]
317
+ ffi.lib.LLVMPY_ModuleGlobalsIter.restype = ffi.LLVMGlobalsIterator
318
+
319
+ ffi.lib.LLVMPY_DisposeGlobalsIter.argtypes = [ffi.LLVMGlobalsIterator]
320
+
321
+ ffi.lib.LLVMPY_GlobalsIterNext.argtypes = [ffi.LLVMGlobalsIterator]
322
+ ffi.lib.LLVMPY_GlobalsIterNext.restype = ffi.LLVMValueRef
323
+
324
+ ffi.lib.LLVMPY_ModuleFunctionsIter.argtypes = [ffi.LLVMModuleRef]
325
+ ffi.lib.LLVMPY_ModuleFunctionsIter.restype = ffi.LLVMFunctionsIterator
326
+
327
+ ffi.lib.LLVMPY_ModuleTypesIter.argtypes = [ffi.LLVMModuleRef]
328
+ ffi.lib.LLVMPY_ModuleTypesIter.restype = ffi.LLVMTypesIterator
329
+
330
+ ffi.lib.LLVMPY_DisposeFunctionsIter.argtypes = [ffi.LLVMFunctionsIterator]
331
+
332
+ ffi.lib.LLVMPY_DisposeTypesIter.argtypes = [ffi.LLVMTypesIterator]
333
+
334
+ ffi.lib.LLVMPY_FunctionsIterNext.argtypes = [ffi.LLVMFunctionsIterator]
335
+ ffi.lib.LLVMPY_FunctionsIterNext.restype = ffi.LLVMValueRef
336
+
337
+ ffi.lib.LLVMPY_TypesIterNext.argtypes = [ffi.LLVMTypesIterator]
338
+ ffi.lib.LLVMPY_TypesIterNext.restype = ffi.LLVMTypeRef
339
+
340
+ ffi.lib.LLVMPY_CloneModule.argtypes = [ffi.LLVMModuleRef]
341
+ ffi.lib.LLVMPY_CloneModule.restype = ffi.LLVMModuleRef
342
+
343
+ ffi.lib.LLVMPY_GetModuleName.argtypes = [ffi.LLVMModuleRef]
344
+ ffi.lib.LLVMPY_GetModuleName.restype = c_char_p
345
+
346
+ ffi.lib.LLVMPY_SetModuleName.argtypes = [ffi.LLVMModuleRef, c_char_p]
347
+
348
+ ffi.lib.LLVMPY_GetModuleSourceFileName.argtypes = [ffi.LLVMModuleRef]
349
+ ffi.lib.LLVMPY_GetModuleSourceFileName.restype = c_char_p