wcc: stamp un-suffixed f32-context float literals (#104 fold-2)

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.
This commit is contained in:
2026-05-26 12:06:47 +09:00
parent acaf0152da
commit a1dff13ec1
6 changed files with 374 additions and 0 deletions

View File

@@ -333,6 +333,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \
$(BIN)/test_tuprecv_run \
$(BIN)/test_f64xmm_run \
$(BIN)/test_f32lit_run \
$(BIN)/test_f32stamp_run \
$(BIN)/test_tuprecv_f64_run \
$(BIN)/test_floats_run \
$(BIN)/test_size_type_run \
@@ -1139,6 +1140,11 @@ $(BIN)/test_f32lit_run: test/wcc/964_f32lit_run.c $(BIN)/ww \
$(LIB)/libwwrt.a | $(BIN)
$(CC) $(CFLAGS) -o $@ $<
$(BIN)/test_f32stamp_run: test/wcc/965_f32stamp_run.c $(BIN)/ww \
$(BIN)/w6c $(BIN)/w6c_ww $(BIN)/w6a $(BIN)/w6l \
$(LIB)/libwwrt.a | $(BIN)
$(CC) $(CFLAGS) -o $@ $<
$(BIN)/test_tuprecv_f64_run: test/wcc/956_tuprecv_f64_run.c $(BIN)/ww \
$(BIN)/w6c $(BIN)/w6c_ww $(BIN)/w6a $(BIN)/w6l \
$(LIB)/libwwrt.a | $(BIN)

View File

@@ -786,6 +786,46 @@ unify_arith(Checker *c, Pos p, Type *a, Type *b)
type_name(c->a, a), type_name(c->a, b));
}
/* #104 fold-2: an un-suffixed float literal stays ty_untyped_float through
* the checker, so fold-1's cgen narrow (gated on the node's f32-ness) never
* fires for `let x: f32 = 1.0` — the literal materialises as a double whose
* low 4 bytes (0.0f for clean values) are what the f32 consumer reads. Stamp
* such a literal f32 when an f32 target type is in context, mirroring harec's
* lower_implicit_cast sites (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 f32 type.
*
* SCOPED to untyped_float -> f32 ONLY: untyped_float -> f64 already works via
* cgen's double default, so stamping it would broaden the surface for no gain.
*
* Symmetric subset (rule 10), DIRECT N_FLOATLIT at let-init / return only —
* because the wwstage cgen (selfhost/cmd/wcc/cgenutil.ww exprfloatkind) does
* NOT read node.type_ for a float literal: it hardcodes N_FLOATLIT -> f64 and
* cgbin picks f32 off the OPERANDS' float-kind, not the node stamp. So the
* wwstage materialiser (fold-1 isf32type, the one path that does read the
* stamp) narrows a let-init / return literal correctly, but a stamped literal
* inside an arith-binop or behind a unary minus is NOT narrowed by cgbin /
* the negate — cstage would emit ADDSS/SUBSS while wwstage emits ADDSD/SUBSD,
* breaking the cs==ww byte-id gate. Likewise the wwstage checker has no
* N_ASSIGN check, no param-typed call-arg loop, and a head-only struct
* literal. binop / unary-minus / assign / call-arg / struct-field therefore
* wait on the wwstage cgen + checker gaining those (#120). */
static void
coerce_floatlit(Node *n, Type *target)
{
if (n == NULL || target == NULL)
return;
/* Chase the full alias chain (wwstage resolvealias does the same), so
* a doubly-aliased f32 target stamps in both stages or neither. */
Type *u = target;
while (u && u->kind == TY_NAMED)
u = u->under;
if (u == NULL || u->kind != TY_F32)
return;
if (n->kind == N_FLOATLIT && n->type == ty_untyped_float)
n->type = ty_f32;
}
static Type *
cbinop(Checker *c, Node *n)
{
@@ -1757,6 +1797,8 @@ clet(Checker *c, Node *n)
!type_assignable(declared, initt))
err(c, n->pos, "init %s not assignable to declared %s",
type_name(c->a, initt), type_name(c->a, declared));
/* #104 fold-2: `let x: f32 = 1.0` — narrow the init literal to f32. */
coerce_floatlit(n->rhs, declared);
n->type = t;
if (n->str && n->str[0]) {
check_module_shadow(c, n->str, n->pos, "let");
@@ -1795,6 +1837,8 @@ cstmt(Checker *c, Node *n)
&& !type_assignable(c->ret, rt))
err(c, n->pos, "return %s not assignable to %s",
type_name(c->a, rt), type_name(c->a, c->ret));
/* #104 fold-2: `fn g() f32 = { return 1.0; }` — narrow to f32. */
coerce_floatlit(n->lhs, c->ret);
break;
}
case N_IF: {

View File

@@ -7609,6 +7609,17 @@ fn resolvewalk(c: *checker, n: *node) void = {
};
};
// #104 fold-2: narrow a bare f32-context float literal AFTER the
// child walk above — the post-order exprtype dispatch (below) re-
// stamps a bare N_FLOATLIT back to untyped_float, so coercing earlier
// (e.g. in checkletassign) would be undone. Placed here, the f32
// stamp on n.rhs / n.lhs sticks; cgen's fold-1 narrow then fires. let
// / return only — see coercefloatlit's docstring for the rule-10 scope
// (the cstage twin coerces in clet / cstmt N_RETURN). c.fnret is set
// by resolvefnbody for the enclosing fn, mirroring checkretassign.
if (k == nkind.N_LET) { coercefloatlit(c, n.rhs, n.lhs); };
if (k == nkind.N_RETURN) { coercefloatlit(c, n.lhs, c.fnret); };
// A.6.0: post-order dispatch of exprtype on every expression-yielding
// node kind so n.type_ stamps fire universally — not only when reached
// through checkletassign / checkretassign / checktryprop / the size-
@@ -8877,6 +8888,31 @@ fn unifyarith(c: *checker, ltn: *node, rtn: *node) *node = {
return ltn;
};
// coercefloatlit — twin of cstage cmd/wcc/check.c coerce_floatlit (see there
// for the full rationale + the rule-10 scope note). Stamp an un-suffixed
// float literal (whose type_ is the untyped_float singleton) as f32 when the
// target type resolves to f32, so fold-1's cgen narrow (isf32type, cgenexpr.
// ww) fires off the now-f32 node.type_. SCOPED to a DIRECT untyped_float
// N_FLOATLIT at let-init / return only: the wwstage cgen's exprfloatkind
// (cgenutil.ww) hardcodes N_FLOATLIT -> f64 and cgbin / the unary negate pick
// f32 off the operands' float-kind, not the node stamp, so a stamped literal
// inside an arith-binop / behind a unary minus does NOT narrow there —
// binop / unary-minus / assign / call-arg / struct-field wait on #120.
fn coercefloatlit(c: *checker, e: *node, target: *node) void = {
if (e == nil) { return; };
if (target == nil) { return; };
let tu: *node = resolvealias(c, unwrapbang(target));
if (tu == nil) { return; };
if (tu.kind != nkind.N_TNAME) { return; };
if (!streq(tu.str, "f32")) { return; };
if (e.kind == nkind.N_FLOATLIT) {
if ((e.type_: *tinfo) == c.tc.tyuntypedfloat) {
let f32t: *node = mktname(c, "f32");
e.type_ = tinfofornode(c, f32t): *void;
};
};
};
// binoptype — derive the result tnode of an N_BIN operator expression.
// Mirrors cstage cmd/wcc/check.c:598-640 `cbinop`. Operates on tnodes
// returned by exprtype; ptr arithmetic / bitwise / shifts / comparisons /

View File

@@ -458,6 +458,17 @@ fn resolvewalk(c: *checker, n: *node) void = {
};
};
// #104 fold-2: narrow a bare f32-context float literal AFTER the
// child walk above — the post-order exprtype dispatch (below) re-
// stamps a bare N_FLOATLIT back to untyped_float, so coercing earlier
// (e.g. in checkletassign) would be undone. Placed here, the f32
// stamp on n.rhs / n.lhs sticks; cgen's fold-1 narrow then fires. let
// / return only — see coercefloatlit's docstring for the rule-10 scope
// (the cstage twin coerces in clet / cstmt N_RETURN). c.fnret is set
// by resolvefnbody for the enclosing fn, mirroring checkretassign.
if (k == nkind.N_LET) { coercefloatlit(c, n.rhs, n.lhs); };
if (k == nkind.N_RETURN) { coercefloatlit(c, n.lhs, c.fnret); };
// A.6.0: post-order dispatch of exprtype on every expression-yielding
// node kind so n.type_ stamps fire universally — not only when reached
// through checkletassign / checkretassign / checktryprop / the size-
@@ -1726,6 +1737,31 @@ fn unifyarith(c: *checker, ltn: *node, rtn: *node) *node = {
return ltn;
};
// coercefloatlit — twin of cstage cmd/wcc/check.c coerce_floatlit (see there
// for the full rationale + the rule-10 scope note). Stamp an un-suffixed
// float literal (whose type_ is the untyped_float singleton) as f32 when the
// target type resolves to f32, so fold-1's cgen narrow (isf32type, cgenexpr.
// ww) fires off the now-f32 node.type_. SCOPED to a DIRECT untyped_float
// N_FLOATLIT at let-init / return only: the wwstage cgen's exprfloatkind
// (cgenutil.ww) hardcodes N_FLOATLIT -> f64 and cgbin / the unary negate pick
// f32 off the operands' float-kind, not the node stamp, so a stamped literal
// inside an arith-binop / behind a unary minus does NOT narrow there —
// binop / unary-minus / assign / call-arg / struct-field wait on #120.
fn coercefloatlit(c: *checker, e: *node, target: *node) void = {
if (e == nil) { return; };
if (target == nil) { return; };
let tu: *node = resolvealias(c, unwrapbang(target));
if (tu == nil) { return; };
if (tu.kind != nkind.N_TNAME) { return; };
if (!streq(tu.str, "f32")) { return; };
if (e.kind == nkind.N_FLOATLIT) {
if ((e.type_: *tinfo) == c.tc.tyuntypedfloat) {
let f32t: *node = mktname(c, "f32");
e.type_ = tinfofornode(c, f32t): *void;
};
};
};
// binoptype — derive the result tnode of an N_BIN operator expression.
// Mirrors cstage cmd/wcc/check.c:598-640 `cbinop`. Operates on tnodes
// returned by exprtype; ptr arithmetic / bitwise / shifts / comparisons /

View File

@@ -7609,6 +7609,17 @@ fn resolvewalk(c: *checker, n: *node) void = {
};
};
// #104 fold-2: narrow a bare f32-context float literal AFTER the
// child walk above — the post-order exprtype dispatch (below) re-
// stamps a bare N_FLOATLIT back to untyped_float, so coercing earlier
// (e.g. in checkletassign) would be undone. Placed here, the f32
// stamp on n.rhs / n.lhs sticks; cgen's fold-1 narrow then fires. let
// / return only — see coercefloatlit's docstring for the rule-10 scope
// (the cstage twin coerces in clet / cstmt N_RETURN). c.fnret is set
// by resolvefnbody for the enclosing fn, mirroring checkretassign.
if (k == nkind.N_LET) { coercefloatlit(c, n.rhs, n.lhs); };
if (k == nkind.N_RETURN) { coercefloatlit(c, n.lhs, c.fnret); };
// A.6.0: post-order dispatch of exprtype on every expression-yielding
// node kind so n.type_ stamps fire universally — not only when reached
// through checkletassign / checkretassign / checktryprop / the size-
@@ -8877,6 +8888,31 @@ fn unifyarith(c: *checker, ltn: *node, rtn: *node) *node = {
return ltn;
};
// coercefloatlit — twin of cstage cmd/wcc/check.c coerce_floatlit (see there
// for the full rationale + the rule-10 scope note). Stamp an un-suffixed
// float literal (whose type_ is the untyped_float singleton) as f32 when the
// target type resolves to f32, so fold-1's cgen narrow (isf32type, cgenexpr.
// ww) fires off the now-f32 node.type_. SCOPED to a DIRECT untyped_float
// N_FLOATLIT at let-init / return only: the wwstage cgen's exprfloatkind
// (cgenutil.ww) hardcodes N_FLOATLIT -> f64 and cgbin / the unary negate pick
// f32 off the operands' float-kind, not the node stamp, so a stamped literal
// inside an arith-binop / behind a unary minus does NOT narrow there —
// binop / unary-minus / assign / call-arg / struct-field wait on #120.
fn coercefloatlit(c: *checker, e: *node, target: *node) void = {
if (e == nil) { return; };
if (target == nil) { return; };
let tu: *node = resolvealias(c, unwrapbang(target));
if (tu == nil) { return; };
if (tu.kind != nkind.N_TNAME) { return; };
if (!streq(tu.str, "f32")) { return; };
if (e.kind == nkind.N_FLOATLIT) {
if ((e.type_: *tinfo) == c.tc.tyuntypedfloat) {
let f32t: *node = mktname(c, "f32");
e.type_ = tinfofornode(c, f32t): *void;
};
};
};
// binoptype — derive the result tnode of an N_BIN operator expression.
// Mirrors cstage cmd/wcc/check.c:598-640 `cbinop`. Operates on tnodes
// returned by exprtype; ptr arithmetic / bitwise / shifts / comparisons /

216
test/wcc/965_f32stamp_run.c Normal file
View File

@@ -0,0 +1,216 @@
/*
* 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;
}