/* * 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 #include #include #include #include #include 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; }