@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,497 @@
1
+ import {
2
+ type CoreExpression,
3
+ type CorePath,
4
+ type CoreStatement,
5
+ type CoreTables,
6
+ isBlockRoutine,
7
+ isTemporary,
8
+ projectReconstruction,
9
+ type ReconstructedSegment,
10
+ type StatementConditions,
11
+ standardBlocks,
12
+ staticNames,
13
+ } from "@tsplc/compiler/ir";
14
+ import type {
15
+ ExplainAssignment,
16
+ ExplainCall,
17
+ ExplainFile,
18
+ } from "@tsplc/debug/explain-file";
19
+ import { required } from "../required";
20
+ import type { ExplainEncoder, Spelled } from "./expressions";
21
+ import { parameterIndex, parameterKey } from "./parameters";
22
+ import { calleeRows, type Row } from "./rows";
23
+ import type { ExplainTables } from "./tables";
24
+
25
+ type Invoke = Extract<CoreStatement, { kind: "invoke" }>;
26
+
27
+ /** What every record of a statement starts with. */
28
+ type StatementRecord = Pick<
29
+ ExplainAssignment,
30
+ "routine" | "position" | "conditions" | "site"
31
+ >;
32
+
33
+ /**
34
+ * Writes through by-reference parameters are followed this many calls deep: each level fans out
35
+ * to every instance the call can run on.
36
+ */
37
+ const maxParameterDepth = 16;
38
+
39
+ /**
40
+ * Every row's loops, calls, returns and the writes of stored state its statements make, the
41
+ * writes through by-reference parameters included, as `explain.json` holds them.
42
+ */
43
+ export function explainWrites({
44
+ rows,
45
+ core,
46
+ tables,
47
+ encoder,
48
+ writesThrough,
49
+ }: {
50
+ rows: readonly Row[];
51
+ core: CoreTables;
52
+ tables: ExplainTables;
53
+ encoder: ExplainEncoder;
54
+ /** The parameters each routine writes through, by `parameterKey`. */
55
+ writesThrough: ReadonlySet<string>;
56
+ }) {
57
+ const { expression, condition, spell, extend, local, placeRef, isAggregate } =
58
+ encoder;
59
+ const loops: ExplainFile["loops"] = [];
60
+ const assignments: ExplainAssignment[] = [];
61
+ const returns: ExplainFile["returns"] = [];
62
+ const callRecords = new Map<number, ExplainCall>();
63
+ /** The assignments each statement makes itself, in every instance that runs it. */
64
+ const recorded = new Map<CoreStatement, number[]>();
65
+
66
+ const segments = (
67
+ row: Row,
68
+ path: CorePath,
69
+ at: number,
70
+ ): ReconstructedSegment[] =>
71
+ path.map((segment) =>
72
+ segment.kind === "field"
73
+ ? segment
74
+ : {
75
+ kind: "index",
76
+ value: row.conditions.reconstruct(segment.value, at),
77
+ },
78
+ );
79
+ const loopOf = (row: Row, loop: number | null): number | null =>
80
+ loop === null ? null : row.firstLoop + loop;
81
+ const statementRecord = (
82
+ row: Row,
83
+ item: CoreStatement,
84
+ flow: StatementConditions,
85
+ ): StatementRecord => ({
86
+ routine: row.index,
87
+ position: flow.position,
88
+ conditions: tables.conditionsIndex(
89
+ flow.conditions.map((part) => condition(row, part)),
90
+ ),
91
+ site: tables.site(item.span),
92
+ });
93
+
94
+ /** A block instance a call runs on, at the element it indexes. */
95
+ const spellInstance = (
96
+ row: Row,
97
+ instance: string,
98
+ index: CoreExpression | undefined,
99
+ at: number,
100
+ ) =>
101
+ spell(
102
+ row,
103
+ instance,
104
+ index
105
+ ? [{ kind: "index", value: row.conditions.reconstruct(index, at) }]
106
+ : [],
107
+ );
108
+ /** The receiver of a call in a row: a block instance's path, or none for a function. */
109
+ const receiver = (
110
+ row: Row,
111
+ item: Invoke,
112
+ at: number,
113
+ ): ExplainCall["receiver"] => {
114
+ if (item.instance === undefined) {
115
+ return core.functions.get(item.function)?.owner && row.instance !== null
116
+ ? { kind: "path", path: tables.paths.add(row.instance) }
117
+ : { kind: "none" };
118
+ }
119
+ const spelled = spellInstance(row, item.instance, item.index, at);
120
+ return spelled ? placeRef(spelled) : { kind: "none" };
121
+ };
122
+
123
+ for (const row of rows) {
124
+ const { routine, conditions } = row;
125
+ conditions.loops.forEach((loop, index) => {
126
+ const counter = core.variableOf(loop.counter);
127
+ loops.push({
128
+ routine: row.index,
129
+ position: loop.position,
130
+ counter: expression(
131
+ row,
132
+ conditions.reconstruct(
133
+ {
134
+ kind: "load",
135
+ root: loop.counter,
136
+ type: counter.type,
137
+ span: loop.statement.span,
138
+ },
139
+ loop.position,
140
+ ),
141
+ ),
142
+ first: expression(row, loop.first),
143
+ limit: expression(row, loop.limit),
144
+ exclusive: loop.exclusive,
145
+ step: tables.expressions.add({
146
+ kind: "constant",
147
+ value: Number(loop.step),
148
+ type: counter.type,
149
+ }),
150
+ maxIterations: loop.maxIterations,
151
+ breaks: loop.breaks.map((all) =>
152
+ all.map((part) => condition(row, part)),
153
+ ),
154
+ parent: loopOf(row, loop.parent),
155
+ site: required(row.loopSites[index]),
156
+ });
157
+ });
158
+ const block = isBlockRoutine(routine) ? routine : undefined;
159
+ for (const [item, flow] of conditions.statements) {
160
+ const first = assignments.length;
161
+ const at = flow.position;
162
+ const record = statementRecord(row, item, flow);
163
+ const loop = loopOf(row, flow.loop);
164
+ const reconstruct = (value: CoreExpression) =>
165
+ expression(row, conditions.reconstruct(value, at));
166
+ /** A write of `value` to `root`: stored, a local's definition, or nothing for a temporary. */
167
+ const write = (
168
+ root: string,
169
+ path: CorePath,
170
+ kind: "assign" | "call" | "counter",
171
+ value: number | null,
172
+ ): void => {
173
+ const variable = core.variableOf(root);
174
+ const parameter = parameterIndex(routine, root);
175
+ const isLocal =
176
+ variable.storage === "local" &&
177
+ routine.locals.some((candidate) => candidate.id === root);
178
+ if (isLocal && isTemporary(routine.id, variable)) {
179
+ return;
180
+ }
181
+ if (isLocal || parameter >= 0) {
182
+ const names = staticNames(segments(row, path, at));
183
+ local(row, root).definitions.push({
184
+ ...record,
185
+ kind: kind === "counter" ? "counter" : "assign",
186
+ path: names ?? [],
187
+ value: names ? value : null,
188
+ call: null,
189
+ loop,
190
+ });
191
+ return;
192
+ }
193
+ const spelled = spell(row, root, segments(row, path, at));
194
+ if (!spelled) {
195
+ return;
196
+ }
197
+ assignments.push({
198
+ ...record,
199
+ kind,
200
+ writes: placeRef(spelled),
201
+ whole: isAggregate(spelled),
202
+ value,
203
+ block: null,
204
+ via: [],
205
+ loop,
206
+ });
207
+ };
208
+ /** A local or parameter a call borrows, which the callee writes through. */
209
+ const borrowed = (
210
+ call: number,
211
+ argument: CoreExpression,
212
+ index: number,
213
+ callee: string,
214
+ ) => {
215
+ if (
216
+ argument.kind !== "borrow" ||
217
+ !writesThrough.has(parameterKey(callee, index))
218
+ ) {
219
+ return;
220
+ }
221
+ const root = argument.place.root;
222
+ const isLocal =
223
+ routine.locals.some((candidate) => candidate.id === root) &&
224
+ !isTemporary(routine.id, core.variableOf(root));
225
+ if (!isLocal && parameterIndex(routine, root) < 0) {
226
+ return;
227
+ }
228
+ const names = staticNames(segments(row, argument.place.path, at));
229
+ local(row, root).definitions.push({
230
+ ...record,
231
+ kind: "borrowed",
232
+ path: names ?? [],
233
+ value: null,
234
+ call,
235
+ loop,
236
+ });
237
+ };
238
+ switch (item.kind) {
239
+ case "assign":
240
+ write(item.root, item.path ?? [], "assign", reconstruct(item.value));
241
+ break;
242
+ case "invoke": {
243
+ const call = required(row.calls.get(item));
244
+ callRecords.set(call, {
245
+ ...record,
246
+ callee: item.function,
247
+ receiver: receiver(row, item, at),
248
+ arguments: item.arguments.map((argument) => reconstruct(argument)),
249
+ loop,
250
+ });
251
+ item.arguments.forEach((argument, index) => {
252
+ borrowed(call, argument, index, item.function);
253
+ });
254
+ if (item.destination) {
255
+ write(
256
+ item.destination,
257
+ [],
258
+ "call",
259
+ tables.expressions.add({
260
+ kind: "call",
261
+ call,
262
+ path: [],
263
+ type: required(core.functionOf(item.function).result),
264
+ }),
265
+ );
266
+ }
267
+ break;
268
+ }
269
+ case "blockUpdate": {
270
+ const call = required(row.calls.get(item));
271
+ const called = core.updatedBlock(item);
272
+ const spelled = spellInstance(row, item.instance, item.index, at);
273
+ callRecords.set(call, {
274
+ ...record,
275
+ callee: called.id,
276
+ receiver: spelled ? placeRef(spelled) : { kind: "none" },
277
+ arguments: called.inputs.flatMap((input) => {
278
+ const value = item.inputs[input.name];
279
+ return value ? [reconstruct(value)] : [];
280
+ }),
281
+ loop,
282
+ });
283
+ if (!spelled) {
284
+ break;
285
+ }
286
+ if (called.standard) {
287
+ const native = standardBlocks[called.standard];
288
+ const elapsed = native.outputs.some(([name]) => name === "elapsed")
289
+ ? extend(row, spelled, [{ kind: "field", name: "elapsed" }])
290
+ : undefined;
291
+ assignments.push({
292
+ ...record,
293
+ kind: "block",
294
+ writes: placeRef(spelled),
295
+ whole: true,
296
+ value: null,
297
+ block: {
298
+ kind: called.standard,
299
+ inputs: Object.fromEntries(
300
+ Object.entries(item.inputs).map(([name, value]) => [
301
+ name,
302
+ reconstruct(value),
303
+ ]),
304
+ ),
305
+ elapsed: elapsed
306
+ ? encoder.storedRead(row, elapsed, {
307
+ capturedAt: at,
308
+ type: "TIME",
309
+ })
310
+ : null,
311
+ },
312
+ via: [],
313
+ loop,
314
+ });
315
+ break;
316
+ }
317
+ called.inputs.forEach((input) => {
318
+ const value = item.inputs[input.name];
319
+ if (!value || value.kind === "borrow") {
320
+ return;
321
+ }
322
+ const field = extend(row, spelled, [
323
+ { kind: "field", name: input.name },
324
+ ]);
325
+ assignments.push({
326
+ ...record,
327
+ kind: "input",
328
+ writes: placeRef(field),
329
+ whole: isAggregate(field),
330
+ value: reconstruct(value),
331
+ block: null,
332
+ via: [],
333
+ loop,
334
+ });
335
+ });
336
+ break;
337
+ }
338
+ case "for":
339
+ write(item.counter, [], "counter", null);
340
+ break;
341
+ case "return": {
342
+ returns.push({
343
+ ...record,
344
+ value: item.value ? reconstruct(item.value) : null,
345
+ });
346
+ const value = item.value;
347
+ if (!block || !value || row.instance === null) {
348
+ break;
349
+ }
350
+ const returned = conditions.reconstruct(value, at);
351
+ for (const output of block.outputs) {
352
+ const field = spell(row, output.id, []);
353
+ if (!field) {
354
+ continue;
355
+ }
356
+ assignments.push({
357
+ ...record,
358
+ kind: "return",
359
+ writes: placeRef(field),
360
+ whole: isAggregate(field),
361
+ value: expression(
362
+ row,
363
+ projectReconstruction(
364
+ returned,
365
+ [{ kind: "field", name: output.name }],
366
+ output.type,
367
+ ),
368
+ ),
369
+ block: null,
370
+ via: [],
371
+ loop,
372
+ });
373
+ }
374
+ break;
375
+ }
376
+ default:
377
+ break;
378
+ }
379
+ if (assignments.length > first) {
380
+ recorded.set(item, [
381
+ ...(recorded.get(item) ?? []),
382
+ ...Array.from(
383
+ { length: assignments.length - first },
384
+ (_, offset) => first + offset,
385
+ ),
386
+ ]);
387
+ }
388
+ }
389
+ }
390
+ const calls = Array.from({ length: callRecords.size }, (_, index) =>
391
+ required(callRecords.get(index)),
392
+ );
393
+
394
+ // Writes through a by-reference parameter land in the storage each call site passes.
395
+ const throughParameter = (
396
+ target: Spelled,
397
+ callee: Row,
398
+ parameter: number,
399
+ via: readonly number[],
400
+ depth: number,
401
+ ): void => {
402
+ const root = isBlockRoutine(callee.routine)
403
+ ? undefined
404
+ : callee.routine.parameters[parameter]?.id;
405
+ if (root === undefined || depth > maxParameterDepth) {
406
+ return;
407
+ }
408
+ for (const [item, flow] of callee.conditions.statements) {
409
+ const at = flow.position;
410
+ const record = statementRecord(callee, item, flow);
411
+ // A written place's runtime indexes are evaluated where the place is named, in the
412
+ // outermost caller; one the callee computes itself isn't followed.
413
+ const fixed = (path: CorePath) =>
414
+ path.every(
415
+ (segment) =>
416
+ segment.kind === "field" || segment.value.kind === "constant",
417
+ );
418
+ if (
419
+ item.kind === "assign" &&
420
+ item.root === root &&
421
+ fixed(item.path ?? [])
422
+ ) {
423
+ const spelled = extend(
424
+ callee,
425
+ target,
426
+ segments(callee, item.path ?? [], at),
427
+ );
428
+ assignments.push({
429
+ ...record,
430
+ kind: "assign",
431
+ writes: placeRef(spelled),
432
+ whole: isAggregate(spelled),
433
+ value: expression(
434
+ callee,
435
+ callee.conditions.reconstruct(item.value, at),
436
+ ),
437
+ block: null,
438
+ via: [...via],
439
+ loop: loopOf(callee, flow.loop),
440
+ });
441
+ }
442
+ const call = callee.calls.get(item);
443
+ if (item.kind !== "invoke" || call === undefined) {
444
+ continue;
445
+ }
446
+ const nested = required(calls[call]);
447
+ item.arguments.forEach((argument, index) => {
448
+ if (
449
+ argument.kind !== "borrow" ||
450
+ argument.place.root !== root ||
451
+ !fixed(argument.place.path) ||
452
+ !writesThrough.has(parameterKey(item.function, index))
453
+ ) {
454
+ return;
455
+ }
456
+ const inner = extend(
457
+ callee,
458
+ target,
459
+ segments(callee, argument.place.path, at),
460
+ );
461
+ for (const next of calleeRows(rows, tables, core, nested, callee)) {
462
+ throughParameter(inner, next, index, [call, ...via], depth + 1);
463
+ }
464
+ });
465
+ }
466
+ };
467
+ for (const row of rows) {
468
+ for (const [item, call] of row.calls) {
469
+ if (item.kind !== "invoke") {
470
+ continue;
471
+ }
472
+ const record = required(calls[call]);
473
+ const at = required(row.conditions.statements.get(item)).position;
474
+ item.arguments.forEach((argument, index) => {
475
+ if (
476
+ argument.kind !== "borrow" ||
477
+ !writesThrough.has(parameterKey(item.function, index))
478
+ ) {
479
+ return;
480
+ }
481
+ const target = spell(
482
+ row,
483
+ argument.place.root,
484
+ segments(row, argument.place.path, at),
485
+ );
486
+ if (!target) {
487
+ return;
488
+ }
489
+ for (const callee of calleeRows(rows, tables, core, record, row)) {
490
+ throughParameter(target, callee, index, [call], 0);
491
+ }
492
+ });
493
+ }
494
+ }
495
+
496
+ return { loops, calls, assignments, returns, recorded };
497
+ }
@@ -0,0 +1,149 @@
1
+ import { type CoreStatement, coreTables } from "@tsplc/compiler/ir";
2
+ import { createExplainer } from "@tsplc/debug/explain";
3
+ import type { ExplainFile } from "@tsplc/debug/explain-file";
4
+ import type { DebugManifest } from "@tsplc/debug/manifest";
5
+ import { blockInstances } from "./debug";
6
+ import { explainEncoder } from "./explain/expressions";
7
+ import { explainFlags } from "./explain/flags";
8
+ import { writtenThroughParameters } from "./explain/parameters";
9
+ import { routineRows } from "./explain/rows";
10
+ import { explainTables } from "./explain/tables";
11
+ import { taskEntries } from "./explain/tasks";
12
+ import { explainWrites } from "./explain/writes";
13
+ import { required } from "./required";
14
+ import type { Sources } from "./sources";
15
+ import type { EmissionContext } from "./symbols";
16
+
17
+ /** A build's `explain.json`, and the assignments of it each statement makes itself. */
18
+ export interface Explanations {
19
+ file: ExplainFile;
20
+ assignmentsOf(statement: CoreStatement): readonly number[];
21
+ }
22
+
23
+ /**
24
+ * Every statement of the specialized program that writes stored state, per routine instance, with
25
+ * the conditions it runs under and the expressions it evaluates, as `explain.json` holds them.
26
+ * `functionIds` are the manifest's function ids by routine; `sources` is the manifest's, so source
27
+ * ranges index the same files.
28
+ */
29
+ export function explanations(
30
+ context: EmissionContext,
31
+ manifest: DebugManifest,
32
+ sources: Sources,
33
+ functionIds: ReadonlyMap<string, number>,
34
+ ): Explanations {
35
+ const { plan, instrumentation } = context;
36
+ const program = plan.specialized;
37
+ const core = coreTables(program);
38
+ const instances = blockInstances(program);
39
+ const tables = explainTables(sources);
40
+ const rows = routineRows({
41
+ program,
42
+ core,
43
+ instances,
44
+ tables,
45
+ jobs: instrumentation.jobs,
46
+ functionIds,
47
+ });
48
+ const writesThrough = writtenThroughParameters([
49
+ ...program.functions,
50
+ ...program.blocks,
51
+ ]);
52
+ const encoder = explainEncoder(program, core, tables, writesThrough);
53
+ const { loops, calls, assignments, returns, recorded } = explainWrites({
54
+ rows,
55
+ core,
56
+ tables,
57
+ encoder,
58
+ writesThrough,
59
+ });
60
+ const { entries, rowTasks } = taskEntries({
61
+ program,
62
+ deployment: plan.specializedTasks,
63
+ rows,
64
+ calls,
65
+ core,
66
+ tables,
67
+ });
68
+ const flags = explainFlags({
69
+ plan,
70
+ program,
71
+ instances,
72
+ instrumentation,
73
+ manifest,
74
+ tables,
75
+ assignments,
76
+ readers: encoder.readers,
77
+ rowTasks,
78
+ });
79
+ const references = Object.fromEntries(
80
+ (program.references?.objects ?? []).map(({ id, path }) => [
81
+ String(id),
82
+ tables.paths.add(path),
83
+ ]),
84
+ );
85
+
86
+ const file: ExplainFile = {
87
+ kind: "tsplc-explain",
88
+ version: 1,
89
+ paths: tables.paths.items,
90
+ texts: tables.texts.items,
91
+ expressions: tables.expressions.items,
92
+ conditions: tables.conditions.items,
93
+ sites: tables.sites.items,
94
+ routines: rows.map((row) => ({
95
+ ...row.entry,
96
+ paths: row.slots.paths.items,
97
+ locals: row.slots.locals.items,
98
+ calls: row.slots.calls.items,
99
+ loops: row.conditions.loops.map((_, loop) => row.firstLoop + loop),
100
+ })),
101
+ loops,
102
+ assignments,
103
+ locals: encoder.locals,
104
+ returns,
105
+ calls,
106
+ entries,
107
+ flags,
108
+ references,
109
+ };
110
+ return { file, assignmentsOf: (statement) => recorded.get(statement) ?? [] };
111
+ }
112
+
113
+ /**
114
+ * The manifest's last-writer slots with the assignments each site stands for, and whether the slot
115
+ * names its path's writer exactly: when every statement that can write the path writes it directly,
116
+ * where a probe records it. Writes made by a block, a loop counter or through a parameter aren't probed.
117
+ */
118
+ export function explainedWriters(
119
+ manifest: DebugManifest,
120
+ explained: Explanations,
121
+ sites: readonly (readonly { id: number; statement: CoreStatement }[])[],
122
+ ): DebugManifest["writers"] {
123
+ const explainer = createExplainer(manifest, explained.file);
124
+ return manifest.writers.map((writer, index) => {
125
+ const probed = required(sites[index]);
126
+ const { writers } = explainer.explain(writer.output);
127
+ const exact = writers.every((item) => {
128
+ const assignment = required(explained.file.assignments[item.assignment]);
129
+ return (
130
+ (assignment.kind === "assign" || assignment.kind === "call") &&
131
+ !assignment.via.length
132
+ );
133
+ });
134
+ const explainedAssignments = new Set(
135
+ writers.map((item) => item.assignment),
136
+ );
137
+ return {
138
+ ...writer,
139
+ exact,
140
+ sites: writer.sites.map((site) => ({
141
+ ...site,
142
+ assignments: probed
143
+ .filter((item) => item.id === site.id)
144
+ .flatMap((item) => [...explained.assignmentsOf(item.statement)])
145
+ .filter((assignment) => explainedAssignments.has(assignment)),
146
+ })),
147
+ };
148
+ });
149
+ }