@tsplc/tmp-compiler 0.0.0-stage → 0.6.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +41 -2
- package/dist/cli.js +2325 -0
- package/dist/cli.js.map +26 -0
- package/dist/clock.js +11 -0
- package/dist/clock.js.map +11 -0
- package/dist/codesys.js +6563 -0
- package/dist/codesys.js.map +51 -0
- package/dist/collections.js +399 -0
- package/dist/collections.js.map +10 -0
- package/dist/index.js +16198 -0
- package/dist/index.js.map +62 -0
- package/dist/ir.js +3948 -0
- package/dist/ir.js.map +35 -0
- package/dist/retain.js +10 -0
- package/dist/retain.js.map +10 -0
- package/dist/signals.js +115 -0
- package/dist/signals.js.map +10 -0
- package/dist/telemetry.js +121 -0
- package/dist/telemetry.js.map +12 -0
- package/package.json +122 -4
- package/src/codesys/config.ts +50 -0
- package/src/codesys/debug.ts +655 -0
- package/src/codesys/emit.ts +185 -0
- package/src/codesys/endpoints.ts +111 -0
- package/src/codesys/engineering.ts +96 -0
- package/src/codesys/explain/expressions.ts +445 -0
- package/src/codesys/explain/flags.ts +134 -0
- package/src/codesys/explain/parameters.ts +53 -0
- package/src/codesys/explain/rows.ts +182 -0
- package/src/codesys/explain/tables.ts +79 -0
- package/src/codesys/explain/tasks.ts +68 -0
- package/src/codesys/explain/writes.ts +497 -0
- package/src/codesys/explain.ts +149 -0
- package/src/codesys/expressions.ts +100 -0
- package/src/codesys/global-lists.ts +284 -0
- package/src/codesys/global-names.ts +66 -0
- package/src/codesys/http.ts +1090 -0
- package/src/codesys/index.ts +13 -0
- package/src/codesys/instrumentation.ts +574 -0
- package/src/codesys/io-paths.ts +149 -0
- package/src/codesys/loops.ts +101 -0
- package/src/codesys/manifest.ts +684 -0
- package/src/codesys/names.ts +58 -0
- package/src/codesys/numeric.ts +516 -0
- package/src/codesys/plan.ts +688 -0
- package/src/codesys/programs.ts +138 -0
- package/src/codesys/publication.ts +99 -0
- package/src/codesys/required.ts +8 -0
- package/src/codesys/routines.ts +247 -0
- package/src/codesys/scan-clock.ts +103 -0
- package/src/codesys/scheduling.ts +96 -0
- package/src/codesys/service-channels.ts +24 -0
- package/src/codesys/sources.ts +118 -0
- package/src/codesys/st-files.ts +219 -0
- package/src/codesys/statements.ts +409 -0
- package/src/codesys/storage.ts +150 -0
- package/src/codesys/symbols.ts +240 -0
- package/src/codesys/task-access.ts +186 -0
- package/src/codesys/telemetry-settings.ts +19 -0
- package/src/codesys/telemetry.ts +852 -0
- package/src/codesys/types.ts +310 -0
- package/src/codesys/vendor.ts +113 -0
- package/src/frontend/analyzer.ts +779 -0
- package/src/frontend/async.ts +530 -0
- package/src/frontend/blocks.ts +578 -0
- package/src/frontend/configuration.ts +83 -0
- package/src/frontend/const-enums.ts +196 -0
- package/src/frontend/const-functions.ts +204 -0
- package/src/frontend/control-flow.ts +787 -0
- package/src/frontend/cooperative.ts +114 -0
- package/src/frontend/coroutines.ts +1344 -0
- package/src/frontend/dialect-names.ts +104 -0
- package/src/frontend/elaborate/array-searches.ts +224 -0
- package/src/frontend/elaborate/assignments.ts +343 -0
- package/src/frontend/elaborate/calls.ts +1342 -0
- package/src/frontend/elaborate/capture-protocol.ts +237 -0
- package/src/frontend/elaborate/capture.ts +388 -0
- package/src/frontend/elaborate/declarations.ts +124 -0
- package/src/frontend/elaborate/dialect.ts +473 -0
- package/src/frontend/elaborate/expressions.ts +2177 -0
- package/src/frontend/elaborate/filtered-arrays.ts +643 -0
- package/src/frontend/elaborate/frame.ts +145 -0
- package/src/frontend/elaborate/generics.ts +123 -0
- package/src/frontend/elaborate/graph.ts +141 -0
- package/src/frontend/elaborate/imports.ts +105 -0
- package/src/frontend/elaborate/index.ts +851 -0
- package/src/frontend/elaborate/initializers.ts +235 -0
- package/src/frontend/elaborate/iterators.ts +1541 -0
- package/src/frontend/elaborate/layouts.ts +1025 -0
- package/src/frontend/elaborate/model.ts +478 -0
- package/src/frontend/elaborate/names.ts +134 -0
- package/src/frontend/elaborate/reduce.ts +900 -0
- package/src/frontend/elaborate/references.ts +727 -0
- package/src/frontend/elaborate/retention.ts +284 -0
- package/src/frontend/elaborate/specialize.ts +1137 -0
- package/src/frontend/elaborate/statements.ts +1220 -0
- package/src/frontend/elaborate/storage.ts +836 -0
- package/src/frontend/elaborate/time-constructors.ts +34 -0
- package/src/frontend/elaborate/values.ts +200 -0
- package/src/frontend/elaborate/writes.ts +415 -0
- package/src/frontend/expressions.ts +1382 -0
- package/src/frontend/generated-names.ts +95 -0
- package/src/frontend/intrinsics.ts +382 -0
- package/src/frontend/lowering-key.ts +23 -0
- package/src/frontend/module-graph.ts +156 -0
- package/src/frontend/positions.ts +359 -0
- package/src/frontend/promise-chains.ts +872 -0
- package/src/frontend/promises.ts +419 -0
- package/src/frontend/routines.ts +239 -0
- package/src/frontend/sdk.ts +187 -0
- package/src/frontend/services.ts +612 -0
- package/src/frontend/statements.ts +871 -0
- package/src/frontend/tasks.ts +526 -0
- package/src/frontend/telemetry.ts +174 -0
- package/src/frontend/types.ts +1073 -0
- package/src/frontend/typescript.ts +659 -0
- package/src/index.ts +9 -0
- package/src/ir/aggregates.ts +291 -0
- package/src/ir/allocation.ts +96 -0
- package/src/ir/conditions.ts +855 -0
- package/src/ir/core-model.ts +289 -0
- package/src/ir/declarations.ts +281 -0
- package/src/ir/diagnostics.ts +68 -0
- package/src/ir/domains.ts +151 -0
- package/src/ir/fold.ts +279 -0
- package/src/ir/identity.ts +19 -0
- package/src/ir/index.ts +137 -0
- package/src/ir/job-errors.ts +69 -0
- package/src/ir/layouts.ts +211 -0
- package/src/ir/link.ts +302 -0
- package/src/ir/model.ts +394 -0
- package/src/ir/normalization-error.ts +10 -0
- package/src/ir/normalize.ts +52 -0
- package/src/ir/numeric.ts +163 -0
- package/src/ir/operands.ts +160 -0
- package/src/ir/ranges.ts +420 -0
- package/src/ir/routine.ts +1116 -0
- package/src/ir/scalars.ts +453 -0
- package/src/ir/share.ts +790 -0
- package/src/ir/short-circuit.ts +140 -0
- package/src/ir/tables.ts +111 -0
- package/src/ir/temporaries.ts +138 -0
- package/src/ir/types.ts +151 -0
- package/src/project/artifacts.ts +424 -0
- package/src/project/cli.ts +211 -0
- package/src/project/debugger.ts +111 -0
- package/src/project/deployments.ts +387 -0
- package/src/project/flags.ts +14 -0
- package/src/project/git.ts +35 -0
- package/src/project/plant.ts +629 -0
- package/src/project/project-commands.ts +507 -0
- package/src/project/project.ts +559 -0
- package/src/project/release.ts +152 -0
- package/src/project/server-check.ts +54 -0
- package/src/project/server.ts +165 -0
- package/src/project/terminal.ts +45 -0
- package/src/runtime/async.ts +207 -0
- package/src/runtime/launch.ts +59 -0
- package/src/runtime/main.ts +59 -0
- package/src/runtime/missing-file.ts +3 -0
- package/src/runtime/process.ts +177 -0
- package/src/runtime/program.ts +109 -0
- package/src/runtime/retain.ts +108 -0
- package/src/runtime/runtime.ts +324 -0
- package/src/runtime/settings.ts +27 -0
- package/src/runtime/storage.ts +456 -0
- package/src/runtime/symbols.ts +97 -0
- package/src/runtime/telemetry.ts +154 -0
- package/src/runtime/transport.ts +168 -0
- package/stdlib/clock.ts +8 -0
- package/stdlib/collections.ts +534 -0
- package/stdlib/plc-time.ts +2 -0
- package/stdlib/retain.ts +4 -0
- package/stdlib/signals.ts +142 -0
- package/stdlib/telemetry.ts +89 -0
- package/types/async.d.ts +87 -0
- package/types/clock.d.ts +8 -0
- package/types/config.d.ts +2 -0
- package/types/data-types.d.ts +31 -0
- package/types/dialect.d.ts +49 -0
- package/types/globals.d.ts +10 -0
- package/types/index.d.ts +212 -0
- package/types/retain.d.ts +12 -0
- package/types/telemetry.d.ts +47 -0
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export { engineeringBuildSchema } from "@tsplc/config/build";
|
|
2
|
+
export { type CodesysOptions, codesys, direct } from "./config";
|
|
3
|
+
export { emitCodesys, type SourceMapping } from "./emit";
|
|
4
|
+
export {
|
|
5
|
+
buildCodesys,
|
|
6
|
+
type CodesysEngineeringBuild,
|
|
7
|
+
codesysBuildConfig,
|
|
8
|
+
} from "./engineering";
|
|
9
|
+
export { ioTargets } from "./io-paths";
|
|
10
|
+
export type { ProjectHardware } from "./manifest";
|
|
11
|
+
export { type CodesysPlan, type CodesysProject, planCodesys } from "./plan";
|
|
12
|
+
export { publicationSelectors } from "./publication";
|
|
13
|
+
export { diagnoseCodesys } from "./vendor";
|
|
@@ -0,0 +1,574 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import {
|
|
3
|
+
type CoreExpression,
|
|
4
|
+
type CoreFunctionBlock,
|
|
5
|
+
type CoreStatement,
|
|
6
|
+
type GeneratedObject,
|
|
7
|
+
jobError,
|
|
8
|
+
type SourceSpan,
|
|
9
|
+
type VerifiedCoreModule,
|
|
10
|
+
} from "@tsplc/compiler/ir";
|
|
11
|
+
import type { ResolvedDebugConfig } from "@tsplc/config";
|
|
12
|
+
import {
|
|
13
|
+
type DebugManifest,
|
|
14
|
+
type jobStateFields,
|
|
15
|
+
scanClockType,
|
|
16
|
+
writerSlotType,
|
|
17
|
+
} from "@tsplc/debug/manifest";
|
|
18
|
+
import { createPaths, TooManyMatches } from "@tsplc/debug/paths";
|
|
19
|
+
import { elementaryType, integerRange } from "@tsplc/iec";
|
|
20
|
+
import { buildIdLength } from "@tsplc/server/telemetry";
|
|
21
|
+
import type { ServiceChannel } from "@tsplc/server/wire";
|
|
22
|
+
import { isOutputAddress } from "@tsplc/typegen/hardware";
|
|
23
|
+
import {
|
|
24
|
+
DebugAnalysis,
|
|
25
|
+
instanceText,
|
|
26
|
+
type Operand,
|
|
27
|
+
type Output,
|
|
28
|
+
typescriptPath,
|
|
29
|
+
} from "./debug";
|
|
30
|
+
import {
|
|
31
|
+
expressionText,
|
|
32
|
+
placeText,
|
|
33
|
+
type StorageScope,
|
|
34
|
+
storageName,
|
|
35
|
+
} from "./expressions";
|
|
36
|
+
import { rpcSlots, unsignedWidening } from "./http";
|
|
37
|
+
import {
|
|
38
|
+
isStoragePath,
|
|
39
|
+
resolveStoragePath,
|
|
40
|
+
type StorageBinding,
|
|
41
|
+
} from "./io-paths";
|
|
42
|
+
import { dataDebugType, ioAddress } from "./manifest";
|
|
43
|
+
import { Names } from "./names";
|
|
44
|
+
import type { CodesysPlan, Report } from "./plan";
|
|
45
|
+
import { required } from "./required";
|
|
46
|
+
import type { EmissionContext, GlobalSymbols } from "./symbols";
|
|
47
|
+
import {
|
|
48
|
+
fillBuildId,
|
|
49
|
+
type TelemetryTransport,
|
|
50
|
+
telemetryClient,
|
|
51
|
+
telemetryObjects,
|
|
52
|
+
} from "./telemetry";
|
|
53
|
+
import { stStringLiteral } from "./types";
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* The most storage paths `debug.writers.paths` may match. Each is a slot in `GVL_TsplcDebug` and
|
|
57
|
+
* a store on every statement that writes it.
|
|
58
|
+
*/
|
|
59
|
+
export const maxWriterPaths = 256;
|
|
60
|
+
|
|
61
|
+
/** Module storage as the debugger's paths name it, for expanding `debug.writers.paths`. Blocks are leaves: their fields aren't module storage. */
|
|
62
|
+
const storagePaths = (core: VerifiedCoreModule) =>
|
|
63
|
+
createPaths({
|
|
64
|
+
roots: core.variables.flatMap((variable) => {
|
|
65
|
+
const path =
|
|
66
|
+
variable.storage === "module" ? typescriptPath(variable) : undefined;
|
|
67
|
+
return path
|
|
68
|
+
? [{ path, target: "", type: variable.type, readable: true }]
|
|
69
|
+
: [];
|
|
70
|
+
}),
|
|
71
|
+
io: [],
|
|
72
|
+
constants: [],
|
|
73
|
+
types: Object.fromEntries(
|
|
74
|
+
core.types.flatMap((type) =>
|
|
75
|
+
type.kind === "block"
|
|
76
|
+
? []
|
|
77
|
+
: [[type.id, dataDebugType(type, (_, field) => field)]],
|
|
78
|
+
),
|
|
79
|
+
),
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* The module storage paths `debug.writers.paths` matches. A pattern that names a block's field or
|
|
84
|
+
* anything else that isn't module storage is reported, as is matching more than `maxWriterPaths`.
|
|
85
|
+
*/
|
|
86
|
+
export function writerPaths(
|
|
87
|
+
core: VerifiedCoreModule,
|
|
88
|
+
settings: ResolvedDebugConfig | undefined,
|
|
89
|
+
report: Report,
|
|
90
|
+
): StorageBinding[] {
|
|
91
|
+
const writers = settings?.writers;
|
|
92
|
+
if (typeof writers !== "object") {
|
|
93
|
+
return [];
|
|
94
|
+
}
|
|
95
|
+
const expanded = (() => {
|
|
96
|
+
try {
|
|
97
|
+
return storagePaths(core).expand(writers.paths, maxWriterPaths);
|
|
98
|
+
} catch (error) {
|
|
99
|
+
if (error instanceof TooManyMatches) {
|
|
100
|
+
report(`debug.writers.paths match more than ${maxWriterPaths} values.`);
|
|
101
|
+
return [];
|
|
102
|
+
}
|
|
103
|
+
throw error;
|
|
104
|
+
}
|
|
105
|
+
})();
|
|
106
|
+
return expanded.flatMap((path) => {
|
|
107
|
+
const resolved = resolveStoragePath(core, path);
|
|
108
|
+
if ("error" in resolved) {
|
|
109
|
+
report(`debug.writers.paths: ${resolved.error}`);
|
|
110
|
+
return [];
|
|
111
|
+
}
|
|
112
|
+
return [resolved];
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* The debugger's and telemetry's names in the program: `GVL_TsplcDebug` and its fields, each
|
|
118
|
+
* task's ring, and the instrumentation fields of each block, by block id.
|
|
119
|
+
*/
|
|
120
|
+
export type Instrumentation = ReturnType<typeof planInstrumentation> & {
|
|
121
|
+
fields: ReadonlyMap<string, BlockInstrumentation>;
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
export function planInstrumentation(plan: CodesysPlan, symbols: GlobalSymbols) {
|
|
125
|
+
const { core, options } = plan;
|
|
126
|
+
const settings = plan.project.debug;
|
|
127
|
+
// A simulation build binds no fieldbus I/O, but its outputs are still the configured ones.
|
|
128
|
+
const hardware = plan.project.hardware;
|
|
129
|
+
const outputs: Output[] = [
|
|
130
|
+
...core.variables
|
|
131
|
+
.filter((variable) => variable.storage === "output")
|
|
132
|
+
.map((root) => ({ key: root.name, root, segments: [] })),
|
|
133
|
+
...(hardware
|
|
134
|
+
? Object.entries(hardware.config.io).flatMap(([key, binding]) => {
|
|
135
|
+
const address = ioAddress(hardware, binding);
|
|
136
|
+
if (
|
|
137
|
+
!isStoragePath(key) ||
|
|
138
|
+
address === undefined ||
|
|
139
|
+
!isOutputAddress(address)
|
|
140
|
+
) {
|
|
141
|
+
return [];
|
|
142
|
+
}
|
|
143
|
+
const resolved = resolveStoragePath(core, key);
|
|
144
|
+
return "error" in resolved ? [] : [resolved];
|
|
145
|
+
})
|
|
146
|
+
: plan.storageBindings.filter(({ key }) =>
|
|
147
|
+
isOutputAddress(required(options.io[key]).address),
|
|
148
|
+
)),
|
|
149
|
+
];
|
|
150
|
+
for (const binding of plan.writerPaths) {
|
|
151
|
+
if (!outputs.some((output) => output.key === binding.key)) {
|
|
152
|
+
outputs.push(binding);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
const analysis = new DebugAnalysis(core, plan.deployment, settings, outputs);
|
|
156
|
+
const global = symbols.names.allocateObject("GVL", "TsplcDebug");
|
|
157
|
+
const names = new Names();
|
|
158
|
+
const slots = {
|
|
159
|
+
buildId: names.allocate("buildId"),
|
|
160
|
+
decisions: names.allocate("decisions"),
|
|
161
|
+
writers: names.allocate("writers"),
|
|
162
|
+
};
|
|
163
|
+
/** Each task's ring and sender in `GVL_TsplcDebug`. */
|
|
164
|
+
const rings = new Map<string, string>();
|
|
165
|
+
const telemetry = plan.telemetry;
|
|
166
|
+
if (telemetry) {
|
|
167
|
+
const tasks = new Set([
|
|
168
|
+
...[...telemetry.calls.values(), ...telemetry.failures.values()].map(
|
|
169
|
+
(site) => site.task,
|
|
170
|
+
),
|
|
171
|
+
...analysis.events.map((site) => site.task),
|
|
172
|
+
]);
|
|
173
|
+
for (const task of plan.deployment.tasks) {
|
|
174
|
+
if (tasks.has(task.name)) {
|
|
175
|
+
rings.set(task.name, names.allocate(task.name));
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
const telemetryNames = rings.size
|
|
180
|
+
? {
|
|
181
|
+
writer: symbols.names.allocateObject("FB", "TsplcTelemetry"),
|
|
182
|
+
record: symbols.names.allocateObject("ST", "TsplcRecord"),
|
|
183
|
+
client: symbols.names.allocateObject(
|
|
184
|
+
"FB",
|
|
185
|
+
telemetry?.endpoint.transport === "opcua"
|
|
186
|
+
? "TsplcTelemetryOpcUa"
|
|
187
|
+
: "TsplcTelemetryHttp",
|
|
188
|
+
),
|
|
189
|
+
}
|
|
190
|
+
: undefined;
|
|
191
|
+
/**
|
|
192
|
+
* Job instances in manifest order, each with its id and instance text, and the job id each
|
|
193
|
+
* single-instance declaration passes to its service calls.
|
|
194
|
+
*/
|
|
195
|
+
const jobs = core.blocks
|
|
196
|
+
.filter((block) => block.job)
|
|
197
|
+
.flatMap((block) =>
|
|
198
|
+
required(analysis.instances.get(block.id)).map((path) => ({
|
|
199
|
+
block,
|
|
200
|
+
path,
|
|
201
|
+
})),
|
|
202
|
+
)
|
|
203
|
+
.map((job, index) => ({
|
|
204
|
+
...job,
|
|
205
|
+
id: index + 1,
|
|
206
|
+
instance: instanceText(job.path),
|
|
207
|
+
}));
|
|
208
|
+
const jobIds = new Map<string, number>();
|
|
209
|
+
for (const { path, id } of jobs) {
|
|
210
|
+
const declared = required(path.at(-1)).variable;
|
|
211
|
+
if (analysis.owningPath(declared) === path) {
|
|
212
|
+
jobIds.set(declared.id, id);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
/** Raw bytes over each assembly, in `GVL_TsplcDebug`, by device. */
|
|
216
|
+
const devices = new Map<
|
|
217
|
+
string,
|
|
218
|
+
Partial<
|
|
219
|
+
Record<
|
|
220
|
+
"input" | "output",
|
|
221
|
+
{ field: string; address: string; bytes: number }
|
|
222
|
+
>
|
|
223
|
+
>
|
|
224
|
+
>();
|
|
225
|
+
if (settings && hardware?.tree) {
|
|
226
|
+
for (const device of hardware.catalog.devices) {
|
|
227
|
+
devices.set(
|
|
228
|
+
device.id,
|
|
229
|
+
Object.fromEntries(
|
|
230
|
+
device.assemblies.map((assembly) => [
|
|
231
|
+
assembly.direction,
|
|
232
|
+
{
|
|
233
|
+
field: names.allocate(`${device.id}_${assembly.direction}s`),
|
|
234
|
+
address: assembly.address,
|
|
235
|
+
bytes: assembly.size,
|
|
236
|
+
},
|
|
237
|
+
]),
|
|
238
|
+
),
|
|
239
|
+
);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
// Without a ring, a task whose writers the debugger follows records the clock itself.
|
|
243
|
+
const scanClocks = new Map<string, string>(
|
|
244
|
+
plan.writerPaths.length
|
|
245
|
+
? plan.deployment.tasks.flatMap((task) =>
|
|
246
|
+
rings.has(task.name)
|
|
247
|
+
? []
|
|
248
|
+
: [[task.name, names.allocate(`scanClock_${task.name}`)]],
|
|
249
|
+
)
|
|
250
|
+
: [],
|
|
251
|
+
);
|
|
252
|
+
return {
|
|
253
|
+
analysis,
|
|
254
|
+
global,
|
|
255
|
+
slots,
|
|
256
|
+
rings,
|
|
257
|
+
/** Each task's clock field in `GVL_TsplcDebug`, by task, when it has no ring that reads the clock. */
|
|
258
|
+
scanClocks,
|
|
259
|
+
telemetryNames,
|
|
260
|
+
jobs,
|
|
261
|
+
jobIds,
|
|
262
|
+
devices,
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/** Instrumentation fields of a block: its job state and the requesting job's id. */
|
|
267
|
+
export interface BlockInstrumentation {
|
|
268
|
+
busy?: string;
|
|
269
|
+
/** The method that records the job starting and finishing, allocated with `busy`. */
|
|
270
|
+
track?: string;
|
|
271
|
+
step?: string;
|
|
272
|
+
startedAt?: string;
|
|
273
|
+
job?: string;
|
|
274
|
+
sequence?: string;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/** The telemetry sender and its client; over OPC UA also the client's slots and their channels. */
|
|
278
|
+
export function emitTelemetry(
|
|
279
|
+
plan: CodesysPlan,
|
|
280
|
+
symbols: GlobalSymbols,
|
|
281
|
+
instrumentation: Instrumentation,
|
|
282
|
+
networkName: string | undefined,
|
|
283
|
+
): { objects: GeneratedObject[]; rpcChannels: ServiceChannel[] } {
|
|
284
|
+
const { telemetry } = plan;
|
|
285
|
+
const names = instrumentation.telemetryNames;
|
|
286
|
+
if (!telemetry || !names) {
|
|
287
|
+
return { objects: [], rpcChannels: [] };
|
|
288
|
+
}
|
|
289
|
+
const { endpoint } = telemetry;
|
|
290
|
+
const objects = (transport: TelemetryTransport) =>
|
|
291
|
+
telemetryObjects({
|
|
292
|
+
plan: telemetry,
|
|
293
|
+
names,
|
|
294
|
+
target: symbols.targetTypes,
|
|
295
|
+
transport,
|
|
296
|
+
types: symbols.typeMap,
|
|
297
|
+
});
|
|
298
|
+
if (endpoint.transport === "opcua") {
|
|
299
|
+
const slots = rpcSlots(
|
|
300
|
+
symbols,
|
|
301
|
+
telemetryClient,
|
|
302
|
+
names.client,
|
|
303
|
+
endpoint.path,
|
|
304
|
+
);
|
|
305
|
+
return {
|
|
306
|
+
objects: [
|
|
307
|
+
slots.list,
|
|
308
|
+
...objects({
|
|
309
|
+
kind: "opcua",
|
|
310
|
+
timeoutMs: endpoint.timeoutMs,
|
|
311
|
+
rpcGlobal: slots.list.name,
|
|
312
|
+
}),
|
|
313
|
+
],
|
|
314
|
+
rpcChannels: slots.channels,
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
return {
|
|
318
|
+
objects: objects({
|
|
319
|
+
kind: "http",
|
|
320
|
+
endpoint,
|
|
321
|
+
network: `${required(networkName)}.network`,
|
|
322
|
+
}),
|
|
323
|
+
rpcChannels: [],
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* `GVL_TsplcDebug` holds the build id, which identifies this ST and its manifest, so the id is
|
|
329
|
+
* computed over everything else first.
|
|
330
|
+
*/
|
|
331
|
+
export function emitDebugGlobal(
|
|
332
|
+
plan: CodesysPlan,
|
|
333
|
+
instrumentation: Instrumentation,
|
|
334
|
+
objects: GeneratedObject[],
|
|
335
|
+
manifest: DebugManifest,
|
|
336
|
+
): DebugManifest {
|
|
337
|
+
const hash = createHash("sha256");
|
|
338
|
+
for (const object of objects) {
|
|
339
|
+
hash.update(
|
|
340
|
+
`${object.name}\n${object.declaration}\n${object.implementation ?? ""}\n`,
|
|
341
|
+
);
|
|
342
|
+
}
|
|
343
|
+
hash.update(JSON.stringify(manifest));
|
|
344
|
+
const id = hash.digest("hex").slice(0, buildIdLength);
|
|
345
|
+
|
|
346
|
+
fillBuildId(objects, id);
|
|
347
|
+
|
|
348
|
+
const fields = [
|
|
349
|
+
`${instrumentation.slots.buildId} : STRING(${buildIdLength}) := '${id}'`,
|
|
350
|
+
];
|
|
351
|
+
for (const field of instrumentation.scanClocks.values()) {
|
|
352
|
+
fields.push(`${field} : ${scanClockType}`);
|
|
353
|
+
}
|
|
354
|
+
for (const [task, field] of instrumentation.rings) {
|
|
355
|
+
fields.push(
|
|
356
|
+
`${field} : ${required(instrumentation.telemetryNames).writer} := (task := '${task}')`,
|
|
357
|
+
);
|
|
358
|
+
}
|
|
359
|
+
const settings = plan.project.debug;
|
|
360
|
+
if (settings) {
|
|
361
|
+
if (instrumentation.analysis.decisions.size) {
|
|
362
|
+
fields.push(
|
|
363
|
+
`${instrumentation.slots.decisions} : ARRAY[0..${instrumentation.analysis.decisions.size - 1}] OF DINT`,
|
|
364
|
+
);
|
|
365
|
+
}
|
|
366
|
+
if (settings.writers && instrumentation.analysis.outputs.length) {
|
|
367
|
+
fields.push(
|
|
368
|
+
`${instrumentation.slots.writers} : ARRAY[0..${instrumentation.analysis.outputs.length - 1}] OF ${writerSlotType}`,
|
|
369
|
+
);
|
|
370
|
+
}
|
|
371
|
+
for (const raw of [...instrumentation.devices.values()].flatMap((device) =>
|
|
372
|
+
Object.values(device),
|
|
373
|
+
)) {
|
|
374
|
+
fields.push(
|
|
375
|
+
`${raw.field} AT ${raw.address} : ARRAY[0..${raw.bytes - 1}] OF BYTE`,
|
|
376
|
+
);
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
objects.push({
|
|
380
|
+
id: "debug",
|
|
381
|
+
kind: "globalList",
|
|
382
|
+
name: instrumentation.global,
|
|
383
|
+
// Nothing in the program reads the build id; linkalways keeps it in the application.
|
|
384
|
+
declaration: `{attribute 'linkalways'}
|
|
385
|
+
{attribute 'qualified_only'}
|
|
386
|
+
VAR_GLOBAL
|
|
387
|
+
${fields.map((field) => ` ${field};\n`).join("")}END_VAR
|
|
388
|
+
`,
|
|
389
|
+
});
|
|
390
|
+
return { ...manifest, build: { ...manifest.build, id } };
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
/** What a routine's probes read, and the task whose ring they record into unless told otherwise. */
|
|
394
|
+
export interface RoutineProbes {
|
|
395
|
+
context: EmissionContext;
|
|
396
|
+
storage: StorageScope;
|
|
397
|
+
task: string | undefined;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
/** Appends a line of a routine's implementation, mapped to `source` when given. */
|
|
401
|
+
type WriteLine = (line: string, depth: number, source?: SourceSpan) => void;
|
|
402
|
+
|
|
403
|
+
export const ringTarget = (
|
|
404
|
+
{ global, rings }: Instrumentation,
|
|
405
|
+
task: string | undefined,
|
|
406
|
+
) => `${global}.${required(rings.get(required(task)))}`;
|
|
407
|
+
|
|
408
|
+
const [dintMinimum, dintMaximum] = integerRange(elementaryType("DINT"));
|
|
409
|
+
|
|
410
|
+
// Trace operands are DINTs: BOOL as 0/1, enums by value, integers clamped to the DINT range.
|
|
411
|
+
const traceOperand = (storage: StorageScope, value: CoreExpression): string => {
|
|
412
|
+
const type = required(storage.symbols.typeMap.get(value.type));
|
|
413
|
+
const text = expressionText(storage, value);
|
|
414
|
+
if (type.kind === "bool") {
|
|
415
|
+
return `BOOL_TO_DINT(${text})`;
|
|
416
|
+
}
|
|
417
|
+
if (type.kind !== "integer" && type.kind !== "bits") {
|
|
418
|
+
return `TO_DINT(${text})`;
|
|
419
|
+
}
|
|
420
|
+
const signed = type.kind === "integer" && type.signed;
|
|
421
|
+
const [minimum, maximum] = integerRange({ bits: type.bits, signed });
|
|
422
|
+
if (minimum >= dintMinimum && maximum <= dintMaximum) {
|
|
423
|
+
return `TO_DINT(${text})`;
|
|
424
|
+
}
|
|
425
|
+
if (signed) {
|
|
426
|
+
return `TO_DINT(LIMIT(LINT#${dintMinimum}, ${text}, LINT#${dintMaximum}))`;
|
|
427
|
+
}
|
|
428
|
+
const wide = unsignedWidening(type.bits);
|
|
429
|
+
return `TO_DINT(MIN(TO_${wide}(${text}), ${wide}#${dintMaximum}))`;
|
|
430
|
+
};
|
|
431
|
+
|
|
432
|
+
// CODESYS requires every input, so operands a site lacks are passed as 0.
|
|
433
|
+
const traceCall = (
|
|
434
|
+
{ context, storage, task }: RoutineProbes,
|
|
435
|
+
site: number,
|
|
436
|
+
value: string,
|
|
437
|
+
operands: readonly Operand[],
|
|
438
|
+
): string => {
|
|
439
|
+
const captured = Array.from(
|
|
440
|
+
{ length: required(context.plan.telemetry?.traceOperands) },
|
|
441
|
+
(_, index) => {
|
|
442
|
+
const operand = operands[index];
|
|
443
|
+
return `, operand${index} := ${operand ? traceOperand(storage, operand.capture) : "DINT#0"}`;
|
|
444
|
+
},
|
|
445
|
+
);
|
|
446
|
+
return `${ringTarget(context.instrumentation, task)}.Trace(site := UINT#${site}, value := ${value}, operands := USINT#${operands.length}${captured.join("")});`;
|
|
447
|
+
};
|
|
448
|
+
|
|
449
|
+
/** The writer and trace probes the debug analysis placed on a statement, written before it. */
|
|
450
|
+
export function writeProbes(
|
|
451
|
+
probes: RoutineProbes,
|
|
452
|
+
write: WriteLine,
|
|
453
|
+
item: CoreStatement,
|
|
454
|
+
depth: number,
|
|
455
|
+
): void {
|
|
456
|
+
const { instrumentation } = probes.context;
|
|
457
|
+
const { storage } = probes;
|
|
458
|
+
for (const probe of instrumentation.analysis.probes.get(item) ?? []) {
|
|
459
|
+
if (probe.kind === "writer") {
|
|
460
|
+
const writer = `${instrumentation.global}.${instrumentation.slots.writers}[${probe.output}] := UINT#${probe.site};`;
|
|
461
|
+
if (!probe.when.length) {
|
|
462
|
+
write(writer, depth, item.span);
|
|
463
|
+
continue;
|
|
464
|
+
}
|
|
465
|
+
const reached = probe.when.map(
|
|
466
|
+
({ index, value }) =>
|
|
467
|
+
`TO_DINT(${expressionText(storage, index)}) = DINT#${value}`,
|
|
468
|
+
);
|
|
469
|
+
write(`IF ${reached.join(" AND ")} THEN`, depth, item.span);
|
|
470
|
+
write(writer, depth + 1, item.span);
|
|
471
|
+
write("END_IF;", depth, item.span);
|
|
472
|
+
continue;
|
|
473
|
+
}
|
|
474
|
+
const value = `TO_DINT(${expressionText(storage, probe.value)})`;
|
|
475
|
+
if (item.kind === "assign") {
|
|
476
|
+
write(
|
|
477
|
+
`IF ${placeText(storage, item.root, item.path)} <> ${expressionText(storage, probe.value)} THEN`,
|
|
478
|
+
depth,
|
|
479
|
+
item.span,
|
|
480
|
+
);
|
|
481
|
+
} else {
|
|
482
|
+
const last = `${instrumentation.global}.${instrumentation.slots.decisions}[${required(probe.slot)}]`;
|
|
483
|
+
write(`IF ${value} <> ${last} THEN`, depth, item.span);
|
|
484
|
+
write(`${last} := ${value};`, depth + 1, item.span);
|
|
485
|
+
}
|
|
486
|
+
write(
|
|
487
|
+
traceCall(probes, probe.site, value, probe.operands),
|
|
488
|
+
depth + 1,
|
|
489
|
+
item.span,
|
|
490
|
+
);
|
|
491
|
+
write("END_IF;", depth, item.span);
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
export const jobField = (
|
|
496
|
+
storage: StorageScope,
|
|
497
|
+
job: CoreFunctionBlock,
|
|
498
|
+
name: keyof typeof jobStateFields,
|
|
499
|
+
) =>
|
|
500
|
+
storageName(
|
|
501
|
+
storage,
|
|
502
|
+
required(job.state.find((item) => item.name === name)).id,
|
|
503
|
+
);
|
|
504
|
+
|
|
505
|
+
/**
|
|
506
|
+
* The job's tracking method, which records it starting or finishing. The job calls it on
|
|
507
|
+
* entry and on every exit: its caller stops calling it once it is done, so only the call that
|
|
508
|
+
* finishes it sees the finish.
|
|
509
|
+
*/
|
|
510
|
+
export function jobTracking(
|
|
511
|
+
probes: RoutineProbes,
|
|
512
|
+
job: CoreFunctionBlock,
|
|
513
|
+
fields: BlockInstrumentation,
|
|
514
|
+
): string {
|
|
515
|
+
const { plan, instrumentation } = probes.context;
|
|
516
|
+
const busy = jobField(probes.storage, job, "busy");
|
|
517
|
+
const sites = instrumentation.analysis.jobSites.get(job.id);
|
|
518
|
+
const failure = plan.telemetry?.failures.get(job.id);
|
|
519
|
+
const lines: string[] = [];
|
|
520
|
+
const line = (text: string, depth: number) =>
|
|
521
|
+
lines.push(" ".repeat(depth) + text);
|
|
522
|
+
line(`IF ${busy} = ${required(fields.busy)} THEN`, 0);
|
|
523
|
+
line("RETURN;", 1);
|
|
524
|
+
line("END_IF;", 0);
|
|
525
|
+
if (fields.startedAt || sites) {
|
|
526
|
+
line(`IF ${busy} THEN`, 0);
|
|
527
|
+
if (fields.startedAt) {
|
|
528
|
+
line(`${fields.startedAt} := TIME_TO_UDINT(TIME());`, 1);
|
|
529
|
+
}
|
|
530
|
+
if (sites) {
|
|
531
|
+
line(
|
|
532
|
+
traceCall(
|
|
533
|
+
probes,
|
|
534
|
+
sites.start,
|
|
535
|
+
`TO_DINT(${jobField(probes.storage, job, "step")})`,
|
|
536
|
+
[],
|
|
537
|
+
),
|
|
538
|
+
1,
|
|
539
|
+
);
|
|
540
|
+
}
|
|
541
|
+
line("END_IF;", 0);
|
|
542
|
+
}
|
|
543
|
+
if (sites || failure) {
|
|
544
|
+
const error = jobField(probes.storage, job, "error");
|
|
545
|
+
line(`IF NOT ${busy} THEN`, 0);
|
|
546
|
+
if (sites) {
|
|
547
|
+
line(`IF ${error} <> 0 THEN`, 1);
|
|
548
|
+
line(traceCall(probes, sites.completion, `-TO_DINT(${error})`, []), 2);
|
|
549
|
+
line("ELSE", 1);
|
|
550
|
+
line(
|
|
551
|
+
traceCall(
|
|
552
|
+
probes,
|
|
553
|
+
sites.completion,
|
|
554
|
+
`TO_DINT(${required(fields.step)})`,
|
|
555
|
+
[],
|
|
556
|
+
),
|
|
557
|
+
2,
|
|
558
|
+
);
|
|
559
|
+
line("END_IF;", 1);
|
|
560
|
+
}
|
|
561
|
+
// Cancellation is asked for, so it isn't a failure.
|
|
562
|
+
if (failure) {
|
|
563
|
+
line(`IF ${error} <> 0 AND ${error} <> ${jobError.cancelled} THEN`, 1);
|
|
564
|
+
line(
|
|
565
|
+
`${ringTarget(instrumentation, failure.task)}.Failure${failure.id}(code := ${error}, step := ${required(fields.step)}${failure.instance === null ? "" : `, instance := ${stStringLiteral(failure.instance)}`});`,
|
|
566
|
+
2,
|
|
567
|
+
);
|
|
568
|
+
line("END_IF;", 1);
|
|
569
|
+
}
|
|
570
|
+
line("END_IF;", 0);
|
|
571
|
+
}
|
|
572
|
+
line(`${fields.busy} := ${busy};`, 0);
|
|
573
|
+
return `${lines.join("\n")}\n`;
|
|
574
|
+
}
|