@tsplc/compiler 0.2.13 → 0.2.16
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.
- package/backends/codesys/debug.ts +509 -0
- package/backends/codesys/emit.ts +701 -9
- package/backends/codesys/http.ts +110 -34
- package/backends/codesys/index.ts +2 -1
- package/backends/codesys/io-paths.ts +3 -1
- package/backends/codesys/manifest.ts +312 -0
- package/backends/codesys/plan.ts +39 -9
- package/backends/codesys/publication.ts +2 -2
- package/backends/codesys/storage.ts +10 -3
- package/backends/codesys/task-access.ts +51 -1
- package/backends/codesys/telemetry.ts +593 -0
- package/backends/codesys/types.ts +2 -1
- package/dist/cli.js +205 -36
- package/dist/cli.js.map +6 -6
- package/dist/codesys.js +2148 -432
- package/dist/codesys.js.map +14 -11
- package/dist/elaborate-capture.js +7 -1
- package/dist/elaborate-capture.js.map +3 -3
- package/dist/index.js +877 -459
- package/dist/index.js.map +16 -15
- package/dist/ir.js +87 -23
- package/dist/ir.js.map +10 -9
- package/dist/project.js +42 -9
- package/dist/project.js.map +3 -3
- package/dist/telemetry.js +83 -0
- package/dist/telemetry.js.map +12 -0
- package/ir/core-model.ts +13 -0
- package/ir/core.ts +1 -0
- package/ir/errors.ts +20 -0
- package/ir/index.ts +1 -0
- package/ir/link.ts +17 -8
- package/ir/model.ts +39 -0
- package/ir/routine.ts +62 -29
- package/ir/types.ts +7 -1
- package/package.json +12 -6
- package/project/cli.ts +96 -6
- package/project/plant.ts +74 -15
- package/project/project.ts +56 -10
- package/src/frontend/async.ts +284 -195
- package/src/frontend/configuration.ts +7 -21
- package/src/frontend/const-functions.ts +44 -38
- package/src/frontend/elaborate/capture.ts +7 -0
- package/src/frontend/elaborate/index.ts +135 -61
- package/src/frontend/elaborate/layouts.ts +8 -0
- package/src/frontend/elaborate/model.ts +13 -0
- package/src/frontend/elaborate/specialize.ts +302 -111
- package/src/frontend/exposure.ts +28 -26
- package/src/frontend/positions.ts +272 -0
- package/src/frontend/promises.ts +52 -47
- package/src/frontend/services.ts +16 -26
- package/src/frontend/types.ts +14 -0
- package/src/frontend/typescript.ts +185 -65
- package/stdlib/telemetry.ts +72 -0
- package/types/telemetry.d.ts +44 -0
|
@@ -0,0 +1,509 @@
|
|
|
1
|
+
import { instancedBlock, nestedBodies } from "@tsplc/compiler/ir";
|
|
2
|
+
import { routineTasks } from "./task-access.ts";
|
|
3
|
+
import type {
|
|
4
|
+
CoreExpression,
|
|
5
|
+
CoreFunction,
|
|
6
|
+
CoreFunctionBlock,
|
|
7
|
+
CorePath,
|
|
8
|
+
CoreStatement,
|
|
9
|
+
Deployment,
|
|
10
|
+
PlcType,
|
|
11
|
+
SourceSpan,
|
|
12
|
+
Variable,
|
|
13
|
+
VerifiedCoreModule,
|
|
14
|
+
} from "@tsplc/compiler/ir";
|
|
15
|
+
import type { ResolvedDebugConfig } from "@tsplc/config";
|
|
16
|
+
import { moduleName } from "./storage.ts";
|
|
17
|
+
|
|
18
|
+
type Routine = CoreFunction | CoreFunctionBlock;
|
|
19
|
+
|
|
20
|
+
/** A guard leaf captured at an event site; `capture` is evaluated at the site. */
|
|
21
|
+
export interface Operand {
|
|
22
|
+
capture: CoreExpression;
|
|
23
|
+
span: SourceSpan;
|
|
24
|
+
path?: string;
|
|
25
|
+
stable: boolean;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
type Probe =
|
|
29
|
+
| {
|
|
30
|
+
kind: "state" | "decision";
|
|
31
|
+
site: number;
|
|
32
|
+
value: CoreExpression;
|
|
33
|
+
operands: readonly Operand[];
|
|
34
|
+
/** A decision's last-value slot. */
|
|
35
|
+
slot?: number;
|
|
36
|
+
}
|
|
37
|
+
| {
|
|
38
|
+
kind: "writer";
|
|
39
|
+
site: number;
|
|
40
|
+
output: number;
|
|
41
|
+
/** Dynamic indexes that must equal the output's index for this write to reach it. */
|
|
42
|
+
when: readonly { index: CoreExpression; value: number }[];
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
interface EventSite {
|
|
46
|
+
id: number;
|
|
47
|
+
kind: "state" | "decision" | "job";
|
|
48
|
+
/** A job site's block and whether the job starts or finishes there. */
|
|
49
|
+
job?: { block: string; phase: "started" | "finished" };
|
|
50
|
+
path: string;
|
|
51
|
+
routine?: string;
|
|
52
|
+
type: string;
|
|
53
|
+
span: SourceSpan;
|
|
54
|
+
/** Defaults to the source text of `span`. */
|
|
55
|
+
text?: string;
|
|
56
|
+
task: string;
|
|
57
|
+
operands: readonly Operand[];
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
interface WriterSite {
|
|
61
|
+
id: number;
|
|
62
|
+
routine: string;
|
|
63
|
+
span: SourceSpan;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** An output the program drives: a logical output, or a storage path bound to a %Q address. */
|
|
67
|
+
interface Output {
|
|
68
|
+
key: string;
|
|
69
|
+
root: Variable;
|
|
70
|
+
segments: readonly ({ kind: "field"; name: string } | { kind: "index"; value: number })[];
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** One declared block instance, from its module root down: each variable and its array index. */
|
|
74
|
+
export type InstancePath = readonly { variable: Variable; index?: number }[];
|
|
75
|
+
|
|
76
|
+
type Load = CoreExpression & { kind: "load" };
|
|
77
|
+
|
|
78
|
+
const temporary = (id: string) => id.includes(":temporary:");
|
|
79
|
+
|
|
80
|
+
const constantIndex = (value: CoreExpression): number | undefined =>
|
|
81
|
+
value.kind === "constant" && value.value.kind === "integer"
|
|
82
|
+
? Number(value.value.value)
|
|
83
|
+
: undefined;
|
|
84
|
+
|
|
85
|
+
/** Every declared instance of each block, from its module root down. */
|
|
86
|
+
export function blockInstances(core: VerifiedCoreModule): Map<string, InstancePath[]> {
|
|
87
|
+
const types = new Map(core.types.map((type) => [type.id, type]));
|
|
88
|
+
const blocks = new Map(core.blocks.map((block) => [block.id, block]));
|
|
89
|
+
const instances = new Map<string, InstancePath[]>(core.blocks.map((block) => [block.id, []]));
|
|
90
|
+
const visit = (variable: Variable, path: InstancePath): void => {
|
|
91
|
+
const type = types.get(variable.type)!;
|
|
92
|
+
const block = instancedBlock(variable.type, types)!;
|
|
93
|
+
const declared =
|
|
94
|
+
type.kind === "array"
|
|
95
|
+
? Array.from({ length: type.length }, (_, offset) => [
|
|
96
|
+
...path,
|
|
97
|
+
{ variable, index: type.lowerBound + offset },
|
|
98
|
+
])
|
|
99
|
+
: [[...path, { variable }]];
|
|
100
|
+
for (const instance of declared) {
|
|
101
|
+
instances.get(block.block)!.push(instance);
|
|
102
|
+
for (const child of blocks.get(block.block)!.state)
|
|
103
|
+
if (instancedBlock(child.type, types)) visit(child, instance);
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
for (const variable of core.variables)
|
|
107
|
+
if (instancedBlock(variable.type, types)) visit(variable, []);
|
|
108
|
+
return instances;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** The TypeScript path of a module storage place, when every index is constant. */
|
|
112
|
+
export function storagePath(root: Variable, path: CorePath = []): string | undefined {
|
|
113
|
+
const logical = root.storage === "input" || root.storage === "output";
|
|
114
|
+
if (!logical && root.storage !== "module" && root.storage !== "instance") return undefined;
|
|
115
|
+
let text = logical ? root.name : `${moduleName(root)}.${root.name}`;
|
|
116
|
+
for (const segment of path) {
|
|
117
|
+
if (segment.kind === "field") {
|
|
118
|
+
text += `.${segment.name}`;
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
const index = constantIndex(segment.value);
|
|
122
|
+
if (index === undefined) return undefined;
|
|
123
|
+
text += `[${index}]`;
|
|
124
|
+
}
|
|
125
|
+
return text;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Where the debugger looks: which task runs each routine, every block instance, and the
|
|
130
|
+
* probes each instrumented statement gets. Probes follow `debug`; with no settings there are none.
|
|
131
|
+
*/
|
|
132
|
+
export class DebugAnalysis {
|
|
133
|
+
readonly tasks;
|
|
134
|
+
readonly instances;
|
|
135
|
+
readonly probes = new Map<CoreStatement, Probe[]>();
|
|
136
|
+
readonly events: EventSite[] = [];
|
|
137
|
+
readonly writers: WriterSite[][];
|
|
138
|
+
/**
|
|
139
|
+
* Event sites of each job block: its start, with the entry step, and its completion, with the
|
|
140
|
+
* last step or the negated error. Steps in between are not recorded: a job that polls would fill the ring.
|
|
141
|
+
*/
|
|
142
|
+
readonly jobSites = new Map<string, { start: number; completion: number }>();
|
|
143
|
+
/** Decision slots by routine. */
|
|
144
|
+
readonly decisions = new Map<string, number>();
|
|
145
|
+
/**
|
|
146
|
+
* Call sites per routine, a call in a loop counting as many. A decision has one last-value slot,
|
|
147
|
+
* so a function called more than once per scan would flip it on every call and flood the ring.
|
|
148
|
+
*/
|
|
149
|
+
private readonly callSites = new Map<string, number>();
|
|
150
|
+
/** Blocks that contain a service call, so they carry the requesting job's id down to it. */
|
|
151
|
+
readonly carriers = new Set<string>();
|
|
152
|
+
readonly variables;
|
|
153
|
+
private readonly types;
|
|
154
|
+
private readonly routines;
|
|
155
|
+
private readonly blocks;
|
|
156
|
+
private readonly routineWrites = new Map<string, ReadonlySet<string>>();
|
|
157
|
+
private readonly temporaries = new Map<string, ReadonlyMap<string, readonly CoreStatement[]>>();
|
|
158
|
+
private writerSites = 0;
|
|
159
|
+
|
|
160
|
+
constructor(
|
|
161
|
+
private readonly core: VerifiedCoreModule,
|
|
162
|
+
deployment: Deployment,
|
|
163
|
+
settings: ResolvedDebugConfig | undefined,
|
|
164
|
+
readonly outputs: readonly Output[],
|
|
165
|
+
) {
|
|
166
|
+
this.types = new Map(core.types.map((type) => [type.id, type]));
|
|
167
|
+
this.blocks = new Map(core.blocks.map((block) => [block.id, block]));
|
|
168
|
+
this.routines = new Map<string, Routine>(
|
|
169
|
+
[...core.functions, ...core.blocks].map((routine) => [routine.id, routine]),
|
|
170
|
+
);
|
|
171
|
+
this.variables = new Map(
|
|
172
|
+
[
|
|
173
|
+
...core.variables,
|
|
174
|
+
...core.functions.flatMap((fn) => [...fn.parameters, ...fn.locals]),
|
|
175
|
+
...core.blocks.flatMap((block) => [
|
|
176
|
+
...block.inputs,
|
|
177
|
+
...block.outputs,
|
|
178
|
+
...block.state,
|
|
179
|
+
...block.locals,
|
|
180
|
+
]),
|
|
181
|
+
].map((variable) => [variable.id, variable]),
|
|
182
|
+
);
|
|
183
|
+
this.writers = outputs.map(() => []);
|
|
184
|
+
this.tasks = routineTasks(core, deployment);
|
|
185
|
+
this.instances = blockInstances(core);
|
|
186
|
+
this.findCarriers();
|
|
187
|
+
if (!settings) return;
|
|
188
|
+
const routines = [...core.functions, ...core.blocks.filter((block) => !block.standard)];
|
|
189
|
+
for (const routine of routines) this.countCalls(routine.body, 1);
|
|
190
|
+
for (const routine of routines) this.walk(settings, routine, routine.body, [], []);
|
|
191
|
+
if (settings.trace)
|
|
192
|
+
for (const block of core.blocks) {
|
|
193
|
+
const { job } = block;
|
|
194
|
+
const task = this.tasks.get(block.id);
|
|
195
|
+
if (!job || !task || this.instances.get(block.id)?.length !== 1) continue;
|
|
196
|
+
const site = (type: string, phase: "started" | "finished") =>
|
|
197
|
+
this.site({
|
|
198
|
+
kind: "job",
|
|
199
|
+
job: { block: block.id, phase },
|
|
200
|
+
path: job.name,
|
|
201
|
+
type,
|
|
202
|
+
span: job.span,
|
|
203
|
+
text: `${job.name}: ${phase}`,
|
|
204
|
+
task,
|
|
205
|
+
});
|
|
206
|
+
this.jobSites.set(block.id, {
|
|
207
|
+
start: site("UINT", "started"),
|
|
208
|
+
completion: site("DINT", "finished"),
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/** The instance path of a job declaration, when it declares exactly one instance. */
|
|
214
|
+
owningPath(variable: Variable): InstancePath | undefined {
|
|
215
|
+
const type = this.types.get(variable.type);
|
|
216
|
+
const block = type?.kind === "block" ? this.blocks.get(type.block) : undefined;
|
|
217
|
+
if (!block?.job) return undefined;
|
|
218
|
+
const paths = this.instances
|
|
219
|
+
.get(block.id)!
|
|
220
|
+
.filter((path) => path.at(-1)!.variable.id === variable.id);
|
|
221
|
+
return paths.length === 1 ? paths[0] : undefined;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
private site(site: Omit<EventSite, "id" | "operands"> & { operands?: Operand[] }): number {
|
|
225
|
+
const id = this.events.length + 1;
|
|
226
|
+
this.events.push({ ...site, id, operands: site.operands ?? [] });
|
|
227
|
+
return id;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
private findCarriers(): void {
|
|
231
|
+
const carries = (id: string): boolean => {
|
|
232
|
+
const block = this.blocks.get(id)!;
|
|
233
|
+
if (block.runtime?.kind === "http") return true;
|
|
234
|
+
return block.state.some((child) => {
|
|
235
|
+
const type = instancedBlock(child.type, this.types);
|
|
236
|
+
return type !== undefined && carries(type.block);
|
|
237
|
+
});
|
|
238
|
+
};
|
|
239
|
+
for (const block of this.core.blocks) if (carries(block.id)) this.carriers.add(block.id);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
private placeType(root: Variable, path: CorePath = []): PlcType | undefined {
|
|
243
|
+
let type = this.types.get(root.type);
|
|
244
|
+
for (const segment of path) {
|
|
245
|
+
if (segment.kind === "field" && type?.kind === "record")
|
|
246
|
+
type = this.types.get(type.fields.find((field) => field.name === segment.name)!.type);
|
|
247
|
+
else if (segment.kind === "index" && type?.kind === "array")
|
|
248
|
+
type = this.types.get(type.element);
|
|
249
|
+
else return undefined;
|
|
250
|
+
}
|
|
251
|
+
return type;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
private countCalls(items: readonly CoreStatement[], weight: number): void {
|
|
255
|
+
for (const item of items) {
|
|
256
|
+
if (item.kind === "invoke")
|
|
257
|
+
this.callSites.set(item.function, (this.callSites.get(item.function) ?? 0) + weight);
|
|
258
|
+
for (const body of nestedBodies(item))
|
|
259
|
+
this.countCalls(body, item.kind === "for" ? 2 : weight);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/** Storage a routine can assign, including through every routine it calls. */
|
|
264
|
+
private writes(id: string): ReadonlySet<string> {
|
|
265
|
+
const cached = this.routineWrites.get(id);
|
|
266
|
+
if (cached) return cached;
|
|
267
|
+
const result = new Set<string>();
|
|
268
|
+
this.routineWrites.set(id, result);
|
|
269
|
+
for (const root of this.statementWrites(this.routines.get(id)?.body ?? [])) result.add(root);
|
|
270
|
+
return result;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
private statementWrites(items: readonly CoreStatement[]): Set<string> {
|
|
274
|
+
const result = new Set<string>();
|
|
275
|
+
for (const item of items) {
|
|
276
|
+
if (item.kind === "assign") result.add(item.root);
|
|
277
|
+
if (item.kind === "for") result.add(item.counter);
|
|
278
|
+
if (item.kind === "invoke") {
|
|
279
|
+
if (item.destination) result.add(item.destination);
|
|
280
|
+
for (const argument of item.arguments)
|
|
281
|
+
if (argument.kind === "borrow") result.add(argument.place.root);
|
|
282
|
+
for (const root of this.writes(item.function)) result.add(root);
|
|
283
|
+
}
|
|
284
|
+
if (item.kind === "blockUpdate") {
|
|
285
|
+
for (const input of Object.values(item.inputs))
|
|
286
|
+
if (input.kind === "borrow") result.add(input.place.root);
|
|
287
|
+
const block = instancedBlock(this.variables.get(item.instance)!.type, this.types);
|
|
288
|
+
if (block) for (const root of this.writes(block.block)) result.add(root);
|
|
289
|
+
}
|
|
290
|
+
for (const body of nestedBodies(item))
|
|
291
|
+
for (const root of this.statementWrites(body)) result.add(root);
|
|
292
|
+
}
|
|
293
|
+
return result;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Visit each statement with the statements that precede it on its path, one list per
|
|
298
|
+
* nesting level, and the `if`/`switch` guards enclosing it, innermost last.
|
|
299
|
+
*/
|
|
300
|
+
private walk(
|
|
301
|
+
settings: ResolvedDebugConfig,
|
|
302
|
+
routine: Routine,
|
|
303
|
+
items: readonly CoreStatement[],
|
|
304
|
+
prefixes: readonly (readonly CoreStatement[])[],
|
|
305
|
+
guards: readonly { conditions: readonly CoreExpression[]; depth: number }[],
|
|
306
|
+
): void {
|
|
307
|
+
for (const [index, item] of items.entries()) {
|
|
308
|
+
const here = [...prefixes, items.slice(0, index)];
|
|
309
|
+
this.probe(settings, routine, item, here, guards);
|
|
310
|
+
const guard = (conditions: readonly CoreExpression[]) => [
|
|
311
|
+
...guards,
|
|
312
|
+
{ conditions, depth: here.length },
|
|
313
|
+
];
|
|
314
|
+
if (item.kind === "if") {
|
|
315
|
+
this.walk(settings, routine, item.then, here, guard([item.condition]));
|
|
316
|
+
this.walk(settings, routine, item.else, here, guard([item.condition]));
|
|
317
|
+
}
|
|
318
|
+
if (item.kind === "switch")
|
|
319
|
+
for (const body of nestedBodies(item))
|
|
320
|
+
this.walk(settings, routine, body, here, guard([item.value]));
|
|
321
|
+
if (item.kind === "for") this.walk(settings, routine, item.body, here, guards);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
private probe(
|
|
326
|
+
settings: ResolvedDebugConfig,
|
|
327
|
+
routine: Routine,
|
|
328
|
+
item: CoreStatement,
|
|
329
|
+
prefixes: readonly (readonly CoreStatement[])[],
|
|
330
|
+
guards: readonly { conditions: readonly CoreExpression[]; depth: number }[],
|
|
331
|
+
): void {
|
|
332
|
+
const task = this.tasks.get(routine.id);
|
|
333
|
+
const probes: Probe[] = [];
|
|
334
|
+
if (item.kind === "assign" && settings.writers) {
|
|
335
|
+
let site: number | undefined;
|
|
336
|
+
this.outputs.forEach((output, index) => {
|
|
337
|
+
const when = this.reaches(item.root, item.path ?? [], output);
|
|
338
|
+
if (!when) return;
|
|
339
|
+
site ??= ++this.writerSites;
|
|
340
|
+
this.writers[index]!.push({ id: site, routine: routine.id, span: item.span });
|
|
341
|
+
probes.push({ kind: "writer", site, output: index, when });
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
if (settings.trace && task) {
|
|
345
|
+
if (item.kind === "assign") {
|
|
346
|
+
const root = this.variables.get(item.root)!;
|
|
347
|
+
const type = this.placeType(root, item.path);
|
|
348
|
+
const path = root.storage === "module" ? storagePath(root, item.path) : undefined;
|
|
349
|
+
if (type?.kind === "enum" && path) {
|
|
350
|
+
const operands = this.operands(routine, prefixes, guards, settings.trace);
|
|
351
|
+
const site = this.site({
|
|
352
|
+
kind: "state",
|
|
353
|
+
path,
|
|
354
|
+
routine: routine.id,
|
|
355
|
+
type: type.id,
|
|
356
|
+
span: item.span,
|
|
357
|
+
task,
|
|
358
|
+
operands,
|
|
359
|
+
});
|
|
360
|
+
probes.push({ kind: "state", value: item.value, site, operands });
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
if (
|
|
364
|
+
item.kind === "return" &&
|
|
365
|
+
item.value &&
|
|
366
|
+
"parameters" in routine &&
|
|
367
|
+
routine.result &&
|
|
368
|
+
this.callSites.get(routine.id) === 1
|
|
369
|
+
) {
|
|
370
|
+
const type = this.types.get(routine.result);
|
|
371
|
+
if (type?.kind === "enum") {
|
|
372
|
+
const slot = this.decisions.get(routine.id) ?? this.decisions.size;
|
|
373
|
+
this.decisions.set(routine.id, slot);
|
|
374
|
+
const path = routine.origin?.instance
|
|
375
|
+
? `${routine.origin.instance}.${routine.origin.name.split(".").at(-1)}`
|
|
376
|
+
: (routine.origin?.name ?? routine.name);
|
|
377
|
+
const operands = this.operands(routine, prefixes, guards, settings.trace);
|
|
378
|
+
const site = this.site({
|
|
379
|
+
kind: "decision",
|
|
380
|
+
path,
|
|
381
|
+
routine: routine.id,
|
|
382
|
+
type: type.id,
|
|
383
|
+
span: item.span,
|
|
384
|
+
task,
|
|
385
|
+
operands,
|
|
386
|
+
});
|
|
387
|
+
probes.push({ kind: "decision", value: item.value, site, slot, operands });
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
if (probes.length) this.probes.set(item, probes);
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
/** Undefined when the write can't reach the output; otherwise the dynamic indexes it depends on. */
|
|
395
|
+
private reaches(root: string, path: CorePath, output: Output) {
|
|
396
|
+
if (root !== output.root.id) return undefined;
|
|
397
|
+
const when: { index: CoreExpression; value: number }[] = [];
|
|
398
|
+
for (let index = 0; index < Math.min(path.length, output.segments.length); index++) {
|
|
399
|
+
const written = path[index]!,
|
|
400
|
+
bound = output.segments[index]!;
|
|
401
|
+
if (written.kind === "field") {
|
|
402
|
+
if (bound.kind !== "field" || bound.name !== written.name) return undefined;
|
|
403
|
+
continue;
|
|
404
|
+
}
|
|
405
|
+
if (bound.kind !== "index") return undefined;
|
|
406
|
+
const value = constantIndex(written.value);
|
|
407
|
+
if (value === undefined) when.push({ index: written.value, value: bound.value });
|
|
408
|
+
else if (value !== bound.value) return undefined;
|
|
409
|
+
}
|
|
410
|
+
return when;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
/** The statements that assign each temporary of a routine. */
|
|
414
|
+
private definitions(routine: Routine): ReadonlyMap<string, readonly CoreStatement[]> {
|
|
415
|
+
const cached = this.temporaries.get(routine.id);
|
|
416
|
+
if (cached) return cached;
|
|
417
|
+
const definitions = new Map<string, CoreStatement[]>();
|
|
418
|
+
const define = (items: readonly CoreStatement[]): void => {
|
|
419
|
+
for (const item of items) {
|
|
420
|
+
const target =
|
|
421
|
+
item.kind === "assign" && !item.path?.length
|
|
422
|
+
? item.root
|
|
423
|
+
: item.kind === "invoke"
|
|
424
|
+
? item.destination
|
|
425
|
+
: undefined;
|
|
426
|
+
if (target && temporary(target))
|
|
427
|
+
definitions.set(target, [...(definitions.get(target) ?? []), item]);
|
|
428
|
+
for (const body of nestedBodies(item)) define(body);
|
|
429
|
+
}
|
|
430
|
+
};
|
|
431
|
+
define(routine.body);
|
|
432
|
+
this.temporaries.set(routine.id, definitions);
|
|
433
|
+
return definitions;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
/** The leaves of the enclosing guards, innermost first, as the site can still read them. */
|
|
437
|
+
private operands(
|
|
438
|
+
routine: Routine,
|
|
439
|
+
prefixes: readonly (readonly CoreStatement[])[],
|
|
440
|
+
guards: readonly { conditions: readonly CoreExpression[]; depth: number }[],
|
|
441
|
+
trace: Exclude<ResolvedDebugConfig["trace"], false>,
|
|
442
|
+
): Operand[] {
|
|
443
|
+
const definitions = this.definitions(routine);
|
|
444
|
+
const result: Operand[] = [];
|
|
445
|
+
const seen = new Set<string>();
|
|
446
|
+
for (const guard of [...guards].reverse()) {
|
|
447
|
+
const executed = new Set(prefixes.slice(0, guard.depth).flat());
|
|
448
|
+
const between = this.statementWrites(prefixes.slice(guard.depth).flat());
|
|
449
|
+
/** `read` is the storage the leaf reads; a call result has none. */
|
|
450
|
+
const add = (span: SourceSpan, capture: CoreExpression, read?: Load): void => {
|
|
451
|
+
const kind = this.types.get(capture.type)?.kind;
|
|
452
|
+
const key = `${span.file}:${span.start}:${span.end}`;
|
|
453
|
+
if (seen.has(key) || !["bool", "enum", "integer", "bits"].includes(kind ?? "")) return;
|
|
454
|
+
seen.add(key);
|
|
455
|
+
const path = read && storagePath(this.variables.get(read.root)!, read.path);
|
|
456
|
+
result.push({
|
|
457
|
+
capture,
|
|
458
|
+
span,
|
|
459
|
+
...(path && { path }),
|
|
460
|
+
stable: !read || !between.has(read.root),
|
|
461
|
+
});
|
|
462
|
+
};
|
|
463
|
+
// A leaf the guard may have skipped is read again at the site, when that has no effects.
|
|
464
|
+
const direct = (value: Load) =>
|
|
465
|
+
(value.path ?? []).every(
|
|
466
|
+
(segment) => segment.kind === "field" || constantIndex(segment.value) !== undefined,
|
|
467
|
+
);
|
|
468
|
+
const visit = (value: CoreExpression, evaluated: boolean): void => {
|
|
469
|
+
if (value.kind === "unary" || value.kind === "convert") visit(value.operand, evaluated);
|
|
470
|
+
if (value.kind === "binary") {
|
|
471
|
+
visit(value.left, evaluated);
|
|
472
|
+
visit(value.right, evaluated);
|
|
473
|
+
}
|
|
474
|
+
if (value.kind !== "load") return;
|
|
475
|
+
if (!temporary(value.root)) {
|
|
476
|
+
if (evaluated || direct(value)) add(value.span, value, value);
|
|
477
|
+
return;
|
|
478
|
+
}
|
|
479
|
+
const defined = definitions.get(value.root) ?? [];
|
|
480
|
+
if (defined.length !== 1) {
|
|
481
|
+
for (const item of defined)
|
|
482
|
+
if (item.kind === "assign") visit(item.value, evaluated && executed.has(item));
|
|
483
|
+
return;
|
|
484
|
+
}
|
|
485
|
+
const item = defined[0]!;
|
|
486
|
+
const reached = evaluated && executed.has(item);
|
|
487
|
+
if (item.kind === "invoke") {
|
|
488
|
+
if (reached) add(item.span, value);
|
|
489
|
+
return;
|
|
490
|
+
}
|
|
491
|
+
if (item.kind !== "assign") return;
|
|
492
|
+
const source = item.value;
|
|
493
|
+
if (source.kind === "load" && !temporary(source.root)) {
|
|
494
|
+
const read = {
|
|
495
|
+
...source,
|
|
496
|
+
type: value.type,
|
|
497
|
+
path: [...(source.path ?? []), ...(value.path ?? [])],
|
|
498
|
+
};
|
|
499
|
+
if (reached) add(source.span, value, read);
|
|
500
|
+
else if (direct(read)) add(source.span, read, read);
|
|
501
|
+
return;
|
|
502
|
+
}
|
|
503
|
+
visit(source, reached);
|
|
504
|
+
};
|
|
505
|
+
for (const condition of guard.conditions) visit(condition, true);
|
|
506
|
+
}
|
|
507
|
+
return result.slice(0, trace.operands);
|
|
508
|
+
}
|
|
509
|
+
}
|