wcc: float-typed def/let DATA emit via SSoT helper (#129 A.1)

Extract emit_floatlit_data helper for def/let with float-typed top-level
initializer; replaces inline emit_lets float arm and adds previously-
absent emit_defs float arm. Helper peels N_CAST then N_UN(±, N_FLOATLIT),
bit-casts magnitude (f32 via union narrow), emits in little-endian byte
order, applies sign-XOR to top byte inside the loop (bit 31 for f32, bit
63 for f64). The byte-loop XOR avoids materialising 2^63, sidestepping
the strconv.i64tos INT64_MIN bug (#144 / task #37) on the wwstage self-
build path. Mirrored cstage (cgen.c) and wwstage (cgen.ww). Both stages'
float-typed def materialisation (cgexpr N_IDENT / cgident def-branch)
widened to route through the same LEAQ+MOVSS/MOVSD shape as float-typed
let.

Closes 3 latent bugs (all bootstrap-NEUTRAL, no current consumer):
- def: f64 = literal silently emitted undefined ref
- let: f64 = -literal silently emitted undefined ref (N_UN peel absent)
- wwstage let: f32 = literal silently truncated to low 4 of f64 bits

Test 917 (7 rows: f64_def_pos / f64_def_neg / f32_def_pos / f32_def_neg
matrix-closure / f64_let_neg / f64_let_pos / f32_let_pos) registered.
Make test: 180/180 incl. 990-997 byte-id + combined_ww_fresh + 995_self_
rebuild.

#144 (strconv.i64tos INT64_MIN two's-complement-overflow root) filed
separately as task #37 for its own fold.
This commit is contained in:
2026-05-27 04:19:37 +09:00
parent 61e6aab384
commit 1f8fcdc0ee
7 changed files with 708 additions and 146 deletions

View File

@@ -2106,10 +2106,16 @@ cgexpr(Cg *c, Node *n, Local *locals)
ins2(c, A_MOVQ, amem(D_CX, 16), areg(D_CX));
goto ident_done;
}
if (let_islet(n->str) && let_isfloat(n->type)) {
/* Top-level float global: same LEAQ-indirect
* shape as str/slice, since MOVSS/MOVSD have
* no D_EXTERN operand form in w6a. */
if (let_isfloat(n->type)) {
/* Top-level float global (let OR def): same
* LEAQ-indirect shape as str/slice, since
* MOVSS/MOVSD have no D_EXTERN operand form in
* w6a. Pre-#129 this gated on `let_islet` so
* float defs fell through to the MOVQ-AX
* integer-convention fallback below; that
* load-shape mismatched the float storage emit
* (#129 Phase A.1 LOAD-side twin of the
* emit_floatlit_data DATA-side SSoT). */
int op = type_isf32(n->type) ? A_MOVSS : A_MOVSD;
ins2(c, A_LEAQ, masym(c, n->str), areg(D_CX));
ins2(c, op, amem(D_CX, 0), areg(D_X0));
@@ -8415,6 +8421,58 @@ emit_data_row_zero(FILE *out, const char *dir, const char *name, int sz)
* literal slice syntax to honour, so this is the natural shape.
*
* Struct lets (size from Type.size): no-init only. */
/* Emit a (DATA|DATAW) row for a float-typed top-level let/def with a
* FLOATLIT RHS (optionally wrapped in N_CAST or N_UN(±, ...)). Shared
* SSoT for emit_lets's float arm and emit_defs's float arm (#129
* Phase A.1, rule-12). The N_UN peel mirrors fold_int_literal's
* MINUS/TILDE/PLUS peel (#24) — the float arm had never been given
* the same treatment, so `let g: f64 = -1.5;` silently fell through
* to no-emit + undef-ref at link. Returns 1 on emit, 0 if the rhs
* shape doesn't reduce to a foldable float literal. */
static int
emit_floatlit_data(FILE *out, Cg *c, const char *directive,
const char *name, Type *t, Node *rhs)
{
int isf32 = type_isf32(t);
int sz = isf32 ? 4 : 8;
u64 v = 0;
int neg = 0;
if (rhs != NULL) {
Node *r = rhs;
while (r != NULL && r->kind == N_CAST) r = r->lhs;
if (r != NULL && r->kind == N_UN
&& (r->op == TK_MINUS || r->op == TK_PLUS)) {
if (r->op == TK_MINUS) neg = 1;
r = r->lhs;
while (r != NULL && r->kind == N_CAST) r = r->lhs;
}
if (r == NULL || r->kind != N_FLOATLIT) return 0;
if (isf32) {
union { float f; u32 u; } x;
x.f = (float)r->fval;
v = (u64)x.u;
} else {
union { double d; u64 u; } x;
x.d = r->fval;
v = x.u;
}
}
fprintf(out, "%s %s(SB),\"", directive, mod_mangle(c, name));
/* IEEE-754 sign-bit XOR for negation happens INSIDE the emit
* loop on the top byte only — semantically identical to a whole-
* u64 XOR with 2^63 (or 2^31 for f32) but never materialises
* that constant. Mirrors the wwstage helper's shape so the
* cgen.ww self-rebuild stays cs==ww byte-identical. */
for (int i = 0; i < sz; i++) {
u8 b = (u8)((v >> (i * 8)) & 0xff);
if (neg && i == sz - 1)
b = (u8)(b ^ 0x80);
emit_data_byte(out, b);
}
fputs("\"\n", out);
return 1;
}
static void
emit_lets(Cg *c, FILE *out, Node *file)
{
@@ -8424,28 +8482,8 @@ emit_lets(Cg *c, FILE *out, Node *file)
int sz = let_emit_size(d->type);
if (sz == 0) continue;
if (let_isfloat(d->type)) {
/* sz is 4 (f32) or 8 (f64). FLOATLIT init or zero. */
int isf32 = type_isf32(d->type);
u64 v = 0;
if (d->rhs != NULL) {
Node *r = d->rhs;
while (r != NULL && r->kind == N_CAST) r = r->lhs;
if (r == NULL) continue;
if (r->kind != N_FLOATLIT) continue;
if (isf32) {
union { float f; u32 u; } x;
x.f = (float)r->fval;
v = (u64)x.u;
} else {
union { double d; u64 u; } x;
x.d = r->fval;
v = x.u;
}
}
fprintf(out, "DATAW %s(SB),\"", mod_mangle(c, d->str));
for (int i = 0; i < sz; i++)
emit_data_byte(out, (u8)((v >> (i * 8)) & 0xff));
fputs("\"\n", out);
(void)emit_floatlit_data(out, c, "DATAW",
d->str, d->type, d->rhs);
continue;
}
if (sz == 8 && !let_isarray(d->type)) {
@@ -8578,19 +8616,29 @@ emit_defs(Cg *c, FILE *out, Node *file)
for (Node *d = file->list; d; d = d->next) {
if (d->kind != N_DEF || d->rhs == NULL) continue;
u64 v;
if (!fold_int_literal(d->rhs, &v))
continue; /* not a fold-able literal constant */
fprintf(out, "DATA %s(SB),\"", mod_mangle(c, d->str));
for (int i = 0; i < 8; i++) {
unsigned b = (unsigned)((v >> (i * 8)) & 0xff);
if (b == '"' || b == '\\')
fprintf(out, "\\%c", b);
else if (b < 0x20 || b >= 0x7f)
fprintf(out, "\\x%02x", b);
else
fputc(b, out);
if (fold_int_literal(d->rhs, &v)) {
fprintf(out, "DATA %s(SB),\"", mod_mangle(c, d->str));
for (int i = 0; i < 8; i++) {
unsigned b = (unsigned)((v >> (i * 8)) & 0xff);
if (b == '"' || b == '\\')
fprintf(out, "\\%c", b);
else if (b < 0x20 || b >= 0x7f)
fprintf(out, "\\x%02x", b);
else
fputc(b, out);
}
fputs("\"\n", out);
continue;
}
/* Float-typed def with FLOATLIT (or N_UN(±,FLOATLIT)) rhs.
* Routes through the same SSoT helper as emit_lets's float
* arm — pre-#129 this fell through to no-emit + undef-ref
* at link. */
if (let_isfloat(d->type)) {
(void)emit_floatlit_data(out, c, "DATA",
d->str, d->type, d->rhs);
continue;
}
fputs("\"\n", out);
}
(void)c;
}