Files
ww/test/wcc/754_slice_of_slice_index.c
Hojun-Cho 7b9488706b parse: enforce strict-package — reject package-less files (#24a)
Flip the soft-default to a hard "missing package clause" error symmetrically in
both stages (cmd/wcc/parse.c + lib/ww/syntax/parse.ww): the first real decl of a
primary section with empty pathmod/resetmod and no seen clause is now rejected.
Closes the documented soft-default divergence (the 63-wrapper carve-out).

The gate flip can't be split from the migration it breaks, so this is one atomic
commit: ~80 test/wcc wrappers gain `package main;` via a shared wwtestpkg.h
helper, 6 data fixtures plus 17 asm-grep assertions update for the bare->main.<leaf>
root-helper mangle shift, and rt/ declares `package rt;` with @symbol pinning the
bare rt_ensure/rt_malloc linker names.

Root mangling narrows: the executable entry `main` stays bare (existing
carve-out), but root helper symbols become main.X. The #84 cluster is rewritten
to assert main.run distinct from aa.run/test.run; its cgen fix and bare machinery
are retained — still load-bearing for package-less module-reset deps. New
table-driven test 782_strict_package.c (6 rows, both stages).

Retiring //ww:module-reset is deferred to #24b: it is load-bearing (clears the
.wwi pathmod so the body's package clause asserts), not a vestige; fusing its
removal here would be a silent mismatch.

All byte-id gates green; full make test reports "all 335 tests passed".
2026-06-29 03:55:26 +09:00

237 lines
7.2 KiB
C

/*
* 754_slice_of_slice_index — sentinel for latent #20. Pre-fix, cstage's
* check.c typed `p[i]` for `p: *[]T` as `T` (element-of-element) via a
* bespoke TY_PTR-over-TY_SLICE clause that auto-decayed the pointer
* twice: first peeling the `*`, then peeling the `[]`. The Hare-faithful
* shape is `[]T` — a pointer-to-slice is a 1D array of slice headers,
* not a 2D array of T. The `*[N]T → T` auto-decay (TY_PTR over TY_ARRAY)
* IS the C-style array-to-element semantics callers expect; only the
* TY_SLICE arm of the same clause was wrong.
*
* Selfhost mirror lived in cgenutil.ww elemsizeof: for `*[]T`, the
* elem walk falls into `elem.kind == nkind.N_TSLICE` which had no
* recognition — fell through to the catch-all `return 8`. Post-fix
* the N_TSLICE arm returns 24 (slice header stride). Both stages now
* agree on stride; cstage already computed 24 via Type.size on the
* idx_eff(TY_PTR) sub.
*
* Surface impact (the wedge that flushed this latent): a `[][]u8`
* indexed through a `*[][]u8` (e.g. `tokens.ptr[0] = ...` for tokens:
* [][]u8) was rejected by cstage's `init u8 not assignable to declared
* []u8` and silently 8B-stored by wwstage. Symmetric fix:
* - cstage: drop the TY_SLICE branch in check.c N_INDEX so `*[]T[i]`
* falls through to the default `*U → U` path with U=[]T.
* - wwstage: extend cgenutil.ww elemsizeof to return 24 when the
* elem (post-N_TPTR peel) is N_TSLICE.
*
* Probes:
* row 0 — `let r: []u8 = p[0]` for `p: *[]u8`. Pre-fix cstage errors
* ("init u8 not assignable to declared []u8"); wwstage
* silently emits an 8B load. Post-fix both stages compile
* and emit the same MOVQ load with stride 24 (slice element
* span — read width is still 8B today; the full 24B slice
* element copy is a separately-tracked downstream gap noted
* at cmd/w6c/cgen.c:6518 ("Slice (24B) and struct/tuple/
* tagged element arrays land in the same multi-word-store
* gap")). Asserts byte-identical asm between stages.
*
* row 1 — `let v: u8 = p[0]` for `p: *u8`. Default `*T → T` path:
* unchanged by the fix. Confirms shlex / getopt's
* `slice.ptr[i]` (where `slice.ptr` is `*T` for some
* non-slice T) still routes through the default and emits
* a narrow MOVZBQ. Byte-id between stages.
*
* No 24B-element-emit assertion: the 24B slice / 16B struct / etc.
* multi-word-element copy is a parallel codegen wedge already noted
* inline at cgen.c:6515-6523. Splitting keeps this commit bisect-clean
* (rule 11).
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include "wwtestpkg.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;
const char *stride_imm; /* expected `MOVQ\t$<stride>, CX` */
};
static const struct row rows[] = {
/* `p: *[]u8` indexed: result type is `[]u8`, stride = 24
* (slice header). Pre-fix cstage rejected the assignment;
* wwstage silently used stride 8. Post-fix both agree on 24. */
{ "ptr_to_slice",
"fn probe(p: *[]u8) i32 = {\n"
"\tlet r: []u8 = p[0];\n"
"\treturn r.len;\n"
"};\n"
"export fn main() i32 = { return 0; };\n",
"MOVQ\t$24, CX" },
/* `p: *u8` default path: stride = 1, no MOVQ-$-CX scaling at
* all. Confirms the TY_SLICE-clause removal doesn't re-route
* non-slice pointer indexing. */
{ "ptr_to_byte",
"fn probe(p: *u8) i32 = {\n"
"\tlet x: u8 = p[0];\n"
"\treturn x: i32;\n"
"};\n"
"export fn main() i32 = { return 0; };\n",
NULL },
};
static int
slurp(const char *path, char *buf, size_t cap)
{
FILE *f = fopen(path, "rb");
if (!f) return -1;
size_t n = fread(buf, 1, cap - 1, f);
fclose(f);
buf[n] = '\0';
return (int)n;
}
static int
emit_s(const char *w6c, const struct row *r, int i, char *out_s, size_t cap)
{
char src[96], cmd[1024];
snprintf(src, sizeof src, "/tmp/sosi_%d_%d.ww", getpid(), i);
snprintf(out_s, cap, "/tmp/sosi_%d_%d_%s.s",
getpid(), i, w6c[strlen(w6c) - 1] == 'w' ? "ww" : "c");
FILE *f = fopen(src, "wb");
if (!f) return -1;
wwtest_fputs(r->src, f);
fclose(f);
snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null", w6c, out_s, src);
int rc = runwait(cmd);
unlink(src);
return rc;
}
static int
check_stride(const char *spath, const struct row *r, const char *stage)
{
char buf[1 << 14];
if (slurp(spath, buf, sizeof buf) < 0) {
fprintf(stderr, "row[%s][%s]: cannot read %s\n",
r->label, stage, spath);
return -1;
}
const char *fn = strstr(buf, "TEXT main.probe");
if (!fn) {
fprintf(stderr,
"row[%s][%s]: no TEXT probe in %s\n",
r->label, stage, spath);
return -1;
}
const char *ret = strstr(fn, "\tRET\n");
if (!ret) ret = fn + strlen(fn);
if (r->stride_imm) {
const char *m = strstr(fn, r->stride_imm);
if (!m || m >= ret) {
fprintf(stderr,
"row[%s][%s]: expected `%s` inside TEXT probe\n",
r->label, stage, r->stride_imm);
return -1;
}
} else {
const char *m = strstr(fn, "\tMOVQ\t$");
while (m && m < ret) {
const char *eol = strchr(m, '\n');
if (eol && eol < ret &&
strstr(m, ", CX\n") && (strstr(m, ", CX\n") < eol)) {
fprintf(stderr,
"row[%s][%s]: unexpected MOVQ $imm, CX "
"(scale) inside TEXT probe — `*u8` path "
"should index with stride 1\n",
r->label, stage);
return -1;
}
m = eol ? strstr(eol, "\tMOVQ\t$") : NULL;
}
}
return 0;
}
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 w6c[640], w6c_ww[640];
snprintf(w6c, sizeof w6c, "%s/w6c", bin);
snprintf(w6c_ww, sizeof w6c_ww, "%s/w6c_ww", bin);
int have_ww = (access(w6c_ww, X_OK) == 0);
int n = (int)(sizeof rows / sizeof rows[0]);
int total = 0, fail = 0;
for (int i = 0; i < n; i++) {
char cs_path[128], ws_path[128];
if (emit_s(w6c, &rows[i], i, cs_path, sizeof cs_path) != 0) {
fprintf(stderr,
"slice_of_slice_index[cstage][%s]: w6c failed\n",
rows[i].label);
fail++; total++; continue;
}
total++;
if (check_stride(cs_path, &rows[i], "cstage") != 0) fail++;
if (!have_ww) { unlink(cs_path); continue; }
if (emit_s(w6c_ww, &rows[i], i, ws_path, sizeof ws_path) != 0) {
fprintf(stderr,
"slice_of_slice_index[wwstage][%s]: w6c_ww failed\n",
rows[i].label);
fail++; total++;
unlink(cs_path); continue;
}
total++;
if (check_stride(ws_path, &rows[i], "wwstage") != 0) fail++;
total++;
char cmd[512];
snprintf(cmd, sizeof cmd, "cmp -s %s %s", cs_path, ws_path);
if (runwait(cmd) != 0) {
fprintf(stderr,
"slice_of_slice_index[%s]: cstage vs wwstage asm "
"differs\n",
rows[i].label);
fail++;
}
unlink(cs_path); unlink(ws_path);
}
if (fail) {
fprintf(stderr,
"slice_of_slice_index: %d/%d fixtures failed\n",
fail, total);
return 1;
}
printf("slice_of_slice_index: %d/%d ok\n", total, total);
return 0;
}