Skip to content
← Back

src/ir/emitter/cil_lowering.ghul

1
@@IF.dotnet()
2
3
namespace IR.Emitter is
4
// Lowers IR values to CIL, through the method body emitter of the
5
// function being generated.
6
//
7
// A cast of a value's symbol to the kind the value was built for is
8
// a checked construction invariant: a throw means the value was
9
// built with the wrong kind of symbol.
10
@suppress("cast-may-throw")
11
class CIL_LOWERING: Values.Visitor is
12
_context: IR.Emitter.CIL_CONTEXT
13
14
context: CIL_CONTEXT => _context
15
16
_body: SRM_METHOD_BODY_EMITTER => _context.current_srm_body_emitter!
17
18
_async: CIL_ASYNC_LOWERING
19
20
init(context: IR.Emitter.CIL_CONTEXT) is
21
_context = context
22
_async = CIL_ASYNC_LOWERING(context)
23
si
24
25
visit_value(value: Values.Value) is
26
throw System.NotImplementedException("no lowering for {value.get_type()} {value}")
27
si
28
29
visit_address_of_value(value: Values.Value) is
30
throw System.NotImplementedException("no address lowering for {value.get_type()} {value}")
31
si
32
33
gen(value: Values.Value?) is
34
if value? then
35
value.accept(self)
36
else
37
_context.fixme("null value")
38
fi
39
si
40
41
gen_address(value: Values.Value) is
42
value.accept_address(self)
43
si
44
45
// Lowers an operand as a block lowers each of its values,
46
// marking its source position first.
47
gen_marked(value: Values.Value) is
48
if value.has_location then
49
_context.mark_location(value.location)
50
fi
51
52
gen(value)
53
si
54
55
// Marks the position `value` was built at, after its operands.
56
mark_operator(value: Values.Value) is
57
if let location = value.operator_location then
58
_context.mark_location(location)
59
fi
60
si
61
62
_field(symbol: Semantic.Symbols.Symbol) -> System.Reflection.Metadata.EntityHandle =>
63
_context.resolve_field_target(cast Semantic.Symbols.Field(symbol))
64
65
visit(number: IR.Values.Literal.NUMBER) is
66
let body = _body
67
68
case number.constant
69
when i4: IR.Values.Literal.CONSTANT.I4 then body.ldc_i4(i4.value)
70
when i8: IR.Values.Literal.CONSTANT.I8 then body.ldc_i8(i8.value)
71
when r4: IR.Values.Literal.CONSTANT.R4 then body.ldc_r4(r4.value)
72
when r8: IR.Values.Literal.CONSTANT.R8 then body.ldc_r8(r8.value)
73
esac
74
75
if let to = number.narrowing then
76
let from =
77
case number.constant
78
when _: IR.Values.Literal.CONSTANT.I4 then Scalar.INT32
79
when _: IR.Values.Literal.CONSTANT.I8 then Scalar.INT64
80
when _: IR.Values.Literal.CONSTANT.R4 then Scalar.FLOAT32
81
when _: IR.Values.Literal.CONSTANT.R8 then Scalar.FLOAT64
82
esac
83
84
for op_code in CIL_OPERATIONS.conversion(from, to) do
85
body.op(op_code)
86
od
87
fi
88
si
89
90
visit(string: IR.Values.Literal.STRING) is
91
_body.ldstr(_context.srm_assembly_emitter, string.value)
92
si
93
94
visit(decimal: IR.Values.Literal.DECIMAL) is
95
let body = _body
96
97
body.ldc_i4(decimal.lo)
98
body.ldc_i4(decimal.mid)
99
body.ldc_i4(decimal.hi)
100
body.ldc_i4(0)
101
body.ldc_i4(decimal.scale)
102
body.new_object(_context.resolve_decimal_constructor())
103
si
104
105
visit(bigint: IR.Values.Literal.BIGINT) is
106
let body = _body
107
108
if bigint.fits_long then
109
body.ldc_i8(bigint.as_long)
110
body.new_object(_context.resolve_bigint_constructor(bigint.type))
111
else
112
body.ldstr(_context.srm_assembly_emitter, bigint.value)
113
body.call(_context.resolve_bigint_parse(bigint.type))
114
fi
115
si
116
117
visit(load: Values.Load.TEMP) is
118
_body.ldloc(load.local)
119
si
120
121
visit_address(load: Values.Load.TEMP) is
122
_body.ldloca(load.local)
123
si
124
125
visit(load: Values.Load.STATIC_FIELD) is
126
_body.ldsfld(_field(load.symbol))
127
si
128
129
visit_address(load: Values.Load.STATIC_FIELD) is
130
_body.ldsflda(_field(load.symbol))
131
si
132
133
visit(load: Values.Load.GLOBAL_FIELD) is
134
_body.ldsfld(_field(load.symbol))
135
si
136
137
visit_address(load: Values.Load.GLOBAL_FIELD) is
138
_body.ldsflda(_field(load.symbol))
139
si
140
141
// A generator's arguments live in fields of its state-machine
142
// frame, and an argument spread out of a pack in a local of its
143
// own.
144
visit(load: Values.Load.LOCAL_ARGUMENT) is
145
let symbol = load.symbol
146
147
if let state_machine_field = Values.state_machine_field_for(symbol) then
148
_body.ldarg(0)
149
_body.ldfld(_context.resolve_field_target(state_machine_field))
150
elif let packed_local = Values.packed_parameter_local_for(symbol) then
151
_body.ldloc(packed_local)
152
else
153
_body.ldarg(_context.resolve_argument_index(symbol))
154
fi
155
156
_narrow_guard(load.snapshot_type)
157
si
158
159
visit_address(load: Values.Load.LOCAL_ARGUMENT) is
160
let symbol = load.symbol
161
162
if let state_machine_field = Values.state_machine_field_for(symbol) then
163
_body.ldarg(0)
164
_body.ldflda(_context.resolve_field_target(state_machine_field))
165
elif let packed_local = Values.packed_parameter_local_for(symbol) then
166
_body.ldloca(packed_local)
167
else
168
_body.ldarga(_context.resolve_argument_index(symbol))
169
fi
170
si
171
172
// A generator's locals live in fields of its state-machine frame.
173
visit(load: Values.Load.LOCAL_VARIABLE) is
174
if let state_machine_field = Values.state_machine_field_for(load.symbol) then
175
_body.ldarg(0)
176
_body.ldfld(_context.resolve_field_target(state_machine_field))
177
else
178
_body.ldloc((cast Semantic.Symbols.Variable(load.symbol)).ir_local)
179
fi
180
181
_narrow_guard(load.snapshot_type)
182
si
183
184
visit_address(load: Values.Load.LOCAL_VARIABLE) is
185
if let state_machine_field = Values.state_machine_field_for(load.symbol) then
186
_body.ldarg(0)
187
_body.ldflda(_context.resolve_field_target(state_machine_field))
188
else
189
_body.ldloca((cast Semantic.Symbols.Variable(load.symbol)).ir_local)
190
fi
191
si
192
193
// An instance method's receiver is slot 0, so its declared
194
// arguments sit one further along.
195
visit(load: Values.Load.ARGUMENT) is
196
_body.ldarg(if load.function.is_emitted_with_receiver then load.index + 1 else load.index fi)
197
si
198
199
visit(load: Values.Load.RECEIVER) is
200
assert load.function.is_emitted_with_receiver else "{load.function.name} has no receiver"
201
202
_body.ldarg(0)
203
si
204
205
visit(store: Values.Store.LOCAL_VARIABLE) is
206
if let state_machine_field = Values.state_machine_field_for(store.symbol) then
207
_body.ldarg(0)
208
gen(store.value)
209
_body.stfld(_context.resolve_field_target(state_machine_field))
210
else
211
gen(store.value)
212
_body.stloc((cast Semantic.Symbols.Variable(store.symbol)).ir_local)
213
fi
214
si
215
216
visit(store: Values.Store.LOCAL_ARGUMENT) is
217
let symbol = store.symbol
218
219
if let state_machine_field = Values.state_machine_field_for(symbol) then
220
_body.ldarg(0)
221
gen(store.value)
222
_body.stfld(_context.resolve_field_target(state_machine_field))
223
elif let packed_local = Values.packed_parameter_local_for(symbol) then
224
gen(store.value)
225
_body.stloc(packed_local)
226
else
227
gen(store.value)
228
_body.starg(_context.resolve_argument_index(symbol))
229
fi
230
si
231
232
visit(store: Values.Store.STATIC_FIELD) is
233
gen(store.value)
234
_body.stsfld(_field(store.symbol))
235
si
236
237
visit(store: Values.Store.GLOBAL_FIELD) is
238
gen(store.value)
239
_body.stsfld(_field(store.symbol))
240
si
241
242
visit(store: Values.Store.INSTANCE_FIELD) is
243
gen(store.from)
244
gen(store.value)
245
_body.stfld(_field(store.symbol))
246
si
247
248
// A load of a symbol with no lowering of its own.
249
visit(load: Values.Load.SYMBOL) is
250
gen(load.from)
251
_context.fixme("load member {load.symbol.qualified_name}")
252
si
253
254
visit_address(load: Values.Load.SYMBOL) is
255
gen(load.from)
256
_context.fixme("load member address {load.symbol.qualified_name}")
257
si
258
259
visit(load: Values.Load.INSTANCE_FIELD) is
260
gen(load.from)
261
_body.ldfld(_field(load.symbol))
262
si
263
264
// `ldflda` takes the containing instance: the reference for a
265
// reference-type receiver, a managed pointer for a value-type
266
// one. Taking a reference receiver's address would compute the
267
// field's offset from the local holding the reference.
268
visit_address(load: Values.Load.INSTANCE_FIELD) is
269
let from = load.from!
270
271
if from.is_value_type then
272
gen_address(from)
273
else
274
gen(from)
275
fi
276
277
_body.ldflda(_field(load.symbol))
278
si
279
280
visit(load: Values.Load.STATIC_ANONYMOUS_FUNCTION) is
281
_body.ldnull()
282
_load_delegate(load.symbol, load.func_type)
283
si
284
285
visit(load: Values.Load.GLOBAL_ANONYMOUS_FUNCTION) is
286
_body.ldnull()
287
_load_delegate(load.symbol, load.func_type)
288
si
289
290
visit(load: Values.Load.INSTANCE_ANONYMOUS_FUNCTION) is
291
_body.ldarg(0)
292
_load_delegate(load.symbol, load.func_type)
293
si
294
295
_load_delegate(function: Semantic.Symbols.Symbol, delegate_type: Semantic.Types.Type) is
296
_body.load_function_pointer(_context.resolve_call_target(cast Semantic.Symbols.Function(function)))
297
_body.new_object(_context.resolve_delegate_constructor(delegate_type))
298
si
299
300
// A store to a symbol with no lowering of its own.
301
visit(store: Values.Store.SYMBOL) is
302
gen(store.from)
303
gen(store.value)
304
305
throw System.NotImplementedException(
306
"the binary back end has no emission for storing to '{store.symbol}' yet")
307
si
308
309
_lower_all(values: Collections.Iterable[Values.Value]) is
310
for value in values do
311
gen(value)
312
od
313
si
314
315
visit(call: Values.Call.GLOBAL) is
316
_lower_all(call.arguments)
317
_body.call(_context.resolve_call_target(call.function))
318
si
319
320
// A static interface member reached through a type parameter,
321
// or through the type an interface is instantiated at, takes
322
// the `constrained.` prefix.
323
visit(call: Values.Call.STATIC) is
324
_lower_all(call.arguments)
325
326
if let receiver = call.constrained_receiver then
327
_body.constrained(_context.resolve_type_token(receiver))
328
fi
329
330
_body.call(_context.resolve_call_target(call.function))
331
si
332
333
visit(call: Values.Call.STRUCT) is
334
gen(call.from)
335
_lower_all(call.arguments)
336
_body.call(_context.resolve_call_target(call.function))
337
si
338
339
visit(call: Values.Call.INSTANCE) is
340
let from = call.from
341
342
if let element_type = _array_element_read(call) then
343
gen(from)
344
_lower_all(call.arguments)
345
_body.load_element(_type_token(element_type))
346
return
347
fi
348
349
if _is_array_count(call) then
350
gen(from)
351
_body.op(System.Reflection.Metadata.ILOpCode.LDLEN)
352
_body.op(System.Reflection.Metadata.ILOpCode.CONV_I4)
353
return
354
fi
355
356
gen(from)
357
_lower_all(call.arguments)
358
359
let dispatch = call.dispatch
360
361
if dispatch == IR.Dispatch.CONSTRAINED then
362
constrained_prefix(cast Values.CONSTRAINED(from))
363
fi
364
365
let target = _context.resolve_call_target(call.function)
366
367
// A constrained call to a function that cannot be overridden
368
// stays a plain call, as it always has.
369
if dispatch == IR.Dispatch.DIRECT \/ !call.function.is_virtual then
370
_body.call(target)
371
else
372
_body.call_virtual(target)
373
fi
374
si
375
376
// The element type of an array a call reads with its indexer, or
377
// null where the call is not one. An array's indexer is
378
// `IReadOnlyList<T>`'s, which would be an interface call that
379
// throws ArgumentOutOfRangeException out of range, where `ldelem`
380
// is one instruction and throws IndexOutOfRangeException.
381
_array_element_read(call: Values.Call.INSTANCE) -> Semantic.Types.Type? is
382
if call.function.name !~ Semantic.Symbols.INDEXER_NAMES.read \/ call.arguments.count != 1 then
383
return null
384
fi
385
386
if let array: Semantic.Types.ARRAY = call.from.type, index_type = call.arguments[0].type then
387
if IoC.CONTAINER.instance.innate_symbol_lookup.get_int_type().compare(index_type) == Semantic.Types.MATCH.SAME then
388
return array.get_element_type()
389
fi
390
fi
391
392
return null
393
si
394
395
// Whether a call reads an array's count, which would be
396
// `IReadOnlyCollection<T>.Count`, an interface call, where `ldlen`
397
// is one instruction.
398
_is_array_count(call: Values.Call.INSTANCE) -> bool =>
399
call.function.name =~ "$get_count" /\ call.arguments.count == 0 /\ isa Semantic.Types.ARRAY(call.from.type)
400
401
// A function type still carrying an inference placeholder means
402
// inference never settled and nothing upstream reported it.
403
// Encoding the signature would fail inside the emitter as an
404
// opaque internal error, so report a located diagnostic instead
405
// and leave the call unemitted.
406
visit(call: Values.Call.CLOSURE) is
407
let func_type = call.func_type
408
409
if IR.Emitter.CIL_CONTEXT.contains_sentinel(func_type) then
410
_context.report_unresolved_type(call.location, func_type!)
411
return
412
fi
413
414
gen(call.from)
415
_lower_all(call.arguments)
416
417
_body.call_virtual(
418
_context.resolve_delegate_invoke(func_type!, call.arguments.count, call.is_action))
419
si
420
421
// A built-in operation is lowered to the values that implement
422
// it the first time it is generated.
423
visit(call: Values.Call.INNATE) is
424
if !call.is_lowered then
425
gen(call.lower())
426
fi
427
si
428
429
visit(node: Values.Breakable) is
430
assert node.is_closed else "structured node generated before it was closed"
431
432
STRUCTURED_LOWERING.gen(node, _context)
433
si
434
435
visit(exit: Values.Exit) is
436
STRUCTURED_LOWERING.gen(exit, _context)
437
si
438
439
visit(dispatch: Values.DISPATCH) is
440
STRUCTURED_LOWERING.gen(dispatch, _context)
441
si
442
443
visit(resume: Values.RESUME_POINT) is
444
STRUCTURED_LOWERING.gen(resume, _context)
445
si
446
447
visit(composite: Values.Composite) is
448
gen_expansion(composite.expand())
449
si
450
451
// A coalesced load has no storage of its own to take the address
452
// of, so its value is spilled to a local first.
453
visit_address(coalesce: Values.COALESCE_LOAD) is
454
let local = IR.LOCAL(".coalesce.{TEMP.get_next_id()}", coalesce.type)
455
456
_body.declare_local(local)
457
gen(coalesce)
458
_body.stloc(local)
459
_body.ldloca(local)
460
si
461
462
// A value type reached through a wider slot is boxed there, so
463
// the check and the unboxing are the same instruction.
464
_narrow_guard(narrowed: Semantic.Types.Type?) is
465
if !Values.NARROW_GUARD.is_needed(narrowed) then
466
return
467
fi
468
469
if narrowed!.is_value_type then
470
_body.unbox_any(_context.resolve_type_token(narrowed!))
471
else
472
_body.cast_class(_context.resolve_type_token(narrowed!))
473
fi
474
si
475
476
// Encodes an expansion as part of the composite that built it,
477
// marking no source positions for the structure inside it.
478
gen_expansion(expansion: Values.BLOCK) is
479
_context.begin_unmarked()
480
481
for value in expansion.values do
482
gen(value)
483
od
484
485
_context.end_unmarked()
486
si
487
488
_type_token(type: Semantic.Types.Type) -> System.Reflection.Metadata.EntityHandle =>
489
_context.resolve_type_token(type)
490
491
// `isinst Nullable[T]` tests for a boxed T (ECMA-335 III.4.6), the
492
// same as `isinst T`. Native AOT's ILC through .NET 10 folds a branch
493
// on the former to always-false, so a successful test never returns
494
// (dotnet/runtime#134799). Naming T avoids it.
495
_type_test_token(type: Semantic.Types.Type) -> System.Reflection.Metadata.EntityHandle =>
496
if isa Semantic.Types.NULLABLE(type) then
497
_type_token(type.optional_inner_type!)
498
else
499
_type_token(type)
500
fi
501
502
visit(null_value: Values.NULL) is
503
_body.ldnull()
504
si
505
506
visit(box: Values.BOX) is
507
gen(box.value)
508
_body.box(_type_token(box.type!))
509
si
510
511
visit(size_of: Values.SIZE_OF) is
512
_body.size_of(_type_token(size_of.measured))
513
si
514
515
visit(unbox: Values.UNBOX) is
516
gen(unbox.value)
517
_body.unbox_any(_type_token(unbox.type!))
518
si
519
520
visit(conversion: Values.CAST) is
521
gen(Values.TYPE_TEST_OPERAND.operand(conversion.value))
522
523
if conversion.faults_on_failure then
524
_body.cast_class(_type_token(conversion.type))
525
else
526
_body.is_instance(_type_test_token(conversion.type))
527
fi
528
si
529
530
visit(test: Values.ISA) is
531
gen(Values.TYPE_TEST_OPERAND.operand(test.value))
532
533
_body.is_instance(_type_test_token(test.isa_type))
534
_body.ldnull()
535
_body.op(System.Reflection.Metadata.ILOpCode.CGT_UN)
536
si
537
538
visit(type_of: Values.TYPEOF) is
539
_body.ldtoken(_type_token(type_of.typeof_type))
540
_body.call(_context.resolve_get_type_from_handle())
541
si
542
543
visit(tuple: Values.TUPLE) is
544
_lower_all(tuple.values)
545
_body.new_object(_context.resolve_generic_constructor(tuple.type, tuple.values.count))
546
si
547
548
// A unit variant is a single shared instance. Which one to load
549
// follows from the value's own type rather than from wherever the
550
// constructor was found: for a generic variant the type names the
551
// instantiation, while the constructor's owner is the open
552
// definition, whose parameters an imported variant does not even
553
// declare.
554
visit(construction: Values.NEW) is
555
if construction.constructor.owner!.is_unit_variant then
556
_body.ldsfld(_context.resolve_unit_variant_instance(construction.type.symbol))
557
558
return
559
fi
560
561
_lower_all(construction.arguments)
562
_body.new_object(_context.resolve_call_target(construction.constructor))
563
si
564
565
visit(negation: Values.NOT) is
566
if negation.is_operator then
567
gen_marked(negation.value)
568
mark_operator(negation)
569
else
570
gen(negation.value)
571
fi
572
573
_body.ldc_i4(0)
574
_body.op(System.Reflection.Metadata.ILOpCode.CEQ)
575
si
576
577
visit(arithmetic: Values.ARITHMETIC) is
578
let scalar = IoC.CONTAINER.instance.value_converter.scalar(arithmetic.type)
579
580
for i in 0..arithmetic.operands.count do
581
let operand = arithmetic.operands[i]
582
583
if arithmetic.is_marked then
584
gen_marked(operand)
585
else
586
gen(operand)
587
fi
588
589
// An unsigned shift of a narrow signed kind shifts in zeros
590
// at the kind's own width, so its sign-extended operand is
591
// first cut back to that width.
592
if i == 0 /\ arithmetic.operation == ArithmeticOperation.SHIFT_RIGHT_UNSIGNED then
593
if scalar =~ Scalar.INT8 then
594
_body.op(System.Reflection.Metadata.ILOpCode.CONV_U1)
595
elif scalar =~ Scalar.INT16 then
596
_body.op(System.Reflection.Metadata.ILOpCode.CONV_U2)
597
fi
598
fi
599
od
600
601
if arithmetic.is_marked then
602
mark_operator(arithmetic)
603
fi
604
605
_body.op(CIL_OPERATIONS.arithmetic(arithmetic.operation))
606
607
// The evaluation stack holds a narrow kind's result as an
608
// int32, so it is wrapped back to the kind's width here rather
609
// than only where it is next stored.
610
if let narrow = scalar /\ _is_narrow(narrow) then
611
for op_code in CIL_OPERATIONS.conversion(Scalar.INT32, narrow) do
612
_body.op(op_code)
613
od
614
fi
615
si
616
617
_is_narrow(scalar: Scalar) -> bool =>
618
case scalar
619
when Scalar.CHAR, Scalar.INT8, Scalar.UINT8, Scalar.INT16, Scalar.UINT16 then true
620
else false
621
esac
622
623
visit(compare: Values.COMPARE) is
624
gen_marked(compare.left)
625
gen_marked(compare.right)
626
627
mark_operator(compare)
628
629
let (op_code, negates) = CIL_OPERATIONS.comparison(compare.comparison, compare.is_unsigned)
630
631
_body.op(op_code)
632
633
if negates then
634
_body.ldc_i4(0)
635
_body.op(System.Reflection.Metadata.ILOpCode.CEQ)
636
fi
637
si
638
639
visit(convert: Values.CONVERT) is
640
gen(convert.value)
641
642
for op_code in CIL_OPERATIONS.conversion(convert.from, convert.to) do
643
_body.op(op_code)
644
od
645
si
646
647
visit(declaration: Values.DECLARE_LOCAL) is
648
_body.declare_local(declaration.local)
649
si
650
651
visit(store: Values.STORE_TEMP) is
652
_body.stloc(store.local)
653
si
654
655
visit(tee: Values.TEE_TEMP) is
656
gen_marked(tee.value)
657
mark_operator(tee)
658
659
_body.op(System.Reflection.Metadata.ILOpCode.DUP)
660
_body.stloc(tee.local)
661
si
662
663
visit(discard: Values.DISCARD) is
664
if let value = discard.value then
665
gen_marked(value)
666
fi
667
668
mark_operator(discard)
669
670
_body.op(System.Reflection.Metadata.ILOpCode.POP)
671
si
672
673
visit(thrown: Values.THROW) is
674
if let exception = thrown.exception then
675
gen_marked(exception)
676
fi
677
678
mark_operator(thrown)
679
680
_body.op(System.Reflection.Metadata.ILOpCode.THROW)
681
si
682
683
visit(ret: Values.RET) is
684
_body.ret()
685
si
686
687
// A fresh local of the return type, never assigned.
688
visit(default_return: Values.DEFAULT_RETURN) is
689
let local = IR.LOCAL(".default", default_return.type)
690
691
_body.declare_local(local)
692
_body.ldloc(local)
693
si
694
695
// The default of a type is what a fresh local of it holds.
696
visit(default_value: Values.DEFAULT) is
697
let local = IR.LOCAL(".default.{IR.TEMP.get_next_id()}", default_value.type)
698
699
_body.declare_local(local)
700
_body.ldloc(local)
701
si
702
703
visit_address(default_value: Values.DEFAULT) is
704
let local = IR.LOCAL(".default.{IR.TEMP.get_next_id()}", default_value.type)
705
706
_body.declare_local(local)
707
_body.ldloca(local)
708
si
709
710
visit(deref: Values.DEREF) is
711
gen(deref.value)
712
_body.load_object(_type_token(deref.type))
713
si
714
715
visit_address(deref: Values.DEREF) is
716
gen(deref.value)
717
si
718
719
// A value with no address of its own is copied into a local
720
// whose address is taken instead.
721
visit(address: Values.ADDRESS) is
722
let value = address.value
723
724
if value.has_address then
725
gen_address(value)
726
727
return
728
fi
729
730
if !address.local? then
731
address.local = IR.LOCAL(Values.ADDRESS.next_name, address.type)
732
fi
733
734
let local = address.local!
735
736
_body.declare_local(local)
737
gen(value)
738
_body.stloc(local)
739
_body.ldloca(local)
740
si
741
742
visit_address(address: Values.ADDRESS) is
743
visit(address)
744
si
745
746
visit(steps: Values.STEPS) is
747
_lower_all(steps.steps)
748
si
749
750
visit(decrement: Values.DECREMENT) is
751
_body.ldloc(decrement.counter)
752
_body.ldc_i4(1)
753
_body.op(System.Reflection.Metadata.ILOpCode.SUB)
754
_body.stloc(decrement.counter)
755
si
756
757
// Leaves the counter's value before the increment.
758
visit(increment: Values.POST_INCREMENT) is
759
_body.ldloc(increment.counter)
760
_body.ldloc(increment.counter)
761
_body.ldc_i4(1)
762
_body.op(System.Reflection.Metadata.ILOpCode.ADD)
763
_body.stloc(increment.counter)
764
si
765
766
// Leaves the counter's value after the decrement.
767
visit(decrement: Values.PRE_DECREMENT) is
768
_body.ldloc(decrement.counter)
769
_body.ldc_i4(1)
770
_body.op(System.Reflection.Metadata.ILOpCode.SUB)
771
_body.op(System.Reflection.Metadata.ILOpCode.DUP)
772
_body.stloc(decrement.counter)
773
si
774
775
visit(repack: Values.TUPLE_REPACK) is
776
_body.declare_local(repack.temp)
777
gen(repack.source)
778
_body.stloc(repack.temp)
779
gen(repack.built)
780
si
781
782
// The subject is already on the stack, left by the wrap around
783
// the arm this value is built for.
784
visit(spilled: Values.SPILLED_SUBJECT) is
785
_body.declare_local(spilled.temp)
786
_body.stloc(spilled.temp)
787
gen(spilled.built)
788
si
789
790
visit(address: Values.ARRAY_ELEMENT_ADDRESS) is
791
gen(address.array)
792
gen(address.index)
793
_body.load_element_address(_type_token(address.element_type))
794
si
795
796
visit_address(address: Values.ARRAY_ELEMENT_ADDRESS) is
797
visit(address)
798
si
799
800
visit(array: Values.NEW_ARRAY) is
801
gen(array.count)
802
_body.new_array(_type_token(array.element_type))
803
si
804
805
visit(store: Values.STORE_ARRAY_ELEMENT) is
806
gen(store.array)
807
gen(store.index)
808
gen(IoC.CONTAINER.instance.value_boxer.box_if_needed(store.element, store.element_type))
809
_body.store_element(_type_token(store.element_type))
810
si
811
812
visit(sequence: Values.SEQUENCE) is
813
let boxer = IoC.CONTAINER.instance.value_boxer
814
let element = _type_token(sequence.element_type)
815
let values = sequence.values
816
817
_body.ldc_i4(values.count)
818
_body.new_array(element)
819
820
for i in 0..values.count do
821
_body.op(System.Reflection.Metadata.ILOpCode.DUP)
822
_body.ldc_i4(i)
823
824
gen(boxer.box_if_needed(values[i], sequence.element_type))
825
826
_body.store_element(element)
827
od
828
si
829
830
visit(compare: Values.COMPARE_ORDER_TO_ZERO) is
831
let (op_code, negates) = CIL_OPERATIONS.comparison(compare.comparison, false)
832
833
gen(compare.value)
834
835
_body.ldc_i4(0)
836
_body.op(op_code)
837
838
if negates then
839
_body.ldc_i4(0)
840
_body.op(System.Reflection.Metadata.ILOpCode.CEQ)
841
fi
842
si
843
844
visit(receiver: Values.CONSTRAINED) is
845
gen(receiver.value)
846
si
847
848
// The `constrained.` prefix a call through `receiver` takes,
849
// immediately before its `callvirt`.
850
constrained_prefix(receiver: Values.CONSTRAINED) is
851
_body.constrained(_type_token(receiver.type!))
852
si
853
854
// Decimal's operators are its own static members.
855
visit(arithmetic: Values.DECIMAL_ARITHMETIC) is
856
for operand in arithmetic.operands do
857
gen_marked(operand)
858
od
859
860
mark_operator(arithmetic)
861
862
_call_decimal(CIL_OPERATIONS.decimal_arithmetic(arithmetic.operation), arithmetic.operands.count, arithmetic.type)
863
si
864
865
visit(compare: Values.DECIMAL_COMPARE) is
866
gen_marked(compare.left)
867
gen_marked(compare.right)
868
869
mark_operator(compare)
870
871
_call_decimal(CIL_OPERATIONS.decimal_comparison(compare.comparison), 2, compare.type)
872
si
873
874
_call_decimal(method_name: string, operand_count: int, return_type: Semantic.Types.Type) is
875
let decimal_type = IoC.CONTAINER.instance.innate_symbol_lookup.get_decimal_type()
876
let parameter_types = Collections.LIST[Semantic.Types.Type]()
877
878
for i in 0..operand_count do
879
parameter_types.add(decimal_type)
880
od
881
882
_body.call(_context.resolve_decimal_method(method_name, parameter_types, return_type))
883
si
884
885
visit(convert: Values.DECIMAL_CONVERT) is
886
gen(convert.value)
887
888
let conversion = IoC.CONTAINER.instance.value_converter.get_decimal_conversion(convert.from, convert.type)!
889
890
let parameter_types = Collections.LIST[Semantic.Types.Type]()
891
892
parameter_types.add(conversion.parameter_type)
893
894
_body.call(_context.resolve_decimal_method(CIL_OPERATIONS.decimal_conversion(conversion.is_widening), parameter_types, convert.type))
895
si
896
897
visit(equality: Values.EQUALITY_COMPARISON) is
898
_body.call(_context.resolve_equality_comparer_default(equality.comparer_type!))
899
900
gen(equality.left)
901
gen(equality.right)
902
903
_body.call_virtual(_context.resolve_equality_comparer_equals(equality.comparer_type!))
904
si
905
906
visit(test: Values.HAS_VALUE) is
907
gen(test.value)
908
909
_body.ldnull()
910
_body.op(System.Reflection.Metadata.ILOpCode.CGT_UN)
911
si
912
913
// Calling GetType through the reference faults on null, and
914
// nothing else; the reference itself is left in place.
915
visit(check: Values.REFERENCE_NULL_CHECK) is
916
gen(check.inner)
917
918
_body.op(System.Reflection.Metadata.ILOpCode.DUP)
919
_body.call_virtual(_context.resolve_object_get_type())
920
_body.op(System.Reflection.Metadata.ILOpCode.POP)
921
si
922
923
visit(conversion: Values.SCALAR_CONVERSION) is
924
gen(conversion.converted)
925
si
926
927
visit(dummy: Values.DUMMY) is
928
IoC.CONTAINER.instance.logger.poison(dummy.location, "generated dummy value: {dummy.type} from {dummy.location}")
929
_context.fixme("dummy {dummy.type} from {dummy.location}")
930
si
931
932
visit(type_expression: Values.TYPE_EXPRESSION) is
933
IoC.CONTAINER.instance.logger.poison(type_expression.location, "generated type expression: {type_expression.type} from {type_expression.location}")
934
_context.fixme("type expression {type_expression.type} from {type_expression.location}")
935
si
936
937
visit(store: Values.Need.STORE) is
938
gen(store.value)
939
940
assert false else "unresolved need store"
941
si
942
943
visit(store: Values.Store.INDIRECT) is
944
gen(store.address)
945
gen(store.value)
946
_body.store_object(_type_token(store.type))
947
si
948
949
visit(wrapper: Values.TYPE_WRAPPER) is
950
gen(wrapper.value)
951
si
952
953
visit_address(wrapper: Values.TYPE_WRAPPER) is
954
gen_address(wrapper.value)
955
si
956
957
visit(wrapper: Values.WRAPPER) is
958
assert wrapper.value? else "generate wrapper without value"
959
960
gen(wrapper.value)
961
si
962
963
visit_address(wrapper: Values.WRAPPER) is
964
assert wrapper.value? else "generate wrapper without value"
965
966
gen_address(wrapper.value)
967
si
968
969
visit(wrap: Values.WRAP_OPTIONAL) is
970
gen(wrap.value)
971
_body.new_object(_context.resolve_generic_constructor(wrap.type, 1))
972
si
973
974
visit(projection: Values.NARROW_PROJECT) is
975
gen(projection.projected)
976
977
_narrow_guard(projection.type)
978
si
979
980
visit(view: Values.NARROW_VIEW) is
981
gen(view.underlying)
982
983
_narrow_guard(view.type)
984
si
985
986
visit_address(view: Values.NARROW_VIEW) is
987
gen_address(view.underlying)
988
si
989
990
// Records the offset range the marked statement emits. The range
991
// leaves the method as a custom attribute the IL test runner
992
// reads. Outside a body there is no range to record: a pragma on
993
// a declaration marks nothing this can measure.
994
visit(enter: Values.ENTER_FILE) is
995
if let body = _context.current_srm_body_emitter then
996
body.begin_il_output(enter.path, _context.next_il_output_sequence())
997
fi
998
si
999
1000
visit(leave: Values.LEAVE_FILE) is
1001
if let body = _context.current_srm_body_emitter then
1002
body.end_il_output()
1003
fi
1004
si
1005
1006
// A delegate over the function pointer, constructed with its
1007
// receiver. ldvirtftn consumes the receiver it dispatches on, so
1008
// a virtual one loads it twice.
1009
visit(closure: Values.MAKE_CLOSURE) is
1010
gen(closure.receiver)
1011
1012
let target = _context.resolve_call_target(closure.function)
1013
1014
if closure.is_virtual then
1015
_body.op(System.Reflection.Metadata.ILOpCode.DUP)
1016
_body.load_virtual_function_pointer(target)
1017
else
1018
_body.load_function_pointer(target)
1019
fi
1020
1021
_body.new_object(_context.resolve_delegate_constructor(closure.type))
1022
si
1023
1024
visit(block: Values.BLOCK) is
1025
assert !block.is_emitted else "oops: block value already emitted"
1026
1027
block.is_emitted = true
1028
1029
for value in block.values do
1030
gen_marked(value)
1031
od
1032
si
1033
1034
visit(block: Values.DUMMY_BLOCK) is
1035
throw System.InvalidOperationException("oops: dummy block generated, created at: {block.location} because: {block.why}")
1036
si
1037
1038
visit(self_load: Values.Load.REFERENCE_SELF) is
1039
_body.ldarg(0)
1040
si
1041
1042
visit_address(self_load: Values.Load.REFERENCE_SELF) is
1043
_body.ldarg(0)
1044
si
1045
1046
// A closure's frame holds the self of the function it was made in.
1047
visit(self_load: Values.Load.OUTER_SELF) is
1048
_body.ldarg(0)
1049
_body.ldfld(_context.resolve_field_target(self_load.outer_self_field))
1050
si
1051
1052
visit_address(self_load: Values.Load.OUTER_SELF) is
1053
visit(self_load)
1054
si
1055
1056
// A struct's own `self` is its address; the value is loaded from it.
1057
visit(self_load: Values.Load.VALUE_SELF) is
1058
_body.ldarg(0)
1059
_body.load_object(_type_token(self_load.type!))
1060
si
1061
1062
visit_address(self_load: Values.Load.VALUE_SELF) is
1063
_body.ldarg(0)
1064
si
1065
1066
// Each runtime operation is a member of the .NET runtime or of
1067
// ghūl's own runtime library.
1068
visit(runtime: Values.RUNTIME) is
1069
if runtime.is_marked_as_call then
1070
gen(_context.string_builder.lowered(runtime))
1071
1072
return
1073
fi
1074
1075
for operand in runtime.operands do
1076
gen_marked(operand)
1077
od
1078
1079
mark_operator(runtime)
1080
1081
case runtime.operation
1082
when IR.RuntimeOperation.CONCAT_STRINGS then
1083
_body.call(_context.resolve_string_concat())
1084
when IR.RuntimeOperation.STRINGS_EQUAL then
1085
_body.call(_context.resolve_string_equality())
1086
when IR.RuntimeOperation.NEW_NOT_IMPLEMENTED then
1087
_body.new_object(_context.resolve_not_implemented_constructor())
1088
when IR.RuntimeOperation.NEW_NOT_SUPPORTED then
1089
_body.new_object(_context.resolve_not_supported_constructor())
1090
when IR.RuntimeOperation.NEW_ASSERT_FAILED then
1091
_body.new_object(_context.resolve_assert_failed_constructor())
1092
when IR.RuntimeOperation.INIT_OBJECT then
1093
_body.call(_context.resolve_object_constructor())
1094
when IR.RuntimeOperation.CURRENT_THREAD_ID then
1095
_body.call(_context.resolve_current_managed_thread_id())
1096
when IR.RuntimeOperation.JOIN_PIPE then
1097
_body.call(_context.resolve_pipe_join(runtime.type_argument!))
1098
when IR.RuntimeOperation.NEW_RANGE then
1099
_body.new_object(_context.resolve_range_constructor(runtime.type!))
1100
else
1101
throw System.NotImplementedException("no lowering for runtime operation {runtime.operation}")
1102
esac
1103
si
1104
1105
visit(creation: Values.CREATE_UNIT_VARIANT_INSTANCE) is
1106
_body.new_object(_context.resolve_call_target(creation.constructor))
1107
_body.stsfld(_context.resolve_unit_variant_instance(creation.variant))
1108
si
1109
1110
// The emitter reserves the same depth for every body it writes,
1111
// so nothing is encoded for this.
1112
visit(max_stack: Values.MAX_STACK) is
1113
si
1114
1115
visit(equality: Values.REFERENCE_EQUALITY) is
1116
gen(equality.left)
1117
gen(equality.right)
1118
_body.op(System.Reflection.Metadata.ILOpCode.CEQ)
1119
si
1120
1121
visit(construct: Values.CONSTRUCT_MAYBE) is
1122
gen(construct.value)
1123
_body.new_object(_context.resolve_generic_constructor(construct.type, 1))
1124
si
1125
1126
// Stands for a value an earlier step left on the stack, so there
1127
// is nothing to encode.
1128
visit(stack_top: Values.STACK_TOP) is
1129
si
1130
1131
visit(stack_top: Values.STACK_TOP_ADDRESS) is
1132
si
1133
1134
visit_address(stack_top: Values.STACK_TOP_ADDRESS) is
1135
si
1136
1137
visit(launch: Values.ASYNC_METHOD_LAUNCH) is
1138
_async.launch(launch)
1139
si
1140
1141
visit(completion: Values.ASYNC_COMPLETION) is
1142
_async.complete(completion)
1143
si
1144
1145
visit(completed: Values.COMPLETED_TASK_LIKE) is
1146
_async.completed(completed)
1147
si
1148
1149
visit(suspend: Values.AWAIT_SUSPEND) is
1150
_async.suspend(suspend)
1151
si
1152
1153
visit(step: Values.AWAIT_SUSPEND_STEP) is
1154
_async.step(step)
1155
si
1156
si
1157
si