Files
ww/test/wcc/827_str_eq.c
Hojun-Cho 5dd239d01e wcc/cgen: #146 wwstage str ==/!= via rt_streq, not ptr-only CMPQ (the #154 ww-twin)
wwstage compiled str ==/!= as a single CMPQ on the eager-eval'd ptr word
(len ignored), so two distinct-pointer equal-content strings compared
unequal. cstage was already correct (CALLs rt_streq, the #154 cbinop fix).
The wwstage cgbin had no str-awareness -- every comparison fell to the
generic CMPQ tail; the #154 fix was never mirrored.

wwstage-only: a cgstreqpush helper + a str ==/!= branch at the top of
cgbin (before the generic eval collapses the header), byte-matching cstage
cbinop:4564-4623 -- push rhs/lhs (len,ptr), POPQ DI/SI/DX/CX, CALL
rt_streq, XORQ $1 for !=. Gated on typeisstr (= cstage node_isstr, which
also catches module-global str idents). cstage cgen unchanged (w6c md5
unchanged). The str== .s is byte-identical cs==ww for local, global,
aliased, chained, and condition operands.

Graduates 3 lib byte-id pins (test/wcc/989_lib_byteid #59.1 asciitest,
#59.11 toktest, #59.12 asttest) M_DIVERGE->M_ID -- they used == on str and
were pinned divergent because of this bug; now byte-identical. byte-id
990-997 8/8. test/wcc/827 table-driven.
2026-06-08 23:49:16 +09:00

316 lines
9.2 KiB
C

/*
* 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 <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; 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 src[64], tmpdir[64], cmd[1024];
snprintf(src, sizeof src, "/tmp/seq_%d_%d.ww", getpid(), i);
snprintf(tmpdir, sizeof tmpdir, "/tmp/seq_%d_d_%d", getpid(), i);
FILE *f = fopen(src, "wb");
if (!f) return -1;
fputs(r->src, f);
fclose(f);
mkdir(tmpdir, 0755);
snprintf(cmd, sizeof cmd, "cd %s && %s build %s 2>/dev/null",
tmpdir, driver, src);
if (runwait(cmd) != 0) {
fprintf(stderr, "row[%s]: build via %s failed\n",
r->label, driver);
unlink(src); rmdir(tmpdir);
return -1;
}
const char *base = strrchr(src, '/');
base = base ? base + 1 : src;
char outbin[128];
snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base);
char *dot = strrchr(outbin, '.');
if (dot && strcmp(dot, ".ww") == 0) *dot = '\0';
int got = runwait(outbin);
unlink(src); unlink(outbin); rmdir(tmpdir);
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;
}