wcc/ww: emit correct (tag,payload) for a tagged value in static-init (#19)

A tagged-union value nested in module-level array/struct static-init
mis-emitted in both stages: the lit-bytes emitters had no TY_TAGGED
arm, so a tagged element/field fell to the int path and the payload
landed in the TAG word -- match then read the wrong variant. The
zero-placeholder idiom (today the only way to declare a tagged global:
zero-init in static, write at runtime) was correct only by accident
(int-variant zero folds to (0,0), which equals the right (tag0,0)).

Extract a raw-byte core emittaggedbytes/emit_tagged_bytes -- variant
tag@+0, int payload@+8, zero-pad to the slot size; no directive, no
offset, no reloc -- and refactor the scalar tagged emitter to delegate
to it (byte-id-neutral). Add a TY_TAGGED member branch to the array
and struct lit-bytes emitters (both stages) that calls the core at the
existing full-slot stride, before the int fallthrough. Zero stays
(0,0) byte-identical; a non-zero element/field now emits (tag,payload)
correctly.

A wide (str/slice) or struct/>8B payload nested in an aggregate needs
reloc-at-member-offset machinery the aggregate byte-emitters don't
have, so it is loud-rejected (rule 7), deferred to #30; the existing
slice-of-tagged static-init reject is unchanged.

Regenerates the w6c and wwdump combined.ww. Table-driven 843 test:
non-zero array/struct (pre-fix returned the wrong variant), the
non-tag-0 bool-variant edge, byte-id-neutral zero-placeholder rows,
and wide-payload reject rows; each run row also pins cs-vs-ww asm.
This commit is contained in:
2026-06-14 18:11:28 +09:00
parent 34c1051a63
commit 9767ff8fff
6 changed files with 831 additions and 53 deletions

View File

@@ -0,0 +1,371 @@
/*
* 843_tagged_staticinit — #19 option A (rob's EXTRACT ruling). A tagged-
* union VALUE nested in module-level static-init DATA (an array element or
* a struct field) now emits the correct (tag@+0, payload@+8) box, the same
* SSoT a SCALAR tagged global and a runtime LOCAL box use. Pre-fix the
* nested member fell to the INT emitter, which mis-folded the payload into
* the tag word — cstage (value, value), wwstage (value, 0); NEITHER is
* (tag, value). Both BUILT and ran WRONG.
*
* The fix extracts a shared raw-byte core emit_tagged_bytes / emittaggedbytes
* (tag@0, int payload@8, zero-pad) from the scalar emitter, and the array/
* struct member emitters call it at the full slot stride. A WIDE (str/slice)
* payload nested in an aggregate needs a reloc the raw core cannot place
* mid-directive, and a struct/non-foldable payload has no scalar form —
* both LOUD-REJECT (deferred, task #30; mirror of the slice-of-tagged
* reject). Only zero + int payload is implemented, which is exactly what
* the corpus exercises.
*
* row | shape | kind
* ---------------------+----------------------------------------+------
* array_nonzero | let gs:[2](int|bool)=[42,7]; gs[0] | RUN 42
* array_bool_variant | let gs:[2](int|bool)=[7,true]; gs[1] | RUN 2
* | (NON-tag-0: tag=1,payload=1 — pre-fix |
* | (1,0) read bool=false -> 3) |
* struct_nonzero | let g:box{t:(int|bool)}=box{t=55};g.t | RUN 55
* array_zero_then_set | let gs:[2](int|bool)=[0,0]; gs[0]=42 | RUN 42
* | (zero-placeholder idiom — byte-id- |
* | neutral build, mirrors 841/989) |
* struct_zero_then_set | let g:box{t=0}; g.t=42 | RUN 42
* plain_struct_ok | non-tagged struct static-init | RUN 9
* wide_array_str | let gs:[2](int|str)=["hi",0] | REJECT
* wide_struct_str | let g:sbox{s:(int|str)}=sbox{s="hi"} | REJECT
*
* DISCRIMINATION: pre-fix array_nonzero BUILT and match(gs[0]) returned 1
* (the match fallthrough — the payload 42 sat in the TAG word, matching no
* variant); post-fix it returns 42. array_bool_variant pins the NON-tag-0
* edge (tag != payload word): pre-fix (1,0) read the bool payload as 0
* (false) -> 3; post-fix (1,1) -> true -> 2. RUN rows also assert cstage/
* wwstage asm byte-id (rule 10). The reject diagnostic core text is shared
* across stages (cstage adds the harness "ww: " prefix). NNN<950, self-
* contained (/tmp, no imports).
*/
#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 expect_build; /* 1 = build+run to want_exit; 0 = must REJECT */
int want_exit;
const char *diag; /* reject core substring (NULL for RUN rows) */
};
static const struct row rows[] = {
/* RUN — non-zero int payload in an array element: now correct. */
{ "array_nonzero",
"package main;\n"
"let gs: [2](int | bool) = [42, 7];\n"
"export fn main() int = {\n"
"\tmatch (gs[0]) {\n"
"\tcase let n: int => return n;\n"
"\tcase bool => return 99;\n"
"\t};\n"
"\treturn 1;\n"
"};\n",
1, 42, NULL },
/* RUN — NON-tag-0 edge: a bool element (variant index 1, payload 1).
* Pre-fix the int-fold emitted (1, 0) so the bool payload read as 0
* (false) -> 3; post-fix (1, 1) -> true -> 2. */
{ "array_bool_variant",
"package main;\n"
"let gs: [2](int | bool) = [7, true];\n"
"export fn main() int = {\n"
"\tmatch (gs[1]) {\n"
"\tcase let n: int => return 1;\n"
"\tcase let b: bool => { if (b) { return 2; }; return 3; };\n"
"\t};\n"
"\treturn 4;\n"
"};\n",
1, 2, NULL },
/* RUN — non-zero int payload in a struct field: now correct. */
{ "struct_nonzero",
"package main;\n"
"type box = struct { t: (int | bool), n: int };\n"
"let g: box = box { t = 55, n = 5 };\n"
"fn main() i32 = {\n"
"\tmatch (g.t) {\n"
"\tcase let v: int => return v: i32;\n"
"\tcase bool => return 7;\n"
"\t};\n"
"\treturn 1;\n"
"};\n",
1, 55, NULL },
/* RUN — zero-placeholder array idiom (mirror 989_taggedglobalindex):
* static-init [0,0] is byte-id-neutral, runtime write then matches. */
{ "array_zero_then_set",
"package main;\n"
"let gs: [2](int | bool) = [0, 0];\n"
"export fn main() int = {\n"
"\tgs[0] = 42;\n"
"\tmatch (gs[0]) {\n"
"\tcase let n: int => return n;\n"
"\tcase bool => return 9;\n"
"\t};\n"
"\treturn 1;\n"
"};\n",
1, 42, NULL },
/* RUN — zero-placeholder struct idiom (mirror 841): {t=0} byte-id-
* neutral, runtime write then matches. */
{ "struct_zero_then_set",
"package main;\n"
"type box = struct { t: (int | bool), n: int };\n"
"let g: box = box { t = 0, n = 5 };\n"
"fn main() i32 = {\n"
"\tg.t = 42;\n"
"\tmatch (g.t) {\n"
"\tcase let v: int => return v: i32;\n"
"\tcase bool => return 7;\n"
"\t};\n"
"\treturn 1;\n"
"};\n",
1, 42, NULL },
/* RUN — non-tagged struct static-init still emits + runs. 9. */
{ "plain_struct_ok",
"package main;\n"
"type pt = struct { x: int, y: int };\n"
"let p: pt = pt { x = 5, y = 9 };\n"
"export fn main() int = {\n"
"\treturn p.y;\n"
"};\n",
1, 9, NULL },
/* REJECT — wide (str) payload tagged element in an array static-init:
* the reloc-in-aggregate case is deferred (task #30). */
{ "wide_array_str",
"package main;\n"
"let gs: [2](int | str) = [\"hi\", 0];\n"
"export fn main() int = {\n"
"\treturn 0;\n"
"};\n",
0, 0,
"tagged-union array element static-init needs a zero/int payload" },
/* REJECT — wide (str) payload tagged field in a struct static-init. */
{ "wide_struct_str",
"package main;\n"
"type sbox = struct { s: (int | str) };\n"
"let g: sbox = sbox { s = \"hi\" };\n"
"export fn main() int = {\n"
"\treturn 0;\n"
"};\n",
0, 0,
"tagged-union struct-field" },
};
static int
run_build(const char *driver, const struct row *r, int i)
{
char src[64], tmpdir[64], cmd[1024];
snprintf(src, sizeof src, "/tmp/tsi_%d_%d.ww", getpid(), i);
snprintf(tmpdir, sizeof tmpdir, "/tmp/tsi_%d_d_%d", getpid(), i);
FILE *f = fopen(src, "wb");
if (!f) return -2;
fputs(r->src, f);
fclose(f);
mkdir(tmpdir, 0755);
snprintf(cmd, sizeof cmd, "cd %s && %s build %s 2>/dev/null",
tmpdir, driver, src);
int brc = runwait(cmd);
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 = -1;
if (brc == 0) got = runwait(outbin);
unlink(src); unlink(outbin); rmdir(tmpdir);
return brc == 0 ? got : -1;
}
/* A reject row must (a) fail to build and (b) emit the shared diagnostic
* core text. Returns 0 on the expected reject, -1 otherwise. */
static int
build_should_reject(const char *driver, const struct row *r, int i)
{
char s[64], tmpdir[64], errf[80], cmd[1280];
snprintf(s, sizeof s, "/tmp/tsn_%d_%d.ww", getpid(), i);
snprintf(tmpdir, sizeof tmpdir, "/tmp/tsn_%d_d_%d", getpid(), i);
snprintf(errf, sizeof errf, "/tmp/tsn_%d_%d.err", getpid(), i);
FILE *f = fopen(s, "wb");
if (!f) return -1;
fputs(r->src, f);
fclose(f);
mkdir(tmpdir, 0755);
snprintf(cmd, sizeof cmd, "cd %s && %s build %s 2>%s",
tmpdir, driver, s, errf);
int rc = runwait(cmd);
int have_diag = 0;
FILE *e = fopen(errf, "rb");
if (e) {
char buf[4096];
size_t n = fread(buf, 1, sizeof buf - 1, e);
buf[n] = '\0';
fclose(e);
have_diag = (strstr(buf, r->diag) != NULL);
}
unlink(s); unlink(errf);
const char *base = strrchr(s, '/');
base = base ? base + 1 : s;
char outbin[128];
snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base);
char *dot = strrchr(outbin, '.');
if (dot && strcmp(dot, ".ww") == 0) *dot = '\0';
unlink(outbin);
rmdir(tmpdir);
/* build must NOT succeed AND the shared diagnostic must appear. */
return (rc != 0 && have_diag) ? 0 : -1;
}
/* 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/tsi_asm_%d_%d.ww", getpid(), i);
snprintf(cs, sizeof cs, "/tmp/tsi_asm_%d_%d_c.s", getpid(), i);
snprintf(ws, sizeof ws, "/tmp/tsi_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], wdrv[1024];
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin);
struct { const char *name; const char *drv; int gated; }
drivers[] = {
{ "cstage", cdrv, 0 },
{ "wwstage", wdrv, 1 },
{ NULL, NULL, 0 },
};
int n = (int)(sizeof rows / sizeof rows[0]);
int total = 0, fail = 0;
for (int d = 0; drivers[d].name; d++) {
if (drivers[d].gated && access(drivers[d].drv, X_OK) != 0) {
fprintf(stderr, "tagged_staticinit: skip %s (no %s)\n",
drivers[d].name, drivers[d].drv);
continue;
}
for (int i = 0; i < n; i++) {
total++;
if (rows[i].expect_build) {
int got = run_build(drivers[d].drv, &rows[i], i);
if (got != rows[i].want_exit) {
fprintf(stderr, "tagged_staticinit[%s][%s]: "
"exit=%d want=%d\n", drivers[d].name,
rows[i].label, got, rows[i].want_exit);
fail++;
}
} else {
if (build_should_reject(drivers[d].drv, &rows[i],
100 + i) != 0) {
fprintf(stderr, "tagged_staticinit[%s][%s]: "
"expected a loud reject with \"%s\"\n",
drivers[d].name, rows[i].label, rows[i].diag);
fail++;
}
}
}
}
/* RUN rows must be cstage/wwstage asm byte-id (rule 10). */
if (access(wdrv, X_OK) == 0) {
for (int i = 0; i < n; i++) {
if (!rows[i].expect_build) continue;
total++;
if (asm_byte_identical(bin, &rows[i], i) != 0)
fail++;
}
}
if (fail) {
fprintf(stderr, "tagged_staticinit: %d/%d fixtures failed\n",
fail, total);
return 1;
}
printf("tagged_staticinit: %d/%d ok\n", total, total);
return 0;
}