Skip to content
← Back

src/ir/llvm/llvm_types.ghul

1
namespace IR.LLVMTarget is
2
use Semantic.Types.Type
3
4
use LLVM.Types
5
6
/// How the LLVM target holds values of the built-in scalar kinds.
7
///
8
/// Every location holding a value, a local, an argument, a field, a
9
/// global, an array element, a member of a struct or a box, stores it at
10
/// the width of its kind: a `byte` in an `i8`, a `char` in an `i16`, as
11
/// clang stores a `char`. An integer kind narrower than 32 bits is
12
/// computed with as an `i32`, as CIL holds it on its evaluation stack, so
13
/// a load from memory widens it and a store narrows it again. A bool is
14
/// an `i1` wherever it is computed with and a byte holding 0 or 1 in
15
/// memory, as clang holds one. A reference to any location of a kind
16
/// therefore means the same width.
17
class LLVM_TYPES is
18
/// The scalar kind a value of `type` is, or null where the type is
19
/// not one of the built-in scalars. An enum is held as the `int`
20
/// underneath it.
21
scalar(type: Type?) -> Scalar? static =>
22
if type? /\ isa Semantic.Symbols.ENUM_STRUCT(type.symbol) /\ !type.is_optional then
23
Scalar.INT32
24
else
25
IoC.CONTAINER.instance.value_converter.scalar(type)
26
fi
27
28
/// The LLVM type a value of the scalar kind is held in.
29
value_type(scalar: Scalar) -> LLVM.Type static =>
30
case scalar
31
when Scalar.BOOL then Types.i1
32
when Scalar.INT64, Scalar.UINT64, Scalar.INT_NATIVE, Scalar.UINT_NATIVE then Types.i64
33
when Scalar.FLOAT32 then LLVM.Type.FLOAT
34
when Scalar.FLOAT64 then LLVM.Type.DOUBLE
35
else Types.i32
36
esac
37
38
/// The LLVM type a value of `type` is held in, or null where the
39
/// target holds no values of the type yet.
40
value_type(type: Type?) -> LLVM.Type? static =>
41
if let kind = scalar(type) then
42
value_type(kind)
43
elif is_string(type) \/ is_array(type) \/ is_object_reference(type) then
44
Types.pointer
45
else
46
null
47
fi
48
49
/// Whether `type` is `string`, which is held as a pointer to the
50
/// string object.
51
is_string(type: Type?) -> bool static =>
52
type? /\ type.symbol == IoC.CONTAINER.instance.innate_symbol_lookup.get_string_type().symbol
53
54
/// Whether `type` is a class, a trait, a union or one of its variants,
55
/// `object`, a function type or a frame, whose values are
56
/// held as pointers to objects.
57
is_object_reference(type: Type?) -> bool static =>
58
type? /\ !type.is_value_type /\ !(type.optional_inner_type?.is_value_type ?? false) /\ (type.is_class \/ type.is_trait \/ type.is_object \/ type.is_function \/ type.is_action \/ _is_frame(type) \/ _is_union(type))
59
60
// A closure's frame, or a generator's or asynchronous function's.
61
_is_frame(type: Type) -> bool static is
62
let symbol = if let generic: Semantic.Symbols.GENERIC = type.symbol then generic.unspecialized_symbol else type.symbol fi
63
64
return isa Semantic.Symbols.FRAME(symbol) \/ isa Semantic.Symbols.STATE_MACHINE_FRAME_BASE(symbol)
65
si
66
67
_is_union(type: Type) -> bool static =>
68
if let classy: Semantic.Symbols.Classy = type.symbol then
69
classy.is_union \/ classy.is_variant
70
elif let generic: Semantic.Symbols.GENERIC = type.symbol then
71
if let classy: Semantic.Symbols.Classy = generic.unspecialized_symbol then classy.is_union \/ classy.is_variant else false fi
72
else
73
false
74
fi
75
76
/// Whether `type` is an array type, which is held as a pointer to the
77
/// array object.
78
is_array(type: Type?) -> bool static =>
79
if let generic: Semantic.Symbols.GENERIC = type?.symbol then
80
cast Semantic.Symbols.Classy?(generic.unspecialized_symbol) == IR.Program.INNATE_CORE_CLASSES().array_definition
81
else
82
false
83
fi
84
85
/// The type a function returning `type` is declared to return: `void`
86
/// for nothing, or null where the target holds no values of the type
87
/// yet.
88
result_type(type: Type?) -> LLVM.Type? static =>
89
if is_void(type) then LLVM.Type.VOID else value_type(type) fi
90
91
/// Whether `type` is absent or `void`.
92
is_void(type: Type?) -> bool static => !type? \/ type.is_void
93
94
/// Whether the kind is an integer computed with as an `i32` but held
95
/// in memory in fewer bits.
96
is_narrow(scalar: Scalar) -> bool static =>
97
case scalar
98
when Scalar.CHAR, Scalar.INT8, Scalar.UINT8, Scalar.INT16, Scalar.UINT16 then true
99
else false
100
esac
101
102
/// The LLVM type a value of the scalar kind is stored as in memory:
103
/// the width of the kind.
104
stored_type(scalar: Scalar) -> LLVM.Type static =>
105
case scalar
106
when Scalar.BOOL, Scalar.INT8, Scalar.UINT8 then Types.i8
107
when Scalar.CHAR, Scalar.INT16, Scalar.UINT16 then Types.i16
108
else value_type(scalar)
109
esac
110
111
/// The LLVM type a value of `type` is stored as in memory, or null
112
/// where it is not one of the built-in scalars.
113
stored_type(type: Type?) -> LLVM.Type? static =>
114
if let kind = scalar(type) then stored_type(kind) else null fi
115
116
/// `value`, loaded from memory where a value of the kind is stored,
117
/// widened to the type the kind is computed with.
118
///
119
/// - block: where the conversion goes
120
/// - value: the value as stored
121
/// - scalar: its kind
122
widen(block: LLVM.BLOCK, value: LLVM.Value, scalar: Scalar) -> LLVM.Value static =>
123
if scalar == Scalar.BOOL then
124
block.convert(LLVM.Conversion.TRUNCATE, value, Types.i1)
125
elif is_narrow(scalar) then
126
block.convert(if is_unsigned(scalar) then LLVM.Conversion.ZERO_EXTEND else LLVM.Conversion.SIGN_EXTEND fi, value, value_type(scalar))
127
else
128
value
129
fi
130
131
/// `value`, computed with as the kind is, narrowed to the width the
132
/// kind is stored at.
133
///
134
/// - block: where the conversion goes
135
/// - value: the value as computed with
136
/// - scalar: its kind
137
narrow(block: LLVM.BLOCK, value: LLVM.Value, scalar: Scalar) -> LLVM.Value static =>
138
if scalar == Scalar.BOOL then
139
block.convert(LLVM.Conversion.ZERO_EXTEND, value, Types.i8)
140
elif is_narrow(scalar) then
141
block.convert(LLVM.Conversion.TRUNCATE, value, stored_type(scalar))
142
else
143
value
144
fi
145
146
/// Whether the kind's top bit is part of its magnitude.
147
is_unsigned(scalar: Scalar) -> bool static =>
148
case scalar
149
when Scalar.BOOL, Scalar.CHAR, Scalar.UINT8, Scalar.UINT16 then true
150
when Scalar.UINT32, Scalar.UINT64, Scalar.UINT_NATIVE then true
151
else false
152
esac
153
154
/// Whether `type` is a floating point type.
155
is_floating(type: LLVM.Type) -> bool static =>
156
type =~ LLVM.Type.FLOAT \/ type =~ LLVM.Type.DOUBLE
157
158
/// The width in bits of an integer type, or zero for any other.
159
width(type: LLVM.Type) -> int static =>
160
if let integer: LLVM.Type.INTEGER = type then integer.width else 0 fi
161
162
163
/// Whether a value of `type` can hold a pointer, so memory holding one
164
/// has to be scanned by the collector rather than allocated atomic: a
165
/// pointer itself, and a struct, array or vector with one anywhere in
166
/// it. A named struct is taken to hold one, as its fields are not
167
/// known here.
168
holds_pointer(type: LLVM.Type) -> bool static =>
169
case type
170
when _: LLVM.Type.POINTER then true
171
when s: LLVM.Type.STRUCT then s.fields |> Ghul.Pipes.any(f => holds_pointer(f))
172
when a: LLVM.Type.ARRAY then holds_pointer(a.element)
173
when v: LLVM.Type.VECTOR then holds_pointer(v.element)
174
when _: LLVM.Type.NAMED then true
175
else false
176
esac
177
178
/// The size in bytes of a value of `type` as x86-64 lays it out: each
179
/// struct field at its natural alignment, and the whole rounded up to
180
/// the largest of them, as LLVM's own layout for the target does.
181
size_of(type: LLVM.Type) -> int static =>
182
case type
183
when integer: LLVM.Type.INTEGER then if integer.width <= 8 then 1 elif integer.width <= 16 then 2 elif integer.width <= 32 then 4 else 8 fi
184
when LLVM.Type.HALF then 2
185
when LLVM.Type.FLOAT then 4
186
when s: LLVM.Type.STRUCT then _struct_size(s)
187
when a: LLVM.Type.ARRAY then a.count * size_of(a.element)
188
else 8
189
esac
190
191
/// The alignment in bytes of a value of `type` as x86-64 lays it out.
192
align_of(type: LLVM.Type) -> int static =>
193
case type
194
when s: LLVM.Type.STRUCT then s.fields |> Ghul.Pipes.reduce(1, (a, f) => if align_of(f) > a then align_of(f) else a fi)
195
when a: LLVM.Type.ARRAY then align_of(a.element)
196
else size_of(type)
197
esac
198
199
/// The offset in bytes of field `index` of `s` as x86-64 lays it out:
200
/// each field at its natural alignment after the one before.
201
///
202
/// - s: the structure
203
/// - index: the field's position, from 0
204
offset_of(s: LLVM.Type.STRUCT, index: int) -> int static is
205
let offset mut = 0
206
207
for i in 0..index + 1 do
208
let alignment = align_of(s.fields[i])
209
210
offset = (offset + alignment - 1) / alignment * alignment
211
212
if i < index then
213
offset = offset + size_of(s.fields[i])
214
fi
215
od
216
217
return offset
218
si
219
220
_struct_size(s: LLVM.Type.STRUCT) -> int static is
221
if s.fields.count == 0 then
222
return 0
223
fi
224
225
let last = s.fields.count - 1
226
let whole = align_of(s)
227
let offset = offset_of(s, last) + size_of(s.fields[last])
228
229
return (offset + whole - 1) / whole * whole
230
si
231
/// The zero of `type`.
232
zero(type: LLVM.Type) -> LLVM.Value static =>
233
case type
234
when LLVM.Type.FLOAT then LLVM.Values.of_single(0.0s)
235
when LLVM.Type.DOUBLE then LLVM.Values.of_double(0.0)
236
when _: LLVM.Type.POINTER then LLVM.Values.null_pointer
237
when _: LLVM.Type.STRUCT then LLVM.Values.zero(type)
238
else LLVM.Values.integer(type, bigint.zero)
239
esac
240
si
241
si