/* * 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 ".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 #include #include #include #include #include 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 src[64], tmpdir[64], cmd[1024]; snprintf(src, sizeof src, "/tmp/slg_%d_%d.ww", getpid(), i); snprintf(tmpdir, sizeof tmpdir, "/tmp/slg_%d_d_%d", getpid(), i); FILE *f = fopen(src, "wb"); if (!f) return -1; fputs(r->src, f); fclose(f); mkdir(tmpdir, 0755); snprintf(cmd, sizeof cmd, "cd %s && %s build %s 2>/dev/null", tmpdir, driver, src); if (runwait(cmd) != 0) { fprintf(stderr, "row[%s]: build via %s failed\n", r->label, driver); unlink(src); rmdir(tmpdir); return -1; } 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 = runwait(outbin); unlink(src); unlink(outbin); rmdir(tmpdir); 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 s[64], tmpdir[64], cmd[1024]; snprintf(s, sizeof s, "/tmp/slgn_%d_%d.ww", getpid(), i); snprintf(tmpdir, sizeof tmpdir, "/tmp/slgn_%d_d_%d", getpid(), i); FILE *f = fopen(s, "wb"); if (!f) return -1; fputs(src, f); fclose(f); mkdir(tmpdir, 0755); snprintf(cmd, sizeof cmd, "cd %s && %s build %s 2>/dev/null", tmpdir, driver, s); int rc = runwait(cmd); unlink(s); const char *base = strrchr(s, '/'); base = base ? base + 1 : s; char outbin[128]; snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base); char *dot = strrchr(outbin, '.'); if (dot && strcmp(dot, ".ww") == 0) *dot = '\0'; unlink(outbin); rmdir(tmpdir); 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; }