Skip to content
← Back

src/syntax/process/generate-il/generate_il_definitions.ghul

1
namespace Syntax.Process is
2
use IO.Std
3
4
use System.Text.StringBuilder
5
6
use Logging
7
use Trees
8
use Source
9
10
use IR
11
use IR.Values
12
13
use Ghul.Pipes
14
15
16
// Type-level walks: namespace, class, partial and impl blocks, trait, struct, union,
17
// variant, enum, plus bodies, properties, pragmas and test attributes.
18
partial GENERATE_IL is
19
enter_class(symbol: Semantic.Symbols.Classy) is
20
ADDRESS.reset_id()
21
TEMP.reset_id()
22
LABEL.reset_id()
23
24
// A body that fails to emit unwinds to `Classy.walk`, which
25
// restores the symbol table and the diagnostics but knows
26
// nothing about emission state. Without this the abandoned
27
// body is still open when the next class opens, and
28
// everything that class emits is written into it.
29
_backend.abandon_bodies()
30
si
31
32
leave_class(symbol: Semantic.Symbols.Classy) is
33
si
34
35
pre(`namespace: Definitions.NAMESPACE) -> bool => super.pre(`namespace)
36
37
visit(`namespace: Definitions.NAMESPACE) is
38
let context = _symbol_table.current_closure_context
39
40
gen_closures(context.get_closures())
41
42
emit_pending_state_machines()
43
44
super.visit(`namespace)
45
si
46
47
// A `reset` the type never declared has no body walked from a
48
// tree, so its throw is deposited here.
49
_gen_synthesized_reset(symbol: Semantic.Symbols.Classy) is
50
let found = symbol.find_direct("reset")
51
52
let reset =
53
if let group: Semantic.Symbols.FUNCTION_GROUP = found then
54
group.functions
55
|> Ghul.Pipes.find(f => f.throws_not_supported)
56
elif let single: Semantic.Symbols.Function = found /\ single.throws_not_supported then
57
single
58
else
59
null
60
fi
61
62
if let reset0 = reset then
63
64
_backend.begin_body()
65
66
try
67
_backend.write(IR.Values.THROW(IR.Values.RUNTIME(IR.RuntimeOperation.NEW_NOT_SUPPORTED, null)))
68
69
_backend.finish_body(reset0)
70
finally
71
_backend.end_body()
72
yrt
73
fi
74
si
75
76
pre(`class: Definitions.CLASS) -> bool is
77
let symbol = cast Semantic.Symbols.Classy?(symbol_for(`class))!
78
79
enter_class(symbol)
80
81
let owner = cast Semantic.Symbols.Symbol?(symbol.owner)!
82
83
84
enter_scope(`class)
85
86
87
return false
88
si
89
90
visit(`class: Definitions.CLASS) is
91
let symbol = cast Semantic.Symbols.Classy?(symbol_for(`class))!
92
93
let closures = symbol.get_closures()
94
95
96
_gen_synthesized_reset(symbol)
97
98
leave_class(symbol)
99
100
leave_scope(`class)
101
102
gen_closures(closures)
103
104
emit_pending_state_machines()
105
si
106
107
// A `partial` block contributes members to a type declared
108
// elsewhere; the member order collects them onto that type's one
109
// row. The class header (extends/implements/attributes) is the primary
110
// definition's job, so this only walks the block's members; closures
111
// declared here are emitted by visit(partial), after their bodies are
112
// walked.
113
pre(`partial: Definitions.PARTIAL) -> bool is
114
let symbol = cast Semantic.Symbols.Classy?(symbol_for(`partial))!
115
116
enter_class(symbol)
117
118
119
enter_scope(`partial)
120
121
122
return false
123
si
124
125
visit(`partial: Definitions.PARTIAL) is
126
let symbol = cast Semantic.Symbols.Classy?(symbol_for(`partial))!
127
128
// Closures declared in this block are tracked on its injection
129
// scope and emitted here, after the member walk has generated
130
// their bodies - the target's primary definition runs before
131
// this block is walked and would emit them bodyless.
132
let injection = cast Semantic.INJECTION_SCOPE?(scope_for(`partial))
133
let closures = injection?.get_closures()
134
135
136
137
leave_class(symbol)
138
139
leave_scope(`partial)
140
141
gen_closures(closures)
142
143
emit_pending_state_machines()
144
si
145
146
// An `impl` block contributes members to its target exactly as
147
// `partial` does. The interface the target now implements is carried on
148
// the target symbol, so the InterfaceImpl row is written from the
149
// primary definition.
150
pre(`impl: Definitions.IMPL) -> bool is
151
let symbol = cast Semantic.Symbols.Classy?(symbol_for(`impl))!
152
153
enter_class(symbol)
154
155
156
enter_scope(`impl)
157
158
159
return false
160
si
161
162
visit(`impl: Definitions.IMPL) is
163
let symbol = cast Semantic.Symbols.Classy?(symbol_for(`impl))!
164
165
// As visit(partial): closures declared in this block are tracked
166
// on its injection scope and emitted here, after the member walk
167
// has generated their bodies.
168
let injection = cast Semantic.INJECTION_SCOPE?(scope_for(`impl))
169
let closures = injection?.get_closures()
170
171
172
173
leave_class(symbol)
174
175
leave_scope(`impl)
176
177
gen_closures(closures)
178
179
emit_pending_state_machines()
180
si
181
182
pre(`trait: Definitions.TRAIT) -> bool is
183
let symbol = cast Semantic.Symbols.Classy?(symbol_for(`trait))!
184
let owner = cast Semantic.Symbols.Symbol?(symbol.owner)!
185
186
enter_class(symbol)
187
188
189
enter_scope(`trait)
190
191
192
193
return false
194
si
195
196
visit(`trait: Definitions.TRAIT) is
197
let symbol = cast Semantic.Symbols.Classy?(symbol_for(`trait))!
198
199
let closures = symbol.get_closures()
200
201
202
203
leave_class(symbol)
204
205
leave_scope(`trait)
206
207
gen_closures(closures)
208
209
emit_pending_state_machines()
210
si
211
212
pre(`struct: Definitions.STRUCT) -> bool is
213
let symbol = cast Semantic.Symbols.Classy?(symbol_for(`struct))!
214
let owner = cast Semantic.Symbols.Symbol?(symbol.owner)!
215
216
enter_class(symbol)
217
218
219
220
enter_scope(`struct)
221
222
223
return false
224
si
225
226
visit(`struct: Definitions.STRUCT) is
227
let symbol = cast Semantic.Symbols.Classy?(symbol_for(`struct))!
228
229
let closures = symbol.get_closures()
230
231
232
233
_gen_synthesized_reset(symbol)
234
235
leave_class(symbol)
236
237
leave_scope(`struct)
238
239
gen_closures(closures)
240
si
241
242
pre(`union: Definitions.UNION) -> bool is
243
let symbol = cast Semantic.Symbols.Classy?(symbol_for(`union))!
244
245
enter_class(symbol)
246
247
let owner = cast Semantic.Symbols.Symbol?(symbol.owner)!
248
249
250
enter_scope(`union)
251
252
`union.name.walk(self)
253
254
if `union.arguments? then
255
`union.arguments.walk(self)
256
fi
257
258
`union.modifiers.walk(self)
259
260
for member in `union.body |> filter(m => !isa Definitions.VARIANT(m)) do
261
member.walk(self)
262
od
263
264
265
266
267
268
leave_class(symbol)
269
270
leave_scope(`union)
271
272
for member in `union.body |> filter(m => isa Definitions.VARIANT(m)) do
273
member.walk(self)
274
od
275
276
277
return true
278
si
279
280
visit(`union: Definitions.UNION) is
281
// all done in pre
282
si
283
284
pre(variant: Definitions.VARIANT) -> bool is
285
let symbol = cast Semantic.Symbols.Classy?(symbol_for(variant))!
286
287
enter_class(symbol)
288
289
let owner = cast Semantic.Symbols.Symbol?(symbol.owner)!
290
291
292
enter_scope(variant)
293
294
295
return false
296
si
297
298
visit(variant: Definitions.VARIANT) is
299
let symbol = cast Semantic.Symbols.Classy?(symbol_for(variant))!
300
301
let closures = symbol.get_closures()
302
303
gen_unit_variant_singleton(symbol)
304
305
306
leave_class(symbol)
307
308
leave_scope(variant)
309
310
gen_closures(closures)
311
si
312
313
// For a unit variant (no constructor parameters / no
314
// instance fields), emit a static `_instance` field plus a
315
// `.cctor` that allocates exactly one instance. `IR.Values.NEW`
316
// redirects construction sites for unit variants to a `ldsfld`
317
// of this field, so `RED()` (or `NONE[int]()`) returns the
318
// cached singleton instead of allocating each call.
319
gen_unit_variant_singleton(symbol: Semantic.Symbols.Classy) is
320
if let variant_symbol: Semantic.Symbols.VARIANT = symbol then
321
if !variant_symbol.is_unit_variant then
322
return
323
fi
324
325
_without_position(() is _gen_unit_variant_singleton(variant_symbol) si)
326
fi
327
si
328
329
// The initializer that creates a unit variant's shared instance.
330
_gen_unit_variant_singleton(variant: Semantic.Symbols.VARIANT) is
331
if !_backend.has_unit_variant_instance(variant) then
332
return
333
fi
334
335
let constructors = cast Semantic.Symbols.FUNCTION_GROUP?(variant.find_direct("init"))
336
337
if !constructors? \/ constructors.functions.count == 0 then
338
return
339
fi
340
341
_backend.begin_body()
342
_backend.write(_returning(IR.Values.CREATE_UNIT_VARIANT_INSTANCE(variant, constructors.functions[0])))
343
_backend.finish_unit_variant_initializer(variant)
344
_backend.end_body()
345
si
346
347
348
349
// A closure's frame has a constructor no source declares. Its
350
// body is generated in the closure's terms.
351
_gen_closure_frame(closure: Semantic.Symbols.Closure) is
352
if !closure.frame? then
353
return
354
fi
355
356
let frame = closure.frame
357
358
closure.map_type_arguments()
359
closure.set_type_arguments()
360
361
_backend.begin_body()
362
363
try
364
try
365
for value in frame.constructor_body(_symbol_loader) do
366
_backend.write(value)
367
od
368
catch ex: System.Exception
369
_logger.exception(frame.location, ex, "error generating closure frame constructor")
370
yrt
371
372
_backend.write(IR.Values.RET())
373
_backend.finish_body(frame.constructor)
374
finally
375
_backend.end_body()
376
yrt
377
378
closure.unmap_type_arguments()
379
si
380
381
gen_closures(closures: Collections.Iterable[Semantic.Symbols.Closure]?) is
382
if !closures? then
383
return
384
fi
385
386
// A delegate has no frame of its own unless it is cached over one.
387
for closure in closures |> filter(c => !c.is_anon_func /\ (!c.is_delegate \/ c.is_cached_on_frame)) do
388
_gen_closure_frame(closure)
389
od
390
391
for closure in closures |> filter(c => c.pack_thunk?) do
392
_gen_pack_thunk(closure)
393
od
394
si
395
396
pre(`enum: Definitions.ENUM) -> bool is
397
let symbol = cast Semantic.Symbols.Symbol?(symbol_for(`enum))!
398
399
let owner = cast Semantic.Symbols.Symbol?(symbol.owner)!
400
401
402
enter_scope(`enum)
403
404
405
return false
406
si
407
408
visit(`enum: Definitions.ENUM) is
409
410
leave_scope(`enum)
411
si
412
413
pre(enum_member: Definitions.ENUM_MEMBER) -> bool is
414
return false
415
si
416
pre(body: Bodies.BLOCK) -> bool is
417
add(Values.MAX_STACK(64))
418
419
return super.pre(body)
420
si
421
422
pre(expression_body: Bodies.EXPRESSION) -> bool is
423
add(Values.MAX_STACK(64))
424
425
_expression_body_regions.add(
426
if let disposals = expression_body.let_in_disposals then
427
_open_disposal_region(disposals)
428
else
429
null
430
fi
431
)
432
433
return super.pre(expression_body)
434
si
435
436
visit(expression_body: Bodies.EXPRESSION) is
437
// An `=> body` whose function awaits is emitted after this
438
// walk, by the state machine's own emitter, which puts its
439
// value in the frame's result. Nothing is emitted here: the
440
// `ret` below would return out of MoveNext without ever
441
// completing the builder.
442
if let casm = _current_async_state_machine, function = current_function then
443
if function == casm.function then
444
super.visit(expression_body)
445
446
return
447
fi
448
fi
449
450
let region = _expression_body_regions[_expression_body_regions.count - 1]
451
452
_expression_body_regions.remove_at(_expression_body_regions.count - 1)
453
454
if let disposals = expression_body.let_in_disposals, open = region then
455
_gen_disposing_expression_body(expression_body, disposals, open)
456
457
super.visit(expression_body)
458
459
return
460
fi
461
462
let body_value = expression_body.expression.value
463
if let value = body_value, value_type = value.type then
464
let return_type = current_function!.return_type!
465
466
// A void body discards whatever its expression leaves
467
// standing, the same as a block body's tail: emit it
468
// for its effects, then pop what it pushed.
469
if return_type.is_void /\ !value_type.is_void then
470
add(Values.DISCARD(value))
471
else
472
add(_boxer.box_if_needed(value, return_type))
473
fi
474
elif let
475
self.current_function? /\
476
current_function.return_type? /\
477
!current_function.return_type.is_void
478
then
479
// Diverging body (`=> throw E`): the body's own IL
480
// already terminates control flow. Emit a default-value
481
// trailer so the unreachable fall-off is verifiable,
482
// matching statement-bodied functions that end in throw.
483
add(Values.DEFAULT_RETURN(current_function.return_type!))
484
fi
485
486
add(Values.RET())
487
488
super.visit(expression_body)
489
si
490
491
// An expression body holding `let use ... in` locals: the value is
492
// left in a temporary inside the disposal region, since a region
493
// is left with `leave`, which empties the stack, and returned once
494
// the region is behind it.
495
_gen_disposing_expression_body(
496
expression_body: Bodies.EXPRESSION,
497
disposals: Collections.List[Semantic.Symbols.Variable],
498
region: DISPOSAL_REGION
499
) is
500
let return_type = current_function!.return_type!
501
let result: TEMP? mut = null
502
503
if let value = expression_body.expression.value, value_type = value.type then
504
if return_type.is_void then
505
if value_type.is_void then
506
add(value)
507
else
508
add(Values.DISCARD(value))
509
fi
510
else
511
let temp = TEMP(current_block, "body", return_type)
512
513
temp.store(_boxer.box_if_needed(value, return_type))
514
result = temp
515
fi
516
fi
517
518
_close_disposal_region(region, disposals, expression_body.location)
519
520
if let r = result then
521
add(r.load())
522
elif !return_type.is_void then
523
add(Values.DEFAULT_RETURN(return_type))
524
fi
525
526
add(Values.RET())
527
si
528
529
pre(property: Definitions.PROPERTY) -> bool is
530
return true
531
si
532
533
pre(pragma: Definitions.PRAGMA) -> bool is
534
process_pragma(pragma.pragma, true)
535
return false
536
si
537
538
visit(pragma: Definitions.PRAGMA) is
539
process_pragma(pragma.pragma, false)
540
si
541
542
process_pragma(pragma: Pragmas.PRAGMA, is_enter: bool) is
543
let name = pragma.name.to_string()
544
545
if name =~ IL_PRAGMAS.OUTPUT then
546
if pragma.arguments.expressions.count != 1 then
547
_logger.error(pragma.arguments.location, "expected 1 argument")
548
return
549
fi
550
551
let argument = pragma.arguments.expressions[0]
552
553
if !isa Expressions.Literals.STRING(argument) then
554
_logger.error(pragma.arguments.location, "expected a string literal argument")
555
return
556
fi
557
558
let file_name = argument.value_string
559
560
if is_enter then
561
_il_output_depth = _il_output_depth + 1
562
563
// Only a statement inside a body has instructions to
564
// measure; a pragma on a declaration marks nothing.
565
if _block_context.is_in_block then
566
current_block.add(Values.ENTER_FILE(file_name))
567
fi
568
else
569
_il_output_depth = _il_output_depth - 1
570
571
if _block_context.is_in_block then
572
current_block.add(Values.LEAVE_FILE(file_name))
573
fi
574
fi
575
fi
576
si
577
578
si
579
si