@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
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type CoreExpression,
|
|
3
|
+
type CoreStatement,
|
|
4
|
+
wholeAssign,
|
|
5
|
+
} from "./core-model";
|
|
6
|
+
import type { SourceSpan } from "./diagnostics";
|
|
7
|
+
|
|
8
|
+
type Assign = Extract<CoreStatement, { kind: "assign" }>;
|
|
9
|
+
type If = Extract<CoreStatement, { kind: "if" }>;
|
|
10
|
+
|
|
11
|
+
/** An operand that runs only on one branch: the statements that evaluate it, then its value. */
|
|
12
|
+
export interface BranchOperand {
|
|
13
|
+
readonly statements: readonly CoreStatement[];
|
|
14
|
+
readonly value: CoreExpression;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** Where a lowered `&&`, `||` or `?:` stores its result: a BOOL temporary for `&&` and `||`. */
|
|
18
|
+
interface Result {
|
|
19
|
+
readonly root: string;
|
|
20
|
+
readonly type: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const store = (result: Result, value: CoreExpression, span: SourceSpan) =>
|
|
24
|
+
({ kind: "assign", root: result.root, value, span }) satisfies Assign;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* `left && right` as `t := left; IF t THEN …; t := right END_IF`, and `||` the same under
|
|
28
|
+
* `IF NOT t`, so `right` runs only when `left` does not decide.
|
|
29
|
+
*/
|
|
30
|
+
export const lowerLogical = ({
|
|
31
|
+
operator,
|
|
32
|
+
result,
|
|
33
|
+
left,
|
|
34
|
+
right,
|
|
35
|
+
span,
|
|
36
|
+
}: {
|
|
37
|
+
operator: "and" | "or";
|
|
38
|
+
result: Result;
|
|
39
|
+
left: CoreExpression;
|
|
40
|
+
right: BranchOperand;
|
|
41
|
+
span: SourceSpan;
|
|
42
|
+
}): readonly [CoreStatement, CoreStatement] => {
|
|
43
|
+
const tested: CoreExpression = {
|
|
44
|
+
kind: "load",
|
|
45
|
+
root: result.root,
|
|
46
|
+
type: result.type,
|
|
47
|
+
span,
|
|
48
|
+
};
|
|
49
|
+
return [
|
|
50
|
+
store(result, left, span),
|
|
51
|
+
{
|
|
52
|
+
kind: "if",
|
|
53
|
+
condition:
|
|
54
|
+
operator === "and"
|
|
55
|
+
? tested
|
|
56
|
+
: {
|
|
57
|
+
kind: "unary",
|
|
58
|
+
operator: "not",
|
|
59
|
+
operand: tested,
|
|
60
|
+
type: "BOOL",
|
|
61
|
+
span,
|
|
62
|
+
},
|
|
63
|
+
// biome-ignore lint/suspicious/noThenProperty: PLC branch data is an array, not a thenable callback.
|
|
64
|
+
then: [...right.statements, store(result, right.value, span)],
|
|
65
|
+
else: [],
|
|
66
|
+
span,
|
|
67
|
+
},
|
|
68
|
+
];
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
/** The `&&` or `||` that `item` and `next` are, as `lowerLogical` writes it. */
|
|
72
|
+
export const loweredLogical = (
|
|
73
|
+
item: CoreStatement,
|
|
74
|
+
next: CoreStatement | undefined,
|
|
75
|
+
):
|
|
76
|
+
| { operator: "and" | "or"; left: Assign; right: Assign; test: If }
|
|
77
|
+
| undefined => {
|
|
78
|
+
if (
|
|
79
|
+
item.kind !== "assign" ||
|
|
80
|
+
item.path?.length ||
|
|
81
|
+
next?.kind !== "if" ||
|
|
82
|
+
next.else.length
|
|
83
|
+
) {
|
|
84
|
+
return undefined;
|
|
85
|
+
}
|
|
86
|
+
const { condition } = next;
|
|
87
|
+
const negated = condition.kind === "unary" && condition.operator === "not";
|
|
88
|
+
const tested = negated ? condition.operand : condition;
|
|
89
|
+
const right = next.then.at(-1);
|
|
90
|
+
if (
|
|
91
|
+
tested.kind !== "load" ||
|
|
92
|
+
tested.path?.length ||
|
|
93
|
+
tested.root !== item.root ||
|
|
94
|
+
!wholeAssign(right, item.root)
|
|
95
|
+
) {
|
|
96
|
+
return undefined;
|
|
97
|
+
}
|
|
98
|
+
return { operator: negated ? "or" : "and", left: item, right, test: next };
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
/** `condition ? then : else` as `IF condition THEN …; t := then ELSE …; t := else END_IF`. */
|
|
102
|
+
export const lowerSelect = ({
|
|
103
|
+
result,
|
|
104
|
+
condition,
|
|
105
|
+
whenTrue,
|
|
106
|
+
whenFalse,
|
|
107
|
+
span,
|
|
108
|
+
}: {
|
|
109
|
+
result: Result;
|
|
110
|
+
condition: CoreExpression;
|
|
111
|
+
whenTrue: BranchOperand;
|
|
112
|
+
whenFalse: BranchOperand;
|
|
113
|
+
span: SourceSpan;
|
|
114
|
+
}): CoreStatement => ({
|
|
115
|
+
kind: "if",
|
|
116
|
+
condition,
|
|
117
|
+
// biome-ignore lint/suspicious/noThenProperty: PLC branch data is an array, not a thenable callback.
|
|
118
|
+
then: [...whenTrue.statements, store(result, whenTrue.value, span)],
|
|
119
|
+
else: [...whenFalse.statements, store(result, whenFalse.value, span)],
|
|
120
|
+
span,
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
/** The `?:` that `item` is, as `lowerSelect` writes it. */
|
|
124
|
+
export const loweredSelect = (
|
|
125
|
+
item: CoreStatement,
|
|
126
|
+
): { root: string; test: If; then: Assign; else: Assign } | undefined => {
|
|
127
|
+
if (item.kind !== "if") {
|
|
128
|
+
return undefined;
|
|
129
|
+
}
|
|
130
|
+
const then = item.then.at(-1);
|
|
131
|
+
const otherwise = item.else.at(-1);
|
|
132
|
+
if (
|
|
133
|
+
then?.kind !== "assign" ||
|
|
134
|
+
!wholeAssign(then, then.root) ||
|
|
135
|
+
!wholeAssign(otherwise, then.root)
|
|
136
|
+
) {
|
|
137
|
+
return undefined;
|
|
138
|
+
}
|
|
139
|
+
return { root: then.root, test: item, then, else: otherwise };
|
|
140
|
+
};
|
package/src/ir/tables.ts
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type CoreFunction,
|
|
3
|
+
type CoreFunctionBlock,
|
|
4
|
+
type CorePath,
|
|
5
|
+
type CoreStatement,
|
|
6
|
+
calledRoutine,
|
|
7
|
+
routineVariables,
|
|
8
|
+
type VerifiedCoreModule,
|
|
9
|
+
} from "./core-model";
|
|
10
|
+
import type { Variable } from "./model";
|
|
11
|
+
import type { PlcType } from "./types";
|
|
12
|
+
|
|
13
|
+
/** The entry `id` names in a table that admission closed over every reference, so a miss is a compiler bug. */
|
|
14
|
+
export const lookup = <Key, T>(
|
|
15
|
+
table: ReadonlyMap<Key, T>,
|
|
16
|
+
id: Key,
|
|
17
|
+
kind: string,
|
|
18
|
+
): T => {
|
|
19
|
+
const entry = table.get(id);
|
|
20
|
+
if (entry === undefined) {
|
|
21
|
+
throw new Error(`Unknown ${kind} ${String(id)}.`);
|
|
22
|
+
}
|
|
23
|
+
return entry;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export interface CoreTables {
|
|
27
|
+
readonly types: ReadonlyMap<string, PlcType>;
|
|
28
|
+
readonly functions: ReadonlyMap<string, CoreFunction>;
|
|
29
|
+
readonly blocks: ReadonlyMap<string, CoreFunctionBlock>;
|
|
30
|
+
/** Module storage, and each routine's parameters, locals and members. */
|
|
31
|
+
readonly variables: ReadonlyMap<string, Variable>;
|
|
32
|
+
readonly typeOf: (id: string) => PlcType;
|
|
33
|
+
readonly functionOf: (id: string) => CoreFunction;
|
|
34
|
+
readonly blockOf: (id: string) => CoreFunctionBlock;
|
|
35
|
+
readonly variableOf: (id: string) => Variable;
|
|
36
|
+
/** The routine a statement runs, as `calledRoutine` names it. */
|
|
37
|
+
readonly calledRoutineOf: (
|
|
38
|
+
item: CoreStatement,
|
|
39
|
+
) => CoreFunction | CoreFunctionBlock | undefined;
|
|
40
|
+
readonly updatedBlock: (
|
|
41
|
+
item: Extract<CoreStatement, { kind: "blockUpdate" }>,
|
|
42
|
+
) => CoreFunctionBlock;
|
|
43
|
+
/** The type a path selects from a root type through record fields and array elements. */
|
|
44
|
+
readonly placeType: (rootType: string, path: CorePath) => PlcType | undefined;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const built = new WeakMap<VerifiedCoreModule, CoreTables>();
|
|
48
|
+
|
|
49
|
+
const buildTables = (core: VerifiedCoreModule): CoreTables => {
|
|
50
|
+
const types = new Map(core.types.map((type) => [type.id, type]));
|
|
51
|
+
const functions = new Map(core.functions.map((fn) => [fn.id, fn]));
|
|
52
|
+
const blocks = new Map(core.blocks.map((block) => [block.id, block]));
|
|
53
|
+
const variables = new Map(
|
|
54
|
+
[
|
|
55
|
+
...core.variables,
|
|
56
|
+
...core.functions.flatMap(routineVariables),
|
|
57
|
+
...core.blocks.flatMap(routineVariables),
|
|
58
|
+
].map((variable) => [variable.id, variable]),
|
|
59
|
+
);
|
|
60
|
+
const functionOf = (id: string) => lookup(functions, id, "function");
|
|
61
|
+
const blockOf = (id: string) => lookup(blocks, id, "block");
|
|
62
|
+
const variableOf = (id: string) => lookup(variables, id, "variable");
|
|
63
|
+
return {
|
|
64
|
+
types,
|
|
65
|
+
functions,
|
|
66
|
+
blocks,
|
|
67
|
+
variables,
|
|
68
|
+
typeOf: (id) => lookup(types, id, "type"),
|
|
69
|
+
functionOf,
|
|
70
|
+
blockOf,
|
|
71
|
+
variableOf,
|
|
72
|
+
calledRoutineOf: (item) => {
|
|
73
|
+
const called = calledRoutine(item, variableOf, types);
|
|
74
|
+
if (called === undefined) {
|
|
75
|
+
return undefined;
|
|
76
|
+
}
|
|
77
|
+
return item.kind === "invoke" ? functionOf(called) : blockOf(called);
|
|
78
|
+
},
|
|
79
|
+
updatedBlock: (item) => {
|
|
80
|
+
const called = calledRoutine(item, variableOf, types);
|
|
81
|
+
if (called === undefined) {
|
|
82
|
+
throw new Error(`${item.instance} is not a block instance.`);
|
|
83
|
+
}
|
|
84
|
+
return blockOf(called);
|
|
85
|
+
},
|
|
86
|
+
placeType: (rootType, path) =>
|
|
87
|
+
path.reduce<PlcType | undefined>((type, segment) => {
|
|
88
|
+
if (segment.kind === "field" && type?.kind === "record") {
|
|
89
|
+
const field = type.fields.find(
|
|
90
|
+
(candidate) => candidate.name === segment.name,
|
|
91
|
+
);
|
|
92
|
+
return field && types.get(field.type);
|
|
93
|
+
}
|
|
94
|
+
if (segment.kind === "index" && type?.kind === "array") {
|
|
95
|
+
return types.get(type.element);
|
|
96
|
+
}
|
|
97
|
+
return undefined;
|
|
98
|
+
}, types.get(rootType)),
|
|
99
|
+
};
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
/** A verified module's types, routines and variables by id, built once per module: it is frozen. */
|
|
103
|
+
export const coreTables = (core: VerifiedCoreModule): CoreTables => {
|
|
104
|
+
const known = built.get(core);
|
|
105
|
+
if (known) {
|
|
106
|
+
return known;
|
|
107
|
+
}
|
|
108
|
+
const tables = buildTables(core);
|
|
109
|
+
built.set(core, tables);
|
|
110
|
+
return tables;
|
|
111
|
+
};
|
package/src/ir/temporaries.ts
CHANGED
|
@@ -1,15 +1,20 @@
|
|
|
1
1
|
import {
|
|
2
|
+
admitCore,
|
|
2
3
|
type CoreExpression,
|
|
3
4
|
type CoreFunction,
|
|
4
5
|
type CoreFunctionBlock,
|
|
5
|
-
type CorePath,
|
|
6
6
|
type CoreStatement,
|
|
7
|
+
isTemporary,
|
|
8
|
+
mapBodies,
|
|
7
9
|
nestedBodies,
|
|
8
10
|
type VerifiedCoreModule,
|
|
9
11
|
} from "./core-model";
|
|
10
|
-
import {
|
|
11
|
-
|
|
12
|
-
|
|
12
|
+
import {
|
|
13
|
+
mapExpression,
|
|
14
|
+
mapStatementExpressions,
|
|
15
|
+
statementExpressions,
|
|
16
|
+
subexpressions,
|
|
17
|
+
} from "./operands";
|
|
13
18
|
|
|
14
19
|
/**
|
|
15
20
|
* Writes a temporary's value where it is read, when nothing can happen in between: the temporary
|
|
@@ -84,7 +89,7 @@ function forward<Routine extends CoreFunction | CoreFunctionBlock>(
|
|
|
84
89
|
|
|
85
90
|
const forwarded = new Set<string>();
|
|
86
91
|
const rewrite = (items: readonly CoreStatement[]): CoreStatement[] => {
|
|
87
|
-
const result = items.map((item) =>
|
|
92
|
+
const result = items.map((item) => mapBodies(item, rewrite));
|
|
88
93
|
// From the end, so a value can move through a chain of temporaries.
|
|
89
94
|
for (let index = result.length - 2; index >= 0; index--) {
|
|
90
95
|
const item = result[index];
|
|
@@ -119,118 +124,15 @@ function readsOwn(item: CoreStatement, root: string): boolean {
|
|
|
119
124
|
return statementExpressions(item).some(reads);
|
|
120
125
|
}
|
|
121
126
|
|
|
122
|
-
/** A statement with each nested body rewritten. */
|
|
123
|
-
function withBodies(
|
|
124
|
-
item: CoreStatement,
|
|
125
|
-
rewrite: (items: readonly CoreStatement[]) => CoreStatement[],
|
|
126
|
-
): CoreStatement {
|
|
127
|
-
switch (item.kind) {
|
|
128
|
-
case "if":
|
|
129
|
-
return {
|
|
130
|
-
...item,
|
|
131
|
-
// biome-ignore lint/suspicious/noThenProperty: PLC branch data is an array, not a thenable callback.
|
|
132
|
-
then: rewrite(item.then),
|
|
133
|
-
else: rewrite(item.else),
|
|
134
|
-
};
|
|
135
|
-
case "switch":
|
|
136
|
-
return {
|
|
137
|
-
...item,
|
|
138
|
-
cases: item.cases.map((branch) => ({
|
|
139
|
-
...branch,
|
|
140
|
-
body: rewrite(branch.body),
|
|
141
|
-
})),
|
|
142
|
-
fallback: rewrite(item.fallback),
|
|
143
|
-
};
|
|
144
|
-
case "for":
|
|
145
|
-
return { ...item, body: rewrite(item.body) };
|
|
146
|
-
default:
|
|
147
|
-
return item;
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
|
|
151
127
|
/** A statement with its read of `root` replaced by `value`. */
|
|
152
128
|
function substitute(
|
|
153
129
|
item: CoreStatement,
|
|
154
130
|
root: string,
|
|
155
131
|
value: CoreExpression,
|
|
156
132
|
): CoreStatement {
|
|
157
|
-
const replace = (expression: CoreExpression): CoreExpression =>
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
case "constant":
|
|
163
|
-
return expression;
|
|
164
|
-
case "load":
|
|
165
|
-
return expression.path
|
|
166
|
-
? { ...expression, path: replacePath(expression.path) }
|
|
167
|
-
: expression;
|
|
168
|
-
case "borrow":
|
|
169
|
-
return {
|
|
170
|
-
...expression,
|
|
171
|
-
place: {
|
|
172
|
-
...expression.place,
|
|
173
|
-
path: replacePath(expression.place.path),
|
|
174
|
-
},
|
|
175
|
-
};
|
|
176
|
-
case "unary":
|
|
177
|
-
case "convert":
|
|
178
|
-
return { ...expression, operand: replace(expression.operand) };
|
|
179
|
-
case "binary":
|
|
180
|
-
return {
|
|
181
|
-
...expression,
|
|
182
|
-
left: replace(expression.left),
|
|
183
|
-
right: replace(expression.right),
|
|
184
|
-
};
|
|
185
|
-
}
|
|
186
|
-
};
|
|
187
|
-
const replacePath = (path: CorePath): CorePath =>
|
|
188
|
-
path.map((segment) =>
|
|
189
|
-
segment.kind === "field"
|
|
190
|
-
? segment
|
|
191
|
-
: { kind: "index", value: replace(segment.value) },
|
|
192
|
-
);
|
|
193
|
-
switch (item.kind) {
|
|
194
|
-
case "assign":
|
|
195
|
-
return {
|
|
196
|
-
...item,
|
|
197
|
-
...(item.path && { path: replacePath(item.path) }),
|
|
198
|
-
value: replace(item.value),
|
|
199
|
-
};
|
|
200
|
-
case "if":
|
|
201
|
-
return { ...item, condition: replace(item.condition) };
|
|
202
|
-
case "invoke":
|
|
203
|
-
return {
|
|
204
|
-
...item,
|
|
205
|
-
...(item.index && { index: replace(item.index) }),
|
|
206
|
-
arguments: item.arguments.map(replace),
|
|
207
|
-
};
|
|
208
|
-
case "blockUpdate":
|
|
209
|
-
return {
|
|
210
|
-
...item,
|
|
211
|
-
...(item.index && { index: replace(item.index) }),
|
|
212
|
-
inputs: Object.fromEntries(
|
|
213
|
-
Object.entries(item.inputs).map(([name, input]) => [
|
|
214
|
-
name,
|
|
215
|
-
replace(input),
|
|
216
|
-
]),
|
|
217
|
-
),
|
|
218
|
-
};
|
|
219
|
-
case "return":
|
|
220
|
-
return item.value ? { ...item, value: replace(item.value) } : item;
|
|
221
|
-
case "telemetry":
|
|
222
|
-
return {
|
|
223
|
-
...item,
|
|
224
|
-
...(item.record.kind === "exception" && {
|
|
225
|
-
record: { ...item.record, code: replace(item.record.code) },
|
|
226
|
-
}),
|
|
227
|
-
properties: item.properties.map((property) => ({
|
|
228
|
-
...property,
|
|
229
|
-
value: replace(property.value),
|
|
230
|
-
...(property.length && { length: replace(property.length) }),
|
|
231
|
-
})),
|
|
232
|
-
};
|
|
233
|
-
default:
|
|
234
|
-
return item;
|
|
235
|
-
}
|
|
133
|
+
const replace = (expression: CoreExpression): CoreExpression =>
|
|
134
|
+
expression.kind === "load" && expression.root === root
|
|
135
|
+
? value
|
|
136
|
+
: mapExpression(expression, replace);
|
|
137
|
+
return mapStatementExpressions(item, replace);
|
|
236
138
|
}
|
package/src/ir/types.ts
CHANGED
|
@@ -51,6 +51,32 @@ export const instancedBlock = (
|
|
|
51
51
|
return element?.kind === "block" ? element : undefined;
|
|
52
52
|
};
|
|
53
53
|
|
|
54
|
+
/**
|
|
55
|
+
* Whether a type holds a value: neither a block instance nor a record with a reference field,
|
|
56
|
+
* through every field and element.
|
|
57
|
+
*/
|
|
58
|
+
export const isValueType = (
|
|
59
|
+
id: TypeId,
|
|
60
|
+
types: ReadonlyMap<TypeId, PlcType>,
|
|
61
|
+
): boolean => {
|
|
62
|
+
const seen = new Set<TypeId>();
|
|
63
|
+
const valueType = (current: TypeId): boolean => {
|
|
64
|
+
if (seen.has(current)) {
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
seen.add(current);
|
|
68
|
+
const type = types.get(current);
|
|
69
|
+
return Boolean(
|
|
70
|
+
type &&
|
|
71
|
+
type.kind !== "block" &&
|
|
72
|
+
(type.kind !== "record" ||
|
|
73
|
+
type.fields.every((field) => !field.mode && valueType(field.type))) &&
|
|
74
|
+
(type.kind !== "array" || valueType(type.element)),
|
|
75
|
+
);
|
|
76
|
+
};
|
|
77
|
+
return valueType(id);
|
|
78
|
+
};
|
|
79
|
+
|
|
54
80
|
const primitiveType = (type: ElementaryType): PlcType[] => {
|
|
55
81
|
switch (type.family) {
|
|
56
82
|
case "bool":
|
|
@@ -82,12 +108,12 @@ const primitiveType = (type: ElementaryType): PlcType[] => {
|
|
|
82
108
|
}
|
|
83
109
|
};
|
|
84
110
|
|
|
85
|
-
type
|
|
111
|
+
type IntegerType = Extract<PlcType, { kind: "integer" | "bits" }>;
|
|
86
112
|
|
|
87
|
-
const
|
|
113
|
+
export const integerType = (type: PlcType): type is IntegerType =>
|
|
88
114
|
type.kind === "integer" || type.kind === "bits";
|
|
89
115
|
|
|
90
|
-
const integerKindRank = (type:
|
|
116
|
+
const integerKindRank = (type: IntegerType) => {
|
|
91
117
|
if (type.kind === "bits") {
|
|
92
118
|
return 2;
|
|
93
119
|
}
|
|
@@ -106,20 +132,18 @@ const elementaryPrimitives = elementaryTypes.flatMap(primitiveType);
|
|
|
106
132
|
export const primitiveTypes: readonly PlcType[] = [
|
|
107
133
|
...elementaryPrimitives.filter((type) => type.kind === "bool"),
|
|
108
134
|
...elementaryPrimitives
|
|
109
|
-
.filter(
|
|
135
|
+
.filter(integerType)
|
|
110
136
|
.sort(
|
|
111
137
|
(left, right) =>
|
|
112
138
|
left.bits - right.bits ||
|
|
113
139
|
integerKindRank(left) - integerKindRank(right),
|
|
114
140
|
),
|
|
115
141
|
...elementaryPrimitives.filter(
|
|
116
|
-
(type) => type.kind !== "bool" && !
|
|
142
|
+
(type) => type.kind !== "bool" && !integerType(type),
|
|
117
143
|
),
|
|
118
144
|
];
|
|
119
145
|
|
|
120
|
-
export function integerRange(
|
|
121
|
-
type: Extract<PlcType, { kind: "integer" | "bits" }>,
|
|
122
|
-
): readonly [bigint, bigint] {
|
|
146
|
+
export function integerRange(type: IntegerType): readonly [bigint, bigint] {
|
|
123
147
|
return elementaryIntegerRange({
|
|
124
148
|
bits: type.bits,
|
|
125
149
|
signed: type.kind === "integer" && type.signed,
|