selfhost/cmd/wcc: route cgalloc CALL through ffiresolve

Wwstage's cgalloc hardcoded `CALL rt_alloc(SB)` at cgenexpr.ww:2747 and
cgenstmt.ww:647. Cstage already routes through ffi_resolve("alloc")
at cmd/w6c/cgen.c:4149 — when a fixture lacks the @symbol("rt_alloc")
decl in scope, cstage falls back to `CALL alloc(SB)` while wwstage
still emits `CALL rt_alloc(SB)`. The divergence is dormant in
ww build (combined.ww always pulls lib/os/os.ww's decl) but activates
under direct `w6c file.ww` and any other single-file path.

Replace the hardcoded line with the ffiresolve(c, "alloc") pattern
already used for user-function calls. The @symbol decl in lib/os/os.ww
is unchanged and propagates via the combine step.

Extends test/wcc/758_cgalloc_str_field.c with 4 table-driven asm rows
that compile a fixture via direct w6c (no combine) and `cmp` the
CALL <sym>(SB) line between stages. The 3 noscope rows fail without
the fix and pass with it; the withsym row pins the positive ffi-hit
path. Test count internal: 12 → 16; total make test: 132/132.
This commit is contained in:
2026-05-20 17:18:32 +09:00
parent af1549d9c5
commit 4c51bce244
5 changed files with 204 additions and 29 deletions

View File

@@ -15953,7 +15953,9 @@ fn cgalloc(c: *cgen, n: *node) void = {
emitline("\tMOVQ\t$");
emitint(sz: i64);
emitline(", DI\n");
emitline("\tCALL\trt_alloc(SB)\n");
emitline("\tCALL\t");
emitline(ffiresolve(c, "alloc"));
emitline("(SB)\n");
emitline("\tCMPQ\t$0, AX\n");
emitline("\tJNE\t"); emitline(okl); emitline("\n");
emitline("\tMOVQ\t$1, AX\n");
@@ -19651,7 +19653,9 @@ fn cglet(c: *cgen, n: *node) void = {
emitline("\tIMULQ\tBX, AX\n");
};
emitline("\tMOVQ\tAX, DI\n");
emitline("\tCALL\trt_alloc(SB)\n");
emitline("\tCALL\t");
emitline(ffiresolve(c, "alloc"));
emitline("(SB)\n");
if (viatryunw) {
let okl: str = mklabel(c, "tryunw_ok");
emitline("\tCMPQ\t$0, AX\n");

View File

@@ -2744,7 +2744,9 @@ fn cgalloc(c: *cgen, n: *node) void = {
emitline("\tMOVQ\t$");
emitint(sz: i64);
emitline(", DI\n");
emitline("\tCALL\trt_alloc(SB)\n");
emitline("\tCALL\t");
emitline(ffiresolve(c, "alloc"));
emitline("(SB)\n");
emitline("\tCMPQ\t$0, AX\n");
emitline("\tJNE\t"); emitline(okl); emitline("\n");
emitline("\tMOVQ\t$1, AX\n");

View File

@@ -644,7 +644,9 @@ fn cglet(c: *cgen, n: *node) void = {
emitline("\tIMULQ\tBX, AX\n");
};
emitline("\tMOVQ\tAX, DI\n");
emitline("\tCALL\trt_alloc(SB)\n");
emitline("\tCALL\t");
emitline(ffiresolve(c, "alloc"));
emitline("(SB)\n");
if (viatryunw) {
let okl: str = mklabel(c, "tryunw_ok");
emitline("\tCMPQ\t$0, AX\n");

View File

@@ -15953,7 +15953,9 @@ fn cgalloc(c: *cgen, n: *node) void = {
emitline("\tMOVQ\t$");
emitint(sz: i64);
emitline(", DI\n");
emitline("\tCALL\trt_alloc(SB)\n");
emitline("\tCALL\t");
emitline(ffiresolve(c, "alloc"));
emitline("(SB)\n");
emitline("\tCMPQ\t$0, AX\n");
emitline("\tJNE\t"); emitline(okl); emitline("\n");
emitline("\tMOVQ\t$1, AX\n");
@@ -19651,7 +19653,9 @@ fn cglet(c: *cgen, n: *node) void = {
emitline("\tIMULQ\tBX, AX\n");
};
emitline("\tMOVQ\tAX, DI\n");
emitline("\tCALL\trt_alloc(SB)\n");
emitline("\tCALL\t");
emitline(ffiresolve(c, "alloc"));
emitline("(SB)\n");
if (viatryunw) {
let okl: str = mklabel(c, "tryunw_ok");
emitline("\tCMPQ\t$0, AX\n");

View File

@@ -1,34 +1,42 @@
/*
* 758_cgalloc_str_field — cgalloc N_STRUCTLIT str-field store (task #22).
* 758_cgalloc_str_field — cgalloc N_STRUCTLIT str-field store (task #22)
* and the cgalloc CALL-site ffiresolve parity (task #25 / #24-part-A).
*
* Pre-fix: wwstage cgalloc's per-field walk routed str-typed fields
* through the generic `MOVQ (SP), BX ; MOVQ AX, foff(BX)` path, which
* landed only AX (str.ptr) and clobbered BX (str.len) with the heap
* base. str.len silently stayed zero (rt_alloc is MAP_ANON-backed, so
* the slot was zero-init rather than garbage — but still wrong). 990
* and 995 byte-identity didn't catch this because nothing in the
* Pre-fix (#22): wwstage cgalloc's per-field walk routed str-typed
* fields through the generic `MOVQ (SP), BX ; MOVQ AX, foff(BX)` path,
* which landed only AX (str.ptr) and clobbered BX (str.len) with the
* heap base. str.len silently stayed zero (rt_alloc is MAP_ANON-backed,
* so the slot was zero-init rather than garbage — but still wrong).
* 990 and 995 byte-identity didn't catch this because nothing in the
* bootstrapped selfhost source uses `alloc(T { strfield = "..." })!`.
*
* Fix: a str-field-typed branch mirrors cmd/w6c/cgen.c:4184-4190
* route the heap base through CX so BX=len survives both stores
* Fix (#22): a str-field-typed branch mirrors cmd/w6c/cgen.c:4184-4190
* route the heap base through CX so BX=len survives both stores
* (ptr at foff+0, len at foff+8). Task #23 (slice/tagged/fn-pair
* multi-word fields) is the broader follow-up; this row pins str.
*
* Each row runs the generated binary under cstage and (when present)
* wwstage; the exit code is the regression-catching gate. The cstage
* row is the cross-check oracle.
* Pre-fix (#25): wwstage's cgalloc emitted a hardcoded `CALL
* rt_alloc(SB)` while cstage routed the same site through
* ffi_resolve("alloc"), so direct `w6c` vs `w6c_ww` on a fixture
* without the @symbol decl in scope diverged (cstage: `CALL
* alloc(SB)`; wwstage: `CALL rt_alloc(SB)`). The fix swaps both
* cgenexpr.ww and cgenstmt.ww cgalloc CALL sites to
* `ffiresolve(c, "alloc")`, aligning wwstage down to the leaner
* cstage shape (CLAUDE.md rule 10). The `asm_rows` table below pins
* the CALL line via single-file `w6c -o` / `w6c_ww -o`; `ww build`
* combines lib/os/os.ww into the fixture and would always supply
* the @symbol decl, masking the regression.
*
* Byte-identity is intentionally NOT checked here. cgalloc N_STRUCTLIT
* already diverges on two pre-existing axes that 995_self_rebuild
* doesn't exercise because selfhost source uses amalloc, never
* `alloc(T{...})!`:
* - `CALL alloc(SB)` (cstage, via ffi_resolve) vs `CALL rt_alloc(SB)`
* (wwstage, hardcoded symbol).
* - `(BX)` (cstage txt.c omits zero displacement) vs `0(BX)`
* (wwstage emitint(0) is unconditional). The new str branch
* follows the existing float / int branch shape and inherits the
* same formatting; aligning all three with cstage is a separate
* follow-up.
* The runtime `rows` below run the generated binary under cstage and
* (when present) wwstage; the exit code is the regression-catching
* gate. The cstage row is the cross-check oracle.
*
* Full asm byte-identity is intentionally NOT checked here — the
* `(BX)` (cstage txt.c omits zero displacement) vs `0(BX)` (wwstage
* emitint(0) is unconditional) divergence still stands. The new str
* branch follows the existing float / int branch shape and inherits
* the same formatting; aligning all three with cstage is task #24
* part B.
*/
#include <stdio.h>
#include <stdlib.h>
@@ -164,6 +172,144 @@ run_driver(const char *driver, const struct row *r, int i)
return got;
}
/* asm_row — fixture for the ffiresolve CALL-line check. `want_sym`
* is the unqualified symbol expected inside `CALL\t<sym>(SB)` from
* both stages' .s output. Single-file `w6c` compile (no `ww build`
* combine), so the @symbol decl is only in scope when the fixture
* writes one — that's how `noscope_*` rows pin the pre-fix wwstage
* hardcoded-`rt_alloc` divergence and `withsym_*` rows pin that
* ffiresolve actually hits when the decl is present. */
struct asm_row { const char *label; const char *src; const char *want_sym; };
static const struct asm_row asm_rows[] = {
/* Int-field, no @symbol in scope. ffiresolve("alloc") misses the
* ffi table and returns "alloc" unchanged on both stages. Pre-fix
* wwstage hardcoded `CALL rt_alloc(SB)` → diverged from cstage's
* ffi_resolve-mediated `CALL alloc(SB)`. */
{ "noscope_intfield",
"package main;\n"
"type holder = struct { n: i32 };\n"
"fn dummy() *holder = { return alloc(holder { n = 42 })!; };\n",
"alloc" },
/* Str-field, no @symbol in scope. Confirms the str-field branch
* (the #22 fix) still routes its CALL through ffiresolve, not a
* stray hardcoded literal copied alongside the #22 emit. */
{ "noscope_strfield",
"package main;\n"
"type holder = struct { s: str };\n"
"fn dummy() *holder = { return alloc(holder { s = \"hi\" })!; };\n",
"alloc" },
/* Two-step let-then-assign. cglet's CALL site (cgenstmt.ww:647)
* is the second cgalloc emit point; this row covers it. */
{ "noscope_let_then_assign",
"package main;\n"
"type holder = struct { n: i32 };\n"
"fn dummy() *holder = {\n"
" let p: *holder = alloc(holder { n = 0 })!;\n"
" p.n = 7;\n"
" return p;\n"
"};\n",
"alloc" },
/* Explicit @symbol decl in scope. ffiresolve("alloc") → "rt_alloc"
* so both stages emit `CALL rt_alloc(SB)` — positive confirmation
* that the ffi table lookup hits, complementing the noscope rows. */
{ "withsym_intfield",
"package main;\n"
"@symbol(\"rt_alloc\") export fn alloc(n: u64) *void;\n"
"type holder = struct { n: i32 };\n"
"fn dummy() *holder = { return alloc(holder { n = 42 })!; };\n",
"rt_alloc" },
};
/* asm_call_check — compile via direct w6c / w6c_ww (no `ww build`),
* extract the `CALL\t<sym>(SB)` line referencing alloc/rt_alloc from
* each .s file, and verify (a) both stages emit the same line and
* (b) the symbol matches r->want_sym. Returns 0 on success. */
static int
asm_call_check(const char *bin, const struct asm_row *r, int i)
{
char src[64], cs[64], ws[64], cmd[1024];
snprintf(src, sizeof src, "/tmp/wcas_asm_%d_%d.ww", getpid(), i);
snprintf(cs, sizeof cs, "/tmp/wcas_asm_%d_%d_c.s", getpid(), i);
snprintf(ws, sizeof ws, "/tmp/wcas_asm_%d_%d_w.s", getpid(), i);
FILE *f = fopen(src, "wb");
if (!f) return -1;
fputs(r->src, f);
fclose(f);
int rc = 0;
snprintf(cmd, sizeof cmd, "%s/w6c -o %s %s 2>/dev/null", bin, cs, src);
if (runwait(cmd) != 0) {
fprintf(stderr, "asm[%s]: w6c errored\n", r->label);
unlink(src);
return -1;
}
snprintf(cmd, sizeof cmd, "%s/w6c_ww -o %s %s 2>/dev/null",
bin, ws, src);
if (runwait(cmd) != 0) {
fprintf(stderr, "asm[%s]: w6c_ww errored\n", r->label);
unlink(src); unlink(cs);
return -1;
}
char want[64];
snprintf(want, sizeof want, "\tCALL\t%s(SB)\n", r->want_sym);
char cline[256] = {0}, wline[256] = {0};
FILE *fc = fopen(cs, "rb");
FILE *fw = fopen(ws, "rb");
if (!fc || !fw) {
fprintf(stderr, "asm[%s]: open .s failed\n", r->label);
rc = -1;
} else {
char buf[256];
while (fgets(buf, sizeof buf, fc)) {
if (strstr(buf, "\tCALL\t")
&& (strstr(buf, "alloc(SB)")
|| strstr(buf, "rt_alloc(SB)"))) {
strncpy(cline, buf, sizeof cline - 1);
break;
}
}
while (fgets(buf, sizeof buf, fw)) {
if (strstr(buf, "\tCALL\t")
&& (strstr(buf, "alloc(SB)")
|| strstr(buf, "rt_alloc(SB)"))) {
strncpy(wline, buf, sizeof wline - 1);
break;
}
}
if (cline[0] == '\0' || wline[0] == '\0') {
fprintf(stderr,
"asm[%s]: no CALL alloc line found (c=%d w=%d)\n",
r->label, cline[0] != '\0', wline[0] != '\0');
rc = -1;
} else if (strcmp(cline, wline) != 0) {
/* strncpy may leave no '\n'; both lines came from
* fgets so they include it. Strip for cleaner err. */
char *p;
if ((p = strchr(cline, '\n'))) *p = '\0';
if ((p = strchr(wline, '\n'))) *p = '\0';
fprintf(stderr,
"asm[%s]: cstage=<%s> wwstage=<%s>\n",
r->label, cline, wline);
rc = -1;
} else if (strcmp(cline, want) != 0) {
char *p;
if ((p = strchr(cline, '\n'))) *p = '\0';
fprintf(stderr,
"asm[%s]: got=<%s> want=<\tCALL\t%s(SB)>\n",
r->label, cline, r->want_sym);
rc = -1;
}
}
if (fc) fclose(fc);
if (fw) fclose(fw);
unlink(src); unlink(cs); unlink(ws);
return rc;
}
int
main(void)
{
@@ -212,6 +358,23 @@ main(void)
}
}
/* Asm CALL-line check via direct w6c / w6c_ww. Gated on w6c_ww
* existence — when wwstage isn't built yet the cstage half alone
* can't catch the divergence. */
char w6c_ww[1024];
snprintf(w6c_ww, sizeof w6c_ww, "%s/w6c_ww", bin);
if (access(w6c_ww, X_OK) == 0) {
int an = (int)(sizeof asm_rows / sizeof asm_rows[0]);
for (int i = 0; i < an; i++) {
total++;
if (asm_call_check(bin, &asm_rows[i], i) != 0)
fail++;
}
} else {
fprintf(stderr,
"cgalloc_str_field: skip asm rows (no %s)\n", w6c_ww);
}
if (fail) {
fprintf(stderr,
"cgalloc_str_field: %d/%d fixtures failed\n",