wcc/ww: cgcall drain pops widened GP words before the float arm
The arg-drain loop checked node_isfloat before popping a widened arg's GP words, so a float arg adjacent to a widened (tagged) arg read the wrong stack slot: the f64 took the widened payload (#30), or the float arm ate the tag word into X0 and the payload landed in DI as the tag (#48). One missing branch, two manifestations — mirror cstage's widen-first pop (cgen.c:9650-9665). Review items #30+#48 (fold reviewer-verified one-mechanism against the cstage twin).
This commit is contained in:
221
test/wcc/949_f9_float_run.c
Normal file
221
test/wcc/949_f9_float_run.c
Normal file
@@ -0,0 +1,221 @@
|
||||
/*
|
||||
* 949_f9_float_run — F9 argument widen/drain ABI (float) cluster.
|
||||
*
|
||||
* The cgcall push/drain handling of a CONCRETE arg widened into a
|
||||
* tagged-union parameter slot, where a float is involved:
|
||||
*
|
||||
* #30 + #48 (align-UP, wwstage-only): the cgcall DRAIN loop had no
|
||||
* widen-first pop branch. A concrete arg widened into a tagged param
|
||||
* was pushed as slotsize/8 GP words (tag + payload), but the drain
|
||||
* classified per the ARG's source type:
|
||||
* #30 — a float arg AFTER a widened (i64|void) arg: the widened
|
||||
* box under-drained by one GP word, so `MOVSD (SP),X0` for
|
||||
* the following f64 read the box's leftover payload word.
|
||||
* #48 — the widened arg IS an f64 source: source-type=f64 hit the
|
||||
* float arm, so `MOVSD (SP),X0` ate the TAG word into X0 and
|
||||
* the payload landed in DI as the tag → callee fell through.
|
||||
* Both are one missing branch — a widen-first GP pop placed BEFORE
|
||||
* the float check, draining slotsize/8 words into the integer arg
|
||||
* cursor (DI=tag, SI=payload). cstage already does this (precomputed
|
||||
* widen[i]); this aligns wwstage UP and the rows pin cs==ww byte-id.
|
||||
*
|
||||
* REGISTER-CURSOR INVARIANT (rob): a lucky-but-wrong float shuffle can pass
|
||||
* exit codes, so the run rows use values that only survive if the tag/
|
||||
* payload land in the right GP regs AND the float in the right XMM:
|
||||
* #30 returns 0 iff f's b (the f64 arg following the widen) == 1.5;
|
||||
* #48 returns 2 iff the (i64|f64) box's tag selects the f64 arm (DI=tag).
|
||||
* Both also assert cstage/wwstage .s byte-identical.
|
||||
*
|
||||
* NNN<950, self-contained (/tmp, no imports; #30 returns i32 rather than
|
||||
* importing os, so the asm byte-id row can run w6c/w6c_ww directly).
|
||||
*/
|
||||
#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;
|
||||
}
|
||||
|
||||
static int
|
||||
slurp_eq(const char *a, const char *b)
|
||||
{
|
||||
FILE *fa = fopen(a, "rb");
|
||||
FILE *fb = fopen(b, "rb");
|
||||
if (!fa || !fb) { if (fa) fclose(fa); if (fb) fclose(fb); return -1; }
|
||||
int rc = 0;
|
||||
for (;;) {
|
||||
int ca = fgetc(fa), cb = fgetc(fb);
|
||||
if (ca != cb) { rc = -1; break; }
|
||||
if (ca == EOF) break;
|
||||
}
|
||||
fclose(fa); fclose(fb);
|
||||
return rc;
|
||||
}
|
||||
|
||||
#define K_RUN 0
|
||||
#define K_BUILDERR 1
|
||||
|
||||
struct row { const char *label; const char *src; int want;
|
||||
int kind; const char *experr; };
|
||||
|
||||
static const struct row rows[] = {
|
||||
/* #30: a widened (i64|void) arg FOLLOWED by an f64 arg. Pre-fix the
|
||||
* f64 drain (MOVSD (SP),X0) read the widened box's leftover payload
|
||||
* word (int 7) instead of 1.5, so r != 1.5 → 1. */
|
||||
{ "widen_then_float",
|
||||
"package main;\n"
|
||||
"fn f(a: (i64 | void), b: f64) f64 = { return b; };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let r: f64 = f(7, 1.5);\n"
|
||||
" if (r == 1.5) { return 0; };\n"
|
||||
" return 1;\n"
|
||||
"};\n", 0, K_RUN, NULL },
|
||||
/* #48: the widened arg IS an f64 source (i64|f64). Pre-fix the float
|
||||
* arm ate the tag word into X0 and the payload landed in DI as the
|
||||
* tag, so the match fell through both arms → 9. Correct = 2 (f64). */
|
||||
{ "float_source_widen",
|
||||
"package main;\n"
|
||||
"fn g(v: (i64 | f64)) i32 = {\n"
|
||||
" match (v) {\n"
|
||||
" case let n: i64 =>\n"
|
||||
" return 1;\n"
|
||||
" case let d: f64 =>\n"
|
||||
" return 2;\n"
|
||||
" };\n"
|
||||
" return 9;\n"
|
||||
"};\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let d: f64 = 3.5;\n"
|
||||
" return g(d);\n"
|
||||
"};\n", 2, K_RUN, NULL },
|
||||
};
|
||||
|
||||
static int
|
||||
run_driver(const char *driver, const struct row *r, int i)
|
||||
{
|
||||
char src[96], tmpdir[96], errf[96], cmd[1024];
|
||||
snprintf(src, sizeof src, "/tmp/f9f_%d_%d.ww", getpid(), i);
|
||||
snprintf(tmpdir, sizeof tmpdir, "/tmp/f9f_%d_d_%d", getpid(), i);
|
||||
snprintf(errf, sizeof errf, "/tmp/f9f_%d_e_%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 >/dev/null 2>%s",
|
||||
tmpdir, driver, src, errf);
|
||||
int brc = runwait(cmd);
|
||||
if (brc != 0) {
|
||||
fprintf(stderr, "row[%s]: build via %s failed\n",
|
||||
r->label, driver);
|
||||
unlink(src); unlink(errf); rmdir(tmpdir);
|
||||
return -1;
|
||||
}
|
||||
|
||||
const char *base = strrchr(src, '/');
|
||||
base = base ? base + 1 : src;
|
||||
char outbin[256];
|
||||
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); unlink(errf); rmdir(tmpdir);
|
||||
if (got != r->want) {
|
||||
fprintf(stderr, "row[%s]: %s exit %d, want %d\n",
|
||||
r->label, driver, got, r->want);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
asm_byte_identical(const char *bin, const struct row *r, int i)
|
||||
{
|
||||
char src[96], cs[96], ws[96], cmd[1024];
|
||||
snprintf(src, sizeof src, "/tmp/f9f_asm_%d_%d.ww", getpid(), i);
|
||||
snprintf(cs, sizeof cs, "/tmp/f9f_asm_%d_%d_c.s", getpid(), i);
|
||||
snprintf(ws, sizeof ws, "/tmp/f9f_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;
|
||||
}
|
||||
int rc = slurp_eq(cs, ws);
|
||||
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[2080];
|
||||
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[2120], wdrv[2120];
|
||||
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
|
||||
snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin);
|
||||
|
||||
int n = (int)(sizeof rows / sizeof rows[0]);
|
||||
int total = 0, fail = 0;
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
total++;
|
||||
if (run_driver(cdrv, &rows[i], i) != 0) fail++;
|
||||
}
|
||||
if (access(wdrv, X_OK) == 0) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
total++;
|
||||
if (run_driver(wdrv, &rows[i], i) != 0) fail++;
|
||||
}
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (rows[i].kind == K_BUILDERR)
|
||||
continue;
|
||||
total++;
|
||||
if (asm_byte_identical(bin, &rows[i], i) != 0) fail++;
|
||||
}
|
||||
}
|
||||
|
||||
if (fail) {
|
||||
fprintf(stderr, "f9_float: %d/%d checks failed\n",
|
||||
fail, total);
|
||||
return 1;
|
||||
}
|
||||
printf("f9_float: %d/%d ok\n", total, total);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user