@tsplc/tmp-compiler 0.0.0-stage → 0.6.3
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/LICENSE +21 -0
- package/README.md +41 -2
- package/dist/cli.js +2325 -0
- package/dist/cli.js.map +26 -0
- package/dist/clock.js +11 -0
- package/dist/clock.js.map +11 -0
- package/dist/codesys.js +6563 -0
- package/dist/codesys.js.map +51 -0
- package/dist/collections.js +399 -0
- package/dist/collections.js.map +10 -0
- package/dist/index.js +16198 -0
- package/dist/index.js.map +62 -0
- package/dist/ir.js +3948 -0
- package/dist/ir.js.map +35 -0
- package/dist/retain.js +10 -0
- package/dist/retain.js.map +10 -0
- package/dist/signals.js +115 -0
- package/dist/signals.js.map +10 -0
- package/dist/telemetry.js +121 -0
- package/dist/telemetry.js.map +12 -0
- package/package.json +122 -4
- package/src/codesys/config.ts +50 -0
- package/src/codesys/debug.ts +655 -0
- package/src/codesys/emit.ts +185 -0
- package/src/codesys/endpoints.ts +111 -0
- package/src/codesys/engineering.ts +96 -0
- package/src/codesys/explain/expressions.ts +445 -0
- package/src/codesys/explain/flags.ts +134 -0
- package/src/codesys/explain/parameters.ts +53 -0
- package/src/codesys/explain/rows.ts +182 -0
- package/src/codesys/explain/tables.ts +79 -0
- package/src/codesys/explain/tasks.ts +68 -0
- package/src/codesys/explain/writes.ts +497 -0
- package/src/codesys/explain.ts +149 -0
- package/src/codesys/expressions.ts +100 -0
- package/src/codesys/global-lists.ts +284 -0
- package/src/codesys/global-names.ts +66 -0
- package/src/codesys/http.ts +1090 -0
- package/src/codesys/index.ts +13 -0
- package/src/codesys/instrumentation.ts +574 -0
- package/src/codesys/io-paths.ts +149 -0
- package/src/codesys/loops.ts +101 -0
- package/src/codesys/manifest.ts +684 -0
- package/src/codesys/names.ts +58 -0
- package/src/codesys/numeric.ts +516 -0
- package/src/codesys/plan.ts +688 -0
- package/src/codesys/programs.ts +138 -0
- package/src/codesys/publication.ts +99 -0
- package/src/codesys/required.ts +8 -0
- package/src/codesys/routines.ts +247 -0
- package/src/codesys/scan-clock.ts +103 -0
- package/src/codesys/scheduling.ts +96 -0
- package/src/codesys/service-channels.ts +24 -0
- package/src/codesys/sources.ts +118 -0
- package/src/codesys/st-files.ts +219 -0
- package/src/codesys/statements.ts +409 -0
- package/src/codesys/storage.ts +150 -0
- package/src/codesys/symbols.ts +240 -0
- package/src/codesys/task-access.ts +186 -0
- package/src/codesys/telemetry-settings.ts +19 -0
- package/src/codesys/telemetry.ts +852 -0
- package/src/codesys/types.ts +310 -0
- package/src/codesys/vendor.ts +113 -0
- package/src/frontend/analyzer.ts +779 -0
- package/src/frontend/async.ts +530 -0
- package/src/frontend/blocks.ts +578 -0
- package/src/frontend/configuration.ts +83 -0
- package/src/frontend/const-enums.ts +196 -0
- package/src/frontend/const-functions.ts +204 -0
- package/src/frontend/control-flow.ts +787 -0
- package/src/frontend/cooperative.ts +114 -0
- package/src/frontend/coroutines.ts +1344 -0
- package/src/frontend/dialect-names.ts +104 -0
- package/src/frontend/elaborate/array-searches.ts +224 -0
- package/src/frontend/elaborate/assignments.ts +343 -0
- package/src/frontend/elaborate/calls.ts +1342 -0
- package/src/frontend/elaborate/capture-protocol.ts +237 -0
- package/src/frontend/elaborate/capture.ts +388 -0
- package/src/frontend/elaborate/declarations.ts +124 -0
- package/src/frontend/elaborate/dialect.ts +473 -0
- package/src/frontend/elaborate/expressions.ts +2177 -0
- package/src/frontend/elaborate/filtered-arrays.ts +643 -0
- package/src/frontend/elaborate/frame.ts +145 -0
- package/src/frontend/elaborate/generics.ts +123 -0
- package/src/frontend/elaborate/graph.ts +141 -0
- package/src/frontend/elaborate/imports.ts +105 -0
- package/src/frontend/elaborate/index.ts +851 -0
- package/src/frontend/elaborate/initializers.ts +235 -0
- package/src/frontend/elaborate/iterators.ts +1541 -0
- package/src/frontend/elaborate/layouts.ts +1025 -0
- package/src/frontend/elaborate/model.ts +478 -0
- package/src/frontend/elaborate/names.ts +134 -0
- package/src/frontend/elaborate/reduce.ts +900 -0
- package/src/frontend/elaborate/references.ts +727 -0
- package/src/frontend/elaborate/retention.ts +284 -0
- package/src/frontend/elaborate/specialize.ts +1137 -0
- package/src/frontend/elaborate/statements.ts +1220 -0
- package/src/frontend/elaborate/storage.ts +836 -0
- package/src/frontend/elaborate/time-constructors.ts +34 -0
- package/src/frontend/elaborate/values.ts +200 -0
- package/src/frontend/elaborate/writes.ts +415 -0
- package/src/frontend/expressions.ts +1382 -0
- package/src/frontend/generated-names.ts +95 -0
- package/src/frontend/intrinsics.ts +382 -0
- package/src/frontend/lowering-key.ts +23 -0
- package/src/frontend/module-graph.ts +156 -0
- package/src/frontend/positions.ts +359 -0
- package/src/frontend/promise-chains.ts +872 -0
- package/src/frontend/promises.ts +419 -0
- package/src/frontend/routines.ts +239 -0
- package/src/frontend/sdk.ts +187 -0
- package/src/frontend/services.ts +612 -0
- package/src/frontend/statements.ts +871 -0
- package/src/frontend/tasks.ts +526 -0
- package/src/frontend/telemetry.ts +174 -0
- package/src/frontend/types.ts +1073 -0
- package/src/frontend/typescript.ts +659 -0
- package/src/index.ts +9 -0
- package/src/ir/aggregates.ts +291 -0
- package/src/ir/allocation.ts +96 -0
- package/src/ir/conditions.ts +855 -0
- package/src/ir/core-model.ts +289 -0
- package/src/ir/declarations.ts +281 -0
- package/src/ir/diagnostics.ts +68 -0
- package/src/ir/domains.ts +151 -0
- package/src/ir/fold.ts +279 -0
- package/src/ir/identity.ts +19 -0
- package/src/ir/index.ts +137 -0
- package/src/ir/job-errors.ts +69 -0
- package/src/ir/layouts.ts +211 -0
- package/src/ir/link.ts +302 -0
- package/src/ir/model.ts +394 -0
- package/src/ir/normalization-error.ts +10 -0
- package/src/ir/normalize.ts +52 -0
- package/src/ir/numeric.ts +163 -0
- package/src/ir/operands.ts +160 -0
- package/src/ir/ranges.ts +420 -0
- package/src/ir/routine.ts +1116 -0
- package/src/ir/scalars.ts +453 -0
- package/src/ir/share.ts +790 -0
- package/src/ir/short-circuit.ts +140 -0
- package/src/ir/tables.ts +111 -0
- package/src/ir/temporaries.ts +138 -0
- package/src/ir/types.ts +151 -0
- package/src/project/artifacts.ts +424 -0
- package/src/project/cli.ts +211 -0
- package/src/project/debugger.ts +111 -0
- package/src/project/deployments.ts +387 -0
- package/src/project/flags.ts +14 -0
- package/src/project/git.ts +35 -0
- package/src/project/plant.ts +629 -0
- package/src/project/project-commands.ts +507 -0
- package/src/project/project.ts +559 -0
- package/src/project/release.ts +152 -0
- package/src/project/server-check.ts +54 -0
- package/src/project/server.ts +165 -0
- package/src/project/terminal.ts +45 -0
- package/src/runtime/async.ts +207 -0
- package/src/runtime/launch.ts +59 -0
- package/src/runtime/main.ts +59 -0
- package/src/runtime/missing-file.ts +3 -0
- package/src/runtime/process.ts +177 -0
- package/src/runtime/program.ts +109 -0
- package/src/runtime/retain.ts +108 -0
- package/src/runtime/runtime.ts +324 -0
- package/src/runtime/settings.ts +27 -0
- package/src/runtime/storage.ts +456 -0
- package/src/runtime/symbols.ts +97 -0
- package/src/runtime/telemetry.ts +154 -0
- package/src/runtime/transport.ts +168 -0
- package/stdlib/clock.ts +8 -0
- package/stdlib/collections.ts +534 -0
- package/stdlib/plc-time.ts +2 -0
- package/stdlib/retain.ts +4 -0
- package/stdlib/signals.ts +142 -0
- package/stdlib/telemetry.ts +89 -0
- package/types/async.d.ts +87 -0
- package/types/clock.d.ts +8 -0
- package/types/config.d.ts +2 -0
- package/types/data-types.d.ts +31 -0
- package/types/dialect.d.ts +49 -0
- package/types/globals.d.ts +10 -0
- package/types/index.d.ts +212 -0
- package/types/retain.d.ts +12 -0
- package/types/telemetry.d.ts +47 -0
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import type { CoreExpression, CorePath, CoreStatement } from "./core-model";
|
|
2
|
+
import { telemetryOperands } from "./model";
|
|
3
|
+
|
|
4
|
+
/** The index expressions along a place's path. */
|
|
5
|
+
export const pathIndexes = (path: CorePath = []): CoreExpression[] =>
|
|
6
|
+
path.flatMap((segment) => (segment.kind === "index" ? [segment.value] : []));
|
|
7
|
+
|
|
8
|
+
/** A path with each index replaced by `map` of it. */
|
|
9
|
+
export const mapPath = (
|
|
10
|
+
path: CorePath,
|
|
11
|
+
map: (index: CoreExpression) => CoreExpression,
|
|
12
|
+
): CorePath =>
|
|
13
|
+
path.map((segment) =>
|
|
14
|
+
segment.kind === "field"
|
|
15
|
+
? segment
|
|
16
|
+
: { kind: "index", value: map(segment.value) },
|
|
17
|
+
);
|
|
18
|
+
|
|
19
|
+
/** The expressions an expression evaluates directly: its operands, or the indexes of the place it loads or borrows. */
|
|
20
|
+
export const subexpressions = (
|
|
21
|
+
value: CoreExpression,
|
|
22
|
+
): readonly CoreExpression[] => {
|
|
23
|
+
switch (value.kind) {
|
|
24
|
+
case "constant":
|
|
25
|
+
return [];
|
|
26
|
+
case "load":
|
|
27
|
+
return pathIndexes(value.path);
|
|
28
|
+
case "borrow":
|
|
29
|
+
return pathIndexes(value.place.path);
|
|
30
|
+
case "unary":
|
|
31
|
+
case "convert":
|
|
32
|
+
return [value.operand];
|
|
33
|
+
case "binary":
|
|
34
|
+
return [value.left, value.right];
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/** An expression with each of its `subexpressions` replaced by `map` of it. */
|
|
39
|
+
export const mapExpression = (
|
|
40
|
+
value: CoreExpression,
|
|
41
|
+
map: (operand: CoreExpression) => CoreExpression,
|
|
42
|
+
): CoreExpression => {
|
|
43
|
+
switch (value.kind) {
|
|
44
|
+
case "constant":
|
|
45
|
+
return value;
|
|
46
|
+
case "load":
|
|
47
|
+
return value.path ? { ...value, path: mapPath(value.path, map) } : value;
|
|
48
|
+
case "borrow":
|
|
49
|
+
return {
|
|
50
|
+
...value,
|
|
51
|
+
place: { ...value.place, path: mapPath(value.place.path, map) },
|
|
52
|
+
};
|
|
53
|
+
case "unary":
|
|
54
|
+
case "convert":
|
|
55
|
+
return { ...value, operand: map(value.operand) };
|
|
56
|
+
case "binary":
|
|
57
|
+
return { ...value, left: map(value.left), right: map(value.right) };
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* The expressions a statement evaluates itself, a place's indexes before the value stored or
|
|
63
|
+
* passed there. The statements nested in it are `nestedBodies`.
|
|
64
|
+
*/
|
|
65
|
+
export const statementExpressions = (
|
|
66
|
+
item: CoreStatement,
|
|
67
|
+
): readonly CoreExpression[] => {
|
|
68
|
+
switch (item.kind) {
|
|
69
|
+
case "assign":
|
|
70
|
+
return [...pathIndexes(item.path), item.value];
|
|
71
|
+
case "if":
|
|
72
|
+
return [item.condition];
|
|
73
|
+
case "switch":
|
|
74
|
+
return [item.value];
|
|
75
|
+
case "for":
|
|
76
|
+
return item.dynamic ? [item.dynamic.start, item.dynamic.limit] : [];
|
|
77
|
+
case "invoke":
|
|
78
|
+
return [...(item.index ? [item.index] : []), ...item.arguments];
|
|
79
|
+
case "blockUpdate":
|
|
80
|
+
return [
|
|
81
|
+
...(item.index ? [item.index] : []),
|
|
82
|
+
...Object.values(item.inputs),
|
|
83
|
+
];
|
|
84
|
+
case "return":
|
|
85
|
+
return item.value ? [item.value] : [];
|
|
86
|
+
case "telemetry":
|
|
87
|
+
return telemetryOperands(item);
|
|
88
|
+
case "break":
|
|
89
|
+
case "continue":
|
|
90
|
+
return [];
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* A statement with each of its `statementExpressions` replaced by `map` of it. A telemetry call's
|
|
96
|
+
* properties are mapped before its exception code: routine sharing numbers constants in the order
|
|
97
|
+
* it maps them, and that order is the merged routine's value parameter order in the emitted ST.
|
|
98
|
+
*/
|
|
99
|
+
export const mapStatementExpressions = (
|
|
100
|
+
item: CoreStatement,
|
|
101
|
+
map: (operand: CoreExpression) => CoreExpression,
|
|
102
|
+
): CoreStatement => {
|
|
103
|
+
switch (item.kind) {
|
|
104
|
+
case "assign":
|
|
105
|
+
return {
|
|
106
|
+
...item,
|
|
107
|
+
...(item.path && { path: mapPath(item.path, map) }),
|
|
108
|
+
value: map(item.value),
|
|
109
|
+
};
|
|
110
|
+
case "if":
|
|
111
|
+
return { ...item, condition: map(item.condition) };
|
|
112
|
+
case "switch":
|
|
113
|
+
return { ...item, value: map(item.value) };
|
|
114
|
+
case "for":
|
|
115
|
+
return item.dynamic
|
|
116
|
+
? {
|
|
117
|
+
...item,
|
|
118
|
+
dynamic: {
|
|
119
|
+
...item.dynamic,
|
|
120
|
+
start: map(item.dynamic.start),
|
|
121
|
+
limit: map(item.dynamic.limit),
|
|
122
|
+
},
|
|
123
|
+
}
|
|
124
|
+
: item;
|
|
125
|
+
case "invoke":
|
|
126
|
+
return {
|
|
127
|
+
...item,
|
|
128
|
+
...(item.index && { index: map(item.index) }),
|
|
129
|
+
arguments: item.arguments.map(map),
|
|
130
|
+
};
|
|
131
|
+
case "blockUpdate":
|
|
132
|
+
return {
|
|
133
|
+
...item,
|
|
134
|
+
...(item.index && { index: map(item.index) }),
|
|
135
|
+
inputs: Object.fromEntries(
|
|
136
|
+
Object.entries(item.inputs).map(([name, input]) => [
|
|
137
|
+
name,
|
|
138
|
+
map(input),
|
|
139
|
+
]),
|
|
140
|
+
),
|
|
141
|
+
};
|
|
142
|
+
case "return":
|
|
143
|
+
return item.value ? { ...item, value: map(item.value) } : item;
|
|
144
|
+
case "telemetry":
|
|
145
|
+
return {
|
|
146
|
+
...item,
|
|
147
|
+
properties: item.properties.map((property) => ({
|
|
148
|
+
...property,
|
|
149
|
+
value: map(property.value),
|
|
150
|
+
...(property.length && { length: map(property.length) }),
|
|
151
|
+
})),
|
|
152
|
+
...(item.record.kind === "exception" && {
|
|
153
|
+
record: { ...item.record, code: map(item.record.code) },
|
|
154
|
+
}),
|
|
155
|
+
};
|
|
156
|
+
case "break":
|
|
157
|
+
case "continue":
|
|
158
|
+
return item;
|
|
159
|
+
}
|
|
160
|
+
};
|
package/src/ir/ranges.ts
ADDED
|
@@ -0,0 +1,420 @@
|
|
|
1
|
+
import { type BinaryOperator, loopIterations } from "@tsplc/iec/arithmetic";
|
|
2
|
+
import { fold } from "./fold";
|
|
3
|
+
import type { Expression, Variable } from "./model";
|
|
4
|
+
import {
|
|
5
|
+
admits,
|
|
6
|
+
highest,
|
|
7
|
+
type IntegerInterval,
|
|
8
|
+
lowest,
|
|
9
|
+
within,
|
|
10
|
+
} from "./numeric";
|
|
11
|
+
import { integerRange, integerType, type PlcType } from "./types";
|
|
12
|
+
|
|
13
|
+
export const interval = (
|
|
14
|
+
expression: Expression,
|
|
15
|
+
types: ReadonlyMap<string, PlcType>,
|
|
16
|
+
variables: ReadonlyMap<string, Variable>,
|
|
17
|
+
ranges: ReadonlyMap<string, IntegerInterval>,
|
|
18
|
+
): IntegerInterval | undefined =>
|
|
19
|
+
factInterval(expression, { types, variables, ranges, facts: [] });
|
|
20
|
+
|
|
21
|
+
/** What a range proof reads, and what the conditions around a conditional's branch proved about its operands. */
|
|
22
|
+
interface RangeContext {
|
|
23
|
+
types: ReadonlyMap<string, PlcType>;
|
|
24
|
+
variables: ReadonlyMap<string, Variable>;
|
|
25
|
+
ranges: ReadonlyMap<string, IntegerInterval>;
|
|
26
|
+
facts: readonly Fact[];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** A range an expression is proved to keep where a branch runs, as `a` is below `b` in `a < b ? a : b`. */
|
|
30
|
+
interface Fact {
|
|
31
|
+
expression: Expression;
|
|
32
|
+
range: IntegerInterval;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const factInterval = (
|
|
36
|
+
expression: Expression,
|
|
37
|
+
context: RangeContext,
|
|
38
|
+
): IntegerInterval | undefined => {
|
|
39
|
+
const own = ownInterval(expression, context);
|
|
40
|
+
const fact = context.facts.find((known) =>
|
|
41
|
+
sameValue(known.expression, expression),
|
|
42
|
+
);
|
|
43
|
+
if (!own || !fact) {
|
|
44
|
+
return own;
|
|
45
|
+
}
|
|
46
|
+
const met = excluding(
|
|
47
|
+
excluding(
|
|
48
|
+
[highest(own[0], fact.range[0]), lowest(own[1], fact.range[1])],
|
|
49
|
+
own[2],
|
|
50
|
+
),
|
|
51
|
+
fact.range[2],
|
|
52
|
+
);
|
|
53
|
+
return met[0] <= met[1] ? met : own;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const ownInterval = (
|
|
57
|
+
expression: Expression,
|
|
58
|
+
context: RangeContext,
|
|
59
|
+
): IntegerInterval | undefined => {
|
|
60
|
+
const { types, variables, ranges } = context;
|
|
61
|
+
const interval = (operand: Expression) => factInterval(operand, context);
|
|
62
|
+
if (expression.kind === "length") {
|
|
63
|
+
const array = types.get(expression.operand.type);
|
|
64
|
+
if (array?.kind === "array") {
|
|
65
|
+
return [BigInt(array.length), BigInt(array.length)];
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
const value = fold(expression, variables, types);
|
|
69
|
+
if (value?.kind === "integer") {
|
|
70
|
+
return [BigInt(value.value), BigInt(value.value)];
|
|
71
|
+
}
|
|
72
|
+
const type = types.get(expression.type);
|
|
73
|
+
if (!type || !integerType(type)) {
|
|
74
|
+
return undefined;
|
|
75
|
+
}
|
|
76
|
+
if (expression.kind === "load") {
|
|
77
|
+
return ranges.get(expression.place.root) ?? integerRange(type);
|
|
78
|
+
}
|
|
79
|
+
if (expression.kind === "convert") {
|
|
80
|
+
return expression.policy === "exact"
|
|
81
|
+
? interval(expression.operand)
|
|
82
|
+
: integerRange(type);
|
|
83
|
+
}
|
|
84
|
+
if (expression.kind === "unary" && expression.operator === "negate") {
|
|
85
|
+
const operand = interval(expression.operand);
|
|
86
|
+
return operand && [-operand[1], -operand[0]];
|
|
87
|
+
}
|
|
88
|
+
if (expression.kind === "binary" && !expression.policy) {
|
|
89
|
+
const left = interval(expression.left);
|
|
90
|
+
const right = interval(expression.right);
|
|
91
|
+
return (
|
|
92
|
+
left &&
|
|
93
|
+
right &&
|
|
94
|
+
arithmeticInterval(expression.operator, left, right, integerRange(type))
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
if (expression.kind === "conditional") {
|
|
98
|
+
const whenTrue = branchInterval(expression, true, context);
|
|
99
|
+
const whenFalse = branchInterval(expression, false, context);
|
|
100
|
+
return (
|
|
101
|
+
whenTrue &&
|
|
102
|
+
whenFalse && [
|
|
103
|
+
lowest(whenTrue[0], whenFalse[0]),
|
|
104
|
+
highest(whenTrue[1], whenFalse[1]),
|
|
105
|
+
]
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
return integerRange(type);
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* A conditional's branch, with what its condition proves where the branch runs: the ranges an `if`
|
|
113
|
+
* narrows, and each compared operand bounded by the other, which gives `a < b ? a : b` (`Math.min`)
|
|
114
|
+
* and `x < 0 ? -x : x` (`Math.abs`) their ranges.
|
|
115
|
+
*/
|
|
116
|
+
const branchInterval = (
|
|
117
|
+
expression: Extract<Expression, { kind: "conditional" }>,
|
|
118
|
+
truth: boolean,
|
|
119
|
+
context: RangeContext,
|
|
120
|
+
): IntegerInterval | undefined => {
|
|
121
|
+
const { condition } = expression;
|
|
122
|
+
const narrowed: RangeContext = {
|
|
123
|
+
...context,
|
|
124
|
+
ranges: narrowRanges(
|
|
125
|
+
condition,
|
|
126
|
+
truth,
|
|
127
|
+
context.types,
|
|
128
|
+
context.variables,
|
|
129
|
+
context.ranges,
|
|
130
|
+
),
|
|
131
|
+
facts: [...context.facts, ...comparisonFacts(condition, truth, context)],
|
|
132
|
+
};
|
|
133
|
+
return factInterval(truth ? expression.then : expression.else, narrowed);
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
const flippedComparisons: Partial<Record<BinaryOperator, BinaryOperator>> = {
|
|
137
|
+
less: "greater",
|
|
138
|
+
lessEqual: "greaterEqual",
|
|
139
|
+
greater: "less",
|
|
140
|
+
greaterEqual: "lessEqual",
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
/** The range each side of an integer comparison keeps where the comparison is `truth`, bounded by the other side. */
|
|
144
|
+
const comparisonFacts = (
|
|
145
|
+
condition: Expression,
|
|
146
|
+
truth: boolean,
|
|
147
|
+
context: RangeContext,
|
|
148
|
+
): Fact[] => {
|
|
149
|
+
if (condition.kind !== "binary") {
|
|
150
|
+
return [];
|
|
151
|
+
}
|
|
152
|
+
const operator = truth
|
|
153
|
+
? condition.operator
|
|
154
|
+
: negatedComparisons[condition.operator];
|
|
155
|
+
const flipped = operator && flippedComparisons[operator];
|
|
156
|
+
if (!operator || !flipped) {
|
|
157
|
+
return [];
|
|
158
|
+
}
|
|
159
|
+
const left = factInterval(condition.left, context);
|
|
160
|
+
const right = factInterval(condition.right, context);
|
|
161
|
+
if (!left || !right) {
|
|
162
|
+
return [];
|
|
163
|
+
}
|
|
164
|
+
const side = (
|
|
165
|
+
expression: Expression,
|
|
166
|
+
own: IntegerInterval,
|
|
167
|
+
comparison: BinaryOperator,
|
|
168
|
+
other: IntegerInterval,
|
|
169
|
+
): Fact[] =>
|
|
170
|
+
pure(expression)
|
|
171
|
+
? [{ expression, range: boundedBy(own, comparison, other) }]
|
|
172
|
+
: [];
|
|
173
|
+
return [
|
|
174
|
+
...side(condition.left, left, operator, right),
|
|
175
|
+
...side(condition.right, right, flipped, left),
|
|
176
|
+
];
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
/** `own` where `own comparison other` holds for some value of `other`. */
|
|
180
|
+
const boundedBy = (
|
|
181
|
+
own: IntegerInterval,
|
|
182
|
+
comparison: BinaryOperator,
|
|
183
|
+
other: IntegerInterval,
|
|
184
|
+
): IntegerInterval => {
|
|
185
|
+
switch (comparison) {
|
|
186
|
+
case "less":
|
|
187
|
+
case "lessEqual":
|
|
188
|
+
return bounded(own, comparison, other[1]);
|
|
189
|
+
default:
|
|
190
|
+
return bounded(own, comparison, other[0]);
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
/** Whether an expression reads the same value each time it is evaluated: no call, whose result may differ. */
|
|
195
|
+
const pure = (expression: Expression): boolean => {
|
|
196
|
+
switch (expression.kind) {
|
|
197
|
+
case "constant":
|
|
198
|
+
return true;
|
|
199
|
+
case "load":
|
|
200
|
+
return expression.place.path.every(
|
|
201
|
+
(segment) => segment.kind === "field" || pure(segment.value),
|
|
202
|
+
);
|
|
203
|
+
case "unary":
|
|
204
|
+
case "convert":
|
|
205
|
+
case "length":
|
|
206
|
+
return pure(expression.operand);
|
|
207
|
+
case "binary":
|
|
208
|
+
return pure(expression.left) && pure(expression.right);
|
|
209
|
+
default:
|
|
210
|
+
return false;
|
|
211
|
+
}
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
/** Whether two expressions compute the same value: the same tree, wherever each is written. */
|
|
215
|
+
const sameValue = (left: Expression, right: Expression): boolean =>
|
|
216
|
+
pure(left) && valueKey(left) === valueKey(right);
|
|
217
|
+
|
|
218
|
+
const valueKey = (expression: Expression): string =>
|
|
219
|
+
JSON.stringify(expression, (key, value) => {
|
|
220
|
+
if (key === "span") {
|
|
221
|
+
return undefined;
|
|
222
|
+
}
|
|
223
|
+
return typeof value === "bigint" ? String(value) : value;
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
/** The range of `left operator right`; a result that may wrap spans its whole type. */
|
|
227
|
+
const arithmeticInterval = (
|
|
228
|
+
operator: BinaryOperator,
|
|
229
|
+
left: IntegerInterval,
|
|
230
|
+
right: IntegerInterval,
|
|
231
|
+
type: IntegerInterval,
|
|
232
|
+
): IntegerInterval | undefined => {
|
|
233
|
+
const unwrapped = (result: IntegerInterval): IntegerInterval =>
|
|
234
|
+
within(result, type) ? result : type;
|
|
235
|
+
const corners = (
|
|
236
|
+
combine: (a: bigint, b: bigint) => bigint,
|
|
237
|
+
divisor: IntegerInterval = right,
|
|
238
|
+
) => [
|
|
239
|
+
combine(left[0], divisor[0]),
|
|
240
|
+
combine(left[0], divisor[1]),
|
|
241
|
+
combine(left[1], divisor[0]),
|
|
242
|
+
combine(left[1], divisor[1]),
|
|
243
|
+
];
|
|
244
|
+
switch (operator) {
|
|
245
|
+
case "add":
|
|
246
|
+
return unwrapped([left[0] + right[0], left[1] + right[1]]);
|
|
247
|
+
case "subtract":
|
|
248
|
+
return unwrapped([left[0] - right[1], left[1] - right[0]]);
|
|
249
|
+
case "multiply": {
|
|
250
|
+
const products = corners((a, b) => a * b);
|
|
251
|
+
return unwrapped([products.reduce(lowest), products.reduce(highest)]);
|
|
252
|
+
}
|
|
253
|
+
case "divide": {
|
|
254
|
+
if (admits(right, 0n)) {
|
|
255
|
+
return undefined;
|
|
256
|
+
}
|
|
257
|
+
// A divisor whose range runs through an excluded zero divides by each side of it, where
|
|
258
|
+
// the quotients nearest zero's neighbours are the largest.
|
|
259
|
+
const divisors: readonly IntegerInterval[] =
|
|
260
|
+
right[2] === 0n
|
|
261
|
+
? [
|
|
262
|
+
[right[0], -1n],
|
|
263
|
+
[1n, right[1]],
|
|
264
|
+
]
|
|
265
|
+
: [right];
|
|
266
|
+
const quotients = divisors.flatMap((divisor) =>
|
|
267
|
+
corners((a, b) => a / b, divisor),
|
|
268
|
+
);
|
|
269
|
+
return [quotients.reduce(lowest), quotients.reduce(highest)];
|
|
270
|
+
}
|
|
271
|
+
case "remainder": {
|
|
272
|
+
if (admits(right, 0n)) {
|
|
273
|
+
return undefined;
|
|
274
|
+
}
|
|
275
|
+
const magnitude = (value: bigint) => (value < 0n ? -value : value);
|
|
276
|
+
const largest = highest(magnitude(right[0]), magnitude(right[1])) - 1n;
|
|
277
|
+
return [left[0] < 0n ? -largest : 0n, left[1] > 0n ? largest : 0n];
|
|
278
|
+
}
|
|
279
|
+
default:
|
|
280
|
+
return type;
|
|
281
|
+
}
|
|
282
|
+
};
|
|
283
|
+
|
|
284
|
+
/** The most iterations a bounded loop may prove. */
|
|
285
|
+
export const maxLoopIterations = 1_000_000n;
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* A loop's widest counter range and most iterations over every start and limit in their ranges.
|
|
289
|
+
* `terminal` is the counter's value after a loop that ran: exact when both bounds are single
|
|
290
|
+
* values, otherwise the furthest it can reach.
|
|
291
|
+
*/
|
|
292
|
+
export const loopBounds = (
|
|
293
|
+
start: IntegerInterval,
|
|
294
|
+
limit: IntegerInterval,
|
|
295
|
+
step: bigint,
|
|
296
|
+
exclusive = false,
|
|
297
|
+
) => {
|
|
298
|
+
const excluded = exclusive ? 1n : 0n;
|
|
299
|
+
const first = step > 0n ? start[0] : start[1];
|
|
300
|
+
const last = step > 0n ? limit[1] - excluded : limit[0] + excluded;
|
|
301
|
+
const count = loopIterations(
|
|
302
|
+
first,
|
|
303
|
+
limit[step > 0n ? 1 : 0],
|
|
304
|
+
exclusive,
|
|
305
|
+
step,
|
|
306
|
+
);
|
|
307
|
+
const range: IntegerInterval = first <= last ? [first, last] : [last, first];
|
|
308
|
+
const fixed = start[0] === start[1] && limit[0] === limit[1];
|
|
309
|
+
const terminal = fixed
|
|
310
|
+
? first + count * step
|
|
311
|
+
: (step > 0n ? range[1] : range[0]) + step;
|
|
312
|
+
return { first, last, count, range, terminal };
|
|
313
|
+
};
|
|
314
|
+
|
|
315
|
+
const negatedComparisons: Partial<Record<BinaryOperator, BinaryOperator>> = {
|
|
316
|
+
less: "greaterEqual",
|
|
317
|
+
lessEqual: "greater",
|
|
318
|
+
greater: "lessEqual",
|
|
319
|
+
greaterEqual: "less",
|
|
320
|
+
equal: "notEqual",
|
|
321
|
+
notEqual: "equal",
|
|
322
|
+
};
|
|
323
|
+
|
|
324
|
+
/** `current` where `current operator value` holds, keeping the value it already excludes. */
|
|
325
|
+
const bounded = (
|
|
326
|
+
current: IntegerInterval,
|
|
327
|
+
operator: BinaryOperator | undefined,
|
|
328
|
+
value: bigint,
|
|
329
|
+
): IntegerInterval => {
|
|
330
|
+
const [lower, upper, excluded] = current;
|
|
331
|
+
switch (operator) {
|
|
332
|
+
case "less":
|
|
333
|
+
return excluding([lower, lowest(upper, value - 1n)], excluded);
|
|
334
|
+
case "lessEqual":
|
|
335
|
+
return excluding([lower, lowest(upper, value)], excluded);
|
|
336
|
+
case "greater":
|
|
337
|
+
return excluding([highest(lower, value + 1n), upper], excluded);
|
|
338
|
+
case "greaterEqual":
|
|
339
|
+
return excluding([highest(lower, value), upper], excluded);
|
|
340
|
+
case "equal":
|
|
341
|
+
return excluding([highest(lower, value), lowest(upper, value)], excluded);
|
|
342
|
+
case "notEqual":
|
|
343
|
+
return excluding(current, value);
|
|
344
|
+
default:
|
|
345
|
+
return current;
|
|
346
|
+
}
|
|
347
|
+
};
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* `range` without `value`: an end moves past it, and a value strictly inside becomes the range's
|
|
351
|
+
* one exclusion. When it already has one, zero is kept, the value a divisor must not be.
|
|
352
|
+
*/
|
|
353
|
+
const excluding = (
|
|
354
|
+
range: IntegerInterval,
|
|
355
|
+
value: bigint | undefined,
|
|
356
|
+
): IntegerInterval => {
|
|
357
|
+
const [lower, upper, held] = range;
|
|
358
|
+
if (value === undefined || value < lower || value > upper) {
|
|
359
|
+
return range;
|
|
360
|
+
}
|
|
361
|
+
if (value === lower) {
|
|
362
|
+
return excluding([lower + 1n, upper], held);
|
|
363
|
+
}
|
|
364
|
+
if (value === upper) {
|
|
365
|
+
return excluding([lower, upper - 1n], held);
|
|
366
|
+
}
|
|
367
|
+
return [lower, upper, held === undefined || value === 0n ? value : held];
|
|
368
|
+
};
|
|
369
|
+
|
|
370
|
+
// Only storage no statement can change carries branch refinements: loop counters,
|
|
371
|
+
// by-value parameters and const locals, so the proof survives through the branch.
|
|
372
|
+
export const narrowRanges = (
|
|
373
|
+
condition: Expression,
|
|
374
|
+
truth: boolean,
|
|
375
|
+
types: ReadonlyMap<string, PlcType>,
|
|
376
|
+
variables: ReadonlyMap<string, Variable>,
|
|
377
|
+
ranges: ReadonlyMap<string, IntegerInterval>,
|
|
378
|
+
): Map<string, IntegerInterval> => {
|
|
379
|
+
const narrowed = new Map(ranges);
|
|
380
|
+
if (condition.kind !== "binary") {
|
|
381
|
+
return narrowed;
|
|
382
|
+
}
|
|
383
|
+
if (condition.operator === "and") {
|
|
384
|
+
return truth
|
|
385
|
+
? narrowRanges(
|
|
386
|
+
condition.right,
|
|
387
|
+
truth,
|
|
388
|
+
types,
|
|
389
|
+
variables,
|
|
390
|
+
narrowRanges(condition.left, truth, types, variables, ranges),
|
|
391
|
+
)
|
|
392
|
+
: narrowed;
|
|
393
|
+
}
|
|
394
|
+
if (condition.left.kind !== "load" || condition.left.place.path.length) {
|
|
395
|
+
return narrowed;
|
|
396
|
+
}
|
|
397
|
+
const root = condition.left.place.root;
|
|
398
|
+
const variable = variables.get(root);
|
|
399
|
+
const type = types.get(condition.left.type);
|
|
400
|
+
const fixed =
|
|
401
|
+
variable &&
|
|
402
|
+
((variable.storage === "parameter" && !variable.mode) ||
|
|
403
|
+
(variable.storage === "local" && !variable.mutable));
|
|
404
|
+
const current =
|
|
405
|
+
ranges.get(root) ??
|
|
406
|
+
(fixed && type && integerType(type) ? integerRange(type) : undefined);
|
|
407
|
+
const bound = interval(condition.right, types, variables, ranges);
|
|
408
|
+
if (!current || !bound || bound[0] !== bound[1]) {
|
|
409
|
+
return narrowed;
|
|
410
|
+
}
|
|
411
|
+
const range = bounded(
|
|
412
|
+
current,
|
|
413
|
+
truth ? condition.operator : negatedComparisons[condition.operator],
|
|
414
|
+
bound[0],
|
|
415
|
+
);
|
|
416
|
+
if (range[0] <= range[1]) {
|
|
417
|
+
narrowed.set(root, range);
|
|
418
|
+
}
|
|
419
|
+
return narrowed;
|
|
420
|
+
};
|