@tsplc/tmp-compiler 0.0.0-stage → 0.6.3
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 +6563 -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 +16198 -0
- package/dist/index.js.map +62 -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 +1090 -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-enums.ts +196 -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 +1344 -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 +2177 -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 +612 -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 +659 -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,211 @@
|
|
|
1
|
+
import { elementaryType, integerRange } from "@tsplc/iec";
|
|
2
|
+
import { reject } from "./normalization-error";
|
|
3
|
+
import { constantFits } from "./numeric";
|
|
4
|
+
import { lookup } from "./tables";
|
|
5
|
+
import { type PlcType, primitiveTypes } from "./types";
|
|
6
|
+
|
|
7
|
+
/** The finite storage budget: scalar elements in one layout or allocation, and nesting depth. */
|
|
8
|
+
export const maxScalarElements = 1_000_000;
|
|
9
|
+
/** `maxScalarElements` as diagnostics spell it. */
|
|
10
|
+
export const maxScalarElementsText = "one million";
|
|
11
|
+
export const maxNestingDepth = 64;
|
|
12
|
+
|
|
13
|
+
/** The largest capacity a bounded string may declare. */
|
|
14
|
+
export const maxStringCapacity = 65535;
|
|
15
|
+
|
|
16
|
+
// Array bounds and indexes are DINT.
|
|
17
|
+
const [firstIndex, lastIndex] = integerRange(elementaryType("DINT"));
|
|
18
|
+
|
|
19
|
+
const definitionIssue = (
|
|
20
|
+
type: PlcType,
|
|
21
|
+
types: ReadonlyMap<string, PlcType>,
|
|
22
|
+
): string | undefined => {
|
|
23
|
+
const canonical = primitiveTypes.find((item) => item.id === type.id);
|
|
24
|
+
if (canonical) {
|
|
25
|
+
return type.kind !== canonical.kind ||
|
|
26
|
+
("bits" in canonical &&
|
|
27
|
+
(!("bits" in type) || type.bits !== canonical.bits)) ||
|
|
28
|
+
("signed" in canonical &&
|
|
29
|
+
(!("signed" in type) || type.signed !== canonical.signed)) ||
|
|
30
|
+
("domain" in canonical &&
|
|
31
|
+
(!("domain" in type) || type.domain !== canonical.domain))
|
|
32
|
+
? `Conflicting primitive type ${type.id}`
|
|
33
|
+
: undefined;
|
|
34
|
+
}
|
|
35
|
+
switch (type.kind) {
|
|
36
|
+
case "record":
|
|
37
|
+
return !type.fields.length ||
|
|
38
|
+
new Set(type.fields.map((field) => field.name)).size !==
|
|
39
|
+
type.fields.length ||
|
|
40
|
+
type.fields.some((field) => !field.name || !types.has(field.type))
|
|
41
|
+
? `Invalid closed record layout ${type.id}`
|
|
42
|
+
: undefined;
|
|
43
|
+
case "array": {
|
|
44
|
+
if (
|
|
45
|
+
!types.has(type.element) ||
|
|
46
|
+
!Number.isSafeInteger(type.length) ||
|
|
47
|
+
type.length < 1 ||
|
|
48
|
+
type.length > maxScalarElements ||
|
|
49
|
+
!Number.isInteger(type.lowerBound) ||
|
|
50
|
+
BigInt(type.lowerBound) < firstIndex ||
|
|
51
|
+
BigInt(type.lowerBound + type.length - 1) > lastIndex
|
|
52
|
+
) {
|
|
53
|
+
return `Invalid fixed array layout ${type.id}`;
|
|
54
|
+
}
|
|
55
|
+
if (type.index === undefined) {
|
|
56
|
+
return undefined;
|
|
57
|
+
}
|
|
58
|
+
const index = types.get(type.index);
|
|
59
|
+
return index?.kind !== "enum" ||
|
|
60
|
+
index.members.length !== type.length ||
|
|
61
|
+
index.members.some(
|
|
62
|
+
(member) =>
|
|
63
|
+
Number(member.value) < type.lowerBound ||
|
|
64
|
+
Number(member.value) >= type.lowerBound + type.length,
|
|
65
|
+
)
|
|
66
|
+
? `Invalid enum-keyed array layout ${type.id}`
|
|
67
|
+
: undefined;
|
|
68
|
+
}
|
|
69
|
+
case "enum": {
|
|
70
|
+
const base = types.get(type.base);
|
|
71
|
+
return type.base !== "DINT" ||
|
|
72
|
+
base?.kind !== "integer" ||
|
|
73
|
+
!type.members.length ||
|
|
74
|
+
new Set(type.members.map((member) => member.name)).size !==
|
|
75
|
+
type.members.length ||
|
|
76
|
+
new Set(type.members.map((member) => member.value)).size !==
|
|
77
|
+
type.members.length ||
|
|
78
|
+
type.members.some(
|
|
79
|
+
(member) =>
|
|
80
|
+
!member.name ||
|
|
81
|
+
!constantFits({ kind: "integer", value: member.value }, base),
|
|
82
|
+
)
|
|
83
|
+
? `Invalid enum layout ${type.id}`
|
|
84
|
+
: undefined;
|
|
85
|
+
}
|
|
86
|
+
case "string":
|
|
87
|
+
return !Number.isInteger(type.capacity) ||
|
|
88
|
+
type.capacity < 1 ||
|
|
89
|
+
type.capacity > maxStringCapacity
|
|
90
|
+
? `Invalid bounded string layout ${type.id}`
|
|
91
|
+
: undefined;
|
|
92
|
+
case "block":
|
|
93
|
+
return !type.block ||
|
|
94
|
+
(type.output && types.get(type.output)?.kind !== "record")
|
|
95
|
+
? `Invalid block type ${type.id}`
|
|
96
|
+
: undefined;
|
|
97
|
+
default:
|
|
98
|
+
return `Unsupported core type definition ${type.id}`;
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
interface Layout {
|
|
103
|
+
size: number;
|
|
104
|
+
height: number;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** The part of the storage budget a layout exceeds. */
|
|
108
|
+
export type LayoutBudget = "size" | "nesting";
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Measures by-value layouts against the storage budget, each once, and hands the first that
|
|
112
|
+
* exceeds it to `exceeded`.
|
|
113
|
+
*/
|
|
114
|
+
const layoutMeasure = (
|
|
115
|
+
types: ReadonlyMap<string, PlcType>,
|
|
116
|
+
exceeded: (budget: LayoutBudget, message: string) => never,
|
|
117
|
+
) => {
|
|
118
|
+
const active = new Set<string>();
|
|
119
|
+
const layouts = new Map<string, Layout>();
|
|
120
|
+
const measure = (id: string, depth: number): Layout => {
|
|
121
|
+
if (active.has(id) || depth > maxNestingDepth) {
|
|
122
|
+
exceeded(
|
|
123
|
+
"nesting",
|
|
124
|
+
`Recursive or excessively nested by-value layout ${id}`,
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
const cached = layouts.get(id);
|
|
128
|
+
if (cached) {
|
|
129
|
+
if (depth + cached.height > maxNestingDepth) {
|
|
130
|
+
exceeded(
|
|
131
|
+
"nesting",
|
|
132
|
+
`Layout nesting exceeds ${maxNestingDepth} levels ${id}`,
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
return cached;
|
|
136
|
+
}
|
|
137
|
+
active.add(id);
|
|
138
|
+
const layout = composite(lookup(types, id, "type"), depth);
|
|
139
|
+
active.delete(id);
|
|
140
|
+
if (layout.size > maxScalarElements) {
|
|
141
|
+
exceeded(
|
|
142
|
+
"size",
|
|
143
|
+
`Layout exceeds ${maxScalarElementsText} scalar elements ${id}`,
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
if (depth + layout.height > maxNestingDepth) {
|
|
147
|
+
exceeded(
|
|
148
|
+
"nesting",
|
|
149
|
+
`Layout nesting exceeds ${maxNestingDepth} levels ${id}`,
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
layouts.set(id, layout);
|
|
153
|
+
return layout;
|
|
154
|
+
};
|
|
155
|
+
const composite = (type: PlcType, depth: number): Layout => {
|
|
156
|
+
if (type.kind === "array") {
|
|
157
|
+
const element = measure(type.element, depth + 1);
|
|
158
|
+
return { size: type.length * element.size, height: 1 + element.height };
|
|
159
|
+
}
|
|
160
|
+
if (type.kind === "record") {
|
|
161
|
+
const fields = type.fields.map((field) => measure(field.type, depth + 1));
|
|
162
|
+
return {
|
|
163
|
+
size: fields.reduce((sum, field) => sum + field.size, 0),
|
|
164
|
+
height:
|
|
165
|
+
1 +
|
|
166
|
+
fields.reduce((maximum, field) => Math.max(maximum, field.height), 0),
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
return { size: 1, height: 0 };
|
|
170
|
+
};
|
|
171
|
+
return (id: string) => measure(id, 0);
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Validates every layout edge once, before constants or places can traverse them, and measures
|
|
176
|
+
* each by-value layout against the storage budget. Returns each type's size in scalar elements.
|
|
177
|
+
*/
|
|
178
|
+
export const admitLayouts = (
|
|
179
|
+
types: ReadonlyMap<string, PlcType>,
|
|
180
|
+
): ReadonlyMap<string, number> => {
|
|
181
|
+
for (const type of types.values()) {
|
|
182
|
+
const issue = definitionIssue(type, types);
|
|
183
|
+
if (issue) {
|
|
184
|
+
reject(issue);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
const measure = layoutMeasure(types, (_budget, message) => reject(message));
|
|
188
|
+
return new Map([...types.keys()].map((id) => [id, measure(id).size]));
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
class BudgetExceeded {
|
|
192
|
+
constructor(readonly budget: LayoutBudget) {}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** Which part of the storage budget the by-value layout of `id` exceeds, if any. */
|
|
196
|
+
export const layoutIssue = (
|
|
197
|
+
types: ReadonlyMap<string, PlcType>,
|
|
198
|
+
id: string,
|
|
199
|
+
): LayoutBudget | undefined => {
|
|
200
|
+
try {
|
|
201
|
+
layoutMeasure(types, (budget) => {
|
|
202
|
+
throw new BudgetExceeded(budget);
|
|
203
|
+
})(id);
|
|
204
|
+
return undefined;
|
|
205
|
+
} catch (cause) {
|
|
206
|
+
if (cause instanceof BudgetExceeded) {
|
|
207
|
+
return cause.budget;
|
|
208
|
+
}
|
|
209
|
+
throw cause;
|
|
210
|
+
}
|
|
211
|
+
};
|
package/src/ir/link.ts
ADDED
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type CoreFunction,
|
|
3
|
+
type CoreFunctionBlock,
|
|
4
|
+
type CoreStatement,
|
|
5
|
+
calledRoutine,
|
|
6
|
+
directWrites,
|
|
7
|
+
nestedBodies,
|
|
8
|
+
routineVariables,
|
|
9
|
+
} from "./core-model";
|
|
10
|
+
import type { ModuleDeclarations } from "./declarations";
|
|
11
|
+
import { type PlcModule, telemetryOperands } from "./model";
|
|
12
|
+
import { reject } from "./normalization-error";
|
|
13
|
+
import { lookup } from "./tables";
|
|
14
|
+
import { instancedBlock, type PlcType } from "./types";
|
|
15
|
+
|
|
16
|
+
type Edges = ReadonlyMap<string, ReadonlySet<string>>;
|
|
17
|
+
type Routine = CoreFunction | CoreFunctionBlock;
|
|
18
|
+
|
|
19
|
+
// Static instances keep their child layouts even when a child has no scan call.
|
|
20
|
+
export function linkModule(
|
|
21
|
+
module: PlcModule,
|
|
22
|
+
declarations: ModuleDeclarations,
|
|
23
|
+
functions: CoreFunction[],
|
|
24
|
+
normalizedBlocks: CoreFunctionBlock[],
|
|
25
|
+
edges: Edges,
|
|
26
|
+
) {
|
|
27
|
+
const entries = checkPrograms(module, declarations);
|
|
28
|
+
const routines = new Map<string, Routine>(
|
|
29
|
+
[...functions, ...normalizedBlocks].map((fn) => [fn.id, fn]),
|
|
30
|
+
);
|
|
31
|
+
checkRecursion(routines, edges, entries, declarations);
|
|
32
|
+
checkLoopCounterWrites(module, declarations, routines, edges);
|
|
33
|
+
|
|
34
|
+
const reachable = reachableRoutines(
|
|
35
|
+
module,
|
|
36
|
+
declarations,
|
|
37
|
+
functions,
|
|
38
|
+
entries,
|
|
39
|
+
edges,
|
|
40
|
+
);
|
|
41
|
+
const linkedFunctions = functions.filter((fn) => reachable.has(fn.id));
|
|
42
|
+
const linkedBlocks = normalizedBlocks.filter((block) =>
|
|
43
|
+
reachable.has(block.id),
|
|
44
|
+
);
|
|
45
|
+
const usedTypes = usedTypeIds(
|
|
46
|
+
module,
|
|
47
|
+
declarations.types,
|
|
48
|
+
linkedFunctions,
|
|
49
|
+
linkedBlocks,
|
|
50
|
+
);
|
|
51
|
+
return {
|
|
52
|
+
types: module.types.filter((type) => usedTypes.has(type.id)),
|
|
53
|
+
variables: module.variables,
|
|
54
|
+
functions: linkedFunctions,
|
|
55
|
+
blocks: linkedBlocks,
|
|
56
|
+
programs: module.programs,
|
|
57
|
+
...(module.folded && { folded: module.folded }),
|
|
58
|
+
...(module.references && { references: module.references }),
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Checks each program's entry and name, and returns the entry routines. */
|
|
63
|
+
const checkPrograms = (
|
|
64
|
+
module: PlcModule,
|
|
65
|
+
declarations: ModuleDeclarations,
|
|
66
|
+
): ReadonlySet<string> => {
|
|
67
|
+
const names = new Set<string>();
|
|
68
|
+
const entries = new Set<string>();
|
|
69
|
+
for (const program of module.programs) {
|
|
70
|
+
declarations.claimId(program.id);
|
|
71
|
+
const entry = declarations.signatures.get(program.entry);
|
|
72
|
+
if (!entry || entry.owner || entry.parameters.length || entry.result) {
|
|
73
|
+
reject("Program entry must be a parameterless void helper.");
|
|
74
|
+
}
|
|
75
|
+
if (names.has(program.name)) {
|
|
76
|
+
reject(`Duplicate program ${program.name}.`);
|
|
77
|
+
}
|
|
78
|
+
names.add(program.name);
|
|
79
|
+
entries.add(program.entry);
|
|
80
|
+
}
|
|
81
|
+
if (!module.programs.length) {
|
|
82
|
+
reject("At least one program is required.");
|
|
83
|
+
}
|
|
84
|
+
return entries;
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
// Authored execution cycles are invalid even in unreachable routines. Static
|
|
88
|
+
// allocation has its own cycle check because it does not depend on scan calls.
|
|
89
|
+
const checkRecursion = (
|
|
90
|
+
routines: ReadonlyMap<string, Routine>,
|
|
91
|
+
edges: Edges,
|
|
92
|
+
entries: ReadonlySet<string>,
|
|
93
|
+
{ signatures }: ModuleDeclarations,
|
|
94
|
+
): void => {
|
|
95
|
+
const visited = new Set<string>();
|
|
96
|
+
const active = new Set<string>();
|
|
97
|
+
const visit = (id: string): void => {
|
|
98
|
+
if (active.has(id)) {
|
|
99
|
+
reject("Recursive scan calls are prohibited.", signatures.get(id)?.span);
|
|
100
|
+
}
|
|
101
|
+
if (visited.has(id)) {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
active.add(id);
|
|
105
|
+
for (const child of edges.get(id) ?? []) {
|
|
106
|
+
if (entries.has(child)) {
|
|
107
|
+
reject(
|
|
108
|
+
"Program entries cannot be called as ordinary helpers.",
|
|
109
|
+
signatures.get(id)?.span,
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
visit(child);
|
|
113
|
+
}
|
|
114
|
+
active.delete(id);
|
|
115
|
+
visited.add(id);
|
|
116
|
+
};
|
|
117
|
+
for (const id of routines.keys()) {
|
|
118
|
+
visit(id);
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
// A bounded loop can only reconstruct its counter if called helpers leave
|
|
123
|
+
// that storage alone. Include transitive calls, not just direct assignments.
|
|
124
|
+
const checkLoopCounterWrites = (
|
|
125
|
+
module: PlcModule,
|
|
126
|
+
{ types }: ModuleDeclarations,
|
|
127
|
+
routines: ReadonlyMap<string, Routine>,
|
|
128
|
+
edges: Edges,
|
|
129
|
+
): void => {
|
|
130
|
+
const variables = new Map(
|
|
131
|
+
[
|
|
132
|
+
...module.variables,
|
|
133
|
+
...[...routines.values()].flatMap(routineVariables),
|
|
134
|
+
].map((variable) => [variable.id, variable]),
|
|
135
|
+
);
|
|
136
|
+
const writes = new Map<string, Set<string>>();
|
|
137
|
+
const routineWrites = (id: string): Set<string> => {
|
|
138
|
+
const cached = writes.get(id);
|
|
139
|
+
if (cached) {
|
|
140
|
+
return cached;
|
|
141
|
+
}
|
|
142
|
+
const result = new Set<string>();
|
|
143
|
+
const inspect = (body: readonly CoreStatement[]) => {
|
|
144
|
+
for (const item of body) {
|
|
145
|
+
for (const root of directWrites(item)) {
|
|
146
|
+
result.add(root);
|
|
147
|
+
}
|
|
148
|
+
for (const nested of nestedBodies(item)) {
|
|
149
|
+
inspect(nested);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
inspect(lookup(routines, id, "routine").body);
|
|
154
|
+
for (const child of edges.get(id) ?? []) {
|
|
155
|
+
for (const root of routineWrites(child)) {
|
|
156
|
+
result.add(root);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
writes.set(id, result);
|
|
160
|
+
return result;
|
|
161
|
+
};
|
|
162
|
+
const checkCalls = (
|
|
163
|
+
body: readonly CoreStatement[],
|
|
164
|
+
counters: ReadonlySet<string>,
|
|
165
|
+
) => {
|
|
166
|
+
for (const item of body) {
|
|
167
|
+
const called = calledRoutine(item, (id) => variables.get(id), types);
|
|
168
|
+
if (
|
|
169
|
+
called &&
|
|
170
|
+
[...counters].some((counter) => routineWrites(called).has(counter))
|
|
171
|
+
) {
|
|
172
|
+
reject(
|
|
173
|
+
"A called routine may mutate an active loop counter.",
|
|
174
|
+
item.span,
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
const nestedCounters =
|
|
178
|
+
item.kind === "for" ? new Set([...counters, item.counter]) : counters;
|
|
179
|
+
for (const nested of nestedBodies(item)) {
|
|
180
|
+
checkCalls(nested, nestedCounters);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
for (const routine of routines.values()) {
|
|
185
|
+
checkCalls(routine.body, new Set());
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
const reachableRoutines = (
|
|
190
|
+
module: PlcModule,
|
|
191
|
+
{ signatures, blocks, types }: ModuleDeclarations,
|
|
192
|
+
functions: readonly CoreFunction[],
|
|
193
|
+
entries: ReadonlySet<string>,
|
|
194
|
+
edges: Edges,
|
|
195
|
+
): ReadonlySet<string> => {
|
|
196
|
+
const reachable = new Set<string>();
|
|
197
|
+
const link = (id: string): void => {
|
|
198
|
+
if (reachable.has(id)) {
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
reachable.add(id);
|
|
202
|
+
for (const child of edges.get(id) ?? []) {
|
|
203
|
+
link(child);
|
|
204
|
+
}
|
|
205
|
+
const owner = signatures.get(id)?.owner;
|
|
206
|
+
if (owner) {
|
|
207
|
+
link(owner.block);
|
|
208
|
+
}
|
|
209
|
+
for (const method of functions.filter((fn) => fn.owner?.block === id)) {
|
|
210
|
+
link(method.id);
|
|
211
|
+
}
|
|
212
|
+
for (const instance of blocks
|
|
213
|
+
.get(id)
|
|
214
|
+
?.state.filter((variable) => variable.storage === "instance") ?? []) {
|
|
215
|
+
const type = instancedBlock(instance.type, types);
|
|
216
|
+
if (type) {
|
|
217
|
+
link(type.block);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
for (const entry of entries) {
|
|
222
|
+
link(entry);
|
|
223
|
+
}
|
|
224
|
+
for (const variable of module.variables) {
|
|
225
|
+
const type = instancedBlock(variable.type, types);
|
|
226
|
+
if (type) {
|
|
227
|
+
link(type.block);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
return reachable;
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
const usedTypeIds = (
|
|
234
|
+
module: PlcModule,
|
|
235
|
+
types: ReadonlyMap<string, PlcType>,
|
|
236
|
+
linkedFunctions: readonly CoreFunction[],
|
|
237
|
+
linkedBlocks: readonly CoreFunctionBlock[],
|
|
238
|
+
): ReadonlySet<string> => {
|
|
239
|
+
const usedTypes = new Set<string>();
|
|
240
|
+
const useType = (id: string): void => {
|
|
241
|
+
if (usedTypes.has(id)) {
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
usedTypes.add(id);
|
|
245
|
+
const type = lookup(types, id, "type");
|
|
246
|
+
if (type.kind === "record") {
|
|
247
|
+
for (const field of type.fields) {
|
|
248
|
+
useType(field.type);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
if (type.kind === "array") {
|
|
252
|
+
useType(type.element);
|
|
253
|
+
if (type.index) {
|
|
254
|
+
useType(type.index);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
if (type.kind === "enum") {
|
|
258
|
+
useType(type.base);
|
|
259
|
+
}
|
|
260
|
+
if (type.kind === "block" && type.output) {
|
|
261
|
+
useType(type.output);
|
|
262
|
+
}
|
|
263
|
+
};
|
|
264
|
+
// Primitive definitions also support backend-generated numeric helpers.
|
|
265
|
+
types.forEach((type) => {
|
|
266
|
+
if (
|
|
267
|
+
["bool", "integer", "bits", "float", "duration", "calendar"].includes(
|
|
268
|
+
type.kind,
|
|
269
|
+
)
|
|
270
|
+
) {
|
|
271
|
+
useType(type.id);
|
|
272
|
+
}
|
|
273
|
+
});
|
|
274
|
+
for (const variable of module.variables) {
|
|
275
|
+
useType(variable.type);
|
|
276
|
+
}
|
|
277
|
+
for (const routine of [...linkedFunctions, ...linkedBlocks]) {
|
|
278
|
+
if (routine.result) {
|
|
279
|
+
useType(routine.result);
|
|
280
|
+
}
|
|
281
|
+
for (const variable of routineVariables(routine)) {
|
|
282
|
+
useType(variable.type);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
// A telemetry property can be its type's only use, such as an enum field the elaborator folded.
|
|
286
|
+
const useTelemetryTypes = (body: readonly CoreStatement[]): void => {
|
|
287
|
+
for (const item of body) {
|
|
288
|
+
if (item.kind === "telemetry") {
|
|
289
|
+
for (const operand of telemetryOperands(item)) {
|
|
290
|
+
useType(operand.type);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
for (const nested of nestedBodies(item)) {
|
|
294
|
+
useTelemetryTypes(nested);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
};
|
|
298
|
+
for (const routine of [...linkedFunctions, ...linkedBlocks]) {
|
|
299
|
+
useTelemetryTypes(routine.body);
|
|
300
|
+
}
|
|
301
|
+
return usedTypes;
|
|
302
|
+
};
|