@tsplc/compiler 0.5.13 → 0.5.15
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/README.md +33 -8
- package/dist/cli.js +794 -621
- package/dist/cli.js.map +19 -16
- package/dist/clock.js +5 -2
- package/dist/clock.js.map +5 -4
- package/dist/codesys.js +4162 -4157
- package/dist/codesys.js.map +43 -29
- package/dist/index.js +7852 -6549
- package/dist/index.js.map +53 -50
- package/dist/ir.js +2482 -2277
- package/dist/ir.js.map +28 -25
- package/dist/retain.js +3 -1
- package/dist/retain.js.map +3 -3
- package/dist/signals.js +115 -0
- package/dist/signals.js.map +10 -0
- package/dist/telemetry.js +25 -20
- package/dist/telemetry.js.map +6 -6
- package/package.json +19 -39
- package/src/codesys/config.ts +3 -3
- package/src/codesys/debug.ts +92 -108
- package/src/codesys/emit.ts +100 -88
- package/src/codesys/endpoints.ts +111 -0
- package/src/codesys/engineering.ts +30 -54
- 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 +85 -1401
- package/src/codesys/expressions.ts +41 -33
- package/src/codesys/global-lists.ts +114 -94
- package/src/codesys/global-names.ts +12 -14
- package/src/codesys/http.ts +428 -388
- package/src/codesys/instrumentation.ts +142 -148
- package/src/codesys/io-paths.ts +31 -23
- package/src/codesys/loops.ts +101 -0
- package/src/codesys/manifest.ts +269 -269
- package/src/codesys/names.ts +10 -33
- package/src/codesys/numeric.ts +89 -42
- package/src/codesys/plan.ts +202 -190
- package/src/codesys/programs.ts +23 -16
- package/src/codesys/publication.ts +13 -86
- package/src/codesys/required.ts +8 -0
- package/src/codesys/routines.ts +103 -489
- package/src/codesys/scheduling.ts +96 -0
- package/src/codesys/service-channels.ts +23 -0
- package/src/codesys/sources.ts +34 -28
- package/src/codesys/st-files.ts +112 -105
- package/src/codesys/statements.ts +409 -0
- package/src/codesys/storage.ts +41 -23
- package/src/codesys/symbols.ts +130 -119
- package/src/codesys/task-access.ts +65 -221
- package/src/codesys/telemetry-settings.ts +19 -0
- package/src/codesys/telemetry.ts +185 -148
- package/src/codesys/types.ts +69 -109
- package/src/codesys/vendor.ts +13 -50
- package/src/frontend/analyzer.ts +66 -111
- package/src/frontend/async.ts +316 -128
- package/src/frontend/blocks.ts +178 -154
- package/src/frontend/configuration.ts +49 -153
- package/src/frontend/const-functions.ts +50 -40
- package/src/frontend/control-flow.ts +164 -129
- package/src/frontend/cooperative.ts +17 -9
- package/src/frontend/coroutines.ts +539 -441
- package/src/frontend/dialect-names.ts +104 -0
- package/src/frontend/elaborate/array-searches.ts +77 -69
- package/src/frontend/elaborate/assignments.ts +68 -77
- package/src/frontend/elaborate/calls.ts +878 -512
- package/src/frontend/elaborate/capture-protocol.ts +118 -21
- package/src/frontend/elaborate/capture.ts +196 -109
- package/src/frontend/elaborate/declarations.ts +124 -0
- package/src/frontend/elaborate/dialect.ts +119 -46
- package/src/frontend/elaborate/expressions.ts +763 -442
- package/src/frontend/elaborate/filtered-arrays.ts +127 -91
- package/src/frontend/elaborate/frame.ts +50 -2
- package/src/frontend/elaborate/generics.ts +20 -34
- package/src/frontend/elaborate/graph.ts +13 -87
- package/src/frontend/elaborate/imports.ts +0 -5
- package/src/frontend/elaborate/index.ts +416 -169
- package/src/frontend/elaborate/initializers.ts +64 -42
- package/src/frontend/elaborate/iterators.ts +228 -364
- package/src/frontend/elaborate/layouts.ts +477 -116
- package/src/frontend/elaborate/model.ts +113 -55
- package/src/frontend/elaborate/references.ts +116 -138
- package/src/frontend/elaborate/retention.ts +137 -59
- package/src/frontend/elaborate/specialize.ts +617 -308
- package/src/frontend/elaborate/statements.ts +436 -247
- package/src/frontend/elaborate/storage.ts +280 -213
- package/src/frontend/elaborate/time-constructors.ts +34 -0
- package/src/frontend/elaborate/values.ts +77 -7
- package/src/frontend/elaborate/writes.ts +98 -84
- package/src/frontend/expressions.ts +149 -151
- package/src/frontend/generated-names.ts +3 -3
- package/src/frontend/intrinsics.ts +31 -17
- package/src/frontend/lowering-key.ts +23 -0
- package/src/frontend/module-graph.ts +105 -43
- package/src/frontend/positions.ts +43 -35
- package/src/frontend/promise-chains.ts +226 -266
- package/src/frontend/promises.ts +88 -67
- package/src/frontend/routines.ts +122 -65
- package/src/frontend/sdk.ts +104 -5
- package/src/frontend/services.ts +149 -102
- package/src/frontend/statements.ts +60 -64
- package/src/frontend/tasks.ts +151 -96
- package/src/frontend/telemetry.ts +1 -1
- package/src/frontend/types.ts +366 -181
- package/src/frontend/typescript.ts +239 -112
- package/src/index.ts +2 -1
- package/src/ir/aggregates.ts +90 -75
- package/src/ir/allocation.ts +29 -18
- package/src/ir/conditions.ts +38 -78
- package/src/ir/core-model.ts +128 -11
- package/src/ir/declarations.ts +18 -32
- package/src/ir/diagnostics.ts +25 -18
- package/src/ir/domains.ts +26 -23
- package/src/ir/fold.ts +55 -50
- package/src/ir/identity.ts +10 -0
- package/src/ir/index.ts +31 -7
- package/src/ir/job-errors.ts +30 -8
- package/src/ir/layouts.ts +63 -34
- package/src/ir/link.ts +11 -46
- package/src/ir/model.ts +25 -6
- package/src/ir/normalize.ts +3 -28
- package/src/ir/numeric.ts +45 -18
- package/src/ir/operands.ts +102 -0
- package/src/ir/ranges.ts +242 -67
- package/src/ir/routine.ts +126 -183
- package/src/ir/scalars.ts +29 -25
- package/src/ir/share.ts +323 -343
- package/src/ir/short-circuit.ts +140 -0
- package/src/ir/tables.ts +111 -0
- package/src/ir/temporaries.ts +15 -113
- package/src/ir/types.ts +32 -8
- package/src/project/artifacts.ts +143 -71
- package/src/project/cli.ts +104 -87
- package/src/project/debugger.ts +48 -34
- package/src/project/deployments.ts +12 -68
- package/src/project/flags.ts +14 -0
- package/src/project/plant.ts +293 -225
- package/src/project/project-commands.ts +254 -192
- package/src/project/project.ts +184 -141
- package/src/project/release.ts +22 -28
- package/src/project/server.ts +22 -26
- package/src/project/terminal.ts +13 -2
- package/src/runtime/async.ts +17 -13
- package/src/runtime/launch.ts +5 -2
- package/src/runtime/main.ts +8 -3
- package/src/runtime/missing-file.ts +3 -0
- package/src/runtime/process.ts +24 -19
- package/src/runtime/program.ts +20 -53
- package/src/runtime/retain.ts +18 -10
- package/src/runtime/runtime.ts +47 -60
- package/src/runtime/settings.ts +27 -0
- package/src/runtime/storage.ts +49 -74
- package/src/runtime/symbols.ts +19 -75
- package/src/runtime/telemetry.ts +37 -14
- package/src/runtime/transport.ts +26 -30
- package/stdlib/clock.ts +4 -2
- package/stdlib/plc-time.ts +2 -0
- package/stdlib/retain.ts +2 -0
- package/stdlib/signals.ts +142 -0
- package/stdlib/telemetry.ts +5 -12
- package/types/dialect.d.ts +49 -0
- package/types/globals.d.ts +2 -42
- package/types/index.d.ts +3 -1
- package/types/retain.d.ts +8 -2
- package/types/telemetry.d.ts +3 -0
- package/dist/bun-runtime.js +0 -1472
- package/dist/bun-runtime.js.map +0 -26
- package/dist/elaborate-capture.js +0 -304
- package/dist/elaborate-capture.js.map +0 -11
- package/dist/math.js +0 -24
- package/dist/math.js.map +0 -10
- package/dist/project.js +0 -626
- package/dist/project.js.map +0 -12
- package/dist/server-check.js +0 -59
- package/dist/server-check.js.map +0 -11
- package/src/frontend/exposure.ts +0 -181
- package/src/project/index.ts +0 -6
- package/src/runtime/.runtime-65O4JH/server/router.ts +0 -17
- package/src/runtime/.runtime-65O4JH/src/cell.ts +0 -45
- package/src/runtime/.runtime-65O4JH/src/controller.ts +0 -58
- package/src/runtime/.runtime-65O4JH/src/lane.ts +0 -6
- package/src/runtime/.runtime-65O4JH/src/main.ts +0 -11
- package/src/runtime/.runtime-65O4JH/src/services.ts +0 -8
- package/src/runtime/.runtime-65O4JH/tsconfig.json +0 -13
- package/src/runtime/.runtime-65O4JH/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-Brw2kO/server/router.ts +0 -17
- package/src/runtime/.runtime-Brw2kO/src/cell.ts +0 -45
- package/src/runtime/.runtime-Brw2kO/src/controller.ts +0 -58
- package/src/runtime/.runtime-Brw2kO/src/lane.ts +0 -6
- package/src/runtime/.runtime-Brw2kO/src/main.ts +0 -11
- package/src/runtime/.runtime-Brw2kO/src/services.ts +0 -8
- package/src/runtime/.runtime-Brw2kO/tsconfig.json +0 -13
- package/src/runtime/.runtime-Brw2kO/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-NHfu1D/server/router.ts +0 -17
- package/src/runtime/.runtime-NHfu1D/src/cell.ts +0 -45
- package/src/runtime/.runtime-NHfu1D/src/controller.ts +0 -58
- package/src/runtime/.runtime-NHfu1D/src/lane.ts +0 -6
- package/src/runtime/.runtime-NHfu1D/src/main.ts +0 -11
- package/src/runtime/.runtime-NHfu1D/src/services.ts +0 -8
- package/src/runtime/.runtime-NHfu1D/tsconfig.json +0 -13
- package/src/runtime/.runtime-NHfu1D/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-VaUSOu/server/router.ts +0 -17
- package/src/runtime/.runtime-VaUSOu/src/cell.ts +0 -45
- package/src/runtime/.runtime-VaUSOu/src/controller.ts +0 -58
- package/src/runtime/.runtime-VaUSOu/src/lane.ts +0 -6
- package/src/runtime/.runtime-VaUSOu/src/main.ts +0 -11
- package/src/runtime/.runtime-VaUSOu/src/services.ts +0 -8
- package/src/runtime/.runtime-VaUSOu/tsconfig.json +0 -13
- package/src/runtime/.runtime-VaUSOu/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-WbtNvR/server/router.ts +0 -17
- package/src/runtime/.runtime-WbtNvR/src/cell.ts +0 -45
- package/src/runtime/.runtime-WbtNvR/src/controller.ts +0 -58
- package/src/runtime/.runtime-WbtNvR/src/lane.ts +0 -6
- package/src/runtime/.runtime-WbtNvR/src/main.ts +0 -11
- package/src/runtime/.runtime-WbtNvR/src/services.ts +0 -8
- package/src/runtime/.runtime-WbtNvR/tsconfig.json +0 -13
- package/src/runtime/.runtime-WbtNvR/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-opcua-GMPUt8/server/router.ts +0 -17
- package/src/runtime/.runtime-opcua-GMPUt8/src/cell.ts +0 -45
- package/src/runtime/.runtime-opcua-GMPUt8/src/controller.ts +0 -58
- package/src/runtime/.runtime-opcua-GMPUt8/src/lane.ts +0 -6
- package/src/runtime/.runtime-opcua-GMPUt8/src/main.ts +0 -11
- package/src/runtime/.runtime-opcua-GMPUt8/src/services.ts +0 -8
- package/src/runtime/.runtime-opcua-GMPUt8/tsconfig.json +0 -13
- package/src/runtime/.runtime-opcua-GMPUt8/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-opcua-UwafxO/server/router.ts +0 -17
- package/src/runtime/.runtime-opcua-UwafxO/src/cell.ts +0 -45
- package/src/runtime/.runtime-opcua-UwafxO/src/controller.ts +0 -58
- package/src/runtime/.runtime-opcua-UwafxO/src/lane.ts +0 -6
- package/src/runtime/.runtime-opcua-UwafxO/src/main.ts +0 -11
- package/src/runtime/.runtime-opcua-UwafxO/src/services.ts +0 -8
- package/src/runtime/.runtime-opcua-UwafxO/tsconfig.json +0 -13
- package/src/runtime/.runtime-opcua-UwafxO/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-opcua-ecaSee/server/router.ts +0 -17
- package/src/runtime/.runtime-opcua-ecaSee/src/cell.ts +0 -45
- package/src/runtime/.runtime-opcua-ecaSee/src/controller.ts +0 -58
- package/src/runtime/.runtime-opcua-ecaSee/src/lane.ts +0 -6
- package/src/runtime/.runtime-opcua-ecaSee/src/main.ts +0 -11
- package/src/runtime/.runtime-opcua-ecaSee/src/services.ts +0 -8
- package/src/runtime/.runtime-opcua-ecaSee/tsconfig.json +0 -13
- package/src/runtime/.runtime-opcua-ecaSee/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-opcua-y91UCm/server/router.ts +0 -17
- package/src/runtime/.runtime-opcua-y91UCm/src/cell.ts +0 -45
- package/src/runtime/.runtime-opcua-y91UCm/src/controller.ts +0 -58
- package/src/runtime/.runtime-opcua-y91UCm/src/lane.ts +0 -6
- package/src/runtime/.runtime-opcua-y91UCm/src/main.ts +0 -11
- package/src/runtime/.runtime-opcua-y91UCm/src/services.ts +0 -8
- package/src/runtime/.runtime-opcua-y91UCm/tsconfig.json +0 -13
- package/src/runtime/.runtime-opcua-y91UCm/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-ybIxYh/server/router.ts +0 -17
- package/src/runtime/.runtime-ybIxYh/src/cell.ts +0 -45
- package/src/runtime/.runtime-ybIxYh/src/controller.ts +0 -58
- package/src/runtime/.runtime-ybIxYh/src/lane.ts +0 -6
- package/src/runtime/.runtime-ybIxYh/src/main.ts +0 -11
- package/src/runtime/.runtime-ybIxYh/src/services.ts +0 -8
- package/src/runtime/.runtime-ybIxYh/tsconfig.json +0 -13
- package/src/runtime/.runtime-ybIxYh/tsplc.config.ts +0 -31
- package/stdlib/math.ts +0 -23
- package/types/plc.d.ts +0 -4
package/src/ir/ranges.ts
CHANGED
|
@@ -1,22 +1,64 @@
|
|
|
1
1
|
import { type BinaryOperator, loopIterations } from "@tsplc/iec/arithmetic";
|
|
2
2
|
import { fold } from "./fold";
|
|
3
|
-
import type { Expression,
|
|
3
|
+
import type { Expression, Variable } from "./model";
|
|
4
4
|
import {
|
|
5
5
|
admits,
|
|
6
6
|
highest,
|
|
7
7
|
type IntegerInterval,
|
|
8
|
-
integerType,
|
|
9
8
|
lowest,
|
|
10
9
|
within,
|
|
11
10
|
} from "./numeric";
|
|
12
|
-
import { integerRange, type PlcType } from "./types";
|
|
11
|
+
import { integerRange, integerType, type PlcType } from "./types";
|
|
13
12
|
|
|
14
13
|
export const interval = (
|
|
15
14
|
expression: Expression,
|
|
16
15
|
types: ReadonlyMap<string, PlcType>,
|
|
17
16
|
variables: ReadonlyMap<string, Variable>,
|
|
18
|
-
ranges: ReadonlyMap<string, IntegerInterval
|
|
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,
|
|
19
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);
|
|
20
62
|
if (expression.kind === "length") {
|
|
21
63
|
const array = types.get(expression.operand.type);
|
|
22
64
|
if (array?.kind === "array") {
|
|
@@ -36,25 +78,151 @@ export const interval = (
|
|
|
36
78
|
}
|
|
37
79
|
if (expression.kind === "convert") {
|
|
38
80
|
return expression.policy === "exact"
|
|
39
|
-
? interval(expression.operand
|
|
81
|
+
? interval(expression.operand)
|
|
40
82
|
: integerRange(type);
|
|
41
83
|
}
|
|
42
84
|
if (expression.kind === "unary" && expression.operator === "negate") {
|
|
43
|
-
const operand = interval(expression.operand
|
|
85
|
+
const operand = interval(expression.operand);
|
|
44
86
|
return operand && [-operand[1], -operand[0]];
|
|
45
87
|
}
|
|
46
88
|
if (expression.kind === "binary" && !expression.policy) {
|
|
47
|
-
const left = interval(expression.left
|
|
48
|
-
const right = interval(expression.right
|
|
89
|
+
const left = interval(expression.left);
|
|
90
|
+
const right = interval(expression.right);
|
|
49
91
|
return (
|
|
50
92
|
left &&
|
|
51
93
|
right &&
|
|
52
94
|
arithmeticInterval(expression.operator, left, right, integerRange(type))
|
|
53
95
|
);
|
|
54
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
|
+
}
|
|
55
108
|
return integerRange(type);
|
|
56
109
|
};
|
|
57
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
|
+
|
|
58
226
|
/** The range of `left operator right`; a result that may wrap spans its whole type. */
|
|
59
227
|
const arithmeticInterval = (
|
|
60
228
|
operator: BinaryOperator,
|
|
@@ -64,11 +232,14 @@ const arithmeticInterval = (
|
|
|
64
232
|
): IntegerInterval | undefined => {
|
|
65
233
|
const unwrapped = (result: IntegerInterval): IntegerInterval =>
|
|
66
234
|
within(result, type) ? result : type;
|
|
67
|
-
const corners = (
|
|
68
|
-
combine(
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
combine(left[
|
|
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]),
|
|
72
243
|
];
|
|
73
244
|
switch (operator) {
|
|
74
245
|
case "add":
|
|
@@ -83,7 +254,18 @@ const arithmeticInterval = (
|
|
|
83
254
|
if (admits(right, 0n)) {
|
|
84
255
|
return undefined;
|
|
85
256
|
}
|
|
86
|
-
|
|
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
|
+
);
|
|
87
269
|
return [quotients.reduce(lowest), quotients.reduce(highest)];
|
|
88
270
|
}
|
|
89
271
|
case "remainder": {
|
|
@@ -102,6 +284,11 @@ const arithmeticInterval = (
|
|
|
102
284
|
/** The most iterations a bounded loop may prove. */
|
|
103
285
|
export const maxLoopIterations = 1_000_000n;
|
|
104
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
|
+
*/
|
|
105
292
|
export const loopBounds = (
|
|
106
293
|
start: IntegerInterval,
|
|
107
294
|
limit: IntegerInterval,
|
|
@@ -118,7 +305,11 @@ export const loopBounds = (
|
|
|
118
305
|
step,
|
|
119
306
|
);
|
|
120
307
|
const range: IntegerInterval = first <= last ? [first, last] : [last, first];
|
|
121
|
-
|
|
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 };
|
|
122
313
|
};
|
|
123
314
|
|
|
124
315
|
const negatedComparisons: Partial<Record<BinaryOperator, BinaryOperator>> = {
|
|
@@ -126,28 +317,56 @@ const negatedComparisons: Partial<Record<BinaryOperator, BinaryOperator>> = {
|
|
|
126
317
|
lessEqual: "greater",
|
|
127
318
|
greater: "lessEqual",
|
|
128
319
|
greaterEqual: "less",
|
|
320
|
+
equal: "notEqual",
|
|
321
|
+
notEqual: "equal",
|
|
129
322
|
};
|
|
130
323
|
|
|
324
|
+
/** `current` where `current operator value` holds, keeping the value it already excludes. */
|
|
131
325
|
const bounded = (
|
|
132
326
|
current: IntegerInterval,
|
|
133
327
|
operator: BinaryOperator | undefined,
|
|
134
328
|
value: bigint,
|
|
135
329
|
): IntegerInterval => {
|
|
136
|
-
const [lower, upper] = current;
|
|
330
|
+
const [lower, upper, excluded] = current;
|
|
137
331
|
switch (operator) {
|
|
138
332
|
case "less":
|
|
139
|
-
return [lower, lowest(upper, value - 1n)];
|
|
333
|
+
return excluding([lower, lowest(upper, value - 1n)], excluded);
|
|
140
334
|
case "lessEqual":
|
|
141
|
-
return [lower, lowest(upper, value)];
|
|
335
|
+
return excluding([lower, lowest(upper, value)], excluded);
|
|
142
336
|
case "greater":
|
|
143
|
-
return [highest(lower, value + 1n), upper];
|
|
337
|
+
return excluding([highest(lower, value + 1n), upper], excluded);
|
|
144
338
|
case "greaterEqual":
|
|
145
|
-
return [highest(lower, value), upper];
|
|
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);
|
|
146
344
|
default:
|
|
147
345
|
return current;
|
|
148
346
|
}
|
|
149
347
|
};
|
|
150
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
|
+
|
|
151
370
|
// Only storage no statement can change carries branch refinements: loop counters,
|
|
152
371
|
// by-value parameters and const locals, so the proof survives through the branch.
|
|
153
372
|
export const narrowRanges = (
|
|
@@ -189,57 +408,13 @@ export const narrowRanges = (
|
|
|
189
408
|
if (!current || !bound || bound[0] !== bound[1]) {
|
|
190
409
|
return narrowed;
|
|
191
410
|
}
|
|
192
|
-
const
|
|
411
|
+
const range = bounded(
|
|
193
412
|
current,
|
|
194
413
|
truth ? condition.operator : negatedComparisons[condition.operator],
|
|
195
414
|
bound[0],
|
|
196
415
|
);
|
|
197
|
-
if (
|
|
198
|
-
narrowed.set(root,
|
|
416
|
+
if (range[0] <= range[1]) {
|
|
417
|
+
narrowed.set(root, range);
|
|
199
418
|
}
|
|
200
419
|
return narrowed;
|
|
201
420
|
};
|
|
202
|
-
|
|
203
|
-
/** A value's proved integer range; a math helper's result follows from its arguments. */
|
|
204
|
-
export const valueRange = (
|
|
205
|
-
expression: Expression,
|
|
206
|
-
types: ReadonlyMap<string, PlcType>,
|
|
207
|
-
variables: ReadonlyMap<string, Variable>,
|
|
208
|
-
ranges: ReadonlyMap<string, IntegerInterval>,
|
|
209
|
-
functions: ReadonlyMap<string, { bounds?: MathHelper }>,
|
|
210
|
-
): IntegerInterval | undefined => {
|
|
211
|
-
if (expression.kind !== "call") {
|
|
212
|
-
return interval(expression, types, variables, ranges);
|
|
213
|
-
}
|
|
214
|
-
const helper = functions.get(expression.function)?.bounds;
|
|
215
|
-
return (
|
|
216
|
-
helper &&
|
|
217
|
-
mathHelperRange(
|
|
218
|
-
helper,
|
|
219
|
-
expression.arguments.map((argument) =>
|
|
220
|
-
valueRange(argument, types, variables, ranges, functions),
|
|
221
|
-
),
|
|
222
|
-
)
|
|
223
|
-
);
|
|
224
|
-
};
|
|
225
|
-
|
|
226
|
-
const mathHelperRange = (
|
|
227
|
-
helper: MathHelper,
|
|
228
|
-
[first, second, third]: readonly (IntegerInterval | undefined)[],
|
|
229
|
-
): IntegerInterval | undefined => {
|
|
230
|
-
if (!first || !second) {
|
|
231
|
-
return undefined;
|
|
232
|
-
}
|
|
233
|
-
if (helper === "min") {
|
|
234
|
-
return [lowest(first[0], second[0]), lowest(first[1], second[1])];
|
|
235
|
-
}
|
|
236
|
-
if (helper === "max") {
|
|
237
|
-
return [highest(first[0], second[0]), highest(first[1], second[1])];
|
|
238
|
-
}
|
|
239
|
-
// clamp(value, lower, upper) returns one of its three arguments, never outside lower..upper,
|
|
240
|
-
// and only lower or upper when the bounds are reversed.
|
|
241
|
-
if (!third) {
|
|
242
|
-
return undefined;
|
|
243
|
-
}
|
|
244
|
-
return [lowest(second[0], third[0]), highest(second[1], third[1])];
|
|
245
|
-
};
|