cg_structlit_fill / cgstructlitfill had a TY_STR arm that stored all
three header words (ptr@+0, len@+8, cap@+16) but no TY_SLICE arm, so a
slice field in a struct literal `cl{ items = b, n = .. }` fell through to
the generic scalar tail and stored only the ptr word — the field's .len
and .cap read 0. str fields (the same 24B {ptr,len,cap} shape) worked;
slice fields silently dropped two words.
Both stages emitted IDENTICAL wrong asm, so the 990-997 byte-id gate was
green on both-wrong; runtime readback is the only correctness net. Same
is_str/is_slice discrimination gap as #10 part-b, here in the
struct-literal field-init path.
A slice is the same 24B header shape as str, so widen the str arm's
guard to TY_STR || TY_SLICE (cstage) / isstrtype || isslicetype
(wwstage) and let a slice ride the already-correct 3-word store. The
TAGGED arm stays ordered before it, so a nullable/tagged slice
(TY_TAGGED) still routes to the widener, not the 3-word store.
Test 689 (table-driven, runtime readback + dual-stage asm byte-id):
slice .len/.cap/.ptr, a scalar field beside/before the slice, a slice at
a non-zero field offset, two slice fields, and a str field beside a
slice (str-arm regression pin). 33/33 ok.
349 lines
10 KiB
C
349 lines
10 KiB
C
/*
|
|
* 689_structlit_slice_field — cstage and wwstage agree, byte-for-byte
|
|
* and at runtime, that a struct-LITERAL initializer `cl{ items = b, n =
|
|
* .. }` with a SLICE field copies the FULL 24B { ptr, len, cap } header
|
|
* of that field (task #24).
|
|
*
|
|
* The bug: cg_structlit_fill / cgstructlitfill had a TY_STR arm that
|
|
* stored all three header words (ptr@+0, len@+8, cap@+16), but NO
|
|
* TY_SLICE arm — a slice field fell through to the generic scalar tail
|
|
* which stored only the ptr word, so the stored .len and .cap read 0.
|
|
* str fields (same 24B shape) worked; slice fields silently dropped
|
|
* two words. Both stages emitted IDENTICAL wrong asm (the 990-997
|
|
* byte-id gate was green on both-wrong), so RUNTIME readback is the
|
|
* correctness net. Same is_str/is_slice discrimination gap as #10
|
|
* part-b, here in the struct-literal field-init path.
|
|
*
|
|
* The fix (BOTH stages, converged byte-identical): the str arm's guard
|
|
* widens to `TY_STR || TY_SLICE` (cstage) / `isstrtype || isslicetype`
|
|
* (wwstage) — a slice rides the existing, already-correct 3-word store.
|
|
* The TAGGED arm stays ordered BEFORE it, so a nullable/tagged slice
|
|
* (TY_TAGGED) still routes to the widener, not the 3-word store.
|
|
*
|
|
* Mutation-sanity: the .len and .cap rows fail if the store drops
|
|
* either word (the pre-fix 1-word slice store), and the scalar-field
|
|
* rows (y.n, y.head) pin that the field beside/before the slice is
|
|
* untouched. The str_* rows are a regression pin on the untouched str
|
|
* arm.
|
|
*
|
|
* row | shape | want
|
|
* ---------------+------------------------------------------+------
|
|
* slice_len | cl{items=b,n=9}; b.len=5; y.items.len | 5
|
|
* slice_cap | cl{items=b,n=9}; b.cap=8; y.items.cap | 8
|
|
* slice_n | cl{items=b,n=9}; y.n (scalar beside slice)| 9
|
|
* slice_ptr | hb[0]=4; cl{items=b,..}; y.items[0] | 4
|
|
* off_len | struct{head:i64,s:[]u8}; s @foff 8; | 7
|
|
* | s.len=7; y.s.len (pins disp+foff+8 hdr) |
|
|
* off_cap | same; s.cap=8; y.s.cap (disp+foff+16) | 8
|
|
* off_head | same; head=1; y.head (scalar before slice)| 1
|
|
* two_b_len | struct{a:[]u8,b:[]u8}; b @foff 24; | 6
|
|
* | b.len=6; y.b.len (2nd slice, non-0 foff) |
|
|
* two_b_cap | same; b.cap=8; y.b.cap | 8
|
|
* str_name_len | struct{s:[]u8,name:str}; name="hello"; | 5
|
|
* | y.name.len (str arm regression pin) |
|
|
* str_s_len | same; s.len=3; y.s.len (slice beside str) | 3
|
|
*
|
|
* The asm-byte-id rows pin the symmetric fix (cstage == wwstage); a
|
|
* non-empty diff means one stage missed the new slice arm.
|
|
*/
|
|
#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[] = {
|
|
{ "slice_len",
|
|
"package main;\n"
|
|
"type cl = struct { items: []u8, n: i64 };\n"
|
|
"export fn main() i32 = {\n"
|
|
"\tlet hb: [8]u8;\n"
|
|
"\tlet b: []u8; b.ptr = &hb[0]; b.len = 5; b.cap = 8;\n"
|
|
"\tlet y: cl = cl { items = b, n = 9i64 };\n"
|
|
"\treturn y.items.len: i32;\n"
|
|
"};\n",
|
|
5 },
|
|
|
|
{ "slice_cap",
|
|
"package main;\n"
|
|
"type cl = struct { items: []u8, n: i64 };\n"
|
|
"export fn main() i32 = {\n"
|
|
"\tlet hb: [8]u8;\n"
|
|
"\tlet b: []u8; b.ptr = &hb[0]; b.len = 5; b.cap = 8;\n"
|
|
"\tlet y: cl = cl { items = b, n = 9i64 };\n"
|
|
"\treturn y.items.cap: i32;\n"
|
|
"};\n",
|
|
8 },
|
|
|
|
{ "slice_n",
|
|
"package main;\n"
|
|
"type cl = struct { items: []u8, n: i64 };\n"
|
|
"export fn main() i32 = {\n"
|
|
"\tlet hb: [8]u8;\n"
|
|
"\tlet b: []u8; b.ptr = &hb[0]; b.len = 5; b.cap = 8;\n"
|
|
"\tlet y: cl = cl { items = b, n = 9i64 };\n"
|
|
"\treturn y.n: i32;\n"
|
|
"};\n",
|
|
9 },
|
|
|
|
{ "slice_ptr",
|
|
"package main;\n"
|
|
"type cl = struct { items: []u8, n: i64 };\n"
|
|
"export fn main() i32 = {\n"
|
|
"\tlet hb: [8]u8; hb[0] = 4u8;\n"
|
|
"\tlet b: []u8; b.ptr = &hb[0]; b.len = 5; b.cap = 8;\n"
|
|
"\tlet y: cl = cl { items = b, n = 9i64 };\n"
|
|
"\treturn y.items[0]: i32;\n"
|
|
"};\n",
|
|
4 },
|
|
|
|
/* slice field at a NON-ZERO field offset (head: i64 @0, s @8).
|
|
* Pins disp+foff+8/+16 in the stored header. */
|
|
{ "off_len",
|
|
"package main;\n"
|
|
"type hs = struct { head: i64, s: []u8 };\n"
|
|
"export fn main() i32 = {\n"
|
|
"\tlet hb: [8]u8;\n"
|
|
"\tlet b: []u8; b.ptr = &hb[0]; b.len = 7; b.cap = 8;\n"
|
|
"\tlet y: hs = hs { head = 1i64, s = b };\n"
|
|
"\treturn y.s.len: i32;\n"
|
|
"};\n",
|
|
7 },
|
|
|
|
{ "off_cap",
|
|
"package main;\n"
|
|
"type hs = struct { head: i64, s: []u8 };\n"
|
|
"export fn main() i32 = {\n"
|
|
"\tlet hb: [8]u8;\n"
|
|
"\tlet b: []u8; b.ptr = &hb[0]; b.len = 7; b.cap = 8;\n"
|
|
"\tlet y: hs = hs { head = 1i64, s = b };\n"
|
|
"\treturn y.s.cap: i32;\n"
|
|
"};\n",
|
|
8 },
|
|
|
|
{ "off_head",
|
|
"package main;\n"
|
|
"type hs = struct { head: i64, s: []u8 };\n"
|
|
"export fn main() i32 = {\n"
|
|
"\tlet hb: [8]u8;\n"
|
|
"\tlet b: []u8; b.ptr = &hb[0]; b.len = 7; b.cap = 8;\n"
|
|
"\tlet y: hs = hs { head = 1i64, s = b };\n"
|
|
"\treturn y.head: i32;\n"
|
|
"};\n",
|
|
1 },
|
|
|
|
/* two slice fields: a @0, b @24. Pins the 2nd slice's header at a
|
|
* non-zero field offset (the 24B stride). */
|
|
{ "two_b_len",
|
|
"package main;\n"
|
|
"type tt = struct { a: []u8, b: []u8 };\n"
|
|
"export fn main() i32 = {\n"
|
|
"\tlet hb: [8]u8;\n"
|
|
"\tlet p: []u8; p.ptr = &hb[0]; p.len = 3; p.cap = 8;\n"
|
|
"\tlet q: []u8; q.ptr = &hb[0]; q.len = 6; q.cap = 8;\n"
|
|
"\tlet y: tt = tt { a = p, b = q };\n"
|
|
"\treturn y.b.len: i32;\n"
|
|
"};\n",
|
|
6 },
|
|
|
|
{ "two_b_cap",
|
|
"package main;\n"
|
|
"type tt = struct { a: []u8, b: []u8 };\n"
|
|
"export fn main() i32 = {\n"
|
|
"\tlet hb: [8]u8;\n"
|
|
"\tlet p: []u8; p.ptr = &hb[0]; p.len = 3; p.cap = 8;\n"
|
|
"\tlet q: []u8; q.ptr = &hb[0]; q.len = 6; q.cap = 8;\n"
|
|
"\tlet y: tt = tt { a = p, b = q };\n"
|
|
"\treturn y.b.cap: i32;\n"
|
|
"};\n",
|
|
8 },
|
|
|
|
/* str field beside a slice field — the untouched str arm must still
|
|
* store its full header (regression pin). */
|
|
{ "str_name_len",
|
|
"package main;\n"
|
|
"type sn = struct { s: []u8, name: str };\n"
|
|
"export fn main() i32 = {\n"
|
|
"\tlet hb: [8]u8;\n"
|
|
"\tlet b: []u8; b.ptr = &hb[0]; b.len = 3; b.cap = 8;\n"
|
|
"\tlet y: sn = sn { s = b, name = \"hello\" };\n"
|
|
"\treturn y.name.len: i32;\n"
|
|
"};\n",
|
|
5 },
|
|
|
|
{ "str_s_len",
|
|
"package main;\n"
|
|
"type sn = struct { s: []u8, name: str };\n"
|
|
"export fn main() i32 = {\n"
|
|
"\tlet hb: [8]u8;\n"
|
|
"\tlet b: []u8; b.ptr = &hb[0]; b.len = 3; b.cap = 8;\n"
|
|
"\tlet y: sn = sn { s = b, name = \"hello\" };\n"
|
|
"\treturn y.s.len: i32;\n"
|
|
"};\n",
|
|
3 },
|
|
};
|
|
|
|
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/slf_%d_%d.ww", getpid(), i);
|
|
snprintf(tmpdir, sizeof tmpdir, "/tmp/slf_%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;
|
|
}
|
|
|
|
/* asm_byte_identical — w6c vs w6c_ww .s for the same source must match.
|
|
* Pre-fix BOTH stages dropped len+cap identically (byte-id green on
|
|
* both-wrong); the symmetric fix keeps them byte-identical and correct. */
|
|
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/slf_asm_%d_%d.ww", getpid(), i);
|
|
snprintf(cs, sizeof cs, "/tmp/slf_asm_%d_%d_c.s", getpid(), i);
|
|
snprintf(ws, sizeof ws, "/tmp/slf_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 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, "structlit_slice_field: 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,
|
|
"structlit_slice_field[%s][%s]: exit=%d want=%d\n",
|
|
drivers[d].name, rows[i].label,
|
|
got, rows[i].want);
|
|
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,
|
|
"structlit_slice_field: %d/%d fixtures failed\n",
|
|
fail, total);
|
|
return 1;
|
|
}
|
|
printf("structlit_slice_field: %d/%d ok\n", total, total);
|
|
return 0;
|
|
}
|