Baseline: tree as shared

This commit is contained in:
alexm committed 2026-10-05 03:03:38 +00:00
commit 659c03bbf8
1471 files changed
+990535

No files matched your search

+243
View File
@@ -0,0 +1,243 @@
/**
* Compiler Explorer (godbolt.org): the real Mojo compiler behind the Compile
* button. Programming+ ships no compiler and never says a program compiles
* without asking one.
*
* One request compiles, returns the listing, and runs the program (what the
* corpus calls mode "both"; probes/godbolt-asm-and-run confirmed one request
* returns both). The body is the one scripts/mojo/mojocorpus/godbolt.py
* sends, and tests/mojo.test.mjs compares the two, so the button and the
* corpus can never ask different questions.
*
* What the probes taught us about Mojo 1.1.0 on godbolt, which shapes this file:
* - godbolt runs `mojo build --emit=asm -o /app/output.s <args> /app/example.mojo`
* and passes no Intel-syntax flag, so the listing is AT&T (see src/mojo/att.ts);
* - errors arrive as `<source>:LINE:COL: error: ...`, then the source line, a
* caret line, and sometimes a fix-it line (the replacement or insertion);
* - the user's own functions are labelled "example::name(args)", and only an
* unoptimised build (-O0) keeps them out of line;
* - with --debug-level=line-tables (probes/godbolt-line-tables*), instructions
* carry source lines, at no cost in compile time; but instructions inlined
* from the standard library carry lines of the library's own files (print's
* default arguments point at line 218 of io.mojo), so a line is the
* program's only when godbolt says it comes from the program's file.
*/
import type { Diagnostic, QuickFix } from '../core/types.js';
import { GODBOLT_BASE, GODBOLT_COMPILER, MOJO_VERSION } from '../mojo/generated/pin.js';
/** How the program is built: as written (every function kept, -O0) or as `mojo build` does by default. */
export type BuildLevel = 'O0' | 'default';
/** Line tables on both: they map instructions to lines and cost nothing in compile time (probes/godbolt-line-tables*). */
export const BUILD_ARGS: Record<BuildLevel, string> = { O0: '-O0 --debug-level=line-tables', default: '--debug-level=line-tables' };
const ASM_FILTERS = { binary: false, execute: false, intel: true, demangle: true, labels: true, libraryCode: false, directives: true, commentOnly: true, trim: true };
/** The request body, field for field what the Python harness sends in mode "both". */
export function mojoRequestBody(source: string, stdin = '', args = ''): Record<string, unknown> {
return {
source,
options: {
userArguments: args,
executeParameters: { args: [], stdin },
compilerOptions: { executorRequest: false, skipAsm: false },
filters: { ...ASM_FILTERS, execute: true },
tools: [],
libraries: [],
},
lang: 'mojo',
allowStoreCodeDebug: false,
};
}
export function mojoRequestUrl(base = GODBOLT_BASE, compiler = GODBOLT_COMPILER): string {
return `${base.replace(/\/+$/, '')}/api/compiler/${encodeURIComponent(compiler)}/compile`;
}
/**
* One listing line. `line` is a line of the program, when the instruction came
* from it; `from` says where an instruction inlined from elsewhere (the standard
* library) came from, so it is never mistaken for a line of the program.
*/
export interface AsmLine { text: string; line: number | null; from?: { file: string; line: number } }
/**
* Which file a source line belongs to. Accepts each way godbolt can mark the
* program's own file: `mainsource: true`, no file named, or the input file's
* name (`inputFilename`, example.mojo).
*/
export function sourceOf(src: any, inputFile = 'example.mojo'): Pick<AsmLine, 'line' | 'from'> {
if (!src || typeof src.line !== 'number') return { line: null };
const file: string | null = src.file ? String(src.file) : null;
const base = file ? file.split('/').pop()! : null;
const main = src.mainsource === true || (src.mainsource !== false && (!file || base === inputFile));
return main ? { line: src.line } : { line: null, from: { file: base ?? '?', line: src.line } };
}
/** One answer, in the shape the Python harness normalises to. */
export interface Answer {
compiled: boolean;
buildStderr: string;
executed: boolean;
exitCode: number | null;
stdout: string;
stderr: string;
timedOut: boolean;
asm: AsmLine[];
}
const joinLines = (xs: unknown): string => (Array.isArray(xs) ? xs.map((l: any) => (typeof l === 'string' ? l : l?.text ?? '')).join('\n') : '');
export function normaliseAnswer(data: any): Answer {
const exe = data?.execResult ?? {};
return {
compiled: data?.code === 0,
buildStderr: joinLines(data?.stderr),
executed: !!exe.didExecute,
exitCode: typeof exe.code === 'number' ? exe.code : null,
stdout: joinLines(exe.stdout),
stderr: joinLines(exe.stderr),
timedOut: !!exe.timedOut,
asm: Array.isArray(data?.asm) ? data.asm.filter((a: any) => a && typeof a === 'object').map((a: any) => ({ text: String(a.text ?? ''), ...sourceOf(a.source, data?.inputFilename || 'example.mojo') })) : [],
};
}
/* ---------------------------------------------------------------------------
* Compiler diagnostics
* ------------------------------------------------------------------------- */
const HEAD = /^<source>:(\d+):(\d+): (error|warning|note|remark): (.*)$/;
const ORIGIN = `the Mojo ${MOJO_VERSION} compiler`;
interface RawDiag { severity: string; line: number; col: number; message: string; excerpt: string; caret: string; fixit: string | null }
function readRaw(stderr: string): RawDiag[] {
const lines = stderr.split('\n');
const out: RawDiag[] = [];
for (let i = 0; i < lines.length; i++) {
const m = HEAD.exec(lines[i]);
if (!m) continue;
const d: RawDiag = { severity: m[3], line: Number(m[1]), col: Number(m[2]), message: m[4], excerpt: '', caret: '', fixit: null };
const body: string[] = [];
for (let j = i + 1; j < lines.length && !HEAD.test(lines[j]) && !/^\S.*: (error|warning): /.test(lines[j]); j++) body.push(lines[j]);
d.excerpt = body[0] ?? '';
d.caret = body[1] ?? '';
if (body[2] !== undefined && body[2].trim() && /^[ ~^]*$/.test(d.caret)) d.fixit = body[2];
out.push(d);
}
return out;
}
/** The fix a caret line and a fix-it line describe: a replacement under a range, else an insertion. */
function fixOf(d: RawDiag): QuickFix | null {
if (d.fixit == null) return null;
const start = d.caret.search(/[\^~]/);
if (start < 0) return null;
const end = d.caret.replace(/\s+$/, '').length; // one past the last ^ or ~
const at = d.fixit.search(/\S/);
const text = d.fixit.trim();
if (at === start && end - start > 1) return { label: `Replace with ${text}`, line: d.line, col: start + 1, endCol: end + 1, text };
return { label: `Insert ${text}`, line: d.line, col: at + 1, endCol: at + 1, text };
}
/**
* The compiler's messages as diagnostics on the Mojo pane. Notes stay
* notes; notes that carry a fix-it become the choices of a question on the
* error they follow ("consider transferring the value with '^'" or "you can
* copy it explicitly with '.copy()'"); an error's own fix-it is its fix.
*/
export function compilerDiagnostics(stderr: string): Diagnostic[] {
const out: Diagnostic[] = [];
let parent: Diagnostic | null = null;
for (const d of readRaw(stderr)) {
const sev = d.severity === 'remark' ? 'note' : d.severity as 'error' | 'warning' | 'note';
const diag: Diagnostic = { lang: 'mojo', severity: sev, id: 'compiler', line: d.line, col: d.col, message: d.message, origin: ORIGIN };
const fix = fixOf(d);
if (sev === 'note' && parent) {
if (fix) {
parent.question ??= { text: 'The compiler suggests:', options: [] };
parent.question.options.push({ ...fix, label: d.message.replace(/^consider /, '').replace(/^you can /, '') });
}
out.push(diag);
continue;
}
if (fix) diag.fix = fix;
out.push(diag);
if (sev !== 'note') parent = diag;
}
return out;
}
/* ---------------------------------------------------------------------------
* The listing, as functions
* ------------------------------------------------------------------------- */
export type FunctionKind = 'user' | 'entry' | 'library' | 'data';
export interface ListingFunction { label: string; name: string; kind: FunctionKind; lines: AsmLine[] }
const TYPE_NAMES: Record<string, string> = { int: 'Int', uint: 'UInt', bool: 'Bool', float64: 'Float64', float32: 'Float32', float16: 'Float16', int8: 'Int8', int16: 'Int16', int32: 'Int32', int64: 'Int64', uint8: 'UInt8', uint16: 'UInt16', uint32: 'UInt32', uint64: 'UInt64' };
/** `"example::add(::SIMD[DType.int, 1],::SIMD[DType.int, 1])"` reads as `add(Int, Int)`. */
export function demangle(label: string): string {
let s = label.replace(/^"|"$/g, '').replace(/^example::/, '');
s = s.replace(/::SIMD\[DType\.(\w+), 1\]/g, (_, t: string) => TYPE_NAMES[t] ?? `Scalar[${t}]`);
s = s.replace(/(^|[(,\s\[])::/g, '$1');
return s.replace(/,(?=\S)/g, ', ');
}
function kindOf(label: string): FunctionKind {
if (label.startsWith('"example::') || label.startsWith('example::')) return 'user';
if (label === 'main') return 'entry';
if (/^(static_string_\w+|\.L\.str[\w.]*)$/.test(label)) return 'data'; // a read-only string, not code
return 'library';
}
const LABEL = /^(?:"((?:[^"\\]|\\.)*)"|([A-Za-z_.$][\w.$@]*)):\s*$/;
/** Split the listing at each function label; local labels (.LBB2_2) stay inside their function. */
export function listingFunctions(asm: AsmLine[]): ListingFunction[] {
const out: ListingFunction[] = [];
let cur: ListingFunction | null = null;
for (const a of asm) {
const m = LABEL.exec(a.text.trim());
if (m && !(m[2] ?? '').startsWith('.L')) {
const label = m[1] !== undefined ? `"${m[1]}"` : m[2];
cur = { label, name: m[1] !== undefined ? demangle(label) : label, kind: kindOf(m[1] !== undefined ? m[1] : label), lines: [] };
out.push(cur);
continue;
}
if (!cur) continue;
if (!a.text.trim() || /^\s*\.(type|size|section|globl|p2align|cfi_\w+|file|loc|text|data)\b/.test(a.text)) continue;
cur.lines.push(a);
}
return out.filter((f) => f.lines.length);
}
/* ---------------------------------------------------------------------------
* Asking
* ------------------------------------------------------------------------- */
export interface CompileOutcome { answer: Answer | null; error: string | null; seconds: number }
/** Ask godbolt. A network or HTTP failure is an outcome to show, not an exception. */
export async function compileMojo(source: string, opts: { stdin?: string; level?: BuildLevel; signal?: AbortSignal; timeoutMs?: number } = {}): Promise<CompileOutcome> {
const t0 = Date.now();
const ctl = new AbortController();
const timer = setTimeout(() => ctl.abort(), opts.timeoutMs ?? 60000);
opts.signal?.addEventListener('abort', () => ctl.abort());
try {
const res = await fetch(mojoRequestUrl(), {
method: 'POST',
headers: { 'Content-Type': 'application/json', Accept: 'application/json' },
body: JSON.stringify(mojoRequestBody(source, opts.stdin ?? '', BUILD_ARGS[opts.level ?? 'O0'])),
signal: ctl.signal,
});
if (res.status === 429) return { answer: null, error: 'godbolt.org is rate-limiting requests from here. Wait a minute and compile again.', seconds: (Date.now() - t0) / 1000 };
if (!res.ok) return { answer: null, error: `godbolt.org answered ${res.status} ${res.statusText}.`, seconds: (Date.now() - t0) / 1000 };
return { answer: normaliseAnswer(await res.json()), error: null, seconds: (Date.now() - t0) / 1000 };
} catch (e: any) {
const msg = e?.name === 'AbortError' ? 'godbolt.org did not answer in time.' : `Could not reach godbolt.org (${e?.message || e}).`;
return { answer: null, error: msg, seconds: (Date.now() - t0) / 1000 };
} finally {
clearTimeout(timer);
}
}