toolchain: banner purge + WHY-only comment sweep (rule 8)

selfhost/, cmd/, internal/ join the tree-wide sweep: every section
banner dies (91 selfhost + the cmd C-style dividers -> 0); narration
and stale contracts deleted (pre-#22 bundler notes, retired
single-PT_LOAD and no-archive claims, superseded ABI tables); every
ref/harec/qbe cite, task cite, encoding/ELF contract, and rule-10
twin pointer kept; lost lifetime/rationale lines restored where the
sweep over-cut (elf_globals ownership, kwtab linear-scan). Comment-
only proven: all five wwstage tool binaries byte-identical across
the sweep; test-commit, test-byteid (161+1399, 0 pinned-divergent),
and test-bootstrap (fixed point + 991-995 byte-id) all exit 0.
The read-through banked 66 latent-bug leads (checkpoint).
This commit is contained in:
2026-08-08 23:14:03 +09:00
parent 83f5956df2
commit 62b9d20383
60 changed files with 232 additions and 1045 deletions

View File

@@ -1,13 +1,5 @@
// selfhost/cmd/w6a/asm.ww — port of cmd/w6a/asm.c.
//
// Encode the parsed aprog list into amd64 machine bytes, appending to
// asm_.text. Relocations for CALL/branch targets that resolve to
// externals are queued in asm_.relocs.
//
// Encoding subset matches what w6c emits — see cmd/w6a/asm.c for the
// authoritative list. Helpers (rcode/rhi/modrm/emitrex etc.) are
// fully ported; encode itself is still a stub pending the full
// switch over A_*.
// Port of cmd/w6a/asm.c. Encoding subset matches what w6c emits — see
// cmd/w6a/asm.c for the authoritative list.
package w6a;
@@ -16,8 +8,6 @@ import rt;
import opcodes;
import strings;
// ---- text buffer growth ------------------------------------------------
export fn emitbyte(a: *asm_, b: u8) void = {
if (a.textlen + 1u64 > a.textcap) {
let nc: u64 = a.textcap;
@@ -45,16 +35,14 @@ export fn addreloc(a: *asm_, off: u64, kind: i32, s: *asym, add: i64) void = {
a.relocs = r;
};
// Record a relocation that lives in the .data section. Used by
// DATAR to patch a 64-bit slot with a symbol's runtime VA. obj.ww
// separates these into .rela.data when emitting the .o.
// DATAR patches a 64-bit .data slot with a symbol's runtime VA;
// obj.ww separates section=1 relocs into .rela.data when emitting
// the .o.
export fn addrelocdata(a: *asm_, off: u64, kind: i32, s: *asym, add: i64) void = {
let r: *areloc = alloc(areloc { off = off, section = 1, kind = kind, asy = s, addend = add, rnext = a.relocs })!;
a.relocs = r;
};
// Append one byte to the writable .data buffer. Mirrors emitbyte
// but targets a.data instead of a.text.
export fn emitdatabyte(a: *asm_, b: u8) void = {
if (a.datalen + 1u64 > a.datacap) {
let nc: u64 = a.datacap;
@@ -70,8 +58,6 @@ export fn emitdatabyte(a: *asm_, b: u8) void = {
a.datalen += 1u64;
};
// ---- register codes ----------------------------------------------------
// Low 3 bits of register encoding.
fn rcode(r: i32) i32 = {
if (r == D_AX) { return 0; }; if (r == D_CX) { return 1; };
@@ -105,7 +91,6 @@ fn isxmm(r: i32) bool = {
return false;
};
// ModR/M byte builder.
fn modrmbyte(mod: i32, reg: i32, rm: i32) u8 = {
return (((mod & 3) << 6) | ((reg & 7) << 3) | (rm & 7)): u8;
};
@@ -210,8 +195,6 @@ fn sserrw(a: *asm_, prefix: u8, op2: u8, regop: i32, rmop: i32) void = {
emitbyte(a, modrmbyte(3, rcode(regop), rcode(rmop)));
};
// ---- label resolution / fixups ----------------------------------------
fn resolvelabel(a: *asm_, name: str) u64 = {
let s: *asym = a.syms;
for (s != nil) {
@@ -230,8 +213,6 @@ fn labeldefined(a: *asm_, name: str) bool = {
return false;
};
// ---- fixup helper -----------------------------------------------------
fn addfixup(a: *asm_, off: u64, label: str) void = {
let f: *afixup = alloc(afixup { off = off, label = label, fnext = a.fixups })!;
a.fixups = f;
@@ -242,15 +223,9 @@ fn isgpr(t: i32) bool = {
return false;
};
// `intern` lives in parse.ww — flat-scope concat lets us call it
// directly without an @symbol declaration here.
// ---- encode ----------------------------------------------------------
export fn encode(a: *asm_) i32 = {
let p: *aprog = a.head;
for (p != nil) {
// Define any pending label at the current PC.
if (p.label.len > 0) {
let s: *asym = intern(a, p.label);
s.defined = 1;
@@ -281,10 +256,9 @@ export fn encode(a: *asm_) i32 = {
p = p.link; continue;
};
if (op == A_DATAW) {
// Writable variant: bytes go into .data instead of
// .text. obj.ww emits the extra section conditionally
// on datalen > 0 so .o output stays byte-identical
// for inputs that don't use DATAW.
// obj.ww emits the .data section conditionally on
// datalen > 0 so .o output stays byte-identical for
// inputs that don't use DATAW.
let s: *asym = intern(a, p.to.asym);
s.defined = 1;
s.isdata = 1;

View File

@@ -1,8 +1,4 @@
// selfhost/cmd/w6a/lex.ww — port of cmd/w6a/lex.c.
//
// Character-level helpers for w6a's line-oriented parser. The parser
// itself is in parse.ww; here we keep tokenisers for identifiers and
// numbers so parse.ww stays focused on syntax.
// Port of cmd/w6a/lex.c.
package w6a;
@@ -20,10 +16,6 @@ export fn isidcont(c: i32) bool = {
return false;
};
// parsenum — read a leading [+-]?[0x|0X|0]?digits from p[0..n-1].
// Returns (value, consumed). Stops at first non-digit.
// Plain Plan 9-style: $123 / $0x1f / $-7. Decimal default; 0x prefix
// for hex; 0 prefix for octal when followed by a digit (else just 0).
export fn parsenum(p: *u8, n: u64) (i64, u64) = {
// strtoll(s, end, 0) semantics, matching the C twin cmd/w6a/lex.c:30:
// skip leading whitespace, optional sign, base-0 prefix detection

View File

@@ -1,6 +1,4 @@
// selfhost/cmd/w6a/main.ww — port of cmd/w6a/main.c.
//
// w6a = amd64 assembler. Read .s, parse, encode, emit ELF .o.
// Port of cmd/w6a/main.c.
//
// w6a_ww -o file.o file.s
@@ -33,8 +31,7 @@ fn cstrlen(p: *u8) u64 = {
return n;
};
// pathstr — view a NUL-terminated *u8 as a str. Bridges argv-style
// callers to lib/os entrypoints (str post-task-#23).
// Bridges argv-style callers to lib/os entrypoints (str post-task-#23).
fn pathstr(p: *u8) str = {
let r: str;
r.ptr = p;
@@ -42,7 +39,6 @@ fn pathstr(p: *u8) str = {
return r;
};
// Slurp the whole file into a fresh buffer.
fn slurp(path: *u8) (*u8, u64) = {
let fd: i32 = os.open(pathstr(path), os.flag.RDONLY, 0i32);
if (fd < 0) { return nil, 0u64; };
@@ -122,7 +118,6 @@ export fn main(argc: i32, argv: **u8) i32 = {
if (parse(&s) != 0) { return 1; };
if (encode(&s) != 0) { return 1; };
// Open output for write.
let fd: i32 = os.open(pathstr(out), os.flag.WRONLY | os.flag.CREATE | os.flag.TRUNC, 420i32); // 0o644
if (fd < 0) {
os.write(2, "w6a: cannot open output\n".ptr, 23u64);

View File

@@ -1,4 +1,4 @@
// selfhost/cmd/w6a/obj.ww — port of cmd/w6a/obj.c.
// Port of cmd/w6a/obj.c.
//
// Emit a tiny ELF64 relocatable object. Layout (in file order):
// [0] ELF header
@@ -36,7 +36,6 @@ fn wrdrop(fd: i32, p: *u8, n: u64) void = {
};
};
// ---- ELF constants ----------------------------------------------------
def ELFCLASS64: u8 = 2u8;
def ELFDATA2LSB: u8 = 1u8;
def EV_CURRENT_W: u32 = 1u32;
@@ -59,14 +58,11 @@ def STT_NOTYPE: u8 = 0u8;
def STT_OBJECT: u8 = 1u8;
def STT_FUNC: u8 = 2u8;
// Sizes of fixed structures.
def EHDR_SZ: u64 = 64u64;
def SHDR_SZ: u64 = 64u64;
def SYM_SZ: u64 = 24u64;
def RELA_SZ: u64 = 24u64;
// ---- LE byte writers (own the bytes — write into a *u8 + offset) ----
fn wru8(p: *u8, off: u64, v: u8) void = { p[off] = v; };
fn wru16(p: *u8, off: u64, v: u16) void = {
p[off] = (v & 255u16): u8;
@@ -83,8 +79,6 @@ fn wru64(p: *u8, off: u64, v: u64) void = {
wru32(p, off + 4u64, ((v >> 32u64) & 4294967295u64): u32);
};
// ---- growable byte buffer ---------------------------------------------
type buf = struct {
p: *u8,
n: u64,
@@ -128,8 +122,6 @@ fn bufputcstr(b: *buf, s: str) u32 = {
return off;
};
// ---- emitelf ---------------------------------------------------------
export fn emitelf(a: *asm_, fd: i32) i32 = {
let shstr: buf; bufinit(&shstr);
let str_: buf; bufinit(&str_);
@@ -174,9 +166,9 @@ export fn emitelf(a: *asm_, fd: i32) i32 = {
let SH_STRTAB: u16 = SH_SYMTAB + 1u16;
let SH_SHSTR: u16 = SH_STRTAB + 1u16;
// Section name offsets. Append .data / .rela.data only when
// used so the .shstrtab buffer stays byte-identical for the
// no-DATAW case (test 991 byte-diff invariant).
// Append .data / .rela.data names only when used so the
// .shstrtab buffer stays byte-identical for the no-DATAW case
// (test 991 byte-diff invariant).
let shntext: u32 = bufputcstr(&shstr, ".text");
let shnrela: u32 = bufputcstr(&shstr, ".rela.text");
let shndata: u32 = 0u32;
@@ -195,7 +187,6 @@ export fn emitelf(a: *asm_, fd: i32) i32 = {
for (zi < 24) { zsym[zi] = 0u8; zi += 1; };
bufputb(&sym, zsym.ptr, 24u64);
// Build symbols.
let idx: i32 = 1;
let s: *asym = a.syms;
for (s != nil) {
@@ -223,7 +214,6 @@ export fn emitelf(a: *asm_, fd: i32) i32 = {
s = s.snext;
};
// Build relocations — split into text vs data buffers.
let r: *areloc = a.relocs;
for (r != nil) {
let entry: [24]u8;
@@ -239,7 +229,6 @@ export fn emitelf(a: *asm_, fd: i32) i32 = {
r = r.rnext;
};
// File offsets.
let off: u64 = EHDR_SZ;
let offtext: u64 = off; off = off + a.textlen;
let offrela: u64 = off; off = off + rela.n;
@@ -258,7 +247,6 @@ export fn emitelf(a: *asm_, fd: i32) i32 = {
else { NSECT = 7u16; };
};
// ---- Ehdr ----
let eh: [64]u8;
let i: i32 = 0;
for (i < 64) { eh[i] = 0u8; i += 1; };
@@ -321,13 +309,11 @@ export fn emitelf(a: *asm_, fd: i32) i32 = {
written += 1u64;
};
// Section header table — 6 headers of 64 bytes each = 384 bytes.
let shbuf: [64]u8;
// SHT_NULL
let sn: i32 = 0;
for (sn < 64) { shbuf[sn] = 0u8; sn += 1; };
wrdrop(fd, shbuf.ptr, 64u64);
// .text
sn = 0;
for (sn < 64) { shbuf[sn] = 0u8; sn += 1; };
wru32(shbuf.ptr, 0u64, shntext);
@@ -337,7 +323,6 @@ export fn emitelf(a: *asm_, fd: i32) i32 = {
wru64(shbuf.ptr, 32u64, a.textlen);
wru64(shbuf.ptr, 48u64, 1u64); // sh_addralign
wrdrop(fd, shbuf.ptr, 64u64);
// .rela.text
sn = 0;
for (sn < 64) { shbuf[sn] = 0u8; sn += 1; };
wru32(shbuf.ptr, 0u64, shnrela);
@@ -351,7 +336,6 @@ export fn emitelf(a: *asm_, fd: i32) i32 = {
wru64(shbuf.ptr, 56u64, RELA_SZ);
wrdrop(fd, shbuf.ptr, 64u64);
if (hasdata) {
// .data
sn = 0;
for (sn < 64) { shbuf[sn] = 0u8; sn += 1; };
wru32(shbuf.ptr, 0u64, shndata);
@@ -362,7 +346,6 @@ export fn emitelf(a: *asm_, fd: i32) i32 = {
wru64(shbuf.ptr, 48u64, 8u64); // sh_addralign
wrdrop(fd, shbuf.ptr, 64u64);
if (hasdatarelocs) {
// .rela.data
sn = 0;
for (sn < 64) { shbuf[sn] = 0u8; sn += 1; };
wru32(shbuf.ptr, 0u64, shnrelad);
@@ -377,7 +360,6 @@ export fn emitelf(a: *asm_, fd: i32) i32 = {
wrdrop(fd, shbuf.ptr, 64u64);
};
};
// .symtab
sn = 0;
for (sn < 64) { shbuf[sn] = 0u8; sn += 1; };
wru32(shbuf.ptr, 0u64, shnsymtab);
@@ -389,7 +371,6 @@ export fn emitelf(a: *asm_, fd: i32) i32 = {
wru64(shbuf.ptr, 48u64, 8u64);
wru64(shbuf.ptr, 56u64, SYM_SZ);
wrdrop(fd, shbuf.ptr, 64u64);
// .strtab
sn = 0;
for (sn < 64) { shbuf[sn] = 0u8; sn += 1; };
wru32(shbuf.ptr, 0u64, shnstrtab);
@@ -398,7 +379,6 @@ export fn emitelf(a: *asm_, fd: i32) i32 = {
wru64(shbuf.ptr, 32u64, str_.n);
wru64(shbuf.ptr, 48u64, 1u64);
wrdrop(fd, shbuf.ptr, 64u64);
// .shstrtab
sn = 0;
for (sn < 64) { shbuf[sn] = 0u8; sn += 1; };
wru32(shbuf.ptr, 0u64, shnshstrtab);

View File

@@ -1,12 +1,10 @@
// selfhost/cmd/w6a/opcodes.ww — types + constants shared across the
// w6a port. Mirrors cmd/w6a/a.h and cmd/w6c/6.out.h.
// Mirrors cmd/w6a/a.h and cmd/w6c/6.out.h.
package w6a;
// ---- registers + operand kinds (from 6.out.h) -------------------------
// These must stay numerically aligned with the C enum so that ww-cgen
// output (which reads them via `D_AX(SB)` etc.) lands on the same
// integers when read by ww-w6a.
// Registers + operand kinds must stay numerically aligned with the
// 6.out.h C enum so that ww-cgen output (which reads them via
// `D_AX(SB)` etc.) lands on the same integers when read by ww-w6a.
def D_NONE: i32 = 0;
def D_AX: i32 = 1;
@@ -52,7 +50,6 @@ def D_BRANCH: i32 = 37;
def D_EXTERN: i32 = 38;
def D_INDIR: i32 = 39;
// ---- opcodes ----------------------------------------------------------
def A_NOP: i32 = 0;
def A_TEXT: i32 = 1;
def A_DATA: i32 = 2;
@@ -145,8 +142,6 @@ def A_DATAR: i32 = 61;
// with DIVQ.
def A_CQO: i32 = 66;
// ---- structs (mirror cmd/w6a/a.h) --------------------------------------
type aoperand = struct {
atype: i32, // D_NONE / D_AX..D_R15 / D_CONST / D_INDIR / D_EXTERN / D_BRANCH
reg: i32,

View File

@@ -1,4 +1,4 @@
// selfhost/cmd/w6a/parse.ww — port of cmd/w6a/parse.c.
// Port of cmd/w6a/parse.c.
//
// Line-oriented parser for the asm subset emitted by w6c.
// Grammar:
@@ -28,8 +28,7 @@ fn streqlit(p: *u8, n: u64, lit: str) bool = {
return true;
};
// opcodelookup — name (length-bounded *u8) → A_*. Returns 0 (A_NOP)
// if not found.
// Returns 0 (A_NOP) if not found.
fn opcodelookup(p: *u8, n: u64) i32 = {
if (streqlit(p, n, "MOVQ")) { return A_MOVQ; };
if (streqlit(p, n, "MOVL")) { return A_MOVL; };
@@ -100,7 +99,7 @@ fn opcodelookup(p: *u8, n: u64) i32 = {
return A_NOP;
};
// reglookup — name → D_*. Returns D_NONE if not found.
// Returns D_NONE if not found.
fn reglookup(p: *u8, n: u64) i32 = {
if (streqlit(p, n, "AX")) { return D_AX; };
if (streqlit(p, n, "BX")) { return D_BX; };
@@ -180,7 +179,6 @@ fn perr(a: *asm_, msg: str) void = {
a.errs += 1;
};
// dupstr — copy n bytes from p into a fresh heap str.
fn dupstr(p: *u8, n: u64) str = {
let view: str;
view.ptr = p;
@@ -188,8 +186,6 @@ fn dupstr(p: *u8, n: u64) str = {
return strings.dup(view);
};
// ---- line iteration & whitespace --------------------------------------
// Read next line into a fresh heap buffer; returns (ptr, len) or (nil,0)
// at EOF. Advances a.pos past the newline.
fn nextline(a: *asm_) (*u8, u64) = {
@@ -204,7 +200,7 @@ fn nextline(a: *asm_) (*u8, u64) = {
let i: u64 = 0u64;
for (i < n) { buf[i] = a.src[start + i]; i += 1u64; };
buf[n] = 0u8;
a.pos += 1u64; // skip newline
a.pos += 1u64;
return buf.ptr, n;
};
// EOF without trailing newline
@@ -226,7 +222,6 @@ fn skipws(p: *u8, off: u64, n: u64) u64 = {
return i;
};
// parseoperand — parse one operand from p[off..n), populate out.
// Returns new offset (clamped to n on error).
fn parseoperand(a: *asm_, p: *u8, offin: u64, n: u64, out: *aoperand) u64 = {
let off: u64 = skipws(p, offin, n);
@@ -239,7 +234,6 @@ fn parseoperand(a: *asm_, p: *u8, offin: u64, n: u64, out: *aoperand) u64 = {
if (off >= n) { return off; };
let c0: u8 = p[off];
// $NUM
if (c0 == '$') {
off += 1u64;
let v: i64;
@@ -250,7 +244,6 @@ fn parseoperand(a: *asm_, p: *u8, offin: u64, n: u64, out: *aoperand) u64 = {
return off + used;
};
// (REG)
if (c0 == '(') {
off += 1u64;
let rstart: u64 = off;
@@ -377,7 +370,6 @@ fn parseoperand(a: *asm_, p: *u8, offin: u64, n: u64, out: *aoperand) u64 = {
return n;
};
// Append a fresh aprog to the list with given opcode and label.
fn addprog(a: *asm_, opc: i32, lbl: str) *aprog = {
let pr: *aprog = alloc(aprog { as_ = opc, line = a.line, label = lbl })!;
pr.from = alloc(aoperand { })!;
@@ -398,9 +390,7 @@ export fn parse(a: *asm_) i32 = {
line, n = nextline(a);
if (line == nil) { return a.errs; };
// skip leading ws
let i: u64 = skipws(line, 0u64, n);
// blank or //-comment
if (i >= n) { a.line += 1; continue; };
if (i + 1u64 < n) {
if (line[i] == '/') { if (line[i + 1u64] == '/') {
@@ -435,7 +425,6 @@ export fn parse(a: *asm_) i32 = {
};
};
// MNEMONIC at the start of the rest. Scan to first ws/EOL.
let mstart: u64 = i;
let m: u64 = mstart;
let scan: bool = true;
@@ -458,11 +447,9 @@ export fn parse(a: *asm_) i32 = {
let pr: *aprog = addprog(a, opc, pending);
pending.ptr = nil; pending.len = 0;
// Skip ws after mnemonic
let r0: u64 = skipws(line, m, n);
if (opc == A_TEXT) {
// TEXT name,$framesize — find first ',' as the end of name.
let q: u64 = r0;
let commapos: u64 = n;
let scant: bool = true;
@@ -502,7 +489,6 @@ export fn parse(a: *asm_) i32 = {
let toop: *aoperand = pr.to;
toop.atype = D_EXTERN;
toop.asym = dupstr(line + r0, lparen - r0);
// Skip past `(SB)` to land just after ')'.
let p2: u64 = lparen;
let scand2: bool = true;
for (scand2) {
@@ -510,7 +496,6 @@ export fn parse(a: *asm_) i32 = {
else { if (line[p2] == ')') { p2 += 1u64; scand2 = false; }
else { p2 += 1u64; }; };
};
// Skip ws / ',' / tab between `)` and the `"`.
let scand3: bool = true;
for (scand3) {
if (p2 >= n) { scand3 = false; }
@@ -521,8 +506,7 @@ export fn parse(a: *asm_) i32 = {
};
if (p2 >= n) { perr(a, "DATA missing payload"); a.line += 1; continue; };
if (line[p2] != '"') { perr(a, "DATA expects \"...\""); a.line += 1; continue; };
p2 += 1u64; // past opening "
// Parse escape sequence into a fresh growable buffer.
p2 += 1u64;
let cap: u64 = 32u64;
let blen: u64 = 0u64;
let dbuf: []u8 = alloc([], cap)!;
@@ -575,8 +559,6 @@ export fn parse(a: *asm_) i32 = {
a.line += 1; continue;
};
// Generic instruction: 0/1/2 operands separated by ','.
// Find top-level comma.
let comma: i64 = -1i64;
let q: u64 = r0;
for (q < n) {