llvmlite 0.42.0rc1__cp311-cp311-win_amd64.whl → 0.43.0rc1__cp311-cp311-win_amd64.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 (46) hide show
  1. llvmlite/__init__.py +3 -3
  2. llvmlite/_version.py +2 -2
  3. llvmlite/binding/__init__.py +18 -18
  4. llvmlite/binding/analysis.py +69 -69
  5. llvmlite/binding/common.py +34 -34
  6. llvmlite/binding/context.py +29 -29
  7. llvmlite/binding/dylib.py +45 -45
  8. llvmlite/binding/executionengine.py +330 -330
  9. llvmlite/binding/ffi.py +390 -390
  10. llvmlite/binding/initfini.py +73 -73
  11. llvmlite/binding/linker.py +20 -20
  12. llvmlite/binding/llvmlite.dll +0 -0
  13. llvmlite/binding/module.py +349 -349
  14. llvmlite/binding/object_file.py +82 -82
  15. llvmlite/binding/options.py +17 -17
  16. llvmlite/binding/orcjit.py +342 -342
  17. llvmlite/binding/passmanagers.py +939 -932
  18. llvmlite/binding/targets.py +450 -450
  19. llvmlite/binding/transforms.py +151 -151
  20. llvmlite/binding/typeref.py +198 -198
  21. llvmlite/binding/value.py +618 -618
  22. llvmlite/ir/__init__.py +11 -11
  23. llvmlite/ir/_utils.py +80 -80
  24. llvmlite/ir/builder.py +1119 -1119
  25. llvmlite/ir/context.py +20 -20
  26. llvmlite/ir/instructions.py +893 -893
  27. llvmlite/ir/module.py +246 -246
  28. llvmlite/ir/transforms.py +64 -64
  29. llvmlite/ir/types.py +614 -614
  30. llvmlite/ir/values.py +1217 -1217
  31. llvmlite/tests/__init__.py +57 -57
  32. llvmlite/tests/__main__.py +3 -3
  33. llvmlite/tests/customize.py +407 -407
  34. llvmlite/tests/refprune_proto.py +329 -329
  35. llvmlite/tests/test_binding.py +2585 -2582
  36. llvmlite/tests/test_ir.py +2729 -2729
  37. llvmlite/tests/test_refprune.py +557 -526
  38. llvmlite/tests/test_valuerepr.py +60 -60
  39. llvmlite/utils.py +29 -29
  40. {llvmlite-0.42.0rc1.dist-info → llvmlite-0.43.0rc1.dist-info}/LICENSE +24 -24
  41. {llvmlite-0.42.0rc1.dist-info → llvmlite-0.43.0rc1.dist-info}/LICENSE.thirdparty +225 -225
  42. {llvmlite-0.42.0rc1.dist-info → llvmlite-0.43.0rc1.dist-info}/METADATA +1 -1
  43. llvmlite-0.43.0rc1.dist-info/RECORD +45 -0
  44. {llvmlite-0.42.0rc1.dist-info → llvmlite-0.43.0rc1.dist-info}/WHEEL +1 -1
  45. llvmlite-0.42.0rc1.dist-info/RECORD +0 -45
  46. {llvmlite-0.42.0rc1.dist-info → llvmlite-0.43.0rc1.dist-info}/top_level.txt +0 -0
llvmlite/ir/types.py CHANGED
@@ -1,614 +1,614 @@
1
- """
2
- Classes that are LLVM types
3
- """
4
-
5
- import struct
6
-
7
- from llvmlite.ir._utils import _StrCaching
8
-
9
-
10
- def _wrapname(x):
11
- return '"{0}"'.format(x.replace('\\', '\\5c').replace('"', '\\22'))
12
-
13
-
14
- class Type(_StrCaching):
15
- """
16
- The base class for all LLVM types.
17
- """
18
- is_pointer = False
19
- null = 'zeroinitializer'
20
-
21
- def __repr__(self):
22
- return "<%s %s>" % (type(self), str(self))
23
-
24
- def _to_string(self):
25
- raise NotImplementedError
26
-
27
- def as_pointer(self, addrspace=0):
28
- return PointerType(self, addrspace)
29
-
30
- def __ne__(self, other):
31
- return not (self == other)
32
-
33
- def _get_ll_pointer_type(self, target_data, context=None):
34
- """
35
- Convert this type object to an LLVM type.
36
- """
37
- from llvmlite.ir import Module, GlobalVariable
38
- from llvmlite.binding import parse_assembly
39
-
40
- if context is None:
41
- m = Module()
42
- else:
43
- m = Module(context=context)
44
- foo = GlobalVariable(m, self, name="foo")
45
- with parse_assembly(str(m)) as llmod:
46
- return llmod.get_global_variable(foo.name).type
47
-
48
- def get_abi_size(self, target_data, context=None):
49
- """
50
- Get the ABI size of this type according to data layout *target_data*.
51
- """
52
- llty = self._get_ll_pointer_type(target_data, context)
53
- return target_data.get_pointee_abi_size(llty)
54
-
55
- def get_abi_alignment(self, target_data, context=None):
56
- """
57
- Get the minimum ABI alignment of this type according to data layout
58
- *target_data*.
59
- """
60
- llty = self._get_ll_pointer_type(target_data, context)
61
- return target_data.get_pointee_abi_alignment(llty)
62
-
63
- def format_constant(self, value):
64
- """
65
- Format constant *value* of this type. This method may be overriden
66
- by subclasses.
67
- """
68
- return str(value)
69
-
70
- def wrap_constant_value(self, value):
71
- """
72
- Wrap constant *value* if necessary. This method may be overriden
73
- by subclasses (especially aggregate types).
74
- """
75
- return value
76
-
77
- def __call__(self, value):
78
- """
79
- Create a LLVM constant of this type with the given Python value.
80
- """
81
- from llvmlite.ir import Constant
82
- return Constant(self, value)
83
-
84
-
85
- class MetaDataType(Type):
86
-
87
- def _to_string(self):
88
- return "metadata"
89
-
90
- def as_pointer(self):
91
- raise TypeError
92
-
93
- def __eq__(self, other):
94
- return isinstance(other, MetaDataType)
95
-
96
- def __hash__(self):
97
- return hash(MetaDataType)
98
-
99
-
100
- class LabelType(Type):
101
- """
102
- The label type is the type of e.g. basic blocks.
103
- """
104
-
105
- def _to_string(self):
106
- return "label"
107
-
108
-
109
- class PointerType(Type):
110
- """
111
- The type of all pointer values.
112
- """
113
- is_pointer = True
114
- null = 'null'
115
-
116
- def __init__(self, pointee, addrspace=0):
117
- assert not isinstance(pointee, VoidType)
118
- self.pointee = pointee
119
- self.addrspace = addrspace
120
-
121
- def _to_string(self):
122
- if self.addrspace != 0:
123
- return "{0} addrspace({1})*".format(self.pointee, self.addrspace)
124
- else:
125
- return "{0}*".format(self.pointee)
126
-
127
- def __eq__(self, other):
128
- if isinstance(other, PointerType):
129
- return (self.pointee, self.addrspace) == (other.pointee,
130
- other.addrspace)
131
- else:
132
- return False
133
-
134
- def __hash__(self):
135
- return hash(PointerType)
136
-
137
- def gep(self, i):
138
- """
139
- Resolve the type of the i-th element (for getelementptr lookups).
140
- """
141
- if not isinstance(i.type, IntType):
142
- raise TypeError(i.type)
143
- return self.pointee
144
-
145
- @property
146
- def intrinsic_name(self):
147
- return 'p%d%s' % (self.addrspace, self.pointee.intrinsic_name)
148
-
149
-
150
- class VoidType(Type):
151
- """
152
- The type for empty values (e.g. a function returning no value).
153
- """
154
-
155
- def _to_string(self):
156
- return 'void'
157
-
158
- def __eq__(self, other):
159
- return isinstance(other, VoidType)
160
-
161
- def __hash__(self):
162
- return hash(VoidType)
163
-
164
-
165
- class FunctionType(Type):
166
- """
167
- The type for functions.
168
- """
169
-
170
- def __init__(self, return_type, args, var_arg=False):
171
- self.return_type = return_type
172
- self.args = tuple(args)
173
- self.var_arg = var_arg
174
-
175
- def _to_string(self):
176
- if self.args:
177
- strargs = ', '.join([str(a) for a in self.args])
178
- if self.var_arg:
179
- return '{0} ({1}, ...)'.format(self.return_type, strargs)
180
- else:
181
- return '{0} ({1})'.format(self.return_type, strargs)
182
- elif self.var_arg:
183
- return '{0} (...)'.format(self.return_type)
184
- else:
185
- return '{0} ()'.format(self.return_type)
186
-
187
- def __eq__(self, other):
188
- if isinstance(other, FunctionType):
189
- return (self.return_type == other.return_type and
190
- self.args == other.args and self.var_arg == other.var_arg)
191
- else:
192
- return False
193
-
194
- def __hash__(self):
195
- return hash(FunctionType)
196
-
197
-
198
- class IntType(Type):
199
- """
200
- The type for integers.
201
- """
202
- null = '0'
203
- _instance_cache = {}
204
- width: int
205
-
206
- def __new__(cls, bits):
207
- # Cache all common integer types
208
- if 0 <= bits <= 128:
209
- try:
210
- return cls._instance_cache[bits]
211
- except KeyError:
212
- inst = cls._instance_cache[bits] = cls.__new(bits)
213
- return inst
214
- return cls.__new(bits)
215
-
216
- @classmethod
217
- def __new(cls, bits):
218
- assert isinstance(bits, int) and bits >= 0
219
- self = super(IntType, cls).__new__(cls)
220
- self.width = bits
221
- return self
222
-
223
- def __getnewargs__(self):
224
- return self.width,
225
-
226
- def __copy__(self):
227
- return self
228
-
229
- def _to_string(self):
230
- return 'i%u' % (self.width,)
231
-
232
- def __eq__(self, other):
233
- if isinstance(other, IntType):
234
- return self.width == other.width
235
- else:
236
- return False
237
-
238
- def __hash__(self):
239
- return hash(IntType)
240
-
241
- def format_constant(self, val):
242
- if isinstance(val, bool):
243
- return str(val).lower()
244
- else:
245
- return str(val)
246
-
247
- def wrap_constant_value(self, val):
248
- if val is None:
249
- return 0
250
- return val
251
-
252
- @property
253
- def intrinsic_name(self):
254
- return str(self)
255
-
256
-
257
- def _as_float(value):
258
- """
259
- Truncate to single-precision float.
260
- """
261
- return struct.unpack('f', struct.pack('f', value))[0]
262
-
263
-
264
- def _as_half(value):
265
- """
266
- Truncate to half-precision float.
267
- """
268
- try:
269
- return struct.unpack('e', struct.pack('e', value))[0]
270
- except struct.error:
271
- # 'e' only added in Python 3.6+
272
- return _as_float(value)
273
-
274
-
275
- def _format_float_as_hex(value, packfmt, unpackfmt, numdigits):
276
- raw = struct.pack(packfmt, float(value))
277
- intrep = struct.unpack(unpackfmt, raw)[0]
278
- out = '{{0:#{0}x}}'.format(numdigits).format(intrep)
279
- return out
280
-
281
-
282
- def _format_double(value):
283
- """
284
- Format *value* as a hexadecimal string of its IEEE double precision
285
- representation.
286
- """
287
- return _format_float_as_hex(value, 'd', 'Q', 16)
288
-
289
-
290
- class _BaseFloatType(Type):
291
-
292
- def __new__(cls):
293
- return cls._instance_cache
294
-
295
- def __eq__(self, other):
296
- return isinstance(other, type(self))
297
-
298
- def __hash__(self):
299
- return hash(type(self))
300
-
301
- @classmethod
302
- def _create_instance(cls):
303
- cls._instance_cache = super(_BaseFloatType, cls).__new__(cls)
304
-
305
-
306
- class HalfType(_BaseFloatType):
307
- """
308
- The type for single-precision floats.
309
- """
310
- null = '0.0'
311
- intrinsic_name = 'f16'
312
-
313
- def __str__(self):
314
- return 'half'
315
-
316
- def format_constant(self, value):
317
- return _format_double(_as_half(value))
318
-
319
-
320
- class FloatType(_BaseFloatType):
321
- """
322
- The type for single-precision floats.
323
- """
324
- null = '0.0'
325
- intrinsic_name = 'f32'
326
-
327
- def __str__(self):
328
- return 'float'
329
-
330
- def format_constant(self, value):
331
- return _format_double(_as_float(value))
332
-
333
-
334
- class DoubleType(_BaseFloatType):
335
- """
336
- The type for double-precision floats.
337
- """
338
- null = '0.0'
339
- intrinsic_name = 'f64'
340
-
341
- def __str__(self):
342
- return 'double'
343
-
344
- def format_constant(self, value):
345
- return _format_double(value)
346
-
347
-
348
- for _cls in (HalfType, FloatType, DoubleType):
349
- _cls._create_instance()
350
-
351
-
352
- class _Repeat(object):
353
- def __init__(self, value, size):
354
- self.value = value
355
- self.size = size
356
-
357
- def __len__(self):
358
- return self.size
359
-
360
- def __getitem__(self, item):
361
- if 0 <= item < self.size:
362
- return self.value
363
- else:
364
- raise IndexError(item)
365
-
366
-
367
- class VectorType(Type):
368
- """
369
- The type for vectors of primitive data items (e.g. "<f32 x 4>").
370
- """
371
-
372
- def __init__(self, element, count):
373
- self.element = element
374
- self.count = count
375
-
376
- @property
377
- def elements(self):
378
- return _Repeat(self.element, self.count)
379
-
380
- def __len__(self):
381
- return self.count
382
-
383
- def _to_string(self):
384
- return "<%d x %s>" % (self.count, self.element)
385
-
386
- def __eq__(self, other):
387
- if isinstance(other, VectorType):
388
- return self.element == other.element and self.count == other.count
389
-
390
- def __hash__(self):
391
- # TODO: why does this not take self.element/self.count into account?
392
- return hash(VectorType)
393
-
394
- def __copy__(self):
395
- return self
396
-
397
- def format_constant(self, value):
398
- itemstring = ", " .join(["{0} {1}".format(x.type, x.get_reference())
399
- for x in value])
400
- return "<{0}>".format(itemstring)
401
-
402
- def wrap_constant_value(self, values):
403
- from . import Value, Constant
404
- if not isinstance(values, (list, tuple)):
405
- if isinstance(values, Constant):
406
- if values.type != self.element:
407
- raise TypeError("expected {} for {}".format(
408
- self.element, values.type))
409
- return (values, ) * self.count
410
- return (Constant(self.element, values), ) * self.count
411
- if len(values) != len(self):
412
- raise ValueError("wrong constant size for %s: got %d elements"
413
- % (self, len(values)))
414
- return [Constant(ty, val) if not isinstance(val, Value) else val
415
- for ty, val in zip(self.elements, values)]
416
-
417
-
418
- class Aggregate(Type):
419
- """
420
- Base class for aggregate types.
421
- See http://llvm.org/docs/LangRef.html#t-aggregate
422
- """
423
-
424
- def wrap_constant_value(self, values):
425
- from . import Value, Constant
426
-
427
- if not isinstance(values, (list, tuple)):
428
- return values
429
- if len(values) != len(self):
430
- raise ValueError("wrong constant size for %s: got %d elements"
431
- % (self, len(values)))
432
- return [Constant(ty, val) if not isinstance(val, Value) else val
433
- for ty, val in zip(self.elements, values)]
434
-
435
-
436
- class ArrayType(Aggregate):
437
- """
438
- The type for fixed-size homogenous arrays (e.g. "[f32 x 3]").
439
- """
440
-
441
- def __init__(self, element, count):
442
- self.element = element
443
- self.count = count
444
-
445
- @property
446
- def elements(self):
447
- return _Repeat(self.element, self.count)
448
-
449
- def __len__(self):
450
- return self.count
451
-
452
- def _to_string(self):
453
- return "[%d x %s]" % (self.count, self.element)
454
-
455
- def __eq__(self, other):
456
- if isinstance(other, ArrayType):
457
- return self.element == other.element and self.count == other.count
458
-
459
- def __hash__(self):
460
- return hash(ArrayType)
461
-
462
- def gep(self, i):
463
- """
464
- Resolve the type of the i-th element (for getelementptr lookups).
465
- """
466
- if not isinstance(i.type, IntType):
467
- raise TypeError(i.type)
468
- return self.element
469
-
470
- def format_constant(self, value):
471
- itemstring = ", " .join(["{0} {1}".format(x.type, x.get_reference())
472
- for x in value])
473
- return "[{0}]".format(itemstring)
474
-
475
-
476
- class BaseStructType(Aggregate):
477
- """
478
- The base type for heterogenous struct types.
479
- """
480
- _packed = False
481
-
482
- @property
483
- def packed(self):
484
- """
485
- A boolean attribute that indicates whether the structure uses
486
- packed layout.
487
- """
488
- return self._packed
489
-
490
- @packed.setter
491
- def packed(self, val):
492
- self._packed = bool(val)
493
-
494
- def __len__(self):
495
- assert self.elements is not None
496
- return len(self.elements)
497
-
498
- def __iter__(self):
499
- assert self.elements is not None
500
- return iter(self.elements)
501
-
502
- @property
503
- def is_opaque(self):
504
- return self.elements is None
505
-
506
- def structure_repr(self):
507
- """
508
- Return the LLVM IR for the structure representation
509
- """
510
- ret = '{%s}' % ', '.join([str(x) for x in self.elements])
511
- return self._wrap_packed(ret)
512
-
513
- def format_constant(self, value):
514
- itemstring = ", " .join(["{0} {1}".format(x.type, x.get_reference())
515
- for x in value])
516
- ret = "{{{0}}}".format(itemstring)
517
- return self._wrap_packed(ret)
518
-
519
- def gep(self, i):
520
- """
521
- Resolve the type of the i-th element (for getelementptr lookups).
522
-
523
- *i* needs to be a LLVM constant, so that the type can be determined
524
- at compile-time.
525
- """
526
- if not isinstance(i.type, IntType):
527
- raise TypeError(i.type)
528
- return self.elements[i.constant]
529
-
530
- def _wrap_packed(self, textrepr):
531
- """
532
- Internal helper to wrap textual repr of struct type into packed struct
533
- """
534
- if self.packed:
535
- return '<{}>'.format(textrepr)
536
- else:
537
- return textrepr
538
-
539
-
540
- class LiteralStructType(BaseStructType):
541
- """
542
- The type of "literal" structs, i.e. structs with a literally-defined
543
- type (by contrast with IdentifiedStructType).
544
- """
545
-
546
- null = 'zeroinitializer'
547
-
548
- def __init__(self, elems, packed=False):
549
- """
550
- *elems* is a sequence of types to be used as members.
551
- *packed* controls the use of packed layout.
552
- """
553
- self.elements = tuple(elems)
554
- self.packed = packed
555
-
556
- def _to_string(self):
557
- return self.structure_repr()
558
-
559
- def __eq__(self, other):
560
- if isinstance(other, LiteralStructType):
561
- return self.elements == other.elements
562
-
563
- def __hash__(self):
564
- return hash(LiteralStructType)
565
-
566
-
567
- class IdentifiedStructType(BaseStructType):
568
- """
569
- A type which is a named alias for another struct type, akin to a typedef.
570
- While literal struct types can be structurally equal (see
571
- LiteralStructType), identified struct types are compared by name.
572
-
573
- Do not use this directly.
574
- """
575
- null = 'zeroinitializer'
576
-
577
- def __init__(self, context, name, packed=False):
578
- """
579
- *context* is a llvmlite.ir.Context.
580
- *name* is the identifier for the new struct type.
581
- *packed* controls the use of packed layout.
582
- """
583
- assert name
584
- self.context = context
585
- self.name = name
586
- self.elements = None
587
- self.packed = packed
588
-
589
- def _to_string(self):
590
- return "%{name}".format(name=_wrapname(self.name))
591
-
592
- def get_declaration(self):
593
- """
594
- Returns the string for the declaration of the type
595
- """
596
- if self.is_opaque:
597
- out = "{strrep} = type opaque".format(strrep=str(self))
598
- else:
599
- out = "{strrep} = type {struct}".format(
600
- strrep=str(self), struct=self.structure_repr())
601
- return out
602
-
603
- def __eq__(self, other):
604
- if isinstance(other, IdentifiedStructType):
605
- return self.name == other.name
606
-
607
- def __hash__(self):
608
- return hash(IdentifiedStructType)
609
-
610
- def set_body(self, *elems):
611
- if not self.is_opaque:
612
- raise RuntimeError("{name} is already defined".format(
613
- name=self.name))
614
- self.elements = tuple(elems)
1
+ """
2
+ Classes that are LLVM types
3
+ """
4
+
5
+ import struct
6
+
7
+ from llvmlite.ir._utils import _StrCaching
8
+
9
+
10
+ def _wrapname(x):
11
+ return '"{0}"'.format(x.replace('\\', '\\5c').replace('"', '\\22'))
12
+
13
+
14
+ class Type(_StrCaching):
15
+ """
16
+ The base class for all LLVM types.
17
+ """
18
+ is_pointer = False
19
+ null = 'zeroinitializer'
20
+
21
+ def __repr__(self):
22
+ return "<%s %s>" % (type(self), str(self))
23
+
24
+ def _to_string(self):
25
+ raise NotImplementedError
26
+
27
+ def as_pointer(self, addrspace=0):
28
+ return PointerType(self, addrspace)
29
+
30
+ def __ne__(self, other):
31
+ return not (self == other)
32
+
33
+ def _get_ll_pointer_type(self, target_data, context=None):
34
+ """
35
+ Convert this type object to an LLVM type.
36
+ """
37
+ from llvmlite.ir import Module, GlobalVariable
38
+ from llvmlite.binding import parse_assembly
39
+
40
+ if context is None:
41
+ m = Module()
42
+ else:
43
+ m = Module(context=context)
44
+ foo = GlobalVariable(m, self, name="foo")
45
+ with parse_assembly(str(m)) as llmod:
46
+ return llmod.get_global_variable(foo.name).type
47
+
48
+ def get_abi_size(self, target_data, context=None):
49
+ """
50
+ Get the ABI size of this type according to data layout *target_data*.
51
+ """
52
+ llty = self._get_ll_pointer_type(target_data, context)
53
+ return target_data.get_pointee_abi_size(llty)
54
+
55
+ def get_abi_alignment(self, target_data, context=None):
56
+ """
57
+ Get the minimum ABI alignment of this type according to data layout
58
+ *target_data*.
59
+ """
60
+ llty = self._get_ll_pointer_type(target_data, context)
61
+ return target_data.get_pointee_abi_alignment(llty)
62
+
63
+ def format_constant(self, value):
64
+ """
65
+ Format constant *value* of this type. This method may be overriden
66
+ by subclasses.
67
+ """
68
+ return str(value)
69
+
70
+ def wrap_constant_value(self, value):
71
+ """
72
+ Wrap constant *value* if necessary. This method may be overriden
73
+ by subclasses (especially aggregate types).
74
+ """
75
+ return value
76
+
77
+ def __call__(self, value):
78
+ """
79
+ Create a LLVM constant of this type with the given Python value.
80
+ """
81
+ from llvmlite.ir import Constant
82
+ return Constant(self, value)
83
+
84
+
85
+ class MetaDataType(Type):
86
+
87
+ def _to_string(self):
88
+ return "metadata"
89
+
90
+ def as_pointer(self):
91
+ raise TypeError
92
+
93
+ def __eq__(self, other):
94
+ return isinstance(other, MetaDataType)
95
+
96
+ def __hash__(self):
97
+ return hash(MetaDataType)
98
+
99
+
100
+ class LabelType(Type):
101
+ """
102
+ The label type is the type of e.g. basic blocks.
103
+ """
104
+
105
+ def _to_string(self):
106
+ return "label"
107
+
108
+
109
+ class PointerType(Type):
110
+ """
111
+ The type of all pointer values.
112
+ """
113
+ is_pointer = True
114
+ null = 'null'
115
+
116
+ def __init__(self, pointee, addrspace=0):
117
+ assert not isinstance(pointee, VoidType)
118
+ self.pointee = pointee
119
+ self.addrspace = addrspace
120
+
121
+ def _to_string(self):
122
+ if self.addrspace != 0:
123
+ return "{0} addrspace({1})*".format(self.pointee, self.addrspace)
124
+ else:
125
+ return "{0}*".format(self.pointee)
126
+
127
+ def __eq__(self, other):
128
+ if isinstance(other, PointerType):
129
+ return (self.pointee, self.addrspace) == (other.pointee,
130
+ other.addrspace)
131
+ else:
132
+ return False
133
+
134
+ def __hash__(self):
135
+ return hash(PointerType)
136
+
137
+ def gep(self, i):
138
+ """
139
+ Resolve the type of the i-th element (for getelementptr lookups).
140
+ """
141
+ if not isinstance(i.type, IntType):
142
+ raise TypeError(i.type)
143
+ return self.pointee
144
+
145
+ @property
146
+ def intrinsic_name(self):
147
+ return 'p%d%s' % (self.addrspace, self.pointee.intrinsic_name)
148
+
149
+
150
+ class VoidType(Type):
151
+ """
152
+ The type for empty values (e.g. a function returning no value).
153
+ """
154
+
155
+ def _to_string(self):
156
+ return 'void'
157
+
158
+ def __eq__(self, other):
159
+ return isinstance(other, VoidType)
160
+
161
+ def __hash__(self):
162
+ return hash(VoidType)
163
+
164
+
165
+ class FunctionType(Type):
166
+ """
167
+ The type for functions.
168
+ """
169
+
170
+ def __init__(self, return_type, args, var_arg=False):
171
+ self.return_type = return_type
172
+ self.args = tuple(args)
173
+ self.var_arg = var_arg
174
+
175
+ def _to_string(self):
176
+ if self.args:
177
+ strargs = ', '.join([str(a) for a in self.args])
178
+ if self.var_arg:
179
+ return '{0} ({1}, ...)'.format(self.return_type, strargs)
180
+ else:
181
+ return '{0} ({1})'.format(self.return_type, strargs)
182
+ elif self.var_arg:
183
+ return '{0} (...)'.format(self.return_type)
184
+ else:
185
+ return '{0} ()'.format(self.return_type)
186
+
187
+ def __eq__(self, other):
188
+ if isinstance(other, FunctionType):
189
+ return (self.return_type == other.return_type and
190
+ self.args == other.args and self.var_arg == other.var_arg)
191
+ else:
192
+ return False
193
+
194
+ def __hash__(self):
195
+ return hash(FunctionType)
196
+
197
+
198
+ class IntType(Type):
199
+ """
200
+ The type for integers.
201
+ """
202
+ null = '0'
203
+ _instance_cache = {}
204
+ width: int
205
+
206
+ def __new__(cls, bits):
207
+ # Cache all common integer types
208
+ if 0 <= bits <= 128:
209
+ try:
210
+ return cls._instance_cache[bits]
211
+ except KeyError:
212
+ inst = cls._instance_cache[bits] = cls.__new(bits)
213
+ return inst
214
+ return cls.__new(bits)
215
+
216
+ @classmethod
217
+ def __new(cls, bits):
218
+ assert isinstance(bits, int) and bits >= 0
219
+ self = super(IntType, cls).__new__(cls)
220
+ self.width = bits
221
+ return self
222
+
223
+ def __getnewargs__(self):
224
+ return self.width,
225
+
226
+ def __copy__(self):
227
+ return self
228
+
229
+ def _to_string(self):
230
+ return 'i%u' % (self.width,)
231
+
232
+ def __eq__(self, other):
233
+ if isinstance(other, IntType):
234
+ return self.width == other.width
235
+ else:
236
+ return False
237
+
238
+ def __hash__(self):
239
+ return hash(IntType)
240
+
241
+ def format_constant(self, val):
242
+ if isinstance(val, bool):
243
+ return str(val).lower()
244
+ else:
245
+ return str(val)
246
+
247
+ def wrap_constant_value(self, val):
248
+ if val is None:
249
+ return 0
250
+ return val
251
+
252
+ @property
253
+ def intrinsic_name(self):
254
+ return str(self)
255
+
256
+
257
+ def _as_float(value):
258
+ """
259
+ Truncate to single-precision float.
260
+ """
261
+ return struct.unpack('f', struct.pack('f', value))[0]
262
+
263
+
264
+ def _as_half(value):
265
+ """
266
+ Truncate to half-precision float.
267
+ """
268
+ try:
269
+ return struct.unpack('e', struct.pack('e', value))[0]
270
+ except struct.error:
271
+ # 'e' only added in Python 3.6+
272
+ return _as_float(value)
273
+
274
+
275
+ def _format_float_as_hex(value, packfmt, unpackfmt, numdigits):
276
+ raw = struct.pack(packfmt, float(value))
277
+ intrep = struct.unpack(unpackfmt, raw)[0]
278
+ out = '{{0:#{0}x}}'.format(numdigits).format(intrep)
279
+ return out
280
+
281
+
282
+ def _format_double(value):
283
+ """
284
+ Format *value* as a hexadecimal string of its IEEE double precision
285
+ representation.
286
+ """
287
+ return _format_float_as_hex(value, 'd', 'Q', 16)
288
+
289
+
290
+ class _BaseFloatType(Type):
291
+
292
+ def __new__(cls):
293
+ return cls._instance_cache
294
+
295
+ def __eq__(self, other):
296
+ return isinstance(other, type(self))
297
+
298
+ def __hash__(self):
299
+ return hash(type(self))
300
+
301
+ @classmethod
302
+ def _create_instance(cls):
303
+ cls._instance_cache = super(_BaseFloatType, cls).__new__(cls)
304
+
305
+
306
+ class HalfType(_BaseFloatType):
307
+ """
308
+ The type for single-precision floats.
309
+ """
310
+ null = '0.0'
311
+ intrinsic_name = 'f16'
312
+
313
+ def __str__(self):
314
+ return 'half'
315
+
316
+ def format_constant(self, value):
317
+ return _format_double(_as_half(value))
318
+
319
+
320
+ class FloatType(_BaseFloatType):
321
+ """
322
+ The type for single-precision floats.
323
+ """
324
+ null = '0.0'
325
+ intrinsic_name = 'f32'
326
+
327
+ def __str__(self):
328
+ return 'float'
329
+
330
+ def format_constant(self, value):
331
+ return _format_double(_as_float(value))
332
+
333
+
334
+ class DoubleType(_BaseFloatType):
335
+ """
336
+ The type for double-precision floats.
337
+ """
338
+ null = '0.0'
339
+ intrinsic_name = 'f64'
340
+
341
+ def __str__(self):
342
+ return 'double'
343
+
344
+ def format_constant(self, value):
345
+ return _format_double(value)
346
+
347
+
348
+ for _cls in (HalfType, FloatType, DoubleType):
349
+ _cls._create_instance()
350
+
351
+
352
+ class _Repeat(object):
353
+ def __init__(self, value, size):
354
+ self.value = value
355
+ self.size = size
356
+
357
+ def __len__(self):
358
+ return self.size
359
+
360
+ def __getitem__(self, item):
361
+ if 0 <= item < self.size:
362
+ return self.value
363
+ else:
364
+ raise IndexError(item)
365
+
366
+
367
+ class VectorType(Type):
368
+ """
369
+ The type for vectors of primitive data items (e.g. "<f32 x 4>").
370
+ """
371
+
372
+ def __init__(self, element, count):
373
+ self.element = element
374
+ self.count = count
375
+
376
+ @property
377
+ def elements(self):
378
+ return _Repeat(self.element, self.count)
379
+
380
+ def __len__(self):
381
+ return self.count
382
+
383
+ def _to_string(self):
384
+ return "<%d x %s>" % (self.count, self.element)
385
+
386
+ def __eq__(self, other):
387
+ if isinstance(other, VectorType):
388
+ return self.element == other.element and self.count == other.count
389
+
390
+ def __hash__(self):
391
+ # TODO: why does this not take self.element/self.count into account?
392
+ return hash(VectorType)
393
+
394
+ def __copy__(self):
395
+ return self
396
+
397
+ def format_constant(self, value):
398
+ itemstring = ", " .join(["{0} {1}".format(x.type, x.get_reference())
399
+ for x in value])
400
+ return "<{0}>".format(itemstring)
401
+
402
+ def wrap_constant_value(self, values):
403
+ from . import Value, Constant
404
+ if not isinstance(values, (list, tuple)):
405
+ if isinstance(values, Constant):
406
+ if values.type != self.element:
407
+ raise TypeError("expected {} for {}".format(
408
+ self.element, values.type))
409
+ return (values, ) * self.count
410
+ return (Constant(self.element, values), ) * self.count
411
+ if len(values) != len(self):
412
+ raise ValueError("wrong constant size for %s: got %d elements"
413
+ % (self, len(values)))
414
+ return [Constant(ty, val) if not isinstance(val, Value) else val
415
+ for ty, val in zip(self.elements, values)]
416
+
417
+
418
+ class Aggregate(Type):
419
+ """
420
+ Base class for aggregate types.
421
+ See http://llvm.org/docs/LangRef.html#t-aggregate
422
+ """
423
+
424
+ def wrap_constant_value(self, values):
425
+ from . import Value, Constant
426
+
427
+ if not isinstance(values, (list, tuple)):
428
+ return values
429
+ if len(values) != len(self):
430
+ raise ValueError("wrong constant size for %s: got %d elements"
431
+ % (self, len(values)))
432
+ return [Constant(ty, val) if not isinstance(val, Value) else val
433
+ for ty, val in zip(self.elements, values)]
434
+
435
+
436
+ class ArrayType(Aggregate):
437
+ """
438
+ The type for fixed-size homogenous arrays (e.g. "[f32 x 3]").
439
+ """
440
+
441
+ def __init__(self, element, count):
442
+ self.element = element
443
+ self.count = count
444
+
445
+ @property
446
+ def elements(self):
447
+ return _Repeat(self.element, self.count)
448
+
449
+ def __len__(self):
450
+ return self.count
451
+
452
+ def _to_string(self):
453
+ return "[%d x %s]" % (self.count, self.element)
454
+
455
+ def __eq__(self, other):
456
+ if isinstance(other, ArrayType):
457
+ return self.element == other.element and self.count == other.count
458
+
459
+ def __hash__(self):
460
+ return hash(ArrayType)
461
+
462
+ def gep(self, i):
463
+ """
464
+ Resolve the type of the i-th element (for getelementptr lookups).
465
+ """
466
+ if not isinstance(i.type, IntType):
467
+ raise TypeError(i.type)
468
+ return self.element
469
+
470
+ def format_constant(self, value):
471
+ itemstring = ", " .join(["{0} {1}".format(x.type, x.get_reference())
472
+ for x in value])
473
+ return "[{0}]".format(itemstring)
474
+
475
+
476
+ class BaseStructType(Aggregate):
477
+ """
478
+ The base type for heterogenous struct types.
479
+ """
480
+ _packed = False
481
+
482
+ @property
483
+ def packed(self):
484
+ """
485
+ A boolean attribute that indicates whether the structure uses
486
+ packed layout.
487
+ """
488
+ return self._packed
489
+
490
+ @packed.setter
491
+ def packed(self, val):
492
+ self._packed = bool(val)
493
+
494
+ def __len__(self):
495
+ assert self.elements is not None
496
+ return len(self.elements)
497
+
498
+ def __iter__(self):
499
+ assert self.elements is not None
500
+ return iter(self.elements)
501
+
502
+ @property
503
+ def is_opaque(self):
504
+ return self.elements is None
505
+
506
+ def structure_repr(self):
507
+ """
508
+ Return the LLVM IR for the structure representation
509
+ """
510
+ ret = '{%s}' % ', '.join([str(x) for x in self.elements])
511
+ return self._wrap_packed(ret)
512
+
513
+ def format_constant(self, value):
514
+ itemstring = ", " .join(["{0} {1}".format(x.type, x.get_reference())
515
+ for x in value])
516
+ ret = "{{{0}}}".format(itemstring)
517
+ return self._wrap_packed(ret)
518
+
519
+ def gep(self, i):
520
+ """
521
+ Resolve the type of the i-th element (for getelementptr lookups).
522
+
523
+ *i* needs to be a LLVM constant, so that the type can be determined
524
+ at compile-time.
525
+ """
526
+ if not isinstance(i.type, IntType):
527
+ raise TypeError(i.type)
528
+ return self.elements[i.constant]
529
+
530
+ def _wrap_packed(self, textrepr):
531
+ """
532
+ Internal helper to wrap textual repr of struct type into packed struct
533
+ """
534
+ if self.packed:
535
+ return '<{}>'.format(textrepr)
536
+ else:
537
+ return textrepr
538
+
539
+
540
+ class LiteralStructType(BaseStructType):
541
+ """
542
+ The type of "literal" structs, i.e. structs with a literally-defined
543
+ type (by contrast with IdentifiedStructType).
544
+ """
545
+
546
+ null = 'zeroinitializer'
547
+
548
+ def __init__(self, elems, packed=False):
549
+ """
550
+ *elems* is a sequence of types to be used as members.
551
+ *packed* controls the use of packed layout.
552
+ """
553
+ self.elements = tuple(elems)
554
+ self.packed = packed
555
+
556
+ def _to_string(self):
557
+ return self.structure_repr()
558
+
559
+ def __eq__(self, other):
560
+ if isinstance(other, LiteralStructType):
561
+ return self.elements == other.elements
562
+
563
+ def __hash__(self):
564
+ return hash(LiteralStructType)
565
+
566
+
567
+ class IdentifiedStructType(BaseStructType):
568
+ """
569
+ A type which is a named alias for another struct type, akin to a typedef.
570
+ While literal struct types can be structurally equal (see
571
+ LiteralStructType), identified struct types are compared by name.
572
+
573
+ Do not use this directly.
574
+ """
575
+ null = 'zeroinitializer'
576
+
577
+ def __init__(self, context, name, packed=False):
578
+ """
579
+ *context* is a llvmlite.ir.Context.
580
+ *name* is the identifier for the new struct type.
581
+ *packed* controls the use of packed layout.
582
+ """
583
+ assert name
584
+ self.context = context
585
+ self.name = name
586
+ self.elements = None
587
+ self.packed = packed
588
+
589
+ def _to_string(self):
590
+ return "%{name}".format(name=_wrapname(self.name))
591
+
592
+ def get_declaration(self):
593
+ """
594
+ Returns the string for the declaration of the type
595
+ """
596
+ if self.is_opaque:
597
+ out = "{strrep} = type opaque".format(strrep=str(self))
598
+ else:
599
+ out = "{strrep} = type {struct}".format(
600
+ strrep=str(self), struct=self.structure_repr())
601
+ return out
602
+
603
+ def __eq__(self, other):
604
+ if isinstance(other, IdentifiedStructType):
605
+ return self.name == other.name
606
+
607
+ def __hash__(self):
608
+ return hash(IdentifiedStructType)
609
+
610
+ def set_body(self, *elems):
611
+ if not self.is_opaque:
612
+ raise RuntimeError("{name} is already defined".format(
613
+ name=self.name))
614
+ self.elements = tuple(elems)