|
1 | 1 | from llvmlite import ir |
2 | 2 |
|
| 3 | + |
| 4 | +class IntTy(ir.IntType): |
| 5 | + """An LLVM integer type that also remembers its signedness. |
| 6 | +
|
| 7 | + LLVM integer types are sign-agnostic by design: `i32` is just 32 bits, and |
| 8 | + the sign lives in the operations (sdiv/udiv, sext/zext, icmp s*/u*). The |
| 9 | + frontend therefore has to carry it. IntTy is a plain ir.IntType for every |
| 10 | + purpose LLVM cares about -- it renders as `i32`, compares and hashes equal |
| 11 | + to ir.IntType(32), and passes every isinstance check -- with one extra |
| 12 | + attribute the compiler reads when choosing between signed and unsigned |
| 13 | + forms of an operation. |
| 14 | +
|
| 15 | + Invariant: the sign is read only from a *descriptor* -- a Symbol.ir_type |
| 16 | + or the type half of an eval_expr result -- never from `value.type`. Values |
| 17 | + produced by the IRBuilder (loads, arithmetic, extensions) come back with a |
| 18 | + plain ir.IntType, so a sign on a value's own type is lost at the first |
| 19 | + operation. Descriptors are constructed by the compiler; that is where the |
| 20 | + sign lives. |
| 21 | + """ |
| 22 | + |
| 23 | + def __new__(cls, bits: int, signed: bool = True): |
| 24 | + # ir.IntType.__new__ memoises one instance per width in a cache shared |
| 25 | + # with subclasses. Going through it would (a) merge the signed and |
| 26 | + # unsigned flavours of a width into one object and (b) plant an IntTy |
| 27 | + # in the cache so that ir.IntType(32) itself started returning one. |
| 28 | + # Construct directly instead; equality and hashing are inherited and |
| 29 | + # depend only on the width, so an IntTy still compares equal to i32. |
| 30 | + self = object.__new__(cls) |
| 31 | + self.width = bits |
| 32 | + return self |
| 33 | + |
| 34 | + def __init__(self, bits: int, signed: bool = True): |
| 35 | + self.signed = signed |
| 36 | + |
| 37 | + def __getnewargs__(self): |
| 38 | + return self.width, self.signed |
| 39 | + |
| 40 | + def describe(self) -> str: |
| 41 | + return f"{'i' if self.signed else 'u'}{self.width}" |
| 42 | + |
| 43 | + |
| 44 | +def signedness(ty) -> bool: |
| 45 | + """Sign of an integer type descriptor. Plain ir.IntType (a site that has not |
| 46 | + been taught to carry a sign yet) reads as signed, which is the compiler's |
| 47 | + historical behaviour.""" |
| 48 | + return getattr(ty, "signed", True) |
| 49 | + |
| 50 | + |
| 51 | +_SIGNED_CTYPES = { |
| 52 | + "c_int8", |
| 53 | + "c_int16", |
| 54 | + "c_int32", |
| 55 | + "c_int64", |
| 56 | + "c_int", |
| 57 | + "c_short", |
| 58 | + "c_long", |
| 59 | + "c_longlong", |
| 60 | + "c_byte", |
| 61 | +} |
| 62 | + |
| 63 | +_INT_CTYPE_WIDTHS = { |
| 64 | + "c_int8": 8, |
| 65 | + "c_uint8": 8, |
| 66 | + "c_byte": 8, |
| 67 | + "c_ubyte": 8, |
| 68 | + "c_int16": 16, |
| 69 | + "c_uint16": 16, |
| 70 | + "c_short": 16, |
| 71 | + "c_ushort": 16, |
| 72 | + "c_int32": 32, |
| 73 | + "c_uint32": 32, |
| 74 | + "c_int": 32, |
| 75 | + "c_uint": 32, |
| 76 | + "c_int64": 64, |
| 77 | + "c_uint64": 64, |
| 78 | + "c_long": 64, |
| 79 | + "c_ulong": 64, |
| 80 | + "c_longlong": 64, |
| 81 | + # A pointer-sized integer; treated as unsigned like uintptr_t. |
| 82 | + "c_void_p": 64, |
| 83 | +} |
| 84 | + |
| 85 | + |
| 86 | +def is_signed_ctype(ctype: str) -> bool: |
| 87 | + return ctype in _SIGNED_CTYPES |
| 88 | + |
| 89 | + |
3 | 90 | # TODO: THIS IS NOT SUPPOSED TO MATCH STRINGS :skull: |
4 | 91 | mapping = { |
5 | | - "c_int8": ir.IntType(8), |
6 | | - "c_uint8": ir.IntType(8), |
7 | | - "c_int16": ir.IntType(16), |
8 | | - "c_uint16": ir.IntType(16), |
9 | | - "c_int32": ir.IntType(32), |
10 | | - "c_uint32": ir.IntType(32), |
11 | | - "c_int64": ir.IntType(64), |
12 | | - "c_uint64": ir.IntType(64), |
13 | | - "c_float": ir.FloatType(), |
14 | | - "c_double": ir.DoubleType(), |
15 | | - "c_void_p": ir.IntType(64), |
16 | | - "c_long": ir.IntType(64), |
17 | | - "c_ulong": ir.IntType(64), |
18 | | - "c_longlong": ir.IntType(64), |
19 | | - "c_uint": ir.IntType(32), |
20 | | - "c_int": ir.IntType(32), |
21 | | - "c_ushort": ir.IntType(16), |
22 | | - "c_short": ir.IntType(16), |
23 | | - "c_ubyte": ir.IntType(8), |
24 | | - "c_byte": ir.IntType(8), |
25 | | - # Not so sure about this one |
26 | | - "str": ir.PointerType(ir.IntType(8)), |
| 92 | + name: IntTy(width, is_signed_ctype(name)) |
| 93 | + for name, width in _INT_CTYPE_WIDTHS.items() |
27 | 94 | } |
| 95 | +mapping.update( |
| 96 | + { |
| 97 | + "c_float": ir.FloatType(), |
| 98 | + "c_double": ir.DoubleType(), |
| 99 | + # Not so sure about this one |
| 100 | + "str": ir.PointerType(ir.IntType(8)), |
| 101 | + } |
| 102 | +) |
28 | 103 |
|
29 | 104 |
|
30 | 105 | def ctypes_to_ir(ctype: str): |
|
0 commit comments