Files
ww/selfhost/cmd/wwdump/main.ww
Hojun-Cho 46edb8db4a w6c+selfhost+lib: cgen quality batch + lib Hare-shape graduation
Six fixes across the toolchain, surfaced by lib/lisp porting work.

  1. f64 compound assigns (`acc += d`, `-=`, `*=`, `/=`). Both stages
     load slot → X1, OP X0 into X1, store back (ADDSD/SUBSD/MULSD/
     DIVSD are reg-reg only). Previous MOVSD-overwrite dropped the
     OP. Locals and top-level lets.

  2. Top-level `[N]u8` arrays + `&arr[i]`. let_emit_size grows a
     TY_ARRAY branch so zero-init DATAW lands; cgindex / N_INDEX
     store / `&base[i]` all detect a global array base and use
     LEAQ name(SB) instead of LEAQ (BP). TK_AMP no longer pre-
     evaluates the operand as a value-load — `&base[i]` computes
     base + i*esz directly. Unblocks Hare's static-buffer pattern:
     strconv.{u64,i64,f64}tos graduate to module-level `*_buf`
     arrays and return owned views.

  3. Cross-module `pkg.Enum.MEMBER`. Nested N_DOT chains that
     don't fold to a known shape now emit `MOVQ <leaf>(SB), AX`
     (mirrors the bare-IDENT unresolved fallback), so isolation
     probes — and the test 990 cgen-match floor — stay consistent
     across stages. strconv exposes `base` as a real `enum i32`;
     callers updated. The `main` exemption (linker entry-point
     keeps bare name even when not exported) mirrors C-side
     collectmods into selfhost cgendecl.

  4. Sum-typed parameter ABI. lib/bytes.{index,rindex} take
     `(u8 | []u8)` needle; lib/strings.byteindex / rbyteindex take
     `(str | rune)` needle (Hare-shaped; the byte-wise misnomer
     `index` is dropped). tagged_arg_size cap bumps to 48 (6 int
     regs), with a new partial-fit branch on the callee: when an
     N-word tagged arg overflows remaining regs, fill what fits and
     stitch the rest from positive BP offsets. scanlocals MCASE
     handles slice binds (24B) and walks each arm with a saved /
     restored seenmark set so two arms naming the same local each
     get their own slot — matches cstage's per-arm scope reset.

  5. 4-reg tagged-return ABI (AX=tag, DX=word0, CX=word1, R8=word2),
     up from 3 regs. Slice-payload variants (`([]T | E)`, slot 32B)
     round-trip ptr/len/cap end-to-end. Every receive site updates:
     let-init via cgwidentaggedstore, match scrutinee spill, cgindex
     tagged-element load (both N_IDENT and fallback bases),
     pushargsrev tagged-ident arg (reads word count from slot size),
     cgreturn slice variant in the shuffle path.

  6. `expr: TaggedAlias` is a widening, not a re-interpret. C cgen +
     selfhost cgwidentaggedstore peel an N_CAST whose destination IS
     the union — so cgexpr's natural shape (str: AX=ptr, BX=len;
     slice: AX=ptr, BX=len, CX=cap) is consumed by the matching
     concrete-variant branch instead of being misread as a tagged
     AX/DX/CX triple. Inner casts to a concrete variant (`7: i32`)
     keep their type for proper tag lookup. `[N]Alias` arrays
     resolve element size via slotsize + aliaslookup, and aliaslookup
     strips a `pkg.` prefix so cross-module references work.

lib/fmt grows `formattable = (i64 | str | bool | rune)` plus
`printv` / `printlnv` taking an explicit `[]formattable` slice (the
receive side of Hare's `args: formattable...`). Call-site variadic
gather isn't wired — callers either hand-build the slice or compose
strconv.i64tos + strings.concat.

700_e2e: 114 → 123 rows (f64 compound, top-level u8 arrays + `&buf[i]`,
pkg.Enum.MEMBER, sum-typed (str|rune) and (u8|[]u8) params, 4-reg
slice-return ABI, formattable array). 26/26 tests, bootstrap stable
through ww4.
2026-05-13 08:05:01 +09:00

168 lines
4.2 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)
use os;
use mem;
use tok;
use lex;
use ast;
use parse;
use typ;
use sym;
use check;
use cgen;
use 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(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);
let cg: cgen;
cgeninit(&cg, a);
cgfile(&cg, f);
};};};};
if (l.errs > 0) { return 1; };
return 0;
};