Files
ww/test/wcc/917_def_float_lit_run.c
Hojun-Cho 1f8fcdc0ee 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.
2026-05-27 04:19:37 +09:00

236 lines
7.2 KiB
C

/*
* 917_def_float_lit_run — runtime + byte-id net for #129 Phase A.1:
* module-level `def NAME: f64 = literal;` (and `def: f32 = …`)
* silently fell through emit_defs's int-only `fold_int_literal` gate,
* so no DATAW row landed in the .data section — `w6l` failed link
* with `undefined reference to 'main.NAME'`. Symmetric latent bug in
* emit_lets's float arm: it only handled bare N_FLOATLIT, never
* peeled N_UN(MINUS/PLUS, N_FLOATLIT), so `let g: f64 = -1.5;`
* silently zero-emitted into the same undef-ref shape.
*
* Phase A.1 fix (rule-12 sea-of-stars consolidation):
* - cstage: extract `emit_floatlit_data(out, c, dir, name, T, rhs)`
* helper, called by emit_lets's float arm (now collapsed) AND
* emit_defs (new float arm). N_CAST + N_UN(MINUS/PLUS,
* N_FLOATLIT) peeled inside the helper.
* - wwstage: parallel `emitfloatlitdata` helper. Negation via
* IEEE-754 sign-bit XOR (avoids dragging the math bitcast helpers
* into cgen).
* - LOAD side: cstage's N_IDENT float-let arm and wwstage's cgident
* def-branch both gated on let_islet/deflookup-but-not-float; the
* gate widens to also catch float defs (they emit through the
* same mod_mangle / emitsymname symbol).
*
* Bootstrap NEUTRAL: zero current `def: f{32,64} = lit` consumers in
* lib/ or selfhost/. The N_UN-peel collateral fix for lets has no
* shipped consumers either (audit: no `let g: f64 = -lit;` anywhere).
*
* Rows cover both fix sides (DATA emit + LOAD) and the N_UN collateral:
* - f64_def_pos: `def K: f64 = 1.5;` — was undef-ref pre-fix, exit
* 1 (1.5 → i32 truncates to 1).
* - f64_def_neg: `def K: f64 = -1.5;` — was undef-ref pre-fix; exit
* 255 (sign-bit flip via XOR; -1.5 → i32 truncates to -1 → 255
* in unsigned exit byte).
* - f32_def_pos: `def K: f32 = 1.5f32; …` — same shape, f32 width.
* - f64_let_neg: `let g: f64 = -1.5;` — pre-existing latent bug
* (N_UN peel never added to emit_lets); now fixed by the same
* helper.
* - f64_let_pos: `let g: f64 = 1.5;` — regression guard (existing
* bare-N_FLOATLIT path).
* - f32_let_pos: `let g: f32 = 1.5f32;` — regression guard, f32 path.
*
* Each row carries (a) cstage `ww build` + run asserting exit code
* and (b) w6c vs w6c_ww `.s` cmp (rule-10 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;
}
struct row { const char *label; const char *src; int want_exit; };
static const struct row rows[] = {
{ "f64_def_pos",
"package main;\n"
"def K: f64 = 1.5;\n"
"export fn main() i32 = {\n"
" let x: f64 = K;\n"
" return (x: i32);\n"
"};\n", 1 },
{ "f64_def_neg",
"package main;\n"
"def K: f64 = -1.5;\n"
"export fn main() i32 = {\n"
" let x: f64 = K;\n"
" return (x: i32);\n"
"};\n", 255 /* -1 as unsigned exit byte */ },
{ "f32_def_pos",
"package main;\n"
"def K: f32 = 1.5f32;\n"
"export fn main() i32 = {\n"
" let x: f32 = K;\n"
" return (x: i32);\n"
"};\n", 1 },
/* Matrix-closure: f32 + neg combined directly. Exercises the
* f32-narrow path PLUS the top-byte-XOR negation in one row.
* The other rows cover each leg individually (f64_def_neg for
* negation, f32_def_pos for f32 narrow); this row pins they
* compose correctly. */
{ "f32_def_neg",
"package main;\n"
"def K: f32 = -1.5f32;\n"
"export fn main() i32 = {\n"
" let x: f32 = K;\n"
" return (x: i32);\n"
"};\n", 255 /* -1 as exit byte */ },
/* Pre-existing latent: emit_lets's float arm never peeled
* N_UN(MINUS,N_FLOATLIT). The class-closure consolidation
* fix in emit_floatlit_data heals this too. */
{ "f64_let_neg",
"package main;\n"
"let g: f64 = -1.5;\n"
"export fn main() i32 = {\n"
" return (g: i32);\n"
"};\n", 255 },
{ "f64_let_pos",
"package main;\n"
"let g: f64 = 1.5;\n"
"export fn main() i32 = {\n"
" return (g: i32);\n"
"};\n", 1 },
{ "f32_let_pos",
"package main;\n"
"let g: f32 = 1.5f32;\n"
"export fn main() i32 = {\n"
" return (g: i32);\n"
"};\n", 1 },
{ 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, "deflitf: 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/wwdflf_%d_%d.ww", getpid(), i);
FILE *f = fopen(src, "wb");
if (f == NULL) { fail++; continue; }
fputs(rows[i].src, f);
fclose(f);
char tmpdir[64];
snprintf(tmpdir, sizeof tmpdir, "/tmp/wwdflf_%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);
char cs_s[64], ws_s[64];
snprintf(cs_s, sizeof cs_s, "/tmp/wwdflf_%d_%d_cs.s",
getpid(), i);
snprintf(ws_s, sizeof ws_s, "/tmp/wwdflf_%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 def-float-lit tests failed\n", fail, n);
return 1;
}
printf("deflitf: %d/%d ok (cstage run + cs==ww byte-id)\n",
n, n);
return 0;
}