@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.
Files changed (184) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +41 -2
  3. package/dist/cli.js +2325 -0
  4. package/dist/cli.js.map +26 -0
  5. package/dist/clock.js +11 -0
  6. package/dist/clock.js.map +11 -0
  7. package/dist/codesys.js +6558 -0
  8. package/dist/codesys.js.map +51 -0
  9. package/dist/collections.js +399 -0
  10. package/dist/collections.js.map +10 -0
  11. package/dist/index.js +16076 -0
  12. package/dist/index.js.map +61 -0
  13. package/dist/ir.js +3948 -0
  14. package/dist/ir.js.map +35 -0
  15. package/dist/retain.js +10 -0
  16. package/dist/retain.js.map +10 -0
  17. package/dist/signals.js +115 -0
  18. package/dist/signals.js.map +10 -0
  19. package/dist/telemetry.js +121 -0
  20. package/dist/telemetry.js.map +12 -0
  21. package/package.json +122 -4
  22. package/src/codesys/config.ts +50 -0
  23. package/src/codesys/debug.ts +655 -0
  24. package/src/codesys/emit.ts +185 -0
  25. package/src/codesys/endpoints.ts +111 -0
  26. package/src/codesys/engineering.ts +96 -0
  27. package/src/codesys/explain/expressions.ts +445 -0
  28. package/src/codesys/explain/flags.ts +134 -0
  29. package/src/codesys/explain/parameters.ts +53 -0
  30. package/src/codesys/explain/rows.ts +182 -0
  31. package/src/codesys/explain/tables.ts +79 -0
  32. package/src/codesys/explain/tasks.ts +68 -0
  33. package/src/codesys/explain/writes.ts +497 -0
  34. package/src/codesys/explain.ts +149 -0
  35. package/src/codesys/expressions.ts +100 -0
  36. package/src/codesys/global-lists.ts +284 -0
  37. package/src/codesys/global-names.ts +66 -0
  38. package/src/codesys/http.ts +1078 -0
  39. package/src/codesys/index.ts +13 -0
  40. package/src/codesys/instrumentation.ts +574 -0
  41. package/src/codesys/io-paths.ts +149 -0
  42. package/src/codesys/loops.ts +101 -0
  43. package/src/codesys/manifest.ts +684 -0
  44. package/src/codesys/names.ts +58 -0
  45. package/src/codesys/numeric.ts +516 -0
  46. package/src/codesys/plan.ts +688 -0
  47. package/src/codesys/programs.ts +138 -0
  48. package/src/codesys/publication.ts +99 -0
  49. package/src/codesys/required.ts +8 -0
  50. package/src/codesys/routines.ts +247 -0
  51. package/src/codesys/scan-clock.ts +103 -0
  52. package/src/codesys/scheduling.ts +96 -0
  53. package/src/codesys/service-channels.ts +24 -0
  54. package/src/codesys/sources.ts +118 -0
  55. package/src/codesys/st-files.ts +219 -0
  56. package/src/codesys/statements.ts +409 -0
  57. package/src/codesys/storage.ts +150 -0
  58. package/src/codesys/symbols.ts +240 -0
  59. package/src/codesys/task-access.ts +186 -0
  60. package/src/codesys/telemetry-settings.ts +19 -0
  61. package/src/codesys/telemetry.ts +852 -0
  62. package/src/codesys/types.ts +310 -0
  63. package/src/codesys/vendor.ts +113 -0
  64. package/src/frontend/analyzer.ts +779 -0
  65. package/src/frontend/async.ts +530 -0
  66. package/src/frontend/blocks.ts +578 -0
  67. package/src/frontend/configuration.ts +83 -0
  68. package/src/frontend/const-functions.ts +204 -0
  69. package/src/frontend/control-flow.ts +787 -0
  70. package/src/frontend/cooperative.ts +114 -0
  71. package/src/frontend/coroutines.ts +1336 -0
  72. package/src/frontend/dialect-names.ts +104 -0
  73. package/src/frontend/elaborate/array-searches.ts +224 -0
  74. package/src/frontend/elaborate/assignments.ts +343 -0
  75. package/src/frontend/elaborate/calls.ts +1342 -0
  76. package/src/frontend/elaborate/capture-protocol.ts +237 -0
  77. package/src/frontend/elaborate/capture.ts +388 -0
  78. package/src/frontend/elaborate/declarations.ts +124 -0
  79. package/src/frontend/elaborate/dialect.ts +473 -0
  80. package/src/frontend/elaborate/expressions.ts +2171 -0
  81. package/src/frontend/elaborate/filtered-arrays.ts +643 -0
  82. package/src/frontend/elaborate/frame.ts +145 -0
  83. package/src/frontend/elaborate/generics.ts +123 -0
  84. package/src/frontend/elaborate/graph.ts +141 -0
  85. package/src/frontend/elaborate/imports.ts +105 -0
  86. package/src/frontend/elaborate/index.ts +851 -0
  87. package/src/frontend/elaborate/initializers.ts +235 -0
  88. package/src/frontend/elaborate/iterators.ts +1541 -0
  89. package/src/frontend/elaborate/layouts.ts +1025 -0
  90. package/src/frontend/elaborate/model.ts +478 -0
  91. package/src/frontend/elaborate/names.ts +134 -0
  92. package/src/frontend/elaborate/reduce.ts +900 -0
  93. package/src/frontend/elaborate/references.ts +727 -0
  94. package/src/frontend/elaborate/retention.ts +284 -0
  95. package/src/frontend/elaborate/specialize.ts +1137 -0
  96. package/src/frontend/elaborate/statements.ts +1220 -0
  97. package/src/frontend/elaborate/storage.ts +836 -0
  98. package/src/frontend/elaborate/time-constructors.ts +34 -0
  99. package/src/frontend/elaborate/values.ts +200 -0
  100. package/src/frontend/elaborate/writes.ts +415 -0
  101. package/src/frontend/expressions.ts +1382 -0
  102. package/src/frontend/generated-names.ts +95 -0
  103. package/src/frontend/intrinsics.ts +382 -0
  104. package/src/frontend/lowering-key.ts +23 -0
  105. package/src/frontend/module-graph.ts +156 -0
  106. package/src/frontend/positions.ts +359 -0
  107. package/src/frontend/promise-chains.ts +872 -0
  108. package/src/frontend/promises.ts +419 -0
  109. package/src/frontend/routines.ts +239 -0
  110. package/src/frontend/sdk.ts +187 -0
  111. package/src/frontend/services.ts +609 -0
  112. package/src/frontend/statements.ts +871 -0
  113. package/src/frontend/tasks.ts +526 -0
  114. package/src/frontend/telemetry.ts +174 -0
  115. package/src/frontend/types.ts +1073 -0
  116. package/src/frontend/typescript.ts +651 -0
  117. package/src/index.ts +9 -0
  118. package/src/ir/aggregates.ts +291 -0
  119. package/src/ir/allocation.ts +96 -0
  120. package/src/ir/conditions.ts +855 -0
  121. package/src/ir/core-model.ts +289 -0
  122. package/src/ir/declarations.ts +281 -0
  123. package/src/ir/diagnostics.ts +68 -0
  124. package/src/ir/domains.ts +151 -0
  125. package/src/ir/fold.ts +279 -0
  126. package/src/ir/identity.ts +19 -0
  127. package/src/ir/index.ts +137 -0
  128. package/src/ir/job-errors.ts +69 -0
  129. package/src/ir/layouts.ts +211 -0
  130. package/src/ir/link.ts +302 -0
  131. package/src/ir/model.ts +394 -0
  132. package/src/ir/normalization-error.ts +10 -0
  133. package/src/ir/normalize.ts +52 -0
  134. package/src/ir/numeric.ts +163 -0
  135. package/src/ir/operands.ts +160 -0
  136. package/src/ir/ranges.ts +420 -0
  137. package/src/ir/routine.ts +1116 -0
  138. package/src/ir/scalars.ts +453 -0
  139. package/src/ir/share.ts +790 -0
  140. package/src/ir/short-circuit.ts +140 -0
  141. package/src/ir/tables.ts +111 -0
  142. package/src/ir/temporaries.ts +138 -0
  143. package/src/ir/types.ts +151 -0
  144. package/src/project/artifacts.ts +424 -0
  145. package/src/project/cli.ts +211 -0
  146. package/src/project/debugger.ts +111 -0
  147. package/src/project/deployments.ts +387 -0
  148. package/src/project/flags.ts +14 -0
  149. package/src/project/git.ts +35 -0
  150. package/src/project/plant.ts +629 -0
  151. package/src/project/project-commands.ts +507 -0
  152. package/src/project/project.ts +559 -0
  153. package/src/project/release.ts +152 -0
  154. package/src/project/server-check.ts +54 -0
  155. package/src/project/server.ts +165 -0
  156. package/src/project/terminal.ts +45 -0
  157. package/src/runtime/async.ts +207 -0
  158. package/src/runtime/launch.ts +59 -0
  159. package/src/runtime/main.ts +59 -0
  160. package/src/runtime/missing-file.ts +3 -0
  161. package/src/runtime/process.ts +177 -0
  162. package/src/runtime/program.ts +109 -0
  163. package/src/runtime/retain.ts +108 -0
  164. package/src/runtime/runtime.ts +324 -0
  165. package/src/runtime/settings.ts +27 -0
  166. package/src/runtime/storage.ts +456 -0
  167. package/src/runtime/symbols.ts +97 -0
  168. package/src/runtime/telemetry.ts +154 -0
  169. package/src/runtime/transport.ts +168 -0
  170. package/stdlib/clock.ts +8 -0
  171. package/stdlib/collections.ts +534 -0
  172. package/stdlib/plc-time.ts +2 -0
  173. package/stdlib/retain.ts +4 -0
  174. package/stdlib/signals.ts +142 -0
  175. package/stdlib/telemetry.ts +89 -0
  176. package/types/async.d.ts +87 -0
  177. package/types/clock.d.ts +8 -0
  178. package/types/config.d.ts +2 -0
  179. package/types/data-types.d.ts +31 -0
  180. package/types/dialect.d.ts +49 -0
  181. package/types/globals.d.ts +10 -0
  182. package/types/index.d.ts +212 -0
  183. package/types/retain.d.ts +12 -0
  184. package/types/telemetry.d.ts +47 -0
@@ -0,0 +1,445 @@
1
+ import {
2
+ type Constant,
3
+ type CoreTables,
4
+ instanceFields,
5
+ type PathCondition,
6
+ type PlcType,
7
+ type ReconstructedSegment,
8
+ type Reconstruction,
9
+ staticNames,
10
+ type Variable,
11
+ type VerifiedCoreModule,
12
+ } from "@tsplc/compiler/ir";
13
+ import type {
14
+ ExplainAssignment,
15
+ ExplainCondition,
16
+ ExplainLocal,
17
+ } from "@tsplc/debug/explain-file";
18
+ import { anyIndex, patternOf } from "@tsplc/debug/paths";
19
+ import { typescriptPath } from "../debug";
20
+ import { constantValue } from "../manifest";
21
+ import { storagePath } from "../storage";
22
+ import { parameterKey } from "./parameters";
23
+ import type { Row } from "./rows";
24
+ import type { ExplainTables } from "./tables";
25
+
26
+ /** A place as the debugger names it: a storage path, with a `[*]` for each dynamic index. */
27
+ export interface Spelled {
28
+ readonly text: string;
29
+ readonly indexes: readonly number[];
30
+ readonly type: PlcType | undefined;
31
+ }
32
+
33
+ const aggregateKinds: ReadonlySet<PlcType["kind"]> = new Set([
34
+ "record",
35
+ "array",
36
+ "block",
37
+ ]);
38
+
39
+ /** The storage path of a module variable or constant table; undefined for anything else. */
40
+ export const storedPath = (variable: Variable): string | undefined =>
41
+ variable.storage === "constant"
42
+ ? storagePath(variable)
43
+ : typescriptPath(variable);
44
+
45
+ /** The part of a constant at a path of record fields; an element of an array stays a read of the table. */
46
+ const constantAt = (
47
+ value: Constant,
48
+ names: readonly string[],
49
+ ): Constant | undefined => {
50
+ const [name, ...rest] = names;
51
+ if (name === undefined) {
52
+ return value;
53
+ }
54
+ if (value.kind === "record") {
55
+ const field = value.fields[name];
56
+ return field && constantAt(field, rest);
57
+ }
58
+ return undefined;
59
+ };
60
+
61
+ /**
62
+ * Writes reconstructions, conditions and places into `tables` as a row names them, and keeps the
63
+ * locals the rows define and the rows that read each path.
64
+ */
65
+ export function explainEncoder(
66
+ program: VerifiedCoreModule,
67
+ core: CoreTables,
68
+ tables: ExplainTables,
69
+ /** The parameters each routine writes through, by `parameterKey`. */
70
+ writesThrough: ReadonlySet<string>,
71
+ ) {
72
+ const { paths, texts, expressions } = tables;
73
+ /** Fields that hold an object reference, with every index as `[*]`. */
74
+ const referenceFields = new Set(
75
+ (program.references?.fields ?? []).map(patternOf),
76
+ );
77
+ /** The rows that read each path. */
78
+ const readers = new Map<string, Set<number>>();
79
+ const locals: ExplainLocal[] = [];
80
+ const rowLocals = new Map<
81
+ string,
82
+ { index: number; definitions: ExplainLocal["definitions"] }
83
+ >();
84
+
85
+ /** A local of a row, by its index in `locals`, made when the row first names it. */
86
+ const local = (row: Row, root: string) => {
87
+ const key = `${row.index}:${root}`;
88
+ const known = rowLocals.get(key);
89
+ if (known) {
90
+ return known;
91
+ }
92
+ const variable = core.variableOf(root);
93
+ const created: ExplainLocal = {
94
+ routine: row.index,
95
+ name: variable.name,
96
+ parameter: variable.storage === "parameter",
97
+ reference: false,
98
+ definitions: [],
99
+ };
100
+ const entry = { index: locals.length, definitions: created.definitions };
101
+ locals.push(created);
102
+ rowLocals.set(key, entry);
103
+ return entry;
104
+ };
105
+
106
+ const fieldType = (
107
+ type: PlcType | undefined,
108
+ name: string,
109
+ ): PlcType | undefined => {
110
+ if (type?.kind === "block") {
111
+ const block = core.blockOf(type.block);
112
+ const field = instanceFields(block).find(
113
+ (variable) => variable.name === name,
114
+ );
115
+ return field && core.types.get(field.type);
116
+ }
117
+ if (type?.kind === "record") {
118
+ const field = type.fields.find((item) => item.name === name);
119
+ return field && core.types.get(field.type);
120
+ }
121
+ return undefined;
122
+ };
123
+
124
+ /** A spelled place followed further along a path evaluated in `row`. */
125
+ const extend = (
126
+ row: Row,
127
+ spelled: Spelled,
128
+ path: readonly ReconstructedSegment[],
129
+ ): Spelled =>
130
+ path.reduce<Spelled>((place, segment) => {
131
+ if (segment.kind === "field") {
132
+ // Block outputs are fields of the instance itself, so its `out` view adds no segment.
133
+ if (place.type?.kind === "block" && segment.name === "out") {
134
+ const { output } = place.type;
135
+ return {
136
+ text: place.text,
137
+ indexes: place.indexes,
138
+ type: output ? core.types.get(output) : undefined,
139
+ };
140
+ }
141
+ return {
142
+ text: `${place.text}.${segment.name}`,
143
+ indexes: place.indexes,
144
+ type: fieldType(place.type, segment.name),
145
+ };
146
+ }
147
+ const type =
148
+ place.type?.kind === "array"
149
+ ? core.types.get(place.type.element)
150
+ : undefined;
151
+ const value = segment.value;
152
+ if (value.kind === "constant" && value.value.kind === "integer") {
153
+ return {
154
+ text: `${place.text}[${value.value.value}]`,
155
+ indexes: place.indexes,
156
+ type,
157
+ };
158
+ }
159
+ return {
160
+ text: `${place.text}${anyIndex}`,
161
+ indexes: [...place.indexes, expression(row, value)],
162
+ type,
163
+ };
164
+ }, spelled);
165
+
166
+ /** The storage path of a place in a row, or nothing for a temporary, local or parameter. */
167
+ const spell = (
168
+ row: Row,
169
+ root: string,
170
+ path: readonly ReconstructedSegment[],
171
+ ): Spelled | undefined => {
172
+ const variable = core.variableOf(root);
173
+ const field = row.fields.has(root);
174
+ if (field && row.instance === null) {
175
+ return undefined;
176
+ }
177
+ const start = field
178
+ ? `${row.instance}.${variable.name}`
179
+ : storedPath(variable);
180
+ if (start === undefined) {
181
+ return undefined;
182
+ }
183
+ return extend(
184
+ row,
185
+ { text: start, indexes: [], type: core.types.get(variable.type) },
186
+ path,
187
+ );
188
+ };
189
+
190
+ /** A place as a record names what it writes or calls. */
191
+ const placeRef = (spelled: Spelled): ExplainAssignment["writes"] =>
192
+ spelled.indexes.length
193
+ ? {
194
+ kind: "pattern",
195
+ path: paths.add(spelled.text),
196
+ indexes: [...spelled.indexes],
197
+ }
198
+ : { kind: "path", path: paths.add(spelled.text) };
199
+
200
+ const isAggregate = (spelled: Spelled): boolean =>
201
+ spelled.type !== undefined && aggregateKinds.has(spelled.type.kind);
202
+
203
+ /** A live read of a stored place at `capturedAt`. */
204
+ const storedRead = (
205
+ row: Row,
206
+ spelled: Spelled,
207
+ read: {
208
+ capturedAt: number;
209
+ interleaved?: true;
210
+ type: string;
211
+ },
212
+ ): number => {
213
+ const path = row.slots.paths.add(paths.add(spelled.text));
214
+ const { capturedAt, interleaved, type } = read;
215
+ return expressions.add(
216
+ spelled.indexes.length
217
+ ? {
218
+ kind: "pattern",
219
+ path,
220
+ indexes: [...spelled.indexes],
221
+ capturedAt,
222
+ ...(interleaved && { interleaved }),
223
+ type,
224
+ }
225
+ : {
226
+ kind: "read",
227
+ path,
228
+ capturedAt,
229
+ ...(interleaved && { interleaved }),
230
+ type,
231
+ },
232
+ );
233
+ };
234
+
235
+ const opaque = (
236
+ reason: string,
237
+ type: string,
238
+ reads: readonly number[] = [],
239
+ ): number =>
240
+ expressions.add({
241
+ kind: "opaque",
242
+ text: texts.add(reason),
243
+ reads: [...reads],
244
+ type,
245
+ });
246
+
247
+ const expression = (row: Row, node: Reconstruction): number => {
248
+ switch (node.kind) {
249
+ case "read": {
250
+ // A constant table read at fixed indexes is its value; there is nothing to read live.
251
+ const table = core.variableOf(node.root);
252
+ const names = staticNames(node.path);
253
+ const fixed =
254
+ table.storage === "constant" && table.initial && names
255
+ ? constantAt(table.initial, names)
256
+ : undefined;
257
+ if (fixed) {
258
+ return expressions.add({
259
+ kind: "constant",
260
+ value: constantValue(fixed),
261
+ type: node.type,
262
+ });
263
+ }
264
+ const spelled = spell(row, node.root, node.path);
265
+ if (!spelled) {
266
+ return opaque("not stored", node.type);
267
+ }
268
+ const read = storedRead(row, spelled, {
269
+ capturedAt: node.capturedAt,
270
+ ...(node.interleaved && { interleaved: true as const }),
271
+ type: node.type,
272
+ });
273
+ const readBy = readers.get(spelled.text) ?? new Set<number>();
274
+ readBy.add(row.index);
275
+ readers.set(spelled.text, readBy);
276
+ // A field holding an object reference holds the object's id: show the object.
277
+ return referenceFields.has(patternOf(spelled.text))
278
+ ? expressions.add({ kind: "reference", operand: read })
279
+ : read;
280
+ }
281
+ case "local": {
282
+ const names = staticNames(node.path);
283
+ return names
284
+ ? expressions.add({
285
+ kind: "local",
286
+ local: row.slots.locals.add(local(row, node.root).index),
287
+ path: names,
288
+ at: node.at,
289
+ type: node.type,
290
+ })
291
+ : opaque("a local at a dynamic index", node.type);
292
+ }
293
+ case "parameter": {
294
+ const names = staticNames(node.path);
295
+ if (!names) {
296
+ return opaque("a parameter at a dynamic index", node.type);
297
+ }
298
+ const written = writesThrough.has(
299
+ parameterKey(row.routine.id, node.parameter),
300
+ );
301
+ return expressions.add({
302
+ kind: "parameter",
303
+ parameter: node.parameter,
304
+ path: names,
305
+ at: node.at,
306
+ local: written
307
+ ? row.slots.locals.add(local(row, node.root).index)
308
+ : null,
309
+ type: node.type,
310
+ });
311
+ }
312
+ case "call": {
313
+ const call = row.calls.get(node.call);
314
+ return call === undefined
315
+ ? opaque("a call that isn't traced", node.type)
316
+ : expressions.add({
317
+ kind: "call",
318
+ call: row.slots.calls.add(call),
319
+ path: [...node.path],
320
+ type: node.type,
321
+ });
322
+ }
323
+ case "constant":
324
+ return expressions.add({
325
+ kind: "constant",
326
+ value: constantValue(node.value),
327
+ type: node.type,
328
+ });
329
+ case "clock":
330
+ return expressions.add({ kind: "clock", type: node.type });
331
+ case "unary":
332
+ return expressions.add({
333
+ kind: "unary",
334
+ operator: node.operator,
335
+ operand: expression(row, node.operand),
336
+ type: node.type,
337
+ });
338
+ case "binary":
339
+ return expressions.add({
340
+ kind: "binary",
341
+ operator: node.operator,
342
+ ...(node.policy && { policy: node.policy }),
343
+ left: expression(row, node.left),
344
+ right: expression(row, node.right),
345
+ type: node.type,
346
+ });
347
+ case "convert":
348
+ return expressions.add({
349
+ kind: "convert",
350
+ policy: node.policy,
351
+ operand: expression(row, node.operand),
352
+ type: node.type,
353
+ });
354
+ case "logical":
355
+ return expressions.add({
356
+ kind: "logical",
357
+ operator: node.operator,
358
+ left: expression(row, node.left),
359
+ right: expression(row, node.right),
360
+ });
361
+ case "select":
362
+ return expressions.add({
363
+ kind: "select",
364
+ condition: expression(row, node.condition),
365
+ // biome-ignore lint/suspicious/noThenProperty: a `?:` branch is data, not a thenable.
366
+ then: expression(row, node.then),
367
+ else: expression(row, node.else),
368
+ type: node.type,
369
+ });
370
+ case "record":
371
+ return expressions.add({
372
+ kind: "record",
373
+ fields: Object.fromEntries(
374
+ Object.entries(node.fields).map(([name, value]) => [
375
+ name,
376
+ expression(row, value),
377
+ ]),
378
+ ),
379
+ type: node.type,
380
+ });
381
+ case "array":
382
+ return expressions.add({
383
+ kind: "array",
384
+ lower: node.lower,
385
+ items: node.items.map((item) => expression(row, item)),
386
+ type: node.type,
387
+ });
388
+ case "opaque":
389
+ return opaque(
390
+ node.reason,
391
+ node.type,
392
+ node.reads.map((read) => expression(row, read)),
393
+ );
394
+ }
395
+ };
396
+
397
+ const condition = (row: Row, item: PathCondition): ExplainCondition => {
398
+ switch (item.kind) {
399
+ case "test":
400
+ return {
401
+ kind: "test",
402
+ test: expression(row, item.test),
403
+ holds: item.holds,
404
+ at: item.at,
405
+ text: tables.text(item.span),
406
+ };
407
+ case "cases":
408
+ return {
409
+ kind: "cases",
410
+ test: expression(row, item.test),
411
+ values: item.values.map(constantValue),
412
+ member: item.member,
413
+ at: item.at,
414
+ text: tables.text(item.span),
415
+ };
416
+ case "exits":
417
+ return {
418
+ kind: "exits",
419
+ any: item.any.map((all) => all.map((part) => condition(row, part))),
420
+ };
421
+ case "loop":
422
+ case "loopReturns":
423
+ return { kind: item.kind, loop: item.loop };
424
+ case "job":
425
+ return { kind: "job", step: item.step };
426
+ case "jobBusy":
427
+ return { kind: "jobBusy" };
428
+ }
429
+ };
430
+
431
+ return {
432
+ expression,
433
+ condition,
434
+ spell,
435
+ extend,
436
+ local,
437
+ placeRef,
438
+ isAggregate,
439
+ storedRead,
440
+ readers,
441
+ locals,
442
+ };
443
+ }
444
+
445
+ export type ExplainEncoder = ReturnType<typeof explainEncoder>;
@@ -0,0 +1,134 @@
1
+ import type { VerifiedCoreModule } from "@tsplc/compiler/ir";
2
+ import type {
3
+ ExplainAssignment,
4
+ ExplainFlags,
5
+ } from "@tsplc/debug/explain-file";
6
+ import type { DebugManifest } from "@tsplc/debug/manifest";
7
+ import { createPaths } from "@tsplc/debug/paths";
8
+ import { type InstancePath, instanceText } from "../debug";
9
+ import { isPublished } from "../global-lists";
10
+ import { serviceBlock, serviceName } from "../http";
11
+ import type { Instrumentation } from "../instrumentation";
12
+ import { constantValue, enclosingJob } from "../manifest";
13
+ import type { CodesysPlan } from "../plan";
14
+ import { required } from "../required";
15
+ import { storedPath } from "./expressions";
16
+ import type { ExplainTables } from "./tables";
17
+
18
+ /** What explain.json says about stored paths beyond their writers, each at the shortest path it applies to. */
19
+ export function explainFlags({
20
+ plan,
21
+ program,
22
+ instances,
23
+ instrumentation,
24
+ manifest,
25
+ tables,
26
+ assignments,
27
+ readers,
28
+ rowTasks,
29
+ }: {
30
+ plan: CodesysPlan;
31
+ program: VerifiedCoreModule;
32
+ instances: ReadonlyMap<string, readonly InstancePath[]>;
33
+ instrumentation: Instrumentation;
34
+ manifest: DebugManifest;
35
+ tables: ExplainTables;
36
+ assignments: readonly ExplainAssignment[];
37
+ /** The rows that read each path. */
38
+ readers: ReadonlyMap<string, ReadonlySet<number>>;
39
+ rowTasks: ReadonlyMap<number, ReadonlySet<string>>;
40
+ }): ExplainFlags[] {
41
+ const { paths } = tables;
42
+ const clientWritable = isPublished(plan);
43
+ const flags = new Map<string, ExplainFlags>();
44
+ const flag = (path: string): ExplainFlags => {
45
+ const known = flags.get(path);
46
+ if (known) {
47
+ return known;
48
+ }
49
+ const created: ExplainFlags = {
50
+ path: paths.add(path),
51
+ input: null,
52
+ clientWritable: false,
53
+ hmiControl: null,
54
+ retained: false,
55
+ constantTable: false,
56
+ writerTasks: [],
57
+ service: null,
58
+ initial: null,
59
+ reference: false,
60
+ };
61
+ flags.set(path, created);
62
+ return created;
63
+ };
64
+ const writtenPath = (assignment: ExplainAssignment) =>
65
+ required(paths.items[assignment.writes.path]);
66
+ const writtenPaths = new Set(assignments.map(writtenPath));
67
+ for (const variable of program.variables) {
68
+ const path = storedPath(variable);
69
+ if (path === undefined) {
70
+ continue;
71
+ }
72
+ const published = clientWritable(variable);
73
+ const constant = variable.storage === "constant";
74
+ const retained = variable.retention !== "none";
75
+ if (published || retained || constant) {
76
+ const item = flag(path);
77
+ item.clientWritable = published;
78
+ item.retained = retained;
79
+ item.constantTable = constant;
80
+ }
81
+ if (variable.initial && (constant || !writtenPaths.has(path))) {
82
+ flag(path).initial = constantValue(variable.initial);
83
+ }
84
+ }
85
+ manifest.io.forEach((io, index) => {
86
+ if (io.direction === "input") {
87
+ flag(io.path).input = index;
88
+ }
89
+ });
90
+ for (const block of program.blocks) {
91
+ const endpoint = plan.http[block.id];
92
+ if (!serviceBlock(block) || !endpoint) {
93
+ continue;
94
+ }
95
+ for (const path of required(instances.get(block.id))) {
96
+ const instance = instanceText(path);
97
+ flag(instance).service = {
98
+ job: enclosingJob(instrumentation, instance) ?? 0,
99
+ service: serviceName(endpoint.path),
100
+ };
101
+ }
102
+ }
103
+ for (const field of program.references?.fields ?? []) {
104
+ flag(field).reference = true;
105
+ }
106
+ // An HMI control names the published IEC symbol it writes.
107
+ const targets = createPaths(manifest);
108
+ for (const control of plan.project.hmi?.controls ?? []) {
109
+ const path = targets.fromTarget(control.target);
110
+ if (path) {
111
+ flag(path).hmiControl = control.label;
112
+ }
113
+ }
114
+ const writers = new Map<string, Set<string>>();
115
+ for (const assignment of assignments) {
116
+ const path = writtenPath(assignment);
117
+ const tasks = writers.get(path) ?? new Set<string>();
118
+ for (const task of rowTasks.get(assignment.routine) ?? []) {
119
+ tasks.add(task);
120
+ }
121
+ writers.set(path, tasks);
122
+ }
123
+ for (const [path, tasks] of writers) {
124
+ const readTasks = new Set(
125
+ [...(readers.get(path) ?? [])].flatMap((row) => [
126
+ ...(rowTasks.get(row) ?? []),
127
+ ]),
128
+ );
129
+ if (tasks.size > 1 || [...readTasks].some((task) => !tasks.has(task))) {
130
+ flag(path).writerTasks = [...tasks].sort();
131
+ }
132
+ }
133
+ return [...flags.values()];
134
+ }
@@ -0,0 +1,53 @@
1
+ import {
2
+ type CoreStatement,
3
+ isBlockRoutine,
4
+ nestedBodies,
5
+ } from "@tsplc/compiler/ir";
6
+ import type { Routine } from "./rows";
7
+
8
+ /** A parameter of a routine, by its position. */
9
+ export const parameterKey = (routine: string, index: number) =>
10
+ `${routine}#${index}`;
11
+
12
+ /** The position of `root` among a function's parameters, or -1. */
13
+ export const parameterIndex = (routine: Routine, root: string): number =>
14
+ isBlockRoutine(routine)
15
+ ? -1
16
+ : routine.parameters.findIndex((parameter) => parameter.id === root);
17
+
18
+ const statementsOf = (items: readonly CoreStatement[]): CoreStatement[] =>
19
+ items.flatMap((item) => [item, ...nestedBodies(item).flatMap(statementsOf)]);
20
+
21
+ /**
22
+ * The parameters each routine writes through, by `parameterKey`: those it assigns, and those it
23
+ * passes on to a routine that writes through the parameter it receives them as.
24
+ */
25
+ export function writtenThroughParameters(
26
+ routines: readonly Routine[],
27
+ ): ReadonlySet<string> {
28
+ const propagate = (written: ReadonlySet<string>): ReadonlySet<string> => {
29
+ const added = routines.flatMap((routine) =>
30
+ statementsOf(routine.body).flatMap((item) => {
31
+ const indexes = [
32
+ ...(item.kind === "assign"
33
+ ? [parameterIndex(routine, item.root)]
34
+ : []),
35
+ ...(item.kind === "invoke"
36
+ ? item.arguments.flatMap((argument, index) =>
37
+ argument.kind === "borrow" &&
38
+ written.has(parameterKey(item.function, index))
39
+ ? [parameterIndex(routine, argument.place.root)]
40
+ : [],
41
+ )
42
+ : []),
43
+ ];
44
+ return indexes
45
+ .filter((index) => index >= 0)
46
+ .map((index) => parameterKey(routine.id, index))
47
+ .filter((key) => !written.has(key));
48
+ }),
49
+ );
50
+ return added.length ? propagate(new Set([...written, ...added])) : written;
51
+ };
52
+ return propagate(new Set());
53
+ }