wcc_ww/cgen: #55 tagged-source arg-widen into wider tagged slot (align-up)
wwstage pushargsrev treated a narrower tagged-union argument widened into
a wider tagged param slot as a concrete variant: taggedvariantindex<0
clamped the tag to 0 and pushed word0 only (deref/index/dot silent-wrong;
ident ran correct only by prefix-union tag-index luck). cstage is correct
(cg_widen_tagged_push routes src_is_tagged unconditionally); align ww UP.
Three arms in cgenutil.ww, all mirroring cstage cgen.c:
- slot-gate the ident aistagged short-circuit so a slot-differ tagged
ident falls to the widen path instead of the raw 2-word push;
- route a tagged source in the widensz>0 arm through @tagscr +
cgwidentaggedstore + push high->low (cgen.c cg_widen_tagged_push);
- cgwidentaggedstore cursor arm (<=32B INDEX/DOT) spills by source
width, zero-pads, and tag-remaps (cgen.c 2698-2714) — was dst-slot
spill of stale high regs with no pad and no remap.
Same-slot tagged->tagged is byte-id-neutral by construction (empty pad +
identity remap). cstage untouched; 4 legs x {aligned, misaligned-tag}
converge ww->cs byte-identical. Pin 944_tagged_widen_arg_run.
This commit is contained in:
287
test/wcc/944_tagged_widen_arg_run.c
Normal file
287
test/wcc/944_tagged_widen_arg_run.c
Normal file
@@ -0,0 +1,287 @@
|
||||
/*
|
||||
* 944_tagged_widen_arg_run — #55 tagged-SOURCE arg widened into a wider
|
||||
* tagged-union PARAM slot (slot-DIFFER).
|
||||
*
|
||||
* A call arg whose own type is a NARROWER tagged union (un16=16B) passed
|
||||
* where the param is a WIDER tagged union (un3=24B). wwstage pushargsrev
|
||||
* mis-pushed every leg but the lucky aligned-ident: the slot-DIFFER arg
|
||||
* fell past the slot-gated aistagged arms to the concrete-variant scalar
|
||||
* boxing (taggedvariantindex<0 → tag clamped to 0 → word0 boxed, real tag
|
||||
* + high words dropped, NO variant remap). cstage routes EVERY tagged
|
||||
* source through cg_widen_tagged_store (cgen.c:2982 src_is_tagged) →
|
||||
* scratch + tag-remap, correct on all 8.
|
||||
*
|
||||
* The fix (ww align-UP, 3 coordinated arms in cgenutil.ww):
|
||||
* 1. the IDENT aistagged gate is now slot-gated (was ungated TRUE) so a
|
||||
* slot-DIFFER ident falls to the widen scratch instead of the no-
|
||||
* remap natural push (the prefix-luck);
|
||||
* 2. pushargsrev's widensz>0 arm routes a tagged source (argistagged)
|
||||
* through @tagscr + cgwidentaggedstore + push high→low (the
|
||||
* pname/argistuple twin);
|
||||
* 3. cgwidentaggedstore's cursor arm (INDEX/DOT ≤32B) spills by the
|
||||
* SOURCE width, zero-pads, and tag-REMAPS (mirrors cstage's
|
||||
* cg_widen_tagged_store cursor + shared pad/remap tail, cgen.c:2695-
|
||||
* 2714) — pre-#55 it spilled by the dst slot (stale high regs) and
|
||||
* skipped the remap.
|
||||
*
|
||||
* row | leg | tag | want
|
||||
* ----------------+-------+-----------+-----
|
||||
* ident_aligned | e | i64=tag0 | 42
|
||||
* deref_aligned | *p | i64=tag0 | 42
|
||||
* index_aligned | xs[0] | i64=tag0 | 42
|
||||
* dot_aligned | w.f | i64=tag0 | 42
|
||||
* ident_remap | e | i64=tag1 | 42 (un16b reorders i64→tag1)
|
||||
* deref_remap | *p | i64=tag1 | 42
|
||||
* index_remap | xs[0] | i64=tag1 | 42
|
||||
* dot_remap | w.f | i64=tag1 | 42
|
||||
*
|
||||
* The remap rows carry the MISALIGNED tag: un16b=(bool|i64) puts i64 at
|
||||
* tag1, widened into un3b=(i64|bool|s16) where i64 is tag0 — so the source
|
||||
* tag MUST be rewritten or the callee reads the wrong arm. An aligned-only
|
||||
* suite false-greens a remap-less fix (the exact trap here, ken #55).
|
||||
*
|
||||
* Pre-fix: deref/index/dot legs cs42/ww!=42 (silent wrong); aligned-ident
|
||||
* ww42 by LUCK but asm cs!=ww. Post-fix: all 8 cs42/ww42, all 8 byte-id.
|
||||
*/
|
||||
#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;
|
||||
}
|
||||
|
||||
struct row { const char *label; const char *src; int want; };
|
||||
|
||||
#define TAKE_ALIGNED \
|
||||
"type s16 = struct { a: i64, b: i64 };\n" \
|
||||
"type un16 = (i64 | bool);\n" \
|
||||
"type un3 = (i64 | bool | s16);\n" \
|
||||
"fn take(e: un3) i64 = {\n" \
|
||||
" match (e) {\n" \
|
||||
" case let v: i64 => { return v; };\n" \
|
||||
" case let b: bool => { return -1; };\n" \
|
||||
" case let s: s16 => { return s.a + s.b; };\n" \
|
||||
" };\n" \
|
||||
"};\n"
|
||||
|
||||
#define TAKE_REMAP \
|
||||
"type s16 = struct { a: i64, b: i64 };\n" \
|
||||
"type un16b = (bool | i64);\n" \
|
||||
"type un3b = (i64 | bool | s16);\n" \
|
||||
"fn take(e: un3b) i64 = {\n" \
|
||||
" match (e) {\n" \
|
||||
" case let v: i64 => { return v; };\n" \
|
||||
" case let b: bool => { return -1; };\n" \
|
||||
" case let s: s16 => { return s.a + s.b; };\n" \
|
||||
" };\n" \
|
||||
"};\n"
|
||||
|
||||
static const struct row rows[] = {
|
||||
{ "ident_aligned",
|
||||
"package main;\n" TAKE_ALIGNED
|
||||
"export fn main() i32 = {\n"
|
||||
" let v: un16 = (42: i64);\n"
|
||||
" if (take(v) != 42) { return 1; };\n"
|
||||
" return 0;\n"
|
||||
"};\n", 0 },
|
||||
{ "deref_aligned",
|
||||
"package main;\n" TAKE_ALIGNED
|
||||
"export fn main() i32 = {\n"
|
||||
" let v: un16 = (42: i64);\n"
|
||||
" let p: *un16 = &v;\n"
|
||||
" if (take(*p) != 42) { return 1; };\n"
|
||||
" return 0;\n"
|
||||
"};\n", 0 },
|
||||
{ "index_aligned",
|
||||
"package main;\n" TAKE_ALIGNED
|
||||
"export fn main() i32 = {\n"
|
||||
" let xs: [2]un16 = [(42: i64), (7: i64)];\n"
|
||||
" if (take(xs[0]) != 42) { return 1; };\n"
|
||||
" return 0;\n"
|
||||
"};\n", 0 },
|
||||
{ "dot_aligned",
|
||||
"package main;\n" TAKE_ALIGNED
|
||||
"type wrap = struct { f: un16 };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let w: wrap = wrap { f = (42: i64) };\n"
|
||||
" if (take(w.f) != 42) { return 1; };\n"
|
||||
" return 0;\n"
|
||||
"};\n", 0 },
|
||||
/* misaligned-tag rows — i64 is tag1 in un16b, tag0 in un3b: the
|
||||
* remap MUST fire or the callee reads the wrong match arm. */
|
||||
{ "ident_remap",
|
||||
"package main;\n" TAKE_REMAP
|
||||
"export fn main() i32 = {\n"
|
||||
" let v: un16b = (42: i64);\n"
|
||||
" if (take(v) != 42) { return 1; };\n"
|
||||
" return 0;\n"
|
||||
"};\n", 0 },
|
||||
{ "deref_remap",
|
||||
"package main;\n" TAKE_REMAP
|
||||
"export fn main() i32 = {\n"
|
||||
" let v: un16b = (42: i64);\n"
|
||||
" let p: *un16b = &v;\n"
|
||||
" if (take(*p) != 42) { return 1; };\n"
|
||||
" return 0;\n"
|
||||
"};\n", 0 },
|
||||
{ "index_remap",
|
||||
"package main;\n" TAKE_REMAP
|
||||
"export fn main() i32 = {\n"
|
||||
" let xs: [2]un16b = [(42: i64), (7: i64)];\n"
|
||||
" if (take(xs[0]) != 42) { return 1; };\n"
|
||||
" return 0;\n"
|
||||
"};\n", 0 },
|
||||
{ "dot_remap",
|
||||
"package main;\n" TAKE_REMAP
|
||||
"type wrap = struct { f: un16b };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let w: wrap = wrap { f = (42: i64) };\n"
|
||||
" if (take(w.f) != 42) { return 1; };\n"
|
||||
" return 0;\n"
|
||||
"};\n", 0 },
|
||||
};
|
||||
|
||||
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/twa_%d_%d.ww", getpid(), i);
|
||||
snprintf(tmpdir, sizeof tmpdir, "/tmp/twa_%d_d_%d", getpid(), i);
|
||||
snprintf(errf, sizeof errf, "/tmp/twa_%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 && timeout 20 %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/twa_asm_%d_%d.ww", getpid(), i);
|
||||
snprintf(cs, sizeof cs, "/tmp/twa_asm_%d_%d_c.s", getpid(), i);
|
||||
snprintf(ws, sizeof ws, "/tmp/twa_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++) {
|
||||
total++;
|
||||
if (asm_byte_identical(bin, &rows[i], i) != 0) fail++;
|
||||
}
|
||||
}
|
||||
|
||||
if (fail) {
|
||||
fprintf(stderr, "tagged_widen_arg: %d/%d checks failed\n",
|
||||
fail, total);
|
||||
return 1;
|
||||
}
|
||||
printf("tagged_widen_arg: %d/%d ok\n", total, total);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user