@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,20 +1,27 @@
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
- CoreExpression,
4
- CoreFunction,
5
- CoreFunctionBlock,
6
- CorePath,
7
- CoreStatement,
8
- Deployment,
9
- PlcType,
10
- SourceSpan,
11
- Variable,
12
- VerifiedCoreModule,
1
+ import {
2
+ type CoreExpression,
3
+ type CoreFunction,
4
+ type CoreFunctionBlock,
5
+ type CorePath,
6
+ type CoreStatement,
7
+ type CoreTables,
8
+ constantIndex,
9
+ coreTables,
10
+ type Deployment,
11
+ directWrites,
12
+ instancedBlock,
13
+ isBlockRoutine,
14
+ isTemporary,
15
+ nestedBodies,
16
+ type SourceSpan,
17
+ type Variable,
18
+ type VerifiedCoreModule,
13
19
  } from "@tsplc/compiler/ir";
14
- import { directWrites, instancedBlock, nestedBodies } from "@tsplc/compiler/ir";
15
20
  import type { ResolvedDebugConfig } from "@tsplc/config";
16
- import { moduleName } from "./storage";
17
- import { routineTasks, variablesById } from "./task-access";
21
+ import { neverWrittenSlot } from "@tsplc/debug/manifest";
22
+ import { required } from "./required";
23
+ import { storagePath } from "./storage";
24
+ import { routineTasks } from "./task-access";
18
25
 
19
26
  type Routine = CoreFunction | CoreFunctionBlock;
20
27
 
@@ -66,7 +73,7 @@ interface WriterSite {
66
73
  }
67
74
 
68
75
  /** An output the program drives: a logical output, or a storage path bound to a %Q address. */
69
- interface Output {
76
+ export interface Output {
70
77
  key: string;
71
78
  root: Variable;
72
79
  segments: readonly (
@@ -80,12 +87,8 @@ export type InstancePath = readonly { variable: Variable; index?: number }[];
80
87
 
81
88
  type Load = CoreExpression & { kind: "load" };
82
89
 
83
- const temporary = (id: string) => id.includes(":temporary:");
84
-
85
- const constantIndex = (value: CoreExpression): number | undefined =>
86
- value.kind === "constant" && value.value.kind === "integer"
87
- ? Number(value.value.value)
88
- : undefined;
90
+ /** A call in a loop may run any number of times a scan: counted as two, it is never the only call. */
91
+ const callsInLoop = 2;
89
92
 
90
93
  /** The place a statement writes directly: an assignment's, or a call's stored result, written as directly. */
91
94
  const writtenPlace = (
@@ -111,8 +114,7 @@ const wholeTarget = (item: CoreStatement): string | undefined => {
111
114
  export function blockInstances(
112
115
  core: VerifiedCoreModule,
113
116
  ): Map<string, InstancePath[]> {
114
- const types = new Map(core.types.map((type) => [type.id, type]));
115
- const blocks = new Map(core.blocks.map((block) => [block.id, block]));
117
+ const { types, blockOf } = coreTables(core);
116
118
  const instances = new Map<string, InstancePath[]>(
117
119
  core.blocks.map((block) => [block.id, []]),
118
120
  );
@@ -130,8 +132,8 @@ export function blockInstances(
130
132
  ])
131
133
  : [[...path, { variable }]];
132
134
  for (const instance of declared) {
133
- instances.get(block)!.push(instance);
134
- for (const child of blocks.get(block)!.state) {
135
+ required(instances.get(block)).push(instance);
136
+ for (const child of blockOf(block).state) {
135
137
  const instanced = instancedBlock(child.type, types);
136
138
  if (instanced) {
137
139
  visit(child, instanced.block, instance);
@@ -149,7 +151,7 @@ export function blockInstances(
149
151
  }
150
152
 
151
153
  /** The TypeScript path of a module storage place, when every index is constant. */
152
- export function storagePath(
154
+ export function typescriptPath(
153
155
  root: Variable,
154
156
  path: CorePath = [],
155
157
  ): string | undefined {
@@ -157,21 +159,33 @@ export function storagePath(
157
159
  if (!logical && root.storage !== "module" && root.storage !== "instance") {
158
160
  return undefined;
159
161
  }
160
- let text = logical ? root.name : `${moduleName(root)}.${root.name}`;
162
+ const segments: string[] = [];
161
163
  for (const segment of path) {
162
164
  if (segment.kind === "field") {
163
- text += `.${segment.name}`;
165
+ segments.push(`.${segment.name}`);
164
166
  continue;
165
167
  }
166
168
  const index = constantIndex(segment.value);
167
169
  if (index === undefined) {
168
170
  return undefined;
169
171
  }
170
- text += `[${index}]`;
172
+ segments.push(`[${index}]`);
171
173
  }
172
- return text;
174
+ return `${logical ? root.name : storagePath(root)}${segments.join("")}`;
173
175
  }
174
176
 
177
+ /** The TypeScript path of a block instance. */
178
+ export const instanceText = (path: InstancePath): string =>
179
+ path
180
+ .map(({ variable, index }, position) => {
181
+ const name =
182
+ position === 0
183
+ ? (typescriptPath(variable) ?? variable.name)
184
+ : `.${variable.name}`;
185
+ return index === undefined ? name : `${name}[${index}]`;
186
+ })
187
+ .join("");
188
+
175
189
  /**
176
190
  * Where the debugger looks: which task runs each routine, every block instance, and the
177
191
  * probes each instrumented statement gets. Probes follow `debug`; with no settings there are none.
@@ -196,16 +210,14 @@ export class DebugAnalysis {
196
210
  private readonly callSites = new Map<string, number>();
197
211
  /** Blocks that contain a service call, so they carry the requesting job's id down to it. */
198
212
  readonly carriers = new Set<string>();
199
- readonly variables;
200
- private readonly types;
201
- private readonly routines;
202
- private readonly blocks;
213
+ private readonly tables: CoreTables;
203
214
  private readonly routineWrites = new Map<string, ReadonlySet<string>>();
204
215
  private readonly temporaries = new Map<
205
216
  string,
206
217
  ReadonlyMap<string, readonly CoreStatement[]>
207
218
  >();
208
- private writerSites = 0;
219
+ /** The last writer site id given out; the first follows the id of a slot never written. */
220
+ private writerSites = neverWrittenSlot;
209
221
 
210
222
  constructor(
211
223
  private readonly core: VerifiedCoreModule,
@@ -213,15 +225,7 @@ export class DebugAnalysis {
213
225
  settings: ResolvedDebugConfig | undefined,
214
226
  readonly outputs: readonly Output[],
215
227
  ) {
216
- this.types = new Map(core.types.map((type) => [type.id, type]));
217
- this.blocks = new Map(core.blocks.map((block) => [block.id, block]));
218
- this.routines = new Map<string, Routine>(
219
- [...core.functions, ...core.blocks].map((routine) => [
220
- routine.id,
221
- routine,
222
- ]),
223
- );
224
- this.variables = variablesById(core);
228
+ this.tables = coreTables(core);
225
229
  this.writers = outputs.map(() => []);
226
230
  this.tasks = routineTasks(core, deployment);
227
231
  this.instances = blockInstances(core);
@@ -266,15 +270,15 @@ export class DebugAnalysis {
266
270
 
267
271
  /** The instance path of a job declaration, when it declares exactly one instance. */
268
272
  owningPath(variable: Variable): InstancePath | undefined {
269
- const type = this.types.get(variable.type);
273
+ const type = this.tables.types.get(variable.type);
270
274
  const block =
271
- type?.kind === "block" ? this.blocks.get(type.block) : undefined;
275
+ type?.kind === "block" ? this.tables.blockOf(type.block) : undefined;
272
276
  if (!block?.job) {
273
277
  return undefined;
274
278
  }
275
- const paths = this.instances
276
- .get(block.id)!
277
- .filter((path) => path.at(-1)!.variable.id === variable.id);
279
+ const paths = required(this.instances.get(block.id)).filter(
280
+ (path) => path.at(-1)?.variable.id === variable.id,
281
+ );
278
282
  return paths.length === 1 ? paths[0] : undefined;
279
283
  }
280
284
 
@@ -288,12 +292,12 @@ export class DebugAnalysis {
288
292
 
289
293
  private findCarriers(): void {
290
294
  const carries = (id: string): boolean => {
291
- const block = this.blocks.get(id)!;
295
+ const block = this.tables.blockOf(id);
292
296
  if (block.runtime?.kind === "http") {
293
297
  return true;
294
298
  }
295
299
  return block.state.some((child) => {
296
- const type = instancedBlock(child.type, this.types);
300
+ const type = instancedBlock(child.type, this.tables.types);
297
301
  return type !== undefined && carries(type.block);
298
302
  });
299
303
  };
@@ -304,22 +308,6 @@ export class DebugAnalysis {
304
308
  }
305
309
  }
306
310
 
307
- private placeType(root: Variable, path: CorePath = []): PlcType | undefined {
308
- let type = this.types.get(root.type);
309
- for (const segment of path) {
310
- if (segment.kind === "field" && type?.kind === "record") {
311
- type = this.types.get(
312
- type.fields.find((field) => field.name === segment.name)!.type,
313
- );
314
- } else if (segment.kind === "index" && type?.kind === "array") {
315
- type = this.types.get(type.element);
316
- } else {
317
- return undefined;
318
- }
319
- }
320
- return type;
321
- }
322
-
323
311
  private countCalls(items: readonly CoreStatement[], weight: number): void {
324
312
  for (const item of items) {
325
313
  if (item.kind === "invoke") {
@@ -329,22 +317,20 @@ export class DebugAnalysis {
329
317
  );
330
318
  }
331
319
  for (const body of nestedBodies(item)) {
332
- this.countCalls(body, item.kind === "for" ? 2 : weight);
320
+ this.countCalls(body, item.kind === "for" ? callsInLoop : weight);
333
321
  }
334
322
  }
335
323
  }
336
324
 
337
325
  /** Storage a routine can assign, including through every routine it calls. */
338
- private writes(id: string): ReadonlySet<string> {
339
- const cached = this.routineWrites.get(id);
326
+ private writes(routine: Routine): ReadonlySet<string> {
327
+ const cached = this.routineWrites.get(routine.id);
340
328
  if (cached) {
341
329
  return cached;
342
330
  }
343
331
  const result = new Set<string>();
344
- this.routineWrites.set(id, result);
345
- for (const root of this.statementWrites(
346
- this.routines.get(id)?.body ?? [],
347
- )) {
332
+ this.routineWrites.set(routine.id, result);
333
+ for (const root of this.statementWrites(routine.body)) {
348
334
  result.add(root);
349
335
  }
350
336
  return result;
@@ -356,22 +342,12 @@ export class DebugAnalysis {
356
342
  for (const root of directWrites(item)) {
357
343
  result.add(root);
358
344
  }
359
- if (item.kind === "invoke") {
360
- for (const root of this.writes(item.function)) {
345
+ const called = this.tables.calledRoutineOf(item);
346
+ if (called) {
347
+ for (const root of this.writes(called)) {
361
348
  result.add(root);
362
349
  }
363
350
  }
364
- if (item.kind === "blockUpdate") {
365
- const block = instancedBlock(
366
- this.variables.get(item.instance)!.type,
367
- this.types,
368
- );
369
- if (block) {
370
- for (const root of this.writes(block.block)) {
371
- result.add(root);
372
- }
373
- }
374
- }
375
351
  for (const body of nestedBodies(item)) {
376
352
  for (const root of this.statementWrites(body)) {
377
353
  result.add(root);
@@ -424,29 +400,33 @@ export class DebugAnalysis {
424
400
  const task = this.tasks.get(routine.id);
425
401
  const probes: Probe[] = [];
426
402
  const written = writtenPlace(item);
427
- if (written && settings.writers) {
428
- let site: number | undefined;
429
- this.outputs.forEach((output, index) => {
430
- const when = this.reaches(written.root, written.path, output);
431
- if (!when) {
432
- return;
433
- }
434
- site ??= ++this.writerSites;
435
- this.writers[index]!.push({
403
+ const reached =
404
+ written && settings.writers
405
+ ? this.outputs.flatMap((output, index) => {
406
+ const when = this.reaches(written.root, written.path, output);
407
+ return when ? [{ index, when }] : [];
408
+ })
409
+ : [];
410
+ if (reached.length) {
411
+ const site = ++this.writerSites;
412
+ for (const { index, when } of reached) {
413
+ required(this.writers[index]).push({
436
414
  id: site,
437
415
  routine: routine.id,
438
416
  span: item.span,
439
417
  statement: item,
440
418
  });
441
419
  probes.push({ kind: "writer", site, output: index, when });
442
- });
420
+ }
443
421
  }
444
422
  if (settings.trace && task) {
445
423
  if (item.kind === "assign") {
446
- const root = this.variables.get(item.root)!;
447
- const type = this.placeType(root, item.path);
424
+ const root = this.tables.variableOf(item.root);
425
+ const type = this.tables.placeType(root.type, item.path ?? []);
448
426
  const path =
449
- root.storage === "module" ? storagePath(root, item.path) : undefined;
427
+ root.storage === "module"
428
+ ? typescriptPath(root, item.path)
429
+ : undefined;
450
430
  if (type?.kind === "enum" && path) {
451
431
  const operands = this.operands(
452
432
  routine,
@@ -469,11 +449,11 @@ export class DebugAnalysis {
469
449
  if (
470
450
  item.kind === "return" &&
471
451
  item.value &&
472
- "parameters" in routine &&
452
+ !isBlockRoutine(routine) &&
473
453
  routine.result &&
474
454
  this.callSites.get(routine.id) === 1
475
455
  ) {
476
- const type = this.types.get(routine.result);
456
+ const type = this.tables.types.get(routine.result);
477
457
  if (type?.kind === "enum") {
478
458
  const slot = this.decisions.get(routine.id) ?? this.decisions.size;
479
459
  this.decisions.set(routine.id, slot);
@@ -540,6 +520,10 @@ export class DebugAnalysis {
540
520
  return when;
541
521
  }
542
522
 
523
+ private isTemporary(routine: Routine, root: string): boolean {
524
+ return isTemporary(routine.id, this.tables.variableOf(root));
525
+ }
526
+
543
527
  /** The statements that assign each temporary of a routine. */
544
528
  private definitions(
545
529
  routine: Routine,
@@ -552,7 +536,7 @@ export class DebugAnalysis {
552
536
  const define = (items: readonly CoreStatement[]): void => {
553
537
  for (const item of items) {
554
538
  const target = wholeTarget(item);
555
- if (target && temporary(target)) {
539
+ if (target && this.isTemporary(routine, target)) {
556
540
  definitions.set(target, [...(definitions.get(target) ?? []), item]);
557
541
  }
558
542
  for (const body of nestedBodies(item)) {
@@ -584,7 +568,7 @@ export class DebugAnalysis {
584
568
  capture: CoreExpression,
585
569
  read?: Load,
586
570
  ): void => {
587
- const kind = this.types.get(capture.type)?.kind;
571
+ const kind = this.tables.types.get(capture.type)?.kind;
588
572
  const key = `${span.file}:${span.start}:${span.end}`;
589
573
  if (
590
574
  seen.has(key) ||
@@ -594,7 +578,7 @@ export class DebugAnalysis {
594
578
  }
595
579
  seen.add(key);
596
580
  const path =
597
- read && storagePath(this.variables.get(read.root)!, read.path);
581
+ read && typescriptPath(this.tables.variableOf(read.root), read.path);
598
582
  result.push({
599
583
  capture,
600
584
  span,
@@ -620,7 +604,7 @@ export class DebugAnalysis {
620
604
  if (value.kind !== "load") {
621
605
  return;
622
606
  }
623
- if (!temporary(value.root)) {
607
+ if (!this.isTemporary(routine, value.root)) {
624
608
  if (evaluated || direct(value)) {
625
609
  add(value.span, value, value);
626
610
  }
@@ -647,7 +631,7 @@ export class DebugAnalysis {
647
631
  return;
648
632
  }
649
633
  const source = item.value;
650
- if (source.kind === "load" && !temporary(source.root)) {
634
+ if (source.kind === "load" && !this.isTemporary(routine, source.root)) {
651
635
  const read = {
652
636
  ...source,
653
637
  type: value.type,
@@ -4,38 +4,33 @@ import type {
4
4
  GeneratedObject,
5
5
  SourceSpan,
6
6
  } from "@tsplc/compiler/ir";
7
+ import { isBlockRoutine } from "@tsplc/compiler/ir";
7
8
  import type { Publication } from "@tsplc/config";
8
9
  import type { DebugManifest } from "@tsplc/debug/manifest";
10
+ import type { ServiceChannel } from "@tsplc/server/wire";
9
11
  import { explainedWriters, explanations } from "./explain";
10
- import {
11
- emitGlobalLists,
12
- emitIoList,
13
- type IoMapping,
14
- ioTarget,
15
- } from "./global-lists";
16
- import {
17
- emitServiceBlock,
18
- httpNetwork,
19
- type RpcChannel,
20
- serviceBlock,
21
- } from "./http";
12
+ import { emitGlobalLists, emitIoList, type IoMapping } from "./global-lists";
13
+ import { emitServiceBlock, httpNetwork, serviceBlock } from "./http";
22
14
  import {
23
15
  emitDebugGlobal,
24
16
  emitTelemetry,
25
17
  planInstrumentation,
26
18
  } from "./instrumentation";
27
19
  import { debugManifest } from "./manifest";
28
- import { type CodesysPlan, diagnostic, isCodesysPlan } from "./plan";
20
+ import { type CodesysPlan, isPlanned } from "./plan";
29
21
  import { emitScanProgram, emitTaskWrappers } from "./programs";
30
- import { machineInterfaces, publishedSymbols } from "./publication";
22
+ import { publishedSymbols } from "./publication";
31
23
  import { emitRoutine } from "./routines";
24
+ import { serviceChannelsFile } from "./service-channels";
32
25
  import { Sources } from "./sources";
33
- import { stFiles } from "./st-files";
26
+ import { type ObjectMapping, type PlacedObject, stFiles } from "./st-files";
34
27
  import {
35
- allocateBlockFields,
28
+ allocateBlockMembers,
36
29
  allocateFunctionNames,
37
30
  allocateRoutineScope,
38
- createSymbols,
31
+ type CodesysSymbols,
32
+ type EmissionContext,
33
+ globalSymbols,
39
34
  } from "./symbols";
40
35
 
41
36
  export interface SourceMapping {
@@ -44,126 +39,143 @@ export interface SourceMapping {
44
39
  span: SourceSpan;
45
40
  }
46
41
 
47
- type CodesysBundle = EmitResult & {
42
+ export type CodesysBundle = Omit<EmitResult, "objects"> & {
43
+ objects: PlacedObject[];
48
44
  /** Variables a generated device tree maps to their bit or byte channels. */
49
45
  ioMappings: readonly IoMapping[];
50
46
  mappings: readonly SourceMapping[];
51
47
  ioSymbols: Readonly<Record<string, string>>;
52
48
  publication: Publication;
53
49
  /** The debugger's view of this build; its id is also published as `build.target`. */
54
- debug?: DebugManifest;
50
+ debug: DebugManifest;
55
51
  /** `explain.json`'s text, whose digest `debug` records. */
56
- explain?: string;
52
+ explain: string;
57
53
  };
58
54
 
59
- // A bundle owns target names and source mappings. Per-routine declarations and
60
- // implementation lines stay local to emitRoutine, so they cannot leak between POUs.
61
- export function emitCodesys(plan: CodesysPlan): CodesysBundle {
62
- if (!isCodesysPlan(plan)) {
63
- return {
64
- artifacts: [],
65
- objects: [],
66
- mappings: [],
67
- ioSymbols: {},
68
- publication: { production: [], development: [] },
69
- ioMappings: [],
70
- diagnostics: [diagnostic("Emission requires a validated CODESYS plan.")],
71
- };
72
- }
55
+ /** The routines emitted as objects: every function, and every block CODESYS does not provide. */
56
+ const emittedRoutines = ({ core }: CodesysPlan) => [
57
+ ...core.functions,
58
+ ...core.blocks.filter((block) => !block.standard),
59
+ ];
60
+
61
+ /**
62
+ * Allocates every target name the emitters read, in the order that fixes them, with the objects
63
+ * emitted on the way: types, storage lists, the network, numeric helpers and telemetry. Each
64
+ * routine's scope reserves those objects' names, as an ST local must never shadow a helper,
65
+ * function, FB, or DUT that the implementation calls.
66
+ */
67
+ function emissionContext(plan: CodesysPlan) {
73
68
  const { core } = plan;
74
- const symbols = createSymbols(plan);
75
- const instrumentation = planInstrumentation(plan, symbols);
76
- const context = { plan, symbols, instrumentation };
77
- const globalLists = emitGlobalLists(plan, symbols);
78
- const ioList = emitIoList(plan, symbols);
79
- const network = httpNetwork(plan, symbols);
69
+ const global = globalSymbols(plan);
70
+ const planned = planInstrumentation(plan, global);
71
+ const globalLists = emitGlobalLists(plan, global);
72
+ const ioList = emitIoList(plan, global, globalLists.references);
73
+ const network = httpNetwork(plan, global);
80
74
  const objects: GeneratedObject[] = [
81
- ...symbols.targetTypes.objects,
75
+ ...global.targetTypes.objects,
82
76
  ...globalLists.objects,
83
77
  ...ioList.objects,
84
78
  ...(network ? [network] : []),
85
79
  ];
86
- const mappings: SourceMapping[] = [
87
- ...globalLists.mappings,
88
- ...ioList.mappings,
89
- ];
90
80
 
91
- // Helpers must be discovered before allocating local names: an ST local must
92
- // never shadow a helper, function, FB, or DUT that the implementation calls.
93
- allocateFunctionNames(symbols, plan);
94
- for (const fn of core.functions) {
95
- symbols.numeric.prepareStatements(fn.body);
96
- }
97
- for (const block of core.blocks) {
98
- symbols.numeric.prepareStatements(block.body);
81
+ const routineNames = allocateFunctionNames(plan, global);
82
+ for (const routine of [...core.functions, ...core.blocks]) {
83
+ global.numeric.prepareStatements(routine.body);
99
84
  }
100
- objects.push(...symbols.numeric.objects);
101
- allocateBlockFields(symbols, instrumentation, plan, objects);
102
-
103
- const telemetry = emitTelemetry(
85
+ objects.push(...global.numeric.objects);
86
+ const members = allocateBlockMembers(
104
87
  plan,
105
- symbols,
106
- instrumentation,
107
- network?.name,
88
+ { ...global, functionNames: routineNames },
89
+ planned,
90
+ objects,
108
91
  );
92
+ const functionNames = new Map([...routineNames, ...members.methods]);
93
+ const instrumentation = { ...planned, fields: members.instrumented };
94
+
95
+ const telemetry = emitTelemetry(plan, global, instrumentation, network?.name);
109
96
  objects.push(...telemetry.objects);
110
- const routines = [
111
- ...core.functions,
112
- ...core.blocks.filter((block) => !block.standard),
113
- ];
114
- for (const fn of routines) {
115
- allocateRoutineScope(symbols, instrumentation, objects, fn);
97
+ const scopes = new Map(
98
+ emittedRoutines(plan).map((fn) => [
99
+ fn.id,
100
+ allocateRoutineScope(
101
+ { ...global, functionNames, blockFields: members.fields },
102
+ objects,
103
+ instrumentation.global,
104
+ fn,
105
+ ),
106
+ ]),
107
+ );
108
+ const symbols: CodesysSymbols = {
109
+ ...global,
110
+ references: new Map([...globalLists.references, ...ioList.references]),
111
+ functionNames,
112
+ blockFields: members.fields,
113
+ mailboxLocks: members.mailboxLocks,
114
+ programResults: new Map(
115
+ [...scopes].flatMap(([id, { result }]) => (result ? [[id, result]] : [])),
116
+ ),
117
+ routineScopes: new Map([...scopes].map(([id, { scope }]) => [id, scope])),
118
+ };
119
+ const context: EmissionContext = { plan, symbols, instrumentation };
120
+ return {
121
+ context,
122
+ objects,
123
+ mappings: [...globalLists.mappings, ...ioList.mappings],
124
+ ioList,
125
+ network,
126
+ rpcChannels: telemetry.rpcChannels,
127
+ };
128
+ }
129
+
130
+ export function emitCodesys(plan: CodesysPlan): CodesysBundle {
131
+ if (!isPlanned(plan)) {
132
+ throw new Error("Emission requires a validated CODESYS plan.");
116
133
  }
117
- const rpcChannels: RpcChannel[] = [...telemetry.rpcChannels];
118
- for (const fn of routines) {
119
- if ("inputs" in fn && serviceBlock(fn)) {
134
+ const { context, objects, mappings, ioList, network, rpcChannels } =
135
+ emissionContext(plan);
136
+ const channels: ServiceChannel[] = [...rpcChannels];
137
+ const lines: ObjectMapping[] = [...mappings];
138
+ for (const fn of emittedRoutines(plan)) {
139
+ if (isBlockRoutine(fn) && serviceBlock(fn)) {
120
140
  const service = emitServiceBlock(context, network?.name, fn);
121
141
  objects.push(...service.objects);
122
- rpcChannels.push(...service.rpcChannels);
142
+ channels.push(...service.rpcChannels);
123
143
  continue;
124
144
  }
125
145
  const routine = emitRoutine(context, fn);
126
146
  objects.push(...routine.objects);
127
- mappings.push(...routine.mappings);
147
+ lines.push(...routine.mappings);
128
148
  }
129
- for (const program of core.programs) {
149
+ for (const program of plan.core.programs) {
130
150
  objects.push(emitScanProgram(context, ioList.channels, program));
131
151
  }
132
152
  objects.push(...emitTaskWrappers(context, objects));
133
153
  // Explanations index the manifest's sources and functions, and its digest of them feeds the build id.
134
154
  const sources = new Sources(plan.project.root);
135
- const functionIds = new Map<string, number>();
136
- const manifest = debugManifest(
137
- context,
138
- (key) => ioTarget(plan, symbols, key),
139
- sources,
140
- functionIds,
141
- );
155
+ const { manifest, functionIds } = debugManifest(context, sources);
142
156
  const explained = explanations(context, manifest, sources, functionIds);
143
157
  const explain = JSON.stringify(explained.file);
144
- const debug = emitDebugGlobal(plan, instrumentation, objects, {
158
+ const debug = emitDebugGlobal(plan, context.instrumentation, objects, {
145
159
  ...manifest,
146
160
  writers: explainedWriters(
147
161
  manifest,
148
162
  explained,
149
- instrumentation.analysis.writers,
163
+ context.instrumentation.analysis.writers,
150
164
  ),
151
165
  explain: { sha256: createHash("sha256").update(explain).digest("hex") },
152
166
  });
167
+ const placed = stFiles(context, objects, lines);
153
168
 
154
169
  return {
155
- artifacts: [
156
- ...stFiles(plan, symbols, objects, mappings),
157
- ...machineInterfaces(plan, symbols, rpcChannels),
158
- ],
159
- objects,
160
- mappings,
170
+ artifacts: [...placed.artifacts, serviceChannelsFile(channels)],
171
+ objects: placed.objects,
172
+ mappings: placed.mappings,
161
173
  ioSymbols: Object.fromEntries([
162
174
  ...ioList.channels.map((channel) => [channel.key, channel.target]),
163
175
  ...ioList.ioMappings.map((mapping) => [mapping.key, mapping.variable]),
164
176
  ]),
165
177
  ioMappings: ioList.ioMappings,
166
- publication: publishedSymbols(plan, symbols, rpcChannels),
178
+ publication: publishedSymbols(context, channels),
167
179
  debug,
168
180
  explain,
169
181
  diagnostics: [],