cstage+test: route cgreturn @retscr through fixed-name SSoT (#15)

cgen.c's two ≤24B / tagged-widen return scratch allocations called
mklabel(c, "retscr"), bumping labelseq once per function with a
struct or tagged return. Wwstage's mirror uses the fixed `@retscr`
name through `c.retscroff` SSoT (post-#14 b401cce) and never
touches labelseq for the scratch alloc. Result: cstage's labelseq
runs 1 ahead of wwstage in every fn with a struct/tagged return,
so every subsequent ct_N / ce_N / end_N branch label diverged by
the same offset.

Class A byte-id drift, previously latent. Filed STATUS-3 #15 —
promoted to bootstrap-blocking once lib/strings's time.add-chain
and nested-if shapes compounded the cumulative skew past the
993/995 byte-id threshold. The label name was never emitted (it's
a hidden local-table key); only the labelseq side-effect mattered.

Polarity catalog: cstage OVER — extra mklabel per fn. Convergence
cstage → wwstage's fixed-name SSoT per rule 10 (wwstage's pattern
is the cleanup target; STATUS-3 #14 already enforced single-slot
@retscr on both stages, this commit aligns the *name source* too).

Site 2 (struct/tagged @retscr in cgreturn) is the actively-tripping
site that reproduces in the in-tree corpus (lib/strings's time.add
chain). Site 1 is preventive symmetry per rule 10 — its sentinel
flip is masked by pre-existing pre-existing #20/#21 struct-widen
offset latents, documented inline.

Filed follow-up (NOT in scope here): #26 graduate the other
hidden-name mklabel sites (tagscr, tagbase, argscr, idxscr) to
@-prefix SSoT. Same family but per-site allocations, structurally
bigger; belongs with STATUS-4 task #1 variant-widen consolidation
refactor.

Tests:
  - 725_nested_if_labels pins cstage vs wwstage cmp -s byte-id on
    a canonical struct-return row mirroring lib/time.add shape
    (multi-arm if + struct {sec,nsec: i64} return). This row
    actually exercises mklabel site 2; the prior worker draft used
    a scalar-widen path that bypassed both mklabel sites.

97/97 ok. 995_self_rebuild stays green (ww2==ww3==ww4 byte-id).
This commit is contained in:
2026-05-18 03:15:40 +09:00
parent c893c4bc37
commit 987391bd12
3 changed files with 180 additions and 7 deletions

View File

@@ -253,6 +253,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \
$(BIN)/test_composite_call_arg \ $(BIN)/test_composite_call_arg \
$(BIN)/test_composite_call_arg_run \ $(BIN)/test_composite_call_arg_run \
$(BIN)/test_letdecl_zeroinit \ $(BIN)/test_letdecl_zeroinit \
$(BIN)/test_nested_if_labels \
$(BIN)/test_param_shadow_mod \ $(BIN)/test_param_shadow_mod \
$(BIN)/test_localoff_scope \ $(BIN)/test_localoff_scope \
$(BIN)/test_cast_enum_movl \ $(BIN)/test_cast_enum_movl \
@@ -572,6 +573,10 @@ $(BIN)/test_letdecl_zeroinit: test/wcc/724_letdecl_zeroinit.c \
$(BIN)/w6c $(BIN)/w6c_ww | $(BIN) $(BIN)/w6c $(BIN)/w6c_ww | $(BIN)
$(CC) $(CFLAGS) -o $@ $< $(CC) $(CFLAGS) -o $@ $<
$(BIN)/test_nested_if_labels: test/wcc/725_nested_if_labels.c \
$(BIN)/w6c $(BIN)/w6c_ww | $(BIN)
$(CC) $(CFLAGS) -o $@ $<
$(BIN)/test_use_promote_alias: test/wcc/699_use_promote_alias.c \ $(BIN)/test_use_promote_alias: test/wcc/699_use_promote_alias.c \
$(BIN)/ww $(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \ $(BIN)/ww $(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \
$(LIB)/libwwrt.a | $(BIN) $(LIB)/libwwrt.a | $(BIN)

View File

@@ -6531,9 +6531,21 @@ cgstmt(Cg *c, Node *n, Local **locals, int *frame)
if (cg_retscr != 0) { if (cg_retscr != 0) {
scr = cg_retscr; scr = cg_retscr;
} else { } else {
const char *scrn = mklabel(c, "retscr"); /* STATUS-3 #15: use the fixed
scr = local_alloc(c, locals, scrn, * "@retscr" SSoT name (mirrors
sz, cg_frame); * wwstage's c.retscroff pattern in
* selfhost/cmd/wcc/cgen.ww). Pre-
* fix mklabel(c, "retscr") consumed
* one labelseq counter slot per
* function with a tagged return,
* pushing every subsequent ct/ce/
* else/end label 1 ahead of wwstage.
* Site 1 sentinel masked by latent
* struct-widen offset divergence
* (STATUS-3 #20/#21); fix is
* preventive symmetry per rule 10. */
scr = local_alloc(c, locals,
"@retscr", sz, cg_frame);
cg_retscr = scr; cg_retscr = scr;
} }
ins2(c, A_XORQ, areg(D_AX), areg(D_AX)); ins2(c, A_XORQ, areg(D_AX), areg(D_AX));
@@ -6674,14 +6686,15 @@ cgstmt(Cg *c, Node *n, Local **locals, int *frame)
|| n->lhs->kind == N_STRUCTLIT)) { || n->lhs->kind == N_STRUCTLIT)) {
int sz = (int)rt->size; int sz = (int)rt->size;
/* Single-slot @retscr (#14): see tagged arm /* Single-slot @retscr (#14): see tagged arm
* above for rationale. */ * above for rationale. STATUS-3 #15: fixed
* "@retscr" name avoids bumping labelseq;
* mirrors wwstage's c.retscroff SSoT. */
int scr; int scr;
if (cg_retscr != 0) { if (cg_retscr != 0) {
scr = cg_retscr; scr = cg_retscr;
} else { } else {
const char *scrn = mklabel(c, "retscr"); scr = local_alloc(c, locals, "@retscr",
scr = local_alloc(c, locals, scrn, 24, 24, cg_frame);
cg_frame);
cg_retscr = scr; cg_retscr = scr;
} }
ins2(c, A_XORQ, areg(D_AX), areg(D_AX)); ins2(c, A_XORQ, areg(D_AX), areg(D_AX));

View File

@@ -0,0 +1,155 @@
/*
* 725_nested_if_labels — sentinel for STATUS-3 #15. Cstage's
* `cgen.c`-side per-function ≤24B-struct return path called
* `mklabel(c, "retscr")`, consuming one `labelseq` counter slot per
* function that returns a struct. Wwstage's equivalent emits a
* fixed-name `@retscr` SSoT (mirrors `c.retscroff` in
* selfhost/cmd/wcc/cgen.ww). Net effect pre-fix: every subsequent
* control-flow label (`ct_N`, `ce_N`, `else_N`, `end_N`) in any such
* function was 1 ahead in cstage.
*
* Filed STATUS-3 #15. Previously byte-id drift only; 995_self_rebuild
* masked because the bootstrap main.combined.ww's drift was within
* the threshold the test happens to allow. lib/strings (commit 1) +
* lib/time.add nested-if shapes compounded the skew past that
* threshold once worker-strings-v2 attempted the landing — promotes
* #15 from latent to bootstrap-blocking.
*
* Fix aligns cstage DOWN to wwstage's single-slot SSoT (rule 10):
* cgen.c's two `mklabel(c, "retscr")` sites now use the fixed string
* "@retscr" passed to local_alloc. Labels never appear in emitted
* asm — only the labelseq side-effect mattered, and dropping it
* lets cstage's label numbering match wwstage's.
*
* Row pins asm `cmp -s` byte-id between stages on the canonical
* time.add shape: ≤24B struct return + nested if/if/fallthrough.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/wait.h>
static int
runwait(const char *cmd)
{
int rc = system(cmd);
if (rc == -1) return -1;
if (WIFEXITED(rc)) return WEXITSTATUS(rc);
return -1;
}
struct row { const char *label; const char *src; };
static const struct row rows[] = {
/* Plain struct return + nested-if (time.add shape — struct
* `instant` return, multi-arm if/if/fallthrough). Each return
* site hits cgreturn's ≤24B-struct arm which allocates
* @retscr; pre-fix mklabel(c, "retscr") bumped labelseq, post-
* fix the fixed name leaves labelseq alone so cstage's
* ct/ce/end numbering matches wwstage. */
{ "struct_return_then_nested_if",
"type instant = struct { sec: i64, nsec: i64 };\n"
"export fn add(i: instant, x: i64) instant = {\n"
" let r: instant;\n"
" if (x == 0i64) {\n"
" r.sec = i.sec; r.nsec = i.nsec;\n"
" return r;\n"
" };\n"
" if (x > 0i64) {\n"
" r.sec = i.sec + x; r.nsec = i.nsec;\n"
" return r;\n"
" };\n"
" r.sec = i.sec - 1i64; r.nsec = i.nsec + x;\n"
" return r;\n"
"};\n" },
};
static int
emit_s(const char *w6c, const struct row *r, int i, char *out_s, size_t cap)
{
char src[64], cmd[1024];
snprintf(src, sizeof src, "/tmp/nil_asm_%d_%d.ww", getpid(), i);
snprintf(out_s, cap, "/tmp/nil_asm_%d_%d_%s.s",
getpid(), i, w6c[strlen(w6c) - 1] == 'w' ? "ww" : "c");
FILE *f = fopen(src, "wb");
if (!f) return -1;
fputs(r->src, f);
fclose(f);
snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null", w6c, out_s, src);
int rc = runwait(cmd);
unlink(src);
return rc;
}
int
main(void)
{
const char *bin = getenv("BIN");
if (!bin) bin = "out/bin";
char absbin[512];
if (bin[0] != '/') {
char cwd[256];
if (getcwd(cwd, sizeof cwd) == NULL) return 1;
snprintf(absbin, sizeof absbin, "%s/%s", cwd, bin);
bin = absbin;
}
char w6c[640], w6c_ww[640];
snprintf(w6c, sizeof w6c, "%s/w6c", bin);
snprintf(w6c_ww, sizeof w6c_ww, "%s/w6c_ww", bin);
int have_ww = (access(w6c_ww, X_OK) == 0);
int n = (int)(sizeof rows / sizeof rows[0]);
int total = 0, fail = 0;
for (int i = 0; i < n; i++) {
char cs_path[128], ws_path[128];
if (emit_s(w6c, &rows[i], i, cs_path, sizeof cs_path) != 0) {
fprintf(stderr,
"nested_if_labels[cstage][%s]: w6c failed\n",
rows[i].label);
fail++; total++; continue;
}
total++;
if (!have_ww) { unlink(cs_path); continue; }
if (emit_s(w6c_ww, &rows[i], i, ws_path, sizeof ws_path) != 0) {
fprintf(stderr,
"nested_if_labels[wwstage][%s]: w6c_ww failed\n",
rows[i].label);
fail++; total++;
unlink(cs_path);
continue;
}
total++;
/* Byte-id between stages — the fix-pin. Pre-fix cstage's
* `add_ct_N` was 1 ahead of wwstage's; post-fix they match. */
total++;
char cmd[512];
snprintf(cmd, sizeof cmd, "cmp -s %s %s", cs_path, ws_path);
if (runwait(cmd) != 0) {
fprintf(stderr,
"nested_if_labels[%s]: cstage vs wwstage asm differs\n",
rows[i].label);
fail++;
}
unlink(cs_path); unlink(ws_path);
}
if (fail) {
fprintf(stderr,
"nested_if_labels: %d/%d fixtures failed\n", fail, total);
return 1;
}
printf("nested_if_labels: %d/%d ok\n", total, total);
return 0;
}