Files
ww/test/wcc/723_composite_call_arg.c
Hojun-Cho 438efab8c6 test: migrate str/slice global family-1 batch to test/lang @test, retire C twins (fold-2)
Continue fold-2 (after 374e97b): migrate the remaining Family-1 str/slice
global + literal + index + call-arg group from bespoke build+run C twins to
test/lang @test, retiring each twin in the same commit. Runtime coverage MOVES
from $(TESTS) to test-lang (T1 runs+asserts via `ww test`) + test-lang-byteid
(T2 keeps cs==ww .s byte-id); coverage is preserved, the $(TESTS) headline
drops 9 (434->425). All asserts are primitive int/u8/bool comparisons (no
fmt/strconv in the assert path); the slice-store/index rows reset the global
each fn and sum ADJACENT elements so a dropped/mis-strided/over-wide word FAILS.

  989_globslicefield_run.c    -> glob_slice_field_test.ww     (slice field of a global struct: g.f=<slice> stores full 24B header; len/cap/non-zero-offset + str/scalar controls)
  989_globstrslice_run.c      -> glob_str_slice_arg_test.ww   (global str sliced with default hi passed as call arg loads its len word; explicit-hi control)
  989_trystr_run.c            -> try_str_unwrap_test.ww       (`!` unwrap of str-success tagged union shuffles the str header for ident-source/error-first/success-first)
  797_len_strglobal_run.c     -> len_str_global_test.ww       (len(str-global) loads .len via name(SB); local-str control)
  801_litstr_pseudo_run.c     -> lit_str_pseudo_test.ww       (string-literal .len/.ptr pseudo-field; empty/multibyte + arg-passthrough)
  803_globalidx_run.c         -> global_index_test.ww         (global str/slice index read/addr-of/store/compound, esz 1/4; local regression pins)
  903_tuple_elem_slice_len.c  -> tuple_elem_slice_len_test.ww (len(t.N) of a slice/str tuple element loads .len at +8; 2/3-slice, str-slice both orders)
  927_composite_call_arg_run.c-> composite_call_arg_test.ww   (slice-returning CALL passed inline as a composite arg; canonical/letslice/two-call/middle/nested/scalar/tagged)
  952_slicecopy_assign_run.c  -> slice_copy_assign_test.ww    (bulk slice-copy-assign `arr[lo:hi]=bs`, esz 1/4, field/via-ptr/local bases; reslice-read companion)

rd_reslice asserts the TRUE value 360 (the .c twin's want=104 was 360 & 0xFF,
an exit-code truncation). 723_composite_call_arg.c's comment repointed to the
new test/lang location. 802_lenidx_run.c is DEFERRED (it carries //ww:error
reject rows — needs a value-rows-only split + a slim reject carrier, a fold-3
pass). Bump LANGBYTEID_EXPECTED_MIN 22->31 to ratchet the new corpus floor.
2026-06-22 11:52:41 +09:00

222 lines
5.9 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>
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\tview");
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;
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;
}