Files
ww/test/wcc/777_fmt_vstream_run.c
Hojun-Cho 7d39f6d623 lib: collapse the parallel vstream scaffold onto the single Hare io surface (#94 fold-eFinal)
The Option-C parallel _v vstream API was scaffolding to bring the io stack up alongside the old surface; carrying both permanently is a rule-9 divergence from ref/hare, which has exactly one io surface. Collapse onto that surface (stream = *vtable, ref/hare/io/stream.ha) and rename the _v symbols to their Hare names (io vstream->stream, fmt vfprint->fprint, bufio/memio/log surfaces, log.new). Deletes the 4 lib/*/vstream.ww scaffold files; regenerates w6c/wwdump combined.ww. cstage and wwstage stay byte-identical and combined_ww_fresh holds; all 220 tests pass.
2026-05-30 03:24:23 +09:00

397 lines
14 KiB
C

/*
* 777_fmt_vstream_run — project #94 fold-eFinal sentinel. Pins the
* single-surface lib/fmt fd shim: stack-resident fd_ctx with
* `vt: io.vtable` as the first field for the intrusive io.stream
* cast, four wrappers (fdprint / fdprintln / fdprintf / fdprintfln)
* that take a raw fd, allocate fd_ctx on their own frame (never
* escapes — ken's dispatcher-CONTAINED discipline), and dispatch
* through io.write via the wired fdsinkwrite callback. The fd shim
* has no Hare counterpart; it folds into fNNN-over-handle once io
* fold-2 (#5) ports `handle = (file | int)`.
*
* Each row imports fmt + os + io, opens a per-process /tmp output
* file, calls one V wrapper to write through the fd, closes, re-
* opens for read, reads the bytes back, and asserts both (a) the
* exact byte content and (b) a unique row-tagged exit constant.
*
* Cstage-only per row (no STAGE_WW, no byte_id) — pre-existing
* wwstage bug #209 (sibling of #190): the wwstage checker bails
* `case: not a variant of scrutinee (X)` + `match: variant not
* handled (formattable)` on the OLD fmt.fdprint / fdprintf's
* match-on-formattable arms whenever a downstream probe `import
* fmt;`s the package. The bug bites the OLD surface identically
* — `fmt.errorln("hi")` from a probe trips the same trace. The
* pre-existing surface (fmt.ww) is fine via cstage because every
* other test that uses fmt is cstage-only (970 fmttest runs the
* @test fixture under cstage `ww run` only). 995 self-rebuild
* dodges it because no selfhost cmd imports fmt transitively —
* fmt is consumed by selfhost/cmd/wcc/err.ww but no main.ww in
* cmd/{ww,w6c,w6a,w6l,wwdump} pulls err.ww in. Fix-of-the-bug =
* one-class checker repair, out-of-scope for the additive
* fold-e3; closes the wwstage half here when it lands. Byte-id
* graduates with #209 close.
*
* row | what it pins
* --------------------------+--------------------------------------
* fdprintf_v_int | fdprintf with `"v={}\n"`-style
* | format + one i64 arg. Asserts the
* | full intrusive shape (stack fd_ctx →
* | &c.vt → io.write → fdsinkwrite
* | → os.write) plus the {n}-placeholder
* | parser routed through vformatfield.
* fdprintln_v_multi | fdprintln with three positional
* | args (i64 + str + bool). Asserts the
* | space-separator + trailing newline
* | shape mirrors OLD fdprintln (fmt.ww:
* | 145), routed through io.write.
* fdprint_v_raw | fdprint with one str arg, no
* | format mods (zero-format raw). Pins
* | the per-arg loop without the
* | placeholder parser (fprint code
* | path, fmt.ww).
* branched_fdprintf | branched callee per #105: two
* | distinct format strings runtime-
* | selected from a single fdprintf
* | call site. Catches a constant-fold
* | mistake in the format-string
* | dispatch (mirror of 776's
* | branched_fixed for the format-side).
*
* IMPORT-ORDER WORKAROUND (sibling task #208, pre-existing): every
* row places `import os;` FIRST for the same reason 776 documents
* (wwstage checker ordering-sensitivity on os.tryread/trywrite/
* tryopen's bare `return r;` over a tagged return type).
*
* DEFERRALS / RELATED (NOT addressed by these rows):
*
* - io fold-2 (handle port): drew-deferred. Hare's
* ref/hare/fmt/wrappers.ha:9-25 routes through io::handle; fdNNN
* collapses into bare fNNN once handle = (file | int) lands.
* #5 graduates.
*
* - #206 (bare &fn → (*alias|void)): 2 cast sites per wrapper
* vtable wire-up; explicit casts KEPT (ken/#214 — cast-drop
* deferred, gated on #214).
*
* - #173 (TRY-on-tagged-return both-stages broken): fdsinkwrite
* constructs nomem and widens to io.error explicitly rather
* than using `os.trywrite(...)?`; same shape memio.ww adopts.
*
* GATE POLARITY: must stay GREEN. A red here means lib/fmt
* regressed, the stack fd_ctx → io.stream cast miscomputed offsets,
* the {n}-placeholder dispatch through formatfield regressed, or
* io.vtable's first-field embed lost its offset-0 invariant.
*/
#include <stdio.h>
#include <stdlib.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;
}
#define STAGE_CS 1
#define STAGE_WW 2
struct row {
const char *label;
const char *src;
int want_exit;
int stage_mask;
int byte_id;
};
static const struct row rows[] = {
{ "fdprintf_v_int",
"package main;\n"
"import os;\n"
"import fmt;\n"
"import io;\n"
"export fn main() i32 = {\n"
" let path: str = \"/tmp/wwfmtv_777_a\";\n"
" let fd: i32 = os.open(path, os.flag.RDWR | os.flag.CREATE | os.flag.TRUNC, 0o644: i32);\n"
" if (fd < 0) { return 90; };\n"
" let wr = fmt.fdprintf(fd, \"v={}\\n\", 42i64);\n"
" let nw: size = 0;\n"
" match (wr) {\n"
" case let n: size => { nw = n; };\n"
" case io.error => { return 91; };\n"
" };\n"
" os.close(fd);\n"
" let rd: i32 = os.open(path, os.flag.RDONLY, 0i32);\n"
" if (rd < 0) { return 92; };\n"
" let buf: [16]u8;\n"
" let n: i64 = os.read(rd, &buf[0], 16u64);\n"
" os.close(rd);\n"
" if (n != 5i64) { return 93; };\n"
" if (nw != 5: size) { return 94; };\n"
" if (buf[0] != 118u8 || buf[1] != 61u8 || buf[2] != 52u8 || buf[3] != 50u8 || buf[4] != 10u8) { return 95; };\n"
" return 42;\n"
"};\n",
42,
STAGE_CS, 0 },
{ "fdprintln_v_multi",
"package main;\n"
"import os;\n"
"import fmt;\n"
"import io;\n"
"export fn main() i32 = {\n"
" let path: str = \"/tmp/wwfmtv_777_b\";\n"
" let fd: i32 = os.open(path, os.flag.RDWR | os.flag.CREATE | os.flag.TRUNC, 0o644: i32);\n"
" if (fd < 0) { return 90; };\n"
" let wr = fmt.fdprintln(fd, 7i64, \"hi\", true);\n"
" let nw: size = 0;\n"
" match (wr) {\n"
" case let n: size => { nw = n; };\n"
" case io.error => { return 91; };\n"
" };\n"
" os.close(fd);\n"
" let rd: i32 = os.open(path, os.flag.RDONLY, 0i32);\n"
" if (rd < 0) { return 92; };\n"
" let buf: [16]u8;\n"
" let n: i64 = os.read(rd, &buf[0], 16u64);\n"
" os.close(rd);\n"
" if (n != 10i64) { return 93; };\n"
" if (nw != 10: size) { return 94; };\n"
" if (buf[0] != 55u8 || buf[1] != 32u8 || buf[2] != 104u8 || buf[3] != 105u8 || buf[4] != 32u8 || buf[5] != 116u8 || buf[6] != 114u8 || buf[7] != 117u8 || buf[8] != 101u8 || buf[9] != 10u8) { return 95; };\n"
" return 43;\n"
"};\n",
43,
STAGE_CS, 0 },
{ "fdprint_v_raw",
"package main;\n"
"import os;\n"
"import fmt;\n"
"import io;\n"
"export fn main() i32 = {\n"
" let path: str = \"/tmp/wwfmtv_777_c\";\n"
" let fd: i32 = os.open(path, os.flag.RDWR | os.flag.CREATE | os.flag.TRUNC, 0o644: i32);\n"
" if (fd < 0) { return 90; };\n"
" let wr = fmt.fdprint(fd, \"raw\");\n"
" let nw: size = 0;\n"
" match (wr) {\n"
" case let n: size => { nw = n; };\n"
" case io.error => { return 91; };\n"
" };\n"
" os.close(fd);\n"
" let rd: i32 = os.open(path, os.flag.RDONLY, 0i32);\n"
" if (rd < 0) { return 92; };\n"
" let buf: [8]u8;\n"
" let n: i64 = os.read(rd, &buf[0], 8u64);\n"
" os.close(rd);\n"
" if (n != 3i64) { return 93; };\n"
" if (nw != 3: size) { return 94; };\n"
" if (buf[0] != 114u8 || buf[1] != 97u8 || buf[2] != 119u8) { return 95; };\n"
" return 44;\n"
"};\n",
44,
STAGE_CS, 0 },
{ "branched_fdprintf",
"package main;\n"
"import os;\n"
"import fmt;\n"
"import io;\n"
"export fn main() i32 = {\n"
" let path: str = \"/tmp/wwfmtv_777_d\";\n"
" let fd: i32 = os.open(path, os.flag.RDWR | os.flag.CREATE | os.flag.TRUNC, 0o644: i32);\n"
" if (fd < 0) { return 90; };\n"
" let sel: i32 = 1;\n"
" let f1: str = \"A{}\";\n"
" let f2: str = \"B{}\";\n"
" let fmtstr: str = f1;\n"
" if (sel == 0) { fmtstr = f2; };\n"
" let wr = fmt.fdprintf(fd, fmtstr, 9i64);\n"
" let nw: size = 0;\n"
" match (wr) {\n"
" case let n: size => { nw = n; };\n"
" case io.error => { return 91; };\n"
" };\n"
" os.close(fd);\n"
" let rd: i32 = os.open(path, os.flag.RDONLY, 0i32);\n"
" if (rd < 0) { return 92; };\n"
" let buf: [8]u8;\n"
" let n: i64 = os.read(rd, &buf[0], 8u64);\n"
" os.close(rd);\n"
" if (n != 2i64) { return 93; };\n"
" if (nw != 2: size) { return 94; };\n"
" if (buf[0] != 65u8 || buf[1] != 57u8) { return 95; };\n"
" return 45;\n"
"};\n",
45,
STAGE_CS, 0 },
};
static int
write_source(const char *path, const char *src)
{
FILE *f = fopen(path, "wb");
if (!f) return -1;
fputs(src, f);
fclose(f);
return 0;
}
/* Per-row tmpdir cleanup. ww_ww writes intermediates next to the
* source (filed task #15), so each row's build leaves
* <src>.{combined.ww,s,o} + bare exe alongside. Sweep all then
* rmdir. Mirror of 776's cleanup_tmp. */
static void
cleanup_tmp(const char *tmpdir, const char *base)
{
char p[640];
snprintf(p, sizeof p, "%s/%s.ww", tmpdir, base); unlink(p);
snprintf(p, sizeof p, "%s/%s.s", tmpdir, base); unlink(p);
snprintf(p, sizeof p, "%s/%s.o", tmpdir, base); unlink(p);
snprintf(p, sizeof p, "%s/%s.combined.ww", tmpdir, base); unlink(p);
snprintf(p, sizeof p, "%s/%s", tmpdir, base); unlink(p);
rmdir(tmpdir);
}
static int
build_via_driver(const char *driver, const char *tmpdir, const char *cwd,
const char *src)
{
char cmd[2048];
snprintf(cmd, sizeof cmd,
"cd %s && timeout 180 %s build -I %s/lib %s 2>/dev/null",
tmpdir, driver, cwd, src);
return runwait(cmd);
}
static int
run_row(const char *driver, const char *cwd, const struct row *r, int seq)
{
char tmpdir[256], src[512], base[64], outbin[768];
snprintf(tmpdir, sizeof tmpdir, "/tmp/fvs_%d_d_%d", getpid(), seq);
snprintf(base, sizeof base, "main777");
snprintf(src, sizeof src, "%s/%s.ww", tmpdir, base);
mkdir(tmpdir, 0755);
if (write_source(src, r->src) != 0) { cleanup_tmp(tmpdir, base); return -1; }
int rc;
int br = build_via_driver(driver, tmpdir, cwd, src);
if (br == 0) {
snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base);
rc = runwait(outbin);
} else {
rc = -1;
}
cleanup_tmp(tmpdir, base);
return rc;
}
/* asm_byte_identical — diff cstage vs wwstage .s. Parallel trees so
* ww_ww writing intermediates next to the source doesn't clobber the
* cstage .s (CLAUDE.md rule 14 phase split). Mirror of 776's. */
static int
asm_byte_identical(const char *cdrv, const char *wdrv, const char *cwd,
const struct row *r, int seq)
{
char src[512], tdc[256], tdw[256], base[64], cs[512], ws[512];
snprintf(tdc, sizeof tdc, "/tmp/fvs_%d_c_%d", getpid(), seq);
snprintf(tdw, sizeof tdw, "/tmp/fvs_%d_w_%d", getpid(), seq);
snprintf(base, sizeof base, "main777");
mkdir(tdc, 0755);
mkdir(tdw, 0755);
snprintf(src, sizeof src, "%s/%s.ww", tdc, base);
if (write_source(src, r->src) != 0) { cleanup_tmp(tdc, base); cleanup_tmp(tdw, base); return -1; }
int rc = -1;
if (build_via_driver(cdrv, tdc, cwd, src) != 0) goto out;
snprintf(cs, sizeof cs, "%s/%s.s", tdc, base);
snprintf(src, sizeof src, "%s/%s.ww", tdw, base);
if (write_source(src, r->src) != 0) goto out;
if (build_via_driver(wdrv, tdw, cwd, src) != 0) goto out;
snprintf(ws, sizeof ws, "%s/%s.s", tdw, base);
FILE *fc = fopen(cs, "rb");
FILE *fw = fopen(ws, "rb");
if (fc && fw) {
rc = 0;
for (;;) {
int a = fgetc(fc);
int b = fgetc(fw);
if (a != b) { rc = -1; break; }
if (a == EOF) break;
}
}
if (fc) fclose(fc);
if (fw) fclose(fw);
out:
cleanup_tmp(tdc, base);
cleanup_tmp(tdw, base);
return rc;
}
int
main(void)
{
const char *bin = getenv("BIN");
if (!bin) bin = "out/bin";
char cwd[256];
if (getcwd(cwd, sizeof cwd) == NULL) return 1;
char absbin[512];
if (bin[0] != '/') {
snprintf(absbin, sizeof absbin, "%s/%s", cwd, bin);
bin = absbin;
}
char cdrv[640], wdrv[640];
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin);
int n = (int)(sizeof rows / sizeof rows[0]);
int total = 0, fail = 0;
int wwpresent = (access(wdrv, X_OK) == 0);
int seq = 0;
for (int i = 0; i < n; i++) {
if (rows[i].stage_mask & STAGE_CS) {
total++;
int got = run_row(cdrv, cwd, &rows[i], seq++);
if (got != rows[i].want_exit) {
fprintf(stderr,
"fmt_vstream_run[cs][%s]: exit=%d want=%d\n",
rows[i].label, got, rows[i].want_exit);
fail++;
}
}
if (wwpresent && (rows[i].stage_mask & STAGE_WW)) {
total++;
int got = run_row(wdrv, cwd, &rows[i], seq++);
if (got != rows[i].want_exit) {
fprintf(stderr,
"fmt_vstream_run[ww][%s]: exit=%d want=%d\n",
rows[i].label, got, rows[i].want_exit);
fail++;
}
if (rows[i].byte_id) {
total++;
if (asm_byte_identical(cdrv, wdrv, cwd, &rows[i], seq++) != 0) {
fprintf(stderr,
"fmt_vstream_run[byte-id][%s]: cstage vs wwstage asm differs\n",
rows[i].label);
fail++;
}
}
}
}
if (!wwpresent)
fprintf(stderr, "fmt_vstream_run: skip wwstage (no %s)\n", wdrv);
if (fail) {
fprintf(stderr, "fmt_vstream_run: %d/%d fixtures failed\n",
fail, total);
return 1;
}
printf("fmt_vstream_run: %d/%d ok\n", total, total);
return 0;
}