@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
@@ -0,0 +1,134 @@
1
+ import type { VerifiedCoreModule } from "@tsplc/compiler/ir";
2
+ import type {
3
+ ExplainAssignment,
4
+ ExplainFlags,
5
+ } from "@tsplc/debug/explain-file";
6
+ import type { DebugManifest } from "@tsplc/debug/manifest";
7
+ import { createPaths } from "@tsplc/debug/paths";
8
+ import { type InstancePath, instanceText } from "../debug";
9
+ import { isPublished } from "../global-lists";
10
+ import { serviceBlock, serviceName } from "../http";
11
+ import type { Instrumentation } from "../instrumentation";
12
+ import { constantValue, enclosingJob } from "../manifest";
13
+ import type { CodesysPlan } from "../plan";
14
+ import { required } from "../required";
15
+ import { storedPath } from "./expressions";
16
+ import type { ExplainTables } from "./tables";
17
+
18
+ /** What explain.json says about stored paths beyond their writers, each at the shortest path it applies to. */
19
+ export function explainFlags({
20
+ plan,
21
+ program,
22
+ instances,
23
+ instrumentation,
24
+ manifest,
25
+ tables,
26
+ assignments,
27
+ readers,
28
+ rowTasks,
29
+ }: {
30
+ plan: CodesysPlan;
31
+ program: VerifiedCoreModule;
32
+ instances: ReadonlyMap<string, readonly InstancePath[]>;
33
+ instrumentation: Instrumentation;
34
+ manifest: DebugManifest;
35
+ tables: ExplainTables;
36
+ assignments: readonly ExplainAssignment[];
37
+ /** The rows that read each path. */
38
+ readers: ReadonlyMap<string, ReadonlySet<number>>;
39
+ rowTasks: ReadonlyMap<number, ReadonlySet<string>>;
40
+ }): ExplainFlags[] {
41
+ const { paths } = tables;
42
+ const clientWritable = isPublished(plan);
43
+ const flags = new Map<string, ExplainFlags>();
44
+ const flag = (path: string): ExplainFlags => {
45
+ const known = flags.get(path);
46
+ if (known) {
47
+ return known;
48
+ }
49
+ const created: ExplainFlags = {
50
+ path: paths.add(path),
51
+ input: null,
52
+ clientWritable: false,
53
+ hmiControl: null,
54
+ retained: false,
55
+ constantTable: false,
56
+ writerTasks: [],
57
+ service: null,
58
+ initial: null,
59
+ reference: false,
60
+ };
61
+ flags.set(path, created);
62
+ return created;
63
+ };
64
+ const writtenPath = (assignment: ExplainAssignment) =>
65
+ required(paths.items[assignment.writes.path]);
66
+ const writtenPaths = new Set(assignments.map(writtenPath));
67
+ for (const variable of program.variables) {
68
+ const path = storedPath(variable);
69
+ if (path === undefined) {
70
+ continue;
71
+ }
72
+ const published = clientWritable(variable);
73
+ const constant = variable.storage === "constant";
74
+ const retained = variable.retention !== "none";
75
+ if (published || retained || constant) {
76
+ const item = flag(path);
77
+ item.clientWritable = published;
78
+ item.retained = retained;
79
+ item.constantTable = constant;
80
+ }
81
+ if (variable.initial && (constant || !writtenPaths.has(path))) {
82
+ flag(path).initial = constantValue(variable.initial);
83
+ }
84
+ }
85
+ manifest.io.forEach((io, index) => {
86
+ if (io.direction === "input") {
87
+ flag(io.path).input = index;
88
+ }
89
+ });
90
+ for (const block of program.blocks) {
91
+ const endpoint = plan.http[block.id];
92
+ if (!serviceBlock(block) || !endpoint) {
93
+ continue;
94
+ }
95
+ for (const path of required(instances.get(block.id))) {
96
+ const instance = instanceText(path);
97
+ flag(instance).service = {
98
+ job: enclosingJob(instrumentation, instance) ?? 0,
99
+ service: serviceName(endpoint.path),
100
+ };
101
+ }
102
+ }
103
+ for (const field of program.references?.fields ?? []) {
104
+ flag(field).reference = true;
105
+ }
106
+ // An HMI control names the published IEC symbol it writes.
107
+ const targets = createPaths(manifest);
108
+ for (const control of plan.project.hmi?.controls ?? []) {
109
+ const path = targets.fromTarget(control.target);
110
+ if (path) {
111
+ flag(path).hmiControl = control.label;
112
+ }
113
+ }
114
+ const writers = new Map<string, Set<string>>();
115
+ for (const assignment of assignments) {
116
+ const path = writtenPath(assignment);
117
+ const tasks = writers.get(path) ?? new Set<string>();
118
+ for (const task of rowTasks.get(assignment.routine) ?? []) {
119
+ tasks.add(task);
120
+ }
121
+ writers.set(path, tasks);
122
+ }
123
+ for (const [path, tasks] of writers) {
124
+ const readTasks = new Set(
125
+ [...(readers.get(path) ?? [])].flatMap((row) => [
126
+ ...(rowTasks.get(row) ?? []),
127
+ ]),
128
+ );
129
+ if (tasks.size > 1 || [...readTasks].some((task) => !tasks.has(task))) {
130
+ flag(path).writerTasks = [...tasks].sort();
131
+ }
132
+ }
133
+ return [...flags.values()];
134
+ }
@@ -0,0 +1,53 @@
1
+ import {
2
+ type CoreStatement,
3
+ isBlockRoutine,
4
+ nestedBodies,
5
+ } from "@tsplc/compiler/ir";
6
+ import type { Routine } from "./rows";
7
+
8
+ /** A parameter of a routine, by its position. */
9
+ export const parameterKey = (routine: string, index: number) =>
10
+ `${routine}#${index}`;
11
+
12
+ /** The position of `root` among a function's parameters, or -1. */
13
+ export const parameterIndex = (routine: Routine, root: string): number =>
14
+ isBlockRoutine(routine)
15
+ ? -1
16
+ : routine.parameters.findIndex((parameter) => parameter.id === root);
17
+
18
+ const statementsOf = (items: readonly CoreStatement[]): CoreStatement[] =>
19
+ items.flatMap((item) => [item, ...nestedBodies(item).flatMap(statementsOf)]);
20
+
21
+ /**
22
+ * The parameters each routine writes through, by `parameterKey`: those it assigns, and those it
23
+ * passes on to a routine that writes through the parameter it receives them as.
24
+ */
25
+ export function writtenThroughParameters(
26
+ routines: readonly Routine[],
27
+ ): ReadonlySet<string> {
28
+ const propagate = (written: ReadonlySet<string>): ReadonlySet<string> => {
29
+ const added = routines.flatMap((routine) =>
30
+ statementsOf(routine.body).flatMap((item) => {
31
+ const indexes = [
32
+ ...(item.kind === "assign"
33
+ ? [parameterIndex(routine, item.root)]
34
+ : []),
35
+ ...(item.kind === "invoke"
36
+ ? item.arguments.flatMap((argument, index) =>
37
+ argument.kind === "borrow" &&
38
+ written.has(parameterKey(item.function, index))
39
+ ? [parameterIndex(routine, argument.place.root)]
40
+ : [],
41
+ )
42
+ : []),
43
+ ];
44
+ return indexes
45
+ .filter((index) => index >= 0)
46
+ .map((index) => parameterKey(routine.id, index))
47
+ .filter((key) => !written.has(key));
48
+ }),
49
+ );
50
+ return added.length ? propagate(new Set([...written, ...added])) : written;
51
+ };
52
+ return propagate(new Set());
53
+ }
@@ -0,0 +1,182 @@
1
+ import {
2
+ type CoreFunction,
3
+ type CoreFunctionBlock,
4
+ type CoreStatement,
5
+ type CoreTables,
6
+ instanceFields,
7
+ isBlockRoutine,
8
+ pathConditions,
9
+ type RoutineConditions,
10
+ type VerifiedCoreModule,
11
+ } from "@tsplc/compiler/ir";
12
+ import type { ExplainCall, ExplainFile } from "@tsplc/debug/explain-file";
13
+ import { type InstancePath, instanceText } from "../debug";
14
+ import { serviceBlock } from "../http";
15
+ import type { Instrumentation } from "../instrumentation";
16
+ import { routineLabel } from "../manifest";
17
+ import { required } from "../required";
18
+ import { type ExplainTables, Interned } from "./tables";
19
+
20
+ export type Routine = CoreFunction | CoreFunctionBlock;
21
+
22
+ /** A row's table of global indexes; an expression names an entry by its position here. */
23
+ const slots = () => new Interned<number, number>((global) => global);
24
+ export type Slots = ReturnType<typeof slots>;
25
+
26
+ /** One routine as one instance runs it: a function, or a block body or method on one block instance. */
27
+ export interface Row {
28
+ readonly index: number;
29
+ readonly routine: Routine;
30
+ readonly conditions: RoutineConditions;
31
+ /** The block instance's storage path. */
32
+ readonly instance: string | null;
33
+ /** The variables of the block the routine runs on, which are fields of `instance`. */
34
+ readonly fields: ReadonlySet<string>;
35
+ /** Its `routines` entry, but for the slots its records fill. */
36
+ readonly entry: Omit<
37
+ ExplainFile["routines"][number],
38
+ "paths" | "locals" | "calls" | "loops"
39
+ >;
40
+ /**
41
+ * The paths, locals and calls its expressions name, each by its slot here: two instances of one
42
+ * routine then share their expressions and conditions, and differ only in these tables.
43
+ */
44
+ readonly slots: { paths: Slots; locals: Slots; calls: Slots };
45
+ /** Each call it makes, by its index in `calls`. Expressions name call results, so these come first. */
46
+ readonly calls: ReadonlyMap<CoreStatement, number>;
47
+ /** The index in `loops` of its first loop; the rest follow in the order of `conditions.loops`. */
48
+ readonly firstLoop: number;
49
+ /** Each loop's site. */
50
+ readonly loopSites: readonly (number | null)[];
51
+ }
52
+
53
+ /**
54
+ * The rows of a program: a function without a block once, a method or block body once per block
55
+ * instance. `functionIds` are the manifest's function ids by routine.
56
+ */
57
+ export function routineRows({
58
+ program,
59
+ core,
60
+ instances,
61
+ tables,
62
+ jobs,
63
+ functionIds,
64
+ }: {
65
+ program: VerifiedCoreModule;
66
+ core: CoreTables;
67
+ instances: ReadonlyMap<string, readonly InstancePath[]>;
68
+ tables: ExplainTables;
69
+ jobs: Instrumentation["jobs"];
70
+ functionIds: ReadonlyMap<string, number>;
71
+ }): Row[] {
72
+ const analysed = new Map<string, RoutineConditions>();
73
+ const conditionsOf = (routine: Routine) => {
74
+ const known = analysed.get(routine.id);
75
+ if (known) {
76
+ return known;
77
+ }
78
+ const conditions = pathConditions(routine, core.variables);
79
+ analysed.set(routine.id, conditions);
80
+ return conditions;
81
+ };
82
+ const rows: Row[] = [];
83
+ const addRow = (routine: Routine, path: InstancePath | null): void => {
84
+ const block = isBlockRoutine(routine)
85
+ ? routine
86
+ : routine.owner && core.blockOf(routine.owner.block);
87
+ const instance = path ? instanceText(path) : null;
88
+ const job = jobs.find(
89
+ (item) => item.block.id === routine.id && item.instance === instance,
90
+ );
91
+ const label = routineLabel(routine);
92
+ const conditions = conditionsOf(routine);
93
+ const callsBefore = rows.reduce((total, row) => total + row.calls.size, 0);
94
+ const calls = new Map(
95
+ [...conditions.statements.keys()]
96
+ .filter((item) => item.kind === "invoke" || item.kind === "blockUpdate")
97
+ .map((item, index) => [item, callsBefore + index]),
98
+ );
99
+ const row: Row = {
100
+ index: rows.length,
101
+ routine,
102
+ conditions,
103
+ instance,
104
+ fields: new Set(
105
+ block ? instanceFields(block).map((variable) => variable.id) : [],
106
+ ),
107
+ entry: {
108
+ key: routine.id,
109
+ function: functionIds.get(routine.id) ?? null,
110
+ instance: instance === null ? null : tables.paths.add(instance),
111
+ name: label.name,
112
+ declaration: tables.range(label.span),
113
+ job: job?.id ?? null,
114
+ },
115
+ slots: { paths: slots(), locals: slots(), calls: slots() },
116
+ calls,
117
+ firstLoop: rows.reduce(
118
+ (total, row) => total + row.conditions.loops.length,
119
+ 0,
120
+ ),
121
+ loopSites: conditions.loops.map((loop) =>
122
+ tables.site(loop.statement.span),
123
+ ),
124
+ };
125
+ for (const call of calls.values()) {
126
+ row.slots.calls.add(call);
127
+ }
128
+ rows.push(row);
129
+ };
130
+ for (const fn of program.functions) {
131
+ const owner = fn.owner?.block;
132
+ if (owner === undefined) {
133
+ addRow(fn, null);
134
+ continue;
135
+ }
136
+ for (const path of required(instances.get(owner))) {
137
+ addRow(fn, path);
138
+ }
139
+ }
140
+ for (const block of program.blocks) {
141
+ if (block.standard || serviceBlock(block) || !block.body.length) {
142
+ continue;
143
+ }
144
+ for (const path of required(instances.get(block.id))) {
145
+ addRow(block, path);
146
+ }
147
+ }
148
+ return rows;
149
+ }
150
+
151
+ /** The rows a call can run: its callee on the receiver's instance, or as a function or a method of the caller's own instance. */
152
+ export const calleeRows = (
153
+ rows: readonly Row[],
154
+ { paths }: ExplainTables,
155
+ core: CoreTables,
156
+ call: ExplainCall,
157
+ caller: Row,
158
+ ): Row[] =>
159
+ rows
160
+ .filter((row) => {
161
+ if (row.routine.id !== call.callee) {
162
+ return false;
163
+ }
164
+ if (call.receiver.kind === "none") {
165
+ return row.instance === null;
166
+ }
167
+ if (call.receiver.kind === "pattern") {
168
+ return false;
169
+ }
170
+ return paths.items[call.receiver.path] === row.instance;
171
+ })
172
+ .concat(
173
+ call.receiver.kind === "none" &&
174
+ caller.instance !== null &&
175
+ core.functions.get(call.callee)?.owner
176
+ ? rows.filter(
177
+ (row) =>
178
+ row.routine.id === call.callee &&
179
+ row.instance === caller.instance,
180
+ )
181
+ : [],
182
+ );
@@ -0,0 +1,79 @@
1
+ import type { SourceSpan } from "@tsplc/compiler/ir";
2
+ import type {
3
+ ExplainCondition,
4
+ ExplainExpression,
5
+ ExplainFile,
6
+ } from "@tsplc/debug/explain-file";
7
+ import type { SourceRange } from "@tsplc/debug/manifest";
8
+ import type { Sources } from "../sources";
9
+
10
+ /** An interning table: each distinct value once, by its key; a value is named by its position. */
11
+ export class Interned<Value, Key> {
12
+ readonly items: Value[] = [];
13
+ private readonly positions = new Map<Key, number>();
14
+
15
+ constructor(private readonly key: (value: Value) => Key) {}
16
+
17
+ add(value: Value): number {
18
+ const key = this.key(value);
19
+ const known = this.positions.get(key);
20
+ if (known !== undefined) {
21
+ return known;
22
+ }
23
+ this.positions.set(key, this.items.length);
24
+ this.items.push(value);
25
+ return this.items.length - 1;
26
+ }
27
+ }
28
+
29
+ const byJson = <Value>() =>
30
+ new Interned<Value, string>((value) => JSON.stringify(value));
31
+
32
+ /** The most source lines a statement's snippet shows; longer spans are shown by their range alone. */
33
+ const maxSnippetLines = 8;
34
+
35
+ /** The tables of `explain.json` that its records name entries of, and where its sites point. */
36
+ export function explainTables(sources: Sources) {
37
+ const paths = byJson<string>();
38
+ const texts = byJson<string>();
39
+ const expressions = byJson<ExplainExpression>();
40
+ const conditions = byJson<ExplainFile["conditions"][number]>();
41
+ const sites = byJson<ExplainFile["sites"][number]>();
42
+
43
+ const range = (span: SourceSpan): SourceRange | null =>
44
+ sources.range(span) ?? null;
45
+ const text = (span: SourceSpan): number | null => {
46
+ const source = sources.range(span) && sources.text(span);
47
+ return source ? texts.add(source) : null;
48
+ };
49
+ const site = (span: SourceSpan): number | null => {
50
+ const found = range(span);
51
+ if (!found) {
52
+ return null;
53
+ }
54
+ // Generated code is mapped to the whole declaration it came from, which is no snippet.
55
+ const snippet =
56
+ found.endLine - found.line < maxSnippetLines ? text(span) : null;
57
+ return sites.add({ range: found, text: snippet });
58
+ };
59
+ /** The tree entry for a list of conditions, outermost first. */
60
+ const conditionsIndex = (list: readonly ExplainCondition[]): number | null =>
61
+ list.reduce<number | null>(
62
+ (parent, condition) => conditions.add({ parent, condition }),
63
+ null,
64
+ );
65
+
66
+ return {
67
+ paths,
68
+ texts,
69
+ expressions,
70
+ conditions,
71
+ sites,
72
+ range,
73
+ text,
74
+ site,
75
+ conditionsIndex,
76
+ };
77
+ }
78
+
79
+ export type ExplainTables = ReturnType<typeof explainTables>;
@@ -0,0 +1,68 @@
1
+ import type {
2
+ CoreTables,
3
+ Deployment,
4
+ VerifiedCoreModule,
5
+ } from "@tsplc/compiler/ir";
6
+ import type { ExplainCall, ExplainFile } from "@tsplc/debug/explain-file";
7
+ import { required } from "../required";
8
+ import { calleeRows, type Row } from "./rows";
9
+ import type { ExplainTables } from "./tables";
10
+
11
+ /** The routines each task runs directly, in the order it runs them, and the tasks that reach each row through calls. */
12
+ export function taskEntries({
13
+ program,
14
+ deployment,
15
+ rows,
16
+ calls,
17
+ core,
18
+ tables,
19
+ }: {
20
+ program: VerifiedCoreModule;
21
+ deployment: Deployment;
22
+ rows: readonly Row[];
23
+ calls: readonly ExplainCall[];
24
+ core: CoreTables;
25
+ tables: ExplainTables;
26
+ }): {
27
+ entries: ExplainFile["entries"];
28
+ rowTasks: ReadonlyMap<number, ReadonlySet<string>>;
29
+ } {
30
+ const entries: ExplainFile["entries"] = [];
31
+ const rowTasks = new Map<number, Set<string>>();
32
+ for (const task of deployment.tasks) {
33
+ const reached = new Set<number>();
34
+ const visit = (row: Row): void => {
35
+ if (reached.has(row.index)) {
36
+ return;
37
+ }
38
+ reached.add(row.index);
39
+ const tasks = rowTasks.get(row.index) ?? new Set<string>();
40
+ tasks.add(task.name);
41
+ rowTasks.set(row.index, tasks);
42
+ for (const call of row.calls.values()) {
43
+ const record = required(calls[call]);
44
+ const targets =
45
+ record.receiver.kind === "pattern"
46
+ ? rows.filter((candidate) => candidate.routine.id === record.callee)
47
+ : calleeRows(rows, tables, core, record, row);
48
+ targets.forEach(visit);
49
+ }
50
+ };
51
+ for (const name of task.programs) {
52
+ const entry = program.programs.find((item) => item.name === name)?.entry;
53
+ const row = rows.find(
54
+ (candidate) =>
55
+ candidate.routine.id === entry && candidate.instance === null,
56
+ );
57
+ if (row) {
58
+ entries.push({
59
+ routine: row.index,
60
+ task: task.name,
61
+ order: entries.filter((item) => item.task === task.name).length,
62
+ });
63
+ visit(row);
64
+ }
65
+ }
66
+ }
67
+ return { entries, rowTasks };
68
+ }