Files
ww/test/wcc/723_composite_call_arg.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

223 lines
6.0 KiB
C

/*
* 723_composite_call_arg — sentinel for #24. Pins that wwstage emits
* three PUSHQs (CX, BX, AX) after a CALL whose return type is a 3-reg
* composite (`[]u8` slice, ptr/len/cap = AX/BX/CX) when that call's
* result is fed directly as a composite arg to another call. Pre-fix
* `nodeisslice` in cgenutil.ww had no N_CALL arm, so the natural-push
* branch in pushargsrev fell through to a single `PUSHQ AX` and the
* receiver's R8/R9 stayed unset (and arg2's pop drained off residual
* stack words, shifting all subsequent args).
*
* Cstage already had the right shape via typed-AST `node_isslice`
* (cmd/w6c/cgen.c:node_isslice). Fix aligns wwstage DOWN to cstage
* (rule 10): add an N_CALL arm to `nodeisslice` that mirrors the
* existing N_CALL arm in `nodeisstr` (cgenutil.ww:574+).
*
* test/lang/composite_call_arg_test.ww pins the runtime behaviour; this row
* pins the asm shape so a future cgen refactor that re-routes
* pushargsrev can't silently regress back to the dropped-len/cap
* sequence.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.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; };
/* Canonical row: f(g()) with g returning `[]u8`, f taking 2x `[]u8`.
* Mirrors strings.hasprefix(bytes.X(toutf8(in), p)) shape. */
static const struct row rows[] = {
{ "slice_call_into_2slice_arg",
"fn view(s: str) []u8 = {\n"
" let r: []u8;\n"
" r.ptr = s.ptr;\n"
" r.len = s.len;\n"
" r.cap = s.len;\n"
" return r;\n"
"};\n"
"fn check(a: []u8, b: []u8) bool = {\n"
" if (a.len != b.len) { return false; };\n"
" return true;\n"
"};\n"
"export fn caller(in: str, p: []u8) bool = {\n"
" return check(view(in), p);\n"
"};\n" },
};
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 long
find_after(const char *buf, long start, const char *needle)
{
const char *p = strstr(buf + start, needle);
if (!p) return -1;
return (long)(p - buf);
}
/* After every `CALL\tview` site within caller, three PUSHQs (CX, BX,
* AX in that order) must appear before the next CALL site. Pre-fix
* wwstage emitted only one PUSHQ AX. */
static int
check_three_push_after_call(const char *spath, const struct row *r)
{
char buf[1 << 16];
if (slurp(spath, buf, sizeof buf) < 0) return -1;
long call = find_after(buf, 0, "CALL\tmain.view");
if (call < 0) {
fprintf(stderr, "row[%s]: no CALL view site\n", r->label);
return -1;
}
long nextcall = find_after(buf, call + 1, "CALL\t");
if (nextcall < 0) {
fprintf(stderr, "row[%s]: no follow-up CALL\n", r->label);
return -1;
}
long pcx = find_after(buf, call, "PUSHQ\tCX");
long pbx = find_after(buf, call, "PUSHQ\tBX");
long pax = find_after(buf, call, "PUSHQ\tAX");
if (pcx < 0 || pcx > nextcall) {
fprintf(stderr,
"row[%s]: no PUSHQ CX between CALL view and next CALL\n",
r->label);
return -1;
}
if (pbx < 0 || pbx > nextcall) {
fprintf(stderr,
"row[%s]: no PUSHQ BX between CALL view and next CALL\n",
r->label);
return -1;
}
if (pax < 0 || pax > nextcall) {
fprintf(stderr,
"row[%s]: no PUSHQ AX between CALL view and next CALL\n",
r->label);
return -1;
}
/* High → low order: CX first, then BX, then AX. */
if (!(pcx < pbx && pbx < pax)) {
fprintf(stderr,
"row[%s]: PUSHQ order != CX,BX,AX (%ld,%ld,%ld)\n",
r->label, pcx, pbx, pax);
return -1;
}
return 0;
}
static int
emit_s(const char *w6c, const struct row *r, int i, char *out_s, size_t cap)
{
char src[64], cmd[1024];
snprintf(src, sizeof src, "/tmp/cca_asm_%d_%d.ww", getpid(), i);
snprintf(out_s, cap, "/tmp/cca_asm_%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;
}
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,
"composite_call_arg[cstage][%s]: w6c failed\n",
rows[i].label);
fail++; total++; continue;
}
total++;
if (check_three_push_after_call(cs_path, &rows[i]) != 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,
"composite_call_arg[wwstage][%s]: w6c_ww failed\n",
rows[i].label);
fail++; total++;
unlink(cs_path);
continue;
}
total++;
if (check_three_push_after_call(ws_path, &rows[i]) != 0) {
fail++;
}
/* Byte-id diff: this canonical row has no !void / no tagged
* variants, so it is not gated by #22 or #21 and must
* cmp -s clean post-#24. */
total++;
char cmd[512];
snprintf(cmd, sizeof cmd, "cmp -s %s %s", cs_path, ws_path);
if (runwait(cmd) != 0) {
fprintf(stderr,
"composite_call_arg[%s]: cstage vs wwstage asm differs\n",
rows[i].label);
fail++;
}
unlink(cs_path); unlink(ws_path);
}
if (fail) {
fprintf(stderr,
"composite_call_arg: %d/%d fixtures failed\n", fail, total);
return 1;
}
printf("composite_call_arg: %d/%d ok\n", total, total);
return 0;
}