136 lines
7.3 KiB
JavaScript
136 lines
7.3 KiB
JavaScript
#!/usr/bin/env node
|
|
/**
|
|
* Turn godbolt's saved answers into x86-64 panes.
|
|
*
|
|
* For every program in mojo/local/ that has a golden with assembly (the header
|
|
* says `# mode: asm` or `# mode: both`), find the function the exercise is
|
|
* about, translate it to the model machine's syntax the way "Send to x86-64
|
|
* pane" does (src/mojo/att.ts), and write src/mojo/generated/listings.ts. The
|
|
* catalog hands that listing to any exercise that names the program and
|
|
* leaves its x86-64 pane empty (src/core/catalog.ts).
|
|
*
|
|
* node scripts/mojo/listings.mjs # all programs
|
|
* node scripts/mojo/listings.mjs demo # all programs, and say what happened to this one
|
|
*
|
|
* Needs `pixi run build` first (it imports from dist/). Offline: it reads the
|
|
* goldens only. A program with no matching golden (never verified, or edited
|
|
* since) simply has no listing yet, and the script says so.
|
|
*/
|
|
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
import crypto from 'node:crypto';
|
|
import { fileURLToPath } from 'node:url';
|
|
|
|
const ROOT = fileURLToPath(new URL('../..', import.meta.url));
|
|
|
|
/** The header keys scripts/mojo/mojocorpus/verify.py reads, and emit_ts.py strips from what the pane shows. */
|
|
const HEADER = /^#\s*(?:probe|stdin|args|mode|expect-stdout|expect|origin|status)\s*:\s?(.*)$/;
|
|
|
|
/** What emit_ts.py does to a program before the Mojo pane shows it: drop the header, then leading blank lines. Returns the source and how many lines went. */
|
|
export function stripHeader(text) {
|
|
const lines = text.split('\n');
|
|
let dropped = 0;
|
|
while (lines.length && HEADER.test(lines[0])) { lines.shift(); dropped++; }
|
|
while (lines.length && lines[0] === '') { lines.shift(); dropped++; }
|
|
return { source: lines.join('\n'), dropped };
|
|
}
|
|
|
|
/** The `# key: value` lines at the top of a program (only the ones verify.py knows), the last of each key winning. */
|
|
export function readHeader(text) {
|
|
const h = {};
|
|
for (const line of text.split('\n')) {
|
|
if (!line.startsWith('#')) break;
|
|
const m = /^#\s*(probe|stdin|args|mode|expect-stdout|expect|origin|status)\s*:\s?(.*)$/.exec(line);
|
|
if (m) h[m[1]] = m[2].trim();
|
|
}
|
|
return h;
|
|
}
|
|
|
|
/** godbolt marks the program's own lines (`main`) and the library's; only the program's become `; mojo line N`. */
|
|
function programLine(a, offset) {
|
|
const mine = a.main === true || (a.main !== false && (!a.file || String(a.file).split('/').pop() === 'example.mojo'));
|
|
if (!mine || typeof a.line !== 'number') return null;
|
|
const n = a.line - offset;
|
|
return n >= 1 ? n : null;
|
|
}
|
|
|
|
/** The golden that answers exactly this program: same source, mode and arguments, and it carries a listing. */
|
|
export function findGolden(goldenDir, text, mode, args) {
|
|
const want = crypto.createHash('sha256').update(Buffer.from(text, 'utf8')).digest('hex');
|
|
for (const f of fs.readdirSync(goldenDir)) {
|
|
if (!f.endsWith('.json')) continue;
|
|
const raw = fs.readFileSync(path.join(goldenDir, f), 'utf8');
|
|
if (!raw.includes(want)) continue; // cheap test before parsing the whole file
|
|
const g = JSON.parse(raw);
|
|
if (g.sourceSha256 === want && g.mode === mode && (g.args ?? '') === args && Array.isArray(g.result?.asm)) return g;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/** The function to start from: the first `def` in the program that is not `main`, as it appears in the listing. */
|
|
export function pickEntry(paneSource, functions) {
|
|
const user = functions.filter((f) => f.kind === 'user' && !/^main\(/.test(f.name));
|
|
for (const m of paneSource.matchAll(/^def\s+(\w+)/gm)) {
|
|
const hit = user.find((f) => f.name.startsWith(m[1] + '('));
|
|
if (hit) return hit;
|
|
}
|
|
return user[0] ?? null;
|
|
}
|
|
|
|
/**
|
|
* The listing for one program text, or { why } when there is none.
|
|
* `att` and `godbolt` are the compiled modules, passed in so tests can load them from dist/.
|
|
*/
|
|
export function buildListing(text, goldenDir, { forSimulator, simulatorSetup, listingFunctions }) {
|
|
const h = readHeader(text);
|
|
const mode = h.mode ?? 'run', args = h.args ?? '';
|
|
if (mode === 'run') return { why: 'its header says `# mode: run`; use `# mode: both` to keep the compiler\'s listing' };
|
|
const g = findGolden(goldenDir, text, mode, args);
|
|
if (!g) return { why: 'no godbolt answer with a listing for this exact source yet (run `pixi run play <name>`, which asks godbolt)' };
|
|
if (!g.result.compiled) return { why: 'godbolt could not compile it, so there is no listing' };
|
|
const { source: pane, dropped } = stripHeader(text);
|
|
const asm = g.result.asm.map((a) => ({ text: a.text ?? '', line: programLine(a, dropped) }));
|
|
const fns = listingFunctions(asm);
|
|
const entry = pickEntry(pane, fns);
|
|
if (!entry) return { why: `the listing holds none of the program's own functions${/-O0/.test(args) ? '' : ' (the optimiser may have inlined them all; put `-O0` in `# args:` to keep each one)'}` };
|
|
// the whole program's own code in the pane, with the entry first so it is the one that runs
|
|
const { source, data } = forSimulator(fns, entry, { all: true });
|
|
return { entry: entry.name, asm: source, setup: simulatorSetup(entry, data), build: args };
|
|
}
|
|
|
|
/** listings.ts with its header and interface kept and the table rewritten, programs in name order so the diff is only what changed. */
|
|
export function render(listings) {
|
|
const sorted = Object.fromEntries(Object.keys(listings).sort().map((k) => [k, listings[k]]));
|
|
const head = fs.readFileSync(path.join(ROOT, 'src/mojo/generated/listings.ts'), 'utf8').split('export const MOJO_LISTINGS')[0];
|
|
return `${head}export const MOJO_LISTINGS: Readonly<Record<string, MojoListing>> = ${JSON.stringify(sorted, null, 4)};\n`;
|
|
}
|
|
|
|
async function main() {
|
|
let att, godbolt;
|
|
try {
|
|
att = await import(path.join(ROOT, 'dist/mojo/att.js'));
|
|
godbolt = await import(path.join(ROOT, 'dist/services/godbolt.js'));
|
|
} catch (e) {
|
|
console.error('listings: dist/ is missing or stale; run `pixi run build` first.\n ' + e.message);
|
|
process.exit(1);
|
|
}
|
|
const mods = { forSimulator: att.forSimulator, simulatorSetup: att.simulatorSetup, listingFunctions: godbolt.listingFunctions };
|
|
const localDir = path.join(ROOT, 'mojo/local'), goldenDir = path.join(ROOT, 'mojo/golden');
|
|
const named = process.argv.slice(2).filter((a) => !a.startsWith('-'));
|
|
const listings = {};
|
|
for (const f of fs.readdirSync(localDir).filter((x) => x.endsWith('.mojo')).sort()) {
|
|
const name = f.replace(/\.mojo$/, '');
|
|
const r = buildListing(fs.readFileSync(path.join(localDir, f), 'utf8'), goldenDir, mods);
|
|
if (r.why) { if (named.includes(name)) console.log(`listings: ${name}: no listing — ${r.why}`); continue; }
|
|
listings[name] = { entry: r.entry, asm: r.asm, setup: r.setup, build: r.build };
|
|
if (named.includes(name) || !named.length) console.log(`listings: ${name}: ${r.entry}, ${r.asm.split('\n').length} lines of x86-64`);
|
|
}
|
|
const out = path.join(ROOT, 'src/mojo/generated/listings.ts');
|
|
const next = render(listings);
|
|
const changed = fs.readFileSync(out, 'utf8') !== next;
|
|
if (changed) fs.writeFileSync(out, next);
|
|
console.log(`listings: ${Object.keys(listings).length} program(s) with a listing; ${changed ? 'listings.ts rewritten' : 'listings.ts unchanged'}`);
|
|
}
|
|
|
|
if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) await main();
|