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