@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
package/src/ir/routine.ts CHANGED
@@ -1,11 +1,12 @@
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 { aggregate, expressionIssue, placeType, valueFits } from "./aggregates";
3
- import type {
4
- CoreExpression,
5
- CoreFunction,
6
- CoreFunctionBlock,
7
- CorePath,
8
- CoreStatement,
2
+ import {
3
+ type CoreExpression,
4
+ type CoreFunction,
5
+ type CoreFunctionBlock,
6
+ type CorePath,
7
+ type CoreStatement,
8
+ isBlockRoutine,
9
+ temporaryId,
9
10
  } from "./core-model";
10
11
  import type { ModuleDeclarations } from "./declarations";
11
12
  import type { SourceSpan } from "./diagnostics";
@@ -25,9 +26,10 @@ import {
25
26
  loopBounds,
26
27
  maxLoopIterations,
27
28
  narrowRanges,
28
- valueRange,
29
29
  } from "./ranges";
30
- import { instancedBlock, integerRange } from "./types";
30
+ import { type BranchOperand, lowerLogical, lowerSelect } from "./short-circuit";
31
+ import { lookup } from "./tables";
32
+ import { instancedBlock, integerRange, isValueType } from "./types";
31
33
 
32
34
  type Flow = "fallthrough" | "return" | "loop" | "mixed";
33
35
  interface FlowState {
@@ -65,32 +67,14 @@ const join = (
65
67
  };
66
68
  };
67
69
 
68
- // All mutable lowering state belongs to one invocation of one authored routine.
69
- // sourceScope admits authored places; scope additionally contains captured temps.
70
- const temporaryPrefix = (routine: string) => `${routine}:temporary:`;
71
- const temporaryId = (routine: string, index: number) =>
72
- `${temporaryPrefix(routine)}${index}`;
73
-
74
- /** Whether a local is a temporary the normalizer made for `routine`, rather than one its source declares. */
75
- export function isTemporary(routine: string, variable: Variable): boolean {
76
- const prefix = temporaryPrefix(routine);
77
- return (
78
- variable.storage === "local" &&
79
- variable.id.startsWith(prefix) &&
80
- /^\d+$/.test(variable.id.slice(prefix.length))
81
- );
82
- }
83
-
84
70
  export class RoutineNormalizer {
85
- private readonly fn: PlcFunction;
71
+ private readonly parameters: readonly Variable[];
86
72
 
87
- private readonly block: FunctionBlock | undefined;
88
73
  private readonly owner: FunctionBlock | undefined;
89
74
 
75
+ /** The storage the routine's source may name; temporaries are not in it, so a place the source names can't reach one. */
90
76
  private readonly scope: Map<string, Variable>;
91
77
 
92
- private readonly sourceScope: Map<string, Variable>;
93
-
94
78
  private readonly locals: Variable[];
95
79
 
96
80
  private readonly ranges = new Map<string, IntegerInterval>();
@@ -102,37 +86,26 @@ export class RoutineNormalizer {
102
86
  private temporaryCount = 0;
103
87
 
104
88
  constructor(
105
- routine: PlcFunction | FunctionBlock,
89
+ private readonly routine: PlcFunction | FunctionBlock,
106
90
  private readonly declarations: ModuleDeclarations,
107
91
  ) {
108
- this.block = "inputs" in routine ? routine : undefined;
109
- this.owner =
110
- this.block ??
111
- ("owner" in routine && routine.owner
92
+ if (isBlockRoutine(routine)) {
93
+ this.owner = routine;
94
+ this.parameters = routine.inputs;
95
+ } else {
96
+ this.owner = routine.owner
112
97
  ? declarations.blocks.get(routine.owner.block)
113
- : undefined);
114
- const parameters =
115
- "parameters" in routine ? routine.parameters : routine.inputs;
116
- this.fn = {
117
- id: routine.id,
118
- name: routine.name,
119
- ...("owner" in routine && routine.owner && { owner: routine.owner }),
120
- ...("origin" in routine && routine.origin && { origin: routine.origin }),
121
- parameters,
122
- locals: routine.locals,
123
- body: routine.body,
124
- span: routine.span,
125
- ...(routine.result && { result: routine.result }),
126
- };
98
+ : undefined;
99
+ this.parameters = routine.parameters;
100
+ }
127
101
  this.scope = new Map(declarations.globals);
128
102
  for (const item of [
129
- ...parameters,
103
+ ...this.parameters,
130
104
  ...(this.owner ? [...this.owner.state, ...this.owner.outputs] : []),
131
105
  ...routine.locals,
132
106
  ]) {
133
107
  this.scope.set(item.id, item);
134
108
  }
135
- this.sourceScope = new Map(this.scope);
136
109
  this.locals = [...routine.locals];
137
110
  }
138
111
 
@@ -140,27 +113,30 @@ export class RoutineNormalizer {
140
113
  routine: CoreFunction | CoreFunctionBlock;
141
114
  callees: ReadonlySet<string>;
142
115
  } {
143
- if (this.block?.standard) {
144
- return { routine: { ...this.block, body: [] }, callees: this.callees };
116
+ const { routine } = this;
117
+ // A standard block has no body: the vendor library implements it.
118
+ if (isBlockRoutine(routine) && routine.standard) {
119
+ return { routine: { ...routine, body: [] }, callees: this.callees };
145
120
  }
121
+ return {
122
+ routine: { ...routine, locals: this.locals, body: this.routineBody() },
123
+ callees: this.callees,
124
+ };
125
+ }
126
+
127
+ private routineBody(): CoreStatement[] {
146
128
  const normalized = this.body(
147
- this.fn.body,
148
- new Set(this.fn.parameters.map((item) => item.id)),
129
+ this.routine.body,
130
+ new Set(this.parameters.map((item) => item.id)),
149
131
  );
150
- if (this.fn.result && normalized.flow !== "return") {
151
- reject("A typed helper must return on every path.", this.fn.span);
132
+ if (this.routine.result && normalized.flow !== "return") {
133
+ reject("A typed helper must return on every path.", this.routine.span);
152
134
  }
153
- const routine = this.block
154
- ? { ...this.block, locals: this.locals, body: normalized.body }
155
- : { ...this.fn, locals: this.locals, body: normalized.body };
156
- return { routine, callees: this.callees };
135
+ return normalized.body;
157
136
  }
158
137
 
159
138
  private temporary(type: string, span: SourceSpan): Variable {
160
- let id: string;
161
- do {
162
- id = temporaryId(this.fn.id, this.temporaryCount++);
163
- } while (this.declarations.hasId(id));
139
+ const id = this.unclaimedTemporaryId();
164
140
  this.declarations.claimId(id);
165
141
  const item: Variable = {
166
142
  id,
@@ -168,21 +144,25 @@ export class RoutineNormalizer {
168
144
  type,
169
145
  storage: "local",
170
146
  mutable: true,
171
- retain: false,
147
+ retention: "none",
172
148
  span,
173
149
  };
174
- this.scope.set(id, item);
175
150
  this.locals.push(item);
176
151
  return item;
177
152
  }
178
153
 
154
+ private unclaimedTemporaryId(): string {
155
+ const id = temporaryId(this.routine.id, this.temporaryCount++);
156
+ return this.declarations.hasId(id) ? this.unclaimedTemporaryId() : id;
157
+ }
158
+
179
159
  private load(variable: Variable, span: SourceSpan): CoreExpression {
180
160
  return { kind: "load", root: variable.id, type: variable.type, span };
181
161
  }
182
162
 
183
163
  /** The block a method or update receiver instantiates: a static instance, or an InOut reference to one. */
184
164
  private receiverBlock(id: string): string | undefined {
185
- const variable = this.sourceScope.get(id);
165
+ const variable = this.scope.get(id);
186
166
  if (variable?.storage !== "instance" && variable?.mode !== "inOut") {
187
167
  return undefined;
188
168
  }
@@ -238,10 +218,10 @@ export class RoutineNormalizer {
238
218
  value: Expression,
239
219
  initialized: Set<string>,
240
220
  output: CoreStatement[],
241
- allowAggregateLoad = false,
221
+ { allowAggregateLoad }: { allowAggregateLoad?: true } = {},
242
222
  ): CoreExpression {
243
223
  const type = this.declarations.types.get(value.type);
244
- if (!type || !this.declarations.isValueType(value.type)) {
224
+ if (!type || !isValueType(value.type, this.declarations.types)) {
245
225
  reject("Unknown or non-value expression type.", value.span);
246
226
  }
247
227
  if (value.kind === "borrow") {
@@ -257,11 +237,11 @@ export class RoutineNormalizer {
257
237
  reject("Length requires a fixed array and DINT result.", value.span);
258
238
  }
259
239
  if (value.operand.kind === "load") {
260
- const source = this.sourceScope.get(value.operand.place.root);
240
+ const source = this.scope.get(value.operand.place.root);
261
241
  const checked = placeType(
262
242
  value.operand.place,
263
243
  this.declarations.types,
264
- this.sourceScope,
244
+ this.scope,
265
245
  this.ranges,
266
246
  );
267
247
  if (
@@ -274,7 +254,9 @@ export class RoutineNormalizer {
274
254
  }
275
255
  this.normalizePath(value.operand.place, initialized, output);
276
256
  } else {
277
- this.expression(value.operand, initialized, output, true);
257
+ this.expression(value.operand, initialized, output, {
258
+ allowAggregateLoad: true,
259
+ });
278
260
  }
279
261
  return {
280
262
  ...common,
@@ -292,29 +274,17 @@ export class RoutineNormalizer {
292
274
  }
293
275
  const condition = this.expression(value.condition, initialized, output);
294
276
  const result = this.temporary(value.type, value.span);
295
- const branch = (operand: Expression): CoreStatement[] => {
296
- const statements: CoreStatement[] = [];
297
- const evaluated = this.expression(
298
- operand,
299
- new Set(initialized),
300
- statements,
301
- );
302
- statements.push({
303
- kind: "assign",
304
- root: result.id,
305
- value: evaluated,
277
+ const whenTrue = this.branchOperand(value.then, initialized);
278
+ const whenFalse = this.branchOperand(value.else, initialized);
279
+ output.push(
280
+ lowerSelect({
281
+ result: { root: result.id, type: result.type },
282
+ condition,
283
+ whenTrue,
284
+ whenFalse,
306
285
  span: value.span,
307
- });
308
- return statements;
309
- };
310
- output.push({
311
- kind: "if",
312
- condition,
313
- // biome-ignore lint/suspicious/noThenProperty: PLC branch data is an array, not a thenable callback.
314
- then: branch(value.then),
315
- else: branch(value.else),
316
- span: value.span,
317
- });
286
+ }),
287
+ );
318
288
  initialized.add(result.id);
319
289
  return this.load(result, value.span);
320
290
  }
@@ -367,7 +337,9 @@ export class RoutineNormalizer {
367
337
  }
368
338
  switch (value.kind) {
369
339
  case "copy":
370
- return this.expression(value.operand, initialized, output, true);
340
+ return this.expression(value.operand, initialized, output, {
341
+ allowAggregateLoad: true,
342
+ });
371
343
  case "record":
372
344
  case "array": {
373
345
  const result = this.temporary(value.type, value.span);
@@ -426,11 +398,11 @@ export class RoutineNormalizer {
426
398
  return this.load(result, value.span);
427
399
  }
428
400
  case "load": {
429
- const source = this.sourceScope.get(value.place.root);
401
+ const source = this.scope.get(value.place.root);
430
402
  const checked = placeType(
431
403
  value.place,
432
404
  this.declarations.types,
433
- this.sourceScope,
405
+ this.scope,
434
406
  this.ranges,
435
407
  );
436
408
  if (!source || checked.issue || checked.type !== value.type) {
@@ -469,12 +441,9 @@ export class RoutineNormalizer {
469
441
  },
470
442
  span: value.span,
471
443
  };
472
- const captured = this.expression(
473
- selected,
474
- initialized,
475
- output,
476
- true,
477
- );
444
+ const captured = this.expression(selected, initialized, output, {
445
+ allowAggregateLoad: true,
446
+ });
478
447
  output.push({
479
448
  kind: "assign",
480
449
  root: result.id,
@@ -524,10 +493,10 @@ export class RoutineNormalizer {
524
493
  }
525
494
  case "convert": {
526
495
  const operand = this.expression(value.operand, initialized, output);
527
- const constant = fold(value, this.scope, this.declarations.types);
528
496
  if (value.type === value.operand.type) {
529
497
  return operand;
530
498
  }
499
+ const constant = fold(value, this.scope, this.declarations.types);
531
500
  return constant
532
501
  ? { ...common, kind: "constant", value: constant }
533
502
  : { ...common, kind: "convert", operand, policy: value.policy };
@@ -536,42 +505,16 @@ export class RoutineNormalizer {
536
505
  const left = this.expression(value.left, initialized, output);
537
506
  if (value.operator === "and" || value.operator === "or") {
538
507
  const result = this.temporary("BOOL", value.span);
539
- const branch: CoreStatement[] = [];
540
- output.push({
541
- kind: "assign",
542
- root: result.id,
543
- value: left,
544
- span: value.span,
545
- });
546
- const right = this.expression(
547
- value.right,
548
- new Set(initialized),
549
- branch,
508
+ const right = this.branchOperand(value.right, initialized);
509
+ output.push(
510
+ ...lowerLogical({
511
+ operator: value.operator,
512
+ result: { root: result.id, type: result.type },
513
+ left,
514
+ right,
515
+ span: value.span,
516
+ }),
550
517
  );
551
- branch.push({
552
- kind: "assign",
553
- root: result.id,
554
- value: right,
555
- span: value.span,
556
- });
557
- const condition: CoreExpression =
558
- value.operator === "and"
559
- ? this.load(result, value.span)
560
- : {
561
- kind: "unary",
562
- operator: "not",
563
- operand: this.load(result, value.span),
564
- type: "BOOL",
565
- span: value.span,
566
- };
567
- output.push({
568
- kind: "if",
569
- condition,
570
- // biome-ignore lint/suspicious/noThenProperty: PLC branch data is an array, not a thenable callback.
571
- then: branch,
572
- else: [],
573
- span: value.span,
574
- });
575
518
  initialized.add(result.id);
576
519
  return this.load(result, value.span);
577
520
  }
@@ -592,6 +535,16 @@ export class RoutineNormalizer {
592
535
  }
593
536
  }
594
537
 
538
+ /** An operand that runs only on one branch, so what it initializes stays there. */
539
+ private branchOperand(
540
+ operand: Expression,
541
+ initialized: Set<string>,
542
+ ): BranchOperand {
543
+ const statements: CoreStatement[] = [];
544
+ const value = this.expression(operand, new Set(initialized), statements);
545
+ return { statements, value };
546
+ }
547
+
595
548
  private normalizePath(
596
549
  place: Place,
597
550
  initialized: Set<string>,
@@ -615,7 +568,7 @@ export class RoutineNormalizer {
615
568
  initialized: Set<string>,
616
569
  span: SourceSpan,
617
570
  ): Variable {
618
- const target = this.sourceScope.get(place.root);
571
+ const target = this.scope.get(place.root);
619
572
  const initializing =
620
573
  target?.storage === "local" &&
621
574
  !initialized.has(place.root) &&
@@ -623,7 +576,7 @@ export class RoutineNormalizer {
623
576
  const checked = placeType(
624
577
  place,
625
578
  this.declarations.types,
626
- this.sourceScope,
579
+ this.scope,
627
580
  this.ranges,
628
581
  !initializing,
629
582
  );
@@ -658,7 +611,7 @@ export class RoutineNormalizer {
658
611
  const checked = placeType(
659
612
  { root: instance, path: [{ kind: "index", value: index }] },
660
613
  this.declarations.types,
661
- this.sourceScope,
614
+ this.scope,
662
615
  this.ranges,
663
616
  );
664
617
  if (checked.issue) {
@@ -718,16 +671,21 @@ export class RoutineNormalizer {
718
671
  });
719
672
  }
720
673
 
721
- // verifyCall has matched the argument count to the parameters.
722
674
  private arguments(
723
675
  parameters: readonly Variable[],
724
676
  values: readonly Expression[],
725
677
  initialized: Set<string>,
726
678
  output: CoreStatement[],
727
679
  ): CoreExpression[] {
728
- return values.map((value, index) =>
729
- this.argument(parameters[index]!, value, initialized, output),
730
- );
680
+ return values.map((value, index) => {
681
+ const parameter = parameters[index];
682
+ if (!parameter) {
683
+ throw new Error(
684
+ "A call passes more arguments than its callee declares.",
685
+ );
686
+ }
687
+ return this.argument(parameter, value, initialized, output);
688
+ });
731
689
  }
732
690
 
733
691
  private argument(
@@ -743,7 +701,6 @@ export class RoutineNormalizer {
743
701
  return this.borrow(value, initialized, output);
744
702
  }
745
703
  if (input.mode === "inOut") {
746
- // A value with no writable address reaches the callee through a temporary.
747
704
  if (this.declarations.types.get(value.type)?.kind === "block") {
748
705
  reject("Pass a block instance by name.", value.span);
749
706
  }
@@ -768,7 +725,9 @@ export class RoutineNormalizer {
768
725
  value.span,
769
726
  );
770
727
  }
771
- return this.expression(value, initialized, output, true);
728
+ return this.expression(value, initialized, output, {
729
+ allowAggregateLoad: true,
730
+ });
772
731
  }
773
732
 
774
733
  private borrow(
@@ -777,11 +736,11 @@ export class RoutineNormalizer {
777
736
  output: CoreStatement[],
778
737
  ): CoreExpression {
779
738
  const block = this.declarations.types.get(value.type)?.kind === "block";
780
- const root = this.sourceScope.get(value.place.root);
739
+ const root = this.scope.get(value.place.root);
781
740
  const checked = placeType(
782
741
  value.place,
783
742
  this.declarations.types,
784
- this.sourceScope,
743
+ this.scope,
785
744
  this.ranges,
786
745
  !block,
787
746
  );
@@ -789,7 +748,7 @@ export class RoutineNormalizer {
789
748
  !root ||
790
749
  checked.issue ||
791
750
  checked.type !== value.type ||
792
- (!block && !this.declarations.isValueType(value.type)) ||
751
+ (!block && !isValueType(value.type, this.declarations.types)) ||
793
752
  this.counters.has(root.id) ||
794
753
  (root.storage === "local" && !initialized.has(root.id))
795
754
  ) {
@@ -815,13 +774,13 @@ export class RoutineNormalizer {
815
774
  initialized: Set<string>,
816
775
  ): BodyResult {
817
776
  const output: CoreStatement[] = [];
818
- let state: FlowState = { flow: "fallthrough", initialized };
819
- for (const item of statements) {
820
- if (state.flow !== "fallthrough") {
821
- break;
822
- }
823
- state = this.statement(item, state.initialized, output);
824
- }
777
+ const state = statements.reduce<FlowState>(
778
+ (reached, item) =>
779
+ reached.flow === "fallthrough"
780
+ ? this.statement(item, reached.initialized, output)
781
+ : reached,
782
+ { flow: "fallthrough", initialized },
783
+ );
825
784
  return { body: output, ...state };
826
785
  }
827
786
 
@@ -848,7 +807,7 @@ export class RoutineNormalizer {
848
807
  this.forStatement(item, initialized, output);
849
808
  return { flow: "fallthrough", initialized };
850
809
  case "return": {
851
- if (item.value?.type !== this.fn.result) {
810
+ if (item.value?.type !== this.routine.result) {
852
811
  reject("Return differs from the function signature.", item.span);
853
812
  }
854
813
  const value =
@@ -865,14 +824,11 @@ export class RoutineNormalizer {
865
824
  return { flow: "fallthrough", initialized };
866
825
  case "break":
867
826
  case "continue":
868
- // Every active bounded loop holds its counter.
869
827
  if (!this.counters.size) {
870
828
  reject("Loop control appears outside a bounded loop.", item.span);
871
829
  }
872
830
  output.push({ kind: item.kind, span: item.span });
873
831
  return { flow: "loop", initialized };
874
- default:
875
- reject("Unknown PLC statement.");
876
832
  }
877
833
  }
878
834
 
@@ -887,13 +843,7 @@ export class RoutineNormalizer {
887
843
  const bound =
888
844
  !path.length &&
889
845
  this.scope.get(item.destination.root)?.mutable === false &&
890
- valueRange(
891
- item.value,
892
- this.declarations.types,
893
- this.scope,
894
- this.ranges,
895
- this.declarations.signatures,
896
- );
846
+ interval(item.value, this.declarations.types, this.scope, this.ranges);
897
847
  const value = this.expression(item.value, initialized, output);
898
848
  if (bound) {
899
849
  this.ranges.set(item.destination.root, bound);
@@ -974,7 +924,7 @@ export class RoutineNormalizer {
974
924
  item.condition,
975
925
  truth,
976
926
  this.declarations.types,
977
- this.sourceScope,
927
+ this.scope,
978
928
  originalRanges,
979
929
  )) {
980
930
  this.ranges.set(root, range);
@@ -1007,8 +957,7 @@ export class RoutineNormalizer {
1007
957
  output: CoreStatement[],
1008
958
  ): FlowState {
1009
959
  const value = this.expression(item.value, initialized, output);
1010
- // The expression above admitted the value's type.
1011
- const type = this.declarations.types.get(item.value.type)!;
960
+ const type = lookup(this.declarations.types, item.value.type, "type");
1012
961
  const seen = new Set<string>();
1013
962
  const cases = item.cases.map((branch) => {
1014
963
  for (const constant of branch.values) {
@@ -1048,7 +997,7 @@ export class RoutineNormalizer {
1048
997
  initialized: Set<string>,
1049
998
  output: CoreStatement[],
1050
999
  ): void {
1051
- const counter = this.sourceScope.get(item.counter);
1000
+ const counter = this.scope.get(item.counter);
1052
1001
  const counterType = counter && this.declarations.types.get(counter.type);
1053
1002
  const storage =
1054
1003
  item.counterScope === "existing"
@@ -1072,36 +1021,30 @@ export class RoutineNormalizer {
1072
1021
  const startRange = interval(
1073
1022
  item.start,
1074
1023
  this.declarations.types,
1075
- this.sourceScope,
1024
+ this.scope,
1076
1025
  this.ranges,
1077
1026
  );
1078
1027
  const limitRange = interval(
1079
1028
  item.limit,
1080
1029
  this.declarations.types,
1081
- this.sourceScope,
1030
+ this.scope,
1082
1031
  this.ranges,
1083
1032
  );
1084
1033
  if (!startRange || !limitRange) {
1085
1034
  reject("Loop bounds require finite integer ranges.", item.span);
1086
1035
  }
1087
1036
  const step = BigInt(item.step);
1088
- const { first, last, count, range } = loopBounds(
1037
+ const { first, last, count, range, terminal } = loopBounds(
1089
1038
  startRange,
1090
1039
  limitRange,
1091
1040
  step,
1092
1041
  item.exclusive,
1093
1042
  );
1094
1043
  const dynamic = start?.kind !== "integer" || limit?.kind !== "integer";
1095
- if (
1096
- count > maxLoopIterations ||
1097
- Number(count) !== item.maxIterations ||
1098
- !Number.isSafeInteger(item.maxIterations)
1099
- ) {
1044
+ if (count > maxLoopIterations || Number(count) !== item.maxIterations) {
1100
1045
  reject("Loop iteration proof differs from its bounds.", item.span);
1101
1046
  }
1102
1047
  if (item.counterScope === "existing") {
1103
- const lastValue = step > 0n ? range[1] : range[0];
1104
- const terminal = dynamic ? lastValue + step : first + count * step;
1105
1048
  if (count > 0n && !admits(integerRange(counterType), terminal)) {
1106
1049
  reject(
1107
1050
  "The existing loop counter overflows on termination.",