@tsplc/tmp-debug 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.
- package/README.md +38 -2
- package/dist/api.js +744 -0
- package/dist/api.js.map +12 -0
- package/dist/constant.js +33 -0
- package/dist/constant.js.map +10 -0
- package/dist/explain-file.js +468 -0
- package/dist/explain-file.js.map +11 -0
- package/dist/explain.js +1310 -0
- package/dist/explain.js.map +19 -0
- package/dist/format.js +214 -0
- package/dist/format.js.map +12 -0
- package/dist/index.js +4066 -0
- package/dist/index.js.map +44 -0
- package/dist/manifest.js +240 -0
- package/dist/manifest.js.map +10 -0
- package/dist/paths.js +242 -0
- package/dist/paths.js.map +10 -0
- package/package.json +72 -4
- package/src/adapter.ts +77 -0
- package/src/api.ts +446 -0
- package/src/app-server.ts +151 -0
- package/src/build-files.ts +46 -0
- package/src/constant.ts +41 -0
- package/src/debugger.ts +237 -0
- package/src/errors.ts +13 -0
- package/src/explain-expressions.ts +484 -0
- package/src/explain-file.ts +325 -0
- package/src/explain-frames.ts +155 -0
- package/src/explain-loops.ts +151 -0
- package/src/explain-operations.ts +232 -0
- package/src/explain-tables.ts +345 -0
- package/src/explain-value.ts +203 -0
- package/src/explain-verdicts.ts +548 -0
- package/src/explain.ts +82 -0
- package/src/hardware.ts +159 -0
- package/src/http.ts +218 -0
- package/src/index.ts +44 -0
- package/src/jobs.ts +139 -0
- package/src/login-value.ts +165 -0
- package/src/login.ts +315 -0
- package/src/manifest.ts +328 -0
- package/src/opcua.ts +80 -0
- package/src/paths.ts +363 -0
- package/src/requests.ts +99 -0
- package/src/server-cursor.ts +22 -0
- package/src/shown.ts +229 -0
- package/src/telemetry.ts +125 -0
- package/src/timeline.ts +148 -0
- package/src/trace.ts +62 -0
- package/src/ui/bits.ts +48 -0
- package/src/ui/client.ts +87 -0
- package/src/ui/dom.ts +61 -0
- package/src/ui/explain-read.ts +105 -0
- package/src/ui/format.ts +261 -0
- package/src/ui/hardware.ts +425 -0
- package/src/ui/header.ts +207 -0
- package/src/ui/health.ts +64 -0
- package/src/ui/index.ts +27 -0
- package/src/ui/jobs.ts +263 -0
- package/src/ui/links.ts +59 -0
- package/src/ui/live-value.ts +85 -0
- package/src/ui/main.ts +76 -0
- package/src/ui/overview.ts +178 -0
- package/src/ui/page.ts +286 -0
- package/src/ui/pages.ts +64 -0
- package/src/ui/recent-window.ts +49 -0
- package/src/ui/recent.ts +355 -0
- package/src/ui/request-trace.ts +38 -0
- package/src/ui/requests.ts +80 -0
- package/src/ui/routes.ts +98 -0
- package/src/ui/store.ts +295 -0
- package/src/ui/success-cache.ts +11 -0
- package/src/ui/tree.ts +162 -0
- package/src/ui/variables.ts +597 -0
- package/src/ui/why-watch.ts +11 -0
- package/src/ui/why.ts +694 -0
- package/src/values.ts +170 -0
- package/src/watch.ts +32 -0
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
import {
|
|
2
|
+
valueBinaryOperators,
|
|
3
|
+
valueUnaryOperators,
|
|
4
|
+
} from "@tsplc/iec/arithmetic";
|
|
5
|
+
import { z } from "zod";
|
|
6
|
+
import { json, sourceRange } from "./manifest";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Why each stored value is what it is: every statement that writes it, the conditions it runs
|
|
10
|
+
* under and the expressions it evaluates, rebuilt from the compiler's per-instance model. Written
|
|
11
|
+
* beside `debug.json`, whose `explain.sha256` binds it to the build, as `explainFile`.
|
|
12
|
+
*/
|
|
13
|
+
export const explainFileName = "explain.json";
|
|
14
|
+
|
|
15
|
+
/** An index into one of this file's tables. */
|
|
16
|
+
const tableIndex = z.number().int().nonnegative();
|
|
17
|
+
/** A pre-order statement index within a routine. */
|
|
18
|
+
const position = z.number().int().nonnegative();
|
|
19
|
+
|
|
20
|
+
export const explainExpression = z.discriminatedUnion("kind", [
|
|
21
|
+
/** Stored state, captured at `capturedAt`; `path` is a slot of its routine row's `paths`. */
|
|
22
|
+
z.object({
|
|
23
|
+
kind: z.literal("read"),
|
|
24
|
+
path: tableIndex,
|
|
25
|
+
capturedAt: position,
|
|
26
|
+
/** A call runs between the read and its use, and may have changed what was read. */
|
|
27
|
+
interleaved: z.literal(true).optional(),
|
|
28
|
+
type: z.string(),
|
|
29
|
+
}),
|
|
30
|
+
/** An element at runtime indexes: `path` has a `[*]` for each of `indexes`. */
|
|
31
|
+
z.object({
|
|
32
|
+
kind: z.literal("pattern"),
|
|
33
|
+
path: tableIndex,
|
|
34
|
+
indexes: z.array(tableIndex),
|
|
35
|
+
capturedAt: position,
|
|
36
|
+
interleaved: z.literal(true).optional(),
|
|
37
|
+
type: z.string(),
|
|
38
|
+
}),
|
|
39
|
+
z.object({ kind: z.literal("constant"), value: json, type: z.string() }),
|
|
40
|
+
z.object({
|
|
41
|
+
kind: z.literal("record"),
|
|
42
|
+
fields: z.record(z.string(), tableIndex),
|
|
43
|
+
type: z.string(),
|
|
44
|
+
}),
|
|
45
|
+
z.object({
|
|
46
|
+
kind: z.literal("array"),
|
|
47
|
+
lower: z.number().int(),
|
|
48
|
+
items: z.array(tableIndex),
|
|
49
|
+
type: z.string(),
|
|
50
|
+
}),
|
|
51
|
+
/** A local, by its slot in its routine row's `locals`, read at `at`. */
|
|
52
|
+
z.object({
|
|
53
|
+
kind: z.literal("local"),
|
|
54
|
+
local: tableIndex,
|
|
55
|
+
path: z.array(z.string()),
|
|
56
|
+
at: position,
|
|
57
|
+
type: z.string(),
|
|
58
|
+
}),
|
|
59
|
+
/** The routine's parameter: the call site's argument, or its `locals` slot when the routine writes it. */
|
|
60
|
+
z.object({
|
|
61
|
+
kind: z.literal("parameter"),
|
|
62
|
+
parameter: z.number().int().nonnegative(),
|
|
63
|
+
path: z.array(z.string()),
|
|
64
|
+
at: position,
|
|
65
|
+
local: tableIndex.nullable(),
|
|
66
|
+
type: z.string(),
|
|
67
|
+
}),
|
|
68
|
+
/** A call's result: `call` is a slot of its routine row's `calls`. */
|
|
69
|
+
z.object({
|
|
70
|
+
kind: z.literal("call"),
|
|
71
|
+
call: tableIndex,
|
|
72
|
+
path: z.array(z.string()),
|
|
73
|
+
type: z.string(),
|
|
74
|
+
}),
|
|
75
|
+
/** The PLC's millisecond clock as a DINT. */
|
|
76
|
+
z.object({ kind: z.literal("clock"), type: z.string() }),
|
|
77
|
+
/** An object reference: its operand holds a setup object's id, listed in `references`. */
|
|
78
|
+
z.object({ kind: z.literal("reference"), operand: tableIndex }),
|
|
79
|
+
z.object({
|
|
80
|
+
kind: z.literal("unary"),
|
|
81
|
+
operator: z.enum(valueUnaryOperators),
|
|
82
|
+
operand: tableIndex,
|
|
83
|
+
type: z.string(),
|
|
84
|
+
}),
|
|
85
|
+
z.object({
|
|
86
|
+
kind: z.literal("binary"),
|
|
87
|
+
operator: z.enum(valueBinaryOperators),
|
|
88
|
+
policy: z.enum(["wrap", "saturate"]).optional(),
|
|
89
|
+
left: tableIndex,
|
|
90
|
+
right: tableIndex,
|
|
91
|
+
type: z.string(),
|
|
92
|
+
}),
|
|
93
|
+
z.object({
|
|
94
|
+
kind: z.literal("convert"),
|
|
95
|
+
policy: z.enum(["exact", "wrap", "saturate"]),
|
|
96
|
+
operand: tableIndex,
|
|
97
|
+
type: z.string(),
|
|
98
|
+
}),
|
|
99
|
+
z.object({
|
|
100
|
+
kind: z.literal("logical"),
|
|
101
|
+
operator: z.enum(["and", "or"]),
|
|
102
|
+
left: tableIndex,
|
|
103
|
+
right: tableIndex,
|
|
104
|
+
}),
|
|
105
|
+
z.object({
|
|
106
|
+
kind: z.literal("select"),
|
|
107
|
+
condition: tableIndex,
|
|
108
|
+
// biome-ignore lint/suspicious/noThenProperty: a `?:` branch is data, not a thenable.
|
|
109
|
+
then: tableIndex,
|
|
110
|
+
else: tableIndex,
|
|
111
|
+
type: z.string(),
|
|
112
|
+
}),
|
|
113
|
+
/** Not traced: `text` says why, and `reads` lists the stored values it depends on. */
|
|
114
|
+
z.object({
|
|
115
|
+
kind: z.literal("opaque"),
|
|
116
|
+
text: tableIndex,
|
|
117
|
+
reads: z.array(tableIndex),
|
|
118
|
+
type: z.string(),
|
|
119
|
+
}),
|
|
120
|
+
]);
|
|
121
|
+
export type ExplainExpression = z.infer<typeof explainExpression>;
|
|
122
|
+
|
|
123
|
+
/** Holds when none of the conjunctions holds: no earlier statement left. */
|
|
124
|
+
const exitsCondition = z.object({
|
|
125
|
+
kind: z.literal("exits"),
|
|
126
|
+
get any(): z.ZodArray<z.ZodArray<typeof explainCondition>> {
|
|
127
|
+
return z.array(z.array(explainCondition));
|
|
128
|
+
},
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
// A union rather than a discriminated one: TypeScript can't infer a discriminated union that contains itself.
|
|
132
|
+
export const explainCondition = z.union([
|
|
133
|
+
z.object({
|
|
134
|
+
kind: z.literal("test"),
|
|
135
|
+
test: tableIndex,
|
|
136
|
+
holds: z.boolean(),
|
|
137
|
+
at: position,
|
|
138
|
+
text: tableIndex.nullable(),
|
|
139
|
+
}),
|
|
140
|
+
z.object({
|
|
141
|
+
kind: z.literal("cases"),
|
|
142
|
+
test: tableIndex,
|
|
143
|
+
values: z.array(json),
|
|
144
|
+
member: z.boolean(),
|
|
145
|
+
at: position,
|
|
146
|
+
text: tableIndex.nullable(),
|
|
147
|
+
}),
|
|
148
|
+
exitsCondition,
|
|
149
|
+
/** `loop` is a slot of the routine row's `loops`. */
|
|
150
|
+
z.object({ kind: z.literal("loop"), loop: tableIndex }),
|
|
151
|
+
/** Holds when no iteration of the loop returned. */
|
|
152
|
+
z.object({ kind: z.literal("loopReturns"), loop: tableIndex }),
|
|
153
|
+
/** The routine row's job is at `step`. */
|
|
154
|
+
z.object({ kind: z.literal("job"), step: z.number().int() }),
|
|
155
|
+
/** Code outside the job's step switch, which runs while the job is busy. */
|
|
156
|
+
z.object({ kind: z.literal("jobBusy") }),
|
|
157
|
+
]);
|
|
158
|
+
export type ExplainCondition = z.infer<typeof explainCondition>;
|
|
159
|
+
|
|
160
|
+
const record = {
|
|
161
|
+
routine: tableIndex,
|
|
162
|
+
position,
|
|
163
|
+
/** The innermost `conditions` entry the statement runs under; its parents are the outer ones. Null for none. */
|
|
164
|
+
conditions: tableIndex.nullable(),
|
|
165
|
+
/** The `sites` entry for where the statement is; null when it has no source in the project. */
|
|
166
|
+
site: tableIndex.nullable(),
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
const written = z.discriminatedUnion("kind", [
|
|
170
|
+
z.object({ kind: z.literal("path"), path: tableIndex }),
|
|
171
|
+
z.object({
|
|
172
|
+
kind: z.literal("pattern"),
|
|
173
|
+
path: tableIndex,
|
|
174
|
+
indexes: z.array(tableIndex),
|
|
175
|
+
}),
|
|
176
|
+
]);
|
|
177
|
+
|
|
178
|
+
export const explainAssignment = z.object({
|
|
179
|
+
...record,
|
|
180
|
+
kind: z.enum(["assign", "call", "block", "input", "return", "counter"]),
|
|
181
|
+
writes: written,
|
|
182
|
+
/** A record or array written as a whole, so it writes every path below `writes`. */
|
|
183
|
+
whole: z.boolean(),
|
|
184
|
+
value: tableIndex.nullable(),
|
|
185
|
+
/** A standard block's update: its kind, inputs and elapsed-time output. */
|
|
186
|
+
block: z
|
|
187
|
+
.object({
|
|
188
|
+
kind: z.string(),
|
|
189
|
+
inputs: z.record(z.string(), tableIndex),
|
|
190
|
+
elapsed: tableIndex.nullable(),
|
|
191
|
+
})
|
|
192
|
+
.nullable(),
|
|
193
|
+
/**
|
|
194
|
+
* For a write through a by-reference parameter, the calls from the writer up to where the place
|
|
195
|
+
* is named, innermost first. The write reaches the path only through these calls, and the
|
|
196
|
+
* indexes of a pattern it writes are evaluated in the outermost one's caller.
|
|
197
|
+
*/
|
|
198
|
+
via: z.array(tableIndex),
|
|
199
|
+
loop: tableIndex.nullable(),
|
|
200
|
+
});
|
|
201
|
+
export type ExplainAssignment = z.infer<typeof explainAssignment>;
|
|
202
|
+
|
|
203
|
+
export const explainLocal = z.object({
|
|
204
|
+
routine: tableIndex,
|
|
205
|
+
name: z.string(),
|
|
206
|
+
parameter: z.boolean(),
|
|
207
|
+
reference: z.boolean(),
|
|
208
|
+
definitions: z.array(
|
|
209
|
+
z.object({
|
|
210
|
+
...record,
|
|
211
|
+
kind: z.enum(["assign", "borrowed", "counter"]),
|
|
212
|
+
path: z.array(z.string()),
|
|
213
|
+
value: tableIndex.nullable(),
|
|
214
|
+
call: tableIndex.nullable(),
|
|
215
|
+
loop: tableIndex.nullable(),
|
|
216
|
+
}),
|
|
217
|
+
),
|
|
218
|
+
});
|
|
219
|
+
export type ExplainLocal = z.infer<typeof explainLocal>;
|
|
220
|
+
|
|
221
|
+
export const explainCall = z.object({
|
|
222
|
+
...record,
|
|
223
|
+
/** The callee's `routines[].key`; the receiver picks the instance row. */
|
|
224
|
+
callee: z.string(),
|
|
225
|
+
receiver: z.discriminatedUnion("kind", [
|
|
226
|
+
z.object({ kind: z.literal("none") }),
|
|
227
|
+
z.object({ kind: z.literal("path"), path: tableIndex }),
|
|
228
|
+
z.object({
|
|
229
|
+
kind: z.literal("pattern"),
|
|
230
|
+
path: tableIndex,
|
|
231
|
+
indexes: z.array(tableIndex),
|
|
232
|
+
}),
|
|
233
|
+
]),
|
|
234
|
+
/** One expression per parameter; a by-reference argument is the place it borrows. */
|
|
235
|
+
arguments: z.array(tableIndex),
|
|
236
|
+
loop: tableIndex.nullable(),
|
|
237
|
+
});
|
|
238
|
+
export type ExplainCall = z.infer<typeof explainCall>;
|
|
239
|
+
|
|
240
|
+
export const explainFlags = z.object({
|
|
241
|
+
path: tableIndex,
|
|
242
|
+
/** The manifest `io` row the path is bound to. */
|
|
243
|
+
input: z.number().int().nullable(),
|
|
244
|
+
clientWritable: z.boolean(),
|
|
245
|
+
/** The label of the HMI control that writes the path. */
|
|
246
|
+
hmiControl: z.string().nullable(),
|
|
247
|
+
retained: z.boolean(),
|
|
248
|
+
constantTable: z.boolean(),
|
|
249
|
+
/** Every task that writes the path, when that isn't only the task that reads it. */
|
|
250
|
+
writerTasks: z.array(z.string()),
|
|
251
|
+
/** The manifest job and service whose response the path holds. */
|
|
252
|
+
service: z.object({ job: z.number().int(), service: z.string() }).nullable(),
|
|
253
|
+
initial: json.nullable(),
|
|
254
|
+
reference: z.boolean(),
|
|
255
|
+
});
|
|
256
|
+
export type ExplainFlags = z.infer<typeof explainFlags>;
|
|
257
|
+
|
|
258
|
+
export const explainFile = z.object({
|
|
259
|
+
kind: z.literal("tsplc-explain"),
|
|
260
|
+
version: z.literal(1),
|
|
261
|
+
paths: z.array(z.string()),
|
|
262
|
+
texts: z.array(z.string()),
|
|
263
|
+
expressions: z.array(explainExpression),
|
|
264
|
+
/**
|
|
265
|
+
* Conditions as a tree: each entry holds under its parent, so a statement's conditions are its
|
|
266
|
+
* entry and every parent, outermost last. Statements in one branch share the outer entries.
|
|
267
|
+
*/
|
|
268
|
+
conditions: z.array(
|
|
269
|
+
z.object({ parent: tableIndex.nullable(), condition: explainCondition }),
|
|
270
|
+
),
|
|
271
|
+
/** Source locations, with the source text there. */
|
|
272
|
+
sites: z.array(z.object({ range: sourceRange, text: tableIndex.nullable() })),
|
|
273
|
+
routines: z.array(
|
|
274
|
+
z.object({
|
|
275
|
+
key: z.string(),
|
|
276
|
+
/** The manifest's function id, when the manifest lists the routine. */
|
|
277
|
+
function: z.number().int().nullable(),
|
|
278
|
+
instance: tableIndex.nullable(),
|
|
279
|
+
name: z.string(),
|
|
280
|
+
declaration: sourceRange.nullable(),
|
|
281
|
+
/** The manifest job the instance is. */
|
|
282
|
+
job: z.number().int().nullable(),
|
|
283
|
+
/**
|
|
284
|
+
* What its expressions and conditions name, by slot: instances of one routine share those
|
|
285
|
+
* and differ only here. `paths` index `paths`, `locals` index `locals`, `calls` index
|
|
286
|
+
* `calls` and `loops` index `loops`.
|
|
287
|
+
*/
|
|
288
|
+
paths: z.array(tableIndex),
|
|
289
|
+
locals: z.array(tableIndex),
|
|
290
|
+
calls: z.array(tableIndex),
|
|
291
|
+
loops: z.array(tableIndex),
|
|
292
|
+
}),
|
|
293
|
+
),
|
|
294
|
+
loops: z.array(
|
|
295
|
+
z.object({
|
|
296
|
+
routine: tableIndex,
|
|
297
|
+
position,
|
|
298
|
+
counter: tableIndex,
|
|
299
|
+
first: tableIndex,
|
|
300
|
+
limit: tableIndex,
|
|
301
|
+
exclusive: z.boolean(),
|
|
302
|
+
step: tableIndex,
|
|
303
|
+
maxIterations: z.number().int().nonnegative(),
|
|
304
|
+
breaks: z.array(z.array(explainCondition)),
|
|
305
|
+
parent: tableIndex.nullable(),
|
|
306
|
+
site: tableIndex.nullable(),
|
|
307
|
+
}),
|
|
308
|
+
),
|
|
309
|
+
assignments: z.array(explainAssignment),
|
|
310
|
+
locals: z.array(explainLocal),
|
|
311
|
+
returns: z.array(z.object({ ...record, value: tableIndex.nullable() })),
|
|
312
|
+
calls: z.array(explainCall),
|
|
313
|
+
/** The routines each task runs directly, in the order it runs them. */
|
|
314
|
+
entries: z.array(
|
|
315
|
+
z.object({
|
|
316
|
+
routine: tableIndex,
|
|
317
|
+
task: z.string(),
|
|
318
|
+
order: z.number().int().nonnegative(),
|
|
319
|
+
}),
|
|
320
|
+
),
|
|
321
|
+
flags: z.array(explainFlags),
|
|
322
|
+
/** Object id → `paths` index; −1 is `undefined`. */
|
|
323
|
+
references: z.record(z.string(), tableIndex),
|
|
324
|
+
});
|
|
325
|
+
export type ExplainFile = z.infer<typeof explainFile>;
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import type { Quality } from "@tsplc/opcua/contract";
|
|
2
|
+
import type { ExplainCondition, ExplainFile } from "./explain-file";
|
|
3
|
+
import type { Chain, ExplainTables } from "./explain-tables";
|
|
4
|
+
import {
|
|
5
|
+
merge,
|
|
6
|
+
type Outcome,
|
|
7
|
+
type UnknownOutcome,
|
|
8
|
+
unknown,
|
|
9
|
+
} from "./explain-value";
|
|
10
|
+
import type { ConditionResult, ConditionsTruth } from "./explain-verdicts";
|
|
11
|
+
import type { DebugManifest } from "./manifest";
|
|
12
|
+
|
|
13
|
+
/** One read: every value an evaluation uses comes from it. */
|
|
14
|
+
export interface ExplainRead {
|
|
15
|
+
/** A stored path's sample, decoded as the debugger decodes it; undefined when this read lacks it. */
|
|
16
|
+
sample(path: string): { value: unknown; quality: Quality } | undefined;
|
|
17
|
+
/** A manifest job's state; undefined when it isn't known. */
|
|
18
|
+
job(id: number):
|
|
19
|
+
| {
|
|
20
|
+
busy: boolean | null;
|
|
21
|
+
step?: number;
|
|
22
|
+
cancelling?: boolean;
|
|
23
|
+
unread?: true;
|
|
24
|
+
}
|
|
25
|
+
| undefined;
|
|
26
|
+
/** The PLC clock in DINT milliseconds as a task saw it; undefined when this build has no clock to read. */
|
|
27
|
+
clock(task: string): { low: bigint; high: bigint } | undefined;
|
|
28
|
+
/** The paths the controller forces, or null when the forced state isn't known. */
|
|
29
|
+
readonly forced: ReadonlySet<string> | null;
|
|
30
|
+
/**
|
|
31
|
+
* The path's last-writer slot in this read: the assignments its last writer stands for, or null
|
|
32
|
+
* when nothing wrote it since the controller started; undefined when the build has no slot for it.
|
|
33
|
+
*/
|
|
34
|
+
slot(
|
|
35
|
+
path: string,
|
|
36
|
+
): { assignments: readonly number[] | null; exact: boolean } | undefined;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** A routine row as one chain of calls runs it. */
|
|
40
|
+
export interface Frame {
|
|
41
|
+
readonly row: number;
|
|
42
|
+
readonly caller: { readonly call: number; readonly frame: Frame } | null;
|
|
43
|
+
readonly task: string;
|
|
44
|
+
/** Scan order: the entry's order, then each call's position down the chain. */
|
|
45
|
+
readonly order: readonly number[];
|
|
46
|
+
/** Loop counters bound while a loop is executed, by loop index. */
|
|
47
|
+
readonly counters: ReadonlyMap<number, bigint>;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* One explanation's evaluation over one read, which each topic's functions take: its tables and
|
|
52
|
+
* read, what it has spent, and the evaluations that recurse across topics.
|
|
53
|
+
*/
|
|
54
|
+
export interface Evaluation {
|
|
55
|
+
readonly manifest: DebugManifest;
|
|
56
|
+
readonly file: ExplainFile;
|
|
57
|
+
readonly tables: ExplainTables;
|
|
58
|
+
readonly read: ExplainRead;
|
|
59
|
+
/** Stored paths the evaluation needed and its read didn't have. */
|
|
60
|
+
readonly needs: Set<string>;
|
|
61
|
+
/** Each expression's outcome by expression and frame. */
|
|
62
|
+
readonly memo: Map<string, Outcome>;
|
|
63
|
+
/** Expression nodes evaluated so far, and how deep the evaluation is now. */
|
|
64
|
+
readonly budget: { spent: number; depth: number };
|
|
65
|
+
expression(index: number, frame: Frame): Outcome;
|
|
66
|
+
conditionTruth(condition: ExplainCondition, frame: Frame): ConditionResult;
|
|
67
|
+
conditionsTruth(entry: number | null, frame: Frame): ConditionsTruth;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Which of two scan-order keys runs first in one task: negative when `left` does. */
|
|
71
|
+
export const compareOrder = (
|
|
72
|
+
left: readonly number[],
|
|
73
|
+
right: readonly number[],
|
|
74
|
+
): number => {
|
|
75
|
+
for (let index = 0; index < Math.min(left.length, right.length); index++) {
|
|
76
|
+
const difference = (left[index] ?? 0) - (right[index] ?? 0);
|
|
77
|
+
if (difference !== 0) {
|
|
78
|
+
return difference;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return left.length - right.length;
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
export const frameKey = (frame: Frame): string =>
|
|
85
|
+
`${frame.row}:${frame.caller ? `${frame.caller.call}@${frameKey(frame.caller.frame)}` : ""}:${[...frame.counters].join(",")}`;
|
|
86
|
+
|
|
87
|
+
/** The frame `call`, at `position` of `frame`, runs routine row `row` in. */
|
|
88
|
+
const callFrame = (
|
|
89
|
+
row: number,
|
|
90
|
+
call: number,
|
|
91
|
+
position: number,
|
|
92
|
+
frame: Frame,
|
|
93
|
+
): Frame => ({
|
|
94
|
+
row,
|
|
95
|
+
caller: { call, frame },
|
|
96
|
+
task: frame.task,
|
|
97
|
+
order: [...frame.order, position],
|
|
98
|
+
counters: new Map(),
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
/** The innermost frame of a chain, each call running its first callee. */
|
|
102
|
+
export const rootFrame = (
|
|
103
|
+
{ file, tables }: Pick<Evaluation, "file" | "tables">,
|
|
104
|
+
chain: Chain,
|
|
105
|
+
): Frame => {
|
|
106
|
+
const start: Frame = {
|
|
107
|
+
row: chain.entry,
|
|
108
|
+
caller: null,
|
|
109
|
+
task: chain.task,
|
|
110
|
+
order: [tables.entries.get(chain.entry)?.order ?? 0],
|
|
111
|
+
counters: new Map(),
|
|
112
|
+
};
|
|
113
|
+
return chain.calls.reduce<Frame>((frame, call) => {
|
|
114
|
+
const record = file.calls[call];
|
|
115
|
+
const callee = record ? tables.calleesOf(record)[0]?.row : undefined;
|
|
116
|
+
return callFrame(callee ?? frame.row, call, record?.position ?? 0, frame);
|
|
117
|
+
}, start);
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
/** The frame a call runs its callee in, when the receiver picks one row. */
|
|
121
|
+
export const calleeFrame = (
|
|
122
|
+
evaluation: Evaluation,
|
|
123
|
+
call: number,
|
|
124
|
+
frame: Frame,
|
|
125
|
+
): Frame | UnknownOutcome => {
|
|
126
|
+
const record = evaluation.file.calls[call];
|
|
127
|
+
if (!record) {
|
|
128
|
+
return unknown("a call that isn't traced");
|
|
129
|
+
}
|
|
130
|
+
const callees = evaluation.tables.calleesOf(record);
|
|
131
|
+
if (record.receiver.kind === "pattern") {
|
|
132
|
+
const indexes = record.receiver.indexes.map((item) =>
|
|
133
|
+
evaluation.expression(item, frame),
|
|
134
|
+
);
|
|
135
|
+
const values = indexes.flatMap((item) =>
|
|
136
|
+
item.known && item.value.kind === "integer" ? [item.value.value] : [],
|
|
137
|
+
);
|
|
138
|
+
if (values.length !== indexes.length) {
|
|
139
|
+
return unknown(
|
|
140
|
+
"depends on which instance the call runs on",
|
|
141
|
+
merge(...indexes.map((item) => item.estimate)),
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
const match = callees.find((callee) =>
|
|
145
|
+
callee.indexes.every((value, position) => value === values[position]),
|
|
146
|
+
);
|
|
147
|
+
return match
|
|
148
|
+
? callFrame(match.row, call, record.position, frame)
|
|
149
|
+
: unknown("an instance this build doesn't have");
|
|
150
|
+
}
|
|
151
|
+
const [only] = callees;
|
|
152
|
+
return only && callees.length === 1
|
|
153
|
+
? callFrame(only.row, call, record.position, frame)
|
|
154
|
+
: unknown("a call this debugger can't follow");
|
|
155
|
+
};
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { loopIterations } from "@tsplc/iec/arithmetic";
|
|
2
|
+
import type { Evaluation, Frame } from "./explain-frames";
|
|
3
|
+
import {
|
|
4
|
+
and,
|
|
5
|
+
merge,
|
|
6
|
+
type Outcome,
|
|
7
|
+
type Truth,
|
|
8
|
+
type UnknownOutcome,
|
|
9
|
+
unknown,
|
|
10
|
+
} from "./explain-value";
|
|
11
|
+
|
|
12
|
+
/** The most iterations a loop is executed to follow it; longer loops are unknown. */
|
|
13
|
+
export const maxLoopIterations = 64;
|
|
14
|
+
|
|
15
|
+
/** What a loop's execution checks in each iteration. */
|
|
16
|
+
export type LoopWatch = (inner: Frame) => {
|
|
17
|
+
truth: Truth;
|
|
18
|
+
estimate: readonly string[];
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export type LoopRun =
|
|
22
|
+
| {
|
|
23
|
+
kind: "run";
|
|
24
|
+
/** The last iteration whose watched conditions held. */
|
|
25
|
+
ran: Frame | null;
|
|
26
|
+
returned: boolean;
|
|
27
|
+
estimate: string[];
|
|
28
|
+
}
|
|
29
|
+
| { kind: "unknown"; reason: string; estimate: string[] };
|
|
30
|
+
|
|
31
|
+
/** The value a loop's counter starts from, in `frame`. */
|
|
32
|
+
export const firstOf = (
|
|
33
|
+
evaluation: Evaluation,
|
|
34
|
+
loop: number,
|
|
35
|
+
frame: Frame,
|
|
36
|
+
): Outcome => {
|
|
37
|
+
const record = evaluation.file.loops[loop];
|
|
38
|
+
return record
|
|
39
|
+
? evaluation.expression(record.first, frame)
|
|
40
|
+
: unknown("a loop this debugger can't follow");
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Executes a loop in order: `watch`'s conditions are checked in each iteration. A loop runs
|
|
45
|
+
* until an iteration breaks or returns, or its iteration count; over the cap it is unknown.
|
|
46
|
+
*/
|
|
47
|
+
export const executeLoop = (
|
|
48
|
+
evaluation: Evaluation,
|
|
49
|
+
loop: number,
|
|
50
|
+
frame: Frame,
|
|
51
|
+
watch: LoopWatch | null,
|
|
52
|
+
): LoopRun => {
|
|
53
|
+
const { file, tables } = evaluation;
|
|
54
|
+
const record = file.loops[loop];
|
|
55
|
+
if (!record) {
|
|
56
|
+
return {
|
|
57
|
+
kind: "unknown",
|
|
58
|
+
reason: "a loop this debugger can't follow",
|
|
59
|
+
estimate: [],
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
if (record.parent !== null && !frame.counters.has(record.parent)) {
|
|
63
|
+
return {
|
|
64
|
+
kind: "unknown",
|
|
65
|
+
reason: "a loop inside another loop",
|
|
66
|
+
estimate: [],
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
const bounds = [record.first, record.limit, record.step].map((index) =>
|
|
70
|
+
evaluation.expression(index, frame),
|
|
71
|
+
);
|
|
72
|
+
const estimate = merge(...bounds.map((item) => item.estimate));
|
|
73
|
+
const missing = bounds.find((item): item is UnknownOutcome => !item.known);
|
|
74
|
+
if (missing) {
|
|
75
|
+
return { kind: "unknown", reason: missing.reason, estimate };
|
|
76
|
+
}
|
|
77
|
+
const [first, limit, step] = bounds.map((item) =>
|
|
78
|
+
item.known && item.value.kind === "integer" ? item.value.value : undefined,
|
|
79
|
+
);
|
|
80
|
+
if (
|
|
81
|
+
first === undefined ||
|
|
82
|
+
limit === undefined ||
|
|
83
|
+
step === undefined ||
|
|
84
|
+
step === 0n
|
|
85
|
+
) {
|
|
86
|
+
return {
|
|
87
|
+
kind: "unknown",
|
|
88
|
+
reason: "a loop this debugger can't follow",
|
|
89
|
+
estimate,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
const iterations = loopIterations(first, limit, record.exclusive, step);
|
|
93
|
+
if (iterations > BigInt(Math.min(record.maxIterations, maxLoopIterations))) {
|
|
94
|
+
return { kind: "unknown", reason: "loop too long", estimate };
|
|
95
|
+
}
|
|
96
|
+
const returns = file.returns.filter(
|
|
97
|
+
(item) =>
|
|
98
|
+
item.routine === frame.row &&
|
|
99
|
+
tables
|
|
100
|
+
.conditionList(item.conditions)
|
|
101
|
+
.some(
|
|
102
|
+
(part) =>
|
|
103
|
+
part.kind === "loop" &&
|
|
104
|
+
tables.loopAt(frame.row, part.loop) === loop,
|
|
105
|
+
),
|
|
106
|
+
);
|
|
107
|
+
let ran: Frame | null = null;
|
|
108
|
+
const seen = [...estimate];
|
|
109
|
+
for (let iteration = 0n; iteration < iterations; iteration++) {
|
|
110
|
+
const inner: Frame = {
|
|
111
|
+
...frame,
|
|
112
|
+
counters: new Map([...frame.counters, [loop, first + iteration * step]]),
|
|
113
|
+
};
|
|
114
|
+
if (watch !== null) {
|
|
115
|
+
const truth = watch(inner);
|
|
116
|
+
seen.push(...truth.estimate);
|
|
117
|
+
if (truth.truth === "true") {
|
|
118
|
+
ran = inner;
|
|
119
|
+
} else if (truth.truth !== "false") {
|
|
120
|
+
return {
|
|
121
|
+
kind: "unknown",
|
|
122
|
+
reason: "depends on a condition inside the loop",
|
|
123
|
+
estimate: merge(seen),
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
const breaks = record.breaks.map((all) =>
|
|
128
|
+
and(all.map((part) => evaluation.conditionTruth(part, inner).truth)),
|
|
129
|
+
);
|
|
130
|
+
const returned = returns.map(
|
|
131
|
+
(item) => evaluation.conditionsTruth(item.conditions, inner).truth,
|
|
132
|
+
);
|
|
133
|
+
const stops = [...breaks, ...returned];
|
|
134
|
+
if (stops.includes("true")) {
|
|
135
|
+
return {
|
|
136
|
+
kind: "run",
|
|
137
|
+
ran,
|
|
138
|
+
returned: returned.includes("true"),
|
|
139
|
+
estimate: merge(seen),
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
if (stops.includes("unknown") || stops.includes("candidate")) {
|
|
143
|
+
return {
|
|
144
|
+
kind: "unknown",
|
|
145
|
+
reason: "can't tell whether the loop stopped",
|
|
146
|
+
estimate: merge(seen),
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return { kind: "run", ran, returned: false, estimate: merge(seen) };
|
|
151
|
+
};
|