forked from ballerina-platform/nballerina
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstructure.bal
578 lines (531 loc) · 26.7 KB
/
structure.bal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
import wso2/nballerina.comm.err;
import wso2/nballerina.bir;
import wso2/nballerina.types as t;
import wso2/nballerina.print.llvm;
final RuntimeFunction mappingSetFunction = {
name: "mapping_set",
ty: {
returnType: "i64",
paramTypes: [LLVM_TAGGED_PTR, LLVM_TAGGED_PTR, LLVM_TAGGED_PTR]
},
attrs: []
};
final RuntimeFunction mappingInexactSetFunction = {
name: "mapping_inexact_set",
ty: {
returnType: "i64",
paramTypes: [LLVM_TAGGED_PTR, LLVM_TAGGED_PTR, LLVM_TAGGED_PTR]
},
attrs: []
};
final RuntimeFunction mappingIndexedSetFunction = {
name: "mapping_indexed_set",
ty: {
returnType: "i64",
paramTypes: [LLVM_TAGGED_PTR, LLVM_INT, LLVM_TAGGED_PTR]
},
attrs: []
};
final RuntimeFunction listConstruct8Function = {
name: "list_construct_8",
ty: {
returnType: heapPointerType(llListType),
paramTypes: [llvm:pointerType(llTypeIdDescType), LLVM_INT]
},
attrs: []
};
final RuntimeFunction listConstruct1Function = {
name: "list_construct_1",
ty: {
returnType: heapPointerType(llListType),
paramTypes: [llvm:pointerType(llTypeIdDescType), LLVM_INT]
},
attrs: []
};
final RuntimeFunction listFillingGetFunction = {
name: "list_filling_get",
ty: {
returnType: LLVM_TAGGED_WITH_PANIC_CODE,
paramTypes: [LLVM_TAGGED_PTR, LLVM_INT]
},
attrs: []
};
final RuntimeFunction mappingGetFunction = {
name: "mapping_get",
ty: {
returnType: LLVM_TAGGED_PTR,
paramTypes: [LLVM_TAGGED_PTR, LLVM_TAGGED_PTR]
},
attrs: [["param", 0, "readonly"]]
};
final RuntimeFunction mappingFillingGetFunction = {
name: "mapping_filling_get",
ty: {
returnType: LLVM_TAGGED_WITH_PANIC_CODE,
paramTypes: [LLVM_TAGGED_PTR, LLVM_TAGGED_PTR]
},
attrs: []
};
final RuntimeFunction mappingIndexedGetFunction = {
name: "mapping_indexed_get",
ty: {
returnType: LLVM_TAGGED_PTR,
paramTypes: [LLVM_TAGGED_PTR, LLVM_INT]
},
attrs: [["param", 0, "readonly"]]
};
final RuntimeFunction mappingInitMemberFunction = {
name: "mapping_init_member",
ty: {
returnType: REPR_VOID.llvm,
paramTypes: [LLVM_TAGGED_PTR, LLVM_TAGGED_PTR, LLVM_TAGGED_PTR]
},
attrs: []
};
final RuntimeFunction mappingConstructFunction = {
name: "mapping_construct",
ty: {
returnType: LLVM_TAGGED_PTR,
paramTypes: [llvm:pointerType(llTypeIdDescType), "i64"]
},
attrs: []
};
final RuntimeFunction taggedMemberClearExactAnyFunction = {
name: "tagged_member_clear_exact_any",
ty: {
returnType: LLVM_TAGGED_PTR,
paramTypes: [LLVM_TAGGED_PTR, LLVM_TAGGED_PTR]
},
attrs: ["readnone"]
};
final RuntimeFunction taggedMemberClearExactPtrFunction = {
name: "tagged_member_clear_exact_ptr",
ty: {
returnType: LLVM_TAGGED_PTR,
paramTypes: [LLVM_TAGGED_PTR, LLVM_TAGGED_PTR]
},
attrs: ["readnone"]
};
const LLVM_INDEX = "i32";
// Describes function pointer offsets for getting/setting values of a given Repr from a list.
// Correct ListAccess can be picked base on the static type of a list, runtime type is not needed.
type ListAccess readonly & record {|
int descBaseIndex;
// Repr of set's param and get's return
Repr repr;
|};
// Index of first function in the list descriptor
// Have tid, nMemberTypes, minLength before function pointers
const LIST_DESC_FIRST_FUNCTION_INDEX = 3;
final ListAccess LIST_ACCESS_TAGGED = { descBaseIndex: LIST_DESC_FIRST_FUNCTION_INDEX, repr: REPR_ANY };
final ListAccess LIST_ACCESS_INT = { descBaseIndex: LIST_DESC_FIRST_FUNCTION_INDEX + 3, repr: REPR_INT };
final ListAccess LIST_ACCESS_FLOAT = { descBaseIndex: LIST_DESC_FIRST_FUNCTION_INDEX + 6, repr: REPR_FLOAT };
// Offset form to ListAccess.descBaseIndex.
const LIST_DESC_GET_OFFSET = 0;
const LIST_DESC_SET_OFFSET = 1;
const LIST_DESC_INEXACT_SET_OFFSET = 2;
// Pick ListAccess based on the expected repr of param/return.
// Caller will convert the value between expectedRepr and ListAccess.repr (ie: when expectedRepr is BooleanRepr)
function listAccess(Repr expectedRepr) returns ListAccess {
if expectedRepr is IntRepr {
return LIST_ACCESS_INT;
}
else if expectedRepr is FloatRepr {
return LIST_ACCESS_FLOAT;
}
return LIST_ACCESS_TAGGED;
}
// Describes a list implementation.
// Known at compile time during construction, otherwise depends on runtime type.
type ListRepr readonly & record {|
llvm:Type memberHeapLlvm;
Repr memberRepr;
RuntimeFunction construct;
|};
final ListRepr GENERIC_LIST_REPR = { memberHeapLlvm: LLVM_TAGGED_PTR, memberRepr: REPR_ANY, construct: listConstruct8Function };
final readonly & map<ListRepr> SPECIALIZED_LIST_REPRS = {
int_array: { memberHeapLlvm: LLVM_INT, memberRepr: REPR_INT, construct: listConstruct8Function },
byte_array: { memberHeapLlvm: LLVM_BYTE, memberRepr: REPR_BYTE, construct: listConstruct1Function },
float_array: { memberHeapLlvm: LLVM_FLOAT, memberRepr: REPR_FLOAT, construct: listConstruct8Function }
};
function listTypeToSpecializedListRepr(t:Context tc, t:SemType listType) returns ListRepr? {
return listAtomicTypeToSpecializedListRepr(t:listAtomicType(tc, listType));
}
function listAtomicTypeToSpecializedListRepr(t:ListAtomicType? atomic) returns ListRepr? {
return SPECIALIZED_LIST_REPRS[listAtomicTypeToListReprPrefix(atomic)];
}
function buildListConstruct(llvm:Builder builder, Scaffold scaffold, bir:ListConstructInsn insn) returns BuildError? {
final int length = insn.operands.length();
t:SemType listType = insn.result.semType;
var atomic = <t:ListAtomicType>t:listAtomicType(scaffold.typeContext(), listType);
ListRepr repr = listAtomicTypeToSpecializedListRepr(atomic) ?: GENERIC_LIST_REPR;
llvm:ConstPointerValue inherentType = scaffold.getConstructType(listType);
llvm:PointerValue struct = <llvm:PointerValue>buildRuntimeFunctionCall(builder, scaffold, repr.construct,
[inherentType, constInt(scaffold, length)]);
if length > 0 {
// de-refer the member array from the list struct
llvm:PointerValue array = <llvm:PointerValue>builder.load(builder.getElementPtr(struct,
[constInt(scaffold, 0), constIndex(scaffold, 3)],
"inbounds"),
ALIGN_HEAP);
array = builder.bitCast(array, heapPointerType(llvm:arrayType(repr.memberHeapLlvm, 0)));
foreach int i in 0 ..< length {
llvm:Value val = check buildWideRepr(builder, scaffold, insn.operands[i], repr.memberRepr, t:listAtomicTypeMemberAtInnerVal(atomic, i));
builder.store(listReprConvertToHeapType(builder, repr, val),
builder.getElementPtr(array, [constInt(scaffold, 0), constInt(scaffold, i)], "inbounds"));
}
builder.store(constInt(scaffold, length),
builder.getElementPtr(struct, [constInt(scaffold, 0), constIndex(scaffold, 1)], "inbounds"));
}
builder.store(buildTaggedPtr(builder, scaffold, builder.bitCast(struct, LLVM_TAGGED_PTR), TAG_LIST|FLAG_EXACT), scaffold.address(insn.result));
}
function buildListGet(llvm:Builder builder, Scaffold scaffold, bir:ListGetInsn insn) returns BuildError? {
bir:Register listReg = insn.operands[0];
bir:IntOperand indexOperand = insn.operands[1];
ListRepr? reprIfExact = listTypeToSpecializedListRepr(scaffold.typeContext(), listReg.semType);
llvm:Value taggedStruct = builder.load(scaffold.address(listReg));
llvm:Value index = buildInt(builder, scaffold, indexOperand);
llvm:BasicBlock? bbJoin = ();
llvm:Value member;
Repr memberTmpRepr;
if insn.fill {
if reprIfExact != () {
panic err:impossible("filling-get with type that has specialization");
}
llvm:Value memberWithErr = buildRuntimeFunctionCall(builder, scaffold, listFillingGetFunction, [taggedStruct, index]);
member = buildCheckPanicCode(builder, scaffold, memberWithErr, insn.pos);
memberTmpRepr = REPR_ANY;
}
else {
// struct is the untagged pointer to the struct
llvm:PointerValue struct = builder.bitCast(<llvm:PointerValue>builder.call(scaffold.getIntrinsicFunction("ptrmask.p1.i64"),
[taggedStruct, constInt(scaffold, POINTER_MASK)]),
heapPointerType(llListType));
llvm:BasicBlock continueBlock = scaffold.addBasicBlock();
llvm:BasicBlock outOfBoundsBlock = scaffold.addBasicBlock();
builder.condBr(builder.iCmp("ult",
index,
builder.load(builder.getElementPtr(struct, [constInt(scaffold, 0), constIndex(scaffold, 1)]), ALIGN_HEAP)),
continueBlock,
outOfBoundsBlock);
builder.positionAtEnd(outOfBoundsBlock);
builder.store(buildErrorForConstPanic(builder, scaffold, PANIC_INDEX_OUT_OF_BOUNDS, insn.pos), scaffold.panicAddress());
builder.br(scaffold.getOnPanic());
builder.positionAtEnd(continueBlock);
if reprIfExact != () {
bbJoin = buildSpecializedListGet(builder, scaffold, taggedStruct, struct, index, reprIfExact, insn.result);
}
ListAccess la = listAccess(scaffold.getRepr(insn.result));
llvm:PointerValue desc = <llvm:PointerValue>builder.load(builder.getElementPtr(struct, [constInt(scaffold, 0), constIndex(scaffold, 0)]), ALIGN_HEAP);
llvm:PointerValue func = <llvm:PointerValue>builder.load(builder.getElementPtr(desc, [constInt(scaffold, 0), constIndex(scaffold, la.descBaseIndex + LIST_DESC_GET_OFFSET)]), ALIGN_HEAP);
member = <llvm:Value>builder.call(func, [taggedStruct, index]);
memberTmpRepr = la.repr;
}
t:SemType resultType = insn.result.semType;
if isPotentiallyExact(resultType) {
if !isListMemberTypeExact(scaffold.typeContext(), listReg.semType, indexOperand, resultType) {
// this clears the exact bit of member
member = buildClearExact(builder, scaffold, member, resultType);
}
else {
member = buildMemberClearExact(builder, scaffold, taggedStruct, member, resultType);
}
}
buildStoreRepr(builder, scaffold, member, insn.result, memberTmpRepr);
if bbJoin != () {
builder.br(bbJoin);
builder.positionAtEnd(bbJoin);
}
}
function buildSpecializedListGet(llvm:Builder builder, Scaffold scaffold, llvm:Value taggedStruct, llvm:PointerValue struct, llvm:Value index, ListRepr repr, bir:Register result) returns llvm:BasicBlock {
llvm:BasicBlock bbExact = scaffold.addBasicBlock();
llvm:BasicBlock bbInexact = scaffold.addBasicBlock();
llvm:BasicBlock bbJoin = scaffold.addBasicBlock();
llvm:Value isExact = buildIsExact(builder, scaffold, taggedStruct);
builder.condBr(isExact, bbExact, bbInexact);
builder.positionAtEnd(bbExact);
llvm:PointerValue array = <llvm:PointerValue>builder.load(builder.getElementPtr(struct, [constInt(scaffold, 0), constIndex(scaffold, 3)]), ALIGN_HEAP);
array = builder.bitCast(array, heapPointerType(llvm:arrayType(repr.memberHeapLlvm, 0)));
llvm:Value value = builder.load(builder.getElementPtr(array, [constInt(scaffold, 0), index], "inbounds"), ALIGN_HEAP);
buildStoreRepr(builder, scaffold, listReprConvertFromHeapType(builder, repr, value), result, repr.memberRepr);
builder.br(bbJoin);
builder.positionAtEnd(bbInexact);
return bbJoin;
}
function buildListSet(llvm:Builder builder, Scaffold scaffold, bir:ListSetInsn insn) returns BuildError? {
bir:Register listOperand = insn.operands[0];
bir:IntOperand indexOperand = insn.operands[1];
bir:Operand newMemberOperand = insn.operands[2];
llvm:Value taggedStruct = builder.load(scaffold.address(listOperand));
llvm:PointerValue struct = builder.bitCast(<llvm:PointerValue>buildFunctionCall(builder, scaffold, scaffold.getIntrinsicFunction("ptrmask.p1.i64"),
[taggedStruct, constInt(scaffold, POINTER_MASK)]),
heapPointerType(llListType));
llvm:BasicBlock? bbJoin = ();
t:SemType listType = listOperand.semType;
t:Context tc = scaffold.typeContext();
t:SemType memberType = t:listMemberTypeInnerVal(tc, listType, indexOperand.semType);
llvm:Value index = buildInt(builder, scaffold, indexOperand);
ListAccess la = listAccess(semTypeRepr(newMemberOperand.semType));
llvm:Value val = check buildWideRepr(builder, scaffold, newMemberOperand, la.repr, memberType);
ListRepr? reprIfExact = listTypeToSpecializedListRepr(tc, listType);
if reprIfExact != () {
// Value build for calling list access (val) happens to be reuseable for specialized set. May change, eg: specialized boolean[]
bbJoin = check buildSpecializedListSet(builder, scaffold, taggedStruct, struct, index, reprIfExact, val);
}
llvm:PointerValue desc = <llvm:PointerValue>builder.load(builder.getElementPtr(struct, [constInt(scaffold, 0), constIndex(scaffold, 0)]), ALIGN_HEAP);
int offset = isListSetAlwaysInexact(tc, listType, indexOperand.semType, newMemberOperand.semType) ? LIST_DESC_INEXACT_SET_OFFSET : LIST_DESC_SET_OFFSET;
llvm:PointerValue func = <llvm:PointerValue>builder.load(builder.getElementPtr(desc, [constInt(scaffold, 0), constIndex(scaffold, la.descBaseIndex + offset)]), ALIGN_HEAP);
llvm:Value err = <llvm:Value>builder.call(func, [taggedStruct, index, val]);
buildCheckError(builder, scaffold, err, insn.pos);
if bbJoin != () {
builder.br(bbJoin);
builder.positionAtEnd(bbJoin);
}
}
function buildSpecializedListSet(llvm:Builder builder, Scaffold scaffold, llvm:Value taggedStruct, llvm:PointerValue struct, llvm:Value index, ListRepr repr, llvm:Value val)
returns llvm:BasicBlock|BuildError {
llvm:BasicBlock bbExact = scaffold.addBasicBlock();
llvm:BasicBlock bbTaggedSet = scaffold.addBasicBlock();
llvm:BasicBlock bbJoin = scaffold.addBasicBlock();
llvm:Value isExact = buildIsExact(builder, scaffold, taggedStruct);
builder.condBr(isExact, bbExact, bbTaggedSet);
builder.positionAtEnd(bbExact);
llvm:BasicBlock bbSpecializedSet = scaffold.addBasicBlock();
builder.condBr(builder.iCmp("ult",
index,
builder.load(builder.getElementPtr(struct, [constInt(scaffold, 0), constIndex(scaffold, 1)]), ALIGN_HEAP)),
bbSpecializedSet,
bbTaggedSet);
builder.positionAtEnd(bbSpecializedSet);
llvm:PointerValue array = <llvm:PointerValue>builder.load(builder.getElementPtr(struct, [constInt(scaffold, 0), constIndex(scaffold, 3)]), ALIGN_HEAP);
array = builder.bitCast(array, heapPointerType(llvm:arrayType(repr.memberHeapLlvm, 0)));
llvm:Value heapVal = listReprConvertToHeapType(builder, repr, val);
builder.store(heapVal, builder.getElementPtr(array, [constInt(scaffold, 0), index], "inbounds"));
builder.br(bbJoin);
builder.positionAtEnd(bbTaggedSet);
return bbJoin;
}
function listReprConvertToHeapType(llvm:Builder builder, ListRepr repr, llvm:Value value) returns llvm:Value {
return convertValue(builder, value, repr.memberRepr.llvm, repr.memberHeapLlvm);
}
function listReprConvertFromHeapType(llvm:Builder builder, ListRepr repr, llvm:Value value) returns llvm:Value {
return convertValue(builder, value, repr.memberHeapLlvm, repr.memberRepr.llvm);
}
function convertValue(llvm:Builder builder, llvm:Value value, llvm:Type fromTy, llvm:Type toTy) returns llvm:Value {
if fromTy == toTy {
return value;
}
else if fromTy == LLVM_INT && toTy == LLVM_BYTE {
return builder.trunc(value, toTy);
}
else if fromTy == LLVM_BYTE && toTy == LLVM_INT {
return builder.zExt(value, toTy);
}
panic err:impossible("unsupported llvm type conversion");
}
function buildMappingConstruct(llvm:Builder builder, Scaffold scaffold, bir:MappingConstructInsn insn) returns BuildError? {
t:SemType mappingType = insn.result.semType;
llvm:ConstPointerValue inherentType = scaffold.getConstructType(mappingType);
llvm:PointerValue m = <llvm:PointerValue>buildRuntimeFunctionCall(builder, scaffold, mappingConstructFunction,
[inherentType, constInt(scaffold, insn.operands.length())]);
t:Context tc = scaffold.typeContext();
t:MappingAtomicType mat = <t:MappingAtomicType>t:mappingAtomicType(tc, mappingType);
foreach var [fieldName, operand] in mappingOrderFields(mat, insn.fieldNames, insn.operands) {
_ = buildVoidRuntimeFunctionCall(builder, scaffold, mappingInitMemberFunction,
[
m,
check buildConstString(builder, scaffold, fieldName),
check buildWideRepr(builder, scaffold, operand, REPR_ANY,
t:mappingMemberTypeInnerVal(tc, mappingType, t:stringConst(fieldName)))
]);
}
builder.store(m, scaffold.address(insn.result));
}
// When there are required fields, we need to reorder so that
// required fields are in the same order here as in the type descriptor.
function mappingOrderFields(t:MappingAtomicType mat, string[] fieldNames, bir:Operand[] operands) returns [string, bir:Operand][] {
int length = fieldNames.length();
string[] requiredFieldNames = mat.names;
int nRequiredFields = requiredFieldNames.length();
if nRequiredFields != 0 {
map<int> requiredFieldIndex = {};
foreach int i in 0 ..< nRequiredFields {
requiredFieldIndex[requiredFieldNames[i]] = i;
}
return
from int i in 0 ..< length
let string fieldName = fieldNames[i]
let int sortIndex = requiredFieldIndex[fieldName] ?: nRequiredFields + i
order by sortIndex
select [fieldName, operands[i]];
}
else {
return from int i in 0 ..< length select [fieldNames[i], operands[i]];
}
}
function buildMappingGet(llvm:Builder builder, Scaffold scaffold, bir:MappingGetInsn insn) returns BuildError? {
bir:Register mappingReg = insn.operands[0];
bir:StringOperand keyOperand = insn.operands[1];
int? fieldIndex = mappingFieldIndex(scaffold.typeContext(), mappingReg.semType, keyOperand);
boolean fill;
RuntimeFunction rf;
llvm:Value k;
if fieldIndex == () {
fill = insn.name == bir:INSN_MAPPING_FILLING_GET;
rf = fill ? mappingFillingGetFunction : mappingGetFunction;
k = check buildString(builder, scaffold, keyOperand);
}
else {
fill = false;
rf = mappingIndexedGetFunction;
k = constInt(scaffold, fieldIndex);
}
llvm:Value mapping = builder.load(scaffold.address(mappingReg));
llvm:Value memberWithErr = buildRuntimeFunctionCall(builder, scaffold, rf, [mapping, k]);
llvm:Value member = fill ? buildCheckPanicCode(builder, scaffold, memberWithErr, insn.pos) : memberWithErr;
t:SemType resultType = insn.result.semType;
if isPotentiallyExact(resultType) {
if !isMappingMemberTypeExact(scaffold.typeContext(), mappingReg.semType, keyOperand, resultType) {
// this clears the exact bit of member
member = buildClearExact(builder, scaffold, member, resultType);
}
else {
// this clears the exact bit of `member` only if `mapping` is not exact
member = buildMemberClearExact(builder, scaffold, mapping, member, resultType);
}
}
buildStoreTagged(builder, scaffold, member, insn.result);
}
// When this returns false, we need clear the exact bit on a member that we get from a mapping value.
// Let M be mapping type and let K be the type of keyOperand.
// If this function returns true, then it must be the case when a mappping value has as M as
// its inherent type, then for any field name k in K, if M has a field k, then the type that M requires for k must be
// equal to t:mappingMemberType(cx, M, K).
function isMappingMemberTypeExact(t:Context tc, t:SemType mappingType, bir:StringOperand keyOperand, t:SemType resultType) returns boolean {
t:MappingAtomicType? mat = t:mappingAtomicType(tc, mappingType);
if mat == () {
return false;
}
// don't need to check when the condition is false, because there can be only one applicable member type
else if t:singleStringShape(keyOperand.semType) == () && mat.names.length() != 0 {
t:SemType peResult = t:intersect(resultType, POTENTIALLY_EXACT);
foreach t:SemType ty in t:mappingAtomicTypeApplicableMemberTypesInner(mat, keyOperand.semType) {
if !isSameTypeWithin(tc, ty, POTENTIALLY_EXACT, peResult) {
return false;
}
}
}
return true;
}
function isListMemberTypeExact(t:Context tc, t:SemType listType, bir:IntOperand indexOperand, t:SemType resultType) returns boolean {
t:ListAtomicType? lat = t:listAtomicType(tc, listType);
if lat == () {
return false;
}
// don't need to check when the condition is false, because there can be only one applicable member type
else if t:singleIntShape(indexOperand.semType) == () && lat.members.fixedLength != 0 {
t:SemType peResult = t:intersect(resultType, POTENTIALLY_EXACT);
foreach t:SemType ty in t:listAtomicTypeApplicableMemberTypesInnerVal(tc, lat, indexOperand.semType) {
if !isSameTypeWithin(tc, ty, POTENTIALLY_EXACT, peResult) {
return false;
}
}
}
return true;
}
function isSameTypeWithin(t:Context tc, t:SemType semType, t:SemType within, t:SemType targetType) returns boolean {
t:SemType ty = t:intersect(semType, within);
return ty == t:NEVER || t:isSameType(tc, ty, targetType);
}
function buildMappingSet(llvm:Builder builder, Scaffold scaffold, bir:MappingSetInsn insn) returns BuildError? {
bir:Register mappingReg = insn.operands[0];
bir:SemType mappingType = mappingReg.semType;
bir:StringOperand keyOperand = insn.operands[1];
int? fieldIndex = mappingFieldIndex(scaffold.typeContext(), mappingReg.semType, keyOperand);
t:Context tc = scaffold.typeContext();
bir:Operand newMemberOperand = insn.operands[2];
RuntimeFunction rf;
llvm:Value k;
if fieldIndex == () {
if isMappingSetAlwaysInexact(tc, mappingType, keyOperand.semType, newMemberOperand.semType) {
rf = mappingInexactSetFunction;
}
else {
rf = mappingSetFunction;
}
k = check buildString(builder, scaffold, keyOperand);
}
else {
rf = mappingIndexedSetFunction;
k = constInt(scaffold, fieldIndex);
}
t:SemType memberType = t:mappingMemberTypeInnerVal(scaffold.typeContext(), mappingType, keyOperand.semType);
// Note that we do not need to check the exactness of the mapping value, nor do we need
// to check the exactness of the member type: buildWideRepr does all that is necessary.
// See exact.md for more details.
llvm:Value err = buildRuntimeFunctionCall(builder, scaffold, rf,
[
builder.load(scaffold.address(mappingReg)),
k,
check buildWideRepr(builder, scaffold, newMemberOperand, REPR_ANY, memberType)
]);
buildCheckError(builder, scaffold, <llvm:Value>err, insn.pos);
}
// Returns true if it is possible for both the mapping type to be equal to the inherent type
// (i.e. for the mapping value reference to be exact)
// and for the mapping_set to panic at runtime.
// In this case, we cannot optimize based on the exactness of the mapping value, and so
// we have to do the same was what we would do if the mapping value was inexact.
function isMappingSetAlwaysInexact(t:Context tc, t:SemType mappingType, t:SemType keyType, t:SemType newMemberType) returns boolean {
t:MappingAtomicType? mat = t:mappingAtomicType(tc, mappingType);
if mat == () {
// inherent type is atomic, so if mapping type isn't, they cannot be equal
return false;
}
foreach t:SemType ty in t:mappingAtomicTypeApplicableMemberTypesInner(mat, keyType) {
if !t:isSubtype(tc, newMemberType, ty) {
return true;
}
}
return false;
}
function isListSetAlwaysInexact(t:Context tc, t:SemType listType, t:SemType indexType, t:SemType newMemberType) returns boolean {
t:ListAtomicType? lat = t:listAtomicType(tc, listType);
if lat == () || t:singleIntShape(indexType) != () {
// inherent type is atomic, so if list type isn't, they cannot be equal
// If indexing with a constant value, then we have the precise type
return false;
}
foreach t:SemType ty in t:listAtomicTypeApplicableMemberTypesInnerVal(tc, lat, indexType) {
if !t:isSubtype(tc, newMemberType, ty) {
return true;
}
}
return false;
}
function mappingFieldIndex(t:Context tc, t:SemType mappingType, bir:StringOperand keyOperand) returns int? {
string? k = t:singleStringShape(keyOperand.semType);
if k is string {
t:MappingAtomicType? mat = t:mappingAtomicType(tc, mappingType);
if mat != () && t:cellInner(mat.rest) == t:UNDEF {
return mat.names.indexOf(k);
}
}
return ();
}
// This clears the exact bit of the member if the structure is not exact.
function buildMemberClearExact(llvm:Builder builder, Scaffold scaffold, llvm:Value structure, llvm:Value member, t:SemType sourceType) returns llvm:Value {
RuntimeFunction rf = overloadsExactBit(sourceType) ? taggedMemberClearExactAnyFunction : taggedMemberClearExactPtrFunction;
return buildRuntimeFunctionCall(builder, scaffold, rf, [structure, member]);
}
function buildCheckError(llvm:Builder builder, Scaffold scaffold, llvm:Value err, bir:Position pos) {
llvm:BasicBlock continueBlock = scaffold.addBasicBlock();
llvm:BasicBlock errorBlock = scaffold.addBasicBlock();
builder.condBr(builder.iCmp("eq", err, constI64(scaffold, 0)),
continueBlock,
errorBlock);
builder.positionAtEnd(errorBlock);
builder.store(buildErrorForPanic(builder, scaffold, err, pos), scaffold.panicAddress());
builder.br(scaffold.getOnPanic());
builder.positionAtEnd(continueBlock);
}