Files
ww/test/wcc/680_arr_elem_field.c
Hojun-Cho 9ef9bef340 w6c+selfhost: cgen N_DOT N_INDEX-lhs branch (closes #8)
cstage cmd/w6c/cgen.c gained the missing N_DOT N_INDEX-lhs branch.
Covers both [N]*Struct and [N]Struct via fldloadop. wwstage already
handled [N]*Struct since 7c75dd2; refactored to mirror cstage exactly
and added [N]Struct. The spill workaround in dotchainresolve stays
(Pike rule); task #14 retires it as a follow-up.

Wwstage cgassign N_DOT(N_INDEX,...) silent store-drop discovered in
scope, filed as task #16.
2026-05-13 23:45:35 +09:00

233 lines
7.2 KiB
C

/*
* 680_arr_elem_field — `arr[i].field` lowers to a single (idx*esz +
* base) → field-load sequence, not a fall-through that returns the
* element value (or address) as the dot's result. Filed from task #8
* (originally framed as a wwstage bug; investigation flipped — cstage's
* N_DOT had no N_INDEX-lhs branch and fell through, leaving the
* caller's (AX, BX) shape junk for str/scalar leaf consumers).
*
* The cgenutil dotchainresolve workaround spills via `let nd = stk[i];
* nd.str` to keep cstage from seeing the direct shape; this test pins
* the direct shape so the spill can be retired in a follow-up.
*
* Coverage — both `[N]*Struct` and `[N]Struct` shapes, str / i32 / u8
* fields. Cstage and wwstage each on every fixture; wwstage gated on
* access(X_OK).
*
* `&arr[i].field` (TK_AMP through chained N_DOT N_INDEX) is task #9
* territory and intentionally out of scope here. Write-side
* `arr[i].field = v` is covered elsewhere — the wwstage write gap
* surfaces separately.
*/
#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[] = {
/* [N]*Struct, str field at non-zero offset. The original
* task-#8 repro — stk[0].name where stk: [16]*nd.
* Returns len("hello") = 5. */
{ "ptr_arr_str_field",
"type nd = struct {\n"
" a: i32, b: i32,\n"
" name: str,\n"
"};\n"
"fn main() i32 = {\n"
" let v: nd; v.a = 1; v.b = 2; v.name = \"hello\";\n"
" let stk: [16]*nd; stk[0] = &v;\n"
" let s: str = stk[0].name;\n"
" return s.len: i32;\n"
"};\n",
5 },
/* [N]*Struct, scalar i32 field. Pins the non-str leaf load
* path through fldloadop (MOVSXD here for i32). Returns 42. */
{ "ptr_arr_i32_field",
"type nd = struct { name: str, x: i32 };\n"
"fn main() i32 = {\n"
" let v: nd; v.name = \"hi\"; v.x = 42;\n"
" let stk: [16]*nd; stk[0] = &v;\n"
" return stk[0].x;\n"
"};\n",
42 },
/* [N]*Struct, u8 field — pins the sub-word zero-extend (MOVZBQ)
* branch. Returns 7. */
{ "ptr_arr_u8_field",
"type nd = struct { tag: u8, pad: u8, x: i32 };\n"
"fn main() i32 = {\n"
" let v: nd; v.tag = 7u8; v.pad = 0u8; v.x = 99;\n"
" let stk: [8]*nd; stk[0] = &v;\n"
" return stk[0].tag: i32;\n"
"};\n",
7 },
/* [N]Struct (value array), str field. Element address goes
* straight into AX; field load reads at foff(AX). Returns
* len("world") = 5. */
{ "val_arr_str_field",
"type nd = struct { name: str, x: i32 };\n"
"fn setit(p: *nd, s: str, vv: i32) void = { p.name = s; p.x = vv; };\n"
"fn main() i32 = {\n"
" let arr: [4]nd;\n"
" setit(&arr[1], \"world\", 100);\n"
" let s: str = arr[1].name;\n"
" return s.len: i32;\n"
"};\n",
5 },
/* [N]Struct, scalar i32 field. Pins the value-array path for a
* non-str leaf — same address compute, MOVL load. Returns 100. */
{ "val_arr_i32_field",
"type nd = struct { name: str, x: i32 };\n"
"fn setit(p: *nd, s: str, vv: i32) void = { p.name = s; p.x = vv; };\n"
"fn main() i32 = {\n"
" let arr: [4]nd;\n"
" setit(&arr[2], \"q\", 100);\n"
" return arr[2].x;\n"
"};\n",
100 },
/* [N]Struct, u8 leaf — pins the value-array sub-word path
* (no MOVQ-to-deref, direct address arithmetic into AX) then
* fldloadop's MOVZBQ. Returns 9. */
{ "val_arr_u8_field",
"type nd = struct { tag: u8, pad: u8, x: i32 };\n"
"fn setit(p: *nd, t: u8, vv: i32) void = { p.tag = t; p.x = vv; };\n"
"fn main() i32 = {\n"
" let arr: [4]nd;\n"
" setit(&arr[2], 9u8, 50);\n"
" return arr[2].tag: i32;\n"
"};\n",
9 },
/* [N]*Struct, i8 leaf with a negative value — pins that
* fldloadop sign-extends (MOVBQSX) through the new branch
* rather than the old MOVZBQ. Compare against -3 in i32 to
* avoid the WEXITSTATUS truncation collision (sign-ext: -3,
* zero-ext: 253 — both clash mod 256 if returned directly). */
{ "ptr_arr_i8_signed",
"type nd = struct { tag: i8, pad: u8, x: i32 };\n"
"fn main() i32 = {\n"
" let v: nd; v.tag = -3i8; v.pad = 0u8; v.x = 0;\n"
" let stk: [4]*nd; stk[0] = &v;\n"
" let t: i32 = stk[0].tag: i32;\n"
" if (t == -3) { return 42; };\n"
" return 0;\n"
"};\n",
42 },
/* Idx is a non-zero variable expression — pins the IMULQ path
* and confirms the address compute doesn't accidentally fold
* to a constant displacement. Returns len("third") = 5. */
{ "ptr_arr_dyn_idx",
"type nd = struct { name: str, x: i32 };\n"
"fn main() i32 = {\n"
" let a: nd; a.name = \"first\"; a.x = 1;\n"
" let b: nd; b.name = \"two\"; b.x = 2;\n"
" let c: nd; c.name = \"third\"; c.x = 3;\n"
" let stk: [4]*nd;\n"
" stk[0] = &a; stk[1] = &b; stk[2] = &c;\n"
" let i: i32 = 2;\n"
" let s: str = stk[i].name;\n"
" return s.len: i32;\n"
"};\n",
5 },
};
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/wae_%d_%d.ww", getpid(), i);
snprintf(tmpdir, sizeof tmpdir, "/tmp/wae_%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[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, "arr_elem_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,
"arr_elem_field[%s][%s]: exit=%d want=%d\n",
drivers[d].name, rows[i].label,
got, rows[i].want);
fail++;
}
}
}
if (fail) {
fprintf(stderr,
"arr_elem_field: %d/%d fixtures failed\n", fail, total);
return 1;
}
printf("arr_elem_field: %d/%d ok\n", total, total);
return 0;
}