/* * 827_str_eq — str `==` / `!=` is a CONTENT compare (rt_streq), not a * pointer compare. #146 (the ww-twin of the #154 cstage fix). * * THE BUG (wwstage-only; cstage was already correct via #154): wwstage * cgbin (selfhost/cmd/wcc/cgenexpr.ww) had NO str-awareness, so every * `==`/`!=` fell to the generic comparison tail — a single `CMPQ BX, AX` * on the eager-eval'd registers. For a str, eager-eval collapses the * 3-word header to its ptr word, so the compare became ptr-vs-ptr: two * DISTINCT-pointer equal-content strings answered FALSE on wwstage while * cstage (which CALLs rt_streq) answered TRUE. THE divergence row is * eq_dup_true: `let a="abc"; let b=strings.dup("abc"); a==b`. * * THE FIX: cgbin now has a str ==/!= branch at the top (before the * generic eval), mirroring cstage cgen.c cbinop:4564-4623 — push rhs * then lhs (len, ptr each), POPQ DI/SI/DX/CX = a.ptr,a.len,b.ptr,b.len * (rt/streq.s ABI), CALL rt_streq -> AX in {0,1}, XORQ $1 for !=. * * Two row sets: * runrows — built+RUN on BOTH drivers (need strings.dup for a * distinct-pointer copy, so they `import strings` and build * via `ww` which links the lib). These pin RUNTIME * correctness; eq_dup_true/ne_dup_false are mutation-sane * (pre-fix wwstage gave the wrong ptr-compare answer). * asmrows — import-free single files run straight through w6c / * w6c_ww for the BYTE-ID headline (w6c compiles one file and * can't resolve strings.dup, so the import rows can't be * byte-id'd directly; these import-free str==/!= sources * exercise the identical cgbin branch and pin cs==ww `.s`). * * runrows | want * -------------------------------------------------+------ * eq_dup_true a=="abc", b=dup("abc"); a==b | 1 (THE bug) * ne_dup_false a=="abc", b=dup("abc"); a!=b | 0 (!= twin) * diff_content "abc"=="abd" | 0 (control) * eq_self a="x"; a==a | 1 (same ptr) * empty a="", b=dup(""); a==b | 1 (len-0 edge) */ #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; int want; }; static const struct row runrows[] = { /* eq_dup_true — THE regression pin: distinct pointer, equal * content. Pre-fix wwstage ptr-compared -> 0 (WRONG); cstage * CALLs rt_streq -> 1. */ { "eq_dup_true", "package main;\n" "import strings;\n" "export fn main() i32 = {\n" "\tlet a: str = \"abc\";\n" "\tlet b: str = strings.dup(\"abc\");\n" "\tif (a == b) { return 1; };\n" "\treturn 0;\n" "};\n", 1 }, /* ne_dup_false — the != twin: distinct pointer, equal content, so * `!=` is FALSE. Pre-fix wwstage ptr-compared -> != true (WRONG). */ { "ne_dup_false", "package main;\n" "import strings;\n" "export fn main() i32 = {\n" "\tlet a: str = \"abc\";\n" "\tlet b: str = strings.dup(\"abc\");\n" "\tif (a != b) { return 1; };\n" "\treturn 0;\n" "};\n", 0 }, /* diff_content — distinct content; both stages agree (control). */ { "diff_content", "package main;\n" "export fn main() i32 = {\n" "\tif (\"abc\" == \"abd\") { return 1; };\n" "\treturn 0;\n" "};\n", 0 }, /* eq_self — same pointer, equal content; both stages agree. */ { "eq_self", "package main;\n" "export fn main() i32 = {\n" "\tlet a: str = \"x\";\n" "\tif (a == a) { return 1; };\n" "\treturn 0;\n" "};\n", 1 }, /* empty — len-0 edge: rt_streq must match two empty strings. */ { "empty", "package main;\n" "import strings;\n" "export fn main() i32 = {\n" "\tlet a: str = \"\";\n" "\tlet b: str = strings.dup(\"\");\n" "\tif (a == b) { return 1; };\n" "\treturn 0;\n" "};\n", 1 }, }; /* Import-free sources for the byte-id pass (w6c compiles a single file * and can't resolve cross-module strings.dup). Each exercises the cgbin * str ==/!= branch; the `.s` must be identical cstage vs wwstage. */ static const struct row asmrows[] = { /* be_eq_local — local str == local str (ident operand load). */ { "be_eq_local", "package main;\n" "export fn main() i32 = {\n" "\tlet a: str = \"abc\";\n" "\tlet b: str = \"abd\";\n" "\tif (a == b) { return 1; };\n" "\treturn 0;\n" "};\n", 0 }, /* be_ne_local — the != twin (pins the XORQ $1, AX). */ { "be_ne_local", "package main;\n" "export fn main() i32 = {\n" "\tlet a: str = \"abc\";\n" "\tlet b: str = \"abd\";\n" "\tif (a != b) { return 1; };\n" "\treturn 0;\n" "};\n", 1 }, /* be_eq_strlit — strlit == strlit (non-ident cgexpr operand). */ { "be_eq_strlit", "package main;\n" "export fn main() i32 = {\n" "\tif (\"abc\" == \"abc\") { return 1; };\n" "\treturn 0;\n" "};\n", 1 }, /* be_eq_global — module-GLOBAL str == global str. This is the * raison d'etre of the typeisstr gate (vs the spec's nodeisstr, * whose N_IDENT arm keys on localfindnode and returns false for a * global, leaving a latent cs!=ww hole). The cgstreqpush global arm * (LEAQ name(SB)) must byte-match cstage's #154 let_islet branch. */ { "be_eq_global", "package main;\n" "let g: str = \"abc\";\n" "let h: str = \"abd\";\n" "export fn main() i32 = {\n" "\tif (g == h) { return 1; };\n" "\treturn 0;\n" "};\n", 0 }, }; static int run_driver(const char *driver, const struct row *r, int i) { char tmpdir[64], src[128], outbin[128], rmcmd[160], cmd[1024]; snprintf(tmpdir, sizeof tmpdir, "/tmp/seq_%d_d_%d", getpid(), i); mkdir(tmpdir, 0755); snprintf(src, sizeof src, "%s/seq_%d_%d.ww", tmpdir, getpid(), i); snprintf(outbin, sizeof outbin, "%s/seq_%d_%d", tmpdir, getpid(), i); snprintf(rmcmd, sizeof rmcmd, "rm -rf %s", tmpdir); FILE *f = fopen(src, "wb"); if (!f) { runwait(rmcmd); return -1; } fputs(r->src, f); fclose(f); snprintf(cmd, sizeof cmd, "%s build -o %s %s 2>/dev/null", driver, outbin, src); if (runwait(cmd) != 0) { fprintf(stderr, "row[%s]: build via %s failed\n", r->label, driver); runwait(rmcmd); return -1; } int got = runwait(outbin); runwait(rmcmd); return got; } /* asm_byte_identical — w6c vs w6c_ww .s for the same source must match. */ static int asm_byte_identical(const char *bin, const struct row *r, int i) { char src[64], cs[64], ws[64], cmd[1024]; snprintf(src, sizeof src, "/tmp/seq_asm_%d_%d.ww", getpid(), i); snprintf(cs, sizeof cs, "/tmp/seq_asm_%d_%d_c.s", getpid(), i); snprintf(ws, sizeof ws, "/tmp/seq_asm_%d_%d_w.s", getpid(), i); FILE *f = fopen(src, "wb"); if (!f) return -1; fputs(r->src, f); fclose(f); snprintf(cmd, sizeof cmd, "%s/w6c -o %s %s 2>/dev/null", bin, cs, src); if (runwait(cmd) != 0) { fprintf(stderr, "row[%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, "row[%s]: w6c_ww errored\n", r->label); unlink(src); unlink(cs); return -1; } FILE *fc = fopen(cs, "rb"); FILE *fw = fopen(ws, "rb"); int rc = 0; if (!fc || !fw) { rc = -1; } else { for (;;) { int a = fgetc(fc); int b = fgetc(fw); if (a != b) { rc = -1; break; } if (a == EOF) break; } } if (fc) fclose(fc); if (fw) fclose(fw); if (rc != 0) fprintf(stderr, "row[%s]: cstage vs wwstage asm differs\n", r->label); unlink(src); unlink(cs); unlink(ws); return rc; } int main(void) { const char *bin = getenv("BIN"); if (!bin) bin = "out/bin"; char absbin[1024]; if (bin[0] != '/') { char cwd[1024]; if (getcwd(cwd, sizeof cwd) == NULL) return 1; snprintf(absbin, sizeof absbin, "%s/%s", cwd, bin); bin = absbin; } char cdrv[1024]; snprintf(cdrv, sizeof cdrv, "%s/ww", bin); char wdrv[1024]; snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin); struct { const char *name; const char *path; int gated_on_existence; } drivers[] = { { "cstage", cdrv, 0 }, { "wwstage", wdrv, 1 }, { NULL, NULL, 0 }, }; int nrun = (int)(sizeof runrows / sizeof runrows[0]); int nasm = (int)(sizeof asmrows / sizeof asmrows[0]); int total = 0, fail = 0; for (int d = 0; drivers[d].name; d++) { if (drivers[d].gated_on_existence && access(drivers[d].path, X_OK) != 0) { fprintf(stderr, "str_eq: skip %s (no %s)\n", drivers[d].name, drivers[d].path); continue; } for (int i = 0; i < nrun; i++) { int got = run_driver(drivers[d].path, &runrows[i], i); total++; if (got != runrows[i].want) { fprintf(stderr, "str_eq[%s][%s]: exit=%d want=%d\n", drivers[d].name, runrows[i].label, got, runrows[i].want); fail++; } } } if (access(wdrv, X_OK) == 0) { for (int i = 0; i < nasm; i++) { total++; if (asm_byte_identical(bin, &asmrows[i], i) != 0) fail++; } } if (fail) { fprintf(stderr, "str_eq: %d/%d fixtures failed\n", fail, total); return 1; } printf("str_eq: %d/%d ok\n", total, total); return 0; }