@tsplc/tmp-compiler 0.0.0-stage → 0.6.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +41 -2
- package/dist/cli.js +2325 -0
- package/dist/cli.js.map +26 -0
- package/dist/clock.js +11 -0
- package/dist/clock.js.map +11 -0
- package/dist/codesys.js +6558 -0
- package/dist/codesys.js.map +51 -0
- package/dist/collections.js +399 -0
- package/dist/collections.js.map +10 -0
- package/dist/index.js +16076 -0
- package/dist/index.js.map +61 -0
- package/dist/ir.js +3948 -0
- package/dist/ir.js.map +35 -0
- package/dist/retain.js +10 -0
- package/dist/retain.js.map +10 -0
- package/dist/signals.js +115 -0
- package/dist/signals.js.map +10 -0
- package/dist/telemetry.js +121 -0
- package/dist/telemetry.js.map +12 -0
- package/package.json +122 -4
- package/src/codesys/config.ts +50 -0
- package/src/codesys/debug.ts +655 -0
- package/src/codesys/emit.ts +185 -0
- package/src/codesys/endpoints.ts +111 -0
- package/src/codesys/engineering.ts +96 -0
- package/src/codesys/explain/expressions.ts +445 -0
- package/src/codesys/explain/flags.ts +134 -0
- package/src/codesys/explain/parameters.ts +53 -0
- package/src/codesys/explain/rows.ts +182 -0
- package/src/codesys/explain/tables.ts +79 -0
- package/src/codesys/explain/tasks.ts +68 -0
- package/src/codesys/explain/writes.ts +497 -0
- package/src/codesys/explain.ts +149 -0
- package/src/codesys/expressions.ts +100 -0
- package/src/codesys/global-lists.ts +284 -0
- package/src/codesys/global-names.ts +66 -0
- package/src/codesys/http.ts +1078 -0
- package/src/codesys/index.ts +13 -0
- package/src/codesys/instrumentation.ts +574 -0
- package/src/codesys/io-paths.ts +149 -0
- package/src/codesys/loops.ts +101 -0
- package/src/codesys/manifest.ts +684 -0
- package/src/codesys/names.ts +58 -0
- package/src/codesys/numeric.ts +516 -0
- package/src/codesys/plan.ts +688 -0
- package/src/codesys/programs.ts +138 -0
- package/src/codesys/publication.ts +99 -0
- package/src/codesys/required.ts +8 -0
- package/src/codesys/routines.ts +247 -0
- package/src/codesys/scan-clock.ts +103 -0
- package/src/codesys/scheduling.ts +96 -0
- package/src/codesys/service-channels.ts +24 -0
- package/src/codesys/sources.ts +118 -0
- package/src/codesys/st-files.ts +219 -0
- package/src/codesys/statements.ts +409 -0
- package/src/codesys/storage.ts +150 -0
- package/src/codesys/symbols.ts +240 -0
- package/src/codesys/task-access.ts +186 -0
- package/src/codesys/telemetry-settings.ts +19 -0
- package/src/codesys/telemetry.ts +852 -0
- package/src/codesys/types.ts +310 -0
- package/src/codesys/vendor.ts +113 -0
- package/src/frontend/analyzer.ts +779 -0
- package/src/frontend/async.ts +530 -0
- package/src/frontend/blocks.ts +578 -0
- package/src/frontend/configuration.ts +83 -0
- package/src/frontend/const-functions.ts +204 -0
- package/src/frontend/control-flow.ts +787 -0
- package/src/frontend/cooperative.ts +114 -0
- package/src/frontend/coroutines.ts +1336 -0
- package/src/frontend/dialect-names.ts +104 -0
- package/src/frontend/elaborate/array-searches.ts +224 -0
- package/src/frontend/elaborate/assignments.ts +343 -0
- package/src/frontend/elaborate/calls.ts +1342 -0
- package/src/frontend/elaborate/capture-protocol.ts +237 -0
- package/src/frontend/elaborate/capture.ts +388 -0
- package/src/frontend/elaborate/declarations.ts +124 -0
- package/src/frontend/elaborate/dialect.ts +473 -0
- package/src/frontend/elaborate/expressions.ts +2171 -0
- package/src/frontend/elaborate/filtered-arrays.ts +643 -0
- package/src/frontend/elaborate/frame.ts +145 -0
- package/src/frontend/elaborate/generics.ts +123 -0
- package/src/frontend/elaborate/graph.ts +141 -0
- package/src/frontend/elaborate/imports.ts +105 -0
- package/src/frontend/elaborate/index.ts +851 -0
- package/src/frontend/elaborate/initializers.ts +235 -0
- package/src/frontend/elaborate/iterators.ts +1541 -0
- package/src/frontend/elaborate/layouts.ts +1025 -0
- package/src/frontend/elaborate/model.ts +478 -0
- package/src/frontend/elaborate/names.ts +134 -0
- package/src/frontend/elaborate/reduce.ts +900 -0
- package/src/frontend/elaborate/references.ts +727 -0
- package/src/frontend/elaborate/retention.ts +284 -0
- package/src/frontend/elaborate/specialize.ts +1137 -0
- package/src/frontend/elaborate/statements.ts +1220 -0
- package/src/frontend/elaborate/storage.ts +836 -0
- package/src/frontend/elaborate/time-constructors.ts +34 -0
- package/src/frontend/elaborate/values.ts +200 -0
- package/src/frontend/elaborate/writes.ts +415 -0
- package/src/frontend/expressions.ts +1382 -0
- package/src/frontend/generated-names.ts +95 -0
- package/src/frontend/intrinsics.ts +382 -0
- package/src/frontend/lowering-key.ts +23 -0
- package/src/frontend/module-graph.ts +156 -0
- package/src/frontend/positions.ts +359 -0
- package/src/frontend/promise-chains.ts +872 -0
- package/src/frontend/promises.ts +419 -0
- package/src/frontend/routines.ts +239 -0
- package/src/frontend/sdk.ts +187 -0
- package/src/frontend/services.ts +609 -0
- package/src/frontend/statements.ts +871 -0
- package/src/frontend/tasks.ts +526 -0
- package/src/frontend/telemetry.ts +174 -0
- package/src/frontend/types.ts +1073 -0
- package/src/frontend/typescript.ts +651 -0
- package/src/index.ts +9 -0
- package/src/ir/aggregates.ts +291 -0
- package/src/ir/allocation.ts +96 -0
- package/src/ir/conditions.ts +855 -0
- package/src/ir/core-model.ts +289 -0
- package/src/ir/declarations.ts +281 -0
- package/src/ir/diagnostics.ts +68 -0
- package/src/ir/domains.ts +151 -0
- package/src/ir/fold.ts +279 -0
- package/src/ir/identity.ts +19 -0
- package/src/ir/index.ts +137 -0
- package/src/ir/job-errors.ts +69 -0
- package/src/ir/layouts.ts +211 -0
- package/src/ir/link.ts +302 -0
- package/src/ir/model.ts +394 -0
- package/src/ir/normalization-error.ts +10 -0
- package/src/ir/normalize.ts +52 -0
- package/src/ir/numeric.ts +163 -0
- package/src/ir/operands.ts +160 -0
- package/src/ir/ranges.ts +420 -0
- package/src/ir/routine.ts +1116 -0
- package/src/ir/scalars.ts +453 -0
- package/src/ir/share.ts +790 -0
- package/src/ir/short-circuit.ts +140 -0
- package/src/ir/tables.ts +111 -0
- package/src/ir/temporaries.ts +138 -0
- package/src/ir/types.ts +151 -0
- package/src/project/artifacts.ts +424 -0
- package/src/project/cli.ts +211 -0
- package/src/project/debugger.ts +111 -0
- package/src/project/deployments.ts +387 -0
- package/src/project/flags.ts +14 -0
- package/src/project/git.ts +35 -0
- package/src/project/plant.ts +629 -0
- package/src/project/project-commands.ts +507 -0
- package/src/project/project.ts +559 -0
- package/src/project/release.ts +152 -0
- package/src/project/server-check.ts +54 -0
- package/src/project/server.ts +165 -0
- package/src/project/terminal.ts +45 -0
- package/src/runtime/async.ts +207 -0
- package/src/runtime/launch.ts +59 -0
- package/src/runtime/main.ts +59 -0
- package/src/runtime/missing-file.ts +3 -0
- package/src/runtime/process.ts +177 -0
- package/src/runtime/program.ts +109 -0
- package/src/runtime/retain.ts +108 -0
- package/src/runtime/runtime.ts +324 -0
- package/src/runtime/settings.ts +27 -0
- package/src/runtime/storage.ts +456 -0
- package/src/runtime/symbols.ts +97 -0
- package/src/runtime/telemetry.ts +154 -0
- package/src/runtime/transport.ts +168 -0
- package/stdlib/clock.ts +8 -0
- package/stdlib/collections.ts +534 -0
- package/stdlib/plc-time.ts +2 -0
- package/stdlib/retain.ts +4 -0
- package/stdlib/signals.ts +142 -0
- package/stdlib/telemetry.ts +89 -0
- package/types/async.d.ts +87 -0
- package/types/clock.d.ts +8 -0
- package/types/config.d.ts +2 -0
- package/types/data-types.d.ts +31 -0
- package/types/dialect.d.ts +49 -0
- package/types/globals.d.ts +10 -0
- package/types/index.d.ts +212 -0
- package/types/retain.d.ts +12 -0
- package/types/telemetry.d.ts +47 -0
|
@@ -0,0 +1,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
|
+
};
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import {
|
|
2
|
+
admitCore,
|
|
3
|
+
type CoreExpression,
|
|
4
|
+
type CoreFunction,
|
|
5
|
+
type CoreFunctionBlock,
|
|
6
|
+
type CoreStatement,
|
|
7
|
+
isTemporary,
|
|
8
|
+
mapBodies,
|
|
9
|
+
nestedBodies,
|
|
10
|
+
type VerifiedCoreModule,
|
|
11
|
+
} from "./core-model";
|
|
12
|
+
import {
|
|
13
|
+
mapExpression,
|
|
14
|
+
mapStatementExpressions,
|
|
15
|
+
statementExpressions,
|
|
16
|
+
subexpressions,
|
|
17
|
+
} from "./operands";
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Writes a temporary's value where it is read, when nothing can happen in between: the temporary
|
|
21
|
+
* is assigned once, as a whole, and read once, by the statement right after the assignment, in
|
|
22
|
+
* an expression that statement evaluates before it writes or calls anything. Loop bounds and
|
|
23
|
+
* switch values are left alone: ST evaluates them more than once.
|
|
24
|
+
*
|
|
25
|
+
* Only a load, a constant or a BOOL moves. An arithmetic value keeps its temporary, which
|
|
26
|
+
* stores it at its own type: written into a wider expression, ST could compute it wider.
|
|
27
|
+
*/
|
|
28
|
+
export function forwardTemporaries(
|
|
29
|
+
core: VerifiedCoreModule,
|
|
30
|
+
): VerifiedCoreModule {
|
|
31
|
+
const functions = core.functions.map((fn): CoreFunction => forward(fn));
|
|
32
|
+
const blocks = core.blocks.map((block): CoreFunctionBlock => forward(block));
|
|
33
|
+
return admitCore({ ...core, functions, blocks });
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function forward<Routine extends CoreFunction | CoreFunctionBlock>(
|
|
37
|
+
routine: Routine,
|
|
38
|
+
): Routine {
|
|
39
|
+
const temporaries = new Set(
|
|
40
|
+
routine.locals
|
|
41
|
+
.filter((local) => isTemporary(routine.id, local))
|
|
42
|
+
.map((local) => local.id),
|
|
43
|
+
);
|
|
44
|
+
if (!temporaries.size) {
|
|
45
|
+
return routine;
|
|
46
|
+
}
|
|
47
|
+
const writes = new Map<string, number>();
|
|
48
|
+
const reads = new Map<string, number>();
|
|
49
|
+
const count = (counts: Map<string, number>, root: string) =>
|
|
50
|
+
counts.set(root, (counts.get(root) ?? 0) + 1);
|
|
51
|
+
const expression = (value: CoreExpression): void => {
|
|
52
|
+
if (value.kind === "load") {
|
|
53
|
+
count(reads, value.root);
|
|
54
|
+
}
|
|
55
|
+
if (value.kind === "borrow") {
|
|
56
|
+
// A borrowed temporary is written through its reference.
|
|
57
|
+
count(writes, value.place.root);
|
|
58
|
+
count(reads, value.place.root);
|
|
59
|
+
}
|
|
60
|
+
subexpressions(value).forEach(expression);
|
|
61
|
+
};
|
|
62
|
+
const statements = (items: readonly CoreStatement[]): void => {
|
|
63
|
+
for (const item of items) {
|
|
64
|
+
if (item.kind === "assign") {
|
|
65
|
+
count(writes, item.root);
|
|
66
|
+
}
|
|
67
|
+
if (item.kind === "for") {
|
|
68
|
+
count(writes, item.counter);
|
|
69
|
+
}
|
|
70
|
+
if (item.kind === "invoke" && item.destination !== undefined) {
|
|
71
|
+
count(writes, item.destination);
|
|
72
|
+
}
|
|
73
|
+
statementExpressions(item).forEach(expression);
|
|
74
|
+
nestedBodies(item).forEach(statements);
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
statements(routine.body);
|
|
78
|
+
const movable = (
|
|
79
|
+
item: CoreStatement,
|
|
80
|
+
): item is Extract<CoreStatement, { kind: "assign" }> =>
|
|
81
|
+
item.kind === "assign" &&
|
|
82
|
+
!item.path?.length &&
|
|
83
|
+
temporaries.has(item.root) &&
|
|
84
|
+
writes.get(item.root) === 1 &&
|
|
85
|
+
reads.get(item.root) === 1 &&
|
|
86
|
+
(item.value.kind === "load" ||
|
|
87
|
+
item.value.kind === "constant" ||
|
|
88
|
+
item.value.type === "BOOL");
|
|
89
|
+
|
|
90
|
+
const forwarded = new Set<string>();
|
|
91
|
+
const rewrite = (items: readonly CoreStatement[]): CoreStatement[] => {
|
|
92
|
+
const result = items.map((item) => mapBodies(item, rewrite));
|
|
93
|
+
// From the end, so a value can move through a chain of temporaries.
|
|
94
|
+
for (let index = result.length - 2; index >= 0; index--) {
|
|
95
|
+
const item = result[index];
|
|
96
|
+
const next = result[index + 1];
|
|
97
|
+
if (!item || !next || !movable(item) || !readsOwn(next, item.root)) {
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
result.splice(index, 2, substitute(next, item.root, item.value));
|
|
101
|
+
forwarded.add(item.root);
|
|
102
|
+
}
|
|
103
|
+
return result;
|
|
104
|
+
};
|
|
105
|
+
const body = rewrite(routine.body);
|
|
106
|
+
if (!forwarded.size) {
|
|
107
|
+
return routine;
|
|
108
|
+
}
|
|
109
|
+
return {
|
|
110
|
+
...routine,
|
|
111
|
+
locals: routine.locals.filter((local) => !forwarded.has(local.id)),
|
|
112
|
+
body,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Whether a statement reads `root` in what it evaluates once, before it writes or calls. */
|
|
117
|
+
function readsOwn(item: CoreStatement, root: string): boolean {
|
|
118
|
+
if (item.kind === "for" || item.kind === "switch") {
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
const reads = (value: CoreExpression): boolean =>
|
|
122
|
+
(value.kind === "load" && value.root === root) ||
|
|
123
|
+
subexpressions(value).some(reads);
|
|
124
|
+
return statementExpressions(item).some(reads);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** A statement with its read of `root` replaced by `value`. */
|
|
128
|
+
function substitute(
|
|
129
|
+
item: CoreStatement,
|
|
130
|
+
root: string,
|
|
131
|
+
value: CoreExpression,
|
|
132
|
+
): CoreStatement {
|
|
133
|
+
const replace = (expression: CoreExpression): CoreExpression =>
|
|
134
|
+
expression.kind === "load" && expression.root === root
|
|
135
|
+
? value
|
|
136
|
+
: mapExpression(expression, replace);
|
|
137
|
+
return mapStatementExpressions(item, replace);
|
|
138
|
+
}
|
package/src/ir/types.ts
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type ElementaryType,
|
|
3
|
+
integerRange as elementaryIntegerRange,
|
|
4
|
+
elementaryTypes,
|
|
5
|
+
} from "@tsplc/iec";
|
|
6
|
+
|
|
7
|
+
export type TypeId = string;
|
|
8
|
+
export type SymbolId = string;
|
|
9
|
+
|
|
10
|
+
export interface Field {
|
|
11
|
+
name: string;
|
|
12
|
+
type: TypeId;
|
|
13
|
+
readonly: boolean;
|
|
14
|
+
mode?: "inOut";
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export type PlcType =
|
|
18
|
+
| { id: TypeId; kind: "bool" }
|
|
19
|
+
| { id: TypeId; kind: "integer"; bits: 8 | 16 | 32 | 64; signed: boolean }
|
|
20
|
+
| { id: TypeId; kind: "bits"; bits: 8 | 16 | 32 | 64 }
|
|
21
|
+
| { id: TypeId; kind: "float"; bits: 32 | 64 }
|
|
22
|
+
| { id: TypeId; kind: "duration" }
|
|
23
|
+
| { id: TypeId; kind: "calendar"; domain: "date" | "timeOfDay" | "dateTime" }
|
|
24
|
+
| { id: TypeId; kind: "string"; capacity: number; encoding: "utf8" | "utf16" }
|
|
25
|
+
| {
|
|
26
|
+
id: TypeId;
|
|
27
|
+
kind: "array";
|
|
28
|
+
element: TypeId;
|
|
29
|
+
length: number;
|
|
30
|
+
lowerBound: number;
|
|
31
|
+
/** `Record<Enum, T>`: the enum whose contiguous member values are the bounds and the only indexes. */
|
|
32
|
+
index?: TypeId;
|
|
33
|
+
}
|
|
34
|
+
| { id: TypeId; kind: "record"; fields: readonly Field[] }
|
|
35
|
+
| {
|
|
36
|
+
id: TypeId;
|
|
37
|
+
kind: "enum";
|
|
38
|
+
base: TypeId;
|
|
39
|
+
/** `text` is the string a plain TypeScript program's literal union member stands for. */
|
|
40
|
+
members: readonly { name: string; value: string; text?: string }[];
|
|
41
|
+
}
|
|
42
|
+
| { id: TypeId; kind: "block"; block: SymbolId; output?: TypeId };
|
|
43
|
+
|
|
44
|
+
/** The block allocated by a type: a block itself, or a fixed array of one. */
|
|
45
|
+
export const instancedBlock = (
|
|
46
|
+
id: TypeId,
|
|
47
|
+
types: ReadonlyMap<TypeId, PlcType>,
|
|
48
|
+
): Extract<PlcType, { kind: "block" }> | undefined => {
|
|
49
|
+
const type = types.get(id);
|
|
50
|
+
const element = type?.kind === "array" ? types.get(type.element) : type;
|
|
51
|
+
return element?.kind === "block" ? element : undefined;
|
|
52
|
+
};
|
|
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
|
+
|
|
80
|
+
const primitiveType = (type: ElementaryType): PlcType[] => {
|
|
81
|
+
switch (type.family) {
|
|
82
|
+
case "bool":
|
|
83
|
+
return [{ id: type.name, kind: "bool" }];
|
|
84
|
+
case "integer":
|
|
85
|
+
return [
|
|
86
|
+
{
|
|
87
|
+
id: type.name,
|
|
88
|
+
kind: "integer",
|
|
89
|
+
bits: type.bits,
|
|
90
|
+
signed: type.signed,
|
|
91
|
+
},
|
|
92
|
+
];
|
|
93
|
+
case "bits":
|
|
94
|
+
return [{ id: type.name, kind: "bits", bits: type.bits }];
|
|
95
|
+
case "real":
|
|
96
|
+
return [{ id: type.name, kind: "float", bits: type.bits }];
|
|
97
|
+
case "duration":
|
|
98
|
+
return type.bits === 32 ? [{ id: type.name, kind: "duration" }] : [];
|
|
99
|
+
case "date":
|
|
100
|
+
case "timeOfDay":
|
|
101
|
+
case "dateTime":
|
|
102
|
+
return type.bits === 32
|
|
103
|
+
? [{ id: type.name, kind: "calendar", domain: type.family }]
|
|
104
|
+
: [];
|
|
105
|
+
case "char":
|
|
106
|
+
case "string":
|
|
107
|
+
return [];
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
type IntegerType = Extract<PlcType, { kind: "integer" | "bits" }>;
|
|
112
|
+
|
|
113
|
+
export const integerType = (type: PlcType): type is IntegerType =>
|
|
114
|
+
type.kind === "integer" || type.kind === "bits";
|
|
115
|
+
|
|
116
|
+
const integerKindRank = (type: IntegerType) => {
|
|
117
|
+
if (type.kind === "bits") {
|
|
118
|
+
return 2;
|
|
119
|
+
}
|
|
120
|
+
return type.signed ? 0 : 1;
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
const elementaryPrimitives = elementaryTypes.flatMap(primitiveType);
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* The elementary types a program uses by name. Date and time types are the 32-bit ones;
|
|
127
|
+
* strings are sized where they are declared, and characters are not modelled.
|
|
128
|
+
*
|
|
129
|
+
* Integers are grouped by width (signed, unsigned, bit string), because the debug manifest
|
|
130
|
+
* lists types in this order and the build id written into the emitted ST hashes it.
|
|
131
|
+
*/
|
|
132
|
+
export const primitiveTypes: readonly PlcType[] = [
|
|
133
|
+
...elementaryPrimitives.filter((type) => type.kind === "bool"),
|
|
134
|
+
...elementaryPrimitives
|
|
135
|
+
.filter(integerType)
|
|
136
|
+
.sort(
|
|
137
|
+
(left, right) =>
|
|
138
|
+
left.bits - right.bits ||
|
|
139
|
+
integerKindRank(left) - integerKindRank(right),
|
|
140
|
+
),
|
|
141
|
+
...elementaryPrimitives.filter(
|
|
142
|
+
(type) => type.kind !== "bool" && !integerType(type),
|
|
143
|
+
),
|
|
144
|
+
];
|
|
145
|
+
|
|
146
|
+
export function integerRange(type: IntegerType): readonly [bigint, bigint] {
|
|
147
|
+
return elementaryIntegerRange({
|
|
148
|
+
bits: type.bits,
|
|
149
|
+
signed: type.kind === "integer" && type.signed,
|
|
150
|
+
});
|
|
151
|
+
}
|