Files
ww/selfhost/cmd/w6a/parse.ww
Hojun-Cho 2c33228b7e ww: rename toolchain to w-prefix + hare-style build/run/test driver
Plan 9-style w-prefix on the per-arch tools, disambiguating from the
real Plan 9 6c/6a/6l in ref/plan9front/:

    cmd/wwc/      → cmd/wcc/        libwwc.a → libwcc.a
    cmd/6{c,a,l}  → cmd/w6{c,a,l}   binary names too
    test/wwc/     → test/wcc/       6 test files w/ w6 prefix
    selfhost/cmd  mirror in lockstep
    bootstrap/amd64/{w6c,w6a,w6l}   snapshot binaries (gitignored)
    WW_6{C,A,L}   → WW_W6{C,A,L}    env-var overrides

Plan 9 source-tree refs ("Plan 9 6c shape", ref/plan9front/, etc.)
preserved. Hare-style driver, both C and ww sides:

    ww test [path]   discover *_test.ww in a directory module, run
                     each; single-file mode for `ww test foo.ww`
    Module-by-name   `ww build foo` resolves to foo.ww or foo/foo.ww
                     via search path (cwd : -I dirs : $WW_LIB)
    Default-to-cwd   `ww build` / `ww test` build the cwd module
    Run pass-through `ww run path arg1 arg2` reaches the program

lib/os: getcwd (79) and getdents64 (217) syscalls power `.` resolution
and directory enumeration on the ww side.

Makefile: wwstage tool deps now include lib/os/os.ww (+ lib/strconv
for wwdump_ww) so lib/* edits force their rebuild instead of leaving
stale binaries — surfaced when test 995 first failed against a stale
w6c_ww built before the lib/os additions.

Test 993 byte-identical parity gate (C-side ww vs ww-side ww_ww on a
build corpus) stays green; all 19 tests pass.
2026-05-11 13:49:27 +09:00

584 lines
18 KiB
Plaintext

// selfhost/cmd/w6a/parse.ww — port of cmd/w6a/parse.c.
//
// Line-oriented parser for the asm subset emitted by w6c.
// Grammar:
// line := blank | comment | label | text | instr
// blank := /^\s*$/
// comment := /^\s*\/\/.*$/
// label := /^IDENT:$/
// text := TEXT name,$framesize
// instr := \tMNEM\t[OP1[, OP2]]
// OP := $NUM | REG | NUM(REG) | (REG) | name(SB) | label
use os;
use mem;
use lex;
use types;
fn streq_lit(p: *u8, n: u64, lit: str) bool = {
if (n != lit.len: u64) { return false; };
let i: u64 = 0u64;
for (i < n) {
let li: i32 = i: i32;
if (p[i] != lit[li]) { return false; };
i += 1u64;
};
return true;
};
// opcode_lookup — name (length-bounded *u8) → A_*. Returns 0 (A_NOP)
// if not found.
fn opcode_lookup(p: *u8, n: u64) i32 = {
if (streq_lit(p, n, "MOVQ")) { return A_MOVQ; };
if (streq_lit(p, n, "MOVL")) { return A_MOVL; };
if (streq_lit(p, n, "MOVB")) { return A_MOVB; };
if (streq_lit(p, n, "MOVZBQ")) { return A_MOVZBQ; };
if (streq_lit(p, n, "MOVSXD")) { return A_MOVSXD; };
if (streq_lit(p, n, "MOVSD")) { return A_MOVSD; };
if (streq_lit(p, n, "ADDSD")) { return A_ADDSD; };
if (streq_lit(p, n, "SUBSD")) { return A_SUBSD; };
if (streq_lit(p, n, "MULSD")) { return A_MULSD; };
if (streq_lit(p, n, "DIVSD")) { return A_DIVSD; };
if (streq_lit(p, n, "UCOMISD")) { return A_UCOMISD; };
if (streq_lit(p, n, "CVTTSD2SI")) { return A_CVTTSD2SI; };
if (streq_lit(p, n, "CVTSI2SD")) { return A_CVTSI2SD; };
if (streq_lit(p, n, "MOVSS")) { return A_MOVSS; };
if (streq_lit(p, n, "ADDSS")) { return A_ADDSS; };
if (streq_lit(p, n, "SUBSS")) { return A_SUBSS; };
if (streq_lit(p, n, "MULSS")) { return A_MULSS; };
if (streq_lit(p, n, "DIVSS")) { return A_DIVSS; };
if (streq_lit(p, n, "UCOMISS")) { return A_UCOMISS; };
if (streq_lit(p, n, "CVTTSS2SI")) { return A_CVTTSS2SI; };
if (streq_lit(p, n, "CVTSI2SS")) { return A_CVTSI2SS; };
if (streq_lit(p, n, "CVTSD2SS")) { return A_CVTSD2SS; };
if (streq_lit(p, n, "CVTSS2SD")) { return A_CVTSS2SD; };
if (streq_lit(p, n, "ADDQ")) { return A_ADDQ; };
if (streq_lit(p, n, "SUBQ")) { return A_SUBQ; };
if (streq_lit(p, n, "IMULQ")) { return A_IMULQ; };
if (streq_lit(p, n, "IDIVQ")) { return A_IDIVQ; };
if (streq_lit(p, n, "DIVQ")) { return A_DIVQ; };
if (streq_lit(p, n, "NEGQ")) { return A_NEGQ; };
if (streq_lit(p, n, "NOTQ")) { return A_NOTQ; };
if (streq_lit(p, n, "ANDQ")) { return A_ANDQ; };
if (streq_lit(p, n, "ORQ")) { return A_ORQ; };
if (streq_lit(p, n, "XORQ")) { return A_XORQ; };
if (streq_lit(p, n, "SHLQ")) { return A_SHLQ; };
if (streq_lit(p, n, "SHRQ")) { return A_SHRQ; };
if (streq_lit(p, n, "CMPQ")) { return A_CMPQ; };
if (streq_lit(p, n, "PUSHQ")) { return A_PUSHQ; };
if (streq_lit(p, n, "POPQ")) { return A_POPQ; };
if (streq_lit(p, n, "LEAQ")) { return A_LEAQ; };
if (streq_lit(p, n, "CALL")) { return A_CALL; };
if (streq_lit(p, n, "RET")) { return A_RET; };
if (streq_lit(p, n, "JMP")) { return A_JMP; };
if (streq_lit(p, n, "JE")) { return A_JE; };
if (streq_lit(p, n, "JNE")) { return A_JNE; };
if (streq_lit(p, n, "JL")) { return A_JL; };
if (streq_lit(p, n, "JLE")) { return A_JLE; };
if (streq_lit(p, n, "JG")) { return A_JG; };
if (streq_lit(p, n, "JGE")) { return A_JGE; };
if (streq_lit(p, n, "JB")) { return A_JB; };
if (streq_lit(p, n, "JBE")) { return A_JBE; };
if (streq_lit(p, n, "JA")) { return A_JA; };
if (streq_lit(p, n, "JAE")) { return A_JAE; };
if (streq_lit(p, n, "JZ")) { return A_JZ; };
if (streq_lit(p, n, "JNZ")) { return A_JNZ; };
if (streq_lit(p, n, "SYSCALL")) { return A_SYSCALL; };
if (streq_lit(p, n, "TEXT")) { return A_TEXT; };
if (streq_lit(p, n, "DATA")) { return A_DATA; };
return A_NOP;
};
// reg_lookup — name → D_*. Returns D_NONE if not found.
fn reg_lookup(p: *u8, n: u64) i32 = {
if (streq_lit(p, n, "AX")) { return D_AX; };
if (streq_lit(p, n, "BX")) { return D_BX; };
if (streq_lit(p, n, "CX")) { return D_CX; };
if (streq_lit(p, n, "DX")) { return D_DX; };
if (streq_lit(p, n, "SP")) { return D_SP; };
if (streq_lit(p, n, "BP")) { return D_BP; };
if (streq_lit(p, n, "SI")) { return D_SI; };
if (streq_lit(p, n, "DI")) { return D_DI; };
if (streq_lit(p, n, "R8")) { return D_R8; };
if (streq_lit(p, n, "R9")) { return D_R9; };
if (streq_lit(p, n, "R10")) { return D_R10; };
if (streq_lit(p, n, "R11")) { return D_R11; };
if (streq_lit(p, n, "R12")) { return D_R12; };
if (streq_lit(p, n, "R13")) { return D_R13; };
if (streq_lit(p, n, "R14")) { return D_R14; };
if (streq_lit(p, n, "R15")) { return D_R15; };
if (streq_lit(p, n, "X0")) { return D_X0; };
if (streq_lit(p, n, "X1")) { return D_X1; };
if (streq_lit(p, n, "X2")) { return D_X2; };
if (streq_lit(p, n, "X3")) { return D_X3; };
if (streq_lit(p, n, "X4")) { return D_X4; };
if (streq_lit(p, n, "X5")) { return D_X5; };
if (streq_lit(p, n, "X6")) { return D_X6; };
if (streq_lit(p, n, "X7")) { return D_X7; };
if (streq_lit(p, n, "X8")) { return D_X8; };
if (streq_lit(p, n, "X9")) { return D_X9; };
if (streq_lit(p, n, "X10")) { return D_X10; };
if (streq_lit(p, n, "X11")) { return D_X11; };
if (streq_lit(p, n, "X12")) { return D_X12; };
if (streq_lit(p, n, "X13")) { return D_X13; };
if (streq_lit(p, n, "X14")) { return D_X14; };
if (streq_lit(p, n, "X15")) { return D_X15; };
if (streq_lit(p, n, "SB")) { return D_PSB; };
if (streq_lit(p, n, "FP")) { return D_PFP; };
return D_NONE;
};
export fn a_init(a: *asm_, ar: *arena, file: str, src: *u8, len: u64) void = {
a.a = ar;
a.file = file;
a.src = src;
a.srclen = len;
a.pos = 0u64;
a.line = 1;
a.head = nil;
a.tail = nil;
a.text = nil;
a.textcap = 0u64;
a.textlen = 0u64;
a.syms = nil;
a.relocs = nil;
a.fixups = nil;
a.errs = 0;
};
fn streq_str(a: str, b: str) bool = {
if (a.len != b.len) { return false; };
let i: i32 = 0;
for (i < a.len) {
if (a[i] != b[i]) { return false; };
i += 1;
};
return true;
};
export fn a_intern(a: *asm_, name: str) *asym = {
let s: *asym = a.syms;
for (s != nil) {
if (streq_str(s.name, name)) { return s; };
s = s.snext;
};
let n: *asym = amalloc(a.a, 64u64): *asym;
n.name = name;
n.snext = a.syms;
a.syms = n;
return n;
};
fn perr(a: *asm_, msg: str) void = {
os.write(2, "w6a: ".ptr, 4u64);
let f: str = a.file;
os.write(2, f.ptr, f.len: u64);
os.write(2, ": ".ptr, 2u64);
os.write(2, msg.ptr, msg.len: u64);
os.write(2, "\n".ptr, 1u64);
a.errs += 1;
};
// dup_str — copy n bytes from p into a fresh heap str.
fn dup_str(a: *arena, p: *u8, n: u64) str = {
return astrndup(a, p, n);
};
// ---- 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 next_line(a: *asm_) (*u8, u64) = {
if (a.pos >= a.srclen) { return nil, 0u64; };
let start: u64 = a.pos;
for (a.pos < a.srclen) {
if (a.src[a.pos] == 10u8) { a.pos = a.pos; a.pos += 0u64; } // no-op; explicit break via condition
else { a.pos += 1u64; continue; };
// hit newline
let n: u64 = a.pos - start;
let buf: *u8 = amalloc(a.a, n + 1u64): *u8;
let i: u64 = 0u64;
for (i < n) { buf[i] = a.src[start + i]; i += 1u64; };
buf[n] = 0u8;
a.pos += 1u64; // skip newline
return buf, n;
};
// EOF without trailing newline
let n: u64 = a.pos - start;
if (n == 0u64) { return nil, 0u64; };
let buf: *u8 = amalloc(a.a, n + 1u64): *u8;
let i: u64 = 0u64;
for (i < n) { buf[i] = a.src[start + i]; i += 1u64; };
buf[n] = 0u8;
return buf, n;
};
fn skip_ws(p: *u8, off: u64, n: u64) u64 = {
let i: u64 = off;
for (i < n) {
if (p[i] != 32u8) { if (p[i] != 9u8) { return i; }; };
i += 1u64;
};
return i;
};
// parse_operand — parse one operand from p[off..n), populate out.
// Returns new offset (clamped to n on error).
fn parse_operand(a: *asm_, p: *u8, off_in: u64, n: u64, out: *aoperand) u64 = {
let off: u64 = skip_ws(p, off_in, n);
out.atype = D_NONE;
out.reg = 0;
out.offset = 0i64;
let empty_str: str;
empty_str.ptr = nil; empty_str.len = 0;
out.asym = empty_str;
if (off >= n) { return off; };
let c0: u8 = p[off];
// $NUM
if (c0 == 36u8) { // '$'
off += 1u64;
let v: i64;
let used: u64;
v, used = a_parsenum(p + off, n - off);
out.atype = D_CONST;
out.offset = v;
return off + used;
};
// (REG)
if (c0 == 40u8) { // '('
off += 1u64;
let rstart: u64 = off;
for (off < n) {
if (p[off] == 41u8) { off = off; off += 0u64; } // no-op marker
else { off += 1u64; continue; };
let rn: u64 = off - rstart;
let r: i32 = reg_lookup(p + rstart, rn);
if (r == 0) { perr(a, "bad register in indirect"); return n; };
out.atype = D_INDIR;
out.reg = r;
out.offset = 0i64;
return off + 1u64; // past ')'
};
perr(a, "missing ')' in indirect");
return n;
};
// number(REG) — possibly signed — or bare $NUM-less constant
let cur: u64 = off;
let is_num: bool = false;
if (cur < n) {
if (p[cur] == 45u8) { is_num = true; }
else { if (p[cur] >= 48u8) { if (p[cur] <= 57u8) { is_num = true; }; }; };
};
if (is_num) {
let v: i64;
let used: u64;
v, used = a_parsenum(p + off, n - off);
let after: u64 = off + used;
if (after < n) { if (p[after] == 40u8) { // '('
let rstart: u64 = after + 1u64;
let cur2: u64 = rstart;
for (cur2 < n) {
if (p[cur2] == 41u8) { cur2 = cur2; cur2 += 0u64; }
else { cur2 += 1u64; continue; };
let rn: u64 = cur2 - rstart;
let r: i32 = reg_lookup(p + rstart, rn);
if (r == 0) { perr(a, "bad register"); return n; };
out.atype = D_INDIR;
out.reg = r;
out.offset = v;
return cur2 + 1u64;
};
perr(a, "missing ')'");
return n;
};};
out.atype = D_CONST;
out.offset = v;
return after;
};
// IDENT — register, symbol(SB), or branch label
if (a_isidstart(c0: i32)) {
let istart: u64 = off;
for (off < n) {
if (a_isidcont(p[off]: i32)) { off += 1u64; continue; };
off = off; off += 0u64; // loop break
let in_: u64 = off - istart;
// IDENT(SB) — external
if (off < n) { if (p[off] == 40u8) { // '('
let rstart: u64 = off + 1u64;
let cur2: u64 = rstart;
for (cur2 < n) {
if (p[cur2] == 41u8) { cur2 = cur2; cur2 += 0u64; }
else { cur2 += 1u64; continue; };
let rn: u64 = cur2 - rstart;
let r: i32 = reg_lookup(p + rstart, rn);
if (r == D_PSB) {
out.atype = D_EXTERN;
out.asym = dup_str(a.a, p + istart, in_);
} else {
out.atype = D_INDIR;
out.reg = r;
out.offset = 0i64;
};
return cur2 + 1u64;
};
perr(a, "missing ')'");
return n;
};};
let r: i32 = reg_lookup(p + istart, in_);
if (r != D_NONE) {
out.atype = r;
return off;
};
out.atype = D_BRANCH;
out.asym = dup_str(a.a, p + istart, in_);
return off;
};
// EOF inside ident
let in_: u64 = off - istart;
let r: i32 = reg_lookup(p + istart, in_);
if (r != D_NONE) { out.atype = r; return off; };
out.atype = D_BRANCH;
out.asym = dup_str(a.a, p + istart, in_);
return off;
};
perr(a, "unrecognised operand");
return n;
};
// Append a fresh aprog to the list with given opcode and label.
fn add_prog(a: *asm_, opc: i32, lbl: str) *aprog = {
let pr: *aprog = amalloc(a.a, 96u64): *aprog;
pr.as_ = opc;
pr.line = a.line;
pr.label = lbl;
pr.link = nil;
pr.bytes = nil;
pr.nbytes = 0u64;
pr.from = amalloc(a.a, 48u64): *aoperand;
pr.to = amalloc(a.a, 48u64): *aoperand;
if (a.head == nil) { a.head = pr; }
else {
// `a.tail.link = pr` would be a chained-dot write through a
// pointer field, which the C cgen we bootstrap on doesn't
// support (silently drops the store). Bind a local first.
let tail: *aprog = a.tail;
tail.link = pr;
};
a.tail = pr;
return pr;
};
export fn a_parse(a: *asm_) i32 = {
let pending: str;
pending.ptr = nil; pending.len = 0;
for (true) {
let line: *u8;
let n: u64;
line, n = next_line(a);
if (line == nil) { return a.errs; };
// skip leading ws
let i: u64 = skip_ws(line, 0u64, n);
// blank or //-comment
if (i >= n) { a.line += 1; continue; };
if (i + 1u64 < n) {
if (line[i] == 47u8) { if (line[i + 1u64] == 47u8) {
a.line += 1; continue;
};};
};
// Label? IDENT: starting at column 0 (no leading tab).
// Only if the identifier is followed by ':'. Otherwise, fall
// through to mnemonic parsing so e.g. `TEXT foo,$0` (which
// also starts with an idchar in column 0) gets parsed.
if (line[0u64] != 9u8) {
if (a_isidstart(line[i]: i32)) {
let q: u64 = i;
let scan_id: bool = true;
for (scan_id) {
if (q >= n) { scan_id = false; }
else { if (a_isidcont(line[q]: i32)) { q += 1u64; }
else { scan_id = false; }; };
};
if (q < n) { if (line[q] == 58u8) { // ':'
let nm: str = dup_str(a.a, line + i, q - i);
// Pending label gets a NOP prog so addresses pin.
if (pending.len > 0) {
let np: *aprog = add_prog(a, A_NOP, pending);
};
pending = nm;
a.line += 1;
continue;
};};
// not a label — fall through to mnemonic parse
};
};
// MNEMONIC at the start of the rest. Scan to first ws/EOL.
let mstart: u64 = i;
let m: u64 = mstart;
let scan: bool = true;
for (scan) {
if (m >= n) { scan = false; }
else { if (line[m] == 32u8) { scan = false; }
else { if (line[m] == 9u8) { scan = false; }
else { m += 1u64; }; }; };
};
let mlen: u64 = m - mstart;
let opc: i32 = opcode_lookup(line + mstart, mlen);
if (opc == 0) {
if (mlen > 0u64) {
perr(a, "unknown opcode");
};
pending.ptr = nil; pending.len = 0;
a.line += 1; continue;
};
let pr: *aprog = add_prog(a, opc, pending);
pending.ptr = nil; pending.len = 0;
// Skip ws after mnemonic
let r0: u64 = skip_ws(line, m, n);
if (opc == A_TEXT) {
// TEXT name,$framesize — find first ',' as the end of name.
let q: u64 = r0;
let comma_pos: u64 = n;
let scan_t: bool = true;
for (scan_t) {
if (q >= n) { scan_t = false; }
else { if (line[q] == 44u8) { comma_pos = q; scan_t = false; }
else { q += 1u64; }; };
};
let to_op: *aoperand = pr.to;
to_op.atype = D_EXTERN;
to_op.asym = dup_str(a.a, line + r0, comma_pos - r0);
if (comma_pos < n) {
let p2: u64 = comma_pos + 1u64;
p2 = skip_ws(line, p2, n);
if (p2 < n) { if (line[p2] == 36u8) { p2 += 1u64; }; };
let v: i64;
let used: u64;
v, used = a_parsenum(line + p2, n - p2);
let from_op: *aoperand = pr.from;
from_op.atype = D_CONST;
from_op.offset = v;
};
a.line += 1; continue;
};
if (opc == A_DATA) {
// DATA name(SB),"escaped bytes" — find first '(' as end of name.
let q: u64 = r0;
let lparen: u64 = n;
let scan_d: bool = true;
for (scan_d) {
if (q >= n) { scan_d = false; }
else { if (line[q] == 40u8) { lparen = q; scan_d = false; }
else { q += 1u64; }; };
};
let to_op: *aoperand = pr.to;
to_op.atype = D_EXTERN;
to_op.asym = dup_str(a.a, line + r0, lparen - r0);
// Skip past `(SB)` to land just after ')'.
let p2: u64 = lparen;
let scan_d2: bool = true;
for (scan_d2) {
if (p2 >= n) { scan_d2 = false; }
else { if (line[p2] == 41u8) { p2 += 1u64; scan_d2 = false; }
else { p2 += 1u64; }; };
};
// Skip ws / ',' / tab between `)` and the `"`.
let scan_d3: bool = true;
for (scan_d3) {
if (p2 >= n) { scan_d3 = false; }
else { if (line[p2] == 32u8) { p2 += 1u64; }
else { if (line[p2] == 44u8) { p2 += 1u64; }
else { if (line[p2] == 9u8) { p2 += 1u64; }
else { scan_d3 = false; }; }; }; };
};
if (p2 >= n) { perr(a, "DATA missing payload"); a.line += 1; continue; };
if (line[p2] != 34u8) { perr(a, "DATA expects \"...\""); a.line += 1; continue; };
p2 += 1u64; // past opening "
// Parse escape sequence into a fresh growable buffer.
let cap: u64 = 32u64;
let blen: u64 = 0u64;
let dbuf: *u8 = amalloc(a.a, cap): *u8;
for (p2 < n) {
if (line[p2] == 34u8) { p2 = p2; p2 += 0u64; p2 = n + 1u64; }
else {
let ch: u8 = line[p2];
p2 += 1u64;
if (ch == 92u8) { // '\'
if (p2 < n) {
let e: u8 = line[p2];
p2 += 1u64;
if (e == 110u8) { ch = 10u8; } // 'n'
else { if (e == 116u8) { ch = 9u8; }
else { if (e == 114u8) { ch = 13u8; }
else { if (e == 92u8) { ch = 92u8; }
else { if (e == 34u8) { ch = 34u8; }
else { if (e == 48u8) { ch = 0u8; }
else { if (e == 120u8) { // 'x'
if (p2 + 1u64 < n) {
let hi: u8 = line[p2];
let lo: u8 = line[p2 + 1u64];
p2 += 2u64;
let h: u8 = 0u8;
let l: u8 = 0u8;
if (hi <= 57u8) { h = hi - 48u8; }
else { h = (hi | 32u8) - 97u8 + 10u8; };
if (lo <= 57u8) { l = lo - 48u8; }
else { l = (lo | 32u8) - 97u8 + 10u8; };
ch = (h << 4u8) | l;
};
}
else { ch = e; };};};};};};};
};
};
if (blen + 1u64 > cap) {
let ncap: u64 = cap * 2u64;
let nb: *u8 = amalloc(a.a, ncap): *u8;
let bi: u64 = 0u64;
for (bi < blen) { nb[bi] = dbuf[bi]; bi += 1u64; };
dbuf = nb;
cap = ncap;
};
dbuf[blen] = ch;
blen += 1u64;
};
};
pr.bytes = dbuf;
pr.nbytes = blen;
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) {
if (line[q] == 44u8) {
if (comma < 0i64) { comma = q: i64; };
};
q += 1u64;
};
if (comma >= 0i64) {
let cu: u64 = comma: u64;
parse_operand(a, line, r0, cu, pr.from);
parse_operand(a, line + (cu + 1u64), 0u64, n - (cu + 1u64), pr.to);
} else { if (r0 < n) {
parse_operand(a, line, r0, n, pr.to);
};};
a.line += 1;
};
return a.errs;
};