fold-1 narrows a float literal at materialisation only when its node already carries an f32 type — the `f32` suffix. The common un-suffixed case `let x: f32 = 1.0` stays ty_untyped_float through the checker, so the node is never f32-typed: the literal materialises as a 64-bit double and the f32 consumer reads the low 4 bytes (0.0f for clean values). Stamp such a literal f32 when an f32 target type is in context, the way harec's lower_implicit_cast does (ref/harec/src/check.c:148): a float literal's bit pattern is target-dependent, unlike a width-agnostic int immediate, so the value-producing node must carry the type. Scoped to untyped_float -> f32 only (f64 already works via cgen's double default). coerce_floatlit (cstage clet + cstmt N_RETURN) / coercefloatlit (wwstage resolvewalk's post-order N_LET / N_RETURN handler) are logically identical. The wwstage stamp is placed AFTER the child re-walk: the post-order exprtype dispatch re-stamps a bare N_FLOATLIT back to untyped_float, so coercing earlier (checkletassign) would be undone. Scope is let-init and return ONLY, aligned down to the leaner wwstage (rule 10). The wwstage cgen's exprfloatkind hardcodes a float literal to f64 and cgbin / the unary negate pick f32 off the operands, not the node stamp — so a stamped literal in an arith-binop / behind a unary minus narrows in cstage (ADDSS) but not wwstage (ADDSD), a byte-id break. The wwstage checker also has no assign / param-typed call-arg / per-field struct-lit site. binop, unary-minus, assign, call-arg, struct-field wait on #120 (wwstage cgen + checker build-out). 965_f32stamp_run: cstage run + cs==ww byte-id over un-suffixed let-init and return literals, the hole 964 left open. Regen w6c/wwdump combined.ww embeds.
217 lines
6.8 KiB
C
217 lines
6.8 KiB
C
/*
|
|
* 965_f32stamp_run — runtime + byte-id regression net for #104 fold-2: an
|
|
* UN-suffixed float literal in an f32 context (`let x: f32 = 1.0`, `return
|
|
* 1.0` from an f32 fn) must be stamped f32 by the checker so fold-1's cgen
|
|
* narrow (CVTSD2SS at the literal materialise site) fires. Without the stamp
|
|
* the literal stays ty_untyped_float, materialises as a 64-bit double, and
|
|
* the f32 consumer reads the LOW 4 BYTES of that double — 0x00000000 == 0.0f
|
|
* for clean values (`1.0` -> 0.0f, so `x: f64 != 1.0` trips). fold-1 (964)
|
|
* only covered SUFFIXED literals (`1.0f32`); the un-suffixed common case was
|
|
* its documented hole, closed here.
|
|
*
|
|
* Both checkers stamp the literal: cstage cmd/wcc/check.c coerce_floatlit (at
|
|
* clet + cstmt N_RETURN), wwstage selfhost/cmd/wcc/check.ww coercefloatlit (in
|
|
* resolvewalk's post-order N_LET / N_RETURN handler — placed AFTER the child
|
|
* re-walk so the post-order exprtype re-stamp doesn't undo it). Both stages
|
|
* emit byte-identical asm, so the 990-997 byte-id gates can NEVER catch a
|
|
* reintroduction — only an executed-and-checked runtime probe can.
|
|
*
|
|
* SCOPE (#104 fold-2): the stamp fires at let-init and return ONLY. binop
|
|
* (`1.0 + x_f32`), unary minus (`-1.0`), assign, call-arg, and struct-field
|
|
* are DEFERRED to #120 — the wwstage cgen's exprfloatkind (cgenutil.ww)
|
|
* hardcodes a float literal to f64 and picks f32 off the operands, not the
|
|
* node stamp, so a stamped literal in those positions does not narrow in
|
|
* wwstage (cs would emit ADDSS, ww ADDSD — a byte-id break). This probe
|
|
* therefore uses bare let-init / return literals exclusively.
|
|
*
|
|
* Each row carries BOTH dimensions (like 955 / 964):
|
|
* (a) cstage `ww build` + run, asserting the exit code.
|
|
* (b) w6c vs w6c_ww `.s` cmp — FAILS if the stages diverge (rule-10).
|
|
*/
|
|
#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_exit; };
|
|
|
|
static const struct row rows[] = {
|
|
/* The hole 964 leaves: a bare (UN-suffixed) f32-context literal. On
|
|
* the bug `let x: f32 = 1.0` stores the low 4 bytes of double 1.0
|
|
* (== 0x00000000 == 0.0f), so x:f64 == 0.0 != 1.0 -> 1. */
|
|
{ "let_one",
|
|
"package main;\n"
|
|
"export fn main() i32 = {\n"
|
|
" let x: f32 = 1.0;\n"
|
|
" if (x: f64 != 1.0) { return 1; };\n"
|
|
" return 0;\n"
|
|
"};\n", 0 },
|
|
/* a bare decimal literal truncated through i32: 8.0 -> 8. On the bug
|
|
* the f32 slot holds 0.0f -> 0. */
|
|
{ "let_decimal",
|
|
"package main;\n"
|
|
"export fn main() i32 = {\n"
|
|
" let y: f32 = 8.0;\n"
|
|
" return y: i32;\n"
|
|
"};\n", 8 },
|
|
/* a fractional value (exactly representable): 0.5. Bug -> 0.0f, so
|
|
* p:f64 == 0.0 != 0.5 -> 1. */
|
|
{ "let_frac",
|
|
"package main;\n"
|
|
"export fn main() i32 = {\n"
|
|
" let p: f32 = 0.5;\n"
|
|
" if (p: f64 != 0.5) { return 1; };\n"
|
|
" return 0;\n"
|
|
"};\n", 0 },
|
|
/* return context: an f32 fn returning a bare literal, truncated to
|
|
* i32 at the call site. 2.0 -> 2. On the bug the X0 single is the
|
|
* low half of double 2.0 (== 0.0f) -> 0. */
|
|
{ "return_bare",
|
|
"package main;\n"
|
|
"fn g() f32 = { return 2.0; };\n"
|
|
"export fn main() i32 = {\n"
|
|
" return g(): i32;\n"
|
|
"};\n", 2 },
|
|
/* return feeding a let, both un-suffixed: the literal narrows in the
|
|
* fn return, the let-init binds the (already-f32) call value. 4.0 ->
|
|
* 4. */
|
|
{ "return_then_let",
|
|
"package main;\n"
|
|
"fn h() f32 = { return 4.0; };\n"
|
|
"export fn main() i32 = {\n"
|
|
" let r: f32 = h();\n"
|
|
" if (r: f64 != 4.0) { return 1; };\n"
|
|
" return r: i32;\n"
|
|
"};\n", 4 },
|
|
{ NULL, NULL, 0 }
|
|
};
|
|
|
|
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);
|
|
int cb = fgetc(fb);
|
|
if (ca != cb) { rc = -1; break; }
|
|
if (ca == EOF) break;
|
|
}
|
|
fclose(fa); fclose(fb);
|
|
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 w6c[1100], w6c_ww[1100];
|
|
snprintf(w6c, sizeof w6c, "%s/w6c", bin);
|
|
snprintf(w6c_ww, sizeof w6c_ww, "%s/w6c_ww", bin);
|
|
if (access(w6c_ww, X_OK) != 0) {
|
|
fprintf(stderr, "f32stamp: w6c_ww missing — cannot run the "
|
|
"cs==ww byte-id gate (the whole point of this test)\n");
|
|
return 1;
|
|
}
|
|
|
|
int n = 0, fail = 0;
|
|
for (int i = 0; rows[i].src; i++, n++) {
|
|
char src[64];
|
|
snprintf(src, sizeof src, "/tmp/wwf32s_%d_%d.ww", getpid(), i);
|
|
FILE *f = fopen(src, "wb");
|
|
if (f == NULL) { fail++; continue; }
|
|
fputs(rows[i].src, f);
|
|
fclose(f);
|
|
|
|
/* (a) cstage build + run. */
|
|
char tmpdir[64];
|
|
snprintf(tmpdir, sizeof tmpdir, "/tmp/wwf32s_%d_d_%d",
|
|
getpid(), i);
|
|
mkdir(tmpdir, 0755);
|
|
|
|
char cmd[2048];
|
|
snprintf(cmd, sizeof cmd, "cd %s && %s/ww build %s",
|
|
tmpdir, bin, src);
|
|
if (runwait(cmd) != 0) {
|
|
fprintf(stderr, "row[%s]: cstage build failed\n",
|
|
rows[i].label);
|
|
fail++;
|
|
unlink(src); rmdir(tmpdir);
|
|
continue;
|
|
}
|
|
|
|
char outbin[128];
|
|
const char *base = strrchr(src, '/');
|
|
base = base ? base + 1 : src;
|
|
snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base);
|
|
char *dot = strrchr(outbin, '.');
|
|
if (dot && strcmp(dot, ".ww") == 0) *dot = '\0';
|
|
|
|
int got = runwait(outbin);
|
|
if (got != rows[i].want_exit) {
|
|
fprintf(stderr, "row[%s]: cstage exit %d, want %d\n",
|
|
rows[i].label, got, rows[i].want_exit);
|
|
fail++;
|
|
}
|
|
unlink(outbin); rmdir(tmpdir);
|
|
|
|
/* (b) cs==ww byte-id gate: emit .s from both stages, cmp. */
|
|
char cs_s[64], ws_s[64];
|
|
snprintf(cs_s, sizeof cs_s, "/tmp/wwf32s_%d_%d_cs.s",
|
|
getpid(), i);
|
|
snprintf(ws_s, sizeof ws_s, "/tmp/wwf32s_%d_%d_ww.s",
|
|
getpid(), i);
|
|
|
|
snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null",
|
|
w6c, cs_s, src);
|
|
if (runwait(cmd) != 0) {
|
|
fprintf(stderr, "row[%s]: w6c failed\n", rows[i].label);
|
|
fail++; unlink(src); continue;
|
|
}
|
|
snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null",
|
|
w6c_ww, ws_s, src);
|
|
if (runwait(cmd) != 0) {
|
|
fprintf(stderr, "row[%s]: w6c_ww failed\n",
|
|
rows[i].label);
|
|
fail++; unlink(src); unlink(cs_s); continue;
|
|
}
|
|
if (slurp_eq(cs_s, ws_s) != 0) {
|
|
fprintf(stderr,
|
|
"row[%s]: cstage/wwstage .s DIFFER (rule-10 "
|
|
"byte-id violation)\n", rows[i].label);
|
|
fail++;
|
|
}
|
|
unlink(src); unlink(cs_s); unlink(ws_s);
|
|
}
|
|
|
|
if (fail) {
|
|
fprintf(stderr, "%d/%d f32 un-suffixed stamp tests failed\n",
|
|
fail, n);
|
|
return 1;
|
|
}
|
|
printf("f32stamp: %d/%d ok (cstage run + cs==ww byte-id)\n", n, n);
|
|
return 0;
|
|
}
|