@tsplc/compiler 0.2.17 → 0.2.18
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 +2 -2
- package/dist/cli.js +920 -943
- package/dist/cli.js.map +13 -10
- package/dist/codesys.js +3893 -3352
- package/dist/codesys.js.map +29 -20
- package/dist/elaborate-capture.js +33 -17
- package/dist/elaborate-capture.js.map +3 -3
- package/dist/index.js +8787 -6855
- package/dist/index.js.map +40 -20
- package/dist/ir.js +1575 -1072
- package/dist/ir.js.map +21 -17
- package/dist/math.js.map +2 -2
- package/dist/project.js +154 -60
- package/dist/project.js.map +5 -5
- package/dist/telemetry.js +27 -9
- package/dist/telemetry.js.map +5 -5
- package/package.json +112 -112
- package/src/codesys/config.ts +50 -0
- package/src/codesys/debug.ts +670 -0
- package/src/codesys/emit.ts +142 -0
- package/src/codesys/engineering.ts +109 -0
- package/src/codesys/expressions.ts +92 -0
- package/src/codesys/global-lists.ts +250 -0
- package/src/codesys/global-names.ts +68 -0
- package/src/codesys/http.ts +890 -0
- package/src/codesys/index.ts +30 -0
- package/src/codesys/instrumentation.ts +430 -0
- package/src/codesys/io-paths.ts +140 -0
- package/src/codesys/manifest.ts +643 -0
- package/src/codesys/names.ts +81 -0
- package/src/codesys/numeric.ts +409 -0
- package/src/codesys/operands.ts +62 -0
- package/src/codesys/plan.ts +605 -0
- package/src/codesys/programs.ts +53 -0
- package/src/codesys/publication.ts +171 -0
- package/src/codesys/routines.ts +603 -0
- package/src/codesys/sources.ts +71 -0
- package/src/codesys/st-files.ts +115 -0
- package/src/codesys/storage.ts +97 -0
- package/src/codesys/symbols.ts +220 -0
- package/src/codesys/task-access.ts +320 -0
- package/src/codesys/telemetry.ts +732 -0
- package/src/codesys/types.ts +250 -0
- package/src/codesys/vendor.ts +150 -0
- package/src/frontend/analyzer.ts +794 -0
- package/src/frontend/async.ts +317 -1137
- package/src/frontend/blocks.ts +537 -403
- package/src/frontend/configuration.ts +176 -144
- package/src/frontend/const-functions.ts +183 -148
- package/src/frontend/cooperative.ts +73 -43
- package/src/frontend/coroutines.ts +1032 -0
- package/src/frontend/elaborate/calls.ts +535 -0
- package/src/frontend/elaborate/capture.ts +157 -101
- package/src/frontend/elaborate/dialect.ts +257 -0
- package/src/frontend/elaborate/expressions.ts +992 -0
- package/src/frontend/elaborate/frame.ts +53 -0
- package/src/frontend/elaborate/graph.ts +116 -71
- package/src/frontend/elaborate/imports.ts +70 -0
- package/src/frontend/elaborate/index.ts +435 -331
- package/src/frontend/elaborate/initializers.ts +198 -0
- package/src/frontend/elaborate/layouts.ts +591 -384
- package/src/frontend/elaborate/model.ts +264 -169
- package/src/frontend/elaborate/specialize.ts +343 -2586
- package/src/frontend/elaborate/statements.ts +606 -0
- package/src/frontend/elaborate/storage.ts +546 -0
- package/src/frontend/elaborate/values.ts +58 -0
- package/src/frontend/elaborate/writes.ts +100 -0
- package/src/frontend/exposure.ts +172 -140
- package/src/frontend/expressions.ts +1383 -0
- package/src/frontend/intrinsics.ts +337 -0
- package/src/frontend/module-graph.ts +89 -59
- package/src/frontend/positions.ts +271 -200
- package/src/frontend/promise-chains.ts +897 -0
- package/src/frontend/promises.ts +390 -747
- package/src/frontend/routines.ts +181 -0
- package/src/frontend/sdk.ts +82 -0
- package/src/frontend/services.ts +506 -372
- package/src/frontend/statements.ts +855 -0
- package/src/frontend/tasks.ts +471 -0
- package/src/frontend/telemetry.ts +174 -0
- package/src/frontend/types.ts +845 -551
- package/src/frontend/typescript.ts +323 -2543
- package/src/index.ts +5 -5
- package/src/ir/aggregates.ts +275 -0
- package/src/ir/allocation.ts +84 -0
- package/src/ir/core-model.ts +146 -0
- package/src/ir/declarations.ts +295 -0
- package/src/ir/diagnostics.ts +61 -0
- package/src/ir/domains.ts +152 -0
- package/src/ir/fold.ts +342 -0
- package/src/ir/index.ts +90 -0
- package/{ir/errors.ts → src/ir/job-errors.ts} +12 -10
- package/src/ir/layouts.ts +181 -0
- package/src/ir/link.ts +354 -0
- package/src/ir/model.ts +386 -0
- package/{ir → src/ir}/normalization-error.ts +4 -4
- package/src/ir/normalize.ts +70 -0
- package/src/ir/numeric.ts +171 -0
- package/src/ir/ranges.ts +256 -0
- package/src/ir/routine.ts +1163 -0
- package/src/ir/scalars.ts +321 -0
- package/src/ir/types.ts +127 -0
- package/src/project/artifacts.ts +342 -0
- package/src/project/cli.ts +177 -0
- package/src/project/debugger.ts +82 -0
- package/src/project/index.ts +6 -0
- package/src/project/plant.ts +467 -0
- package/src/project/project-commands.ts +372 -0
- package/src/project/project.ts +457 -0
- package/src/project/release.ts +153 -0
- package/src/project/server.ts +150 -0
- package/src/project/terminal.ts +32 -0
- package/stdlib/math.ts +14 -10
- package/stdlib/telemetry.ts +71 -47
- package/types/async.d.ts +46 -40
- package/types/data-types.d.ts +26 -26
- package/types/globals.d.ts +41 -38
- package/types/index.d.ts +96 -51
- package/types/plc.d.ts +3 -4
- package/types/telemetry.d.ts +14 -14
- package/backends/codesys/config.ts +0 -54
- package/backends/codesys/debug.ts +0 -509
- package/backends/codesys/emit.ts +0 -1738
- package/backends/codesys/engineering.ts +0 -88
- package/backends/codesys/global-names.ts +0 -49
- package/backends/codesys/http.ts +0 -675
- package/backends/codesys/index.ts +0 -13
- package/backends/codesys/io-paths.ts +0 -105
- package/backends/codesys/manifest.ts +0 -312
- package/backends/codesys/names.ts +0 -76
- package/backends/codesys/numeric.ts +0 -356
- package/backends/codesys/plan.ts +0 -457
- package/backends/codesys/publication.ts +0 -59
- package/backends/codesys/storage.ts +0 -70
- package/backends/codesys/task-access.ts +0 -293
- package/backends/codesys/telemetry.ts +0 -593
- package/backends/codesys/types.ts +0 -179
- package/backends/codesys/vendor.ts +0 -108
- package/ir/aggregates.ts +0 -333
- package/ir/allocation.ts +0 -74
- package/ir/core-model.ts +0 -129
- package/ir/core.ts +0 -64
- package/ir/declarations.ts +0 -208
- package/ir/diagnostics.ts +0 -43
- package/ir/domains.ts +0 -96
- package/ir/index.ts +0 -8
- package/ir/link.ts +0 -185
- package/ir/model.ts +0 -346
- package/ir/routine.ts +0 -859
- package/ir/scalars.ts +0 -675
- package/ir/types.ts +0 -76
- package/project/artifacts.ts +0 -277
- package/project/cli.ts +0 -395
- package/project/index.ts +0 -1
- package/project/plant.ts +0 -509
- package/project/project.ts +0 -358
- package/project/server.ts +0 -113
|
@@ -0,0 +1,605 @@
|
|
|
1
|
+
import {
|
|
2
|
+
instancedBlock,
|
|
3
|
+
type Constant,
|
|
4
|
+
type CoreExpression,
|
|
5
|
+
type CoreStatement,
|
|
6
|
+
type Deployment,
|
|
7
|
+
type Diagnostic,
|
|
8
|
+
isVerifiedCore,
|
|
9
|
+
nestedBodies,
|
|
10
|
+
type PlcType,
|
|
11
|
+
type SourceSpan,
|
|
12
|
+
type VerifiedCoreModule,
|
|
13
|
+
} from "@tsplc/compiler/ir";
|
|
14
|
+
import { type CodesysOptions, optionsSchema } from "./config.ts";
|
|
15
|
+
import { ioScalarBits, sizePrefixes } from "@tsplc/iec";
|
|
16
|
+
import { ioLayout } from "@tsplc/typegen/hardware";
|
|
17
|
+
import {
|
|
18
|
+
iecSpelling,
|
|
19
|
+
isStoragePath,
|
|
20
|
+
resolveStoragePath,
|
|
21
|
+
type StorageBinding,
|
|
22
|
+
} from "./io-paths.ts";
|
|
23
|
+
import { isIecIdentifier } from "./names.ts";
|
|
24
|
+
import { statementExpressions, subexpressions } from "./operands.ts";
|
|
25
|
+
import { storageLayout } from "./storage.ts";
|
|
26
|
+
import {
|
|
27
|
+
validateHttpLayout,
|
|
28
|
+
resolveHttpEndpoint,
|
|
29
|
+
type HttpEndpoint,
|
|
30
|
+
} from "./http.ts";
|
|
31
|
+
import {
|
|
32
|
+
accessedStorage,
|
|
33
|
+
schedulePrograms,
|
|
34
|
+
taskStorageOwnership,
|
|
35
|
+
} from "./task-access.ts";
|
|
36
|
+
import { resolvePublication } from "./publication.ts";
|
|
37
|
+
import type {
|
|
38
|
+
Environment,
|
|
39
|
+
Publication,
|
|
40
|
+
ResolvedDebugConfig,
|
|
41
|
+
ResolvedTelemetryConfig,
|
|
42
|
+
ResolvedTsplcConfig,
|
|
43
|
+
} from "@tsplc/config";
|
|
44
|
+
import { cyclicTasksSchema } from "@tsplc/config/build";
|
|
45
|
+
import type { ProjectHardware } from "./manifest.ts";
|
|
46
|
+
import {
|
|
47
|
+
planTelemetry,
|
|
48
|
+
telemetryClient,
|
|
49
|
+
type TelemetryPlan,
|
|
50
|
+
} from "./telemetry.ts";
|
|
51
|
+
|
|
52
|
+
// Only this planner can admit a target configuration for emission. Cloning a
|
|
53
|
+
// branded-looking object does not give it membership in this private identity set.
|
|
54
|
+
const planned: unique symbol = Symbol("CODESYS plan");
|
|
55
|
+
export interface CodesysPlan {
|
|
56
|
+
readonly [planned]: true;
|
|
57
|
+
readonly core: VerifiedCoreModule;
|
|
58
|
+
readonly deployment: Deployment;
|
|
59
|
+
readonly options: CodesysOptions;
|
|
60
|
+
/** `io` keys that name authored storage, resolved against the verified types. */
|
|
61
|
+
readonly storageBindings: readonly StorageBinding[];
|
|
62
|
+
readonly publication: Publication;
|
|
63
|
+
readonly http: Readonly<Record<string, HttpEndpoint>>;
|
|
64
|
+
readonly project: CodesysProject;
|
|
65
|
+
/** Each task's records and their sender, when the program has telemetry. */
|
|
66
|
+
readonly telemetry?: TelemetryPlan;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* What the debug manifest describes beyond the program, the instrumentation `debug` asks for,
|
|
70
|
+
* and the telemetry settings and server its records go to.
|
|
71
|
+
*/
|
|
72
|
+
export interface CodesysProject {
|
|
73
|
+
/** Manifest sources and telemetry `source`s are relative to this directory. */
|
|
74
|
+
root: string;
|
|
75
|
+
debug?: ResolvedDebugConfig;
|
|
76
|
+
telemetry?: ResolvedTelemetryConfig;
|
|
77
|
+
server?: Pick<
|
|
78
|
+
NonNullable<ResolvedTsplcConfig["server"]>,
|
|
79
|
+
"transport" | "baseUrlEnv"
|
|
80
|
+
>;
|
|
81
|
+
hardware?: ProjectHardware;
|
|
82
|
+
/** What the server's base URL and the HTTP credentials are read from; `process.env` when omitted. */
|
|
83
|
+
env?: Environment;
|
|
84
|
+
}
|
|
85
|
+
const plans = new WeakSet<object>();
|
|
86
|
+
export function diagnostic(message: string, span?: SourceSpan): Diagnostic {
|
|
87
|
+
return {
|
|
88
|
+
origin: "backend",
|
|
89
|
+
code: "PLC7001",
|
|
90
|
+
severity: "error",
|
|
91
|
+
message,
|
|
92
|
+
...(span && { span }),
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
export function isCodesysPlan(plan: CodesysPlan): boolean {
|
|
96
|
+
return plans.has(plan);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function planCodesys(
|
|
100
|
+
core: VerifiedCoreModule,
|
|
101
|
+
deployment: Deployment,
|
|
102
|
+
value: unknown,
|
|
103
|
+
project: CodesysProject = { root: process.cwd() },
|
|
104
|
+
): { plan?: CodesysPlan; diagnostics: readonly Diagnostic[] } {
|
|
105
|
+
if (!isVerifiedCore(core)) {
|
|
106
|
+
return {
|
|
107
|
+
diagnostics: [
|
|
108
|
+
diagnostic("CODESYS requires a verified, normalized core module."),
|
|
109
|
+
],
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const parsed = optionsSchema.safeParse(value);
|
|
114
|
+
if (!parsed.success) {
|
|
115
|
+
return {
|
|
116
|
+
diagnostics: parsed.error.issues.map((issue) =>
|
|
117
|
+
diagnostic(`${issue.path.join(".")}: ${issue.message}`),
|
|
118
|
+
),
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const options = parsed.data;
|
|
123
|
+
const diagnostics: Diagnostic[] = [];
|
|
124
|
+
const report = (message: string, span?: SourceSpan): void => {
|
|
125
|
+
diagnostics.push(diagnostic(message, span));
|
|
126
|
+
};
|
|
127
|
+
const scheduled: Deployment = {
|
|
128
|
+
tasks: schedulePrograms(core, deployment.tasks, report),
|
|
129
|
+
};
|
|
130
|
+
const env = project.env ?? process.env;
|
|
131
|
+
const http = new Map<string, HttpEndpoint>();
|
|
132
|
+
for (const block of core.blocks) {
|
|
133
|
+
if (block.runtime?.kind === "http" || block.runtime?.kind === "rpc") {
|
|
134
|
+
try {
|
|
135
|
+
validateHttpLayout(
|
|
136
|
+
block,
|
|
137
|
+
new Map(core.types.map((type) => [type.id, type])),
|
|
138
|
+
);
|
|
139
|
+
http.set(
|
|
140
|
+
block.id,
|
|
141
|
+
block.runtime.kind === "rpc"
|
|
142
|
+
? {
|
|
143
|
+
transport: "opcua",
|
|
144
|
+
host: "",
|
|
145
|
+
port: 0,
|
|
146
|
+
authority: "",
|
|
147
|
+
path: block.runtime.path,
|
|
148
|
+
token: "",
|
|
149
|
+
timeoutMs: options.http.timeoutMs,
|
|
150
|
+
tls: false,
|
|
151
|
+
}
|
|
152
|
+
: resolveHttpEndpoint(block.runtime.url, options.http, env),
|
|
153
|
+
);
|
|
154
|
+
} catch (cause) {
|
|
155
|
+
report(
|
|
156
|
+
cause instanceof Error ? cause.message : String(cause),
|
|
157
|
+
block.span,
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const storageBindings = validateIo(core, options, report);
|
|
164
|
+
validateTasks(core, scheduled, report);
|
|
165
|
+
validateStorage(core, options, report);
|
|
166
|
+
validateConstants(core, report);
|
|
167
|
+
const { telemetry, endpoint } = planTelemetry(
|
|
168
|
+
core,
|
|
169
|
+
scheduled,
|
|
170
|
+
options,
|
|
171
|
+
{ ...project, env, trace: project.debug?.trace },
|
|
172
|
+
report,
|
|
173
|
+
);
|
|
174
|
+
if (endpoint) {
|
|
175
|
+
http.set(telemetryClient, endpoint);
|
|
176
|
+
}
|
|
177
|
+
const { publication, errors } = resolvePublication(core, options.publish);
|
|
178
|
+
for (const error of errors) {
|
|
179
|
+
report(error);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
if (diagnostics.length) {
|
|
183
|
+
return { diagnostics };
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const plan: CodesysPlan = Object.freeze({
|
|
187
|
+
core,
|
|
188
|
+
deployment: structuredClone(scheduled),
|
|
189
|
+
options: structuredClone(options),
|
|
190
|
+
storageBindings,
|
|
191
|
+
publication,
|
|
192
|
+
http: Object.fromEntries(http),
|
|
193
|
+
project,
|
|
194
|
+
...(telemetry && { telemetry }),
|
|
195
|
+
[planned]: true as const,
|
|
196
|
+
});
|
|
197
|
+
// Parsed options can contain nested engineering settings; detach and freeze the complete plan.
|
|
198
|
+
const freeze = (value: unknown): void => {
|
|
199
|
+
if (typeof value !== "object" || value === null || Object.isFrozen(value)) {
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
for (const child of Object.values(value)) {
|
|
203
|
+
freeze(child);
|
|
204
|
+
}
|
|
205
|
+
Object.freeze(value);
|
|
206
|
+
};
|
|
207
|
+
freeze(plan.deployment);
|
|
208
|
+
freeze(plan.options);
|
|
209
|
+
freeze(plan.storageBindings);
|
|
210
|
+
freeze(plan.publication);
|
|
211
|
+
plans.add(plan);
|
|
212
|
+
return { plan, diagnostics: [] };
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
type Report = (message: string, span?: SourceSpan) => void;
|
|
216
|
+
|
|
217
|
+
/** Validate every `io` binding and return the ones that name authored storage. */
|
|
218
|
+
function validateIo(
|
|
219
|
+
core: VerifiedCoreModule,
|
|
220
|
+
options: CodesysOptions,
|
|
221
|
+
report: Report,
|
|
222
|
+
): StorageBinding[] {
|
|
223
|
+
// Compare physical bindings as bit intervals, regardless of their spelling.
|
|
224
|
+
// Different widths can still overlap.
|
|
225
|
+
const types = new Map(core.types.map((type) => [type.id, type]));
|
|
226
|
+
const targets: {
|
|
227
|
+
name: string;
|
|
228
|
+
binding: CodesysOptions["io"][string];
|
|
229
|
+
type: PlcType;
|
|
230
|
+
region: "I" | "Q";
|
|
231
|
+
span?: SourceSpan;
|
|
232
|
+
}[] = [];
|
|
233
|
+
const names = new Set<string>();
|
|
234
|
+
for (const item of core.variables.filter(
|
|
235
|
+
(item) => item.storage === "input" || item.storage === "output",
|
|
236
|
+
)) {
|
|
237
|
+
if (names.has(item.name)) {
|
|
238
|
+
report(
|
|
239
|
+
`Ambiguous I/O name ${item.name}; logical I/O names must be unique.`,
|
|
240
|
+
item.span,
|
|
241
|
+
);
|
|
242
|
+
}
|
|
243
|
+
names.add(item.name);
|
|
244
|
+
const binding = Object.hasOwn(options.io, item.name)
|
|
245
|
+
? options.io[item.name]
|
|
246
|
+
: undefined;
|
|
247
|
+
if (!binding) {
|
|
248
|
+
report(`Missing I/O binding for ${item.name}.`, item.span);
|
|
249
|
+
continue;
|
|
250
|
+
}
|
|
251
|
+
targets.push({
|
|
252
|
+
name: item.name,
|
|
253
|
+
binding,
|
|
254
|
+
type: types.get(item.type)!,
|
|
255
|
+
region: item.storage === "input" ? "I" : "Q",
|
|
256
|
+
span: item.span,
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
const scheduled = new Set<string>();
|
|
261
|
+
for (const program of core.programs) {
|
|
262
|
+
const fn = core.functions.find((item) => item.id === program.entry)!;
|
|
263
|
+
for (const id of accessedStorage(fn, core)) {
|
|
264
|
+
scheduled.add(id);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
const storageBindings: StorageBinding[] = [];
|
|
268
|
+
for (const [name, binding] of Object.entries(options.io)) {
|
|
269
|
+
if (names.has(name)) {
|
|
270
|
+
continue;
|
|
271
|
+
}
|
|
272
|
+
if (!isStoragePath(name)) {
|
|
273
|
+
report(`Unknown I/O binding ${name}.`);
|
|
274
|
+
continue;
|
|
275
|
+
}
|
|
276
|
+
const resolved = resolveStoragePath(core, name);
|
|
277
|
+
if ("error" in resolved) {
|
|
278
|
+
report(resolved.error);
|
|
279
|
+
continue;
|
|
280
|
+
}
|
|
281
|
+
const overlapping = storageBindings.find((other) =>
|
|
282
|
+
nested(other, resolved),
|
|
283
|
+
);
|
|
284
|
+
if (overlapping) {
|
|
285
|
+
report(`${name} overlaps the binding ${overlapping.key}.`);
|
|
286
|
+
}
|
|
287
|
+
if (!scheduled.has(resolved.root.id)) {
|
|
288
|
+
report(
|
|
289
|
+
`${name}: no scheduled program uses ${resolved.root.name}, so it would never be copied.`,
|
|
290
|
+
);
|
|
291
|
+
}
|
|
292
|
+
storageBindings.push(resolved);
|
|
293
|
+
targets.push({
|
|
294
|
+
name,
|
|
295
|
+
binding,
|
|
296
|
+
type: resolved.type,
|
|
297
|
+
region: binding.address[1] === "I" ? "I" : "Q",
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
const addresses: {
|
|
302
|
+
region: string;
|
|
303
|
+
start: bigint;
|
|
304
|
+
end: bigint;
|
|
305
|
+
name: string;
|
|
306
|
+
shared?: boolean;
|
|
307
|
+
}[] = [];
|
|
308
|
+
for (const { name, binding, type, region, span } of targets) {
|
|
309
|
+
// An array binds at its first element; the remaining elements follow at
|
|
310
|
+
// consecutive addresses of the same width, like an EDS byte/word span.
|
|
311
|
+
const array = type.kind === "array" ? type : undefined;
|
|
312
|
+
const element = array ? types.get(array.element) : type;
|
|
313
|
+
// Typegen spells channels as IEC types; equal layouts bind (an EDS UINT flag word into a WORD).
|
|
314
|
+
const layout = ioLayout(iecSpelling(type, types) ?? "");
|
|
315
|
+
if (binding.type && (!layout || ioLayout(binding.type) !== layout)) {
|
|
316
|
+
report(
|
|
317
|
+
`I/O type mismatch for ${name}: ${type.id} cannot bind to ${binding.type}.`,
|
|
318
|
+
span,
|
|
319
|
+
);
|
|
320
|
+
}
|
|
321
|
+
if (!binding.address.startsWith(`%${region}`)) {
|
|
322
|
+
report(`Wrong address region for ${name}: expected %${region}X.`, span);
|
|
323
|
+
}
|
|
324
|
+
const match = /^%([IQ])([XBWD])([0-9]+)(?:\.([0-7]))?$/.exec(
|
|
325
|
+
binding.address,
|
|
326
|
+
);
|
|
327
|
+
const size = sizePrefixes.get(binding.address.charAt(2))?.bits;
|
|
328
|
+
if (!match || size === undefined) {
|
|
329
|
+
continue;
|
|
330
|
+
}
|
|
331
|
+
if (region === "I" && element?.kind === "float") {
|
|
332
|
+
report(
|
|
333
|
+
"Floating logical inputs require an explicit runtime nonfinite policy, which is not supported yet.",
|
|
334
|
+
span,
|
|
335
|
+
);
|
|
336
|
+
}
|
|
337
|
+
if (array && element?.kind === "bool") {
|
|
338
|
+
report(
|
|
339
|
+
`BOOL arrays are not bit-packed in CODESYS I/O; bind each element of ${name} separately.`,
|
|
340
|
+
span,
|
|
341
|
+
);
|
|
342
|
+
}
|
|
343
|
+
const elementBits =
|
|
344
|
+
element && ioScalarBits(iecSpelling(element, types) ?? "");
|
|
345
|
+
if (elementBits !== size) {
|
|
346
|
+
report(`Address size does not match ${name}:${type.id}.`, span);
|
|
347
|
+
}
|
|
348
|
+
// CODESYS numbers %IW/%ID by element: %IW12 is bytes 24-25. See verify.py.
|
|
349
|
+
const unit = size === 1 ? 8n : BigInt(size);
|
|
350
|
+
const offset = BigInt(match[3]!);
|
|
351
|
+
const start = offset * unit + BigInt(match[4] ?? 0);
|
|
352
|
+
const end = start + BigInt(size) * BigInt(array?.length ?? 1);
|
|
353
|
+
if (
|
|
354
|
+
binding.inverted &&
|
|
355
|
+
(region !== "I" || element?.kind !== "bool" || array)
|
|
356
|
+
) {
|
|
357
|
+
report(`Only a BOOL input can be inverted: ${name}.`, span);
|
|
358
|
+
}
|
|
359
|
+
if (binding.shared && region !== "I") {
|
|
360
|
+
report(`Only an input can be shared: ${name}.`, span);
|
|
361
|
+
}
|
|
362
|
+
const collision = addresses.find(
|
|
363
|
+
(address) =>
|
|
364
|
+
address.region === match[1] &&
|
|
365
|
+
address.start < end &&
|
|
366
|
+
start < address.end &&
|
|
367
|
+
!(
|
|
368
|
+
binding.shared &&
|
|
369
|
+
address.shared &&
|
|
370
|
+
address.start === start &&
|
|
371
|
+
address.end === end
|
|
372
|
+
),
|
|
373
|
+
);
|
|
374
|
+
if (collision) {
|
|
375
|
+
report(
|
|
376
|
+
`Overlapping I/O address ${binding.address} with ${collision.name}.`,
|
|
377
|
+
span,
|
|
378
|
+
);
|
|
379
|
+
}
|
|
380
|
+
addresses.push({
|
|
381
|
+
region: match[1]!,
|
|
382
|
+
start,
|
|
383
|
+
end,
|
|
384
|
+
name,
|
|
385
|
+
shared: binding.shared,
|
|
386
|
+
});
|
|
387
|
+
if (offset > 0x7fff_ffffn) {
|
|
388
|
+
report(`I/O offset is too large: ${binding.address}.`, span);
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
return storageBindings;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
/** Two storage bindings conflict when one target contains the other. */
|
|
395
|
+
function nested(left: StorageBinding, right: StorageBinding): boolean {
|
|
396
|
+
return (
|
|
397
|
+
left.root.id === right.root.id &&
|
|
398
|
+
left.segments.every((a, index) => {
|
|
399
|
+
const b = right.segments[index];
|
|
400
|
+
if (!b) {
|
|
401
|
+
return true;
|
|
402
|
+
}
|
|
403
|
+
return a.kind === "field"
|
|
404
|
+
? b.kind === "field" && a.name === b.name
|
|
405
|
+
: b.kind === "index" && a.value === b.value;
|
|
406
|
+
})
|
|
407
|
+
);
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
// The project CLI checks names and priorities, but not exact TIME intervals or case-insensitive
|
|
411
|
+
// task names, and `planCodesys` callers pass deployments that no config schema has seen. So the
|
|
412
|
+
// planner checks each task against the raw ST schedule's rules.
|
|
413
|
+
const taskSchema = cyclicTasksSchema.element.shape;
|
|
414
|
+
|
|
415
|
+
function validateTasks(
|
|
416
|
+
core: VerifiedCoreModule,
|
|
417
|
+
deployment: Deployment,
|
|
418
|
+
report: Report,
|
|
419
|
+
): void {
|
|
420
|
+
const programs = new Map(
|
|
421
|
+
core.programs.map((program) => [program.name, program]),
|
|
422
|
+
);
|
|
423
|
+
const assignment = new Map<string, string>();
|
|
424
|
+
const taskNames = new Set<string>();
|
|
425
|
+
const checkOwnership = taskStorageOwnership(core, report);
|
|
426
|
+
for (const task of deployment.tasks) {
|
|
427
|
+
if (!isIecIdentifier(task.name)) {
|
|
428
|
+
report(`Invalid task name ${task.name}.`);
|
|
429
|
+
}
|
|
430
|
+
if (taskNames.has(task.name.toUpperCase())) {
|
|
431
|
+
report(`Duplicate task name ${task.name}.`);
|
|
432
|
+
}
|
|
433
|
+
taskNames.add(task.name.toUpperCase());
|
|
434
|
+
if (!taskSchema.priority.safeParse(task.priority).success) {
|
|
435
|
+
report(`Task ${task.name} priority must be 0..31.`);
|
|
436
|
+
}
|
|
437
|
+
const interval = taskSchema.intervalNanoseconds.safeParse(
|
|
438
|
+
task.intervalNanoseconds,
|
|
439
|
+
);
|
|
440
|
+
if (interval.error?.issues.some(({ code }) => code === "invalid_format")) {
|
|
441
|
+
report(`Invalid task interval for ${task.name}.`);
|
|
442
|
+
} else if (interval.error) {
|
|
443
|
+
report(
|
|
444
|
+
`Task ${task.name} requires an exact positive TIME interval in milliseconds.`,
|
|
445
|
+
);
|
|
446
|
+
}
|
|
447
|
+
if (!task.programs.length) {
|
|
448
|
+
report(`Task ${task.name} has no programs.`);
|
|
449
|
+
}
|
|
450
|
+
for (const name of task.programs) {
|
|
451
|
+
const program = programs.get(name);
|
|
452
|
+
if (!program) {
|
|
453
|
+
report(`Unknown task program ${name}.`);
|
|
454
|
+
continue;
|
|
455
|
+
}
|
|
456
|
+
if (assignment.has(name)) {
|
|
457
|
+
report(`Program ${name} must belong to exactly one task.`);
|
|
458
|
+
}
|
|
459
|
+
assignment.set(name, task.name);
|
|
460
|
+
checkOwnership(task.name, program);
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
for (const name of programs.keys()) {
|
|
464
|
+
if (!assignment.has(name)) {
|
|
465
|
+
report(`Program ${name} has no cyclic task.`);
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
function validateStorage(
|
|
471
|
+
core: VerifiedCoreModule,
|
|
472
|
+
options: CodesysOptions,
|
|
473
|
+
report: Report,
|
|
474
|
+
): void {
|
|
475
|
+
const programs = new Map(
|
|
476
|
+
core.programs.map((program) => [program.name, program]),
|
|
477
|
+
);
|
|
478
|
+
if (core.blocks.some((block) => block.standard === "Blinker")) {
|
|
479
|
+
for (const target of Object.values(options.programNames ?? {})) {
|
|
480
|
+
if (target.toUpperCase() === "UTIL") {
|
|
481
|
+
report(
|
|
482
|
+
`IEC object name ${target} shadows the Util library namespace required by Blinker.`,
|
|
483
|
+
);
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
const explicitNames = new Set<string>();
|
|
488
|
+
for (const [name, target] of Object.entries(options.programNames ?? {})) {
|
|
489
|
+
if (!programs.has(name)) {
|
|
490
|
+
report(`Unknown program name override ${name}.`);
|
|
491
|
+
}
|
|
492
|
+
if (explicitNames.has(target.toUpperCase())) {
|
|
493
|
+
report(`Duplicate IEC program name ${target}.`);
|
|
494
|
+
}
|
|
495
|
+
explicitNames.add(target.toUpperCase());
|
|
496
|
+
}
|
|
497
|
+
const types = new Map(core.types.map((type) => [type.id, type]));
|
|
498
|
+
const blocks = new Map(core.blocks.map((block) => [block.id, block]));
|
|
499
|
+
|
|
500
|
+
// InOut addresses must not survive a retained restart, including nested children.
|
|
501
|
+
const blockHasInOut = (id: string): boolean => {
|
|
502
|
+
const block = blocks.get(id)!;
|
|
503
|
+
return (
|
|
504
|
+
block.inputs.some((input) => input.mode === "inOut") ||
|
|
505
|
+
block.state.some((field) => {
|
|
506
|
+
const type = instancedBlock(field.type, types);
|
|
507
|
+
return type !== undefined && blockHasInOut(type.block);
|
|
508
|
+
})
|
|
509
|
+
);
|
|
510
|
+
};
|
|
511
|
+
|
|
512
|
+
const layout = storageLayout(core);
|
|
513
|
+
for (const block of core.blocks) {
|
|
514
|
+
if (layout.retainedBlocks.has(block.id) && blockHasInOut(block.id)) {
|
|
515
|
+
report(
|
|
516
|
+
"Retained function blocks cannot contain direct or nested InOut references.",
|
|
517
|
+
block.span,
|
|
518
|
+
);
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
for (const variable of core.variables) {
|
|
522
|
+
const type = instancedBlock(variable.type, types);
|
|
523
|
+
if (variable.retain && type && blockHasInOut(type.block)) {
|
|
524
|
+
report(
|
|
525
|
+
"Retained function blocks cannot contain direct or nested InOut references.",
|
|
526
|
+
variable.span,
|
|
527
|
+
);
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
function validateConstants(core: VerifiedCoreModule, report: Report): void {
|
|
533
|
+
const types = new Map(core.types.map((type) => [type.id, type]));
|
|
534
|
+
const constant = (value: Constant, id: string, span?: SourceSpan): void => {
|
|
535
|
+
const type = types.get(id)!;
|
|
536
|
+
if (type.kind === "string" && value.kind === "string") {
|
|
537
|
+
if (
|
|
538
|
+
type.encoding === "utf8" &&
|
|
539
|
+
[...value.value].some(
|
|
540
|
+
(character) =>
|
|
541
|
+
character.charCodeAt(0) === 0 || character.charCodeAt(0) > 127,
|
|
542
|
+
)
|
|
543
|
+
) {
|
|
544
|
+
report(
|
|
545
|
+
"Non-ASCII STRING literals require a verified project-wide UTF8 setting; this profile supports ASCII literals.",
|
|
546
|
+
span,
|
|
547
|
+
);
|
|
548
|
+
}
|
|
549
|
+
if (type.encoding === "utf16" && /[\uD800-\uDFFF]/.test(value.value)) {
|
|
550
|
+
report(
|
|
551
|
+
"CODESYS WSTRING uses UCS-2 and cannot represent surrogate code units.",
|
|
552
|
+
span,
|
|
553
|
+
);
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
if (type.kind === "array" && value.kind === "array") {
|
|
557
|
+
for (const item of value.items) {
|
|
558
|
+
constant(item, type.element, span);
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
if (type.kind === "record" && value.kind === "record") {
|
|
562
|
+
for (const field of type.fields) {
|
|
563
|
+
constant(value.fields[field.name]!, field.type, span);
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
};
|
|
567
|
+
const constantsInExpression = (value: CoreExpression): void => {
|
|
568
|
+
if (value.kind === "constant") {
|
|
569
|
+
constant(value.value, value.type, value.span);
|
|
570
|
+
}
|
|
571
|
+
subexpressions(value).forEach(constantsInExpression);
|
|
572
|
+
};
|
|
573
|
+
const constantsInBody = (items: readonly CoreStatement[]): void => {
|
|
574
|
+
for (const item of items) {
|
|
575
|
+
statementExpressions(item).forEach(constantsInExpression);
|
|
576
|
+
if (item.kind !== "switch") {
|
|
577
|
+
nestedBodies(item).forEach(constantsInBody);
|
|
578
|
+
continue;
|
|
579
|
+
}
|
|
580
|
+
for (const branch of item.cases) {
|
|
581
|
+
for (const value of branch.values) {
|
|
582
|
+
constant(value, item.value.type, item.span);
|
|
583
|
+
}
|
|
584
|
+
constantsInBody(branch.body);
|
|
585
|
+
}
|
|
586
|
+
constantsInBody(item.fallback);
|
|
587
|
+
}
|
|
588
|
+
};
|
|
589
|
+
core.variables.forEach((variable) => {
|
|
590
|
+
if (variable.initial) {
|
|
591
|
+
constant(variable.initial, variable.type, variable.span);
|
|
592
|
+
}
|
|
593
|
+
});
|
|
594
|
+
for (const fn of core.functions) {
|
|
595
|
+
constantsInBody(fn.body);
|
|
596
|
+
}
|
|
597
|
+
core.blocks.forEach((block) => {
|
|
598
|
+
[...block.outputs, ...block.state, ...block.locals].forEach((variable) => {
|
|
599
|
+
if (variable.initial) {
|
|
600
|
+
constant(variable.initial, variable.type, variable.span);
|
|
601
|
+
}
|
|
602
|
+
});
|
|
603
|
+
constantsInBody(block.body);
|
|
604
|
+
});
|
|
605
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { GeneratedObject, PlcProgram } from "@tsplc/compiler/ir";
|
|
2
|
+
import type { IoChannel } from "./global-lists.ts";
|
|
3
|
+
import { type EmissionContext, required } from "./symbols.ts";
|
|
4
|
+
import { accessedStorage } from "./task-access.ts";
|
|
5
|
+
|
|
6
|
+
/** The PROGRAM a task calls for a program: its IO scan around a call of the authored routine. */
|
|
7
|
+
export function emitScanProgram(
|
|
8
|
+
{ plan, symbols, instrumentation }: EmissionContext,
|
|
9
|
+
ioChannels: readonly IoChannel[],
|
|
10
|
+
program: PlcProgram,
|
|
11
|
+
): GeneratedObject {
|
|
12
|
+
const { core, options } = plan;
|
|
13
|
+
const { names, functionNames } = symbols;
|
|
14
|
+
// Each wrapper snapshots physical inputs before the authored scan and commits
|
|
15
|
+
// outputs afterwards. RETURN in an authored routine cannot skip that epilogue.
|
|
16
|
+
const explicit =
|
|
17
|
+
options.programNames && Object.hasOwn(options.programNames, program.name)
|
|
18
|
+
? options.programNames[program.name]
|
|
19
|
+
: undefined;
|
|
20
|
+
const name = explicit ?? names.allocateObject("PRG", program.name);
|
|
21
|
+
const fn = required(core.functions.find((item) => item.id === program.entry));
|
|
22
|
+
const used = accessedStorage(fn, core);
|
|
23
|
+
const channels = ioChannels.filter((channel) => used.has(channel.root));
|
|
24
|
+
const stateful = symbols.programRoutines.has(program.entry);
|
|
25
|
+
const call = `${functionNames.get(program.entry)}();`;
|
|
26
|
+
// The first program of a task with a ring runs its sender first, which counts the scan and reads the clock.
|
|
27
|
+
const task = plan.deployment.tasks.find(
|
|
28
|
+
(item) => item.programs[0] === program.name,
|
|
29
|
+
);
|
|
30
|
+
const ring = task && instrumentation.rings.get(task.name);
|
|
31
|
+
const lines = [
|
|
32
|
+
...(ring ? [`${instrumentation.global}.${ring}();`] : []),
|
|
33
|
+
...channels
|
|
34
|
+
.filter((channel) => channel.input)
|
|
35
|
+
.map(
|
|
36
|
+
({ target, wire, inverted }) =>
|
|
37
|
+
`${target} := ${inverted ? "NOT " : ""}${wire};`,
|
|
38
|
+
),
|
|
39
|
+
stateful ? call : `plc_discard := ${call}`,
|
|
40
|
+
...channels
|
|
41
|
+
.filter((channel) => !channel.input)
|
|
42
|
+
.map(({ target, wire }) => `${wire} := ${target};`),
|
|
43
|
+
];
|
|
44
|
+
return {
|
|
45
|
+
id: program.id,
|
|
46
|
+
kind: "program",
|
|
47
|
+
name,
|
|
48
|
+
declaration: stateful
|
|
49
|
+
? `PROGRAM ${name}\n`
|
|
50
|
+
: `PROGRAM ${name}\nVAR\n plc_discard : BOOL;\nEND_VAR\n`,
|
|
51
|
+
implementation: `${lines.join("\n")}\n`,
|
|
52
|
+
};
|
|
53
|
+
}
|