@tsplc/tmp-compiler 0.0.0-stage → 0.6.2

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 (184) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +41 -2
  3. package/dist/cli.js +2325 -0
  4. package/dist/cli.js.map +26 -0
  5. package/dist/clock.js +11 -0
  6. package/dist/clock.js.map +11 -0
  7. package/dist/codesys.js +6558 -0
  8. package/dist/codesys.js.map +51 -0
  9. package/dist/collections.js +399 -0
  10. package/dist/collections.js.map +10 -0
  11. package/dist/index.js +16076 -0
  12. package/dist/index.js.map +61 -0
  13. package/dist/ir.js +3948 -0
  14. package/dist/ir.js.map +35 -0
  15. package/dist/retain.js +10 -0
  16. package/dist/retain.js.map +10 -0
  17. package/dist/signals.js +115 -0
  18. package/dist/signals.js.map +10 -0
  19. package/dist/telemetry.js +121 -0
  20. package/dist/telemetry.js.map +12 -0
  21. package/package.json +122 -4
  22. package/src/codesys/config.ts +50 -0
  23. package/src/codesys/debug.ts +655 -0
  24. package/src/codesys/emit.ts +185 -0
  25. package/src/codesys/endpoints.ts +111 -0
  26. package/src/codesys/engineering.ts +96 -0
  27. package/src/codesys/explain/expressions.ts +445 -0
  28. package/src/codesys/explain/flags.ts +134 -0
  29. package/src/codesys/explain/parameters.ts +53 -0
  30. package/src/codesys/explain/rows.ts +182 -0
  31. package/src/codesys/explain/tables.ts +79 -0
  32. package/src/codesys/explain/tasks.ts +68 -0
  33. package/src/codesys/explain/writes.ts +497 -0
  34. package/src/codesys/explain.ts +149 -0
  35. package/src/codesys/expressions.ts +100 -0
  36. package/src/codesys/global-lists.ts +284 -0
  37. package/src/codesys/global-names.ts +66 -0
  38. package/src/codesys/http.ts +1078 -0
  39. package/src/codesys/index.ts +13 -0
  40. package/src/codesys/instrumentation.ts +574 -0
  41. package/src/codesys/io-paths.ts +149 -0
  42. package/src/codesys/loops.ts +101 -0
  43. package/src/codesys/manifest.ts +684 -0
  44. package/src/codesys/names.ts +58 -0
  45. package/src/codesys/numeric.ts +516 -0
  46. package/src/codesys/plan.ts +688 -0
  47. package/src/codesys/programs.ts +138 -0
  48. package/src/codesys/publication.ts +99 -0
  49. package/src/codesys/required.ts +8 -0
  50. package/src/codesys/routines.ts +247 -0
  51. package/src/codesys/scan-clock.ts +103 -0
  52. package/src/codesys/scheduling.ts +96 -0
  53. package/src/codesys/service-channels.ts +24 -0
  54. package/src/codesys/sources.ts +118 -0
  55. package/src/codesys/st-files.ts +219 -0
  56. package/src/codesys/statements.ts +409 -0
  57. package/src/codesys/storage.ts +150 -0
  58. package/src/codesys/symbols.ts +240 -0
  59. package/src/codesys/task-access.ts +186 -0
  60. package/src/codesys/telemetry-settings.ts +19 -0
  61. package/src/codesys/telemetry.ts +852 -0
  62. package/src/codesys/types.ts +310 -0
  63. package/src/codesys/vendor.ts +113 -0
  64. package/src/frontend/analyzer.ts +779 -0
  65. package/src/frontend/async.ts +530 -0
  66. package/src/frontend/blocks.ts +578 -0
  67. package/src/frontend/configuration.ts +83 -0
  68. package/src/frontend/const-functions.ts +204 -0
  69. package/src/frontend/control-flow.ts +787 -0
  70. package/src/frontend/cooperative.ts +114 -0
  71. package/src/frontend/coroutines.ts +1336 -0
  72. package/src/frontend/dialect-names.ts +104 -0
  73. package/src/frontend/elaborate/array-searches.ts +224 -0
  74. package/src/frontend/elaborate/assignments.ts +343 -0
  75. package/src/frontend/elaborate/calls.ts +1342 -0
  76. package/src/frontend/elaborate/capture-protocol.ts +237 -0
  77. package/src/frontend/elaborate/capture.ts +388 -0
  78. package/src/frontend/elaborate/declarations.ts +124 -0
  79. package/src/frontend/elaborate/dialect.ts +473 -0
  80. package/src/frontend/elaborate/expressions.ts +2171 -0
  81. package/src/frontend/elaborate/filtered-arrays.ts +643 -0
  82. package/src/frontend/elaborate/frame.ts +145 -0
  83. package/src/frontend/elaborate/generics.ts +123 -0
  84. package/src/frontend/elaborate/graph.ts +141 -0
  85. package/src/frontend/elaborate/imports.ts +105 -0
  86. package/src/frontend/elaborate/index.ts +851 -0
  87. package/src/frontend/elaborate/initializers.ts +235 -0
  88. package/src/frontend/elaborate/iterators.ts +1541 -0
  89. package/src/frontend/elaborate/layouts.ts +1025 -0
  90. package/src/frontend/elaborate/model.ts +478 -0
  91. package/src/frontend/elaborate/names.ts +134 -0
  92. package/src/frontend/elaborate/reduce.ts +900 -0
  93. package/src/frontend/elaborate/references.ts +727 -0
  94. package/src/frontend/elaborate/retention.ts +284 -0
  95. package/src/frontend/elaborate/specialize.ts +1137 -0
  96. package/src/frontend/elaborate/statements.ts +1220 -0
  97. package/src/frontend/elaborate/storage.ts +836 -0
  98. package/src/frontend/elaborate/time-constructors.ts +34 -0
  99. package/src/frontend/elaborate/values.ts +200 -0
  100. package/src/frontend/elaborate/writes.ts +415 -0
  101. package/src/frontend/expressions.ts +1382 -0
  102. package/src/frontend/generated-names.ts +95 -0
  103. package/src/frontend/intrinsics.ts +382 -0
  104. package/src/frontend/lowering-key.ts +23 -0
  105. package/src/frontend/module-graph.ts +156 -0
  106. package/src/frontend/positions.ts +359 -0
  107. package/src/frontend/promise-chains.ts +872 -0
  108. package/src/frontend/promises.ts +419 -0
  109. package/src/frontend/routines.ts +239 -0
  110. package/src/frontend/sdk.ts +187 -0
  111. package/src/frontend/services.ts +609 -0
  112. package/src/frontend/statements.ts +871 -0
  113. package/src/frontend/tasks.ts +526 -0
  114. package/src/frontend/telemetry.ts +174 -0
  115. package/src/frontend/types.ts +1073 -0
  116. package/src/frontend/typescript.ts +651 -0
  117. package/src/index.ts +9 -0
  118. package/src/ir/aggregates.ts +291 -0
  119. package/src/ir/allocation.ts +96 -0
  120. package/src/ir/conditions.ts +855 -0
  121. package/src/ir/core-model.ts +289 -0
  122. package/src/ir/declarations.ts +281 -0
  123. package/src/ir/diagnostics.ts +68 -0
  124. package/src/ir/domains.ts +151 -0
  125. package/src/ir/fold.ts +279 -0
  126. package/src/ir/identity.ts +19 -0
  127. package/src/ir/index.ts +137 -0
  128. package/src/ir/job-errors.ts +69 -0
  129. package/src/ir/layouts.ts +211 -0
  130. package/src/ir/link.ts +302 -0
  131. package/src/ir/model.ts +394 -0
  132. package/src/ir/normalization-error.ts +10 -0
  133. package/src/ir/normalize.ts +52 -0
  134. package/src/ir/numeric.ts +163 -0
  135. package/src/ir/operands.ts +160 -0
  136. package/src/ir/ranges.ts +420 -0
  137. package/src/ir/routine.ts +1116 -0
  138. package/src/ir/scalars.ts +453 -0
  139. package/src/ir/share.ts +790 -0
  140. package/src/ir/short-circuit.ts +140 -0
  141. package/src/ir/tables.ts +111 -0
  142. package/src/ir/temporaries.ts +138 -0
  143. package/src/ir/types.ts +151 -0
  144. package/src/project/artifacts.ts +424 -0
  145. package/src/project/cli.ts +211 -0
  146. package/src/project/debugger.ts +111 -0
  147. package/src/project/deployments.ts +387 -0
  148. package/src/project/flags.ts +14 -0
  149. package/src/project/git.ts +35 -0
  150. package/src/project/plant.ts +629 -0
  151. package/src/project/project-commands.ts +507 -0
  152. package/src/project/project.ts +559 -0
  153. package/src/project/release.ts +152 -0
  154. package/src/project/server-check.ts +54 -0
  155. package/src/project/server.ts +165 -0
  156. package/src/project/terminal.ts +45 -0
  157. package/src/runtime/async.ts +207 -0
  158. package/src/runtime/launch.ts +59 -0
  159. package/src/runtime/main.ts +59 -0
  160. package/src/runtime/missing-file.ts +3 -0
  161. package/src/runtime/process.ts +177 -0
  162. package/src/runtime/program.ts +109 -0
  163. package/src/runtime/retain.ts +108 -0
  164. package/src/runtime/runtime.ts +324 -0
  165. package/src/runtime/settings.ts +27 -0
  166. package/src/runtime/storage.ts +456 -0
  167. package/src/runtime/symbols.ts +97 -0
  168. package/src/runtime/telemetry.ts +154 -0
  169. package/src/runtime/transport.ts +168 -0
  170. package/stdlib/clock.ts +8 -0
  171. package/stdlib/collections.ts +534 -0
  172. package/stdlib/plc-time.ts +2 -0
  173. package/stdlib/retain.ts +4 -0
  174. package/stdlib/signals.ts +142 -0
  175. package/stdlib/telemetry.ts +89 -0
  176. package/types/async.d.ts +87 -0
  177. package/types/clock.d.ts +8 -0
  178. package/types/config.d.ts +2 -0
  179. package/types/data-types.d.ts +31 -0
  180. package/types/dialect.d.ts +49 -0
  181. package/types/globals.d.ts +10 -0
  182. package/types/index.d.ts +212 -0
  183. package/types/retain.d.ts +12 -0
  184. package/types/telemetry.d.ts +47 -0
@@ -0,0 +1,100 @@
1
+ import type { CoreExpression, CorePath, CoreTables } from "@tsplc/compiler/ir";
2
+ import { required } from "./required";
3
+ import type { CodesysSymbols } from "./symbols";
4
+
5
+ /** The storage a routine's ST can name (module storage, IO and its own variables), and each variable's declaration. */
6
+ export interface StorageScope {
7
+ symbols: CodesysSymbols;
8
+ names: ReadonlyMap<string, string>;
9
+ variableOf: CoreTables["variableOf"];
10
+ }
11
+
12
+ export const storageName = ({ names }: StorageScope, id: string): string => {
13
+ const value = names.get(id);
14
+ if (!value) {
15
+ throw new Error(`Missing verified storage ${id}.`);
16
+ }
17
+ return value;
18
+ };
19
+
20
+ export const placeText = (
21
+ scope: StorageScope,
22
+ id: string,
23
+ path: CorePath = [],
24
+ ): string => {
25
+ const { typeMap, blocks, blockFields, targetTypes } = scope.symbols;
26
+ const place = (
27
+ text: string,
28
+ typeId: string,
29
+ [segment, ...rest]: CorePath,
30
+ ): string => {
31
+ if (!segment) {
32
+ return text;
33
+ }
34
+ const type = required(typeMap.get(typeId));
35
+ // Block output records are a source abstraction; target FB fields are direct.
36
+ if (
37
+ segment.kind === "field" &&
38
+ type.kind === "block" &&
39
+ segment.name === "out"
40
+ ) {
41
+ const [field, ...after] = rest;
42
+ if (!field) {
43
+ return text;
44
+ }
45
+ const output = required(
46
+ required(blocks.get(type.block)).outputs.find(
47
+ (item) => field.kind === "field" && item.name === field.name,
48
+ ),
49
+ );
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") {
64
+ // A strict enum converts explicitly to its DINT base, as HTTP JSON encoding does.
65
+ const index =
66
+ typeMap.get(segment.value.type)?.kind === "enum"
67
+ ? `TO_DINT(${expressionText(scope, segment.value)})`
68
+ : expressionText(scope, segment.value);
69
+ return place(`${text}[${index}]`, type.element, rest);
70
+ }
71
+ throw new Error("Invalid verified place");
72
+ };
73
+ return place(storageName(scope, id), scope.variableOf(id).type, path);
74
+ };
75
+
76
+ export const expressionText = (
77
+ scope: StorageScope,
78
+ value: CoreExpression,
79
+ ): string => {
80
+ const { typeMap, targetTypes, numeric } = scope.symbols;
81
+ if (value.kind === "constant") {
82
+ return targetTypes.literal(value.value, value.type);
83
+ }
84
+ if (value.kind === "load") {
85
+ return placeText(scope, value.root, value.path);
86
+ }
87
+ if (value.kind === "borrow") {
88
+ return placeText(scope, value.place.root, value.place.path);
89
+ }
90
+ if (value.kind === "convert" && typeMap.get(value.type)?.kind === "string") {
91
+ return expressionText(scope, value.operand);
92
+ }
93
+ if (value.kind === "unary" || value.kind === "convert") {
94
+ return numeric.render(value, [expressionText(scope, value.operand)]);
95
+ }
96
+ return numeric.render(value, [
97
+ expressionText(scope, value.left),
98
+ expressionText(scope, value.right),
99
+ ]);
100
+ };
@@ -0,0 +1,284 @@
1
+ import {
2
+ defaultValue,
3
+ type GeneratedObject,
4
+ instancedBlock,
5
+ isLogicalIo,
6
+ type SourceSpan,
7
+ type Variable,
8
+ } from "@tsplc/compiler/ir";
9
+ import { isOutputAddress } from "@tsplc/typegen/hardware";
10
+ import {
11
+ isStoragePath,
12
+ resolveStoragePath,
13
+ type StorageBinding,
14
+ } from "./io-paths";
15
+ import { Names } from "./names";
16
+ import type { CodesysPlan } from "./plan";
17
+ import { required } from "./required";
18
+ import type { ObjectMapping } from "./st-files";
19
+ import type { GlobalStorage } from "./storage";
20
+ import type { EmissionContext, GlobalSymbols } from "./symbols";
21
+
22
+ const globalSections = {
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",
26
+ retained: "VAR_GLOBAL RETAIN",
27
+ state: "VAR_GLOBAL",
28
+ } as const satisfies Record<GlobalStorage, string>;
29
+
30
+ /** Each `io` binding: an AT wire copied to or from its logical target around the scan. */
31
+ export interface IoChannel {
32
+ key: string;
33
+ root: string;
34
+ input: boolean;
35
+ target: string;
36
+ wire: string;
37
+ inverted?: boolean;
38
+ }
39
+
40
+ /** A variable a generated device tree maps to its bit or byte channel. */
41
+ export interface IoMapping {
42
+ key: string;
43
+ address: string;
44
+ variable: string;
45
+ }
46
+
47
+ const globalInitializer = (
48
+ { typeMap, targetTypes }: GlobalSymbols,
49
+ storage: GlobalStorage,
50
+ item: Variable,
51
+ ): string => {
52
+ if (instancedBlock(item.type, typeMap)) {
53
+ return "";
54
+ }
55
+ const value = item.initial ?? defaultValue(item.type, typeMap);
56
+ // CONSTANT declarations always keep their initializer. Mutable zero
57
+ // aggregates use the same implicit initialization as the original ST.
58
+ return storage === "constants"
59
+ ? ` := ${targetTypes.literal(value, item.type)}`
60
+ : targetTypes.declarationInitializer(value, item.type);
61
+ };
62
+
63
+ /** Whether the build target publishes a variable, which clients may then read and write. */
64
+ export const isPublished = (
65
+ plan: CodesysPlan,
66
+ ): ((variable: Variable) => boolean) => {
67
+ const published = new Set(plan.publication[plan.options.buildTarget]);
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
+ },
90
+ };
91
+ };
92
+
93
+ /** The global lists of module storage, and the global symbol of each variable they declare. */
94
+ export function emitGlobalLists(
95
+ plan: CodesysPlan,
96
+ symbols: GlobalSymbols,
97
+ ): {
98
+ objects: GeneratedObject[];
99
+ mappings: ObjectMapping[];
100
+ references: Map<string, string>;
101
+ } {
102
+ const { globals, targetTypes } = symbols;
103
+ const objects: GeneratedObject[] = [];
104
+ const mappings: ObjectMapping[] = [];
105
+ const references = new Map<string, string>();
106
+ const published = isPublished(plan);
107
+ for (const group of globals) {
108
+ const { name, fields } = group;
109
+ const publish = group.variables.some(published);
110
+ const list = listLines(group.id, [
111
+ ...(publish ? ["{attribute 'linkalways'}"] : []),
112
+ "{attribute 'qualified_only'}",
113
+ ]);
114
+ for (const section of group.sections) {
115
+ list.write(globalSections[section.storage]);
116
+ for (const item of section.variables) {
117
+ const field = required(fields.get(item.id));
118
+ references.set(item.id, `${name}.${field}`);
119
+ if (published(item)) {
120
+ list.write(" {attribute 'symbol' := 'readwrite'}");
121
+ }
122
+ const initial = globalInitializer(symbols, section.storage, item);
123
+ list.write(
124
+ ` ${field} : ${targetTypes.name(item.type)}${initial};`,
125
+ item.span,
126
+ );
127
+ }
128
+ list.write("END_VAR");
129
+ }
130
+ objects.push({
131
+ id: group.id,
132
+ kind: "globalList",
133
+ name,
134
+ declaration: `${list.lines.join("\n")}\n`,
135
+ });
136
+ mappings.push(...list.mappings);
137
+ }
138
+ return { objects, mappings, references };
139
+ }
140
+
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. */
145
+ export function emitIoList(
146
+ plan: CodesysPlan,
147
+ symbols: GlobalSymbols,
148
+ references: ReadonlyMap<string, string>,
149
+ ): {
150
+ objects: GeneratedObject[];
151
+ mappings: ObjectMapping[];
152
+ channels: IoChannel[];
153
+ ioMappings: IoMapping[];
154
+ references: Map<string, string>;
155
+ } {
156
+ const { core, options } = plan;
157
+ const { names, targetTypes, typeMap } = symbols;
158
+ const channels: IoChannel[] = [];
159
+ const ioMappings: IoMapping[] = [];
160
+ const ioReferences = new Map<string, string>();
161
+ const ioName = names.allocate("GVL_IO");
162
+ const ioFields = new Names(symbols.blockNames.values());
163
+ const header = [
164
+ "{attribute 'linkalways'}",
165
+ "{attribute 'qualified_only'}",
166
+ "VAR_GLOBAL",
167
+ ];
168
+ const list = listLines(ioListId, header);
169
+ for (const item of core.variables.filter(isLogicalIo)) {
170
+ const field = ioFields.allocate(item.name);
171
+ const initial = item.initial ?? defaultValue(item.type, typeMap);
172
+ const binding = required(options.io[item.name]);
173
+ const declaration = ` ${field} : ${targetTypes.name(item.type)} := ${targetTypes.literal(initial, item.type)};`;
174
+ ioReferences.set(item.id, `${ioName}.${field}`);
175
+ if (binding.mapped) {
176
+ ioMappings.push({
177
+ key: item.name,
178
+ address: binding.address,
179
+ variable: `${ioName}.${field}`,
180
+ });
181
+ list.write(" {attribute 'symbol' := 'read'}");
182
+ list.write(declaration, item.span);
183
+ continue;
184
+ }
185
+ const wire = ioFields.allocate(`${item.name}_physical`);
186
+ channels.push({
187
+ key: item.name,
188
+ root: item.id,
189
+ input: item.storage === "input",
190
+ target: `${ioName}.${field}`,
191
+ wire: `${ioName}.${wire}`,
192
+ inverted: binding.inverted,
193
+ });
194
+ list.write(
195
+ ` ${wire} AT ${binding.address} : ${targetTypes.name(item.type)};`,
196
+ item.span,
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);
202
+ }
203
+ for (const { key, root, segments, type } of plan.storageBindings) {
204
+ const { address, mapped, inverted } = required(options.io[key]);
205
+ const target = bindingTarget(symbols, references, { root, segments });
206
+ if (mapped) {
207
+ ioMappings.push({ key, address, variable: target });
208
+ continue;
209
+ }
210
+ const wire = ioFields.allocate(
211
+ `${key.replace(/\W+/g, "_").replace(/_+$/, "")}_physical`,
212
+ );
213
+ // The wire takes the target's own type, so equal-width EDS types need no conversion.
214
+ list.write(` ${wire} AT ${address} : ${targetTypes.name(type.id)};`);
215
+ channels.push({
216
+ key,
217
+ root: root.id,
218
+ input: !isOutputAddress(address),
219
+ target,
220
+ wire: `${ioName}.${wire}`,
221
+ inverted,
222
+ });
223
+ }
224
+ // Mapped storage bindings need no IO list; mapped logical IO is declared here.
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
+ };
243
+ }
244
+
245
+ /** The target symbol of an `io` storage path. */
246
+ function bindingTarget(
247
+ { targetTypes }: GlobalSymbols,
248
+ references: ReadonlyMap<string, string>,
249
+ { root, segments }: Pick<StorageBinding, "root" | "segments">,
250
+ ): string {
251
+ return segments.reduce(
252
+ (path, segment) =>
253
+ segment.kind === "field"
254
+ ? `${path}.${targetTypes.field(segment.record, segment.name)}`
255
+ : `${path}[${segment.value}]`,
256
+ required(references.get(root.id)),
257
+ );
258
+ }
259
+
260
+ /** The target symbol and type that hold an `io` key's value, when this program has it. */
261
+ export function ioTarget(
262
+ { plan: { core }, symbols }: EmissionContext,
263
+ key: string,
264
+ ): { target: string; type: string } | undefined {
265
+ const logical = core.variables.find(
266
+ (variable) => isLogicalIo(variable) && variable.name === key,
267
+ );
268
+ if (logical) {
269
+ return {
270
+ target: required(symbols.references.get(logical.id)),
271
+ type: logical.type,
272
+ };
273
+ }
274
+ if (!isStoragePath(key)) {
275
+ return undefined;
276
+ }
277
+ const resolved = resolveStoragePath(core, key);
278
+ return "error" in resolved
279
+ ? undefined
280
+ : {
281
+ target: bindingTarget(symbols, symbols.references, resolved),
282
+ type: resolved.type.id,
283
+ };
284
+ }
@@ -0,0 +1,66 @@
1
+ import {
2
+ coreTables,
3
+ pascalCase,
4
+ standardBlocks,
5
+ type VerifiedCoreModule,
6
+ } from "@tsplc/compiler/ir";
7
+ import { Names } from "./names";
8
+ import { storageLayout } from "./storage";
9
+ import { CodesysTypes } from "./types";
10
+
11
+ export const planGlobalNames = (
12
+ core: VerifiedCoreModule,
13
+ programNames: ReadonlyMap<string, string>,
14
+ ) => {
15
+ const names = new Names(programNames.values());
16
+ const typeMap = coreTables(core).types;
17
+ const blockNames = new Map(
18
+ core.blocks.map((block) => [
19
+ block.id,
20
+ block.standard
21
+ ? standardBlocks[block.standard].native
22
+ : names.allocateObject("FB", block.name),
23
+ ]),
24
+ );
25
+ for (const name of blockNames.values()) {
26
+ names.reserve(name);
27
+ }
28
+ const targetTypes = new CodesysTypes(typeMap, names, blockNames);
29
+
30
+ const { globalLists, retainedVariables } = storageLayout(core);
31
+ const hasExports = (group: (typeof globalLists)[number]) =>
32
+ group.variables.some((variable) => variable.exported);
33
+ const globals = globalLists.map((group) => {
34
+ const splitByStorage = globalLists.some(
35
+ (other) =>
36
+ other.module === group.module &&
37
+ other.kind !== group.kind &&
38
+ hasExports(other),
39
+ );
40
+ // Exported lists are the program's public interface. Private storage stays generated.
41
+ const name = hasExports(group)
42
+ ? names.allocate(
43
+ pascalCase(
44
+ splitByStorage ? `${group.module}-${group.kind}` : group.module,
45
+ ),
46
+ )
47
+ : names.allocateObject("GVL", `${group.module}_${group.kind}`);
48
+ const fields = new Names(blockNames.values());
49
+ return {
50
+ ...group,
51
+ name,
52
+ fields: new Map(
53
+ group.variables.map((item) => [item.id, fields.allocate(item.name)]),
54
+ ),
55
+ };
56
+ });
57
+
58
+ return {
59
+ names,
60
+ typeMap,
61
+ blockNames,
62
+ targetTypes,
63
+ globals,
64
+ retainedVariables,
65
+ };
66
+ };