Skip to content
← Back

src/ir/llvm/llvm_closures.ghul

1
namespace IR.LLVMTarget is
2
use IR.Program
3
4
use Collections.LIST
5
use Collections.MAP
6
7
use LLVM.BLOCK
8
use LLVM.FUNCTION
9
use LLVM.GLOBAL_VARIABLE
10
use LLVM.Linkage
11
use LLVM.MODULE
12
use LLVM.PARAMETER
13
use LLVM.Types
14
use LLVM.Values
15
16
/// How a closure's thunk reaches the function it calls.
17
enum ThunkKind is
18
/// The function takes no receiver, so the closure's is dropped.
19
DROP_RECEIVER,
20
21
/// The function takes the closure's receiver as its first argument.
22
PASS_RECEIVER,
23
24
/// The receiver's vtable slot holds the function.
25
VIRTUAL,
26
27
/// The receiver's row in the trait table holds the function.
28
INTERFACE
29
si
30
31
/// Function values on the LLVM target. A closure is
32
/// `{ ptr descriptor, ptr function, ptr receiver }`: calling it calls
33
/// the function with the receiver, then the call's own arguments. The
34
/// receiver is null for a function that takes none.
35
///
36
/// The function a closure holds has exactly the signature its type calls
37
/// through: a receiver, then the arguments, then the result. A function
38
/// that does not, because it takes no receiver, answers a result the
39
/// value's type discards, or is reached through a vtable or the trait
40
/// table, is reached through a thunk that does.
41
class LLVM_CLOSURES is
42
_module: MODULE
43
_objects: LLVM_OBJECTS
44
_thunks: MAP[THUNK_KEY, FUNCTION]
45
_unboxing: MAP[FUNCTION, FUNCTION]
46
_referencing: MAP[FUNCTION, FUNCTION]
47
_constants: MAP[FUNCTION, LLVM.Value]
48
_is_used: bool
49
50
/// The size of a closure in bytes.
51
SIZE: int static => 24
52
53
/// The offset of the function a closure calls.
54
FUNCTION_OFFSET: int static => 8
55
56
/// The offset of the receiver a closure passes.
57
RECEIVER_OFFSET: int static => 16
58
59
/// The name of the descriptor every closure carries.
60
DESCRIPTOR_NAME: string static => "$closure.$descriptor"
61
62
/// The class number a closure's descriptor holds, which falls in no
63
/// class's range.
64
CLASS_ID: int static => 0x7fffffff
65
66
/// - module: where thunks and the descriptor are added
67
/// - objects: the program's classes, whose vtables and trait table the thunks dispatch through
68
init(module: MODULE, objects: LLVM_OBJECTS) is
69
_module = module
70
_objects = objects
71
_thunks = MAP[THUNK_KEY, FUNCTION]()
72
_unboxing = MAP[FUNCTION, FUNCTION]()
73
_referencing = MAP[FUNCTION, FUNCTION]()
74
_constants = MAP[FUNCTION, LLVM.Value]()
75
si
76
77
/// The descriptor every closure carries.
78
descriptor: LLVM.Value is
79
_is_used = true
80
81
return Values.global(DESCRIPTOR_NAME)
82
si
83
84
/// The one closure holding `thunk` with no receiver, a constant every
85
/// evaluation of a capture-free function literal yields, as the
86
/// delegate .NET caches for it is. It lies outside the collected heap
87
/// and holds no heap pointers, so the collector need not know of it.
88
///
89
/// - thunk: the function the closure calls
90
constant(thunk: FUNCTION) -> LLVM.Value is
91
let found: LLVM.Value mut
92
93
if _constants.try_get_value(thunk, found ref) then
94
return found
95
fi
96
97
let type = Types.structure([Types.pointer, Types.pointer, Types.pointer])
98
99
let global =
100
GLOBAL_VARIABLE(
101
_objects.names.unique("{thunk.name}.$value"),
102
type,
103
LLVM.Value.AGGREGATE([descriptor, thunk.reference, Values.null_pointer], type),
104
true)
105
106
global.linkage = Linkage.INTERNAL
107
global.alignment = 8
108
109
_module.add_global(global)
110
111
let reference = global.reference
112
113
_constants[thunk] = reference
114
115
return reference
116
si
117
118
/// The function a closure of the function type `signature` holds to
119
/// call `target`, the compiled function of `instance`, reached as
120
/// `kind` says.
121
///
122
/// - signature: the function type a call through the closure has
123
/// - instance: the function the closure stands for
124
/// - target: its compiled function, which only `DROP_RECEIVER` and `PASS_RECEIVER` call
125
/// - kind: how the thunk reaches the function
126
/// - offset: the vtable slot or trait table row a dispatching thunk reads
127
thunk(signature: LLVM.Type.FUNCTION, instance: FUNCTION_INSTANCE, target: FUNCTION?, kind: ThunkKind, offset: int) -> FUNCTION? is
128
let discards = signature.result =~ LLVM.Type.VOID /\ !(target?.result =~ LLVM.Type.VOID ?? true)
129
130
if kind == ThunkKind.PASS_RECEIVER /\ !discards /\ target? then
131
if _same_signature(signature, target) then
132
return target
133
fi
134
fi
135
136
let key = THUNK_KEY(instance, kind, signature)
137
let found: FUNCTION mut
138
139
if _thunks.try_get_value(key, found ref) then
140
return found
141
fi
142
143
let parameters = LIST[PARAMETER]()
144
145
for (i, type) in signature.parameters |> Ghul.Pipes.index() do
146
parameters.add(PARAMETER(type, "a{i}"))
147
od
148
149
let thunk = FUNCTION(_objects.names.unique("{_objects.names.label(instance)}.$thunk"), signature.result, parameters.to_array())
150
151
thunk.linkage = Linkage.INTERNAL
152
153
let block = thunk.add_block("entry")
154
let receiver = thunk.parameter(0)
155
let arguments = LIST[LLVM.Value]()
156
157
if kind != ThunkKind.DROP_RECEIVER then
158
arguments.add(receiver)
159
fi
160
161
for i in 1..signature.parameters.count do
162
arguments.add(thunk.parameter(i))
163
od
164
165
let result =
166
case kind
167
when ThunkKind.DROP_RECEIVER, ThunkKind.PASS_RECEIVER then
168
if let t = target then block.call(t, arguments.to_array()) else null fi
169
else
170
_dispatch(block, signature, instance, receiver, kind, offset, arguments)
171
esac
172
173
if signature.result =~ LLVM.Type.VOID then
174
block.return_void()
175
elif let r = result then
176
block.return_value(r)
177
else
178
block.return_value(LLVM_TYPES.zero(signature.result))
179
fi
180
181
_module.add_function(thunk)
182
183
_thunks[key] = thunk
184
185
return thunk
186
si
187
188
// Calls through the receiver's vtable slot, or its row of the trait
189
// table, at `offset`.
190
_dispatch(
191
block: BLOCK,
192
signature: LLVM.Type.FUNCTION,
193
instance: FUNCTION_INSTANCE,
194
receiver: LLVM.Value,
195
kind: ThunkKind,
196
offset: int,
197
arguments: LIST[LLVM.Value]
198
) -> LLVM.Value? is
199
let descriptor = block.load(Types.pointer, receiver, 8)
200
201
let entry =
202
if kind == ThunkKind.VIRTUAL then
203
block.element_pointer(Types.i8, descriptor, [Values.i32(LLVM_OBJECTS.DESCRIPTOR_VTABLE + 8 * offset)], true)
204
else
205
let class_id = block.load(Types.i32, descriptor, 8)
206
let index = block.binary(LLVM.BinaryOperator.ADD, LLVM.IntegerFlags.NONE, class_id, Values.i32(offset))
207
208
block.element_pointer(Types.pointer, Values.global(LLVM_OBJECTS.TRAIT_TABLE_NAME), [index], true)
209
fi
210
211
let function = block.load(Types.pointer, entry, 8)
212
213
let called = _called_signature(instance)
214
215
if !called? then
216
return null
217
fi
218
219
let result = block.call(called, function, arguments.to_array())
220
221
return if signature.result =~ LLVM.Type.VOID then null else result fi
222
si
223
224
// The signature of the method a dispatching thunk calls: the receiver,
225
// its arguments and its result, at the instance's type arguments.
226
_called_signature(instance: FUNCTION_INSTANCE) -> LLVM.Type? is
227
let parameters = LIST[LLVM.Type]()
228
229
parameters.add(Types.pointer)
230
231
for argument in instance.definition.arguments do
232
if let held = LLVM_TYPES.value_type(instance.specialize(argument)) then
233
parameters.add(held)
234
else
235
return null
236
fi
237
od
238
239
if let result = LLVM_TYPES.result_type(instance.specialize(instance.definition.return_type)) then
240
return LLVM.Type.FUNCTION(result, parameters.to_array(), false)
241
fi
242
243
return null
244
si
245
246
/// The function a box's vtable slot or trait table entry holds for a
247
/// method of the value it boxes, a scalar or a struct, whose receiver
248
/// is the address of that value: it is called with the box, and calls
249
/// `target` with the address of the value within it.
250
///
251
/// - target: the method's compiled function
252
unboxing_thunk(target: FUNCTION) -> FUNCTION is
253
let found: FUNCTION mut
254
255
if _unboxing.try_get_value(target, found ref) then
256
return found
257
fi
258
259
let parameters = LIST[PARAMETER]()
260
261
for (i, parameter) in target.parameters |> Ghul.Pipes.index() do
262
parameters.add(PARAMETER(parameter.type, "a{i}"))
263
od
264
265
let thunk = FUNCTION(_objects.names.unique("{target.name}.$unboxed"), target.result, parameters.to_array())
266
267
thunk.linkage = Linkage.INTERNAL
268
269
let block = thunk.add_block("entry")
270
let arguments = LIST[LLVM.Value]()
271
272
arguments.add(block.element_pointer(Types.i8, thunk.parameter(0), [Values.i32(LLVM_OBJECTS.BOX_VALUE_OFFSET)], true))
273
274
for i in 1..parameters.count do
275
arguments.add(thunk.parameter(i))
276
od
277
278
let result = block.call(target, arguments.to_array())
279
280
if target.result =~ LLVM.Type.VOID then
281
block.return_void()
282
else
283
block.return_value(result!)
284
fi
285
286
_module.add_function(thunk)
287
288
_unboxing[target] = thunk
289
290
return thunk
291
si
292
/// The function a bigint's vtable slot holds for one of its
293
/// methods. A bigint is a reference to its box, and its methods take
294
/// the address of that reference: the thunk is called with the box,
295
/// and calls `target` with the address of a slot holding it.
296
///
297
/// - target: the method's compiled function
298
referencing_thunk(target: FUNCTION) -> FUNCTION is
299
let found: FUNCTION mut
300
301
if _referencing.try_get_value(target, found ref) then
302
return found
303
fi
304
305
let parameters = LIST[PARAMETER]()
306
307
for (i, parameter) in target.parameters |> Ghul.Pipes.index() do
308
parameters.add(PARAMETER(parameter.type, "a{i}"))
309
od
310
311
let thunk = FUNCTION(_objects.names.unique("{target.name}.$referencing"), target.result, parameters.to_array())
312
313
thunk.linkage = Linkage.INTERNAL
314
315
let block = thunk.add_block("entry")
316
let slot = block.alloca(Types.pointer, 0)
317
318
block.store(thunk.parameter(0), slot, 0)
319
320
let arguments = LIST[LLVM.Value]()
321
322
arguments.add(slot)
323
324
for i in 1..parameters.count do
325
arguments.add(thunk.parameter(i))
326
od
327
328
let result = block.call(target, arguments.to_array())
329
330
if target.result =~ LLVM.Type.VOID then
331
block.return_void()
332
else
333
block.return_value(result!)
334
fi
335
336
_module.add_function(thunk)
337
338
_referencing[target] = thunk
339
340
return thunk
341
si
342
343
_same_signature(signature: LLVM.Type.FUNCTION, target: FUNCTION) -> bool is
344
if signature.parameters.count != target.parameters.count \/ !(signature.result =~ target.result) then
345
return false
346
fi
347
348
for i in 0..signature.parameters.count do
349
if !(signature.parameters[i] =~ target.parameters[i].type) then
350
return false
351
fi
352
od
353
354
return true
355
si
356
357
/// Adds the closures' descriptor, where any closure was made. Its
358
/// vtable is the root's, so a virtual call through `object` on a
359
/// closure runs the root's method.
360
finish(strings: LLVM_STRINGS, callee: (FUNCTION_INSTANCE) -> FUNCTION?) is
361
if !_is_used then
362
return
363
fi
364
365
let entries = _objects.root_vtable(callee)
366
let vtable_type = LLVM.Type.ARRAY(entries.count, Types.pointer)
367
368
let type =
369
Types.structure([
370
Types.i32, Types.i32, Types.pointer, Types.pointer, Types.pointer, Types.pointer, Types.i64, vtable_type
371
])
372
373
let superclass =
374
if let root = _objects.root then Values.global(_objects.descriptor_name(root)) else Values.null_pointer fi
375
376
let initializer =
377
LLVM.Value.AGGREGATE(
378
[
379
Values.i32(CLASS_ID),
380
Values.i32(CLASS_ID),
381
superclass,
382
strings.literal("System.Delegate"),
383
Values.null_pointer,
384
_objects.type_object(DESCRIPTOR_NAME, strings.literal("System.Delegate")),
385
Values.i64(cast long(SIZE)),
386
LLVM.Value.AGGREGATE(entries.to_array(), vtable_type)
387
],
388
type)
389
390
let global = GLOBAL_VARIABLE(DESCRIPTOR_NAME, type, initializer, true)
391
392
global.linkage = Linkage.INTERNAL
393
global.alignment = 8
394
395
_module.add_global(global)
396
si
397
si
398
399
/// A thunk, told apart by the function it stands for, how it reaches it,
400
/// and the signature it is called with.
401
class THUNK_KEY(instance: FUNCTION_INSTANCE, kind: ThunkKind, signature: LLVM.Type) is
402
=~(other: THUNK_KEY) -> bool pure =>
403
instance =~ other.instance /\ kind == other.kind /\ signature =~ other.signature
404
405
get_hash_code() -> int pure => instance.get_hash_code()
406
si
407
si