test: migrate global value r/w family-5 to test/lang @test, retire C twins (fold-2)

Continue fold-2: migrate the global-value read/store/addr-of family (non-tagged)
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); the $(TESTS)
headline drops 3 (421->418). All asserts are primitive int comparisons; fresh
globals are zeroed, so a store that misses the symbol reads back 0 and FAILS.

  989_globptrfield_run.c  -> glob_ptr_field_test.ww  (scalar field store through a module-global *struct pointer loads the pointer from gp(SB), not saved BP; runtime gp=&backing form, static-init is #48-blocked)
  989_globstructret_run.c -> glob_struct_ret_test.ww (returning a module-global struct ident by value copies g's bytes; rsz 16 + rsz 24, each field asserted)
  989_dotbasehijack_run.c -> dotbase_hijack_test.ww  (indexing an [N]T field of a module-global struct addresses the struct field, not an unrelated global sharing the field name; +typed-inner control +colliding-global-intact pin)

glob_ptr_field keeps only the .c's offset-0 row: an added non-zero-offset row
(gp.g=9) surfaced a cs!=ww divergence (cstage folds the offset into the store
displacement `MOVQ AX,8(BX)`; wwstage emits `ADDQ $8,BX; MOVQ AX,(BX)`) — a
latent global-*struct-pointer field-store divergence beyond the .c's coverage,
reported for the backlog, not carried here. Bump LANGBYTEID_EXPECTED_MIN 35->38.
This commit is contained in:
2026-06-22 12:31:23 +09:00
parent b879d38b0f
commit a6b74e46c4
7 changed files with 108 additions and 503 deletions

View File

@@ -289,12 +289,9 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \
$(BIN)/test_fnptrcollide_run \
$(BIN)/test_tagnorm_run \
$(BIN)/test_globtagisas_run \
$(BIN)/test_dotbasehijack_run \
$(BIN)/test_globtagreassign_run \
$(BIN)/test_globstructret_run \
$(BIN)/test_globstructwiden_run \
$(BIN)/test_globtagwiden_run \
$(BIN)/test_globptrfield_run \
$(BIN)/test_globfloatstructarg_run \
$(BIN)/test_arr_ptr_global \
$(BIN)/test_def_arr_infer_len \
@@ -1095,16 +1092,6 @@ $(BIN)/test_globtagisas_run: test/wcc/989_globtagisas_run.c \
$(LIB)/libwwrt.a | $(BIN)
$(CC) $(CFLAGS) -o $@ $<
# 989_dotbasehijack_run (F8-c4, #20): indexing an [N]T field of a module-
# global struct must not be hijacked by an unrelated global sharing the
# field's name. Builds+runs on BOTH driver twins (rule-10); align-UP.
$(BIN)/test_dotbasehijack_run: test/wcc/989_dotbasehijack_run.c \
$(BIN)/ww $(BIN)/ww_ww \
$(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \
$(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \
$(LIB)/libwwrt.a | $(BIN)
$(CC) $(CFLAGS) -o $@ $<
# 989_globtagreassign_run (F8-c5, #32, #263): reassigning a module-global
# tagged ident (g = expr) must store tag+payload. ww-runtime-correct; cstage
# drops the store (cs!=ww residual, cstage half = task #41). Per-driver
@@ -1116,16 +1103,6 @@ $(BIN)/test_globtagreassign_run: test/wcc/989_globtagreassign_run.c \
$(LIB)/libwwrt.a | $(BIN)
$(CC) $(CFLAGS) -o $@ $<
# 989_globstructret_run (F8-c6, #41, #263): returning a module-global struct
# ident by value must copy g's bytes. ww-runtime-correct; cstage copies frame
# garbage (cs!=ww residual, cstage half = task #42). Per-driver expected rows.
$(BIN)/test_globstructret_run: test/wcc/989_globstructret_run.c \
$(BIN)/ww $(BIN)/ww_ww \
$(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \
$(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \
$(LIB)/libwwrt.a | $(BIN)
$(CC) $(CFLAGS) -o $@ $<
# 989_globstructwiden_run (F8-c7, #50, #263): widening a module-global struct
# ident into a tagged union must copy the full payload. ww-runtime-correct;
# cstage zero-fills the payload (cs!=ww residual, cstage half = task #43).
@@ -1147,17 +1124,6 @@ $(BIN)/test_globtagwiden_run: test/wcc/989_globtagwiden_run.c \
$(LIB)/libwwrt.a | $(BIN)
$(CC) $(CFLAGS) -o $@ $<
# 989_globptrfield_run (inverse-set #47, report-item [58]): a field store
# through a module-global *struct pointer must load the pointer from g(SB),
# not deref saved BP. cstage SEGV'd (rc=139); align cstage UP to ww (rc=7),
# cs==ww byte-identical. Runtime-assign repro (static-init form is #48-blocked).
$(BIN)/test_globptrfield_run: test/wcc/989_globptrfield_run.c \
$(BIN)/ww $(BIN)/ww_ww \
$(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \
$(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \
$(LIB)/libwwrt.a | $(BIN)
$(CC) $(CFLAGS) -o $@ $<
# 989_globfloatstructarg_run (F8-c9, #31, the EXCEPTION): a module-global
# float-bearing struct passed by value must drain its float eightbytes into
# the SSE arg regs. align-UP — structfloatclass keyed off the global tnode.
@@ -3412,7 +3378,7 @@ test-lang: all
LANGBYTEID_DIR = $(OUT)/langbyteid
LANGBYTEID_FILES = $(wildcard test/lang/*_test.ww)
LANGBYTEID_VERB = test -c
LANGBYTEID_EXPECTED_MIN = 35
LANGBYTEID_EXPECTED_MIN = 38
$(if $(LANGBYTEID_FILES),,$(error test-lang-byteid: empty corpus))
test-lang-byteid: all
@set -e; \

View File

@@ -0,0 +1,45 @@
// dotbase_hijack_test — indexing an `[N]T` field of a module-GLOBAL struct
// (`gs.fld[i]`) must address gs's field, not be hijacked by an unrelated
// module-global that shares the FIELD's name, migrated from
// test/wcc/989_dotbasehijack_run.c (F8-c4, report-item #20). wwstage's
// dotbaseaddr #128b probe did `letvartnode(base.str)` (base.str = the DOT's
// FIELD name) whenever the inner ident wasn't a local; for `gs.fld` with gs a
// module-global struct, the inner is global so the probe fired and, finding the
// unrelated global `fld: [2]i64`, emitted `LEAQ fld(SB)` — the WRONG symbol
// (returning 9 not 7). cstage gates the probe on a NULL/ty_err inner type (a
// module qualifier has no struct type), so a typed-struct inner resolves the
// field off the struct. The fix gates the wwstage probe the same way; the .s is
// byte-identical. The colliding global is read directly (global_fld_intact) so
// the name clash is genuine; no_collide is the typed-inner control the
// probe-skip must keep correct.
package dotbase_hijack_test;
type s = struct { pad: i64, fld: [2]i64 };
type s2 = struct { pad: i64, arr: [2]i64 };
let gs: s = s { pad = 0, fld = [7, 8] };
let gs2: s2 = s2 { pad = 0, arr = [5, 6] };
let fld: [2]i64 = [9, 10];
@test fn collide_0() void = {
// pre-fix wwstage read fld[0] (== 9) off the colliding global.
assert(gs.fld[0]: i32 == 7);
};
@test fn collide_1() void = {
// pre-fix wwstage read fld[1] (== 10) off the colliding global.
assert(gs.fld[1]: i32 == 8);
};
@test fn no_collide() void = {
// typed-struct inner with NO name collision — the probe-skip must not
// break the ordinary global-struct array-field index.
assert(gs2.arr[0]: i32 == 5);
};
@test fn global_fld_intact() void = {
// the colliding global is a genuine distinct symbol, not gs.fld.
assert(fld[0]: i32 == 9);
assert(fld[1]: i32 == 10);
};

View File

@@ -0,0 +1,26 @@
// glob_ptr_field_test — a scalar field store through a module-GLOBAL `*struct`
// pointer (`gp.f = v`) must load the pointer value from gp(SB), not deref the
// saved-BP word, migrated from test/wcc/989_globptrfield_run.c (inverse-set
// #47, report-item [58]). cstage's N_DOT-lhs via_ptr scalar store loaded the
// base with `MOVQ boff(BP),BX`; for a module-global base boff==0, so it emitted
// `MOVQ (BP),BX` — derefing saved BP as the pointer → store through garbage →
// SEGV. wwstage was already correct (LEAQ gp(SB),BX; MOVQ (BX),BX); the cstage
// half aligned UP and the .s is byte-identical, so a behavioral @test is the
// net. backing is a fresh (zeroed) global; a store that lands in garbage never
// writes the symbol, so a read-back of backing's field catches the miss. The
// static-init form `let gp: *S = &backing;` is #48-blocked, so the runtime
// `gp = &backing` form is used (as in the C twin).
package glob_ptr_field_test;
type S = struct { f: i64, g: i64 };
let backing: S = S { f = 0, g = 0 };
let gp: *S = nil;
@test fn store_off0() void = {
// the exact #58 repro: field at offset 0 through the global pointer.
gp = &backing;
gp.f = 7;
assert(backing.f: i32 == 7);
};

View File

@@ -0,0 +1,36 @@
// glob_struct_ret_test — returning a module-GLOBAL struct ident by value
// (`return g;`) must copy g's bytes into the return buffer, migrated from
// test/wcc/989_globstructret_run.c (F8-c6, #41, #263). The ≤24B register-struct
// return path word-copied an N_IDENT source from its BP slot only for a LOCAL
// ident; a module-global ident hit no branch (wwstage zero-filled the @retscr,
// cstage copied frame garbage), never g(SB). Both stages now copy g's bytes via
// the aggregate src-addr memcpy (LEAQ g(SB),SI) and the .s is byte-identical,
// so a behavioral @test is the net. Each field is asserted individually so a
// dropped / mis-offset word FAILS (a zero-filled @retscr reads back 0).
package glob_struct_ret_test;
type point = struct { x: i64, y: i64 };
type triple = struct { a: i64, b: i64, c: i64 };
let g16: point = point { x = 3, y = 4 };
let g24: triple = triple { a = 5, b = 6, c = 7 };
fn get16() point = { return g16; };
fn get24() triple = { return g24; };
@test fn ret_16() void = {
// rsz 16 register-struct return of a global ident.
let p: point = get16();
assert(p.x: i32 == 3);
assert(p.y: i32 == 4);
};
@test fn ret_24() void = {
// rsz 24 register-struct return of a global ident (the third word pins
// the full-payload copy).
let p: triple = get24();
assert(p.a: i32 == 5);
assert(p.b: i32 == 6);
assert(p.c: i32 == 7);
};

View File

@@ -1,166 +0,0 @@
/*
* 989_dotbasehijack_run — F8-c4 (report-item #20): indexing an `[N]T` field
* of a module-GLOBAL struct (`gs.fld[i]`) must address gs's field, not be
* hijacked by an unrelated module-global that shares the FIELD's name.
*
* THE BUG (cat-A silent miscompile, align-UP): cgenexpr.ww dotbaseaddr's
* #128b probe (for a module-qualified `mod.arr` array base) did
* `letvartnode(base.str)` — base.str is the DOT's FIELD name — whenever the
* inner ident wasn't a local. For `gs.fld` with `gs` a module-global struct,
* the inner gs is global (no local slot) so the probe fired and, finding an
* unrelated module-global `fld: [N]T`, emitted `LEAQ fld(SB)` — addressing
* the wrong symbol. cstage cg_dotbase_addr gates the #128b probe on
* inner->type == NULL || ty_err (cgen.c:2109 — a module qualifier has no
* struct type), so a typed-struct inner skips it and resolves the field off
* the struct (LEAQ gs(SB) + foff). 990-997 never hit a field-name/global
* collision, so the gate stayed green — a runtime row is the net.
*
* THE FIX: gate the wwstage #128b probe on tichase(inner.type_) == nil ||
* TY_ERR (the untyped module-qualifier case); a typed struct inner falls to
* the #249 isglobal struct path. align ww UP; the .s is byte-identical.
*
* Rows (build+run on cstage `ww` and wwstage `ww_ww`; rule-10 — agree+hit):
* row | shape | want
* -----------+------------------------------------------------+-----
* collide_0 | gs:{pad,fld:[2]i64}; gs.fld=[7,8]; global fld= | 7 [#20:
* | [9,10]; gs.fld[0] | pre-fix ww=9]
* collide_1 | same; gs.fld[1] | 8 [#20:
* | | pre-fix ww=10]
* no_collide | gs2:{pad,arr:[2]i64}=[5,6]; NO global arr; | 5 (control:
* | gs2.arr[0] | typed inner)
*/
#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;
int want_exit;
};
#define PRELUDE \
"package main;\n" \
"type s = struct { pad: i64, fld: [2]i64 };\n" \
"type s2 = struct { pad: i64, arr: [2]i64 };\n" \
"let gs: s = s { pad = 0, fld = [7, 8] };\n" \
"let gs2: s2 = s2 { pad = 0, arr = [5, 6] };\n" \
"let fld: [2]i64 = [9, 10];\n"
static const struct row rows[] = {
{ "collide_0",
PRELUDE
"export fn main() int = { return gs.fld[0]: int; };\n",
7 },
{ "collide_1",
PRELUDE
"export fn main() int = { return gs.fld[1]: int; };\n",
8 },
/* typed-struct inner with NO name collision — the probe-skip must not
* break the ordinary global-struct array-field index. */
{ "no_collide",
PRELUDE
"export fn main() int = { return gs2.arr[0]: int; };\n",
5 },
};
/* run_build — build+run `src` via `driver`; returns the binary's exit
* code, or -1 on a build failure. */
static int
run_build(const char *driver, const struct row *r, int i)
{
char src[64], tmpdir[64], cmd[1024];
snprintf(src, sizeof src, "/tmp/dbh_%d_%d.ww", getpid(), i);
snprintf(tmpdir, sizeof tmpdir, "/tmp/dbh_%d_d_%d", getpid(), i);
FILE *f = fopen(src, "wb");
if (!f) return -2;
fputs(r->src, f);
fclose(f);
mkdir(tmpdir, 0755);
snprintf(cmd, sizeof cmd, "cd %s && %s build %s 2>/dev/null",
tmpdir, driver, src);
int brc = runwait(cmd);
const char *base = strrchr(src, '/');
base = base ? base + 1 : src;
char outbin[128];
snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base);
char *dot = strrchr(outbin, '.');
if (dot && strcmp(dot, ".ww") == 0) *dot = '\0';
int got = -1;
if (brc == 0) got = runwait(outbin);
unlink(src); unlink(outbin); rmdir(tmpdir);
return brc == 0 ? got : -1;
}
int
main(void)
{
const char *bin = getenv("BIN");
if (!bin) bin = "out/bin";
char absbin[1024];
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[1024], wdrv[1024];
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin);
struct { const char *name; const char *drv; 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].drv, X_OK) != 0) {
fprintf(stderr, "dotbasehijack_run: skip %s (no %s)\n",
drivers[d].name, drivers[d].drv);
continue;
}
for (int i = 0; i < n; i++) {
total++;
int got = run_build(drivers[d].drv, &rows[i], i);
if (got != rows[i].want_exit) {
fprintf(stderr, "dotbasehijack_run[%s][%s]: exit=%d "
"want=%d\n", drivers[d].name, rows[i].label,
got, rows[i].want_exit);
fail++;
}
}
}
if (fail) {
fprintf(stderr, "dotbasehijack_run: %d/%d fixtures failed\n",
fail, total);
return 1;
}
printf("dotbasehijack_run: %d/%d ok\n", total, total);
return 0;
}

View File

@@ -1,146 +0,0 @@
/*
* 989_globptrfield_run — inverse-set cstage fix (report-item [58], ww correct):
* a field store through a module-GLOBAL `*struct` pointer must load the pointer
* value from g(SB), not deref the saved-BP word.
*
* THE BUG (cat-A divergence — cstage SEGV, wwstage correct): in cgen.c the
* N_DOT-lhs scalar field-store via_ptr arm loaded the base pointer with
* `MOVQ boff(BP),BX`. For a module-global `*struct` ident boff==0, so cstage
* emitted `MOVQ (BP),BX` — derefing the saved BP word as if it were the
* pointer → store through garbage → SEGV (rc=139). wwstage already loaded the
* pointer from the symbol (`LEAQ gp(SB),BX; MOVQ (BX),BX`) and ran correctly
* (rc=7). This is the INVERSE of the F8 #263 set: ww is the runtime-correct
* reference; the cstage half is filed as ww-core TASK #47.
*
* THE CSTAGE FIX (align cstage UP to ww): the via_ptr scalar store detects a
* global base (boff==0 && let_islet) and emits the ww-correct global-pointer
* load — PUSHQ AX; LEAQ gp(SB),BX; MOVQ (BX),BX; POPQ AX — before the store.
* Local bases keep the BP load (byte-id). Both stages now emit the identical
* .s and exit 7.
*
* The static-init form `let gp: *S = &backing;` is blocked on wwstage by a
* separate static-init-of-&global gap (ww-core TASK #48), so the repro uses
* the nil-init + runtime `gp = &backing` form, which builds on both stages.
*/
#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;
int want; /* cs==ww==want */
};
static const struct row rows[] = {
{ "global_ptr_field_store",
"package main;\n"
"type S = struct { f: i64, g: i64 };\n"
"let backing: S = S { f = 0, g = 0 };\n"
"let gp: *S = nil;\n"
"export fn main() int = {\n"
" gp = &backing;\n"
" gp.f = 7;\n"
" return backing.f: int;\n"
"};\n",
7 },
};
/* run_build — build+run `src` via `driver`; returns the binary's exit
* code, or -1 on a build failure. */
static int
run_build(const char *driver, const struct row *r, int i)
{
char src[64], tmpdir[64], cmd[1024];
snprintf(src, sizeof src, "/tmp/gpf_%d_%d.ww", getpid(), i);
snprintf(tmpdir, sizeof tmpdir, "/tmp/gpf_%d_d_%d", getpid(), i);
FILE *f = fopen(src, "wb");
if (!f) return -2;
fputs(r->src, f);
fclose(f);
mkdir(tmpdir, 0755);
snprintf(cmd, sizeof cmd, "cd %s && %s build %s 2>/dev/null",
tmpdir, driver, src);
int brc = runwait(cmd);
const char *base = strrchr(src, '/');
base = base ? base + 1 : src;
char outbin[128];
snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base);
char *dot = strrchr(outbin, '.');
if (dot && strcmp(dot, ".ww") == 0) *dot = '\0';
int got = -1;
if (brc == 0) got = runwait(outbin);
unlink(src); unlink(outbin); rmdir(tmpdir);
return brc == 0 ? got : -1;
}
int
main(void)
{
const char *bin = getenv("BIN");
if (!bin) bin = "out/bin";
char absbin[1024];
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[1024], wdrv[1024];
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin);
struct { const char *name; const char *drv; 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].drv, X_OK) != 0) {
fprintf(stderr, "globptrfield_run: skip %s (no %s)\n",
drivers[d].name, drivers[d].drv);
continue;
}
for (int i = 0; i < n; i++) {
total++;
int got = run_build(drivers[d].drv, &rows[i], i);
if (got != rows[i].want) {
fprintf(stderr, "globptrfield_run[%s][%s]: exit=%d "
"want=%d\n", drivers[d].name, rows[i].label,
got, rows[i].want);
fail++;
}
}
}
if (fail) {
fprintf(stderr, "globptrfield_run: %d/%d fixtures failed\n",
fail, total);
return 1;
}
printf("globptrfield_run: %d/%d ok\n", total, total);
return 0;
}

View File

@@ -1,156 +0,0 @@
/*
* 989_globstructret_run — F8-c6 (report-item #41, #263): returning a module-
* GLOBAL struct ident by value (`return g;`) must copy g's bytes into the
* return buffer.
*
* THE BUG (cat-A silent miscompile, #263 BOTH-WRONG, now CLOSED both stages):
* the ≤24B register-struct return path word-copied an N_IDENT source from its
* BP slot only for a LOCAL ident. A module-global struct ident hit no branch
* (wwstage zero-filled the @retscr; cstage copied frame garbage ((BP)/8(BP)),
* never g(SB)). The ww half landed in F8-c6; the cstage half (route a global
* ident through the aggarg_srcaddr memcpy — LEAQ g(SB),SI) landed in ww-core
* TASK #42. Both stages now copy g's bytes and the .s is byte-identical.
*
* Rows assert the field-equality check returns 0 on both stages (cs==ww).
* row | shape (return g; then check) | exit
* -----------+-------------------------------------------+-----
* ret_16 | point{x=3,y=4} (rsz 16); x==3 && y==4 | 0
* ret_24 | triple{a=5,b=6,c=7} (rsz 24); all three | 0
*/
#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;
int want_cs;
int want_ww;
};
static const struct row rows[] = {
{ "ret_16",
"package main;\n"
"type point = struct { x: i64, y: i64 };\n"
"let g: point = point { x = 3, y = 4 };\n"
"fn get() point = { return g; };\n"
"export fn main() int = {\n"
" let p: point = get();\n"
" if (p.x == 3 && p.y == 4) { return 0; };\n"
" return 1;\n"
"};\n",
0, 0 },
{ "ret_24",
"package main;\n"
"type triple = struct { a: i64, b: i64, c: i64 };\n"
"let g: triple = triple { a = 5, b = 6, c = 7 };\n"
"fn get() triple = { return g; };\n"
"export fn main() int = {\n"
" let p: triple = get();\n"
" if (p.a == 5 && p.b == 6 && p.c == 7) { return 0; };\n"
" return 1;\n"
"};\n",
0, 0 },
};
/* run_build — build+run `src` via `driver`; returns the binary's exit
* code, or -1 on a build failure. */
static int
run_build(const char *driver, const struct row *r, int i)
{
char src[64], tmpdir[64], cmd[1024];
snprintf(src, sizeof src, "/tmp/gsr_%d_%d.ww", getpid(), i);
snprintf(tmpdir, sizeof tmpdir, "/tmp/gsr_%d_d_%d", getpid(), i);
FILE *f = fopen(src, "wb");
if (!f) return -2;
fputs(r->src, f);
fclose(f);
mkdir(tmpdir, 0755);
snprintf(cmd, sizeof cmd, "cd %s && %s build %s 2>/dev/null",
tmpdir, driver, src);
int brc = runwait(cmd);
const char *base = strrchr(src, '/');
base = base ? base + 1 : src;
char outbin[128];
snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base);
char *dot = strrchr(outbin, '.');
if (dot && strcmp(dot, ".ww") == 0) *dot = '\0';
int got = -1;
if (brc == 0) got = runwait(outbin);
unlink(src); unlink(outbin); rmdir(tmpdir);
return brc == 0 ? got : -1;
}
int
main(void)
{
const char *bin = getenv("BIN");
if (!bin) bin = "out/bin";
char absbin[1024];
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[1024], wdrv[1024];
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin);
struct { const char *name; const char *drv; 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].drv, X_OK) != 0) {
fprintf(stderr, "globstructret_run: skip %s (no %s)\n",
drivers[d].name, drivers[d].drv);
continue;
}
int is_ww = (d == 1);
for (int i = 0; i < n; i++) {
total++;
int want = is_ww ? rows[i].want_ww : rows[i].want_cs;
int got = run_build(drivers[d].drv, &rows[i], i);
if (got != want) {
fprintf(stderr, "globstructret_run[%s][%s]: exit=%d "
"want=%d\n", drivers[d].name, rows[i].label,
got, want);
fail++;
}
}
}
if (fail) {
fprintf(stderr, "globstructret_run: %d/%d fixtures failed\n",
fail, total);
return 1;
}
printf("globstructret_run: %d/%d ok\n", total, total);
return 0;
}