Skip to content
← Back

src/ir/llvm/llvm_lowering.ghul

1
namespace IR.LLVMTarget is
2
use IR.Program
3
4
use Collections.LIST
5
use Collections.MAP
6
7
use Semantic.Types.Type
8
use Semantic.Symbols.Function
9
10
use LLVM.BinaryOperator
11
use LLVM.BLOCK
12
use LLVM.Conversion
13
use LLVM.FloatPredicate
14
use LLVM.FUNCTION
15
use LLVM.IntegerFlags
16
use LLVM.IntegerPredicate
17
use LLVM.Intrinsics
18
use LLVM.MODULE
19
use LLVM.Types
20
use LLVM.FastMath
21
22
use Values.Value
23
24
/// Lowers the IR values of one function body into the blocks of an
25
/// LLVM function. A construct it cannot lower yet is recorded in
26
/// `unsupported` rather than thrown.
27
///
28
/// Every local, argument and temporary lives in an `alloca` in the entry
29
/// block, which LLVM promotes to registers, so nothing here builds SSA
30
/// form. The values the IR leaves on CIL's evaluation stack are kept on
31
/// a stack of LLVM values while the body is lowered.
32
class LLVM_LOWERING: Values.Visitor is
33
_module: MODULE
34
_function: FUNCTION
35
_instance: FUNCTION_INSTANCE?
36
_type_map: Collections.Map[Semantic.Symbols.Symbol, Type]
37
_return_type: Type?
38
_await_structures: MAP[Values.AWAIT_SUSPEND, Values.REGION]
39
_callee: (FUNCTION_INSTANCE) -> FUNCTION?
40
_scalars: (Type?) -> Scalar?
41
_is_string: (Type?) -> bool
42
_strings: LLVM_STRINGS?
43
_arrays: LLVM_ARRAYS?
44
_objects: LLVM_OBJECTS?
45
_receiver_offset: int
46
47
_entry: BLOCK
48
_block: BLOCK
49
_is_terminated: bool
50
_next_block: int
51
52
_stack: LIST[LLVM.Value]
53
54
// The calls of the `for` loops lowered over their storage.
55
_counted_loops: MAP[Value, COUNTED_LOOP]
56
57
// Receivers already lowered, each for the one value that reads it.
58
_preevaluated: MAP[Value, LLVM.Value]
59
60
// Where each value with a known exact-class source came from.
61
_sources: MAP[LLVM.Value, EXACT_SOURCE]
62
63
// The values that are nulls the program wrote or defaults.
64
_nulls: Collections.SET[LLVM.Value]
65
66
// The shallowest the stack has been since the innermost _value_of
67
// began, so a value that reads an operand already on the stack, as
68
// a member load reads its receiver, still counts as producing one.
69
_low_water: int
70
71
_locals: MAP[LOCAL, (pointer: LLVM.Value, type: LLVM.Type)]
72
_arguments: LIST[(pointer: LLVM.Value, type: LLVM.Type, value_type: Type?)]
73
74
// The structured nodes open at the point being lowered, innermost
75
// last: each node's continuation, and a loop's header after it, which
76
// is where a CONTINUE goes. A region that produces a value has an
77
// `alloca` each arm stores it into, and `depth` is how deep the value
78
// stack was when the node began.
79
_labels: LIST[(node: Values.Breakable, is_loop_head: bool, target: BLOCK, result: LLVM.Value?, depth: int)]
80
81
// The try being lowered's landing pad, which calls unwind to, or null
82
// outside every try.
83
_unwind: BLOCK?
84
85
// Where each catch being lowered keeps the exception it caught,
86
// innermost last.
87
_caught: LIST[LLVM.Value]
88
89
// The position on the label stack of each try with a finally whose
90
// protected body is being lowered, with the slot an exit leaving it
91
// records its number in and the numbers given out.
92
_finally_exits: MAP[int, (selector: LLVM.Value, exits: IR.Program.EXIT_NUMBERING)]
93
94
// The block of each resume point, made where a dispatch or the point
95
// itself is first lowered.
96
_resume_blocks: MAP[IR.LABEL, BLOCK]
97
98
_unsupported: LIST[string]
99
100
/// Where the debug information is recorded, or null where the module
101
/// carries none.
102
debug_info: LLVM_DEBUG_INFO? public
103
104
/// The subprogram the function's locations are scoped to, where it
105
/// carries debug information.
106
debug_scope: LLVM.Metadata? public
107
108
_debug_location: LLVM.Metadata?
109
_debug_position: Source.LOCATION?
110
_debug_line: int
111
_debug_column: int
112
113
/// The kinds of construct the lowered values used that this target
114
/// cannot generate yet, each once, in the order they were met. A body
115
/// with any is not a valid function.
116
unsupported: Collections.List[string] => _unsupported
117
118
/// - module: where intrinsics the body calls are declared
119
/// - function: the function the body is lowered into, with no blocks yet
120
/// - instance: the function instance the body belongs to, or null where it belongs to none
121
/// - return_type: the type the function returns
122
/// - callee: the LLVM function a call to an instance names, or null where it cannot be called
123
/// - strings: where string literals and the runtime's functions are declared, or null where strings are not lowered
124
/// - arrays: where array descriptors are declared, or null where arrays are not lowered
125
/// - objects: the program's classes, or null where objects are not lowered
126
init(
127
module: MODULE,
128
function: FUNCTION,
129
instance: FUNCTION_INSTANCE?,
130
return_type: Type?,
131
callee: (FUNCTION_INSTANCE) -> FUNCTION?,
132
strings: LLVM_STRINGS?,
133
arrays: LLVM_ARRAYS?,
134
objects: LLVM_OBJECTS?
135
) is
136
self.init(module, function, instance, return_type, callee, strings, arrays, objects, t => LLVM_TYPES.scalar(t), t => LLVM_TYPES.is_string(t))
137
si
138
139
/// - module: where intrinsics the body calls are declared
140
/// - function: the function the body is lowered into, with no blocks yet
141
/// - instance: the function instance the body belongs to, or null where it belongs to none
142
/// - return_type: the type the function returns
143
/// - callee: the LLVM function a call to an instance names, or null where it cannot be called
144
/// - strings: where string literals and the runtime's functions are declared, or null where strings are not lowered
145
/// - arrays: where array descriptors are declared, or null where arrays are not lowered
146
/// - objects: the program's classes, or null where objects are not lowered
147
/// - scalars: the scalar kind a value of a type is, or null where it is not a scalar
148
/// - is_string: whether a type is `string`
149
init(
150
module: MODULE,
151
function: FUNCTION,
152
instance: FUNCTION_INSTANCE?,
153
return_type: Type?,
154
callee: (FUNCTION_INSTANCE) -> FUNCTION?,
155
strings: LLVM_STRINGS?,
156
arrays: LLVM_ARRAYS?,
157
objects: LLVM_OBJECTS?,
158
scalars: (Type?) -> Scalar?,
159
is_string: (Type?) -> bool
160
) is
161
_strings = strings
162
_arrays = arrays
163
_objects = objects
164
_receiver_offset = if instance?.definition?.is_emitted_with_receiver ?? false then 1 else 0 fi
165
_scalars = scalars
166
_is_string = is_string
167
_module = module
168
_function = function
169
_instance = instance
170
_type_map = if let i = instance then TYPE_MAPS.body_map(i) else MAP[Semantic.Symbols.Symbol, Type]() fi
171
_return_type = return_type
172
_callee = callee
173
174
_stack = LIST[LLVM.Value]()
175
_counted_loops = MAP[Value, COUNTED_LOOP](Collections.ReferenceEqualityComparer.instance)
176
_preevaluated = MAP[Value, LLVM.Value](Collections.ReferenceEqualityComparer.instance)
177
_sources = MAP[LLVM.Value, EXACT_SOURCE](Collections.ReferenceEqualityComparer.instance)
178
_nulls = Collections.SET[LLVM.Value](Collections.ReferenceEqualityComparer.instance)
179
_locals = MAP[LOCAL, (pointer: LLVM.Value, type: LLVM.Type)]()
180
_arguments = LIST[(pointer: LLVM.Value, type: LLVM.Type, value_type: Type?)]()
181
_labels = LIST[(node: Values.Breakable, is_loop_head: bool, target: BLOCK, result: LLVM.Value?, depth: int)]()
182
_caught = LIST[LLVM.Value]()
183
_await_structures = MAP[Values.AWAIT_SUSPEND, Values.REGION]()
184
_finally_exits = MAP[int, (selector: LLVM.Value, exits: IR.Program.EXIT_NUMBERING)]()
185
_resume_blocks = MAP[IR.LABEL, BLOCK]()
186
_unsupported = LIST[string]()
187
188
_entry = function.add_block("entry")
189
_block = _entry
190
si
191
192
/// Lowers the body into the function. Afterwards every block ends in
193
/// exactly one terminator.
194
lower(values: Collections.Iterable[Value]) is
195
// An instruction before the body's first position, such as an
196
// argument's spill, is placed at the function's declaration.
197
if let instance = _instance then
198
_note_position(instance.definition.location)
199
_entry.debug_location = _debug_location
200
fi
201
202
// Each argument is spilled to a slot of the width its type is stored
203
// at, so its address means the same as any other location's.
204
for (i, parameter) in _function.parameters |> Ghul.Pipes.index() do
205
let value_type = _argument_type(i)
206
let stored = if let scalar = _scalar(value_type) then LLVM_TYPES.stored_type(scalar) else parameter.type fi
207
let pointer = _entry.alloca(stored, 0)
208
209
_entry.store(_narrow_in(_entry, parameter.value, value_type), pointer, 0)
210
_arguments.add((pointer, stored, value_type))
211
od
212
213
_declare_arguments()
214
215
let start = _new_block()
216
217
_start(start)
218
219
for value in values do
220
gen(value)
221
od
222
223
// A body that falls off its end returns what it leaves, or the
224
// default value of its result where it leaves nothing.
225
if !_is_terminated then
226
_return()
227
fi
228
229
_entry.branch(start)
230
si
231
232
/// Lowers one value. An absent value lowers to nothing. Where the
233
/// function carries debug information, what it emits is recorded
234
/// against the value's source position.
235
gen(value: Value?) is
236
if value? then
237
if value.has_location then
238
_note_position(value.location)
239
fi
240
241
if _counted_loops.count > 0 /\ _lower_counted(value) then
242
return
243
fi
244
245
value.accept(self)
246
fi
247
si
248
249
// Each argument is declared to a debugger as the variable it is, in
250
// the slot it is spilled to.
251
_declare_arguments() is
252
let info = debug_info
253
let scope = debug_scope
254
let instance = _instance
255
256
if !info? \/ !scope? \/ !instance? \/ !_debug_location? then
257
return
258
fi
259
260
let definition = instance.definition
261
262
for i in 0.._arguments.count do
263
let (name, type) =
264
if i < _receiver_offset then
265
("self", null)
266
elif i - _receiver_offset < definition.argument_names.count then
267
(definition.argument_names[i - _receiver_offset], instance.specialize(definition.arguments[i - _receiver_offset]))
268
else
269
continue
270
fi
271
272
_entry.declare(_arguments[i].pointer, info.variable(name, i + 1, scope, definition.location, type), _debug_location!)
273
od
274
si
275
276
// A local the source names is declared to a debugger, under the name
277
// it has in the source. One the compiler made up starts with a `.`.
278
_declare_local(local: LOCAL, pointer: LLVM.Value) is
279
if let info = debug_info, scope = debug_scope, position = _debug_position, location = _debug_location then
280
if local.name.starts_with('.') \/ local.name.length == 0 then
281
return
282
fi
283
284
let dot = local.name.last_index_of('.')
285
let name = if dot > 0 then local.name.substring(0, dot) else local.name fi
286
287
_entry.declare(pointer, info.variable(name, 0, scope, position, _specialize(local.type)), location)
288
fi
289
si
290
291
_note_position(location: Source.LOCATION) is
292
if let info = debug_info, scope = debug_scope then
293
_debug_position = location
294
295
if location.start_line != _debug_line \/ location.start_column != _debug_column then
296
_debug_line = location.start_line
297
_debug_column = location.start_column
298
_debug_location = info.location(location, scope)
299
_block.debug_location = _debug_location
300
fi
301
fi
302
si
303
304
_report(construct: string) is
305
if !_unsupported.contains(construct) then
306
_unsupported.add(construct)
307
fi
308
si
309
310
_specialize(type: Type?) -> Type? => TYPE_MAPS.specialize(type, _type_map)
311
312
_scalar_of(type: Type?) -> Scalar? => _scalars(type)
313
314
_scalar(type: Type?) -> Scalar? => _scalar_of(_specialize(type))
315
316
_held(type: Type?) -> LLVM.Type? =>
317
if isa Semantic.Types.REFERENCE(_specialize(type)) then
318
Types.pointer
319
elif let inner = _value_optional_inner(type) then
320
_optional_type(inner)
321
elif let scalar = _scalar(type) then
322
LLVM_TYPES.value_type(scalar)
323
elif _is_string(_specialize(type)) \/ LLVM_TYPES.is_array(_specialize(type)) \/ LLVM_TYPES.is_object_reference(_specialize(type)) then
324
Types.pointer
325
else
326
_objects?.held_type(_specialize(type))
327
fi
328
329
// The type a value of `type` is stored as in memory: a scalar at the
330
// width of its kind, anything else as it is held.
331
_stored(type: Type?) -> LLVM.Type? =>
332
if let scalar = _scalar(type) then LLVM_TYPES.stored_type(scalar) else _held(type) fi
333
334
// `value` of `type` as loaded from memory, widened to the width it is
335
// computed with.
336
_widen(value: LLVM.Value, type: Type?) -> LLVM.Value =>
337
if let scalar = _scalar(type) then LLVM_TYPES.widen(_current, value, scalar) else value fi
338
339
// `value` of `type` narrowed to the width it is stored at.
340
_narrow(value: LLVM.Value, type: Type?) -> LLVM.Value => _narrow_in(_current, value, type)
341
342
_narrow_in(block: BLOCK, value: LLVM.Value, type: Type?) -> LLVM.Value =>
343
if let scalar = _scalar(type) then LLVM_TYPES.narrow(block, value, scalar) else value fi
344
345
// A value of `type` loaded from `pointer`, which holds it as stored.
346
_load_stored(type: Type?, pointer: LLVM.Value) -> LLVM.Value? =>
347
if let stored = _stored(type) then _widen(_current.load(stored, pointer, 0), type) else null fi
348
349
// Stores `value` of `type` at `pointer`, at the width it is stored at.
350
_store_stored(value: LLVM.Value, type: Type?, pointer: LLVM.Value) is
351
_current.store(_narrow(value, type), pointer, 0)
352
si
353
354
// The member at `index` of an aggregate held in place, a struct, a
355
// tuple or an optional value type, whose type is `member`.
356
_extract_member(aggregate: LLVM.Value, index: int, member: Type?) -> LLVM.Value =>
357
_widen(_current.extract_value(_stored(member)!, aggregate, [index]), member)
358
359
// `aggregate` with its member at `index`, of type `member`, replaced.
360
_insert_member(aggregate: LLVM.Value, value: LLVM.Value, index: int, member: Type?) -> LLVM.Value =>
361
_current.insert_value(aggregate, _narrow(value, member), [index])
362
363
// The type of the argument at `index` among the function's
364
// parameters, or null for the receiver or where there is no instance.
365
_argument_type(index: int) -> Type? is
366
if let instance = _instance then
367
let at = index - _receiver_offset
368
369
if at >= 0 /\ at < instance.definition.arguments.count then
370
return instance.specialize(instance.definition.arguments[at])
371
fi
372
fi
373
374
return null
375
si
376
377
// The block instructions go into, opened afresh where the last one
378
// ended in a terminator: code after an unconditional exit is
379
// unreachable, but every block needs exactly one terminator.
380
_current: BLOCK is
381
if _is_terminated then
382
_start(_new_block())
383
fi
384
385
return _block
386
si
387
388
_new_block() -> BLOCK is
389
let block = _function.add_block("b{_next_block}")
390
391
_next_block = _next_block + 1
392
393
return block
394
si
395
396
_start(block: BLOCK) is
397
_block = block
398
block.debug_location = _debug_location
399
_is_terminated = false
400
si
401
402
_branch(target: BLOCK) is
403
_current.branch(target)
404
_is_terminated = true
405
si
406
407
_push(value: LLVM.Value) is
408
_stack.add(value)
409
si
410
411
_pop() -> LLVM.Value? is
412
if _stack.count == 0 then
413
_report("a value the lowering did not produce")
414
return null
415
fi
416
417
let value = _stack[_stack.count - 1]
418
419
_stack.remove_at(_stack.count - 1)
420
421
if _stack.count < _low_water then
422
_low_water = _stack.count
423
fi
424
425
return value
426
si
427
428
_peek() -> LLVM.Value? =>
429
if _stack.count > 0 then _stack[_stack.count - 1] else null fi
430
431
// Lowers `value` and takes what it produces off the stack.
432
_value_of(value: Value) -> LLVM.Value? is
433
if _preevaluated.count > 0 then
434
let ready: LLVM.Value mut
435
436
if _preevaluated.try_get_value(value, ready ref) then
437
_preevaluated.remove(value)
438
439
return ready
440
fi
441
fi
442
443
// A value already on the stack, or its address there, is taken
444
// from it: lowering one leaves the stack as deep as it was.
445
let inner = if let address: Values.ADDRESS = value then address.value else value fi
446
447
if isa Values.STACK_TOP(inner) \/ isa Values.STACK_TOP_ADDRESS(inner) then
448
return _pop()
449
fi
450
451
let outer_low_water = _low_water
452
453
_low_water = _stack.count
454
455
gen(value)
456
457
let produced = _stack.count > _low_water
458
459
if _low_water > outer_low_water then
460
_low_water = outer_low_water
461
fi
462
463
if !produced then
464
if _unsupported.count == 0 then
465
_report("a value that produces nothing")
466
fi
467
468
return null
469
fi
470
471
return _pop()
472
si
473
474
// An `alloca` in the entry block, holding the zero of `type` from the
475
// start, as a .NET local holds a zeroed value.
476
_slot(type: LLVM.Type) -> LLVM.Value is
477
let pointer = _entry.alloca(type, 0)
478
479
_entry.store(LLVM_TYPES.zero(type), pointer, 0)
480
481
return pointer
482
si
483
484
_local(local: LOCAL) -> (pointer: LLVM.Value, type: LLVM.Type)? is
485
let found: (pointer: LLVM.Value, type: LLVM.Type) mut
486
487
if _locals.try_get_value(local, found ref) then
488
return found
489
fi
490
491
if let type = _stored(local.type) then
492
let slot = (pointer = _slot(type), type = type)
493
494
_locals[local] = slot
495
496
_declare_local(local, slot.pointer)
497
498
return slot
499
fi
500
501
_report("a local of type {local.type}")
502
503
return null
504
si
505
506
_local_address(local: LOCAL) is
507
_note_store(local, null)
508
509
if let (pointer, _) = _local(local) then
510
_push(pointer)
511
fi
512
si
513
514
// `value` where it is an address, and otherwise a new place holding
515
// it. Null stays null.
516
_held_in_place(value: LLVM.Value?) -> LLVM.Value? is
517
if !value? \/ isa LLVM.Type.POINTER(value.type) then
518
return value
519
fi
520
521
let slot = _slot(value.type)
522
523
_current.store(value, slot, 0)
524
525
return slot
526
si
527
528
// The address of `value`, for a struct method's receiver or a field
529
// of a struct. A value with a place of its own is reached where it
530
// is, so a method changes it there; any other is copied into a new
531
// place first, as .NET copies a temporary.
532
_address_of(value: Value) -> LLVM.Value? is
533
// A receiver the IR already gives as an address is that address.
534
// Where what the stack holds is the value itself, as the payload a
535
// coalescing access unwraps is, it is copied into a new place.
536
if isa Values.STACK_TOP_ADDRESS(value) then
537
return _held_in_place(_pop())
538
fi
539
540
if isa Values.ADDRESS(value) then
541
return _held_in_place(_value_of(value))
542
fi
543
544
if value.has_address then
545
let depth = _stack.count
546
547
value.accept_address(self)
548
549
if _stack.count > depth then
550
return _pop()
551
fi
552
553
return null
554
fi
555
556
let stored = _stored(value.type)
557
558
if !stored? then
559
_report("the address of a value of type {value.type}")
560
return null
561
fi
562
563
let result = _value_of(value)
564
565
if !result? then
566
return null
567
fi
568
569
let slot = _slot(stored)
570
571
_store_stored(result, value.type, slot)
572
573
return slot
574
si
575
576
_load_local(local: LOCAL) is
577
if let (pointer, type) = _local(local) then
578
let value = _widen(_current.load(type, pointer, 0), local.type)
579
580
if let instance = _instance then
581
_sources[value] = EXACT_SOURCE(null, instance, local)
582
fi
583
584
_push(value)
585
fi
586
si
587
588
_store_local(local: LOCAL, value: LLVM.Value?) is
589
if !_is_null(value) then
590
_note_store(local, _source_of(value))
591
fi
592
593
if let (pointer, _) = _local(local) /\ value? then
594
_current.store(_narrow(value, local.type), pointer, 0)
595
fi
596
si
597
598
// The position of the parameter `symbol` among the arguments of the
599
// function being lowered, or null where it belongs to another.
600
_argument_index(symbol: Semantic.Symbols.Symbol) -> int? is
601
if let instance = _instance /\ symbol.owner == instance.definition then
602
let names = instance.definition.argument_names
603
604
for i in 0..names.count do
605
if names[i] =~ symbol.name then
606
return i + _receiver_offset
607
fi
608
od
609
fi
610
611
return null
612
si
613
614
_load_argument(index: int) is
615
if index < _arguments.count then
616
let (pointer, type, value_type) = _arguments[index]
617
618
_push(_widen(_current.load(type, pointer, 0), value_type))
619
else
620
_report("an argument the function does not take")
621
fi
622
si
623
624
_store_argument(index: int, value: LLVM.Value?, type: Type?) is
625
if index < _arguments.count /\ value? then
626
let (pointer, _, _) = _arguments[index]
627
628
_current.store(_narrow(value, type), pointer, 0)
629
else
630
_report("an argument the function does not take")
631
fi
632
si
633
634
// Where a value came from, as far as its exact class goes, or null
635
// where nothing is known of it.
636
_source_of(value: LLVM.Value?) -> EXACT_SOURCE? is
637
let source: EXACT_SOURCE mut
638
639
if let v = value /\ _sources.try_get_value(v, source ref) then
640
return source
641
fi
642
643
return null
644
si
645
646
// Records, while discovering, what is stored in `local`: a value
647
// from `source`, or anything at all where it is null.
648
_note_store(local: LOCAL, source: EXACT_SOURCE?) is
649
if let objects = _objects, instance = _instance /\ !objects.is_built then
650
objects.exact.note_store(instance, local, source)
651
fi
652
si
653
654
_is_null(value: LLVM.Value?) -> bool => if let v = value then _nulls.contains(v) else false fi
655
656
// The exact class of `value`, where it is known to be one class.
657
_exact_class(value: LLVM.Value) -> CLASS_INSTANCE? =>
658
if let objects = _objects then objects.exact.exact(_source_of(value)) else null fi
659
660
// Returns the value on the stack, or the default value of the result
661
// where the stack is empty.
662
_return() is
663
if LLVM_TYPES.is_void(_return_type) then
664
_current.return_void()
665
elif let type = _held(_return_type) then
666
let is_default = _stack.count == 0
667
let value = if _stack.count > 0 then _pop()! else LLVM_TYPES.zero(type) fi
668
669
if let objects = _objects, instance = _instance /\ !objects.is_built /\ !is_default /\ !_is_null(value) then
670
objects.exact.note_return(instance, _source_of(value))
671
fi
672
673
_current.return_value(_normalise_for(value, _return_type))
674
else
675
_report("a result of type {_return_type}")
676
_current.unreachable()
677
fi
678
679
_is_terminated = true
680
si
681
682
// Brings a value of a narrow kind back into its range: sign-extended
683
// for a signed kind and masked for an unsigned one, which is what
684
// CIL's narrowing conversions and narrow stores do.
685
_normalise(value: LLVM.Value, scalar: Scalar) -> LLVM.Value =>
686
case scalar
687
when Scalar.INT8 then _sign_extend_from(value, 8)
688
when Scalar.INT16 then _sign_extend_from(value, 16)
689
when Scalar.UINT8 then _current.binary(BinaryOperator.AND, IntegerFlags.NONE, value, _i32(0xFF))
690
when Scalar.UINT16, Scalar.CHAR then _current.binary(BinaryOperator.AND, IntegerFlags.NONE, value, _i32(0xFFFF))
691
else value
692
esac
693
694
_sign_extend_from(value: LLVM.Value, width: int) -> LLVM.Value =>
695
_current.convert(
696
Conversion.SIGN_EXTEND,
697
_current.convert(Conversion.TRUNCATE, value, LLVM.Type.INTEGER(width)),
698
Types.i32)
699
700
_normalise_for(value: LLVM.Value, type: Type?) -> LLVM.Value =>
701
if let scalar = _scalar(type) /\ LLVM_TYPES.is_narrow(scalar) then
702
_normalise(value, scalar)
703
else
704
value
705
fi
706
707
_i32(value: int) -> LLVM.Value => LLVM.Values.integer(Types.i32, bigint(value))
708
709
visit_value(value: Value) is
710
if let lowered: LLVM_VALUE = value then
711
_push(lowered.value)
712
else
713
_report("{value.get_type().name}")
714
fi
715
si
716
717
visit_address_of_value(value: Value) is
718
_report("the address of {value.get_type().name}")
719
si
720
721
visit(number: Values.Literal.NUMBER) is
722
let (value, from) =
723
case number.constant
724
when i4: Values.Literal.CONSTANT.I4 then (LLVM.Values.integer(Types.i32, bigint(i4.value)), Scalar.INT32)
725
when i8: Values.Literal.CONSTANT.I8 then (LLVM.Values.integer(Types.i64, bigint(i8.value)), Scalar.INT64)
726
when r4: Values.Literal.CONSTANT.R4 then (LLVM.Values.of_single(r4.value), Scalar.FLOAT32)
727
when r8: Values.Literal.CONSTANT.R8 then (LLVM.Values.of_double(r8.value), Scalar.FLOAT64)
728
esac
729
730
if let to = number.narrowing then
731
_push(_convert(value, from, to))
732
elif _scalar(number.type) =~ Scalar.BOOL then
733
_push(_convert(value, from, Scalar.BOOL))
734
else
735
_push(value)
736
fi
737
si
738
739
visit(string: Values.Literal.STRING) is
740
if let strings = _strings then
741
_push(strings.literal(string.value))
742
else
743
_report("a string literal")
744
fi
745
si
746
visit(decimal: Values.Literal.DECIMAL) is _report("a decimal literal") si
747
// A bigint literal that fits in a long is built into the box's i64
748
// field. A wider one is built at run time from its decimal digits,
749
// through the static `parse` the core library gives `bigint`.
750
visit(bigint: Values.Literal.BIGINT) is
751
let small: long mut
752
753
if long.try_parse(bigint.value, small ref) then
754
let long_type = IoC.CONTAINER.instance.innate_symbol_lookup.get_long_type()
755
756
_bigint_of_small(bigint.type, Values.Literal.NUMBER(Values.Literal.CONSTANT.I8(small), long_type))
757
elif let parse = _static_method(bigint.type, "parse", 1) then
758
let string_type = IoC.CONTAINER.instance.innate_symbol_lookup.get_string_type()
759
760
gen(Values.Call.STATIC(parse, LIST[Value]([Values.Literal.STRING(bigint.value, string_type)]), bigint.type, null))
761
else
762
_report("a bigint literal, whose type has no parse")
763
fi
764
si
765
766
visit(load: Values.Load.SYMBOL) is _report("a load of {load.symbol.name}") si
767
visit_address(load: Values.Load.SYMBOL) is _report("the address of {load.symbol.name}") si
768
visit_address(load: Values.Load.INSTANCE_FIELD) is
769
if let address = _field_address(load.from, load.symbol) then
770
_push(address)
771
fi
772
si
773
774
visit(load: Values.Load.TEMP) is
775
_load_local(load.local)
776
si
777
778
visit_address(load: Values.Load.TEMP) is _local_address(load.local) si
779
visit_address(load: Values.Load.STATIC_FIELD) is _global_address(load.symbol, true) si
780
visit_address(load: Values.Load.GLOBAL_FIELD) is _global_address(load.symbol, false) si
781
782
_global_address(symbol: Semantic.Symbols.Symbol, is_static: bool) is
783
if let (name, _) = _global(symbol, is_static) then
784
_push(LLVM.Values.global(name))
785
fi
786
si
787
788
visit(load: Values.Load.LOCAL_ARGUMENT) is
789
let symbol = load.symbol
790
791
if let state_machine_field = Values.state_machine_field_for(symbol) then
792
gen(_frame_field_load(state_machine_field))
793
elif let packed_local = Values.packed_parameter_local_for(symbol) then
794
_load_local(packed_local)
795
elif let index = _argument_index(symbol) then
796
_load_argument(index)
797
else
798
_report("an argument of another function")
799
fi
800
si
801
802
visit_address(load: Values.Load.LOCAL_ARGUMENT) is
803
let symbol = load.symbol
804
805
if let state_machine_field = Values.state_machine_field_for(symbol) then
806
_frame_field_load(state_machine_field).accept_address(self)
807
elif let packed_local = Values.packed_parameter_local_for(symbol) then
808
_local_address(packed_local)
809
elif let index = _argument_index(symbol) /\ index < _arguments.count then
810
_push(_arguments[index].pointer)
811
else
812
_report("the address of an argument of another function")
813
fi
814
si
815
816
visit(load: Values.Load.LOCAL_VARIABLE) is
817
if let state_machine_field = Values.state_machine_field_for(load.symbol) then
818
gen(_frame_field_load(state_machine_field))
819
elif let variable = cast Semantic.Symbols.Variable?(load.symbol) then
820
_load_local(variable.ir_local)
821
else
822
_report("a load of {load.symbol.name}")
823
fi
824
si
825
826
visit_address(load: Values.Load.LOCAL_VARIABLE) is
827
if let state_machine_field = Values.state_machine_field_for(load.symbol) then
828
_frame_field_load(state_machine_field).accept_address(self)
829
elif let variable = cast Semantic.Symbols.Variable?(load.symbol) then
830
_local_address(variable.ir_local)
831
else
832
_report("the address of {load.symbol.name}")
833
fi
834
si
835
836
visit(load: Values.Load.ARGUMENT) is
837
_load_argument(load.index + _receiver_offset)
838
si
839
840
/// A composite value's structured expansion is lowered as any other
841
/// structured IR is.
842
visit(composite: Values.Composite) is
843
// A capture-free literal the cache would hold is one constant
844
// closure instead.
845
if let memoized: Values.MEMOIZED_DELEGATE = composite /\ memoized.function? then
846
_closure(Values.MAKE_CLOSURE(memoized.type, memoized.function!, Values.NULL(Semantic.Types.NULL())), true)
847
return
848
fi
849
850
for value in composite.expand().values do
851
gen(value)
852
od
853
si
854
855
visit_address(coalesce: Values.COALESCE_LOAD) is _report("the address of a coalesced load") si
856
857
// An asynchronous function's body constructs its frame, marks it not
858
// yet started, gives it a builder, starts it through the builder,
859
// and answers the builder's task, as the .NET async builder protocol
860
// has it. The builder's members are found by name and argument count
861
// on the builder type the frame was given.
862
visit(launch: Values.ASYNC_METHOD_LAUNCH) is
863
let builder_type = launch.builder_field.type!
864
let local = launch.state_machine_local
865
let frame = Values.Load.TEMP(local, launch.state_machine_type)
866
867
let arguments =
868
if let block: Values.BLOCK = launch.ctor_arg_loads then
869
LIST[Value](block.values)
870
else
871
LIST[Value]([launch.ctor_arg_loads])
872
fi
873
874
gen(Values.DECLARE_LOCAL(local))
875
gen(Values.NEW(launch.state_machine_type, launch.constructor, arguments))
876
gen(Values.STORE_TEMP(local))
877
878
gen(Values.Store.INSTANCE_FIELD(frame, launch.state_field, _int_literal(-1)))
879
880
if let create = _builder_member(builder_type, "create", 0) then
881
gen(Values.Store.INSTANCE_FIELD(frame, launch.builder_field, Values.Call.STATIC(create, LIST[Value](), builder_type, null)))
882
fi
883
884
let builder = Values.Load.INSTANCE_FIELD(frame, launch.builder_field)
885
886
if let start = _builder_generic_member(builder_type, "start", 1, LIST[Type]([launch.state_machine_type])) then
887
gen(Values.Call.INSTANCE(builder, start, _by_reference(start, LIST[Value]([frame]), LIST[bool]([true])), null))
888
fi
889
890
_builder_task(builder, builder_type)
891
si
892
893
// The end of a frame's `move_next`: the machine is finished, and the
894
// builder is handed the result, or the exception the handler caught,
895
// which the handler leaves on the stack.
896
visit(completion: Values.ASYNC_COMPLETION) is
897
let builder_type = completion.builder_field.type!
898
899
let exception =
900
if let exception_type = completion.exception_type then
901
let local = IR.LOCAL(".caught_exception", exception_type)
902
903
gen(Values.DECLARE_LOCAL(local))
904
_store_local(local, _pop())
905
906
Values.Load.TEMP(local, exception_type)
907
else
908
null
909
fi
910
911
gen(_frame_field_store(completion.state_field, _int_literal(-1)))
912
913
let builder = _frame_field_load(completion.builder_field)
914
915
if let e = exception then
916
if let set_exception = _builder_member(builder_type, "set_exception", 1) then
917
gen(Values.Call.INSTANCE(builder, set_exception, LIST[Value]([e]), null))
918
fi
919
else
920
let arguments =
921
if let result = completion.result_field then
922
LIST[Value]([_frame_field_load(result)])
923
else
924
LIST[Value]()
925
fi
926
927
if let set_result = _builder_member(builder_type, "set_result", arguments.count) then
928
gen(Values.Call.INSTANCE(builder, set_result, arguments, null))
929
fi
930
fi
931
si
932
933
// A task-like that is complete from the start: a builder is created,
934
// given its result at once, and asked for its task.
935
visit(completed: Values.COMPLETED_TASK_LIKE) is
936
let builder_type = completed.builder_type
937
938
let create = _builder_member(builder_type, "create", 0)
939
let set_result = _builder_member(builder_type, "set_result", 0)
940
941
if !create? \/ !set_result? then
942
return
943
fi
944
945
let local = IR.LOCAL(".completed_builder", builder_type)
946
let builder = Values.Load.TEMP(local, builder_type)
947
948
gen(Values.DECLARE_LOCAL(local))
949
gen(Values.Call.STATIC(create, LIST[Value](), builder_type, null))
950
gen(Values.STORE_TEMP(local))
951
952
gen(Values.Call.INSTANCE(builder, set_result, LIST[Value](), null))
953
954
_builder_task(builder, builder_type)
955
si
956
957
// An await is a region holding its steps and the resume point a
958
// later `move_next` re-enters at, and then the awaiter's result.
959
// The region is made once for each await, since every lowering of
960
// the body has to see the same resume point.
961
visit(suspend: Values.AWAIT_SUSPEND) is
962
let structure: Values.REGION mut
963
964
if !_await_structures.try_get_value(suspend, structure ref) then
965
structure = suspend.structure()
966
_await_structures[suspend] = structure
967
fi
968
969
gen(structure)
970
gen(suspend.get_result)
971
si
972
973
visit(step: Values.AWAIT_SUSPEND_STEP) is
974
let suspend = step.suspend
975
976
case step.kind
977
when Values.AwaitSuspendStep.CAPTURE_AWAITER then
978
gen(_frame_field_store(suspend.awaiter_field, suspend.get_awaiter))
979
980
when Values.AwaitSuspendStep.REGISTER_CONTINUATION then
981
_stamp_state(suspend, suspend.state_number)
982
_register_continuation(suspend)
983
984
when Values.AwaitSuspendStep.CLEAR_STATE then
985
_stamp_state(suspend, -1)
986
esac
987
si
988
989
// The state goes to both the local the dispatch reads and the
990
// frame's field, which the next `move_next` starts from.
991
_stamp_state(suspend: Values.AWAIT_SUSPEND, state: int) is
992
_store_local(suspend.state_local, _i32(state))
993
994
gen(_frame_field_store(suspend.state_field, _int_literal(state)))
995
si
996
997
// Registers the frame's resumption with the awaiter through the
998
// builder's `await_on_completed`, or `await_unsafe_on_completed` for
999
// an awaiter implementing `ICriticalNotifyCompletion`. The awaiter
1000
// goes by reference, as the address of the frame's copy, and the
1001
// machine by reference or as the interface, as the member takes it.
1002
_register_continuation(suspend: Values.AWAIT_SUSPEND) is
1003
let builder_type = suspend.builder_field.type!
1004
let name = if suspend.is_critical then "await_unsafe_on_completed" else "await_on_completed" fi
1005
1006
let member = Semantic.TASK_LIKE.member_of_arity(builder_type, name, 2)
1007
1008
if !member? then
1009
_report("a builder with no {name}")
1010
return
1011
fi
1012
1013
let machine_by_reference = isa Semantic.Types.REFERENCE(member.arguments[1])
1014
1015
let type_arguments = LIST[Type]([suspend.awaiter_type])
1016
1017
if member.generic_arguments.count > 1 then
1018
type_arguments.add(suspend.state_machine_type)
1019
fi
1020
1021
if let await_on_completed = _builder_generic_member(builder_type, name, 2, type_arguments) then
1022
let frame = Values.Load.REFERENCE_SELF(cast Semantic.Symbols.Classy(suspend.state_field.owner), suspend.state_machine_type)
1023
1024
gen(
1025
Values.Call.INSTANCE(
1026
_frame_field_load(suspend.builder_field),
1027
await_on_completed,
1028
_by_reference(
1029
await_on_completed,
1030
LIST[Value]([_frame_field_load(suspend.awaiter_field), frame]),
1031
LIST[bool]([true, machine_by_reference])),
1032
null))
1033
fi
1034
si
1035
1036
// The arguments of a call to a builder member, those `by_reference`
1037
// marks passed as their address, as the member's `ref` parameters
1038
// take them.
1039
_by_reference(function: Function, arguments: Collections.List[Value], by_reference: Collections.List[bool]) -> LIST[Value] is
1040
let result = LIST[Value]()
1041
1042
for i in 0..arguments.count do
1043
if by_reference[i] then
1044
result.add(Values.ADDRESS(arguments[i], function.arguments[i]))
1045
else
1046
result.add(arguments[i])
1047
fi
1048
od
1049
1050
return result
1051
si
1052
1053
// The builder member `name` taking `arity` arguments, or null, with
1054
// the build reported, where the builder declares none.
1055
_builder_member(builder_type: Type, name: string, arity: int) -> Function? is
1056
let member = Semantic.TASK_LIKE.member_of_arity(builder_type, name, arity)
1057
1058
if !member? then
1059
_report("a builder with no {name} taking {arity} arguments")
1060
fi
1061
1062
return member
1063
si
1064
1065
// The generic builder member `name`, at `type_arguments`.
1066
_builder_generic_member(builder_type: Type, name: string, arity: int, type_arguments: Collections.List[Type]) -> Function? is
1067
if let member = _builder_member(builder_type, name, arity) then
1068
return cast Function?(member.specialize(type_arguments))
1069
fi
1070
1071
return null
1072
si
1073
1074
_builder_task(builder: Value, builder_type: Type) is
1075
if let property: Semantic.Symbols.Property = builder_type.find_member("task") /\ property.read_function? then
1076
gen(Values.Call.INSTANCE(builder, property.read_function, LIST[Value](), null))
1077
else
1078
_report("a builder with no task")
1079
fi
1080
si
1081
1082
_int_literal(value: int) -> Value =>
1083
Values.Literal.NUMBER(value, IoC.CONTAINER.instance.innate_symbol_lookup.get_int_type())
1084
1085
1086
visit(max_stack: Values.MAX_STACK) is si
1087
// A MAYBE holding a value is built by the core library's constructor
1088
// taking that value.
1089
visit(construct: Values.CONSTRUCT_MAYBE) is
1090
if let constructor = INTERPOLATION_CALLS.method(construct.type, "init", 1) then
1091
gen(Values.NEW(construct.type, constructor, LIST[Value]([construct.value])))
1092
else
1093
_report("an optional value, whose MAYBE has no constructor taking its value")
1094
fi
1095
si
1096
1097
// The value is already on the stack, where whatever reads it finds it.
1098
visit(stack_top: Values.STACK_TOP) is si
1099
// An address already on top of the stack, which stays where it is.
1100
visit(stack_top: Values.STACK_TOP_ADDRESS) is si
1101
visit_address(stack_top: Values.STACK_TOP_ADDRESS) is si
1102
1103
/// A block lowers each of its values in turn. One that produces a
1104
/// value and ends in a region produces it through that region.
1105
visit(block: Values.BLOCK) is
1106
let values = block.values
1107
1108
for i in 0..values.count do
1109
let value = values[i]
1110
1111
if i == values.count - 1 /\ _produces_value(block) /\ isa Values.REGION(value) then
1112
let result = _held(block.type)
1113
1114
if !result? then
1115
_report("a value of type {block.type}")
1116
fi
1117
1118
_lower_region(cast Values.REGION(value), result)
1119
else
1120
gen(value)
1121
fi
1122
od
1123
si
1124
1125
// A body every path out of which diverges, by a throw or a return,
1126
// is typed as an error, and produces no value.
1127
_produces_value(block: Values.BLOCK) -> bool =>
1128
!LLVM_TYPES.is_void(block.type) /\ !(block.type?.is_error ?? false)
1129
1130
visit(block: Values.DUMMY_BLOCK) is
1131
throw System.InvalidOperationException("dummy block generated, created at: {block.location} because: {block.why}")
1132
si
1133
1134
// A struct method's receiver is already the address of the struct.
1135
// A class's receiver is a reference, so its address is the slot the
1136
// reference is held in, as an asynchronous function's frame passes
1137
// itself by reference to the builder that resumes it.
1138
visit_address(self_load: Values.Load.REFERENCE_SELF) is
1139
if isa Semantic.Symbols.STRUCT(self_load.`self) then
1140
if let pointer = _receiver_address() then
1141
_push(pointer)
1142
fi
1143
elif _receiver_offset == 0 \/ _arguments.count == 0 then
1144
_report("self outside an instance method")
1145
else
1146
_push(_arguments[0].pointer)
1147
fi
1148
si
1149
/// Inside a generator's frame, the receiver of the method that made
1150
/// it, held in a field of the frame.
1151
visit(self_load: Values.Load.OUTER_SELF) is gen(_frame_field_load(self_load.outer_self_field)) si
1152
visit_address(self_load: Values.Load.OUTER_SELF) is visit(self_load) si
1153
// A struct method's receiver is the address of its struct.
1154
visit(self_load: Values.Load.VALUE_SELF) is
1155
if !_stored(self_load.type)? then
1156
_report("self of type {self_load.type}")
1157
return
1158
fi
1159
1160
if let pointer = _receiver_address() then
1161
_push(_load_stored(self_load.type, pointer)!)
1162
fi
1163
si
1164
1165
visit_address(self_load: Values.Load.VALUE_SELF) is
1166
if let pointer = _receiver_address() then
1167
_push(pointer)
1168
fi
1169
si
1170
1171
_receiver_address() -> LLVM.Value? is
1172
if _receiver_offset == 0 \/ _arguments.count == 0 then
1173
_report("self outside an instance method")
1174
return null
1175
fi
1176
1177
return _current.load(Types.pointer, _arguments[0].pointer, 0)
1178
si
1179
/// An object is allocated whole where it is constructed, so there
1180
/// is nothing for its constructor to initialise first, and a program
1181
/// runs on one thread.
1182
visit(runtime: Values.RUNTIME) is
1183
if runtime.operation == IR.RuntimeOperation.INIT_OBJECT then
1184
return
1185
elif runtime.operation == IR.RuntimeOperation.CURRENT_THREAD_ID then
1186
_push(_i32(0))
1187
elif runtime.operation == IR.RuntimeOperation.NEW_RANGE then
1188
// A range is constructed from its two endpoints.
1189
if let constructor = INTERPOLATION_CALLS.method(runtime.type, "init", 2) then
1190
gen(Values.NEW(runtime.type!, constructor, LIST[Value](runtime.operands)))
1191
else
1192
_report("a range, whose type has no constructor taking its endpoints")
1193
fi
1194
elif let kind = _runtime_exception(runtime.operation) then
1195
if let exception = _new_exception(kind, runtime.operands) then
1196
_push(exception)
1197
fi
1198
elif runtime.operation == IR.RuntimeOperation.STRINGS_EQUAL then
1199
_call_string_helper(_strings?.equals_function, runtime.operands)
1200
elif runtime.operation == IR.RuntimeOperation.CONCAT_STRINGS then
1201
_call_string_helper(_strings?.concat_function, runtime.operands)
1202
elif !_lower_builder(runtime) then
1203
_report("the runtime operation {runtime.operation}")
1204
fi
1205
si
1206
visit(creation: Values.CREATE_UNIT_VARIANT_INSTANCE) is _report("a union") si
1207
1208
visit(dummy: Values.DUMMY) is _report("a placeholder value") si
1209
visit(type_expression: Values.TYPE_EXPRESSION) is _report("a type used as a value") si
1210
visit(store: Values.Need.STORE) is _report("an indirect store") si
1211
// A store through a reference, which is a pointer to where its value
1212
// is held.
1213
visit(store: Values.Store.INDIRECT) is
1214
if let pointer = _value_of(store.address), value = _value_of(store.value) then
1215
_store_stored(value, store.type, pointer)
1216
fi
1217
si
1218
visit(wrapper: Values.TYPE_WRAPPER) is gen(wrapper.value) si
1219
visit_address(wrapper: Values.TYPE_WRAPPER) is _push_address_of(wrapper.value) si
1220
visit(wrapper: Values.WRAPPER) is
1221
assert wrapper.value? else "generate wrapper without value"
1222
1223
gen(wrapper.value)
1224
si
1225
visit_address(wrapper: Values.WRAPPER) is
1226
if let value = wrapper.value then
1227
_push_address_of(value)
1228
else
1229
_report("the address of a value")
1230
fi
1231
si
1232
1233
// The value type `type` is an optional of, or null where it is not
1234
// one. A reference to an optional, which a method of the optional
1235
// receives, counts as the optional.
1236
_value_optional_inner(type: Type?) -> Type? is
1237
let specialized mut = _specialize(type)
1238
1239
if let reference: Semantic.Types.REFERENCE = specialized then
1240
specialized = reference.arguments[0]
1241
fi
1242
1243
if specialized? /\ specialized.is_optional /\ specialized.is_value_type /\ !specialized.is_maybe then
1244
return specialized.optional_inner_type
1245
fi
1246
1247
return null
1248
si
1249
1250
// An optional value type is held in place as whether it is present,
1251
// a byte holding 0 or 1, and its value as stored: `{ i8, T }`, zeroed
1252
// where absent.
1253
_optional_type(inner: Type) -> LLVM.Type? =>
1254
if let stored = _stored(inner) then Types.structure([Types.i8, stored]) else null fi
1255
1256
// The present optional of type `optional` holding `value` of type
1257
// `inner`.
1258
_wrap(value: LLVM.Value, optional: LLVM.Type, inner: Type?) -> LLVM.Value =>
1259
_insert_member(_current.insert_value(LLVM_TYPES.zero(optional), LLVM.Values.integer(Types.i8, 1n), [0]), value, 1, inner)
1260
1261
// Whether the optional value type `optional` holds a value.
1262
_is_present(optional: LLVM.Value) -> LLVM.Value =>
1263
_current.convert(Conversion.TRUNCATE, _current.extract_value(Types.i8, optional, [0]), Types.i1)
1264
1265
visit(wrap: Values.WRAP_OPTIONAL) is
1266
// A MAYBE, the optional of an unconstrained type parameter, is the
1267
// core library's struct, built by its constructor.
1268
if _specialize(wrap.type)?.is_maybe ?? false then
1269
gen(Values.CONSTRUCT_MAYBE(_specialize(wrap.type)!, wrap.value))
1270
return
1271
fi
1272
1273
let optional = _held(wrap.type)
1274
1275
if !optional? \/ !_value_optional_inner(wrap.type)? then
1276
_report("an optional value of type {wrap.type}")
1277
return
1278
fi
1279
1280
if let value = _value_of(wrap.value) then
1281
_push(_wrap(value, optional, _value_optional_inner(wrap.type)))
1282
fi
1283
si
1284
1285
// `has_value` and `value` of an optional value type, called on where
1286
// it is held. Reading the value of an absent optional throws, as .NET
1287
// does.
1288
_lower_nullable(operation: string, from: Value?) is
1289
let inner = if let f = from then _value_optional_inner(f.type) else null fi
1290
let optional = if let i = inner then _optional_type(i) else null fi
1291
1292
if !from? \/ !optional? then
1293
_report("the operation {operation}")
1294
return
1295
fi
1296
1297
let pointer = _optional_pointer(from)
1298
1299
if !pointer? then
1300
return
1301
fi
1302
1303
let held = _current.load(optional, pointer, 0)
1304
let present = _is_present(held)
1305
1306
if operation =~ "nullable.has_value" then
1307
_push(present)
1308
elif operation =~ "nullable.value" then
1309
let absent = _new_block()
1310
let carry_on = _new_block()
1311
1312
_current.branch(present, carry_on, absent)
1313
1314
_start(absent)
1315
1316
let message = Values.Literal.STRING("Nullable object must have a value.", IoC.CONTAINER.instance.innate_symbol_lookup.get_string_type())
1317
1318
// Where the program has no such class, what is thrown carries
1319
// no type or message, as for any exception the runtime makes.
1320
if IR.Program.CORE_LIBRARY.exception_class(Semantic.Lookups.RuntimeException.INVALID_OPERATION)? then
1321
_throw(_new_exception(Semantic.Lookups.RuntimeException.INVALID_OPERATION, LIST[Value]([message])) ?? LLVM.Values.null_pointer)
1322
else
1323
_throw(LLVM.Values.null_pointer)
1324
fi
1325
1326
_start(carry_on)
1327
1328
_push(_extract_member(held, 1, inner))
1329
else
1330
_report("the operation {operation}")
1331
fi
1332
si
1333
1334
// Where the optional `from` stands for is held: `from` itself where
1335
// it is a reference to one, otherwise its address.
1336
_optional_pointer(from: Value) -> LLVM.Value? =>
1337
if isa Semantic.Types.REFERENCE(_specialize(from.type)) then _value_of(from) else _address_of(from) fi
1338
// A narrowed value is the value it narrows, read as its narrower
1339
// type: the IR already holds what that reading is.
1340
visit(projection: Values.NARROW_PROJECT) is gen(projection.projected) si
1341
visit(view: Values.NARROW_VIEW) is gen(view.underlying) si
1342
// A view held as its underlying value is where that value is; one
1343
// held otherwise, such as an optional value type viewed as the value
1344
// it holds, is copied into a place of its own.
1345
visit_address(view: Values.NARROW_VIEW) is
1346
let viewed = _stored(view.type)
1347
let underlying = _stored(view.underlying.type)
1348
1349
if viewed? /\ underlying? /\ viewed =~ underlying then
1350
_push_address_of(view.underlying)
1351
elif let stored = viewed, value = _value_of(view) then
1352
let slot = _slot(stored)
1353
1354
_store_stored(value, view.type, slot)
1355
_push(slot)
1356
else
1357
_report("the address of a value of type {view.type}")
1358
fi
1359
si
1360
1361
_push_address_of(value: Value) is
1362
if let address = _address_of(value) then
1363
_push(address)
1364
fi
1365
si
1366
visit(enter: Values.ENTER_FILE) is si
1367
visit(leave: Values.LEAVE_FILE) is si
1368
1369
visit(address: Values.ARRAY_ELEMENT_ADDRESS) is _element_address(address) si
1370
visit_address(address: Values.ARRAY_ELEMENT_ADDRESS) is _element_address(address) si
1371
1372
// The address of an array element, checked against the array's count.
1373
_element_address(address: Values.ARRAY_ELEMENT_ADDRESS) is
1374
let arrays = _arrays
1375
let type = _array_of(address.array.type)
1376
1377
if !arrays? \/ !type? then
1378
_report("the address of an array element")
1379
return
1380
fi
1381
1382
if let array = _value_of(address.array), index = _value_of(address.index) then
1383
_check_index(array, index)
1384
_push(arrays.element_address(_current, type, array, index))
1385
fi
1386
si
1387
1388
visit(array: Values.NEW_ARRAY) is
1389
_new_array(array.type, array.count)
1390
si
1391
1392
visit(store: Values.STORE_ARRAY_ELEMENT) is
1393
_store_array_element(store.array, store.index, store.element)
1394
si
1395
1396
visit(sequence: Values.SEQUENCE) is
1397
let type = _array_of(sequence.type)
1398
1399
if !type? then
1400
return
1401
fi
1402
1403
let arrays = _arrays!
1404
let result = arrays.allocate(_current, type, _i32(sequence.values.count))
1405
1406
// An element whose type is not the array's, an int in an
1407
// object[], is boxed as it is stored.
1408
let boxer = IoC.CONTAINER.instance.value_boxer
1409
1410
for (i, value) in sequence.values |> Ghul.Pipes.index() do
1411
if let element = _value_of(boxer.box_if_needed(value, sequence.element_type)) then
1412
arrays.store(_current, type, result, _i32(i), _normalise_for(element, sequence.element_type))
1413
else
1414
return
1415
fi
1416
od
1417
1418
_push(result)
1419
si
1420
1421
// The array type a value of `type` is, or null, reported, where the
1422
// target holds no arrays of its element type.
1423
_array_of(type: Type?) -> CLASS_INSTANCE? is
1424
if let arrays = _arrays, array = arrays.array_of(_specialize(type)) then
1425
return array
1426
fi
1427
1428
_report("an array of type {type}")
1429
1430
return null
1431
si
1432
1433
_new_array(array_type: Type?, count: Value) is
1434
if let type = _array_of(array_type), length = _value_of(count) then
1435
_push(_arrays!.allocate(_current, type, length))
1436
fi
1437
si
1438
1439
// An element whose type is not the array's, an int in an object[],
1440
// is boxed as it is stored.
1441
_store_array_element(array_value: Value, index_value: Value, element_value: Value) is
1442
let type = _array_of(array_value.type)
1443
1444
if !type? then
1445
return
1446
fi
1447
1448
let boxed = IoC.CONTAINER.instance.value_boxer.box_if_needed(element_value, _arrays!.element_of(type))
1449
1450
if let array = _value_of(array_value),
1451
index = _value_of(index_value),
1452
element = _value_of(boxed)
1453
then
1454
let arrays = _arrays!
1455
1456
_check_index(array, index)
1457
1458
arrays.store(_current, type, array, index, _normalise_for(element, arrays.element_of(type)))
1459
fi
1460
si
1461
1462
// Throws where `array` is null, or where `index` is outside the array
1463
// it points at, an unsigned comparison catching a negative index too.
1464
_check_index(array: LLVM.Value, index: LLVM.Value) is
1465
_check_dereference(array)
1466
1467
let in_range = _current.compare(IntegerPredicate.ULT, index, _arrays!.count(_current, array))
1468
1469
_throw_if(_current.compare(IntegerPredicate.EQ, in_range, LLVM.Values.boolean(false)), Semantic.Lookups.RuntimeException.INDEX_OUT_OF_RANGE)
1470
si
1471
1472
// The built-in array operation a call to `function` performs, or
1473
// null where it performs none.
1474
_array_operation(function: Function) -> string? is
1475
if let operation = cast Function?(function.root_specialized_from)?.intrinsic_operation /\ operation.starts_with("array.") then
1476
return operation
1477
fi
1478
1479
return null
1480
si
1481
1482
visit(compare: Values.COMPARE_ORDER_TO_ZERO) is
1483
if let value = _value_of(compare.value) then
1484
_push(_compare(compare.comparison, value, _i32(0), false))
1485
fi
1486
si
1487
1488
visit(receiver: Values.CONSTRAINED) is _report("a constrained call") si
1489
visit(arithmetic: Values.DECIMAL_ARITHMETIC) is _report("decimal arithmetic") si
1490
visit(compare: Values.DECIMAL_COMPARE) is _report("decimal arithmetic") si
1491
visit(convert: Values.DECIMAL_CONVERT) is _report("a decimal conversion") si
1492
/// A reference optional is present where it is not null. A value-type
1493
/// optional is not lowered yet.
1494
visit(test: Values.HAS_VALUE) is
1495
// A box of a value that is never absent is always present, so the
1496
// value is evaluated for its effects and nothing is allocated.
1497
if let boxed = _boxed_present_value(test.value) then
1498
if _value_of(boxed)? then
1499
_push(LLVM.Values.boolean(true))
1500
fi
1501
1502
return
1503
fi
1504
1505
if let value = _value_of(test.value) then
1506
if value.type =~ Types.pointer then
1507
_push(_current.compare(IntegerPredicate.NE, value, LLVM.Values.null_pointer))
1508
elif _value_optional_inner(test.value.type)? then
1509
_push(_is_present(value))
1510
else
1511
_report("an optional value of type {test.value.type}")
1512
fi
1513
fi
1514
si
1515
1516
visit(conversion: Values.SCALAR_CONVERSION) is
1517
gen(conversion.converted)
1518
si
1519
1520
visit(declaration: Values.DECLARE_LOCAL) is
1521
_local(declaration.local)
1522
_note_iteration(declaration.local)
1523
si
1524
1525
visit(store: Values.STORE_TEMP) is
1526
_store_local(store.local, _pop())
1527
si
1528
1529
visit(tee: Values.TEE_TEMP) is
1530
if let value = _value_of(tee.value) then
1531
let held = _normalise_for(value, tee.local.type)
1532
1533
_store_local(tee.local, held)
1534
_push(held)
1535
fi
1536
si
1537
1538
visit(discard: Values.DISCARD) is
1539
if let value = discard.value then
1540
let depth = _stack.count
1541
1542
gen(value)
1543
1544
while _stack.count > depth do
1545
_pop()
1546
od
1547
else
1548
_pop()
1549
fi
1550
si
1551
1552
visit(ret: Values.RET) is
1553
_return()
1554
si
1555
1556
visit(default_return: Values.DEFAULT_RETURN) is
1557
_default(default_return.type)
1558
_mark_null_top()
1559
si
1560
1561
_mark_null_top() is
1562
if _stack.count > 0 then
1563
_nulls.add(_stack[_stack.count - 1])
1564
fi
1565
si
1566
1567
visit(size_of: Values.SIZE_OF) is
1568
_push(LLVM.Values.i32(_size_of(size_of.measured)))
1569
si
1570
1571
// The bytes a value of `type` takes where it is stored, as an array's
1572
// element holds it.
1573
_size_of(type: Type) -> int =>
1574
if let stored = _objects?.stored_type(_specialize(type)) then LLVM_TYPES.size_of(stored) else 8 fi
1575
1576
visit(default_value: Values.DEFAULT) is
1577
_default(default_value.type)
1578
_mark_null_top()
1579
si
1580
1581
_default(type: Type?) is
1582
if LLVM_TYPES.is_void(type) then
1583
return
1584
elif let value_type = _held(type) then
1585
_push(LLVM_TYPES.zero(value_type))
1586
else
1587
_report("the default value of {type}")
1588
fi
1589
si
1590
1591
visit_address(default_value: Values.DEFAULT) is _report("the address of a value") si
1592
// A reference is a pointer to where its value is held.
1593
visit(deref: Values.DEREF) is
1594
if !_stored(deref.type)? then
1595
_report("a reference to {deref.type}")
1596
return
1597
fi
1598
1599
if let pointer = _value_of(deref.value) then
1600
_push(_load_stored(deref.type, pointer)!)
1601
fi
1602
si
1603
1604
visit_address(deref: Values.DEREF) is gen(deref.value) si
1605
1606
visit(address: Values.ADDRESS) is
1607
if let pointer = _address_of(address.value) then
1608
_push(pointer)
1609
fi
1610
si
1611
1612
visit_address(address: Values.ADDRESS) is _report("the address of a reference") si
1613
// Each step in turn, the last leaving the value.
1614
visit(steps: Values.STEPS) is
1615
for step in steps.steps do
1616
gen(step)
1617
od
1618
si
1619
1620
// Decrements the counter in place, leaving nothing.
1621
visit(decrement: Values.DECREMENT) is
1622
if let before = _counter(decrement.counter) then
1623
_store_local(decrement.counter, _step(before, BinaryOperator.SUB))
1624
fi
1625
si
1626
1627
// Leaves the counter's value before the increment, and stores the
1628
// incremented value back.
1629
visit(increment: Values.POST_INCREMENT) is
1630
if let before = _counter(increment.counter) then
1631
_store_local(increment.counter, _step(before, BinaryOperator.ADD))
1632
_push(before)
1633
fi
1634
si
1635
1636
// Leaves the counter's value after the decrement.
1637
visit(decrement: Values.PRE_DECREMENT) is
1638
if let before = _counter(decrement.counter) then
1639
let after = _step(before, BinaryOperator.SUB)
1640
1641
_store_local(decrement.counter, after)
1642
_push(after)
1643
fi
1644
si
1645
1646
_counter(counter: LOCAL) -> LLVM.Value? is
1647
_load_local(counter)
1648
1649
return _pop()
1650
si
1651
1652
_step(value: LLVM.Value, operator: BinaryOperator) -> LLVM.Value =>
1653
_current.binary(operator, IntegerFlags.NONE, value, _i32(1))
1654
1655
// The source is held in a local, which the rebuilt tuple reads.
1656
visit(repack: Values.TUPLE_REPACK) is
1657
_store_local(repack.temp, _value_of(repack.source))
1658
gen(repack.built)
1659
si
1660
// The subject already on the stack is held in a local, which what is
1661
// built from it reads.
1662
visit(spilled: Values.SPILLED_SUBJECT) is
1663
_store_local(spilled.temp, _pop())
1664
gen(spilled.built)
1665
si
1666
1667
visit(null_value: Values.NULL) is
1668
_push_null(LLVM.Values.null_pointer)
1669
si
1670
1671
// Pushes `value`, a null the program wrote or a default it falls
1672
// back on. A null reaches a call as a null receiver whatever class
1673
// the call is bound to, so it says nothing against exactness.
1674
_push_null(value: LLVM.Value) is
1675
_nulls.add(value)
1676
_push(value)
1677
si
1678
/// A scalar is boxed in an object carrying its box's descriptor, so a
1679
/// boxed int and a boxed long answer type tests differently. A
1680
/// reference needs no box.
1681
visit(box: Values.BOX) is
1682
let value = box.value
1683
1684
if _value_optional_inner(value.type)? then
1685
_box_optional(value)
1686
elif let objects = _objects, s = objects.struct_of(_specialize(value.type)) then
1687
if let held = _value_of(value) then
1688
_push(_box_struct(objects, s, held))
1689
fi
1690
elif let objects = _objects, box_type = objects.box_type(_specialize(value.type)) then
1691
if let held = _value_of(value) then
1692
_push(_box_value(objects, box_type, held, value.type))
1693
fi
1694
elif let held = _held(value.type) /\ isa LLVM.Type.POINTER(held) then
1695
gen(value)
1696
else
1697
_report("boxing a {value.type}")
1698
fi
1699
si
1700
1701
1702
// A new box of `box_type` holding `held`, a value of `type`.
1703
// A box holding something the collector must follow is one it scans.
1704
_box_value(objects: LLVM_OBJECTS, box_type: CLASS_INSTANCE, held: LLVM.Value, type: Type?) -> LLVM.Value is
1705
objects.note_boxed(box_type)
1706
1707
let scanned = LLVM_TYPES.holds_pointer(held.type)
1708
1709
let object =
1710
_strings!.allocate_object(_current, LLVM.Values.global(objects.descriptor_name(box_type)), LLVM.Values.i64(cast long(objects.size_of(box_type))), scanned)
1711
1712
_store_stored(held, type, _current.element_pointer(objects.struct_type(box_type), object, [_i32(0), _i32(1)], true))
1713
1714
return object
1715
si
1716
1717
// An optional value type boxes as .NET boxes one: an absent value is
1718
// null, and a present one is a box of the value it holds.
1719
_box_optional(value: Value) is
1720
let inner = _value_optional_inner(value.type)!
1721
let objects = _objects
1722
let box_type = if let o = objects then o.box_type(_specialize(inner)) else null fi
1723
let struct_type = if let o = objects then o.struct_of(_specialize(inner)) else null fi
1724
let optional = _held(value.type)
1725
1726
if !objects? \/ (!box_type? /\ !struct_type?) \/ !optional? then
1727
_report("boxing a {value.type}")
1728
return
1729
fi
1730
1731
let held = _value_of(value)
1732
1733
if !held? then
1734
return
1735
fi
1736
1737
let slot = _slot(Types.pointer)
1738
1739
_current.store(LLVM.Values.null_pointer, slot, 0)
1740
1741
let present = _new_block()
1742
let join = _new_block()
1743
1744
_current.branch(_is_present(held), present, join)
1745
1746
_start(present)
1747
1748
let inner_value = _extract_member(held, 1, inner)
1749
let object =
1750
if let b = box_type then
1751
_box_value(objects, b, inner_value, inner)
1752
else
1753
_box_struct(objects, struct_type!, inner_value)
1754
fi
1755
1756
_current.store(object, slot, 0)
1757
_current.branch(join)
1758
1759
_start(join)
1760
1761
_push(_current.load(Types.pointer, slot, 0))
1762
si
1763
1764
// Unboxing to an optional value type answers absent for a null, and
1765
// otherwise unboxes the value, throwing where the box is of another
1766
// type.
1767
_unbox_optional(value: Value, type: Type?) is
1768
let inner = _value_optional_inner(type)!
1769
let optional = _held(type)
1770
1771
if !optional? then
1772
_report("unboxing a {type}")
1773
return
1774
fi
1775
1776
let object = _value_of(value)
1777
1778
if !object? then
1779
return
1780
fi
1781
1782
let slot = _slot(optional)
1783
1784
_current.store(LLVM_TYPES.zero(optional), slot, 0)
1785
1786
let present = _new_block()
1787
let join = _new_block()
1788
1789
_current.branch(_current.compare(IntegerPredicate.NE, object, LLVM.Values.null_pointer), present, join)
1790
1791
_start(present)
1792
1793
if let unboxed = _unbox_value(object, inner) then
1794
let wrapped = _wrap(unboxed, optional, inner)
1795
1796
_current.store(wrapped, slot, 0)
1797
fi
1798
1799
_current.branch(join)
1800
1801
_start(join)
1802
1803
_push(_current.load(optional, slot, 0))
1804
si
1805
1806
// A cast to an optional value type tests the object as CIL's `isinst`
1807
// does, and never throws: it answers the object where that is a box
1808
// of the inner type, a scalar or a struct, and null otherwise. The
1809
// unboxing that follows it turns null into an absent value.
1810
_cast_to_optional(conversion: Values.CAST, target: Type) is
1811
let inner = _value_optional_inner(target)!
1812
let objects = _objects
1813
let box_type = if let o = objects then o.box_type(_specialize(inner)) ?? o.struct_of(_specialize(inner)) else null fi
1814
1815
if !objects? \/ !box_type? then
1816
_report("a cast to {conversion.type}")
1817
return
1818
fi
1819
1820
let object = _value_of(conversion.value)
1821
1822
if !object? then
1823
return
1824
fi
1825
1826
let range = LIST[(first: int, last: int)]()
1827
1828
range.add(objects.class_range(box_type))
1829
1830
let id = _class_id(object)
1831
let fits = _in_ranges(id, range)
1832
1833
_push(_current.select(fits, object, LLVM.Values.null_pointer))
1834
si
1835
/// A null, or a box of another type, throws, as .NET does. Unboxing
1836
/// through a type parameter follows the cast that tested the box,
1837
/// so there a null is the declined cast and gives the default.
1838
visit(unbox: Values.UNBOX) is
1839
if (unbox.type?.is_type_variable ?? false) /\ !LLVM_TYPES.is_object_reference(_specialize(unbox.type)) then
1840
_unbox_or_default(unbox.value, unbox.type)
1841
else
1842
_unbox(unbox.value, unbox.type)
1843
fi
1844
si
1845
1846
_unbox_or_default(value: Value, type: Type?) is
1847
let held = _held(type)
1848
let object = _value_of(value)
1849
1850
if !held? \/ !object? then
1851
if !held? then
1852
_report("unboxing a {_specialize(type)}")
1853
fi
1854
1855
return
1856
fi
1857
1858
let slot = _slot(held)
1859
1860
_current.store(LLVM_TYPES.zero(held), slot, 0)
1861
1862
let present = _new_block()
1863
let join = _new_block()
1864
1865
_current.branch(_current.compare(IntegerPredicate.NE, object, LLVM.Values.null_pointer), present, join)
1866
1867
_start(present)
1868
1869
if let unboxed = _unbox_value(object, type) then
1870
_current.store(unboxed, slot, 0)
1871
fi
1872
1873
_current.branch(join)
1874
1875
_start(join)
1876
1877
_push(_current.load(held, slot, 0))
1878
si
1879
1880
_unbox(value: Value, type: Type?) is
1881
if _value_optional_inner(type)? then
1882
_unbox_optional(value, type)
1883
return
1884
fi
1885
1886
if let object = _value_of(value), unboxed = _unbox_value(object, type) then
1887
_push(unboxed)
1888
fi
1889
si
1890
1891
// A struct boxes as an object carrying the struct's own descriptor,
1892
// so a type test finds it, with the struct after it. A struct holding
1893
// a reference is boxed where the collector scans it.
1894
_box_struct(objects: LLVM_OBJECTS, s: CLASS_INSTANCE, held: LLVM.Value) -> LLVM.Value is
1895
objects.note_allocated(s)
1896
1897
let boxed = Types.structure([Types.pointer, held.type])
1898
let scanned = LLVM_TYPES.holds_pointer(held.type)
1899
1900
let object =
1901
_strings!.allocate_object(_current, LLVM.Values.global(objects.descriptor_name(s)), LLVM.Values.i64(cast long(LLVM_TYPES.size_of(boxed))), scanned)
1902
1903
_current.store(held, _current.element_pointer(boxed, object, [_i32(0), _i32(1)], true), 0)
1904
1905
return object
1906
si
1907
1908
// The value of `type` held in the box `object` points at, or null,
1909
// reported, where the target has no box for `type`. A null, or a box
1910
// of another type, throws.
1911
_unbox_value(object: LLVM.Value, unboxed: Type?) -> LLVM.Value? is
1912
let objects = _objects
1913
let type = _specialize(unboxed)
1914
1915
if let o = objects, s = o.struct_of(type) then
1916
let held = o.struct_type(s)
1917
let range = LIST[(first: int, last: int)]()
1918
1919
range.add(o.class_range(s))
1920
1921
let id = _class_id(object)
1922
let fits = _in_ranges(id, range)
1923
1924
_throw_if(_current.compare(IntegerPredicate.EQ, fits, LLVM.Values.boolean(false)), Semantic.Lookups.RuntimeException.INVALID_CAST)
1925
1926
return _current.load(held, _current.element_pointer(Types.structure([Types.pointer, held]), object, [_i32(0), _i32(1)], true), 0)
1927
fi
1928
1929
// A bigint is held as a reference to its box, so the box is the
1930
// value once it is known to be one.
1931
if let o = objects, b = o.bigint_of(type) then
1932
let range = LIST[(first: int, last: int)]()
1933
1934
range.add(o.class_range(b))
1935
1936
let fits = _in_ranges(_class_id(object), range)
1937
1938
_throw_if(_current.compare(IntegerPredicate.EQ, fits, LLVM.Values.boolean(false)), Semantic.Lookups.RuntimeException.INVALID_CAST)
1939
1940
return object
1941
fi
1942
1943
// Unboxing to a reference type, as a type parameter bound to one
1944
// does, is a checked cast that lets null through.
1945
if type? /\ LLVM_TYPES.is_object_reference(type) then
1946
if type.is_object then
1947
return object
1948
fi
1949
1950
let ranges = _class_ranges(type)
1951
1952
if !ranges? then
1953
_report("unboxing a {type}")
1954
return null
1955
fi
1956
1957
let id = _class_id(object)
1958
let fits = _current.binary(BinaryOperator.OR, IntegerFlags.NONE, _in_ranges(id, ranges), _current.compare(IntegerPredicate.EQ, id, _i32(-1)))
1959
1960
_throw_if(_current.compare(IntegerPredicate.EQ, fits, LLVM.Values.boolean(false)), Semantic.Lookups.RuntimeException.INVALID_CAST)
1961
1962
return object
1963
fi
1964
1965
let box_type = if let o = objects then o.box_type(type) else null fi
1966
1967
if !objects? \/ !box_type? \/ !_stored(type)? then
1968
_report("unboxing a {type}")
1969
return null
1970
fi
1971
1972
let range = LIST[(first: int, last: int)]()
1973
1974
range.add(objects.class_range(box_type))
1975
1976
let id = _class_id(object)
1977
1978
_throw_if(_current.compare(IntegerPredicate.EQ, _in_ranges(id, range), LLVM.Values.boolean(false)), Semantic.Lookups.RuntimeException.INVALID_CAST)
1979
1980
return _load_stored(type, _current.element_pointer(objects.struct_type(box_type), object, [_i32(0), _i32(1)], true))
1981
si
1982
1983
/// The type object of a type named in the program: the one its
1984
/// descriptor stands for: a scalar's box's, or a struct's own.
1985
visit(type_of: Values.TYPEOF) is
1986
let objects = _objects!
1987
let type = _specialize(type_of.typeof_type)
1988
let held = if let box = objects.box_type(type) then box else objects.class_of(type) fi
1989
1990
if !held? then
1991
_report("typeof {type}")
1992
elif let t = _type_object(LLVM.Values.global(objects.descriptor_name(held))) then
1993
_push(t)
1994
fi
1995
si
1996
1997
// A tuple is a struct of its elements, in order, held in place.
1998
visit(tuple: Values.TUPLE) is
1999
let held = _held(tuple.type)
2000
2001
if !isa LLVM.Type.STRUCT(held) then
2002
_report("a tuple of type {tuple.type}")
2003
return
2004
fi
2005
2006
let result mut = LLVM_TYPES.zero(held!)
2007
let element_types = cast Semantic.Types.TUPLE?(_specialize(tuple.type))?.arguments
2008
2009
for (i, element) in tuple.values |> Ghul.Pipes.index() do
2010
if let value = _value_of(element) then
2011
result = _insert_member(result, value, i, if let types = element_types then types[i] else element.type fi)
2012
else
2013
return
2014
fi
2015
od
2016
2017
_push(result)
2018
si
2019
2020
visit(negation: Values.NOT) is
2021
if let value = _value_of(negation.value) then
2022
_push(_current.binary(BinaryOperator.XOR, IntegerFlags.NONE, value, LLVM.Values.boolean(true)))
2023
fi
2024
si
2025
2026
visit(arithmetic: Values.ARITHMETIC) is
2027
let type = _specialize(arithmetic.type)
2028
let held = if let scalar = _scalar_of(type) then LLVM_TYPES.value_type(scalar) else null fi
2029
2030
if !held? then
2031
_report("arithmetic on {arithmetic.type}")
2032
return
2033
fi
2034
2035
let operation = arithmetic.operation
2036
let is_floating = LLVM_TYPES.is_floating(held)
2037
2038
let result =
2039
if operation == ArithmeticOperation.NEGATE then
2040
_negate(arithmetic.operands[0], is_floating)
2041
elif operation == ArithmeticOperation.COMPLEMENT then
2042
_complement(arithmetic.operands[0], is_floating)
2043
elif _is_shift(operation) then
2044
_shift(operation, arithmetic.operands[0], arithmetic.operands[1], held, _scalar_of(type))
2045
else
2046
_binary(operation, arithmetic.operands[0], arithmetic.operands[1], is_floating, held!)
2047
fi
2048
2049
if !result? then
2050
return
2051
fi
2052
2053
// A narrow kind's arithmetic runs at 32 bits, so its result is
2054
// wrapped back to the kind's width as CIL's narrow stores do.
2055
if let scalar = _scalar_of(type) /\ LLVM_TYPES.is_narrow(scalar) then
2056
_push(_normalise(result, scalar))
2057
else
2058
_push(result)
2059
fi
2060
si
2061
2062
_is_shift(operation: ArithmeticOperation) -> bool =>
2063
operation == ArithmeticOperation.SHIFT_LEFT \/
2064
operation == ArithmeticOperation.SHIFT_RIGHT \/
2065
operation == ArithmeticOperation.SHIFT_RIGHT_UNSIGNED
2066
2067
_negate(operand: Value, is_floating: bool) -> LLVM.Value? is
2068
if let value = _value_of(operand) then
2069
if is_floating then
2070
return _current.negate(value)
2071
fi
2072
2073
return _current.binary(BinaryOperator.SUB, IntegerFlags.NONE, LLVM_TYPES.zero(value.type), value)
2074
fi
2075
2076
return null
2077
si
2078
2079
_complement(operand: Value, is_floating: bool) -> LLVM.Value? is
2080
if is_floating then
2081
_report("complement of a floating point value")
2082
return null
2083
fi
2084
2085
if let value = _value_of(operand) then
2086
return _current.binary(BinaryOperator.XOR, IntegerFlags.NONE, value, LLVM.Values.integer(value.type, bigint.minus_one))
2087
fi
2088
2089
return null
2090
si
2091
2092
// A shift count is an int whatever the shifted value is, and only its
2093
// low bits count, as CIL's and wasm's shifts use them: LLVM makes a
2094
// count at or past the width poison instead.
2095
_shift(operation: ArithmeticOperation, operand: Value, count: Value, held: LLVM.Type, scalar: Scalar?) -> LLVM.Value? is
2096
let value_found = _value_of(operand)
2097
let count_found = _value_of(count)
2098
2099
if !value_found? \/ !count_found? then
2100
return null
2101
fi
2102
2103
let value mut = value_found
2104
let amount mut = count_found
2105
2106
// An unsigned shift of a narrow signed kind shifts in zeros at the
2107
// kind's own width, so its sign-extended operand is first cut back
2108
// to that width.
2109
if operation == ArithmeticOperation.SHIFT_RIGHT_UNSIGNED then
2110
if scalar =~ Scalar.INT8 then
2111
value = _normalise(value, Scalar.UINT8)
2112
elif scalar =~ Scalar.INT16 then
2113
value = _normalise(value, Scalar.UINT16)
2114
fi
2115
fi
2116
2117
let width = LLVM_TYPES.width(held)
2118
2119
if LLVM_TYPES.width(amount.type) < width then
2120
amount = _current.convert(Conversion.ZERO_EXTEND, amount, held)
2121
elif LLVM_TYPES.width(amount.type) > width then
2122
amount = _current.convert(Conversion.TRUNCATE, amount, held)
2123
fi
2124
2125
amount = _current.binary(BinaryOperator.AND, IntegerFlags.NONE, amount, LLVM.Values.integer(held, bigint(width - 1)))
2126
2127
let operator =
2128
case operation
2129
when ArithmeticOperation.SHIFT_LEFT then BinaryOperator.SHL
2130
when ArithmeticOperation.SHIFT_RIGHT then BinaryOperator.ASHR
2131
else BinaryOperator.LSHR
2132
esac
2133
2134
return _current.binary(operator, IntegerFlags.NONE, value, amount)
2135
si
2136
2137
_binary(operation: ArithmeticOperation, left: Value, right: Value, is_floating: bool, held: LLVM.Type) -> LLVM.Value? is
2138
if !is_floating /\ _is_division(operation) then
2139
return _divide(operation, left, right)
2140
fi
2141
2142
let operator = _operator(operation, is_floating)
2143
2144
if !operator? then
2145
return null
2146
fi
2147
2148
let l = _value_of(left)
2149
let r = _value_of(right)
2150
2151
if !l? \/ !r? then
2152
return null
2153
fi
2154
2155
if is_floating then
2156
return _current.floating(operator, FastMath.NONE, l, r)
2157
fi
2158
2159
return _current.binary(operator, IntegerFlags.NONE, _bool_as_integer(l, held), _bool_as_integer(r, held))
2160
si
2161
2162
// A bool operand of integer arithmetic is 0 or 1 at the width the
2163
// result is computed at, as CIL holds one, so `(a > b) - (a < b)`
2164
// orders. Arithmetic on bools, such as `&`, stays an `i1`.
2165
_bool_as_integer(value: LLVM.Value, held: LLVM.Type) -> LLVM.Value =>
2166
if value.type =~ Types.i1 /\ !(held =~ Types.i1) then
2167
_current.convert(Conversion.ZERO_EXTEND, value, held)
2168
else
2169
value
2170
fi
2171
2172
2173
// Integer division and remainder throw where .NET's do: on a zero
2174
// divisor, and, signed, on the one quotient that does not fit, the
2175
// most negative value divided by -1.
2176
_divide(operation: ArithmeticOperation, left: Value, right: Value) -> LLVM.Value? is
2177
let l = _value_of(left)
2178
let r = _value_of(right)
2179
2180
if !l? \/ !r? then
2181
return null
2182
fi
2183
2184
let is_zero = _current.compare(IntegerPredicate.EQ, r, LLVM.Values.integer(r.type, bigint.zero))
2185
2186
_throw_if(is_zero, Semantic.Lookups.RuntimeException.DIVIDE_BY_ZERO)
2187
2188
let is_signed = operation == ArithmeticOperation.DIVIDE \/ operation == ArithmeticOperation.REMAINDER
2189
2190
if is_signed then
2191
let lowest = -(bigint.one << (LLVM_TYPES.width(l.type) - 1))
2192
let is_lowest = _current.compare(IntegerPredicate.EQ, l, LLVM.Values.integer(l.type, lowest))
2193
let is_minus_one = _current.compare(IntegerPredicate.EQ, r, LLVM.Values.integer(r.type, -bigint.one))
2194
let overflows = _current.binary(BinaryOperator.AND, IntegerFlags.NONE, is_lowest, is_minus_one)
2195
2196
_throw_if(overflows, Semantic.Lookups.RuntimeException.OVERFLOW)
2197
fi
2198
2199
let operator =
2200
case operation
2201
when ArithmeticOperation.DIVIDE then BinaryOperator.SDIV
2202
when ArithmeticOperation.DIVIDE_UNSIGNED then BinaryOperator.UDIV
2203
when ArithmeticOperation.REMAINDER then BinaryOperator.SREM
2204
else BinaryOperator.UREM
2205
esac
2206
2207
return _current.binary(operator, IntegerFlags.NONE, l, r)
2208
si
2209
// Integer division and remainder are lowered by `_divide`.
2210
_operator(operation: ArithmeticOperation, is_floating: bool) -> BinaryOperator? is
2211
let result: BinaryOperator? =
2212
if is_floating then
2213
case operation
2214
when ArithmeticOperation.ADD then BinaryOperator.FADD
2215
when ArithmeticOperation.SUBTRACT then BinaryOperator.FSUB
2216
when ArithmeticOperation.MULTIPLY then BinaryOperator.FMUL
2217
when ArithmeticOperation.DIVIDE then BinaryOperator.FDIV
2218
when ArithmeticOperation.REMAINDER then BinaryOperator.FREM
2219
else null
2220
esac
2221
else
2222
case operation
2223
when ArithmeticOperation.ADD then BinaryOperator.ADD
2224
when ArithmeticOperation.SUBTRACT then BinaryOperator.SUB
2225
when ArithmeticOperation.MULTIPLY then BinaryOperator.MUL
2226
when ArithmeticOperation.AND then BinaryOperator.AND
2227
when ArithmeticOperation.OR then BinaryOperator.OR
2228
when ArithmeticOperation.XOR then BinaryOperator.XOR
2229
else null
2230
esac
2231
fi
2232
2233
if !result? then
2234
if !is_floating /\ _is_division(operation) then
2235
_report("integer division")
2236
else
2237
_report("{operation}")
2238
fi
2239
fi
2240
2241
return result
2242
si
2243
2244
_is_division(operation: ArithmeticOperation) -> bool =>
2245
operation == ArithmeticOperation.DIVIDE \/
2246
operation == ArithmeticOperation.DIVIDE_UNSIGNED \/
2247
operation == ArithmeticOperation.REMAINDER \/
2248
operation == ArithmeticOperation.REMAINDER_UNSIGNED
2249
2250
visit(compare: Values.COMPARE) is
2251
if !_held(compare.left.type)? then
2252
_report("a comparison of {compare.left.type}")
2253
return
2254
fi
2255
2256
let left = _value_of(compare.left)
2257
let right = _value_of(compare.right)
2258
2259
if left? /\ right? then
2260
_push(_compare(compare.comparison, left, right, compare.is_unsigned))
2261
fi
2262
si
2263
2264
// An unsigned comparison orders integers as unsigned, and answers true
2265
// for floating values with unordered operands, as CIL's `.un`
2266
// comparisons do. Inequality of floating values is always unordered,
2267
// since it is the negation of an ordered equality.
2268
_compare(comparison: Comparison, left: LLVM.Value, right: LLVM.Value, is_unsigned: bool) -> LLVM.Value is
2269
if LLVM_TYPES.is_floating(left.type) then
2270
let predicate =
2271
case comparison
2272
when Comparison.EQUAL then if is_unsigned then FloatPredicate.UEQ else FloatPredicate.OEQ fi
2273
when Comparison.NOT_EQUAL then FloatPredicate.UNE
2274
when Comparison.LESS then if is_unsigned then FloatPredicate.ULT else FloatPredicate.OLT fi
2275
when Comparison.LESS_OR_EQUAL then if is_unsigned then FloatPredicate.ULE else FloatPredicate.OLE fi
2276
when Comparison.GREATER then if is_unsigned then FloatPredicate.UGT else FloatPredicate.OGT fi
2277
when Comparison.GREATER_OR_EQUAL then if is_unsigned then FloatPredicate.UGE else FloatPredicate.OGE fi
2278
esac
2279
2280
return _current.compare(predicate, left, right)
2281
fi
2282
2283
let predicate =
2284
case comparison
2285
when Comparison.EQUAL then IntegerPredicate.EQ
2286
when Comparison.NOT_EQUAL then IntegerPredicate.NE
2287
when Comparison.LESS then if is_unsigned then IntegerPredicate.ULT else IntegerPredicate.SLT fi
2288
when Comparison.LESS_OR_EQUAL then if is_unsigned then IntegerPredicate.ULE else IntegerPredicate.SLE fi
2289
when Comparison.GREATER then if is_unsigned then IntegerPredicate.UGT else IntegerPredicate.SGT fi
2290
when Comparison.GREATER_OR_EQUAL then if is_unsigned then IntegerPredicate.UGE else IntegerPredicate.SGE fi
2291
esac
2292
2293
return _current.compare(predicate, left, right)
2294
si
2295
2296
visit(convert: Values.CONVERT) is
2297
if !convert.from? /\ !_scalar(convert.value.type)? then
2298
_report("unboxing a {convert.type}")
2299
return
2300
fi
2301
2302
if let value = _value_of(convert.value) then
2303
// A source narrowed from a reference is still the box its
2304
// value is held in.
2305
if isa LLVM.Type.POINTER(value.type) then
2306
if let unboxed = _unbox_value(value, convert.type) then
2307
_push(unboxed)
2308
fi
2309
2310
return
2311
fi
2312
2313
_push(_convert(value, convert.from ?? Scalar.INT32, convert.to))
2314
fi
2315
si
2316
2317
// The conversion of a value of kind `from` to kind `to`, saturating
2318
// from floating to integer as .NET does.
2319
_convert(value: LLVM.Value, from: Scalar, to: Scalar) -> LLVM.Value is
2320
// A value is true where it is not zero.
2321
if to == Scalar.BOOL /\ from != Scalar.BOOL then
2322
if LLVM_TYPES.is_floating(value.type) then
2323
return _current.compare(FloatPredicate.UNE, value, LLVM_TYPES.zero(value.type))
2324
fi
2325
2326
return _current.compare(IntegerPredicate.NE, value, LLVM_TYPES.zero(value.type))
2327
fi
2328
2329
let source = LLVM_TYPES.value_type(from)
2330
let target = LLVM_TYPES.value_type(to)
2331
let source_unsigned = LLVM_TYPES.is_unsigned(from)
2332
let target_unsigned = LLVM_TYPES.is_unsigned(to)
2333
2334
let changed mut = value
2335
2336
if LLVM_TYPES.is_floating(source) then
2337
if LLVM_TYPES.is_floating(target) then
2338
if source =~ LLVM.Type.DOUBLE /\ target =~ LLVM.Type.FLOAT then
2339
changed = _current.convert(Conversion.FLOAT_TRUNCATE, value, target)
2340
elif source =~ LLVM.Type.FLOAT /\ target =~ LLVM.Type.DOUBLE then
2341
changed = _current.convert(Conversion.FLOAT_EXTEND, value, target)
2342
fi
2343
else
2344
// .NET converts to a type narrower than 32 bits through int,
2345
// saturating there and then truncating, whatever the
2346
// narrower type's sign.
2347
let intrinsic =
2348
if target_unsigned /\ !LLVM_TYPES.is_narrow(to) then
2349
Intrinsics.float_to_unsigned_saturating(target, source)
2350
else
2351
Intrinsics.float_to_signed_saturating(target, source)
2352
fi
2353
2354
changed = _current.call(_module.declare_intrinsic(intrinsic), [value])!
2355
fi
2356
elif LLVM_TYPES.is_floating(target) then
2357
changed =
2358
_current.convert(
2359
if source_unsigned then Conversion.UNSIGNED_TO_FLOAT else Conversion.SIGNED_TO_FLOAT fi,
2360
value,
2361
target)
2362
else
2363
let source_width = LLVM_TYPES.width(source)
2364
let target_width = LLVM_TYPES.width(target)
2365
2366
if source_width > target_width then
2367
changed = _current.convert(Conversion.TRUNCATE, value, target)
2368
elif source_width < target_width then
2369
changed =
2370
_current.convert(
2371
if source_unsigned then Conversion.ZERO_EXTEND else Conversion.SIGN_EXTEND fi,
2372
value,
2373
target)
2374
fi
2375
fi
2376
2377
if LLVM_TYPES.is_narrow(to) then
2378
return _normalise(changed, to)
2379
fi
2380
2381
return changed
2382
si
2383
2384
visit(node: Values.Breakable) is
2385
assert node.is_closed else "structured node generated before it was closed"
2386
2387
if let region: Values.REGION = node then
2388
_lower_region(region, null)
2389
elif let loop: Values.LOOP = node then
2390
_lower_loop(loop)
2391
elif let `try: Values.TRY = node then
2392
if let `finally = `try.finally_region then
2393
if `try.catches.count > 0 then
2394
_report("a try with both catches and a finally")
2395
else
2396
_lower_try_finally(`try, `finally)
2397
fi
2398
elif `try.catches.count > 0 then
2399
_lower_try_catch(`try)
2400
else
2401
_report("a try with no handler")
2402
fi
2403
elif let resumable: Values.RESUMABLE = node then
2404
_lower_resumable(resumable)
2405
else
2406
_report("a generator or asynchronous function")
2407
fi
2408
si
2409
2410
// A region continues at a block of its own. One that produces a value
2411
// has each arm store the value into an `alloca` before it leaves,
2412
// and the value is read back where the region continues.
2413
_lower_region(region: Values.REGION, result_type: LLVM.Type?) is
2414
let end = _new_block()
2415
let result = if let type = result_type then _slot(type) else null fi
2416
let depth = _stack.count
2417
2418
_labels.add((region, false, end, result, depth))
2419
2420
let values = region.body.values
2421
2422
// A region that ends in another produces its value through that
2423
// one: a block expression's statements end with the value they
2424
// leave, and that region falls through holding it.
2425
let last_region =
2426
if result_type? /\ values.count > 0 then
2427
cast Values.REGION?(values[values.count - 1])
2428
else
2429
null
2430
fi
2431
2432
if let last = last_region then
2433
for i in 0..values.count - 1 do
2434
gen(values[i])
2435
od
2436
2437
_lower_region(last, result_type)
2438
else
2439
for value in values do
2440
gen(value)
2441
od
2442
fi
2443
2444
if !_is_terminated then
2445
_leave_with_result(result, depth)
2446
_branch(end)
2447
fi
2448
2449
_labels.remove_at(_labels.count - 1)
2450
2451
_start(end)
2452
2453
_check_depth(depth, "region")
2454
2455
if let pointer = result, type = result_type then
2456
_push(_current.load(type, pointer, 0))
2457
fi
2458
si
2459
2460
// Stores the value an arm leaves into its region's result.
2461
_leave_with_result(result: LLVM.Value?, depth: int) is
2462
if let pointer = result /\ _stack.count > depth then
2463
_current.store(_pop()!, pointer, 0)
2464
fi
2465
si
2466
2467
// The IR never leaves a value on the stack across a branch, so the
2468
// stack is as deep where a node continues as where it began.
2469
_check_depth(depth: int, construct: string) is
2470
if _stack.count != depth then
2471
_report("a value left on the stack across a {construct}")
2472
2473
while _stack.count > depth do
2474
_pop()
2475
od
2476
fi
2477
si
2478
2479
// A loop has a header, where its body starts and where reaching the
2480
// end of the body and a CONTINUE both go, and a continuation a BREAK
2481
// goes to.
2482
_lower_loop(loop: Values.LOOP) is
2483
if let counted = _guarded_loop_of(loop) then
2484
_lower_loop_twice(loop, counted)
2485
return
2486
fi
2487
2488
let header = _new_block()
2489
let end = _new_block()
2490
let depth = _stack.count
2491
2492
_labels.add((loop, false, end, null, depth))
2493
_labels.add((loop, true, header, null, depth))
2494
2495
_branch(header)
2496
_start(header)
2497
2498
for value in loop.body.values do
2499
gen(value)
2500
od
2501
2502
if !_is_terminated then
2503
_branch(header)
2504
fi
2505
2506
_labels.remove_at(_labels.count - 1)
2507
_labels.remove_at(_labels.count - 1)
2508
2509
_start(end)
2510
2511
_check_depth(depth, "loop")
2512
si
2513
2514
visit(exit: Values.Exit) is _route(exit, false) si
2515
2516
// An exit goes to its target's block, unless it leaves the protected
2517
// body of a try with a finally on the way. Where `is_taken`, the
2518
// exit's condition has already held and any value it carries is
2519
// already where its target reads it: the exit is being taken again
2520
// after a finally.
2521
_route(exit: Values.Exit, is_taken: bool) is
2522
let is_continue = isa Values.CONTINUE(exit)
2523
2524
let i mut = _labels.count - 1
2525
2526
while i >= 0 do
2527
let label = _labels[i]
2528
2529
let finally_exits: (selector: LLVM.Value, exits: IR.Program.EXIT_NUMBERING) mut
2530
2531
if _finally_exits.try_get_value(i, finally_exits ref) then
2532
_leave_through_finally(exit, is_taken, finally_exits.selector, finally_exits.exits, label.target)
2533
return
2534
fi
2535
2536
if label.node == exit.target /\ label.is_loop_head == is_continue then
2537
if is_taken then
2538
_branch(label.target)
2539
else
2540
_exit(exit, label.target, label.result, label.depth)
2541
fi
2542
2543
return
2544
fi
2545
2546
i = i - 1
2547
od
2548
2549
_report("an exit from a construct the lowering does not handle")
2550
si
2551
2552
// The label an exit goes to, outside any try it leaves.
2553
_target_label(exit: Values.Exit) -> (node: Values.Breakable, is_loop_head: bool, target: BLOCK, result: LLVM.Value?, depth: int)? is
2554
let is_continue = isa Values.CONTINUE(exit)
2555
2556
let i mut = _labels.count - 1
2557
2558
while i >= 0 do
2559
let label = _labels[i]
2560
2561
if label.node == exit.target /\ label.is_loop_head == is_continue /\ !_finally_exits.contains_key(i) then
2562
return label
2563
fi
2564
2565
i = i - 1
2566
od
2567
2568
return null
2569
si
2570
2571
_exit(exit: Values.Exit, target: BLOCK, result: LLVM.Value?, depth: int) is
2572
if exit.condition == IR.BRANCH.ALWAYS then
2573
_leave_with_result(result, depth)
2574
_branch(target)
2575
return
2576
fi
2577
2578
// A conditional exit from a region that produces a value leaves
2579
// the value on the stack for the path that carries on.
2580
if let pointer = result /\ _stack.count > depth then
2581
_current.store(_peek()!, pointer, 0)
2582
fi
2583
2584
if let condition = _test(exit.condition, exit.operands) then
2585
let next = _new_block()
2586
2587
_current.branch(condition, target, next)
2588
_start(next)
2589
fi
2590
si
2591
2592
// The `i1` a conditional exit branches on.
2593
_test(condition: IR.BRANCH, operands: Collections.List[Value]) -> LLVM.Value? is
2594
let first = _value_of(operands[0])
2595
2596
if !first? then
2597
return null
2598
fi
2599
2600
if condition == IR.BRANCH.Z \/ condition == IR.BRANCH.NZ then
2601
if LLVM_TYPES.is_floating(first.type) then
2602
_report("a condition on {operands[0].type}")
2603
return null
2604
fi
2605
2606
// A bool is its own condition.
2607
if first.type =~ Types.i1 then
2608
return
2609
if condition == IR.BRANCH.Z then
2610
_current.binary(BinaryOperator.XOR, IntegerFlags.NONE, first, LLVM.Values.boolean(true))
2611
else
2612
first
2613
fi
2614
fi
2615
2616
return _current.compare(
2617
if condition == IR.BRANCH.Z then IntegerPredicate.EQ else IntegerPredicate.NE fi,
2618
first,
2619
LLVM_TYPES.zero(first.type))
2620
fi
2621
2622
if operands.count < 2 then
2623
_report("the condition {condition}")
2624
return null
2625
fi
2626
2627
let second = _value_of(operands[1])
2628
2629
if !second? then
2630
return null
2631
fi
2632
2633
let comparison: (Comparison, bool)? =
2634
case condition
2635
when IR.BRANCH.EQ then (Comparison.EQUAL, false)
2636
when IR.BRANCH.NE then (Comparison.NOT_EQUAL, true)
2637
when IR.BRANCH.GE then (Comparison.GREATER_OR_EQUAL, false)
2638
when IR.BRANCH.GEU then (Comparison.GREATER_OR_EQUAL, true)
2639
when IR.BRANCH.GT then (Comparison.GREATER, false)
2640
when IR.BRANCH.GTU then (Comparison.GREATER, true)
2641
when IR.BRANCH.LE then (Comparison.LESS_OR_EQUAL, false)
2642
when IR.BRANCH.LEU then (Comparison.LESS_OR_EQUAL, true)
2643
when IR.BRANCH.LT then (Comparison.LESS, false)
2644
when IR.BRANCH.LTU then (Comparison.LESS, true)
2645
else null
2646
esac
2647
2648
if let (operation, is_unsigned) = comparison then
2649
return _compare(operation, first, second, is_unsigned)
2650
fi
2651
2652
_report("the condition {condition}")
2653
2654
return null
2655
si
2656
2657
/// A resumed state machine finds its place through one `switch` on its
2658
/// state, which can enter a resume point's block wherever it is,
2659
/// inside a try included: the try's landing pad belongs to its calls,
2660
/// not to how control reached them. Any other state carries on.
2661
visit(dispatch: Values.DISPATCH) is
2662
// An enclosing dispatch reaches a resume point inside a try by
2663
// jumping to the try's own dispatch, at its top.
2664
let top = _resume_block(dispatch.top)
2665
2666
if !_is_terminated then
2667
_current.branch(top)
2668
fi
2669
2670
_start(top)
2671
2672
if let state = _local(dispatch.state_local) then
2673
let carry_on = _new_block()
2674
let cases = LIST[(LLVM.Value, BLOCK)]()
2675
2676
for (value, target) in dispatch.entries do
2677
cases.add((LLVM.Values.integer(state.type, bigint(value)), _resume_block(target)))
2678
od
2679
2680
_current.switch(_current.load(state.type, state.pointer, 0), carry_on, cases.to_array())
2681
_is_terminated = true
2682
2683
_start(carry_on)
2684
else
2685
_report("a dispatch on a state with no local")
2686
fi
2687
si
2688
2689
/// A resume point is a block of its own, which control falls into
2690
/// and a dispatch can jump to.
2691
visit(resume: Values.RESUME_POINT) is
2692
let block = _resume_block(resume.label)
2693
2694
if !_is_terminated then
2695
_current.branch(block)
2696
fi
2697
2698
_start(block)
2699
si
2700
2701
_resume_block(label: IR.LABEL) -> BLOCK is
2702
let found: BLOCK mut
2703
2704
if _resume_blocks.try_get_value(label, found ref) then
2705
return found
2706
fi
2707
2708
let block = _new_block()
2709
2710
_resume_blocks[label] = block
2711
2712
return block
2713
si
2714
2715
// A state machine's resumable body is a region that a suspend inside a
2716
// try leaves, continuing after it where the method returns.
2717
_lower_resumable(resumable: Values.RESUMABLE) is
2718
let end = _new_block()
2719
2720
_labels.add((resumable, false, end, null, _stack.count))
2721
2722
for value in resumable.body.values do
2723
gen(value)
2724
od
2725
2726
if !_is_terminated then
2727
_branch(end)
2728
fi
2729
2730
_labels.remove_at(_labels.count - 1)
2731
2732
_start(end)
2733
si
2734
2735
_frame_field_load(`field: Semantic.Symbols.Field) -> Value =>
2736
Values.Load.INSTANCE_FIELD(Values.Load.REFERENCE_SELF(cast Semantic.Symbols.Classy(`field.owner)), `field)
2737
2738
_frame_field_store(`field: Semantic.Symbols.Field, value: Value) -> Value =>
2739
Values.Store.INSTANCE_FIELD(Values.Load.REFERENCE_SELF(cast Semantic.Symbols.Classy(`field.owner)), `field, value)
2740
2741
visit(call: Values.Call.GLOBAL) is
2742
_call(call.function, call.arguments)
2743
si
2744
2745
/// A static method takes no receiver, so it is called as a global
2746
/// function is.
2747
visit(call: Values.Call.STATIC) is
2748
_ensure_initialised(call.function.owner)
2749
_call(call.function, call.arguments)
2750
si
2751
2752
// A struct method is called with the address of its receiver, so it
2753
// changes the struct where it is held.
2754
visit(call: Values.Call.STRUCT) is
2755
_call_struct(call.from, call.function, call.arguments)
2756
si
2757
2758
_call_struct(from: Value, function: Function, arguments: Collections.List[Value]) is
2759
2760
if function.intrinsic_operation? then
2761
_report("the intrinsic {function.intrinsic_operation} on a value")
2762
return
2763
fi
2764
2765
let callee = _callee(_instance_of(function))
2766
2767
if !callee? then
2768
_report("a call to {function.name}")
2769
return
2770
fi
2771
2772
let receiver = _address_of(from)
2773
2774
if !receiver? then
2775
return
2776
fi
2777
2778
if let values = _arguments_for(function, arguments) then
2779
values.insert(0, receiver)
2780
2781
if let result = _emit_call(callee, values.to_array()) then
2782
_push(result)
2783
fi
2784
fi
2785
si
2786
2787
/// An array's count and indexer lower to the operations they stand
2788
/// for. No other instance method can be called yet.
2789
visit(call: Values.Call.INSTANCE) is
2790
let operation = _array_operation(call.function)
2791
2792
if operation =~ "array.count" then
2793
if let _ = _array_of(call.from.type), array = _value_of(call.from) then
2794
_check_dereference(array)
2795
_push(_arrays!.count(_current, array))
2796
fi
2797
elif operation =~ "array.get" then
2798
if let type = _array_of(call.from.type), array = _value_of(call.from), index = _value_of(call.arguments[0]) then
2799
_check_index(array, index)
2800
2801
_push(_arrays!.load(_current, type, array, index))
2802
fi
2803
else
2804
_call_instance(call)
2805
fi
2806
si
2807
2808
/// A built-in operation lowers to the IR the operation stands for,
2809
/// made once and reused by every later lowering of the same body.
2810
visit(call: Values.Call.INNATE) is
2811
if call.innate_name =~ "object.type_name" then
2812
_type_name(call.arguments[0])
2813
elif call.innate_name =~ "object.get_type" then
2814
if let from = call.from, value = _value_of(from), t = _type_object_of(value) then
2815
_push(t)
2816
fi
2817
elif call.innate_name.starts_with("host.") \/ call.innate_name.starts_with("object.") then
2818
_report("the operation {call.innate_name}")
2819
elif call.innate_name.starts_with("nullable.") then
2820
_lower_nullable(call.innate_name, call.from)
2821
elif call.innate_name.starts_with("string.") then
2822
_call_string(call.innate_name, call.arguments)
2823
elif call.innate_name =~ "bigint.is_small" \/ call.innate_name =~ "bigint.small" then
2824
_lower_bigint_field(call.innate_name, call.arguments[0])
2825
elif call.is_lowered then
2826
gen(call.lowering)
2827
else
2828
gen(call.lower())
2829
fi
2830
si
2831
2832
// The instance a call from this body names: a reference from inside a
2833
// generic body takes its type arguments from the body's own.
2834
_instance_of(function: Function) -> FUNCTION_INSTANCE =>
2835
FUNCTION_INSTANCE(
2836
cast Function?(function.root_specialized_from)!,
2837
TYPE_MAPS.function_map(function, _type_map))
2838
2839
_call(function: Function, arguments: Collections.List[Value]) is
2840
// A constructor intrinsic is an ordinary method, called as one.
2841
if let operation = function.intrinsic_operation /\ !operation.starts_with("constructor.") then
2842
if operation.starts_with("host.") then
2843
_call_host(operation.substring(5), function, arguments)
2844
elif operation =~ "array.new" then
2845
_new_array(_instance_of(function).specialize(cast Function?(function.root_specialized_from)!.return_type), arguments[0])
2846
elif operation =~ "array.set" then
2847
_store_array_element(arguments[0], arguments[1], arguments[2])
2848
elif operation.starts_with("string.") then
2849
_call_string(operation, arguments)
2850
elif operation =~ "type.size_of" then
2851
_push(LLVM.Values.i32(_size_of(function.generic_arguments[0])))
2852
elif operation =~ "bigint.is_small" \/ operation =~ "bigint.small" then
2853
_lower_bigint_field(operation, arguments[0])
2854
elif operation =~ "object.type_name" then
2855
_type_name(arguments[0])
2856
elif operation =~ "double.bits" \/ operation =~ "single.bits" then
2857
// The bits of a floating value, as the integer of its width.
2858
if let value = _value_of(arguments[0]) then
2859
_push(_current.convert(Conversion.BIT_CAST, value, if operation =~ "double.bits" then Types.i64 else Types.i32 fi))
2860
fi
2861
elif operation =~ "object.get_type" then
2862
if let value = _value_of(arguments[0]), t = _type_object_of(value) then
2863
_push(t)
2864
fi
2865
elif operation =~ "object.display" then
2866
_display(arguments[0], arguments[1])
2867
elif operation =~ "object.memberwise_clone" \/ operation =~ "object.identity_hash" then
2868
// A shallow copy of the whole allocation, and a hash of an
2869
// address the collector never moves: both are libghul's.
2870
let clone = operation =~ "object.memberwise_clone"
2871
2872
if let strings = _strings, value = _value_of(arguments[0]) then
2873
let runtime =
2874
if clone then
2875
strings.runtime("$ghul_clone", Types.pointer, [Types.pointer])
2876
else
2877
strings.runtime("$ghul_identity_hash", Types.i32, [Types.pointer])
2878
fi
2879
2880
if let answer = _current.call(runtime, [value]) then
2881
_push(answer)
2882
fi
2883
else
2884
_report("the intrinsic {operation}")
2885
fi
2886
else
2887
_report("the intrinsic {operation}")
2888
fi
2889
2890
return
2891
fi
2892
2893
let callee = _callee(_instance_of(function))
2894
2895
if !callee? then
2896
_report("a call to {function.name}")
2897
return
2898
fi
2899
2900
let values = LIST[LLVM.Value]()
2901
let parameters = function.arguments
2902
2903
for i in 0..arguments.count do
2904
if let value = _value_of(arguments[i]) then
2905
values.add(if i < parameters.count then _normalise_for(value, parameters[i]) else value fi)
2906
else
2907
return
2908
fi
2909
od
2910
2911
if let result = _emit_call(callee, values.to_array()) then
2912
_sources[result] = EXACT_SOURCE(null, _instance_of(function), null)
2913
_push(result)
2914
fi
2915
si
2916
2917
2918
// An interpolation's builder operations, as INTERPOLATION_CALLS gives
2919
// them. Answers false for any other operation.
2920
_lower_builder(runtime: Values.RUNTIME) -> bool is
2921
let operands = runtime.operands
2922
let calls = INTERPOLATION_CALLS(construct => _report(construct))
2923
2924
case runtime.operation
2925
when IR.RuntimeOperation.NEW_STRING_BUILDER then
2926
_gen_call(calls.new_builder(runtime.type, operands), false)
2927
2928
when IR.RuntimeOperation.APPEND_TEXT then
2929
_gen_call(calls.append(operands[0], operands[1]), true)
2930
2931
when IR.RuntimeOperation.APPEND_VALUE then
2932
_gen_call(calls.append(operands[0], calls.text_of(operands[1], _specialize(operands[1].type))), true)
2933
2934
when IR.RuntimeOperation.BUILD_STRING then
2935
_gen_call(calls.build(operands[0]), false)
2936
2937
when IR.RuntimeOperation.APPEND_ALIGNED_VALUE then
2938
_gen_call(calls.append_aligned(operands[0], calls.text_of(operands[1], _specialize(operands[1].type)), operands[2]), true)
2939
2940
when IR.RuntimeOperation.APPEND_FORMATTED_VALUE then
2941
_gen_call(calls.append(operands[0], calls.formatted_text_of(operands[1], operands[2], _specialize(operands[1].type))), true)
2942
2943
when IR.RuntimeOperation.APPEND_ALIGNED_FORMATTED_VALUE then
2944
_gen_call(calls.append_aligned(operands[0], calls.formatted_text_of(operands[1], operands[3], _specialize(operands[1].type)), operands[2]), true)
2945
2946
else
2947
return false
2948
esac
2949
2950
return true
2951
si
2952
2953
// Lowers `call`, dropping any result it leaves where `discard` says
2954
// to.
2955
_gen_call(call: Value?, discard: bool) is
2956
if let c = call then
2957
let depth = _stack.count
2958
2959
gen(c)
2960
2961
if discard /\ _stack.count > depth then
2962
_pop()
2963
fi
2964
fi
2965
si
2966
// The core library's string intrinsics. A string's length is where an
2967
// array's count is, so an index into one is checked as an array
2968
// index is.
2969
_call_string(operation: string, arguments: Collections.List[Value]) is
2970
let strings = _strings
2971
2972
if !strings? then
2973
_report("the intrinsic {operation}")
2974
return
2975
fi
2976
2977
if operation =~ "string.length" then
2978
if let text = _value_of(arguments[0]) then
2979
_check_dereference(text)
2980
_push(strings.length(_current, text))
2981
fi
2982
elif operation =~ "string.char_at" then
2983
let text = _value_of(arguments[0])
2984
let index = _value_of(arguments[1])
2985
2986
if text? /\ index? then
2987
_check_index(text, index)
2988
_push(strings.char_at(_current, text, index))
2989
fi
2990
elif operation =~ "string.concat" then
2991
_call_string_helper(strings.concat_function, arguments)
2992
elif operation =~ "string.equals" then
2993
_call_string_helper(strings.equals_function, arguments)
2994
elif operation =~ "string.substring" then
2995
_call_string_helper(strings.substring_function, arguments)
2996
elif operation =~ "string.from_chars" then
2997
// An array of `char` has a string's layout, its count where a
2998
// string's length is and its code units where a string's are,
2999
// so the substring helper copies a range of one as it is.
3000
_call_string_helper(strings.substring_function, arguments)
3001
elif operation =~ "string.copy_to" then
3002
_call_string_helper(strings.copy_to_function, arguments)
3003
elif operation =~ "string.from_char" then
3004
_call_string_helper(strings.from_char_function, arguments)
3005
else
3006
_report("the intrinsic {operation}")
3007
fi
3008
si
3009
3010
// Calls one of the string helpers the module defines, with `arguments`
3011
// as they are held. A helper answering an `i1` answers a `bool`.
3012
_call_string_helper(helper: LLVM.FUNCTION?, arguments: Collections.List[Value]) is
3013
if !helper? then
3014
_report("a string operation")
3015
return
3016
fi
3017
3018
let values = LIST[LLVM.Value]()
3019
3020
for argument in arguments do
3021
if let value = _value_of(argument) then
3022
values.add(value)
3023
else
3024
return
3025
fi
3026
od
3027
3028
if let result = _current.call(helper, values.to_array()) then
3029
_push(result)
3030
fi
3031
si
3032
3033
3034
// A new bigint box holding `small` in its i64 field and `big`, the
3035
// runtime's integer or null, in the other. It holds a pointer, so it
3036
// is one the collector scans.
3037
_new_bigint(objects: LLVM_OBJECTS, type: CLASS_INSTANCE, small: LLVM.Value, big: LLVM.Value) -> LLVM.Value is
3038
objects.note_allocated(type)
3039
3040
let box =
3041
_strings!.allocate_object(_current, LLVM.Values.global(objects.descriptor_name(type)), LLVM.Values.i64(cast long(objects.size_of(type))), true)
3042
3043
let layout = objects.struct_type(type)
3044
3045
_current.store(small, _current.element_pointer(layout, box, [_i32(0), _i32(LLVM_OBJECTS.BIGINT_SMALL)], true), 0)
3046
_current.store(big, _current.element_pointer(layout, box, [_i32(0), _i32(LLVM_OBJECTS.BIGINT_BIG)], true), 0)
3047
3048
return box
3049
si
3050
3051
3052
// The name of the type of the object `value` is, which every
3053
// descriptor holds as a string.
3054
_type_name(value: Value) is
3055
if let object = _value_of(value) then
3056
_check_dereference(object)
3057
3058
let descriptor = _strings!.load_descriptor(_current, object)
3059
3060
_push(_current.load(Types.pointer, _current.element_pointer(Types.i8, descriptor, [_i32(LLVM_OBJECTS.DESCRIPTOR_NAME)], true), 8))
3061
fi
3062
si
3063
// A bigint whose value its i64 field holds: `value`, a long or an int.
3064
_bigint_of_small(type: Type?, value: Value) is
3065
let objects = _objects
3066
let b = if let o = objects then o.bigint_of(_specialize(type)) else null fi
3067
3068
if !objects? \/ !b? then
3069
_report("a bigint made from a {value.type}")
3070
return
3071
fi
3072
3073
if let held = _value_of(value) then
3074
let small = if LLVM_TYPES.width(held.type) < 64 then _current.convert(Conversion.SIGN_EXTEND, held, Types.i64) else held fi
3075
3076
_push(_new_bigint(objects, b, small, LLVM.Values.null_pointer))
3077
fi
3078
si
3079
3080
// The bigint `box` points at's i64 field, or whether that holds its
3081
// value: where it does, the runtime's integer is null.
3082
_lower_bigint_field(operation: string, value: Value) is
3083
let objects = _objects
3084
let b = if let o = objects then o.bigint_of(_specialize(value.type)) else null fi
3085
3086
if !objects? \/ !b? then
3087
_report("{operation} on a {value.type}")
3088
return
3089
fi
3090
3091
if let box = _value_of(value) then
3092
let layout = objects.struct_type(b)
3093
3094
if operation =~ "bigint.small" then
3095
_push(_current.load(Types.i64, _current.element_pointer(layout, box, [_i32(0), _i32(LLVM_OBJECTS.BIGINT_SMALL)], true), 0))
3096
else
3097
let big = _current.load(Types.pointer, _current.element_pointer(layout, box, [_i32(0), _i32(LLVM_OBJECTS.BIGINT_BIG)], true), 0)
3098
3099
_push(_current.compare(IntegerPredicate.EQ, big, LLVM.Values.null_pointer))
3100
fi
3101
fi
3102
si
3103
3104
// The runtime's integer for the bigint `box` points at: the one it
3105
// holds, or, for a value its i64 field holds, one made from that.
3106
_bigint_to_host(box: LLVM.Value) -> LLVM.Value is
3107
let layout = LLVM.Types.structure([Types.pointer, Types.i64, Types.pointer])
3108
3109
let slot = _slot(Types.pointer)
3110
let big = _current.load(Types.pointer, _current.element_pointer(layout, box, [_i32(0), _i32(LLVM_OBJECTS.BIGINT_BIG)], true), 0)
3111
3112
_current.store(big, slot, 0)
3113
3114
let from_small = _new_block()
3115
let join = _new_block()
3116
3117
_current.branch(_current.compare(IntegerPredicate.EQ, big, LLVM.Values.null_pointer), from_small, join)
3118
3119
_start(from_small)
3120
3121
let small = _current.load(Types.i64, _current.element_pointer(layout, box, [_i32(0), _i32(LLVM_OBJECTS.BIGINT_SMALL)], true), 0)
3122
let made = _current.call(_strings!.runtime(LLVM_STRINGS.host_name("bigint_from_long"), Types.pointer, [Types.i64]), [small])!
3123
3124
_current.store(made, slot, 0)
3125
_current.branch(join)
3126
3127
_start(join)
3128
3129
return _current.load(Types.pointer, slot, 0)
3130
si
3131
3132
// Wraps the runtime's integer `big` in a new bigint box, which the
3133
// core library's `_canonical` moves into the i64 field where it fits,
3134
// as every other bigint operation looks there first.
3135
_bigint_from_host(b: CLASS_INSTANCE, type: Type, big: LLVM.Value) is
3136
let canonical = _static_method(type, "_canonical", 1)
3137
3138
if !canonical? then
3139
_report("a bigint the runtime answers with, with no bigint._canonical")
3140
return
3141
fi
3142
3143
let box = _new_bigint(_objects!, b, LLVM.Values.i64(0L), big)
3144
3145
if let callee = _callee(_instance_of(canonical)) then
3146
if let result = _emit_call(callee, [box]) then
3147
_push(result)
3148
fi
3149
else
3150
_report("a call to {canonical.name}")
3151
fi
3152
si
3153
3154
// The static method of `type` named `name` that takes `arity`
3155
// arguments.
3156
_static_method(type: Type?, name: string, arity: int) -> Function? is
3157
let found = type?.find_member(name)
3158
3159
if let group: Semantic.Symbols.FUNCTION_GROUP = found then
3160
for function in group.functions do
3161
if !function.is_instance /\ function.arguments.count == arity then
3162
return function
3163
fi
3164
od
3165
elif let function: Function = found /\ !function.is_instance /\ function.arguments.count == arity then
3166
return function
3167
fi
3168
3169
return null
3170
si
3171
// A host operation is a function the native runtime defines, named
3172
// for the operation. A string crosses as the address of its code
3173
// units and their count, and comes back as a string the runtime made
3174
// through `ghul_make_string`.
3175
_call_host(operation: string, function: Function, arguments: Collections.List[Value]) is
3176
_call_host(operation, function, arguments, function.return_type)
3177
si
3178
3179
// A bigint crosses as the runtime's integer, made from its i64 field
3180
// where that holds the value, and comes back wrapped in a new box.
3181
_call_host(operation: string, function: Function, arguments: Collections.List[Value], result: Type?) is
3182
let strings = _strings
3183
3184
if !strings? then
3185
_report("the host operation {operation}")
3186
return
3187
fi
3188
3189
let parameter_types = LIST[LLVM.Type]()
3190
let parameter_attributes = LIST[LLVM.Attribute?]()
3191
let values = LIST[LLVM.Value]()
3192
let formals = function.arguments
3193
3194
for i in 0..arguments.count do
3195
let formal = if i < formals.count then formals[i] else arguments[i].type fi
3196
3197
let value = _value_of(arguments[i])
3198
3199
if !value? then
3200
return
3201
fi
3202
3203
if let scalar = _scalar(formal) then
3204
parameter_types.add(_c_type(scalar))
3205
parameter_attributes.add(_c_extension(scalar))
3206
values.add(_to_c(value, scalar))
3207
elif _is_string(_specialize(formal)) then
3208
// A null string reaches the host as the empty string, as
3209
// .NET writes nothing for one.
3210
let text = _current.select(_current.compare(IntegerPredicate.EQ, value, LLVM.Values.null_pointer), strings.literal(""), value)
3211
3212
parameter_types.add(Types.pointer)
3213
parameter_types.add(Types.i32)
3214
parameter_attributes.add(null)
3215
parameter_attributes.add(null)
3216
values.add(strings.chars(_current, text))
3217
values.add(strings.length(_current, text))
3218
elif _objects?.bigint_of(_specialize(formal))? then
3219
parameter_types.add(Types.pointer)
3220
parameter_attributes.add(null)
3221
values.add(_bigint_to_host(value))
3222
elif let arrays = _arrays, array = arrays.array_of(_specialize(formal)) /\ LLVM_TYPES.scalar(arrays.element_of(array)) =~ Scalar.UINT8 then
3223
// A ubyte[] crosses as C takes bytes: the address of its
3224
// first element and how many there are.
3225
parameter_types.add(Types.pointer)
3226
parameter_types.add(Types.i32)
3227
parameter_attributes.add(null)
3228
parameter_attributes.add(null)
3229
values.add(arrays.element_address(_current, array, value, _i32(0)))
3230
values.add(arrays.count(_current, value))
3231
else
3232
_report("a host operation taking {formal}")
3233
return
3234
fi
3235
od
3236
3237
let result_scalar = if LLVM_TYPES.is_void(result) then null else _scalar(result) fi
3238
3239
let result_type =
3240
if LLVM_TYPES.is_void(result) then
3241
LLVM.Type.VOID
3242
elif let scalar = result_scalar then
3243
_c_type(scalar)
3244
else
3245
_held(result)
3246
fi
3247
3248
if !result_type? then
3249
_report("a host operation answering {result}")
3250
return
3251
fi
3252
3253
let host = strings.runtime(LLVM_STRINGS.host_name(operation), result_type, parameter_types.to_array())
3254
3255
// A small scalar crosses as the C prototype declares it, and is
3256
// extended to a register by the side that passes it, as clang
3257
// declares one.
3258
if host.result_attributes.count == 0 then
3259
if let scalar = result_scalar, extension = _c_extension(scalar) then
3260
host.result_attributes.add(extension)
3261
fi
3262
3263
for (i, attribute) in parameter_attributes |> Ghul.Pipes.index() do
3264
if let a = attribute /\ host.parameters[i].attributes.count == 0 then
3265
host.parameters[i].attributes.add(a)
3266
fi
3267
od
3268
fi
3269
3270
if let answer = _current.call(host, values.to_array()) then
3271
if let b = _objects?.bigint_of(_specialize(result)) then
3272
_bigint_from_host(b, _specialize(result)!, answer)
3273
elif let scalar = result_scalar then
3274
_push(_from_c(answer, scalar))
3275
else
3276
_push(answer)
3277
fi
3278
fi
3279
si
3280
3281
// The type a value of the kind has in a C prototype: a bool as an
3282
// `i1`, a small integer at its width, anything else as it is held.
3283
_c_type(scalar: Scalar) -> LLVM.Type =>
3284
if scalar == Scalar.BOOL then Types.i1 else LLVM_TYPES.stored_type(scalar) fi
3285
3286
// How C extends a value of the kind to a register: `zeroext` for a
3287
// bool or an unsigned small kind, `signext` for a signed one.
3288
_c_extension(scalar: Scalar) -> LLVM.Attribute? =>
3289
if scalar != Scalar.BOOL /\ !LLVM_TYPES.is_narrow(scalar) then
3290
null
3291
elif LLVM_TYPES.is_unsigned(scalar) then
3292
LLVM.Attribute.FLAG("zeroext")
3293
else
3294
LLVM.Attribute.FLAG("signext")
3295
fi
3296
3297
// `value`, as the kind is computed with, as C passes it: a bool is
3298
// the `i1` it already is.
3299
_to_c(value: LLVM.Value, scalar: Scalar) -> LLVM.Value =>
3300
if scalar == Scalar.BOOL then value else LLVM_TYPES.narrow(_current, value, scalar) fi
3301
3302
// `value`, as C passes it, as the kind is computed with.
3303
_from_c(value: LLVM.Value, scalar: Scalar) -> LLVM.Value =>
3304
if scalar == Scalar.BOOL then value else LLVM_TYPES.widen(_current, value, scalar) fi
3305
3306
visit(store: Values.Store.SYMBOL) is _report("a store to {store.symbol.name}") si
3307
3308
visit(store: Values.Store.LOCAL_VARIABLE) is
3309
if let state_machine_field = Values.state_machine_field_for(store.symbol) then
3310
gen(_frame_field_store(state_machine_field, store.value))
3311
elif let variable = cast Semantic.Symbols.Variable?(store.symbol) then
3312
_store_local(variable.ir_local, _value_of(store.value))
3313
else
3314
_report("a store to {store.symbol.name}")
3315
fi
3316
si
3317
3318
visit(store: Values.Store.LOCAL_ARGUMENT) is
3319
let symbol = store.symbol
3320
3321
if let state_machine_field = Values.state_machine_field_for(symbol) then
3322
gen(_frame_field_store(state_machine_field, store.value))
3323
elif let packed_local = Values.packed_parameter_local_for(symbol) then
3324
_store_local(packed_local, _value_of(store.value))
3325
elif let index = _argument_index(symbol) then
3326
_store_argument(index, _value_of(store.value), symbol.type)
3327
else
3328
_report("a store to an argument of another function")
3329
fi
3330
si
3331
3332
_load_receiver() is
3333
if _receiver_offset == 0 then
3334
_report("self outside an instance method")
3335
else
3336
_load_argument(0)
3337
fi
3338
si
3339
3340
// The class a member's owner is, at the arguments this body gives
3341
// it.
3342
_owner_class(owner: Semantic.Scope?) -> CLASS_INSTANCE? is
3343
if let objects = _objects then
3344
if let generic: Semantic.Symbols.GENERIC = owner then
3345
return objects.class_of(_specialize(generic.type))
3346
elif let classy: Semantic.Symbols.Classy = owner then
3347
return objects.class_of(classy, _type_map)
3348
fi
3349
fi
3350
3351
return null
3352
si
3353
3354
// The instance of the trait a member's owner is, at the arguments
3355
// this body gives it.
3356
_trait_instance(owner: Semantic.Scope?) -> CLASS_INSTANCE? is
3357
if let objects = _objects then
3358
if let generic: Semantic.Symbols.GENERIC = owner then
3359
return objects.trait_of(_specialize(generic.type)!)
3360
elif let classy: Semantic.Symbols.Classy = owner then
3361
return objects.trait_of(classy, _type_map)
3362
fi
3363
fi
3364
3365
return null
3366
si
3367
3368
// A type's static constructor runs once, before the type is first
3369
// used: before a static member is read, written or called, and before
3370
// an instance is made. Its flag is set before the body runs, so a use
3371
// of the type from inside it does not run it again.
3372
_ensure_initialised(owner: Semantic.Scope?) is
3373
if let objects = _objects, type = _owner_class(owner), (constructor, flag) = objects.static_constructor(type) then
3374
let callee = _callee(constructor)
3375
3376
if !callee? then
3377
_report("a static constructor of {type}")
3378
return
3379
fi
3380
3381
let run = _new_block()
3382
let carry_on = _new_block()
3383
let state = _current.load(Types.i32, LLVM.Values.global(flag), 4)
3384
3385
_current.branch(_current.compare(IntegerPredicate.EQ, state, _i32(0)), run, carry_on)
3386
3387
_start(run)
3388
3389
_current.store(_i32(1), LLVM.Values.global(flag), 4)
3390
_emit_call(callee, [])
3391
_current.branch(carry_on)
3392
3393
_start(carry_on)
3394
fi
3395
si
3396
3397
// The type object `descriptor` stands for, a constant beside it, so
3398
// a type has one type object however it is asked for. Null, reported,
3399
// where the core library declares no type object.
3400
_type_object(descriptor: LLVM.Value) -> LLVM.Value? is
3401
if !_type_object_constructor()? then
3402
_report("a type object")
3403
return null
3404
fi
3405
3406
return _current.load(Types.pointer, _current.element_pointer(Types.i8, descriptor, [_i32(LLVM_OBJECTS.DESCRIPTOR_TYPE_OBJECT)], true), 8)
3407
si
3408
3409
_type_object_constructor() -> (CLASS_INSTANCE, Function)? is
3410
if let constructor = IR.Program.CORE_LIBRARY.constructor(IR.Program.CORE_LIBRARY.type_class(), 1) then
3411
if let type = _owner_class(constructor.owner), objects = _objects /\ objects.is_object_type(type) then
3412
return (type, constructor)
3413
fi
3414
fi
3415
3416
return null
3417
si
3418
3419
// The type object of the object `value` points to, which throws where
3420
// `value` is null.
3421
_type_object_of(value: LLVM.Value) -> LLVM.Value? is
3422
_check_dereference(value)
3423
3424
return _type_object(_current.load(Types.pointer, value, 8))
3425
si
3426
3427
// Whether a call to `function` asks for its receiver's type object,
3428
// which no type can override: a member declaring the same name
3429
// hides it.
3430
_is_get_type(function: Function) -> bool =>
3431
cast Function?(function.root_specialized_from)?.intrinsic_operation =~ "object.get_type"
3432
3433
// The type object of `call`'s receiver: the one its descriptor stands
3434
// for, or, for a value held in place, the one its box's descriptor does.
3435
_get_type(call: Values.Call.INSTANCE) is
3436
let receiver: Value mut = call.from
3437
3438
if let constrained: Values.CONSTRAINED = receiver then
3439
receiver = if let address: Values.ADDRESS = constrained.value then address.value else constrained.value fi
3440
3441
let type = _specialize(constrained.type)
3442
3443
if !_held(type)? \/ !isa LLVM.Type.POINTER(_held(type)!) then
3444
_value_of(receiver)
3445
3446
if let box = _objects!.box_type(type) ?? _objects!.struct_of(type) then
3447
if let t = _type_object(LLVM.Values.global(_objects!.descriptor_name(box))) then
3448
_push(t)
3449
fi
3450
else
3451
_report("the type of a {type}")
3452
fi
3453
3454
return
3455
fi
3456
fi
3457
3458
if let value = _value_of(receiver), t = _type_object_of(value) then
3459
_push(t)
3460
fi
3461
si
3462
3463
// Writes `value` for display through `state`, a core library display
3464
// state, with the function its descriptor holds: true where it has
3465
// one, and false, leaving `state` as it was, where it has none.
3466
_display(value: Value, state: Value) is
3467
let objects = _objects!
3468
3469
if let state_class = objects.class_of(_specialize(state.type)) then
3470
objects.note_display(state_class)
3471
fi
3472
3473
if let v = _value_of(value), s = _value_of(state) then
3474
let descriptor = _strings!.load_descriptor(_current, v)
3475
let displayer = _current.load(Types.pointer, _current.element_pointer(Types.i8, descriptor, [_i32(LLVM_OBJECTS.DESCRIPTOR_DISPLAY)], true), 8)
3476
let result = _slot(Types.i1)
3477
3478
_current.store(LLVM.Values.boolean(false), result, 1)
3479
3480
let call = _new_block()
3481
let carry_on = _new_block()
3482
3483
_current.branch(_current.compare(IntegerPredicate.NE, displayer, LLVM.Values.null_pointer), call, carry_on)
3484
3485
_start(call)
3486
3487
_current.call(LLVM.Type.FUNCTION(LLVM.Type.VOID, [Types.pointer, Types.pointer], false), displayer, [v, s])
3488
_current.store(LLVM.Values.boolean(true), result, 1)
3489
_current.branch(carry_on)
3490
3491
_start(carry_on)
3492
3493
_push(_current.load(Types.i1, result, 1))
3494
fi
3495
si
3496
3497
// Throws a new instance of the core library's exception class for
3498
// `kind`, such as an invalid cast, where `condition`, an `i1`, holds.
3499
// Where the program has no such class, what is thrown carries no type
3500
// or message.
3501
_throw_if(condition: LLVM.Value, kind: Semantic.Lookups.RuntimeException) is
3502
let thrown = _new_block()
3503
let carry_on = _new_block()
3504
3505
_current.branch(condition, thrown, carry_on)
3506
3507
_start(thrown)
3508
3509
_throw_new(kind)
3510
3511
_start(carry_on)
3512
si
3513
3514
// The exception a runtime operation makes, or null where it makes
3515
// none.
3516
_runtime_exception(operation: IR.RuntimeOperation) -> Semantic.Lookups.RuntimeException? =>
3517
case operation
3518
when IR.RuntimeOperation.NEW_NOT_IMPLEMENTED then Semantic.Lookups.RuntimeException.NOT_IMPLEMENTED
3519
when IR.RuntimeOperation.NEW_NOT_SUPPORTED then Semantic.Lookups.RuntimeException.NOT_SUPPORTED
3520
when IR.RuntimeOperation.NEW_ASSERT_FAILED then Semantic.Lookups.RuntimeException.ASSERT_FAILED
3521
else null
3522
esac
3523
3524
// A new instance of the core library's exception class for `kind`,
3525
// made by its constructor taking `arguments`, or null, reported, where
3526
// the program declares no such constructor.
3527
_new_exception(kind: Semantic.Lookups.RuntimeException, arguments: Collections.List[Value]) -> LLVM.Value? is
3528
let name = Semantic.Lookups.RUNTIME_EXCEPTIONS.name_of(kind)
3529
3530
if let constructor = IR.Program.CORE_LIBRARY.constructor(IR.Program.CORE_LIBRARY.exception_class(kind), arguments.count) then
3531
return _construct(constructor, arguments, name)
3532
fi
3533
3534
_report("a {name} constructor taking {arguments.count} arguments")
3535
3536
return null
3537
si
3538
3539
// Declares the constructor taking nothing of the core library's
3540
// exception class for `kind`, so a later lowering can throw one,
3541
// where there is one.
3542
_reach_exception(kind: Semantic.Lookups.RuntimeException) is
3543
if let constructor = IR.Program.CORE_LIBRARY.constructor(IR.Program.CORE_LIBRARY.exception_class(kind), 0) then
3544
_owner_class(constructor.owner)
3545
_callee(_instance_of(constructor))
3546
fi
3547
si
3548
3549
// Throws a new instance of the core library's exception class for
3550
// `kind`, made by its constructor taking nothing.
3551
_throw_new(kind: Semantic.Lookups.RuntimeException) is
3552
if let constructor = IR.Program.CORE_LIBRARY.constructor(IR.Program.CORE_LIBRARY.exception_class(kind), 0) then
3553
if let exception = _construct(constructor, LIST[Value](), Semantic.Lookups.RUNTIME_EXCEPTIONS.name_of(kind)) then
3554
_throw(exception)
3555
return
3556
fi
3557
fi
3558
3559
_throw(LLVM.Values.null_pointer)
3560
si
3561
3562
// Throws `exception` through the runtime, which unwinds to this
3563
// function's landing pad where the throw is inside a try.
3564
_throw(exception: LLVM.Value) is
3565
_emit_call(_strings!.runtime("$ghul_throw", LLVM.Type.VOID, [Types.pointer]), [exception])
3566
_current.unreachable()
3567
_is_terminated = true
3568
si
3569
3570
// Calls `function`: through `invoke` inside a try, so a throw reaches
3571
// the try's landing pad, and as a plain call outside every try.
3572
_emit_call(function: FUNCTION, arguments: LLVM.Value[]) -> LLVM.Value? =>
3573
_emit_call(function.type, function.reference, arguments)
3574
3575
_emit_call(function_type: LLVM.Type, callee: LLVM.Value, arguments: LLVM.Value[]) -> LLVM.Value? is
3576
if let unwind = _unwind then
3577
let normal = _new_block()
3578
let result = _current.invoke(function_type, callee, arguments, normal, unwind)
3579
3580
_start(normal)
3581
3582
return result
3583
fi
3584
3585
return _current.call(function_type, callee, arguments)
3586
si
3587
3588
// Throws the null-reference exception where `object`, a reference
3589
// about to be read through, is null. The test is marked
3590
// !make.implicit, so where the read follows it LLVM folds the two:
3591
// the read is left to fault, and the runtime resumes at the throw.
3592
_check_dereference(object: LLVM.Value) is
3593
let thrown = _new_block()
3594
let carry_on = _new_block()
3595
3596
_current
3597
.branch(_current.compare(IntegerPredicate.EQ, object, LLVM.Values.null_pointer), thrown, carry_on)
3598
.attach("make.implicit", _module.empty_metadata())
3599
3600
_start(thrown)
3601
3602
_throw_null_reference()
3603
3604
_start(carry_on)
3605
si
3606
3607
// Throws the null-reference exception through the runtime, which makes
3608
// it with the program's $ghul_make_null_reference: one call, so a test
3609
// for null adds little to the function it is in. Where the program
3610
// has no such exception, what is thrown carries no type or message.
3611
_throw_null_reference() is
3612
if !LLVM_EXCEPTIONS.null_reference_constructor? then
3613
_throw_new(Semantic.Lookups.RuntimeException.NULL_REFERENCE)
3614
return
3615
fi
3616
3617
_reach_exception(Semantic.Lookups.RuntimeException.NULL_REFERENCE)
3618
3619
_emit_call(_strings!.throw_null_reference, [])
3620
_current.unreachable()
3621
_is_terminated = true
3622
si
3623
3624
// The address of the instance field `symbol` of the object `from`
3625
// answers, tested for null first, or null, reported, where the target
3626
// does not hold it.
3627
_field_address(from: Value?, symbol: Semantic.Symbols.Symbol) -> LLVM.Value? is
3628
if let (object, address) = _field_access(from, symbol) then
3629
_check_receiver(from, object)
3630
return address
3631
fi
3632
3633
return null
3634
si
3635
3636
// The address of the instance field `symbol` and the object it is in,
3637
// which is null for a field of a struct, untested. The caller tests
3638
// the object before it reads or writes the field.
3639
_field_access(from: Value?, symbol: Semantic.Symbols.Symbol) -> (object: LLVM.Value?, address: LLVM.Value)? is
3640
let objects = _objects
3641
3642
if !objects? \/ !from? then
3643
_report("a field")
3644
return null
3645
fi
3646
3647
let owner = _owner_class(symbol.owner)
3648
let position = objects.field_position(symbol)
3649
3650
if !owner? \/ !position? then
3651
_report("a field of {symbol.owner}")
3652
return null
3653
fi
3654
3655
// A struct's field is within the struct, wherever that is held.
3656
if from.is_value_type then
3657
if !objects.is_struct(owner) then
3658
_report("a field of {symbol.owner}")
3659
return null
3660
fi
3661
3662
if let address = _address_of(from) then
3663
let within: LLVM.Value? = null
3664
3665
return (object = within, address = _current.element_pointer(objects.struct_type(owner), address, [_i32(0), _i32(position)], true))
3666
fi
3667
3668
return null
3669
fi
3670
3671
if let object = _value_of(from) then
3672
return (object = object, address = _current.element_pointer(objects.struct_type(owner), object, [_i32(0), _i32(position)], true))
3673
fi
3674
3675
return null
3676
si
3677
3678
// Tests `object`, which `from` lowered to, for null before it is read
3679
// through, unless `from` is the receiver, which its caller tested, or
3680
// `object` is null because `from` is a struct, which is not a
3681
// reference.
3682
_check_receiver(from: Value?, object: LLVM.Value?) is
3683
if let o = object /\ !isa Values.Load.REFERENCE_SELF(from) then
3684
_check_dereference(o)
3685
fi
3686
si
3687
3688
visit(load: Values.Load.INSTANCE_FIELD) is
3689
if !_stored(load.type)? then
3690
_report("a field of type {load.type}")
3691
return
3692
fi
3693
3694
if let address = _field_address(load.from, load.symbol) then
3695
_push(_load_stored(load.type, address)!)
3696
fi
3697
si
3698
3699
visit(store: Values.Store.INSTANCE_FIELD) is
3700
if let (object, address) = _field_access(store.from, store.symbol), value = _value_of(store.value) then
3701
_check_receiver(store.from, object)
3702
_store_stored(value, store.type, address)
3703
fi
3704
si
3705
3706
// The global holding a static field, or a namespace-level variable
3707
// where `owner` is null.
3708
_global(symbol: Semantic.Symbols.Symbol, is_static: bool) -> (name: string, type: LLVM.Type)? is
3709
let objects = _objects
3710
3711
if !objects? then
3712
_report("a static field")
3713
return null
3714
fi
3715
3716
// A static field of a namespace is a namespace-level variable.
3717
let is_class_static = is_static /\ !isa Semantic.Symbols.NAMESPACE(symbol.owner)
3718
let owner = if is_class_static then _owner_class(symbol.owner) else null fi
3719
3720
if is_class_static then
3721
if !owner? then
3722
_report("a static field of {symbol.owner}")
3723
return null
3724
fi
3725
3726
_ensure_initialised(symbol.owner)
3727
fi
3728
3729
let type = _specialize(cast Semantic.Types.Typed?(symbol)?.type)
3730
3731
if let global = objects.global_of(symbol, owner, type) then
3732
return global
3733
fi
3734
3735
_report("a static field of type {type}")
3736
3737
return null
3738
si
3739
3740
_load_global(symbol: Semantic.Symbols.Symbol, is_static: bool) is
3741
if let (name, type) = _global(symbol, is_static) then
3742
_push(_widen(_current.load(type, LLVM.Values.global(name), 0), _specialize(cast Semantic.Types.Typed?(symbol)?.type)))
3743
fi
3744
si
3745
3746
_store_global(symbol: Semantic.Symbols.Symbol, is_static: bool, value: Value) is
3747
if let (name, _) = _global(symbol, is_static), stored = _value_of(value) then
3748
_store_stored(stored, _specialize(cast Semantic.Types.Typed?(symbol)?.type), LLVM.Values.global(name))
3749
fi
3750
si
3751
3752
visit(load: Values.Load.STATIC_FIELD) is _load_global(load.symbol, true) si
3753
visit(load: Values.Load.GLOBAL_FIELD) is _load_global(load.symbol, false) si
3754
visit(store: Values.Store.STATIC_FIELD) is _store_global(store.symbol, true, store.value) si
3755
visit(store: Values.Store.GLOBAL_FIELD) is _store_global(store.symbol, false, store.value) si
3756
3757
visit(load: Values.Load.RECEIVER) is _load_receiver() si
3758
visit(self_load: Values.Load.REFERENCE_SELF) is _load_receiver() si
3759
3760
/// An object is allocated through the runtime, zeroed, with its
3761
/// descriptor in place, and its constructor is called on it.
3762
visit(construction: Values.NEW) is
3763
let operation = construction.constructor.intrinsic_operation
3764
3765
// A bigint is built in its box's i64 field from a long or an int,
3766
// and by the runtime from anything else.
3767
if operation =~ "bigint.of_small" then
3768
_bigint_of_small(construction.type, construction.arguments[0])
3769
return
3770
elif let o = operation /\ o.starts_with("host.") then
3771
_call_host(o.substring(5), construction.constructor, construction.arguments, construction.type)
3772
return
3773
fi
3774
3775
if let object = _construct(construction.constructor, construction.arguments, "{construction.type}") then
3776
_push(object)
3777
fi
3778
si
3779
3780
// A new struct is zeroed in a place of its own and its constructor
3781
// called on that place; the value is what the constructor leaves.
3782
_construct_struct(objects: LLVM_OBJECTS, type: CLASS_INSTANCE, constructor: Function, arguments: Collections.List[Value]) -> LLVM.Value? is
3783
let callee = _callee(_instance_of(constructor))
3784
3785
if !callee? then
3786
_report("a call to {constructor.name}")
3787
return null
3788
fi
3789
3790
let values = _arguments_for(constructor, arguments)
3791
3792
if !values? then
3793
return null
3794
fi
3795
3796
_ensure_initialised(constructor.owner)
3797
3798
let struct_type = objects.struct_type(type)
3799
let slot = _slot(struct_type)
3800
3801
_current.store(LLVM_TYPES.zero(struct_type), slot, 0)
3802
3803
values.insert(0, slot)
3804
3805
_emit_call(callee, values.to_array())
3806
3807
return _current.load(struct_type, slot, 0)
3808
si
3809
3810
// A new instance of `constructor`'s class, built from `arguments`, or
3811
// null, reported, where the target cannot build one. The arguments
3812
// are evaluated before the object is allocated, as on .NET.
3813
_construct(constructor: Function, arguments: Collections.List[Value], description: string) -> LLVM.Value? is
3814
let objects = _objects
3815
let type = _owner_class(constructor.owner)
3816
3817
if let o = objects, t = type /\ o.is_struct(t) /\ !constructor.intrinsic_operation? then
3818
return _construct_struct(o, t, constructor, arguments)
3819
fi
3820
3821
if constructor.intrinsic_operation? \/ !objects? \/ !type? \/ !(objects.is_object_type(type) \/ objects.is_object(type)) then
3822
_report("constructing a {description}")
3823
return null
3824
fi
3825
3826
// A unit variant has one shared instance, which the variant's
3827
// constructor never runs on.
3828
if type.definition.is_unit_variant then
3829
return LLVM.Values.global(objects.unit_variant_name(type))
3830
fi
3831
3832
let callee = if objects.is_object(type) then null else _callee(_instance_of(constructor)) fi
3833
3834
if !objects.is_object(type) /\ !callee? then
3835
_report("a call to {constructor.name}")
3836
return null
3837
fi
3838
3839
let values = _arguments_for(constructor, arguments)
3840
3841
if !values? then
3842
return null
3843
fi
3844
3845
return _allocate(objects, type, constructor, callee, values)
3846
si
3847
3848
// Allocates an instance of `type` and calls `callee`, its constructor,
3849
// on it with `values`, already lowered. The root's constructor has no
3850
// body, so `callee` is null for it.
3851
_allocate(objects: LLVM_OBJECTS, type: CLASS_INSTANCE, constructor: Function, callee: FUNCTION?, values: LIST[LLVM.Value]) -> LLVM.Value is
3852
_ensure_initialised(constructor.owner)
3853
objects.note_allocated(type)
3854
3855
let object =
3856
_strings!.allocate_object(_current, LLVM.Values.global(objects.descriptor_name(type)), LLVM.Values.i64(cast long(objects.size_of(type))), true)
3857
3858
_sources[object] = EXACT_SOURCE(type, null, null)
3859
3860
_register_finalizer(objects, type, object)
3861
3862
if let c = callee then
3863
values.insert(0, object)
3864
3865
_emit_call(c, values.to_array())
3866
fi
3867
3868
return object
3869
si
3870
3871
// Has the collector call the finalizer `type` or the nearest of its
3872
// superclasses declares, where one does, once `object` is
3873
// unreachable. A class with no finalizer in its chain pays nothing.
3874
_register_finalizer(objects: LLVM_OBJECTS, type: CLASS_INSTANCE, object: LLVM.Value) is
3875
let declarer: CLASS_INSTANCE? mut = type
3876
3877
while let d = declarer do
3878
if let finalizer = d.definition.finalizer then
3879
if let method = _callee(FUNCTION_INSTANCE(finalizer, d.type_map)) then
3880
let register = _strings!.runtime("$ghul_register_finalizer", LLVM.Type.VOID, [Types.pointer, Types.pointer])
3881
3882
_current.call(register, [object, LLVM.Values.global(method.name)])
3883
fi
3884
3885
return
3886
fi
3887
3888
declarer = objects.superclass_of(d)
3889
od
3890
si
3891
3892
// The arguments of a call to `function`, each brought into the
3893
// range of its parameter's kind, or null where one did not lower.
3894
_arguments_for(function: Function, arguments: Collections.List[Value]) -> LIST[LLVM.Value]? is
3895
let values = LIST[LLVM.Value]()
3896
let parameters = function.arguments
3897
3898
for i in 0..arguments.count do
3899
if let value = _value_of(arguments[i]) then
3900
values.add(if i < parameters.count then _normalise_for(value, parameters[i]) else value fi)
3901
else
3902
return null
3903
fi
3904
od
3905
3906
return values
3907
si
3908
3909
_is_trait_method(function: Function) -> bool =>
3910
cast Semantic.Symbols.Classy?(function.root_specialized_from.owner)?.is_trait ?? false
3911
3912
// A call to a method of the root's constructor runs nothing, since
3913
// the root has no state of its own.
3914
_is_object_constructor(function: Function) -> bool =>
3915
function.is_constructor /\ (cast Semantic.Symbols.Classy?(function.owner)?.is_object ?? false)
3916
3917
_call_instance(call: Values.Call.INSTANCE) is
3918
let function = call.function
3919
let dispatch = call.dispatch
3920
3921
if _is_get_type(function) then
3922
_get_type(call)
3923
return
3924
fi
3925
3926
// An enum value is held as its integer, so its to_string is the
3927
// text the core library gives it from the enum's members.
3928
if function.name =~ "to_string" /\ call.arguments.count == 0 then
3929
if let text = INTERPOLATION_CALLS.enum_text(call.from, _specialize(call.from.type)) then
3930
gen(text)
3931
return
3932
fi
3933
fi
3934
3935
if let constrained: Values.CONSTRAINED = call.from then
3936
_call_constrained(call, constrained)
3937
return
3938
fi
3939
3940
// A value type's own method, of a struct, a scalar or a bigint, is
3941
// called with the address of the value, as a struct's is.
3942
if isa Semantic.Symbols.STRUCT(function.root_specialized_from.owner) /\ (_specialize(call.from.type)?.is_value_type ?? false) then
3943
_call_struct(call.from, function, call.arguments)
3944
return
3945
fi
3946
3947
if _is_object_constructor(function) then
3948
return
3949
fi
3950
3951
if !_objects? then
3952
_report("an instance method")
3953
return
3954
fi
3955
3956
if dispatch == IR.Dispatch.VIRTUAL /\ _is_trait_method(function) \/ dispatch == IR.Dispatch.INTERFACE then
3957
_call_interface(call)
3958
elif dispatch == IR.Dispatch.VIRTUAL /\ !function.has_no_overriders then
3959
_call_virtual(call)
3960
elif dispatch == IR.Dispatch.DIRECT \/ dispatch == IR.Dispatch.VIRTUAL then
3961
_call_direct(call, _instance_of(function))
3962
else
3963
_report("a {dispatch} call")
3964
fi
3965
si
3966
3967
_call_direct(call: Values.Call.INSTANCE, instance: FUNCTION_INSTANCE) is
3968
let callee = _callee(instance)
3969
3970
if !callee? then
3971
_report("a call to {instance.definition.name}")
3972
return
3973
fi
3974
3975
if let receiver = _value_of(call.from) then
3976
_call_direct_on(call, instance, receiver)
3977
fi
3978
si
3979
3980
// Calls `instance` directly on `receiver`, the call's receiver
3981
// already lowered.
3982
_call_direct_on(call: Values.Call.INSTANCE, instance: FUNCTION_INSTANCE, receiver: LLVM.Value) is
3983
let callee = _callee(instance)
3984
3985
if !callee? then
3986
_report("a call to {instance.definition.name}")
3987
return
3988
fi
3989
3990
if let values = _arguments_for(call.function, call.arguments) then
3991
3992
_check_receiver(call.from, if call.from.is_value_type then null else receiver fi)
3993
3994
values.insert(0, _receiver_for(instance, call.from, receiver))
3995
3996
if let result = _emit_call(callee, values.to_array()) then
3997
_sources[result] = EXACT_SOURCE(null, instance, null)
3998
_push(result)
3999
fi
4000
fi
4001
si
4002
4003
// A struct's method takes the address of the value it is called on.
4004
// Reached through a reference, as a trait call with one
4005
// implementation is, the receiver is the box holding the value, and
4006
// the address passed is that of the value within it.
4007
_receiver_for(instance: FUNCTION_INSTANCE, from: Value, receiver: LLVM.Value) -> LLVM.Value is
4008
if isa Semantic.Symbols.STRUCT(instance.definition.owner) /\ !(_specialize(from.type)?.is_value_type ?? false) then
4009
return _current.element_pointer(Types.i8, receiver, [_i32(LLVM_OBJECTS.BOX_VALUE_OFFSET)], true)
4010
fi
4011
4012
return receiver
4013
si
4014
4015
// The function type a call to `instance` through a pointer has: the
4016
// receiver, then its own arguments, then its result.
4017
_function_type(instance: FUNCTION_INSTANCE) -> LLVM.Type? is
4018
let parameters = LIST[LLVM.Type]()
4019
4020
parameters.add(Types.pointer)
4021
4022
let objects = _objects
4023
4024
if !objects? then
4025
return null
4026
fi
4027
4028
// Each type as the function is declared with it, so a call
4029
// through a pointer agrees with the function it reaches.
4030
for argument in instance.definition.arguments do
4031
if let held = objects.held_type(instance.specialize(argument)) then
4032
parameters.add(held)
4033
else
4034
return null
4035
fi
4036
od
4037
4038
let return_type = instance.specialize(instance.definition.return_type)
4039
let result = if LLVM_TYPES.is_void(return_type) then LLVM.Type.VOID else objects.held_type(return_type) fi
4040
4041
if let r = result then
4042
return LLVM.Type.FUNCTION(r, parameters.to_array(), false)
4043
fi
4044
4045
return null
4046
si
4047
4048
// While discovering, a call through a vtable or the trait table is
4049
// followed only for what its receiver and arguments reach, and
4050
// stands for a value of its result type.
4051
_discover_dispatched_call(call: Values.Call.INSTANCE, targets: (() -> Collections.Iterable[FUNCTION_INSTANCE]?)?) is
4052
gen(call.from)
4053
4054
for argument in call.arguments do
4055
gen(argument)
4056
od
4057
4058
if let result = _held(call.type) /\ !LLVM_TYPES.is_void(call.type) then
4059
let value = LLVM_TYPES.zero(result)
4060
4061
if let t = targets then
4062
_sources[value] = EXACT_SOURCE(t)
4063
fi
4064
4065
_push(value)
4066
fi
4067
si
4068
4069
// Marks the value a dispatched call left on the stack as coming from
4070
// whichever of `targets` it ran.
4071
_note_dispatched_result(depth: int, targets: (() -> Collections.Iterable[FUNCTION_INSTANCE]?)?) is
4072
if let t = targets /\ _stack.count > depth then
4073
_sources[_stack[_stack.count - 1]] = EXACT_SOURCE(t)
4074
fi
4075
si
4076
4077
// Calls through `entry`, the address of a function pointer, with
4078
// the receiver and the arguments.
4079
_call_through(call: Values.Call.INSTANCE, receiver: LLVM.Value, values: LIST[LLVM.Value], entry: LLVM.Value) is
4080
let function_type = _function_type(_instance_of(call.function))
4081
4082
if !function_type? then
4083
_report("a call to {call.function.name}")
4084
return
4085
fi
4086
4087
values.insert(0, receiver)
4088
4089
let target = _current.load(Types.pointer, entry, 8)
4090
4091
if let result = _emit_call(function_type, target, values.to_array()) then
4092
_push(result)
4093
fi
4094
si
4095
4096
// The receiver's vtable is read from its descriptor, and the
4097
// method's slot called.
4098
_call_virtual(call: Values.Call.INSTANCE) is
4099
let objects = _objects!
4100
4101
let method = _instance_of(call.function)
4102
let targets = () -> Collections.Iterable[FUNCTION_INSTANCE]? => objects.slot_targets(method)
4103
4104
if !objects.is_built then
4105
objects.note_dispatched(call.function)
4106
_discover_dispatched_call(call, targets)
4107
return
4108
fi
4109
4110
let slot = objects.slot_of(method)
4111
4112
if !slot? then
4113
_report("a virtual call to {call.function.name}")
4114
return
4115
fi
4116
4117
// Where every class that can exist runs the same method through
4118
// the slot, the call is a direct one, which the optimiser can
4119
// inline.
4120
if let only = objects.only_slot_implementation(slot) /\ _callee(only)? then
4121
_call_direct(call, only)
4122
return
4123
fi
4124
4125
let receiver = _value_of(call.from)
4126
4127
if !receiver? then
4128
return
4129
fi
4130
4131
// A receiver whose exact class is known runs that class's own
4132
// entry for the slot.
4133
if let type = _exact_class(receiver), exact = objects.exact_slot_implementation(slot, type) /\ _callee(exact)? then
4134
_call_direct_on(call, exact, receiver)
4135
return
4136
fi
4137
4138
if let values = _arguments_for(call.function, call.arguments) then
4139
4140
_check_receiver(call.from, receiver)
4141
4142
let descriptor = _strings!.load_descriptor(_current, receiver)
4143
let entry = _current.element_pointer(Types.i8, descriptor, [_i32(LLVM_OBJECTS.DESCRIPTOR_VTABLE + 8 * slot.index)], true)
4144
let depth = _stack.count
4145
4146
_call_through(call, receiver, values, entry)
4147
_note_dispatched_result(depth, targets)
4148
fi
4149
si
4150
4151
// A trait call no instance the program makes can receive: nothing
4152
// implements the trait, or the one class that does is never
4153
// instantiated. The receiver can only be null, and the call throws as
4154
// a call on null does.
4155
_call_on_null(call: Values.Call.INSTANCE) is
4156
_value_of(call.from)
4157
_arguments_for(call.function, call.arguments)
4158
_throw_new(Semantic.Lookups.RuntimeException.NULL_REFERENCE)
4159
_start(_new_block())
4160
4161
if let result = _held(_instance_of(call.function).specialize(call.function.return_type)) then
4162
_push(LLVM_TYPES.zero(result))
4163
fi
4164
si
4165
4166
// The receiver's class number, read from its descriptor, picks the
4167
// method's entry in its row of the trait table.
4168
_call_interface(call: Values.Call.INSTANCE) is
4169
let objects = _objects!
4170
let function = call.function
4171
let trait_type = _trait_instance(function.owner)
4172
let root = cast Function?(function.root_specialized_from)!
4173
4174
let generic_arguments = DISPATCH_PLAN.own_type_arguments(_instance_of(function))
4175
4176
let targets: (() -> Collections.Iterable[FUNCTION_INSTANCE]?)? =
4177
if let t = trait_type /\ !generic_arguments? then
4178
() -> Collections.Iterable[FUNCTION_INSTANCE]? => objects.trait_targets(root, t)
4179
else
4180
null
4181
fi
4182
4183
if !objects.is_built then
4184
if let arguments = generic_arguments, t = trait_type then
4185
objects.note_generic_call(root, t, arguments)
4186
else
4187
objects.note_dispatched(function)
4188
4189
if let t = trait_type then
4190
objects.selector_of(root, t)
4191
fi
4192
fi
4193
4194
// A trait nothing implements is called by throwing, below.
4195
_reach_exception(Semantic.Lookups.RuntimeException.NULL_REFERENCE)
4196
4197
_discover_dispatched_call(call, targets)
4198
return
4199
fi
4200
4201
// A method generic in its own right has a row for each
4202
// instantiation a call was recorded at.
4203
let selector =
4204
if let t = trait_type then
4205
if let arguments = generic_arguments then objects.generic_selector_of(root, t, arguments) else objects.selector_of(root, t) fi
4206
else
4207
null
4208
fi
4209
4210
if !selector? then
4211
_call_on_null(call)
4212
return
4213
fi
4214
4215
if let only = objects.only_implementation(selector) then
4216
if _callee(only)? then
4217
_call_direct(call, only)
4218
else
4219
_call_on_null(call)
4220
fi
4221
4222
return
4223
fi
4224
4225
let receiver = _value_of(call.from)
4226
4227
if !receiver? then
4228
return
4229
fi
4230
4231
if let type = _exact_class(receiver), exact = objects.exact_implementation(selector, type) /\ _callee(exact)? then
4232
_call_direct_on(call, exact, receiver)
4233
return
4234
fi
4235
4236
if let values = _arguments_for(function, call.arguments) then
4237
4238
_check_receiver(call.from, receiver)
4239
4240
let descriptor = _strings!.load_descriptor(_current, receiver)
4241
let class_id = _current.load(Types.i32, descriptor, 8)
4242
let index = _current.binary(BinaryOperator.ADD, IntegerFlags.NONE, class_id, _i32(selector.offset))
4243
let entry = _current.element_pointer(Types.pointer, LLVM.Values.global(LLVM_OBJECTS.TRAIT_TABLE_NAME), [index], true)
4244
let depth = _stack.count
4245
4246
_call_through(call, receiver, values, entry)
4247
_note_dispatched_result(depth, targets)
4248
fi
4249
si
4250
4251
// The class numbers a value must have to be a `type`: one range for
4252
// a class, and the implementors' numbers for a trait. Null where the
4253
// target cannot test for the type.
4254
_class_ranges(type: Type?) -> LIST[(first: int, last: int)]? is
4255
let objects = _objects
4256
4257
if !objects? \/ !type? then
4258
return null
4259
fi
4260
4261
let result = LIST[(first: int, last: int)]()
4262
4263
if let array = objects.classes.array_of(type) then
4264
return objects.array_ranges(array)
4265
elif let target = objects.class_of(type) ?? objects.box_type(type) ?? objects.struct_of(type) then
4266
result.add(objects.class_range(target))
4267
4268
return result
4269
fi
4270
4271
if type.is_trait then
4272
if let trait_type = objects.trait_of(type) then
4273
if let implementors = objects.implementors(trait_type) then
4274
for implementor in implementors do
4275
result.add(objects.class_range(implementor))
4276
od
4277
fi
4278
4279
return result
4280
fi
4281
fi
4282
4283
return null
4284
si
4285
4286
// The class number of the object `value` points at, or -1 where it
4287
// is null, which no class has.
4288
_class_id(value: LLVM.Value) -> LLVM.Value is
4289
let slot = _entry.alloca(Types.i32, 4)
4290
4291
_current.store(_i32(-1), slot, 4)
4292
4293
let present = _new_block()
4294
let join = _new_block()
4295
4296
_current.branch(_current.compare(IntegerPredicate.NE, value, LLVM.Values.null_pointer), present, join)
4297
4298
_start(present)
4299
4300
let descriptor = _strings!.load_descriptor(_current, value)
4301
4302
_current.store(_current.load(Types.i32, descriptor, 8), slot, 4)
4303
_current.branch(join)
4304
4305
_start(join)
4306
4307
return _current.load(Types.i32, slot, 4)
4308
si
4309
4310
// Whether the class number `id` falls in one of `ranges`, as an `i1`.
4311
_in_ranges(id: LLVM.Value, ranges: LIST[(first: int, last: int)]) -> LLVM.Value is
4312
let result mut = LLVM.Values.boolean(false)
4313
4314
for (first, last) in ranges do
4315
let from_first = _current.compare(IntegerPredicate.SGE, id, _i32(first))
4316
let to_last = _current.compare(IntegerPredicate.SLE, id, _i32(last))
4317
let within = _current.binary(BinaryOperator.AND, IntegerFlags.NONE, from_first, to_last)
4318
4319
result = _current.binary(BinaryOperator.OR, IntegerFlags.NONE, result, within)
4320
od
4321
4322
return result
4323
si
4324
4325
visit(test: Values.ISA) is
4326
let ranges = _class_ranges(_specialize(test.isa_type))
4327
4328
if !ranges? then
4329
_report("a type test against {test.isa_type}")
4330
return
4331
fi
4332
4333
if let value = _value_of(_type_test_operand(test.value)) then
4334
if !(isa LLVM.Type.POINTER(value.type)) then
4335
_report("a type test of {test.value.type}")
4336
return
4337
fi
4338
4339
_push(_in_ranges(_class_id(value), ranges))
4340
fi
4341
si
4342
4343
// A cast between references tests the class number, since a
4344
// narrowed operand can carry the type it is being tested for: a
4345
// checked cast throws where the test fails, and one to an optional
4346
// type answers null. A cast to `object` cannot fail.
4347
visit(conversion: Values.CAST) is
4348
let target = _specialize(conversion.type)
4349
let operand = _type_test_operand(conversion.value)
4350
4351
if let t = target /\ _value_optional_inner(t)? then
4352
_cast_to_optional(conversion, t)
4353
return
4354
fi
4355
4356
// `null` cast to a reference type is that type's null.
4357
if (conversion.value.type?.is_null ?? false) /\ isa LLVM.Type.POINTER(_held(target)) then
4358
gen(conversion.value)
4359
return
4360
fi
4361
4362
let held = _held(operand.type)
4363
4364
if !isa Values.BOX(operand) /\ (!held? \/ !isa LLVM.Type.POINTER(held)) then
4365
_report("a cast")
4366
return
4367
fi
4368
4369
// A bigint is a reference to its box, so a cast to one checks
4370
// the object is a bigint and keeps the reference.
4371
if _objects?.bigint_of(_specialize(target))? then
4372
_unbox(conversion.value, target)
4373
return
4374
fi
4375
4376
// A checked cast to a scalar or a struct tests for its box and
4377
// keeps the reference, which is unboxed afterwards, and so does a
4378
// cast to a type parameter, which declines rather than throwing.
4379
// Any other unchecked one unboxes.
4380
let is_box = _objects?.box_type(target)? \/ _objects?.struct_of(target)?
4381
4382
if is_box /\ !conversion.is_checked /\ !conversion.type.is_type_variable then
4383
_unbox(conversion.value, target)
4384
return
4385
fi
4386
4387
if !target? \/ !(LLVM_TYPES.is_object_reference(target) \/ is_box) then
4388
_report("a cast to {conversion.type}")
4389
return
4390
fi
4391
4392
let value = _value_of(operand)
4393
4394
if !value? then
4395
return
4396
fi
4397
4398
if target.is_object then
4399
_push(value)
4400
return
4401
fi
4402
4403
let ranges = _class_ranges(target)
4404
4405
if !ranges? then
4406
_report("a cast to {conversion.type}")
4407
return
4408
fi
4409
4410
let id = _class_id(value)
4411
let fits = _current.binary(BinaryOperator.OR, IntegerFlags.NONE, _in_ranges(id, ranges), _current.compare(IntegerPredicate.EQ, id, _i32(-1)))
4412
4413
// A checked cast to a box throws where the box is of another
4414
// type, unless it is a cast to or from a type parameter, which
4415
// declines.
4416
let declines = conversion.type.is_type_variable \/ (conversion.value.type?.is_type_variable ?? false)
4417
4418
if conversion.faults_on_failure \/ (conversion.is_checked /\ is_box /\ !declines) then
4419
_throw_if(_current.compare(IntegerPredicate.EQ, fits, LLVM.Values.boolean(false)), Semantic.Lookups.RuntimeException.INVALID_CAST)
4420
_push(value)
4421
else
4422
_push(_current.select(fits, value, LLVM.Values.null_pointer))
4423
fi
4424
si
4425
4426
visit(equality: Values.REFERENCE_EQUALITY) is
4427
if let left = _value_of(equality.left), right = _value_of(equality.right) then
4428
_push(_current.compare(IntegerPredicate.EQ, left, right))
4429
fi
4430
si
4431
4432
visit(check: Values.REFERENCE_NULL_CHECK) is
4433
if let value = _value_of(check.inner) then
4434
_check_dereference(value)
4435
_push(value)
4436
fi
4437
si
4438
4439
// The signature a call through a function value of `type` has: the
4440
// closure's receiver, then the function type's own parameters, then
4441
// its result. Null where `type` is not a function type the target
4442
// holds values of.
4443
_closure_signature(type: Type?) -> LLVM.Type.FUNCTION? is
4444
let specialized = _specialize(type)
4445
4446
if !specialized? \/ !(specialized.is_function \/ specialized.is_action) then
4447
return null
4448
fi
4449
4450
let arguments = specialized.arguments
4451
let count = if specialized.is_action then arguments.count else arguments.count - 1 fi
4452
4453
let parameters = LIST[LLVM.Type]()
4454
4455
parameters.add(Types.pointer)
4456
4457
for i in 0..count do
4458
if let held = _held(arguments[i]) then
4459
parameters.add(held)
4460
else
4461
return null
4462
fi
4463
od
4464
4465
let result =
4466
if specialized.is_action \/ LLVM_TYPES.is_void(arguments[count]) then
4467
LLVM.Type.VOID
4468
else
4469
_held(arguments[count])
4470
fi
4471
4472
if !result? then
4473
return null
4474
fi
4475
4476
return cast LLVM.Type.FUNCTION(LLVM.Type.FUNCTION(result, parameters.to_array(), false))
4477
si
4478
4479
visit(load: Values.Load.STATIC_ANONYMOUS_FUNCTION) is _anonymous_function(load.symbol, load.func_type) si
4480
visit(load: Values.Load.GLOBAL_ANONYMOUS_FUNCTION) is _anonymous_function(load.symbol, load.func_type) si
4481
visit(load: Values.Load.INSTANCE_ANONYMOUS_FUNCTION) is _report("a function value") si
4482
4483
// A literal that captures nothing but its enclosing body's type
4484
// parameters is a function value with no receiver.
4485
_anonymous_function(symbol: Semantic.Symbols.Symbol, type: Type) is
4486
if let function: Function = symbol then
4487
_closure(Values.MAKE_CLOSURE(type, function, Values.NULL(Semantic.Types.NULL())), true)
4488
else
4489
_report("a function value")
4490
fi
4491
si
4492
4493
/// A function value holds the function a call through it reaches,
4494
/// directly or through a thunk, and the receiver or frame it passes.
4495
visit(closure: Values.MAKE_CLOSURE) is _closure(closure, false) si
4496
4497
// A capture-free literal is one constant closure rather than one
4498
// allocated on every evaluation.
4499
_closure(closure: Values.MAKE_CLOSURE, is_capture_free_literal: bool) is
4500
let objects = _objects
4501
4502
if !objects? then
4503
_report("a function value")
4504
return
4505
fi
4506
4507
let function = closure.function
4508
let instance = _instance_of(function)
4509
let root = cast Function?(function.root_specialized_from)!
4510
let is_trait = (closure.is_virtual \/ function.is_abstract) /\ _is_trait_method(function)
4511
let is_virtual = !is_trait /\ closure.is_virtual /\ !function.has_no_overriders
4512
let has_receiver = !isa Values.NULL(closure.receiver)
4513
4514
if !objects.is_built then
4515
if is_trait then
4516
objects.note_dispatched(function)
4517
4518
if let t = _trait_instance(function.owner) then
4519
objects.selector_of(root, t)
4520
fi
4521
elif is_virtual then
4522
objects.note_dispatched(function)
4523
else
4524
_callee(instance)
4525
fi
4526
4527
if has_receiver then
4528
_value_of(closure.receiver)
4529
fi
4530
4531
_push(LLVM.Values.null_pointer)
4532
return
4533
fi
4534
4535
let signature = _closure_signature(closure.type)
4536
4537
if !signature? then
4538
_report("a function value of type {closure.type}")
4539
return
4540
fi
4541
4542
let target: FUNCTION? mut = null
4543
let kind mut = ThunkKind.PASS_RECEIVER
4544
let offset mut = 0
4545
4546
if is_trait then
4547
let selector = if let t = _trait_instance(function.owner) then objects.selector_of(root, t) else null fi
4548
4549
if !selector? then
4550
_report("a function value of a trait method nothing implements")
4551
return
4552
fi
4553
4554
kind = ThunkKind.INTERFACE
4555
offset = selector.offset
4556
elif is_virtual then
4557
let slot = objects.slot_of(instance)
4558
4559
if !slot? then
4560
_report("a function value of an overridden method")
4561
return
4562
fi
4563
4564
kind = ThunkKind.VIRTUAL
4565
offset = slot.index
4566
else
4567
target = _callee(instance)
4568
4569
if !target? then
4570
_report("a function value of {function.name}")
4571
return
4572
fi
4573
4574
// A function with no receiver can still be held closed over
4575
// its first argument, which the value carries where a
4576
// receiver would go.
4577
if !function.is_emitted_with_receiver then
4578
if !has_receiver then
4579
kind = ThunkKind.DROP_RECEIVER
4580
elif target.parameters.count == 0 \/ !isa LLVM.Type.POINTER(target.parameters[0].type) then
4581
_report("a function value closed over a value that is not a reference")
4582
return
4583
fi
4584
fi
4585
fi
4586
4587
let thunk = objects.closures!.thunk(signature, instance, target, kind, offset)
4588
4589
if !thunk? then
4590
_report("a function value of {function.name}")
4591
return
4592
fi
4593
4594
let receiver: LLVM.Value mut = LLVM.Values.null_pointer
4595
4596
if has_receiver /\ kind != ThunkKind.DROP_RECEIVER then
4597
if let value = _value_of(closure.receiver) then
4598
receiver = value
4599
else
4600
return
4601
fi
4602
fi
4603
4604
if is_capture_free_literal /\ !has_receiver then
4605
_push(objects.closures!.constant(thunk))
4606
return
4607
fi
4608
4609
let value =
4610
_strings!.allocate_object(_current, objects.closures!.descriptor, LLVM.Values.i64(cast long(LLVM_CLOSURES.SIZE)), true)
4611
4612
_current.store(thunk.reference, _current.element_pointer(Types.i8, value, [_i32(LLVM_CLOSURES.FUNCTION_OFFSET)], true), 8)
4613
_current.store(receiver, _current.element_pointer(Types.i8, value, [_i32(LLVM_CLOSURES.RECEIVER_OFFSET)], true), 8)
4614
4615
_push(value)
4616
si
4617
4618
/// A call through a function value passes the receiver it holds,
4619
/// then the call's arguments.
4620
visit(call: Values.Call.CLOSURE) is
4621
let func_type = call.func_type ?? call.from.type
4622
let signature = _closure_signature(func_type)
4623
4624
if !signature? then
4625
_report("a call to a function value of type {func_type}")
4626
return
4627
fi
4628
4629
let closure = _value_of(call.from)
4630
4631
if !closure? then
4632
return
4633
fi
4634
4635
let parameters = _specialize(func_type)!.arguments
4636
let values = LIST[LLVM.Value]()
4637
4638
for i in 0..call.arguments.count do
4639
if let value = _value_of(call.arguments[i]) then
4640
values.add(if i < parameters.count then _normalise_for(value, parameters[i]) else value fi)
4641
else
4642
return
4643
fi
4644
od
4645
4646
_check_dereference(closure)
4647
4648
let function = _current.load(Types.pointer, _current.element_pointer(Types.i8, closure, [_i32(LLVM_CLOSURES.FUNCTION_OFFSET)], true), 8)
4649
let receiver = _current.load(Types.pointer, _current.element_pointer(Types.i8, closure, [_i32(LLVM_CLOSURES.RECEIVER_OFFSET)], true), 8)
4650
4651
values.insert(0, receiver)
4652
4653
if let result = _emit_call(signature, function, values.to_array()) then
4654
if !(signature.result =~ LLVM.Type.VOID) then
4655
_push(result)
4656
fi
4657
fi
4658
si
4659
4660
// The landing pad of the innermost try being lowered: the block
4661
// starts the handler, and makes this function name the runtime's
4662
// personality routine.
4663
_landing_pad(block: BLOCK) -> LLVM.Value is
4664
_function.personality = _strings!.runtime("$ghul_personality", Types.i32, []).reference
4665
4666
_start(block)
4667
4668
let landed = _current.landing_pad(Types.structure([Types.pointer, Types.i32]), false, [LLVM.Values.null_pointer])
4669
let header = _current.extract_value(Types.pointer, landed, [0])
4670
4671
return _current.call(_strings!.runtime("$ghul_caught", Types.pointer, [Types.pointer]), [header])!
4672
si
4673
4674
// The protected body's calls unwind to a landing pad that tests the
4675
// exception against each catch's type in turn. A catch that matches
4676
// has the exception on the stack where its body starts. One no catch
4677
// matches is thrown on.
4678
_lower_try_catch(node: Values.TRY) is
4679
let end = _new_block()
4680
let handler = _new_block()
4681
let enclosing = _unwind
4682
4683
_labels.add((node, false, end, null, _stack.count))
4684
4685
_unwind = handler
4686
4687
for value in node.body.values do
4688
gen(value)
4689
od
4690
4691
_unwind = enclosing
4692
4693
if !_is_terminated then
4694
_branch(end)
4695
fi
4696
4697
let exception = _landing_pad(handler)
4698
let caught = _slot(Types.pointer)
4699
4700
_current.store(exception, caught, 8)
4701
4702
for `catch in node.catches do
4703
let ranges = _class_ranges(_specialize(`catch.type))
4704
4705
if !ranges? then
4706
_report("a catch of {`catch.type}")
4707
continue
4708
fi
4709
4710
let id = _class_id(_current.load(Types.pointer, caught, 8))
4711
let matched = _new_block()
4712
let next = _new_block()
4713
4714
_current.branch(_in_ranges(id, ranges), matched, next)
4715
4716
_start(matched)
4717
4718
_caught.add(caught)
4719
_push(_current.load(Types.pointer, caught, 8))
4720
4721
for value in `catch.body.values do
4722
gen(value)
4723
od
4724
4725
_caught.remove_at(_caught.count - 1)
4726
4727
if !_is_terminated then
4728
_branch(end)
4729
fi
4730
4731
_start(next)
4732
od
4733
4734
_throw(_current.load(Types.pointer, caught, 8))
4735
4736
_labels.remove_at(_labels.count - 1)
4737
4738
_start(end)
4739
si
4740
4741
// An exit that leaves the protected body records which exit it is
4742
// and goes to the finally, after which the exit is taken again from
4743
// outside. An exception runs the finally and is thrown on. The
4744
// finally is lowered once for each path.
4745
_lower_try_finally(node: Values.TRY, `finally: Values.REGION) is
4746
let end = _new_block()
4747
let handler = _new_block()
4748
let normal = _new_block()
4749
let selector = _slot(Types.i32)
4750
let exits = IR.Program.EXIT_NUMBERING()
4751
let enclosing = _unwind
4752
4753
_current.store(_i32(0), selector, 4)
4754
4755
_labels.add((node, false, end, null, _stack.count))
4756
4757
let marker = _labels.count
4758
4759
_labels.add((node, true, normal, null, _stack.count))
4760
_finally_exits[marker] = (selector, exits)
4761
4762
_unwind = handler
4763
4764
for value in node.body.values do
4765
gen(value)
4766
od
4767
4768
_unwind = enclosing
4769
4770
if !_is_terminated then
4771
_branch(normal)
4772
fi
4773
4774
_finally_exits.remove(marker)
4775
_labels.remove_at(marker)
4776
4777
let exception = _landing_pad(handler)
4778
let caught = _slot(Types.pointer)
4779
4780
_current.store(exception, caught, 8)
4781
4782
_lower_region(`finally, null)
4783
4784
if !_is_terminated then
4785
_throw(_current.load(Types.pointer, caught, 8))
4786
fi
4787
4788
_start(normal)
4789
4790
_lower_region(`finally, null)
4791
4792
if !_is_terminated then
4793
let taken = _current.load(Types.i32, selector, 4)
4794
4795
for i in 0..exits.exits.count do
4796
let take = _new_block()
4797
let next = _new_block()
4798
4799
_current.branch(_current.compare(IntegerPredicate.EQ, taken, _i32(i + 1)), take, next)
4800
4801
_start(take)
4802
4803
_route(exits.exits[i], true)
4804
4805
if !_is_terminated then
4806
_branch(end)
4807
fi
4808
4809
_start(next)
4810
od
4811
4812
_branch(end)
4813
fi
4814
4815
_labels.remove_at(_labels.count - 1)
4816
4817
_start(end)
4818
si
4819
4820
// Records which exit is being taken and goes to the finally, after
4821
// which the exit is taken again.
4822
_leave_through_finally(exit: Values.Exit, is_taken: bool, selector: LLVM.Value, exits: IR.Program.EXIT_NUMBERING, finally_block: BLOCK) is
4823
let number = exits.number(exit)
4824
let target = _target_label(exit)
4825
4826
if is_taken \/ exit.condition == IR.BRANCH.ALWAYS then
4827
// A value the exit carries goes where its target reads it now,
4828
// since the finally runs between here and there.
4829
if !is_taken then
4830
if let t = target then
4831
_leave_with_result(t.result, t.depth)
4832
fi
4833
fi
4834
4835
_current.store(_i32(number), selector, 4)
4836
_branch(finally_block)
4837
return
4838
fi
4839
4840
if target?.result? ?? false then
4841
_report("a conditional exit carrying a value out of a try with a finally")
4842
return
4843
fi
4844
4845
if let condition = _test(exit.condition, exit.operands) then
4846
let leave = _new_block()
4847
let next = _new_block()
4848
4849
_current.branch(condition, leave, next)
4850
4851
_start(leave)
4852
4853
_current.store(_i32(number), selector, 4)
4854
_branch(finally_block)
4855
4856
_start(next)
4857
fi
4858
si
4859
4860
/// A throw hands the exception to the runtime, which unwinds to
4861
/// the innermost try's landing pad. A throw with no exception, in a
4862
/// catch, throws on the exception the catch caught.
4863
visit(thrown: Values.THROW) is
4864
if let exception = thrown.exception then
4865
if let value = _value_of(exception) then
4866
_throw(value)
4867
fi
4868
elif _caught.count > 0 then
4869
_throw(_current.load(Types.pointer, _caught[_caught.count - 1], 8))
4870
else
4871
_report("a rethrow outside a catch")
4872
fi
4873
si
4874
4875
// `left =~ right` over values of the concrete type the comparer's
4876
// type parameter is.
4877
visit(equality: Values.EQUALITY_COMPARISON) is
4878
let type = _specialize(equality.left.type)
4879
let left = _value_of(equality.left)
4880
let right = _value_of(equality.right)
4881
4882
if left? /\ right? then
4883
if let equal = _equal_held(left, right, type) then
4884
_push(equal)
4885
fi
4886
fi
4887
si
4888
4889
// Whether `left` and `right`, held values of `type`, are equal as `=~`
4890
// compares them, as an `i1`, or null where reported:
4891
// - a scalar by value, a NaN equal to another as .NET's comparer has it
4892
// - a string by its code units
4893
// - a tuple element by element
4894
// - a type through the `=~` it declares, with two absent references
4895
// equal and an absent one equal to nothing
4896
// - any other reference by identity
4897
_equal_held(left: LLVM.Value, right: LLVM.Value, type: Type?) -> LLVM.Value? is
4898
if let inner = _value_optional_inner(type) then
4899
return _equal_optionals(left, right, inner)
4900
fi
4901
4902
if _scalar_of(type)? then
4903
if LLVM_TYPES.is_floating(left.type) then
4904
let equal = _current.compare(FloatPredicate.OEQ, left, right)
4905
let left_nan = _current.compare(FloatPredicate.UNO, left, left)
4906
let right_nan = _current.compare(FloatPredicate.UNO, right, right)
4907
let both_nan = _current.binary(BinaryOperator.AND, IntegerFlags.NONE, left_nan, right_nan)
4908
4909
return _current.binary(BinaryOperator.OR, IntegerFlags.NONE, equal, both_nan)
4910
fi
4911
4912
return _current.compare(IntegerPredicate.EQ, left, right)
4913
fi
4914
4915
if let strings = _strings /\ _is_string(type) then
4916
return _current.call(strings.equals_function, [left, right])
4917
fi
4918
4919
if let tuple: Semantic.Types.TUPLE = type then
4920
let result mut = LLVM.Values.boolean(true)
4921
4922
for (i, element) in tuple.arguments |> Ghul.Pipes.index() do
4923
if !_stored(element)? then
4924
_report("an equality comparison of {type}")
4925
return null
4926
fi
4927
4928
let equal = _equal_held(_extract_member(left, i, element), _extract_member(right, i, element), element)
4929
4930
if !equal? then
4931
return null
4932
fi
4933
4934
result = _current.binary(BinaryOperator.AND, IntegerFlags.NONE, result, equal!)
4935
od
4936
4937
return result
4938
fi
4939
4940
let operator = if let t = type then EQUALITY_OPERATOR.declared(t) else null fi
4941
4942
if let t = type /\ t.is_value_type, o = operator then
4943
return _equal_values_through(o, left, right, t)
4944
elif let t = type, o = operator /\ isa LLVM.Type.POINTER(left.type) then
4945
return _equal_references_through(o, left, right, t)
4946
elif isa LLVM.Type.POINTER(left.type) then
4947
return _current.compare(IntegerPredicate.EQ, left, right)
4948
fi
4949
4950
_report("an equality comparison of {type}")
4951
4952
return null
4953
si
4954
4955
// `left =~ right` over two held optionals of a value type: two absent
4956
// ones are equal and an absent one equals nothing, and only two
4957
// present ones compare what they hold.
4958
_equal_optionals(left: LLVM.Value, right: LLVM.Value, inner: Type) -> LLVM.Value? is
4959
let result = _slot(Types.i1)
4960
let left_present = _is_present(left)
4961
let right_present = _is_present(right)
4962
let both = _new_block()
4963
let not_both = _new_block()
4964
let done = _new_block()
4965
4966
_current.branch(_current.binary(BinaryOperator.AND, IntegerFlags.NONE, left_present, right_present), both, not_both)
4967
4968
_start(not_both)
4969
_current.store(_current.compare(IntegerPredicate.EQ, left_present, right_present), result, 1)
4970
_branch(done)
4971
4972
_start(both)
4973
4974
let equal = _equal_held(_extract_member(left, 1, inner), _extract_member(right, 1, inner), inner)
4975
4976
if !equal? then
4977
return null
4978
fi
4979
4980
_current.store(equal!, result, 1)
4981
_branch(done)
4982
4983
_start(done)
4984
4985
return _current.load(Types.i1, result, 1)
4986
si
4987
4988
// `left =~ right` through a value type's own `=~`, which takes the
4989
// address of its receiver.
4990
_equal_values_through(operator: Function, left: LLVM.Value, right: LLVM.Value, type: Type) -> LLVM.Value? is
4991
let callee = _callee(_instance_of(operator))
4992
4993
if !callee? then
4994
_report("a call to {operator.name}")
4995
return null
4996
fi
4997
4998
let slot = _slot(left.type)
4999
5000
_current.store(left, slot, 0)
5001
5002
if let answer = _emit_call(callee!, [slot, right]) then
5003
return _current.compare(IntegerPredicate.NE, answer, LLVM_TYPES.zero(answer.type))
5004
fi
5005
5006
return null
5007
si
5008
5009
// `left =~ right` through a reference type's `=~`, called as any
5010
// instance method is, so an override is the one reached, and only
5011
// where both are present.
5012
_equal_references_through(operator: Function, left: LLVM.Value, right: LLVM.Value, type: Type) -> LLVM.Value is
5013
let result = _slot(Types.i1)
5014
let left_absent = _new_block()
5015
let left_present = _new_block()
5016
let both_present = _new_block()
5017
let done = _new_block()
5018
5019
_current.branch(_current.compare(IntegerPredicate.EQ, left, LLVM.Values.null_pointer), left_absent, left_present)
5020
5021
_start(left_absent)
5022
_current.store(_current.compare(IntegerPredicate.EQ, right, LLVM.Values.null_pointer), result, 1)
5023
_branch(done)
5024
5025
_start(left_present)
5026
_current.store(LLVM.Values.boolean(false), result, 1)
5027
_current.branch(_current.compare(IntegerPredicate.EQ, right, LLVM.Values.null_pointer), done, both_present)
5028
5029
_start(both_present)
5030
5031
let bool_type = IoC.CONTAINER.instance.innate_symbol_lookup.get_bool_type()
5032
5033
if let compared = _value_of(Values.Call.INSTANCE(LLVM_VALUE(left, type), operator, LIST[Value]([LLVM_VALUE(right, type)]), bool_type)) then
5034
_current.store(compared, result, 1)
5035
fi
5036
5037
_branch(done)
5038
5039
_start(done)
5040
5041
return _current.load(Types.i1, result, 1)
5042
si
5043
5044
// A constrained call on a reference is an ordinary call on it, once
5045
// the type parameter is the reference type it stands for.
5046
_call_constrained(call: Values.Call.INSTANCE, constrained: Values.CONSTRAINED) is
5047
let receiver = if let address: Values.ADDRESS = constrained.value then address.value else constrained.value fi
5048
let type = _specialize(constrained.type)
5049
5050
// A value type calls its own implementation directly, with the
5051
// value's address, and is boxed to reach one it inherits, as
5052
// CIL's constrained call does.
5053
if type?.is_value_type ?? false then
5054
let own = INTERPOLATION_CALLS.method(type, call.function.name, call.function.arguments.count)
5055
5056
if let o = own /\ isa Semantic.Symbols.STRUCT(o.root_specialized_from.owner) then
5057
_call_struct(constrained.value, o, call.arguments)
5058
else
5059
_call_instance(Values.Call.INSTANCE(Values.BOX(receiver, type!), call.function, call.arguments, call.type))
5060
fi
5061
5062
return
5063
fi
5064
5065
if !LLVM_TYPES.is_object_reference(type) /\ !_is_string(type) then
5066
_report("a constrained call on {type}")
5067
return
5068
fi
5069
5070
_call_instance(Values.Call.INSTANCE(receiver, call.function, call.arguments, call.type))
5071
si
5072
5073
// The value `value` boxes, seen through casts, where it is of a value
5074
// type that is not optional at this instantiation, and so the box can
5075
// never be absent. Null otherwise.
5076
_boxed_present_value(value: Value) -> Value? is
5077
let inner mut = value
5078
5079
while true do
5080
if let checked: Values.CAST = inner then
5081
inner = checked.value
5082
else
5083
break
5084
fi
5085
od
5086
5087
if let box: Values.BOX = inner, type = _specialize(box.value.type) /\ type.is_value_type /\ !type.is_optional then
5088
return box.value
5089
fi
5090
5091
return null
5092
si
5093
5094
// The operand of a type test, as the reference the test examines: a
5095
// value whose type parameter stands for a scalar is boxed.
5096
_type_test_operand(value: Value) -> Value =>
5097
if value.type?.type_variable_side? /\ _objects?.box_type(_specialize(value.type))? then
5098
Values.BOX(value)
5099
else
5100
Values.TYPE_TEST_OPERAND.operand(value)
5101
fi
5102
si
5103
si