@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
|
@@ -1,24 +1,29 @@
|
|
|
1
|
-
|
|
1
|
+
import { loopIterations } from "@tsplc/iec/arithmetic";
|
|
2
2
|
import ts from "typescript";
|
|
3
|
-
import { copied,
|
|
3
|
+
import { copied, mapped, type Part } from "../positions";
|
|
4
|
+
import { namesSdk, sdkModules } from "../sdk";
|
|
5
|
+
import { unparenthesized } from "../types";
|
|
6
|
+
import { parameterDefault } from "./declarations";
|
|
4
7
|
import type { DialectText } from "./dialect";
|
|
5
8
|
import type { ExpressionSpecializer, SpecializeContext } from "./expressions";
|
|
6
|
-
import type
|
|
9
|
+
import { type Frame, looping } from "./frame";
|
|
7
10
|
import type { Initializers } from "./initializers";
|
|
8
|
-
import {
|
|
9
|
-
|
|
10
|
-
iterationCount,
|
|
11
|
-
type Jumps,
|
|
12
|
-
type Traversal,
|
|
13
|
-
} from "./iterators";
|
|
14
|
-
import type { Layouts } from "./layouts";
|
|
11
|
+
import type { GeneratorCall, Jumps, Traversal } from "./iterators";
|
|
12
|
+
import { arrayLayout, type Layouts } from "./layouts";
|
|
15
13
|
import {
|
|
16
14
|
constant,
|
|
17
15
|
type FunctionLike,
|
|
16
|
+
isStoredState,
|
|
17
|
+
known,
|
|
18
18
|
SpecializeError,
|
|
19
19
|
type Value,
|
|
20
20
|
} from "./values";
|
|
21
|
-
import {
|
|
21
|
+
import {
|
|
22
|
+
assignsName,
|
|
23
|
+
isAssignment,
|
|
24
|
+
isUpdate,
|
|
25
|
+
occursOutsideFunctions,
|
|
26
|
+
} from "./writes";
|
|
22
27
|
|
|
23
28
|
/**
|
|
24
29
|
* A statement break and continue can reach. One emitted as written keeps its user's labels; one setup
|
|
@@ -38,21 +43,11 @@ export class StatementSpecializer {
|
|
|
38
43
|
readonly failures: SpecializeError[] = [];
|
|
39
44
|
/** The generators running in place, outermost first, so one can't recurse into itself. */
|
|
40
45
|
private readonly generators: FunctionLike[] = [];
|
|
41
|
-
/** Labels written on the statement being specialized, for the loop or switch it turns out to be. */
|
|
42
|
-
private labels: readonly string[] = [];
|
|
43
|
-
private readonly checker: ts.TypeChecker;
|
|
44
|
-
private readonly dialect: DialectText;
|
|
45
|
-
private readonly layouts: Layouts;
|
|
46
|
-
private readonly initializers: Initializers;
|
|
47
46
|
|
|
48
47
|
constructor(
|
|
49
|
-
context: SpecializeContext,
|
|
48
|
+
private readonly context: SpecializeContext,
|
|
50
49
|
private readonly expressions: ExpressionSpecializer,
|
|
51
50
|
) {
|
|
52
|
-
this.checker = context.checker;
|
|
53
|
-
this.dialect = context.dialect;
|
|
54
|
-
this.layouts = context.layouts;
|
|
55
|
-
this.initializers = context.initializers;
|
|
56
51
|
expressions.iterators.attach({
|
|
57
52
|
run: (generator, frame, at, each) =>
|
|
58
53
|
this.runGenerator(generator, frame, [], at, each),
|
|
@@ -70,12 +65,22 @@ export class StatementSpecializer {
|
|
|
70
65
|
}
|
|
71
66
|
}
|
|
72
67
|
|
|
73
|
-
/** Statements after one that always
|
|
68
|
+
/** Statements after one that always jumps are unreachable, and may read what setup left undefined on that path. */
|
|
74
69
|
private statements(
|
|
75
70
|
nodes: ReadonlyArray<ts.Statement>,
|
|
76
71
|
frame: Frame,
|
|
77
72
|
): boolean {
|
|
78
|
-
for (const statement of nodes) {
|
|
73
|
+
for (const [index, statement] of nodes.entries()) {
|
|
74
|
+
const rest = nodes.slice(index + 1);
|
|
75
|
+
if (
|
|
76
|
+
ts.isIfStatement(statement) &&
|
|
77
|
+
!statement.elseStatement &&
|
|
78
|
+
rest.length
|
|
79
|
+
) {
|
|
80
|
+
return this.recorded(statement, frame, () =>
|
|
81
|
+
this.guard(statement, rest, frame),
|
|
82
|
+
);
|
|
83
|
+
}
|
|
79
84
|
if (this.statement(statement, frame)) {
|
|
80
85
|
return true;
|
|
81
86
|
}
|
|
@@ -83,14 +88,76 @@ export class StatementSpecializer {
|
|
|
83
88
|
return false;
|
|
84
89
|
}
|
|
85
90
|
|
|
86
|
-
/**
|
|
91
|
+
/**
|
|
92
|
+
* An `if` without `else`, then the statements after it. When its branch always jumps, as a loop's
|
|
93
|
+
* `if (i >= count) break;` does, they run only when the condition is false: they are its `else`,
|
|
94
|
+
* where the PLC's range proofs know the condition is false, as they know it inside an `else`.
|
|
95
|
+
*/
|
|
96
|
+
private guard(
|
|
97
|
+
node: ts.IfStatement,
|
|
98
|
+
rest: ReadonlyArray<ts.Statement>,
|
|
99
|
+
frame: Frame,
|
|
100
|
+
): boolean {
|
|
101
|
+
const condition = this.expressions.expression(node.expression, frame);
|
|
102
|
+
if (
|
|
103
|
+
condition.kind === "const" ||
|
|
104
|
+
condition.kind === "static" ||
|
|
105
|
+
condition.kind === "function"
|
|
106
|
+
) {
|
|
107
|
+
const truthy =
|
|
108
|
+
condition.kind === "const" ? Boolean(condition.value) : true;
|
|
109
|
+
return (
|
|
110
|
+
(truthy && this.statement(node.thenStatement, frame)) ||
|
|
111
|
+
this.statements(rest, frame)
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
frame.emitter.open(
|
|
115
|
+
mapped`if (${this.dialect.condition(condition, node.expression)}) {`,
|
|
116
|
+
);
|
|
117
|
+
if (!this.inner(node.thenStatement, frame)) {
|
|
118
|
+
frame.emitter.close();
|
|
119
|
+
return this.statements(rest, frame);
|
|
120
|
+
}
|
|
121
|
+
frame.emitter.reopen("} else {");
|
|
122
|
+
const restJumps = this.statements(rest, frame);
|
|
123
|
+
frame.emitter.close();
|
|
124
|
+
return restJumps;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
private get checker(): ts.TypeChecker {
|
|
128
|
+
return this.context.checker;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
private get dialect(): DialectText {
|
|
132
|
+
return this.context.dialect;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
private get layouts(): Layouts {
|
|
136
|
+
return this.context.layouts;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
private get initializers(): Initializers {
|
|
140
|
+
return this.context.initializers;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** True when control never continues past the statement: it returns, throws, breaks or continues on every path. */
|
|
87
144
|
private statement(node: ts.Statement, frame: Frame): boolean {
|
|
145
|
+
return this.recorded(node, frame, () =>
|
|
146
|
+
this.specializeStatement(node, frame),
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** Specializes a statement, recording a failure in it and skipping it, so the build reports every failure. */
|
|
151
|
+
private recorded(
|
|
152
|
+
node: ts.Statement,
|
|
153
|
+
frame: Frame,
|
|
154
|
+
specialize: () => boolean,
|
|
155
|
+
): boolean {
|
|
88
156
|
const enclosing = frame.emitter.origin;
|
|
89
157
|
frame.emitter.origin = node;
|
|
90
158
|
try {
|
|
91
|
-
return
|
|
159
|
+
return specialize();
|
|
92
160
|
} catch (cause) {
|
|
93
|
-
// A failure is recorded and the statement skipped; the build stops after reporting them all.
|
|
94
161
|
if (
|
|
95
162
|
!(cause instanceof SpecializeError) ||
|
|
96
163
|
ts.isBlock(node) ||
|
|
@@ -116,9 +183,9 @@ export class StatementSpecializer {
|
|
|
116
183
|
const { emitter } = frame;
|
|
117
184
|
if (ts.isBlock(node)) {
|
|
118
185
|
emitter.open("{");
|
|
119
|
-
const
|
|
186
|
+
const jumps = this.statements(node.statements, frame);
|
|
120
187
|
emitter.close();
|
|
121
|
-
return
|
|
188
|
+
return jumps;
|
|
122
189
|
}
|
|
123
190
|
if (ts.isVariableStatement(node)) {
|
|
124
191
|
const mutable = !(node.declarationList.flags & ts.NodeFlags.Const);
|
|
@@ -142,39 +209,17 @@ export class StatementSpecializer {
|
|
|
142
209
|
this.returnStatement(node, frame);
|
|
143
210
|
return true;
|
|
144
211
|
}
|
|
145
|
-
if (ts.
|
|
146
|
-
this.
|
|
147
|
-
return false;
|
|
148
|
-
}
|
|
149
|
-
if (ts.isSwitchStatement(node)) {
|
|
150
|
-
this.switch(node, frame);
|
|
151
|
-
return false;
|
|
152
|
-
}
|
|
153
|
-
if (ts.isForStatement(node)) {
|
|
154
|
-
this.forLoop(node, frame);
|
|
212
|
+
if (ts.isIterationStatement(node, false) || ts.isSwitchStatement(node)) {
|
|
213
|
+
this.loopOrSwitch(node, [], frame);
|
|
155
214
|
return false;
|
|
156
215
|
}
|
|
157
|
-
if (
|
|
158
|
-
(ts.isWhileStatement(node) || ts.isDoStatement(node)) &&
|
|
159
|
-
frame.async &&
|
|
160
|
-
containsAwait(node.statement)
|
|
161
|
-
) {
|
|
162
|
-
this.suspendingLoop(node, frame);
|
|
163
|
-
return false;
|
|
164
|
-
}
|
|
165
|
-
if (ts.isWhileStatement(node)) {
|
|
166
|
-
throw new SpecializeError(
|
|
167
|
-
node,
|
|
168
|
-
"Use a counted for loop or for…of; while loops have no bound to prove.",
|
|
169
|
-
);
|
|
170
|
-
}
|
|
171
216
|
if (ts.isTryStatement(node)) {
|
|
172
217
|
this.tryStatement(node, frame);
|
|
173
218
|
return false;
|
|
174
219
|
}
|
|
175
220
|
if (ts.isBreakStatement(node) || ts.isContinueStatement(node)) {
|
|
176
221
|
emitter.line(this.jumpText(node));
|
|
177
|
-
return
|
|
222
|
+
return true;
|
|
178
223
|
}
|
|
179
224
|
if (ts.isLabeledStatement(node)) {
|
|
180
225
|
this.labelled(node, frame);
|
|
@@ -189,9 +234,40 @@ export class StatementSpecializer {
|
|
|
189
234
|
emitter.line(mapped`throw ${this.dialect.code(code, undefined)};`);
|
|
190
235
|
return true;
|
|
191
236
|
}
|
|
237
|
+
throw unsupported(node);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/** A loop or switch, which jumps reach by the labels written on it. */
|
|
241
|
+
private loopOrSwitch(
|
|
242
|
+
node: ts.IterationStatement | ts.SwitchStatement,
|
|
243
|
+
labels: readonly string[],
|
|
244
|
+
frame: Frame,
|
|
245
|
+
): void {
|
|
246
|
+
if (ts.isForOfStatement(node)) {
|
|
247
|
+
this.forOf(node, labels, frame);
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
if (ts.isSwitchStatement(node)) {
|
|
251
|
+
this.switch(node, labels, frame);
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
if (ts.isForStatement(node)) {
|
|
255
|
+
this.forLoop(node, labels, frame);
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
if (!ts.isWhileStatement(node) && !ts.isDoStatement(node)) {
|
|
259
|
+
throw unsupported(node);
|
|
260
|
+
}
|
|
261
|
+
if (
|
|
262
|
+
frame.async &&
|
|
263
|
+
occursOutsideFunctions(node.statement, ts.isAwaitExpression)
|
|
264
|
+
) {
|
|
265
|
+
this.suspendingLoop(node, labels, frame);
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
192
268
|
throw new SpecializeError(
|
|
193
269
|
node,
|
|
194
|
-
|
|
270
|
+
"Use a counted for loop or for…of; while loops have no bound to prove.",
|
|
195
271
|
);
|
|
196
272
|
}
|
|
197
273
|
|
|
@@ -213,28 +289,22 @@ export class StatementSpecializer {
|
|
|
213
289
|
return emitted ? `break ${emitted};` : copied(node);
|
|
214
290
|
}
|
|
215
291
|
|
|
216
|
-
/**
|
|
292
|
+
/** Gives the labels on the statement to the loop or switch it labels; a labelled block keeps them as written. */
|
|
217
293
|
private labelled(node: ts.LabeledStatement, frame: Frame): void {
|
|
218
294
|
const labels: string[] = [];
|
|
219
295
|
const inner = innermostLabelled(node, labels);
|
|
220
296
|
if (ts.isIterationStatement(inner, false) || ts.isSwitchStatement(inner)) {
|
|
221
|
-
this.
|
|
222
|
-
|
|
223
|
-
|
|
297
|
+
this.recorded(inner, frame, () => {
|
|
298
|
+
this.loopOrSwitch(inner, labels, frame);
|
|
299
|
+
return false;
|
|
300
|
+
});
|
|
224
301
|
return;
|
|
225
302
|
}
|
|
226
|
-
frame.emitter.open(`${labels
|
|
303
|
+
frame.emitter.open(`${labelText(labels)}{`);
|
|
227
304
|
this.scoped({ kind: "labelled", labels }, () => this.inner(inner, frame));
|
|
228
305
|
frame.emitter.close();
|
|
229
306
|
}
|
|
230
307
|
|
|
231
|
-
/** The labels on the loop or switch about to be specialized, which it takes. */
|
|
232
|
-
private takeLabels(): readonly string[] {
|
|
233
|
-
const labels = this.labels;
|
|
234
|
-
this.labels = [];
|
|
235
|
-
return labels;
|
|
236
|
-
}
|
|
237
|
-
|
|
238
308
|
private scoped<T>(scope: JumpScope, run: () => T): T {
|
|
239
309
|
this.scopes.push(scope);
|
|
240
310
|
try {
|
|
@@ -264,19 +334,21 @@ export class StatementSpecializer {
|
|
|
264
334
|
frame.emitter.open(
|
|
265
335
|
mapped`if (${this.dialect.condition(condition, node.expression)}) {`,
|
|
266
336
|
);
|
|
267
|
-
const
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
elseReturns = this.inner(node.elseStatement, frame);
|
|
272
|
-
}
|
|
337
|
+
const thenJumps = this.inner(node.thenStatement, frame);
|
|
338
|
+
const elseJumps =
|
|
339
|
+
node.elseStatement !== undefined &&
|
|
340
|
+
this.elseBranch(node.elseStatement, frame);
|
|
273
341
|
frame.emitter.close();
|
|
274
|
-
return
|
|
342
|
+
return thenJumps && elseJumps;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
private elseBranch(node: ts.Statement, frame: Frame): boolean {
|
|
346
|
+
frame.emitter.reopen("} else {");
|
|
347
|
+
return this.inner(node, frame);
|
|
275
348
|
}
|
|
276
349
|
|
|
277
350
|
/** `return x satisfies never` marks an unreachable end, which returns the result type's default. */
|
|
278
351
|
private returnStatement(node: ts.ReturnStatement, frame: Frame): void {
|
|
279
|
-
// A generator run in place ends by leaving its block.
|
|
280
352
|
if (frame.generator) {
|
|
281
353
|
frame.emitter.line(`break ${frame.generator.exit};`);
|
|
282
354
|
return;
|
|
@@ -285,10 +357,7 @@ export class StatementSpecializer {
|
|
|
285
357
|
frame.emitter.line("return;");
|
|
286
358
|
return;
|
|
287
359
|
}
|
|
288
|
-
if (
|
|
289
|
-
ts.isSatisfiesExpression(node.expression) &&
|
|
290
|
-
this.isNever(node.expression.type)
|
|
291
|
-
) {
|
|
360
|
+
if (unreachableEnd(node)) {
|
|
292
361
|
frame.emitter.line(
|
|
293
362
|
`return ${this.initializers.defaultOf(frame.returns)};`,
|
|
294
363
|
);
|
|
@@ -300,13 +369,7 @@ export class StatementSpecializer {
|
|
|
300
369
|
frame.returns,
|
|
301
370
|
);
|
|
302
371
|
this.expressions.references.returned(value, node.expression, frame);
|
|
303
|
-
if (
|
|
304
|
-
value.kind === "code" &&
|
|
305
|
-
((value.place &&
|
|
306
|
-
value.declared &&
|
|
307
|
-
value.declared.flags & ts.TypeFlags.Object) ||
|
|
308
|
-
value.copyOf)
|
|
309
|
-
) {
|
|
372
|
+
if (isStoredState(value)) {
|
|
310
373
|
frame.returnsStored = true;
|
|
311
374
|
}
|
|
312
375
|
// An array's length is what is returned, as its type names none.
|
|
@@ -314,7 +377,10 @@ export class StatementSpecializer {
|
|
|
314
377
|
frame.returns &&
|
|
315
378
|
this.checker.getIndexTypeOfType(frame.returns, ts.IndexKind.Number);
|
|
316
379
|
if (element && value.kind === "virtual" && value.items) {
|
|
317
|
-
frame.resultLayout =
|
|
380
|
+
frame.resultLayout = arrayLayout(
|
|
381
|
+
this.layouts.text(element),
|
|
382
|
+
value.items.length,
|
|
383
|
+
);
|
|
318
384
|
frame.resultLength = value.items.length;
|
|
319
385
|
} else if (element && value.kind === "code" && value.layout) {
|
|
320
386
|
frame.resultLayout = value.layout;
|
|
@@ -329,41 +395,36 @@ export class StatementSpecializer {
|
|
|
329
395
|
/** The IEC width a routine's result is declared with, as `(): UINT` gives it. */
|
|
330
396
|
private resultIec(frame: Frame): string | undefined {
|
|
331
397
|
const declaration = frame.emitter.origin;
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
: undefined;
|
|
398
|
+
return this.layouts.iecName(
|
|
399
|
+
ts.findAncestor(declaration, ts.isFunctionLike),
|
|
400
|
+
);
|
|
336
401
|
}
|
|
337
402
|
|
|
338
403
|
/** A try keeps its blocks, for the service calls in it. */
|
|
339
404
|
private tryStatement(node: ts.TryStatement, frame: Frame): void {
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
405
|
+
// Synchronous scan code can't fail, so its catch block never runs, with or without finally.
|
|
406
|
+
if (!frame.async && !node.finallyBlock) {
|
|
407
|
+
frame.emitter.open("{");
|
|
408
|
+
this.statements(node.tryBlock.statements, frame);
|
|
409
|
+
frame.emitter.close();
|
|
410
|
+
return;
|
|
343
411
|
}
|
|
344
|
-
|
|
412
|
+
frame.emitter.open("try {");
|
|
413
|
+
this.statements(node.tryBlock.statements, frame);
|
|
414
|
+
if (node.catchClause && frame.async) {
|
|
345
415
|
const binding = node.catchClause.variableDeclaration?.name;
|
|
346
416
|
frame.emitter.reopen(
|
|
347
417
|
`} catch${binding ? ` (${binding.getText()})` : ""} {`,
|
|
348
418
|
);
|
|
349
419
|
// The binding holds the error code, which a catch block can only pass on as it is.
|
|
350
420
|
if (binding && ts.isIdentifier(binding)) {
|
|
351
|
-
|
|
352
|
-
kind: "code",
|
|
353
|
-
text: binding.text,
|
|
354
|
-
declared: this.checker.getTypeAtLocation(binding),
|
|
355
|
-
place: true,
|
|
356
|
-
});
|
|
357
|
-
}
|
|
358
|
-
for (const statement of node.catchClause.block.statements) {
|
|
359
|
-
this.statement(statement, frame);
|
|
421
|
+
this.bindLocal(binding, binding.text, frame, undefined);
|
|
360
422
|
}
|
|
423
|
+
this.statements(node.catchClause.block.statements, frame);
|
|
361
424
|
}
|
|
362
425
|
if (node.finallyBlock) {
|
|
363
426
|
frame.emitter.reopen("} finally {");
|
|
364
|
-
|
|
365
|
-
this.statement(statement, frame);
|
|
366
|
-
}
|
|
427
|
+
this.statements(node.finallyBlock.statements, frame);
|
|
367
428
|
}
|
|
368
429
|
frame.emitter.close();
|
|
369
430
|
}
|
|
@@ -375,8 +436,24 @@ export class StatementSpecializer {
|
|
|
375
436
|
: this.statement(node, frame);
|
|
376
437
|
}
|
|
377
438
|
|
|
378
|
-
|
|
379
|
-
|
|
439
|
+
/** Binds a name to the PLC local that holds it, which scan code may assign. */
|
|
440
|
+
private bindLocal(
|
|
441
|
+
name: ts.Identifier,
|
|
442
|
+
local: string,
|
|
443
|
+
frame: Frame,
|
|
444
|
+
iec: string | undefined,
|
|
445
|
+
): void {
|
|
446
|
+
const symbol = this.checker.getSymbolAtLocation(name);
|
|
447
|
+
if (!symbol) {
|
|
448
|
+
throw new SpecializeError(name, `${name.text} names nothing.`);
|
|
449
|
+
}
|
|
450
|
+
frame.env.set(symbol, {
|
|
451
|
+
kind: "code",
|
|
452
|
+
text: local,
|
|
453
|
+
declared: this.checker.getTypeAtLocation(name),
|
|
454
|
+
place: true,
|
|
455
|
+
...(iec && { iec }),
|
|
456
|
+
});
|
|
380
457
|
}
|
|
381
458
|
|
|
382
459
|
private declare(
|
|
@@ -394,14 +471,10 @@ export class StatementSpecializer {
|
|
|
394
471
|
declaration.type && this.checker.getTypeAtLocation(declaration.type),
|
|
395
472
|
)
|
|
396
473
|
: this.bareValue(declared);
|
|
397
|
-
this.expressions.bind(
|
|
398
|
-
declaration.name,
|
|
399
|
-
value,
|
|
474
|
+
this.expressions.bind(declaration.name, value, frame, declared, {
|
|
400
475
|
mutable,
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
this.layouts.iecName(declaration),
|
|
404
|
-
);
|
|
476
|
+
iec: this.layouts.iecName(declaration),
|
|
477
|
+
});
|
|
405
478
|
}
|
|
406
479
|
|
|
407
480
|
/** What a local declared without a value holds until it is assigned. */
|
|
@@ -416,10 +489,25 @@ export class StatementSpecializer {
|
|
|
416
489
|
};
|
|
417
490
|
}
|
|
418
491
|
|
|
419
|
-
private forOf(
|
|
420
|
-
|
|
492
|
+
private forOf(
|
|
493
|
+
node: ts.ForOfStatement,
|
|
494
|
+
labels: readonly string[],
|
|
495
|
+
frame: Frame,
|
|
496
|
+
): void {
|
|
497
|
+
const generator = this.expressions.iterators.generator(
|
|
498
|
+
node.expression,
|
|
499
|
+
frame,
|
|
500
|
+
);
|
|
501
|
+
// Over a generator, each yield runs the loop's body in a labelled block its continue leaves;
|
|
502
|
+
// break or the generator's return leave the block around the whole generator.
|
|
421
503
|
if (generator) {
|
|
422
|
-
this.
|
|
504
|
+
this.runGenerator(
|
|
505
|
+
generator,
|
|
506
|
+
frame,
|
|
507
|
+
labels,
|
|
508
|
+
node,
|
|
509
|
+
this.loopBody(node, labels, frame),
|
|
510
|
+
);
|
|
423
511
|
return;
|
|
424
512
|
}
|
|
425
513
|
const iterated = this.expressions.iterators.iterated(
|
|
@@ -427,7 +515,7 @@ export class StatementSpecializer {
|
|
|
427
515
|
frame,
|
|
428
516
|
);
|
|
429
517
|
if (iterated) {
|
|
430
|
-
this.overTraversal(node, iterated, frame);
|
|
518
|
+
this.overTraversal(node, iterated, labels, frame);
|
|
431
519
|
return;
|
|
432
520
|
}
|
|
433
521
|
const call = node.expression;
|
|
@@ -442,7 +530,45 @@ export class StatementSpecializer {
|
|
|
442
530
|
frame,
|
|
443
531
|
);
|
|
444
532
|
if (array.kind === "code") {
|
|
445
|
-
this.forEntries(node, array.text, frame);
|
|
533
|
+
this.forEntries(node, mapped`${array.text}.entries()`, labels, frame);
|
|
534
|
+
return;
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
// The SDK's entries(record) over stored state stays a loop over the record's enum keys.
|
|
538
|
+
if (
|
|
539
|
+
ts.isCallExpression(call) &&
|
|
540
|
+
namesSdk(this.checker, this.context.sdkFile, call.expression, "entries")
|
|
541
|
+
) {
|
|
542
|
+
const [argument] = call.arguments;
|
|
543
|
+
const record = argument && this.expressions.expression(argument, frame);
|
|
544
|
+
if (record?.kind === "code") {
|
|
545
|
+
this.context.imports.addSdk(sdkModules.types, "entries");
|
|
546
|
+
this.forEntries(node, mapped`entries(${record.text})`, labels, frame);
|
|
547
|
+
return;
|
|
548
|
+
}
|
|
549
|
+
// A record setup built unrolls over its keys, each its enum member.
|
|
550
|
+
if (record?.kind === "static" && record.node.captured.kind === "object") {
|
|
551
|
+
const holder = record.node;
|
|
552
|
+
const pairs = Object.entries(record.node.captured.fields).map(
|
|
553
|
+
([key, field]): Value => ({
|
|
554
|
+
kind: "virtual",
|
|
555
|
+
items: [
|
|
556
|
+
constant(Number(key)),
|
|
557
|
+
this.context.storage.fromCaptured(field, holder, {
|
|
558
|
+
kind: "field",
|
|
559
|
+
name: key,
|
|
560
|
+
}),
|
|
561
|
+
],
|
|
562
|
+
}),
|
|
563
|
+
);
|
|
564
|
+
const name = loopDeclaration(node).name;
|
|
565
|
+
const declared = this.checker.getTypeAtLocation(name);
|
|
566
|
+
this.unrolled(node.statement, labels, pairs, frame, (pair) =>
|
|
567
|
+
this.expressions.bind(name, pair, frame, declared, {
|
|
568
|
+
mutable: false,
|
|
569
|
+
iec: undefined,
|
|
570
|
+
}),
|
|
571
|
+
);
|
|
446
572
|
return;
|
|
447
573
|
}
|
|
448
574
|
}
|
|
@@ -451,24 +577,20 @@ export class StatementSpecializer {
|
|
|
451
577
|
this.overTraversal(
|
|
452
578
|
node,
|
|
453
579
|
this.expressions.iterators.over(iterable),
|
|
580
|
+
labels,
|
|
454
581
|
frame,
|
|
455
582
|
);
|
|
456
583
|
return;
|
|
457
584
|
}
|
|
458
585
|
const items = this.expressions.items(iterable);
|
|
459
|
-
const
|
|
460
|
-
if (!declaration) {
|
|
461
|
-
throw new SpecializeError(
|
|
462
|
-
node.initializer,
|
|
463
|
-
"Declare one const in for…of.",
|
|
464
|
-
);
|
|
465
|
-
}
|
|
466
|
-
const name = declaration.name;
|
|
586
|
+
const name = loopDeclaration(node).name;
|
|
467
587
|
const declared = this.checker.getTypeAtLocation(name);
|
|
468
|
-
const labels = this.takeLabels();
|
|
469
588
|
if (items) {
|
|
470
|
-
this.unrolled(node.statement, labels, items
|
|
471
|
-
this.expressions.bind(name,
|
|
589
|
+
this.unrolled(node.statement, labels, items, frame, (item) =>
|
|
590
|
+
this.expressions.bind(name, item, frame, declared, {
|
|
591
|
+
mutable: false,
|
|
592
|
+
iec: undefined,
|
|
593
|
+
}),
|
|
472
594
|
);
|
|
473
595
|
return;
|
|
474
596
|
}
|
|
@@ -485,37 +607,20 @@ export class StatementSpecializer {
|
|
|
485
607
|
frame.emitter.open(
|
|
486
608
|
mapped`${labelText(labels)}for (const ${local} of ${iterable.text}) {`,
|
|
487
609
|
);
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
text: local,
|
|
491
|
-
declared,
|
|
492
|
-
place: true,
|
|
493
|
-
});
|
|
494
|
-
this.expressions.iterators.looping(undefined, () =>
|
|
495
|
-
this.scoped({ kind: "loop", labels }, () =>
|
|
496
|
-
this.inner(node.statement, frame),
|
|
497
|
-
),
|
|
498
|
-
);
|
|
610
|
+
this.bindLocal(name, local, frame, undefined);
|
|
611
|
+
this.loop(node.statement, labels, undefined, frame);
|
|
499
612
|
frame.emitter.close();
|
|
500
613
|
}
|
|
501
614
|
|
|
502
|
-
/**
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
* the block around the whole generator.
|
|
506
|
-
*/
|
|
507
|
-
private inlineGenerator(
|
|
508
|
-
node: ts.ForOfStatement,
|
|
509
|
-
generator: GeneratorCall,
|
|
615
|
+
/** The body of a loop that stays a loop in the PLC, which runs up to `runs` times when that is known. */
|
|
616
|
+
private loop(
|
|
617
|
+
body: ts.Statement,
|
|
510
618
|
labels: readonly string[],
|
|
619
|
+
runs: number | undefined,
|
|
511
620
|
frame: Frame,
|
|
512
621
|
): void {
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
frame,
|
|
516
|
-
labels,
|
|
517
|
-
node,
|
|
518
|
-
this.loopBody(node, labels, frame),
|
|
622
|
+
looping(frame, runs, () =>
|
|
623
|
+
this.scoped({ kind: "loop", labels }, () => this.inner(body, frame)),
|
|
519
624
|
);
|
|
520
625
|
}
|
|
521
626
|
|
|
@@ -523,9 +628,10 @@ export class StatementSpecializer {
|
|
|
523
628
|
private overTraversal(
|
|
524
629
|
node: ts.ForOfStatement,
|
|
525
630
|
traversal: Traversal,
|
|
631
|
+
labels: readonly string[],
|
|
526
632
|
frame: Frame,
|
|
527
633
|
): void {
|
|
528
|
-
const body = this.loopBody(node,
|
|
634
|
+
const body = this.loopBody(node, labels, frame);
|
|
529
635
|
traversal.run(frame, (element) => body(element.value, element));
|
|
530
636
|
}
|
|
531
637
|
|
|
@@ -538,17 +644,14 @@ export class StatementSpecializer {
|
|
|
538
644
|
labels: readonly string[],
|
|
539
645
|
frame: Frame,
|
|
540
646
|
): (value: Value, jumps: Jumps) => void {
|
|
541
|
-
const declaration =
|
|
542
|
-
if (!declaration) {
|
|
543
|
-
throw new SpecializeError(
|
|
544
|
-
node.initializer,
|
|
545
|
-
"Declare one const in for…of.",
|
|
546
|
-
);
|
|
547
|
-
}
|
|
647
|
+
const declaration = loopDeclaration(node);
|
|
548
648
|
const declared = this.checker.getTypeAtLocation(declaration.name);
|
|
549
649
|
const loop: Frame = { ...frame, env: new Map(frame.env) };
|
|
550
650
|
return (value, jumps) => {
|
|
551
|
-
this.expressions.bind(declaration.name, value,
|
|
651
|
+
this.expressions.bind(declaration.name, value, loop, declared, {
|
|
652
|
+
mutable: false,
|
|
653
|
+
iec: undefined,
|
|
654
|
+
});
|
|
552
655
|
this.scoped(
|
|
553
656
|
{
|
|
554
657
|
kind: "loop",
|
|
@@ -606,19 +709,20 @@ export class StatementSpecializer {
|
|
|
606
709
|
if (!ts.isGetAccessorDeclaration(declaration)) {
|
|
607
710
|
declaration.parameters.forEach((parameter, index) => {
|
|
608
711
|
const given = generator.args[index] ?? constant(undefined);
|
|
609
|
-
const
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
? this.expressions.expression(parameter.initializer, inner)
|
|
614
|
-
: given;
|
|
712
|
+
const fallback = parameterDefault(this.checker, given, parameter);
|
|
713
|
+
const value = fallback
|
|
714
|
+
? this.expressions.expression(fallback, inner)
|
|
715
|
+
: given;
|
|
615
716
|
// A parameter the generator never assigns keeps what was passed, which can be known now.
|
|
616
717
|
this.expressions.bind(
|
|
617
718
|
parameter.name,
|
|
618
719
|
value,
|
|
619
|
-
assignsName(body, parameter.name, this.checker),
|
|
620
720
|
inner,
|
|
621
721
|
this.checker.getTypeAtLocation(parameter),
|
|
722
|
+
{
|
|
723
|
+
mutable: assignsName(body, parameter.name, this.checker),
|
|
724
|
+
iec: this.layouts.iecName(parameter),
|
|
725
|
+
},
|
|
622
726
|
);
|
|
623
727
|
});
|
|
624
728
|
}
|
|
@@ -646,7 +750,10 @@ export class StatementSpecializer {
|
|
|
646
750
|
return;
|
|
647
751
|
}
|
|
648
752
|
if (node.asteriskToken) {
|
|
649
|
-
const delegated = this.expressions.generator(
|
|
753
|
+
const delegated = this.expressions.iterators.generator(
|
|
754
|
+
node.expression,
|
|
755
|
+
frame,
|
|
756
|
+
);
|
|
650
757
|
if (!delegated) {
|
|
651
758
|
throw new SpecializeError(
|
|
652
759
|
node.expression,
|
|
@@ -668,19 +775,19 @@ export class StatementSpecializer {
|
|
|
668
775
|
private unrolled(
|
|
669
776
|
body: ts.Statement,
|
|
670
777
|
labels: readonly string[],
|
|
671
|
-
|
|
778
|
+
items: readonly Value[],
|
|
672
779
|
frame: Frame,
|
|
673
|
-
bindStep: (
|
|
780
|
+
bindStep: (item: Value) => void,
|
|
674
781
|
): void {
|
|
675
782
|
const jumps = containsJump(body);
|
|
676
783
|
const whole = jumps ? frame.emitter.local("tsplcLoop") : undefined;
|
|
677
784
|
if (whole) {
|
|
678
785
|
frame.emitter.open(`${whole}: {`);
|
|
679
786
|
}
|
|
680
|
-
for (
|
|
787
|
+
for (const item of items) {
|
|
681
788
|
const step = jumps ? frame.emitter.local("tsplcStep") : undefined;
|
|
682
789
|
frame.emitter.open(step ? `${step}: {` : "{");
|
|
683
|
-
bindStep(
|
|
790
|
+
bindStep(item);
|
|
684
791
|
this.scoped(
|
|
685
792
|
{ kind: "loop", labels, breakLabel: whole, continueLabel: step },
|
|
686
793
|
() => this.inner(body, frame),
|
|
@@ -695,7 +802,8 @@ export class StatementSpecializer {
|
|
|
695
802
|
/** `for (const [index, item] of array.entries())` over a runtime array stays a loop, as the backend lowers it. */
|
|
696
803
|
private forEntries(
|
|
697
804
|
node: ts.ForOfStatement,
|
|
698
|
-
|
|
805
|
+
iterated: Part,
|
|
806
|
+
labels: readonly string[],
|
|
699
807
|
frame: Frame,
|
|
700
808
|
): void {
|
|
701
809
|
const binding = onlyDeclaration(node.initializer)?.name;
|
|
@@ -716,23 +824,13 @@ export class StatementSpecializer {
|
|
|
716
824
|
}
|
|
717
825
|
const [index, item] = [indexName, itemName].map((name) => {
|
|
718
826
|
const local = frame.emitter.local(name.text);
|
|
719
|
-
|
|
720
|
-
kind: "code",
|
|
721
|
-
text: local,
|
|
722
|
-
declared: this.checker.getTypeAtLocation(name),
|
|
723
|
-
place: true,
|
|
724
|
-
});
|
|
827
|
+
this.bindLocal(name, local, frame, undefined);
|
|
725
828
|
return local;
|
|
726
829
|
});
|
|
727
|
-
const labels = this.takeLabels();
|
|
728
830
|
frame.emitter.open(
|
|
729
|
-
mapped`${labelText(labels)}for (const [${[index, item].join(", ")}] of ${
|
|
730
|
-
);
|
|
731
|
-
this.expressions.iterators.looping(undefined, () =>
|
|
732
|
-
this.scoped({ kind: "loop", labels }, () =>
|
|
733
|
-
this.inner(node.statement, frame),
|
|
734
|
-
),
|
|
831
|
+
mapped`${labelText(labels)}for (const [${[index, item].join(", ")}] of ${iterated}) {`,
|
|
735
832
|
);
|
|
833
|
+
this.loop(node.statement, labels, undefined, frame);
|
|
736
834
|
frame.emitter.close();
|
|
737
835
|
}
|
|
738
836
|
|
|
@@ -742,9 +840,9 @@ export class StatementSpecializer {
|
|
|
742
840
|
*/
|
|
743
841
|
private suspendingLoop(
|
|
744
842
|
node: ts.WhileStatement | ts.DoStatement,
|
|
843
|
+
labels: readonly string[],
|
|
745
844
|
frame: Frame,
|
|
746
845
|
): void {
|
|
747
|
-
const labels = this.takeLabels();
|
|
748
846
|
const test = () =>
|
|
749
847
|
this.dialect.condition(
|
|
750
848
|
this.expressions.expression(node.expression, frame),
|
|
@@ -755,11 +853,7 @@ export class StatementSpecializer {
|
|
|
755
853
|
} else {
|
|
756
854
|
frame.emitter.open(`${labelText(labels)}do {`);
|
|
757
855
|
}
|
|
758
|
-
this.
|
|
759
|
-
this.scoped({ kind: "loop", labels }, () =>
|
|
760
|
-
this.inner(node.statement, frame),
|
|
761
|
-
),
|
|
762
|
-
);
|
|
856
|
+
this.loop(node.statement, labels, undefined, frame);
|
|
763
857
|
if (ts.isWhileStatement(node)) {
|
|
764
858
|
frame.emitter.close();
|
|
765
859
|
return;
|
|
@@ -768,7 +862,11 @@ export class StatementSpecializer {
|
|
|
768
862
|
}
|
|
769
863
|
|
|
770
864
|
/** A counted loop stays a loop, in the header shape the backend proves bounded: `let i = start; i < bound; i++`. */
|
|
771
|
-
private forLoop(
|
|
865
|
+
private forLoop(
|
|
866
|
+
node: ts.ForStatement,
|
|
867
|
+
labels: readonly string[],
|
|
868
|
+
frame: Frame,
|
|
869
|
+
): void {
|
|
772
870
|
const { condition, incrementor, initializer } = node;
|
|
773
871
|
const declaration = onlyDeclaration(initializer);
|
|
774
872
|
if (!declaration || !(declaration.parent.flags & ts.NodeFlags.Let)) {
|
|
@@ -787,6 +885,12 @@ export class StatementSpecializer {
|
|
|
787
885
|
"Compare the counter to a bound and step it in the for header.",
|
|
788
886
|
);
|
|
789
887
|
}
|
|
888
|
+
if (assignsName(node.statement, name, this.checker)) {
|
|
889
|
+
throw new SpecializeError(
|
|
890
|
+
node,
|
|
891
|
+
`The loop changes its counter ${name.text} in its body, and a PLC FOR loop's counter is stepped only by its header: step it in the header, or count with a separate let.`,
|
|
892
|
+
);
|
|
893
|
+
}
|
|
790
894
|
const declared = this.checker.getTypeAtLocation(name);
|
|
791
895
|
// An annotation such as UINT keeps the counter's width, to index alongside unsigned fields.
|
|
792
896
|
const iec = this.layouts.iecName(declaration);
|
|
@@ -797,18 +901,11 @@ export class StatementSpecializer {
|
|
|
797
901
|
);
|
|
798
902
|
const start = this.dialect.code(first, declared);
|
|
799
903
|
const local = frame.emitter.local(name.text);
|
|
800
|
-
|
|
801
|
-
kind: "code",
|
|
802
|
-
text: local,
|
|
803
|
-
declared,
|
|
804
|
-
place: true,
|
|
805
|
-
iec,
|
|
806
|
-
});
|
|
904
|
+
this.bindLocal(name, local, frame, iec);
|
|
807
905
|
const compared = this.expressions.expression(condition.left, frame);
|
|
808
906
|
const limit = this.expressions.expression(condition.right, frame);
|
|
809
907
|
const test = mapped`${this.dialect.code(compared, declared)} ${condition.operatorToken.getText()} ${this.dialect.code(limit, declared)}`;
|
|
810
|
-
const step = this.loopStep(incrementor, local, declared, frame);
|
|
811
|
-
const labels = this.takeLabels();
|
|
908
|
+
const step = this.loopStep(incrementor, name, local, declared, frame);
|
|
812
909
|
frame.emitter.open(
|
|
813
910
|
mapped`${labelText(labels)}for (let ${local}: ${iec ?? this.expressions.localType(declared)} = ${start}; ${test}; ${step}) {`,
|
|
814
911
|
);
|
|
@@ -822,28 +919,35 @@ export class StatementSpecializer {
|
|
|
822
919
|
incrementor,
|
|
823
920
|
)
|
|
824
921
|
: undefined;
|
|
825
|
-
this.
|
|
826
|
-
this.scoped({ kind: "loop", labels }, () =>
|
|
827
|
-
this.inner(node.statement, frame),
|
|
828
|
-
),
|
|
829
|
-
);
|
|
922
|
+
this.loop(node.statement, labels, runs, frame);
|
|
830
923
|
frame.emitter.close();
|
|
831
924
|
}
|
|
832
925
|
|
|
833
926
|
/** The step in a counted loop's header, as `i++` or `i += n` on the counter's local. */
|
|
834
927
|
private loopStep(
|
|
835
928
|
incrementor: ts.Expression,
|
|
929
|
+
counter: ts.Identifier,
|
|
836
930
|
local: string,
|
|
837
931
|
declared: ts.Type,
|
|
838
932
|
frame: Frame,
|
|
839
933
|
): Part {
|
|
934
|
+
const counterSymbol = this.checker.getSymbolAtLocation(counter);
|
|
935
|
+
const isCounter = (target: ts.Expression): boolean =>
|
|
936
|
+
this.checker.getSymbolAtLocation(unparenthesized(target)) ===
|
|
937
|
+
counterSymbol;
|
|
840
938
|
if (
|
|
841
|
-
ts.isPrefixUnaryExpression(incrementor) ||
|
|
842
|
-
|
|
939
|
+
(ts.isPrefixUnaryExpression(incrementor) ||
|
|
940
|
+
ts.isPostfixUnaryExpression(incrementor)) &&
|
|
941
|
+
unitSteps.has(incrementor.operator) &&
|
|
942
|
+
isCounter(incrementor.operand)
|
|
843
943
|
) {
|
|
844
944
|
return `${local}${ts.tokenToString(incrementor.operator)}`;
|
|
845
945
|
}
|
|
846
|
-
if (
|
|
946
|
+
if (
|
|
947
|
+
ts.isBinaryExpression(incrementor) &&
|
|
948
|
+
compoundSteps.has(incrementor.operatorToken.kind) &&
|
|
949
|
+
isCounter(incrementor.left)
|
|
950
|
+
) {
|
|
847
951
|
return mapped`${local} ${incrementor.operatorToken.getText()} ${this.dialect.code(this.expressions.expression(incrementor.right, frame), declared)}`;
|
|
848
952
|
}
|
|
849
953
|
throw new SpecializeError(
|
|
@@ -852,10 +956,13 @@ export class StatementSpecializer {
|
|
|
852
956
|
);
|
|
853
957
|
}
|
|
854
958
|
|
|
855
|
-
private switch(
|
|
959
|
+
private switch(
|
|
960
|
+
node: ts.SwitchStatement,
|
|
961
|
+
labels: readonly string[],
|
|
962
|
+
frame: Frame,
|
|
963
|
+
): void {
|
|
856
964
|
const subject = this.expressions.expression(node.expression, frame);
|
|
857
965
|
const declared = subject.kind === "code" ? subject.declared : undefined;
|
|
858
|
-
const labels = this.takeLabels();
|
|
859
966
|
if (subject.kind === "const") {
|
|
860
967
|
this.constantSwitch(node, subject, labels, frame);
|
|
861
968
|
return;
|
|
@@ -876,24 +983,15 @@ export class StatementSpecializer {
|
|
|
876
983
|
): void {
|
|
877
984
|
for (const clause of node.caseBlock.clauses) {
|
|
878
985
|
if (ts.isDefaultClause(clause)) {
|
|
879
|
-
const unreachable = clause.statements.some(
|
|
880
|
-
(statement) =>
|
|
881
|
-
ts.isReturnStatement(statement) &&
|
|
882
|
-
statement.expression &&
|
|
883
|
-
ts.isSatisfiesExpression(statement.expression) &&
|
|
884
|
-
this.isNever(statement.expression.type),
|
|
885
|
-
);
|
|
886
986
|
frame.emitter.open("default: {");
|
|
887
|
-
if (
|
|
987
|
+
if (clause.statements.some(unreachableEnd)) {
|
|
888
988
|
frame.emitter.line(
|
|
889
989
|
frame.returns && !(frame.returns.flags & ts.TypeFlags.Void)
|
|
890
990
|
? `return ${this.initializers.defaultOf(frame.returns)};`
|
|
891
991
|
: "break;",
|
|
892
992
|
);
|
|
893
993
|
} else {
|
|
894
|
-
|
|
895
|
-
this.statement(statement, frame);
|
|
896
|
-
}
|
|
994
|
+
this.statements(clause.statements, frame);
|
|
897
995
|
}
|
|
898
996
|
frame.emitter.close();
|
|
899
997
|
continue;
|
|
@@ -904,9 +1002,7 @@ export class StatementSpecializer {
|
|
|
904
1002
|
declared,
|
|
905
1003
|
);
|
|
906
1004
|
frame.emitter.open(mapped`case ${this.dialect.code(label, declared)}: {`);
|
|
907
|
-
|
|
908
|
-
this.statement(statement, frame);
|
|
909
|
-
}
|
|
1005
|
+
this.statements(clause.statements, frame);
|
|
910
1006
|
frame.emitter.close();
|
|
911
1007
|
}
|
|
912
1008
|
}
|
|
@@ -932,7 +1028,7 @@ export class StatementSpecializer {
|
|
|
932
1028
|
frame.emitter.open(`${label}: {`);
|
|
933
1029
|
}
|
|
934
1030
|
this.scoped({ kind: "switch", labels, breakLabel: label }, () =>
|
|
935
|
-
this.matchedClauses(node, subject, frame),
|
|
1031
|
+
this.matchedClauses(node, subject, labels, frame),
|
|
936
1032
|
);
|
|
937
1033
|
if (label) {
|
|
938
1034
|
frame.emitter.close();
|
|
@@ -942,6 +1038,7 @@ export class StatementSpecializer {
|
|
|
942
1038
|
private matchedClauses(
|
|
943
1039
|
node: ts.SwitchStatement,
|
|
944
1040
|
subject: Value & { kind: "const" },
|
|
1041
|
+
labels: readonly string[],
|
|
945
1042
|
frame: Frame,
|
|
946
1043
|
): void {
|
|
947
1044
|
const clauses = node.caseBlock.clauses;
|
|
@@ -957,15 +1054,15 @@ export class StatementSpecializer {
|
|
|
957
1054
|
}
|
|
958
1055
|
for (const clause of clauses.slice(start)) {
|
|
959
1056
|
for (const statement of clause.statements) {
|
|
960
|
-
if (
|
|
1057
|
+
if (
|
|
1058
|
+
ts.isBreakStatement(statement) &&
|
|
1059
|
+
(!statement.label || labels.includes(statement.label.text))
|
|
1060
|
+
) {
|
|
1061
|
+
return;
|
|
1062
|
+
}
|
|
1063
|
+
if (this.statement(statement, frame)) {
|
|
961
1064
|
return;
|
|
962
1065
|
}
|
|
963
|
-
this.statement(statement, frame);
|
|
964
|
-
}
|
|
965
|
-
if (
|
|
966
|
-
clause.statements.some((statement) => ts.isReturnStatement(statement))
|
|
967
|
-
) {
|
|
968
|
-
return;
|
|
969
1066
|
}
|
|
970
1067
|
}
|
|
971
1068
|
}
|
|
@@ -994,6 +1091,15 @@ export class StatementSpecializer {
|
|
|
994
1091
|
}
|
|
995
1092
|
}
|
|
996
1093
|
|
|
1094
|
+
/** The one const a for…of declares. */
|
|
1095
|
+
const loopDeclaration = (node: ts.ForOfStatement): ts.VariableDeclaration => {
|
|
1096
|
+
const declaration = onlyDeclaration(node.initializer);
|
|
1097
|
+
if (!declaration) {
|
|
1098
|
+
throw new SpecializeError(node.initializer, "Declare one const in for…of.");
|
|
1099
|
+
}
|
|
1100
|
+
return declaration;
|
|
1101
|
+
};
|
|
1102
|
+
|
|
997
1103
|
/** The one variable a loop header declares; undefined when it declares none or several. */
|
|
998
1104
|
const onlyDeclaration = (
|
|
999
1105
|
initializer: ts.ForInitializer | undefined,
|
|
@@ -1005,6 +1111,80 @@ const onlyDeclaration = (
|
|
|
1005
1111
|
return others.length ? undefined : declaration;
|
|
1006
1112
|
};
|
|
1007
1113
|
|
|
1114
|
+
/**
|
|
1115
|
+
* How many times a counted loop's body can run: from its start, the bound its counter is compared
|
|
1116
|
+
* with and its step, when setup fixed them all. Undefined for a header that doesn't say.
|
|
1117
|
+
*/
|
|
1118
|
+
const iterationCount = (
|
|
1119
|
+
start: Value,
|
|
1120
|
+
comparison: ts.SyntaxKind,
|
|
1121
|
+
limit: Value,
|
|
1122
|
+
incrementor: ts.Expression,
|
|
1123
|
+
): number | undefined => {
|
|
1124
|
+
const first = known(start);
|
|
1125
|
+
const last = known(limit);
|
|
1126
|
+
const step = stepOf(incrementor);
|
|
1127
|
+
if (
|
|
1128
|
+
first === undefined ||
|
|
1129
|
+
last === undefined ||
|
|
1130
|
+
step === undefined ||
|
|
1131
|
+
!Number.isInteger(first) ||
|
|
1132
|
+
!Number.isInteger(last)
|
|
1133
|
+
) {
|
|
1134
|
+
return undefined;
|
|
1135
|
+
}
|
|
1136
|
+
const rising = step > 0;
|
|
1137
|
+
const exclusive = rising
|
|
1138
|
+
? ts.SyntaxKind.LessThanToken
|
|
1139
|
+
: ts.SyntaxKind.GreaterThanToken;
|
|
1140
|
+
const inclusive = rising
|
|
1141
|
+
? ts.SyntaxKind.LessThanEqualsToken
|
|
1142
|
+
: ts.SyntaxKind.GreaterThanEqualsToken;
|
|
1143
|
+
if (comparison !== exclusive && comparison !== inclusive) {
|
|
1144
|
+
return undefined;
|
|
1145
|
+
}
|
|
1146
|
+
return Number(
|
|
1147
|
+
loopIterations(
|
|
1148
|
+
BigInt(first),
|
|
1149
|
+
BigInt(last),
|
|
1150
|
+
comparison === exclusive,
|
|
1151
|
+
BigInt(step),
|
|
1152
|
+
),
|
|
1153
|
+
);
|
|
1154
|
+
};
|
|
1155
|
+
|
|
1156
|
+
/** A counted loop's step, as `i++`, `i--`, `i += 2` or `i -= 2` writes it. */
|
|
1157
|
+
const stepOf = (incrementor: ts.Expression): number | undefined => {
|
|
1158
|
+
if (
|
|
1159
|
+
ts.isPrefixUnaryExpression(incrementor) ||
|
|
1160
|
+
ts.isPostfixUnaryExpression(incrementor)
|
|
1161
|
+
) {
|
|
1162
|
+
return unitSteps.get(incrementor.operator);
|
|
1163
|
+
}
|
|
1164
|
+
if (
|
|
1165
|
+
!ts.isBinaryExpression(incrementor) ||
|
|
1166
|
+
!ts.isNumericLiteral(incrementor.right)
|
|
1167
|
+
) {
|
|
1168
|
+
return undefined;
|
|
1169
|
+
}
|
|
1170
|
+
const size = Number(incrementor.right.text);
|
|
1171
|
+
if (!Number.isInteger(size) || size <= 0) {
|
|
1172
|
+
return undefined;
|
|
1173
|
+
}
|
|
1174
|
+
const sign = compoundSteps.get(incrementor.operatorToken.kind);
|
|
1175
|
+
return sign === undefined ? undefined : sign * size;
|
|
1176
|
+
};
|
|
1177
|
+
|
|
1178
|
+
const unitSteps = new Map<ts.SyntaxKind, number>([
|
|
1179
|
+
[ts.SyntaxKind.PlusPlusToken, 1],
|
|
1180
|
+
[ts.SyntaxKind.MinusMinusToken, -1],
|
|
1181
|
+
]);
|
|
1182
|
+
|
|
1183
|
+
const compoundSteps = new Map<ts.SyntaxKind, number>([
|
|
1184
|
+
[ts.SyntaxKind.PlusEqualsToken, 1],
|
|
1185
|
+
[ts.SyntaxKind.MinusEqualsToken, -1],
|
|
1186
|
+
]);
|
|
1187
|
+
|
|
1008
1188
|
const labelText = (labels: readonly string[]): string =>
|
|
1009
1189
|
labels.map((label) => `${label}: `).join("");
|
|
1010
1190
|
|
|
@@ -1021,11 +1201,20 @@ const innermostLabelled = (
|
|
|
1021
1201
|
|
|
1022
1202
|
/** Whether a break or continue sits in the statement, outside functions it declares. */
|
|
1023
1203
|
const containsJump = (node: ts.Node): boolean =>
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1204
|
+
occursOutsideFunctions(
|
|
1205
|
+
node,
|
|
1206
|
+
(inner) => ts.isBreakStatement(inner) || ts.isContinueStatement(inner),
|
|
1207
|
+
);
|
|
1208
|
+
|
|
1209
|
+
/** `return x satisfies never`, which marks an end the program never reaches. */
|
|
1210
|
+
const unreachableEnd = (statement: ts.Statement): boolean =>
|
|
1211
|
+
ts.isReturnStatement(statement) &&
|
|
1212
|
+
statement.expression !== undefined &&
|
|
1213
|
+
ts.isSatisfiesExpression(statement.expression) &&
|
|
1214
|
+
statement.expression.type.kind === ts.SyntaxKind.NeverKeyword;
|
|
1027
1215
|
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1216
|
+
const unsupported = (node: ts.Statement): SpecializeError =>
|
|
1217
|
+
new SpecializeError(
|
|
1218
|
+
node,
|
|
1219
|
+
`${ts.SyntaxKind[node.kind]} is not supported in scan code.`,
|
|
1220
|
+
);
|