Files
ww/test/wcc/903_tuple_elem_slice_len_run.c
Hojun-Cho 6acddc3a82 w6c+wwstage: len() of tuple-element slice reads .len not .ptr (#235)
len() special-cased only a plain N_IDENT slice operand (load .len at
BP+off+8) and an array operand (fold $alen); every other shape fell back
to a bare cgexpr(operand), which for a slice leaves AX=.ptr. A tuple-
element read (t.N) loads only AX=.ptr, so len(t.N) on a slice/str tuple
element returned the slice's .ptr word AS its length — a silent
miscompile, gate-blind because the bootstrap never does len() on a
slice-typed tuple element (sibling of the #234/#237 tuple-sret cluster).

Both stages: detect a slice/str tuple-element len() operand and load the
element's .len word directly at BP + element_off + 8, mirroring the
N_IDENT slice arm and the tuple-field-offset walk (element_off sums
preceding element sizes through the type table). Byte-identical asm
(rule 10). The separate tuple-element-read full-header gap is #238; a
leading-scalar mixed-tuple has its own pre-existing sret-layout cs/ww
divergence, filed apart from #235.

Test 903_tuple_elem_slice_len_run: 4 slice/str-only tuple rows (two/
three slices, str+slice, slice+str; distinct lengths), build+run both
drivers + cs==ww byte-id. 12/12 ok.
2026-06-01 18:23:29 +09:00

253 lines
7.6 KiB
C

/*
* 903_tuple_elem_slice_len_run — project #235: `len(t.N)` where t is a tuple
* and t.N is a slice/str-typed element.
*
* The len() builtin special-cased only a PLAIN N_IDENT slice operand (load
* .len at BP+off+8) and an array operand (fold $alen); every other shape fell
* back to a bare `cgexpr(operand)`, which for a slice leaves AX=.ptr. The
* tuple-element read (`t.N`) loads only AX=.ptr (it has no slice-header
* sibling — that gap is the separate #238), so `len(t.N)` returned the slice's
* .ptr word AS the length — a SILENT miscompile, gate-blind because the
* bootstrap never does len() on a slice-typed tuple element.
*
* Fix (both stages, byte-identical per rule 10): for a slice/str tuple-element
* len() operand, load the element's .len word directly at
* BP + tuple_off + element_off + 8 — mirroring the N_IDENT slice arm and the
* tuple-field-offset walk. The +8 is the {ptr,len,cap}-header .len offset, the
* same constant the N_IDENT arm and the .len pseudo-field use; element_off sums
* preceding element sizes through the type table.
*
* Rows (every element a DISTINCT length so a dropped/wrong field is caught;
* each ww program self-asserts and returns 0 only when every len(t.N) is
* correct). Tuples are slice/str-ONLY — a leading scalar element exercises a
* SEPARATE pre-existing mixed-tuple sret-layout cs/ww divergence (filed apart
* from #235), out of scope here.
* two_slice ([]u8,[]u8) lens 3,5
* three_slice ([]u8,[]u8,[]u8) lens 3,5,11
* str_slice (str,[]u8) len("abcd")=4, slice 6
* slice_str ([]u8,str) slice 7, len("hi")=2
*
* All K_RUN: build+run exit 0 on BOTH drivers AND cs==ww byte-identical.
* NNN<950, self-contained (/tmp, no imports), so rule-14's selfhost-sibling
* race does not apply (940/945/799 precedent).
*/
#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;
}
struct row { const char *label; const char *src; int want; };
static const struct row rows[] = {
{ "two_slice",
"package main;\n"
"fn mk() ([]u8, []u8) = {\n"
" let a: []u8; a.len = 3; a.cap = 7;\n"
" let b: []u8; b.len = 5; b.cap = 9;\n"
" return (a, b);\n"
"};\n"
"export fn main() i32 = {\n"
" let t: ([]u8, []u8) = mk();\n"
" if (len(t.0) != 3) { return 1; };\n"
" if (len(t.1) != 5) { return 2; };\n"
" return 0;\n"
"};\n", 0 },
{ "three_slice",
"package main;\n"
"fn mk() ([]u8, []u8, []u8) = {\n"
" let a: []u8; a.len = 3; a.cap = 7;\n"
" let b: []u8; b.len = 5; b.cap = 9;\n"
" let c: []u8; c.len = 11; c.cap = 13;\n"
" return (a, b, c);\n"
"};\n"
"export fn main() i32 = {\n"
" let t: ([]u8, []u8, []u8) = mk();\n"
" if (len(t.0) != 3) { return 1; };\n"
" if (len(t.1) != 5) { return 2; };\n"
" if (len(t.2) != 11) { return 3; };\n"
" return 0;\n"
"};\n", 0 },
{ "str_slice",
"package main;\n"
"fn mk() (str, []u8) = {\n"
" let s: str = \"abcd\";\n"
" let b: []u8; b.len = 6; b.cap = 8;\n"
" return (s, b);\n"
"};\n"
"export fn main() i32 = {\n"
" let t: (str, []u8) = mk();\n"
" if (len(t.0) != 4) { return 1; };\n"
" if (len(t.1) != 6) { return 2; };\n"
" return 0;\n"
"};\n", 0 },
{ "slice_str",
"package main;\n"
"fn mk() ([]u8, str) = {\n"
" let b: []u8; b.len = 7; b.cap = 9;\n"
" let s: str = \"hi\";\n"
" return (b, s);\n"
"};\n"
"export fn main() i32 = {\n"
" let t: ([]u8, str) = mk();\n"
" if (len(t.0) != 7) { return 1; };\n"
" if (len(t.1) != 2) { return 2; };\n"
" return 0;\n"
"};\n", 0 },
};
/* build+run via a driver (ww / ww_ww); returns 0 pass, nonzero fail. */
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/telen_%d_%d.ww", getpid(), i);
snprintf(tmpdir, sizeof tmpdir, "/tmp/telen_%d_d_%d", getpid(), i);
snprintf(errf, sizeof errf, "/tmp/telen_%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;
}
/* cs==ww .s byte-id (rule 10). */
static int
byteid(const char *w6c, const char *w6c_ww, const struct row *r, int i)
{
char src[96], cs_s[96], ws_s[96], cmd[1024];
snprintf(src, sizeof src, "/tmp/telen_bi_%d_%d.ww", getpid(), i);
snprintf(cs_s, sizeof cs_s, "/tmp/telen_bi_%d_%d_cs.s", getpid(), i);
snprintf(ws_s, sizeof ws_s, "/tmp/telen_bi_%d_%d_ww.s", getpid(), i);
FILE *f = fopen(src, "wb");
if (!f) return -1;
fputs(r->src, f);
fclose(f);
int rc = 0;
snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null", w6c, cs_s, src);
if (runwait(cmd) != 0) { fprintf(stderr, "row[%s]: w6c failed\n", r->label); rc = 1; }
else {
snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null", w6c_ww, ws_s, src);
if (runwait(cmd) != 0) { fprintf(stderr, "row[%s]: w6c_ww failed\n", r->label); rc = 1; }
else if (slurp_eq(cs_s, ws_s) != 0) {
fprintf(stderr, "row[%s]: cstage/wwstage .s DIFFER "
"(rule-10 byte-id)\n", r->label);
rc = 1;
}
}
unlink(src); unlink(cs_s); unlink(ws_s);
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 cdrv[640], wdrv[640], w6c[640], w6c_ww[640];
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin);
snprintf(w6c, sizeof w6c, "%s/w6c", bin);
snprintf(w6c_ww, sizeof w6c_ww, "%s/w6c_ww", bin);
struct { const char *name; const char *path; 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].path, X_OK) != 0) {
fprintf(stderr, "tuple_elem_slice_len: skip %s (no %s)\n",
drivers[d].name, drivers[d].path);
continue;
}
for (int i = 0; i < n; i++) {
total++;
if (run_driver(drivers[d].path, &rows[i], i) != 0) fail++;
}
}
if (access(w6c_ww, X_OK) == 0) {
for (int i = 0; i < n; i++) {
total++;
if (byteid(w6c, w6c_ww, &rows[i], i) != 0) fail++;
}
}
if (fail) {
fprintf(stderr, "tuple_elem_slice_len: %d/%d checks failed\n",
fail, total);
return 1;
}
printf("tuple_elem_slice_len: %d/%d ok\n", total, total);
return 0;
}