selfhost: mirror writable .data + R_X86_64_64 across the wwstage

Bring the wwstage toolchain to parity with C-side DATAW / DATAR /
.data / .rela.data support. With this, w6c_ww + w6a_ww + w6l_ww can
compile, assemble and link `let g: str = "lit";` (and the scalar /
str / slice / struct globals that landed earlier) end-to-end, with
output that's byte-identical to the C-side pipeline.

w6a (types.ww / parse.ww / asm.ww / obj.ww):
  - A_DATAW + A_DATAR opcodes; parser learns `name+disp(SB)`;
    A_DATAR records an R_X86_64_64 reloc in .data via the new
    addrelocdata helper; areloc gains a `section` flag and asym
    an `isdata` flag; obj.ww splits relocs into .rela.text /
    .rela.data, emits .data PROGBITS + .rela.data conditionally,
    and shuffles section indices the same way cmd/w6a/obj.c does
    so byte output stays identical when no DATAW/DATAR are used.

w6l (sym.ww / obj.ww / pass.ww / out.ww / dynout.ww / main.ww):
  - lrel grows `section`; lsym grows `indata`; lobj tracks
    dataoff / datasize; lnk grows combined .data buffer;
  - obj.ww loads .data and .rela.data, registers data symbols
    with indata=1 and val shifted by the input's data_off, and
    the archive scanner includes both .text and .data globals;
  - pass.ww adds R_X86_64_64 (patch 8 bytes in .text or .data
    with sym_va + addend); relocate's signature becomes
    (textva, datava);
  - out.ww emits a second PT_LOAD (R+W) when datalen > 0, with
    .data at the page-aligned offset after .text;
  - dynout.ww refuses .data + -l/-L cleanly (matches the C-side
    error message);
  - main.ww computes text_va / data_va and passes both to
    relocate.

wcc cgen (cgen.ww / cgendecl.ww):
  - letpreintern walks top-level str-lets and interns the strlit
    BEFORE emitdatasection emits its DATA row, so emitletdataw
    can later look up the same label;
  - emitletdataw's 16B branch detects non-empty strlit init and
    emits the 8-zero + 8-LE-len DATAW plus a DATAR slot+0,strlit
    reloc, mirroring cmd/w6c/cgen.c.

Verified: `wwdump_ww -c` byte-matches `w6c` on a `let g: str =
"hello world\n";` fixture; `w6a_ww` and `w6l_ww` produce a
binary byte-identical to the C-side pipeline that runs and
prints "hello world". Bootstrap fixed-point holds (ww2 == ww3 ==
ww4), 26/26 tests green.
This commit is contained in:
2026-05-12 13:03:03 +09:00
parent 003f707618
commit 1ac9980f7e
16 changed files with 1132 additions and 145 deletions

View File

@@ -86,6 +86,8 @@ fn opcodelookup(p: *u8, n: u64) i32 = {
if (streqlit(p, n, "SYSCALL")) { return A_SYSCALL; };
if (streqlit(p, n, "TEXT")) { return A_TEXT; };
if (streqlit(p, n, "DATA")) { return A_DATA; };
if (streqlit(p, n, "DATAW")) { return A_DATAW; };
if (streqlit(p, n, "DATAR")) { return A_DATAR; };
return A_NOP;
};
@@ -291,13 +293,25 @@ fn parseoperand(a: *asm_, p: *u8, offin: u64, n: u64, out: *aoperand) u64 = {
return after;
};
// IDENT — register, symbol(SB), or branch label
// IDENT — register, symbol(SB), symbol+disp(SB), or branch label
if (isidstart(c0: i32)) {
let istart: u64 = off;
for (off < n) {
if (isidcont(p[off]: i32)) { off += 1u64; continue; };
off = off; off += 0u64; // loop break
let in_: u64 = off - istart;
// Optional `+disp` between the ident and `(SB)`. Used
// by DATAR to address bytes within a previously-defined
// .data slot (e.g. `DATAR s+8(SB),...`).
let symdisp: i64 = 0i64;
if (off < n) { if (p[off] == 43u8) { // '+'
off += 1u64;
let v: i64;
let used: u64;
v, used = parsenum(p + off, n - off);
symdisp = v;
off += used;
};};
// IDENT(SB) — external
if (off < n) { if (p[off] == 40u8) { // '('
let rstart: u64 = off + 1u64;
@@ -310,6 +324,7 @@ fn parseoperand(a: *asm_, p: *u8, offin: u64, n: u64, out: *aoperand) u64 = {
if (r == D_PSB) {
out.atype = D_EXTERN;
out.asym = dupstr(a.a, p + istart, in_);
out.offset = symdisp;
} else {
out.atype = D_INDIR;
out.reg = r;
@@ -459,8 +474,9 @@ export fn parse(a: *asm_) i32 = {
a.line += 1; continue;
};
if (opc == A_DATA) {
// DATA name(SB),"escaped bytes" — find first '(' as end of name.
if (opc == A_DATA || opc == A_DATAW) {
// DATA / DATAW name(SB),"escaped bytes" — same syntax,
// different destination section (.text vs .data).
let q: u64 = r0;
let lparen: u64 = n;
let scand: bool = true;