cstage+selfhost+test: full-element store for [N]str array literals (#21)
[N]str array literals wrote only the .ptr half of each element.
cstage used esz=16 from `lu->sub->size` and a single per-element
MOVQ → .len trailed uninitialized stack residue. Wwstage was worse:
primsize("str")=0 fell through to esz=8, so element i+1's ptr-MOVQ
clobbered element i's .len slot, scrambling everything.
Worker-18 sidestepped during #18 by rewriting array primer rows to
[N]i64.
cstage cgen.c N_ARRLIT TY_STR branch: emit AX → base+i*16 then
BX → base+i*16+8. Repeat-`...` path mirrored. type_isstr handles
TY_UNTYPED_STR + TY_NAMED-aliased-str.
Wwstage cgenstmt.ww: isstrel flag conditionally drives the two-MOVQ
store in both the per-element walk and the repeat fill. The dispatch
loop was refactored to unify FIELD/ellipsis branches via isellip,
cleaning up the duplicated arms.
Wwstage cgenutil.ww slotsize/letslotsize: TNAME-"str" element gets
esz=16, replacing the primsize=0 → 8B fallback. Without this the
frame collapsed to 24B for [3]str.
Slice (24B), struct, tuple, tagged element arrays have the same root
cause but distinct width/layout concerns — deferred to #35 per rob.
Test 711 (arrlit_str_full): 7 rows × 2 stages = 14 fixtures —
str_lens_3el, str_ptrs_3el, str_repeat_5el (TK_ELLIPSIS), bool_3el,
rune_3el, i32_3el, i64_3el. Rune relies on the pre-existing esz==4
→ MOVL path (incidental correctness); sibling slot types pinned as
regression nets.
Followups filed: #34 (wwstage cgindex truncate on [N]str bare-let
read side, surfaced by this fix), #35 (composite element types),
#36 (primsize-returns-0-default-to-8 cleanup).
This commit is contained in:
229
test/wcc/711_arrlit_str_full.c
Normal file
229
test/wcc/711_arrlit_str_full.c
Normal file
@@ -0,0 +1,229 @@
|
||||
/*
|
||||
* 711_arrlit_str_full — `let xs: [N]str = [...]` writes BOTH halves
|
||||
* of every element (task #21).
|
||||
*
|
||||
* Pre-fix: cgen's N_LET / N_ARRLIT branch dispatched its per-element
|
||||
* store off a single esz / mop pair derived from `lu->sub->size`
|
||||
* (cstage) or `primsize(elem.str)` (wwstage). Both fell through to a
|
||||
* single MOVQ AX, off+i*esz(BP) for a str element — leaving the .len
|
||||
* half (off+i*esz+8) as whatever stack residue the prologue's SUBQ
|
||||
* happened to land on. Wwstage was worse: primsize("str") returns 0,
|
||||
* so esz collapsed to 8, and the per-element stride was wrong too —
|
||||
* element i+1's MOVQ overwrote what should have been element i's
|
||||
* .len half.
|
||||
*
|
||||
* Fix: detect str-element arrays at letslotsize / slotsize / cgen
|
||||
* dispatch and emit both halves per element — MOVQ AX, off+i*16(BP)
|
||||
* for .ptr, MOVQ BX, off+i*16+8(BP) for .len. Symmetric across cstage
|
||||
* cgen.c (N_LET / N_ARRLIT, type_isstr dispatch) and wwstage
|
||||
* cgenstmt.ww + cgenutil.ww (slotsize / letslotsize TNAME-"str"
|
||||
* special-case, cgenstmt isstrel branch). bool / rune / iN element
|
||||
* arrays were never broken — their primitive store widths covered
|
||||
* the full element — but they're pinned here as regressions so a
|
||||
* future esz refactor can't quietly redo the slot-rounding gap.
|
||||
*
|
||||
* What this test pins (runtime only):
|
||||
* - [3]str literal: every element's .len reads back correctly
|
||||
* (pre-fix: zero or stack residue).
|
||||
* - [3]str literal: every element's .ptr → first byte reads back
|
||||
* correctly (the ptr half was always right; sanity).
|
||||
* - [3]bool literal: regression-pin true / false / true.
|
||||
* - [3]rune literal: regression-pin primitive size 4 element.
|
||||
* - [3]i32 / [3]i64 literal: regression-pin primitive widths.
|
||||
* - [5]str = ["x", ...] repeat marker: all 5 slots get full 16B
|
||||
* stores (pre-fix: trailing slots' .len = zero).
|
||||
*
|
||||
* Slice / struct / tuple / tagged element arrays are NOT covered:
|
||||
* the read side (cgindex of an [N]slice) has its own truncating-to-
|
||||
* ptr bug, so an end-to-end repro surfaces sub-issues. Tracked as a
|
||||
* follow-up.
|
||||
*
|
||||
* Asm byte-identity across stages is NOT diffed here: 995_self_rebuild
|
||||
* covers the broader cross-stage drift surface, and the str / repeat
|
||||
* rows produce identical MOVQ pairs across stages by construction.
|
||||
*/
|
||||
#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[] = {
|
||||
/* 1. [3]str — every .len reads back correctly. Exit code is
|
||||
* 100*a[0].len + 10*a[1].len + a[2].len = 100*2 + 10*6 + 1 = 261
|
||||
* mod 256 = 5. Pre-fix: zero (all .len halves uninit). */
|
||||
{ "str_lens_3el",
|
||||
"fn main() i32 = {\n"
|
||||
" let a: [3]str = [\"hi\", \"byebye\", \"x\"];\n"
|
||||
" let p: i32 = a[0].len: i32;\n"
|
||||
" let q: i32 = a[1].len: i32;\n"
|
||||
" let r: i32 = a[2].len: i32;\n"
|
||||
" return p * 100i32 + q * 10i32 + r;\n"
|
||||
"};\n",
|
||||
5 },
|
||||
/* 2. [3]str — every .ptr is reachable. Reads element 0's first
|
||||
* byte through `.ptr` to confirm the ptr half wasn't broken by
|
||||
* the .len-half fix. 'h' (104) - 100 = 4. */
|
||||
{ "str_ptrs_3el",
|
||||
"fn main() i32 = {\n"
|
||||
" let a: [3]str = [\"hi\", \"by\", \"x\"];\n"
|
||||
" let p: *u8 = a[0].ptr;\n"
|
||||
" let c: u8 = *p;\n"
|
||||
" return (c: i32) - 100i32;\n"
|
||||
"};\n",
|
||||
4 },
|
||||
/* 3. [5]str = ["x", ...] — repeat marker fills all 5 slots with
|
||||
* a full 16B store, not just .ptr. Sum each .len: 5*1 = 5.
|
||||
* Pre-fix: a[0].len = 1 from explicit init, a[1..4].len = stack
|
||||
* residue (often 0, but unspecified). */
|
||||
{ "str_repeat_5el",
|
||||
"fn main() i32 = {\n"
|
||||
" let a: [5]str = [\"x\"...];\n"
|
||||
" let s: i32 = 0i32;\n"
|
||||
" let i: i32 = 0;\n"
|
||||
" for (i < 5) {\n"
|
||||
" s += a[i].len: i32;\n"
|
||||
" i += 1;\n"
|
||||
" };\n"
|
||||
" return s;\n"
|
||||
"};\n",
|
||||
5 },
|
||||
/* 4. [3]bool — regression-pin. Pre-fix and post-fix: true/false/
|
||||
* true with element width 1. Exit = 4*b[0] + 2*b[1] + b[2] =
|
||||
* 4 + 0 + 1 = 5. */
|
||||
{ "bool_3el_regression",
|
||||
"fn main() i32 = {\n"
|
||||
" let b: [3]bool = [true, false, true];\n"
|
||||
" let s: i32 = 0i32;\n"
|
||||
" if (b[0]) { s += 4i32; };\n"
|
||||
" if (b[1]) { s += 2i32; };\n"
|
||||
" if (b[2]) { s += 1i32; };\n"
|
||||
" return s;\n"
|
||||
"};\n",
|
||||
5 },
|
||||
/* 5. [3]rune — regression-pin primitive size 4. 'b' - 'a' = 1. */
|
||||
{ "rune_3el_regression",
|
||||
"fn main() i32 = {\n"
|
||||
" let r: [3]rune = ['a', 'b', 'c'];\n"
|
||||
" let v: rune = r[1];\n"
|
||||
" return (v: i32) - 97i32;\n"
|
||||
"};\n",
|
||||
1 },
|
||||
/* 6. [3]i32 — regression-pin primitive size 4. */
|
||||
{ "i32_3el_regression",
|
||||
"fn main() i32 = {\n"
|
||||
" let a: [3]i32 = [10i32, 20i32, 30i32];\n"
|
||||
" return a[0] + a[1] - a[2];\n"
|
||||
"};\n",
|
||||
0 },
|
||||
/* 7. [3]i64 — regression-pin primitive size 8. Sum 1+2+3 = 6. */
|
||||
{ "i64_3el_regression",
|
||||
"fn main() i32 = {\n"
|
||||
" let a: [3]i64 = [1i64, 2i64, 3i64];\n"
|
||||
" return (a[0] + a[1] + a[2]): i32;\n"
|
||||
"};\n",
|
||||
6 },
|
||||
};
|
||||
|
||||
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/wcrarrs_%d_%d.ww", getpid(), i);
|
||||
snprintf(tmpdir, sizeof tmpdir, "/tmp/wcrarrs_%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",
|
||||
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;
|
||||
}
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
const char *bin = getenv("BIN");
|
||||
if (!bin) bin = "out/bin";
|
||||
char absbin[512];
|
||||
if (bin[0] != '/') {
|
||||
char cwd[256];
|
||||
if (getcwd(cwd, sizeof cwd) == NULL) return 1;
|
||||
snprintf(absbin, sizeof absbin, "%s/%s", cwd, bin);
|
||||
bin = absbin;
|
||||
}
|
||||
|
||||
char cdrv[640];
|
||||
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
|
||||
char wdrv[640];
|
||||
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, "arrlit_str_full: 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,
|
||||
"arrlit_str_full[%s][%s]: exit=%d want=%d\n",
|
||||
drivers[d].name, rows[i].label,
|
||||
got, rows[i].want);
|
||||
fail++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (fail) {
|
||||
fprintf(stderr,
|
||||
"arrlit_str_full: %d/%d fixtures failed\n", fail, total);
|
||||
return 1;
|
||||
}
|
||||
printf("arrlit_str_full: %d/%d ok\n", total, total);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user