Skip to content
← Back

src/ir/emitter/srm_attribute_blob_encoder.ghul

1
@@IF.dotnet()
2
3
namespace IR.Emitter is
4
use Collections
5
6
use System.Reflection.Metadata.BlobBuilder
7
8
use Semantic.Types.Type
9
10
// Encodes a custom attribute's value blob: ECMA-335 II.23.3.
11
//
12
// The bytes are encoded from the resolved arguments the attribute
13
// carries, never from any rendering of them: a rendering read back
14
// as data would be taking presentation for fact, and would drift
15
// the first time the rendering changed.
16
//
17
// Every fixed argument is encoded against its parameter's declared
18
// type rather than against the value's own, because the blob has no
19
// room to say which type it meant: the reader walks the constructor
20
// signature alongside the bytes. The one exception is a parameter
21
// typed `object`, whose value is preceded by a tag naming the type
22
// it was boxed from.
23
class SRM_ATTRIBUTE_BLOB_ENCODER is
24
// ECMA-335 II.23.1.16 element type codes, as far as a value blob
25
// can carry them.
26
BOOLEAN: ubyte static => 2ub
27
CHAR: ubyte static => 3ub
28
I1: ubyte static => 4ub
29
U1: ubyte static => 5ub
30
I2: ubyte static => 6ub
31
U2: ubyte static => 7ub
32
I4: ubyte static => 8ub
33
U4: ubyte static => 9ub
34
I8: ubyte static => 10ub
35
U8: ubyte static => 11ub
36
R4: ubyte static => 12ub
37
R8: ubyte static => 13ub
38
STRING: ubyte static => 14ub
39
SZARRAY: ubyte static => 29ub
40
41
// II.23.3's own additions, which exist only inside a value blob.
42
TYPE: ubyte static => 80ub
43
BOXED: ubyte static => 81ub
44
FIELD: ubyte static => 83ub
45
PROPERTY: ubyte static => 84ub
46
ENUM: ubyte static => 85ub
47
48
_lookup: Semantic.Lookups.InnateSymbolLookup
49
50
init(lookup: Semantic.Lookups.InnateSymbolLookup) is
51
super.init()
52
53
_lookup = lookup
54
si
55
56
// The whole blob for an attribute application, or null if any
57
// argument is not a constant this encoder can express.
58
//
59
// The resolver has already rejected an inexpressible argument
60
// with a diagnostic, so a null here means the two disagree about
61
// what is expressible rather than that the source was bad.
62
encode(attribute: Semantic.CUSTOM_ATTRIBUTE) -> ubyte[]? is
63
let buffer = BlobBuilder(32)
64
65
buffer.write_u_int16(1us)
66
67
let parameters = attribute.constructor.arguments
68
let positional = attribute.positional
69
70
for i in 0..parameters.count do
71
if i >= positional.count then
72
return null
73
fi
74
75
if !_write_fixed(buffer, parameters[i], positional[i]) then
76
return null
77
fi
78
od
79
80
let named = attribute.named
81
82
buffer.write_u_int16(cast ushort(named.count))
83
84
for argument in named do
85
if !_write_named(buffer, argument) then
86
return null
87
fi
88
od
89
90
return buffer.to_array()
91
si
92
93
// A `NullableAttribute`, `TupleElementNamesAttribute` or any
94
// other framework attribute the compiler synthesises rather than
95
// resolving from a pragma: the arguments are known here, so the
96
// blob is built from them directly.
97
encode_string_argument(value: string?) -> ubyte[] static is
98
let buffer = BlobBuilder(32)
99
100
buffer.write_u_int16(1us)
101
buffer.write_serialized_string(value)
102
buffer.write_u_int16(0us)
103
104
return buffer.to_array()
105
si
106
107
encode_string_arguments(values: List[string]) -> ubyte[] static is
108
let buffer = BlobBuilder(64)
109
110
buffer.write_u_int16(1us)
111
112
for value in values do
113
buffer.write_serialized_string(value)
114
od
115
116
buffer.write_u_int16(0us)
117
118
return buffer.to_array()
119
si
120
121
encode_byte_argument(value: int) -> ubyte[] static is
122
let buffer = BlobBuilder(8)
123
124
buffer.write_u_int16(1us)
125
buffer.write_byte(cast ubyte(value))
126
buffer.write_u_int16(0us)
127
128
return buffer.to_array()
129
si
130
131
encode_byte_array_argument(values: List[int]) -> ubyte[] static is
132
let buffer = BlobBuilder(16)
133
134
buffer.write_u_int16(1us)
135
buffer.write_int32(values.count)
136
137
for value in values do
138
buffer.write_byte(cast ubyte(value))
139
od
140
141
buffer.write_u_int16(0us)
142
143
return buffer.to_array()
144
si
145
146
// A null entry is an unnamed element, which the blob carries as
147
// a null string rather than as an empty one - an empty name
148
// would read back as an element actually called "".
149
encode_string_array_argument(values: List[string?]) -> ubyte[] static is
150
let buffer = BlobBuilder(32)
151
152
buffer.write_u_int16(1us)
153
buffer.write_int32(values.count)
154
155
for value in values do
156
buffer.write_serialized_string(value)
157
od
158
159
buffer.write_u_int16(0us)
160
161
return buffer.to_array()
162
si
163
164
// The four parallel arrays an `@IL.output` ranges attribute carries:
165
// the pragma's file name, the start and end instruction offsets, and
166
// the source-order sequence - one entry per marked statement. Each
167
// array carries its own length prefix, as every fixed array argument
168
// does, and the trailing zero is the named-arguments count.
169
encode_il_output_ranges(ranges: List[SRM_IL_OUTPUT_RANGE]) -> ubyte[] static is
170
let buffer = BlobBuilder(64)
171
172
buffer.write_u_int16(1us)
173
174
buffer.write_int32(ranges.count)
175
for range in ranges do
176
buffer.write_serialized_string(range.path)
177
od
178
179
buffer.write_int32(ranges.count)
180
for range in ranges do
181
buffer.write_int32(range.start_offset)
182
od
183
184
buffer.write_int32(ranges.count)
185
for range in ranges do
186
buffer.write_int32(range.end_offset)
187
od
188
189
buffer.write_int32(ranges.count)
190
for range in ranges do
191
buffer.write_int32(range.sequence)
192
od
193
194
buffer.write_u_int16(0us)
195
196
return buffer.to_array()
197
si
198
199
_write_named(
200
buffer: BlobBuilder,
201
argument: Semantic.NAMED_ATTRIBUTE_ARGUMENT
202
) -> bool is
203
buffer.write_byte(if argument.is_field then FIELD else PROPERTY fi)
204
205
if !_write_field_or_prop_type(buffer, argument.member_type) then
206
return false
207
fi
208
209
buffer.write_serialized_string(argument.il_name)
210
211
return _write_fixed(buffer, argument.member_type, argument.value)
212
si
213
214
// The type tag a named argument carries ahead of its value,
215
// because a reader has no signature to walk for one.
216
_write_field_or_prop_type(buffer: BlobBuilder, type: Type) -> bool is
217
if _is_object(type) then
218
buffer.write_byte(BOXED)
219
220
return true
221
fi
222
223
if let array: Semantic.Types.ARRAY = type then
224
buffer.write_byte(SZARRAY)
225
226
return _write_field_or_prop_type(buffer, array.arguments[0])
227
fi
228
229
if _is_string(type) then
230
buffer.write_byte(STRING)
231
232
return true
233
fi
234
235
if _is_type(type) then
236
buffer.write_byte(TYPE)
237
238
return true
239
fi
240
241
if _is_enum(type) then
242
buffer.write_byte(ENUM)
243
buffer.write_serialized_string(_serialized_type_name(type))
244
245
return true
246
fi
247
248
let code = _element_type(type)
249
250
if !code? then
251
return false
252
fi
253
254
buffer.write_byte(code)
255
256
return true
257
si
258
259
// One value, encoded as the slot's declared type says to read it.
260
_write_fixed(buffer: BlobBuilder, type: Type?, value: IR.Values.Value?) -> bool is
261
if !type? \/ !value? then
262
return false
263
fi
264
265
if _is_object(type) then
266
return _write_boxed(buffer, value)
267
fi
268
269
if let array: Semantic.Types.ARRAY = type then
270
return _write_array(buffer, array.arguments[0], value)
271
fi
272
273
if _is_string(type) then
274
if isa IR.Values.NULL(value) then
275
buffer.write_serialized_string(null)
276
277
return true
278
fi
279
280
if let text: IR.Values.Literal.STRING = value then
281
buffer.write_serialized_string(text.value)
282
283
return true
284
fi
285
286
return false
287
fi
288
289
if _is_type(type) then
290
if isa IR.Values.NULL(value) then
291
buffer.write_serialized_string(null)
292
293
return true
294
fi
295
296
if let typeof_value: IR.Values.TYPEOF = value then
297
buffer.write_serialized_string(
298
_serialized_type_name(typeof_value.typeof_type))
299
300
return true
301
fi
302
303
return false
304
fi
305
306
return _write_scalar(buffer, type, value)
307
si
308
309
// An array's length prefix is a full four bytes rather than a
310
// compressed integer, and all-ones marks a null array as
311
// distinct from an empty one.
312
_write_array(buffer: BlobBuilder, element_type: Type, value: IR.Values.Value) -> bool is
313
if isa IR.Values.NULL(value) then
314
buffer.write_u_int32(4294967295u)
315
316
return true
317
fi
318
319
let sequence = cast IR.Values.SEQUENCE?(value)
320
321
if !sequence? then
322
return false
323
fi
324
325
buffer.write_int32(sequence.values.count)
326
327
for element in sequence.values do
328
if !_write_fixed(buffer, element_type, element) then
329
return false
330
fi
331
od
332
333
return true
334
si
335
336
// A value in an `object` slot describes itself: the tag says
337
// what type the reader should box it back to. A null reference
338
// has no type to name, and is encoded as a null string - which
339
// is what a C# `[Foo((object) null)]` produces.
340
_write_boxed(buffer: BlobBuilder, value: IR.Values.Value) -> bool is
341
if isa IR.Values.NULL(value) then
342
buffer.write_byte(STRING)
343
buffer.write_serialized_string(null)
344
345
return true
346
fi
347
348
if let text: IR.Values.Literal.STRING = value then
349
buffer.write_byte(STRING)
350
buffer.write_serialized_string(text.value)
351
352
return true
353
fi
354
355
if let typeof_value: IR.Values.TYPEOF = value then
356
buffer.write_byte(TYPE)
357
buffer.write_serialized_string(
358
_serialized_type_name(typeof_value.typeof_type))
359
360
return true
361
fi
362
363
if let sequence: IR.Values.SEQUENCE = value then
364
buffer.write_byte(SZARRAY)
365
366
if !_write_field_or_prop_type(buffer, sequence.element_type) then
367
return false
368
fi
369
370
return _write_array(buffer, sequence.element_type, value)
371
fi
372
373
let value_type = value.type
374
375
if !value_type? then
376
return false
377
fi
378
379
if !_write_field_or_prop_type(buffer, value_type) then
380
return false
381
fi
382
383
return _write_scalar(buffer, value_type, value)
384
si
385
386
// A bool, char, integer or float. An enum is its underlying
387
// value: which enum it was is carried by the signature, or by
388
// the tag a named or boxed argument writes ahead of it.
389
_write_scalar(buffer: BlobBuilder, type: Type, value: IR.Values.Value) -> bool is
390
let constant = _constant_of(value)
391
392
if !constant? then
393
return false
394
fi
395
396
let code = _element_type(type) ?? I4
397
398
if code == BOOLEAN then
399
buffer.write_byte(if _as_signed(constant) == 0L then 0ub else 1ub fi)
400
elif code == R4 then
401
buffer.write_single(cast single(_as_real(constant)))
402
elif code == R8 then
403
buffer.write_double(_as_real(constant))
404
elif code == U1 then
405
buffer.write_byte(cast ubyte(_as_signed(constant)))
406
elif code == U2 \/ code == CHAR then
407
buffer.write_u_int16(cast ushort(_as_signed(constant)))
408
elif code == U4 then
409
buffer.write_u_int32(cast uint(_as_signed(constant)))
410
elif code == U8 then
411
buffer.write_u_int64(cast ulong(_as_signed(constant)))
412
elif code == I1 then
413
buffer.write_s_byte(cast byte(_as_signed(constant)))
414
elif code == I2 then
415
buffer.write_int16(cast short(_as_signed(constant)))
416
elif code == I4 then
417
buffer.write_int32(cast int(_as_signed(constant)))
418
elif code == I8 then
419
buffer.write_int64(_as_signed(constant))
420
else
421
return false
422
fi
423
424
return true
425
si
426
427
// The integral constant, widened. A narrower target takes the
428
// low bytes of it, which is what the attribute blob holds.
429
_as_signed(constant: IR.Values.Literal.CONSTANT) -> long static =>
430
case constant
431
when i4: IR.Values.Literal.CONSTANT.I4 then cast long(i4.value)
432
when i8: IR.Values.Literal.CONSTANT.I8 then i8.value
433
when r4: IR.Values.Literal.CONSTANT.R4 then cast long(r4.value)
434
when r8: IR.Values.Literal.CONSTANT.R8 then cast long(r8.value)
435
esac
436
437
_as_real(constant: IR.Values.Literal.CONSTANT) -> double static =>
438
case constant
439
when i4: IR.Values.Literal.CONSTANT.I4 then cast double(i4.value)
440
when i8: IR.Values.Literal.CONSTANT.I8 then cast double(i8.value)
441
when r4: IR.Values.Literal.CONSTANT.R4 then cast double(r4.value)
442
when r8: IR.Values.Literal.CONSTANT.R8 then r8.value
443
esac
444
445
// The constant a value denotes: a literal, or the unary minus
446
// applied to one - `-5` is a negation over `5` rather than a
447
// negative literal.
448
_constant_of(value: IR.Values.Value) -> IR.Values.Literal.CONSTANT? is
449
if let number: IR.Values.Literal.NUMBER = value then
450
return number.constant
451
fi
452
453
if let negation: IR.Values.Call.INNATE = value then
454
if negation.function.name =~ "-" /\ negation.arguments.count == 1 then
455
if let number: IR.Values.Literal.NUMBER = negation.arguments[0] then
456
return _negate(number.constant)
457
fi
458
fi
459
fi
460
461
return null
462
si
463
464
_negate(constant: IR.Values.Literal.CONSTANT) -> IR.Values.Literal.CONSTANT static =>
465
case constant
466
when i4: IR.Values.Literal.CONSTANT.I4 then IR.Values.Literal.CONSTANT.I4(-i4.value)
467
when i8: IR.Values.Literal.CONSTANT.I8 then IR.Values.Literal.CONSTANT.I8(-i8.value)
468
when r4: IR.Values.Literal.CONSTANT.R4 then IR.Values.Literal.CONSTANT.R4(-r4.value)
469
when r8: IR.Values.Literal.CONSTANT.R8 then IR.Values.Literal.CONSTANT.R8(-r8.value)
470
esac
471
472
473
474
475
// A source literal keeps the shape it was written in: a sign, an
476
// optional radix prefix, and digits that may be grouped.
477
_split_literal(text: string) -> (negative: bool, digits: string, radix: int) static is
478
let negative = text.starts_with("-")
479
let unsigned = if negative then text.substring(1) else text fi
480
let stripped = unsigned.replace("_", "")
481
482
if stripped.starts_with("0x") \/ stripped.starts_with("0X") then
483
return (negative = negative, digits = stripped.substring(2), radix = 16)
484
fi
485
486
return (negative = negative, digits = stripped, radix = 10)
487
si
488
489
_parse_magnitude(digits: string, radix: int) -> ulong? static is
490
if digits.length == 0 then
491
return null
492
fi
493
494
try
495
return System.Convert.to_u_int64(digits, radix)
496
catch e: System.Exception
497
return null
498
yrt
499
si
500
501
// The name a blob carries for a type. The attribute resolver
502
// rejects a typeof argument the reflection name grammar cannot
503
// carry, so a null here is a compiler defect - an attribute
504
// reached emission without passing that gate - and stops the
505
// build rather than encoding a blob no reader could resolve.
506
_serialized_type_name(type: Type) -> string is
507
let name = Semantic.DotNet.REFLECTION_TYPE_NAME.try_serialize(type)
508
509
assert name? else "typeof argument type has no serializable name: {type}"
510
511
return name
512
si
513
514
_is_string(type: Type) -> bool => type.matches(_lookup.get_string_type())
515
516
_is_object(type: Type) -> bool => type.matches(_lookup.get_object_type())
517
518
_is_type(type: Type) -> bool => type.matches(_lookup.get_type_type())
519
520
_is_enum(type: Type) -> bool =>
521
type.symbol.symbol_kind == Semantic.Symbols.SymbolKind.ENUM
522
523
// The element type code for a primitive, or null for anything
524
// else. An enum answers for its underlying type, which the
525
// compiler models as `int` throughout.
526
_element_type(type: Type) -> ubyte? is
527
if type.matches(_lookup.get_bool_type()) then return BOOLEAN; fi
528
if type.matches(_lookup.get_char_type()) then return CHAR; fi
529
if type.matches(_lookup.get_byte_type()) then return I1; fi
530
if type.matches(_lookup.get_ubyte_type()) then return U1; fi
531
if type.matches(_lookup.get_short_type()) then return I2; fi
532
if type.matches(_lookup.get_ushort_type()) then return U2; fi
533
if type.matches(_lookup.get_int_type()) then return I4; fi
534
if type.matches(_lookup.get_uint_type()) then return U4; fi
535
if type.matches(_lookup.get_long_type()) then return I8; fi
536
if type.matches(_lookup.get_ulong_type()) then return U8; fi
537
if type.matches(_lookup.get_single_type()) then return R4; fi
538
if type.matches(_lookup.get_double_type()) then return R8; fi
539
540
if _is_enum(type) then
541
return I4
542
fi
543
544
return null
545
si
546
si
547
si