@tsplc/compiler 0.5.13 → 0.5.15

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 (263) hide show
  1. package/README.md +33 -8
  2. package/dist/cli.js +794 -621
  3. package/dist/cli.js.map +19 -16
  4. package/dist/clock.js +5 -2
  5. package/dist/clock.js.map +5 -4
  6. package/dist/codesys.js +4162 -4157
  7. package/dist/codesys.js.map +43 -29
  8. package/dist/index.js +7852 -6549
  9. package/dist/index.js.map +53 -50
  10. package/dist/ir.js +2482 -2277
  11. package/dist/ir.js.map +28 -25
  12. package/dist/retain.js +3 -1
  13. package/dist/retain.js.map +3 -3
  14. package/dist/signals.js +115 -0
  15. package/dist/signals.js.map +10 -0
  16. package/dist/telemetry.js +25 -20
  17. package/dist/telemetry.js.map +6 -6
  18. package/package.json +19 -39
  19. package/src/codesys/config.ts +3 -3
  20. package/src/codesys/debug.ts +92 -108
  21. package/src/codesys/emit.ts +100 -88
  22. package/src/codesys/endpoints.ts +111 -0
  23. package/src/codesys/engineering.ts +30 -54
  24. package/src/codesys/explain/expressions.ts +445 -0
  25. package/src/codesys/explain/flags.ts +134 -0
  26. package/src/codesys/explain/parameters.ts +53 -0
  27. package/src/codesys/explain/rows.ts +182 -0
  28. package/src/codesys/explain/tables.ts +79 -0
  29. package/src/codesys/explain/tasks.ts +68 -0
  30. package/src/codesys/explain/writes.ts +497 -0
  31. package/src/codesys/explain.ts +85 -1401
  32. package/src/codesys/expressions.ts +41 -33
  33. package/src/codesys/global-lists.ts +114 -94
  34. package/src/codesys/global-names.ts +12 -14
  35. package/src/codesys/http.ts +428 -388
  36. package/src/codesys/instrumentation.ts +142 -148
  37. package/src/codesys/io-paths.ts +31 -23
  38. package/src/codesys/loops.ts +101 -0
  39. package/src/codesys/manifest.ts +269 -269
  40. package/src/codesys/names.ts +10 -33
  41. package/src/codesys/numeric.ts +89 -42
  42. package/src/codesys/plan.ts +202 -190
  43. package/src/codesys/programs.ts +23 -16
  44. package/src/codesys/publication.ts +13 -86
  45. package/src/codesys/required.ts +8 -0
  46. package/src/codesys/routines.ts +103 -489
  47. package/src/codesys/scheduling.ts +96 -0
  48. package/src/codesys/service-channels.ts +23 -0
  49. package/src/codesys/sources.ts +34 -28
  50. package/src/codesys/st-files.ts +112 -105
  51. package/src/codesys/statements.ts +409 -0
  52. package/src/codesys/storage.ts +41 -23
  53. package/src/codesys/symbols.ts +130 -119
  54. package/src/codesys/task-access.ts +65 -221
  55. package/src/codesys/telemetry-settings.ts +19 -0
  56. package/src/codesys/telemetry.ts +185 -148
  57. package/src/codesys/types.ts +69 -109
  58. package/src/codesys/vendor.ts +13 -50
  59. package/src/frontend/analyzer.ts +66 -111
  60. package/src/frontend/async.ts +316 -128
  61. package/src/frontend/blocks.ts +178 -154
  62. package/src/frontend/configuration.ts +49 -153
  63. package/src/frontend/const-functions.ts +50 -40
  64. package/src/frontend/control-flow.ts +164 -129
  65. package/src/frontend/cooperative.ts +17 -9
  66. package/src/frontend/coroutines.ts +539 -441
  67. package/src/frontend/dialect-names.ts +104 -0
  68. package/src/frontend/elaborate/array-searches.ts +77 -69
  69. package/src/frontend/elaborate/assignments.ts +68 -77
  70. package/src/frontend/elaborate/calls.ts +878 -512
  71. package/src/frontend/elaborate/capture-protocol.ts +118 -21
  72. package/src/frontend/elaborate/capture.ts +196 -109
  73. package/src/frontend/elaborate/declarations.ts +124 -0
  74. package/src/frontend/elaborate/dialect.ts +119 -46
  75. package/src/frontend/elaborate/expressions.ts +763 -442
  76. package/src/frontend/elaborate/filtered-arrays.ts +127 -91
  77. package/src/frontend/elaborate/frame.ts +50 -2
  78. package/src/frontend/elaborate/generics.ts +20 -34
  79. package/src/frontend/elaborate/graph.ts +13 -87
  80. package/src/frontend/elaborate/imports.ts +0 -5
  81. package/src/frontend/elaborate/index.ts +416 -169
  82. package/src/frontend/elaborate/initializers.ts +64 -42
  83. package/src/frontend/elaborate/iterators.ts +228 -364
  84. package/src/frontend/elaborate/layouts.ts +477 -116
  85. package/src/frontend/elaborate/model.ts +113 -55
  86. package/src/frontend/elaborate/references.ts +116 -138
  87. package/src/frontend/elaborate/retention.ts +137 -59
  88. package/src/frontend/elaborate/specialize.ts +617 -308
  89. package/src/frontend/elaborate/statements.ts +436 -247
  90. package/src/frontend/elaborate/storage.ts +280 -213
  91. package/src/frontend/elaborate/time-constructors.ts +34 -0
  92. package/src/frontend/elaborate/values.ts +77 -7
  93. package/src/frontend/elaborate/writes.ts +98 -84
  94. package/src/frontend/expressions.ts +149 -151
  95. package/src/frontend/generated-names.ts +3 -3
  96. package/src/frontend/intrinsics.ts +31 -17
  97. package/src/frontend/lowering-key.ts +23 -0
  98. package/src/frontend/module-graph.ts +105 -43
  99. package/src/frontend/positions.ts +43 -35
  100. package/src/frontend/promise-chains.ts +226 -266
  101. package/src/frontend/promises.ts +88 -67
  102. package/src/frontend/routines.ts +122 -65
  103. package/src/frontend/sdk.ts +104 -5
  104. package/src/frontend/services.ts +149 -102
  105. package/src/frontend/statements.ts +60 -64
  106. package/src/frontend/tasks.ts +151 -96
  107. package/src/frontend/telemetry.ts +1 -1
  108. package/src/frontend/types.ts +366 -181
  109. package/src/frontend/typescript.ts +239 -112
  110. package/src/index.ts +2 -1
  111. package/src/ir/aggregates.ts +90 -75
  112. package/src/ir/allocation.ts +29 -18
  113. package/src/ir/conditions.ts +38 -78
  114. package/src/ir/core-model.ts +128 -11
  115. package/src/ir/declarations.ts +18 -32
  116. package/src/ir/diagnostics.ts +25 -18
  117. package/src/ir/domains.ts +26 -23
  118. package/src/ir/fold.ts +55 -50
  119. package/src/ir/identity.ts +10 -0
  120. package/src/ir/index.ts +31 -7
  121. package/src/ir/job-errors.ts +30 -8
  122. package/src/ir/layouts.ts +63 -34
  123. package/src/ir/link.ts +11 -46
  124. package/src/ir/model.ts +25 -6
  125. package/src/ir/normalize.ts +3 -28
  126. package/src/ir/numeric.ts +45 -18
  127. package/src/ir/operands.ts +102 -0
  128. package/src/ir/ranges.ts +242 -67
  129. package/src/ir/routine.ts +126 -183
  130. package/src/ir/scalars.ts +29 -25
  131. package/src/ir/share.ts +323 -343
  132. package/src/ir/short-circuit.ts +140 -0
  133. package/src/ir/tables.ts +111 -0
  134. package/src/ir/temporaries.ts +15 -113
  135. package/src/ir/types.ts +32 -8
  136. package/src/project/artifacts.ts +143 -71
  137. package/src/project/cli.ts +104 -87
  138. package/src/project/debugger.ts +48 -34
  139. package/src/project/deployments.ts +12 -68
  140. package/src/project/flags.ts +14 -0
  141. package/src/project/plant.ts +293 -225
  142. package/src/project/project-commands.ts +254 -192
  143. package/src/project/project.ts +184 -141
  144. package/src/project/release.ts +22 -28
  145. package/src/project/server.ts +22 -26
  146. package/src/project/terminal.ts +13 -2
  147. package/src/runtime/async.ts +17 -13
  148. package/src/runtime/launch.ts +5 -2
  149. package/src/runtime/main.ts +8 -3
  150. package/src/runtime/missing-file.ts +3 -0
  151. package/src/runtime/process.ts +24 -19
  152. package/src/runtime/program.ts +20 -53
  153. package/src/runtime/retain.ts +18 -10
  154. package/src/runtime/runtime.ts +47 -60
  155. package/src/runtime/settings.ts +27 -0
  156. package/src/runtime/storage.ts +49 -74
  157. package/src/runtime/symbols.ts +19 -75
  158. package/src/runtime/telemetry.ts +37 -14
  159. package/src/runtime/transport.ts +26 -30
  160. package/stdlib/clock.ts +4 -2
  161. package/stdlib/plc-time.ts +2 -0
  162. package/stdlib/retain.ts +2 -0
  163. package/stdlib/signals.ts +142 -0
  164. package/stdlib/telemetry.ts +5 -12
  165. package/types/dialect.d.ts +49 -0
  166. package/types/globals.d.ts +2 -42
  167. package/types/index.d.ts +3 -1
  168. package/types/retain.d.ts +8 -2
  169. package/types/telemetry.d.ts +3 -0
  170. package/dist/bun-runtime.js +0 -1472
  171. package/dist/bun-runtime.js.map +0 -26
  172. package/dist/elaborate-capture.js +0 -304
  173. package/dist/elaborate-capture.js.map +0 -11
  174. package/dist/math.js +0 -24
  175. package/dist/math.js.map +0 -10
  176. package/dist/project.js +0 -626
  177. package/dist/project.js.map +0 -12
  178. package/dist/server-check.js +0 -59
  179. package/dist/server-check.js.map +0 -11
  180. package/src/frontend/exposure.ts +0 -181
  181. package/src/project/index.ts +0 -6
  182. package/src/runtime/.runtime-65O4JH/server/router.ts +0 -17
  183. package/src/runtime/.runtime-65O4JH/src/cell.ts +0 -45
  184. package/src/runtime/.runtime-65O4JH/src/controller.ts +0 -58
  185. package/src/runtime/.runtime-65O4JH/src/lane.ts +0 -6
  186. package/src/runtime/.runtime-65O4JH/src/main.ts +0 -11
  187. package/src/runtime/.runtime-65O4JH/src/services.ts +0 -8
  188. package/src/runtime/.runtime-65O4JH/tsconfig.json +0 -13
  189. package/src/runtime/.runtime-65O4JH/tsplc.config.ts +0 -31
  190. package/src/runtime/.runtime-Brw2kO/server/router.ts +0 -17
  191. package/src/runtime/.runtime-Brw2kO/src/cell.ts +0 -45
  192. package/src/runtime/.runtime-Brw2kO/src/controller.ts +0 -58
  193. package/src/runtime/.runtime-Brw2kO/src/lane.ts +0 -6
  194. package/src/runtime/.runtime-Brw2kO/src/main.ts +0 -11
  195. package/src/runtime/.runtime-Brw2kO/src/services.ts +0 -8
  196. package/src/runtime/.runtime-Brw2kO/tsconfig.json +0 -13
  197. package/src/runtime/.runtime-Brw2kO/tsplc.config.ts +0 -31
  198. package/src/runtime/.runtime-NHfu1D/server/router.ts +0 -17
  199. package/src/runtime/.runtime-NHfu1D/src/cell.ts +0 -45
  200. package/src/runtime/.runtime-NHfu1D/src/controller.ts +0 -58
  201. package/src/runtime/.runtime-NHfu1D/src/lane.ts +0 -6
  202. package/src/runtime/.runtime-NHfu1D/src/main.ts +0 -11
  203. package/src/runtime/.runtime-NHfu1D/src/services.ts +0 -8
  204. package/src/runtime/.runtime-NHfu1D/tsconfig.json +0 -13
  205. package/src/runtime/.runtime-NHfu1D/tsplc.config.ts +0 -31
  206. package/src/runtime/.runtime-VaUSOu/server/router.ts +0 -17
  207. package/src/runtime/.runtime-VaUSOu/src/cell.ts +0 -45
  208. package/src/runtime/.runtime-VaUSOu/src/controller.ts +0 -58
  209. package/src/runtime/.runtime-VaUSOu/src/lane.ts +0 -6
  210. package/src/runtime/.runtime-VaUSOu/src/main.ts +0 -11
  211. package/src/runtime/.runtime-VaUSOu/src/services.ts +0 -8
  212. package/src/runtime/.runtime-VaUSOu/tsconfig.json +0 -13
  213. package/src/runtime/.runtime-VaUSOu/tsplc.config.ts +0 -31
  214. package/src/runtime/.runtime-WbtNvR/server/router.ts +0 -17
  215. package/src/runtime/.runtime-WbtNvR/src/cell.ts +0 -45
  216. package/src/runtime/.runtime-WbtNvR/src/controller.ts +0 -58
  217. package/src/runtime/.runtime-WbtNvR/src/lane.ts +0 -6
  218. package/src/runtime/.runtime-WbtNvR/src/main.ts +0 -11
  219. package/src/runtime/.runtime-WbtNvR/src/services.ts +0 -8
  220. package/src/runtime/.runtime-WbtNvR/tsconfig.json +0 -13
  221. package/src/runtime/.runtime-WbtNvR/tsplc.config.ts +0 -31
  222. package/src/runtime/.runtime-opcua-GMPUt8/server/router.ts +0 -17
  223. package/src/runtime/.runtime-opcua-GMPUt8/src/cell.ts +0 -45
  224. package/src/runtime/.runtime-opcua-GMPUt8/src/controller.ts +0 -58
  225. package/src/runtime/.runtime-opcua-GMPUt8/src/lane.ts +0 -6
  226. package/src/runtime/.runtime-opcua-GMPUt8/src/main.ts +0 -11
  227. package/src/runtime/.runtime-opcua-GMPUt8/src/services.ts +0 -8
  228. package/src/runtime/.runtime-opcua-GMPUt8/tsconfig.json +0 -13
  229. package/src/runtime/.runtime-opcua-GMPUt8/tsplc.config.ts +0 -31
  230. package/src/runtime/.runtime-opcua-UwafxO/server/router.ts +0 -17
  231. package/src/runtime/.runtime-opcua-UwafxO/src/cell.ts +0 -45
  232. package/src/runtime/.runtime-opcua-UwafxO/src/controller.ts +0 -58
  233. package/src/runtime/.runtime-opcua-UwafxO/src/lane.ts +0 -6
  234. package/src/runtime/.runtime-opcua-UwafxO/src/main.ts +0 -11
  235. package/src/runtime/.runtime-opcua-UwafxO/src/services.ts +0 -8
  236. package/src/runtime/.runtime-opcua-UwafxO/tsconfig.json +0 -13
  237. package/src/runtime/.runtime-opcua-UwafxO/tsplc.config.ts +0 -31
  238. package/src/runtime/.runtime-opcua-ecaSee/server/router.ts +0 -17
  239. package/src/runtime/.runtime-opcua-ecaSee/src/cell.ts +0 -45
  240. package/src/runtime/.runtime-opcua-ecaSee/src/controller.ts +0 -58
  241. package/src/runtime/.runtime-opcua-ecaSee/src/lane.ts +0 -6
  242. package/src/runtime/.runtime-opcua-ecaSee/src/main.ts +0 -11
  243. package/src/runtime/.runtime-opcua-ecaSee/src/services.ts +0 -8
  244. package/src/runtime/.runtime-opcua-ecaSee/tsconfig.json +0 -13
  245. package/src/runtime/.runtime-opcua-ecaSee/tsplc.config.ts +0 -31
  246. package/src/runtime/.runtime-opcua-y91UCm/server/router.ts +0 -17
  247. package/src/runtime/.runtime-opcua-y91UCm/src/cell.ts +0 -45
  248. package/src/runtime/.runtime-opcua-y91UCm/src/controller.ts +0 -58
  249. package/src/runtime/.runtime-opcua-y91UCm/src/lane.ts +0 -6
  250. package/src/runtime/.runtime-opcua-y91UCm/src/main.ts +0 -11
  251. package/src/runtime/.runtime-opcua-y91UCm/src/services.ts +0 -8
  252. package/src/runtime/.runtime-opcua-y91UCm/tsconfig.json +0 -13
  253. package/src/runtime/.runtime-opcua-y91UCm/tsplc.config.ts +0 -31
  254. package/src/runtime/.runtime-ybIxYh/server/router.ts +0 -17
  255. package/src/runtime/.runtime-ybIxYh/src/cell.ts +0 -45
  256. package/src/runtime/.runtime-ybIxYh/src/controller.ts +0 -58
  257. package/src/runtime/.runtime-ybIxYh/src/lane.ts +0 -6
  258. package/src/runtime/.runtime-ybIxYh/src/main.ts +0 -11
  259. package/src/runtime/.runtime-ybIxYh/src/services.ts +0 -8
  260. package/src/runtime/.runtime-ybIxYh/tsconfig.json +0 -13
  261. package/src/runtime/.runtime-ybIxYh/tsplc.config.ts +0 -31
  262. package/stdlib/math.ts +0 -23
  263. package/types/plc.d.ts +0 -4
@@ -1,22 +1,31 @@
1
- // biome-ignore-all lint/style/noNonNullAssertion: Retain existing non-null assertions during the tooling migration; changing their contracts requires a separate review.
2
1
  import { createHash } from "node:crypto";
3
- import type {
4
- CoreExpression,
5
- CoreFunctionBlock,
6
- CoreStatement,
7
- GeneratedObject,
8
- SourceSpan,
9
- VerifiedCoreModule,
2
+ import {
3
+ type CoreExpression,
4
+ type CoreFunctionBlock,
5
+ type CoreStatement,
6
+ type GeneratedObject,
7
+ jobError,
8
+ type SourceSpan,
9
+ type VerifiedCoreModule,
10
10
  } from "@tsplc/compiler/ir";
11
11
  import type { ResolvedDebugConfig } from "@tsplc/config";
12
- import type { DebugManifest, DebugType } from "@tsplc/debug/manifest";
13
- import { createPaths, type PathManifest } from "@tsplc/debug/paths";
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";
14
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";
15
23
  import {
16
24
  DebugAnalysis,
17
- type InstancePath,
25
+ instanceText,
18
26
  type Operand,
19
- storagePath,
27
+ type Output,
28
+ typescriptPath,
20
29
  } from "./debug";
21
30
  import {
22
31
  expressionText,
@@ -24,17 +33,23 @@ import {
24
33
  type StorageScope,
25
34
  storageName,
26
35
  } from "./expressions";
27
- import { type RpcChannel, rpcSlots } from "./http";
36
+ import { rpcSlots, unsignedWidening } from "./http";
28
37
  import {
29
38
  isStoragePath,
30
39
  resolveStoragePath,
31
40
  type StorageBinding,
32
41
  } from "./io-paths";
33
- import { ioAddress } from "./manifest";
42
+ import { dataDebugType, ioAddress } from "./manifest";
34
43
  import { Names } from "./names";
35
- import type { CodesysPlan } from "./plan";
36
- import { type CodesysSymbols, type EmissionContext, required } from "./symbols";
37
- import { telemetryClient, telemetryObjects } from "./telemetry";
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";
38
53
  import { stStringLiteral } from "./types";
39
54
 
40
55
  /**
@@ -43,53 +58,26 @@ import { stStringLiteral } from "./types";
43
58
  */
44
59
  export const maxWriterPaths = 256;
45
60
 
46
- /** Module storage as the debugger's paths name it, for expanding `debug.writers.paths`. */
47
- const storageShape = (core: VerifiedCoreModule): PathManifest => ({
48
- roots: core.variables.flatMap((variable) => {
49
- const path =
50
- variable.storage === "module" ? storagePath(variable) : undefined;
51
- return path
52
- ? [{ path, target: "", type: variable.type, readable: true }]
53
- : [];
54
- }),
55
- io: [],
56
- constants: [],
57
- types: Object.fromEntries(
58
- core.types.flatMap((type): [string, DebugType][] => {
59
- switch (type.kind) {
60
- case "record":
61
- return [
62
- [
63
- type.id,
64
- {
65
- kind: "record",
66
- name: type.id,
67
- fields: type.fields.map((field) => ({
68
- name: field.name,
69
- target: field.name,
70
- type: field.type,
71
- })),
72
- },
73
- ],
74
- ];
75
- case "array":
76
- return [
77
- [
78
- type.id,
79
- {
80
- kind: "array",
81
- element: type.element,
82
- length: type.length,
83
- lower: type.lowerBound,
84
- },
85
- ],
86
- ];
87
- default:
88
- return [];
89
- }
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
+ : [];
90
70
  }),
91
- ),
92
- });
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
+ });
93
81
 
94
82
  /**
95
83
  * The module storage paths `debug.writers.paths` matches. A pattern that names a block's field or
@@ -98,7 +86,7 @@ const storageShape = (core: VerifiedCoreModule): PathManifest => ({
98
86
  export function writerPaths(
99
87
  core: VerifiedCoreModule,
100
88
  settings: ResolvedDebugConfig | undefined,
101
- report: (message: string) => void,
89
+ report: Report,
102
90
  ): StorageBinding[] {
103
91
  const writers = settings?.writers;
104
92
  if (typeof writers !== "object") {
@@ -106,12 +94,9 @@ export function writerPaths(
106
94
  }
107
95
  const expanded = (() => {
108
96
  try {
109
- return createPaths(storageShape(core)).expand(
110
- writers.paths,
111
- maxWriterPaths,
112
- );
97
+ return storagePaths(core).expand(writers.paths, maxWriterPaths);
113
98
  } catch (error) {
114
- if (error instanceof RangeError) {
99
+ if (error instanceof TooManyMatches) {
115
100
  report(`debug.writers.paths match more than ${maxWriterPaths} values.`);
116
101
  return [];
117
102
  }
@@ -130,27 +115,28 @@ export function writerPaths(
130
115
 
131
116
  /**
132
117
  * The debugger's and telemetry's names in the program: `GVL_TsplcDebug` and its fields, each
133
- * task's ring, and the instrumentation fields of each block.
118
+ * task's ring, and the instrumentation fields of each block, by block id.
134
119
  */
135
- export type Instrumentation = ReturnType<typeof planInstrumentation>;
120
+ export type Instrumentation = ReturnType<typeof planInstrumentation> & {
121
+ fields: ReadonlyMap<string, BlockInstrumentation>;
122
+ };
136
123
 
137
- export function planInstrumentation(
138
- plan: CodesysPlan,
139
- symbols: CodesysSymbols,
140
- ) {
124
+ export function planInstrumentation(plan: CodesysPlan, symbols: GlobalSymbols) {
141
125
  const { core, options } = plan;
142
126
  const settings = plan.project.debug;
143
127
  // A simulation build binds no fieldbus I/O, but its outputs are still the configured ones.
144
128
  const hardware = plan.project.hardware;
145
- const outputs = [
129
+ const outputs: Output[] = [
146
130
  ...core.variables
147
131
  .filter((variable) => variable.storage === "output")
148
132
  .map((root) => ({ key: root.name, root, segments: [] })),
149
133
  ...(hardware
150
134
  ? Object.entries(hardware.config.io).flatMap(([key, binding]) => {
135
+ const address = ioAddress(hardware, binding);
151
136
  if (
152
137
  !isStoragePath(key) ||
153
- !ioAddress(hardware, binding)?.startsWith("%Q")
138
+ address === undefined ||
139
+ !isOutputAddress(address)
154
140
  ) {
155
141
  return [];
156
142
  }
@@ -158,21 +144,19 @@ export function planInstrumentation(
158
144
  return "error" in resolved ? [] : [resolved];
159
145
  })
160
146
  : plan.storageBindings.filter(({ key }) =>
161
- required(options.io[key]).address.startsWith("%Q"),
162
- )
163
- ).map(({ key, root, segments }) => ({ key, root, segments })),
147
+ isOutputAddress(required(options.io[key]).address),
148
+ )),
164
149
  ];
165
- for (const { key, root, segments } of plan.writerPaths) {
166
- if (!outputs.some((output) => output.key === key)) {
167
- outputs.push({ key, root, segments });
150
+ for (const binding of plan.writerPaths) {
151
+ if (!outputs.some((output) => output.key === binding.key)) {
152
+ outputs.push(binding);
168
153
  }
169
154
  }
170
155
  const analysis = new DebugAnalysis(core, plan.deployment, settings, outputs);
171
- /** `GVL_TsplcDebug`, and the names of its fields. */
172
156
  const global = symbols.names.allocateObject("GVL", "TsplcDebug");
173
157
  const names = new Names();
174
- names.allocate("buildId");
175
158
  const slots = {
159
+ buildId: names.allocate("buildId"),
176
160
  decisions: names.allocate("decisions"),
177
161
  writers: names.allocate("writers"),
178
162
  };
@@ -198,25 +182,36 @@ export function planInstrumentation(
198
182
  record: symbols.names.allocateObject("ST", "TsplcRecord"),
199
183
  client: symbols.names.allocateObject(
200
184
  "FB",
201
- plan.http[telemetryClient]?.transport === "opcua"
185
+ telemetry?.endpoint.transport === "opcua"
202
186
  ? "TsplcTelemetryOpcUa"
203
187
  : "TsplcTelemetryHttp",
204
188
  ),
205
189
  }
206
190
  : undefined;
207
- /** Job instances in manifest order, and the job id each single-instance declaration passes to its service calls. */
208
- const jobs: { block: CoreFunctionBlock; path: InstancePath }[] = core.blocks
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
209
196
  .filter((block) => block.job)
210
197
  .flatMap((block) =>
211
- analysis.instances.get(block.id)!.map((path) => ({ block, path })),
212
- );
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
+ }));
213
208
  const jobIds = new Map<string, number>();
214
- jobs.forEach(({ path }, index) => {
215
- const declared = path.at(-1)!.variable;
209
+ for (const { path, id } of jobs) {
210
+ const declared = required(path.at(-1)).variable;
216
211
  if (analysis.owningPath(declared) === path) {
217
- jobIds.set(declared.id, index + 1);
212
+ jobIds.set(declared.id, id);
218
213
  }
219
- });
214
+ }
220
215
  /** Raw bytes over each assembly, in `GVL_TsplcDebug`, by device. */
221
216
  const devices = new Map<
222
217
  string,
@@ -250,12 +245,7 @@ export function planInstrumentation(
250
245
  ? plan.deployment.tasks.flatMap((task) =>
251
246
  rings.has(task.name)
252
247
  ? []
253
- : [
254
- [
255
- task.name,
256
- `${global}.${names.allocate(`scanClock_${task.name}`)}`,
257
- ],
258
- ],
248
+ : [[task.name, names.allocate(`scanClock_${task.name}`)]],
259
249
  )
260
250
  : [],
261
251
  );
@@ -264,18 +254,17 @@ export function planInstrumentation(
264
254
  global,
265
255
  slots,
266
256
  rings,
267
- /** Each task's clock in `GVL_TsplcDebug`, by task, when it has no ring that reads the clock. */
257
+ /** Each task's clock field in `GVL_TsplcDebug`, by task, when it has no ring that reads the clock. */
268
258
  scanClocks,
269
259
  telemetryNames,
270
260
  jobs,
271
261
  jobIds,
272
262
  devices,
273
- fields: new Map<string, BlockInstrumentation>(),
274
263
  };
275
264
  }
276
265
 
277
266
  /** Instrumentation fields of a block: its job state and the requesting job's id. */
278
- interface BlockInstrumentation {
267
+ export interface BlockInstrumentation {
279
268
  busy?: string;
280
269
  /** The method that records the job starting and finishing, allocated with `busy`. */
281
270
  track?: string;
@@ -285,47 +274,54 @@ interface BlockInstrumentation {
285
274
  sequence?: string;
286
275
  }
287
276
 
288
- /** Stands for the build id in the sender until the id, a hash over everything else, is known. */
289
- const buildPlaceholder = "<build>";
290
-
291
277
  /** The telemetry sender and its client; over OPC UA also the client's slots and their channels. */
292
278
  export function emitTelemetry(
293
279
  plan: CodesysPlan,
294
- symbols: CodesysSymbols,
280
+ symbols: GlobalSymbols,
295
281
  instrumentation: Instrumentation,
296
282
  networkName: string | undefined,
297
- ): { objects: GeneratedObject[]; rpcChannels: RpcChannel[] } {
283
+ ): { objects: GeneratedObject[]; rpcChannels: ServiceChannel[] } {
298
284
  const { telemetry } = plan;
299
285
  const names = instrumentation.telemetryNames;
300
286
  if (!telemetry || !names) {
301
287
  return { objects: [], rpcChannels: [] };
302
288
  }
303
- const endpoint = required(plan.http[telemetryClient]);
304
- const slots =
305
- endpoint.transport === "opcua"
306
- ? rpcSlots(symbols, telemetryClient, names.client, endpoint.path)
307
- : undefined;
308
- const objects = telemetryObjects({
309
- plan: telemetry,
310
- names,
311
- target: symbols.targetTypes,
312
- transport: slots
313
- ? {
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({
314
309
  kind: "opcua",
315
310
  timeoutMs: endpoint.timeoutMs,
316
311
  rpcGlobal: slots.list.name,
317
- }
318
- : {
319
- kind: "http",
320
- endpoint,
321
- network: `${required(networkName)}.network`,
322
- },
323
- build: buildPlaceholder,
324
- types: symbols.typeMap,
325
- });
326
- return slots
327
- ? { objects: [slots.list, ...objects], rpcChannels: slots.channels }
328
- : { objects, rpcChannels: [] };
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
+ };
329
325
  }
330
326
 
331
327
  /**
@@ -345,19 +341,15 @@ export function emitDebugGlobal(
345
341
  );
346
342
  }
347
343
  hash.update(JSON.stringify(manifest));
348
- const id = hash.digest("hex").slice(0, 16);
344
+ const id = hash.digest("hex").slice(0, buildIdLength);
349
345
 
350
- // A batch names its build; the id is known only now, so the sender's literal is filled in last.
351
- const send = objects.find(
352
- (object) => object.id === "tsplc:telemetryWriter:Send",
353
- );
354
- if (send?.implementation !== undefined) {
355
- send.implementation = send.implementation.replace(buildPlaceholder, id);
356
- }
346
+ fillBuildId(objects, id);
357
347
 
358
- const fields = [`buildId : STRING(16) := '${id}'`];
359
- for (const target of instrumentation.scanClocks.values()) {
360
- fields.push(`${target.slice(instrumentation.global.length + 1)} : DINT`);
348
+ const fields = [
349
+ `${instrumentation.slots.buildId} : STRING(${buildIdLength}) := '${id}'`,
350
+ ];
351
+ for (const field of instrumentation.scanClocks.values()) {
352
+ fields.push(`${field} : ${scanClockType}`);
361
353
  }
362
354
  for (const [task, field] of instrumentation.rings) {
363
355
  fields.push(
@@ -373,7 +365,7 @@ export function emitDebugGlobal(
373
365
  }
374
366
  if (settings.writers && instrumentation.analysis.outputs.length) {
375
367
  fields.push(
376
- `${instrumentation.slots.writers} : ARRAY[0..${instrumentation.analysis.outputs.length - 1}] OF UINT`,
368
+ `${instrumentation.slots.writers} : ARRAY[0..${instrumentation.analysis.outputs.length - 1}] OF ${writerSlotType}`,
377
369
  );
378
370
  }
379
371
  for (const raw of [...instrumentation.devices.values()].flatMap((device) =>
@@ -433,7 +425,7 @@ const traceOperand = (storage: StorageScope, value: CoreExpression): string => {
433
425
  if (signed) {
434
426
  return `TO_DINT(LIMIT(LINT#${dintMinimum}, ${text}, LINT#${dintMaximum}))`;
435
427
  }
436
- const wide = type.bits === 32 ? "UDINT" : "ULINT";
428
+ const wide = unsignedWidening(type.bits);
437
429
  return `TO_DINT(MIN(TO_${wide}(${text}), ${wide}#${dintMaximum}))`;
438
430
  };
439
431
 
@@ -442,7 +434,7 @@ const traceCall = (
442
434
  { context, storage, task }: RoutineProbes,
443
435
  site: number,
444
436
  value: string,
445
- operands: readonly Operand[] = [],
437
+ operands: readonly Operand[],
446
438
  ): string => {
447
439
  const captured = Array.from(
448
440
  { length: required(context.plan.telemetry?.traceOperands) },
@@ -503,7 +495,7 @@ export function writeProbes(
503
495
  export const jobField = (
504
496
  storage: StorageScope,
505
497
  job: CoreFunctionBlock,
506
- name: string,
498
+ name: keyof typeof jobStateFields,
507
499
  ) =>
508
500
  storageName(
509
501
  storage,
@@ -541,6 +533,7 @@ export function jobTracking(
541
533
  probes,
542
534
  sites.start,
543
535
  `TO_DINT(${jobField(probes.storage, job, "step")})`,
536
+ [],
544
537
  ),
545
538
  1,
546
539
  );
@@ -552,13 +545,14 @@ export function jobTracking(
552
545
  line(`IF NOT ${busy} THEN`, 0);
553
546
  if (sites) {
554
547
  line(`IF ${error} <> 0 THEN`, 1);
555
- line(traceCall(probes, sites.completion, `-TO_DINT(${error})`), 2);
548
+ line(traceCall(probes, sites.completion, `-TO_DINT(${error})`, []), 2);
556
549
  line("ELSE", 1);
557
550
  line(
558
551
  traceCall(
559
552
  probes,
560
553
  sites.completion,
561
554
  `TO_DINT(${required(fields.step)})`,
555
+ [],
562
556
  ),
563
557
  2,
564
558
  );
@@ -566,7 +560,7 @@ export function jobTracking(
566
560
  }
567
561
  // Cancellation is asked for, so it isn't a failure.
568
562
  if (failure) {
569
- line(`IF ${error} <> 0 AND ${error} <> 1 THEN`, 1);
563
+ line(`IF ${error} <> 0 AND ${error} <> ${jobError.cancelled} THEN`, 1);
570
564
  line(
571
565
  `${ringTarget(instrumentation, failure.task)}.Failure${failure.id}(code := ${error}, step := ${required(fields.step)}${failure.instance === null ? "" : `, instance := ${stStringLiteral(failure.instance)}`});`,
572
566
  2,
@@ -1,7 +1,13 @@
1
- // biome-ignore-all lint/style/noNonNullAssertion: Retain existing non-null assertions during the tooling migration; changing their contracts requires a separate review.
2
- import type { PlcType, Variable, VerifiedCoreModule } from "@tsplc/compiler/ir";
1
+ import {
2
+ coreTables,
3
+ isLogicalIo,
4
+ type PlcType,
5
+ type Variable,
6
+ type VerifiedCoreModule,
7
+ } from "@tsplc/compiler/ir";
8
+ import { arrayTypeText } from "@tsplc/iec";
3
9
  import { type IoTarget, ioLayout } from "@tsplc/typegen/hardware";
4
- import { moduleName } from "./storage";
10
+ import { moduleName, storagePath } from "./storage";
5
11
 
6
12
  type StorageSegment =
7
13
  | { kind: "field"; record: string; name: string }
@@ -32,7 +38,7 @@ export function resolveStoragePath(
32
38
  error: `${key}: expected Module.variable followed by .field or [index].`,
33
39
  };
34
40
  }
35
- const [, module, name, rest] = match;
41
+ const [, module, name, rest = ""] = match;
36
42
  const roots = core.variables.filter(
37
43
  (item) =>
38
44
  item.storage === "module" &&
@@ -47,10 +53,10 @@ export function resolveStoragePath(
47
53
  : `${key}: no mutable module variable ${module}.${name}.`,
48
54
  };
49
55
  }
50
- const types = new Map(core.types.map((type) => [type.id, type]));
51
- let type = types.get(root.type)!;
56
+ const { typeOf } = coreTables(core);
57
+ let type = typeOf(root.type);
52
58
  const segments: StorageSegment[] = [];
53
- for (const [, field, index] of rest!.matchAll(
59
+ for (const [, field, index] of rest.matchAll(
54
60
  /\.([A-Za-z_$][\w$]*)|\[(-?\d+)\]/g,
55
61
  )) {
56
62
  if (field !== undefined) {
@@ -62,7 +68,7 @@ export function resolveStoragePath(
62
68
  return { error: `${key}: no field ${field}.` };
63
69
  }
64
70
  segments.push({ kind: "field", record: type.id, name: field });
65
- type = types.get(member.type)!;
71
+ type = typeOf(member.type);
66
72
  } else {
67
73
  const value = Number(index);
68
74
  if (type.kind !== "array") {
@@ -74,7 +80,7 @@ export function resolveStoragePath(
74
80
  };
75
81
  }
76
82
  segments.push({ kind: "index", value });
77
- type = types.get(type.element)!;
83
+ type = typeOf(type.element);
78
84
  }
79
85
  }
80
86
  return { key, root, segments, type };
@@ -98,19 +104,24 @@ export function iecSpelling(
98
104
  if (type.kind !== "array") {
99
105
  return undefined;
100
106
  }
101
- const element = iecSpelling(types.get(type.element)!, types);
102
- const upper = type.lowerBound + type.length - 1;
103
- return element && !element.startsWith("ARRAY")
104
- ? `ARRAY[${type.lowerBound}..${upper}] OF ${element}`
107
+ const element = types.get(type.element);
108
+ const spelling = element && iecSpelling(element, types);
109
+ return spelling && element.kind !== "array"
110
+ ? arrayTypeText({
111
+ lower: type.lowerBound,
112
+ upper: type.lowerBound + type.length - 1,
113
+ element: spelling,
114
+ })
105
115
  : undefined;
106
116
  }
107
117
 
108
118
  /** Every key an `io` mapping may use: logical IO names and paths into mutable module storage. */
109
119
  export function ioTargets(core: VerifiedCoreModule): IoTarget[] {
110
- const types = new Map(core.types.map((type) => [type.id, type]));
120
+ const { types, typeOf } = coreTables(core);
111
121
  const targets: IoTarget[] = [];
112
122
  const add = (key: string, type: PlcType) => {
113
- const layout = ioLayout(iecSpelling(type, types) ?? "");
123
+ const spelling = iecSpelling(type, types);
124
+ const layout = spelling && ioLayout(spelling);
114
125
  if (layout) {
115
126
  targets.push({ key, layout });
116
127
  }
@@ -119,22 +130,19 @@ export function ioTargets(core: VerifiedCoreModule): IoTarget[] {
119
130
  add(key, type);
120
131
  if (type.kind === "record") {
121
132
  for (const field of type.fields) {
122
- visit(`${key}.${field.name}`, types.get(field.type)!);
133
+ visit(`${key}.${field.name}`, typeOf(field.type));
123
134
  }
124
135
  }
125
136
  // Typegen writes keys as template literal types, so any index matches here.
126
137
  if (type.kind === "array") {
127
- visit(`${key}[\${number}]`, types.get(type.element)!);
138
+ visit(`${key}[\${number}]`, typeOf(type.element));
128
139
  }
129
140
  };
130
141
  for (const variable of core.variables) {
131
- if (variable.storage === "input" || variable.storage === "output") {
132
- add(variable.name, types.get(variable.type)!);
142
+ if (isLogicalIo(variable)) {
143
+ add(variable.name, typeOf(variable.type));
133
144
  } else if (variable.storage === "module") {
134
- visit(
135
- `${moduleName(variable)}.${variable.name}`,
136
- types.get(variable.type)!,
137
- );
145
+ visit(storagePath(variable), typeOf(variable.type));
138
146
  }
139
147
  }
140
148
  return targets;