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:
@@ -1190,6 +1190,88 @@ export fn letpreintern(c: *cgen, file: *node) void = {
|
||||
// sz struct — zero only.
|
||||
// Non-literal scalar inits and unsupported shapes are skipped so the
|
||||
// link surfaces an undefined-symbol error if the binding is used.
|
||||
// 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 emitletdataw float arm + emitdefconstants float arm (#129
|
||||
// Phase A.1, rule-12 sea-of-stars). The N_UN(MINUS/PLUS) peel mirrors
|
||||
// foldintliteral'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. Negation is
|
||||
// an IEEE-754 sign-bit XOR (bit 63 f64, bit 31 f32) to avoid pulling
|
||||
// f64/f32 bitcast helpers into cgen. Returns true on emit, false if
|
||||
// rhs doesn't reduce to a foldable float literal.
|
||||
fn emitfloatlitdata(c: *cgen, directive: str, name: str,
|
||||
sz: i32, rhs: *node) bool = {
|
||||
let isf32: bool = (sz == 4);
|
||||
let bits: u64 = 0u64;
|
||||
let neg: bool = false;
|
||||
if (rhs != nil) {
|
||||
let r: *node = rhs;
|
||||
for (r != nil) {
|
||||
if (r.kind != nkind.N_CAST) { break; };
|
||||
r = r.lhs;
|
||||
};
|
||||
if (r != nil) {
|
||||
if (r.kind == nkind.N_UN) {
|
||||
if (r.op == tkind.TK_MINUS) {
|
||||
neg = true;
|
||||
r = r.lhs;
|
||||
for (r != nil) {
|
||||
if (r.kind != nkind.N_CAST) { break; };
|
||||
r = r.lhs;
|
||||
};
|
||||
} else { if (r.op == tkind.TK_PLUS) {
|
||||
r = r.lhs;
|
||||
for (r != nil) {
|
||||
if (r.kind != nkind.N_CAST) { break; };
|
||||
r = r.lhs;
|
||||
};
|
||||
};};
|
||||
};
|
||||
};
|
||||
if (r == nil) { return false; };
|
||||
if (r.kind != nkind.N_FLOATLIT) { return false; };
|
||||
// r.uval holds f64 bits regardless of literal suffix (lexer
|
||||
// stores the pre-narrow bits). f32 needs an explicit
|
||||
// (double→float) narrowing at emit time — mirrors cstage's
|
||||
// `union { float f; u32 u; } x; x.f = (float)r->fval`
|
||||
// (cgen.c:8436). Pre-#129 wwstage truncated the low 4 bytes
|
||||
// of the f64 bits, which silently emitted 0 for f32 lits;
|
||||
// the bug never bit because no current consumer has a f32
|
||||
// let-init (surfaced by the consolidation gate).
|
||||
bits = r.uval;
|
||||
if (isf32) {
|
||||
let dv: f64 = *((&bits): *f64);
|
||||
let fv: f32 = (dv: f32);
|
||||
let uv: u32 = *((&fv): *u32);
|
||||
bits = uv: u64;
|
||||
};
|
||||
};
|
||||
emitline(directive);
|
||||
emitline(" ");
|
||||
emitsymname(c, name);
|
||||
emitline("(SB),\"");
|
||||
// IEEE-754 sign-bit XOR for negation happens INSIDE the emit
|
||||
// loop on the top byte only — equivalent to a whole-u64 XOR with
|
||||
// 2^63 but never materialises that constant. Avoids strconv's
|
||||
// i64tos-on-i64-MIN bug (#144) and any future cstage const-fold
|
||||
// of `1 << 63` back to the i64-MIN immediate, either of which
|
||||
// would break cs==ww byte-id on the cgen.ww self-rebuild (995).
|
||||
let i: i32 = 0;
|
||||
let nb: u64 = bits;
|
||||
for (i < sz) {
|
||||
let b: u8 = (nb & 255u64): u8;
|
||||
if (neg) {
|
||||
if (i == sz - 1) { b = b ^ 128u8; };
|
||||
};
|
||||
emitdatawbyte(b);
|
||||
nb = nb >> 8u64;
|
||||
i += 1;
|
||||
};
|
||||
emitline("\"\n");
|
||||
return true;
|
||||
};
|
||||
|
||||
fn emitletdataw(c: *cgen, file: *node) void = {
|
||||
let d: *node = file.list;
|
||||
for (d != nil) {
|
||||
@@ -1200,42 +1282,14 @@ fn emitletdataw(c: *cgen, file: *node) void = {
|
||||
let issg: bool = letvarisstruct(c, nm);
|
||||
let fsz: i32 = letvarisfloat(c, nm);
|
||||
if (fsz > 0) {
|
||||
// Float global: 4B (f32) or 8B (f64).
|
||||
// Two init shapes:
|
||||
// - no rhs: emit fsz zero bytes
|
||||
// - N_FLOATLIT: bake the IEEE bits the
|
||||
// parser stashed in r.uval (lexer
|
||||
// bit-casts t.fval into t.uval). f32
|
||||
// emits the low 4 bytes; f64 emits 8.
|
||||
let bits: u64 = 0u64;
|
||||
let ok: bool = true;
|
||||
if (d.rhs != nil) {
|
||||
let r: *node = d.rhs;
|
||||
for (r != nil) {
|
||||
if (r.kind != nkind.N_CAST) { break; };
|
||||
r = r.lhs;
|
||||
};
|
||||
ok = false;
|
||||
if (r != nil) {
|
||||
if (r.kind == nkind.N_FLOATLIT) {
|
||||
bits = r.uval;
|
||||
ok = true;
|
||||
};
|
||||
};
|
||||
};
|
||||
if (ok) {
|
||||
emitline("DATAW ");
|
||||
emitsymname(c, nm);
|
||||
emitline("(SB),\"");
|
||||
let i: i32 = 0;
|
||||
let nb: u64 = bits;
|
||||
for (i < fsz) {
|
||||
emitdatawbyte((nb & 255u64): u8);
|
||||
nb = nb >> 8u64;
|
||||
i += 1;
|
||||
};
|
||||
emitline("\"\n");
|
||||
};
|
||||
// Float global: routes through the
|
||||
// emitfloatlitdata SSoT helper, shared
|
||||
// with emitdefconstants's float arm
|
||||
// (#129 Phase A.1, rule-12). Bare-call
|
||||
// discards the bool return (mirrors
|
||||
// cgen.ww:723 fmt.fprintln pattern).
|
||||
emitfloatlitdata(c, "DATAW", nm, fsz,
|
||||
d.rhs);
|
||||
};
|
||||
// Skip the scalar 8B path when the global is a
|
||||
// fixed-size array that just happens to sum to 8
|
||||
@@ -1479,6 +1533,27 @@ fn emitdefconstants(c: *cgen, file: *node) void = {
|
||||
if (r != nil) {
|
||||
ok = foldintliteral(r, &v);
|
||||
};
|
||||
if (!ok) {
|
||||
// Float-typed def with FLOATLIT (or N_UN(±,FLOATLIT))
|
||||
// rhs: route through the same SSoT helper as
|
||||
// emitletdataw's float arm. Pre-#129 this fell
|
||||
// through to no-emit + undef-ref at link. Type-size
|
||||
// walk mirrors letvarisfloat (#129 Phase A.1).
|
||||
let dfsz: i32 = 0;
|
||||
let dt: *node = d.lhs;
|
||||
for (dt != nil) {
|
||||
if (dt.kind != nkind.N_TNAME) { dfsz = 0; break; };
|
||||
let fsz: i32 = letfloatprim(dt.str);
|
||||
if (fsz > 0) { dfsz = fsz; break; };
|
||||
let nx: *node = aliaslookup(c, dt.str);
|
||||
if (nx == nil) { dfsz = 0; break; };
|
||||
dt = nx;
|
||||
};
|
||||
if (dfsz > 0) {
|
||||
emitfloatlitdata(c, "DATA", d.str,
|
||||
dfsz, d.rhs);
|
||||
};
|
||||
};
|
||||
if (ok) {
|
||||
// #127: route DATA-emit through the SAME emitsymname
|
||||
// SSoT that LOAD/CALL sites use. Replaces the prior
|
||||
|
||||
@@ -642,6 +642,22 @@ fn cgident(c: *cgen, n: *node) void = {
|
||||
return;
|
||||
};
|
||||
};
|
||||
// Float def: load via LEAQ + MOVSS/MOVSD into X0, same shape
|
||||
// as the let-float arm below — MOVSS/MOVSD have no D_EXTERN
|
||||
// operand form. Pre-#129 fell through to the MOVQ-AX
|
||||
// integer-convention fallback, leaving X0 untouched (#129
|
||||
// LOAD-side twin of the emitfloatlitdata DATA-side SSoT).
|
||||
if (isfloattype(c, n)) {
|
||||
let mov: str = "MOVSD";
|
||||
if (isf32type(c, n)) { mov = "MOVSS"; };
|
||||
emitline("\tLEAQ\t");
|
||||
emitsymname(c, nm);
|
||||
emitline("(SB), CX\n");
|
||||
emitline("\t");
|
||||
emitline(mov);
|
||||
emitline("\t(CX), X0\n");
|
||||
return;
|
||||
};
|
||||
emitline("\tMOVQ\t");
|
||||
emitsymname(c, nm);
|
||||
emitline("(SB), AX\n");
|
||||
|
||||
Reference in New Issue
Block a user