wuica-cli 0.1.9 → 0.1.12
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 +140 -40
- package/dist/data/arch/ADL-P.bin.br +0 -0
- package/dist/data/arch/ICX.bin.br +0 -0
- package/dist/data/arch/ZEN3.bin.br +0 -0
- package/dist/data/uarch.json.br +0 -0
- package/dist/manifest.json +19 -19
- package/dist/wuica_wasm.wasm.br +0 -0
- package/index.d.ts +175 -15
- package/lib/cli.js +205 -44
- package/lib/core.js +9 -1
- package/lib/details.js +4 -23
- package/lib/examples.js +1 -0
- package/lib/files.js +2 -1
- package/lib/index.js +120 -21
- package/lib/mcp.js +108 -85
- package/lib/objfile.js +51 -11
- package/lib/thread-worker.js +9 -6
- package/lib/threads.js +8 -2
- package/lib/wasm.js +12 -6
- package/lib/web/ab-diff.js +217 -0
- package/lib/web/accum.js +10 -2
- package/lib/web/asm-regions.js +27 -20
- package/lib/web/asm-tokens.js +357 -0
- package/lib/web/assembler.js +36 -6
- package/lib/web/block-diff.js +165 -0
- package/lib/web/bound-core.js +211 -0
- package/lib/web/branch-label.js +155 -0
- package/lib/web/caveats.js +107 -3
- package/lib/web/details.js +91 -0
- package/lib/web/examples.js +5 -1
- package/lib/web/loop-find.js +398 -35
- package/lib/web/markdown.js +437 -0
- package/lib/web/whatif-core.js +339 -0
- package/package.json +7 -2
- package/lib/insights.js +0 -100
- package/lib/whatif.js +0 -136
package/lib/index.js
CHANGED
|
@@ -12,7 +12,9 @@ import path from 'node:path';
|
|
|
12
12
|
import { fileURLToPath } from 'node:url';
|
|
13
13
|
import { Analyzer, Assembler, parseDiagnostics, codeModeError } from './wasm.js';
|
|
14
14
|
import { archList, DEFAULT_ARCH, ARCH_INFO, ARCH_ORDER } from './archs.js';
|
|
15
|
-
import { insights } from './
|
|
15
|
+
import { insights } from './web/bound-core.js';
|
|
16
|
+
import { cannotRun, noData } from './web/caveats.js';
|
|
17
|
+
import { analyzedLines, analyzedText } from './web/branch-label.js';
|
|
16
18
|
import { instrDetails, detailInsights } from './details.js';
|
|
17
19
|
export { detailInsights };
|
|
18
20
|
import { readMaybeCompressed, readMaybeCompressedSync, tableFile } from './files.js';
|
|
@@ -21,21 +23,33 @@ import { CODES, WARM_HEX, failure, tableFailure, analysisResult, compareFields }
|
|
|
21
23
|
import { ThreadPool, threadLimit, threadOption, threadsForWork } from './threads.js';
|
|
22
24
|
// the web page's reading of pasted assembly (copies of web/js in lib/web: tests-js/web_pkg_copies.mjs): the loop of a whole function, objdump lines,
|
|
23
25
|
// exits as not taken, ';' comments, region markers
|
|
24
|
-
import { assembleMarked, commentFixes, separatorNotes } from './web/asm-regions.js';
|
|
26
|
+
import { assembleMarked, commentFixes, separatorNotes, spanLines } from './web/asm-regions.js';
|
|
25
27
|
import { normalizeText, detectSyntax, preparePaste, syntaxSigns } from './web/loop-find.js';
|
|
26
28
|
export { detectSyntax };
|
|
27
29
|
|
|
28
30
|
// The loops of assembly text, as `loop` and `func` choose among them: [{index, label, function, line, endLine,
|
|
29
31
|
// instructions, depth, auto}] (auto: the one "auto" analyzes) or {error}. No assembling.
|
|
32
|
+
/** @param {string} source @param {{func?: string | null}} [opts] @returns {any} (index.d.ts: LoopListing[] | {error}) */
|
|
30
33
|
export function listLoops(source, { func = null } = {}) {
|
|
34
|
+
if (typeof source !== 'string') return { error: '`source` must be assembly text' };
|
|
31
35
|
const p = preparePaste(normalizeText(source), { loop: 'auto', func });
|
|
32
36
|
if (p.error) return { error: p.error };
|
|
33
|
-
return p.loops
|
|
37
|
+
return loopListing(p.loops, p.choice);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// The loops of a text (loop-find.js preparePaste's, or what an assembly of it found) as listLoops gives them; the one
|
|
41
|
+
// implementation behind -loops, the MCP tool list_loops and the analyze response's otherLoops.
|
|
42
|
+
function loopListing(loops, choice) {
|
|
43
|
+
return loops.map((l, index) => ({ index, label: l.label, function: l.function, line: l.begin, endLine: l.end, instructions: l.instructions, depth: l.depth, auto: choice === index }));
|
|
34
44
|
}
|
|
35
45
|
|
|
36
46
|
export { ARCH_INFO, ARCH_ORDER, DEFAULT_ARCH, CODES, WARM_HEX, parseDiagnostics, insights };
|
|
37
|
-
export { whatIf, VARIANTS as WHAT_IF_VARIANTS } from './whatif.js';
|
|
47
|
+
export { whatIf, VARIANTS as WHAT_IF_VARIANTS, OPT_IN_VARIANTS as WHAT_IF_OPT_IN_VARIANTS, whatIfPerElement, whatIfText, whatIfRequest } from './web/whatif-core.js';
|
|
48
|
+
export { diffResult, diffAnalyses, diffRequest, diffMarkdown, diffText } from './web/block-diff.js';
|
|
49
|
+
import { diffRequest } from './web/block-diff.js';
|
|
50
|
+
import { whatIfRequest } from './web/whatif-core.js';
|
|
38
51
|
export { examples, findExample } from './examples.js';
|
|
52
|
+
export * as markdown from './web/markdown.js';
|
|
39
53
|
export { objectKind } from './objfile.js';
|
|
40
54
|
|
|
41
55
|
const DIST = path.join(path.dirname(fileURLToPath(import.meta.url)), '..', 'dist');
|
|
@@ -121,7 +135,7 @@ export function stderrLines(message) {
|
|
|
121
135
|
const REQUEST_KEYS = ['source', 'asm', 'hex', 'syntax', 'loop', 'func', 'options'];
|
|
122
136
|
|
|
123
137
|
// `loop` of a request: what of the source is analyzed (web/js/loop-find.js). null if it is not one of these.
|
|
124
|
-
export const LOOP_CHOICES = Object.freeze(['auto', 'whole']);
|
|
138
|
+
export const LOOP_CHOICES = /** @type {readonly ['auto', 'whole']} */ (Object.freeze(['auto', 'whole']));
|
|
125
139
|
// (a string other than these is a loop's label: ".LBB0_12", "0x10")
|
|
126
140
|
const loopChoice = (v) => (v === undefined || v === null ? 'auto' : LOOP_CHOICES.includes(v) || (Number.isSafeInteger(v) && v >= 0) || (typeof v === 'string' && v.trim() !== '') ? v : null);
|
|
127
141
|
const LOOP_ERROR = '`loop` must be "auto", "whole", the index of a loop (0-based) or its label';
|
|
@@ -150,6 +164,7 @@ function pickOptions(req, extraKeys) {
|
|
|
150
164
|
// a value of the wrong kind is a bad request (code 1), like an unknown option, not a failed analysis
|
|
151
165
|
const bad = typeof d === 'number' ? !Number.isSafeInteger(v) : typeof d === 'boolean' ? typeof v !== 'boolean'
|
|
152
166
|
: k === 'initPolicy' && !['diff', 'same', 'stack'].includes(v);
|
|
167
|
+
if (bad && typeof d === 'number' && Number.isInteger(v)) return { error: `option ${k}: ${v} is out of range (at most 2^53 - 1 in magnitude)` };
|
|
153
168
|
if (bad) return { error: `option ${k}: ${JSON.stringify(v)} is not ${typeof d === 'number' ? 'an integer' : typeof d === 'boolean' ? 'true or false' : 'one of "diff", "same", "stack"'}` };
|
|
154
169
|
out[k] = v;
|
|
155
170
|
}
|
|
@@ -157,6 +172,33 @@ function pickOptions(req, extraKeys) {
|
|
|
157
172
|
}
|
|
158
173
|
|
|
159
174
|
|
|
175
|
+
// A compare() result with cycles per element (`per` elements per iteration, as the CLI's -arch with -per):
|
|
176
|
+
// elementsPerIteration on it and perElement (tp / per) on each row that has a tp. Unchanged without `per`.
|
|
177
|
+
export function comparePerElement(c, per) {
|
|
178
|
+
if (!per || c.code) return c;
|
|
179
|
+
return { ...c, elementsPerIteration: per, results: c.results.map((r) => (r.code === CODES.OK ? { ...r, perElement: r.tp / per } : r)) };
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// The rows of an analysis with where each instruction came from (assembly input): `line` (1-based, of the text as given)
|
|
183
|
+
// and `loc` ("file.c:12", from .loc/.file directives). Only when the counts fit the instruction rows exactly (from the
|
|
184
|
+
// loop finder and the assembler, never by matching text); unchanged otherwise.
|
|
185
|
+
function withSourceLines(r, src) {
|
|
186
|
+
if (!src || !Array.isArray(r.rows)) return r;
|
|
187
|
+
const instr = r.rows.filter((x) => x.kind === 'instr');
|
|
188
|
+
const lines = Array.isArray(src.lines) && src.lines.length === instr.length ? src.lines : null;
|
|
189
|
+
const locs = src.loc?.spans?.length ? spanLines(r.rows, src.loc.spans) : null;
|
|
190
|
+
const file = src.loc?.file ?? null;
|
|
191
|
+
if (!lines && !(locs && file)) return r;
|
|
192
|
+
let k = 0;
|
|
193
|
+
const rows = r.rows.map((x) => {
|
|
194
|
+
if (x.kind !== 'instr') return x;
|
|
195
|
+
const i = k++;
|
|
196
|
+
const info = lines && src.info ? src.info.get(lines[i]) : undefined; // objdump -S, perf annotate: source, samples
|
|
197
|
+
return { ...x, ...(lines ? { line: lines[i] } : {}), ...(locs && file && locs[i] ? { loc: `${file}:${locs[i]}` } : {}), ...(info ?? {}) };
|
|
198
|
+
});
|
|
199
|
+
return { ...r, rows };
|
|
200
|
+
}
|
|
201
|
+
|
|
160
202
|
// A list of arch names without repeats (the same id, or the same unknown name), in the order of their first appearance.
|
|
161
203
|
function uniqueArchs(list, resolve) {
|
|
162
204
|
const seen = new Set();
|
|
@@ -173,12 +215,14 @@ class Wuica {
|
|
|
173
215
|
#archs;
|
|
174
216
|
#dataDir;
|
|
175
217
|
#asmWasm;
|
|
218
|
+
/** @type {Promise<any> | null} */
|
|
176
219
|
#assembler = null; // promise of an Assembler
|
|
177
220
|
#loading = new Map(); // table name -> promise
|
|
178
221
|
#wasm; // bytes of the analyzer module (the threads compile it with a memory limit of their own)
|
|
179
222
|
#uarch; // bytes of uarch.json
|
|
180
223
|
#threads; // the most threads compare() uses (1: none)
|
|
181
224
|
#threadsAuto; // no number given: at most one thread per two microarchitectures (threads.js, threadsFor)
|
|
225
|
+
/** @type {import('./threads.js').ThreadPool | null} */
|
|
182
226
|
#pool = null; // ThreadPool, started by the first compare() that uses threads
|
|
183
227
|
|
|
184
228
|
constructor(analyzer, configs, dataDir, asmWasm, { wasm, uarch, threads, threadsAuto }) {
|
|
@@ -240,6 +284,7 @@ class Wuica {
|
|
|
240
284
|
}
|
|
241
285
|
|
|
242
286
|
// POST /api/assemble {source, syntax}
|
|
287
|
+
/** @param {string} source @param {string} [syntax] @returns {Promise<any>} */
|
|
243
288
|
async assemble(source, syntax = 'auto') {
|
|
244
289
|
if (typeof source !== 'string') return failure(CODES.REQUEST, '`source` must be a string');
|
|
245
290
|
if (syntaxCode(syntax) < 0) return failure(CODES.REQUEST, SYNTAX_ERROR(syntax));
|
|
@@ -274,6 +319,7 @@ class Wuica {
|
|
|
274
319
|
// lines, exits as not taken, ';' comments, a marked region (web/js/loop-find.js, asm-regions.js). loop: 'auto',
|
|
275
320
|
// 'whole', a loop's index or its label; func: the function whose loops count (or null).
|
|
276
321
|
// -> {code: 0, hex, bytes, stderr, loop, syntax} (loop: what was picked, or null; syntax: {name, detected}) or a failure.
|
|
322
|
+
/** @param {string} source @param {string} [syntax] @param {string | number} [loop] @param {string | null} [func] @returns {Promise<any>} */
|
|
277
323
|
async machineCode(source, syntax = 'auto', loop = 'auto', func = null) {
|
|
278
324
|
if (typeof source !== 'string') return failure(CODES.REQUEST, '`source` must be a string');
|
|
279
325
|
if (syntaxCode(syntax) < 0) return failure(CODES.REQUEST, SYNTAX_ERROR(syntax));
|
|
@@ -292,7 +338,7 @@ class Wuica {
|
|
|
292
338
|
const r = await assembleMarked(assemble, text, { loop: loopChoice(loop), func });
|
|
293
339
|
if (trap) return failure(CODES.ASSEMBLER, trap, undefined, { trapped: true });
|
|
294
340
|
if (!r.ok) {
|
|
295
|
-
const bad = r.diagnostics.find((d) => /^(?:loop|func): /.test(d.message));
|
|
341
|
+
const bad = r.diagnostics.find((d) => /^(?:loop|func|input): /.test(d.message));
|
|
296
342
|
if (bad) return failure(CODES.REQUEST, bad.message);
|
|
297
343
|
if (looksAArch64(text)) return failure(CODES.ASSEMBLER, 'this looks like AArch64 assembly; wuica analyzes x86-64 (compile with --target=x86_64-linux-gnu)');
|
|
298
344
|
const first = r.diagnostics.find((d) => d.severity === 'error');
|
|
@@ -309,7 +355,12 @@ class Wuica {
|
|
|
309
355
|
const loopInfo = picked && { index: picked.index, count: picked.count, name: picked.name, line: picked.line, endLine: picked.endLine, ...(picked.lines ? { lines: picked.lines } : {}),
|
|
310
356
|
...(info ? { function: info.function, depth: info.depth, instructions: info.instructions } : {}),
|
|
311
357
|
...(picked.address !== null && picked.address !== undefined ? { address: picked.address } : {}), ...(r.alignment !== null ? { alignmentOffset: r.alignment } : {}) };
|
|
312
|
-
|
|
358
|
+
// the other loops of the text, when it has more than one and one of them was analyzed (with -loop whole every loop
|
|
359
|
+
// is in the analyzed code; a marked region has none)
|
|
360
|
+
const otherLoops = r.loops.length > 1 && Number.isInteger(picked?.index) ? loopListing(r.loops, null).filter((l) => l.index !== picked?.index).map(({ auto, ...l }) => l) : null;
|
|
361
|
+
// where each instruction came from: its line of the text (the loop finder's), and its .loc line (the assembler's)
|
|
362
|
+
const sourceLines = { lines: r.lines ?? null, loc: r.source ?? null, info: r.lineInfo ?? null };
|
|
363
|
+
return { code: CODES.OK, hex: Buffer.from(r.bytes).toString('hex'), bytes: r.bytes, stderr: diagnosticLines(r.diagnostics), loop: loopInfo, syntax: { name: syn.name, detected: syn.detected }, ...(otherLoops ? { otherLoops } : {}), sourceLines };
|
|
313
364
|
}
|
|
314
365
|
|
|
315
366
|
// {source | asm | hex, syntax, loop} -> {hex, assembled, stderr, loop} or a failure.
|
|
@@ -332,7 +383,9 @@ class Wuica {
|
|
|
332
383
|
const a = await this.machineCode(source, req.syntax ?? 'auto', loopChoice(req.loop), req.func ?? null);
|
|
333
384
|
if (a.code !== CODES.OK) return a;
|
|
334
385
|
if (a.bytes.length === 0) return failure(CODES.REQUEST, 'the source contains no instructions');
|
|
335
|
-
|
|
386
|
+
// (analyzed: the text of what was analyzed, which names an extension a CPU lacks: web/caveats.js cannotRun)
|
|
387
|
+
const analyzed = typeof source === 'string' ? analyzedText(analyzedLines(normalizeText(source).replace(/^\uFEFF/, ''), { loop: a.loop })) : null;
|
|
388
|
+
return { hex: a.hex, assembled: true, stderr: a.stderr, loop: a.loop, syntax: a.syntax.name, ...(a.otherLoops ? { otherLoops: a.otherLoops } : {}), sourceLines: a.sourceLines, analyzed };
|
|
336
389
|
}
|
|
337
390
|
|
|
338
391
|
// The options of a request with the alignment offset of the code's addresses (objdump input), unless it was given.
|
|
@@ -368,9 +421,12 @@ class Wuica {
|
|
|
368
421
|
// analyze()'s result from the code (#code) and the analysis (#run)
|
|
369
422
|
static #analyzeResult(code, r) {
|
|
370
423
|
if (r.code !== undefined) return r;
|
|
424
|
+
r = withSourceLines(r, code.sourceLines);
|
|
371
425
|
const stderr = [...code.stderr];
|
|
372
426
|
if (r.decodeErrors > 0) stderr.push({ text: `Warning: ${r.decodeErrors} position(s) could not be decoded` });
|
|
373
|
-
|
|
427
|
+
// code this CPU cannot run (web/caveats.js cannotRun): the number is the reference's, but not a result for it
|
|
428
|
+
const bad = cannotRun(r, { arch: r.arch, code: code.analyzed ?? null }), gaps = noData(r, { arch: r.arch });
|
|
429
|
+
return { code: CODES.OK, ...r, ...(bad ? { unsupported: bad.reason } : {}), ...(gaps ? { noData: gaps } : {}), ...(code.assembled ? { hex: code.hex, syntax: code.syntax } : {}), ...(code.loop ? { loop: code.loop } : {}), ...(code.otherLoops ? { otherLoops: code.otherLoops } : {}), stderr };
|
|
374
430
|
}
|
|
375
431
|
|
|
376
432
|
// Several blocks on one microarchitecture: [request] -> [analyze() result] in the same order (each exactly what
|
|
@@ -389,6 +445,7 @@ class Wuica {
|
|
|
389
445
|
// Several blocks on several microarchitectures: [request] -> [compare() result] in the same order (each exactly what
|
|
390
446
|
// compare(request) returns; `archs` is that of each request, else of the call, else all). Every block x CPU is one
|
|
391
447
|
// job for the threads.
|
|
448
|
+
/** @param {any[]} requests @param {{archs?: string[]}} [opts] */
|
|
392
449
|
async compareMany(requests, { archs } = {}) {
|
|
393
450
|
if (!Array.isArray(requests)) return failure(CODES.REQUEST, '`requests` must be an array');
|
|
394
451
|
const prepared = await this.#prepare(requests, ['archs']);
|
|
@@ -458,17 +515,24 @@ class Wuica {
|
|
|
458
515
|
const results = ids.map((arch, i) => {
|
|
459
516
|
const id = this.resolveArch(arch);
|
|
460
517
|
const { name, year, prototype } = id ? this.#info(id) : { name: String(arch), year: null, prototype: false };
|
|
461
|
-
|
|
518
|
+
const { xOps, ...f } = fields[i];
|
|
519
|
+
// code this CPU cannot run (web/caveats.js cannotRun): `unsupported`, the reason; else instructions without data
|
|
520
|
+
// (gaps in its tables): `noData`, their count
|
|
521
|
+
const s = { decodeErrors: f.decodeErrors, xOps: xOps ?? [] };
|
|
522
|
+
const bad = f.code === CODES.OK ? cannotRun(s, { arch: id, code: code.analyzed ?? null }) : null;
|
|
523
|
+
const gaps = f.code === CODES.OK ? noData(s, { arch: id }) : 0;
|
|
524
|
+
return { arch: id ?? String(arch), name, year, prototype, ...f, ...(bad ? { unsupported: bad.reason } : {}), ...(gaps ? { noData: gaps } : {}) };
|
|
462
525
|
});
|
|
463
526
|
// a CPU that could not decode some instructions (FMA on SNB): its throughput leaves them out
|
|
464
527
|
const decode = results.filter((r) => r.code === CODES.OK && r.decodeErrors > 0)
|
|
465
528
|
.map((r) => ({ text: `Warning: ${r.arch}: ${r.decodeErrors} position(s) could not be decoded (instructions it does not have): its throughput leaves them out` }));
|
|
466
|
-
return { code: CODES.OK, ...(code.assembled ? { hex: code.hex, syntax: code.syntax } : {}), ...(code.loop ? { loop: code.loop } : {}), stderr: [...code.stderr, ...decode], results };
|
|
529
|
+
return { code: CODES.OK, ...(code.assembled ? { hex: code.hex, syntax: code.syntax } : {}), ...(code.loop ? { loop: code.loop } : {}), ...(code.otherLoops ? { otherLoops: code.otherLoops } : {}), stderr: [...code.stderr, ...decode], results };
|
|
467
530
|
}
|
|
468
531
|
|
|
469
532
|
// POST /api/compare {source | hex, syntax, archs?, options}: one block on several microarchitectures (default: all).
|
|
533
|
+
/** @param {any} [request] @returns {Promise<any>} */
|
|
470
534
|
async compare(request = {}) {
|
|
471
|
-
const opts = pickOptions(request, ['archs']);
|
|
535
|
+
const opts = pickOptions(request, ['archs', 'per']);
|
|
472
536
|
if (opts.error) return failure(CODES.REQUEST, opts.error);
|
|
473
537
|
const code = await this.#code(request);
|
|
474
538
|
if (code.code !== undefined) return code;
|
|
@@ -476,8 +540,10 @@ class Wuica {
|
|
|
476
540
|
const ids = request.archs?.length ? uniqueArchs(request.archs, (a) => this.resolveArch(a)) : this.#archs.map((a) => a.id);
|
|
477
541
|
const options = { ...Wuica.#withAlignment(request, opts.options, code), includeTrace: false, details: false };
|
|
478
542
|
// each microarchitecture as #run() would do it here, on the threads when the work is worth it (#runJobs)
|
|
543
|
+
const per = request.per;
|
|
544
|
+
if (per !== undefined && !(typeof per === 'number' && Number.isFinite(per) && per > 0)) return failure(CODES.REQUEST, '`per` must be a positive number (elements per iteration)');
|
|
479
545
|
const fields = await this.#runJobs(ids.map((arch) => ({ hex: code.hex, arch, options })));
|
|
480
|
-
return this.#compareResult(code, ids, fields);
|
|
546
|
+
return comparePerElement(this.#compareResult(code, ids, fields), per);
|
|
481
547
|
}
|
|
482
548
|
|
|
483
549
|
// Stops compare()'s threads, if any (they also stop by themselves when idle, and never keep the process alive while
|
|
@@ -489,21 +555,36 @@ class Wuica {
|
|
|
489
555
|
// The number of threads compare() has running now (0 if none).
|
|
490
556
|
get threads() { return this.#pool?.threads ?? 0; }
|
|
491
557
|
|
|
492
|
-
// The derived diagnosis of an analyze() response (
|
|
558
|
+
// The derived diagnosis of an analyze() response (web/bound-core.js) with this CPU's configuration (the split-load caveat
|
|
493
559
|
// depends on it); null for a failure.
|
|
560
|
+
// The what-if experiments of a block (web/js/whatif-core.js whatIfRequest): request as analyze's, with `variants` and
|
|
561
|
+
// `per`. The MCP tool what_if and POST /api/what-if/<arch> give the same.
|
|
562
|
+
/** @param {string} arch @param {any} [request] @returns {Promise<any>} */
|
|
563
|
+
async whatIf(arch, request = {}) {
|
|
564
|
+
return whatIfRequest(this, arch, request);
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
// Two blocks on one microarchitecture (web/js/block-diff.js): {a: block, b: block, options?, per?} -> the change of
|
|
568
|
+
// throughput, bottlenecks, limits, ports and instructions. wuica diff, the MCP tool diff_blocks and POST /api/diff/<arch>.
|
|
569
|
+
/** @param {string} arch @param {any} request @returns {Promise<any>} */
|
|
570
|
+
async diff(arch, request) {
|
|
571
|
+
return diffRequest(this, arch, request);
|
|
572
|
+
}
|
|
573
|
+
|
|
494
574
|
insights(r) {
|
|
495
575
|
return r?.code === CODES.OK ? insights(r, this.#configs[r.arch] ?? null) : null;
|
|
496
576
|
}
|
|
497
577
|
|
|
498
578
|
// The compact form of an analyze() response (used by the MCP server): uop table reduced to non-zero values, plus the
|
|
499
|
-
// derived diagnosis of the web page's bound view (
|
|
579
|
+
// derived diagnosis of the web page's bound view (web/bound-core.js).
|
|
500
580
|
summarize(r) {
|
|
501
581
|
if (r.code !== CODES.OK) return r;
|
|
502
582
|
const { name, year, prototype } = this.#info(r.arch);
|
|
503
583
|
const round = (x) => Math.round(x * 100) / 100;
|
|
504
584
|
const sources = new Set(['MITE', 'MS', 'DSB', 'LSD']);
|
|
505
585
|
const instructions = r.rows.map((row, k) => {
|
|
506
|
-
|
|
586
|
+
/** @type {Record<string, any>} */
|
|
587
|
+
const e = { instr: row.label, ...(row.line ? { line: row.line } : {}), ...(row.loc ? { loc: row.loc } : {}), ...(row.source !== undefined ? { source: row.source } : {}), ...(row.samples !== undefined ? { samples: row.samples } : {}) };
|
|
507
588
|
if (row.opcode) e.bytes = row.opcode.toLowerCase();
|
|
508
589
|
if (row.kind !== 'instr') e.kind = row.kind;
|
|
509
590
|
const uopSource = {}, ports = {};
|
|
@@ -527,15 +608,18 @@ class Wuica {
|
|
|
527
608
|
const ins = base ? { ...base, ...detailInsights(r) } : null;
|
|
528
609
|
return {
|
|
529
610
|
code: r.code, arch: r.arch, name, year, prototype, tp: r.tp, unit: 'cycles per iteration', mode: r.mode,
|
|
530
|
-
bottlenecks: r.bottlenecks, limits: r.limits, decodeErrors: r.decodeErrors, ...(r.hex ? { hex: r.hex } : {}), ...(r.loop ? { loop: r.loop } : {}),
|
|
611
|
+
bottlenecks: r.bottlenecks, limits: r.limits, decodeErrors: r.decodeErrors, ...(r.unsupported ? { unsupported: r.unsupported } : {}), ...(r.noData ? { noData: r.noData } : {}), ...(r.hex ? { hex: r.hex } : {}), ...(r.loop ? { loop: r.loop } : {}), ...(r.otherLoops ? { otherLoops: r.otherLoops } : {}),
|
|
531
612
|
stderr: r.stderr, instructions, ...(ins ? { insights: ins } : {}),
|
|
532
613
|
};
|
|
533
614
|
}
|
|
534
615
|
|
|
535
616
|
// An object file (ELF or Mach-O, x86-64; objfile.js) as llvm-objdump -d's text of its code, which analyze() and
|
|
536
617
|
// compare() read as `source` (the objdump path: the loop at its address, exits stubbed, the alignment from it).
|
|
537
|
-
// -> {code: 0, text, format, functions: [{name, section, address, size}]} or a failure (another architecture
|
|
538
|
-
// to compile for x86-64).
|
|
618
|
+
// -> {code: 0, text, format, functions: [{name, section, address, size[, symbol]}]} or a failure (another architecture
|
|
619
|
+
// says how to compile for x86-64). A name the file gives several functions is NAME.0xADDR, with `symbol` the name in
|
|
620
|
+
// the file (objfile.js uniqueNames). `func`: also resolves a -func name (exact, without Mach-O's "_", or a unique
|
|
621
|
+
// part; a repeated name is an error that lists the functions): `func`, and `text` is that function's alone.
|
|
622
|
+
/** @param {Uint8Array} bytes @param {{name?: string, func?: string | null}} [opts] @returns {any} */
|
|
539
623
|
disassemble(bytes, { name = 'input', func = null } = {}) {
|
|
540
624
|
const obj = readObject(bytes);
|
|
541
625
|
if (obj.error) return failure(CODES.REQUEST, obj.error);
|
|
@@ -546,13 +630,27 @@ class Wuica {
|
|
|
546
630
|
if (r.error) return failure(CODES.REQUEST, r.error);
|
|
547
631
|
resolved = r.name;
|
|
548
632
|
}
|
|
633
|
+
// with a function: the text of that function alone. Its loops are those of the whole text's that lie in it (not a
|
|
634
|
+
// span from it into another function, which the whole text took for a loop: a branch to a .cold part placed before
|
|
635
|
+
// it), and it stays valid and small whatever else the file has (a static executable: thousands of functions,
|
|
636
|
+
// megabytes of text).
|
|
637
|
+
// A function of one instruction comes with the function after it (or before it, the last one): text with a single
|
|
638
|
+
// instruction line is not read as objdump output (loop-find.js fromObjdump)
|
|
639
|
+
const decode = (b) => this.#analyzer.decode(b, 'ALL');
|
|
549
640
|
let text;
|
|
550
641
|
try {
|
|
551
|
-
text = objdumpText(obj,
|
|
642
|
+
if (resolved === null) text = objdumpText(obj, decode, name);
|
|
643
|
+
else {
|
|
644
|
+
const k = obj.functions.findIndex((f) => f.name === resolved);
|
|
645
|
+
text = objdumpText({ ...obj, functions: [obj.functions[k]] }, decode, name);
|
|
646
|
+
const lines = (s) => s.split('\n').filter((l) => /^ *[0-9a-f]+: /.test(l)).length;
|
|
647
|
+
const other = obj.functions[k + 1] ?? obj.functions[k - 1];
|
|
648
|
+
if (lines(text) < 2 && other) text = objdumpText({ ...obj, functions: obj.functions.filter((f) => f === other || f.name === resolved) }, decode, name);
|
|
649
|
+
}
|
|
552
650
|
} catch (e) {
|
|
553
651
|
return failure(CODES.ANALYSIS, `the decoder failed (${e.message})`);
|
|
554
652
|
}
|
|
555
|
-
return { code: CODES.OK, text, format: obj.format, functions: obj.functions.map(({ name: n, section, address, size }) => ({ name: n, section, address, size })), ...(resolved !== null ? { func: resolved } : {}) };
|
|
653
|
+
return { code: CODES.OK, text, format: obj.format, functions: obj.functions.map(({ name: n, section, address, size, symbol }) => ({ name: n, section, address, size, ...(symbol !== undefined ? { symbol } : {}) })), ...(resolved !== null ? { func: resolved } : {}) };
|
|
556
654
|
}
|
|
557
655
|
|
|
558
656
|
get restarts() { return this.#analyzer.restarts; }
|
|
@@ -563,6 +661,7 @@ class Wuica {
|
|
|
563
661
|
// options.threads: the most worker threads compare() uses, one per microarchitecture (default: the environment variable
|
|
564
662
|
// WUICA_THREADS, else "auto": os.availableParallelism(), and one thread per two microarchitectures; 1 = none, every
|
|
565
663
|
// microarchitecture one after another on this thread).
|
|
664
|
+
/** @param {import('../index.d.ts').CreateOptions} [options] */
|
|
566
665
|
export async function createWuica(options = {}) {
|
|
567
666
|
const threads = threadLimit(options.threads);
|
|
568
667
|
const threadsAuto = threadOption(options.threads) === 'auto';
|