@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,11 +1,12 @@
1
- import type { CoreExpression, CorePath } from "@tsplc/compiler/ir";
2
- import { type CodesysSymbols, required } from "./symbols";
1
+ import type { CoreExpression, CorePath, CoreTables } from "@tsplc/compiler/ir";
2
+ import { required } from "./required";
3
+ import type { CodesysSymbols } from "./symbols";
3
4
 
4
- /** The storage a routine's ST can name: module storage, IO and its own variables, with their types. */
5
+ /** The storage a routine's ST can name (module storage, IO and its own variables), and each variable's declaration. */
5
6
  export interface StorageScope {
6
7
  symbols: CodesysSymbols;
7
8
  names: ReadonlyMap<string, string>;
8
- types: ReadonlyMap<string, string>;
9
+ variableOf: CoreTables["variableOf"];
9
10
  }
10
11
 
11
12
  export const storageName = ({ names }: StorageScope, id: string): string => {
@@ -16,53 +17,60 @@ export const storageName = ({ names }: StorageScope, id: string): string => {
16
17
  return value;
17
18
  };
18
19
 
19
- // Block output records are a source abstraction; target FB fields are direct.
20
20
  export const placeText = (
21
21
  scope: StorageScope,
22
22
  id: string,
23
23
  path: CorePath = [],
24
24
  ): string => {
25
25
  const { typeMap, blocks, blockFields, targetTypes } = scope.symbols;
26
- let text = storageName(scope, id);
27
- let typeId = required(scope.types.get(id));
28
- let outputBlock: string | undefined;
29
- for (const segment of path) {
26
+ const place = (
27
+ text: string,
28
+ typeId: string,
29
+ [segment, ...rest]: CorePath,
30
+ ): string => {
31
+ if (!segment) {
32
+ return text;
33
+ }
30
34
  const type = required(typeMap.get(typeId));
35
+ // Block output records are a source abstraction; target FB fields are direct.
31
36
  if (
32
37
  segment.kind === "field" &&
33
38
  type.kind === "block" &&
34
39
  segment.name === "out"
35
40
  ) {
36
- outputBlock = type.block;
37
- typeId = required(type.output);
38
- continue;
39
- }
40
- if (segment.kind === "field" && outputBlock) {
41
+ const [field, ...after] = rest;
42
+ if (!field) {
43
+ return text;
44
+ }
41
45
  const output = required(
42
- required(blocks.get(outputBlock)).outputs.find(
43
- (field) => field.name === segment.name,
46
+ required(blocks.get(type.block)).outputs.find(
47
+ (item) => field.kind === "field" && item.name === field.name,
44
48
  ),
45
49
  );
46
- text += `.${required(required(blockFields.get(outputBlock)).get(output.id))}`;
47
- typeId = output.type;
48
- outputBlock = undefined;
49
- } else if (segment.kind === "field" && type.kind === "record") {
50
- text += `.${targetTypes.field(typeId, segment.name)}`;
51
- typeId = required(
52
- type.fields.find((field) => field.name === segment.name),
53
- ).type;
54
- } else if (segment.kind === "index" && type.kind === "array") {
50
+ return place(
51
+ `${text}.${required(required(blockFields.get(type.block)).get(output.id))}`,
52
+ output.type,
53
+ after,
54
+ );
55
+ }
56
+ if (segment.kind === "field" && type.kind === "record") {
57
+ return place(
58
+ `${text}.${targetTypes.field(typeId, segment.name)}`,
59
+ required(type.fields.find((field) => field.name === segment.name)).type,
60
+ rest,
61
+ );
62
+ }
63
+ if (segment.kind === "index" && type.kind === "array") {
55
64
  // A strict enum converts explicitly to its DINT base, as HTTP JSON encoding does.
56
- text +=
65
+ const index =
57
66
  typeMap.get(segment.value.type)?.kind === "enum"
58
- ? `[TO_DINT(${expressionText(scope, segment.value)})]`
59
- : `[${expressionText(scope, segment.value)}]`;
60
- typeId = type.element;
61
- } else {
62
- throw new Error("Invalid verified place");
67
+ ? `TO_DINT(${expressionText(scope, segment.value)})`
68
+ : expressionText(scope, segment.value);
69
+ return place(`${text}[${index}]`, type.element, rest);
63
70
  }
64
- }
65
- return text;
71
+ throw new Error("Invalid verified place");
72
+ };
73
+ return place(storageName(scope, id), scope.variableOf(id).type, path);
66
74
  };
67
75
 
68
76
  export const expressionText = (
@@ -2,9 +2,11 @@ import {
2
2
  defaultValue,
3
3
  type GeneratedObject,
4
4
  instancedBlock,
5
+ isLogicalIo,
6
+ type SourceSpan,
5
7
  type Variable,
6
8
  } from "@tsplc/compiler/ir";
7
- import type { SourceMapping } from "./emit";
9
+ import { isOutputAddress } from "@tsplc/typegen/hardware";
8
10
  import {
9
11
  isStoragePath,
10
12
  resolveStoragePath,
@@ -12,11 +14,15 @@ import {
12
14
  } from "./io-paths";
13
15
  import { Names } from "./names";
14
16
  import type { CodesysPlan } from "./plan";
17
+ import { required } from "./required";
18
+ import type { ObjectMapping } from "./st-files";
15
19
  import type { GlobalStorage } from "./storage";
16
- import { type CodesysSymbols, required } from "./symbols";
20
+ import type { EmissionContext, GlobalSymbols } from "./symbols";
17
21
 
18
22
  const globalSections = {
19
23
  constants: "VAR_GLOBAL CONSTANT",
24
+ // The build makes a list with this section CODESYS's one persistent variable list.
25
+ persistent: "VAR_GLOBAL PERSISTENT RETAIN",
20
26
  retained: "VAR_GLOBAL RETAIN",
21
27
  state: "VAR_GLOBAL",
22
28
  } as const satisfies Record<GlobalStorage, string>;
@@ -39,7 +45,7 @@ export interface IoMapping {
39
45
  }
40
46
 
41
47
  const globalInitializer = (
42
- { typeMap, targetTypes }: CodesysSymbols,
48
+ { typeMap, targetTypes }: GlobalSymbols,
43
49
  storage: GlobalStorage,
44
50
  item: Variable,
45
51
  ): string => {
@@ -54,118 +60,126 @@ const globalInitializer = (
54
60
  : targetTypes.declarationInitializer(value, item.type);
55
61
  };
56
62
 
57
- /**
58
- * How a variable is published for the build target: an exposure is read-only, and any other
59
- * published variable is writable by clients. Unpublished variables have no access.
60
- */
61
- export const publishedAccess = (
63
+ /** Whether the build target publishes a variable, which clients may then read and write. */
64
+ export const isPublished = (
62
65
  plan: CodesysPlan,
63
- ): ((variable: Variable) => "read" | "readwrite" | undefined) => {
66
+ ): ((variable: Variable) => boolean) => {
64
67
  const published = new Set(plan.publication[plan.options.buildTarget]);
65
- return (variable) => {
66
- if (!published.has(variable.id)) {
67
- return undefined;
68
- }
69
- return variable.exposure ? "read" : "readwrite";
68
+ return (variable) => published.has(variable.id);
69
+ };
70
+
71
+ /** A list's declaration lines and the source each declares, in the order it writes them. */
72
+ const listLines = (object: string, header: readonly string[]) => {
73
+ const lines = [...header];
74
+ const mappings: ObjectMapping[] = [];
75
+ return {
76
+ lines,
77
+ mappings,
78
+ /** Appends a line; one that declares `span` maps back to it. */
79
+ write: (line: string, span?: SourceSpan) => {
80
+ lines.push(line);
81
+ if (span) {
82
+ mappings.push({
83
+ object,
84
+ file: "declaration",
85
+ line: lines.length,
86
+ span,
87
+ });
88
+ }
89
+ },
70
90
  };
71
91
  };
72
92
 
73
- /** The global lists of module storage; each variable's global symbol goes into `symbols.references`. */
93
+ /** The global lists of module storage, and the global symbol of each variable they declare. */
74
94
  export function emitGlobalLists(
75
95
  plan: CodesysPlan,
76
- symbols: CodesysSymbols,
77
- ): { objects: GeneratedObject[]; mappings: SourceMapping[] } {
78
- const { globals, references, targetTypes } = symbols;
96
+ symbols: GlobalSymbols,
97
+ ): {
98
+ objects: GeneratedObject[];
99
+ mappings: ObjectMapping[];
100
+ references: Map<string, string>;
101
+ } {
102
+ const { globals, targetTypes } = symbols;
79
103
  const objects: GeneratedObject[] = [];
80
- const mappings: SourceMapping[] = [];
81
- const access = publishedAccess(plan);
104
+ const mappings: ObjectMapping[] = [];
105
+ const references = new Map<string, string>();
106
+ const published = isPublished(plan);
82
107
  for (const group of globals) {
83
108
  const { name, fields } = group;
84
- const publish = group.variables.some((variable) => access(variable));
85
- const lines = [
109
+ const publish = group.variables.some(published);
110
+ const list = listLines(group.id, [
86
111
  ...(publish ? ["{attribute 'linkalways'}"] : []),
87
112
  "{attribute 'qualified_only'}",
88
- ];
113
+ ]);
89
114
  for (const section of group.sections) {
90
- lines.push(globalSections[section.storage]);
115
+ list.write(globalSections[section.storage]);
91
116
  for (const item of section.variables) {
92
117
  const field = required(fields.get(item.id));
93
118
  references.set(item.id, `${name}.${field}`);
94
- const symbol = access(item);
95
- if (symbol) {
96
- lines.push(` {attribute 'symbol' := '${symbol}'}`);
119
+ if (published(item)) {
120
+ list.write(" {attribute 'symbol' := 'readwrite'}");
97
121
  }
98
- mappings.push({
99
- path: `${name}.st`,
100
- line: lines.length + 1,
101
- span: item.span,
102
- });
103
122
  const initial = globalInitializer(symbols, section.storage, item);
104
- lines.push(` ${field} : ${targetTypes.name(item.type)}${initial};`);
123
+ list.write(
124
+ ` ${field} : ${targetTypes.name(item.type)}${initial};`,
125
+ item.span,
126
+ );
105
127
  }
106
- lines.push("END_VAR");
128
+ list.write("END_VAR");
107
129
  }
108
130
  objects.push({
109
131
  id: group.id,
110
132
  kind: "globalList",
111
133
  name,
112
- declaration: `${lines.join("\n")}\n`,
134
+ declaration: `${list.lines.join("\n")}\n`,
113
135
  });
136
+ mappings.push(...list.mappings);
114
137
  }
115
- return { objects, mappings };
138
+ return { objects, mappings, references };
116
139
  }
117
140
 
118
- /**
119
- * `GVL_IO`: logical IO and the AT wires of `io` bindings. Logical IO's global symbols go into
120
- * `symbols.references`.
121
- */
141
+ /** The id of the I/O list, which no source module places. */
142
+ export const ioListId = "io";
143
+
144
+ /** `GVL_IO`: logical IO and the AT wires of `io` bindings, and logical IO's global symbols. */
122
145
  export function emitIoList(
123
146
  plan: CodesysPlan,
124
- symbols: CodesysSymbols,
147
+ symbols: GlobalSymbols,
148
+ references: ReadonlyMap<string, string>,
125
149
  ): {
126
150
  objects: GeneratedObject[];
127
- mappings: SourceMapping[];
151
+ mappings: ObjectMapping[];
128
152
  channels: IoChannel[];
129
153
  ioMappings: IoMapping[];
154
+ references: Map<string, string>;
130
155
  } {
131
156
  const { core, options } = plan;
132
- const { names, references, targetTypes, typeMap } = symbols;
133
- const objects: GeneratedObject[] = [];
134
- const mappings: SourceMapping[] = [];
157
+ const { names, targetTypes, typeMap } = symbols;
135
158
  const channels: IoChannel[] = [];
136
159
  const ioMappings: IoMapping[] = [];
160
+ const ioReferences = new Map<string, string>();
137
161
  const ioName = names.allocate("GVL_IO");
138
- const ioFields = new Names();
139
- for (const name of symbols.blockNames.values()) {
140
- ioFields.reserve(name);
141
- }
142
- const ioLines = [
162
+ const ioFields = new Names(symbols.blockNames.values());
163
+ const header = [
143
164
  "{attribute 'linkalways'}",
144
165
  "{attribute 'qualified_only'}",
145
166
  "VAR_GLOBAL",
146
167
  ];
147
- for (const item of core.variables.filter(
148
- (item) => item.storage === "input" || item.storage === "output",
149
- )) {
168
+ const list = listLines(ioListId, header);
169
+ for (const item of core.variables.filter(isLogicalIo)) {
150
170
  const field = ioFields.allocate(item.name);
151
171
  const initial = item.initial ?? defaultValue(item.type, typeMap);
152
172
  const binding = required(options.io[item.name]);
153
- references.set(item.id, `${ioName}.${field}`);
173
+ const declaration = ` ${field} : ${targetTypes.name(item.type)} := ${targetTypes.literal(initial, item.type)};`;
174
+ ioReferences.set(item.id, `${ioName}.${field}`);
154
175
  if (binding.mapped) {
155
176
  ioMappings.push({
156
177
  key: item.name,
157
178
  address: binding.address,
158
179
  variable: `${ioName}.${field}`,
159
180
  });
160
- mappings.push({
161
- path: `${ioName}.st`,
162
- line: ioLines.length + 2,
163
- span: item.span,
164
- });
165
- ioLines.push(
166
- " {attribute 'symbol' := 'read'}",
167
- ` ${field} : ${targetTypes.name(item.type)} := ${targetTypes.literal(initial, item.type)};`,
168
- );
181
+ list.write(" {attribute 'symbol' := 'read'}");
182
+ list.write(declaration, item.span);
169
183
  continue;
170
184
  }
171
185
  const wire = ioFields.allocate(`${item.name}_physical`);
@@ -177,21 +191,18 @@ export function emitIoList(
177
191
  wire: `${ioName}.${wire}`,
178
192
  inverted: binding.inverted,
179
193
  });
180
- mappings.push(
181
- { path: `${ioName}.st`, line: ioLines.length + 1, span: item.span },
182
- { path: `${ioName}.st`, line: ioLines.length + 3, span: item.span },
183
- );
184
- ioLines.push(
194
+ list.write(
185
195
  ` ${wire} AT ${binding.address} : ${targetTypes.name(item.type)};`,
186
- // Publish the scan's logical value for monitoring. Physical wires stay
187
- // private; external writes cannot alter the controller's IO snapshot.
188
- " {attribute 'symbol' := 'read'}",
189
- ` ${field} : ${targetTypes.name(item.type)} := ${targetTypes.literal(initial, item.type)};`,
196
+ item.span,
190
197
  );
198
+ // Publish the scan's logical value for monitoring. Physical wires stay
199
+ // private; external writes cannot alter the controller's IO snapshot.
200
+ list.write(" {attribute 'symbol' := 'read'}");
201
+ list.write(declaration, item.span);
191
202
  }
192
203
  for (const { key, root, segments, type } of plan.storageBindings) {
193
204
  const { address, mapped, inverted } = required(options.io[key]);
194
- const target = bindingTarget(symbols, { root, segments });
205
+ const target = bindingTarget(symbols, references, { root, segments });
195
206
  if (mapped) {
196
207
  ioMappings.push({ key, address, variable: target });
197
208
  continue;
@@ -200,53 +211,59 @@ export function emitIoList(
200
211
  `${key.replace(/\W+/g, "_").replace(/_+$/, "")}_physical`,
201
212
  );
202
213
  // The wire takes the target's own type, so equal-width EDS types need no conversion.
203
- ioLines.push(` ${wire} AT ${address} : ${targetTypes.name(type.id)};`);
214
+ list.write(` ${wire} AT ${address} : ${targetTypes.name(type.id)};`);
204
215
  channels.push({
205
216
  key,
206
217
  root: root.id,
207
- input: address[1] === "I",
218
+ input: !isOutputAddress(address),
208
219
  target,
209
220
  wire: `${ioName}.${wire}`,
210
221
  inverted,
211
222
  });
212
223
  }
213
- ioLines.push("END_VAR");
214
224
  // Mapped storage bindings need no IO list; mapped logical IO is declared here.
215
- if (ioLines.length > 4) {
216
- objects.push({
217
- id: "io",
218
- kind: "globalList",
219
- name: ioName,
220
- declaration: `${ioLines.join("\n")}\n`,
221
- });
222
- }
223
- return { objects, mappings, channels, ioMappings };
225
+ const declares = list.lines.length > header.length;
226
+ list.write("END_VAR");
227
+ return {
228
+ objects: declares
229
+ ? [
230
+ {
231
+ id: ioListId,
232
+ kind: "globalList",
233
+ name: ioName,
234
+ declaration: `${list.lines.join("\n")}\n`,
235
+ },
236
+ ]
237
+ : [],
238
+ mappings: list.mappings,
239
+ channels,
240
+ ioMappings,
241
+ references: ioReferences,
242
+ };
224
243
  }
225
244
 
226
245
  /** The target symbol of an `io` storage path. */
227
246
  function bindingTarget(
228
- symbols: CodesysSymbols,
247
+ { targetTypes }: GlobalSymbols,
248
+ references: ReadonlyMap<string, string>,
229
249
  { root, segments }: Pick<StorageBinding, "root" | "segments">,
230
250
  ): string {
231
251
  return segments.reduce(
232
252
  (path, segment) =>
233
253
  segment.kind === "field"
234
- ? `${path}.${symbols.targetTypes.field(segment.record, segment.name)}`
254
+ ? `${path}.${targetTypes.field(segment.record, segment.name)}`
235
255
  : `${path}[${segment.value}]`,
236
- required(symbols.references.get(root.id)),
256
+ required(references.get(root.id)),
237
257
  );
238
258
  }
239
259
 
240
260
  /** The target symbol and type that hold an `io` key's value, when this program has it. */
241
261
  export function ioTarget(
242
- { core }: CodesysPlan,
243
- symbols: CodesysSymbols,
262
+ { plan: { core }, symbols }: EmissionContext,
244
263
  key: string,
245
264
  ): { target: string; type: string } | undefined {
246
265
  const logical = core.variables.find(
247
- (variable) =>
248
- (variable.storage === "input" || variable.storage === "output") &&
249
- variable.name === key,
266
+ (variable) => isLogicalIo(variable) && variable.name === key,
250
267
  );
251
268
  if (logical) {
252
269
  return {
@@ -260,5 +277,8 @@ export function ioTarget(
260
277
  const resolved = resolveStoragePath(core, key);
261
278
  return "error" in resolved
262
279
  ? undefined
263
- : { target: bindingTarget(symbols, resolved), type: resolved.type.id };
280
+ : {
281
+ target: bindingTarget(symbols, symbols.references, resolved),
282
+ type: resolved.type.id,
283
+ };
264
284
  }
@@ -1,18 +1,19 @@
1
- import { standardBlocks, type VerifiedCoreModule } from "@tsplc/compiler/ir";
2
- import { Names, pascalCase } from "./names";
1
+ import {
2
+ coreTables,
3
+ pascalCase,
4
+ standardBlocks,
5
+ type VerifiedCoreModule,
6
+ } from "@tsplc/compiler/ir";
7
+ import { Names } from "./names";
3
8
  import { storageLayout } from "./storage";
4
9
  import { CodesysTypes } from "./types";
5
10
 
6
11
  export const planGlobalNames = (
7
12
  core: VerifiedCoreModule,
8
- programNames: Readonly<Record<string, string>> = {},
13
+ programNames: ReadonlyMap<string, string>,
9
14
  ) => {
10
- const names = new Names();
11
- for (const name of Object.values(programNames)) {
12
- names.reserve(name);
13
- }
14
-
15
- const typeMap = new Map(core.types.map((type) => [type.id, type]));
15
+ const names = new Names(programNames.values());
16
+ const typeMap = coreTables(core).types;
16
17
  const blockNames = new Map(
17
18
  core.blocks.map((block) => [
18
19
  block.id,
@@ -28,7 +29,7 @@ export const planGlobalNames = (
28
29
 
29
30
  const { globalLists, retainedVariables } = storageLayout(core);
30
31
  const hasExports = (group: (typeof globalLists)[number]) =>
31
- group.variables.some((variable) => variable.exported && !variable.exposure);
32
+ group.variables.some((variable) => variable.exported);
32
33
  const globals = globalLists.map((group) => {
33
34
  const splitByStorage = globalLists.some(
34
35
  (other) =>
@@ -44,10 +45,7 @@ export const planGlobalNames = (
44
45
  ),
45
46
  )
46
47
  : names.allocateObject("GVL", `${group.module}_${group.kind}`);
47
- const fields = new Names();
48
- for (const name of blockNames.values()) {
49
- fields.reserve(name);
50
- }
48
+ const fields = new Names(blockNames.values());
51
49
  return {
52
50
  ...group,
53
51
  name,