Indexing a non-ident pointer-yielding base -- a direct cast ((&a):*[4]u32)[i], a call result mk(&a)[i], a slice, a type-assertion -- used wwstage's default 8-byte element stride/load instead of the real element type's, reading garbage (cast-base i32 index: cs=30, ww=0). The cgindex esz derivation gated on a whitelist of base node-kinds (DOT / UN-deref / INDEX); an N_CAST/N_CALL/N_SLICE/N_TYPEASSERT base matched none. Rather than extend the whitelist (whack-a-mole), this mirrors cstage's uniform idx_eff read: N_INDEX keeps its own arm (chained-index byte-id preserved), and every other non-ident base now derives esz/stride/load- width/signedness from the stamped n.type_ -- closing the class by construction (base set ident/dot/un/index/cast/call/slice/typeassert). cstage was already correct (uniform); w6c md5 unchanged. byte-id 990-997 8/8, no lib pin flips. test/wcc/830 (9 base shapes, byte-id per width, signed + unsigned). Folds the N_CALL sibling #22.
285 lines
8.2 KiB
C
285 lines
8.2 KiB
C
/*
|
|
* 830_cast_base_index — indexing a DIRECT-CAST base `(e:*[N]T)[i]` must
|
|
* stride by the cast element type's size, not the 8B default (task
|
|
* #19old, .ai/rob-19old-spec.md). wwstage-only fix; cstage was already
|
|
* correct.
|
|
*
|
|
* The bug: cgindex (selfhost/cmd/wcc/cgenexpr.ww) derived the element
|
|
* stride/load-width `esz` only for N_DOT / N_UN-deref / N_INDEX base
|
|
* node-kinds (read off the stamped index-result tinfo n.type_). An
|
|
* N_CAST base matched NO arm, so esz stayed at the 8B default — for a
|
|
* narrow element (u32/u16/u8) the stride and load width were both
|
|
* wrong and the read returned 0/garbage. cstage reads it uniformly via
|
|
* idx_eff(base->type)->sub->size (cmd/w6c/cgen.c N_INDEX); the fix adds
|
|
* `|| base.kind == nkind.N_CAST` to the stamped-tinfo esz arm, which
|
|
* fixes stride + load width + signedness together (all read from dt).
|
|
*
|
|
* row | shape | stride | want
|
|
* ---------+-------------------------------+--------+------
|
|
* u32_idx | [4]u32, ((&a):*[4]u32)[i] | 4 | 30
|
|
* u16_idx | [4]u16, ((&a):*[4]u16)[i] | 2 | 30
|
|
* u8_idx | [4]u8, ((&a):*[4]u8)[i] | 1 | 40
|
|
* u64_ctl | [4]u64, ((&a):*[4]u64)[i] | 8 | 20 (control)
|
|
*
|
|
* The whitelist of base node-kinds was whack-a-mole (#19old residuals):
|
|
* N_CALL, N_SLICE and N_TYPEASSERT bases ALSO fell to the 8B default. The
|
|
* fix routes every non-ident, non-N_INDEX base through the stamped-n.type_
|
|
* arm (mirrors cstage's uniform idx_eff, no node-kind gate). These rows
|
|
* pin the residual base kinds:
|
|
*
|
|
* call_idx | mk(&a)[i] (N_CALL *[4]u32) | 4 | 30
|
|
* slc_idx | a[0:4][i] (N_SLICE []u16) | 2 | 30
|
|
* asrt_idx | (v as *[4]u16)[i] (N_TYPEASSERT) | 2 | 30
|
|
*
|
|
* The <8B rows are the mutation-sane rows: pre-fix wwstage strides by 8
|
|
* and reads 0/garbage. u64_ctl pins no-regress for the already-correct
|
|
* 8B-stride case. A byte-id row pins cstage==wwstage `.s` for the
|
|
* cast-base index source (the convergence the fix delivers).
|
|
*/
|
|
#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[] = {
|
|
/* u32_idx — stride 4 / MOVL. THE bug: pre-fix wwstage strides by
|
|
* 8 and reads 0. */
|
|
{ "u32_idx",
|
|
"package main;\n"
|
|
"export fn main() i32 = {\n"
|
|
"\tlet a: [4]u32 = [10u32, 20u32, 30u32, 40u32];\n"
|
|
"\tlet i: int = 2;\n"
|
|
"\treturn ((&a): *[4]u32)[i]: i32;\n"
|
|
"};\n",
|
|
30 },
|
|
|
|
/* u16_idx — stride 2 / MOVW. */
|
|
{ "u16_idx",
|
|
"package main;\n"
|
|
"export fn main() i32 = {\n"
|
|
"\tlet a: [4]u16 = [10u16, 20u16, 30u16, 40u16];\n"
|
|
"\tlet i: int = 2;\n"
|
|
"\treturn ((&a): *[4]u16)[i]: i32;\n"
|
|
"};\n",
|
|
30 },
|
|
|
|
/* u8_idx — stride 1 / MOVB. */
|
|
{ "u8_idx",
|
|
"package main;\n"
|
|
"export fn main() i32 = {\n"
|
|
"\tlet a: [4]u8 = [10u8, 20u8, 30u8, 40u8];\n"
|
|
"\tlet i: int = 3;\n"
|
|
"\treturn ((&a): *[4]u8)[i]: i32;\n"
|
|
"};\n",
|
|
40 },
|
|
|
|
/* u64_ctl — stride 8 control: already correct (matches the 8B
|
|
* default), guards no-regress for the wide-element cast base. */
|
|
{ "u64_ctl",
|
|
"package main;\n"
|
|
"export fn main() i32 = {\n"
|
|
"\tlet a: [4]u64 = [10u64, 20u64, 30u64, 40u64];\n"
|
|
"\tlet i: int = 1;\n"
|
|
"\treturn ((&a): *[4]u64)[i]: i32;\n"
|
|
"};\n",
|
|
20 },
|
|
|
|
/* call_idx — N_CALL base mk(&a)[i], stride 4 / MOVL. #19old residual:
|
|
* a call-returning-pointer base also fell to the 8B default. */
|
|
{ "call_idx",
|
|
"package main;\n"
|
|
"fn mk(p: *[4]u32) *[4]u32 = { return p; };\n"
|
|
"export fn main() i32 = {\n"
|
|
"\tlet a: [4]u32 = [10u32, 20u32, 30u32, 40u32];\n"
|
|
"\tlet i: int = 2;\n"
|
|
"\treturn mk(&a)[i]: i32;\n"
|
|
"};\n",
|
|
30 },
|
|
|
|
/* slc_idx — N_SLICE base a[0:4][i], stride 2 / MOVZWQ. #19old
|
|
* residual: a slice-expression base also fell to the 8B default. */
|
|
{ "slc_idx",
|
|
"package main;\n"
|
|
"export fn main() i32 = {\n"
|
|
"\tlet a: [4]u16 = [10u16, 20u16, 30u16, 40u16];\n"
|
|
"\tlet i: int = 2;\n"
|
|
"\treturn a[0:4][i]: i32;\n"
|
|
"};\n",
|
|
30 },
|
|
|
|
/* asrt_idx — N_TYPEASSERT base (v as *[4]u16)[i], stride 2 / MOVZWQ.
|
|
* #19old residual: a type-assert base also fell to the 8B default. */
|
|
{ "asrt_idx",
|
|
"package main;\n"
|
|
"type IP = (*[4]u16 | int);\n"
|
|
"export fn main() i32 = {\n"
|
|
"\tlet a: [4]u16 = [10u16, 20u16, 30u16, 40u16];\n"
|
|
"\tlet v: IP = &a;\n"
|
|
"\tlet i: int = 2;\n"
|
|
"\treturn (v as *[4]u16)[i]: i32;\n"
|
|
"};\n",
|
|
30 },
|
|
};
|
|
|
|
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/cbi_%d_%d.ww", getpid(), i);
|
|
snprintf(tmpdir, sizeof tmpdir, "/tmp/cbi_%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. */
|
|
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/cbi_asm_%d_%d.ww", getpid(), i);
|
|
snprintf(cs, sizeof cs, "/tmp/cbi_asm_%d_%d_c.s", getpid(), i);
|
|
snprintf(ws, sizeof ws, "/tmp/cbi_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, "cast_base_index: 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,
|
|
"cast_base_index[%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,
|
|
"cast_base_index: %d/%d fixtures failed\n", fail, total);
|
|
return 1;
|
|
}
|
|
printf("cast_base_index: %d/%d ok\n", total, total);
|
|
return 0;
|
|
}
|