w6c+w6c_ww: emit module-level slice-literal static-init (header+backing+reloc) (fix #10 part a)
`let g: []T = [v0, v1, …];` at module scope had no cgen arm: emit_lets /
emitletdataw handled str-lit and array-lit but not slice-lit, so NO
`DATAW main.g` was emitted and BOTH stages failed to link ("undefined
reference to main.g"). byte-id-blind — only the link step exposed it.
emit_slice_data / emitslicedata (parallel to the #18 str-array reloc
helper, generalized to a 24B header + array-backed data):
1. writable backing DATAW "<mangled g>.d" holding the k element bytes,
routed through the emit_array_lit_bytes / emitarraylitbytes choke-
point via a synthesized [k]T (int/float element kinds reduce exactly
as a [N]T global's do);
2. 24B header { ptr-placeholder, LE len, LE cap } (len = cap = k), word
sizes from the type table (ty_uintptr/ty_size, primtypesize) per
rule-13;
3. DATAR g+0 -> backing patches the ptr word.
The backing label's second '.' can't collide with a user global (source
identifiers carry no '.').
New emit_lets / slice arm gated on N_ARRLIT + slice-typed; rides on #18,
which keeps the module-level initializer as N_ARRLIT in both stages.
Aliased-slice spelling (`type S = []T; let g: S = [...]`): cstage
let_isslice already resolves the alias via type_unwrap, but wwstage
letvarisslice keyed only on the syntactic N_TSLICE node — unlike its
siblings letvarisstr/letvarisstruct/letvarisfloat, which all walk the
N_TNAME alias chain. So an aliased-slice global misrouted to the str arm
and never reached emitslicedata, link-failing on wwstage while cstage
emitted correctly (a cs≠ww divergence this fix would otherwise introduce).
letvarisslice now walks the alias chain exactly as letvarisstr does
(align wwstage UP to runtime-correct cstage, the #211 pattern); an alias
of a slice IS a slice. emitslicedata gains the nil/non-slice guard cstage
emit_slice_data already had (rule-10 symmetry; unreachable behind the
gate, guards the su.sub deref).
rule-7 loud-stops, symmetric both stages: read-only `def` slice-literal
(DATAR holder must be DATAW, w6a asm.c:362), `...` repeat (a slice
literal has no target length), and slice-of-{str,slice,tagged} elements
(per-element relocs / #17) — never silent no-emit.
Deferred (filed): struct-element module-level slice-literal surfaces a
separate checker cs!=ww ("let: not assignable" on wwstage, wrong runtime
on cstage) — out of #10's data-emission scope.
Test 687 (table-driven): []u8/[]i64/[]i32 element read-back + len + cap +
1-element edge + aliased-slice-type, dual-stage runtime + asm byte-id,
plus 3 build-fail rows for the loud-stops. selfhost combined.ww
regenerated.
This commit is contained in:
325
test/wcc/687_slice_literal_global.c
Normal file
325
test/wcc/687_slice_literal_global.c
Normal file
@@ -0,0 +1,325 @@
|
||||
/*
|
||||
* 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
|
||||
*/
|
||||
#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; };
|
||||
|
||||
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 },
|
||||
};
|
||||
|
||||
/* 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;
|
||||
}
|
||||
Reference in New Issue
Block a user