@tsplc/compiler 0.5.7 → 0.5.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.js +17 -5
- package/dist/cli.js.map +5 -5
- package/dist/codesys.js +2846 -1697
- package/dist/codesys.js.map +17 -16
- package/dist/index.js +54 -6
- package/dist/index.js.map +8 -8
- package/dist/ir.js +1645 -1326
- package/dist/ir.js.map +13 -12
- package/dist/project.js +5 -2
- package/dist/project.js.map +3 -3
- package/package.json +9 -9
- package/src/codesys/debug.ts +17 -2
- package/src/codesys/emit.ts +32 -10
- package/src/codesys/explain.ts +1465 -0
- package/src/codesys/global-lists.ts +21 -9
- package/src/codesys/http.ts +12 -0
- package/src/codesys/instrumentation.ts +131 -3
- package/src/codesys/manifest.ts +29 -12
- package/src/codesys/numeric.ts +5 -3
- package/src/codesys/plan.ts +22 -6
- package/src/codesys/programs.ts +2 -0
- package/src/frontend/elaborate/index.ts +16 -1
- package/src/frontend/elaborate/storage.ts +56 -1
- package/src/frontend/expressions.ts +2 -2
- package/src/frontend/intrinsics.ts +1 -2
- package/src/frontend/statements.ts +3 -2
- package/src/frontend/typescript.ts +3 -0
- package/src/ir/conditions.ts +895 -0
- package/src/ir/core-model.ts +2 -2
- package/src/ir/fold.ts +15 -107
- package/src/ir/index.ts +10 -11
- package/src/ir/link.ts +1 -0
- package/src/ir/model.ts +16 -79
- package/src/ir/numeric.ts +5 -41
- package/src/ir/ranges.ts +8 -14
- package/src/ir/scalars.ts +6 -4
- package/src/project/debugger.ts +9 -0
- package/src/project/project.ts +6 -0
- package/src/project/release.ts +7 -4
package/dist/ir.js
CHANGED
|
@@ -2,39 +2,10 @@
|
|
|
2
2
|
import { elementaryType, integerRange as integerRange2 } from "@tsplc/iec";
|
|
3
3
|
|
|
4
4
|
// packages/compiler/src/ir/model.ts
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
"exp",
|
|
10
|
-
"sin",
|
|
11
|
-
"cos",
|
|
12
|
-
"tan",
|
|
13
|
-
"asin",
|
|
14
|
-
"acos",
|
|
15
|
-
"atan"
|
|
16
|
-
];
|
|
17
|
-
var elementaryValue = {
|
|
18
|
-
sqrt: Math.sqrt,
|
|
19
|
-
ln: Math.log,
|
|
20
|
-
log10: Math.log10,
|
|
21
|
-
exp: Math.exp,
|
|
22
|
-
sin: Math.sin,
|
|
23
|
-
cos: Math.cos,
|
|
24
|
-
tan: Math.tan,
|
|
25
|
-
asin: Math.asin,
|
|
26
|
-
acos: Math.acos,
|
|
27
|
-
atan: Math.atan
|
|
28
|
-
};
|
|
29
|
-
var isElementary = (operator) => elementaryFunctions.includes(operator);
|
|
30
|
-
var comparisonOperators = new Set([
|
|
31
|
-
"equal",
|
|
32
|
-
"notEqual",
|
|
33
|
-
"less",
|
|
34
|
-
"lessEqual",
|
|
35
|
-
"greater",
|
|
36
|
-
"greaterEqual"
|
|
37
|
-
]);
|
|
5
|
+
import {
|
|
6
|
+
comparisonOperatorNames
|
|
7
|
+
} from "@tsplc/iec/arithmetic";
|
|
8
|
+
var comparisonOperators = new Set(comparisonOperatorNames);
|
|
38
9
|
var telemetryOperands = ({
|
|
39
10
|
record,
|
|
40
11
|
properties
|
|
@@ -116,6 +87,9 @@ var standardBlocks = {
|
|
|
116
87
|
}
|
|
117
88
|
};
|
|
118
89
|
|
|
90
|
+
// packages/compiler/src/ir/numeric.ts
|
|
91
|
+
import { roundFloat } from "@tsplc/iec/arithmetic";
|
|
92
|
+
|
|
119
93
|
// packages/compiler/src/ir/types.ts
|
|
120
94
|
import {
|
|
121
95
|
integerRange as elementaryIntegerRange,
|
|
@@ -181,23 +155,8 @@ var within = (inner, outer) => inner[0] >= outer[0] && inner[1] <= outer[1];
|
|
|
181
155
|
var admits = (interval, value) => interval[0] <= value && value <= interval[1];
|
|
182
156
|
var canonicalInteger = /^(?:0|-?[1-9][0-9]*)$/;
|
|
183
157
|
var integerType = (type) => type.kind === "integer" || type.kind === "bits";
|
|
184
|
-
var
|
|
185
|
-
|
|
186
|
-
64: { maximum: Number.MAX_VALUE, precision: 53 }
|
|
187
|
-
};
|
|
188
|
-
var floatMaximum = (type) => floatFormats[type.bits].maximum;
|
|
189
|
-
var exactIntegerLimit = (type) => 1n << BigInt(floatFormats[type.bits].precision);
|
|
190
|
-
var roundFloat = (value, type) => type.bits === 32 ? Math.fround(value) : value;
|
|
191
|
-
var saturateFloat = (value, type) => {
|
|
192
|
-
const maximum = floatMaximum(type);
|
|
193
|
-
if (value < -maximum) {
|
|
194
|
-
return -maximum;
|
|
195
|
-
}
|
|
196
|
-
if (value > maximum) {
|
|
197
|
-
return maximum;
|
|
198
|
-
}
|
|
199
|
-
return roundFloat(value, type);
|
|
200
|
-
};
|
|
158
|
+
var floatPrecision = { 32: 24, 64: 53 };
|
|
159
|
+
var exactIntegerLimit = (type) => 1n << BigInt(floatPrecision[type.bits]);
|
|
201
160
|
var exactDecimal = (text, scale) => {
|
|
202
161
|
const match = /^(-?\d+)(?:\.(\d*))?(?:e([+-]?\d+))?$/i.exec(text);
|
|
203
162
|
if (!match) {
|
|
@@ -256,13 +215,6 @@ var canAssign = (source, destination, value) => {
|
|
|
256
215
|
return source.kind === "float" && destination.kind === "float" && source.bits <= destination.bits;
|
|
257
216
|
};
|
|
258
217
|
var wrapsNatively = (expression) => expression.kind === "binary" && !expression.policy && (expression.operator === "add" || expression.operator === "subtract" || expression.operator === "multiply");
|
|
259
|
-
var applyIntegerPolicy = (value, type, policy) => {
|
|
260
|
-
if (policy === "saturate") {
|
|
261
|
-
const [minimum, maximum] = integerRange(type);
|
|
262
|
-
return highest(minimum, lowest(value, maximum));
|
|
263
|
-
}
|
|
264
|
-
return type.kind === "integer" && type.signed ? BigInt.asIntN(type.bits, value) : BigInt.asUintN(type.bits, value);
|
|
265
|
-
};
|
|
266
218
|
|
|
267
219
|
// packages/compiler/src/ir/domains.ts
|
|
268
220
|
var nanosecondsPerMillisecond = 1000000n;
|
|
@@ -340,7 +292,21 @@ function domainIssue(value, types) {
|
|
|
340
292
|
return "TIME and calendar domains permit storage, typed copies and comparisons; implicit numeric arithmetic is unsupported.";
|
|
341
293
|
}
|
|
342
294
|
|
|
295
|
+
// packages/compiler/src/ir/ranges.ts
|
|
296
|
+
import { loopIterations } from "@tsplc/iec/arithmetic";
|
|
297
|
+
|
|
343
298
|
// packages/compiler/src/ir/fold.ts
|
|
299
|
+
import {
|
|
300
|
+
applyIntegerPolicy,
|
|
301
|
+
booleanOperation,
|
|
302
|
+
compare,
|
|
303
|
+
elementaryValue,
|
|
304
|
+
floatArithmetic,
|
|
305
|
+
integerArithmetic,
|
|
306
|
+
isElementary,
|
|
307
|
+
roundFloat as roundFloat2,
|
|
308
|
+
saturateFloat
|
|
309
|
+
} from "@tsplc/iec/arithmetic";
|
|
344
310
|
var fold = (expression, variables, types) => {
|
|
345
311
|
switch (expression.kind) {
|
|
346
312
|
case "constant":
|
|
@@ -404,12 +370,12 @@ var foldConvert = (expression, value, target) => {
|
|
|
404
370
|
};
|
|
405
371
|
}
|
|
406
372
|
if (value?.kind === "integer" && target?.kind === "float") {
|
|
407
|
-
return { kind: "float", value:
|
|
373
|
+
return { kind: "float", value: roundFloat2(Number(value.value), target) };
|
|
408
374
|
}
|
|
409
375
|
if (value?.kind === "float" && target?.kind === "float") {
|
|
410
376
|
return {
|
|
411
377
|
kind: "float",
|
|
412
|
-
value: expression.policy === "saturate" ? saturateFloat(value.value, target) :
|
|
378
|
+
value: expression.policy === "saturate" ? saturateFloat(value.value, target) : roundFloat2(value.value, target)
|
|
413
379
|
};
|
|
414
380
|
}
|
|
415
381
|
return;
|
|
@@ -457,7 +423,8 @@ var comparable = (value) => {
|
|
|
457
423
|
};
|
|
458
424
|
var foldBinary = (expression, left, right, target) => {
|
|
459
425
|
if (left.kind === "bool" && right.kind === "bool") {
|
|
460
|
-
|
|
426
|
+
const value = booleanOperation(expression.operator, left.value, right.value);
|
|
427
|
+
return value === undefined ? undefined : { kind: "bool", value };
|
|
461
428
|
}
|
|
462
429
|
if (left.kind !== right.kind || !ordered(left) || !ordered(right)) {
|
|
463
430
|
return;
|
|
@@ -481,89 +448,13 @@ var foldBinary = (expression, left, right, target) => {
|
|
|
481
448
|
}
|
|
482
449
|
return {
|
|
483
450
|
kind: "float",
|
|
484
|
-
value: target?.kind === "float" ?
|
|
451
|
+
value: target?.kind === "float" ? roundFloat2(value, target) : value
|
|
485
452
|
};
|
|
486
453
|
}
|
|
487
454
|
return;
|
|
488
455
|
};
|
|
489
|
-
var foldLogical = (operator, left, right) => {
|
|
490
|
-
switch (operator) {
|
|
491
|
-
case "and":
|
|
492
|
-
return { kind: "bool", value: left && right };
|
|
493
|
-
case "or":
|
|
494
|
-
return { kind: "bool", value: left || right };
|
|
495
|
-
case "equal":
|
|
496
|
-
return { kind: "bool", value: left === right };
|
|
497
|
-
case "notEqual":
|
|
498
|
-
return { kind: "bool", value: left !== right };
|
|
499
|
-
default:
|
|
500
|
-
return;
|
|
501
|
-
}
|
|
502
|
-
};
|
|
503
|
-
var compare = (operator, left, right) => {
|
|
504
|
-
switch (operator) {
|
|
505
|
-
case "equal":
|
|
506
|
-
return left === right;
|
|
507
|
-
case "notEqual":
|
|
508
|
-
return left !== right;
|
|
509
|
-
case "less":
|
|
510
|
-
return left < right;
|
|
511
|
-
case "lessEqual":
|
|
512
|
-
return left <= right;
|
|
513
|
-
case "greater":
|
|
514
|
-
return left > right;
|
|
515
|
-
case "greaterEqual":
|
|
516
|
-
return left >= right;
|
|
517
|
-
default:
|
|
518
|
-
return;
|
|
519
|
-
}
|
|
520
|
-
};
|
|
521
|
-
var integerArithmetic = (operator, left, right) => {
|
|
522
|
-
switch (operator) {
|
|
523
|
-
case "add":
|
|
524
|
-
return left + right;
|
|
525
|
-
case "subtract":
|
|
526
|
-
return left - right;
|
|
527
|
-
case "multiply":
|
|
528
|
-
return left * right;
|
|
529
|
-
case "divide":
|
|
530
|
-
return right === 0n ? undefined : left / right;
|
|
531
|
-
case "remainder":
|
|
532
|
-
return right === 0n ? undefined : left % right;
|
|
533
|
-
case "bitAnd":
|
|
534
|
-
return left & right;
|
|
535
|
-
case "bitOr":
|
|
536
|
-
return left | right;
|
|
537
|
-
case "bitXor":
|
|
538
|
-
return left ^ right;
|
|
539
|
-
case "shiftLeft":
|
|
540
|
-
return right < 0n || right > 63n ? undefined : left << right;
|
|
541
|
-
case "shiftRight":
|
|
542
|
-
return right < 0n || right > 63n ? undefined : left >> right;
|
|
543
|
-
default:
|
|
544
|
-
return;
|
|
545
|
-
}
|
|
546
|
-
};
|
|
547
|
-
var floatArithmetic = (operator, left, right) => {
|
|
548
|
-
switch (operator) {
|
|
549
|
-
case "add":
|
|
550
|
-
return left + right;
|
|
551
|
-
case "subtract":
|
|
552
|
-
return left - right;
|
|
553
|
-
case "multiply":
|
|
554
|
-
return left * right;
|
|
555
|
-
case "divide":
|
|
556
|
-
return left / right;
|
|
557
|
-
case "remainder":
|
|
558
|
-
return left % right;
|
|
559
|
-
case "power":
|
|
560
|
-
return left ** right;
|
|
561
|
-
default:
|
|
562
|
-
return;
|
|
563
|
-
}
|
|
564
|
-
};
|
|
565
456
|
var finiteFloat = (value, target) => {
|
|
566
|
-
const rounded = target?.kind === "float" ?
|
|
457
|
+
const rounded = target?.kind === "float" ? roundFloat2(value, target) : value;
|
|
567
458
|
return Number.isFinite(rounded) ? { kind: "float", value: rounded } : undefined;
|
|
568
459
|
};
|
|
569
460
|
|
|
@@ -637,17 +528,11 @@ var arithmeticInterval = (operator, left, right, type) => {
|
|
|
637
528
|
}
|
|
638
529
|
};
|
|
639
530
|
var maxLoopIterations = 1000000n;
|
|
640
|
-
var loopIterations = (first, last, step) => {
|
|
641
|
-
if (step > 0n) {
|
|
642
|
-
return last >= first ? (last - first) / step + 1n : 0n;
|
|
643
|
-
}
|
|
644
|
-
return last <= first ? (first - last) / -step + 1n : 0n;
|
|
645
|
-
};
|
|
646
531
|
var loopBounds = (start, limit, step, exclusive = false) => {
|
|
647
532
|
const excluded = exclusive ? 1n : 0n;
|
|
648
533
|
const first = step > 0n ? start[0] : start[1];
|
|
649
534
|
const last = step > 0n ? limit[1] - excluded : limit[0] + excluded;
|
|
650
|
-
const count = loopIterations(first,
|
|
535
|
+
const count = loopIterations(first, limit[step > 0n ? 1 : 0], exclusive, step);
|
|
651
536
|
const range = first <= last ? [first, last] : [last, first];
|
|
652
537
|
return { first, last, count, range };
|
|
653
538
|
};
|
|
@@ -722,6 +607,11 @@ var mathHelperRange = (helper, [first, second, third]) => {
|
|
|
722
607
|
};
|
|
723
608
|
|
|
724
609
|
// packages/compiler/src/ir/scalars.ts
|
|
610
|
+
import {
|
|
611
|
+
floatMaximum,
|
|
612
|
+
isElementary as isElementary2,
|
|
613
|
+
roundFloat as roundFloat3
|
|
614
|
+
} from "@tsplc/iec/arithmetic";
|
|
725
615
|
var scalarKinds = new Set([
|
|
726
616
|
"bool",
|
|
727
617
|
"integer",
|
|
@@ -790,7 +680,7 @@ var exactConversionIssue = (expression, source, type, proof) => {
|
|
|
790
680
|
return "Exact conversion cannot be proved lossless.";
|
|
791
681
|
}
|
|
792
682
|
if (type.kind === "float" && value?.kind === "integer") {
|
|
793
|
-
const converted =
|
|
683
|
+
const converted = roundFloat3(Number(value.value), type);
|
|
794
684
|
if (Number.isFinite(converted) && Number.isInteger(converted) && BigInt(converted) === BigInt(value.value)) {
|
|
795
685
|
return;
|
|
796
686
|
}
|
|
@@ -808,7 +698,7 @@ var exactConversionIssue = (expression, source, type, proof) => {
|
|
|
808
698
|
return "Exact conversion cannot be proved lossless.";
|
|
809
699
|
};
|
|
810
700
|
var unaryIssue = (expression, source, type, proof) => {
|
|
811
|
-
if (
|
|
701
|
+
if (isElementary2(expression.operator)) {
|
|
812
702
|
if (source.kind !== "float" || source.id !== type.id) {
|
|
813
703
|
return "Elementary functions take and return one floating type.";
|
|
814
704
|
}
|
|
@@ -893,7 +783,7 @@ var domains = {
|
|
|
893
783
|
ln: { holds: (low) => low > 0, needs: "greater than zero" },
|
|
894
784
|
log10: { holds: (low) => low > 0, needs: "greater than zero" },
|
|
895
785
|
exp: {
|
|
896
|
-
holds: (_, high, type) => type.kind === "float" && Number.isFinite(
|
|
786
|
+
holds: (_, high, type) => type.kind === "float" && Number.isFinite(roundFloat3(Math.exp(high), type)),
|
|
897
787
|
needs: "small enough for a finite result"
|
|
898
788
|
},
|
|
899
789
|
asin: {
|
|
@@ -939,7 +829,7 @@ var powerIssue = (expression, type, proof) => {
|
|
|
939
829
|
}
|
|
940
830
|
const magnitudes = [Math.abs(low), Math.abs(high)];
|
|
941
831
|
const largest = exponent.value >= 0 ? Math.max(...magnitudes) ** exponent.value : Math.min(...magnitudes) ** exponent.value;
|
|
942
|
-
return Number.isFinite(
|
|
832
|
+
return Number.isFinite(roundFloat3(largest, type)) ? undefined : "The power may not be finite.";
|
|
943
833
|
};
|
|
944
834
|
var resultIssue = (expression, type, proof) => {
|
|
945
835
|
if (integerType(type)) {
|
|
@@ -967,7 +857,7 @@ var resultIssue = (expression, type, proof) => {
|
|
|
967
857
|
const a = lhs?.kind === "float" ? lhs.value : endpoint;
|
|
968
858
|
const b = rhs?.kind === "float" ? rhs.value : endpoint;
|
|
969
859
|
const value = expression.operator === "add" ? a + b : a - b;
|
|
970
|
-
return Number.isFinite(
|
|
860
|
+
return Number.isFinite(roundFloat3(value, type));
|
|
971
861
|
});
|
|
972
862
|
if (finite) {
|
|
973
863
|
return;
|
|
@@ -1172,6 +1062,7 @@ var directWrites = (item) => {
|
|
|
1172
1062
|
return [];
|
|
1173
1063
|
};
|
|
1174
1064
|
var verified = Symbol("verified core");
|
|
1065
|
+
|
|
1175
1066
|
// packages/compiler/src/ir/diagnostics.ts
|
|
1176
1067
|
function error(code, message, span) {
|
|
1177
1068
|
return {
|
|
@@ -1208,35 +1099,6 @@ ${" ".repeat(String(span.line).length + 3 + span.column - 1)}${"^".repeat(width)
|
|
|
1208
1099
|
}
|
|
1209
1100
|
return result;
|
|
1210
1101
|
}
|
|
1211
|
-
// packages/compiler/src/ir/job-errors.ts
|
|
1212
|
-
var jobError = {
|
|
1213
|
-
cancelled: 1,
|
|
1214
|
-
timeout: 2,
|
|
1215
|
-
tooLarge: 3,
|
|
1216
|
-
malformedResponse: 4,
|
|
1217
|
-
network: 5,
|
|
1218
|
-
tls: 6,
|
|
1219
|
-
noRequestSlot: 7
|
|
1220
|
-
};
|
|
1221
|
-
var httpErrors = { base: 1e4, last: 10999 };
|
|
1222
|
-
var jobErrors = {
|
|
1223
|
-
[jobError.cancelled]: "cancelled",
|
|
1224
|
-
[jobError.timeout]: "timeout",
|
|
1225
|
-
[jobError.tooLarge]: "request or response too large to encode",
|
|
1226
|
-
[jobError.malformedResponse]: "malformed response",
|
|
1227
|
-
[jobError.network]: "network error or server restarted",
|
|
1228
|
-
[jobError.tls]: "TLS error",
|
|
1229
|
-
[jobError.noRequestSlot]: "no free request slot",
|
|
1230
|
-
[httpErrors.base]: "HTTP status: 10000 + status code"
|
|
1231
|
-
};
|
|
1232
|
-
var errorName = (code) => {
|
|
1233
|
-
if (code > httpErrors.base && code <= httpErrors.last) {
|
|
1234
|
-
return `HTTP ${code - httpErrors.base}`;
|
|
1235
|
-
}
|
|
1236
|
-
return code < httpErrors.base ? jobErrors[code] : undefined;
|
|
1237
|
-
};
|
|
1238
|
-
// packages/compiler/src/ir/layouts.ts
|
|
1239
|
-
import { elementaryType as elementaryType2, integerRange as integerRange3 } from "@tsplc/iec";
|
|
1240
1102
|
|
|
1241
1103
|
// packages/compiler/src/ir/normalization-error.ts
|
|
1242
1104
|
class InvalidCore extends Error {
|
|
@@ -1250,1291 +1112,1754 @@ function reject(message, span) {
|
|
|
1250
1112
|
throw new InvalidCore(error("PLC6001", message, span));
|
|
1251
1113
|
}
|
|
1252
1114
|
|
|
1253
|
-
// packages/compiler/src/ir/
|
|
1254
|
-
var
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1115
|
+
// packages/compiler/src/ir/routine.ts
|
|
1116
|
+
var join = (branches, initialized) => {
|
|
1117
|
+
const [falling, ...others] = branches.filter((branch) => branch.flow === "fallthrough");
|
|
1118
|
+
if (falling) {
|
|
1119
|
+
return {
|
|
1120
|
+
flow: "fallthrough",
|
|
1121
|
+
initialized: new Set([...falling.initialized].filter((id) => others.every((branch) => branch.initialized.has(id))))
|
|
1122
|
+
};
|
|
1261
1123
|
}
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1124
|
+
const [{ flow }] = branches;
|
|
1125
|
+
return {
|
|
1126
|
+
flow: branches.every((branch) => branch.flow === flow) ? flow : "mixed",
|
|
1127
|
+
initialized
|
|
1128
|
+
};
|
|
1129
|
+
};
|
|
1130
|
+
var temporaryPrefix = (routine) => `${routine}:temporary:`;
|
|
1131
|
+
var temporaryId = (routine, index) => `${temporaryPrefix(routine)}${index}`;
|
|
1132
|
+
function isTemporary(routine, variable) {
|
|
1133
|
+
const prefix = temporaryPrefix(routine);
|
|
1134
|
+
return variable.storage === "local" && variable.id.startsWith(prefix) && /^\d+$/.test(variable.id.slice(prefix.length));
|
|
1135
|
+
}
|
|
1136
|
+
|
|
1137
|
+
class RoutineNormalizer {
|
|
1138
|
+
declarations;
|
|
1139
|
+
fn;
|
|
1140
|
+
block;
|
|
1141
|
+
owner;
|
|
1142
|
+
scope;
|
|
1143
|
+
sourceScope;
|
|
1144
|
+
locals;
|
|
1145
|
+
ranges = new Map;
|
|
1146
|
+
counters = new Set;
|
|
1147
|
+
callees = new Set;
|
|
1148
|
+
temporaryCount = 0;
|
|
1149
|
+
constructor(routine, declarations) {
|
|
1150
|
+
this.declarations = declarations;
|
|
1151
|
+
this.block = "inputs" in routine ? routine : undefined;
|
|
1152
|
+
this.owner = this.block ?? ("owner" in routine && routine.owner ? declarations.blocks.get(routine.owner.block) : undefined);
|
|
1153
|
+
const parameters = "parameters" in routine ? routine.parameters : routine.inputs;
|
|
1154
|
+
this.fn = {
|
|
1155
|
+
id: routine.id,
|
|
1156
|
+
name: routine.name,
|
|
1157
|
+
..."owner" in routine && routine.owner && { owner: routine.owner },
|
|
1158
|
+
..."origin" in routine && routine.origin && { origin: routine.origin },
|
|
1159
|
+
parameters,
|
|
1160
|
+
locals: routine.locals,
|
|
1161
|
+
body: routine.body,
|
|
1162
|
+
span: routine.span,
|
|
1163
|
+
...routine.result && { result: routine.result }
|
|
1164
|
+
};
|
|
1165
|
+
this.scope = new Map(declarations.globals);
|
|
1166
|
+
for (const item of [
|
|
1167
|
+
...parameters,
|
|
1168
|
+
...this.owner ? [...this.owner.state, ...this.owner.outputs] : [],
|
|
1169
|
+
...routine.locals
|
|
1170
|
+
]) {
|
|
1171
|
+
this.scope.set(item.id, item);
|
|
1274
1172
|
}
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1173
|
+
this.sourceScope = new Map(this.scope);
|
|
1174
|
+
this.locals = [...routine.locals];
|
|
1175
|
+
}
|
|
1176
|
+
normalize() {
|
|
1177
|
+
if (this.block?.standard) {
|
|
1178
|
+
return { routine: { ...this.block, body: [] }, callees: this.callees };
|
|
1278
1179
|
}
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1180
|
+
const normalized = this.body(this.fn.body, new Set(this.fn.parameters.map((item) => item.id)));
|
|
1181
|
+
if (this.fn.result && normalized.flow !== "return") {
|
|
1182
|
+
reject("A typed helper must return on every path.", this.fn.span);
|
|
1183
|
+
}
|
|
1184
|
+
const routine = this.block ? { ...this.block, locals: this.locals, body: normalized.body } : { ...this.fn, locals: this.locals, body: normalized.body };
|
|
1185
|
+
return { routine, callees: this.callees };
|
|
1285
1186
|
}
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1187
|
+
temporary(type, span) {
|
|
1188
|
+
let id;
|
|
1189
|
+
do {
|
|
1190
|
+
id = temporaryId(this.fn.id, this.temporaryCount++);
|
|
1191
|
+
} while (this.declarations.hasId(id));
|
|
1192
|
+
this.declarations.claimId(id);
|
|
1193
|
+
const item = {
|
|
1194
|
+
id,
|
|
1195
|
+
name: `temporary_${this.temporaryCount}`,
|
|
1196
|
+
type,
|
|
1197
|
+
storage: "local",
|
|
1198
|
+
mutable: true,
|
|
1199
|
+
retain: false,
|
|
1200
|
+
span
|
|
1201
|
+
};
|
|
1202
|
+
this.scope.set(id, item);
|
|
1203
|
+
this.locals.push(item);
|
|
1204
|
+
return item;
|
|
1205
|
+
}
|
|
1206
|
+
load(variable, span) {
|
|
1207
|
+
return { kind: "load", root: variable.id, type: variable.type, span };
|
|
1208
|
+
}
|
|
1209
|
+
receiverBlock(id) {
|
|
1210
|
+
const variable = this.sourceScope.get(id);
|
|
1211
|
+
if (variable?.storage !== "instance" && variable?.mode !== "inOut") {
|
|
1212
|
+
return;
|
|
1292
1213
|
}
|
|
1214
|
+
return instancedBlock(variable.type, this.declarations.types)?.block;
|
|
1293
1215
|
}
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
reject(`Recursive or excessively nested by-value layout ${id}`);
|
|
1216
|
+
verifyCall(id, arguments_, span, instance) {
|
|
1217
|
+
const called = this.declarations.signatures.get(id);
|
|
1218
|
+
if (!called || called.parameters.length !== arguments_.length) {
|
|
1219
|
+
reject("Unknown helper or wrong argument count.", span);
|
|
1299
1220
|
}
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1221
|
+
if (called.owner) {
|
|
1222
|
+
if (instance) {
|
|
1223
|
+
if (this.receiverBlock(instance) !== called.owner.block) {
|
|
1224
|
+
reject("Method calls require a matching static block instance.", span);
|
|
1225
|
+
}
|
|
1226
|
+
} else if (this.owner?.id !== called.owner.block) {
|
|
1227
|
+
reject("An implicit method call requires its owning block scope.", span);
|
|
1304
1228
|
}
|
|
1305
|
-
|
|
1229
|
+
if (called.owner.access !== "public" && this.owner?.id !== called.owner.block) {
|
|
1230
|
+
reject("Private and protected methods require their owning block scope.", span);
|
|
1231
|
+
}
|
|
1232
|
+
} else if (instance) {
|
|
1233
|
+
reject("Ordinary functions cannot have an instance receiver.", span);
|
|
1306
1234
|
}
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1235
|
+
this.callees.add(id);
|
|
1236
|
+
arguments_.forEach((argument, index) => {
|
|
1237
|
+
if (argument.type !== called.parameters[index]?.type) {
|
|
1238
|
+
reject("Helper argument has the wrong PLC type.", argument.span);
|
|
1239
|
+
}
|
|
1240
|
+
});
|
|
1241
|
+
return called;
|
|
1242
|
+
}
|
|
1243
|
+
expression(value, initialized, output, allowAggregateLoad = false) {
|
|
1244
|
+
const type = this.declarations.types.get(value.type);
|
|
1245
|
+
if (!type || !this.declarations.isValueType(value.type)) {
|
|
1246
|
+
reject("Unknown or non-value expression type.", value.span);
|
|
1312
1247
|
}
|
|
1313
|
-
if (
|
|
1314
|
-
reject(
|
|
1248
|
+
if (value.kind === "borrow") {
|
|
1249
|
+
reject("Borrowed places are only valid in block update arguments.", value.span);
|
|
1315
1250
|
}
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1251
|
+
const common = { type: value.type, span: value.span };
|
|
1252
|
+
if (value.kind === "length") {
|
|
1253
|
+
const array = this.declarations.types.get(value.operand.type);
|
|
1254
|
+
if (array?.kind !== "array" || value.type !== "DINT") {
|
|
1255
|
+
reject("Length requires a fixed array and DINT result.", value.span);
|
|
1256
|
+
}
|
|
1257
|
+
if (value.operand.kind === "load") {
|
|
1258
|
+
const source = this.sourceScope.get(value.operand.place.root);
|
|
1259
|
+
const checked = placeType(value.operand.place, this.declarations.types, this.sourceScope, this.ranges);
|
|
1260
|
+
if (!source || checked.issue || checked.type !== value.operand.type || source.storage === "local" && !initialized.has(source.id)) {
|
|
1261
|
+
reject(checked.issue ?? "Invalid array receiver.", value.span);
|
|
1262
|
+
}
|
|
1263
|
+
this.normalizePath(value.operand.place, initialized, output);
|
|
1264
|
+
} else {
|
|
1265
|
+
this.expression(value.operand, initialized, output, true);
|
|
1266
|
+
}
|
|
1326
1267
|
return {
|
|
1327
|
-
|
|
1328
|
-
|
|
1268
|
+
...common,
|
|
1269
|
+
kind: "constant",
|
|
1270
|
+
value: { kind: "integer", value: String(array.length) }
|
|
1329
1271
|
};
|
|
1330
1272
|
}
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
measure(id, 0);
|
|
1335
|
-
}
|
|
1336
|
-
return new Map([...layouts].map(([id, layout]) => [id, layout.size]));
|
|
1337
|
-
};
|
|
1338
|
-
var layoutIssue = (types) => {
|
|
1339
|
-
try {
|
|
1340
|
-
admitLayouts(types);
|
|
1341
|
-
return;
|
|
1342
|
-
} catch (cause) {
|
|
1343
|
-
if (cause instanceof InvalidCore) {
|
|
1344
|
-
return cause.diagnostic.message;
|
|
1345
|
-
}
|
|
1346
|
-
throw cause;
|
|
1347
|
-
}
|
|
1348
|
-
};
|
|
1349
|
-
// packages/compiler/src/ir/allocation.ts
|
|
1350
|
-
function checkStaticAllocation(module, declarations) {
|
|
1351
|
-
const { types, blocks, layoutSizes } = declarations;
|
|
1352
|
-
const payload = (id) => layoutSizes.get(id);
|
|
1353
|
-
const instanceSize = (id) => {
|
|
1354
|
-
const type = types.get(id);
|
|
1355
|
-
return type.kind === "array" ? type.length * instanceSize(type.element) : allocation(type.kind === "block" ? type.block : "").size;
|
|
1356
|
-
};
|
|
1357
|
-
const allocations = new Map;
|
|
1358
|
-
const allocating = new Set;
|
|
1359
|
-
const allocation = (id) => {
|
|
1360
|
-
if (allocating.size >= maxNestingDepth) {
|
|
1361
|
-
reject(`Static block allocation nesting exceeds ${maxNestingDepth} levels.`, blocks.get(id)?.span);
|
|
1362
|
-
}
|
|
1363
|
-
if (allocating.has(id)) {
|
|
1364
|
-
reject("Recursive static block allocation.", blocks.get(id)?.span);
|
|
1365
|
-
}
|
|
1366
|
-
const cached = allocations.get(id);
|
|
1367
|
-
if (cached) {
|
|
1368
|
-
return cached;
|
|
1369
|
-
}
|
|
1370
|
-
allocating.add(id);
|
|
1371
|
-
const block = blocks.get(id);
|
|
1372
|
-
const children = block.state.filter((item) => item.storage === "instance").map((item) => ({
|
|
1373
|
-
height: allocation(instancedBlock(item.type, types).block).height,
|
|
1374
|
-
size: instanceSize(item.type)
|
|
1375
|
-
}));
|
|
1376
|
-
const own = [
|
|
1377
|
-
...block.inputs,
|
|
1378
|
-
...block.outputs,
|
|
1379
|
-
...block.state.filter((item) => item.storage !== "instance")
|
|
1380
|
-
].reduce((sum, item) => sum + (item.mode === "inOut" ? 1 : payload(item.type)), 1);
|
|
1381
|
-
const size = own + children.reduce((sum, child) => sum + child.size, 0);
|
|
1382
|
-
const height = children.length ? 1 + Math.max(...children.map((child) => child.height)) : 1;
|
|
1383
|
-
if (size > maxScalarElements || height > maxNestingDepth) {
|
|
1384
|
-
reject("Static block allocation exceeds its finite budget.", block.span);
|
|
1385
|
-
}
|
|
1386
|
-
allocating.delete(id);
|
|
1387
|
-
const result = { size, height };
|
|
1388
|
-
allocations.set(id, result);
|
|
1389
|
-
return result;
|
|
1390
|
-
};
|
|
1391
|
-
for (const block of module.blocks) {
|
|
1392
|
-
allocation(block.id);
|
|
1393
|
-
}
|
|
1394
|
-
const staticPayload = module.variables.reduce((sum, variable) => {
|
|
1395
|
-
return sum + (instancedBlock(variable.type, types) ? instanceSize(variable.type) : payload(variable.type));
|
|
1396
|
-
}, 0);
|
|
1397
|
-
if (staticPayload > maxScalarElements) {
|
|
1398
|
-
reject("Static module storage exceeds one million scalar elements.");
|
|
1399
|
-
}
|
|
1400
|
-
}
|
|
1401
|
-
|
|
1402
|
-
// packages/compiler/src/ir/declarations.ts
|
|
1403
|
-
var matchesPorts = (variables, schema) => variables.length === schema.length && schema.every(([name, , type]) => variables.some((variable) => variable.name === name && variable.type === type && !variable.mode && !variable.retain));
|
|
1404
|
-
var checkStandardBlock = (block, standard) => {
|
|
1405
|
-
const expected = Object.hasOwn(standardBlocks, standard) ? standardBlocks[standard] : undefined;
|
|
1406
|
-
if (!expected || block.name !== standard || block.body.length || block.state.length || block.locals.length || !matchesPorts(block.inputs, expected.inputs) || !matchesPorts(block.outputs, expected.outputs) || block.outputs.some(({ initial }) => {
|
|
1407
|
-
switch (initial?.kind) {
|
|
1408
|
-
case "bool":
|
|
1409
|
-
return initial.value !== false;
|
|
1410
|
-
case "duration":
|
|
1411
|
-
return initial.nanoseconds !== "0";
|
|
1412
|
-
case "integer":
|
|
1413
|
-
return initial.value !== "0";
|
|
1414
|
-
default:
|
|
1415
|
-
return true;
|
|
1416
|
-
}
|
|
1417
|
-
})) {
|
|
1418
|
-
reject("Invalid canonical standard block definition.", block.span);
|
|
1419
|
-
}
|
|
1420
|
-
};
|
|
1421
|
-
|
|
1422
|
-
class ModuleDeclarations {
|
|
1423
|
-
ids = new Set;
|
|
1424
|
-
types = new Map;
|
|
1425
|
-
layoutSizes;
|
|
1426
|
-
globals = new Map;
|
|
1427
|
-
blocks;
|
|
1428
|
-
signatures;
|
|
1429
|
-
constructor(module) {
|
|
1430
|
-
for (const type of module.types) {
|
|
1431
|
-
if (this.types.has(type.id)) {
|
|
1432
|
-
reject(`Duplicate type ${type.id}.`);
|
|
1273
|
+
if (value.kind === "conditional") {
|
|
1274
|
+
if (value.condition.type !== "BOOL" || value.then.type !== value.type || value.else.type !== value.type) {
|
|
1275
|
+
reject("Invalid conditional types.", value.span);
|
|
1433
1276
|
}
|
|
1434
|
-
this.
|
|
1277
|
+
const condition = this.expression(value.condition, initialized, output);
|
|
1278
|
+
const result = this.temporary(value.type, value.span);
|
|
1279
|
+
const branch = (operand) => {
|
|
1280
|
+
const statements = [];
|
|
1281
|
+
const evaluated = this.expression(operand, new Set(initialized), statements);
|
|
1282
|
+
statements.push({
|
|
1283
|
+
kind: "assign",
|
|
1284
|
+
root: result.id,
|
|
1285
|
+
value: evaluated,
|
|
1286
|
+
span: value.span
|
|
1287
|
+
});
|
|
1288
|
+
return statements;
|
|
1289
|
+
};
|
|
1290
|
+
output.push({
|
|
1291
|
+
kind: "if",
|
|
1292
|
+
condition,
|
|
1293
|
+
then: branch(value.then),
|
|
1294
|
+
else: branch(value.else),
|
|
1295
|
+
span: value.span
|
|
1296
|
+
});
|
|
1297
|
+
initialized.add(result.id);
|
|
1298
|
+
return this.load(result, value.span);
|
|
1435
1299
|
}
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1300
|
+
if (value.kind === "call") {
|
|
1301
|
+
const called = this.verifyCall(value.function, value.arguments, value.span, value.instance);
|
|
1302
|
+
if (called.result !== value.type) {
|
|
1303
|
+
reject("Expression call requires a matching result type.", value.span);
|
|
1304
|
+
}
|
|
1305
|
+
const index = value.instance && this.instanceIndex(value.instance, value.index, initialized, output, value.span);
|
|
1306
|
+
const arguments_ = this.arguments(called.parameters, value.arguments, initialized, output);
|
|
1307
|
+
const result = this.temporary(value.type, value.span);
|
|
1308
|
+
output.push({
|
|
1309
|
+
kind: "invoke",
|
|
1310
|
+
function: value.function,
|
|
1311
|
+
...value.instance && { instance: value.instance },
|
|
1312
|
+
...index && { index },
|
|
1313
|
+
arguments: arguments_,
|
|
1314
|
+
destination: result.id,
|
|
1315
|
+
span: value.span
|
|
1316
|
+
});
|
|
1317
|
+
initialized.add(result.id);
|
|
1318
|
+
return this.load(result, value.span);
|
|
1439
1319
|
}
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
"module",
|
|
1444
|
-
"instance",
|
|
1445
|
-
"input",
|
|
1446
|
-
"output",
|
|
1447
|
-
"constant"
|
|
1448
|
-
]);
|
|
1449
|
-
this.globals.set(item.id, item);
|
|
1320
|
+
const issue = expressionIssue(value, this.declarations.types, this.scope, this.ranges);
|
|
1321
|
+
if (issue) {
|
|
1322
|
+
reject(issue, value.span);
|
|
1450
1323
|
}
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1324
|
+
switch (value.kind) {
|
|
1325
|
+
case "copy":
|
|
1326
|
+
return this.expression(value.operand, initialized, output, true);
|
|
1327
|
+
case "record":
|
|
1328
|
+
case "array": {
|
|
1329
|
+
const result = this.temporary(value.type, value.span);
|
|
1330
|
+
if (value.kind === "record") {
|
|
1331
|
+
for (const [name, operand] of Object.entries(value.fields)) {
|
|
1332
|
+
output.push({
|
|
1333
|
+
kind: "assign",
|
|
1334
|
+
root: result.id,
|
|
1335
|
+
path: [{ kind: "field", name }],
|
|
1336
|
+
value: this.expression(operand, initialized, output),
|
|
1337
|
+
span: operand.span
|
|
1338
|
+
});
|
|
1339
|
+
}
|
|
1340
|
+
} else {
|
|
1341
|
+
if (type.kind !== "array") {
|
|
1342
|
+
reject("Invalid array construction.", value.span);
|
|
1343
|
+
}
|
|
1344
|
+
value.items.forEach((operand, index) => {
|
|
1345
|
+
output.push({
|
|
1346
|
+
kind: "assign",
|
|
1347
|
+
root: result.id,
|
|
1348
|
+
path: [
|
|
1349
|
+
{
|
|
1350
|
+
kind: "index",
|
|
1351
|
+
value: {
|
|
1352
|
+
kind: "constant",
|
|
1353
|
+
type: "DINT",
|
|
1354
|
+
value: {
|
|
1355
|
+
kind: "integer",
|
|
1356
|
+
value: String(type.lowerBound + index)
|
|
1357
|
+
},
|
|
1358
|
+
span: operand.span
|
|
1359
|
+
}
|
|
1360
|
+
}
|
|
1361
|
+
],
|
|
1362
|
+
value: this.expression(operand, initialized, output),
|
|
1363
|
+
span: operand.span
|
|
1364
|
+
});
|
|
1365
|
+
});
|
|
1464
1366
|
}
|
|
1367
|
+
initialized.add(result.id);
|
|
1368
|
+
return this.load(result, value.span);
|
|
1465
1369
|
}
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
}
|
|
1470
|
-
for (const block of module.blocks) {
|
|
1471
|
-
this.claimId(block.id, block.span);
|
|
1472
|
-
for (const input of block.inputs) {
|
|
1473
|
-
this.checkVariable(input, ["parameter"], true);
|
|
1474
|
-
if (!input.mode && input.mutable) {
|
|
1475
|
-
reject("Value block inputs are readonly.", input.span);
|
|
1370
|
+
case "constant": {
|
|
1371
|
+
if (!aggregate(type)) {
|
|
1372
|
+
return { ...common, kind: "constant", value: value.value };
|
|
1476
1373
|
}
|
|
1374
|
+
const result = this.temporary(value.type, value.span);
|
|
1375
|
+
output.push({
|
|
1376
|
+
kind: "assign",
|
|
1377
|
+
root: result.id,
|
|
1378
|
+
value: { ...common, kind: "constant", value: value.value },
|
|
1379
|
+
span: value.span
|
|
1380
|
+
});
|
|
1381
|
+
initialized.add(result.id);
|
|
1382
|
+
return this.load(result, value.span);
|
|
1477
1383
|
}
|
|
1478
|
-
|
|
1479
|
-
this.
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
}
|
|
1484
|
-
for (const local of block.locals) {
|
|
1485
|
-
this.checkVariable(local, ["local"]);
|
|
1486
|
-
}
|
|
1487
|
-
for (const ports of [block.inputs, block.outputs]) {
|
|
1488
|
-
if (new Set(ports.map((variable) => variable.name)).size !== ports.length) {
|
|
1489
|
-
reject("Block input and output names must each be unique within their schema.", block.span);
|
|
1384
|
+
case "load": {
|
|
1385
|
+
const source = this.sourceScope.get(value.place.root);
|
|
1386
|
+
const checked = placeType(value.place, this.declarations.types, this.sourceScope, this.ranges);
|
|
1387
|
+
if (!source || checked.issue || checked.type !== value.type) {
|
|
1388
|
+
reject(checked.issue ?? "Invalid PLC load.", value.span);
|
|
1490
1389
|
}
|
|
1390
|
+
if (aggregate(type) && !allowAggregateLoad) {
|
|
1391
|
+
reject("Aggregate loads require explicit copy or a readonly value argument.", value.span);
|
|
1392
|
+
}
|
|
1393
|
+
if (source.storage === "local" && !initialized.has(source.id)) {
|
|
1394
|
+
reject("Local read before initialization.", value.span);
|
|
1395
|
+
}
|
|
1396
|
+
const first = value.place.path[0];
|
|
1397
|
+
if (source.storage === "instance" && value.place.path.length === 1 && first?.kind === "field" && first.name === "out" && type.kind === "record") {
|
|
1398
|
+
const result = this.temporary(value.type, value.span);
|
|
1399
|
+
for (const field of type.fields) {
|
|
1400
|
+
const selected = {
|
|
1401
|
+
kind: "load",
|
|
1402
|
+
type: field.type,
|
|
1403
|
+
place: {
|
|
1404
|
+
root: source.id,
|
|
1405
|
+
path: [
|
|
1406
|
+
...value.place.path,
|
|
1407
|
+
{ kind: "field", name: field.name }
|
|
1408
|
+
]
|
|
1409
|
+
},
|
|
1410
|
+
span: value.span
|
|
1411
|
+
};
|
|
1412
|
+
const captured = this.expression(selected, initialized, output, true);
|
|
1413
|
+
output.push({
|
|
1414
|
+
kind: "assign",
|
|
1415
|
+
root: result.id,
|
|
1416
|
+
path: [{ kind: "field", name: field.name }],
|
|
1417
|
+
value: captured,
|
|
1418
|
+
span: value.span
|
|
1419
|
+
});
|
|
1420
|
+
}
|
|
1421
|
+
initialized.add(result.id);
|
|
1422
|
+
return this.load(result, value.span);
|
|
1423
|
+
}
|
|
1424
|
+
const path = this.normalizePath(value.place, initialized, output);
|
|
1425
|
+
if (source.storage === "constant" && source.initial && !path.length) {
|
|
1426
|
+
return { ...common, kind: "constant", value: source.initial };
|
|
1427
|
+
}
|
|
1428
|
+
const result = this.temporary(value.type, value.span);
|
|
1429
|
+
output.push({
|
|
1430
|
+
kind: "assign",
|
|
1431
|
+
root: result.id,
|
|
1432
|
+
value: {
|
|
1433
|
+
...common,
|
|
1434
|
+
kind: "load",
|
|
1435
|
+
root: source.id,
|
|
1436
|
+
...path.length && { path }
|
|
1437
|
+
},
|
|
1438
|
+
span: value.span
|
|
1439
|
+
});
|
|
1440
|
+
const bound = interval(value, this.declarations.types, this.scope, this.ranges);
|
|
1441
|
+
if (bound) {
|
|
1442
|
+
this.ranges.set(result.id, bound);
|
|
1443
|
+
}
|
|
1444
|
+
initialized.add(result.id);
|
|
1445
|
+
return this.load(result, value.span);
|
|
1491
1446
|
}
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
if (block.standard) {
|
|
1497
|
-
checkStandardBlock(block, block.standard);
|
|
1447
|
+
case "unary": {
|
|
1448
|
+
const operand = this.expression(value.operand, initialized, output);
|
|
1449
|
+
const constant = fold(value, this.scope, this.declarations.types);
|
|
1450
|
+
return constant ? { ...common, kind: "constant", value: constant } : { ...common, kind: "unary", operator: value.operator, operand };
|
|
1498
1451
|
}
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
reject("Block type differs from its definition.");
|
|
1452
|
+
case "convert": {
|
|
1453
|
+
const operand = this.expression(value.operand, initialized, output);
|
|
1454
|
+
const constant = fold(value, this.scope, this.declarations.types);
|
|
1455
|
+
if (value.type === value.operand.type) {
|
|
1456
|
+
return operand;
|
|
1505
1457
|
}
|
|
1458
|
+
return constant ? { ...common, kind: "constant", value: constant } : { ...common, kind: "convert", operand, policy: value.policy };
|
|
1506
1459
|
}
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1460
|
+
case "binary": {
|
|
1461
|
+
const left = this.expression(value.left, initialized, output);
|
|
1462
|
+
if (value.operator === "and" || value.operator === "or") {
|
|
1463
|
+
const result = this.temporary("BOOL", value.span);
|
|
1464
|
+
const branch = [];
|
|
1465
|
+
output.push({
|
|
1466
|
+
kind: "assign",
|
|
1467
|
+
root: result.id,
|
|
1468
|
+
value: left,
|
|
1469
|
+
span: value.span
|
|
1470
|
+
});
|
|
1471
|
+
const right = this.expression(value.right, new Set(initialized), branch);
|
|
1472
|
+
branch.push({
|
|
1473
|
+
kind: "assign",
|
|
1474
|
+
root: result.id,
|
|
1475
|
+
value: right,
|
|
1476
|
+
span: value.span
|
|
1477
|
+
});
|
|
1478
|
+
const condition = value.operator === "and" ? this.load(result, value.span) : {
|
|
1479
|
+
kind: "unary",
|
|
1480
|
+
operator: "not",
|
|
1481
|
+
operand: this.load(result, value.span),
|
|
1482
|
+
type: "BOOL",
|
|
1483
|
+
span: value.span
|
|
1484
|
+
};
|
|
1485
|
+
output.push({
|
|
1486
|
+
kind: "if",
|
|
1487
|
+
condition,
|
|
1488
|
+
then: branch,
|
|
1489
|
+
else: [],
|
|
1490
|
+
span: value.span
|
|
1491
|
+
});
|
|
1492
|
+
initialized.add(result.id);
|
|
1493
|
+
return this.load(result, value.span);
|
|
1494
|
+
}
|
|
1495
|
+
const right = this.expression(value.right, initialized, output);
|
|
1496
|
+
const constant = fold(value, this.scope, this.declarations.types);
|
|
1497
|
+
if (constant) {
|
|
1498
|
+
return { ...common, kind: "constant", value: constant };
|
|
1499
|
+
}
|
|
1500
|
+
return {
|
|
1501
|
+
...common,
|
|
1502
|
+
kind: "binary",
|
|
1503
|
+
operator: value.operator,
|
|
1504
|
+
left,
|
|
1505
|
+
right,
|
|
1506
|
+
...value.policy && { policy: value.policy }
|
|
1507
|
+
};
|
|
1543
1508
|
}
|
|
1544
|
-
} else if (!this.isValueType(item.type) && !(allowBorrow && item.mode === "inOut" && type.kind === "block")) {
|
|
1545
|
-
reject("Block instances and reference schemas cannot be stored as values.", item.span);
|
|
1546
1509
|
}
|
|
1547
|
-
|
|
1548
|
-
|
|
1510
|
+
}
|
|
1511
|
+
normalizePath(place, initialized, output) {
|
|
1512
|
+
return place.path.map((segment) => segment.kind === "field" ? segment : {
|
|
1513
|
+
kind: "index",
|
|
1514
|
+
value: this.expression(segment.value, initialized, output)
|
|
1515
|
+
});
|
|
1516
|
+
}
|
|
1517
|
+
destination(place, type, initialized, span) {
|
|
1518
|
+
const target = this.sourceScope.get(place.root);
|
|
1519
|
+
const initializing = target?.storage === "local" && !initialized.has(place.root) && !place.path.length;
|
|
1520
|
+
const checked = placeType(place, this.declarations.types, this.sourceScope, this.ranges, !initializing);
|
|
1521
|
+
if (!target || checked.issue || checked.type !== type || this.counters.has(place.root)) {
|
|
1522
|
+
reject(checked.issue ?? "Invalid assignment destination.", span);
|
|
1549
1523
|
}
|
|
1550
|
-
if (
|
|
1551
|
-
reject("
|
|
1524
|
+
if (place.path.length && target.storage === "local" && !initialized.has(target.id)) {
|
|
1525
|
+
reject("Initialize the whole aggregate before assigning fields.", span);
|
|
1552
1526
|
}
|
|
1553
|
-
|
|
1554
|
-
|
|
1527
|
+
return target;
|
|
1528
|
+
}
|
|
1529
|
+
instanceIndex(instance, index, initialized, output, span) {
|
|
1530
|
+
if (!index) {
|
|
1531
|
+
return;
|
|
1555
1532
|
}
|
|
1556
|
-
|
|
1557
|
-
|
|
1533
|
+
const checked = placeType({ root: instance, path: [{ kind: "index", value: index }] }, this.declarations.types, this.sourceScope, this.ranges);
|
|
1534
|
+
if (checked.issue) {
|
|
1535
|
+
reject(checked.issue, span);
|
|
1558
1536
|
}
|
|
1537
|
+
return this.expression(index, initialized, output);
|
|
1559
1538
|
}
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
const routines = new Map([...functions, ...normalizedBlocks].map((fn) => [fn.id, fn]));
|
|
1566
|
-
checkRecursion(routines, edges, entries, declarations);
|
|
1567
|
-
checkLoopCounterWrites(module, declarations, routines, edges);
|
|
1568
|
-
const reachable = reachableRoutines(module, declarations, functions, entries, edges);
|
|
1569
|
-
const linkedFunctions = functions.filter((fn) => reachable.has(fn.id));
|
|
1570
|
-
const linkedBlocks = normalizedBlocks.filter((block) => reachable.has(block.id));
|
|
1571
|
-
const usedTypes = usedTypeIds(module, declarations.types, linkedFunctions, linkedBlocks);
|
|
1572
|
-
return {
|
|
1573
|
-
types: module.types.filter((type) => usedTypes.has(type.id)),
|
|
1574
|
-
variables: module.variables,
|
|
1575
|
-
functions: linkedFunctions,
|
|
1576
|
-
blocks: linkedBlocks,
|
|
1577
|
-
programs: module.programs,
|
|
1578
|
-
...module.folded && { folded: module.folded }
|
|
1579
|
-
};
|
|
1580
|
-
}
|
|
1581
|
-
var checkPrograms = (module, declarations) => {
|
|
1582
|
-
const names = new Set;
|
|
1583
|
-
const entries = new Set;
|
|
1584
|
-
for (const program of module.programs) {
|
|
1585
|
-
declarations.claimId(program.id);
|
|
1586
|
-
const entry = declarations.signatures.get(program.entry);
|
|
1587
|
-
if (!entry || entry.owner || entry.parameters.length || entry.result) {
|
|
1588
|
-
reject("Program entry must be a parameterless void helper.");
|
|
1539
|
+
blockUpdate(item, initialized, output) {
|
|
1540
|
+
const block = this.receiverBlock(item.instance);
|
|
1541
|
+
const called = block && this.declarations.blocks.get(block);
|
|
1542
|
+
if (!called) {
|
|
1543
|
+
reject("Updates require a statically allocated block instance.", item.span);
|
|
1589
1544
|
}
|
|
1590
|
-
|
|
1591
|
-
|
|
1545
|
+
const index = this.instanceIndex(item.instance, item.index, initialized, output, item.span);
|
|
1546
|
+
const supplied = Object.entries(item.inputs).flatMap(([name, value]) => {
|
|
1547
|
+
const input = called.inputs.find((port) => port.name === name);
|
|
1548
|
+
return input ? [{ input, value }] : [];
|
|
1549
|
+
});
|
|
1550
|
+
if (Object.keys(item.inputs).length !== called.inputs.length || supplied.length !== called.inputs.length) {
|
|
1551
|
+
reject("Block update must supply every named input exactly once.", item.span);
|
|
1592
1552
|
}
|
|
1593
|
-
|
|
1594
|
-
|
|
1553
|
+
this.callees.add(called.id);
|
|
1554
|
+
const inputs = Object.fromEntries(supplied.map(({ input, value }) => [
|
|
1555
|
+
input.name,
|
|
1556
|
+
this.argument(input, value, initialized, output)
|
|
1557
|
+
]));
|
|
1558
|
+
output.push({
|
|
1559
|
+
kind: "blockUpdate",
|
|
1560
|
+
instance: item.instance,
|
|
1561
|
+
...index && { index },
|
|
1562
|
+
inputs,
|
|
1563
|
+
span: item.span
|
|
1564
|
+
});
|
|
1595
1565
|
}
|
|
1596
|
-
|
|
1597
|
-
|
|
1566
|
+
arguments(parameters, values, initialized, output) {
|
|
1567
|
+
return values.map((value, index) => this.argument(parameters[index], value, initialized, output));
|
|
1598
1568
|
}
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
const visited = new Set;
|
|
1603
|
-
const active = new Set;
|
|
1604
|
-
const visit = (id) => {
|
|
1605
|
-
if (active.has(id)) {
|
|
1606
|
-
reject("Recursive scan calls are prohibited.", signatures.get(id)?.span);
|
|
1569
|
+
argument(input, value, initialized, output) {
|
|
1570
|
+
if (value.type !== input.type) {
|
|
1571
|
+
reject("Block input differs from its concrete PLC type.", value.span);
|
|
1607
1572
|
}
|
|
1608
|
-
if (
|
|
1609
|
-
return;
|
|
1573
|
+
if (input.mode === "inOut" && value.kind === "borrow") {
|
|
1574
|
+
return this.borrow(value, initialized, output);
|
|
1610
1575
|
}
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
reject("Program entries cannot be called as ordinary helpers.", signatures.get(id)?.span);
|
|
1576
|
+
if (input.mode === "inOut") {
|
|
1577
|
+
if (this.declarations.types.get(value.type)?.kind === "block") {
|
|
1578
|
+
reject("Pass a block instance by name.", value.span);
|
|
1615
1579
|
}
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
...routine.outputs,
|
|
1631
|
-
...routine.state,
|
|
1632
|
-
...routine.locals
|
|
1633
|
-
] : [...routine.parameters, ...routine.locals])
|
|
1634
|
-
].map((variable) => [variable.id, variable]));
|
|
1635
|
-
const calledRoutine = (item) => {
|
|
1636
|
-
if (item.kind === "invoke") {
|
|
1637
|
-
return item.function;
|
|
1580
|
+
const temporary = this.temporary(value.type, value.span);
|
|
1581
|
+
output.push({
|
|
1582
|
+
kind: "assign",
|
|
1583
|
+
root: temporary.id,
|
|
1584
|
+
value: this.expression(value, initialized, output),
|
|
1585
|
+
span: value.span
|
|
1586
|
+
});
|
|
1587
|
+
initialized.add(temporary.id);
|
|
1588
|
+
return {
|
|
1589
|
+
kind: "borrow",
|
|
1590
|
+
type: value.type,
|
|
1591
|
+
place: { root: temporary.id, path: [] },
|
|
1592
|
+
span: value.span
|
|
1593
|
+
};
|
|
1638
1594
|
}
|
|
1639
|
-
if (
|
|
1640
|
-
|
|
1595
|
+
if (value.kind === "borrow") {
|
|
1596
|
+
reject("Readonly value inputs cannot receive borrowed references.", value.span);
|
|
1641
1597
|
}
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
const
|
|
1648
|
-
if (
|
|
1649
|
-
|
|
1598
|
+
return this.expression(value, initialized, output, true);
|
|
1599
|
+
}
|
|
1600
|
+
borrow(value, initialized, output) {
|
|
1601
|
+
const block = this.declarations.types.get(value.type)?.kind === "block";
|
|
1602
|
+
const root = this.sourceScope.get(value.place.root);
|
|
1603
|
+
const checked = placeType(value.place, this.declarations.types, this.sourceScope, this.ranges, !block);
|
|
1604
|
+
if (!root || checked.issue || checked.type !== value.type || !block && !this.declarations.isValueType(value.type) || this.counters.has(root.id) || root.storage === "local" && !initialized.has(root.id)) {
|
|
1605
|
+
reject(checked.issue ?? "Borrow requires initialized mutable value storage of the exact type.", value.span);
|
|
1650
1606
|
}
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
}
|
|
1660
|
-
}
|
|
1607
|
+
return {
|
|
1608
|
+
kind: "borrow",
|
|
1609
|
+
type: value.type,
|
|
1610
|
+
place: {
|
|
1611
|
+
root: value.place.root,
|
|
1612
|
+
path: this.normalizePath(value.place, initialized, output)
|
|
1613
|
+
},
|
|
1614
|
+
span: value.span
|
|
1661
1615
|
};
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1616
|
+
}
|
|
1617
|
+
body(statements, initialized) {
|
|
1618
|
+
const output = [];
|
|
1619
|
+
let state = { flow: "fallthrough", initialized };
|
|
1620
|
+
for (const item of statements) {
|
|
1621
|
+
if (state.flow !== "fallthrough") {
|
|
1622
|
+
break;
|
|
1666
1623
|
}
|
|
1624
|
+
state = this.statement(item, state.initialized, output);
|
|
1667
1625
|
}
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1626
|
+
return { body: output, ...state };
|
|
1627
|
+
}
|
|
1628
|
+
statement(item, initialized, output) {
|
|
1629
|
+
switch (item.kind) {
|
|
1630
|
+
case "assign":
|
|
1631
|
+
this.assign(item, initialized, output);
|
|
1632
|
+
return { flow: "fallthrough", initialized };
|
|
1633
|
+
case "blockUpdate":
|
|
1634
|
+
this.blockUpdate(item, initialized, output);
|
|
1635
|
+
return { flow: "fallthrough", initialized };
|
|
1636
|
+
case "invoke":
|
|
1637
|
+
this.invoke(item, initialized, output);
|
|
1638
|
+
return { flow: "fallthrough", initialized };
|
|
1639
|
+
case "if":
|
|
1640
|
+
return this.ifStatement(item, initialized, output);
|
|
1641
|
+
case "switch":
|
|
1642
|
+
return this.switchStatement(item, initialized, output);
|
|
1643
|
+
case "for":
|
|
1644
|
+
this.forStatement(item, initialized, output);
|
|
1645
|
+
return { flow: "fallthrough", initialized };
|
|
1646
|
+
case "return": {
|
|
1647
|
+
if (item.value?.type !== this.fn.result) {
|
|
1648
|
+
reject("Return differs from the function signature.", item.span);
|
|
1649
|
+
}
|
|
1650
|
+
const value = item.value && this.expression(item.value, initialized, output);
|
|
1651
|
+
output.push({
|
|
1652
|
+
kind: "return",
|
|
1653
|
+
span: item.span,
|
|
1654
|
+
...value && { value }
|
|
1655
|
+
});
|
|
1656
|
+
return { flow: "return", initialized };
|
|
1680
1657
|
}
|
|
1658
|
+
case "telemetry":
|
|
1659
|
+
this.telemetry(item, initialized, output);
|
|
1660
|
+
return { flow: "fallthrough", initialized };
|
|
1661
|
+
case "break":
|
|
1662
|
+
case "continue":
|
|
1663
|
+
if (!this.counters.size) {
|
|
1664
|
+
reject("Loop control appears outside a bounded loop.", item.span);
|
|
1665
|
+
}
|
|
1666
|
+
output.push({ kind: item.kind, span: item.span });
|
|
1667
|
+
return { flow: "loop", initialized };
|
|
1668
|
+
default:
|
|
1669
|
+
reject("Unknown PLC statement.");
|
|
1681
1670
|
}
|
|
1682
|
-
};
|
|
1683
|
-
for (const routine of routines.values()) {
|
|
1684
|
-
checkCalls(routine.body, new Set);
|
|
1685
1671
|
}
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
reachable.add(id);
|
|
1694
|
-
for (const child of edges.get(id) ?? []) {
|
|
1695
|
-
link(child);
|
|
1696
|
-
}
|
|
1697
|
-
const owner = signatures.get(id)?.owner;
|
|
1698
|
-
if (owner) {
|
|
1699
|
-
link(owner.block);
|
|
1672
|
+
assign(item, initialized, output) {
|
|
1673
|
+
this.destination(item.destination, item.value.type, initialized, item.span);
|
|
1674
|
+
const path = this.normalizePath(item.destination, initialized, output);
|
|
1675
|
+
const bound = !path.length && this.scope.get(item.destination.root)?.mutable === false && valueRange(item.value, this.declarations.types, this.scope, this.ranges, this.declarations.signatures);
|
|
1676
|
+
const value = this.expression(item.value, initialized, output);
|
|
1677
|
+
if (bound) {
|
|
1678
|
+
this.ranges.set(item.destination.root, bound);
|
|
1700
1679
|
}
|
|
1701
|
-
|
|
1702
|
-
|
|
1680
|
+
if (!path.length) {
|
|
1681
|
+
initialized.add(item.destination.root);
|
|
1703
1682
|
}
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1683
|
+
output.push({
|
|
1684
|
+
kind: "assign",
|
|
1685
|
+
root: item.destination.root,
|
|
1686
|
+
...path.length && { path },
|
|
1687
|
+
value,
|
|
1688
|
+
span: item.span
|
|
1689
|
+
});
|
|
1690
|
+
}
|
|
1691
|
+
invoke(item, initialized, output) {
|
|
1692
|
+
const called = this.verifyCall(item.function, item.arguments, item.span, item.instance);
|
|
1693
|
+
if (item.destination) {
|
|
1694
|
+
if (item.destination.path.length || !called.result) {
|
|
1695
|
+
reject("Invalid helper destination.", item.span);
|
|
1708
1696
|
}
|
|
1697
|
+
this.destination(item.destination, called.result, initialized, item.span);
|
|
1709
1698
|
}
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
1699
|
+
const index = item.instance && this.instanceIndex(item.instance, item.index, initialized, output, item.span);
|
|
1700
|
+
const arguments_ = this.arguments(called.parameters, item.arguments, initialized, output);
|
|
1701
|
+
output.push({
|
|
1702
|
+
kind: "invoke",
|
|
1703
|
+
function: item.function,
|
|
1704
|
+
...item.instance && { instance: item.instance },
|
|
1705
|
+
...index && { index },
|
|
1706
|
+
arguments: arguments_,
|
|
1707
|
+
span: item.span,
|
|
1708
|
+
...item.destination && { destination: item.destination.root }
|
|
1709
|
+
});
|
|
1710
|
+
if (item.destination) {
|
|
1711
|
+
initialized.add(item.destination.root);
|
|
1718
1712
|
}
|
|
1719
1713
|
}
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
const usedTypes = new Set;
|
|
1724
|
-
const useType = (id) => {
|
|
1725
|
-
if (usedTypes.has(id)) {
|
|
1726
|
-
return;
|
|
1714
|
+
ifStatement(item, initialized, output) {
|
|
1715
|
+
if (item.condition.type !== "BOOL") {
|
|
1716
|
+
reject("Conditions require BOOL.", item.span);
|
|
1727
1717
|
}
|
|
1728
|
-
|
|
1729
|
-
const
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1718
|
+
const condition = this.expression(item.condition, initialized, output);
|
|
1719
|
+
const originalRanges = new Map(this.ranges);
|
|
1720
|
+
const branch = (statements, truth) => {
|
|
1721
|
+
this.ranges.clear();
|
|
1722
|
+
for (const [root, range] of narrowRanges(item.condition, truth, this.declarations.types, this.sourceScope, originalRanges)) {
|
|
1723
|
+
this.ranges.set(root, range);
|
|
1724
|
+
}
|
|
1725
|
+
try {
|
|
1726
|
+
return this.body(statements, new Set(initialized));
|
|
1727
|
+
} finally {
|
|
1728
|
+
this.ranges.clear();
|
|
1729
|
+
for (const [root, range] of originalRanges) {
|
|
1730
|
+
this.ranges.set(root, range);
|
|
1731
|
+
}
|
|
1732
|
+
}
|
|
1733
|
+
};
|
|
1734
|
+
const yes = branch(item.then, true);
|
|
1735
|
+
const no = branch(item.else, false);
|
|
1736
|
+
output.push({
|
|
1737
|
+
kind: "if",
|
|
1738
|
+
condition,
|
|
1739
|
+
then: yes.body,
|
|
1740
|
+
else: no.body,
|
|
1741
|
+
span: item.span
|
|
1742
|
+
});
|
|
1743
|
+
return join([yes, no], initialized);
|
|
1744
|
+
}
|
|
1745
|
+
switchStatement(item, initialized, output) {
|
|
1746
|
+
const value = this.expression(item.value, initialized, output);
|
|
1747
|
+
const type = this.declarations.types.get(item.value.type);
|
|
1748
|
+
const seen = new Set;
|
|
1749
|
+
const cases = item.cases.map((branch) => {
|
|
1750
|
+
for (const constant of branch.values) {
|
|
1751
|
+
if (!valueFits(constant, type, this.declarations.types)) {
|
|
1752
|
+
reject("Invalid case constant.", item.span);
|
|
1753
|
+
}
|
|
1754
|
+
const key = JSON.stringify(constant);
|
|
1755
|
+
if (seen.has(key)) {
|
|
1756
|
+
reject("Duplicate case constant.", item.span);
|
|
1757
|
+
}
|
|
1758
|
+
seen.add(key);
|
|
1733
1759
|
}
|
|
1760
|
+
return {
|
|
1761
|
+
values: branch.values,
|
|
1762
|
+
result: this.body(branch.body, new Set(initialized))
|
|
1763
|
+
};
|
|
1764
|
+
});
|
|
1765
|
+
const fallback = this.body(item.fallback, new Set(initialized));
|
|
1766
|
+
output.push({
|
|
1767
|
+
kind: "switch",
|
|
1768
|
+
value,
|
|
1769
|
+
cases: cases.map((branch) => ({
|
|
1770
|
+
values: branch.values,
|
|
1771
|
+
body: branch.result.body
|
|
1772
|
+
})),
|
|
1773
|
+
fallback: fallback.body,
|
|
1774
|
+
span: item.span
|
|
1775
|
+
});
|
|
1776
|
+
return join([fallback, ...cases.map((branch) => branch.result)], initialized);
|
|
1777
|
+
}
|
|
1778
|
+
forStatement(item, initialized, output) {
|
|
1779
|
+
const counter = this.sourceScope.get(item.counter);
|
|
1780
|
+
const counterType = counter && this.declarations.types.get(counter.type);
|
|
1781
|
+
const storage = item.counterScope === "existing" ? ["local", "module", "field"] : ["local"];
|
|
1782
|
+
if (!counter || !storage.includes(counter.storage) || !counter.mutable || counterType?.kind !== "integer" || this.counters.has(counter.id) || item.start.type !== counter.type || item.limit.type !== counter.type || !canonicalInteger.test(item.step) || item.step === "0") {
|
|
1783
|
+
reject("Invalid bounded loop.", item.span);
|
|
1734
1784
|
}
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
|
|
1785
|
+
const start = fold(item.start, this.scope, this.declarations.types);
|
|
1786
|
+
const limit = fold(item.limit, this.scope, this.declarations.types);
|
|
1787
|
+
const startRange = interval(item.start, this.declarations.types, this.sourceScope, this.ranges);
|
|
1788
|
+
const limitRange = interval(item.limit, this.declarations.types, this.sourceScope, this.ranges);
|
|
1789
|
+
if (!startRange || !limitRange) {
|
|
1790
|
+
reject("Loop bounds require finite integer ranges.", item.span);
|
|
1791
|
+
}
|
|
1792
|
+
const step = BigInt(item.step);
|
|
1793
|
+
const { first, last, count, range } = loopBounds(startRange, limitRange, step, item.exclusive);
|
|
1794
|
+
const dynamic = start?.kind !== "integer" || limit?.kind !== "integer";
|
|
1795
|
+
if (count > maxLoopIterations || Number(count) !== item.maxIterations || !Number.isSafeInteger(item.maxIterations)) {
|
|
1796
|
+
reject("Loop iteration proof differs from its bounds.", item.span);
|
|
1797
|
+
}
|
|
1798
|
+
if (item.counterScope === "existing") {
|
|
1799
|
+
const lastValue = step > 0n ? range[1] : range[0];
|
|
1800
|
+
const terminal = dynamic ? lastValue + step : first + count * step;
|
|
1801
|
+
if (count > 0n && !admits(integerRange(counterType), terminal)) {
|
|
1802
|
+
reject("The existing loop counter overflows on termination.", item.span);
|
|
1739
1803
|
}
|
|
1740
1804
|
}
|
|
1741
|
-
|
|
1742
|
-
|
|
1805
|
+
const evaluatedStart = this.expression(item.start, initialized, output);
|
|
1806
|
+
const evaluatedLimit = this.expression(item.limit, initialized, output);
|
|
1807
|
+
initialized.add(counter.id);
|
|
1808
|
+
this.counters.add(counter.id);
|
|
1809
|
+
this.ranges.set(counter.id, range);
|
|
1810
|
+
const nested = this.body(item.body, new Set(initialized));
|
|
1811
|
+
this.counters.delete(counter.id);
|
|
1812
|
+
this.ranges.delete(counter.id);
|
|
1813
|
+
if (item.counterScope !== "existing") {
|
|
1814
|
+
initialized.delete(counter.id);
|
|
1743
1815
|
}
|
|
1744
|
-
|
|
1745
|
-
|
|
1816
|
+
output.push({
|
|
1817
|
+
kind: "for",
|
|
1818
|
+
counter: counter.id,
|
|
1819
|
+
...item.counterScope && { counterScope: item.counterScope },
|
|
1820
|
+
start: { kind: "integer", value: String(first) },
|
|
1821
|
+
limit: { kind: "integer", value: String(last) },
|
|
1822
|
+
...dynamic && {
|
|
1823
|
+
dynamic: {
|
|
1824
|
+
start: evaluatedStart,
|
|
1825
|
+
limit: evaluatedLimit,
|
|
1826
|
+
...item.exclusive && { exclusive: item.exclusive }
|
|
1827
|
+
}
|
|
1828
|
+
},
|
|
1829
|
+
step: item.step,
|
|
1830
|
+
maxIterations: item.maxIterations,
|
|
1831
|
+
body: nested.body,
|
|
1832
|
+
span: item.span
|
|
1833
|
+
});
|
|
1834
|
+
}
|
|
1835
|
+
telemetry(item, initialized, output) {
|
|
1836
|
+
const evaluate = (value) => this.expression(value, initialized, output);
|
|
1837
|
+
const { record } = item;
|
|
1838
|
+
output.push({
|
|
1839
|
+
kind: "telemetry",
|
|
1840
|
+
record: record.kind === "exception" ? { kind: "exception", code: evaluate(record.code) } : record,
|
|
1841
|
+
properties: item.properties.map(({ name, value, length }) => {
|
|
1842
|
+
if (!length) {
|
|
1843
|
+
return { name, value: evaluate(value) };
|
|
1844
|
+
}
|
|
1845
|
+
if (value.kind !== "borrow") {
|
|
1846
|
+
reject("Telemetry text reads a byte array in place.", value.span);
|
|
1847
|
+
}
|
|
1848
|
+
return {
|
|
1849
|
+
name,
|
|
1850
|
+
value: this.borrow(value, initialized, output),
|
|
1851
|
+
length: evaluate(length)
|
|
1852
|
+
};
|
|
1853
|
+
}),
|
|
1854
|
+
span: item.span
|
|
1855
|
+
});
|
|
1856
|
+
}
|
|
1857
|
+
}
|
|
1858
|
+
|
|
1859
|
+
// packages/compiler/src/ir/conditions.ts
|
|
1860
|
+
var noExits = { returns: [], breaks: [], continues: [] };
|
|
1861
|
+
var prefixed = (conditions, exits) => ({
|
|
1862
|
+
returns: exits.returns.map((item) => [...conditions, ...item]),
|
|
1863
|
+
breaks: exits.breaks.map((item) => [...conditions, ...item]),
|
|
1864
|
+
continues: exits.continues.map((item) => [...conditions, ...item])
|
|
1865
|
+
});
|
|
1866
|
+
var merged = (...all) => ({
|
|
1867
|
+
returns: all.flatMap((exits) => exits.returns),
|
|
1868
|
+
breaks: all.flatMap((exits) => exits.breaks),
|
|
1869
|
+
continues: all.flatMap((exits) => exits.continues)
|
|
1870
|
+
});
|
|
1871
|
+
var wholeAssign = (item, root) => item?.kind === "assign" && item.root === root && !item.path?.length;
|
|
1872
|
+
var loadedRoot = (value) => value.kind === "load" && !value.path?.length ? value.root : undefined;
|
|
1873
|
+
var constantIndex = (value) => value.kind === "constant" && value.value.kind === "integer" ? Number(value.value.value) : undefined;
|
|
1874
|
+
var constantPart = (value, segment, lower) => {
|
|
1875
|
+
if (segment.kind === "field") {
|
|
1876
|
+
return value.kind === "record" ? value.fields[segment.name] : undefined;
|
|
1877
|
+
}
|
|
1878
|
+
const index = segment.value.kind === "constant" && segment.value.value.kind === "integer" ? Number(segment.value.value.value) : undefined;
|
|
1879
|
+
return value.kind === "array" && index !== undefined ? value.items[index - lower] : undefined;
|
|
1880
|
+
};
|
|
1881
|
+
var staticNames = (path) => {
|
|
1882
|
+
const names = [];
|
|
1883
|
+
for (const segment of path) {
|
|
1884
|
+
if (segment.kind === "field") {
|
|
1885
|
+
names.push(segment.name);
|
|
1886
|
+
continue;
|
|
1746
1887
|
}
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
if (["bool", "integer", "bits", "float", "duration", "calendar"].includes(type.kind)) {
|
|
1750
|
-
useType(type.id);
|
|
1888
|
+
if (segment.value.kind !== "constant" || segment.value.value.kind !== "integer") {
|
|
1889
|
+
return;
|
|
1751
1890
|
}
|
|
1752
|
-
|
|
1753
|
-
for (const variable of module.variables) {
|
|
1754
|
-
useType(variable.type);
|
|
1891
|
+
names.push(segment.value.value.value);
|
|
1755
1892
|
}
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1893
|
+
return names;
|
|
1894
|
+
};
|
|
1895
|
+
var opaque = (reason, type, span, reads = []) => ({ kind: "opaque", reason, reads, type, span });
|
|
1896
|
+
var projectReconstruction = (node, path, type) => {
|
|
1897
|
+
const [segment, ...rest] = path;
|
|
1898
|
+
if (!segment) {
|
|
1899
|
+
return node;
|
|
1900
|
+
}
|
|
1901
|
+
switch (node.kind) {
|
|
1902
|
+
case "read":
|
|
1903
|
+
case "local":
|
|
1904
|
+
case "parameter":
|
|
1905
|
+
return { ...node, path: [...node.path, ...path], type };
|
|
1906
|
+
case "call": {
|
|
1907
|
+
const names = staticNames(path);
|
|
1908
|
+
return names ? { ...node, path: [...node.path, ...names], type } : opaque("a call result at a dynamic index", type, node.span);
|
|
1759
1909
|
}
|
|
1760
|
-
|
|
1761
|
-
|
|
1910
|
+
case "record": {
|
|
1911
|
+
const field = segment.kind === "field" ? node.fields[segment.name] : undefined;
|
|
1912
|
+
return field ? projectReconstruction(field, rest, type) : opaque("a field the record doesn't set", type, node.span);
|
|
1762
1913
|
}
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1914
|
+
case "array": {
|
|
1915
|
+
const index = segment.kind === "index" && segment.value.kind === "constant" && segment.value.value.kind === "integer" ? Number(segment.value.value.value) : undefined;
|
|
1916
|
+
const item = index === undefined ? undefined : node.items[index - node.lower];
|
|
1917
|
+
return item ? projectReconstruction(item, rest, type) : opaque("an element at a dynamic index", type, node.span);
|
|
1767
1918
|
}
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
...block.state,
|
|
1772
|
-
...block.locals
|
|
1773
|
-
]) {
|
|
1774
|
-
useType(variable.type);
|
|
1919
|
+
case "constant": {
|
|
1920
|
+
const part = constantPart(node.value, segment, 0);
|
|
1921
|
+
return part ? projectReconstruction({ kind: "constant", value: part, type, span: node.span }, rest, type) : opaque("a part of a constant", type, node.span);
|
|
1775
1922
|
}
|
|
1923
|
+
case "select":
|
|
1924
|
+
return {
|
|
1925
|
+
...node,
|
|
1926
|
+
type,
|
|
1927
|
+
then: projectReconstruction(node.then, path, type),
|
|
1928
|
+
else: projectReconstruction(node.else, path, type)
|
|
1929
|
+
};
|
|
1930
|
+
default:
|
|
1931
|
+
return opaque("a part of a computed value", type, node.span, [node]);
|
|
1776
1932
|
}
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
|
|
1782
|
-
|
|
1933
|
+
};
|
|
1934
|
+
function pathConditions(routine, variables) {
|
|
1935
|
+
const temporaries = new Set(routine.locals.filter((local) => isTemporary(routine.id, local)).map((local) => local.id));
|
|
1936
|
+
const locals = new Map(routine.locals.filter((local) => !temporaries.has(local.id)).map((local) => [local.id, local]));
|
|
1937
|
+
const parameters = new Map("parameters" in routine ? routine.parameters.map((parameter, index) => [parameter.id, index]) : []);
|
|
1938
|
+
const typeOf = (root) => locals.get(root)?.type ?? ("parameters" in routine ? routine.parameters.find((parameter) => parameter.id === root)?.type : undefined) ?? variables.get(root)?.type;
|
|
1939
|
+
const positions = new Map;
|
|
1940
|
+
const calls = [];
|
|
1941
|
+
const definitions = new Map;
|
|
1942
|
+
const logicals = new Map;
|
|
1943
|
+
const synthetic = new Map;
|
|
1944
|
+
const selects = new Map;
|
|
1945
|
+
const sequences = [];
|
|
1946
|
+
const number = (items) => {
|
|
1947
|
+
sequences.push(items);
|
|
1948
|
+
for (const item of items) {
|
|
1949
|
+
positions.set(item, positions.size);
|
|
1950
|
+
if (item.kind === "invoke" || item.kind === "blockUpdate") {
|
|
1951
|
+
calls.push(positions.size - 1);
|
|
1783
1952
|
}
|
|
1784
|
-
|
|
1785
|
-
|
|
1953
|
+
const defined = item.kind === "assign" ? item.root : item.kind === "invoke" && item.destination || undefined;
|
|
1954
|
+
if (defined && temporaries.has(defined)) {
|
|
1955
|
+
definitions.set(defined, [...definitions.get(defined) ?? [], item]);
|
|
1786
1956
|
}
|
|
1957
|
+
nestedBodies(item).forEach(number);
|
|
1787
1958
|
}
|
|
1788
1959
|
};
|
|
1789
|
-
|
|
1790
|
-
|
|
1960
|
+
number(routine.body);
|
|
1961
|
+
const position = (item) => positions.get(item) ?? 0;
|
|
1962
|
+
for (const items of sequences) {
|
|
1963
|
+
items.forEach((item, index) => {
|
|
1964
|
+
const next = items[index + 1];
|
|
1965
|
+
if (item.kind === "assign" && !item.path?.length && temporaries.has(item.root) && next?.kind === "if" && !next.else.length) {
|
|
1966
|
+
const negated = next.condition.kind === "unary" && next.condition.operator === "not";
|
|
1967
|
+
const tested = loadedRoot(next.condition.kind === "unary" && next.condition.operator === "not" ? next.condition.operand : next.condition);
|
|
1968
|
+
const right = next.then.at(-1);
|
|
1969
|
+
if (tested === item.root && wholeAssign(right, item.root) && definitions.get(item.root)?.length === 2) {
|
|
1970
|
+
const operator = negated ? "or" : "and";
|
|
1971
|
+
logicals.set(item.root, { operator, left: item, right });
|
|
1972
|
+
synthetic.set(next, { operator, left: item });
|
|
1973
|
+
}
|
|
1974
|
+
}
|
|
1975
|
+
if (item.kind === "if") {
|
|
1976
|
+
const then = item.then.at(-1);
|
|
1977
|
+
const otherwise = item.else.at(-1);
|
|
1978
|
+
if (then?.kind === "assign" && temporaries.has(then.root) && wholeAssign(then, then.root) && wholeAssign(otherwise, then.root) && definitions.get(then.root)?.length === 2) {
|
|
1979
|
+
selects.set(then.root, { test: item, then, else: otherwise });
|
|
1980
|
+
}
|
|
1981
|
+
}
|
|
1982
|
+
});
|
|
1791
1983
|
}
|
|
1792
|
-
|
|
1793
|
-
};
|
|
1794
|
-
|
|
1795
|
-
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
|
|
1984
|
+
const interleaved = (capturedAt, at) => calls.some((call) => call > capturedAt && call < at);
|
|
1985
|
+
const segments = (path, at) => path.map((segment) => segment.kind === "field" ? segment : { kind: "index", value: reconstruct(segment.value, at) });
|
|
1986
|
+
const place = (root, path, capturedAt, at, type, span) => {
|
|
1987
|
+
if (temporaries.has(root)) {
|
|
1988
|
+
return temporary(root, path, at, type, span);
|
|
1989
|
+
}
|
|
1990
|
+
if (locals.has(root)) {
|
|
1991
|
+
return { kind: "local", root, path, at: capturedAt, type, span };
|
|
1992
|
+
}
|
|
1993
|
+
const parameter = parameters.get(root);
|
|
1994
|
+
if (parameter !== undefined) {
|
|
1995
|
+
return {
|
|
1996
|
+
kind: "parameter",
|
|
1997
|
+
parameter,
|
|
1998
|
+
root,
|
|
1999
|
+
path,
|
|
2000
|
+
at: capturedAt,
|
|
2001
|
+
type,
|
|
2002
|
+
span
|
|
2003
|
+
};
|
|
2004
|
+
}
|
|
1799
2005
|
return {
|
|
1800
|
-
|
|
1801
|
-
|
|
2006
|
+
kind: "read",
|
|
2007
|
+
root,
|
|
2008
|
+
path,
|
|
2009
|
+
capturedAt,
|
|
2010
|
+
interleaved: interleaved(capturedAt, at),
|
|
2011
|
+
type,
|
|
2012
|
+
span
|
|
1802
2013
|
};
|
|
1803
|
-
}
|
|
1804
|
-
const [{ flow }] = branches;
|
|
1805
|
-
return {
|
|
1806
|
-
flow: branches.every((branch) => branch.flow === flow) ? flow : "mixed",
|
|
1807
|
-
initialized
|
|
1808
2014
|
};
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
|
|
1812
|
-
|
|
1813
|
-
|
|
1814
|
-
|
|
1815
|
-
|
|
1816
|
-
|
|
1817
|
-
|
|
1818
|
-
|
|
1819
|
-
|
|
1820
|
-
|
|
1821
|
-
|
|
1822
|
-
|
|
1823
|
-
|
|
1824
|
-
|
|
1825
|
-
|
|
1826
|
-
|
|
1827
|
-
|
|
1828
|
-
|
|
1829
|
-
|
|
1830
|
-
|
|
1831
|
-
|
|
1832
|
-
|
|
1833
|
-
const
|
|
1834
|
-
|
|
1835
|
-
|
|
1836
|
-
|
|
1837
|
-
|
|
1838
|
-
|
|
1839
|
-
|
|
1840
|
-
|
|
1841
|
-
|
|
1842
|
-
|
|
1843
|
-
|
|
1844
|
-
|
|
1845
|
-
|
|
1846
|
-
|
|
1847
|
-
|
|
1848
|
-
|
|
1849
|
-
|
|
1850
|
-
|
|
1851
|
-
|
|
2015
|
+
const temporary = (root, path, at, type, span) => {
|
|
2016
|
+
const logical = logicals.get(root);
|
|
2017
|
+
if (logical) {
|
|
2018
|
+
return projectReconstruction({
|
|
2019
|
+
kind: "logical",
|
|
2020
|
+
operator: logical.operator,
|
|
2021
|
+
left: reconstruct(logical.left.value, position(logical.left)),
|
|
2022
|
+
right: reconstruct(logical.right.value, position(logical.right)),
|
|
2023
|
+
type: "BOOL",
|
|
2024
|
+
span
|
|
2025
|
+
}, path, type);
|
|
2026
|
+
}
|
|
2027
|
+
const select = selects.get(root);
|
|
2028
|
+
if (select) {
|
|
2029
|
+
const branch = (item) => reconstruct(item.value, position(item));
|
|
2030
|
+
return projectReconstruction({
|
|
2031
|
+
kind: "select",
|
|
2032
|
+
condition: reconstruct(select.test.condition, position(select.test)),
|
|
2033
|
+
then: branch(select.then),
|
|
2034
|
+
else: branch(select.else),
|
|
2035
|
+
type: select.then.value.type,
|
|
2036
|
+
span
|
|
2037
|
+
}, path, type);
|
|
2038
|
+
}
|
|
2039
|
+
const defined = definitions.get(root) ?? [];
|
|
2040
|
+
const [only] = defined;
|
|
2041
|
+
if (defined.length === 1 && only?.kind === "invoke") {
|
|
2042
|
+
return projectReconstruction({
|
|
2043
|
+
kind: "call",
|
|
2044
|
+
call: only,
|
|
2045
|
+
path: [],
|
|
2046
|
+
type: typeOf(root) ?? type,
|
|
2047
|
+
span: only.span
|
|
2048
|
+
}, path, type);
|
|
2049
|
+
}
|
|
2050
|
+
if (defined.length === 1 && only && wholeAssign(only, root)) {
|
|
2051
|
+
const value = only.value;
|
|
2052
|
+
if (value.kind === "load") {
|
|
2053
|
+
return place(value.root, [...segments(value.path ?? [], position(only)), ...path], position(only), at, type, value.span);
|
|
2054
|
+
}
|
|
2055
|
+
return projectReconstruction(reconstruct(value, position(only)), path, type);
|
|
2056
|
+
}
|
|
2057
|
+
return parts(root, defined, path, type, span);
|
|
2058
|
+
};
|
|
2059
|
+
const parts = (root, defined, path, type, span) => {
|
|
2060
|
+
const fields = new Map;
|
|
2061
|
+
const items = new Map;
|
|
2062
|
+
for (const item of defined) {
|
|
2063
|
+
const [segment, ...rest] = item.kind === "assign" ? item.path ?? [] : [];
|
|
2064
|
+
const index = segment?.kind === "index" ? constantIndex(segment.value) : undefined;
|
|
2065
|
+
if (item.kind !== "assign" || !segment || rest.length) {
|
|
2066
|
+
return opaque("a value set in several places", type, span);
|
|
2067
|
+
}
|
|
2068
|
+
if (segment.kind === "field" && !fields.has(segment.name)) {
|
|
2069
|
+
fields.set(segment.name, item);
|
|
2070
|
+
} else if (index !== undefined && !items.has(index) && !fields.size) {
|
|
2071
|
+
items.set(index, item);
|
|
2072
|
+
} else {
|
|
2073
|
+
return opaque("a value set in several places", type, span);
|
|
2074
|
+
}
|
|
2075
|
+
}
|
|
2076
|
+
const value = (item) => reconstruct(item.value, position(item));
|
|
2077
|
+
if (items.size) {
|
|
2078
|
+
const indexes = [...items.keys()].sort((a, b) => a - b);
|
|
2079
|
+
const lower = indexes[0] ?? 0;
|
|
2080
|
+
const contiguous = indexes.every((index, offset) => index === lower + offset);
|
|
2081
|
+
if (!contiguous) {
|
|
2082
|
+
return opaque("an array with gaps", type, span);
|
|
2083
|
+
}
|
|
2084
|
+
return projectReconstruction({
|
|
2085
|
+
kind: "array",
|
|
2086
|
+
lower,
|
|
2087
|
+
items: indexes.flatMap((index) => {
|
|
2088
|
+
const item = items.get(index);
|
|
2089
|
+
return item ? [value(item)] : [];
|
|
2090
|
+
}),
|
|
2091
|
+
type: typeOf(root) ?? type,
|
|
2092
|
+
span
|
|
2093
|
+
}, path, type);
|
|
2094
|
+
}
|
|
2095
|
+
if (!fields.size) {
|
|
2096
|
+
return opaque("a value with no definition", type, span);
|
|
2097
|
+
}
|
|
2098
|
+
return projectReconstruction({
|
|
2099
|
+
kind: "record",
|
|
2100
|
+
fields: Object.fromEntries([...fields].map(([name, item]) => [name, value(item)])),
|
|
2101
|
+
type: typeOf(root) ?? type,
|
|
2102
|
+
span
|
|
2103
|
+
}, path, type);
|
|
2104
|
+
};
|
|
2105
|
+
const reconstruct = (expression, at) => {
|
|
2106
|
+
const { type, span } = expression;
|
|
2107
|
+
switch (expression.kind) {
|
|
2108
|
+
case "constant":
|
|
2109
|
+
return { kind: "constant", value: expression.value, type, span };
|
|
2110
|
+
case "borrow":
|
|
2111
|
+
return place(expression.place.root, segments(expression.place.path, at), at, at, type, span);
|
|
2112
|
+
case "load":
|
|
2113
|
+
return place(expression.root, segments(expression.path ?? [], at), at, at, type, span);
|
|
2114
|
+
case "unary":
|
|
2115
|
+
if (expression.operator === "clock") {
|
|
2116
|
+
return { kind: "clock", type, span };
|
|
2117
|
+
}
|
|
2118
|
+
return {
|
|
2119
|
+
kind: "unary",
|
|
2120
|
+
operator: expression.operator,
|
|
2121
|
+
operand: reconstruct(expression.operand, at),
|
|
2122
|
+
type,
|
|
2123
|
+
span
|
|
2124
|
+
};
|
|
2125
|
+
case "convert":
|
|
2126
|
+
return {
|
|
2127
|
+
kind: "convert",
|
|
2128
|
+
policy: expression.policy,
|
|
2129
|
+
operand: reconstruct(expression.operand, at),
|
|
2130
|
+
type,
|
|
2131
|
+
span
|
|
2132
|
+
};
|
|
2133
|
+
case "binary": {
|
|
2134
|
+
const left = reconstruct(expression.left, at);
|
|
2135
|
+
const right = reconstruct(expression.right, at);
|
|
2136
|
+
if (expression.operator === "and" || expression.operator === "or") {
|
|
2137
|
+
return {
|
|
2138
|
+
kind: "logical",
|
|
2139
|
+
operator: expression.operator,
|
|
2140
|
+
left,
|
|
2141
|
+
right,
|
|
2142
|
+
type,
|
|
2143
|
+
span
|
|
2144
|
+
};
|
|
2145
|
+
}
|
|
2146
|
+
return {
|
|
2147
|
+
kind: "binary",
|
|
2148
|
+
operator: expression.operator,
|
|
2149
|
+
...expression.policy && { policy: expression.policy },
|
|
2150
|
+
left,
|
|
2151
|
+
right,
|
|
2152
|
+
type,
|
|
2153
|
+
span
|
|
2154
|
+
};
|
|
2155
|
+
}
|
|
1852
2156
|
}
|
|
1853
|
-
|
|
1854
|
-
|
|
1855
|
-
|
|
1856
|
-
|
|
1857
|
-
|
|
1858
|
-
|
|
2157
|
+
};
|
|
2158
|
+
const job = "inputs" in routine && routine.job ? {
|
|
2159
|
+
busy: routine.state.find((field) => field.name === "busy")?.id,
|
|
2160
|
+
step: routine.state.find((field) => field.name === "step")?.id
|
|
2161
|
+
} : undefined;
|
|
2162
|
+
const isField = (node, field) => field !== undefined && node.kind === "read" && node.root === field && !node.path.length;
|
|
2163
|
+
const busyGuard = (item) => {
|
|
2164
|
+
if (item.kind !== "if" || item.else.length || item.then.length !== 1 || item.then[0]?.kind !== "return") {
|
|
2165
|
+
return false;
|
|
1859
2166
|
}
|
|
1860
|
-
const
|
|
1861
|
-
|
|
1862
|
-
|
|
2167
|
+
const test = reconstruct(item.condition, position(item));
|
|
2168
|
+
return test.kind === "unary" && test.operator === "not" && isField(test.operand, job?.busy);
|
|
2169
|
+
};
|
|
2170
|
+
const stepSwitch = (item) => item.kind === "switch" && isField(reconstruct(item.value, position(item)), job?.step);
|
|
2171
|
+
const statements = new Map;
|
|
2172
|
+
const loops = [];
|
|
2173
|
+
const sequence = (items, conditions, loop, mode) => {
|
|
2174
|
+
const guards = [];
|
|
2175
|
+
const exits = [];
|
|
2176
|
+
for (const item of items) {
|
|
2177
|
+
const here = [...conditions, ...guards];
|
|
2178
|
+
statements.set(item, {
|
|
2179
|
+
position: position(item),
|
|
2180
|
+
conditions: here,
|
|
2181
|
+
loop
|
|
2182
|
+
});
|
|
2183
|
+
if (mode === "jobDispatcher" && busyGuard(item)) {
|
|
2184
|
+
for (const branch of nestedBodies(item)) {
|
|
2185
|
+
sequence(branch, here, loop, "plain");
|
|
2186
|
+
}
|
|
2187
|
+
guards.push({ kind: "jobBusy" });
|
|
2188
|
+
continue;
|
|
2189
|
+
}
|
|
2190
|
+
if (mode === "jobDispatcher" && item.kind === "for" && item.body.some(stepSwitch)) {
|
|
2191
|
+
sequence(item.body, here, loop, "jobTransitions");
|
|
2192
|
+
continue;
|
|
2193
|
+
}
|
|
2194
|
+
const own = statementExits(item, here, loop, mode);
|
|
2195
|
+
exits.push(prefixed(guards, own));
|
|
2196
|
+
const any = [...own.returns, ...own.breaks, ...own.continues];
|
|
2197
|
+
if (any.length) {
|
|
2198
|
+
guards.push({ kind: "exits", any });
|
|
2199
|
+
}
|
|
1863
2200
|
}
|
|
1864
|
-
|
|
1865
|
-
|
|
2201
|
+
return merged(...exits);
|
|
2202
|
+
};
|
|
2203
|
+
const statementExits = (item, here, loop, mode) => {
|
|
2204
|
+
const at = position(item);
|
|
2205
|
+
switch (item.kind) {
|
|
2206
|
+
case "return":
|
|
2207
|
+
return { ...noExits, returns: [[]] };
|
|
2208
|
+
case "break":
|
|
2209
|
+
return { ...noExits, breaks: [[]] };
|
|
2210
|
+
case "continue":
|
|
2211
|
+
return { ...noExits, continues: [[]] };
|
|
2212
|
+
case "if": {
|
|
2213
|
+
const lowered = synthetic.get(item);
|
|
2214
|
+
const tested = lowered ? lowered.left.value : item.condition;
|
|
2215
|
+
const test = reconstruct(tested, lowered ? position(lowered.left) : at);
|
|
2216
|
+
const holds = lowered?.operator !== "or";
|
|
2217
|
+
const condition = (truth) => ({
|
|
2218
|
+
kind: "test",
|
|
2219
|
+
test,
|
|
2220
|
+
holds: truth === holds,
|
|
2221
|
+
at,
|
|
2222
|
+
span: tested.span
|
|
2223
|
+
});
|
|
2224
|
+
const yes = condition(true);
|
|
2225
|
+
const no = condition(false);
|
|
2226
|
+
return merged(prefixed([yes], sequence(item.then, [...here, yes], loop, "plain")), prefixed([no], sequence(item.else, [...here, no], loop, "plain")));
|
|
2227
|
+
}
|
|
2228
|
+
case "switch": {
|
|
2229
|
+
const test = reconstruct(item.value, at);
|
|
2230
|
+
const steps = mode === "jobTransitions" && stepSwitch(item);
|
|
2231
|
+
const inside = steps ? here.filter((condition) => condition.kind !== "jobBusy") : here;
|
|
2232
|
+
const branch = (condition, body) => prefixed([condition], sequence(body, [...inside, condition], loop, "plain"));
|
|
2233
|
+
const cases = item.cases.map((entry) => {
|
|
2234
|
+
const [value] = entry.values;
|
|
2235
|
+
const step = steps && value?.kind === "integer" && entry.values.length === 1 ? Number(value.value) : undefined;
|
|
2236
|
+
return branch(step === undefined ? {
|
|
2237
|
+
kind: "cases",
|
|
2238
|
+
test,
|
|
2239
|
+
values: entry.values,
|
|
2240
|
+
member: true,
|
|
2241
|
+
at,
|
|
2242
|
+
span: item.value.span
|
|
2243
|
+
} : { kind: "job", step }, entry.body);
|
|
2244
|
+
});
|
|
2245
|
+
const fallback = branch({
|
|
2246
|
+
kind: "cases",
|
|
2247
|
+
test,
|
|
2248
|
+
values: item.cases.flatMap((entry) => entry.values),
|
|
2249
|
+
member: false,
|
|
2250
|
+
at,
|
|
2251
|
+
span: item.value.span
|
|
2252
|
+
}, item.fallback);
|
|
2253
|
+
return merged(...cases, fallback);
|
|
2254
|
+
}
|
|
2255
|
+
case "for": {
|
|
2256
|
+
const id = loops.length;
|
|
2257
|
+
const counter = typeOf(item.counter) ?? "DINT";
|
|
2258
|
+
const bound = (value) => ({
|
|
2259
|
+
kind: "constant",
|
|
2260
|
+
value,
|
|
2261
|
+
type: counter,
|
|
2262
|
+
span: item.span
|
|
2263
|
+
});
|
|
2264
|
+
const slot = {
|
|
2265
|
+
statement: item,
|
|
2266
|
+
position: at,
|
|
2267
|
+
counter: item.counter,
|
|
2268
|
+
first: item.dynamic ? reconstruct(item.dynamic.start, at) : bound(item.start),
|
|
2269
|
+
limit: item.dynamic ? reconstruct(item.dynamic.limit, at) : bound(item.limit),
|
|
2270
|
+
exclusive: item.dynamic?.exclusive === true,
|
|
2271
|
+
step: BigInt(item.step),
|
|
2272
|
+
maxIterations: item.maxIterations,
|
|
2273
|
+
breaks: [],
|
|
2274
|
+
returns: false,
|
|
2275
|
+
parent: loop
|
|
2276
|
+
};
|
|
2277
|
+
loops.push(slot);
|
|
2278
|
+
const body = sequence(item.body, [...here, { kind: "loop", loop: id }], id, "plain");
|
|
2279
|
+
loops[id] = {
|
|
2280
|
+
...slot,
|
|
2281
|
+
breaks: body.breaks,
|
|
2282
|
+
returns: body.returns.length > 0
|
|
2283
|
+
};
|
|
2284
|
+
return body.returns.length ? { ...noExits, returns: [[{ kind: "loopReturns", loop: id }]] } : noExits;
|
|
2285
|
+
}
|
|
2286
|
+
default:
|
|
2287
|
+
return noExits;
|
|
2288
|
+
}
|
|
2289
|
+
};
|
|
2290
|
+
sequence(routine.body, [], null, job ? "jobDispatcher" : "plain");
|
|
2291
|
+
return { statements, loops, reconstruct };
|
|
2292
|
+
}
|
|
2293
|
+
// packages/compiler/src/ir/job-errors.ts
|
|
2294
|
+
var jobError = {
|
|
2295
|
+
cancelled: 1,
|
|
2296
|
+
timeout: 2,
|
|
2297
|
+
tooLarge: 3,
|
|
2298
|
+
malformedResponse: 4,
|
|
2299
|
+
network: 5,
|
|
2300
|
+
tls: 6,
|
|
2301
|
+
noRequestSlot: 7
|
|
2302
|
+
};
|
|
2303
|
+
var httpErrors = { base: 1e4, last: 10999 };
|
|
2304
|
+
var jobErrors = {
|
|
2305
|
+
[jobError.cancelled]: "cancelled",
|
|
2306
|
+
[jobError.timeout]: "timeout",
|
|
2307
|
+
[jobError.tooLarge]: "request or response too large to encode",
|
|
2308
|
+
[jobError.malformedResponse]: "malformed response",
|
|
2309
|
+
[jobError.network]: "network error or server restarted",
|
|
2310
|
+
[jobError.tls]: "TLS error",
|
|
2311
|
+
[jobError.noRequestSlot]: "no free request slot",
|
|
2312
|
+
[httpErrors.base]: "HTTP status: 10000 + status code"
|
|
2313
|
+
};
|
|
2314
|
+
var errorName = (code) => {
|
|
2315
|
+
if (code > httpErrors.base && code <= httpErrors.last) {
|
|
2316
|
+
return `HTTP ${code - httpErrors.base}`;
|
|
1866
2317
|
}
|
|
1867
|
-
|
|
1868
|
-
|
|
1869
|
-
|
|
1870
|
-
|
|
1871
|
-
|
|
1872
|
-
|
|
1873
|
-
|
|
1874
|
-
|
|
1875
|
-
|
|
1876
|
-
|
|
1877
|
-
|
|
1878
|
-
mutable: true,
|
|
1879
|
-
retain: false,
|
|
1880
|
-
span
|
|
1881
|
-
};
|
|
1882
|
-
this.scope.set(id, item);
|
|
1883
|
-
this.locals.push(item);
|
|
1884
|
-
return item;
|
|
2318
|
+
return code < httpErrors.base ? jobErrors[code] : undefined;
|
|
2319
|
+
};
|
|
2320
|
+
// packages/compiler/src/ir/layouts.ts
|
|
2321
|
+
import { elementaryType as elementaryType2, integerRange as integerRange3 } from "@tsplc/iec";
|
|
2322
|
+
var maxScalarElements = 1e6;
|
|
2323
|
+
var maxNestingDepth = 64;
|
|
2324
|
+
var [firstIndex, lastIndex] = integerRange3(elementaryType2("DINT"));
|
|
2325
|
+
var definitionIssue = (type, types) => {
|
|
2326
|
+
const canonical = primitiveTypes.find((item) => item.id === type.id);
|
|
2327
|
+
if (canonical) {
|
|
2328
|
+
return type.kind !== canonical.kind || "bits" in canonical && (!("bits" in type) || type.bits !== canonical.bits) || "signed" in canonical && (!("signed" in type) || type.signed !== canonical.signed) || "domain" in canonical && (!("domain" in type) || type.domain !== canonical.domain) ? `Conflicting primitive type ${type.id}` : undefined;
|
|
1885
2329
|
}
|
|
1886
|
-
|
|
1887
|
-
|
|
2330
|
+
switch (type.kind) {
|
|
2331
|
+
case "record":
|
|
2332
|
+
return !type.fields.length || new Set(type.fields.map((field) => field.name)).size !== type.fields.length || type.fields.some((field) => !field.name || typeof field.readonly !== "boolean" || field.mode && field.mode !== "inOut" || !types.has(field.type)) ? `Invalid closed record layout ${type.id}` : undefined;
|
|
2333
|
+
case "array": {
|
|
2334
|
+
if (!types.has(type.element) || !Number.isSafeInteger(type.length) || type.length < 1 || type.length > maxScalarElements || !Number.isInteger(type.lowerBound) || BigInt(type.lowerBound) < firstIndex || BigInt(type.lowerBound + type.length - 1) > lastIndex) {
|
|
2335
|
+
return `Invalid fixed array layout ${type.id}`;
|
|
2336
|
+
}
|
|
2337
|
+
if (type.index === undefined) {
|
|
2338
|
+
return;
|
|
2339
|
+
}
|
|
2340
|
+
const index = types.get(type.index);
|
|
2341
|
+
return index?.kind !== "enum" || index.members.length !== type.length || index.members.some((member) => Number(member.value) < type.lowerBound || Number(member.value) >= type.lowerBound + type.length) ? `Invalid enum-keyed array layout ${type.id}` : undefined;
|
|
2342
|
+
}
|
|
2343
|
+
case "enum": {
|
|
2344
|
+
const base = types.get(type.base);
|
|
2345
|
+
return type.base !== "DINT" || base?.kind !== "integer" || !type.members.length || new Set(type.members.map((member) => member.name)).size !== type.members.length || new Set(type.members.map((member) => member.value)).size !== type.members.length || type.members.some((member) => !member.name || !constantFits({ kind: "integer", value: member.value }, base)) ? `Invalid enum layout ${type.id}` : undefined;
|
|
2346
|
+
}
|
|
2347
|
+
case "string":
|
|
2348
|
+
return !Number.isInteger(type.capacity) || type.capacity < 1 || type.capacity > 65535 || !["utf8", "utf16"].includes(type.encoding) ? `Invalid bounded string layout ${type.id}` : undefined;
|
|
2349
|
+
case "block":
|
|
2350
|
+
return !type.block || type.output && types.get(type.output)?.kind !== "record" ? `Invalid block type ${type.id}` : undefined;
|
|
2351
|
+
default:
|
|
2352
|
+
return `Unsupported core type definition ${type.id}`;
|
|
1888
2353
|
}
|
|
1889
|
-
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
|
|
2354
|
+
};
|
|
2355
|
+
var admitLayouts = (types) => {
|
|
2356
|
+
for (const type of types.values()) {
|
|
2357
|
+
const issue = definitionIssue(type, types);
|
|
2358
|
+
if (issue) {
|
|
2359
|
+
reject(issue);
|
|
1893
2360
|
}
|
|
1894
|
-
return instancedBlock(variable.type, this.declarations.types)?.block;
|
|
1895
2361
|
}
|
|
1896
|
-
|
|
1897
|
-
|
|
1898
|
-
|
|
1899
|
-
|
|
2362
|
+
const active = new Set;
|
|
2363
|
+
const layouts = new Map;
|
|
2364
|
+
const measure = (id, depth) => {
|
|
2365
|
+
if (active.has(id) || depth > maxNestingDepth) {
|
|
2366
|
+
reject(`Recursive or excessively nested by-value layout ${id}`);
|
|
1900
2367
|
}
|
|
1901
|
-
|
|
1902
|
-
|
|
1903
|
-
|
|
1904
|
-
|
|
1905
|
-
}
|
|
1906
|
-
} else if (this.owner?.id !== called.owner.block) {
|
|
1907
|
-
reject("An implicit method call requires its owning block scope.", span);
|
|
1908
|
-
}
|
|
1909
|
-
if (called.owner.access !== "public" && this.owner?.id !== called.owner.block) {
|
|
1910
|
-
reject("Private and protected methods require their owning block scope.", span);
|
|
2368
|
+
const cached = layouts.get(id);
|
|
2369
|
+
if (cached) {
|
|
2370
|
+
if (depth + cached.height > maxNestingDepth) {
|
|
2371
|
+
reject(`Layout nesting exceeds ${maxNestingDepth} levels ${id}`);
|
|
1911
2372
|
}
|
|
1912
|
-
|
|
1913
|
-
reject("Ordinary functions cannot have an instance receiver.", span);
|
|
2373
|
+
return cached;
|
|
1914
2374
|
}
|
|
1915
|
-
|
|
1916
|
-
|
|
1917
|
-
|
|
1918
|
-
|
|
1919
|
-
}
|
|
1920
|
-
});
|
|
1921
|
-
return called;
|
|
1922
|
-
}
|
|
1923
|
-
expression(value, initialized, output, allowAggregateLoad = false) {
|
|
1924
|
-
const type = this.declarations.types.get(value.type);
|
|
1925
|
-
if (!type || !this.declarations.isValueType(value.type)) {
|
|
1926
|
-
reject("Unknown or non-value expression type.", value.span);
|
|
2375
|
+
active.add(id);
|
|
2376
|
+
const layout = composite(types.get(id), depth);
|
|
2377
|
+
active.delete(id);
|
|
2378
|
+
if (layout.size > maxScalarElements) {
|
|
2379
|
+
reject(`Layout exceeds one million scalar elements ${id}`);
|
|
1927
2380
|
}
|
|
1928
|
-
if (
|
|
1929
|
-
reject(
|
|
2381
|
+
if (depth + layout.height > maxNestingDepth) {
|
|
2382
|
+
reject(`Layout nesting exceeds ${maxNestingDepth} levels ${id}`);
|
|
1930
2383
|
}
|
|
1931
|
-
|
|
1932
|
-
|
|
1933
|
-
|
|
1934
|
-
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
|
|
1938
|
-
|
|
1939
|
-
|
|
1940
|
-
|
|
1941
|
-
reject(checked.issue ?? "Invalid array receiver.", value.span);
|
|
1942
|
-
}
|
|
1943
|
-
this.normalizePath(value.operand.place, initialized, output);
|
|
1944
|
-
} else {
|
|
1945
|
-
this.expression(value.operand, initialized, output, true);
|
|
1946
|
-
}
|
|
2384
|
+
layouts.set(id, layout);
|
|
2385
|
+
return layout;
|
|
2386
|
+
};
|
|
2387
|
+
const composite = (type, depth) => {
|
|
2388
|
+
if (type.kind === "array") {
|
|
2389
|
+
const element = measure(type.element, depth + 1);
|
|
2390
|
+
return { size: type.length * element.size, height: 1 + element.height };
|
|
2391
|
+
}
|
|
2392
|
+
if (type.kind === "record") {
|
|
2393
|
+
const fields = type.fields.map((field) => measure(field.type, depth + 1));
|
|
1947
2394
|
return {
|
|
1948
|
-
|
|
1949
|
-
|
|
1950
|
-
value: { kind: "integer", value: String(array.length) }
|
|
2395
|
+
size: fields.reduce((sum, field) => sum + field.size, 0),
|
|
2396
|
+
height: 1 + fields.reduce((maximum, field) => Math.max(maximum, field.height), 0)
|
|
1951
2397
|
};
|
|
1952
2398
|
}
|
|
1953
|
-
|
|
1954
|
-
|
|
1955
|
-
|
|
1956
|
-
|
|
1957
|
-
|
|
1958
|
-
|
|
1959
|
-
|
|
1960
|
-
|
|
1961
|
-
|
|
1962
|
-
|
|
1963
|
-
|
|
1964
|
-
|
|
1965
|
-
|
|
1966
|
-
|
|
1967
|
-
});
|
|
1968
|
-
return statements;
|
|
1969
|
-
};
|
|
1970
|
-
output.push({
|
|
1971
|
-
kind: "if",
|
|
1972
|
-
condition,
|
|
1973
|
-
then: branch(value.then),
|
|
1974
|
-
else: branch(value.else),
|
|
1975
|
-
span: value.span
|
|
1976
|
-
});
|
|
1977
|
-
initialized.add(result.id);
|
|
1978
|
-
return this.load(result, value.span);
|
|
2399
|
+
return { size: 1, height: 0 };
|
|
2400
|
+
};
|
|
2401
|
+
for (const id of types.keys()) {
|
|
2402
|
+
measure(id, 0);
|
|
2403
|
+
}
|
|
2404
|
+
return new Map([...layouts].map(([id, layout]) => [id, layout.size]));
|
|
2405
|
+
};
|
|
2406
|
+
var layoutIssue = (types) => {
|
|
2407
|
+
try {
|
|
2408
|
+
admitLayouts(types);
|
|
2409
|
+
return;
|
|
2410
|
+
} catch (cause) {
|
|
2411
|
+
if (cause instanceof InvalidCore) {
|
|
2412
|
+
return cause.diagnostic.message;
|
|
1979
2413
|
}
|
|
1980
|
-
|
|
1981
|
-
|
|
1982
|
-
|
|
1983
|
-
|
|
1984
|
-
|
|
1985
|
-
|
|
1986
|
-
|
|
1987
|
-
|
|
1988
|
-
|
|
1989
|
-
|
|
1990
|
-
|
|
1991
|
-
|
|
1992
|
-
|
|
1993
|
-
|
|
1994
|
-
|
|
1995
|
-
|
|
1996
|
-
});
|
|
1997
|
-
initialized.add(result.id);
|
|
1998
|
-
return this.load(result, value.span);
|
|
2414
|
+
throw cause;
|
|
2415
|
+
}
|
|
2416
|
+
};
|
|
2417
|
+
// packages/compiler/src/ir/allocation.ts
|
|
2418
|
+
function checkStaticAllocation(module, declarations) {
|
|
2419
|
+
const { types, blocks, layoutSizes } = declarations;
|
|
2420
|
+
const payload = (id) => layoutSizes.get(id);
|
|
2421
|
+
const instanceSize = (id) => {
|
|
2422
|
+
const type = types.get(id);
|
|
2423
|
+
return type.kind === "array" ? type.length * instanceSize(type.element) : allocation(type.kind === "block" ? type.block : "").size;
|
|
2424
|
+
};
|
|
2425
|
+
const allocations = new Map;
|
|
2426
|
+
const allocating = new Set;
|
|
2427
|
+
const allocation = (id) => {
|
|
2428
|
+
if (allocating.size >= maxNestingDepth) {
|
|
2429
|
+
reject(`Static block allocation nesting exceeds ${maxNestingDepth} levels.`, blocks.get(id)?.span);
|
|
1999
2430
|
}
|
|
2000
|
-
|
|
2001
|
-
|
|
2002
|
-
reject(issue, value.span);
|
|
2431
|
+
if (allocating.has(id)) {
|
|
2432
|
+
reject("Recursive static block allocation.", blocks.get(id)?.span);
|
|
2003
2433
|
}
|
|
2004
|
-
|
|
2005
|
-
|
|
2006
|
-
|
|
2007
|
-
|
|
2008
|
-
|
|
2009
|
-
|
|
2010
|
-
|
|
2011
|
-
|
|
2012
|
-
|
|
2013
|
-
|
|
2014
|
-
|
|
2015
|
-
|
|
2016
|
-
|
|
2017
|
-
|
|
2018
|
-
|
|
2019
|
-
|
|
2020
|
-
|
|
2021
|
-
|
|
2022
|
-
|
|
2023
|
-
|
|
2024
|
-
|
|
2025
|
-
|
|
2026
|
-
|
|
2027
|
-
|
|
2028
|
-
|
|
2029
|
-
|
|
2030
|
-
|
|
2031
|
-
|
|
2032
|
-
|
|
2033
|
-
|
|
2034
|
-
|
|
2035
|
-
|
|
2036
|
-
|
|
2037
|
-
|
|
2038
|
-
|
|
2039
|
-
|
|
2040
|
-
|
|
2041
|
-
|
|
2042
|
-
|
|
2043
|
-
|
|
2044
|
-
|
|
2045
|
-
|
|
2046
|
-
|
|
2047
|
-
|
|
2048
|
-
|
|
2434
|
+
const cached = allocations.get(id);
|
|
2435
|
+
if (cached) {
|
|
2436
|
+
return cached;
|
|
2437
|
+
}
|
|
2438
|
+
allocating.add(id);
|
|
2439
|
+
const block = blocks.get(id);
|
|
2440
|
+
const children = block.state.filter((item) => item.storage === "instance").map((item) => ({
|
|
2441
|
+
height: allocation(instancedBlock(item.type, types).block).height,
|
|
2442
|
+
size: instanceSize(item.type)
|
|
2443
|
+
}));
|
|
2444
|
+
const own = [
|
|
2445
|
+
...block.inputs,
|
|
2446
|
+
...block.outputs,
|
|
2447
|
+
...block.state.filter((item) => item.storage !== "instance")
|
|
2448
|
+
].reduce((sum, item) => sum + (item.mode === "inOut" ? 1 : payload(item.type)), 1);
|
|
2449
|
+
const size = own + children.reduce((sum, child) => sum + child.size, 0);
|
|
2450
|
+
const height = children.length ? 1 + Math.max(...children.map((child) => child.height)) : 1;
|
|
2451
|
+
if (size > maxScalarElements || height > maxNestingDepth) {
|
|
2452
|
+
reject("Static block allocation exceeds its finite budget.", block.span);
|
|
2453
|
+
}
|
|
2454
|
+
allocating.delete(id);
|
|
2455
|
+
const result = { size, height };
|
|
2456
|
+
allocations.set(id, result);
|
|
2457
|
+
return result;
|
|
2458
|
+
};
|
|
2459
|
+
for (const block of module.blocks) {
|
|
2460
|
+
allocation(block.id);
|
|
2461
|
+
}
|
|
2462
|
+
const staticPayload = module.variables.reduce((sum, variable) => {
|
|
2463
|
+
return sum + (instancedBlock(variable.type, types) ? instanceSize(variable.type) : payload(variable.type));
|
|
2464
|
+
}, 0);
|
|
2465
|
+
if (staticPayload > maxScalarElements) {
|
|
2466
|
+
reject("Static module storage exceeds one million scalar elements.");
|
|
2467
|
+
}
|
|
2468
|
+
}
|
|
2469
|
+
|
|
2470
|
+
// packages/compiler/src/ir/declarations.ts
|
|
2471
|
+
var matchesPorts = (variables, schema) => variables.length === schema.length && schema.every(([name, , type]) => variables.some((variable) => variable.name === name && variable.type === type && !variable.mode && !variable.retain));
|
|
2472
|
+
var checkStandardBlock = (block, standard) => {
|
|
2473
|
+
const expected = Object.hasOwn(standardBlocks, standard) ? standardBlocks[standard] : undefined;
|
|
2474
|
+
if (!expected || block.name !== standard || block.body.length || block.state.length || block.locals.length || !matchesPorts(block.inputs, expected.inputs) || !matchesPorts(block.outputs, expected.outputs) || block.outputs.some(({ initial }) => {
|
|
2475
|
+
switch (initial?.kind) {
|
|
2476
|
+
case "bool":
|
|
2477
|
+
return initial.value !== false;
|
|
2478
|
+
case "duration":
|
|
2479
|
+
return initial.nanoseconds !== "0";
|
|
2480
|
+
case "integer":
|
|
2481
|
+
return initial.value !== "0";
|
|
2482
|
+
default:
|
|
2483
|
+
return true;
|
|
2484
|
+
}
|
|
2485
|
+
})) {
|
|
2486
|
+
reject("Invalid canonical standard block definition.", block.span);
|
|
2487
|
+
}
|
|
2488
|
+
};
|
|
2489
|
+
|
|
2490
|
+
class ModuleDeclarations {
|
|
2491
|
+
ids = new Set;
|
|
2492
|
+
types = new Map;
|
|
2493
|
+
layoutSizes;
|
|
2494
|
+
globals = new Map;
|
|
2495
|
+
blocks;
|
|
2496
|
+
signatures;
|
|
2497
|
+
constructor(module) {
|
|
2498
|
+
for (const type of module.types) {
|
|
2499
|
+
if (this.types.has(type.id)) {
|
|
2500
|
+
reject(`Duplicate type ${type.id}.`);
|
|
2049
2501
|
}
|
|
2050
|
-
|
|
2051
|
-
|
|
2052
|
-
|
|
2053
|
-
|
|
2054
|
-
|
|
2055
|
-
|
|
2056
|
-
|
|
2057
|
-
|
|
2058
|
-
|
|
2059
|
-
|
|
2060
|
-
|
|
2061
|
-
|
|
2062
|
-
|
|
2502
|
+
this.types.set(type.id, type);
|
|
2503
|
+
}
|
|
2504
|
+
this.layoutSizes = admitLayouts(this.types);
|
|
2505
|
+
if (this.types.get("BOOL")?.kind !== "bool") {
|
|
2506
|
+
reject("Missing canonical BOOL type.");
|
|
2507
|
+
}
|
|
2508
|
+
this.blocks = new Map(module.blocks.map((block) => [block.id, block]));
|
|
2509
|
+
for (const item of module.variables) {
|
|
2510
|
+
this.checkVariable(item, [
|
|
2511
|
+
"module",
|
|
2512
|
+
"instance",
|
|
2513
|
+
"input",
|
|
2514
|
+
"output",
|
|
2515
|
+
"constant"
|
|
2516
|
+
]);
|
|
2517
|
+
this.globals.set(item.id, item);
|
|
2518
|
+
}
|
|
2519
|
+
this.signatures = new Map(module.functions.map((fn) => [fn.id, fn]));
|
|
2520
|
+
for (const fn of module.functions) {
|
|
2521
|
+
this.claimId(fn.id, fn.span);
|
|
2522
|
+
if (fn.owner && (!this.blocks.has(fn.owner.block) || this.blocks.get(fn.owner.block)?.standard || !["public", "private", "protected"].includes(fn.owner.access))) {
|
|
2523
|
+
reject("Methods require a user block owner and a valid access modifier.", fn.span);
|
|
2063
2524
|
}
|
|
2064
|
-
|
|
2065
|
-
|
|
2066
|
-
|
|
2067
|
-
|
|
2068
|
-
|
|
2069
|
-
|
|
2070
|
-
|
|
2071
|
-
reject("Aggregate loads require explicit copy or a readonly value argument.", value.span);
|
|
2072
|
-
}
|
|
2073
|
-
if (source.storage === "local" && !initialized.has(source.id)) {
|
|
2074
|
-
reject("Local read before initialization.", value.span);
|
|
2075
|
-
}
|
|
2076
|
-
const first = value.place.path[0];
|
|
2077
|
-
if (source.storage === "instance" && value.place.path.length === 1 && first?.kind === "field" && first.name === "out" && type.kind === "record") {
|
|
2078
|
-
const result = this.temporary(value.type, value.span);
|
|
2079
|
-
for (const field of type.fields) {
|
|
2080
|
-
const selected = {
|
|
2081
|
-
kind: "load",
|
|
2082
|
-
type: field.type,
|
|
2083
|
-
place: {
|
|
2084
|
-
root: source.id,
|
|
2085
|
-
path: [
|
|
2086
|
-
...value.place.path,
|
|
2087
|
-
{ kind: "field", name: field.name }
|
|
2088
|
-
]
|
|
2089
|
-
},
|
|
2090
|
-
span: value.span
|
|
2091
|
-
};
|
|
2092
|
-
const captured = this.expression(selected, initialized, output, true);
|
|
2093
|
-
output.push({
|
|
2094
|
-
kind: "assign",
|
|
2095
|
-
root: result.id,
|
|
2096
|
-
path: [{ kind: "field", name: field.name }],
|
|
2097
|
-
value: captured,
|
|
2098
|
-
span: value.span
|
|
2099
|
-
});
|
|
2100
|
-
}
|
|
2101
|
-
initialized.add(result.id);
|
|
2102
|
-
return this.load(result, value.span);
|
|
2103
|
-
}
|
|
2104
|
-
const path = this.normalizePath(value.place, initialized, output);
|
|
2105
|
-
if (source.storage === "constant" && source.initial && !path.length) {
|
|
2106
|
-
return { ...common, kind: "constant", value: source.initial };
|
|
2107
|
-
}
|
|
2108
|
-
const result = this.temporary(value.type, value.span);
|
|
2109
|
-
output.push({
|
|
2110
|
-
kind: "assign",
|
|
2111
|
-
root: result.id,
|
|
2112
|
-
value: {
|
|
2113
|
-
...common,
|
|
2114
|
-
kind: "load",
|
|
2115
|
-
root: source.id,
|
|
2116
|
-
...path.length && { path }
|
|
2117
|
-
},
|
|
2118
|
-
span: value.span
|
|
2119
|
-
});
|
|
2120
|
-
const bound = interval(value, this.declarations.types, this.scope, this.ranges);
|
|
2121
|
-
if (bound) {
|
|
2122
|
-
this.ranges.set(result.id, bound);
|
|
2525
|
+
if (fn.result && !this.isValueType(fn.result)) {
|
|
2526
|
+
reject("Unknown or non-value function result type.", fn.span);
|
|
2527
|
+
}
|
|
2528
|
+
for (const parameter of fn.parameters) {
|
|
2529
|
+
this.checkVariable(parameter, ["parameter"], true);
|
|
2530
|
+
if (parameter.mutable && !parameter.mode) {
|
|
2531
|
+
reject("Value parameters are readonly.", parameter.span);
|
|
2123
2532
|
}
|
|
2124
|
-
initialized.add(result.id);
|
|
2125
|
-
return this.load(result, value.span);
|
|
2126
2533
|
}
|
|
2127
|
-
|
|
2128
|
-
|
|
2129
|
-
const constant = fold(value, this.scope, this.declarations.types);
|
|
2130
|
-
return constant ? { ...common, kind: "constant", value: constant } : { ...common, kind: "unary", operator: value.operator, operand };
|
|
2534
|
+
for (const local of fn.locals) {
|
|
2535
|
+
this.checkVariable(local, ["local"]);
|
|
2131
2536
|
}
|
|
2132
|
-
|
|
2133
|
-
|
|
2134
|
-
|
|
2135
|
-
|
|
2136
|
-
|
|
2537
|
+
}
|
|
2538
|
+
for (const block of module.blocks) {
|
|
2539
|
+
this.claimId(block.id, block.span);
|
|
2540
|
+
for (const input of block.inputs) {
|
|
2541
|
+
this.checkVariable(input, ["parameter"], true);
|
|
2542
|
+
if (!input.mode && input.mutable) {
|
|
2543
|
+
reject("Value block inputs are readonly.", input.span);
|
|
2137
2544
|
}
|
|
2138
|
-
return constant ? { ...common, kind: "constant", value: constant } : { ...common, kind: "convert", operand, policy: value.policy };
|
|
2139
2545
|
}
|
|
2140
|
-
|
|
2141
|
-
|
|
2142
|
-
|
|
2143
|
-
|
|
2144
|
-
|
|
2145
|
-
|
|
2146
|
-
|
|
2147
|
-
|
|
2148
|
-
|
|
2149
|
-
|
|
2150
|
-
|
|
2151
|
-
|
|
2152
|
-
branch.push({
|
|
2153
|
-
kind: "assign",
|
|
2154
|
-
root: result.id,
|
|
2155
|
-
value: right,
|
|
2156
|
-
span: value.span
|
|
2157
|
-
});
|
|
2158
|
-
const condition = value.operator === "and" ? this.load(result, value.span) : {
|
|
2159
|
-
kind: "unary",
|
|
2160
|
-
operator: "not",
|
|
2161
|
-
operand: this.load(result, value.span),
|
|
2162
|
-
type: "BOOL",
|
|
2163
|
-
span: value.span
|
|
2164
|
-
};
|
|
2165
|
-
output.push({
|
|
2166
|
-
kind: "if",
|
|
2167
|
-
condition,
|
|
2168
|
-
then: branch,
|
|
2169
|
-
else: [],
|
|
2170
|
-
span: value.span
|
|
2171
|
-
});
|
|
2172
|
-
initialized.add(result.id);
|
|
2173
|
-
return this.load(result, value.span);
|
|
2546
|
+
for (const output of block.outputs) {
|
|
2547
|
+
this.checkVariable(output, ["blockOutput"]);
|
|
2548
|
+
}
|
|
2549
|
+
for (const state of block.state) {
|
|
2550
|
+
this.checkVariable(state, ["field", "instance"]);
|
|
2551
|
+
}
|
|
2552
|
+
for (const local of block.locals) {
|
|
2553
|
+
this.checkVariable(local, ["local"]);
|
|
2554
|
+
}
|
|
2555
|
+
for (const ports of [block.inputs, block.outputs]) {
|
|
2556
|
+
if (new Set(ports.map((variable) => variable.name)).size !== ports.length) {
|
|
2557
|
+
reject("Block input and output names must each be unique within their schema.", block.span);
|
|
2174
2558
|
}
|
|
2175
|
-
|
|
2176
|
-
|
|
2177
|
-
|
|
2178
|
-
|
|
2559
|
+
}
|
|
2560
|
+
const result = block.result ? this.types.get(block.result) : undefined;
|
|
2561
|
+
if (block.outputs.length ? result?.kind !== "record" || !this.isValueType(result.id) || result.fields.length !== block.outputs.length || result.fields.some((field) => !block.outputs.some((output) => output.name === field.name && output.type === field.type)) : Boolean(block.result)) {
|
|
2562
|
+
reject("Block output schema differs from its result record.", block.span);
|
|
2563
|
+
}
|
|
2564
|
+
if (block.standard) {
|
|
2565
|
+
checkStandardBlock(block, block.standard);
|
|
2566
|
+
}
|
|
2567
|
+
}
|
|
2568
|
+
for (const type of this.types.values()) {
|
|
2569
|
+
if (type.kind === "block") {
|
|
2570
|
+
const definition = this.blocks.get(type.block);
|
|
2571
|
+
if (!definition || definition.result !== type.output) {
|
|
2572
|
+
reject("Block type differs from its definition.");
|
|
2179
2573
|
}
|
|
2180
|
-
return {
|
|
2181
|
-
...common,
|
|
2182
|
-
kind: "binary",
|
|
2183
|
-
operator: value.operator,
|
|
2184
|
-
left,
|
|
2185
|
-
right,
|
|
2186
|
-
...value.policy && { policy: value.policy }
|
|
2187
|
-
};
|
|
2188
2574
|
}
|
|
2189
2575
|
}
|
|
2190
2576
|
}
|
|
2191
|
-
|
|
2192
|
-
return
|
|
2193
|
-
kind: "index",
|
|
2194
|
-
value: this.expression(segment.value, initialized, output)
|
|
2195
|
-
});
|
|
2577
|
+
hasId(id) {
|
|
2578
|
+
return this.ids.has(id);
|
|
2196
2579
|
}
|
|
2197
|
-
|
|
2198
|
-
|
|
2199
|
-
|
|
2200
|
-
|
|
2201
|
-
|
|
2202
|
-
|
|
2580
|
+
claimId(id, span) {
|
|
2581
|
+
if (this.ids.has(id)) {
|
|
2582
|
+
reject(`Duplicate symbol ${id}.`, span);
|
|
2583
|
+
}
|
|
2584
|
+
this.ids.add(id);
|
|
2585
|
+
}
|
|
2586
|
+
isValueType(id, seen = new Set) {
|
|
2587
|
+
if (seen.has(id)) {
|
|
2588
|
+
return true;
|
|
2589
|
+
}
|
|
2590
|
+
seen.add(id);
|
|
2591
|
+
const type = this.types.get(id);
|
|
2592
|
+
return Boolean(type && type.kind !== "block" && (type.kind !== "record" || type.fields.every((field) => !field.mode && this.isValueType(field.type, seen))) && (type.kind !== "array" || this.isValueType(type.element, seen)));
|
|
2593
|
+
}
|
|
2594
|
+
checkVariable(item, storage, allowBorrow = false) {
|
|
2595
|
+
this.claimId(item.id, item.span);
|
|
2596
|
+
const type = this.types.get(item.type);
|
|
2597
|
+
if (!type || !storage.includes(item.storage)) {
|
|
2598
|
+
reject("Invalid storage type or class.", item.span);
|
|
2599
|
+
}
|
|
2600
|
+
if (item.mode && (item.mode !== "inOut" || !allowBorrow || !item.mutable || item.storage !== "parameter")) {
|
|
2601
|
+
reject("InOut belongs only on mutable parameters.", item.span);
|
|
2602
|
+
}
|
|
2603
|
+
const mutableState = item.mutable || type.kind === "array" || type.kind === "record" && type.fields.some((field) => !field.readonly);
|
|
2604
|
+
if (item.retain && !(item.storage === "instance" || ["module", "field"].includes(item.storage) && mutableState)) {
|
|
2605
|
+
reject("Only mutable static state or block instances may retain.", item.span);
|
|
2606
|
+
}
|
|
2607
|
+
if (item.storage === "instance") {
|
|
2608
|
+
const instanced = instancedBlock(item.type, this.types);
|
|
2609
|
+
if (!instanced || !this.blocks.has(instanced.block) || item.initial || item.mode) {
|
|
2610
|
+
reject("Instances require a known static block and cannot have value initializers.", item.span);
|
|
2611
|
+
}
|
|
2612
|
+
} else if (!this.isValueType(item.type) && !(allowBorrow && item.mode === "inOut" && type.kind === "block")) {
|
|
2613
|
+
reject("Block instances and reference schemas cannot be stored as values.", item.span);
|
|
2203
2614
|
}
|
|
2204
|
-
if (
|
|
2205
|
-
reject("
|
|
2615
|
+
if (item.storage === "parameter" && item.initial) {
|
|
2616
|
+
reject("Inputs are supplied at each invocation and cannot have stored initializers.", item.span);
|
|
2206
2617
|
}
|
|
2207
|
-
|
|
2208
|
-
|
|
2209
|
-
instanceIndex(instance, index, initialized, output, span) {
|
|
2210
|
-
if (!index) {
|
|
2211
|
-
return;
|
|
2618
|
+
if (item.initial && !valueFits(item.initial, type, this.types)) {
|
|
2619
|
+
reject("Initializer has the wrong type or is out of range.", item.span);
|
|
2212
2620
|
}
|
|
2213
|
-
|
|
2214
|
-
|
|
2215
|
-
|
|
2621
|
+
if (["module", "field", "constant", "blockOutput"].includes(item.storage) && !item.initial) {
|
|
2622
|
+
reject("Persistent storage requires initialization.", item.span);
|
|
2623
|
+
}
|
|
2624
|
+
if (["input", "constant", "blockOutput"].includes(item.storage) && item.mutable) {
|
|
2625
|
+
reject("Readonly storage marked mutable.", item.span);
|
|
2216
2626
|
}
|
|
2217
|
-
return this.expression(index, initialized, output);
|
|
2218
2627
|
}
|
|
2219
|
-
|
|
2220
|
-
|
|
2221
|
-
|
|
2222
|
-
|
|
2223
|
-
|
|
2628
|
+
}
|
|
2629
|
+
|
|
2630
|
+
// packages/compiler/src/ir/link.ts
|
|
2631
|
+
function linkModule(module, declarations, functions, normalizedBlocks, edges) {
|
|
2632
|
+
const entries = checkPrograms(module, declarations);
|
|
2633
|
+
const routines = new Map([...functions, ...normalizedBlocks].map((fn) => [fn.id, fn]));
|
|
2634
|
+
checkRecursion(routines, edges, entries, declarations);
|
|
2635
|
+
checkLoopCounterWrites(module, declarations, routines, edges);
|
|
2636
|
+
const reachable = reachableRoutines(module, declarations, functions, entries, edges);
|
|
2637
|
+
const linkedFunctions = functions.filter((fn) => reachable.has(fn.id));
|
|
2638
|
+
const linkedBlocks = normalizedBlocks.filter((block) => reachable.has(block.id));
|
|
2639
|
+
const usedTypes = usedTypeIds(module, declarations.types, linkedFunctions, linkedBlocks);
|
|
2640
|
+
return {
|
|
2641
|
+
types: module.types.filter((type) => usedTypes.has(type.id)),
|
|
2642
|
+
variables: module.variables,
|
|
2643
|
+
functions: linkedFunctions,
|
|
2644
|
+
blocks: linkedBlocks,
|
|
2645
|
+
programs: module.programs,
|
|
2646
|
+
...module.folded && { folded: module.folded },
|
|
2647
|
+
...module.references && { references: module.references }
|
|
2648
|
+
};
|
|
2649
|
+
}
|
|
2650
|
+
var checkPrograms = (module, declarations) => {
|
|
2651
|
+
const names = new Set;
|
|
2652
|
+
const entries = new Set;
|
|
2653
|
+
for (const program of module.programs) {
|
|
2654
|
+
declarations.claimId(program.id);
|
|
2655
|
+
const entry = declarations.signatures.get(program.entry);
|
|
2656
|
+
if (!entry || entry.owner || entry.parameters.length || entry.result) {
|
|
2657
|
+
reject("Program entry must be a parameterless void helper.");
|
|
2224
2658
|
}
|
|
2225
|
-
|
|
2226
|
-
|
|
2227
|
-
const input = called.inputs.find((port) => port.name === name);
|
|
2228
|
-
return input ? [{ input, value }] : [];
|
|
2229
|
-
});
|
|
2230
|
-
if (Object.keys(item.inputs).length !== called.inputs.length || supplied.length !== called.inputs.length) {
|
|
2231
|
-
reject("Block update must supply every named input exactly once.", item.span);
|
|
2659
|
+
if (names.has(program.name)) {
|
|
2660
|
+
reject(`Duplicate program ${program.name}.`);
|
|
2232
2661
|
}
|
|
2233
|
-
|
|
2234
|
-
|
|
2235
|
-
input.name,
|
|
2236
|
-
this.argument(input, value, initialized, output)
|
|
2237
|
-
]));
|
|
2238
|
-
output.push({
|
|
2239
|
-
kind: "blockUpdate",
|
|
2240
|
-
instance: item.instance,
|
|
2241
|
-
...index && { index },
|
|
2242
|
-
inputs,
|
|
2243
|
-
span: item.span
|
|
2244
|
-
});
|
|
2662
|
+
names.add(program.name);
|
|
2663
|
+
entries.add(program.entry);
|
|
2245
2664
|
}
|
|
2246
|
-
|
|
2247
|
-
|
|
2665
|
+
if (!module.programs.length) {
|
|
2666
|
+
reject("At least one program is required.");
|
|
2248
2667
|
}
|
|
2249
|
-
|
|
2250
|
-
|
|
2251
|
-
|
|
2668
|
+
return entries;
|
|
2669
|
+
};
|
|
2670
|
+
var checkRecursion = (routines, edges, entries, { signatures }) => {
|
|
2671
|
+
const visited = new Set;
|
|
2672
|
+
const active = new Set;
|
|
2673
|
+
const visit = (id) => {
|
|
2674
|
+
if (active.has(id)) {
|
|
2675
|
+
reject("Recursive scan calls are prohibited.", signatures.get(id)?.span);
|
|
2252
2676
|
}
|
|
2253
|
-
if (
|
|
2254
|
-
return
|
|
2677
|
+
if (visited.has(id)) {
|
|
2678
|
+
return;
|
|
2255
2679
|
}
|
|
2256
|
-
|
|
2257
|
-
|
|
2258
|
-
|
|
2680
|
+
active.add(id);
|
|
2681
|
+
for (const child of edges.get(id) ?? []) {
|
|
2682
|
+
if (entries.has(child)) {
|
|
2683
|
+
reject("Program entries cannot be called as ordinary helpers.", signatures.get(id)?.span);
|
|
2259
2684
|
}
|
|
2260
|
-
|
|
2261
|
-
output.push({
|
|
2262
|
-
kind: "assign",
|
|
2263
|
-
root: temporary.id,
|
|
2264
|
-
value: this.expression(value, initialized, output),
|
|
2265
|
-
span: value.span
|
|
2266
|
-
});
|
|
2267
|
-
initialized.add(temporary.id);
|
|
2268
|
-
return {
|
|
2269
|
-
kind: "borrow",
|
|
2270
|
-
type: value.type,
|
|
2271
|
-
place: { root: temporary.id, path: [] },
|
|
2272
|
-
span: value.span
|
|
2273
|
-
};
|
|
2274
|
-
}
|
|
2275
|
-
if (value.kind === "borrow") {
|
|
2276
|
-
reject("Readonly value inputs cannot receive borrowed references.", value.span);
|
|
2685
|
+
visit(child);
|
|
2277
2686
|
}
|
|
2278
|
-
|
|
2687
|
+
active.delete(id);
|
|
2688
|
+
visited.add(id);
|
|
2689
|
+
};
|
|
2690
|
+
for (const id of routines.keys()) {
|
|
2691
|
+
visit(id);
|
|
2279
2692
|
}
|
|
2280
|
-
|
|
2281
|
-
|
|
2282
|
-
|
|
2283
|
-
|
|
2284
|
-
|
|
2285
|
-
|
|
2693
|
+
};
|
|
2694
|
+
var checkLoopCounterWrites = (module, { types }, routines, edges) => {
|
|
2695
|
+
const variables = new Map([
|
|
2696
|
+
...module.variables,
|
|
2697
|
+
...[...routines.values()].flatMap((routine) => ("inputs" in routine) ? [
|
|
2698
|
+
...routine.inputs,
|
|
2699
|
+
...routine.outputs,
|
|
2700
|
+
...routine.state,
|
|
2701
|
+
...routine.locals
|
|
2702
|
+
] : [...routine.parameters, ...routine.locals])
|
|
2703
|
+
].map((variable) => [variable.id, variable]));
|
|
2704
|
+
const calledRoutine = (item) => {
|
|
2705
|
+
if (item.kind === "invoke") {
|
|
2706
|
+
return item.function;
|
|
2286
2707
|
}
|
|
2287
|
-
|
|
2288
|
-
|
|
2289
|
-
|
|
2290
|
-
|
|
2291
|
-
|
|
2292
|
-
|
|
2293
|
-
|
|
2294
|
-
|
|
2708
|
+
if (item.kind !== "blockUpdate") {
|
|
2709
|
+
return;
|
|
2710
|
+
}
|
|
2711
|
+
const variable = variables.get(item.instance);
|
|
2712
|
+
return variable && instancedBlock(variable.type, types)?.block;
|
|
2713
|
+
};
|
|
2714
|
+
const writes = new Map;
|
|
2715
|
+
const routineWrites = (id) => {
|
|
2716
|
+
const cached = writes.get(id);
|
|
2717
|
+
if (cached) {
|
|
2718
|
+
return cached;
|
|
2719
|
+
}
|
|
2720
|
+
const result = new Set;
|
|
2721
|
+
const inspect = (body) => {
|
|
2722
|
+
for (const item of body) {
|
|
2723
|
+
for (const root of directWrites(item)) {
|
|
2724
|
+
result.add(root);
|
|
2725
|
+
}
|
|
2726
|
+
for (const nested of nestedBodies(item)) {
|
|
2727
|
+
inspect(nested);
|
|
2728
|
+
}
|
|
2729
|
+
}
|
|
2295
2730
|
};
|
|
2296
|
-
|
|
2297
|
-
|
|
2298
|
-
|
|
2299
|
-
|
|
2300
|
-
for (const item of statements) {
|
|
2301
|
-
if (state.flow !== "fallthrough") {
|
|
2302
|
-
break;
|
|
2731
|
+
inspect(routines.get(id).body);
|
|
2732
|
+
for (const child of edges.get(id) ?? []) {
|
|
2733
|
+
for (const root of routineWrites(child)) {
|
|
2734
|
+
result.add(root);
|
|
2303
2735
|
}
|
|
2304
|
-
state = this.statement(item, state.initialized, output);
|
|
2305
2736
|
}
|
|
2306
|
-
|
|
2307
|
-
|
|
2308
|
-
|
|
2309
|
-
|
|
2310
|
-
|
|
2311
|
-
|
|
2312
|
-
|
|
2313
|
-
|
|
2314
|
-
this.blockUpdate(item, initialized, output);
|
|
2315
|
-
return { flow: "fallthrough", initialized };
|
|
2316
|
-
case "invoke":
|
|
2317
|
-
this.invoke(item, initialized, output);
|
|
2318
|
-
return { flow: "fallthrough", initialized };
|
|
2319
|
-
case "if":
|
|
2320
|
-
return this.ifStatement(item, initialized, output);
|
|
2321
|
-
case "switch":
|
|
2322
|
-
return this.switchStatement(item, initialized, output);
|
|
2323
|
-
case "for":
|
|
2324
|
-
this.forStatement(item, initialized, output);
|
|
2325
|
-
return { flow: "fallthrough", initialized };
|
|
2326
|
-
case "return": {
|
|
2327
|
-
if (item.value?.type !== this.fn.result) {
|
|
2328
|
-
reject("Return differs from the function signature.", item.span);
|
|
2329
|
-
}
|
|
2330
|
-
const value = item.value && this.expression(item.value, initialized, output);
|
|
2331
|
-
output.push({
|
|
2332
|
-
kind: "return",
|
|
2333
|
-
span: item.span,
|
|
2334
|
-
...value && { value }
|
|
2335
|
-
});
|
|
2336
|
-
return { flow: "return", initialized };
|
|
2737
|
+
writes.set(id, result);
|
|
2738
|
+
return result;
|
|
2739
|
+
};
|
|
2740
|
+
const checkCalls = (body, counters) => {
|
|
2741
|
+
for (const item of body) {
|
|
2742
|
+
const called = calledRoutine(item);
|
|
2743
|
+
if (called && [...counters].some((counter) => routineWrites(called).has(counter))) {
|
|
2744
|
+
reject("A called routine may mutate an active loop counter.", item.span);
|
|
2337
2745
|
}
|
|
2338
|
-
|
|
2339
|
-
|
|
2340
|
-
|
|
2341
|
-
|
|
2342
|
-
|
|
2343
|
-
|
|
2344
|
-
|
|
2345
|
-
|
|
2346
|
-
|
|
2347
|
-
|
|
2348
|
-
|
|
2349
|
-
|
|
2746
|
+
const nestedCounters = item.kind === "for" ? new Set([...counters, item.counter]) : counters;
|
|
2747
|
+
for (const nested of nestedBodies(item)) {
|
|
2748
|
+
checkCalls(nested, nestedCounters);
|
|
2749
|
+
}
|
|
2750
|
+
}
|
|
2751
|
+
};
|
|
2752
|
+
for (const routine of routines.values()) {
|
|
2753
|
+
checkCalls(routine.body, new Set);
|
|
2754
|
+
}
|
|
2755
|
+
};
|
|
2756
|
+
var reachableRoutines = (module, { signatures, blocks, types }, functions, entries, edges) => {
|
|
2757
|
+
const reachable = new Set;
|
|
2758
|
+
const link = (id) => {
|
|
2759
|
+
if (reachable.has(id)) {
|
|
2760
|
+
return;
|
|
2761
|
+
}
|
|
2762
|
+
reachable.add(id);
|
|
2763
|
+
for (const child of edges.get(id) ?? []) {
|
|
2764
|
+
link(child);
|
|
2350
2765
|
}
|
|
2351
|
-
|
|
2352
|
-
|
|
2353
|
-
|
|
2354
|
-
const path = this.normalizePath(item.destination, initialized, output);
|
|
2355
|
-
const bound = !path.length && this.scope.get(item.destination.root)?.mutable === false && valueRange(item.value, this.declarations.types, this.scope, this.ranges, this.declarations.signatures);
|
|
2356
|
-
const value = this.expression(item.value, initialized, output);
|
|
2357
|
-
if (bound) {
|
|
2358
|
-
this.ranges.set(item.destination.root, bound);
|
|
2766
|
+
const owner = signatures.get(id)?.owner;
|
|
2767
|
+
if (owner) {
|
|
2768
|
+
link(owner.block);
|
|
2359
2769
|
}
|
|
2360
|
-
|
|
2361
|
-
|
|
2770
|
+
for (const method of functions.filter((fn) => fn.owner?.block === id)) {
|
|
2771
|
+
link(method.id);
|
|
2362
2772
|
}
|
|
2363
|
-
|
|
2364
|
-
|
|
2365
|
-
|
|
2366
|
-
|
|
2367
|
-
value,
|
|
2368
|
-
span: item.span
|
|
2369
|
-
});
|
|
2370
|
-
}
|
|
2371
|
-
invoke(item, initialized, output) {
|
|
2372
|
-
const called = this.verifyCall(item.function, item.arguments, item.span, item.instance);
|
|
2373
|
-
if (item.destination) {
|
|
2374
|
-
if (item.destination.path.length || !called.result) {
|
|
2375
|
-
reject("Invalid helper destination.", item.span);
|
|
2773
|
+
for (const instance of blocks.get(id)?.state.filter((variable) => variable.storage === "instance") ?? []) {
|
|
2774
|
+
const type = instancedBlock(instance.type, types);
|
|
2775
|
+
if (type) {
|
|
2776
|
+
link(type.block);
|
|
2376
2777
|
}
|
|
2377
|
-
this.destination(item.destination, called.result, initialized, item.span);
|
|
2378
2778
|
}
|
|
2379
|
-
|
|
2380
|
-
|
|
2381
|
-
|
|
2382
|
-
|
|
2383
|
-
|
|
2384
|
-
|
|
2385
|
-
|
|
2386
|
-
|
|
2387
|
-
span: item.span,
|
|
2388
|
-
...item.destination && { destination: item.destination.root }
|
|
2389
|
-
});
|
|
2390
|
-
if (item.destination) {
|
|
2391
|
-
initialized.add(item.destination.root);
|
|
2779
|
+
};
|
|
2780
|
+
for (const entry of entries) {
|
|
2781
|
+
link(entry);
|
|
2782
|
+
}
|
|
2783
|
+
for (const variable of module.variables) {
|
|
2784
|
+
const type = instancedBlock(variable.type, types);
|
|
2785
|
+
if (type) {
|
|
2786
|
+
link(type.block);
|
|
2392
2787
|
}
|
|
2393
2788
|
}
|
|
2394
|
-
|
|
2395
|
-
|
|
2396
|
-
|
|
2789
|
+
return reachable;
|
|
2790
|
+
};
|
|
2791
|
+
var usedTypeIds = (module, types, linkedFunctions, linkedBlocks) => {
|
|
2792
|
+
const usedTypes = new Set;
|
|
2793
|
+
const useType = (id) => {
|
|
2794
|
+
if (usedTypes.has(id)) {
|
|
2795
|
+
return;
|
|
2397
2796
|
}
|
|
2398
|
-
|
|
2399
|
-
const
|
|
2400
|
-
|
|
2401
|
-
|
|
2402
|
-
|
|
2403
|
-
this.ranges.set(root, range);
|
|
2404
|
-
}
|
|
2405
|
-
try {
|
|
2406
|
-
return this.body(statements, new Set(initialized));
|
|
2407
|
-
} finally {
|
|
2408
|
-
this.ranges.clear();
|
|
2409
|
-
for (const [root, range] of originalRanges) {
|
|
2410
|
-
this.ranges.set(root, range);
|
|
2411
|
-
}
|
|
2797
|
+
usedTypes.add(id);
|
|
2798
|
+
const type = types.get(id);
|
|
2799
|
+
if (type.kind === "record") {
|
|
2800
|
+
for (const field of type.fields) {
|
|
2801
|
+
useType(field.type);
|
|
2412
2802
|
}
|
|
2413
|
-
}
|
|
2414
|
-
|
|
2415
|
-
|
|
2416
|
-
|
|
2417
|
-
|
|
2418
|
-
condition,
|
|
2419
|
-
then: yes.body,
|
|
2420
|
-
else: no.body,
|
|
2421
|
-
span: item.span
|
|
2422
|
-
});
|
|
2423
|
-
return join([yes, no], initialized);
|
|
2424
|
-
}
|
|
2425
|
-
switchStatement(item, initialized, output) {
|
|
2426
|
-
const value = this.expression(item.value, initialized, output);
|
|
2427
|
-
const type = this.declarations.types.get(item.value.type);
|
|
2428
|
-
const seen = new Set;
|
|
2429
|
-
const cases = item.cases.map((branch) => {
|
|
2430
|
-
for (const constant of branch.values) {
|
|
2431
|
-
if (!valueFits(constant, type, this.declarations.types)) {
|
|
2432
|
-
reject("Invalid case constant.", item.span);
|
|
2433
|
-
}
|
|
2434
|
-
const key = JSON.stringify(constant);
|
|
2435
|
-
if (seen.has(key)) {
|
|
2436
|
-
reject("Duplicate case constant.", item.span);
|
|
2437
|
-
}
|
|
2438
|
-
seen.add(key);
|
|
2803
|
+
}
|
|
2804
|
+
if (type.kind === "array") {
|
|
2805
|
+
useType(type.element);
|
|
2806
|
+
if (type.index) {
|
|
2807
|
+
useType(type.index);
|
|
2439
2808
|
}
|
|
2440
|
-
return {
|
|
2441
|
-
values: branch.values,
|
|
2442
|
-
result: this.body(branch.body, new Set(initialized))
|
|
2443
|
-
};
|
|
2444
|
-
});
|
|
2445
|
-
const fallback = this.body(item.fallback, new Set(initialized));
|
|
2446
|
-
output.push({
|
|
2447
|
-
kind: "switch",
|
|
2448
|
-
value,
|
|
2449
|
-
cases: cases.map((branch) => ({
|
|
2450
|
-
values: branch.values,
|
|
2451
|
-
body: branch.result.body
|
|
2452
|
-
})),
|
|
2453
|
-
fallback: fallback.body,
|
|
2454
|
-
span: item.span
|
|
2455
|
-
});
|
|
2456
|
-
return join([fallback, ...cases.map((branch) => branch.result)], initialized);
|
|
2457
|
-
}
|
|
2458
|
-
forStatement(item, initialized, output) {
|
|
2459
|
-
const counter = this.sourceScope.get(item.counter);
|
|
2460
|
-
const counterType = counter && this.declarations.types.get(counter.type);
|
|
2461
|
-
const storage = item.counterScope === "existing" ? ["local", "module", "field"] : ["local"];
|
|
2462
|
-
if (!counter || !storage.includes(counter.storage) || !counter.mutable || counterType?.kind !== "integer" || this.counters.has(counter.id) || item.start.type !== counter.type || item.limit.type !== counter.type || !canonicalInteger.test(item.step) || item.step === "0") {
|
|
2463
|
-
reject("Invalid bounded loop.", item.span);
|
|
2464
2809
|
}
|
|
2465
|
-
|
|
2466
|
-
|
|
2467
|
-
const startRange = interval(item.start, this.declarations.types, this.sourceScope, this.ranges);
|
|
2468
|
-
const limitRange = interval(item.limit, this.declarations.types, this.sourceScope, this.ranges);
|
|
2469
|
-
if (!startRange || !limitRange) {
|
|
2470
|
-
reject("Loop bounds require finite integer ranges.", item.span);
|
|
2810
|
+
if (type.kind === "enum") {
|
|
2811
|
+
useType(type.base);
|
|
2471
2812
|
}
|
|
2472
|
-
|
|
2473
|
-
|
|
2474
|
-
const dynamic = start?.kind !== "integer" || limit?.kind !== "integer";
|
|
2475
|
-
if (count > maxLoopIterations || Number(count) !== item.maxIterations || !Number.isSafeInteger(item.maxIterations)) {
|
|
2476
|
-
reject("Loop iteration proof differs from its bounds.", item.span);
|
|
2813
|
+
if (type.kind === "block" && type.output) {
|
|
2814
|
+
useType(type.output);
|
|
2477
2815
|
}
|
|
2478
|
-
|
|
2479
|
-
|
|
2480
|
-
|
|
2481
|
-
|
|
2482
|
-
reject("The existing loop counter overflows on termination.", item.span);
|
|
2483
|
-
}
|
|
2816
|
+
};
|
|
2817
|
+
types.forEach((type) => {
|
|
2818
|
+
if (["bool", "integer", "bits", "float", "duration", "calendar"].includes(type.kind)) {
|
|
2819
|
+
useType(type.id);
|
|
2484
2820
|
}
|
|
2485
|
-
|
|
2486
|
-
|
|
2487
|
-
|
|
2488
|
-
|
|
2489
|
-
|
|
2490
|
-
|
|
2491
|
-
|
|
2492
|
-
|
|
2493
|
-
|
|
2494
|
-
|
|
2821
|
+
});
|
|
2822
|
+
for (const variable of module.variables) {
|
|
2823
|
+
useType(variable.type);
|
|
2824
|
+
}
|
|
2825
|
+
for (const fn of linkedFunctions) {
|
|
2826
|
+
if (fn.result) {
|
|
2827
|
+
useType(fn.result);
|
|
2828
|
+
}
|
|
2829
|
+
for (const variable of [...fn.parameters, ...fn.locals]) {
|
|
2830
|
+
useType(variable.type);
|
|
2495
2831
|
}
|
|
2496
|
-
output.push({
|
|
2497
|
-
kind: "for",
|
|
2498
|
-
counter: counter.id,
|
|
2499
|
-
...item.counterScope && { counterScope: item.counterScope },
|
|
2500
|
-
start: { kind: "integer", value: String(first) },
|
|
2501
|
-
limit: { kind: "integer", value: String(last) },
|
|
2502
|
-
...dynamic && {
|
|
2503
|
-
dynamic: {
|
|
2504
|
-
start: evaluatedStart,
|
|
2505
|
-
limit: evaluatedLimit,
|
|
2506
|
-
...item.exclusive && { exclusive: item.exclusive }
|
|
2507
|
-
}
|
|
2508
|
-
},
|
|
2509
|
-
step: item.step,
|
|
2510
|
-
maxIterations: item.maxIterations,
|
|
2511
|
-
body: nested.body,
|
|
2512
|
-
span: item.span
|
|
2513
|
-
});
|
|
2514
2832
|
}
|
|
2515
|
-
|
|
2516
|
-
|
|
2517
|
-
|
|
2518
|
-
|
|
2519
|
-
|
|
2520
|
-
|
|
2521
|
-
|
|
2522
|
-
|
|
2523
|
-
|
|
2524
|
-
|
|
2525
|
-
|
|
2526
|
-
|
|
2833
|
+
for (const block of linkedBlocks) {
|
|
2834
|
+
if (block.result) {
|
|
2835
|
+
useType(block.result);
|
|
2836
|
+
}
|
|
2837
|
+
for (const variable of [
|
|
2838
|
+
...block.inputs,
|
|
2839
|
+
...block.outputs,
|
|
2840
|
+
...block.state,
|
|
2841
|
+
...block.locals
|
|
2842
|
+
]) {
|
|
2843
|
+
useType(variable.type);
|
|
2844
|
+
}
|
|
2845
|
+
}
|
|
2846
|
+
const useTelemetryTypes = (body) => {
|
|
2847
|
+
for (const item of body) {
|
|
2848
|
+
if (item.kind === "telemetry") {
|
|
2849
|
+
for (const operand of telemetryOperands(item)) {
|
|
2850
|
+
useType(operand.type);
|
|
2527
2851
|
}
|
|
2528
|
-
|
|
2529
|
-
|
|
2530
|
-
|
|
2531
|
-
|
|
2532
|
-
|
|
2533
|
-
|
|
2534
|
-
|
|
2535
|
-
|
|
2852
|
+
}
|
|
2853
|
+
for (const nested of nestedBodies(item)) {
|
|
2854
|
+
useTelemetryTypes(nested);
|
|
2855
|
+
}
|
|
2856
|
+
}
|
|
2857
|
+
};
|
|
2858
|
+
for (const routine of [...linkedFunctions, ...linkedBlocks]) {
|
|
2859
|
+
useTelemetryTypes(routine.body);
|
|
2536
2860
|
}
|
|
2537
|
-
|
|
2861
|
+
return usedTypes;
|
|
2862
|
+
};
|
|
2538
2863
|
|
|
2539
2864
|
// packages/compiler/src/ir/normalize.ts
|
|
2540
2865
|
var verifiedModules = new WeakSet;
|
|
@@ -3391,7 +3716,6 @@ function substitute(item, root, value) {
|
|
|
3391
3716
|
export {
|
|
3392
3717
|
aggregate,
|
|
3393
3718
|
aggregateIssue,
|
|
3394
|
-
applyIntegerPolicy,
|
|
3395
3719
|
calendarLiteral,
|
|
3396
3720
|
canAssign,
|
|
3397
3721
|
comparisonOperators,
|
|
@@ -3401,12 +3725,9 @@ export {
|
|
|
3401
3725
|
domainConstantFits,
|
|
3402
3726
|
domainIssue,
|
|
3403
3727
|
durationLiteral,
|
|
3404
|
-
elementaryFunctions,
|
|
3405
|
-
elementaryValue,
|
|
3406
3728
|
error,
|
|
3407
3729
|
errorName,
|
|
3408
3730
|
expressionIssue,
|
|
3409
|
-
floatMaximum,
|
|
3410
3731
|
fold,
|
|
3411
3732
|
foldConstantRoutines,
|
|
3412
3733
|
formatDiagnostic,
|
|
@@ -3417,14 +3738,12 @@ export {
|
|
|
3417
3738
|
integerRange,
|
|
3418
3739
|
integerType,
|
|
3419
3740
|
interval,
|
|
3420
|
-
isElementary,
|
|
3421
3741
|
isVerifiedCore,
|
|
3422
3742
|
jobError,
|
|
3423
3743
|
jobErrors,
|
|
3424
3744
|
joinPrograms,
|
|
3425
3745
|
layoutIssue,
|
|
3426
3746
|
loopBounds,
|
|
3427
|
-
loopIterations,
|
|
3428
3747
|
maxLoopIterations,
|
|
3429
3748
|
maxNestingDepth,
|
|
3430
3749
|
maxScalarElements,
|
|
@@ -3433,11 +3752,11 @@ export {
|
|
|
3433
3752
|
nestedBodies,
|
|
3434
3753
|
normalizeModule,
|
|
3435
3754
|
parseInteger,
|
|
3755
|
+
pathConditions,
|
|
3436
3756
|
pathIndexes,
|
|
3437
3757
|
placeType,
|
|
3438
3758
|
primitiveTypes,
|
|
3439
|
-
|
|
3440
|
-
saturateFloat,
|
|
3759
|
+
projectReconstruction,
|
|
3441
3760
|
scalarIssue,
|
|
3442
3761
|
shareRoutines,
|
|
3443
3762
|
standardBlocks,
|
|
@@ -3449,5 +3768,5 @@ export {
|
|
|
3449
3768
|
wrapsNatively
|
|
3450
3769
|
};
|
|
3451
3770
|
|
|
3452
|
-
//# debugId=
|
|
3771
|
+
//# debugId=1D832A6B2CC6ECA364756E2164756E21
|
|
3453
3772
|
//# sourceMappingURL=ir.js.map
|