#!/usr/bin/env node /** * The assembly census: every function in every compiled listing the Mojo * corpus holds (library/mojo/golden, the answers godbolt gave with `asm`), sent to * the model machine the way "Send to x86-64 pane" sends it, and what the * assembler said. Run after `pixi run build`: * * node scripts/asm-census.mjs # the summary and the reasons * node scripts/asm-census.mjs --ops # every mnemonic, implemented or not, with counts * * docs/asm-coverage.md is this census written up; tests/asm-coverage.test.mjs * holds the numbers as a ratchet. */ import fs from 'node:fs'; import { forSimulator, intelListing } from '../dist/mojo/att.js'; import { listingFunctions } from '../dist/services/godbolt.js'; import { parseAsm } from '../dist/services/asm.js'; import { OPS } from '../dist/engines/asm86/interp.js'; const R = new URL('..', import.meta.url).pathname; export function census() { const ops = new Map(); // mnemonic -> { total, user, example } const reasons = new Map(); // normalised assembler message -> count let user = 0, userOk = 0, lib = 0, libOk = 0; const userFailures = []; for (const f of fs.readdirSync(R + 'library/mojo/golden')) { if (!f.endsWith('.json')) continue; const d = JSON.parse(fs.readFileSync(R + 'library/mojo/golden/' + f, 'utf8')); if (!d.result?.asm) continue; const fns = listingFunctions(d.result.asm); for (const fn of fns) { if (fn.kind === 'data') continue; for (const r of intelListing(fn, fns)) { if (!r.intel || r.intel.endsWith(':')) continue; const op = r.intel.trim().replace(/^lock\s+/, '').split(/\s+/)[0]; const e = ops.get(op) ?? { total: 0, user: 0, example: r.intel.trim() }; e.total++; if (fn.kind === 'user') e.user++; ops.set(op, e); } const { source } = forSimulator(fns, fn); const p = parseAsm(source); if (fn.kind === 'user') { user++; if (p.ok) userOk++; else userFailures.push(`${d.firstItem} ${fn.name}: ${p.diagnostics[0]?.message}`); } else { lib++; if (p.ok) libOk++; } for (const e of p.diagnostics.filter((x) => x.severity === 'error')) { const k = e.message.replace(/line \d+: /, '').replace(/"[^"]*"/g, '…').replace(/\[rip \+ \S+/, '[rip + …').replace(/\(.*\)$/, ''); reasons.set(k, (reasons.get(k) || 0) + 1); } } } const missing = [...ops].filter(([op]) => !OPS[op]).sort((a, b) => b[1].total - a[1].total); return { ops, missing, reasons, user, userOk, lib, libOk, userFailures }; } if (import.meta.url === `file://${process.argv[1]}`) { const c = census(); console.log(`${c.ops.size} distinct instructions in the corpus listings; ${c.missing.length} outside the model machine's set`); console.log(`user functions: ${c.userOk} of ${c.user} assemble; library and runtime: ${c.libOk} of ${c.lib}`); for (const f of c.userFailures) console.log(' user function refused:', f); console.log('\nwhy the rest are refused:'); for (const [k, n] of [...c.reasons].sort((a, b) => b[1] - a[1])) console.log(` ${String(n).padStart(4)} ${k}`); if (process.argv.includes('--ops')) { console.log('\ninstructions outside the set (count, in user functions, example):'); for (const [op, e] of c.missing) console.log(` ${String(e.total).padStart(5)} ${String(e.user).padStart(4)} ${op.padEnd(14)} ${e.example}`); console.log('\nevery instruction:'); for (const [op, e] of [...c.ops].sort((a, b) => b[1].total - a[1].total)) console.log(` ${String(e.total).padStart(5)} ${op.padEnd(14)} ${OPS[op] ? '' : 'NOT MODELLED'}`); } }