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.
Files changed (48) hide show
  1. package/LICENSE +661 -0
  2. package/README.md +546 -2
  3. package/bin/wuica-mcp.js +16 -0
  4. package/bin/wuica.js +25 -0
  5. package/dist/data/arch/ADL-P.bin.br +0 -0
  6. package/dist/data/arch/BDW.bin.br +0 -0
  7. package/dist/data/arch/CFL.bin.br +0 -0
  8. package/dist/data/arch/CLX.bin.br +0 -0
  9. package/dist/data/arch/HSW.bin.br +0 -0
  10. package/dist/data/arch/ICL.bin.br +0 -0
  11. package/dist/data/arch/ICX.bin.br +0 -0
  12. package/dist/data/arch/IVB.bin.br +0 -0
  13. package/dist/data/arch/KBL.bin.br +0 -0
  14. package/dist/data/arch/RKL.bin.br +0 -0
  15. package/dist/data/arch/SKL.bin.br +0 -0
  16. package/dist/data/arch/SKX.bin.br +0 -0
  17. package/dist/data/arch/SNB.bin.br +0 -0
  18. package/dist/data/arch/TGL.bin.br +0 -0
  19. package/dist/data/arch/ZEN3.bin.br +0 -0
  20. package/dist/data/uarch.json.br +0 -0
  21. package/dist/manifest.json +160 -0
  22. package/dist/wuica_asm.wasm.br +0 -0
  23. package/dist/wuica_wasm.wasm.br +0 -0
  24. package/index.d.ts +410 -0
  25. package/lib/archs.js +44 -0
  26. package/lib/cli.js +693 -0
  27. package/lib/core.js +36 -0
  28. package/lib/details.js +60 -0
  29. package/lib/examples.js +26 -0
  30. package/lib/files.js +40 -0
  31. package/lib/index.js +577 -0
  32. package/lib/insights.js +100 -0
  33. package/lib/mcp.js +366 -0
  34. package/lib/objfile.js +216 -0
  35. package/lib/thread-worker.js +49 -0
  36. package/lib/threads.js +235 -0
  37. package/lib/wasm.js +246 -0
  38. package/lib/web/accum.js +202 -0
  39. package/lib/web/asm-regions.js +415 -0
  40. package/lib/web/assembler.js +188 -0
  41. package/lib/web/caveats.js +214 -0
  42. package/lib/web/examples.js +99 -0
  43. package/lib/web/loop-find.js +492 -0
  44. package/lib/web/showcase.js +368 -0
  45. package/lib/whatif.js +136 -0
  46. package/licenses/LICENSE-LLVM.txt +279 -0
  47. package/licenses/LICENSE-XED.txt +178 -0
  48. package/package.json +58 -4
package/lib/core.js ADDED
@@ -0,0 +1,36 @@
1
+ // The result shapes that the library's main thread and its worker threads (threads.js) both build, so that a compare()
2
+ // gives the same rows whichever thread analyzed a microarchitecture.
3
+
4
+ /** Result codes (`code`), like an exit status: 0 = success. */
5
+ export const CODES = Object.freeze({ OK: 0, REQUEST: 1, ASSEMBLER: 2, ANALYSIS: 3 });
6
+
7
+ // A failed call: {code, error, stderr}, stderr as in Compiler Explorer (one entry per line, `tag` on positioned ones).
8
+ export function failure(code, error, stderr = [{ text: error }], extra = {}) {
9
+ return { code, error, stderr, ...extra };
10
+ }
11
+
12
+ // The failure when a microarchitecture's instruction table cannot be loaded.
13
+ export function tableFailure(id, e) {
14
+ return failure(CODES.ANALYSIS, `could not load the instruction table for ${id}: ${e.message}`);
15
+ }
16
+
17
+ // The analyzer's response (Analyzer.analyze) as a library result: the response itself, or a failure.
18
+ export function analysisResult(r) {
19
+ if (r.error) return failure(CODES.ANALYSIS, r.error, undefined, r.trapped ? { trapped: true } : {});
20
+ return r;
21
+ }
22
+
23
+ // The fields of a compare() row taken from an analysis result (after arch, name, year, prototype).
24
+ export function compareFields(r) {
25
+ if (r.code !== undefined) return { code: r.code, error: r.error };
26
+ return { code: CODES.OK, tp: r.tp, mode: r.mode, bottlenecks: r.bottlenecks, limits: r.limits, decodeErrors: r.decodeErrors };
27
+ }
28
+
29
+ // A block for warming up the analyzer: ~500 instructions of several kinds in a loop (a 24-instruction mix, 20 times;
30
+ // ~10 ms). V8 compiles the module's functions when they are first called, with its baseline compiler, and optimizes the
31
+ // hot ones in the background later: after one analysis of this block, the first analysis of a large block runs at
32
+ // nearly full speed (docs/PORTING.md, "Speed").
33
+ const WARM_MIX = '4883c001' + '488906' + '8b06' + 'c4e2d5aaf4' + '4c03d1' + '4d0fafc0' + 'c5fc100c87' + 'c4e27da80c86' + 'c5fc110c86'
34
+ + 'f3480fb80cc7' + '4801ca' + 'f2480f38f104cf' + '660f6f0f' + '660f74c8' + '660fd7c1' + '488b00' + 'c4626db8c0' + '4839d0'
35
+ + '4883c708' + '4c8d0c49' + '48c1e005' + '4421c0' + 'c5f857c0' + '488d0c0a';
36
+ export const WARM_HEX = WARM_MIX.repeat(20) + '49ffcf' + '0f853bf8ffff'; // dec r15; jnz to the start
package/lib/details.js ADDED
@@ -0,0 +1,60 @@
1
+ // The details of an analysis ("details": true; docs/WASM-API.md) in the compact form (summarize(), the MCP `analyze`
2
+ // JSON): per instruction its allowed ports in uops.info's notation, its latencies and front-end facts; the port set behind
3
+ // the Ports limit with the instructions that load it; the issue slots per cause. As the web page shows them (web/js/
4
+ // details.js, portset.js, slots.js); tests-js/web_details.mjs checks that they agree.
5
+
6
+ // "1*p0156+1*p23"; non-Intel port names listed: "1*[ALU0 ALU1]"
7
+ export function portString(portData) {
8
+ if (!Array.isArray(portData) || !portData.length) return '';
9
+ return portData.map((p) => {
10
+ const names = p.ports ?? [];
11
+ const intel = names.every((n) => /^Port \w$/.test(n));
12
+ return `${p.uops}*${intel ? 'p' + names.map((n) => n.slice(5)).join('') : `[${names.join(' ')}]`}`;
13
+ }).join('+');
14
+ }
15
+
16
+ // the latencies per (input, output) pair, without the flag outputs of an instruction that writes a register too
17
+ export function latencyPairs(det) {
18
+ const all = Array.isArray(det?.latencies) ? det.latencies : [];
19
+ const isFlags = (x) => String(x).startsWith('flags');
20
+ const regOut = all.some((l) => !isFlags(l.to));
21
+ const out = [];
22
+ for (const l of all) {
23
+ if (regOut && isFlags(l.to)) continue;
24
+ if (!out.some((x) => x.from === l.from && x.to === l.to)) out.push(l);
25
+ }
26
+ return out;
27
+ }
28
+
29
+ const round = (x) => Math.round(x * 100) / 100;
30
+
31
+ // the fields one instruction of the compact form gets
32
+ export function instrDetails(det) {
33
+ if (!det) return null;
34
+ const out = { allowedPorts: portString(det.portData), uops: det.uops, fusedUops: det.retireSlots };
35
+ if (det.uopsMS) out.msUops = det.uopsMS;
36
+ const lat = latencyPairs(det);
37
+ if (lat.length) out.latencies = lat.map((l) => `${l.from}->${l.to}:${l.cycles}`);
38
+ const fe = { length: det.length, offset: det.offset };
39
+ if (det.crosses?.length) fe.crosses = det.crosses;
40
+ for (const k of ['lcp', 'microcoded', 'complexDecoder', 'macroFusedWithNext', 'macroFusedWithPrev']) if (det[k]) fe[k] = true;
41
+ out.frontEnd = fe;
42
+ return out;
43
+ }
44
+
45
+ // {portSet, issueSlots} for insights (either may be absent)
46
+ export function detailInsights(r) {
47
+ const d = r?.details;
48
+ if (!d) return {};
49
+ const out = {};
50
+ if (d.ports && Array.isArray(d.ports.rows)) {
51
+ const instructions = d.ports.rows.map((uops, index) => ({ index, instr: r.rows[index]?.label, uops: round(uops) })).filter((x) => x.uops > 0)
52
+ .sort((a, b) => b.uops - a.uops || a.index - b.index);
53
+ out.portSet = { ports: d.ports.ports, uops: round(d.ports.uops), cycles: round(d.ports.tp), instructions };
54
+ }
55
+ if (d.issue?.slots) {
56
+ const s = d.issue.slots;
57
+ out.issueSlots = { width: d.issue.width, perIteration: { issued: s.issued, frontEnd: s.frontEnd, rob: s.rob, rs: s.rs, other: s.other } };
58
+ }
59
+ return out;
60
+ }
@@ -0,0 +1,26 @@
1
+ // The built-in examples: the web page's set (web/js/examples.js, copied to lib/web with the other shared files, so the
2
+ // page and the package cannot drift): the basics, the classic loops and the SHA-256 showcase (machine code only: its
3
+ // assembly, about 60 KB, stays on the page). Not the mock engine's entries.
4
+
5
+ import { EXAMPLES, CLASSIC_EXAMPLES, SHOWCASE_EXAMPLES } from './web/examples.js';
6
+
7
+ const GROUPS = [['basics', EXAMPLES], ['classic', CLASSIC_EXAMPLES], ['showcase', SHOWCASE_EXAMPLES]];
8
+
9
+ // [{name, title, group, arch, instructions, hex, intel?, att?}]: arch is the CPU the page shows it on first; intel and
10
+ // att the assembly (absent for the showcase, which is machine code only).
11
+ const LIST = Object.freeze(GROUPS.flatMap(([group, list]) => list.map((e) => {
12
+ const asm = typeof e.text.intel === 'string';
13
+ // the instructions: the lines of the listing (32-byte lines for the showcase: its title says how many)
14
+ const instructions = asm ? e.text.hex.trim().split('\n').length : Number(/([\d,]+) instr/.exec(e.title)?.[1].replace(/,/g, '') ?? 0);
15
+ return Object.freeze({ name: e.id, title: e.title, group, arch: e.arch, instructions, hex: e.hex, ...(asm ? { intel: e.text.intel, att: e.text.att } : {}) });
16
+ })));
17
+
18
+ export function examples() {
19
+ return LIST;
20
+ }
21
+
22
+ // The example called `name` (case-insensitive), or null.
23
+ export function findExample(name) {
24
+ const n = String(name ?? '').toLowerCase();
25
+ return LIST.find((e) => e.name.toLowerCase() === n) ?? null;
26
+ }
package/lib/files.js ADDED
@@ -0,0 +1,40 @@
1
+ // Reading the package's files (shared by the library and its worker threads).
2
+
3
+ import fs from 'node:fs/promises';
4
+ import { readFileSync, existsSync } from 'node:fs';
5
+ import path from 'node:path';
6
+ import zlib from 'node:zlib';
7
+ import { promisify } from 'node:util';
8
+
9
+ const brotli = promisify(zlib.brotliDecompress);
10
+ // a Uint8Array view of a Buffer (new Uint8Array(buffer) would copy it)
11
+ const view = (b) => new Uint8Array(b.buffer, b.byteOffset, b.byteLength);
12
+
13
+ // Reads a file; `.br` files are decompressed. For a path without `.br`, `<path>.br` is tried first.
14
+ export async function readMaybeCompressed(file) {
15
+ if (file.endsWith('.br')) return view(await brotli(await fs.readFile(file)));
16
+ try {
17
+ return view(await brotli(await fs.readFile(file + '.br')));
18
+ } catch (e) {
19
+ if (e.code !== 'ENOENT') throw e;
20
+ }
21
+ return view(await fs.readFile(file));
22
+ }
23
+
24
+ // The file of an instruction table in a data directory: the binary form (<name>.bin, the package's), else the JSON
25
+ // (<name>.json, as tools/export_data.py writes it), each plain or .br.
26
+ export function tableFile(dataDir, name) {
27
+ const bin = path.join(dataDir, 'arch', `${name}.bin`);
28
+ return existsSync(bin) || existsSync(bin + '.br') ? bin : path.join(dataDir, 'arch', `${name}.json`);
29
+ }
30
+
31
+ // The same, synchronously (an analyzer reloads its tables this way after a trap, instead of keeping their bytes).
32
+ export function readMaybeCompressedSync(file) {
33
+ if (file.endsWith('.br')) return view(zlib.brotliDecompressSync(readFileSync(file)));
34
+ try {
35
+ return view(zlib.brotliDecompressSync(readFileSync(file + '.br')));
36
+ } catch (e) {
37
+ if (e.code !== 'ENOENT') throw e;
38
+ }
39
+ return view(readFileSync(file));
40
+ }