Skip to content
← Back

src/ir/emitter/srm_signature_encoder.ghul

1
@@IF.dotnet()
2
3
namespace IR.Emitter is
4
use System.Reflection.Metadata.BlobBuilder
5
use System.Reflection.Metadata.EntityHandle
6
use System.Reflection.Metadata.SignatureCallingConvention
7
use System.Reflection.Metadata.Ecma335.BlobEncoder
8
use System.Reflection.Metadata.Ecma335.MethodSignatureEncoder
9
use System.Reflection.Metadata.Ecma335.ReturnTypeEncoder
10
use System.Reflection.Metadata.Ecma335.ParametersEncoder
11
use System.Reflection.Metadata.Ecma335.SignatureTypeEncoder
12
13
// Encodes a function's argument and return types as an ECMA-335
14
// signature blob.
15
//
16
// The shape mirrors Symbols.Classy.gen_type's decision tree — the
17
// built-in type short-circuit, then a class-versus-valuetype
18
// split, then generic instantiation — with metadata handles in place
19
// of the text that path appends. Keeping the two in step matters:
20
// a methodref whose signature disagrees with the target's own
21
// declaration resolves to nothing at run time.
22
class SRM_SIGNATURE_ENCODER(_assembly: SRM_ASSEMBLY_EMITTER) is
23
// The innate string and object symbols, resolved once: every
24
// signature the assembly emits passes through encode_type.
25
_string_symbol: Semantic.Symbols.Symbol?
26
_object_symbol: Semantic.Symbols.Symbol?
27
28
string_symbol: Semantic.Symbols.Symbol is
29
if !_string_symbol? then
30
_string_symbol = (cast Semantic.Types.NAMED(IoC.CONTAINER.instance.innate_symbol_lookup.get_string_type())).symbol
31
fi
32
33
return _string_symbol
34
si
35
36
object_symbol: Semantic.Symbols.Symbol is
37
if !_object_symbol? then
38
_object_symbol = (cast Semantic.Types.NAMED(IoC.CONTAINER.instance.innate_symbol_lookup.get_object_type())).symbol
39
fi
40
41
return _object_symbol
42
si
43
44
// The closure to install a mapping from while a member of its
45
// frame is encoded, or null for a member that needs none.
46
//
47
// A member's signature is written in its owner's terms, and for
48
// a closure frame that does not happen on its own: the frame's
49
// type parameters are the enclosing function's symbols, which
50
// carry their own method-level position until the closure
51
// installs the frame's class-level one. A body is emitted inside
52
// that window, so its local signature comes out right unaided.
53
// These blobs are not: the structure walk builds them long after
54
// the window has closed, and a call site builds them from
55
// wherever it happens to be. Without reopening it, a field of
56
// `T` encodes as `!!0` on a type whose one parameter is `!0`.
57
//
58
// The reference the member hangs off is deliberately outside
59
// this: it names the frame at the arguments the *caller* passes,
60
// which are the caller's own parameters and are spelled where
61
// the caller stands.
62
// As frame_mapping, but for the member a body is being emitted
63
// into rather than a member whose own signature is being written,
64
// so a null member (outside any) answers null rather than faulting.
65
frame_mapping_for_emission(member: Semantic.Symbols.Symbol?) -> Semantic.Symbols.Closure? static is
66
if member? then
67
return frame_mapping(member)
68
fi
69
70
return null
71
si
72
73
frame_mapping(member: Semantic.Symbols.Symbol) -> Semantic.Symbols.Closure? static is
74
// The closure's own method is emitted as a member of its
75
// frame, but it is declared into the function the literal
76
// was written in, so its owner does not name the frame. A
77
// frameless closure needs the mapping too when it captures
78
// type arguments: its method-level type parameters are
79
// numbered in capture order, so its signature must be
80
// written in that space rather than the enclosing
81
// function's declaration order.
82
if let closure: Semantic.Symbols.Closure = member then
83
if closure.frame? \/ closure.captured_type_arguments? then
84
return closure
85
fi
86
fi
87
88
if let frame: Semantic.Symbols.FRAME = member.owner then
89
return frame.closure
90
fi
91
92
// The frame's own rows need it too: its type parameters are
93
// the enclosing function's symbols, so a bound naming one
94
// encodes at that symbol's method-level position unless the
95
// frame's class-level one is installed.
96
if let frame: Semantic.Symbols.FRAME = member then
97
return frame.closure
98
fi
99
100
return null
101
si
102
103
// A property's signature is its accessors' shape rather than its
104
// own: the calling convention says PROPERTY, and an instance
105
// property sets HASTHIS to match the getter it is read through.
106
// ghūl declares no indexed properties, so the parameter list is
107
// always empty and the signature carries only the type.
108
property_signature(property: Semantic.Symbols.Property) -> ubyte[] is
109
return property_signature(property.type!, _is_instance_property(property))
110
si
111
112
property_signature(`type: Semantic.Types.Type, is_instance: bool) -> ubyte[] is
113
let builder = BlobBuilder(16)
114
115
let return_type: ReturnTypeEncoder mut
116
let parameters: ParametersEncoder mut
117
118
BlobEncoder(builder)
119
.property_signature(is_instance)
120
.parameters(0, return_type ref, parameters ref)
121
122
encode_type(return_type.`type(false), `type)
123
124
return builder.to_array()
125
si
126
127
_is_instance_property(property: Semantic.Symbols.Property) -> bool static =>
128
isa Semantic.Symbols.INSTANCE_PROPERTY(property)
129
130
field_signature(`field: Semantic.Symbols.Field) -> ubyte[] is
131
let closure = frame_mapping(`field)
132
133
if closure? then
134
closure.map_type_arguments()
135
fi
136
137
try
138
let builder = BlobBuilder(16)
139
140
// Same recovery as for a method signature: a field
141
// inherited from a constructed-generic base in another
142
// assembly surfaces via reflection with its type
143
// parameter substituted, and the open form is carried as
144
// unspecialized_type. Prefer it when present.
145
encode_type(
146
BlobEncoder(builder).field_signature(),
147
`field.unspecialized_type ?? `field.type!)
148
149
return builder.to_array()
150
finally
151
if closure? then
152
closure.unmap_type_arguments()
153
fi
154
yrt
155
si
156
157
// The same, for a field the back end synthesises and so has no
158
// symbol to read a type off.
159
field_signature(type: Semantic.Types.Type) -> ubyte[] is
160
let builder = BlobBuilder(16)
161
162
encode_type(BlobEncoder(builder).field_signature(), type)
163
164
return builder.to_array()
165
si
166
167
// A static constructor's signature: static, void, no arguments.
168
static_initializer_signature() -> ubyte[] is
169
let builder = BlobBuilder(8)
170
171
let return_type: ReturnTypeEncoder mut
172
let parameters: ParametersEncoder mut
173
174
BlobEncoder(builder)
175
.method_signature(SignatureCallingConvention.DEFAULT, 0, false)
176
.parameters(0, return_type ref, parameters ref)
177
178
return_type.void()
179
180
return builder.to_array()
181
si
182
183
// An instance method taking nothing and returning the given
184
// reference type, named by handle rather than by symbol: the
185
// members that need this satisfy a framework interface no ghūl
186
// declaration mentions.
187
nullary_instance_signature(return_type: EntityHandle) -> ubyte[] is
188
let builder = BlobBuilder(16)
189
190
let return_encoder: ReturnTypeEncoder mut
191
let parameters: ParametersEncoder mut
192
193
BlobEncoder(builder)
194
.method_signature(SignatureCallingConvention.DEFAULT, 0, true)
195
.parameters(0, return_encoder ref, parameters ref)
196
197
return_encoder.`type(false).`type(return_type, false)
198
199
return builder.to_array()
200
si
201
202
// The same, returning `object`, which has its own element-type
203
// code rather than a reference.
204
nullary_instance_signature_returning_object() -> ubyte[] is
205
let builder = BlobBuilder(16)
206
207
let return_encoder: ReturnTypeEncoder mut
208
let parameters: ParametersEncoder mut
209
210
BlobEncoder(builder)
211
.method_signature(SignatureCallingConvention.DEFAULT, 0, true)
212
.parameters(0, return_encoder ref, parameters ref)
213
214
return_encoder.`type(false).object()
215
216
return builder.to_array()
217
si
218
219
// A constructor's signature: an instance method returning void,
220
// taking the given argument types. Built from types rather than
221
// from a Function because the constructors that need this are
222
// the ones with no symbol to read a signature off.
223
constructor_signature(argument_types: Collections.List[Semantic.Types.Type]) -> ubyte[] is
224
let builder = BlobBuilder(16)
225
226
let return_type: ReturnTypeEncoder mut
227
let parameters: ParametersEncoder mut
228
229
BlobEncoder(builder)
230
.method_signature(SignatureCallingConvention.DEFAULT, 0, true)
231
.parameters(argument_types.count, return_type ref, parameters ref)
232
233
return_type.void()
234
235
for argument in argument_types do
236
let referenced = pointee(argument)
237
238
encode_type(parameters.add_parameter().`type(referenced?), referenced ?? argument)
239
od
240
241
return builder.to_array()
242
si
243
244
// A static method's signature, built from types rather than read
245
// off a Function, for the framework members the back end reaches
246
// for by name.
247
static_signature(
248
return_type_of: Semantic.Types.Type,
249
argument_types: Collections.List[Semantic.Types.Type]
250
) -> ubyte[] =>
251
call_signature(return_type_of, argument_types, false)
252
253
// The same, for a method reached through a receiver. Built from
254
// types rather than from a Function because the members that
255
// need it are ones no symbol in the compilation declares.
256
call_signature(
257
return_type_of: Semantic.Types.Type,
258
argument_types: Collections.List[Semantic.Types.Type],
259
is_instance: bool
260
) -> ubyte[] is
261
let builder = BlobBuilder(16)
262
263
let return_type: ReturnTypeEncoder mut
264
let parameters: ParametersEncoder mut
265
266
BlobEncoder(builder)
267
.method_signature(SignatureCallingConvention.DEFAULT, 0, is_instance)
268
.parameters(argument_types.count, return_type ref, parameters ref)
269
270
encode_return(return_type, return_type_of)
271
272
for argument in argument_types do
273
let referenced = pointee(argument)
274
275
encode_type(parameters.add_parameter().`type(referenced?), referenced ?? argument)
276
od
277
278
return builder.to_array()
279
si
280
281
// A constructor on a constructed generic, taking one argument
282
// per type parameter. The arguments are written as indexes for
283
// the same reason Invoke's are: the reference is to the open
284
// type's constructor, and the instantiation lives in the owning
285
// TypeSpec.
286
generic_constructor_signature(argument_count: int) -> ubyte[] is
287
let builder = BlobBuilder(16)
288
289
let return_type: ReturnTypeEncoder mut
290
let parameters: ParametersEncoder mut
291
292
BlobEncoder(builder)
293
.method_signature(SignatureCallingConvention.DEFAULT, 0, true)
294
.parameters(argument_count, return_type ref, parameters ref)
295
296
return_type.void()
297
298
for i in 0..argument_count do
299
parameters.add_parameter().`type(false).generic_type_parameter(i)
300
od
301
302
return builder.to_array()
303
si
304
305
// A delegate's Invoke, whose parameters and return are the
306
// delegate type's own type parameters. They are written as
307
// indexes rather than as the arguments the delegate was
308
// instantiated with: the reference is to Func`N's Invoke, and
309
// the instantiation lives in the owning TypeSpec.
310
delegate_invoke_signature(argument_count: int, is_action: bool) -> ubyte[] is
311
let builder = BlobBuilder(16)
312
313
let return_type: ReturnTypeEncoder mut
314
let parameters: ParametersEncoder mut
315
316
BlobEncoder(builder)
317
.method_signature(SignatureCallingConvention.DEFAULT, 0, true)
318
.parameters(argument_count, return_type ref, parameters ref)
319
320
if is_action then
321
return_type.void()
322
else
323
return_type.`type(false).generic_type_parameter(argument_count)
324
fi
325
326
for i in 0..argument_count do
327
parameters.add_parameter().`type(false).generic_type_parameter(i)
328
od
329
330
return builder.to_array()
331
si
332
333
// The blob a TypeSpec row holds: the constructed type encoded
334
// structurally, exactly as it would appear inside any other
335
// signature.
336
type_specification_signature(type: Semantic.Types.Type) -> ubyte[] is
337
let builder = BlobBuilder(16)
338
339
encode_type(BlobEncoder(builder).type_specification_signature(), type)
340
341
return builder.to_array()
342
si
343
344
method_signature(function: Semantic.Symbols.Function) -> ubyte[] is
345
let closure = frame_mapping(function)
346
347
if closure? then
348
closure.map_type_arguments()
349
fi
350
351
try
352
return _method_signature(function)
353
finally
354
if closure? then
355
closure.unmap_type_arguments()
356
fi
357
yrt
358
si
359
360
// A signature in the member's own specialised types, for a
361
// definition this assembly writes over a member it takes from an
362
// instantiated ancestor. A reference prefers the open form; a
363
// definition has no instantiation to resolve the indexes against.
364
instantiated_method_signature(function: Semantic.Symbols.Function) -> ubyte[] =>
365
_method_signature(function, false)
366
367
_method_signature(function: Semantic.Symbols.Function) -> ubyte[] =>
368
_method_signature(function, true)
369
370
_method_signature(function: Semantic.Symbols.Function, prefer_open: bool) -> ubyte[] is
371
let builder = BlobBuilder(32)
372
373
// A method inherited from a constructed-generic base in
374
// another assembly surfaces via reflection with its type-
375
// parameter references substituted to concrete types, which
376
// the CLR will not match at the call site. The open-generic
377
// form is recovered at import time (symbol_factory) and
378
// carried as unspecialized_*; prefer it when present.
379
let arguments =
380
if prefer_open then function.unspecialized_arguments ?? function.arguments else function.arguments fi
381
382
let return_type: ReturnTypeEncoder mut
383
let parameters: ParametersEncoder mut
384
385
// A generic method's signature declares its arity, and its
386
// own type parameters appear in it as indexes rather than
387
// as the arguments some instantiation supplied.
388
BlobEncoder(builder)
389
.method_signature(
390
SignatureCallingConvention.DEFAULT,
391
function.generic_arguments.count,
392
function.is_emitted_with_receiver)
393
.parameters(arguments.count, return_type ref, parameters ref)
394
395
encode_return(
396
return_type,
397
if prefer_open then function.unspecialized_return_type ?? function.return_type else function.return_type fi)
398
399
for argument in arguments do
400
let referenced = pointee(argument)
401
402
encode_type(parameters.add_parameter().`type(referenced?), referenced ?? argument)
403
od
404
405
return builder.to_array()
406
si
407
408
// What a `T ref` points at, or null when the type is not a
409
// reference.
410
//
411
// The format carries byref as a flag on the slot rather than as
412
// part of the type, so a `T ref` marks its parameter, return or
413
// local and encodes the pointee in its own place. Encoding the
414
// reference type itself instead names a value type the runtime
415
// will not accept where a managed pointer belongs.
416
pointee(type: Semantic.Types.Type?) -> Semantic.Types.Type? static =>
417
if let reference: Semantic.Types.REFERENCE = type then
418
reference.arguments[0]
419
else
420
null
421
fi
422
423
encode_return(encoder: ReturnTypeEncoder, type: Semantic.Types.Type?) is
424
if !type? \/ is_void(type) then
425
encoder.void()
426
427
return
428
fi
429
430
let referenced = pointee(type)
431
432
encode_type(encoder.`type(referenced?), referenced ?? type)
433
si
434
435
is_void(type: Semantic.Types.Type) -> bool static =>
436
type.symbol.built_in_type =~ Semantic.Symbols.BuiltInType.VOID
437
438
encode_type(encoder: SignatureTypeEncoder, type: Semantic.Types.Type) is
439
if let array: Semantic.Types.ARRAY = type then
440
encode_type(encoder.szarray(), array.arguments[0])
441
442
return
443
fi
444
445
// A pointer is its own element type in the format, not a
446
// generic named `POINTER` over one. Encoding it as the
447
// latter names a class the runtime has never heard of, and
448
// the slot then holds an object reference where the code
449
// reading it expects a native integer.
450
if let pointer: Semantic.Types.POINTER = type then
451
encode_type(encoder.pointer(), pointer.arguments[0])
452
453
return
454
fi
455
456
let symbol = type.symbol
457
458
if let generic_argument: Semantic.Symbols.GenericArgument = symbol then
459
// A body emitted somewhere other than where its type
460
// parameters were declared — a state machine's MoveNext,
461
// a closure's frame — reaches them at a position the
462
// lowering assigns, rather than at their own index.
463
if let position = generic_argument.emitted_position then
464
if position.is_class_level then
465
encoder.generic_type_parameter(position.index)
466
else
467
encoder.generic_method_type_parameter(position.index)
468
fi
469
elif isa Semantic.Symbols.FUNCTION_GENERIC_ARGUMENT(generic_argument) then
470
encoder.generic_method_type_parameter(generic_argument.index)
471
else
472
encoder.generic_type_parameter(generic_argument.index)
473
fi
474
475
return
476
fi
477
478
if let built_in = symbol.built_in_type then
479
encode_built_in(encoder, built_in)
480
481
return
482
fi
483
484
// A constructed generic's symbol is a Symbols.GENERIC, which
485
// holds the open definition rather than being one. The
486
// format wants that definition as the TypeDefOrRef, with
487
// the arguments encoded after it, so the specialization is
488
// unwrapped here rather than referenced in its own right.
489
let classy =
490
if let generic: Semantic.Symbols.GENERIC = symbol then
491
generic.symbol
492
else
493
cast Semantic.Symbols.Classy?(symbol)
494
fi
495
496
if !classy? then
497
throw System.NotImplementedException(
498
"the binary back end cannot encode the type '{type}' yet")
499
fi
500
501
// string and object have dedicated element-type codes rather
502
// than a typeref, and are not built-in types — neither is a CLR primitive. A signature spelling
503
// them as a class reference is well-formed, but no other
504
// assembly declares them that way, so a methodref built from
505
// one binds to nothing.
506
if classy == string_symbol then
507
encoder.string()
508
509
return
510
elif classy == object_symbol then
511
encoder.object()
512
513
return
514
fi
515
516
let handle = _type_handle_for(classy)
517
518
let arguments = type.arguments
519
520
if arguments.count > 0 then
521
let generic_arguments =
522
encoder.generic_instantiation(handle, arguments.count, classy.is_value_type)
523
524
for argument in arguments do
525
encode_type(generic_arguments.add_argument(), argument)
526
od
527
528
return
529
fi
530
531
encoder.`type(handle, classy.is_value_type)
532
si
533
534
encode_built_in(encoder: SignatureTypeEncoder, built_in: Semantic.Symbols.BuiltInType) static is
535
case built_in
536
when Semantic.Symbols.BuiltInType.BOOL then encoder.boolean()
537
when Semantic.Symbols.BuiltInType.CHAR then encoder.char()
538
when Semantic.Symbols.BuiltInType.BYTE then encoder.sbyte()
539
when Semantic.Symbols.BuiltInType.UBYTE then encoder.byte()
540
when Semantic.Symbols.BuiltInType.SHORT then encoder.int16()
541
when Semantic.Symbols.BuiltInType.USHORT then encoder.uint16()
542
when Semantic.Symbols.BuiltInType.INT then encoder.int32()
543
when Semantic.Symbols.BuiltInType.UINT then encoder.uint32()
544
when Semantic.Symbols.BuiltInType.LONG then encoder.int64()
545
when Semantic.Symbols.BuiltInType.ULONG then encoder.uint64()
546
when Semantic.Symbols.BuiltInType.WORD then encoder.int_ptr()
547
when Semantic.Symbols.BuiltInType.UWORD then encoder.uint_ptr()
548
when Semantic.Symbols.BuiltInType.SINGLE then encoder.single()
549
when Semantic.Symbols.BuiltInType.DOUBLE then encoder.double()
550
else
551
throw System.NotImplementedException(
552
"the binary back end cannot encode the built-in type {built_in} here")
553
esac
554
si
555
556
// The name the runtime knows a built-in type by, so that a member
557
// reached through one - to_string on an int - names its owner.
558
clr_name_for(built_in: Semantic.Symbols.BuiltInType) -> string static =>
559
case built_in
560
when Semantic.Symbols.BuiltInType.VOID then "System.Void"
561
when Semantic.Symbols.BuiltInType.BOOL then "System.Boolean"
562
when Semantic.Symbols.BuiltInType.CHAR then "System.Char"
563
when Semantic.Symbols.BuiltInType.BYTE then "System.SByte"
564
when Semantic.Symbols.BuiltInType.UBYTE then "System.Byte"
565
when Semantic.Symbols.BuiltInType.SHORT then "System.Int16"
566
when Semantic.Symbols.BuiltInType.USHORT then "System.UInt16"
567
when Semantic.Symbols.BuiltInType.INT then "System.Int32"
568
when Semantic.Symbols.BuiltInType.UINT then "System.UInt32"
569
when Semantic.Symbols.BuiltInType.LONG then "System.Int64"
570
when Semantic.Symbols.BuiltInType.ULONG then "System.UInt64"
571
when Semantic.Symbols.BuiltInType.WORD then "System.IntPtr"
572
when Semantic.Symbols.BuiltInType.UWORD then "System.UIntPtr"
573
when Semantic.Symbols.BuiltInType.SINGLE then "System.Single"
574
when Semantic.Symbols.BuiltInType.DOUBLE then "System.Double"
575
esac
576
577
// A type this assembly defines is named by its own TypeDef row,
578
// not by a reference; every other type resolves to a TypeRef.
579
// The TypeDef row need not exist yet — the numbering pass has
580
// already fixed which row it will be.
581
_type_handle_for(symbol: Semantic.Symbols.Classy) -> EntityHandle is
582
if let definition = _assembly.handles.type_definition(symbol) then
583
return cast EntityHandle(definition)
584
fi
585
586
return cast EntityHandle(type_reference_for(symbol))
587
si
588
589
type_reference_for(symbol: Semantic.Symbols.Classy) -> System.Reflection.Metadata.TypeReferenceHandle is
590
let assembly_name = symbol.il_assembly_name
591
592
// An empty name is as absent as a null one, and worse to
593
// act on: it yields a reference row the runtime resolves to
594
// the assembly being built, so the type appears to be one
595
// this assembly defines and fails to load.
596
if !assembly_name? \/ assembly_name.length == 0 then
597
throw System.NotImplementedException(
598
"the binary back end cannot yet reference '{symbol.qualified_name}', "
599
"a type defined in the assembly being built")
600
fi
601
602
_assembly.add_assembly_reference(assembly_name)
603
604
return reference_for_clr_name(
605
symbol,
606
assembly_name,
607
symbol.il_name_override ?? symbol.qualified_name)
608
si
609
610
// One CLR type name to one reference row, for both places that
611
// need it: a type named in a signature, and the owner a member
612
// reference hangs off. Each used to split the name itself, and
613
// only one of the two would ever have learned about nesting.
614
//
615
// A nested type is written `Outer/Inner`, and each step is a
616
// reference of its own scoped to the one before it, with only
617
// the outermost carrying a namespace. Flattened into a single
618
// row the name resolves to nothing, and the failure surfaces at
619
// load rather than at emission.
620
reference_for_clr_name(
621
symbol: Semantic.Symbols.Symbol,
622
assembly_name: string,
623
clr_type_name: string
624
) -> System.Reflection.Metadata.TypeReferenceHandle is
625
let nested = clr_type_name.index_of('/')
626
627
if nested >= 0 then
628
let outer_name = clr_type_name.substring(0, nested)
629
let outer_dot = outer_name.last_index_of('.')
630
631
let enclosing mut =
632
_assembly.add_type_reference_by_name(
633
assembly_name,
634
if outer_dot >= 0 then outer_name.substring(0, outer_dot) else "" fi,
635
if outer_dot >= 0 then outer_name.substring(outer_dot + 1) else outer_name fi)
636
637
for step in clr_type_name.substring(nested + 1).split(['/']) do
638
enclosing =
639
_assembly.add_nested_type_reference(assembly_name, enclosing, step)
640
od
641
642
return enclosing
643
fi
644
645
let dot = clr_type_name.last_index_of('.')
646
647
return _assembly.add_type_reference(
648
symbol,
649
assembly_name,
650
if dot >= 0 then clr_type_name.substring(0, dot) else "" fi,
651
if dot >= 0 then clr_type_name.substring(dot + 1) else clr_type_name fi)
652
si
653
si
654
si