@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,291 @@
|
|
|
1
|
+
import { isComparison } from "@tsplc/iec/arithmetic";
|
|
2
|
+
import {
|
|
3
|
+
calendarZero,
|
|
4
|
+
comparedType,
|
|
5
|
+
domainConstantFits,
|
|
6
|
+
domainIssue,
|
|
7
|
+
} from "./domains";
|
|
8
|
+
import type { Constant, Expression, Place, Variable } from "./model";
|
|
9
|
+
import { constantFits, type IntegerInterval } from "./numeric";
|
|
10
|
+
import { interval } from "./ranges";
|
|
11
|
+
import { scalarIssue } from "./scalars";
|
|
12
|
+
import { lookup } from "./tables";
|
|
13
|
+
import type { PlcType } from "./types";
|
|
14
|
+
|
|
15
|
+
export function aggregate(type: PlcType | undefined): boolean {
|
|
16
|
+
return type?.kind === "record" || type?.kind === "array";
|
|
17
|
+
}
|
|
18
|
+
export function valueFits(
|
|
19
|
+
value: Constant,
|
|
20
|
+
type: PlcType,
|
|
21
|
+
types: ReadonlyMap<string, PlcType>,
|
|
22
|
+
): boolean {
|
|
23
|
+
if (type.kind === "duration" || type.kind === "calendar") {
|
|
24
|
+
return domainConstantFits(value, type);
|
|
25
|
+
}
|
|
26
|
+
if (type.kind === "enum") {
|
|
27
|
+
return (
|
|
28
|
+
value.kind === "integer" &&
|
|
29
|
+
type.members.some((member) => member.value === value.value)
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
if (type.kind === "string") {
|
|
33
|
+
return (
|
|
34
|
+
value.kind === "string" &&
|
|
35
|
+
!value.value.includes("\0") &&
|
|
36
|
+
(type.encoding === "utf8"
|
|
37
|
+
? new TextEncoder().encode(value.value).length <= type.capacity
|
|
38
|
+
: value.value.length <= type.capacity)
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
if (type.kind === "array") {
|
|
42
|
+
return (
|
|
43
|
+
value.kind === "array" &&
|
|
44
|
+
value.items.length === type.length &&
|
|
45
|
+
value.items.every((item) =>
|
|
46
|
+
valueFits(item, lookup(types, type.element, "type"), types),
|
|
47
|
+
)
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
if (type.kind === "record") {
|
|
51
|
+
return (
|
|
52
|
+
value.kind === "record" &&
|
|
53
|
+
Object.keys(value.fields).length === type.fields.length &&
|
|
54
|
+
type.fields.every((field) => {
|
|
55
|
+
const fieldValue = Object.hasOwn(value.fields, field.name)
|
|
56
|
+
? value.fields[field.name]
|
|
57
|
+
: undefined;
|
|
58
|
+
return (
|
|
59
|
+
fieldValue !== undefined &&
|
|
60
|
+
valueFits(fieldValue, lookup(types, field.type, "type"), types)
|
|
61
|
+
);
|
|
62
|
+
})
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
return constantFits(value, type);
|
|
66
|
+
}
|
|
67
|
+
export function defaultValue(
|
|
68
|
+
typeId: string,
|
|
69
|
+
types: ReadonlyMap<string, PlcType>,
|
|
70
|
+
): Constant {
|
|
71
|
+
const type = types.get(typeId);
|
|
72
|
+
if (!type) {
|
|
73
|
+
throw new Error("Unknown default-value type");
|
|
74
|
+
}
|
|
75
|
+
if (type.kind === "bool") {
|
|
76
|
+
return { kind: "bool", value: false };
|
|
77
|
+
}
|
|
78
|
+
if (type.kind === "float") {
|
|
79
|
+
return { kind: "float", value: 0 };
|
|
80
|
+
}
|
|
81
|
+
if (type.kind === "string") {
|
|
82
|
+
return { kind: "string", value: "" };
|
|
83
|
+
}
|
|
84
|
+
if (type.kind === "enum") {
|
|
85
|
+
const member =
|
|
86
|
+
type.members.find((item) => item.value === "0") ?? type.members[0];
|
|
87
|
+
if (!member) {
|
|
88
|
+
throw new Error(`Enum ${type.id} has no members.`);
|
|
89
|
+
}
|
|
90
|
+
return { kind: "integer", value: member.value };
|
|
91
|
+
}
|
|
92
|
+
if (type.kind === "array") {
|
|
93
|
+
return {
|
|
94
|
+
kind: "array",
|
|
95
|
+
items: Array.from({ length: type.length }, () =>
|
|
96
|
+
defaultValue(type.element, types),
|
|
97
|
+
),
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
if (type.kind === "record") {
|
|
101
|
+
return {
|
|
102
|
+
kind: "record",
|
|
103
|
+
fields: Object.fromEntries(
|
|
104
|
+
type.fields.map((field) => [
|
|
105
|
+
field.name,
|
|
106
|
+
defaultValue(field.type, types),
|
|
107
|
+
]),
|
|
108
|
+
),
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
if (type.kind === "duration") {
|
|
112
|
+
return { kind: "duration", nanoseconds: "0" };
|
|
113
|
+
}
|
|
114
|
+
if (type.kind === "calendar") {
|
|
115
|
+
return { kind: "calendar", value: calendarZero[type.domain] };
|
|
116
|
+
}
|
|
117
|
+
if (type.kind === "integer" || type.kind === "bits") {
|
|
118
|
+
return { kind: "integer", value: "0" };
|
|
119
|
+
}
|
|
120
|
+
throw new Error("Unsupported default-value type");
|
|
121
|
+
}
|
|
122
|
+
export function placeType(
|
|
123
|
+
place: Place,
|
|
124
|
+
types: ReadonlyMap<string, PlcType>,
|
|
125
|
+
variables: ReadonlyMap<string, Variable>,
|
|
126
|
+
ranges: ReadonlyMap<string, IntegerInterval>,
|
|
127
|
+
writable = false,
|
|
128
|
+
): { type?: string; issue?: string } {
|
|
129
|
+
const root = variables.get(place.root);
|
|
130
|
+
if (!root) {
|
|
131
|
+
return { issue: "Unknown storage root." };
|
|
132
|
+
}
|
|
133
|
+
if (
|
|
134
|
+
writable &&
|
|
135
|
+
(root.storage === "input" ||
|
|
136
|
+
root.storage === "constant" ||
|
|
137
|
+
root.storage === "blockOutput" ||
|
|
138
|
+
(root.storage === "parameter" && root.mode !== "inOut") ||
|
|
139
|
+
(!place.path.length && !root.mutable))
|
|
140
|
+
) {
|
|
141
|
+
return {
|
|
142
|
+
issue:
|
|
143
|
+
"This storage is read-only here: copy it into a let and change the copy.",
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
return place.path.reduce<Walk>(
|
|
147
|
+
(walk, segment) =>
|
|
148
|
+
"issue" in walk
|
|
149
|
+
? walk
|
|
150
|
+
: segmentType(walk.type, segment, types, variables, ranges, writable),
|
|
151
|
+
{ type: root.type },
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
type Walk = { type: string } | { issue: string };
|
|
156
|
+
|
|
157
|
+
/** The type a path segment selects from `id`, or why it cannot. */
|
|
158
|
+
function segmentType(
|
|
159
|
+
id: string,
|
|
160
|
+
segment: Place["path"][number],
|
|
161
|
+
types: ReadonlyMap<string, PlcType>,
|
|
162
|
+
variables: ReadonlyMap<string, Variable>,
|
|
163
|
+
ranges: ReadonlyMap<string, IntegerInterval>,
|
|
164
|
+
writable: boolean,
|
|
165
|
+
): Walk {
|
|
166
|
+
const type = types.get(id);
|
|
167
|
+
if (segment.kind === "field") {
|
|
168
|
+
if (type?.kind === "block" && segment.name === "out" && type.output) {
|
|
169
|
+
return writable
|
|
170
|
+
? { issue: "Function block output views are read-only." }
|
|
171
|
+
: { type: type.output };
|
|
172
|
+
}
|
|
173
|
+
if (type?.kind !== "record") {
|
|
174
|
+
return { issue: "Field access requires a record." };
|
|
175
|
+
}
|
|
176
|
+
const field = type.fields.find((field) => field.name === segment.name);
|
|
177
|
+
if (!field) {
|
|
178
|
+
return { issue: "Unknown record field." };
|
|
179
|
+
}
|
|
180
|
+
if (writable && field.readonly) {
|
|
181
|
+
return { issue: "This field is read-only." };
|
|
182
|
+
}
|
|
183
|
+
return { type: field.type };
|
|
184
|
+
}
|
|
185
|
+
if (type?.kind !== "array") {
|
|
186
|
+
return { issue: "Index access requires a fixed array." };
|
|
187
|
+
}
|
|
188
|
+
// Enum values are closed, and admission proves an enum-keyed array's members are its bounds.
|
|
189
|
+
// An integer, such as a for…of counter, still needs its bounds proof.
|
|
190
|
+
if (type.index && segment.value.type === type.index) {
|
|
191
|
+
return { type: type.element };
|
|
192
|
+
}
|
|
193
|
+
const indexType = types.get(segment.value.type);
|
|
194
|
+
const bounds = interval(segment.value, types, variables, ranges);
|
|
195
|
+
if (
|
|
196
|
+
indexType?.kind !== "integer" ||
|
|
197
|
+
!bounds ||
|
|
198
|
+
bounds[0] < BigInt(type.lowerBound) ||
|
|
199
|
+
bounds[1] >= BigInt(type.lowerBound + type.length)
|
|
200
|
+
) {
|
|
201
|
+
return { issue: "Array index must be proved within the declared bounds." };
|
|
202
|
+
}
|
|
203
|
+
return { type: type.element };
|
|
204
|
+
}
|
|
205
|
+
export function aggregateIssue(
|
|
206
|
+
value: Expression,
|
|
207
|
+
types: ReadonlyMap<string, PlcType>,
|
|
208
|
+
): string | undefined {
|
|
209
|
+
const type = types.get(value.type);
|
|
210
|
+
if (!type) {
|
|
211
|
+
return "Unknown expression type.";
|
|
212
|
+
}
|
|
213
|
+
if (value.kind === "constant") {
|
|
214
|
+
return valueFits(value.value, type, types)
|
|
215
|
+
? undefined
|
|
216
|
+
: "Constant does not fit its closed PLC layout.";
|
|
217
|
+
}
|
|
218
|
+
if (value.kind === "record") {
|
|
219
|
+
return type.kind === "record" &&
|
|
220
|
+
Object.keys(value.fields).length === type.fields.length &&
|
|
221
|
+
type.fields.every(
|
|
222
|
+
(field) =>
|
|
223
|
+
Object.hasOwn(value.fields, field.name) &&
|
|
224
|
+
value.fields[field.name]?.type === field.type,
|
|
225
|
+
)
|
|
226
|
+
? undefined
|
|
227
|
+
: "Record expression differs from its layout.";
|
|
228
|
+
}
|
|
229
|
+
if (value.kind === "array") {
|
|
230
|
+
return type.kind === "array" &&
|
|
231
|
+
value.items.length === type.length &&
|
|
232
|
+
value.items.every((item) => item.type === type.element)
|
|
233
|
+
? undefined
|
|
234
|
+
: "Array expression differs from its layout.";
|
|
235
|
+
}
|
|
236
|
+
if (value.kind === "copy") {
|
|
237
|
+
return aggregate(type) && value.operand.type === value.type
|
|
238
|
+
? undefined
|
|
239
|
+
: "Copy requires one matching fixed aggregate.";
|
|
240
|
+
}
|
|
241
|
+
if (value.kind === "convert") {
|
|
242
|
+
const source = types.get(value.operand.type);
|
|
243
|
+
return type.kind === "string" &&
|
|
244
|
+
source?.kind === "string" &&
|
|
245
|
+
source.encoding === type.encoding &&
|
|
246
|
+
source.capacity <= type.capacity &&
|
|
247
|
+
value.policy === "exact"
|
|
248
|
+
? undefined
|
|
249
|
+
: "Unsupported aggregate/domain conversion.";
|
|
250
|
+
}
|
|
251
|
+
if (
|
|
252
|
+
value.kind === "load" ||
|
|
253
|
+
value.kind === "call" ||
|
|
254
|
+
value.kind === "conditional"
|
|
255
|
+
) {
|
|
256
|
+
return undefined;
|
|
257
|
+
}
|
|
258
|
+
if (value.kind === "binary" && isComparison(value.operator)) {
|
|
259
|
+
const compared = comparedType(value, types, ["enum", "string"]);
|
|
260
|
+
return compared &&
|
|
261
|
+
(compared.kind !== "enum" ||
|
|
262
|
+
["equal", "notEqual"].includes(value.operator))
|
|
263
|
+
? undefined
|
|
264
|
+
: "Comparison requires compatible scalar domains; object identity is unsupported.";
|
|
265
|
+
}
|
|
266
|
+
return "Operation is unsupported for this PLC domain.";
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
// Route a reduced expression to the checker that owns its PLC domain. A binary
|
|
270
|
+
// operation belongs to its operand domain, e.g. a TIME comparison yields BOOL.
|
|
271
|
+
export function expressionIssue(
|
|
272
|
+
value: Expression,
|
|
273
|
+
types: ReadonlyMap<string, PlcType>,
|
|
274
|
+
variables: ReadonlyMap<string, Variable>,
|
|
275
|
+
ranges: ReadonlyMap<string, IntegerInterval>,
|
|
276
|
+
): string | undefined {
|
|
277
|
+
const kinds = [
|
|
278
|
+
value.type,
|
|
279
|
+
...(value.kind === "binary" ? [value.left.type] : []),
|
|
280
|
+
].map((id) => types.get(id)?.kind ?? "");
|
|
281
|
+
if (kinds.some((kind) => ["duration", "calendar"].includes(kind))) {
|
|
282
|
+
return domainIssue(value, types);
|
|
283
|
+
}
|
|
284
|
+
if (
|
|
285
|
+
["copy", "record", "array"].includes(value.kind) ||
|
|
286
|
+
kinds.some((kind) => ["record", "array", "enum", "string"].includes(kind))
|
|
287
|
+
) {
|
|
288
|
+
return aggregateIssue(value, types);
|
|
289
|
+
}
|
|
290
|
+
return scalarIssue(value, types, variables, ranges);
|
|
291
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import type { ModuleDeclarations } from "./declarations";
|
|
2
|
+
import {
|
|
3
|
+
maxNestingDepth,
|
|
4
|
+
maxScalarElements,
|
|
5
|
+
maxScalarElementsText,
|
|
6
|
+
} from "./layouts";
|
|
7
|
+
import type { PlcModule } from "./model";
|
|
8
|
+
import { reject } from "./normalization-error";
|
|
9
|
+
import { lookup } from "./tables";
|
|
10
|
+
import { instancedBlock } from "./types";
|
|
11
|
+
|
|
12
|
+
interface Allocation {
|
|
13
|
+
size: number;
|
|
14
|
+
height: number;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function checkStaticAllocation(
|
|
18
|
+
module: PlcModule,
|
|
19
|
+
declarations: ModuleDeclarations,
|
|
20
|
+
): void {
|
|
21
|
+
const { types, blocks, layoutSizes } = declarations;
|
|
22
|
+
|
|
23
|
+
// Static allocation is a separate graph from scan calls: even an unused child
|
|
24
|
+
// consumes memory, and a recursive child cannot be allocated by value.
|
|
25
|
+
const payload = (id: string): number => lookup(layoutSizes, id, "layout");
|
|
26
|
+
|
|
27
|
+
const instance = (id: string): Allocation => {
|
|
28
|
+
const instanced = instancedBlock(id, types);
|
|
29
|
+
if (!instanced) {
|
|
30
|
+
throw new Error(`Type ${id} instantiates no block.`);
|
|
31
|
+
}
|
|
32
|
+
const block = allocation(instanced.block);
|
|
33
|
+
const type = lookup(types, id, "type");
|
|
34
|
+
return type.kind === "array"
|
|
35
|
+
? { size: type.length * block.size, height: block.height }
|
|
36
|
+
: block;
|
|
37
|
+
};
|
|
38
|
+
const allocations = new Map<string, Allocation>();
|
|
39
|
+
const allocating = new Set<string>();
|
|
40
|
+
const allocation = (id: string): Allocation => {
|
|
41
|
+
if (allocating.size >= maxNestingDepth) {
|
|
42
|
+
reject(
|
|
43
|
+
`Static block allocation nesting exceeds ${maxNestingDepth} levels.`,
|
|
44
|
+
blocks.get(id)?.span,
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
if (allocating.has(id)) {
|
|
48
|
+
reject("Recursive static block allocation.", blocks.get(id)?.span);
|
|
49
|
+
}
|
|
50
|
+
const cached = allocations.get(id);
|
|
51
|
+
if (cached) {
|
|
52
|
+
return cached;
|
|
53
|
+
}
|
|
54
|
+
allocating.add(id);
|
|
55
|
+
const block = lookup(blocks, id, "block");
|
|
56
|
+
const children = block.state
|
|
57
|
+
.filter((item) => item.storage === "instance")
|
|
58
|
+
.map((item) => instance(item.type));
|
|
59
|
+
const own = [
|
|
60
|
+
...block.inputs,
|
|
61
|
+
...block.outputs,
|
|
62
|
+
...block.state.filter((item) => item.storage !== "instance"),
|
|
63
|
+
].reduce(
|
|
64
|
+
(sum, item) => sum + (item.mode === "inOut" ? 1 : payload(item.type)),
|
|
65
|
+
1,
|
|
66
|
+
);
|
|
67
|
+
const size = own + children.reduce((sum, child) => sum + child.size, 0);
|
|
68
|
+
const height = children.length
|
|
69
|
+
? 1 + Math.max(...children.map((child) => child.height))
|
|
70
|
+
: 1;
|
|
71
|
+
if (size > maxScalarElements || height > maxNestingDepth) {
|
|
72
|
+
reject("Static block allocation exceeds its finite budget.", block.span);
|
|
73
|
+
}
|
|
74
|
+
allocating.delete(id);
|
|
75
|
+
const result = { size, height };
|
|
76
|
+
allocations.set(id, result);
|
|
77
|
+
return result;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
for (const block of module.blocks) {
|
|
81
|
+
allocation(block.id);
|
|
82
|
+
}
|
|
83
|
+
const staticPayload = module.variables.reduce((sum, variable) => {
|
|
84
|
+
return (
|
|
85
|
+
sum +
|
|
86
|
+
(instancedBlock(variable.type, types)
|
|
87
|
+
? instance(variable.type).size
|
|
88
|
+
: payload(variable.type))
|
|
89
|
+
);
|
|
90
|
+
}, 0);
|
|
91
|
+
if (staticPayload > maxScalarElements) {
|
|
92
|
+
reject(
|
|
93
|
+
`Static module storage exceeds ${maxScalarElementsText} scalar elements.`,
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
}
|