@tsplc/compiler 0.2.16 → 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,643 @@
|
|
|
1
|
+
import {
|
|
2
|
+
instancedBlock,
|
|
3
|
+
jobErrors,
|
|
4
|
+
nanosecondsPerMillisecond,
|
|
5
|
+
nestedBodies,
|
|
6
|
+
standardBlocks,
|
|
7
|
+
} from "@tsplc/compiler/ir";
|
|
8
|
+
import type {
|
|
9
|
+
Constant,
|
|
10
|
+
CoreExpression,
|
|
11
|
+
CoreFunction,
|
|
12
|
+
CoreFunctionBlock,
|
|
13
|
+
CoreStatement,
|
|
14
|
+
PlcModule,
|
|
15
|
+
PlcType,
|
|
16
|
+
} from "@tsplc/compiler/ir";
|
|
17
|
+
import type { ResolvedTsplcConfig } from "@tsplc/config";
|
|
18
|
+
import type {
|
|
19
|
+
DebugConstant,
|
|
20
|
+
DebugDevice,
|
|
21
|
+
DebugEventSite,
|
|
22
|
+
DebugFunction,
|
|
23
|
+
DebugIo,
|
|
24
|
+
DebugJob,
|
|
25
|
+
DebugManifest,
|
|
26
|
+
DebugType,
|
|
27
|
+
SourceRange,
|
|
28
|
+
} from "@tsplc/debug/manifest";
|
|
29
|
+
import { serviceOfPath } from "@tsplc/server";
|
|
30
|
+
import type { HardwareCatalog } from "@tsplc/typegen/hardware";
|
|
31
|
+
import { version } from "../../package.json";
|
|
32
|
+
import { type InstancePath, storagePath } from "./debug.ts";
|
|
33
|
+
import type { Instrumentation } from "./instrumentation.ts";
|
|
34
|
+
import { statementExpressions, subexpressions } from "./operands.ts";
|
|
35
|
+
import { Sources } from "./sources.ts";
|
|
36
|
+
import {
|
|
37
|
+
type CodesysSymbols,
|
|
38
|
+
type EmissionContext,
|
|
39
|
+
instrumentationFieldTypes,
|
|
40
|
+
required,
|
|
41
|
+
} from "./symbols.ts";
|
|
42
|
+
import { generated } from "./telemetry.ts";
|
|
43
|
+
import { type CodesysTypes, sourceTypeName, stringTypeName } from "./types.ts";
|
|
44
|
+
|
|
45
|
+
/** The configured hardware a manifest describes; `tree` when this build generates the device tree. */
|
|
46
|
+
export interface ProjectHardware {
|
|
47
|
+
catalog: HardwareCatalog;
|
|
48
|
+
config: Pick<ResolvedTsplcConfig, "devices" | "io">;
|
|
49
|
+
tree: boolean;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const scalarTypes: Partial<Record<PlcType["kind"], true>> = {
|
|
53
|
+
bool: true,
|
|
54
|
+
integer: true,
|
|
55
|
+
bits: true,
|
|
56
|
+
float: true,
|
|
57
|
+
duration: true,
|
|
58
|
+
calendar: true,
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
/** Every verified type by id, with TypeScript names and the target's field names. */
|
|
62
|
+
function debugTypes(
|
|
63
|
+
types: readonly PlcType[],
|
|
64
|
+
target: CodesysTypes,
|
|
65
|
+
blocks: ReadonlyMap<string, CoreFunctionBlock>,
|
|
66
|
+
fields: (block: string, variable: string) => string,
|
|
67
|
+
extras: (
|
|
68
|
+
block: string,
|
|
69
|
+
) => readonly { name: string; target: string; type: string }[],
|
|
70
|
+
sources: Sources,
|
|
71
|
+
): Record<string, DebugType> {
|
|
72
|
+
return Object.fromEntries(
|
|
73
|
+
types.map((type): [string, DebugType] => {
|
|
74
|
+
if (scalarTypes[type.kind]) {
|
|
75
|
+
return [type.id, { kind: "scalar", iec: type.id }];
|
|
76
|
+
}
|
|
77
|
+
switch (type.kind) {
|
|
78
|
+
case "string":
|
|
79
|
+
return [
|
|
80
|
+
type.id,
|
|
81
|
+
{
|
|
82
|
+
kind: "scalar",
|
|
83
|
+
iec: stringTypeName(type),
|
|
84
|
+
},
|
|
85
|
+
];
|
|
86
|
+
case "enum":
|
|
87
|
+
return [
|
|
88
|
+
type.id,
|
|
89
|
+
{
|
|
90
|
+
kind: "enum",
|
|
91
|
+
name: sourceTypeName(type.id),
|
|
92
|
+
members: Object.fromEntries(
|
|
93
|
+
type.members.map((member) => [
|
|
94
|
+
member.name,
|
|
95
|
+
Number(member.value),
|
|
96
|
+
]),
|
|
97
|
+
),
|
|
98
|
+
},
|
|
99
|
+
];
|
|
100
|
+
case "array":
|
|
101
|
+
return [
|
|
102
|
+
type.id,
|
|
103
|
+
{
|
|
104
|
+
kind: "array",
|
|
105
|
+
element: type.element,
|
|
106
|
+
length: type.length,
|
|
107
|
+
lower: type.lowerBound,
|
|
108
|
+
},
|
|
109
|
+
];
|
|
110
|
+
case "record":
|
|
111
|
+
return [
|
|
112
|
+
type.id,
|
|
113
|
+
{
|
|
114
|
+
kind: "record",
|
|
115
|
+
name: sourceTypeName(type.id),
|
|
116
|
+
fields: type.fields.map((field) => ({
|
|
117
|
+
name: field.name,
|
|
118
|
+
target: target.field(type.id, field.name),
|
|
119
|
+
type: field.type,
|
|
120
|
+
})),
|
|
121
|
+
},
|
|
122
|
+
];
|
|
123
|
+
case "block": {
|
|
124
|
+
const block = blocks.get(type.block)!;
|
|
125
|
+
const native = block.standard && standardBlocks[block.standard];
|
|
126
|
+
const generated = Boolean(block.job || block.runtime);
|
|
127
|
+
const declaration = sources.range(block.span);
|
|
128
|
+
return [
|
|
129
|
+
type.id,
|
|
130
|
+
{
|
|
131
|
+
kind: "record",
|
|
132
|
+
name: block.job?.name ?? block.name,
|
|
133
|
+
...(declaration && { declaration }),
|
|
134
|
+
fields: [
|
|
135
|
+
...[...block.inputs, ...block.outputs, ...block.state].map(
|
|
136
|
+
(variable) => {
|
|
137
|
+
const declaration = !native && sources.range(variable.span);
|
|
138
|
+
return {
|
|
139
|
+
name: variable.name,
|
|
140
|
+
target: fields(block.id, variable.id),
|
|
141
|
+
type: variable.type,
|
|
142
|
+
...(declaration && { declaration }),
|
|
143
|
+
...(generated && { generated: true as const }),
|
|
144
|
+
};
|
|
145
|
+
},
|
|
146
|
+
),
|
|
147
|
+
...extras(block.id).map((field) => ({
|
|
148
|
+
...field,
|
|
149
|
+
generated: true as const,
|
|
150
|
+
})),
|
|
151
|
+
],
|
|
152
|
+
},
|
|
153
|
+
];
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
throw new Error(`Unsupported verified type ${type.id}`);
|
|
157
|
+
}),
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** A folded constant's JSON value; integers beyond doubles' exact range stay strings. */
|
|
162
|
+
const constantValue = (value: Constant): unknown => {
|
|
163
|
+
switch (value.kind) {
|
|
164
|
+
case "integer":
|
|
165
|
+
return Number.isSafeInteger(Number(value.value))
|
|
166
|
+
? Number(value.value)
|
|
167
|
+
: value.value;
|
|
168
|
+
case "duration":
|
|
169
|
+
return `${BigInt(value.nanoseconds) / nanosecondsPerMillisecond}ms`;
|
|
170
|
+
case "array":
|
|
171
|
+
return value.items.map(constantValue);
|
|
172
|
+
case "record":
|
|
173
|
+
return Object.fromEntries(
|
|
174
|
+
Object.entries(value.fields).map(([field, item]) => [
|
|
175
|
+
field,
|
|
176
|
+
constantValue(item),
|
|
177
|
+
]),
|
|
178
|
+
);
|
|
179
|
+
default:
|
|
180
|
+
return value.value;
|
|
181
|
+
}
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
/** For folded values without a declared type, such as strings: named by the IEC type that holds them. */
|
|
185
|
+
const constantTypes: Record<Constant["kind"], string> = {
|
|
186
|
+
bool: "BOOL",
|
|
187
|
+
integer: "LINT",
|
|
188
|
+
float: "LREAL",
|
|
189
|
+
duration: "TIME",
|
|
190
|
+
calendar: "DATE_AND_TIME",
|
|
191
|
+
string: "STRING",
|
|
192
|
+
array: "ARRAY",
|
|
193
|
+
record: "STRUCT",
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
const debugConstants = (
|
|
197
|
+
folded: NonNullable<PlcModule["folded"]>,
|
|
198
|
+
sources: Sources,
|
|
199
|
+
): DebugConstant[] =>
|
|
200
|
+
folded.map((item) => {
|
|
201
|
+
const declaration = sources.range(item.span);
|
|
202
|
+
return {
|
|
203
|
+
path: item.path,
|
|
204
|
+
type: item.type ?? constantTypes[item.value.kind],
|
|
205
|
+
value: constantValue(item.value),
|
|
206
|
+
...(declaration && { declaration }),
|
|
207
|
+
};
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
/** The `%I`/`%Q` address a configured binding uses: its own, or its device channel's. */
|
|
211
|
+
export const ioAddress = (
|
|
212
|
+
{ catalog }: ProjectHardware,
|
|
213
|
+
binding: ProjectHardware["config"]["io"][string],
|
|
214
|
+
) =>
|
|
215
|
+
"address" in binding
|
|
216
|
+
? binding.address
|
|
217
|
+
: catalog.devices
|
|
218
|
+
.find((device) => device.id === binding.device)
|
|
219
|
+
?.channels.find((item) => item.id === binding.channel)?.address;
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Each configured `io` binding joined with its device channel and the target that holds its
|
|
223
|
+
* value, and each device with its assemblies, named channels and driver state. A generated
|
|
224
|
+
* device tree names each adapter after its device, with a `<device>_Modbus` server for its
|
|
225
|
+
* registers, so the debugger reads their state there.
|
|
226
|
+
*/
|
|
227
|
+
function debugHardware(
|
|
228
|
+
hardware: ProjectHardware,
|
|
229
|
+
io: (key: string) => { target: string; type: string } | undefined,
|
|
230
|
+
/** The byte array over each of a device's assemblies. */
|
|
231
|
+
raw: (id: string) => Partial<Record<"input" | "output", string>>,
|
|
232
|
+
): { io: DebugIo[]; devices: DebugDevice[] } {
|
|
233
|
+
const { catalog, config } = hardware;
|
|
234
|
+
const rows = Object.entries(config.io).flatMap(
|
|
235
|
+
([key, binding]): DebugIo[] => {
|
|
236
|
+
const held = io(key);
|
|
237
|
+
if (!held) {
|
|
238
|
+
return [];
|
|
239
|
+
}
|
|
240
|
+
const address = ioAddress(hardware, binding);
|
|
241
|
+
return [
|
|
242
|
+
{
|
|
243
|
+
path: key,
|
|
244
|
+
...held,
|
|
245
|
+
direction: address?.startsWith("%Q") ? "output" : "input",
|
|
246
|
+
...("device" in binding && {
|
|
247
|
+
device: binding.device,
|
|
248
|
+
channel: binding.channel,
|
|
249
|
+
}),
|
|
250
|
+
...(address && { address }),
|
|
251
|
+
...(binding.inverted && { inverted: true as const }),
|
|
252
|
+
...(binding.shared && { shared: true as const }),
|
|
253
|
+
},
|
|
254
|
+
];
|
|
255
|
+
},
|
|
256
|
+
);
|
|
257
|
+
return {
|
|
258
|
+
io: rows,
|
|
259
|
+
devices: catalog.devices.map((device) => {
|
|
260
|
+
const descriptor = catalog.descriptors.find(
|
|
261
|
+
(item) => item.file === device.descriptor,
|
|
262
|
+
);
|
|
263
|
+
const network = config.devices[device.id]?.network;
|
|
264
|
+
const bytes = raw(device.id);
|
|
265
|
+
const status = {
|
|
266
|
+
...device.statusSymbols,
|
|
267
|
+
...(hardware.tree &&
|
|
268
|
+
device.modbus && {
|
|
269
|
+
"Modbus error": `${device.id}_Modbus.xError`,
|
|
270
|
+
"Modbus error code": `${device.id}_Modbus.byModbusError`,
|
|
271
|
+
}),
|
|
272
|
+
};
|
|
273
|
+
return {
|
|
274
|
+
id: device.id,
|
|
275
|
+
...(descriptor && {
|
|
276
|
+
product: descriptor.name,
|
|
277
|
+
vendor: descriptor.vendor,
|
|
278
|
+
productCode: descriptor.productCode,
|
|
279
|
+
}),
|
|
280
|
+
...(network?.address && {
|
|
281
|
+
network: {
|
|
282
|
+
address: network.address,
|
|
283
|
+
...(network.rpi && {
|
|
284
|
+
rpiMs: Number(BigInt(network.rpi.nanoseconds)) / 1e6,
|
|
285
|
+
}),
|
|
286
|
+
},
|
|
287
|
+
}),
|
|
288
|
+
...(device.connection && { connection: device.connection.name }),
|
|
289
|
+
assemblies: device.assemblies.map((assembly) => ({
|
|
290
|
+
direction: assembly.direction,
|
|
291
|
+
bytes: assembly.size,
|
|
292
|
+
address: assembly.address,
|
|
293
|
+
...(bytes[assembly.direction] && { raw: bytes[assembly.direction] }),
|
|
294
|
+
})),
|
|
295
|
+
...(hardware.tree && {
|
|
296
|
+
health: {
|
|
297
|
+
state: `${device.id}.eState`,
|
|
298
|
+
diagnostic: `${device.id}.xDiagnosticAvailable`,
|
|
299
|
+
message: `${device.id}.sDiagString`,
|
|
300
|
+
},
|
|
301
|
+
}),
|
|
302
|
+
...(Object.keys(status).length && { status }),
|
|
303
|
+
// Byte and bit expansions of an assembly are covered by its raw bytes.
|
|
304
|
+
channels: device.channels
|
|
305
|
+
.filter(
|
|
306
|
+
(channel) =>
|
|
307
|
+
!channel.parent &&
|
|
308
|
+
!/^(inputs|outputs)\.Byte\d+$/.test(channel.id),
|
|
309
|
+
)
|
|
310
|
+
.map((channel) => ({
|
|
311
|
+
id: channel.id,
|
|
312
|
+
name: channel.name,
|
|
313
|
+
direction: channel.direction,
|
|
314
|
+
type: channel.type,
|
|
315
|
+
offsetBits: channel.offsetBits,
|
|
316
|
+
bits: channel.bits,
|
|
317
|
+
...(channel.address && { address: channel.address }),
|
|
318
|
+
...(channel.unit && { unit: channel.unit }),
|
|
319
|
+
...(channel.description && { description: channel.description }),
|
|
320
|
+
})),
|
|
321
|
+
};
|
|
322
|
+
}),
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/** The IEC path of a block instance. */
|
|
327
|
+
function instancePath(symbols: CodesysSymbols, path: InstancePath): string {
|
|
328
|
+
let text = "";
|
|
329
|
+
let holder: string | undefined;
|
|
330
|
+
for (const { variable, index } of path) {
|
|
331
|
+
text += holder
|
|
332
|
+
? `.${required(required(symbols.blockFields.get(holder)).get(variable.id))}`
|
|
333
|
+
: required(symbols.references.get(variable.id));
|
|
334
|
+
if (index !== undefined) {
|
|
335
|
+
text += `[${index}]`;
|
|
336
|
+
}
|
|
337
|
+
holder = required(instancedBlock(variable.type, symbols.typeMap)).block;
|
|
338
|
+
}
|
|
339
|
+
return text;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/** Service calls a job instance makes, down to (not into) the jobs it starts itself. */
|
|
343
|
+
function requests(
|
|
344
|
+
context: EmissionContext,
|
|
345
|
+
id: string,
|
|
346
|
+
prefix: string,
|
|
347
|
+
job?: number,
|
|
348
|
+
): DebugJob["requests"] {
|
|
349
|
+
const { plan, symbols, instrumentation } = context;
|
|
350
|
+
const block = required(symbols.blocks.get(id));
|
|
351
|
+
return block.state.flatMap((child) => {
|
|
352
|
+
const instance = instancedBlock(child.type, symbols.typeMap);
|
|
353
|
+
if (!instance) {
|
|
354
|
+
return [];
|
|
355
|
+
}
|
|
356
|
+
// A nested job tags the calls below it with its own id.
|
|
357
|
+
const caller = instrumentation.jobIds.get(child.id) ?? job;
|
|
358
|
+
const type = required(symbols.typeMap.get(child.type));
|
|
359
|
+
const field = `${prefix}.${required(required(symbols.blockFields.get(id)).get(child.id))}`;
|
|
360
|
+
const targets =
|
|
361
|
+
type.kind === "array"
|
|
362
|
+
? Array.from(
|
|
363
|
+
{ length: type.length },
|
|
364
|
+
(_, index) => `${field}[${type.lowerBound + index}]`,
|
|
365
|
+
)
|
|
366
|
+
: [field];
|
|
367
|
+
const sequence = instrumentation.fields.get(instance.block)?.sequence;
|
|
368
|
+
const path = plan.http[instance.block]?.path;
|
|
369
|
+
return targets.flatMap((target) =>
|
|
370
|
+
sequence && path
|
|
371
|
+
? [
|
|
372
|
+
{
|
|
373
|
+
service: serviceOfPath(path) ?? path,
|
|
374
|
+
target,
|
|
375
|
+
sequence: `${target}.${sequence}`,
|
|
376
|
+
...(caller !== undefined && { job: caller }),
|
|
377
|
+
},
|
|
378
|
+
]
|
|
379
|
+
: requests(context, instance.block, target, caller),
|
|
380
|
+
);
|
|
381
|
+
});
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/** Static member reads in a body, by TypeScript path. */
|
|
385
|
+
function reads(
|
|
386
|
+
instrumentation: Instrumentation,
|
|
387
|
+
body: readonly CoreStatement[],
|
|
388
|
+
sources: Sources,
|
|
389
|
+
): DebugFunction["reads"] {
|
|
390
|
+
const reads = new Map<string, DebugFunction["reads"][number]>();
|
|
391
|
+
const expression = (value: CoreExpression): void => {
|
|
392
|
+
// A borrow is not a read, and the manifest does not list the indexes of its place either.
|
|
393
|
+
if (value.kind === "borrow") {
|
|
394
|
+
return;
|
|
395
|
+
}
|
|
396
|
+
subexpressions(value).forEach(expression);
|
|
397
|
+
if (value.kind !== "load") {
|
|
398
|
+
return;
|
|
399
|
+
}
|
|
400
|
+
const read = storagePath(
|
|
401
|
+
required(instrumentation.analysis.variables.get(value.root)),
|
|
402
|
+
value.path,
|
|
403
|
+
);
|
|
404
|
+
const range = read && sources.range(value.span);
|
|
405
|
+
if (range) {
|
|
406
|
+
reads.set(`${JSON.stringify(range)}${read}`, { range, path: read });
|
|
407
|
+
}
|
|
408
|
+
};
|
|
409
|
+
const statements = (items: readonly CoreStatement[]): void => {
|
|
410
|
+
for (const item of items) {
|
|
411
|
+
statementExpressions(item).forEach(expression);
|
|
412
|
+
nestedBodies(item).forEach(statements);
|
|
413
|
+
}
|
|
414
|
+
};
|
|
415
|
+
statements(body);
|
|
416
|
+
return [...reads.values()];
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/** The IEC name of a routine: its function block, a method under its block, or its function or PROGRAM. */
|
|
420
|
+
const routineTarget = (
|
|
421
|
+
symbols: CodesysSymbols,
|
|
422
|
+
routine: CoreFunction | CoreFunctionBlock,
|
|
423
|
+
): string => {
|
|
424
|
+
if ("inputs" in routine) {
|
|
425
|
+
return required(symbols.blockNames.get(routine.id));
|
|
426
|
+
}
|
|
427
|
+
const name = required(symbols.functionNames.get(routine.id));
|
|
428
|
+
return routine.owner
|
|
429
|
+
? `${required(symbols.blockNames.get(routine.owner.block))}.${name}`
|
|
430
|
+
: name;
|
|
431
|
+
};
|
|
432
|
+
|
|
433
|
+
/** The debugger's view of an emitted program; its build id is filled in once the id is known. */
|
|
434
|
+
export function debugManifest(
|
|
435
|
+
context: EmissionContext,
|
|
436
|
+
ioTarget: (key: string) => { target: string; type: string } | undefined,
|
|
437
|
+
): DebugManifest {
|
|
438
|
+
const { plan, symbols, instrumentation } = context;
|
|
439
|
+
const { core, project, deployment, options } = plan;
|
|
440
|
+
const settings = project.debug;
|
|
441
|
+
const sources = new Sources(project.root);
|
|
442
|
+
const global = (field: string) => `${instrumentation.global}.${field}`;
|
|
443
|
+
|
|
444
|
+
const declaredFunctions = new Map<
|
|
445
|
+
string,
|
|
446
|
+
{ id: number; range: SourceRange }
|
|
447
|
+
>();
|
|
448
|
+
const functions = [
|
|
449
|
+
...core.functions,
|
|
450
|
+
...core.blocks.filter((block) => !block.standard && !block.runtime),
|
|
451
|
+
].flatMap((routine): DebugFunction[] => {
|
|
452
|
+
const block = "inputs" in routine ? routine : undefined;
|
|
453
|
+
const fn = "parameters" in routine ? routine : undefined;
|
|
454
|
+
const declaration = sources.range(block?.job?.span ?? routine.span);
|
|
455
|
+
if (!declaration) {
|
|
456
|
+
return [];
|
|
457
|
+
}
|
|
458
|
+
const id = declaredFunctions.size;
|
|
459
|
+
declaredFunctions.set(routine.id, { id, range: declaration });
|
|
460
|
+
const task = instrumentation.analysis.tasks.get(routine.id);
|
|
461
|
+
const origin = fn?.origin;
|
|
462
|
+
return [
|
|
463
|
+
{
|
|
464
|
+
id,
|
|
465
|
+
name: block?.job?.name ?? origin?.name ?? routine.name,
|
|
466
|
+
declaration,
|
|
467
|
+
...(origin?.instance && { instance: origin.instance }),
|
|
468
|
+
...(task && { task }),
|
|
469
|
+
target: routineTarget(symbols, routine),
|
|
470
|
+
reads: reads(instrumentation, routine.body, sources),
|
|
471
|
+
},
|
|
472
|
+
];
|
|
473
|
+
});
|
|
474
|
+
const declared = (routine: string | undefined) =>
|
|
475
|
+
routine === undefined ? undefined : declaredFunctions.get(routine);
|
|
476
|
+
|
|
477
|
+
const events = instrumentation.analysis.events.flatMap(
|
|
478
|
+
(site): DebugEventSite[] => {
|
|
479
|
+
const { job } = site;
|
|
480
|
+
const owner = declared(site.routine);
|
|
481
|
+
const range = sources.range(site.span) ?? owner?.range;
|
|
482
|
+
if (!range) {
|
|
483
|
+
return [];
|
|
484
|
+
}
|
|
485
|
+
return [
|
|
486
|
+
{
|
|
487
|
+
id: site.id,
|
|
488
|
+
kind: site.kind,
|
|
489
|
+
...(job && {
|
|
490
|
+
job:
|
|
491
|
+
instrumentation.jobs.findIndex(
|
|
492
|
+
({ block }) => block.id === job.block,
|
|
493
|
+
) + 1,
|
|
494
|
+
phase: job.phase,
|
|
495
|
+
}),
|
|
496
|
+
path: site.path,
|
|
497
|
+
...(owner && { function: owner.id }),
|
|
498
|
+
type: site.type,
|
|
499
|
+
range,
|
|
500
|
+
text: site.text ?? sources.text(site.span) ?? "",
|
|
501
|
+
task: site.task,
|
|
502
|
+
operands: site.operands.map((operand) => ({
|
|
503
|
+
text: sources.text(operand.span) ?? "",
|
|
504
|
+
range: sources.range(operand.span) ?? range,
|
|
505
|
+
type: operand.capture.type,
|
|
506
|
+
...(operand.path && { path: operand.path }),
|
|
507
|
+
stable: operand.stable,
|
|
508
|
+
})),
|
|
509
|
+
},
|
|
510
|
+
];
|
|
511
|
+
},
|
|
512
|
+
);
|
|
513
|
+
|
|
514
|
+
const jobs = instrumentation.jobs.flatMap(
|
|
515
|
+
({ block, path }, index): DebugJob[] => {
|
|
516
|
+
const job = required(block.job);
|
|
517
|
+
const declaration = sources.range(job.span);
|
|
518
|
+
if (!declaration) {
|
|
519
|
+
return [];
|
|
520
|
+
}
|
|
521
|
+
const target = instancePath(symbols, path);
|
|
522
|
+
const startedAt = instrumentation.fields.get(block.id)?.startedAt;
|
|
523
|
+
return [
|
|
524
|
+
{
|
|
525
|
+
id: index + 1,
|
|
526
|
+
name: job.name,
|
|
527
|
+
declaration,
|
|
528
|
+
...(job.instance && { instance: job.instance }),
|
|
529
|
+
target,
|
|
530
|
+
steps: job.steps.map((step) => ({
|
|
531
|
+
step: step.step,
|
|
532
|
+
kind: step.kind,
|
|
533
|
+
range: sources.range(step.span) ?? declaration,
|
|
534
|
+
text: step.text,
|
|
535
|
+
})),
|
|
536
|
+
...(startedAt && { startedAt: `${target}.${startedAt}` }),
|
|
537
|
+
requests: requests(context, block.id, target),
|
|
538
|
+
// Compiler-made functions, such as each service call's own job, carry its prefix.
|
|
539
|
+
...(generated(job.name) && { generated: true as const }),
|
|
540
|
+
},
|
|
541
|
+
];
|
|
542
|
+
},
|
|
543
|
+
);
|
|
544
|
+
|
|
545
|
+
const writers = settings?.writers
|
|
546
|
+
? instrumentation.analysis.outputs.map((output, index) => ({
|
|
547
|
+
output: output.key,
|
|
548
|
+
target: global(`${instrumentation.slots.writers}[${index}]`),
|
|
549
|
+
sites: required(instrumentation.analysis.writers[index]).flatMap(
|
|
550
|
+
(site) => {
|
|
551
|
+
const owner = declared(site.routine);
|
|
552
|
+
return owner
|
|
553
|
+
? [
|
|
554
|
+
{
|
|
555
|
+
id: site.id,
|
|
556
|
+
range: sources.range(site.span) ?? owner.range,
|
|
557
|
+
function: owner.id,
|
|
558
|
+
},
|
|
559
|
+
]
|
|
560
|
+
: [];
|
|
561
|
+
},
|
|
562
|
+
),
|
|
563
|
+
}))
|
|
564
|
+
: [];
|
|
565
|
+
|
|
566
|
+
const roots = core.variables
|
|
567
|
+
.filter((variable) =>
|
|
568
|
+
["module", "instance", "input", "output"].includes(variable.storage),
|
|
569
|
+
)
|
|
570
|
+
.map((variable) => {
|
|
571
|
+
const declaration = sources.range(variable.span);
|
|
572
|
+
return {
|
|
573
|
+
path: required(storagePath(variable)),
|
|
574
|
+
target: required(symbols.references.get(variable.id)),
|
|
575
|
+
type: variable.type,
|
|
576
|
+
...(declaration && { declaration }),
|
|
577
|
+
readable: true,
|
|
578
|
+
...(variable.name.startsWith("tsplc") && {
|
|
579
|
+
generated: true as const,
|
|
580
|
+
}),
|
|
581
|
+
};
|
|
582
|
+
});
|
|
583
|
+
|
|
584
|
+
const types = debugTypes(
|
|
585
|
+
core.types,
|
|
586
|
+
symbols.targetTypes,
|
|
587
|
+
symbols.blocks,
|
|
588
|
+
(block, variable) =>
|
|
589
|
+
required(required(symbols.blockFields.get(block)).get(variable)),
|
|
590
|
+
(block) => {
|
|
591
|
+
const fields = instrumentation.fields.get(block);
|
|
592
|
+
return (
|
|
593
|
+
["busy", "step", "startedAt", "job", "sequence"] as const
|
|
594
|
+
).flatMap((key) => {
|
|
595
|
+
const name = fields?.[key];
|
|
596
|
+
return name
|
|
597
|
+
? [{ name, target: name, type: instrumentationFieldTypes[key] }]
|
|
598
|
+
: [];
|
|
599
|
+
});
|
|
600
|
+
},
|
|
601
|
+
sources,
|
|
602
|
+
);
|
|
603
|
+
const hardware = debugHardware(
|
|
604
|
+
project.hardware ?? {
|
|
605
|
+
catalog: { descriptors: [], devices: [] },
|
|
606
|
+
config: { devices: {}, io: options.io },
|
|
607
|
+
tree: false,
|
|
608
|
+
},
|
|
609
|
+
(key) => ioTarget(key),
|
|
610
|
+
(id) =>
|
|
611
|
+
Object.fromEntries(
|
|
612
|
+
Object.entries(instrumentation.devices.get(id) ?? {}).map(
|
|
613
|
+
([direction, raw]) => [direction, global(raw.field)],
|
|
614
|
+
),
|
|
615
|
+
),
|
|
616
|
+
);
|
|
617
|
+
const constants = debugConstants(core.folded ?? [], sources);
|
|
618
|
+
|
|
619
|
+
return {
|
|
620
|
+
kind: "tsplc-debug",
|
|
621
|
+
version: 1,
|
|
622
|
+
build: {
|
|
623
|
+
id: "",
|
|
624
|
+
target: global("buildId"),
|
|
625
|
+
compiler: `@tsplc/compiler ${version}`,
|
|
626
|
+
},
|
|
627
|
+
sources: sources.files,
|
|
628
|
+
types,
|
|
629
|
+
roots,
|
|
630
|
+
constants,
|
|
631
|
+
...hardware,
|
|
632
|
+
functions,
|
|
633
|
+
jobs,
|
|
634
|
+
events,
|
|
635
|
+
writers,
|
|
636
|
+
tasks: deployment.tasks.map((task) => ({
|
|
637
|
+
name: task.name,
|
|
638
|
+
intervalMs: Number(task.intervalNanoseconds) / 1e6,
|
|
639
|
+
priority: task.priority,
|
|
640
|
+
})),
|
|
641
|
+
errors: jobErrors,
|
|
642
|
+
};
|
|
643
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { elementaryTypesByName } from "@tsplc/iec";
|
|
2
|
+
import { reservedWords } from "@tsplc/iec/st";
|
|
3
|
+
|
|
4
|
+
// CODESYS reserved words, plus the elementary-type names and operators it also
|
|
5
|
+
// rejects as identifiers.
|
|
6
|
+
// https://content.helpme-codesys.com/en/CODESYS%20Development%20System/_cds_keywords.html
|
|
7
|
+
const keywords = new Set([
|
|
8
|
+
...reservedWords,
|
|
9
|
+
...elementaryTypesByName.keys(),
|
|
10
|
+
...`
|
|
11
|
+
ANY ANY_BIT ANY_DATE ANY_DERIVED ANY_ELEMENTARY ANY_INT ANY_MAGNITUDE ANY_NUM
|
|
12
|
+
ANY_REAL ANY_STRING ANY_UNSIGNED
|
|
13
|
+
ADD SUB MUL DIV MOVE SEL MIN MAX LIMIT
|
|
14
|
+
ABS ACOS ASIN ATAN COS SIN TAN EXP EXPT LN LOG SQRT TRUNC SHL SHR ROL ROR
|
|
15
|
+
ADR SIZEOF CONCAT DELETE FIND INSERT LEFT LEN MID REPLACE RIGHT
|
|
16
|
+
`
|
|
17
|
+
.trim()
|
|
18
|
+
.split(/\s+/),
|
|
19
|
+
]);
|
|
20
|
+
const types = `(?:${[...elementaryTypesByName.keys()].join("|")})`;
|
|
21
|
+
const conversion = new RegExp(
|
|
22
|
+
`^(?:${types}_TO_${types}|TO_${types}|TRUNC_${types})$`,
|
|
23
|
+
);
|
|
24
|
+
export function isIecIdentifier(name: string): boolean {
|
|
25
|
+
const upper = name.toUpperCase();
|
|
26
|
+
return (
|
|
27
|
+
/^[A-Za-z][A-Za-z0-9_]*$/.test(name) &&
|
|
28
|
+
!name.includes("__") &&
|
|
29
|
+
!keywords.has(upper) &&
|
|
30
|
+
!conversion.test(upper)
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const identifierBody = (label: string): string =>
|
|
35
|
+
label
|
|
36
|
+
.replace(/[^A-Za-z0-9_]/g, "_")
|
|
37
|
+
.replace(/_+/g, "_")
|
|
38
|
+
.replace(/^_+|_+$/g, "") || "name";
|
|
39
|
+
|
|
40
|
+
/** Capitals inside a word are kept, so `PlantIO` stays as written while `plant-io` is `PlantIo`. */
|
|
41
|
+
export const pascalCase = (label: string): string =>
|
|
42
|
+
label
|
|
43
|
+
.split(/[^A-Za-z0-9]+/)
|
|
44
|
+
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
|
|
45
|
+
.join("");
|
|
46
|
+
|
|
47
|
+
export class Names {
|
|
48
|
+
private readonly used = new Set<string>();
|
|
49
|
+
allocateObject(
|
|
50
|
+
prefix: "FB" | "F" | "PRG" | "ST" | "E" | "GVL",
|
|
51
|
+
label: string,
|
|
52
|
+
): string {
|
|
53
|
+
const source = prefix === "F" ? label.replace(/^PRG_/, "") : label;
|
|
54
|
+
const kind = prefix === "F" && source.startsWith("FUN_") ? "FUN" : prefix;
|
|
55
|
+
const body = source.startsWith(`${kind}_`)
|
|
56
|
+
? source.slice(kind.length + 1)
|
|
57
|
+
: source;
|
|
58
|
+
return this.allocate(`${kind}_${identifierBody(body)}`);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
reserve(name: string): void {
|
|
62
|
+
this.used.add(name.toUpperCase());
|
|
63
|
+
// Qualified native types must keep their library prefix resolvable in the
|
|
64
|
+
// declaration scope, not merely reserve the complete dotted spelling.
|
|
65
|
+
const prefix = name.split(".")[0];
|
|
66
|
+
if (prefix) {
|
|
67
|
+
this.used.add(prefix.toUpperCase());
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
allocate(label: string): string {
|
|
71
|
+
const base = isIecIdentifier(label)
|
|
72
|
+
? label
|
|
73
|
+
: `plc_${identifierBody(label)}`;
|
|
74
|
+
let name = base;
|
|
75
|
+
for (let suffix = 2; this.used.has(name.toUpperCase()); suffix++) {
|
|
76
|
+
name = `${base}_${suffix}`;
|
|
77
|
+
}
|
|
78
|
+
this.reserve(name);
|
|
79
|
+
return name;
|
|
80
|
+
}
|
|
81
|
+
}
|