@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/routine.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
// biome-ignore-all lint/style/noNonNullAssertion: Retain existing non-null assertions during the tooling migration; changing their contracts requires a separate review.
|
|
2
1
|
import { aggregate, expressionIssue, placeType, valueFits } from "./aggregates";
|
|
3
|
-
import
|
|
4
|
-
CoreExpression,
|
|
5
|
-
CoreFunction,
|
|
6
|
-
CoreFunctionBlock,
|
|
7
|
-
CorePath,
|
|
8
|
-
CoreStatement,
|
|
2
|
+
import {
|
|
3
|
+
type CoreExpression,
|
|
4
|
+
type CoreFunction,
|
|
5
|
+
type CoreFunctionBlock,
|
|
6
|
+
type CorePath,
|
|
7
|
+
type CoreStatement,
|
|
8
|
+
isBlockRoutine,
|
|
9
|
+
temporaryId,
|
|
9
10
|
} from "./core-model";
|
|
10
11
|
import type { ModuleDeclarations } from "./declarations";
|
|
11
12
|
import type { SourceSpan } from "./diagnostics";
|
|
@@ -25,9 +26,10 @@ import {
|
|
|
25
26
|
loopBounds,
|
|
26
27
|
maxLoopIterations,
|
|
27
28
|
narrowRanges,
|
|
28
|
-
valueRange,
|
|
29
29
|
} from "./ranges";
|
|
30
|
-
import {
|
|
30
|
+
import { type BranchOperand, lowerLogical, lowerSelect } from "./short-circuit";
|
|
31
|
+
import { lookup } from "./tables";
|
|
32
|
+
import { instancedBlock, integerRange, isValueType } from "./types";
|
|
31
33
|
|
|
32
34
|
type Flow = "fallthrough" | "return" | "loop" | "mixed";
|
|
33
35
|
interface FlowState {
|
|
@@ -65,32 +67,14 @@ const join = (
|
|
|
65
67
|
};
|
|
66
68
|
};
|
|
67
69
|
|
|
68
|
-
// All mutable lowering state belongs to one invocation of one authored routine.
|
|
69
|
-
// sourceScope admits authored places; scope additionally contains captured temps.
|
|
70
|
-
const temporaryPrefix = (routine: string) => `${routine}:temporary:`;
|
|
71
|
-
const temporaryId = (routine: string, index: number) =>
|
|
72
|
-
`${temporaryPrefix(routine)}${index}`;
|
|
73
|
-
|
|
74
|
-
/** Whether a local is a temporary the normalizer made for `routine`, rather than one its source declares. */
|
|
75
|
-
export function isTemporary(routine: string, variable: Variable): boolean {
|
|
76
|
-
const prefix = temporaryPrefix(routine);
|
|
77
|
-
return (
|
|
78
|
-
variable.storage === "local" &&
|
|
79
|
-
variable.id.startsWith(prefix) &&
|
|
80
|
-
/^\d+$/.test(variable.id.slice(prefix.length))
|
|
81
|
-
);
|
|
82
|
-
}
|
|
83
|
-
|
|
84
70
|
export class RoutineNormalizer {
|
|
85
|
-
private readonly
|
|
71
|
+
private readonly parameters: readonly Variable[];
|
|
86
72
|
|
|
87
|
-
private readonly block: FunctionBlock | undefined;
|
|
88
73
|
private readonly owner: FunctionBlock | undefined;
|
|
89
74
|
|
|
75
|
+
/** The storage the routine's source may name; temporaries are not in it, so a place the source names can't reach one. */
|
|
90
76
|
private readonly scope: Map<string, Variable>;
|
|
91
77
|
|
|
92
|
-
private readonly sourceScope: Map<string, Variable>;
|
|
93
|
-
|
|
94
78
|
private readonly locals: Variable[];
|
|
95
79
|
|
|
96
80
|
private readonly ranges = new Map<string, IntegerInterval>();
|
|
@@ -102,37 +86,26 @@ export class RoutineNormalizer {
|
|
|
102
86
|
private temporaryCount = 0;
|
|
103
87
|
|
|
104
88
|
constructor(
|
|
105
|
-
routine: PlcFunction | FunctionBlock,
|
|
89
|
+
private readonly routine: PlcFunction | FunctionBlock,
|
|
106
90
|
private readonly declarations: ModuleDeclarations,
|
|
107
91
|
) {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
this.
|
|
111
|
-
|
|
92
|
+
if (isBlockRoutine(routine)) {
|
|
93
|
+
this.owner = routine;
|
|
94
|
+
this.parameters = routine.inputs;
|
|
95
|
+
} else {
|
|
96
|
+
this.owner = routine.owner
|
|
112
97
|
? declarations.blocks.get(routine.owner.block)
|
|
113
|
-
: undefined
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
this.fn = {
|
|
117
|
-
id: routine.id,
|
|
118
|
-
name: routine.name,
|
|
119
|
-
...("owner" in routine && routine.owner && { owner: routine.owner }),
|
|
120
|
-
...("origin" in routine && routine.origin && { origin: routine.origin }),
|
|
121
|
-
parameters,
|
|
122
|
-
locals: routine.locals,
|
|
123
|
-
body: routine.body,
|
|
124
|
-
span: routine.span,
|
|
125
|
-
...(routine.result && { result: routine.result }),
|
|
126
|
-
};
|
|
98
|
+
: undefined;
|
|
99
|
+
this.parameters = routine.parameters;
|
|
100
|
+
}
|
|
127
101
|
this.scope = new Map(declarations.globals);
|
|
128
102
|
for (const item of [
|
|
129
|
-
...parameters,
|
|
103
|
+
...this.parameters,
|
|
130
104
|
...(this.owner ? [...this.owner.state, ...this.owner.outputs] : []),
|
|
131
105
|
...routine.locals,
|
|
132
106
|
]) {
|
|
133
107
|
this.scope.set(item.id, item);
|
|
134
108
|
}
|
|
135
|
-
this.sourceScope = new Map(this.scope);
|
|
136
109
|
this.locals = [...routine.locals];
|
|
137
110
|
}
|
|
138
111
|
|
|
@@ -140,27 +113,30 @@ export class RoutineNormalizer {
|
|
|
140
113
|
routine: CoreFunction | CoreFunctionBlock;
|
|
141
114
|
callees: ReadonlySet<string>;
|
|
142
115
|
} {
|
|
143
|
-
|
|
144
|
-
|
|
116
|
+
const { routine } = this;
|
|
117
|
+
// A standard block has no body: the vendor library implements it.
|
|
118
|
+
if (isBlockRoutine(routine) && routine.standard) {
|
|
119
|
+
return { routine: { ...routine, body: [] }, callees: this.callees };
|
|
145
120
|
}
|
|
121
|
+
return {
|
|
122
|
+
routine: { ...routine, locals: this.locals, body: this.routineBody() },
|
|
123
|
+
callees: this.callees,
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
private routineBody(): CoreStatement[] {
|
|
146
128
|
const normalized = this.body(
|
|
147
|
-
this.
|
|
148
|
-
new Set(this.
|
|
129
|
+
this.routine.body,
|
|
130
|
+
new Set(this.parameters.map((item) => item.id)),
|
|
149
131
|
);
|
|
150
|
-
if (this.
|
|
151
|
-
reject("A typed helper must return on every path.", this.
|
|
132
|
+
if (this.routine.result && normalized.flow !== "return") {
|
|
133
|
+
reject("A typed helper must return on every path.", this.routine.span);
|
|
152
134
|
}
|
|
153
|
-
|
|
154
|
-
? { ...this.block, locals: this.locals, body: normalized.body }
|
|
155
|
-
: { ...this.fn, locals: this.locals, body: normalized.body };
|
|
156
|
-
return { routine, callees: this.callees };
|
|
135
|
+
return normalized.body;
|
|
157
136
|
}
|
|
158
137
|
|
|
159
138
|
private temporary(type: string, span: SourceSpan): Variable {
|
|
160
|
-
|
|
161
|
-
do {
|
|
162
|
-
id = temporaryId(this.fn.id, this.temporaryCount++);
|
|
163
|
-
} while (this.declarations.hasId(id));
|
|
139
|
+
const id = this.unclaimedTemporaryId();
|
|
164
140
|
this.declarations.claimId(id);
|
|
165
141
|
const item: Variable = {
|
|
166
142
|
id,
|
|
@@ -168,21 +144,25 @@ export class RoutineNormalizer {
|
|
|
168
144
|
type,
|
|
169
145
|
storage: "local",
|
|
170
146
|
mutable: true,
|
|
171
|
-
|
|
147
|
+
retention: "none",
|
|
172
148
|
span,
|
|
173
149
|
};
|
|
174
|
-
this.scope.set(id, item);
|
|
175
150
|
this.locals.push(item);
|
|
176
151
|
return item;
|
|
177
152
|
}
|
|
178
153
|
|
|
154
|
+
private unclaimedTemporaryId(): string {
|
|
155
|
+
const id = temporaryId(this.routine.id, this.temporaryCount++);
|
|
156
|
+
return this.declarations.hasId(id) ? this.unclaimedTemporaryId() : id;
|
|
157
|
+
}
|
|
158
|
+
|
|
179
159
|
private load(variable: Variable, span: SourceSpan): CoreExpression {
|
|
180
160
|
return { kind: "load", root: variable.id, type: variable.type, span };
|
|
181
161
|
}
|
|
182
162
|
|
|
183
163
|
/** The block a method or update receiver instantiates: a static instance, or an InOut reference to one. */
|
|
184
164
|
private receiverBlock(id: string): string | undefined {
|
|
185
|
-
const variable = this.
|
|
165
|
+
const variable = this.scope.get(id);
|
|
186
166
|
if (variable?.storage !== "instance" && variable?.mode !== "inOut") {
|
|
187
167
|
return undefined;
|
|
188
168
|
}
|
|
@@ -238,10 +218,10 @@ export class RoutineNormalizer {
|
|
|
238
218
|
value: Expression,
|
|
239
219
|
initialized: Set<string>,
|
|
240
220
|
output: CoreStatement[],
|
|
241
|
-
allowAggregateLoad =
|
|
221
|
+
{ allowAggregateLoad }: { allowAggregateLoad?: true } = {},
|
|
242
222
|
): CoreExpression {
|
|
243
223
|
const type = this.declarations.types.get(value.type);
|
|
244
|
-
if (!type || !
|
|
224
|
+
if (!type || !isValueType(value.type, this.declarations.types)) {
|
|
245
225
|
reject("Unknown or non-value expression type.", value.span);
|
|
246
226
|
}
|
|
247
227
|
if (value.kind === "borrow") {
|
|
@@ -257,11 +237,11 @@ export class RoutineNormalizer {
|
|
|
257
237
|
reject("Length requires a fixed array and DINT result.", value.span);
|
|
258
238
|
}
|
|
259
239
|
if (value.operand.kind === "load") {
|
|
260
|
-
const source = this.
|
|
240
|
+
const source = this.scope.get(value.operand.place.root);
|
|
261
241
|
const checked = placeType(
|
|
262
242
|
value.operand.place,
|
|
263
243
|
this.declarations.types,
|
|
264
|
-
this.
|
|
244
|
+
this.scope,
|
|
265
245
|
this.ranges,
|
|
266
246
|
);
|
|
267
247
|
if (
|
|
@@ -274,7 +254,9 @@ export class RoutineNormalizer {
|
|
|
274
254
|
}
|
|
275
255
|
this.normalizePath(value.operand.place, initialized, output);
|
|
276
256
|
} else {
|
|
277
|
-
this.expression(value.operand, initialized, output,
|
|
257
|
+
this.expression(value.operand, initialized, output, {
|
|
258
|
+
allowAggregateLoad: true,
|
|
259
|
+
});
|
|
278
260
|
}
|
|
279
261
|
return {
|
|
280
262
|
...common,
|
|
@@ -292,29 +274,17 @@ export class RoutineNormalizer {
|
|
|
292
274
|
}
|
|
293
275
|
const condition = this.expression(value.condition, initialized, output);
|
|
294
276
|
const result = this.temporary(value.type, value.span);
|
|
295
|
-
const
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
kind: "assign",
|
|
304
|
-
root: result.id,
|
|
305
|
-
value: evaluated,
|
|
277
|
+
const whenTrue = this.branchOperand(value.then, initialized);
|
|
278
|
+
const whenFalse = this.branchOperand(value.else, initialized);
|
|
279
|
+
output.push(
|
|
280
|
+
lowerSelect({
|
|
281
|
+
result: { root: result.id, type: result.type },
|
|
282
|
+
condition,
|
|
283
|
+
whenTrue,
|
|
284
|
+
whenFalse,
|
|
306
285
|
span: value.span,
|
|
307
|
-
})
|
|
308
|
-
|
|
309
|
-
};
|
|
310
|
-
output.push({
|
|
311
|
-
kind: "if",
|
|
312
|
-
condition,
|
|
313
|
-
// biome-ignore lint/suspicious/noThenProperty: PLC branch data is an array, not a thenable callback.
|
|
314
|
-
then: branch(value.then),
|
|
315
|
-
else: branch(value.else),
|
|
316
|
-
span: value.span,
|
|
317
|
-
});
|
|
286
|
+
}),
|
|
287
|
+
);
|
|
318
288
|
initialized.add(result.id);
|
|
319
289
|
return this.load(result, value.span);
|
|
320
290
|
}
|
|
@@ -367,7 +337,9 @@ export class RoutineNormalizer {
|
|
|
367
337
|
}
|
|
368
338
|
switch (value.kind) {
|
|
369
339
|
case "copy":
|
|
370
|
-
return this.expression(value.operand, initialized, output,
|
|
340
|
+
return this.expression(value.operand, initialized, output, {
|
|
341
|
+
allowAggregateLoad: true,
|
|
342
|
+
});
|
|
371
343
|
case "record":
|
|
372
344
|
case "array": {
|
|
373
345
|
const result = this.temporary(value.type, value.span);
|
|
@@ -426,11 +398,11 @@ export class RoutineNormalizer {
|
|
|
426
398
|
return this.load(result, value.span);
|
|
427
399
|
}
|
|
428
400
|
case "load": {
|
|
429
|
-
const source = this.
|
|
401
|
+
const source = this.scope.get(value.place.root);
|
|
430
402
|
const checked = placeType(
|
|
431
403
|
value.place,
|
|
432
404
|
this.declarations.types,
|
|
433
|
-
this.
|
|
405
|
+
this.scope,
|
|
434
406
|
this.ranges,
|
|
435
407
|
);
|
|
436
408
|
if (!source || checked.issue || checked.type !== value.type) {
|
|
@@ -469,12 +441,9 @@ export class RoutineNormalizer {
|
|
|
469
441
|
},
|
|
470
442
|
span: value.span,
|
|
471
443
|
};
|
|
472
|
-
const captured = this.expression(
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
output,
|
|
476
|
-
true,
|
|
477
|
-
);
|
|
444
|
+
const captured = this.expression(selected, initialized, output, {
|
|
445
|
+
allowAggregateLoad: true,
|
|
446
|
+
});
|
|
478
447
|
output.push({
|
|
479
448
|
kind: "assign",
|
|
480
449
|
root: result.id,
|
|
@@ -524,10 +493,10 @@ export class RoutineNormalizer {
|
|
|
524
493
|
}
|
|
525
494
|
case "convert": {
|
|
526
495
|
const operand = this.expression(value.operand, initialized, output);
|
|
527
|
-
const constant = fold(value, this.scope, this.declarations.types);
|
|
528
496
|
if (value.type === value.operand.type) {
|
|
529
497
|
return operand;
|
|
530
498
|
}
|
|
499
|
+
const constant = fold(value, this.scope, this.declarations.types);
|
|
531
500
|
return constant
|
|
532
501
|
? { ...common, kind: "constant", value: constant }
|
|
533
502
|
: { ...common, kind: "convert", operand, policy: value.policy };
|
|
@@ -536,42 +505,16 @@ export class RoutineNormalizer {
|
|
|
536
505
|
const left = this.expression(value.left, initialized, output);
|
|
537
506
|
if (value.operator === "and" || value.operator === "or") {
|
|
538
507
|
const result = this.temporary("BOOL", value.span);
|
|
539
|
-
const
|
|
540
|
-
output.push(
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
new Set(initialized),
|
|
549
|
-
branch,
|
|
508
|
+
const right = this.branchOperand(value.right, initialized);
|
|
509
|
+
output.push(
|
|
510
|
+
...lowerLogical({
|
|
511
|
+
operator: value.operator,
|
|
512
|
+
result: { root: result.id, type: result.type },
|
|
513
|
+
left,
|
|
514
|
+
right,
|
|
515
|
+
span: value.span,
|
|
516
|
+
}),
|
|
550
517
|
);
|
|
551
|
-
branch.push({
|
|
552
|
-
kind: "assign",
|
|
553
|
-
root: result.id,
|
|
554
|
-
value: right,
|
|
555
|
-
span: value.span,
|
|
556
|
-
});
|
|
557
|
-
const condition: CoreExpression =
|
|
558
|
-
value.operator === "and"
|
|
559
|
-
? this.load(result, value.span)
|
|
560
|
-
: {
|
|
561
|
-
kind: "unary",
|
|
562
|
-
operator: "not",
|
|
563
|
-
operand: this.load(result, value.span),
|
|
564
|
-
type: "BOOL",
|
|
565
|
-
span: value.span,
|
|
566
|
-
};
|
|
567
|
-
output.push({
|
|
568
|
-
kind: "if",
|
|
569
|
-
condition,
|
|
570
|
-
// biome-ignore lint/suspicious/noThenProperty: PLC branch data is an array, not a thenable callback.
|
|
571
|
-
then: branch,
|
|
572
|
-
else: [],
|
|
573
|
-
span: value.span,
|
|
574
|
-
});
|
|
575
518
|
initialized.add(result.id);
|
|
576
519
|
return this.load(result, value.span);
|
|
577
520
|
}
|
|
@@ -592,6 +535,16 @@ export class RoutineNormalizer {
|
|
|
592
535
|
}
|
|
593
536
|
}
|
|
594
537
|
|
|
538
|
+
/** An operand that runs only on one branch, so what it initializes stays there. */
|
|
539
|
+
private branchOperand(
|
|
540
|
+
operand: Expression,
|
|
541
|
+
initialized: Set<string>,
|
|
542
|
+
): BranchOperand {
|
|
543
|
+
const statements: CoreStatement[] = [];
|
|
544
|
+
const value = this.expression(operand, new Set(initialized), statements);
|
|
545
|
+
return { statements, value };
|
|
546
|
+
}
|
|
547
|
+
|
|
595
548
|
private normalizePath(
|
|
596
549
|
place: Place,
|
|
597
550
|
initialized: Set<string>,
|
|
@@ -615,7 +568,7 @@ export class RoutineNormalizer {
|
|
|
615
568
|
initialized: Set<string>,
|
|
616
569
|
span: SourceSpan,
|
|
617
570
|
): Variable {
|
|
618
|
-
const target = this.
|
|
571
|
+
const target = this.scope.get(place.root);
|
|
619
572
|
const initializing =
|
|
620
573
|
target?.storage === "local" &&
|
|
621
574
|
!initialized.has(place.root) &&
|
|
@@ -623,7 +576,7 @@ export class RoutineNormalizer {
|
|
|
623
576
|
const checked = placeType(
|
|
624
577
|
place,
|
|
625
578
|
this.declarations.types,
|
|
626
|
-
this.
|
|
579
|
+
this.scope,
|
|
627
580
|
this.ranges,
|
|
628
581
|
!initializing,
|
|
629
582
|
);
|
|
@@ -658,7 +611,7 @@ export class RoutineNormalizer {
|
|
|
658
611
|
const checked = placeType(
|
|
659
612
|
{ root: instance, path: [{ kind: "index", value: index }] },
|
|
660
613
|
this.declarations.types,
|
|
661
|
-
this.
|
|
614
|
+
this.scope,
|
|
662
615
|
this.ranges,
|
|
663
616
|
);
|
|
664
617
|
if (checked.issue) {
|
|
@@ -718,16 +671,21 @@ export class RoutineNormalizer {
|
|
|
718
671
|
});
|
|
719
672
|
}
|
|
720
673
|
|
|
721
|
-
// verifyCall has matched the argument count to the parameters.
|
|
722
674
|
private arguments(
|
|
723
675
|
parameters: readonly Variable[],
|
|
724
676
|
values: readonly Expression[],
|
|
725
677
|
initialized: Set<string>,
|
|
726
678
|
output: CoreStatement[],
|
|
727
679
|
): CoreExpression[] {
|
|
728
|
-
return values.map((value, index) =>
|
|
729
|
-
|
|
730
|
-
|
|
680
|
+
return values.map((value, index) => {
|
|
681
|
+
const parameter = parameters[index];
|
|
682
|
+
if (!parameter) {
|
|
683
|
+
throw new Error(
|
|
684
|
+
"A call passes more arguments than its callee declares.",
|
|
685
|
+
);
|
|
686
|
+
}
|
|
687
|
+
return this.argument(parameter, value, initialized, output);
|
|
688
|
+
});
|
|
731
689
|
}
|
|
732
690
|
|
|
733
691
|
private argument(
|
|
@@ -743,7 +701,6 @@ export class RoutineNormalizer {
|
|
|
743
701
|
return this.borrow(value, initialized, output);
|
|
744
702
|
}
|
|
745
703
|
if (input.mode === "inOut") {
|
|
746
|
-
// A value with no writable address reaches the callee through a temporary.
|
|
747
704
|
if (this.declarations.types.get(value.type)?.kind === "block") {
|
|
748
705
|
reject("Pass a block instance by name.", value.span);
|
|
749
706
|
}
|
|
@@ -768,7 +725,9 @@ export class RoutineNormalizer {
|
|
|
768
725
|
value.span,
|
|
769
726
|
);
|
|
770
727
|
}
|
|
771
|
-
return this.expression(value, initialized, output,
|
|
728
|
+
return this.expression(value, initialized, output, {
|
|
729
|
+
allowAggregateLoad: true,
|
|
730
|
+
});
|
|
772
731
|
}
|
|
773
732
|
|
|
774
733
|
private borrow(
|
|
@@ -777,11 +736,11 @@ export class RoutineNormalizer {
|
|
|
777
736
|
output: CoreStatement[],
|
|
778
737
|
): CoreExpression {
|
|
779
738
|
const block = this.declarations.types.get(value.type)?.kind === "block";
|
|
780
|
-
const root = this.
|
|
739
|
+
const root = this.scope.get(value.place.root);
|
|
781
740
|
const checked = placeType(
|
|
782
741
|
value.place,
|
|
783
742
|
this.declarations.types,
|
|
784
|
-
this.
|
|
743
|
+
this.scope,
|
|
785
744
|
this.ranges,
|
|
786
745
|
!block,
|
|
787
746
|
);
|
|
@@ -789,7 +748,7 @@ export class RoutineNormalizer {
|
|
|
789
748
|
!root ||
|
|
790
749
|
checked.issue ||
|
|
791
750
|
checked.type !== value.type ||
|
|
792
|
-
(!block && !
|
|
751
|
+
(!block && !isValueType(value.type, this.declarations.types)) ||
|
|
793
752
|
this.counters.has(root.id) ||
|
|
794
753
|
(root.storage === "local" && !initialized.has(root.id))
|
|
795
754
|
) {
|
|
@@ -815,13 +774,13 @@ export class RoutineNormalizer {
|
|
|
815
774
|
initialized: Set<string>,
|
|
816
775
|
): BodyResult {
|
|
817
776
|
const output: CoreStatement[] = [];
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
777
|
+
const state = statements.reduce<FlowState>(
|
|
778
|
+
(reached, item) =>
|
|
779
|
+
reached.flow === "fallthrough"
|
|
780
|
+
? this.statement(item, reached.initialized, output)
|
|
781
|
+
: reached,
|
|
782
|
+
{ flow: "fallthrough", initialized },
|
|
783
|
+
);
|
|
825
784
|
return { body: output, ...state };
|
|
826
785
|
}
|
|
827
786
|
|
|
@@ -848,7 +807,7 @@ export class RoutineNormalizer {
|
|
|
848
807
|
this.forStatement(item, initialized, output);
|
|
849
808
|
return { flow: "fallthrough", initialized };
|
|
850
809
|
case "return": {
|
|
851
|
-
if (item.value?.type !== this.
|
|
810
|
+
if (item.value?.type !== this.routine.result) {
|
|
852
811
|
reject("Return differs from the function signature.", item.span);
|
|
853
812
|
}
|
|
854
813
|
const value =
|
|
@@ -865,14 +824,11 @@ export class RoutineNormalizer {
|
|
|
865
824
|
return { flow: "fallthrough", initialized };
|
|
866
825
|
case "break":
|
|
867
826
|
case "continue":
|
|
868
|
-
// Every active bounded loop holds its counter.
|
|
869
827
|
if (!this.counters.size) {
|
|
870
828
|
reject("Loop control appears outside a bounded loop.", item.span);
|
|
871
829
|
}
|
|
872
830
|
output.push({ kind: item.kind, span: item.span });
|
|
873
831
|
return { flow: "loop", initialized };
|
|
874
|
-
default:
|
|
875
|
-
reject("Unknown PLC statement.");
|
|
876
832
|
}
|
|
877
833
|
}
|
|
878
834
|
|
|
@@ -887,13 +843,7 @@ export class RoutineNormalizer {
|
|
|
887
843
|
const bound =
|
|
888
844
|
!path.length &&
|
|
889
845
|
this.scope.get(item.destination.root)?.mutable === false &&
|
|
890
|
-
|
|
891
|
-
item.value,
|
|
892
|
-
this.declarations.types,
|
|
893
|
-
this.scope,
|
|
894
|
-
this.ranges,
|
|
895
|
-
this.declarations.signatures,
|
|
896
|
-
);
|
|
846
|
+
interval(item.value, this.declarations.types, this.scope, this.ranges);
|
|
897
847
|
const value = this.expression(item.value, initialized, output);
|
|
898
848
|
if (bound) {
|
|
899
849
|
this.ranges.set(item.destination.root, bound);
|
|
@@ -974,7 +924,7 @@ export class RoutineNormalizer {
|
|
|
974
924
|
item.condition,
|
|
975
925
|
truth,
|
|
976
926
|
this.declarations.types,
|
|
977
|
-
this.
|
|
927
|
+
this.scope,
|
|
978
928
|
originalRanges,
|
|
979
929
|
)) {
|
|
980
930
|
this.ranges.set(root, range);
|
|
@@ -1007,8 +957,7 @@ export class RoutineNormalizer {
|
|
|
1007
957
|
output: CoreStatement[],
|
|
1008
958
|
): FlowState {
|
|
1009
959
|
const value = this.expression(item.value, initialized, output);
|
|
1010
|
-
|
|
1011
|
-
const type = this.declarations.types.get(item.value.type)!;
|
|
960
|
+
const type = lookup(this.declarations.types, item.value.type, "type");
|
|
1012
961
|
const seen = new Set<string>();
|
|
1013
962
|
const cases = item.cases.map((branch) => {
|
|
1014
963
|
for (const constant of branch.values) {
|
|
@@ -1048,7 +997,7 @@ export class RoutineNormalizer {
|
|
|
1048
997
|
initialized: Set<string>,
|
|
1049
998
|
output: CoreStatement[],
|
|
1050
999
|
): void {
|
|
1051
|
-
const counter = this.
|
|
1000
|
+
const counter = this.scope.get(item.counter);
|
|
1052
1001
|
const counterType = counter && this.declarations.types.get(counter.type);
|
|
1053
1002
|
const storage =
|
|
1054
1003
|
item.counterScope === "existing"
|
|
@@ -1072,36 +1021,30 @@ export class RoutineNormalizer {
|
|
|
1072
1021
|
const startRange = interval(
|
|
1073
1022
|
item.start,
|
|
1074
1023
|
this.declarations.types,
|
|
1075
|
-
this.
|
|
1024
|
+
this.scope,
|
|
1076
1025
|
this.ranges,
|
|
1077
1026
|
);
|
|
1078
1027
|
const limitRange = interval(
|
|
1079
1028
|
item.limit,
|
|
1080
1029
|
this.declarations.types,
|
|
1081
|
-
this.
|
|
1030
|
+
this.scope,
|
|
1082
1031
|
this.ranges,
|
|
1083
1032
|
);
|
|
1084
1033
|
if (!startRange || !limitRange) {
|
|
1085
1034
|
reject("Loop bounds require finite integer ranges.", item.span);
|
|
1086
1035
|
}
|
|
1087
1036
|
const step = BigInt(item.step);
|
|
1088
|
-
const { first, last, count, range } = loopBounds(
|
|
1037
|
+
const { first, last, count, range, terminal } = loopBounds(
|
|
1089
1038
|
startRange,
|
|
1090
1039
|
limitRange,
|
|
1091
1040
|
step,
|
|
1092
1041
|
item.exclusive,
|
|
1093
1042
|
);
|
|
1094
1043
|
const dynamic = start?.kind !== "integer" || limit?.kind !== "integer";
|
|
1095
|
-
if (
|
|
1096
|
-
count > maxLoopIterations ||
|
|
1097
|
-
Number(count) !== item.maxIterations ||
|
|
1098
|
-
!Number.isSafeInteger(item.maxIterations)
|
|
1099
|
-
) {
|
|
1044
|
+
if (count > maxLoopIterations || Number(count) !== item.maxIterations) {
|
|
1100
1045
|
reject("Loop iteration proof differs from its bounds.", item.span);
|
|
1101
1046
|
}
|
|
1102
1047
|
if (item.counterScope === "existing") {
|
|
1103
|
-
const lastValue = step > 0n ? range[1] : range[0];
|
|
1104
|
-
const terminal = dynamic ? lastValue + step : first + count * step;
|
|
1105
1048
|
if (count > 0n && !admits(integerRange(counterType), terminal)) {
|
|
1106
1049
|
reject(
|
|
1107
1050
|
"The existing loop counter overflows on termination.",
|