From 469ec85551d9bcf3e1f2614758336bcf92e74547 Mon Sep 17 00:00:00 2001 From: Hojun-Cho Date: Wed, 20 May 2026 18:07:32 +0900 Subject: [PATCH] selfhost/cmd/{w6a,w6l}: migrate 6 amalloc sites to alloc(T{...})! MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Phase 0 batch 1. Six typed-struct allocations switch from amalloc(arena, NNu64): *T over-sized byte counts to alloc(T{...})! with partial struct literal initialization. MAP_ANON-zero from rt_alloc covers any field the literal omits — same contract the amalloc bump arena provided via its explicit zero loop, but without the rule-13 size literal at the call site. Converted: - w6a/asm.ww addreloc, addrelocdata, addfixup (areloc, afixup) - w6l/sym.ww intern (lsym) - w6l/dyn.ww loadso (lso), lexport lexport's `if (vernamecs == nil) { e.version.ptr = nil; e.version.len = 0i32; }` branch dropped — MAP_ANON-zero provides the empty-version slot for free; the inverted `if (vernamecs != nil)` only takes the dcstrtostr path. w6l/sym.ww:20 comment updated from "amalloc-zeroing" to "alloc-zeroing gives 0, not -1" so the documented mechanism matches the call. w6a/obj.ww's 2 remaining amalloc sites (bufinit + bufgrow) are runtime-N *u8 byte buffers, deferred to #8 (runtime-N alloc API). Verified via 991_w6a_ww + 992_w6l_ww + 995_self_rebuild + 996_dyn_ww byte-identity. make test 132/132. --- selfhost/cmd/w6a/asm.ww | 21 +++------------------ selfhost/cmd/w6a/main.combined.ww | 21 +++------------------ selfhost/cmd/w6l/dyn.ww | 14 +++----------- selfhost/cmd/w6l/main.combined.ww | 20 +++++--------------- selfhost/cmd/w6l/sym.ww | 6 ++---- 5 files changed, 16 insertions(+), 66 deletions(-) diff --git a/selfhost/cmd/w6a/asm.ww b/selfhost/cmd/w6a/asm.ww index 52dfdec2..d7def543 100644 --- a/selfhost/cmd/w6a/asm.ww +++ b/selfhost/cmd/w6a/asm.ww @@ -40,13 +40,7 @@ export fn emitu32(a: *asm_, v: u32) void = { }; export fn addreloc(a: *asm_, off: u64, kind: i32, s: *asym, add: i64) void = { - let r: *areloc = amalloc(a.a, 64u64): *areloc; - r.off = off; - r.section = 0; // .text - r.kind = kind; - r.asy = s; - r.addend = add; - r.rnext = a.relocs; + let r: *areloc = alloc(areloc { off = off, section = 0, kind = kind, asy = s, addend = add, rnext = a.relocs })!; a.relocs = r; }; @@ -54,13 +48,7 @@ export fn addreloc(a: *asm_, off: u64, kind: i32, s: *asym, add: i64) void = { // DATAR to patch a 64-bit slot with a symbol's runtime VA. obj.ww // separates these into .rela.data when emitting the .o. export fn addrelocdata(a: *asm_, off: u64, kind: i32, s: *asym, add: i64) void = { - let r: *areloc = amalloc(a.a, 64u64): *areloc; - r.off = off; - r.section = 1; // .data - r.kind = kind; - r.asy = s; - r.addend = add; - r.rnext = a.relocs; + let r: *areloc = alloc(areloc { off = off, section = 1, kind = kind, asy = s, addend = add, rnext = a.relocs })!; a.relocs = r; }; @@ -254,10 +242,7 @@ fn labeldefined(a: *asm_, name: str) bool = { // ---- fixup helper ----------------------------------------------------- fn addfixup(a: *asm_, off: u64, label: str) void = { - let f: *afixup = amalloc(a.a, 48u64): *afixup; - f.off = off; - f.label = label; - f.fnext = a.fixups; + let f: *afixup = alloc(afixup { off = off, label = label, fnext = a.fixups })!; a.fixups = f; }; diff --git a/selfhost/cmd/w6a/main.combined.ww b/selfhost/cmd/w6a/main.combined.ww index 5d78e7d2..86b75d32 100644 --- a/selfhost/cmd/w6a/main.combined.ww +++ b/selfhost/cmd/w6a/main.combined.ww @@ -1783,13 +1783,7 @@ export fn emitu32(a: *asm_, v: u32) void = { }; export fn addreloc(a: *asm_, off: u64, kind: i32, s: *asym, add: i64) void = { - let r: *areloc = amalloc(a.a, 64u64): *areloc; - r.off = off; - r.section = 0; // .text - r.kind = kind; - r.asy = s; - r.addend = add; - r.rnext = a.relocs; + let r: *areloc = alloc(areloc { off = off, section = 0, kind = kind, asy = s, addend = add, rnext = a.relocs })!; a.relocs = r; }; @@ -1797,13 +1791,7 @@ export fn addreloc(a: *asm_, off: u64, kind: i32, s: *asym, add: i64) void = { // DATAR to patch a 64-bit slot with a symbol's runtime VA. obj.ww // separates these into .rela.data when emitting the .o. export fn addrelocdata(a: *asm_, off: u64, kind: i32, s: *asym, add: i64) void = { - let r: *areloc = amalloc(a.a, 64u64): *areloc; - r.off = off; - r.section = 1; // .data - r.kind = kind; - r.asy = s; - r.addend = add; - r.rnext = a.relocs; + let r: *areloc = alloc(areloc { off = off, section = 1, kind = kind, asy = s, addend = add, rnext = a.relocs })!; a.relocs = r; }; @@ -1997,10 +1985,7 @@ fn labeldefined(a: *asm_, name: str) bool = { // ---- fixup helper ----------------------------------------------------- fn addfixup(a: *asm_, off: u64, label: str) void = { - let f: *afixup = amalloc(a.a, 48u64): *afixup; - f.off = off; - f.label = label; - f.fnext = a.fixups; + let f: *afixup = alloc(afixup { off = off, label = label, fnext = a.fixups })!; a.fixups = f; }; diff --git a/selfhost/cmd/w6l/dyn.ww b/selfhost/cmd/w6l/dyn.ww index 3156a4cb..135f88fd 100644 --- a/selfhost/cmd/w6l/dyn.ww +++ b/selfhost/cmd/w6l/dyn.ww @@ -289,10 +289,7 @@ export fn loadso(l: *lnk, path: *u8) i32 = { // Build the lso. Exports are appended in dynsym order so // soprovides_v's first-match semantics match the C version. - let so: *lso = amalloc(l.a, 64u64): *lso; - so.path = dcstrtostr(l.a, path); - so.soname = dcstrtostr(l.a, sonamecs); - so.exports = nil; + let so: *lso = alloc(lso { path = dcstrtostr(l.a, path), soname = dcstrtostr(l.a, sonamecs) })!; let tail: *lexport = nil; let si: u64 = 1u64; @@ -344,15 +341,10 @@ export fn loadso(l: *lnk, path: *u8) i32 = { }; if (keep != 0) { - let e: *lexport = amalloc(l.a, 48u64): *lexport; - e.name = dcstrtostr(l.a, nmp); - if (vernamecs == nil) { - e.version.ptr = nil; - e.version.len = 0i32; - } else { + let e: *lexport = alloc(lexport { name = dcstrtostr(l.a, nmp) })!; + if (vernamecs != nil) { e.version = dcstrtostr(l.a, vernamecs); }; - e.enext = nil; if (tail == nil) { so.exports = e; } else { diff --git a/selfhost/cmd/w6l/main.combined.ww b/selfhost/cmd/w6l/main.combined.ww index f4052ccb..4d705566 100644 --- a/selfhost/cmd/w6l/main.combined.ww +++ b/selfhost/cmd/w6l/main.combined.ww @@ -877,7 +877,7 @@ type lsym = struct { idxinowner: i32, // Dynamic-linking fields. Set by resolve when an undefined sym // is provided by some loaded lso. pltidx and dynsymidx default - // to -1 (set explicitly by resolve, not by amalloc-zeroing). + // to -1 (set explicitly by resolve; alloc-zeroing gives 0, not -1). isdyn: i32, dynlib: *lso, dynversion: str, // matched export's version; len 0 if none @@ -956,9 +956,7 @@ export fn intern(l: *lnk, name: str) *lsym = { if (streq(s.name, name)) { return s; }; s = s.snext; }; - let n: *lsym = amalloc(l.a, 96u64): *lsym; - n.name = name; - n.snext = l.syms; + let n: *lsym = alloc(lsym { name = name, snext = l.syms })!; l.syms = n; return n; }; @@ -1876,10 +1874,7 @@ export fn loadso(l: *lnk, path: *u8) i32 = { // Build the lso. Exports are appended in dynsym order so // soprovides_v's first-match semantics match the C version. - let so: *lso = amalloc(l.a, 64u64): *lso; - so.path = dcstrtostr(l.a, path); - so.soname = dcstrtostr(l.a, sonamecs); - so.exports = nil; + let so: *lso = alloc(lso { path = dcstrtostr(l.a, path), soname = dcstrtostr(l.a, sonamecs) })!; let tail: *lexport = nil; let si: u64 = 1u64; @@ -1931,15 +1926,10 @@ export fn loadso(l: *lnk, path: *u8) i32 = { }; if (keep != 0) { - let e: *lexport = amalloc(l.a, 48u64): *lexport; - e.name = dcstrtostr(l.a, nmp); - if (vernamecs == nil) { - e.version.ptr = nil; - e.version.len = 0i32; - } else { + let e: *lexport = alloc(lexport { name = dcstrtostr(l.a, nmp) })!; + if (vernamecs != nil) { e.version = dcstrtostr(l.a, vernamecs); }; - e.enext = nil; if (tail == nil) { so.exports = e; } else { diff --git a/selfhost/cmd/w6l/sym.ww b/selfhost/cmd/w6l/sym.ww index 1b92b50d..c6a3c5a2 100644 --- a/selfhost/cmd/w6l/sym.ww +++ b/selfhost/cmd/w6l/sym.ww @@ -17,7 +17,7 @@ type lsym = struct { idxinowner: i32, // Dynamic-linking fields. Set by resolve when an undefined sym // is provided by some loaded lso. pltidx and dynsymidx default - // to -1 (set explicitly by resolve, not by amalloc-zeroing). + // to -1 (set explicitly by resolve; alloc-zeroing gives 0, not -1). isdyn: i32, dynlib: *lso, dynversion: str, // matched export's version; len 0 if none @@ -96,9 +96,7 @@ export fn intern(l: *lnk, name: str) *lsym = { if (streq(s.name, name)) { return s; }; s = s.snext; }; - let n: *lsym = amalloc(l.a, 96u64): *lsym; - n.name = name; - n.snext = l.syms; + let n: *lsym = alloc(lsym { name = name, snext = l.syms })!; l.syms = n; return n; };