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

@@ -761,13 +761,45 @@ fn emitdatawbyte(b: u8) void = {
os.write(1, bb.ptr, 1u64);
};
// letpreintern — intern strlits referenced from top-level str-let
// initialisers BEFORE emitdatasection runs. Mirrors cmd/w6c/cgen.c
// let_pre_intern: emitletdataw later looks up the same label, and
// emitdatasection emits the DATA row in the same .s file. Running
// emitletdataw after emitdatasection would flip the (DATA strlits,
// DATAW lets) section order and break byte-identity.
export fn letpreintern(c: *cgen, file: *node) void = {
if (file == nil) { return; };
let d: *node = file.list;
for (d != nil) {
if (d.kind == nkind.N_LET) {
let sz: i32 = letemitsize(c, d);
if (sz == 16) {
let r: *node = d.rhs;
for (r != nil) {
if (r.kind != nkind.N_CAST) { break; };
r = r.lhs;
};
if (r != nil) {
if (r.kind == nkind.N_STRLIT) {
if (r.str.len > 0) {
internstrlit(c, r.str);
};
};
};
};
};
d = d.next;
};
};
// emitletdataw — DATAW directive per top-level `let` global.
// 8B scalar with int/rune/bool/nil literal init (or no init).
// 16B str with no init (or `nil` / `""`) — zero header; the
// program must assign a real strlit at runtime before
// using .ptr / .len.
// Non-literal scalar inits and non-empty strlit inits are skipped
// so the link surfaces an undefined-symbol error.
// 16B str no init / `nil` / `""` → 16 zero bytes; or non-empty
// strlit init → 8 zero placeholder + 8 LE len bytes plus a
// DATAR slot+0,strlit reloc that the linker patches at load.
// sz struct — zero only.
// Non-literal scalar inits and unsupported shapes are skipped so the
// link surfaces an undefined-symbol error if the binding is used.
fn emitletdataw(c: *cgen, file: *node) void = {
let d: *node = file.list;
for (d != nil) {
@@ -810,31 +842,67 @@ fn emitletdataw(c: *cgen, file: *node) void = {
};
};
if (sz == 16 && !issg) {
let ok: bool = true;
if (d.rhs != nil) {
let r: *node = d.rhs;
for (r != nil) {
if (r.kind != nkind.N_CAST) { break; };
r = r.lhs;
};
ok = false;
if (r != nil) {
if (r.kind == nkind.N_NIL) { ok = true; };
if (r.kind == nkind.N_STRLIT) {
if (r.str.len == 0) { ok = true; };
};
let r: *node = d.rhs;
for (r != nil) {
if (r.kind != nkind.N_CAST) { break; };
r = r.lhs;
};
// str-literal init (non-empty): emit
// the 16B payload as 8 placeholder zero
// bytes + 8 LE bytes of length, then a
// DATAR reloc to patch the ptr half with
// the strlit's runtime VA.
let strlitinit: bool = false;
if (r != nil) {
if (r.kind == nkind.N_STRLIT) {
if (r.str.len > 0) { strlitinit = true; };
};
};
if (ok) {
if (strlitinit) {
let lab: str = internstrlit(c, r.str);
let v: u64 = r.str.len: u64;
emitline("DATAW ");
emitsymname(c, nm);
emitline("(SB),\"");
let i: i32 = 0;
for (i < 16) {
emitdatawbyte(0u8);
for (i < 8) { emitdatawbyte(0u8); i += 1; };
i = 0;
let nv: u64 = v;
for (i < 8) {
emitdatawbyte((nv & 255u64): u8);
nv = nv >> 8u64;
i += 1;
};
emitline("\"\n");
emitline("DATAR ");
emitsymname(c, nm);
emitline("+0(SB),");
os.write(1, lab.ptr, lab.len: u64);
emitline("(SB)\n");
} else {
// zero-init: accept no rhs, nil,
// or empty strlit.
let ok: bool = true;
if (d.rhs != nil) {
ok = false;
if (r != nil) {
if (r.kind == nkind.N_NIL) { ok = true; };
if (r.kind == nkind.N_STRLIT) {
if (r.str.len == 0) { ok = true; };
};
};
};
if (ok) {
emitline("DATAW ");
emitsymname(c, nm);
emitline("(SB),\"");
let i: i32 = 0;
for (i < 16) {
emitdatawbyte(0u8);
i += 1;
};
emitline("\"\n");
};
};
};
if (sz == 24 && !issg) {

View File

@@ -297,6 +297,7 @@ export fn cgfile(c: *cgen, file: *node) void = {
};
d = d.next;
};
letpreintern(c, file);
emitdatasection(c);
emitdefconstants(c, file);
emitletdataw(c, file);