Files
ww/selfhost/cmd/wwdump/main.ww
Hojun-Cho c58d3511e8 selfhost/cmd+test: run check before cgen in wwstage drivers
w6c_ww and wwdump_ww (-c mode) silently passed source into cgen
without type-checking it; any type error flowed through as broken
asm with exit 0. Mirror cstage cmd/w6c/main.c:73-75: between the
parse-error gate and cgeninit, install typesinit + checkinit +
checkfile + `if (ck.errs > 0) return 1;`. Cgen path unchanged —
drivers own check, cgen owns emit (checkfile not idempotent due to
installdecl scopedefine).

The wire-up was blocked by five latent check.ww divergences from
cstage, all landed first: cross-module type refs (#51), enum-int
reinterprets (#52), per-block scoping (#53), nominal-first
tagged-variant inclusion (#55), and same-module N_IDENT callee
preference (#56). With those clear, the broader exercise of
check across every selfhost driver reaches 131/131 first try.

994_w6c_ww grows by one row: a trivially-wrong `let x: i32 =
"hello";` smoke pins both stages to exit-non-zero. Pre-#50 it
emitted 202 bytes of broken asm with exit=0.

Unblocks #42 (size/align/offset wwstage intercepts) and the
audit-§1.8 UP-polarity refactor (node.type_ population can now
land in the same check pass we just wired up).
2026-05-20 04:52:16 +09:00

181 lines
4.7 KiB
Plaintext

// selfhost/cmd/wwdump/main.ww — ww-side port of cmd/wwdump/main.c.
//
// Reads a .ww file, runs the ww-side lexer, prints tokens through
// the ww-side tokprint. The 990_selfhost test diffs this output
// byte-for-byte against the C-side wwdump on the same file. Any
// divergence is a port bug in lex.ww or tok.ww.
//
// Modes:
// wwdump -t file.ww tokens (default)
// wwdump -a file.ww AST (not yet implemented; reserved)
package main;
import os;
import mem;
import tok;
import lex;
import ast;
import parse;
import typ;
import sym;
import check;
import cgen;
import strconv;
// ---- argv helpers -----------------------------------------------------
// argstrlen — strlen on a NUL-terminated *u8. argv strings are always
// NUL-terminated (kernel-supplied) so this is safe.
fn argstrlen(s: *u8) i32 = {
let n: i32 = 0;
for (s[n] != 0u8) { n += 1; };
return n;
};
fn argstr(p: *u8) str = {
let s: str;
s.ptr = p;
s.len = argstrlen(p);
return s;
};
// streqlit — compare a NUL-terminated argv entry to a string literal.
fn streqlit(p: *u8, lit: str) bool = {
let i: i32 = 0;
for (i < lit.len) {
if (p[i] != lit[i]) { return false; };
i += 1;
};
return p[i] == 0u8;
};
// ---- main -------------------------------------------------------------
export fn main(argc: i32, argv: **u8) i32 = {
let mode: i32 = 116; // 't'
let path: *u8 = nil;
let i: i32 = 1;
for (i < argc) {
let a: *u8 = argv[i];
if (streqlit(a, "-t")) {
mode = 116;
} else { if (streqlit(a, "-a")) {
mode = 97; // 'a'
} else { if (streqlit(a, "-r")) {
mode = 114; // 'r' — resolve / name-check
} else { if (streqlit(a, "-c")) {
mode = 99; // 'c' — codegen / emit asm
} else { if (path == nil) {
path = a;
};};};};};
i += 1;
};
if (path == nil) {
os.write(2, "usage: wwdump [-t|-a] file.ww\n".ptr, 30u64);
return 2;
};
let fdorerr: (i32 | os.oserror) = os.tryopen(argstr(path), os.flag.RDONLY, 0i32);
let fd: i32 = -1;
match (fdorerr) {
case let v: i32 => fd = v;
case let e: os.oserror => {
os.write(2, "wwdump: cannot open ".ptr, 20u64);
os.write(2, path, argstrlen(path): u64);
os.write(2, "\n".ptr, 1u64);
return 1;
};
};
let szr: (i64 | os.oserror) = os.filesize(fd);
let sz: i64 = 0i64;
match (szr) {
case let v: i64 => sz = v;
case let e: os.oserror => {
os.write(2, "wwdump: filesize failed\n".ptr, 24u64);
os.close(fd);
return 1;
};
};
let a: *arena = newarena();
let buf: *u8 = amalloc(a, sz: u64): *u8;
let rr: (i64 | os.oserror) = os.readall(fd, buf, sz: u64);
os.close(fd);
let r: i64 = 0i64;
match (rr) {
case let v: i64 => r = v;
case let e: os.oserror => {
os.write(2, "wwdump: read failed\n".ptr, 20u64);
return 1;
};
};
if (r != sz) {
os.write(2, "wwdump: short read\n".ptr, 19u64);
return 1;
};
let l: lex;
lexinit(&l, a, argstr(path), buf, sz: u64);
if (mode == 116) { // '-t'
for (true) {
let t: tok;
lexnext(&l, &t);
tokprint(1i32, &t);
if (t.kind == tkind.TK_EOF) { break; };
if (t.kind == tkind.TK_ERR) { break; };
};
} else { if (mode == 97) { // '-a'
let ps: parser;
parserinit(&ps, a, &l);
let f: *node = parsefile(&ps);
astprint(1i32, f);
} else { if (mode == 114) { // '-r' — name resolve report
let ps: parser;
parserinit(&ps, a, &l);
let f: *node = parsefile(&ps);
let tc: tctx;
typesinit(&tc, a);
let ck: checker;
checkinit(&ck, a, &tc);
// Quiet by default; flip to 1 when debugging missing names.
ck.verbose = 0;
checkfile(&ck, f);
// (close out the if-else chain — we'll close all braces below)
// "<file>: <resolved>/<resolved+unresolved> resolved"
os.write(1, argstr(path).ptr, argstrlen(path): u64);
os.write(1, ": ".ptr, 2u64);
let rs: str = strconv.i64tos(ck.nresolved: i64, strconv.base.DEC);
os.write(1, rs.ptr, rs.len: u64);
os.write(1, "/".ptr, 1u64);
let total: i32 = ck.nresolved + ck.nunresolved;
let ts: str = strconv.i64tos(total: i64, strconv.base.DEC);
os.write(1, ts.ptr, ts.len: u64);
os.write(1, " resolved\n".ptr, 10u64);
if (ck.nunresolved > 0) { return 1; };
} else { if (mode == 99) { // '-c' — codegen / emit asm
let ps: parser;
parserinit(&ps, a, &l);
let f: *node = parsefile(&ps);
// #50: mirror w6c — run check before cgen so AST mutations
// from #42 (size/align/offset fold) and audit §1.8 (node.type_
// population) land before cgen walks. Without this, wwdump -c
// (the byte-identity probe for 994) would diverge from w6c_ww
// on any program that uses the size/align/offset typed builtins.
let tc: tctx;
typesinit(&tc, a);
let ck: checker;
checkinit(&ck, a, &tc);
checkfile(&ck, f);
if (ck.errs > 0) { return 1; };
let cg: cgen;
cgeninit(&cg, a);
cgfile(&cg, f);
};};};};
if (l.errs > 0) { return 1; };
return 0;
};