@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,684 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type Constant,
|
|
3
|
+
type CoreExpression,
|
|
4
|
+
type CoreFunction,
|
|
5
|
+
type CoreFunctionBlock,
|
|
6
|
+
type CoreStatement,
|
|
7
|
+
coreTables,
|
|
8
|
+
instancedBlock,
|
|
9
|
+
instanceFields,
|
|
10
|
+
isBlockRoutine,
|
|
11
|
+
jobErrors,
|
|
12
|
+
nestedBodies,
|
|
13
|
+
type PlcModule,
|
|
14
|
+
type PlcType,
|
|
15
|
+
standardBlocks,
|
|
16
|
+
statementExpressions,
|
|
17
|
+
subexpressions,
|
|
18
|
+
type Variable,
|
|
19
|
+
} from "@tsplc/compiler/ir";
|
|
20
|
+
import type { ResolvedTsplcConfig } from "@tsplc/config";
|
|
21
|
+
import { encodeConstant } from "@tsplc/debug/constant";
|
|
22
|
+
import {
|
|
23
|
+
type DebugConstant,
|
|
24
|
+
type DebugDevice,
|
|
25
|
+
type DebugEventSite,
|
|
26
|
+
type DebugFunction,
|
|
27
|
+
type DebugIo,
|
|
28
|
+
type DebugJob,
|
|
29
|
+
type DebugManifest,
|
|
30
|
+
type DebugType,
|
|
31
|
+
instrumentationFields,
|
|
32
|
+
type SourceRange,
|
|
33
|
+
} from "@tsplc/debug/manifest";
|
|
34
|
+
import { nanosecondsPerMillisecond } from "@tsplc/iec";
|
|
35
|
+
import {
|
|
36
|
+
deviceChannel,
|
|
37
|
+
type HardwareCatalog,
|
|
38
|
+
isOutputAddress,
|
|
39
|
+
isPayloadByte,
|
|
40
|
+
modbusServerName,
|
|
41
|
+
} from "@tsplc/typegen/hardware";
|
|
42
|
+
import { version } from "../../package.json";
|
|
43
|
+
import { type InstancePath, typescriptPath } from "./debug";
|
|
44
|
+
import { ioTarget } from "./global-lists";
|
|
45
|
+
import { serviceName } from "./http";
|
|
46
|
+
import type { Instrumentation } from "./instrumentation";
|
|
47
|
+
import { required } from "./required";
|
|
48
|
+
import type { Sources } from "./sources";
|
|
49
|
+
import type { CodesysSymbols, EmissionContext } from "./symbols";
|
|
50
|
+
import { generated } from "./telemetry";
|
|
51
|
+
import { sourceTypeName, stringTypeName } from "./types";
|
|
52
|
+
|
|
53
|
+
/** The configured hardware a manifest describes; `tree` when this build generates the device tree. */
|
|
54
|
+
export interface ProjectHardware {
|
|
55
|
+
catalog: HardwareCatalog;
|
|
56
|
+
config: Pick<ResolvedTsplcConfig, "devices" | "io">;
|
|
57
|
+
tree: boolean;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* A value type as the debugger reads it, with TypeScript names; `field` gives a record field's name
|
|
62
|
+
* in the target.
|
|
63
|
+
*/
|
|
64
|
+
export function dataDebugType(
|
|
65
|
+
type: Exclude<PlcType, { kind: "block" }>,
|
|
66
|
+
field: (record: string, name: string) => string,
|
|
67
|
+
): DebugType {
|
|
68
|
+
switch (type.kind) {
|
|
69
|
+
case "bool":
|
|
70
|
+
case "integer":
|
|
71
|
+
case "bits":
|
|
72
|
+
case "float":
|
|
73
|
+
case "duration":
|
|
74
|
+
case "calendar":
|
|
75
|
+
return { kind: "scalar", iec: type.id };
|
|
76
|
+
case "string":
|
|
77
|
+
return {
|
|
78
|
+
kind: "scalar",
|
|
79
|
+
iec: stringTypeName(type),
|
|
80
|
+
capacity: type.capacity,
|
|
81
|
+
};
|
|
82
|
+
case "enum": {
|
|
83
|
+
const texts = type.members.flatMap((member) =>
|
|
84
|
+
member.text === undefined ? [] : [[member.name, member.text]],
|
|
85
|
+
);
|
|
86
|
+
return {
|
|
87
|
+
kind: "enum",
|
|
88
|
+
name: sourceTypeName(type.id),
|
|
89
|
+
members: Object.fromEntries(
|
|
90
|
+
type.members.map((member) => [member.name, Number(member.value)]),
|
|
91
|
+
),
|
|
92
|
+
...(texts.length && { texts: Object.fromEntries(texts) }),
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
case "array":
|
|
96
|
+
return {
|
|
97
|
+
kind: "array",
|
|
98
|
+
element: type.element,
|
|
99
|
+
length: type.length,
|
|
100
|
+
lower: type.lowerBound,
|
|
101
|
+
...(type.index && { index: type.index }),
|
|
102
|
+
};
|
|
103
|
+
case "record":
|
|
104
|
+
return {
|
|
105
|
+
kind: "record",
|
|
106
|
+
name: sourceTypeName(type.id),
|
|
107
|
+
fields: type.fields.map((item) => ({
|
|
108
|
+
name: item.name,
|
|
109
|
+
target: field(type.id, item.name),
|
|
110
|
+
type: item.type,
|
|
111
|
+
})),
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** A block instance's type: its ports and state, then the fields the compiler adds for the debugger. */
|
|
117
|
+
function blockDebugType(
|
|
118
|
+
{ symbols, instrumentation }: EmissionContext,
|
|
119
|
+
block: CoreFunctionBlock,
|
|
120
|
+
sources: Sources,
|
|
121
|
+
): DebugType {
|
|
122
|
+
const native = block.standard && standardBlocks[block.standard];
|
|
123
|
+
const generated = Boolean(block.job || block.runtime);
|
|
124
|
+
const declaration = sources.range(block.span);
|
|
125
|
+
const fields = required(symbols.blockFields.get(block.id));
|
|
126
|
+
const added = new Map(
|
|
127
|
+
Object.entries(instrumentation.fields.get(block.id) ?? {}),
|
|
128
|
+
);
|
|
129
|
+
return {
|
|
130
|
+
kind: "record",
|
|
131
|
+
name: block.job?.name ?? block.name,
|
|
132
|
+
...(declaration && { declaration }),
|
|
133
|
+
fields: [
|
|
134
|
+
...instanceFields(block).map((variable) => {
|
|
135
|
+
const declaration = !native && sources.range(variable.span);
|
|
136
|
+
return {
|
|
137
|
+
name: variable.name,
|
|
138
|
+
target: required(fields.get(variable.id)),
|
|
139
|
+
type: variable.type,
|
|
140
|
+
...(declaration && { declaration }),
|
|
141
|
+
...(generated && { generated: true as const }),
|
|
142
|
+
};
|
|
143
|
+
}),
|
|
144
|
+
...Object.entries(instrumentationFields).flatMap(([key, type]) => {
|
|
145
|
+
const name = added.get(key);
|
|
146
|
+
return name
|
|
147
|
+
? [{ name, target: name, type, generated: true as const }]
|
|
148
|
+
: [];
|
|
149
|
+
}),
|
|
150
|
+
],
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** A folded constant's JSON value. */
|
|
155
|
+
export const constantValue = (value: Constant): unknown => {
|
|
156
|
+
switch (value.kind) {
|
|
157
|
+
case "integer":
|
|
158
|
+
return encodeConstant({ kind: "integer", value: BigInt(value.value) });
|
|
159
|
+
case "duration":
|
|
160
|
+
return encodeConstant({
|
|
161
|
+
kind: "duration",
|
|
162
|
+
nanoseconds: BigInt(value.nanoseconds),
|
|
163
|
+
});
|
|
164
|
+
case "array":
|
|
165
|
+
return value.items.map(constantValue);
|
|
166
|
+
case "record":
|
|
167
|
+
return Object.fromEntries(
|
|
168
|
+
Object.entries(value.fields).map(([field, item]) => [
|
|
169
|
+
field,
|
|
170
|
+
constantValue(item),
|
|
171
|
+
]),
|
|
172
|
+
);
|
|
173
|
+
default:
|
|
174
|
+
return value.value;
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
/** For folded values without a declared type, such as strings: named by the IEC type that holds them. */
|
|
179
|
+
const constantTypes: Record<Constant["kind"], string> = {
|
|
180
|
+
bool: "BOOL",
|
|
181
|
+
integer: "LINT",
|
|
182
|
+
float: "LREAL",
|
|
183
|
+
duration: "TIME",
|
|
184
|
+
calendar: "DATE_AND_TIME",
|
|
185
|
+
string: "STRING",
|
|
186
|
+
array: "ARRAY",
|
|
187
|
+
record: "STRUCT",
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
const debugConstants = (
|
|
191
|
+
folded: NonNullable<PlcModule["folded"]>,
|
|
192
|
+
sources: Sources,
|
|
193
|
+
): DebugConstant[] =>
|
|
194
|
+
folded.map((item) => {
|
|
195
|
+
const declaration = sources.range(item.span);
|
|
196
|
+
return {
|
|
197
|
+
path: item.path,
|
|
198
|
+
type: item.type ?? constantTypes[item.value.kind],
|
|
199
|
+
value: constantValue(item.value),
|
|
200
|
+
...(declaration && { declaration }),
|
|
201
|
+
};
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
/** The `%I`/`%Q` address a configured binding uses: its own, or its device channel's. */
|
|
205
|
+
export const ioAddress = (
|
|
206
|
+
{ catalog }: ProjectHardware,
|
|
207
|
+
binding: ProjectHardware["config"]["io"][string],
|
|
208
|
+
) =>
|
|
209
|
+
"address" in binding
|
|
210
|
+
? binding.address
|
|
211
|
+
: deviceChannel(catalog, binding)?.address;
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Each configured `io` binding joined with its device channel and the target that holds its
|
|
215
|
+
* value, and each device with its assemblies, named channels and driver state. A generated
|
|
216
|
+
* device tree names each adapter after its device, with a `<device>_Modbus` server for its
|
|
217
|
+
* registers, so the debugger reads their state there.
|
|
218
|
+
*/
|
|
219
|
+
function debugHardware(
|
|
220
|
+
context: EmissionContext,
|
|
221
|
+
hardware: ProjectHardware,
|
|
222
|
+
): { io: DebugIo[]; devices: DebugDevice[] } {
|
|
223
|
+
const { instrumentation } = context;
|
|
224
|
+
const { catalog, config } = hardware;
|
|
225
|
+
const rows = Object.entries(config.io).flatMap(
|
|
226
|
+
([key, binding]): DebugIo[] => {
|
|
227
|
+
const held = ioTarget(context, key);
|
|
228
|
+
if (!held) {
|
|
229
|
+
return [];
|
|
230
|
+
}
|
|
231
|
+
const address = ioAddress(hardware, binding);
|
|
232
|
+
return [
|
|
233
|
+
{
|
|
234
|
+
path: key,
|
|
235
|
+
...held,
|
|
236
|
+
direction:
|
|
237
|
+
address !== undefined && isOutputAddress(address)
|
|
238
|
+
? "output"
|
|
239
|
+
: "input",
|
|
240
|
+
...("device" in binding && {
|
|
241
|
+
device: binding.device,
|
|
242
|
+
channel: binding.channel,
|
|
243
|
+
}),
|
|
244
|
+
...(address && { address }),
|
|
245
|
+
...(binding.inverted && { inverted: true as const }),
|
|
246
|
+
...(binding.shared && { shared: true as const }),
|
|
247
|
+
},
|
|
248
|
+
];
|
|
249
|
+
},
|
|
250
|
+
);
|
|
251
|
+
return {
|
|
252
|
+
io: rows,
|
|
253
|
+
devices: catalog.devices.map((device) => {
|
|
254
|
+
const descriptor = catalog.descriptors.find(
|
|
255
|
+
(item) => item.file === device.descriptor,
|
|
256
|
+
);
|
|
257
|
+
const network = config.devices[device.id]?.network;
|
|
258
|
+
const raw = instrumentation.devices.get(device.id);
|
|
259
|
+
const status = {
|
|
260
|
+
...device.statusSymbols,
|
|
261
|
+
...(hardware.tree &&
|
|
262
|
+
device.modbus && {
|
|
263
|
+
"Modbus error": `${modbusServerName(device.id)}.xError`,
|
|
264
|
+
"Modbus error code": `${modbusServerName(device.id)}.byModbusError`,
|
|
265
|
+
}),
|
|
266
|
+
};
|
|
267
|
+
return {
|
|
268
|
+
id: device.id,
|
|
269
|
+
...(descriptor && {
|
|
270
|
+
product: descriptor.name,
|
|
271
|
+
vendor: descriptor.vendor,
|
|
272
|
+
productCode: descriptor.productCode,
|
|
273
|
+
}),
|
|
274
|
+
...(network?.address && {
|
|
275
|
+
network: {
|
|
276
|
+
address: network.address,
|
|
277
|
+
...(network.rpi && {
|
|
278
|
+
rpiMs:
|
|
279
|
+
Number(network.rpi.nanoseconds) /
|
|
280
|
+
Number(nanosecondsPerMillisecond),
|
|
281
|
+
}),
|
|
282
|
+
},
|
|
283
|
+
}),
|
|
284
|
+
...(device.connection && { connection: device.connection.name }),
|
|
285
|
+
assemblies: device.assemblies.map((assembly) => {
|
|
286
|
+
const field = raw?.[assembly.direction]?.field;
|
|
287
|
+
return {
|
|
288
|
+
direction: assembly.direction,
|
|
289
|
+
bytes: assembly.size,
|
|
290
|
+
address: assembly.address,
|
|
291
|
+
...(field && { raw: `${instrumentation.global}.${field}` }),
|
|
292
|
+
};
|
|
293
|
+
}),
|
|
294
|
+
...(hardware.tree && {
|
|
295
|
+
health: {
|
|
296
|
+
state: `${device.id}.eState`,
|
|
297
|
+
diagnostic: `${device.id}.xDiagnosticAvailable`,
|
|
298
|
+
message: `${device.id}.sDiagString`,
|
|
299
|
+
},
|
|
300
|
+
}),
|
|
301
|
+
...(Object.keys(status).length && { status }),
|
|
302
|
+
// Byte and bit expansions of an assembly are covered by its raw bytes.
|
|
303
|
+
channels: device.channels
|
|
304
|
+
.filter((channel) => !channel.parent && !isPayloadByte(channel))
|
|
305
|
+
.map((channel) => ({
|
|
306
|
+
id: channel.id,
|
|
307
|
+
name: channel.name,
|
|
308
|
+
direction: channel.direction,
|
|
309
|
+
type: channel.type,
|
|
310
|
+
offsetBits: channel.offsetBits,
|
|
311
|
+
bits: channel.bits,
|
|
312
|
+
...(channel.address && { address: channel.address }),
|
|
313
|
+
...(channel.unit && { unit: channel.unit }),
|
|
314
|
+
...(channel.description && { description: channel.description }),
|
|
315
|
+
})),
|
|
316
|
+
};
|
|
317
|
+
}),
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/** The IEC path of a block instance. */
|
|
322
|
+
function instancePath(symbols: CodesysSymbols, path: InstancePath): string {
|
|
323
|
+
return path
|
|
324
|
+
.map(({ variable, index }, position) => {
|
|
325
|
+
const holder = path[position - 1];
|
|
326
|
+
const name = holder
|
|
327
|
+
? `.${required(
|
|
328
|
+
required(
|
|
329
|
+
symbols.blockFields.get(
|
|
330
|
+
required(instancedBlock(holder.variable.type, symbols.typeMap))
|
|
331
|
+
.block,
|
|
332
|
+
),
|
|
333
|
+
).get(variable.id),
|
|
334
|
+
)}`
|
|
335
|
+
: required(symbols.references.get(variable.id));
|
|
336
|
+
return index === undefined ? name : `${name}[${index}]`;
|
|
337
|
+
})
|
|
338
|
+
.join("");
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/**
|
|
342
|
+
* The job a service call is made for in debug.json: the id the PLC sends with the call's request,
|
|
343
|
+
* which only a job declared once passes down, so such a job nested in another replaces its caller's.
|
|
344
|
+
*/
|
|
345
|
+
const requestingJob = (
|
|
346
|
+
{ jobIds }: Pick<Instrumentation, "jobIds">,
|
|
347
|
+
declaration: string,
|
|
348
|
+
caller: number | undefined,
|
|
349
|
+
): number | undefined => jobIds.get(declaration) ?? caller;
|
|
350
|
+
|
|
351
|
+
/** The job a service call is made for in explain.json: the innermost job instance whose path holds the call's. */
|
|
352
|
+
export const enclosingJob = (
|
|
353
|
+
{ jobs }: Pick<Instrumentation, "jobs">,
|
|
354
|
+
instance: string,
|
|
355
|
+
): number | undefined =>
|
|
356
|
+
jobs
|
|
357
|
+
.filter((job) => instance.startsWith(`${job.instance}.`))
|
|
358
|
+
.sort((a, b) => b.instance.length - a.instance.length)[0]?.id;
|
|
359
|
+
|
|
360
|
+
/** Service calls a job instance makes, and those of the jobs it starts. */
|
|
361
|
+
function requests(
|
|
362
|
+
context: EmissionContext,
|
|
363
|
+
id: string,
|
|
364
|
+
prefix: string,
|
|
365
|
+
job: number | undefined,
|
|
366
|
+
): DebugJob["requests"] {
|
|
367
|
+
const { plan, symbols, instrumentation } = context;
|
|
368
|
+
const block = required(symbols.blocks.get(id));
|
|
369
|
+
return block.state.flatMap((child) => {
|
|
370
|
+
const instance = instancedBlock(child.type, symbols.typeMap);
|
|
371
|
+
if (!instance) {
|
|
372
|
+
return [];
|
|
373
|
+
}
|
|
374
|
+
const caller = requestingJob(instrumentation, child.id, job);
|
|
375
|
+
const type = required(symbols.typeMap.get(child.type));
|
|
376
|
+
const field = `${prefix}.${required(required(symbols.blockFields.get(id)).get(child.id))}`;
|
|
377
|
+
const targets =
|
|
378
|
+
type.kind === "array"
|
|
379
|
+
? Array.from(
|
|
380
|
+
{ length: type.length },
|
|
381
|
+
(_, index) => `${field}[${type.lowerBound + index}]`,
|
|
382
|
+
)
|
|
383
|
+
: [field];
|
|
384
|
+
const sequence = instrumentation.fields.get(instance.block)?.sequence;
|
|
385
|
+
const path = plan.http[instance.block]?.path;
|
|
386
|
+
return targets.flatMap((target) =>
|
|
387
|
+
sequence && path
|
|
388
|
+
? [
|
|
389
|
+
{
|
|
390
|
+
service: serviceName(path),
|
|
391
|
+
target,
|
|
392
|
+
sequence: `${target}.${sequence}`,
|
|
393
|
+
...(caller !== undefined && { job: caller }),
|
|
394
|
+
},
|
|
395
|
+
]
|
|
396
|
+
: requests(context, instance.block, target, caller),
|
|
397
|
+
);
|
|
398
|
+
});
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/** Static member reads in a body, by TypeScript path. */
|
|
402
|
+
function reads(
|
|
403
|
+
variables: ReadonlyMap<string, Variable>,
|
|
404
|
+
body: readonly CoreStatement[],
|
|
405
|
+
sources: Sources,
|
|
406
|
+
): DebugFunction["reads"] {
|
|
407
|
+
const reads = new Map<string, DebugFunction["reads"][number]>();
|
|
408
|
+
const expression = (value: CoreExpression): void => {
|
|
409
|
+
// A borrow is not a read, and the manifest does not list the indexes of its place either.
|
|
410
|
+
if (value.kind === "borrow") {
|
|
411
|
+
return;
|
|
412
|
+
}
|
|
413
|
+
subexpressions(value).forEach(expression);
|
|
414
|
+
if (value.kind !== "load") {
|
|
415
|
+
return;
|
|
416
|
+
}
|
|
417
|
+
const read = typescriptPath(
|
|
418
|
+
required(variables.get(value.root)),
|
|
419
|
+
value.path,
|
|
420
|
+
);
|
|
421
|
+
const range = read && sources.range(value.span);
|
|
422
|
+
if (range) {
|
|
423
|
+
reads.set(`${JSON.stringify(range)}${read}`, { range, path: read });
|
|
424
|
+
}
|
|
425
|
+
};
|
|
426
|
+
const statements = (items: readonly CoreStatement[]): void => {
|
|
427
|
+
for (const item of items) {
|
|
428
|
+
statementExpressions(item).forEach(expression);
|
|
429
|
+
nestedBodies(item).forEach(statements);
|
|
430
|
+
}
|
|
431
|
+
};
|
|
432
|
+
statements(body);
|
|
433
|
+
return [...reads.values()];
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
/** How the debugger names a routine, and where it shows its declaration: a job's own, else the authored function's. */
|
|
437
|
+
export const routineLabel = (routine: CoreFunction | CoreFunctionBlock) =>
|
|
438
|
+
isBlockRoutine(routine)
|
|
439
|
+
? {
|
|
440
|
+
name: routine.job?.name ?? routine.name,
|
|
441
|
+
span: routine.job?.span ?? routine.span,
|
|
442
|
+
}
|
|
443
|
+
: { name: routine.origin?.name ?? routine.name, span: routine.span };
|
|
444
|
+
|
|
445
|
+
/** The IEC name of a routine: its function block, a method under its block, or its function or PROGRAM. */
|
|
446
|
+
const routineTarget = (
|
|
447
|
+
symbols: CodesysSymbols,
|
|
448
|
+
routine: CoreFunction | CoreFunctionBlock,
|
|
449
|
+
): string => {
|
|
450
|
+
if (isBlockRoutine(routine)) {
|
|
451
|
+
return required(symbols.blockNames.get(routine.id));
|
|
452
|
+
}
|
|
453
|
+
const name = required(symbols.functionNames.get(routine.id));
|
|
454
|
+
return routine.owner
|
|
455
|
+
? `${required(symbols.blockNames.get(routine.owner.block))}.${name}`
|
|
456
|
+
: name;
|
|
457
|
+
};
|
|
458
|
+
|
|
459
|
+
/**
|
|
460
|
+
* The debugger's view of an emitted program, and the id of each function it lists by routine. Its
|
|
461
|
+
* build id is filled in once the id is known.
|
|
462
|
+
*/
|
|
463
|
+
export function debugManifest(
|
|
464
|
+
context: EmissionContext,
|
|
465
|
+
sources: Sources,
|
|
466
|
+
): { manifest: DebugManifest; functionIds: ReadonlyMap<string, number> } {
|
|
467
|
+
const { plan, symbols, instrumentation } = context;
|
|
468
|
+
const { core, project, deployment, options } = plan;
|
|
469
|
+
const settings = project.debug;
|
|
470
|
+
const global = (field: string) => `${instrumentation.global}.${field}`;
|
|
471
|
+
|
|
472
|
+
const declaredFunctions = new Map<
|
|
473
|
+
string,
|
|
474
|
+
{ id: number; range: SourceRange }
|
|
475
|
+
>();
|
|
476
|
+
const { functions: routines } = coreTables(core);
|
|
477
|
+
const { variables } = coreTables(plan.specialized);
|
|
478
|
+
const functions = [
|
|
479
|
+
...plan.specialized.functions,
|
|
480
|
+
...plan.specialized.blocks.filter(
|
|
481
|
+
(block) => !block.standard && !block.runtime,
|
|
482
|
+
),
|
|
483
|
+
].flatMap((routine): DebugFunction[] => {
|
|
484
|
+
const label = routineLabel(routine);
|
|
485
|
+
const declaration = sources.range(label.span);
|
|
486
|
+
if (!declaration) {
|
|
487
|
+
return [];
|
|
488
|
+
}
|
|
489
|
+
// An instance whose routine was merged runs as the routine shared with the others. A
|
|
490
|
+
// routine that only returned a constant was replaced by it, and has nothing to describe.
|
|
491
|
+
const runs = isBlockRoutine(routine)
|
|
492
|
+
? symbols.blocks.get(routine.id)
|
|
493
|
+
: routines.get(plan.sharedRoutines.get(routine.id) ?? routine.id);
|
|
494
|
+
if (!runs) {
|
|
495
|
+
return [];
|
|
496
|
+
}
|
|
497
|
+
const id = declaredFunctions.size;
|
|
498
|
+
declaredFunctions.set(routine.id, { id, range: declaration });
|
|
499
|
+
const task = instrumentation.analysis.tasks.get(runs.id);
|
|
500
|
+
const origin = isBlockRoutine(routine) ? undefined : routine.origin;
|
|
501
|
+
return [
|
|
502
|
+
{
|
|
503
|
+
id,
|
|
504
|
+
name: label.name,
|
|
505
|
+
declaration,
|
|
506
|
+
...(origin?.instance && { instance: origin.instance }),
|
|
507
|
+
...(task && { task }),
|
|
508
|
+
target: routineTarget(symbols, runs),
|
|
509
|
+
reads: reads(variables, routine.body, sources),
|
|
510
|
+
},
|
|
511
|
+
];
|
|
512
|
+
});
|
|
513
|
+
const declared = (routine: string | undefined) =>
|
|
514
|
+
routine === undefined ? undefined : declaredFunctions.get(routine);
|
|
515
|
+
|
|
516
|
+
const events = instrumentation.analysis.events.flatMap(
|
|
517
|
+
(site): DebugEventSite[] => {
|
|
518
|
+
const { job } = site;
|
|
519
|
+
const owner = declared(site.routine);
|
|
520
|
+
const range = sources.range(site.span) ?? owner?.range;
|
|
521
|
+
if (!range) {
|
|
522
|
+
return [];
|
|
523
|
+
}
|
|
524
|
+
return [
|
|
525
|
+
{
|
|
526
|
+
id: site.id,
|
|
527
|
+
kind: site.kind,
|
|
528
|
+
...(job && {
|
|
529
|
+
job: required(
|
|
530
|
+
instrumentation.jobs.find(({ block }) => block.id === job.block),
|
|
531
|
+
).id,
|
|
532
|
+
phase: job.phase,
|
|
533
|
+
}),
|
|
534
|
+
path: site.path,
|
|
535
|
+
...(owner && { function: owner.id }),
|
|
536
|
+
type: site.type,
|
|
537
|
+
range,
|
|
538
|
+
text: site.text ?? sources.text(site.span) ?? "",
|
|
539
|
+
task: site.task,
|
|
540
|
+
operands: site.operands.map((operand) => ({
|
|
541
|
+
text: sources.text(operand.span) ?? "",
|
|
542
|
+
range: sources.range(operand.span) ?? range,
|
|
543
|
+
type: operand.capture.type,
|
|
544
|
+
...(operand.path && { path: operand.path }),
|
|
545
|
+
stable: operand.stable,
|
|
546
|
+
})),
|
|
547
|
+
},
|
|
548
|
+
];
|
|
549
|
+
},
|
|
550
|
+
);
|
|
551
|
+
|
|
552
|
+
const jobs = instrumentation.jobs.flatMap(
|
|
553
|
+
({ block, path, id }): DebugJob[] => {
|
|
554
|
+
const job = required(block.job);
|
|
555
|
+
const declaration = sources.range(job.span);
|
|
556
|
+
if (!declaration) {
|
|
557
|
+
return [];
|
|
558
|
+
}
|
|
559
|
+
const target = instancePath(symbols, path);
|
|
560
|
+
const startedAt = instrumentation.fields.get(block.id)?.startedAt;
|
|
561
|
+
return [
|
|
562
|
+
{
|
|
563
|
+
id,
|
|
564
|
+
name: job.name,
|
|
565
|
+
declaration,
|
|
566
|
+
...(job.instance && { instance: job.instance }),
|
|
567
|
+
target,
|
|
568
|
+
steps: job.steps.map((step) => ({
|
|
569
|
+
step: step.step,
|
|
570
|
+
kind: step.kind,
|
|
571
|
+
range: sources.range(step.span) ?? declaration,
|
|
572
|
+
text: step.text,
|
|
573
|
+
})),
|
|
574
|
+
...(startedAt && { startedAt: `${target}.${startedAt}` }),
|
|
575
|
+
requests: requests(context, block.id, target, undefined),
|
|
576
|
+
...(generated(job.name) && { generated: true as const }),
|
|
577
|
+
},
|
|
578
|
+
];
|
|
579
|
+
},
|
|
580
|
+
);
|
|
581
|
+
|
|
582
|
+
const writers = settings?.writers
|
|
583
|
+
? instrumentation.analysis.outputs.map((output, index) => ({
|
|
584
|
+
output: output.key,
|
|
585
|
+
target: global(`${instrumentation.slots.writers}[${index}]`),
|
|
586
|
+
sites: required(instrumentation.analysis.writers[index]).flatMap(
|
|
587
|
+
(site) => {
|
|
588
|
+
const owner = declared(site.routine);
|
|
589
|
+
return owner
|
|
590
|
+
? [
|
|
591
|
+
{
|
|
592
|
+
id: site.id,
|
|
593
|
+
range: sources.range(site.span) ?? owner.range,
|
|
594
|
+
function: owner.id,
|
|
595
|
+
},
|
|
596
|
+
]
|
|
597
|
+
: [];
|
|
598
|
+
},
|
|
599
|
+
),
|
|
600
|
+
}))
|
|
601
|
+
: [];
|
|
602
|
+
|
|
603
|
+
const roots = core.variables.flatMap((variable) => {
|
|
604
|
+
const path = typescriptPath(variable);
|
|
605
|
+
if (path === undefined) {
|
|
606
|
+
return [];
|
|
607
|
+
}
|
|
608
|
+
const declaration = sources.range(variable.span);
|
|
609
|
+
return [
|
|
610
|
+
{
|
|
611
|
+
path,
|
|
612
|
+
target: required(symbols.references.get(variable.id)),
|
|
613
|
+
type: variable.type,
|
|
614
|
+
...(declaration && { declaration }),
|
|
615
|
+
readable: true,
|
|
616
|
+
...(generated(variable.name) && { generated: true as const }),
|
|
617
|
+
},
|
|
618
|
+
];
|
|
619
|
+
});
|
|
620
|
+
|
|
621
|
+
const types = Object.fromEntries(
|
|
622
|
+
core.types.map((type): [string, DebugType] => [
|
|
623
|
+
type.id,
|
|
624
|
+
type.kind === "block"
|
|
625
|
+
? blockDebugType(
|
|
626
|
+
context,
|
|
627
|
+
required(symbols.blocks.get(type.block)),
|
|
628
|
+
sources,
|
|
629
|
+
)
|
|
630
|
+
: dataDebugType(type, (record, field) =>
|
|
631
|
+
symbols.targetTypes.field(record, field),
|
|
632
|
+
),
|
|
633
|
+
]),
|
|
634
|
+
);
|
|
635
|
+
const hardware = debugHardware(
|
|
636
|
+
context,
|
|
637
|
+
project.hardware ?? {
|
|
638
|
+
catalog: { descriptors: [], devices: [] },
|
|
639
|
+
config: { devices: {}, io: options.io },
|
|
640
|
+
tree: false,
|
|
641
|
+
},
|
|
642
|
+
);
|
|
643
|
+
const constants = debugConstants(core.folded ?? [], sources);
|
|
644
|
+
|
|
645
|
+
const manifest: DebugManifest = {
|
|
646
|
+
kind: "tsplc-debug",
|
|
647
|
+
version: 1,
|
|
648
|
+
build: {
|
|
649
|
+
id: "",
|
|
650
|
+
target: global(instrumentation.slots.buildId),
|
|
651
|
+
compiler: `@tsplc/compiler ${version}`,
|
|
652
|
+
},
|
|
653
|
+
sources: sources.files,
|
|
654
|
+
types,
|
|
655
|
+
roots,
|
|
656
|
+
constants,
|
|
657
|
+
...hardware,
|
|
658
|
+
functions,
|
|
659
|
+
jobs,
|
|
660
|
+
events,
|
|
661
|
+
writers,
|
|
662
|
+
tasks: deployment.tasks.map((task) => {
|
|
663
|
+
// The task's ring sender reads the clock at the start of its first program.
|
|
664
|
+
const ring = instrumentation.rings.get(task.name);
|
|
665
|
+
const clock = ring
|
|
666
|
+
? `${ring}.now`
|
|
667
|
+
: instrumentation.scanClocks.get(task.name);
|
|
668
|
+
return {
|
|
669
|
+
name: task.name,
|
|
670
|
+
intervalMs:
|
|
671
|
+
Number(task.intervalNanoseconds) / Number(nanosecondsPerMillisecond),
|
|
672
|
+
priority: task.priority,
|
|
673
|
+
...(clock && { clock: global(clock) }),
|
|
674
|
+
};
|
|
675
|
+
}),
|
|
676
|
+
errors: jobErrors,
|
|
677
|
+
};
|
|
678
|
+
return {
|
|
679
|
+
manifest,
|
|
680
|
+
functionIds: new Map(
|
|
681
|
+
[...declaredFunctions].map(([routine, { id }]) => [routine, id]),
|
|
682
|
+
),
|
|
683
|
+
};
|
|
684
|
+
}
|