wuica-cli 0.1.7 → 0.1.10

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 (64) hide show
  1. package/README.md +233 -57
  2. package/dist/data/arch/ADL-P.bin.br +0 -0
  3. package/dist/data/arch/BDW.bin.br +0 -0
  4. package/dist/data/arch/CFL.bin.br +0 -0
  5. package/dist/data/arch/CLX.bin.br +0 -0
  6. package/dist/data/arch/HSW.bin.br +0 -0
  7. package/dist/data/arch/ICL.bin.br +0 -0
  8. package/dist/data/arch/ICX.bin.br +0 -0
  9. package/dist/data/arch/IVB.bin.br +0 -0
  10. package/dist/data/arch/KBL.bin.br +0 -0
  11. package/dist/data/arch/RKL.bin.br +0 -0
  12. package/dist/data/arch/SKL.bin.br +0 -0
  13. package/dist/data/arch/SKX.bin.br +0 -0
  14. package/dist/data/arch/SNB.bin.br +0 -0
  15. package/dist/data/arch/TGL.bin.br +0 -0
  16. package/dist/data/arch/ZEN3.bin.br +0 -0
  17. package/dist/data/uarch.json.br +0 -0
  18. package/dist/manifest.json +145 -66
  19. package/dist/wuica_wasm.wasm.br +0 -0
  20. package/index.d.ts +205 -13
  21. package/lib/cli.js +467 -49
  22. package/lib/core.js +9 -1
  23. package/lib/details.js +4 -23
  24. package/lib/examples.js +27 -0
  25. package/lib/files.js +26 -3
  26. package/lib/index.js +228 -42
  27. package/lib/mcp.js +107 -84
  28. package/lib/objfile.js +218 -0
  29. package/lib/thread-worker.js +15 -9
  30. package/lib/threads.js +40 -23
  31. package/lib/wasm.js +42 -11
  32. package/lib/web/ab-diff.js +217 -0
  33. package/lib/web/accum.js +10 -2
  34. package/lib/web/asm-regions.js +24 -17
  35. package/lib/web/asm-tokens.js +357 -0
  36. package/lib/web/assembler.js +36 -6
  37. package/lib/web/block-diff.js +165 -0
  38. package/lib/web/bound-core.js +211 -0
  39. package/lib/web/branch-label.js +155 -0
  40. package/lib/web/caveats.js +115 -3
  41. package/lib/web/details.js +91 -0
  42. package/lib/web/examples.js +103 -0
  43. package/lib/web/loop-find.js +139 -16
  44. package/lib/web/markdown.js +437 -0
  45. package/lib/web/showcase.js +368 -0
  46. package/lib/web/whatif-core.js +339 -0
  47. package/package.json +3 -1
  48. package/dist/data/arch/ADL-P.json.br +0 -0
  49. package/dist/data/arch/BDW.json.br +0 -0
  50. package/dist/data/arch/CFL.json.br +0 -0
  51. package/dist/data/arch/CLX.json.br +0 -0
  52. package/dist/data/arch/HSW.json.br +0 -0
  53. package/dist/data/arch/ICL.json.br +0 -0
  54. package/dist/data/arch/ICX.json.br +0 -0
  55. package/dist/data/arch/IVB.json.br +0 -0
  56. package/dist/data/arch/KBL.json.br +0 -0
  57. package/dist/data/arch/RKL.json.br +0 -0
  58. package/dist/data/arch/SKL.json.br +0 -0
  59. package/dist/data/arch/SKX.json.br +0 -0
  60. package/dist/data/arch/SNB.json.br +0 -0
  61. package/dist/data/arch/TGL.json.br +0 -0
  62. package/dist/data/arch/ZEN3.json.br +0 -0
  63. package/lib/insights.js +0 -100
  64. package/lib/whatif.js +0 -136
@@ -11,6 +11,8 @@
11
11
  // exit a loop with an exit before its branch (a search; a forward branch within the loop, an if, is not an
12
12
  // exit): the exit's mispredict, once per call, is not modelled;
13
13
  // with short trip counts it can cost more than the loop.
14
+ // call a call in the block (a loop that calls a function): the call instruction is simulated, the function it
15
+ // calls is not.
14
16
  // paths a conditional branch before the last instruction whose target is inside the block (an if/else, or a
15
17
  // second back edge: clang's remainder loop of sum_mod, `jne .LBB0_9` back to the top and the latch to the
16
18
  // middle): uiCA runs the block straight through, so both sides of the branch are simulated one after the
@@ -42,6 +44,7 @@ const COND_BRANCH = /^(?:j(?!mp)[a-z]+|loop\w*)$/;
42
44
  // Without a register destination (cmp, test, bt, push, branches) the first operand is only read.
43
45
  const READS_FIRST = /^(?:cmp|test|bt|push\w*|call\w*|jmp|j[a-z]+|loop\w*|out\w*|v?u?comis[sd]|v?ptest|kortest\w|ktest\w|nop\w*|prefetch\w*|clflush\w*|clwb|cldemote)$/;
44
46
  const BOTH_WRITTEN = /^(?:xchg|xadd|mulx)$/; // the second register operand is written too
47
+ /** @type {[RegExp, string[]][]} */
45
48
  const IMPLICIT_WRITES = [
46
49
  [/^(?:i?div|mul|cdq|cqo|cwd|rdtsc)$/, ['rax', 'rdx']],
47
50
  [/^(?:cbw|cwde|cdqe|lahf|cmpxchg|lods\w*)$/, ['rax']],
@@ -97,7 +100,11 @@ export function memOperand(label) {
97
100
  if (!m) return null;
98
101
  const inner = m[2].replace(/\s/g, '').toLowerCase();
99
102
  const parts = inner.split(/(?=[+-])/);
100
- let base = null, index = null, disp = 0;
103
+ /** @type {string | null} */
104
+ let base = null;
105
+ /** @type {string | null} */
106
+ let index = null;
107
+ let disp = 0;
101
108
  for (const p of parts) {
102
109
  const v = p.replace(/^\+/, '');
103
110
  if (/\*/.test(v)) index = v.split('*')[0];
@@ -114,6 +121,7 @@ export function memOperand(label) {
114
121
  // change of its own controls).
115
122
  const memo = new WeakMap();
116
123
  const some = (rows, ks) => `${ks.slice(0, 4).map((k) => rows[k].label).join('; ')}${ks.length > 4 ? '; …' : ''}`;
124
+ /** @param {{splitLoadCycles?: number} | null} [config] */
117
125
  export function caveats(response, config = null) {
118
126
  const splitCost = Number(config?.splitLoadCycles) > 0;
119
127
  const kept = response && typeof response === 'object' ? memo.get(response) : undefined;
@@ -160,12 +168,18 @@ export function caveats(response, config = null) {
160
168
  for (const [addr, st] of stores) if (loads.has(addr)) rmw.push(...loads.get(addr), ...st);
161
169
  // only where the address can repeat with the data: a register of it written other than by advancing it by a constant
162
170
  const dynamic = rmw.length ? untrackedWrites(rows, { lea: true }) : null;
163
- const hit = rmw.filter((row) => { const m = memOperand(rows[row].label); return [m.base, m.index].some((r) => dynamic.has(family(r)?.base)); });
171
+ const hit = rmw.filter((row) => { const m = memOperand(rows[row].label); return [m?.base, m?.index].some((r) => dynamic?.has(family(r)?.base)); });
164
172
  if (hit.length) {
165
173
  const r = [...new Set(hit)].sort((a, b) => a - b);
166
174
  out.push({ id: 'rmw-index', text: 'store forwarding', rows: r,
167
175
  title: `a read-modify-write to an indexed address (${some(rows, r)}): when iterations hit the same address (repeated data), each waits for the previous store; not modelled` });
168
176
  }
177
+ // a call: the function it calls is not part of the block
178
+ const calls = rows.flatMap((x, row) => (x?.kind === 'instr' && /^call/i.test(String(x.label).trim()) ? [row] : []));
179
+ if (calls.length) {
180
+ out.push({ id: 'call', text: 'calls', rows: calls,
181
+ title: `the block calls a function (${some(rows, calls)}): the call instruction is simulated, the function it calls is not; a real iteration also runs that function` });
182
+ }
169
183
  // a branch within the block: both sides are simulated
170
184
  if (inside.length) {
171
185
  const instrRows = rows.flatMap((x, row) => (x?.kind === 'instr' ? [row] : []));
@@ -192,7 +206,7 @@ export function caveats(response, config = null) {
192
206
  } else if (split.length) {
193
207
  // this CPU's model charges the crossings of loads whose address advances by a constant: only the others warn
194
208
  const writes = untrackedWrites(rows);
195
- const regsOf = (row) => { const m = memOperand(rows[row].label); return [m.base, m.index].map((r) => family(r)?.base).filter((r) => r && writes.has(r)); };
209
+ const regsOf = (row) => { const m = memOperand(rows[row].label); return [m?.base, m?.index].map((r) => family(r)?.base).filter((r) => r && writes.has(r)); };
196
210
  const untracked = split.filter((row) => regsOf(row).length);
197
211
  if (untracked.length) {
198
212
  const regs = [...new Set(untracked.flatMap(regsOf))];
@@ -204,3 +218,101 @@ export function caveats(response, config = null) {
204
218
  if (response && typeof response === 'object') (kept ?? memo.set(response, new Map()).get(response)).set(splitCost, out);
205
219
  return out;
206
220
  }
221
+
222
+ // ---- code a CPU cannot run ------------------------------------------------------------------------------------------
223
+ // A block with positions the CPU's decoder skipped (decodeErrors: instructions its XED chip does not have, e.g. AVX-512
224
+ // on SKL, whose bytes it then decodes as other instructions) still gets the reference's throughput, which leaves them out:
225
+ // a number, but not a result for that CPU. So does a block with an instruction the decoder accepts but that is of an
226
+ // extension the CPU lacks (ZEN3's model decodes with XED's chip ALL, which accepts everything: its AVX-512 instructions
227
+ // get the note X and no data). The page (headline, the table of CPUs, A–B), the Markdown and the CLI's text say so with
228
+ // cannotRun's reason, keep the number reachable, and leave such a CPU out of any "fastest". The engine's numbers do not
229
+ // change.
230
+ // Any other instruction with the note X (accepted by the CPU's decoder, of no extension it lacks: x87 tbyte loads,
231
+ // syscall, endbr64, CLWB on ICX, TSX on HSW, ...) is a gap in the instruction tables: the result stands, with a note
232
+ // (noData: "N instructions without data", whose cost the throughput leaves out).
233
+ //
234
+ // Which extension an instruction with the note X is of comes from its encoding (the row's opcode bytes), as XED's ISA
235
+ // set says (tests-js/web_cannot_run.mjs checks the two agree on the fixtures): extensionOf. The reason names the
236
+ // extension: of such an instruction, or, for the positions the decoder skipped (whose rows are other instructions), from
237
+ // the analyzed assembly text (`code`, comments and strings left out), only an extension the CPU lacks; machine code
238
+ // without text: the count ("12 undecodable").
239
+ const EXTENSIONS = /** @type {[string, RegExp][]} */ ([
240
+ ['AMX', /\btmm\d|\b(?:tdp\w+|tileloadd\w*|tilestored|ldtilecfg|sttilecfg|tilezero|tilerelease)\b/i],
241
+ ['AVX-512', /\bzmm\d|%?\bk[1-7]\b|\{1to\d+\}|\b(?:kmov|kand|kor|kxor|knot|kshift|kunpck|kortest|ktest|kadd)\w*|\bv(?:pternlog|pcompress|pexpand|compress|expand|permt2|permi2|pmov[a-z]*2m|pmovm2|range|reduce|getexp|getmant|scalef|fixupimm|rcp14|rsqrt14|pconflict|plzcnt|dbpsadbw|align[dq]|pror|prol|psrav[wq]|psllv[w]|psrlvw|cvtt?p[sd]2u|cvtu)\w*/i],
242
+ ['AVX-VNNI', /\bvpdp(?:bus|ws)ds?\b/i],
243
+ ['SHA', /\bsha(?:1|256)\w+/i],
244
+ ['FMA', /\bvf(?:n?m(?:add|sub)|maddsub|msubadd)\w*/i],
245
+ ['AVX2', /\bv(?:pbroadcast[bwdq]|broadcasti128|perm[dq]\b|permp[sd]|perm2i128|inserti128|extracti128|p?gather\w+|ps[lr]lv[dq]|psravd|pmaskmov[dq]|pblendd)\b|\bvp(?!(?:extr|insr|cmp[ei]str|clmul|ermilp|erm2f128|test|dp(?:bus|ws)))\w+\s[^\n]*\bymm/i],
246
+ ['BMI2', /\b(?:bzhi|mulx|pdep|pext|rorx|sarx|shlx|shrx)[lq]?\b/i],
247
+ ['BMI1', /\b(?:andn|bextr|blsi|blsmsk|blsr)[lq]?\b/i],
248
+ ['ADX', /\b(?:adcx|adox)[lq]?\b/i],
249
+ ['TSX', /\b(?:xbegin|xend|xabort|xtest)\b/i],
250
+ ]);
251
+ const SNB_LACKS = ['AMX', 'AVX-512', 'AVX-VNNI', 'SHA', 'FMA', 'AVX2', 'BMI2', 'BMI1', 'ADX'];
252
+ const SKL_LACKS = ['AMX', 'AVX-512', 'AVX-VNNI', 'SHA'];
253
+ const AVX512_LACKS = ['AMX', 'AVX-VNNI', 'SHA'];
254
+ const ICL_LACKS = ['AMX', 'AVX-VNNI'];
255
+ // the extensions of the list each CPU (the keys of archs.js) does not have (TSX: only where no model of it had it)
256
+ export const LACKS = Object.freeze({
257
+ SNB: SNB_LACKS, IVB: SNB_LACKS, HSW: ['AMX', 'AVX-512', 'AVX-VNNI', 'SHA', 'ADX'], BDW: SKL_LACKS, SKL: SKL_LACKS, KBL: SKL_LACKS, CFL: SKL_LACKS,
258
+ SKX: AVX512_LACKS, CLX: AVX512_LACKS, ICL: ICL_LACKS, TGL: ICL_LACKS, RKL: ICL_LACKS, ICX: ICL_LACKS, ZEN3: ['AMX', 'AVX-512', 'AVX-VNNI', 'TSX'], 'ADL-P': ['AMX', 'AVX-512', 'TSX'],
259
+ });
260
+
261
+ const LEGACY = new Set([0x66, 0x67, 0xf0, 0xf2, 0xf3, 0x2e, 0x36, 0x3e, 0x26, 0x64, 0x65]);
262
+ // The extension of an instruction of the list above that an encoding is of (hex of its bytes), or null: EVEX (AVX-512),
263
+ // VEX map 0F38 0x49/0x4B/0x5C/0x5E (AMX), VEX.66 map 0F38 0x50-0x53 (AVX-VNNI), XBEGIN/XABORT/XEND/XTEST (TSX). The
264
+ // extensions whose instructions any CPU's decoder rejects that lacks them (FMA, AVX2, BMI, SHA, ADX) never get here.
265
+ export function extensionOf(opcode) {
266
+ const b = String(opcode ?? '').match(/[0-9a-f]{2}/gi)?.map((x) => parseInt(x, 16)) ?? [];
267
+ let i = 0;
268
+ while (i < b.length && (LEGACY.has(b[i]) || (b[i] & 0xf0) === 0x40)) i++;
269
+ const [o, p0, p1, p2] = b.slice(i);
270
+ if (o === 0x62) return 'AVX-512';
271
+ if (o === 0xc4 && p0 !== undefined && p1 !== undefined && (p0 & 0x1f) === 2) {
272
+ if ([0x49, 0x4b, 0x5c, 0x5e].includes(p2)) return 'AMX';
273
+ if ((p1 & 3) === 1 && p2 >= 0x50 && p2 <= 0x53) return 'AVX-VNNI';
274
+ }
275
+ if (o === 0x0f && p0 === 0x01 && (p1 === 0xd5 || p1 === 0xd6)) return 'TSX';
276
+ if ((o === 0xc7 || o === 0xc6) && p0 === 0xf8) return 'TSX';
277
+ return null;
278
+ }
279
+
280
+ // the analyzed text without comments and strings (a "zmm" in a comment names nothing)
281
+ // (a ';' only at the start of a line: elsewhere it can separate statements)
282
+ const codeOnly = (code) => String(code).replace(/\/\*[\s\S]*?\*\//g, ' ').replace(/"(?:[^"\\\n]|\\.)*"/g, '""').replace(/(?:#|\/\/)[^\n]*|^\s*;[^\n]*/gm, ' ');
283
+ // the opcodes of a response's rows with the note X, or of a summary's (xOps)
284
+ const xOpsOf = (r) => (Array.isArray(r.rows) ? r.rows.filter((x) => x?.kind === 'instr' && x.notes === 'X').map((x) => String(x.opcode ?? ''))
285
+ : Array.isArray(r.xOps) ? r.xOps.map(String) : []);
286
+ const plural = (n, one, many) => `${n} ${n === 1 ? one : many}`;
287
+
288
+ /**
289
+ * @param {any} r a response (rows: notes X, opcode), or a summary with decodeErrors and the X rows' opcodes (xOps)
290
+ * @param {{arch?: string | null, code?: string | null}} [opts] the CPU's key; the analyzed assembly text, if any
291
+ * @returns {{reason: string, title: string, decodeErrors: number, unsupported: number, needs: string[]} | null}
292
+ */
293
+ export function cannotRun(r, { arch = null, code = null } = {}) {
294
+ if (!r || typeof r !== 'object' || 'error' in r) return null;
295
+ const decodeErrors = Number(r.decodeErrors) || 0;
296
+ const lacks = arch ? LACKS[arch] ?? [] : [];
297
+ // (with skipped positions, the X rows are other instructions than the code's: not counted)
298
+ const lacked = decodeErrors ? [] : xOpsOf(r).map(extensionOf).filter((x) => x && lacks.includes(x));
299
+ if (!decodeErrors && !lacked.length) return null;
300
+ const text = decodeErrors && code ? codeOnly(code) : '';
301
+ const needs = decodeErrors ? EXTENSIONS.filter(([name, re]) => lacks.includes(name) && re.test(text)).map(([name]) => name)
302
+ : EXTENSIONS.map(([name]) => name).filter((name) => lacked.includes(name));
303
+ const unsupported = lacked.length;
304
+ const reason = needs.length ? `needs ${needs.join(', ')}` : `${decodeErrors} undecodable`;
305
+ const what = decodeErrors ? plural(decodeErrors, 'position its decoder skipped', 'positions its decoder skipped') : plural(unsupported, 'instruction of an extension it lacks', 'instructions of an extension it lacks');
306
+ const title = `${arch ? `${arch} cannot run this code` : 'code this CPU cannot run'}${needs.length ? ` (it needs ${needs.join(', ')})` : ''}: ${what}. The throughput leaves them out, so it is not a result for this CPU`;
307
+ return { reason, title, decodeErrors, unsupported, needs };
308
+ }
309
+
310
+ // The instructions with the note X of a block the CPU can run (cannotRun null): gaps in its instruction tables, whose
311
+ // cost the throughput leaves out; 0 if none (or if the CPU cannot run the block: its reason says it).
312
+ /** @param {any} r @param {{arch?: string | null}} [opts] @returns {number} */
313
+ export function noData(r, { arch = null } = {}) {
314
+ if (!r || typeof r !== 'object' || 'error' in r || cannotRun(r, { arch })) return 0;
315
+ return xOpsOf(r).length;
316
+ }
317
+ export const noDataText = (n) => plural(n, 'instruction without data', 'instructions without data');
318
+ export const NO_DATA_TITLE = 'not in the instruction tables of this CPU (uops.info has no data for them): the throughput leaves their cost out';
@@ -0,0 +1,91 @@
1
+ // The details of a result (docs/WASM-API.md, "details": per instruction its allowed ports, latencies and front-end facts;
2
+ // the port set behind the Ports limit; the issue slots per cause), fetched only when a view asks for them: one more
3
+ // analysis of the same request with `details: true`, once per result. Shared by the instruction card (icard.js) and the
4
+ // port-set drill-down of the bound view (portset.js). A response without details (the mock engine, an old module) gives
5
+ // null: the views then show what the response has.
6
+ //
7
+ // The first half has no DOM (tests-js/web_details.mjs runs it under Node).
8
+
9
+ // The details belong to the response if they have one entry per row and the same throughput was predicted.
10
+ export function detailsFit(response, res) {
11
+ const d = res?.details;
12
+ return !!d && Array.isArray(d.rows) && Array.isArray(response?.rows) && d.rows.length === response.rows.length && res.tp === response.tp;
13
+ }
14
+
15
+ // "1*p0156+1*p23" (uops.info's notation) from portData; non-Intel names (ALU0, FP2, ...) are listed: 1*[ALU0 ALU1]
16
+ export function portString(portData) {
17
+ if (!Array.isArray(portData) || !portData.length) return '';
18
+ return portData.map((p) => {
19
+ const names = p.ports ?? [];
20
+ const intel = names.every((n) => /^Port \w$/.test(n));
21
+ return `${p.uops}*${intel ? 'p' + names.map((n) => n.slice(5)).join('') : `[${names.join(' ')}]`}`;
22
+ }).join('+');
23
+ }
24
+
25
+ // The instruction's reciprocal throughput from its ports alone (the most uops per port over the unions of two of its port
26
+ // sets, as Facile's port model computes it for the block), and from the table's limit; cycles per instruction.
27
+ export function portThroughput(det) {
28
+ const sets = (det?.portData ?? []).map((p) => ({ set: new Set(p.ports), uops: Number(p.uops) || 0 })).filter((p) => p.set.size);
29
+ let best = 0;
30
+ for (const a of sets) {
31
+ for (const b of sets) {
32
+ const u = new Set([...a.set, ...b.set]);
33
+ const uops = sets.filter((s) => [...s.set].every((x) => u.has(x))).reduce((n, s) => n + s.uops, 0);
34
+ best = Math.max(best, uops / u.size);
35
+ }
36
+ }
37
+ return Math.max(best, Number(det?.tpTable) || 0);
38
+ }
39
+
40
+ // Latencies to show: per (input, output) pair, without the flag outputs of an instruction that writes a register too.
41
+ export function latencyPairs(det) {
42
+ const all = Array.isArray(det?.latencies) ? det.latencies : [];
43
+ const isFlags = (x) => String(x).startsWith('flags');
44
+ const regOut = all.some((l) => !isFlags(l.to));
45
+ const out = [];
46
+ for (const l of all) {
47
+ if (regOut && isFlags(l.to)) continue;
48
+ if (!out.some((x) => x.from === l.from && x.to === l.to)) out.push(l);
49
+ }
50
+ return out;
51
+ }
52
+
53
+ // ---- the fetcher -----------------------------------------------------------------------------------------------------
54
+
55
+ const isCancel = (e) => e?.name === 'CancelledError';
56
+
57
+ // env: { engine() }. get(view) resolves to the details of view.response, or null; one analysis per view (a cancelled one
58
+ // is tried again on the next get).
59
+ export function createDetails(env) {
60
+ const done = new WeakMap(); // view -> details | null
61
+ const pending = new WeakMap(); // view -> promise
62
+ const listeners = []; // (view, details): details arrived (for any of the views that asked)
63
+ return {
64
+ onLoad(f) { listeners.push(f); },
65
+ peek(view) { return done.get(view); }, // undefined: not fetched
66
+ get(view) {
67
+ if (!view) return Promise.resolve(null);
68
+ if (done.has(view)) return Promise.resolve(done.get(view));
69
+ let p = pending.get(view);
70
+ if (p) return p;
71
+ const r = view.response;
72
+ if (!r || r.error || r.mock || !view.request || !env.engine?.()) { done.set(view, null); return Promise.resolve(null); }
73
+ p = (async () => {
74
+ try {
75
+ const res = await env.engine().analyze({ ...view.request, details: true });
76
+ const d = detailsFit(r, res) ? res.details : null;
77
+ done.set(view, d);
78
+ if (d) for (const f of listeners) { try { f(view, d); } catch { /* a view's problem */ } }
79
+ return d;
80
+ } catch (e) {
81
+ if (!isCancel(e)) done.set(view, null);
82
+ return null;
83
+ } finally {
84
+ pending.delete(view);
85
+ }
86
+ })();
87
+ pending.set(view, p);
88
+ return p;
89
+ },
90
+ };
91
+ }
@@ -0,0 +1,103 @@
1
+ // Built-in examples. Each has the code in both assembly syntaxes and as machine code (a listing that the hex
2
+ // parser reads, so the examples also show that disassembly output can be pasted). The four blocks are in the
3
+ // corpus of the Python reference, so the mock engine has the reference's result for them
4
+ // (see mock-data.js; `arch` is the microarchitecture that result was recorded for). Menu group "basics".
5
+ // An example's machine code is what the assembler makes of its text (tests-js/web_qa.mjs, web_asm_shipped.mjs), so that
6
+ // its result can be shown before the assembler has loaded (asm-shipped.js): 'avx' has `49 01 ca` for `add r10, rcx`,
7
+ // where the corpus has `4c 03 d1` (the mock engine takes it for the recorded block: engine-mock.js SAME_AS).
8
+
9
+ import { SHA256_BLOCK_HEX } from './showcase.js';
10
+
11
+ function ex(id, title, arch, intel, att, listing) {
12
+ const hex = listing.map((l) => l[0].replace(/ /g, '')).join('');
13
+ let addr = 0;
14
+ const width = Math.max(...listing.map((l) => l[0].length));
15
+ const lines = listing.map(([bytes, text]) => {
16
+ const line = `${addr.toString(16).padStart(4)}: ${bytes.padEnd(width)} ${text}`;
17
+ addr += (bytes.length + 1) / 3;
18
+ return line;
19
+ });
20
+ return { id, title, arch, hex, text: { intel: intel.join('\n') + '\n', att: att.join('\n') + '\n', hex: lines.join('\n') + '\n' } };
21
+ }
22
+
23
+ export const EXAMPLES = [
24
+ ex('chain', 'Dependency chain: two dependent adds', 'SKL',
25
+ ['loop:', ' add rax, 1', ' add rax, 1', ' dec r15', ' jnz loop'],
26
+ ['loop:', ' addq $1, %rax', ' addq $1, %rax', ' decq %r15', ' jnz loop'],
27
+ [['48 83 c0 01', 'add rax, 0x1'], ['48 83 c0 01', 'add rax, 0x1'], ['49 ff cf', 'dec r15'], ['75 f3', 'jnz 0x0']]),
28
+ ex('loadstore', 'Load/store: store, then reload', 'ZEN3',
29
+ ['loop:', ' mov qword ptr [rsi], rax', ' mov eax, dword ptr [rsi]', ' add rax, 1', ' dec r15', ' jnz loop'],
30
+ ['loop:', ' movq %rax, (%rsi)', ' movl (%rsi), %eax', ' addq $1, %rax', ' decq %r15', ' jnz loop'],
31
+ [['48 89 06', 'mov qword ptr [rsi], rax'], ['8b 06', 'mov eax, dword ptr [rsi]'], ['48 83 c0 01', 'add rax, 0x1'],
32
+ ['49 ff cf', 'dec r15'], ['75 f2', 'jnz 0x0']]),
33
+ ex('avx', 'AVX: FMA, add and multiply', 'ADL-P',
34
+ ['loop:', ' vfmsub213pd ymm6, ymm5, ymm4', ' add r10, rcx', ' imul r8, r8', ' dec r15', ' jnz loop'],
35
+ ['loop:', ' vfmsub213pd %ymm4, %ymm5, %ymm6', ' addq %rcx, %r10', ' imulq %r8, %r8', ' decq %r15', ' jnz loop'],
36
+ [['c4 e2 d5 aa f4', 'vfmsub213pd ymm6, ymm5, ymm4'], ['49 01 ca', 'add r10, rcx'], ['4d 0f af c0', 'imul r8, r8'],
37
+ ['49 ff cf', 'dec r15'], ['75 ef', 'jnz 0x0']]),
38
+ ex('stack', 'Stack: pops after an add to rsp', 'SKL',
39
+ ['loop:', ' add rsp, 8', ' mov rsi, rbx', ' mov rdx, rbp', ' pop rbx', ' pop rbp', ' mov edi, eax', ' dec r15', ' jnz loop'],
40
+ ['loop:', ' addq $8, %rsp', ' movq %rbx, %rsi', ' movq %rbp, %rdx', ' popq %rbx', ' popq %rbp', ' movl %eax, %edi', ' decq %r15', ' jnz loop'],
41
+ [['48 83 c4 08', 'add rsp, 0x8'], ['48 89 de', 'mov rsi, rbx'], ['48 89 ea', 'mov rdx, rbp'], ['5b', 'pop rbx'], ['5d', 'pop rbp'],
42
+ ['89 c7', 'mov edi, eax'], ['49 ff cf', 'dec r15'], ['75 ed', 'jnz 0x0']]),
43
+ ];
44
+
45
+ // Well-known loops (menu group "classic loops"). Not in the reference's corpus: the mock has no result for them.
46
+ // dot1/dot4 are the default A–B demo (the same work, one accumulator vs four).
47
+ export const CLASSIC_EXAMPLES = [
48
+ ex('saxpy', 'saxpy (AVX2 FMA)', 'SKL',
49
+ ['loop:', ' vmovups ymm1, ymmword ptr [rdi + rax*4]', ' vfmadd213ps ymm1, ymm0, ymmword ptr [rsi + rax*4]', ' vmovups ymmword ptr [rsi + rax*4], ymm1', ' add rax, 8', ' cmp rax, rdx', ' jb loop'],
50
+ ['loop:', ' vmovups (%rdi,%rax,4), %ymm1', ' vfmadd213ps (%rsi,%rax,4), %ymm0, %ymm1', ' vmovups %ymm1, (%rsi,%rax,4)', ' addq $8, %rax', ' cmpq %rdx, %rax', ' jb loop'],
51
+ [['c5 fc 10 0c 87', 'vmovups ymm1, ymmword ptr [rdi+rax*4]'], ['c4 e2 7d a8 0c 86', 'vfmadd213ps ymm1, ymm0, ymmword ptr [rsi+rax*4]'], ['c5 fc 11 0c 86', 'vmovups ymmword ptr [rsi+rax*4], ymm1'], ['48 83 c0 08', 'add rax, 0x8'], ['48 39 d0', 'cmp rax, rdx'], ['72 e7', 'jb 0x0']]),
52
+ ex('dot1', 'dot product, 1 accumulator', 'SKL',
53
+ ['loop:', ' vmovups ymm4, ymmword ptr [rdi + rax*4]', ' vmovups ymm5, ymmword ptr [rdi + rax*4 + 32]', ' vmovups ymm6, ymmword ptr [rdi + rax*4 + 64]', ' vmovups ymm7, ymmword ptr [rdi + rax*4 + 96]', ' vfmadd231ps ymm0, ymm4, ymmword ptr [rsi + rax*4]', ' vfmadd231ps ymm0, ymm5, ymmword ptr [rsi + rax*4 + 32]', ' vfmadd231ps ymm0, ymm6, ymmword ptr [rsi + rax*4 + 64]', ' vfmadd231ps ymm0, ymm7, ymmword ptr [rsi + rax*4 + 96]', ' add rax, 32', ' cmp rax, rdx', ' jb loop'],
54
+ ['loop:', ' vmovups (%rdi,%rax,4), %ymm4', ' vmovups 32(%rdi,%rax,4), %ymm5', ' vmovups 64(%rdi,%rax,4), %ymm6', ' vmovups 96(%rdi,%rax,4), %ymm7', ' vfmadd231ps (%rsi,%rax,4), %ymm4, %ymm0', ' vfmadd231ps 32(%rsi,%rax,4), %ymm5, %ymm0', ' vfmadd231ps 64(%rsi,%rax,4), %ymm6, %ymm0', ' vfmadd231ps 96(%rsi,%rax,4), %ymm7, %ymm0', ' addq $32, %rax', ' cmpq %rdx, %rax', ' jb loop'],
55
+ [['c5 fc 10 24 87', 'vmovups ymm4, ymmword ptr [rdi+rax*4]'], ['c5 fc 10 6c 87 20', 'vmovups ymm5, ymmword ptr [rdi+rax*4+0x20]'], ['c5 fc 10 74 87 40', 'vmovups ymm6, ymmword ptr [rdi+rax*4+0x40]'], ['c5 fc 10 7c 87 60', 'vmovups ymm7, ymmword ptr [rdi+rax*4+0x60]'], ['c4 e2 5d b8 04 86', 'vfmadd231ps ymm0, ymm4, ymmword ptr [rsi+rax*4]'], ['c4 e2 55 b8 44 86 20', 'vfmadd231ps ymm0, ymm5, ymmword ptr [rsi+rax*4+0x20]'], ['c4 e2 4d b8 44 86 40', 'vfmadd231ps ymm0, ymm6, ymmword ptr [rsi+rax*4+0x40]'], ['c4 e2 45 b8 44 86 60', 'vfmadd231ps ymm0, ymm7, ymmword ptr [rsi+rax*4+0x60]'], ['48 83 c0 20', 'add rax, 0x20'], ['48 39 d0', 'cmp rax, rdx'], ['72 c5', 'jb 0x0']]),
56
+ ex('dot4', 'dot product, 4 accumulators', 'SKL',
57
+ ['loop:', ' vmovups ymm4, ymmword ptr [rdi + rax*4]', ' vmovups ymm5, ymmword ptr [rdi + rax*4 + 32]', ' vmovups ymm6, ymmword ptr [rdi + rax*4 + 64]', ' vmovups ymm7, ymmword ptr [rdi + rax*4 + 96]', ' vfmadd231ps ymm0, ymm4, ymmword ptr [rsi + rax*4]', ' vfmadd231ps ymm1, ymm5, ymmword ptr [rsi + rax*4 + 32]', ' vfmadd231ps ymm2, ymm6, ymmword ptr [rsi + rax*4 + 64]', ' vfmadd231ps ymm3, ymm7, ymmword ptr [rsi + rax*4 + 96]', ' add rax, 32', ' cmp rax, rdx', ' jb loop'],
58
+ ['loop:', ' vmovups (%rdi,%rax,4), %ymm4', ' vmovups 32(%rdi,%rax,4), %ymm5', ' vmovups 64(%rdi,%rax,4), %ymm6', ' vmovups 96(%rdi,%rax,4), %ymm7', ' vfmadd231ps (%rsi,%rax,4), %ymm4, %ymm0', ' vfmadd231ps 32(%rsi,%rax,4), %ymm5, %ymm1', ' vfmadd231ps 64(%rsi,%rax,4), %ymm6, %ymm2', ' vfmadd231ps 96(%rsi,%rax,4), %ymm7, %ymm3', ' addq $32, %rax', ' cmpq %rdx, %rax', ' jb loop'],
59
+ [['c5 fc 10 24 87', 'vmovups ymm4, ymmword ptr [rdi+rax*4]'], ['c5 fc 10 6c 87 20', 'vmovups ymm5, ymmword ptr [rdi+rax*4+0x20]'], ['c5 fc 10 74 87 40', 'vmovups ymm6, ymmword ptr [rdi+rax*4+0x40]'], ['c5 fc 10 7c 87 60', 'vmovups ymm7, ymmword ptr [rdi+rax*4+0x60]'], ['c4 e2 5d b8 04 86', 'vfmadd231ps ymm0, ymm4, ymmword ptr [rsi+rax*4]'], ['c4 e2 55 b8 4c 86 20', 'vfmadd231ps ymm1, ymm5, ymmword ptr [rsi+rax*4+0x20]'], ['c4 e2 4d b8 54 86 40', 'vfmadd231ps ymm2, ymm6, ymmword ptr [rsi+rax*4+0x40]'], ['c4 e2 45 b8 5c 86 60', 'vfmadd231ps ymm3, ymm7, ymmword ptr [rsi+rax*4+0x60]'], ['48 83 c0 20', 'add rax, 0x20'], ['48 39 d0', 'cmp rax, rdx'], ['72 c5', 'jb 0x0']]),
60
+ ex('popcnt', 'popcount (false dependency)', 'SKL',
61
+ ['loop:', ' popcnt rcx, qword ptr [rdi + rax*8]', ' add rdx, rcx', ' inc rax', ' cmp rax, rsi', ' jb loop'],
62
+ ['loop:', ' popcntq (%rdi,%rax,8), %rcx', ' addq %rcx, %rdx', ' incq %rax', ' cmpq %rsi, %rax', ' jb loop'],
63
+ [['f3 48 0f b8 0c c7', 'popcnt rcx, qword ptr [rdi+rax*8]'], ['48 01 ca', 'add rdx, rcx'], ['48 ff c0', 'inc rax'], ['48 39 f0', 'cmp rax, rsi'], ['72 ef', 'jb 0x0']]),
64
+ ex('ptrchase', 'pointer chasing', 'SKL',
65
+ ['loop:', ' mov rax, qword ptr [rax]', ' dec rcx', ' jnz loop'],
66
+ ['loop:', ' movq (%rax), %rax', ' decq %rcx', ' jne loop'],
67
+ [['48 8b 00', 'mov rax, qword ptr [rax]'], ['48 ff c9', 'dec rcx'], ['75 f8', 'jnz 0x0']]),
68
+ ex('strlen', 'strlen (SSE2)', 'SKL',
69
+ ['loop:', ' movdqa xmm1, xmmword ptr [rdi]', ' pcmpeqb xmm1, xmm0', ' pmovmskb eax, xmm1', ' add rdi, 16', ' test eax, eax', ' jz loop'],
70
+ ['loop:', ' movdqa (%rdi), %xmm1', ' pcmpeqb %xmm0, %xmm1', ' pmovmskb %xmm1, %eax', ' addq $16, %rdi', ' testl %eax, %eax', ' je loop'],
71
+ [['66 0f 6f 0f', 'movdqa xmm1, xmmword ptr [rdi]'], ['66 0f 74 c8', 'pcmpeqb xmm1, xmm0'], ['66 0f d7 c1', 'pmovmskb eax, xmm1'], ['48 83 c7 10', 'add rdi, 0x10'], ['85 c0', 'test eax, eax'], ['74 ec', 'jz 0x0']]),
72
+ ex('crc32', 'CRC32', 'SKL',
73
+ ['loop:', ' crc32 rax, qword ptr [rdi + rcx*8]', ' inc rcx', ' cmp rcx, rsi', ' jb loop'],
74
+ ['loop:', ' crc32q (%rdi,%rcx,8), %rax', ' incq %rcx', ' cmpq %rsi, %rcx', ' jb loop'],
75
+ [['f2 48 0f 38 f1 04 cf', 'crc32 rax, qword ptr [rdi+rcx*8]'], ['48 ff c1', 'inc rcx'], ['48 39 f1', 'cmp rcx, rsi'], ['72 f1', 'jb 0x0']]),
76
+ ex('memcpy', 'memcpy (AVX)', 'SKL',
77
+ ['loop:', ' vmovdqu ymm0, ymmword ptr [rsi + rax]', ' vmovdqu ymm1, ymmword ptr [rsi + rax + 32]', ' vmovdqu ymmword ptr [rdi + rax], ymm0', ' vmovdqu ymmword ptr [rdi + rax + 32], ymm1', ' add rax, 64', ' cmp rax, rdx', ' jb loop'],
78
+ ['loop:', ' vmovdqu (%rsi,%rax), %ymm0', ' vmovdqu 32(%rsi,%rax), %ymm1', ' vmovdqu %ymm0, (%rdi,%rax)', ' vmovdqu %ymm1, 32(%rdi,%rax)', ' addq $64, %rax', ' cmpq %rdx, %rax', ' jb loop'],
79
+ [['c5 fe 6f 04 06', 'vmovdqu ymm0, ymmword ptr [rsi+rax*1]'], ['c5 fe 6f 4c 06 20', 'vmovdqu ymm1, ymmword ptr [rsi+rax*1+0x20]'], ['c5 fe 7f 04 07', 'vmovdqu ymmword ptr [rdi+rax*1], ymm0'], ['c5 fe 7f 4c 07 20', 'vmovdqu ymmword ptr [rdi+rax*1+0x20], ymm1'], ['48 83 c0 40', 'add rax, 0x40'], ['48 39 d0', 'cmp rax, rdx'], ['72 e1', 'jb 0x0']]),
80
+ ex('gemm', 'GEMM microkernel (4×8 FMA)', 'SKL',
81
+ ['loop:', ' vmovaps ymm0, ymmword ptr [rsi]', ' vmovaps ymm1, ymmword ptr [rsi + 32]', ' vbroadcastss ymm2, dword ptr [rdi]', ' vfmadd231ps ymm8, ymm2, ymm0', ' vfmadd231ps ymm9, ymm2, ymm1', ' vbroadcastss ymm3, dword ptr [rdi + 4]', ' vfmadd231ps ymm10, ymm3, ymm0', ' vfmadd231ps ymm11, ymm3, ymm1', ' vbroadcastss ymm2, dword ptr [rdi + 8]', ' vfmadd231ps ymm12, ymm2, ymm0', ' vfmadd231ps ymm13, ymm2, ymm1', ' vbroadcastss ymm3, dword ptr [rdi + 12]', ' vfmadd231ps ymm14, ymm3, ymm0', ' vfmadd231ps ymm15, ymm3, ymm1', ' add rdi, 16', ' add rsi, 64', ' dec rcx', ' jnz loop'],
82
+ ['loop:', ' vmovaps (%rsi), %ymm0', ' vmovaps 32(%rsi), %ymm1', ' vbroadcastss (%rdi), %ymm2', ' vfmadd231ps %ymm0, %ymm2, %ymm8', ' vfmadd231ps %ymm1, %ymm2, %ymm9', ' vbroadcastss 4(%rdi), %ymm3', ' vfmadd231ps %ymm0, %ymm3, %ymm10', ' vfmadd231ps %ymm1, %ymm3, %ymm11', ' vbroadcastss 8(%rdi), %ymm2', ' vfmadd231ps %ymm0, %ymm2, %ymm12', ' vfmadd231ps %ymm1, %ymm2, %ymm13', ' vbroadcastss 12(%rdi), %ymm3', ' vfmadd231ps %ymm0, %ymm3, %ymm14', ' vfmadd231ps %ymm1, %ymm3, %ymm15', ' addq $16, %rdi', ' addq $64, %rsi', ' decq %rcx', ' jne loop'],
83
+ [['c5 fc 28 06', 'vmovaps ymm0, ymmword ptr [rsi]'], ['c5 fc 28 4e 20', 'vmovaps ymm1, ymmword ptr [rsi+0x20]'], ['c4 e2 7d 18 17', 'vbroadcastss ymm2, dword ptr [rdi]'], ['c4 62 6d b8 c0', 'vfmadd231ps ymm8, ymm2, ymm0'], ['c4 62 6d b8 c9', 'vfmadd231ps ymm9, ymm2, ymm1'], ['c4 e2 7d 18 5f 04', 'vbroadcastss ymm3, dword ptr [rdi+0x4]'], ['c4 62 65 b8 d0', 'vfmadd231ps ymm10, ymm3, ymm0'], ['c4 62 65 b8 d9', 'vfmadd231ps ymm11, ymm3, ymm1'], ['c4 e2 7d 18 57 08', 'vbroadcastss ymm2, dword ptr [rdi+0x8]'], ['c4 62 6d b8 e0', 'vfmadd231ps ymm12, ymm2, ymm0'], ['c4 62 6d b8 e9', 'vfmadd231ps ymm13, ymm2, ymm1'], ['c4 e2 7d 18 5f 0c', 'vbroadcastss ymm3, dword ptr [rdi+0xc]'], ['c4 62 65 b8 f0', 'vfmadd231ps ymm14, ymm3, ymm0'], ['c4 62 65 b8 f9', 'vfmadd231ps ymm15, ymm3, ymm1'], ['48 83 c7 10', 'add rdi, 0x10'], ['48 83 c6 40', 'add rsi, 0x40'], ['48 ff c9', 'dec rcx'], ['75 ab', 'jnz 0x0']]),
84
+ ];
85
+
86
+ // A large block that shows the analyzer's speed (menu group "showcase"). Machine code only: `hexOnly` examples switch the
87
+ // input to hex.
88
+ export const SHOWCASE_EXAMPLES = [
89
+ { id: 'sha256', title: 'SHA-256 block (2,949 instr)', arch: 'SKL', hex: SHA256_BLOCK_HEX.replace(/\s+/g, ''), text: { hex: SHA256_BLOCK_HEX },
90
+ // Intel assembly, loaded when picked (showcase-asm.js, about 60 KB); the example opens in asm mode, hex if it cannot load
91
+ // @ts-ignore: the page's module (the npm package's copy of this file has no showcase-asm.js and never calls asm())
92
+ asm: () => import('./showcase-asm.js').then((m) => m.SHA256_BLOCK_ASM) },
93
+ ];
94
+
95
+ // Only offered while the mock engine is active: inputs that make it return what the recorded results lack.
96
+ export const MOCK_EXAMPLES = [
97
+ ex('synthetic', 'Mock only: synthetic result (all row kinds and notes)', 'SKL',
98
+ ['div rcx', 'mov ah, bl', 'add rax, rcx', 'push rax', 'ud2', 'cmp rax, rbx', 'jz next', 'next:', '.byte 0x06', 'nop'],
99
+ ['divq %rcx', 'movb %bl, %ah', 'addq %rcx, %rax', 'pushq %rax', 'ud2', 'cmpq %rbx, %rax', 'jz next', 'next:', '.byte 0x06', 'nop'],
100
+ [['48 f7 f1', 'div rcx'], ['88 dc', 'mov ah, bl'], ['48 01 c8', 'add rax, rcx'], ['50', 'push rax'], ['0f 0b', 'ud2'],
101
+ ['48 39 d8', 'cmp rax, rbx'], ['74 00', 'jz 0x2'], ['06', '(bad)'], ['90', 'nop']]),
102
+ ex('error', 'Mock only: analysis error', 'SKL', ['ud2'], ['ud2'], [['0f 0b', 'ud2']]),
103
+ ];