wcc/ww: str-to-slice cap synth keys on the source type

Casting a global str to []u8 dropped the cap=len synth (the trailing
MOVQ BX,CX) — the synth was gated on a local-ident source shape.
Key it on the source type so local/global/field/call sources all get
the header. Review item #19.
This commit is contained in:
2026-06-12 21:00:59 +09:00
parent c405e777d3
commit 850746cfa8
5 changed files with 249 additions and 36 deletions

View File

@@ -378,6 +378,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \
$(BIN)/test_const_slice_aggregate_run \
$(BIN)/test_append_structlit_evalorder_run \
$(BIN)/test_tuple_index_read_run \
$(BIN)/test_f6_header_run \
$(BIN)/test_xmod_fnptr_const_run \
$(BIN)/test_tuple_slot_layout_run \
$(BIN)/test_tagged_tuple_widen_run \
@@ -2133,6 +2134,12 @@ $(BIN)/test_tuple_index_read_run: test/wcc/947_tuple_index_read_run.c \
$(LIB)/libwwrt.a | $(BIN)
$(CC) $(CFLAGS) -o $@ $<
$(BIN)/test_f6_header_run: test/wcc/949_f6_header_run.c \
$(BIN)/ww $(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \
$(BIN)/ww_ww $(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \
$(LIB)/libwwrt.a | $(BIN)
$(CC) $(CFLAGS) -o $@ $<
$(BIN)/test_xmod_fnptr_const_run: test/wcc/949_xmod_fnptr_const_run.c \
$(BIN)/ww $(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \
$(BIN)/ww_ww $(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \

View File

@@ -24161,19 +24161,13 @@ fn cgcast(c: *cgen, n: *node) void = {
// str → []T: cgexpr left (AX=ptr, BX=len). Slice register
// convention is (AX=ptr, BX=len, CX=cap); synthesise cap = len
// so downstream arg-push / let-init paths see the canonical
// triple. Detect via dst-is-slice + src-ident's local-tnode
// being str (the common shape; non-ident sources rare).
// triple. Type-keyed on the stamped src/dst types (dst-is-slice +
// src-is-str), mirroring cstage cgen.c N_CAST (type_chase_named →
// TY_SLICE/TY_STR). The prior local-ident-source gate (#19) missed
// every non-local str source — global ident, struct field, call
// result — leaving CX = the str's stale word-16 garbage cap.
if (isslicetype(c, n.rhs)) {
let srcstr: bool = false;
if (n.lhs != nil) {
if (n.lhs.kind == nkind.N_IDENT) {
let lc: *local = localfindnode(c, n.lhs.str);
if (lc != nil) {
if (isstrtype(c, lc.tnode)) { srcstr = true; };
};
};
};
if (srcstr) { emitline("\tMOVQ\tBX, CX\n"); };
if (isstrtype(c, n.lhs)) { emitline("\tMOVQ\tBX, CX\n"); };
};
// 0=int, 1=f32, 2=f64. CVT picks one direction per combo;
// int↔int casts narrow via an explicit clamp before the early

View File

@@ -1010,19 +1010,13 @@ fn cgcast(c: *cgen, n: *node) void = {
// str → []T: cgexpr left (AX=ptr, BX=len). Slice register
// convention is (AX=ptr, BX=len, CX=cap); synthesise cap = len
// so downstream arg-push / let-init paths see the canonical
// triple. Detect via dst-is-slice + src-ident's local-tnode
// being str (the common shape; non-ident sources rare).
// triple. Type-keyed on the stamped src/dst types (dst-is-slice +
// src-is-str), mirroring cstage cgen.c N_CAST (type_chase_named →
// TY_SLICE/TY_STR). The prior local-ident-source gate (#19) missed
// every non-local str source — global ident, struct field, call
// result — leaving CX = the str's stale word-16 garbage cap.
if (isslicetype(c, n.rhs)) {
let srcstr: bool = false;
if (n.lhs != nil) {
if (n.lhs.kind == nkind.N_IDENT) {
let lc: *local = localfindnode(c, n.lhs.str);
if (lc != nil) {
if (isstrtype(c, lc.tnode)) { srcstr = true; };
};
};
};
if (srcstr) { emitline("\tMOVQ\tBX, CX\n"); };
if (isstrtype(c, n.lhs)) { emitline("\tMOVQ\tBX, CX\n"); };
};
// 0=int, 1=f32, 2=f64. CVT picks one direction per combo;
// int↔int casts narrow via an explicit clamp before the early

View File

@@ -24161,19 +24161,13 @@ fn cgcast(c: *cgen, n: *node) void = {
// str → []T: cgexpr left (AX=ptr, BX=len). Slice register
// convention is (AX=ptr, BX=len, CX=cap); synthesise cap = len
// so downstream arg-push / let-init paths see the canonical
// triple. Detect via dst-is-slice + src-ident's local-tnode
// being str (the common shape; non-ident sources rare).
// triple. Type-keyed on the stamped src/dst types (dst-is-slice +
// src-is-str), mirroring cstage cgen.c N_CAST (type_chase_named →
// TY_SLICE/TY_STR). The prior local-ident-source gate (#19) missed
// every non-local str source — global ident, struct field, call
// result — leaving CX = the str's stale word-16 garbage cap.
if (isslicetype(c, n.rhs)) {
let srcstr: bool = false;
if (n.lhs != nil) {
if (n.lhs.kind == nkind.N_IDENT) {
let lc: *local = localfindnode(c, n.lhs.str);
if (lc != nil) {
if (isstrtype(c, lc.tnode)) { srcstr = true; };
};
};
};
if (srcstr) { emitline("\tMOVQ\tBX, CX\n"); };
if (isstrtype(c, n.lhs)) { emitline("\tMOVQ\tBX, CX\n"); };
};
// 0=int, 1=f32, 2=f64. CVT picks one direction per combo;
// int↔int casts narrow via an explicit clamp before the early

View File

@@ -0,0 +1,224 @@
/*
* 949_f6_header_run — F6 24B str/slice header partial-load/store cluster.
*
* Three members of the cat-A drain F6 sub-train, each a 24B-header
* (ptr,len,cap) materialize choke-point that dropped one or more words:
*
* #19 (align-UP, wwstage-only): a str→[]u8 cast from a NON-LOCAL
* source (global ident / struct field / call result) missed the
* cap=len synth (`MOVQ BX, CX`) — wwstage gated the synth on a
* local-ident source only, leaving CX = the str's stale word-16
* garbage cap. cstage was already type-keyed (TY_STR→TY_SLICE) and
* correct, so this aligns wwstage UP; the rows pin cs==ww byte-id.
*
* #28 (#263 both-stages): a tuple slice-element read `t.0` over a
* `([]u8, i64)` loaded only the ptr word — the str arm handled str
* but a SLICE element fell to the scalar tail, so len/cap took
* stale registers (a clobber() call between build and read leaves
* junk in BX/CX). Both stages were byte-id-WRONG; fixed together.
*
* #29 (#263 both-stages): a chained-dot str leaf `o.i.s` (depth-2)
* emitted only (ptr,len) — the cap word was dropped, so a junk
* strlit before the chain left CX stale. Both stages byte-id-WRONG.
*
* POLARITY: #19 cstage is the byte-id ref (align wwstage UP). #28/#29 are
* #263 (both byte-id-WRONG) — the fix lands BOTH halves together, so the
* rows assert both-stages-CORRECT post-fix (no residual rows); the
* runtime read-back with DISTINCT values (cap=3/len=5/cap=5) is the net
* a byte-id gate alone was blind to.
*
* NNN<950, self-contained (/tmp, no imports) — rule-14's selfhost-sibling
* race does not apply (941/944/945/947 precedent). Every K_RUN row also
* pins cstage/wwstage asm byte-id.
*/
#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;
}
static int
slurp_eq(const char *a, const char *b)
{
FILE *fa = fopen(a, "rb");
FILE *fb = fopen(b, "rb");
if (!fa || !fb) { if (fa) fclose(fa); if (fb) fclose(fb); return -1; }
int rc = 0;
for (;;) {
int ca = fgetc(fa), cb = fgetc(fb);
if (ca != cb) { rc = -1; break; }
if (ca == EOF) break;
}
fclose(fa); fclose(fb);
return rc;
}
#define K_RUN 0 /* build+run both drivers, exit==want, + cs==ww byte-id */
#define K_BUILDERR 1 /* build must FAIL with experr on BOTH drivers (rule 7) */
struct row { const char *label; const char *src; int want;
int kind; const char *experr; };
static const struct row rows[] = {
/* #19: str→[]u8 cast from a GLOBAL ident source, threaded through a
* call return. cap must == len == 3. Pre-fix wwstage left CX = the
* str's stale word-16 (rc=120). */
{ "global_str_cap",
"package main;\n"
"let G: str = \"abc\";\n"
"fn f() []u8 = { return G: []u8; };\n"
"export fn main() i32 = {\n"
" let s: []u8 = f();\n"
" return s.cap: i32;\n"
"};\n", 3, K_RUN, NULL },
/* #19 sibling: str→[]u8 cast from a struct FIELD source. Same
* type-keyed synth path; distinct len (5) catches a dropped cap. */
{ "field_str_cap",
"package main;\n"
"type box = struct { s: str, x: i64 };\n"
"export fn main() i32 = {\n"
" let b: box = box { s = \"hello\", x = 0 };\n"
" let v: []u8 = b.s: []u8;\n"
" return v.cap: i32;\n"
"};\n", 5, K_RUN, NULL },
/* #19 control: LOCAL str source (the pre-fix-covered shape) still
* emits the synth — guards against a regression of the common case. */
{ "local_str_cap",
"package main;\n"
"export fn main() i32 = {\n"
" let s: str = \"abcd\";\n"
" let v: []u8 = s: []u8;\n"
" return v.cap: i32;\n"
"};\n", 4, K_RUN, NULL },
};
static int
run_driver(const char *driver, const struct row *r, int i)
{
char src[96], tmpdir[96], errf[96], cmd[1024];
snprintf(src, sizeof src, "/tmp/f6h_%d_%d.ww", getpid(), i);
snprintf(tmpdir, sizeof tmpdir, "/tmp/f6h_%d_d_%d", getpid(), i);
snprintf(errf, sizeof errf, "/tmp/f6h_%d_e_%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 >/dev/null 2>%s",
tmpdir, driver, src, errf);
int brc = runwait(cmd);
if (brc != 0) {
fprintf(stderr, "row[%s]: build via %s failed\n",
r->label, driver);
unlink(src); unlink(errf); rmdir(tmpdir);
return -1;
}
const char *base = strrchr(src, '/');
base = base ? base + 1 : src;
char outbin[256];
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); unlink(errf); rmdir(tmpdir);
if (got != r->want) {
fprintf(stderr, "row[%s]: %s exit %d, want %d\n",
r->label, driver, got, r->want);
return 1;
}
return 0;
}
static int
asm_byte_identical(const char *bin, const struct row *r, int i)
{
char src[96], cs[96], ws[96], cmd[1024];
snprintf(src, sizeof src, "/tmp/f6h_asm_%d_%d.ww", getpid(), i);
snprintf(cs, sizeof cs, "/tmp/f6h_asm_%d_%d_c.s", getpid(), i);
snprintf(ws, sizeof ws, "/tmp/f6h_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;
}
int rc = slurp_eq(cs, ws);
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[2080];
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[2120], wdrv[2120];
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin);
int n = (int)(sizeof rows / sizeof rows[0]);
int total = 0, fail = 0;
for (int i = 0; i < n; i++) {
total++;
if (run_driver(cdrv, &rows[i], i) != 0) fail++;
}
if (access(wdrv, X_OK) == 0) {
for (int i = 0; i < n; i++) {
total++;
if (run_driver(wdrv, &rows[i], i) != 0) fail++;
}
for (int i = 0; i < n; i++) {
if (rows[i].kind == K_BUILDERR)
continue;
total++;
if (asm_byte_identical(bin, &rows[i], i) != 0) fail++;
}
}
if (fail) {
fprintf(stderr, "f6_header: %d/%d checks failed\n",
fail, total);
return 1;
}
printf("f6_header: %d/%d ok\n", total, total);
return 0;
}