@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,13 +1,16 @@
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 {
3
2
  type Constant,
4
3
  type CoreExpression,
5
4
  type CoreStatement,
5
+ type CoreTables,
6
+ coreTables,
6
7
  type Deployment,
7
8
  type Diagnostic,
8
9
  foldConstantRoutines,
9
10
  forwardTemporaries,
11
+ freeze,
10
12
  instancedBlock,
13
+ isLogicalIo,
11
14
  isVerifiedCore,
12
15
  nestedBodies,
13
16
  type PlcType,
@@ -18,22 +21,23 @@ import {
18
21
  type VerifiedCoreModule,
19
22
  } from "@tsplc/compiler/ir";
20
23
  import type {
24
+ Environment,
21
25
  Publication,
22
26
  ResolvedDebugConfig,
23
27
  ResolvedHmiConfig,
24
28
  ResolvedTelemetryConfig,
25
- ResolvedTsplcConfig,
29
+ ServerConfig,
26
30
  } from "@tsplc/config";
27
- import { cyclicTasksSchema } from "@tsplc/config/build";
28
- import { ioScalarBits, sizePrefixes } from "@tsplc/iec";
29
- import { ioLayout } from "@tsplc/typegen/hardware";
30
- import { type CodesysOptions, optionsSchema } from "./config";
31
31
  import {
32
- type HttpEndpoint,
33
- resolveHttpEndpoint,
34
- serviceBlock,
35
- validateHttpLayout,
36
- } from "./http";
32
+ cyclicTasksSchema,
33
+ scheduleRepeats,
34
+ taskPriority,
35
+ } from "@tsplc/config/build";
36
+ import { ioScalarBits } from "@tsplc/iec";
37
+ import { addressBits, ioLayout, regionOf } from "@tsplc/typegen/hardware";
38
+ import { type CodesysOptions, optionsSchema } from "./config";
39
+ import { type HttpEndpoint, resolveHttpEndpoint } from "./endpoints";
40
+ import { httpLayout, serviceBlock } from "./http";
37
41
  import { writerPaths } from "./instrumentation";
38
42
  import {
39
43
  iecSpelling,
@@ -44,21 +48,18 @@ import {
44
48
  import type { ProjectHardware } from "./manifest";
45
49
  import { isIecIdentifier } from "./names";
46
50
  import { resolvePublication } from "./publication";
51
+ import { required } from "./required";
52
+ import { schedulePrograms } from "./scheduling";
47
53
  import { storageLayout } from "./storage";
48
54
  import {
49
55
  accessedStorage,
50
56
  routineTasks,
51
- schedulePrograms,
52
57
  taskStorageOwnership,
53
58
  } from "./task-access";
54
- import {
55
- planTelemetry,
56
- type TelemetryPlan,
57
- telemetryClient,
58
- } from "./telemetry";
59
+ import { planTelemetry, type TelemetryPlan } from "./telemetry";
59
60
 
60
- // Only this planner can admit a target configuration for emission. Cloning a
61
- // branded-looking object does not give it membership in this private identity set.
61
+ // Only this planner can admit a target configuration for emission: other modules cannot write the
62
+ // brand, and a copy of a plan is not in `plans`.
62
63
  const planned: unique symbol = Symbol("CODESYS plan");
63
64
  export interface CodesysPlan {
64
65
  readonly [planned]: true;
@@ -74,16 +75,25 @@ export interface CodesysPlan {
74
75
  readonly sharedRoutines: ReadonlyMap<string, string>;
75
76
  readonly deployment: Deployment;
76
77
  readonly options: CodesysOptions;
78
+ /** The IEC name `programNames` gives a program, by program name. */
79
+ readonly programNames: ReadonlyMap<string, string>;
77
80
  /** `io` keys that name authored storage, resolved against the verified types. */
78
81
  readonly storageBindings: readonly StorageBinding[];
79
82
  /** The storage paths `debug.writers.paths` matches, each recorded in a last-writer slot. */
80
83
  readonly writerPaths: readonly StorageBinding[];
81
84
  readonly publication: Publication;
85
+ /** The endpoint each service block calls, by block id. */
82
86
  readonly http: Readonly<Record<string, HttpEndpoint>>;
83
87
  readonly project: CodesysProject;
84
88
  /** Each task's records and their sender, when the program has telemetry. */
85
89
  readonly telemetry?: TelemetryPlan;
86
90
  }
91
+
92
+ const plans = new WeakSet<CodesysPlan>();
93
+
94
+ /** Whether `plan` is one `planCodesys` returned rather than a copy or a lookalike. */
95
+ export const isPlanned = (plan: CodesysPlan): boolean => plans.has(plan);
96
+
87
97
  /**
88
98
  * What the debug manifest describes beyond the program, the instrumentation `debug` asks for,
89
99
  * and the telemetry settings and server its records go to.
@@ -93,18 +103,16 @@ export interface CodesysProject {
93
103
  root: string;
94
104
  debug?: ResolvedDebugConfig;
95
105
  telemetry?: ResolvedTelemetryConfig;
96
- server?: Pick<
97
- NonNullable<ResolvedTsplcConfig["server"]>,
98
- "transport" | "baseUrlEnv"
99
- >;
106
+ server?: Pick<ServerConfig, "transport" | "baseUrlEnv">;
100
107
  hardware?: ProjectHardware;
101
108
  /** The HMI's controls, whose targets the debugger names as clients that write. */
102
109
  hmi?: Pick<ResolvedHmiConfig, "controls">;
103
110
  /** Where HTTP credentials and the telemetry base URL are read from; this process's environment by default. */
104
- environment?: Readonly<Record<string, string | undefined>>;
111
+ environment?: Environment;
105
112
  }
106
- const plans = new WeakSet<object>();
107
- export function diagnostic(message: string, span?: SourceSpan): Diagnostic {
113
+ /** Where a check of the plan sends what it refuses, each becoming a diagnostic. */
114
+ export type Report = (message: string, span?: SourceSpan) => void;
115
+ function diagnostic(message: string, span?: SourceSpan): Diagnostic {
108
116
  return {
109
117
  origin: "backend",
110
118
  code: "PLC7001",
@@ -113,9 +121,6 @@ export function diagnostic(message: string, span?: SourceSpan): Diagnostic {
113
121
  ...(span && { span }),
114
122
  };
115
123
  }
116
- export function isCodesysPlan(plan: CodesysPlan): boolean {
117
- return plans.has(plan);
118
- }
119
124
 
120
125
  export function planCodesys(
121
126
  specialized: VerifiedCoreModule,
@@ -146,61 +151,49 @@ export function planCodesys(
146
151
  const report = (message: string, span?: SourceSpan): void => {
147
152
  diagnostics.push(diagnostic(message, span));
148
153
  };
149
- const { core, routines } = sharedRoutines(
154
+ const { core, routines, tasks, specializedTasks } = scheduledRoutines(
150
155
  specialized,
151
156
  deployment,
152
157
  project.debug,
158
+ report,
153
159
  );
154
- const scheduled: Deployment = {
155
- tasks: schedulePrograms(core, deployment.tasks, report),
156
- };
160
+ const scheduled: Deployment = { tasks };
161
+ const { types } = coreTables(core);
157
162
  const http = new Map<string, HttpEndpoint>();
158
- for (const block of core.blocks) {
159
- if (serviceBlock(block)) {
160
- try {
161
- validateHttpLayout(
162
- block,
163
- new Map(core.types.map((type) => [type.id, type])),
164
- );
165
- http.set(
166
- block.id,
167
- block.runtime.kind === "rpc"
168
- ? {
169
- transport: "opcua",
170
- host: "",
171
- port: 0,
172
- authority: "",
173
- path: block.runtime.path,
174
- token: "",
175
- timeoutMs: options.http.timeoutMs,
176
- tls: false,
177
- }
178
- : resolveHttpEndpoint(block.runtime.url, options.http, environment),
179
- );
180
- } catch (cause) {
181
- report(
182
- cause instanceof Error ? cause.message : String(cause),
183
- block.span,
184
- );
185
- }
163
+ for (const block of core.blocks.filter(serviceBlock)) {
164
+ const layout = httpLayout(block, types);
165
+ if ("error" in layout) {
166
+ report(layout.error, block.span);
167
+ continue;
186
168
  }
169
+ const endpoint: HttpEndpoint | { error: string } =
170
+ block.runtime.kind === "rpc"
171
+ ? {
172
+ transport: "opcua",
173
+ path: block.runtime.path,
174
+ timeoutMs: options.http.timeoutMs,
175
+ }
176
+ : resolveHttpEndpoint(block.runtime.url, options.http, environment);
177
+ if ("error" in endpoint) {
178
+ report(endpoint.error, block.span);
179
+ continue;
180
+ }
181
+ http.set(block.id, endpoint);
187
182
  }
188
183
 
189
184
  const storageBindings = validateIo(core, options, report);
190
185
  const writerBindings = writerPaths(core, project.debug, report);
191
186
  validateTasks(core, scheduled, report);
192
- validateStorage(core, options, report);
187
+ validateProgramNames(core, options.programNames, report);
188
+ validateRetain(core, report);
193
189
  validateConstants(core, report);
194
- const { telemetry, endpoint } = planTelemetry(
190
+ const telemetry = planTelemetry(
195
191
  core,
196
192
  scheduled,
197
193
  options,
198
194
  { ...project, environment, trace: project.debug?.trace },
199
195
  report,
200
196
  );
201
- if (endpoint) {
202
- http.set(telemetryClient, endpoint);
203
- }
204
197
  const { publication, errors } = resolvePublication(core, options.publish);
205
198
  for (const error of errors) {
206
199
  report(error);
@@ -213,12 +206,11 @@ export function planCodesys(
213
206
  const plan: CodesysPlan = Object.freeze({
214
207
  core,
215
208
  specialized,
216
- specializedTasks: {
217
- tasks: schedulePrograms(specialized, deployment.tasks, () => {}),
218
- },
209
+ specializedTasks: { tasks: specializedTasks },
219
210
  sharedRoutines: routines,
220
211
  deployment: structuredClone(scheduled),
221
- options: structuredClone(options),
212
+ options,
213
+ programNames: new Map(Object.entries(options.programNames)),
222
214
  storageBindings,
223
215
  writerPaths: writerBindings,
224
216
  publication,
@@ -227,16 +219,6 @@ export function planCodesys(
227
219
  ...(telemetry && { telemetry }),
228
220
  [planned]: true as const,
229
221
  });
230
- // Parsed options can contain nested engineering settings; detach and freeze the complete plan.
231
- const freeze = (value: unknown): void => {
232
- if (typeof value !== "object" || value === null || Object.isFrozen(value)) {
233
- return;
234
- }
235
- for (const child of Object.values(value)) {
236
- freeze(child);
237
- }
238
- Object.freeze(value);
239
- };
240
222
  freeze(plan.deployment);
241
223
  freeze(plan.specializedTasks);
242
224
  freeze(plan.options);
@@ -247,41 +229,58 @@ export function planCodesys(
247
229
  return { plan, diagnostics: [] };
248
230
  }
249
231
 
250
- type Report = (message: string, span?: SourceSpan) => void;
251
-
252
232
  /**
253
233
  * Routines specialized per instance, merged where they run the same code in the same task, calls
254
234
  * of routines that only return a constant replaced by it, and temporaries read right after they
255
- * are written folded into their reader. Continuation programs stay apart: each task's wrapper
256
- * calls them, and joining them named the joined program after whichever came first.
235
+ * are written folded into their reader, with each task's programs in the module that results.
236
+ * Continuation programs stay apart: each task's wrapper calls them, and joining them named the
237
+ * joined program after whichever came first.
257
238
  * The debugger's instrumentation is written per statement of each routine, so a program built
258
- * with `debug` keeps every instance's own routine, as does one whose scheduling is invalid: its
259
- * diagnostics name the routines as written.
239
+ * with `debug` keeps every instance's own routine, as does one whose scheduling is invalid or
240
+ * names a program the module lacks: its diagnostics name the routines as written.
260
241
  */
261
- function sharedRoutines(
262
- core: VerifiedCoreModule,
242
+ function scheduledRoutines(
243
+ specialized: VerifiedCoreModule,
263
244
  deployment: Deployment,
264
245
  debug: ResolvedDebugConfig | undefined,
265
- ): { core: VerifiedCoreModule; routines: ReadonlyMap<string, string> } {
266
- const unshared = { core, routines: new Map<string, string>() };
267
- if (debug) {
268
- return unshared;
269
- }
246
+ report: Report,
247
+ ) {
270
248
  const issues: string[] = [];
271
- schedulePrograms(core, deployment.tasks, (message) => {
272
- issues.push(message);
273
- });
274
- if (issues.length) {
275
- return unshared;
249
+ const specializedTasks = schedulePrograms(
250
+ specialized,
251
+ deployment.tasks,
252
+ (message) => {
253
+ issues.push(message);
254
+ },
255
+ );
256
+ const programs = new Set(specialized.programs.map((program) => program.name));
257
+ const unknown = deployment.tasks.some((task) =>
258
+ task.programs.some((name) => !programs.has(name)),
259
+ );
260
+ if (debug || issues.length || unknown) {
261
+ for (const message of issues) {
262
+ report(message);
263
+ }
264
+ return {
265
+ core: specialized,
266
+ routines: new Map<string, string>(),
267
+ tasks: specializedTasks,
268
+ specializedTasks,
269
+ };
276
270
  }
277
- const folded = foldConstantRoutines(core);
278
- const tasks = routineTasks(folded.core, {
271
+ const folded = foldConstantRoutines(specialized);
272
+ const foldedTasks = routineTasks(folded.core, {
279
273
  tasks: schedulePrograms(folded.core, deployment.tasks, () => {}),
280
274
  });
281
- const shared = shareRoutines(folded.core, (routine) => tasks.get(routine.id));
275
+ const shared = shareRoutines(folded.core, (routine) =>
276
+ foldedTasks.get(routine.id),
277
+ );
278
+ const core = forwardTemporaries(shared.core);
282
279
  return {
283
- core: forwardTemporaries(shared.core),
280
+ core,
284
281
  routines: shared.routines,
282
+ tasks: schedulePrograms(core, deployment.tasks, report),
283
+ specializedTasks,
285
284
  };
286
285
  }
287
286
 
@@ -291,9 +290,7 @@ function validateIo(
291
290
  options: CodesysOptions,
292
291
  report: Report,
293
292
  ): StorageBinding[] {
294
- // Compare physical bindings as bit intervals, regardless of their spelling.
295
- // Different widths can still overlap.
296
- const types = new Map(core.types.map((type) => [type.id, type]));
293
+ const tables = coreTables(core);
297
294
  const targets: {
298
295
  name: string;
299
296
  binding: CodesysOptions["io"][string];
@@ -302,9 +299,7 @@ function validateIo(
302
299
  span?: SourceSpan;
303
300
  }[] = [];
304
301
  const names = new Set<string>();
305
- for (const item of core.variables.filter(
306
- (item) => item.storage === "input" || item.storage === "output",
307
- )) {
302
+ for (const item of core.variables.filter(isLogicalIo)) {
308
303
  if (names.has(item.name)) {
309
304
  report(
310
305
  `Ambiguous I/O name ${item.name}; logical I/O names must be unique.`,
@@ -322,16 +317,15 @@ function validateIo(
322
317
  targets.push({
323
318
  name: item.name,
324
319
  binding,
325
- type: types.get(item.type)!,
326
- region: item.storage === "input" ? "I" : "Q",
320
+ type: tables.typeOf(item.type),
321
+ region: regionOf(item.storage),
327
322
  span: item.span,
328
323
  });
329
324
  }
330
325
 
331
326
  const scheduled = new Set<string>();
332
327
  for (const program of core.programs) {
333
- const fn = core.functions.find((item) => item.id === program.entry)!;
334
- for (const id of accessedStorage(fn, core)) {
328
+ for (const id of accessedStorage(tables.functionOf(program.entry), core)) {
335
329
  scheduled.add(id);
336
330
  }
337
331
  }
@@ -365,14 +359,14 @@ function validateIo(
365
359
  name,
366
360
  binding,
367
361
  type: resolved.type,
368
- region: binding.address[1] === "I" ? "I" : "Q",
362
+ region: required(addressBits(binding.address)).region,
369
363
  });
370
364
  }
371
365
 
372
- const addresses: {
373
- region: string;
374
- start: bigint;
375
- end: bigint;
366
+ const placed: {
367
+ region: "I" | "Q";
368
+ start: number;
369
+ end: number;
376
370
  name: string;
377
371
  shared?: boolean;
378
372
  }[] = [];
@@ -380,66 +374,58 @@ function validateIo(
380
374
  // An array binds at its first element; the remaining elements follow at
381
375
  // consecutive addresses of the same width, like an EDS byte/word span.
382
376
  const array = type.kind === "array" ? type : undefined;
383
- const element = array ? types.get(array.element) : type;
377
+ const element = array ? tables.typeOf(array.element) : type;
384
378
  // Typegen spells channels as IEC types; equal layouts bind (an EDS UINT flag word into a WORD).
385
- const layout = ioLayout(iecSpelling(type, types) ?? "");
379
+ const layout = ioSpelling(type, tables, ioLayout);
386
380
  if (binding.type && (!layout || ioLayout(binding.type) !== layout)) {
387
381
  report(
388
382
  `I/O type mismatch for ${name}: ${type.id} cannot bind to ${binding.type}.`,
389
383
  span,
390
384
  );
391
385
  }
392
- if (!binding.address.startsWith(`%${region}`)) {
393
- report(`Wrong address region for ${name}: expected %${region}X.`, span);
394
- }
395
- const match = /^%([IQ])([XBWD])([0-9]+)(?:\.([0-7]))?$/.exec(
396
- binding.address,
397
- );
398
- const size = sizePrefixes.get(binding.address.charAt(2))?.bits;
399
- if (!match || size === undefined) {
400
- continue;
386
+ const address = required(addressBits(binding.address));
387
+ if (address.region !== region) {
388
+ report(
389
+ `Wrong address region for ${name}: expected %${region}${binding.address.charAt(2)}.`,
390
+ span,
391
+ );
401
392
  }
402
- if (region === "I" && element?.kind === "float") {
393
+ if (region === "I" && element.kind === "float") {
403
394
  report(
404
395
  "Floating logical inputs require an explicit runtime nonfinite policy, which is not supported yet.",
405
396
  span,
406
397
  );
407
398
  }
408
- if (array && element?.kind === "bool") {
399
+ if (array && element.kind === "bool") {
409
400
  report(
410
401
  `BOOL arrays are not bit-packed in CODESYS I/O; bind each element of ${name} separately.`,
411
402
  span,
412
403
  );
413
404
  }
414
- const elementBits =
415
- element && ioScalarBits(iecSpelling(element, types) ?? "");
416
- if (elementBits !== size) {
405
+ if (ioSpelling(element, tables, ioScalarBits) !== address.bits) {
417
406
  report(`Address size does not match ${name}:${type.id}.`, span);
418
407
  }
419
- // CODESYS numbers %IW/%ID by element: %IW12 is bytes 24-25. See verify.py.
420
- const unit = size === 1 ? 8n : BigInt(size);
421
- const offset = BigInt(match[3]!);
422
- const start = offset * unit + BigInt(match[4] ?? 0);
423
- const end = start + BigInt(size) * BigInt(array?.length ?? 1);
408
+ const start = address.offsetBits;
409
+ const end = start + address.bits * (array?.length ?? 1);
424
410
  if (
425
411
  binding.inverted &&
426
- (region !== "I" || element?.kind !== "bool" || array)
412
+ (region !== "I" || element.kind !== "bool" || array)
427
413
  ) {
428
414
  report(`Only a BOOL input can be inverted: ${name}.`, span);
429
415
  }
430
416
  if (binding.shared && region !== "I") {
431
417
  report(`Only an input can be shared: ${name}.`, span);
432
418
  }
433
- const collision = addresses.find(
434
- (address) =>
435
- address.region === match[1] &&
436
- address.start < end &&
437
- start < address.end &&
419
+ const collision = placed.find(
420
+ (other) =>
421
+ other.region === address.region &&
422
+ other.start < end &&
423
+ start < other.end &&
438
424
  !(
439
425
  binding.shared &&
440
- address.shared &&
441
- address.start === start &&
442
- address.end === end
426
+ other.shared &&
427
+ other.start === start &&
428
+ other.end === end
443
429
  ),
444
430
  );
445
431
  if (collision) {
@@ -448,20 +434,32 @@ function validateIo(
448
434
  span,
449
435
  );
450
436
  }
451
- addresses.push({
452
- region: match[1]!,
437
+ placed.push({
438
+ region: address.region,
453
439
  start,
454
440
  end,
455
441
  name,
456
442
  shared: binding.shared,
457
443
  });
458
- if (offset > 0x7fff_ffffn) {
444
+ // The address's number, which CODESYS counts in elements of its size: %IW12 is bytes 24-25.
445
+ const unit = address.bits === 1 ? 8 : address.bits;
446
+ if (Math.floor(address.offsetBits / unit) > 0x7fff_ffff) {
459
447
  report(`I/O offset is too large: ${binding.address}.`, span);
460
448
  }
461
449
  }
462
450
  return storageBindings;
463
451
  }
464
452
 
453
+ /** What `read` makes of a type's IEC spelling, or undefined for a type typegen does not spell. */
454
+ const ioSpelling = <T>(
455
+ type: PlcType,
456
+ tables: CoreTables,
457
+ read: (spelling: string) => T,
458
+ ): T | undefined => {
459
+ const spelling = iecSpelling(type, tables.types);
460
+ return spelling === undefined ? undefined : read(spelling);
461
+ };
462
+
465
463
  /** Two storage bindings conflict when one target contains the other. */
466
464
  function nested(left: StorageBinding, right: StorageBinding): boolean {
467
465
  return (
@@ -478,76 +476,76 @@ function nested(left: StorageBinding, right: StorageBinding): boolean {
478
476
  );
479
477
  }
480
478
 
481
- // The project CLI checks names and priorities, but not exact TIME intervals or case-insensitive
482
- // task names, and `planCodesys` callers pass deployments that no config schema has seen. So the
483
- // planner checks each task against the raw ST schedule's rules.
484
- const taskSchema = cyclicTasksSchema.element.shape;
485
-
479
+ // `planCodesys` callers pass deployments that no config schema has seen, so the planner checks
480
+ // each task against the raw ST schedule's rules, and the rules that need the program: CODESYS
481
+ // task names are identifiers, and each program runs in exactly one task.
486
482
  function validateTasks(
487
483
  core: VerifiedCoreModule,
488
484
  deployment: Deployment,
489
485
  report: Report,
490
486
  ): void {
487
+ const issues =
488
+ cyclicTasksSchema.safeParse(deployment.tasks).error?.issues ?? [];
489
+ const refused = (index: number, field: string) =>
490
+ issues.filter(({ path }) => path[0] === index && path[1] === field);
491
+ // zod skips the schema's own repeat check once any task has a type issue.
492
+ const repeats = scheduleRepeats(deployment.tasks);
493
+ const repeated = (task: number, position: number | null) =>
494
+ repeats.some((item) => item.task === task && item.position === position);
491
495
  const programs = new Map(
492
496
  core.programs.map((program) => [program.name, program]),
493
497
  );
494
- const assignment = new Map<string, string>();
495
- const taskNames = new Set<string>();
496
498
  const checkOwnership = taskStorageOwnership(core, report);
497
- for (const task of deployment.tasks) {
499
+ deployment.tasks.forEach((task, index) => {
498
500
  if (!isIecIdentifier(task.name)) {
499
501
  report(`Invalid task name ${task.name}.`);
500
502
  }
501
- if (taskNames.has(task.name.toUpperCase())) {
503
+ if (repeated(index, null)) {
502
504
  report(`Duplicate task name ${task.name}.`);
503
505
  }
504
- taskNames.add(task.name.toUpperCase());
505
- if (!taskSchema.priority.safeParse(task.priority).success) {
506
- report(`Task ${task.name} priority must be 0..31.`);
506
+ if (refused(index, "priority").length) {
507
+ report(
508
+ `Task ${task.name} priority must be ${taskPriority.minValue}..${taskPriority.maxValue}.`,
509
+ );
507
510
  }
508
- const interval = taskSchema.intervalNanoseconds.safeParse(
509
- task.intervalNanoseconds,
510
- );
511
- if (interval.error?.issues.some(({ code }) => code === "invalid_format")) {
511
+ const interval = refused(index, "intervalNanoseconds");
512
+ if (interval.some(({ code }) => code === "invalid_format")) {
512
513
  report(`Invalid task interval for ${task.name}.`);
513
- } else if (interval.error) {
514
+ } else if (interval.length) {
514
515
  report(
515
516
  `Task ${task.name} requires an exact positive TIME interval in milliseconds.`,
516
517
  );
517
518
  }
518
- if (!task.programs.length) {
519
+ if (refused(index, "programs").some(({ path }) => path.length === 2)) {
519
520
  report(`Task ${task.name} has no programs.`);
520
521
  }
521
- for (const name of task.programs) {
522
+ task.programs.forEach((name, position) => {
522
523
  const program = programs.get(name);
523
524
  if (!program) {
524
525
  report(`Unknown task program ${name}.`);
525
- continue;
526
+ return;
526
527
  }
527
- if (assignment.has(name)) {
528
+ if (repeated(index, position)) {
528
529
  report(`Program ${name} must belong to exactly one task.`);
529
530
  }
530
- assignment.set(name, task.name);
531
531
  checkOwnership(task.name, program);
532
- }
533
- }
532
+ });
533
+ });
534
+ const scheduled = new Set(deployment.tasks.flatMap((task) => task.programs));
534
535
  for (const name of programs.keys()) {
535
- if (!assignment.has(name)) {
536
+ if (!scheduled.has(name)) {
536
537
  report(`Program ${name} has no cyclic task.`);
537
538
  }
538
539
  }
539
540
  }
540
541
 
541
- function validateStorage(
542
+ function validateProgramNames(
542
543
  core: VerifiedCoreModule,
543
- options: CodesysOptions,
544
+ programNames: Readonly<Record<string, string>>,
544
545
  report: Report,
545
546
  ): void {
546
- const programs = new Map(
547
- core.programs.map((program) => [program.name, program]),
548
- );
549
547
  if (core.blocks.some((block) => block.standard === "Blinker")) {
550
- for (const target of Object.values(options.programNames ?? {})) {
548
+ for (const target of Object.values(programNames)) {
551
549
  if (target.toUpperCase() === "UTIL") {
552
550
  report(
553
551
  `IEC object name ${target} shadows the Util library namespace required by Blinker.`,
@@ -555,8 +553,9 @@ function validateStorage(
555
553
  }
556
554
  }
557
555
  }
556
+ const programs = new Set(core.programs.map((program) => program.name));
558
557
  const explicitNames = new Set<string>();
559
- for (const [name, target] of Object.entries(options.programNames ?? {})) {
558
+ for (const [name, target] of Object.entries(programNames)) {
560
559
  if (!programs.has(name)) {
561
560
  report(`Unknown program name override ${name}.`);
562
561
  }
@@ -565,12 +564,16 @@ function validateStorage(
565
564
  }
566
565
  explicitNames.add(target.toUpperCase());
567
566
  }
568
- const types = new Map(core.types.map((type) => [type.id, type]));
569
- const blocks = new Map(core.blocks.map((block) => [block.id, block]));
567
+ }
570
568
 
571
- // InOut addresses must not survive a retained restart, including nested children.
569
+ /**
570
+ * InOut addresses must not survive a retained restart, including nested children. CODESYS accepts
571
+ * one persistent variable list in an application, so only one module's storage may persist.
572
+ */
573
+ function validateRetain(core: VerifiedCoreModule, report: Report): void {
574
+ const { types, blockOf } = coreTables(core);
572
575
  const blockHasInOut = (id: string): boolean => {
573
- const block = blocks.get(id)!;
576
+ const block = blockOf(id);
574
577
  return (
575
578
  block.inputs.some((input) => input.mode === "inOut") ||
576
579
  block.state.some((field) => {
@@ -591,19 +594,28 @@ function validateStorage(
591
594
  }
592
595
  for (const variable of core.variables) {
593
596
  const type = instancedBlock(variable.type, types);
594
- if (variable.retain && type && blockHasInOut(type.block)) {
597
+ if (variable.retention !== "none" && type && blockHasInOut(type.block)) {
595
598
  report(
596
599
  "Retained function blocks cannot contain direct or nested InOut references.",
597
600
  variable.span,
598
601
  );
599
602
  }
600
603
  }
604
+ const [, ...others] = layout.globalLists.filter(
605
+ (list) => list.kind === "persistent",
606
+ );
607
+ for (const list of others) {
608
+ report(
609
+ "CODESYS keeps one persistent variable list per application: persist the storage of one module only.",
610
+ list.variables[0]?.span,
611
+ );
612
+ }
601
613
  }
602
614
 
603
615
  function validateConstants(core: VerifiedCoreModule, report: Report): void {
604
- const types = new Map(core.types.map((type) => [type.id, type]));
616
+ const { typeOf } = coreTables(core);
605
617
  const constant = (value: Constant, id: string, span?: SourceSpan): void => {
606
- const type = types.get(id)!;
618
+ const type = typeOf(id);
607
619
  if (type.kind === "string" && value.kind === "string") {
608
620
  if (
609
621
  type.encoding === "utf8" &&
@@ -631,7 +643,7 @@ function validateConstants(core: VerifiedCoreModule, report: Report): void {
631
643
  }
632
644
  if (type.kind === "record" && value.kind === "record") {
633
645
  for (const field of type.fields) {
634
- constant(value.fields[field.name]!, field.type, span);
646
+ constant(required(value.fields[field.name]), field.type, span);
635
647
  }
636
648
  }
637
649
  };