@tsplc/compiler 0.2.17 → 0.2.19

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 (157) hide show
  1. package/README.md +2 -2
  2. package/dist/cli.js +920 -943
  3. package/dist/cli.js.map +13 -10
  4. package/dist/codesys.js +3893 -3352
  5. package/dist/codesys.js.map +29 -20
  6. package/dist/elaborate-capture.js +33 -17
  7. package/dist/elaborate-capture.js.map +3 -3
  8. package/dist/index.js +8787 -6855
  9. package/dist/index.js.map +40 -20
  10. package/dist/ir.js +1575 -1072
  11. package/dist/ir.js.map +21 -17
  12. package/dist/math.js.map +2 -2
  13. package/dist/project.js +154 -60
  14. package/dist/project.js.map +5 -5
  15. package/dist/telemetry.js +27 -9
  16. package/dist/telemetry.js.map +5 -5
  17. package/package.json +112 -112
  18. package/src/codesys/config.ts +50 -0
  19. package/src/codesys/debug.ts +670 -0
  20. package/src/codesys/emit.ts +142 -0
  21. package/src/codesys/engineering.ts +109 -0
  22. package/src/codesys/expressions.ts +92 -0
  23. package/src/codesys/global-lists.ts +250 -0
  24. package/src/codesys/global-names.ts +68 -0
  25. package/src/codesys/http.ts +890 -0
  26. package/src/codesys/index.ts +30 -0
  27. package/src/codesys/instrumentation.ts +430 -0
  28. package/src/codesys/io-paths.ts +140 -0
  29. package/src/codesys/manifest.ts +643 -0
  30. package/src/codesys/names.ts +81 -0
  31. package/src/codesys/numeric.ts +409 -0
  32. package/src/codesys/operands.ts +62 -0
  33. package/src/codesys/plan.ts +605 -0
  34. package/src/codesys/programs.ts +53 -0
  35. package/src/codesys/publication.ts +171 -0
  36. package/src/codesys/routines.ts +603 -0
  37. package/src/codesys/sources.ts +71 -0
  38. package/src/codesys/st-files.ts +115 -0
  39. package/src/codesys/storage.ts +97 -0
  40. package/src/codesys/symbols.ts +220 -0
  41. package/src/codesys/task-access.ts +320 -0
  42. package/src/codesys/telemetry.ts +732 -0
  43. package/src/codesys/types.ts +250 -0
  44. package/src/codesys/vendor.ts +150 -0
  45. package/src/frontend/analyzer.ts +794 -0
  46. package/src/frontend/async.ts +317 -1137
  47. package/src/frontend/blocks.ts +537 -403
  48. package/src/frontend/configuration.ts +176 -144
  49. package/src/frontend/const-functions.ts +183 -148
  50. package/src/frontend/cooperative.ts +73 -43
  51. package/src/frontend/coroutines.ts +1032 -0
  52. package/src/frontend/elaborate/calls.ts +535 -0
  53. package/src/frontend/elaborate/capture.ts +157 -101
  54. package/src/frontend/elaborate/dialect.ts +257 -0
  55. package/src/frontend/elaborate/expressions.ts +992 -0
  56. package/src/frontend/elaborate/frame.ts +53 -0
  57. package/src/frontend/elaborate/graph.ts +116 -71
  58. package/src/frontend/elaborate/imports.ts +70 -0
  59. package/src/frontend/elaborate/index.ts +435 -331
  60. package/src/frontend/elaborate/initializers.ts +198 -0
  61. package/src/frontend/elaborate/layouts.ts +591 -384
  62. package/src/frontend/elaborate/model.ts +264 -169
  63. package/src/frontend/elaborate/specialize.ts +343 -2586
  64. package/src/frontend/elaborate/statements.ts +606 -0
  65. package/src/frontend/elaborate/storage.ts +546 -0
  66. package/src/frontend/elaborate/values.ts +58 -0
  67. package/src/frontend/elaborate/writes.ts +100 -0
  68. package/src/frontend/exposure.ts +172 -140
  69. package/src/frontend/expressions.ts +1383 -0
  70. package/src/frontend/intrinsics.ts +337 -0
  71. package/src/frontend/module-graph.ts +89 -59
  72. package/src/frontend/positions.ts +271 -200
  73. package/src/frontend/promise-chains.ts +897 -0
  74. package/src/frontend/promises.ts +390 -747
  75. package/src/frontend/routines.ts +181 -0
  76. package/src/frontend/sdk.ts +82 -0
  77. package/src/frontend/services.ts +506 -372
  78. package/src/frontend/statements.ts +855 -0
  79. package/src/frontend/tasks.ts +471 -0
  80. package/src/frontend/telemetry.ts +174 -0
  81. package/src/frontend/types.ts +845 -551
  82. package/src/frontend/typescript.ts +323 -2543
  83. package/src/index.ts +5 -5
  84. package/src/ir/aggregates.ts +275 -0
  85. package/src/ir/allocation.ts +84 -0
  86. package/src/ir/core-model.ts +146 -0
  87. package/src/ir/declarations.ts +295 -0
  88. package/src/ir/diagnostics.ts +61 -0
  89. package/src/ir/domains.ts +152 -0
  90. package/src/ir/fold.ts +342 -0
  91. package/src/ir/index.ts +90 -0
  92. package/{ir/errors.ts → src/ir/job-errors.ts} +12 -10
  93. package/src/ir/layouts.ts +181 -0
  94. package/src/ir/link.ts +354 -0
  95. package/src/ir/model.ts +386 -0
  96. package/{ir → src/ir}/normalization-error.ts +4 -4
  97. package/src/ir/normalize.ts +70 -0
  98. package/src/ir/numeric.ts +171 -0
  99. package/src/ir/ranges.ts +256 -0
  100. package/src/ir/routine.ts +1163 -0
  101. package/src/ir/scalars.ts +321 -0
  102. package/src/ir/types.ts +127 -0
  103. package/src/project/artifacts.ts +342 -0
  104. package/src/project/cli.ts +177 -0
  105. package/src/project/debugger.ts +82 -0
  106. package/src/project/index.ts +6 -0
  107. package/src/project/plant.ts +467 -0
  108. package/src/project/project-commands.ts +372 -0
  109. package/src/project/project.ts +457 -0
  110. package/src/project/release.ts +153 -0
  111. package/src/project/server.ts +150 -0
  112. package/src/project/terminal.ts +32 -0
  113. package/stdlib/math.ts +14 -10
  114. package/stdlib/telemetry.ts +71 -47
  115. package/types/async.d.ts +46 -40
  116. package/types/data-types.d.ts +26 -26
  117. package/types/globals.d.ts +41 -38
  118. package/types/index.d.ts +96 -51
  119. package/types/plc.d.ts +3 -4
  120. package/types/telemetry.d.ts +14 -14
  121. package/backends/codesys/config.ts +0 -54
  122. package/backends/codesys/debug.ts +0 -509
  123. package/backends/codesys/emit.ts +0 -1738
  124. package/backends/codesys/engineering.ts +0 -88
  125. package/backends/codesys/global-names.ts +0 -49
  126. package/backends/codesys/http.ts +0 -675
  127. package/backends/codesys/index.ts +0 -13
  128. package/backends/codesys/io-paths.ts +0 -105
  129. package/backends/codesys/manifest.ts +0 -312
  130. package/backends/codesys/names.ts +0 -76
  131. package/backends/codesys/numeric.ts +0 -356
  132. package/backends/codesys/plan.ts +0 -457
  133. package/backends/codesys/publication.ts +0 -59
  134. package/backends/codesys/storage.ts +0 -70
  135. package/backends/codesys/task-access.ts +0 -293
  136. package/backends/codesys/telemetry.ts +0 -593
  137. package/backends/codesys/types.ts +0 -179
  138. package/backends/codesys/vendor.ts +0 -108
  139. package/ir/aggregates.ts +0 -333
  140. package/ir/allocation.ts +0 -74
  141. package/ir/core-model.ts +0 -129
  142. package/ir/core.ts +0 -64
  143. package/ir/declarations.ts +0 -208
  144. package/ir/diagnostics.ts +0 -43
  145. package/ir/domains.ts +0 -96
  146. package/ir/index.ts +0 -8
  147. package/ir/link.ts +0 -185
  148. package/ir/model.ts +0 -346
  149. package/ir/routine.ts +0 -859
  150. package/ir/scalars.ts +0 -675
  151. package/ir/types.ts +0 -76
  152. package/project/artifacts.ts +0 -277
  153. package/project/cli.ts +0 -395
  154. package/project/index.ts +0 -1
  155. package/project/plant.ts +0 -509
  156. package/project/project.ts +0 -358
  157. package/project/server.ts +0 -113
package/src/ir/link.ts ADDED
@@ -0,0 +1,354 @@
1
+ import { telemetryOperands, type PlcModule } from "./model.ts";
2
+ import {
3
+ nestedBodies,
4
+ type CoreFunction,
5
+ type CoreFunctionBlock,
6
+ type CoreStatement,
7
+ } from "./core-model.ts";
8
+ import type { ModuleDeclarations } from "./declarations.ts";
9
+ import { reject } from "./normalization-error.ts";
10
+ import { instancedBlock, type PlcType } from "./types.ts";
11
+
12
+ type Edges = ReadonlyMap<string, ReadonlySet<string>>;
13
+ type Routine = CoreFunction | CoreFunctionBlock;
14
+
15
+ // Validate the complete authored call graph before pruning unreachable code.
16
+ // Static instances keep their child layouts even when a child has no scan call.
17
+ export function linkModule(
18
+ module: PlcModule,
19
+ declarations: ModuleDeclarations,
20
+ functions: CoreFunction[],
21
+ normalizedBlocks: CoreFunctionBlock[],
22
+ edges: Edges,
23
+ ) {
24
+ const entries = checkPrograms(module, declarations);
25
+ const routines = new Map<string, Routine>(
26
+ [...functions, ...normalizedBlocks].map((fn) => [fn.id, fn]),
27
+ );
28
+ checkRecursion(routines, edges, entries, declarations);
29
+ checkLoopCounterWrites(module, declarations, routines, edges);
30
+
31
+ // Only after the complete graph is valid may emission discard unused helpers.
32
+ const reachable = reachableRoutines(
33
+ module,
34
+ declarations,
35
+ functions,
36
+ entries,
37
+ edges,
38
+ );
39
+ const linkedFunctions = functions.filter((fn) => reachable.has(fn.id));
40
+ const linkedBlocks = normalizedBlocks.filter((block) =>
41
+ reachable.has(block.id),
42
+ );
43
+ const usedTypes = usedTypeIds(
44
+ module,
45
+ declarations.types,
46
+ linkedFunctions,
47
+ linkedBlocks,
48
+ );
49
+ return {
50
+ types: module.types.filter((type) => usedTypes.has(type.id)),
51
+ variables: module.variables,
52
+ functions: linkedFunctions,
53
+ blocks: linkedBlocks,
54
+ programs: module.programs,
55
+ ...(module.folded && { folded: module.folded }),
56
+ };
57
+ }
58
+
59
+ /** Checks each program's entry and name, and returns the entry routines. */
60
+ const checkPrograms = (
61
+ module: PlcModule,
62
+ declarations: ModuleDeclarations,
63
+ ): ReadonlySet<string> => {
64
+ const names = new Set<string>();
65
+ const entries = new Set<string>();
66
+ for (const program of module.programs) {
67
+ declarations.claimId(program.id);
68
+ const entry = declarations.signatures.get(program.entry);
69
+ if (!entry || entry.owner || entry.parameters.length || entry.result) {
70
+ reject("Program entry must be a parameterless void helper.");
71
+ }
72
+ if (names.has(program.name)) {
73
+ reject(`Duplicate program ${program.name}.`);
74
+ }
75
+ names.add(program.name);
76
+ entries.add(program.entry);
77
+ }
78
+ if (!module.programs.length) {
79
+ reject("At least one program is required.");
80
+ }
81
+ return entries;
82
+ };
83
+
84
+ // Authored execution cycles are invalid even in unreachable routines. Static
85
+ // allocation has its own cycle check because it does not depend on scan calls.
86
+ const checkRecursion = (
87
+ routines: ReadonlyMap<string, Routine>,
88
+ edges: Edges,
89
+ entries: ReadonlySet<string>,
90
+ { signatures }: ModuleDeclarations,
91
+ ): void => {
92
+ const visited = new Set<string>();
93
+ const active = new Set<string>();
94
+ const visit = (id: string): void => {
95
+ if (active.has(id)) {
96
+ reject("Recursive scan calls are prohibited.", signatures.get(id)?.span);
97
+ }
98
+ if (visited.has(id)) {
99
+ return;
100
+ }
101
+ active.add(id);
102
+ for (const child of edges.get(id) ?? []) {
103
+ if (entries.has(child)) {
104
+ reject(
105
+ "Program entries cannot be called as ordinary helpers.",
106
+ signatures.get(id)?.span,
107
+ );
108
+ }
109
+ visit(child);
110
+ }
111
+ active.delete(id);
112
+ visited.add(id);
113
+ };
114
+ for (const id of routines.keys()) {
115
+ visit(id);
116
+ }
117
+ };
118
+
119
+ // A bounded loop can only reconstruct its counter if called helpers leave
120
+ // that storage alone. Include transitive calls, not just direct assignments.
121
+ const checkLoopCounterWrites = (
122
+ module: PlcModule,
123
+ { types }: ModuleDeclarations,
124
+ routines: ReadonlyMap<string, Routine>,
125
+ edges: Edges,
126
+ ): void => {
127
+ const variables = new Map(
128
+ [
129
+ ...module.variables,
130
+ ...[...routines.values()].flatMap((routine) =>
131
+ "inputs" in routine
132
+ ? [
133
+ ...routine.inputs,
134
+ ...routine.outputs,
135
+ ...routine.state,
136
+ ...routine.locals,
137
+ ]
138
+ : [...routine.parameters, ...routine.locals],
139
+ ),
140
+ ].map((variable) => [variable.id, variable]),
141
+ );
142
+ const calledRoutine = (item: CoreStatement): string | undefined => {
143
+ if (item.kind === "invoke") {
144
+ return item.function;
145
+ }
146
+ if (item.kind !== "blockUpdate") {
147
+ return undefined;
148
+ }
149
+ const variable = variables.get(item.instance);
150
+ return variable && instancedBlock(variable.type, types)?.block;
151
+ };
152
+ const writes = new Map<string, Set<string>>();
153
+ const routineWrites = (id: string): Set<string> => {
154
+ const cached = writes.get(id);
155
+ if (cached) {
156
+ return cached;
157
+ }
158
+ const result = new Set<string>();
159
+ const inspect = (body: readonly CoreStatement[]) => {
160
+ for (const item of body) {
161
+ if (item.kind === "assign") {
162
+ result.add(item.root);
163
+ }
164
+ if (item.kind === "for") {
165
+ result.add(item.counter);
166
+ }
167
+ if (item.kind === "invoke") {
168
+ if (item.destination) {
169
+ result.add(item.destination);
170
+ }
171
+ for (const argument of item.arguments) {
172
+ if (argument.kind === "borrow") {
173
+ result.add(argument.place.root);
174
+ }
175
+ }
176
+ }
177
+ if (item.kind === "blockUpdate") {
178
+ for (const input of Object.values(item.inputs)) {
179
+ if (input.kind === "borrow") {
180
+ result.add(input.place.root);
181
+ }
182
+ }
183
+ }
184
+ for (const nested of nestedBodies(item)) {
185
+ inspect(nested);
186
+ }
187
+ }
188
+ };
189
+ // Calls resolve within the module's routines; routine lowering verified each callee.
190
+ inspect(routines.get(id)!.body);
191
+ for (const child of edges.get(id) ?? []) {
192
+ for (const root of routineWrites(child)) {
193
+ result.add(root);
194
+ }
195
+ }
196
+ writes.set(id, result);
197
+ return result;
198
+ };
199
+ const checkCalls = (
200
+ body: readonly CoreStatement[],
201
+ counters: ReadonlySet<string>,
202
+ ) => {
203
+ for (const item of body) {
204
+ const called = calledRoutine(item);
205
+ if (
206
+ called &&
207
+ [...counters].some((counter) => routineWrites(called).has(counter))
208
+ ) {
209
+ reject(
210
+ "A called routine may mutate an active loop counter.",
211
+ item.span,
212
+ );
213
+ }
214
+ const nestedCounters =
215
+ item.kind === "for" ? new Set([...counters, item.counter]) : counters;
216
+ for (const nested of nestedBodies(item)) {
217
+ checkCalls(nested, nestedCounters);
218
+ }
219
+ }
220
+ };
221
+ for (const routine of routines.values()) {
222
+ checkCalls(routine.body, new Set());
223
+ }
224
+ };
225
+
226
+ const reachableRoutines = (
227
+ module: PlcModule,
228
+ { signatures, blocks, types }: ModuleDeclarations,
229
+ functions: readonly CoreFunction[],
230
+ entries: ReadonlySet<string>,
231
+ edges: Edges,
232
+ ): ReadonlySet<string> => {
233
+ const reachable = new Set<string>();
234
+ const link = (id: string): void => {
235
+ if (reachable.has(id)) {
236
+ return;
237
+ }
238
+ reachable.add(id);
239
+ for (const child of edges.get(id) ?? []) {
240
+ link(child);
241
+ }
242
+ const owner = signatures.get(id)?.owner;
243
+ if (owner) {
244
+ link(owner.block);
245
+ }
246
+ for (const method of functions.filter((fn) => fn.owner?.block === id)) {
247
+ link(method.id);
248
+ }
249
+ for (const instance of blocks
250
+ .get(id)
251
+ ?.state.filter((variable) => variable.storage === "instance") ?? []) {
252
+ const type = instancedBlock(instance.type, types);
253
+ if (type) {
254
+ link(type.block);
255
+ }
256
+ }
257
+ };
258
+ for (const entry of entries) {
259
+ link(entry);
260
+ }
261
+ for (const variable of module.variables) {
262
+ const type = instancedBlock(variable.type, types);
263
+ if (type) {
264
+ link(type.block);
265
+ }
266
+ }
267
+ return reachable;
268
+ };
269
+
270
+ // Admission has already validated every referenced type; these lookups are
271
+ // closed over the declaration registry, including dependencies kept by DUTs.
272
+ const usedTypeIds = (
273
+ module: PlcModule,
274
+ types: ReadonlyMap<string, PlcType>,
275
+ linkedFunctions: readonly CoreFunction[],
276
+ linkedBlocks: readonly CoreFunctionBlock[],
277
+ ): ReadonlySet<string> => {
278
+ const usedTypes = new Set<string>();
279
+ const useType = (id: string): void => {
280
+ if (usedTypes.has(id)) {
281
+ return;
282
+ }
283
+ usedTypes.add(id);
284
+ const type = types.get(id)!;
285
+ if (type.kind === "record") {
286
+ for (const field of type.fields) {
287
+ useType(field.type);
288
+ }
289
+ }
290
+ if (type.kind === "array") {
291
+ useType(type.element);
292
+ if (type.index) {
293
+ useType(type.index);
294
+ }
295
+ }
296
+ if (type.kind === "enum") {
297
+ useType(type.base);
298
+ }
299
+ if (type.kind === "block" && type.output) {
300
+ useType(type.output);
301
+ }
302
+ };
303
+ // Primitive definitions also support backend-generated numeric helpers.
304
+ types.forEach((type) => {
305
+ if (
306
+ ["bool", "integer", "bits", "float", "duration", "calendar"].includes(
307
+ type.kind,
308
+ )
309
+ ) {
310
+ useType(type.id);
311
+ }
312
+ });
313
+ for (const variable of module.variables) {
314
+ useType(variable.type);
315
+ }
316
+ for (const fn of linkedFunctions) {
317
+ if (fn.result) {
318
+ useType(fn.result);
319
+ }
320
+ for (const variable of [...fn.parameters, ...fn.locals]) {
321
+ useType(variable.type);
322
+ }
323
+ }
324
+ for (const block of linkedBlocks) {
325
+ if (block.result) {
326
+ useType(block.result);
327
+ }
328
+ for (const variable of [
329
+ ...block.inputs,
330
+ ...block.outputs,
331
+ ...block.state,
332
+ ...block.locals,
333
+ ]) {
334
+ useType(variable.type);
335
+ }
336
+ }
337
+ // A telemetry property can be its type's only use, such as an enum field the elaborator folded.
338
+ const useTelemetryTypes = (body: readonly CoreStatement[]): void => {
339
+ for (const item of body) {
340
+ if (item.kind === "telemetry") {
341
+ for (const operand of telemetryOperands(item)) {
342
+ useType(operand.type);
343
+ }
344
+ }
345
+ for (const nested of nestedBodies(item)) {
346
+ useTelemetryTypes(nested);
347
+ }
348
+ }
349
+ };
350
+ for (const routine of [...linkedFunctions, ...linkedBlocks]) {
351
+ useTelemetryTypes(routine.body);
352
+ }
353
+ return usedTypes;
354
+ };