wuica-cli 0.0.0-stage → 0.1.9
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/LICENSE +661 -0
- package/README.md +546 -2
- package/bin/wuica-mcp.js +16 -0
- package/bin/wuica.js +25 -0
- package/dist/data/arch/ADL-P.bin.br +0 -0
- package/dist/data/arch/BDW.bin.br +0 -0
- package/dist/data/arch/CFL.bin.br +0 -0
- package/dist/data/arch/CLX.bin.br +0 -0
- package/dist/data/arch/HSW.bin.br +0 -0
- package/dist/data/arch/ICL.bin.br +0 -0
- package/dist/data/arch/ICX.bin.br +0 -0
- package/dist/data/arch/IVB.bin.br +0 -0
- package/dist/data/arch/KBL.bin.br +0 -0
- package/dist/data/arch/RKL.bin.br +0 -0
- package/dist/data/arch/SKL.bin.br +0 -0
- package/dist/data/arch/SKX.bin.br +0 -0
- package/dist/data/arch/SNB.bin.br +0 -0
- package/dist/data/arch/TGL.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 +160 -0
- package/dist/wuica_asm.wasm.br +0 -0
- package/dist/wuica_wasm.wasm.br +0 -0
- package/index.d.ts +410 -0
- package/lib/archs.js +44 -0
- package/lib/cli.js +693 -0
- package/lib/core.js +36 -0
- package/lib/details.js +60 -0
- package/lib/examples.js +26 -0
- package/lib/files.js +40 -0
- package/lib/index.js +577 -0
- package/lib/insights.js +100 -0
- package/lib/mcp.js +366 -0
- package/lib/objfile.js +216 -0
- package/lib/thread-worker.js +49 -0
- package/lib/threads.js +235 -0
- package/lib/wasm.js +246 -0
- package/lib/web/accum.js +202 -0
- package/lib/web/asm-regions.js +415 -0
- package/lib/web/assembler.js +188 -0
- package/lib/web/caveats.js +214 -0
- package/lib/web/examples.js +99 -0
- package/lib/web/loop-find.js +492 -0
- package/lib/web/showcase.js +368 -0
- package/lib/whatif.js +136 -0
- package/licenses/LICENSE-LLVM.txt +279 -0
- package/licenses/LICENSE-XED.txt +178 -0
- package/package.json +58 -4
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
// Known blind spots of the model, found in a response's instructions (no analysis): what uiCA does not simulate and
|
|
2
|
+
// that the block is likely to meet. Shown as one small line under the throughput (render.js) and as insights.caveats in
|
|
3
|
+
// the API (the npm package has a copy: packages/wuica/lib/web). No DOM.
|
|
4
|
+
// rmw-index a read-modify-write to an indexed address (inc dword ptr [rsp+rdx*4]), or a load and a store to the same
|
|
5
|
+
// indexed address, where a register of the address is written in the loop other than by advancing it by a
|
|
6
|
+
// constant (rdx loaded from the data): whether an iteration's load waits for an earlier store (store
|
|
7
|
+
// forwarding) depends on the data (a histogram of repeated bytes); uiCA assumes it does not. Not for an
|
|
8
|
+
// address of induction registers only (saxpy's [rsi+rax*4], rax advanced by add rax, 8: never the same
|
|
9
|
+
// location twice) nor for a loop-invariant one (the same location every iteration: the model has that
|
|
10
|
+
// chain through memory, criticalPath operand "mem").
|
|
11
|
+
// exit a loop with an exit before its branch (a search; a forward branch within the loop, an if, is not an
|
|
12
|
+
// exit): the exit's mispredict, once per call, is not modelled;
|
|
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.
|
|
16
|
+
// paths a conditional branch before the last instruction whose target is inside the block (an if/else, or a
|
|
17
|
+
// second back edge: clang's remainder loop of sum_mod, `jne .LBB0_9` back to the top and the latch to the
|
|
18
|
+
// middle): uiCA runs the block straight through, so both sides of the branch are simulated one after the
|
|
19
|
+
// other, while a real iteration takes one. Not an inner loop's back edge (backward, to after the target
|
|
20
|
+
// of the block's own branch: the outer loop of count_u8). Named by source line when the response has the
|
|
21
|
+
// lines of the picked span (response.loop.lines, the package's analyze; alignment padding, nops the
|
|
22
|
+
// source does not have, left out), else by instruction. Region markers around one side analyze that side.
|
|
23
|
+
// split loads at an offset that is not a multiple of their width (ymmword ptr [rdi+rax*4+0x4]): with a base
|
|
24
|
+
// aligned to the width, some of them cross a 64-byte line (split loads cost extra). Per CPU (`config`, its
|
|
25
|
+
// configuration in uarch.json): where splitLoadCycles is 0 (the stock Intel models) no crossing is charged,
|
|
26
|
+
// so every such load warns. Where it is > 0 (ZEN3, ADL-P, ICX) the model charges the crossings of a load
|
|
27
|
+
// whose address registers change only by adding an immediate (add, sub, inc, dec; instr_builder.rs,
|
|
28
|
+
// split_load_patterns), assuming the start alignment with the fewest crossings; only loads with an
|
|
29
|
+
// address register written otherwise (mov, lea, a load, ...) warn, worded for that.
|
|
30
|
+
|
|
31
|
+
import { family } from './accum.js';
|
|
32
|
+
|
|
33
|
+
const SIZE = { byte: 1, word: 2, dword: 4, qword: 8, xmmword: 16, ymmword: 32, zmmword: 64, tbyte: 10 };
|
|
34
|
+
const RMW = /^(?:add|adc|sub|sbb|and|or|xor|inc|dec|neg|not|shl|shr|sar|sal|rol|ror|rcl|rcr|xadd|btc|btr|bts|cmpxchg\w*)$/;
|
|
35
|
+
// a memory operand in any position is read and written (xchg [mem], reg or xchg reg, [mem])
|
|
36
|
+
const RMW_ANY = /^xchg$/;
|
|
37
|
+
// an address that is not an access (lea), or hints that move no data the loop uses
|
|
38
|
+
const NO_ACCESS = /^(?:lea|nop\w*|prefetch\w*|clflush\w*|clwb|cldemote)$/;
|
|
39
|
+
// instructions whose memory destination is only written (a store, not a read)
|
|
40
|
+
const STORE_ONLY = /^(?:v?mov\w*|movnt\w*|stos\w*|set\w+|v?pextr\w+|v?extract\w*|vextract\w+|v?pmovm\w*|vp?scatter\w+|vmaskmov\w*|vpmaskmov\w*|v?cvt\w*|push\w*|pop\w*|fstp?\w*|fistp?\w*)$/;
|
|
41
|
+
const COND_BRANCH = /^(?:j(?!mp)[a-z]+|loop\w*)$/;
|
|
42
|
+
// Splits with a split-load cost: what writes a general-purpose register. An add or sub of an immediate, inc or dec, to a
|
|
43
|
+
// 32- or 64-bit register keeps it advancing by a constant (instr_builder.rs, affine_delta); any other write does not.
|
|
44
|
+
// Without a register destination (cmp, test, bt, push, branches) the first operand is only read.
|
|
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)$/;
|
|
46
|
+
const BOTH_WRITTEN = /^(?:xchg|xadd|mulx)$/; // the second register operand is written too
|
|
47
|
+
const IMPLICIT_WRITES = [
|
|
48
|
+
[/^(?:i?div|mul|cdq|cqo|cwd|rdtsc)$/, ['rax', 'rdx']],
|
|
49
|
+
[/^(?:cbw|cwde|cdqe|lahf|cmpxchg|lods\w*)$/, ['rax']],
|
|
50
|
+
[/^(?:cpuid|cmpxchg8b|cmpxchg16b)$/, ['rax', 'rbx', 'rcx', 'rdx']],
|
|
51
|
+
[/^(?:rdtscp|rdmsr|rdpmc|xgetbv)$/, ['rax', 'rcx', 'rdx']],
|
|
52
|
+
[/^(?:syscall)$/, ['rcx', 'r11']],
|
|
53
|
+
[/^(?:loop\w*)$/, ['rcx']],
|
|
54
|
+
[/^(?:movs[bwdq]?|cmps[bwdq]?)$/, ['rsi', 'rdi']],
|
|
55
|
+
[/^(?:stos\w*|scas\w*|ins[bwd]?)$/, ['rdi']],
|
|
56
|
+
[/^(?:lods\w*|outs[bwd]?)$/, ['rsi']],
|
|
57
|
+
[/^(?:v?pcmpestri|v?pcmpistri)$/, ['rcx']],
|
|
58
|
+
[/^(?:push\w*|pop\w*|call\w*|ret\w*|enter|leave)$/, ['rsp']],
|
|
59
|
+
[/^(?:leave)$/, ['rbp']],
|
|
60
|
+
];
|
|
61
|
+
const IMM = /^-?(?:0x[0-9a-f]+|\d+)$/i;
|
|
62
|
+
// {register family: [rows]} of the writes that do not advance a register by a constant. lea: lea r, [r+imm] advances it
|
|
63
|
+
// too (for rmw-index; the split-load model, affine_delta, does not count it)
|
|
64
|
+
function untrackedWrites(rows, { lea = false } = {}) {
|
|
65
|
+
const out = new Map();
|
|
66
|
+
const add = (reg, row) => { const f = family(reg); if (f?.kind === 'gpr') (out.get(f.base) ?? out.set(f.base, []).get(f.base)).push(row); };
|
|
67
|
+
rows.forEach((x, row) => {
|
|
68
|
+
if (x?.kind !== 'instr') return;
|
|
69
|
+
const t = String(x.label).trim().replace(/\s+/g, ' ').toLowerCase();
|
|
70
|
+
const rep = /^rep\w* /.test(t);
|
|
71
|
+
const u = t.replace(/^(?:lock|rep[a-z]*|notrack|bnd) /, '');
|
|
72
|
+
const sp = u.indexOf(' ');
|
|
73
|
+
const mn = sp < 0 ? u : u.slice(0, sp);
|
|
74
|
+
const ops = sp < 0 ? [] : u.slice(sp + 1).split(',').map((o) => o.trim());
|
|
75
|
+
if (rep) add('rcx', row);
|
|
76
|
+
for (const [re, regs] of IMPLICIT_WRITES) if (re.test(mn)) for (const r of regs) add(r, row);
|
|
77
|
+
if (mn === 'imul' && ops.length === 1) { add('rax', row); add('rdx', row); }
|
|
78
|
+
if (!ops.length || READS_FIRST.test(mn)) return;
|
|
79
|
+
const f = family(ops[0]);
|
|
80
|
+
if (f?.kind === 'gpr') {
|
|
81
|
+
const self = lea && mn === 'lea' && ops.length === 2 && new RegExp(`^(?:(?:\\w+ )?ptr )?\\[${ops[0]}(?:[+-](?:0x[0-9a-f]+|\\d+))?\\]$`).test(ops[1]);
|
|
82
|
+
const affine = f.size <= 1 && (((mn === 'add' || mn === 'sub') && ops.length === 2 && IMM.test(ops[1])) || ((mn === 'inc' || mn === 'dec') && ops.length === 1) || self);
|
|
83
|
+
if (!affine) add(ops[0], row);
|
|
84
|
+
}
|
|
85
|
+
if (BOTH_WRITTEN.test(mn) && ops[1]) add(ops[1], row);
|
|
86
|
+
});
|
|
87
|
+
return out;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// The memory operand of an instruction label (XED's Intel syntax): {first, size, base, index, disp, text} or null.
|
|
91
|
+
export function memOperand(label) {
|
|
92
|
+
const t = String(label).trim().replace(/\s+/g, ' ').replace(/^(?:lock|rep[a-z]*|notrack|bnd) /i, '');
|
|
93
|
+
const sp = t.indexOf(' ');
|
|
94
|
+
if (sp < 0) return null;
|
|
95
|
+
const mnemonic = t.slice(0, sp).toLowerCase();
|
|
96
|
+
if (NO_ACCESS.test(mnemonic)) return null;
|
|
97
|
+
const ops = t.slice(sp + 1);
|
|
98
|
+
const m = /(?:(\w+) ptr )?\[([^\]]*)\]/.exec(ops);
|
|
99
|
+
if (!m) return null;
|
|
100
|
+
const inner = m[2].replace(/\s/g, '').toLowerCase();
|
|
101
|
+
const parts = inner.split(/(?=[+-])/);
|
|
102
|
+
let base = null, index = null, disp = 0;
|
|
103
|
+
for (const p of parts) {
|
|
104
|
+
const v = p.replace(/^\+/, '');
|
|
105
|
+
if (/\*/.test(v)) index = v.split('*')[0];
|
|
106
|
+
else if (/^-?(?:0x[0-9a-f]+|\d+)$/.test(v)) disp += Number(v.startsWith('-') ? -Number(v.slice(1)) : Number(v));
|
|
107
|
+
else if (/^[a-z]\w*$/.test(v)) { if (base === null) base = v; else index = v; }
|
|
108
|
+
}
|
|
109
|
+
if (base === 'rip') return null;
|
|
110
|
+
return { mnemonic, first: ops.indexOf('[') <= (ops.indexOf(',') < 0 ? ops.length : ops.indexOf(',')), size: SIZE[String(m[1] ?? '').toLowerCase()] ?? null, base, index, disp, text: m[0].replace(/^\w+ ptr /, '') };
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// [{id, text, title, rows}] (rows: indices into response.rows), in the order rmw-index, paths, exit, split; [] if none.
|
|
114
|
+
// config: the CPU's configuration in uarch.json (configs[response.arch]); only splitLoadCycles is read. Without it, as on
|
|
115
|
+
// a CPU without a split-load cost. Kept per response and split-load cost (the result view is drawn again on every
|
|
116
|
+
// change of its own controls).
|
|
117
|
+
const memo = new WeakMap();
|
|
118
|
+
const some = (rows, ks) => `${ks.slice(0, 4).map((k) => rows[k].label).join('; ')}${ks.length > 4 ? '; …' : ''}`;
|
|
119
|
+
export function caveats(response, config = null) {
|
|
120
|
+
const splitCost = Number(config?.splitLoadCycles) > 0;
|
|
121
|
+
const kept = response && typeof response === 'object' ? memo.get(response) : undefined;
|
|
122
|
+
if (kept?.has(splitCost)) return kept.get(splitCost);
|
|
123
|
+
const rows = Array.isArray(response?.rows) ? response.rows : [];
|
|
124
|
+
const out = [];
|
|
125
|
+
const rmw = [], split = [], early = [], inside = [];
|
|
126
|
+
const target = (row, op) => {
|
|
127
|
+
const v = /^0x[0-9a-f]+$/i.test(op ?? '') ? Number(BigInt.asIntN(64, BigInt(op))) : null;
|
|
128
|
+
return v === null ? null : offset.get(row) + v;
|
|
129
|
+
};
|
|
130
|
+
const loads = new Map(), stores = new Map();
|
|
131
|
+
let last = -1, total = 0;
|
|
132
|
+
const offset = new Map();
|
|
133
|
+
rows.forEach((x, row) => { if (x?.kind === 'instr') { last = row; offset.set(row, total); total += String(x.opcode ?? '').length / 2; } });
|
|
134
|
+
// where the block's own branch goes (the loop's header; not always the top: a rotated loop)
|
|
135
|
+
const [lastMn, lastOp] = last >= 0 ? String(rows[last].label).trim().split(/\s+/) : [];
|
|
136
|
+
const latch = last >= 0 && /^(?:j[a-z]+|loop\w*)$/.test(String(lastMn).toLowerCase()) ? target(last, lastOp) : null;
|
|
137
|
+
const head = latch !== null && latch >= 0 && latch < total ? latch : 0;
|
|
138
|
+
rows.forEach((x, row) => {
|
|
139
|
+
if (x?.kind !== 'instr') return;
|
|
140
|
+
const [mn, op] = String(x.label).trim().split(/\s+/);
|
|
141
|
+
if (row !== last && COND_BRANCH.test(mn.toLowerCase())) {
|
|
142
|
+
// the label's target is relative to the branch (XED, as if it were at address 0): inside the loop's code (an
|
|
143
|
+
// if) it is not an exit
|
|
144
|
+
const to = target(row, op);
|
|
145
|
+
if (to === null || to < 0 || to >= total) early.push(row);
|
|
146
|
+
// forward (an if/else), or back to the header or before it (a second back edge, a rotated if/else); backward to
|
|
147
|
+
// after the header is an inner loop's back edge
|
|
148
|
+
else if (to > offset.get(row) || to <= head) inside.push(row);
|
|
149
|
+
}
|
|
150
|
+
const m = memOperand(x.label);
|
|
151
|
+
if (!m) return;
|
|
152
|
+
const storeOnly = m.first && STORE_ONLY.test(m.mnemonic);
|
|
153
|
+
// read-modify-write, or a load and a store, to the same indexed address
|
|
154
|
+
if (m.index) {
|
|
155
|
+
if ((m.first && RMW.test(m.mnemonic)) || RMW_ANY.test(m.mnemonic)) rmw.push(row);
|
|
156
|
+
else if (storeOnly) (stores.get(m.text) ?? stores.set(m.text, []).get(m.text)).push(row);
|
|
157
|
+
else if (!m.first) (loads.get(m.text) ?? loads.set(m.text, []).get(m.text)).push(row);
|
|
158
|
+
}
|
|
159
|
+
// a load that may split a cache line
|
|
160
|
+
if (!storeOnly && m.size && m.size >= 2 && m.size <= 64 && ((m.disp % m.size) + m.size) % m.size !== 0) split.push(row);
|
|
161
|
+
});
|
|
162
|
+
for (const [addr, st] of stores) if (loads.has(addr)) rmw.push(...loads.get(addr), ...st);
|
|
163
|
+
// only where the address can repeat with the data: a register of it written other than by advancing it by a constant
|
|
164
|
+
const dynamic = rmw.length ? untrackedWrites(rows, { lea: true }) : null;
|
|
165
|
+
const hit = rmw.filter((row) => { const m = memOperand(rows[row].label); return [m.base, m.index].some((r) => dynamic.has(family(r)?.base)); });
|
|
166
|
+
if (hit.length) {
|
|
167
|
+
const r = [...new Set(hit)].sort((a, b) => a - b);
|
|
168
|
+
out.push({ id: 'rmw-index', text: 'store forwarding', rows: r,
|
|
169
|
+
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` });
|
|
170
|
+
}
|
|
171
|
+
// a call: the function it calls is not part of the block
|
|
172
|
+
const calls = rows.flatMap((x, row) => (x?.kind === 'instr' && /^call/i.test(String(x.label).trim()) ? [row] : []));
|
|
173
|
+
if (calls.length) {
|
|
174
|
+
out.push({ id: 'call', text: 'calls', rows: calls,
|
|
175
|
+
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` });
|
|
176
|
+
}
|
|
177
|
+
// a branch within the block: both sides are simulated
|
|
178
|
+
if (inside.length) {
|
|
179
|
+
const instrRows = rows.flatMap((x, row) => (x?.kind === 'instr' ? [row] : []));
|
|
180
|
+
// the source line of each row: as many lines as instructions, or as many once the padding (nops of .p2align) is out
|
|
181
|
+
const given = Array.isArray(response?.loop?.lines) ? response.loop.lines : null;
|
|
182
|
+
const real = instrRows.filter((row) => !/^(?:nop|data16|cs nop|xchg ax, ax)\b/i.test(String(rows[row].label).trim()));
|
|
183
|
+
const mapped = given?.length === instrRows.length ? instrRows : given?.length === real.length ? real : null;
|
|
184
|
+
const where = inside.map((row) => (mapped && mapped.includes(row) ? given[mapped.indexOf(row)] : null));
|
|
185
|
+
const lines = where.every((x) => x !== null) ? where : null;
|
|
186
|
+
if (!lines) where.splice(0, where.length, ...inside.map((row) => instrRows.indexOf(row) + 1));
|
|
187
|
+
const at = `${lines ? 'line' : 'instruction'}${where.length > 1 ? 's' : ''} ${where.join(', ')}`;
|
|
188
|
+
const one = inside.length === 1;
|
|
189
|
+
out.push({ id: 'paths', text: 'both paths', rows: inside,
|
|
190
|
+
title: `both sides of the branch${one ? '' : 'es'} at ${at} (${[...new Set(inside.map((row) => String(rows[row].label).trim().split(/\s+/)[0]))].join(', ')}) are simulated, one after the other; a real iteration takes one, so the cycles per iteration (and the step) count both. To analyze one side, put # wuica-begin and # wuica-end around it` });
|
|
191
|
+
}
|
|
192
|
+
// an exit before the loop's branch
|
|
193
|
+
if (response?.mode === 'loop' && early.length) {
|
|
194
|
+
out.push({ id: 'exit', text: 'exit mispredict', rows: early,
|
|
195
|
+
title: 'the loop has an exit before its branch: its mispredict when the loop ends (once per call) is not modelled; with short trip counts it can cost more than the loop' });
|
|
196
|
+
}
|
|
197
|
+
if (split.length && !splitCost) {
|
|
198
|
+
out.push({ id: 'split', text: `split loads? (${split.length})`, rows: split,
|
|
199
|
+
title: `${split.length} load${split.length === 1 ? '' : 's'} at an offset that is not a multiple of the width (${some(rows, split)}): some cross a 64-byte line, which costs extra; not modelled` });
|
|
200
|
+
} else if (split.length) {
|
|
201
|
+
// this CPU's model charges the crossings of loads whose address advances by a constant: only the others warn
|
|
202
|
+
const writes = untrackedWrites(rows);
|
|
203
|
+
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)); };
|
|
204
|
+
const untracked = split.filter((row) => regsOf(row).length);
|
|
205
|
+
if (untracked.length) {
|
|
206
|
+
const regs = [...new Set(untracked.flatMap(regsOf))];
|
|
207
|
+
const by = [...new Set(regs.flatMap((r) => writes.get(r)))].sort((a, b) => a - b);
|
|
208
|
+
out.push({ id: 'split', text: `split loads? (${untracked.length})`, rows: untracked,
|
|
209
|
+
title: `${untracked.length} load${untracked.length === 1 ? '' : 's'} at an offset that is not a multiple of the width (${some(rows, untracked)}): some may cross a 64-byte line. This CPU's model charges a crossing only for a load whose address advances by a constant each iteration (its registers changed only by add, sub, inc or dec of an immediate); ${regs.join(', ')} ${regs.length === 1 ? 'is' : 'are'} written otherwise here (${some(rows, by)}), so these crossings are not modelled` });
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
if (response && typeof response === 'object') (kept ?? memo.set(response, new Map()).get(response)).set(splitCost, out);
|
|
213
|
+
return out;
|
|
214
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
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
|
+
|
|
6
|
+
import { SHA256_BLOCK_HEX } from './showcase.js';
|
|
7
|
+
|
|
8
|
+
function ex(id, title, arch, intel, att, listing) {
|
|
9
|
+
const hex = listing.map((l) => l[0].replace(/ /g, '')).join('');
|
|
10
|
+
let addr = 0;
|
|
11
|
+
const width = Math.max(...listing.map((l) => l[0].length));
|
|
12
|
+
const lines = listing.map(([bytes, text]) => {
|
|
13
|
+
const line = `${addr.toString(16).padStart(4)}: ${bytes.padEnd(width)} ${text}`;
|
|
14
|
+
addr += (bytes.length + 1) / 3;
|
|
15
|
+
return line;
|
|
16
|
+
});
|
|
17
|
+
return { id, title, arch, hex, text: { intel: intel.join('\n') + '\n', att: att.join('\n') + '\n', hex: lines.join('\n') + '\n' } };
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export const EXAMPLES = [
|
|
21
|
+
ex('chain', 'Dependency chain: two dependent adds', 'SKL',
|
|
22
|
+
['loop:', ' add rax, 1', ' add rax, 1', ' dec r15', ' jnz loop'],
|
|
23
|
+
['loop:', ' addq $1, %rax', ' addq $1, %rax', ' decq %r15', ' jnz loop'],
|
|
24
|
+
[['48 83 c0 01', 'add rax, 0x1'], ['48 83 c0 01', 'add rax, 0x1'], ['49 ff cf', 'dec r15'], ['75 f3', 'jnz 0x0']]),
|
|
25
|
+
ex('loadstore', 'Load/store: store, then reload', 'ZEN3',
|
|
26
|
+
['loop:', ' mov qword ptr [rsi], rax', ' mov eax, dword ptr [rsi]', ' add rax, 1', ' dec r15', ' jnz loop'],
|
|
27
|
+
['loop:', ' movq %rax, (%rsi)', ' movl (%rsi), %eax', ' addq $1, %rax', ' decq %r15', ' jnz loop'],
|
|
28
|
+
[['48 89 06', 'mov qword ptr [rsi], rax'], ['8b 06', 'mov eax, dword ptr [rsi]'], ['48 83 c0 01', 'add rax, 0x1'],
|
|
29
|
+
['49 ff cf', 'dec r15'], ['75 f2', 'jnz 0x0']]),
|
|
30
|
+
ex('avx', 'AVX: FMA, add and multiply', 'ADL-P',
|
|
31
|
+
['loop:', ' vfmsub213pd ymm6, ymm5, ymm4', ' add r10, rcx', ' imul r8, r8', ' dec r15', ' jnz loop'],
|
|
32
|
+
['loop:', ' vfmsub213pd %ymm4, %ymm5, %ymm6', ' addq %rcx, %r10', ' imulq %r8, %r8', ' decq %r15', ' jnz loop'],
|
|
33
|
+
[['c4 e2 d5 aa f4', 'vfmsub213pd ymm6, ymm5, ymm4'], ['4c 03 d1', 'add r10, rcx'], ['4d 0f af c0', 'imul r8, r8'],
|
|
34
|
+
['49 ff cf', 'dec r15'], ['75 ef', 'jnz 0x0']]),
|
|
35
|
+
ex('stack', 'Stack: pops after an add to rsp', 'SKL',
|
|
36
|
+
['loop:', ' add rsp, 8', ' mov rsi, rbx', ' mov rdx, rbp', ' pop rbx', ' pop rbp', ' mov edi, eax', ' dec r15', ' jnz loop'],
|
|
37
|
+
['loop:', ' addq $8, %rsp', ' movq %rbx, %rsi', ' movq %rbp, %rdx', ' popq %rbx', ' popq %rbp', ' movl %eax, %edi', ' decq %r15', ' jnz loop'],
|
|
38
|
+
[['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'],
|
|
39
|
+
['89 c7', 'mov edi, eax'], ['49 ff cf', 'dec r15'], ['75 ed', 'jnz 0x0']]),
|
|
40
|
+
];
|
|
41
|
+
|
|
42
|
+
// Well-known loops (menu group "classic loops"). Not in the reference's corpus: the mock has no result for them.
|
|
43
|
+
// dot1/dot4 are the default A–B demo (the same work, one accumulator vs four).
|
|
44
|
+
export const CLASSIC_EXAMPLES = [
|
|
45
|
+
ex('saxpy', 'saxpy (AVX2 FMA)', 'SKL',
|
|
46
|
+
['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'],
|
|
47
|
+
['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'],
|
|
48
|
+
[['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']]),
|
|
49
|
+
ex('dot1', 'dot product, 1 accumulator', 'SKL',
|
|
50
|
+
['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'],
|
|
51
|
+
['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'],
|
|
52
|
+
[['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']]),
|
|
53
|
+
ex('dot4', 'dot product, 4 accumulators', 'SKL',
|
|
54
|
+
['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'],
|
|
55
|
+
['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'],
|
|
56
|
+
[['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']]),
|
|
57
|
+
ex('popcnt', 'popcount (false dependency)', 'SKL',
|
|
58
|
+
['loop:', ' popcnt rcx, qword ptr [rdi + rax*8]', ' add rdx, rcx', ' inc rax', ' cmp rax, rsi', ' jb loop'],
|
|
59
|
+
['loop:', ' popcntq (%rdi,%rax,8), %rcx', ' addq %rcx, %rdx', ' incq %rax', ' cmpq %rsi, %rax', ' jb loop'],
|
|
60
|
+
[['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']]),
|
|
61
|
+
ex('ptrchase', 'pointer chasing', 'SKL',
|
|
62
|
+
['loop:', ' mov rax, qword ptr [rax]', ' dec rcx', ' jnz loop'],
|
|
63
|
+
['loop:', ' movq (%rax), %rax', ' decq %rcx', ' jne loop'],
|
|
64
|
+
[['48 8b 00', 'mov rax, qword ptr [rax]'], ['48 ff c9', 'dec rcx'], ['75 f8', 'jnz 0x0']]),
|
|
65
|
+
ex('strlen', 'strlen (SSE2)', 'SKL',
|
|
66
|
+
['loop:', ' movdqa xmm1, xmmword ptr [rdi]', ' pcmpeqb xmm1, xmm0', ' pmovmskb eax, xmm1', ' add rdi, 16', ' test eax, eax', ' jz loop'],
|
|
67
|
+
['loop:', ' movdqa (%rdi), %xmm1', ' pcmpeqb %xmm0, %xmm1', ' pmovmskb %xmm1, %eax', ' addq $16, %rdi', ' testl %eax, %eax', ' je loop'],
|
|
68
|
+
[['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']]),
|
|
69
|
+
ex('crc32', 'CRC32', 'SKL',
|
|
70
|
+
['loop:', ' crc32 rax, qword ptr [rdi + rcx*8]', ' inc rcx', ' cmp rcx, rsi', ' jb loop'],
|
|
71
|
+
['loop:', ' crc32q (%rdi,%rcx,8), %rax', ' incq %rcx', ' cmpq %rsi, %rcx', ' jb loop'],
|
|
72
|
+
[['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']]),
|
|
73
|
+
ex('memcpy', 'memcpy (AVX)', 'SKL',
|
|
74
|
+
['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'],
|
|
75
|
+
['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'],
|
|
76
|
+
[['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']]),
|
|
77
|
+
ex('gemm', 'GEMM microkernel (4×8 FMA)', 'SKL',
|
|
78
|
+
['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'],
|
|
79
|
+
['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'],
|
|
80
|
+
[['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']]),
|
|
81
|
+
];
|
|
82
|
+
|
|
83
|
+
// A large block that shows the analyzer's speed (menu group "showcase"). Machine code only: `hexOnly` examples switch the
|
|
84
|
+
// input to hex.
|
|
85
|
+
export const SHOWCASE_EXAMPLES = [
|
|
86
|
+
{ id: 'sha256', title: 'SHA-256 block (2,949 instr)', arch: 'SKL', hex: SHA256_BLOCK_HEX.replace(/\s+/g, ''), text: { hex: SHA256_BLOCK_HEX },
|
|
87
|
+
// Intel assembly, loaded when picked (showcase-asm.js, about 60 KB); the example opens in asm mode, hex if it cannot load
|
|
88
|
+
asm: () => import('./showcase-asm.js').then((m) => m.SHA256_BLOCK_ASM) },
|
|
89
|
+
];
|
|
90
|
+
|
|
91
|
+
// Only offered while the mock engine is active: inputs that make it return what the recorded results lack.
|
|
92
|
+
export const MOCK_EXAMPLES = [
|
|
93
|
+
ex('synthetic', 'Mock only: synthetic result (all row kinds and notes)', 'SKL',
|
|
94
|
+
['div rcx', 'mov ah, bl', 'add rax, rcx', 'push rax', 'ud2', 'cmp rax, rbx', 'jz next', 'next:', '.byte 0x06', 'nop'],
|
|
95
|
+
['divq %rcx', 'movb %bl, %ah', 'addq %rcx, %rax', 'pushq %rax', 'ud2', 'cmpq %rbx, %rax', 'jz next', 'next:', '.byte 0x06', 'nop'],
|
|
96
|
+
[['48 f7 f1', 'div rcx'], ['88 dc', 'mov ah, bl'], ['48 01 c8', 'add rax, rcx'], ['50', 'push rax'], ['0f 0b', 'ud2'],
|
|
97
|
+
['48 39 d8', 'cmp rax, rbx'], ['74 00', 'jz 0x2'], ['06', '(bad)'], ['90', 'nop']]),
|
|
98
|
+
ex('error', 'Mock only: analysis error', 'SKL', ['ud2'], ['ud2'], [['0f 0b', 'ud2']]),
|
|
99
|
+
];
|