The wcc test drivers ran `ww build <bare-/tmp src>` with no -o, so the compiler's <stem>.sepwork scratch landed beside the source and was never cleaned: unbounded /tmp growth (2195 stale dirs observed) that fills tmpfs and fabricates phantom test failures + silent harness aborts, and for in-repo fixture builds leaked .sepwork into the tracked tree. Each leaking build now writes its source + output inside a per-invocation tmpdir, passes -o <tmpdir>/<stem> so the .sepwork lands inside it, and rm -rf's the tmpdir on every exit path -- including fopen-fail and the expected-fail reject builds (scratch is mkdir'd before the build can fail). `ww run` and explicit-`-o`/byte-id helpers are left as-is; the 990/993 byte-id comparison logic is byte-for-byte unchanged. Two items filed separately (this commit holds the no-Makefile / no-main.c rail): - #13: a stale <src>.s byte-id readback (749) silently no-ops since separate-compile emits .s to <ostem>.sepwork/__root.s; documented inline. - #14: build-system Makefile recipes build selfhost/cmd/*/main.ww with no -o and leak main.sepwork in-tree (bounded, gitignored; own commit). One concern -- sepwork leak hygiene -- across 228 drivers; uniform transform applied per-file and two-round reviewed. make test: all 402 passed, zero net-new /tmp scratch, zero test-driven in-repo .sepwork.
378 lines
13 KiB
C
378 lines
13 KiB
C
/*
|
|
* 687_slice_literal_global — module-level slice-literal static init
|
|
* `let g: []T = [v0, v1, …];` materializes a writable backing + a 24B
|
|
* { ptr, len, cap } header + a DATAR patching ptr -> backing, and cstage
|
|
* / wwstage agree byte-for-byte and at runtime (task #10 part a).
|
|
*
|
|
* The bug: emit_lets / emitletdataw had str-lit + array-lit arms but no
|
|
* slice-lit arm, so a `let g: []u8 = [1u8,2u8,3u8]` emitted NO `DATAW
|
|
* main.g` — BOTH stages failed to link ("undefined reference to main.g").
|
|
* byte-id-blind; only the link step exposed it. (wwstage further needed
|
|
* #18: its checker desugared the module-level initializer to an N_SLICE
|
|
* runtime borrow, so the rhs reached cgen as N_SLICE not N_ARRLIT — fixed
|
|
* first so this arm sees the raw array literal, mirroring cstage.)
|
|
*
|
|
* The fix (BOTH stages, byte-identical): emit_slice_data / emitslicedata
|
|
* route the k element bytes through the emit_array_lit_bytes choke-point
|
|
* (synthesized [k]T), then emit the header (ptr placeholder + LE len + LE
|
|
* cap, all = k) and the ptr-patch DATAR. Backing symbol "<mangled g>.d".
|
|
*
|
|
* Coverage: []u8 (element read-back + len + cap + sum), []i64 (wider
|
|
* element), []i32, and a 1-element edge. Plus dual-stage asm byte-id.
|
|
* Mutation-sanity: the old no-emit fails every row at LINK. Plus negative
|
|
* rows where the slice-literal static init must FAIL the build loudly
|
|
* (rule 7): read-only `def`, `...` repeat, and slice-of-str (per-element
|
|
* relocs / #17 deferred).
|
|
*
|
|
* row | shape | want
|
|
* ----------------+------------------------------------------+------
|
|
* u8_e0 | let g:[]u8=[1,2,3]; g[0] | 1
|
|
* u8_e2 | g[2] | 3
|
|
* u8_len | g.len | 3
|
|
* u8_cap | g.cap | 3
|
|
* u8_sum | g[0]+g[1]+g[2]+g.len+g.cap | 12
|
|
* i64_e1 | let g:[]i64=[10,20,30]; g[1] | 20
|
|
* i64_lencap | g.len+g.cap | 6
|
|
* i32_sum | let g:[]i32=[7,8,9]; g[0]+g[1]+g[2] | 24
|
|
* one_edge | let g:[]u8=[42]; g[0]+g.len | 43
|
|
* struct_pt | []pt=[pt{1,2},pt{3,4}]; sum+len+cap | 14
|
|
* struct_3f | []q (u8,i64,i32) two elems; sum+len | 23
|
|
* struct_one | []pt=[pt{5,6}]; sum+len+cap (count==1) | 13
|
|
* struct_arrvar_l | local let arr:[2]pt; let g:[]pt=arr | 14
|
|
*
|
|
* #19: wwstage's checker over-rejected the inline NAMED-STRUCT-element
|
|
* array->slice (`let g:[]pt=[pt{..},pt{..}]`) — its N_ARRLIT element-type
|
|
* inference returned the N_STRUCTLIT body (N_TSTRUCT, per #66) and the
|
|
* array->slice isassignable typeeqast saw a TNAME-vs-TSTRUCT kind mismatch
|
|
* against the declared N_TNAME element. cstage already inferred the NAMED
|
|
* type. Fix: infer the named type for a named struct-literal first element
|
|
* (check.ww N_ARRLIT arm); typeeqast untouched.
|
|
*/
|
|
#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;
|
|
}
|
|
|
|
struct row { const char *label; const char *src; int want; };
|
|
|
|
static const struct row rows[] = {
|
|
{ "u8_e0",
|
|
"package main;\n"
|
|
"let g: []u8 = [1u8, 2u8, 3u8];\n"
|
|
"export fn main() i32 = { return g[0]: i32; };\n",
|
|
1 },
|
|
{ "u8_e2",
|
|
"package main;\n"
|
|
"let g: []u8 = [1u8, 2u8, 3u8];\n"
|
|
"export fn main() i32 = { return g[2]: i32; };\n",
|
|
3 },
|
|
{ "u8_len",
|
|
"package main;\n"
|
|
"let g: []u8 = [1u8, 2u8, 3u8];\n"
|
|
"export fn main() i32 = { return g.len: i32; };\n",
|
|
3 },
|
|
{ "u8_cap",
|
|
"package main;\n"
|
|
"let g: []u8 = [1u8, 2u8, 3u8];\n"
|
|
"export fn main() i32 = { return g.cap: i32; };\n",
|
|
3 },
|
|
{ "u8_sum",
|
|
"package main;\n"
|
|
"let g: []u8 = [1u8, 2u8, 3u8];\n"
|
|
"export fn main() i32 = {\n"
|
|
"\treturn (g[0]:i32)+(g[1]:i32)+(g[2]:i32)+(g.len:i32)+(g.cap:i32);\n"
|
|
"};\n",
|
|
12 },
|
|
{ "i64_e1",
|
|
"package main;\n"
|
|
"let g: []i64 = [10i64, 20i64, 30i64];\n"
|
|
"export fn main() i32 = { return g[1]: i32; };\n",
|
|
20 },
|
|
{ "i64_lencap",
|
|
"package main;\n"
|
|
"let g: []i64 = [10i64, 20i64, 30i64];\n"
|
|
"export fn main() i32 = { return (g.len:i32)+(g.cap:i32); };\n",
|
|
6 },
|
|
{ "i32_sum",
|
|
"package main;\n"
|
|
"let g: []i32 = [7i32, 8i32, 9i32];\n"
|
|
"export fn main() i32 = { return (g[0]:i32)+(g[1]:i32)+(g[2]:i32); };\n",
|
|
24 },
|
|
{ "one_edge",
|
|
"package main;\n"
|
|
"let g: []u8 = [42u8];\n"
|
|
"export fn main() i32 = { return (g[0]:i32)+(g.len:i32); };\n",
|
|
43 },
|
|
/* aliased slice type: `type S = []u8` IS a slice — both stages must
|
|
* route through emit_slice_data. cstage resolves it via type_unwrap;
|
|
* wwstage letvarisslice resolves the alias chain (#10). */
|
|
{ "alias_slice",
|
|
"package main;\n"
|
|
"type S = []u8;\n"
|
|
"let g: S = [1u8, 2u8, 3u8];\n"
|
|
"export fn main() i32 = { return (g[0]:i32)+(g.len:i32)+(g.cap:i32); };\n",
|
|
7 },
|
|
/* #19: inline NAMED-STRUCT-element array->slice. wwstage's checker
|
|
* over-rejected this — the N_ARRLIT element-type inference picked up
|
|
* the N_STRUCTLIT's body (N_TSTRUCT, per #66) and the array->slice
|
|
* isassignable typeeqast saw a TNAME-vs-TSTRUCT kind mismatch (the
|
|
* declared element is N_TNAME). Fix infers the NAMED element. cstage
|
|
* already accepted; emit_array_lit_bytes' struct-field recursion
|
|
* makes the backing byte-identical. */
|
|
{ "struct_pt",
|
|
"package main;\n"
|
|
"type pt = struct { a: i64, b: i64 };\n"
|
|
"let g: []pt = [pt{a=1i64, b=2i64}, pt{a=3i64, b=4i64}];\n"
|
|
"export fn main() i32 = {\n"
|
|
"\treturn (g[0].a:i32)+(g[0].b:i32)+(g[1].a:i32)+(g[1].b:i32)+(g.len:i32)+(g.cap:i32);\n"
|
|
"};\n",
|
|
14 },
|
|
/* 3-field mixed-width struct stresses field offsets in the backing
|
|
* (u8 @0, i64 @8, i32 @16 — non-uniform strides). */
|
|
{ "struct_3f",
|
|
"package main;\n"
|
|
"type q = struct { a: u8, b: i64, c: i32 };\n"
|
|
"let g: []q = [q{a=1u8, b=2i64, c=3i32}, q{a=4u8, b=5i64, c=6i32}];\n"
|
|
"export fn main() i32 = {\n"
|
|
"\treturn (g[0].a:i32)+(g[0].b:i32)+(g[0].c:i32)+(g[1].a:i32)+(g[1].b:i32)+(g[1].c:i32)+(g.len:i32);\n"
|
|
"};\n",
|
|
23 },
|
|
/* single-element []struct (count==1 backing edge). */
|
|
{ "struct_one",
|
|
"package main;\n"
|
|
"type pt = struct { a: i64, b: i64 };\n"
|
|
"let g: []pt = [pt{a=5i64, b=6i64}];\n"
|
|
"export fn main() i32 = {\n"
|
|
"\treturn (g[0].a:i32)+(g[0].b:i32)+(g.len:i32)+(g.cap:i32);\n"
|
|
"};\n",
|
|
13 },
|
|
/* regression: the array-VARIABLE form already accepted (arr carries a
|
|
* clean N_TNAME). Local scope, since the MODULE-scope variable form is
|
|
* the still-deferred #22 (no backing symbol -> link fail on BOTH
|
|
* stages). */
|
|
{ "struct_arrvar_local",
|
|
"package main;\n"
|
|
"type pt = struct { a: i64, b: i64 };\n"
|
|
"export fn main() i32 = {\n"
|
|
"\tlet arr: [2]pt = [pt{a=1i64, b=2i64}, pt{a=3i64, b=4i64}];\n"
|
|
"\tlet g: []pt = arr;\n"
|
|
"\treturn (g[0].a:i32)+(g[0].b:i32)+(g[1].a:i32)+(g[1].b:i32)+(g.len:i32)+(g.cap:i32);\n"
|
|
"};\n",
|
|
14 },
|
|
};
|
|
|
|
/* slice-literal static init that must FAIL the build loudly (rule 7). */
|
|
static const char *neg[] = {
|
|
/* read-only `def` can't carry the ptr reloc (DATAR holder must be
|
|
* DATAW, w6a asm.c:362). */
|
|
"package main;\n"
|
|
"def g: []u8 = [1u8, 2u8, 3u8];\n"
|
|
"export fn main() i32 = { return g.len: i32; };\n",
|
|
/* `...` repeat has no target length in a slice literal. Uses the
|
|
* reachable repeat spelling `[v...]` (no comma) so the cgen loud-stop
|
|
* is exercised — the comma form `[v, ...]` parse-errors first and
|
|
* would test the parser, not the emit_slice_data guard. */
|
|
"package main;\n"
|
|
"let g: []u8 = [1u8, 2u8...];\n"
|
|
"export fn main() i32 = { return g.len: i32; };\n",
|
|
/* slice-of-str needs per-element relocs (#17) — deferred loud-stop. */
|
|
"package main;\n"
|
|
"let g: []str = [\"a\", \"b\"];\n"
|
|
"export fn main() i32 = { return g.len: i32; };\n",
|
|
};
|
|
|
|
static int
|
|
run_driver(const char *driver, const struct row *r, int i)
|
|
{
|
|
char tmpdir[64], src[128], outbin[128], rmcmd[160], cmd[1024];
|
|
snprintf(tmpdir, sizeof tmpdir, "/tmp/slg_%d_d_%d", getpid(), i);
|
|
mkdir(tmpdir, 0755);
|
|
snprintf(src, sizeof src, "%s/slg_%d_%d.ww", tmpdir, getpid(), i);
|
|
snprintf(outbin, sizeof outbin, "%s/slg_%d_%d", tmpdir, getpid(), i);
|
|
snprintf(rmcmd, sizeof rmcmd, "rm -rf %s", tmpdir);
|
|
|
|
FILE *f = fopen(src, "wb");
|
|
if (!f) { runwait(rmcmd); return -1; }
|
|
fputs(r->src, f);
|
|
fclose(f);
|
|
|
|
/* #8: -o pins binary + .sepwork scratch under tmpdir; rm -rf on
|
|
* every exit path removes the now-non-empty dir. */
|
|
snprintf(cmd, sizeof cmd, "%s build -o %s %s 2>/dev/null",
|
|
driver, outbin, src);
|
|
if (runwait(cmd) != 0) {
|
|
fprintf(stderr, "row[%s]: build via %s failed\n",
|
|
r->label, driver);
|
|
runwait(rmcmd);
|
|
return -1;
|
|
}
|
|
|
|
int got = runwait(outbin);
|
|
|
|
runwait(rmcmd);
|
|
return got;
|
|
}
|
|
|
|
/* build_should_fail — returns 0 when the build correctly FAILS. */
|
|
static int
|
|
build_should_fail(const char *driver, const char *src, int i)
|
|
{
|
|
char tmpdir[64], s[128], outbin[128], rmcmd[160], cmd[1024];
|
|
snprintf(tmpdir, sizeof tmpdir, "/tmp/slgn_%d_d_%d", getpid(), i);
|
|
mkdir(tmpdir, 0755);
|
|
snprintf(s, sizeof s, "%s/slgn_%d_%d.ww", tmpdir, getpid(), i);
|
|
snprintf(outbin, sizeof outbin, "%s/slgn_%d_%d", tmpdir, getpid(), i);
|
|
snprintf(rmcmd, sizeof rmcmd, "rm -rf %s", tmpdir);
|
|
|
|
FILE *f = fopen(s, "wb");
|
|
if (!f) { runwait(rmcmd); return -1; }
|
|
fputs(src, f);
|
|
fclose(f);
|
|
|
|
/* #8: -o pins binary + .sepwork scratch under tmpdir; a reject build
|
|
* still mkdir's scratch, so rm -rf must run on this path too. */
|
|
snprintf(cmd, sizeof cmd, "%s build -o %s %s 2>/dev/null",
|
|
driver, outbin, s);
|
|
int rc = runwait(cmd);
|
|
runwait(rmcmd);
|
|
return rc == 0 ? -1 : 0; /* build must NOT succeed */
|
|
}
|
|
|
|
/* asm_byte_identical — w6c vs w6c_ww .s for the same source must match. */
|
|
static int
|
|
asm_byte_identical(const char *bin, const struct row *r, int i)
|
|
{
|
|
char src[64], cs[64], ws[64], cmd[1024];
|
|
snprintf(src, sizeof src, "/tmp/slg_asm_%d_%d.ww", getpid(), i);
|
|
snprintf(cs, sizeof cs, "/tmp/slg_asm_%d_%d_c.s", getpid(), i);
|
|
snprintf(ws, sizeof ws, "/tmp/slg_asm_%d_%d_w.s", getpid(), i);
|
|
|
|
FILE *f = fopen(src, "wb");
|
|
if (!f) return -1;
|
|
fputs(r->src, f);
|
|
fclose(f);
|
|
|
|
snprintf(cmd, sizeof cmd, "%s/w6c -o %s %s 2>/dev/null", bin, cs, src);
|
|
if (runwait(cmd) != 0) {
|
|
fprintf(stderr, "row[%s]: w6c errored\n", r->label);
|
|
unlink(src);
|
|
return -1;
|
|
}
|
|
snprintf(cmd, sizeof cmd, "%s/w6c_ww -o %s %s 2>/dev/null",
|
|
bin, ws, src);
|
|
if (runwait(cmd) != 0) {
|
|
fprintf(stderr, "row[%s]: w6c_ww errored\n", r->label);
|
|
unlink(src); unlink(cs);
|
|
return -1;
|
|
}
|
|
|
|
FILE *fc = fopen(cs, "rb");
|
|
FILE *fw = fopen(ws, "rb");
|
|
int rc = 0;
|
|
if (!fc || !fw) {
|
|
rc = -1;
|
|
} else {
|
|
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);
|
|
if (rc != 0)
|
|
fprintf(stderr, "row[%s]: cstage vs wwstage asm differs\n",
|
|
r->label);
|
|
unlink(src); unlink(cs); unlink(ws);
|
|
return rc;
|
|
}
|
|
|
|
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];
|
|
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
|
|
char wdrv[1024];
|
|
snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin);
|
|
|
|
struct { const char *name; const char *path; int gated_on_existence; }
|
|
drivers[] = {
|
|
{ "cstage", cdrv, 0 },
|
|
{ "wwstage", wdrv, 1 },
|
|
{ NULL, NULL, 0 },
|
|
};
|
|
|
|
int n = (int)(sizeof rows / sizeof rows[0]);
|
|
int nn = (int)(sizeof neg / sizeof neg[0]);
|
|
int total = 0, fail = 0;
|
|
|
|
for (int d = 0; drivers[d].name; d++) {
|
|
if (drivers[d].gated_on_existence
|
|
&& access(drivers[d].path, X_OK) != 0) {
|
|
fprintf(stderr, "slice_literal_global: skip %s (no %s)\n",
|
|
drivers[d].name, drivers[d].path);
|
|
continue;
|
|
}
|
|
for (int i = 0; i < n; i++) {
|
|
int got = run_driver(drivers[d].path, &rows[i], i);
|
|
total++;
|
|
if (got != rows[i].want) {
|
|
fprintf(stderr,
|
|
"slice_literal_global[%s][%s]: exit=%d want=%d\n",
|
|
drivers[d].name, rows[i].label,
|
|
got, rows[i].want);
|
|
fail++;
|
|
}
|
|
}
|
|
for (int i = 0; i < nn; i++) {
|
|
total++;
|
|
if (build_should_fail(drivers[d].path, neg[i],
|
|
100 + i) != 0) {
|
|
fprintf(stderr,
|
|
"slice_literal_global[%s][neg%d]: built ok, "
|
|
"expected a loud error\n",
|
|
drivers[d].name, i);
|
|
fail++;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (access(wdrv, X_OK) == 0) {
|
|
for (int i = 0; i < n; i++) {
|
|
total++;
|
|
if (asm_byte_identical(bin, &rows[i], i) != 0)
|
|
fail++;
|
|
}
|
|
}
|
|
|
|
if (fail) {
|
|
fprintf(stderr,
|
|
"slice_literal_global: %d/%d fixtures failed\n",
|
|
fail, total);
|
|
return 1;
|
|
}
|
|
printf("slice_literal_global: %d/%d ok\n", total, total);
|
|
return 0;
|
|
}
|