cstage+selfhost+test: fold N_UN over signed int literal in let DATA emit (#19)

emit_lets / emitletdataw's scalar-8B and array arms only matched bare
N_INTLIT / N_RUNELIT / N_TRUE / N_FALSE / N_NIL on the let rhs. `let
x: i8 = -1i8;` arrives as N_UN(TK_MINUS, N_INTLIT(1)) — none of those
— so cstage's scalar arm hit `else continue;` and dropped the DATAW
row entirely; the array arm bailed at the first non-foldable element
and the skip-array-with-non-NIL-rhs fall-through dropped the whole
row. Wwstage's mirror arms silently emitted zero bytes for negative
literals in both shapes.

Severity split — cstage symptom is no DATA emit, the linker fails
loudly at build time. Wwstage symptom is silent-zero element
substitution for negative array values: compiles, runs, returns
wrong answers. Corpus-coverage-blind on the wwstage side, only
surfaces when a consumer reads the wrong value. Single N_UN-fold
helper application retires both symptoms across both stages. Fourth
corpus-coverage-blind unmask this session (catalog: i64 div/mod CQO
#16, IDENT-local /= no-op, #21 call-arg DX drop, now #19 wwstage
silent-zero).

Route all four sites through fold_int_literal / foldintliteral, the
same helper #24 used on the def-emit side (which already covered
N_UN over the leaf set). The wwstage array arm also picks up an
N_CAST peel and drops a dead non-`...` N_FIELD branch (the parser
never emits non-`...` N_FIELD inside an N_ARRLIT — only as the `...`
repeat marker). Same-path sibling cleanup; rule-11 justified.

Tests:
  - 719_signed_data_emit asserts DATAW <sym>(SB),"<bytes>" lines are
    present in both stages' .s for the {i8, i16, i32, i64} × {scalar,
    1D array} matrix, plus cmp -s byte-id between stages per row.
    Corpus-coverage-blind sentinel per rob's STATUS-3 note.
  - 923_signed_data_emit_run runtime-pins the same matrix plus a
    TK_TILDE row through cstage and wwstage drivers.

995_self_rebuild stays green (ww2==ww3==ww4 byte-id).
This commit is contained in:
2026-05-17 07:25:49 +09:00
parent a8561c03a0
commit 9bd1d0d734
7 changed files with 543 additions and 87 deletions

View File

@@ -242,6 +242,8 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \
$(BIN)/test_match_spill_pointer_payload \ $(BIN)/test_match_spill_pointer_payload \
$(BIN)/test_struct_byval_param \ $(BIN)/test_struct_byval_param \
$(BIN)/test_struct_multi_return_scratch \ $(BIN)/test_struct_multi_return_scratch \
$(BIN)/test_signed_data_emit \
$(BIN)/test_signed_data_emit_run \
$(BIN)/test_param_shadow_mod \ $(BIN)/test_param_shadow_mod \
$(BIN)/test_localoff_scope \ $(BIN)/test_localoff_scope \
$(BIN)/test_cast_enum_movl \ $(BIN)/test_cast_enum_movl \
@@ -507,6 +509,16 @@ $(BIN)/test_struct_multi_return_scratch: test/wcc/718_struct_multi_return_scratc
$(LIB)/libwwrt.a | $(BIN) $(LIB)/libwwrt.a | $(BIN)
$(CC) $(CFLAGS) -o $@ $< $(CC) $(CFLAGS) -o $@ $<
$(BIN)/test_signed_data_emit: test/wcc/719_signed_data_emit.c \
$(BIN)/w6c $(BIN)/w6c_ww | $(BIN)
$(CC) $(CFLAGS) -o $@ $<
$(BIN)/test_signed_data_emit_run: test/wcc/923_signed_data_emit_run.c \
$(BIN)/ww $(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \
$(BIN)/ww_ww $(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \
$(LIB)/libwwrt.a | $(BIN)
$(CC) $(CFLAGS) -o $@ $<
$(BIN)/test_use_promote_alias: test/wcc/699_use_promote_alias.c \ $(BIN)/test_use_promote_alias: test/wcc/699_use_promote_alias.c \
$(BIN)/ww $(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \ $(BIN)/ww $(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \
$(LIB)/libwwrt.a | $(BIN) $(LIB)/libwwrt.a | $(BIN)

View File

@@ -7111,12 +7111,14 @@ emit_lets(Cg *c, FILE *out, Node *file)
Node *r = d->rhs; Node *r = d->rhs;
while (r != NULL && r->kind == N_CAST) r = r->lhs; while (r != NULL && r->kind == N_CAST) r = r->lhs;
if (r == NULL) continue; if (r == NULL) continue;
if (r->kind == N_INTLIT) v = r->uval; /* Same helper as emit_defs (#24): widens
else if (r->kind == N_RUNELIT) v = r->uval; * the gate to cover N_UN(TK_MINUS/TILDE/PLUS,
else if (r->kind == N_TRUE) v = 1; * leaf) so `let x: i8 = -1i8;` and friends
else if (r->kind == N_FALSE) v = 0; * encode as sign-extended two's-complement
else if (r->kind == N_NIL) v = 0; * bytes. emit_data_row writes 8 LE bytes
else continue; * so narrow signed types just naturally
* round-trip via the sign-extended u64. */
if (!fold_int_literal(r, &v)) continue;
} }
emit_data_row(out, "DATAW", mod_mangle(c, d->str), v); emit_data_row(out, "DATAW", mod_mangle(c, d->str), v);
continue; continue;
@@ -7170,15 +7172,12 @@ emit_lets(Cg *c, FILE *out, Node *file)
Node *ev = e; Node *ev = e;
while (ev && ev->kind == N_CAST) ev = ev->lhs; while (ev && ev->kind == N_CAST) ev = ev->lhs;
if (ev == NULL) { ok = 0; break; } if (ev == NULL) { ok = 0; break; }
if (ev->kind == N_INTLIT || ev->kind == N_RUNELIT) { /* Same helper as the scalar arm above —
last = ev->uval; * fold_int_literal covers N_UN over a leaf
} else if (ev->kind == N_TRUE) { * so signed-negative array elements (`-1i8`)
last = 1; * encode as their two's-complement bytes
} else if (ev->kind == N_FALSE) { * once truncated to esz below. */
last = 0; if (!fold_int_literal(ev, &last)) {
} else if (ev->kind == N_NIL) {
last = 0;
} else {
ok = 0; ok = 0;
break; break;
} }

View File

@@ -18808,14 +18808,12 @@ fn emitletdataw(c: *cgen, file: *node) void = {
if (r.kind != nkind.N_CAST) { break; }; if (r.kind != nkind.N_CAST) { break; };
r = r.lhs; r = r.lhs;
}; };
ok = false; // Same helper as emitdefconstants (#24)
if (r != nil) { // — widens the gate so N_UN over an
if (r.kind == nkind.N_INTLIT) { v = r.uval; ok = true; }; // int leaf folds. `let x: i8 = -1i8;`
if (r.kind == nkind.N_RUNELIT) { v = r.uval; ok = true; }; // arrives as N_UN(TK_MINUS, N_INTLIT)
if (r.kind == nkind.N_TRUE) { v = 1u64; ok = true; }; // after the typed-AST cast peel.
if (r.kind == nkind.N_FALSE) { v = 0u64; ok = true; }; ok = foldintliteral(r, &v);
if (r.kind == nkind.N_NIL) { v = 0u64; ok = true; };
};
}; };
if (ok) { if (ok) {
emitline("DATAW "); emitline("DATAW ");
@@ -18945,11 +18943,14 @@ fn emitletdataw(c: *cgen, file: *node) void = {
// Top-level `[N]T = [a, b, ...]` array global. // Top-level `[N]T = [a, b, ...]` array global.
// Emits N*esz bytes with each element's bytes // Emits N*esz bytes with each element's bytes
// little-endian for the declared primitive width. // little-endian for the declared primitive width.
// Without this, `let arr: [N]T = ...` references // Element fold goes through foldintliteral (same
// from function bodies link-fail with `undefined // helper as emitdefconstants / scalar arm above)
// reference to arr`, and bare-name addressing // so `-1i8` and friends emit their two's-complement
// (LEAQ arr(SB)) inside cgindex / cgassign has no // bytes after the leading N_CAST peel — pre-#19
// symbol to bind to. // this arm only matched bare N_INTLIT/N_RUNELIT and
// silently emitted zero for unfoldable elements.
// `...` (N_FIELD with str="...") repeats the last
// folded value across the remaining slots.
if (d.lhs != nil) { if (d.lhs != nil) {
if (d.lhs.kind == nkind.N_TARRAY) { if (d.lhs.kind == nkind.N_TARRAY) {
let elemn: *node = d.lhs.lhs; let elemn: *node = d.lhs.lhs;
@@ -18973,27 +18974,25 @@ fn emitletdataw(c: *cgen, file: *node) void = {
emitline("(SB),\""); emitline("(SB),\"");
let i: i32 = 0; let i: i32 = 0;
let e: *node = elems; let e: *node = elems;
let fillv: u64 = 0u64; let last: u64 = 0u64;
let inrepeat: bool = false; let inrepeat: bool = false;
for (i < alen) { for (i < alen) {
let v: u64 = fillv; let v: u64 = last;
if (!inrepeat && e != nil) { if (!inrepeat && e != nil) {
if (e.kind == nkind.N_FIELD) { if (e.kind == nkind.N_FIELD) {
if (streq(e.str, "...")) { if (streq(e.str, "...")) {
// `..., ...` repeat marker: prior v stays.
inrepeat = true; inrepeat = true;
} else { } else {
if (e.lhs != nil) {
if (e.lhs.kind == nkind.N_INTLIT) { v = e.lhs.uval; };
if (e.lhs.kind == nkind.N_RUNELIT) { v = e.lhs.uval; };
};
fillv = v;
e = e.next; e = e.next;
}; };
} else { } else {
if (e.kind == nkind.N_INTLIT) { v = e.uval; }; let ev: *node = e;
if (e.kind == nkind.N_RUNELIT) { v = e.uval; }; for (ev != nil) {
fillv = v; if (ev.kind != nkind.N_CAST) { break; };
ev = ev.lhs;
};
if (!foldintliteral(ev, &v)) { v = 0u64; };
last = v;
e = e.next; e = e.next;
}; };
}; };

View File

@@ -1108,14 +1108,12 @@ fn emitletdataw(c: *cgen, file: *node) void = {
if (r.kind != nkind.N_CAST) { break; }; if (r.kind != nkind.N_CAST) { break; };
r = r.lhs; r = r.lhs;
}; };
ok = false; // Same helper as emitdefconstants (#24)
if (r != nil) { // — widens the gate so N_UN over an
if (r.kind == nkind.N_INTLIT) { v = r.uval; ok = true; }; // int leaf folds. `let x: i8 = -1i8;`
if (r.kind == nkind.N_RUNELIT) { v = r.uval; ok = true; }; // arrives as N_UN(TK_MINUS, N_INTLIT)
if (r.kind == nkind.N_TRUE) { v = 1u64; ok = true; }; // after the typed-AST cast peel.
if (r.kind == nkind.N_FALSE) { v = 0u64; ok = true; }; ok = foldintliteral(r, &v);
if (r.kind == nkind.N_NIL) { v = 0u64; ok = true; };
};
}; };
if (ok) { if (ok) {
emitline("DATAW "); emitline("DATAW ");
@@ -1245,11 +1243,14 @@ fn emitletdataw(c: *cgen, file: *node) void = {
// Top-level `[N]T = [a, b, ...]` array global. // Top-level `[N]T = [a, b, ...]` array global.
// Emits N*esz bytes with each element's bytes // Emits N*esz bytes with each element's bytes
// little-endian for the declared primitive width. // little-endian for the declared primitive width.
// Without this, `let arr: [N]T = ...` references // Element fold goes through foldintliteral (same
// from function bodies link-fail with `undefined // helper as emitdefconstants / scalar arm above)
// reference to arr`, and bare-name addressing // so `-1i8` and friends emit their two's-complement
// (LEAQ arr(SB)) inside cgindex / cgassign has no // bytes after the leading N_CAST peel — pre-#19
// symbol to bind to. // this arm only matched bare N_INTLIT/N_RUNELIT and
// silently emitted zero for unfoldable elements.
// `...` (N_FIELD with str="...") repeats the last
// folded value across the remaining slots.
if (d.lhs != nil) { if (d.lhs != nil) {
if (d.lhs.kind == nkind.N_TARRAY) { if (d.lhs.kind == nkind.N_TARRAY) {
let elemn: *node = d.lhs.lhs; let elemn: *node = d.lhs.lhs;
@@ -1273,27 +1274,25 @@ fn emitletdataw(c: *cgen, file: *node) void = {
emitline("(SB),\""); emitline("(SB),\"");
let i: i32 = 0; let i: i32 = 0;
let e: *node = elems; let e: *node = elems;
let fillv: u64 = 0u64; let last: u64 = 0u64;
let inrepeat: bool = false; let inrepeat: bool = false;
for (i < alen) { for (i < alen) {
let v: u64 = fillv; let v: u64 = last;
if (!inrepeat && e != nil) { if (!inrepeat && e != nil) {
if (e.kind == nkind.N_FIELD) { if (e.kind == nkind.N_FIELD) {
if (streq(e.str, "...")) { if (streq(e.str, "...")) {
// `..., ...` repeat marker: prior v stays.
inrepeat = true; inrepeat = true;
} else { } else {
if (e.lhs != nil) {
if (e.lhs.kind == nkind.N_INTLIT) { v = e.lhs.uval; };
if (e.lhs.kind == nkind.N_RUNELIT) { v = e.lhs.uval; };
};
fillv = v;
e = e.next; e = e.next;
}; };
} else { } else {
if (e.kind == nkind.N_INTLIT) { v = e.uval; }; let ev: *node = e;
if (e.kind == nkind.N_RUNELIT) { v = e.uval; }; for (ev != nil) {
fillv = v; if (ev.kind != nkind.N_CAST) { break; };
ev = ev.lhs;
};
if (!foldintliteral(ev, &v)) { v = 0u64; };
last = v;
e = e.next; e = e.next;
}; };
}; };

View File

@@ -18808,14 +18808,12 @@ fn emitletdataw(c: *cgen, file: *node) void = {
if (r.kind != nkind.N_CAST) { break; }; if (r.kind != nkind.N_CAST) { break; };
r = r.lhs; r = r.lhs;
}; };
ok = false; // Same helper as emitdefconstants (#24)
if (r != nil) { // — widens the gate so N_UN over an
if (r.kind == nkind.N_INTLIT) { v = r.uval; ok = true; }; // int leaf folds. `let x: i8 = -1i8;`
if (r.kind == nkind.N_RUNELIT) { v = r.uval; ok = true; }; // arrives as N_UN(TK_MINUS, N_INTLIT)
if (r.kind == nkind.N_TRUE) { v = 1u64; ok = true; }; // after the typed-AST cast peel.
if (r.kind == nkind.N_FALSE) { v = 0u64; ok = true; }; ok = foldintliteral(r, &v);
if (r.kind == nkind.N_NIL) { v = 0u64; ok = true; };
};
}; };
if (ok) { if (ok) {
emitline("DATAW "); emitline("DATAW ");
@@ -18945,11 +18943,14 @@ fn emitletdataw(c: *cgen, file: *node) void = {
// Top-level `[N]T = [a, b, ...]` array global. // Top-level `[N]T = [a, b, ...]` array global.
// Emits N*esz bytes with each element's bytes // Emits N*esz bytes with each element's bytes
// little-endian for the declared primitive width. // little-endian for the declared primitive width.
// Without this, `let arr: [N]T = ...` references // Element fold goes through foldintliteral (same
// from function bodies link-fail with `undefined // helper as emitdefconstants / scalar arm above)
// reference to arr`, and bare-name addressing // so `-1i8` and friends emit their two's-complement
// (LEAQ arr(SB)) inside cgindex / cgassign has no // bytes after the leading N_CAST peel — pre-#19
// symbol to bind to. // this arm only matched bare N_INTLIT/N_RUNELIT and
// silently emitted zero for unfoldable elements.
// `...` (N_FIELD with str="...") repeats the last
// folded value across the remaining slots.
if (d.lhs != nil) { if (d.lhs != nil) {
if (d.lhs.kind == nkind.N_TARRAY) { if (d.lhs.kind == nkind.N_TARRAY) {
let elemn: *node = d.lhs.lhs; let elemn: *node = d.lhs.lhs;
@@ -18973,27 +18974,25 @@ fn emitletdataw(c: *cgen, file: *node) void = {
emitline("(SB),\""); emitline("(SB),\"");
let i: i32 = 0; let i: i32 = 0;
let e: *node = elems; let e: *node = elems;
let fillv: u64 = 0u64; let last: u64 = 0u64;
let inrepeat: bool = false; let inrepeat: bool = false;
for (i < alen) { for (i < alen) {
let v: u64 = fillv; let v: u64 = last;
if (!inrepeat && e != nil) { if (!inrepeat && e != nil) {
if (e.kind == nkind.N_FIELD) { if (e.kind == nkind.N_FIELD) {
if (streq(e.str, "...")) { if (streq(e.str, "...")) {
// `..., ...` repeat marker: prior v stays.
inrepeat = true; inrepeat = true;
} else { } else {
if (e.lhs != nil) {
if (e.lhs.kind == nkind.N_INTLIT) { v = e.lhs.uval; };
if (e.lhs.kind == nkind.N_RUNELIT) { v = e.lhs.uval; };
};
fillv = v;
e = e.next; e = e.next;
}; };
} else { } else {
if (e.kind == nkind.N_INTLIT) { v = e.uval; }; let ev: *node = e;
if (e.kind == nkind.N_RUNELIT) { v = e.uval; }; for (ev != nil) {
fillv = v; if (ev.kind != nkind.N_CAST) { break; };
ev = ev.lhs;
};
if (!foldintliteral(ev, &v)) { v = 0u64; };
last = v;
e = e.next; e = e.next;
}; };
}; };

View File

@@ -0,0 +1,221 @@
/*
* 719_signed_data_emit — corpus-coverage-blind sentinel for #19.
*
* Confirms each cstage / wwstage `.s` for a top-level `let` of signed
* integer type contains a `DATAW <sym>(SB),"..."` row, *and* that the
* inline literal bytes encode the value's two's complement at the
* declared element width. Pre-fix:
* - cstage emit_lets dropped the row entirely on `let x: i8 = -1i8;`
* (link failed, no DATAW); the array arm dropped the whole row.
* - wwstage emitletdataw silently emitted zero bytes where the
* negative literal should have produced 0xFF... (link succeeded;
* runtime read 0). The array arm hit the same gap.
*
* Both 923_signed_data_emit_run and the broader bootstrap byte-id
* (995_self_rebuild) would catch a future regression, but this row
* pins the *asm shape* itself — a future cgen refactor that emits
* the slot via a different directive (e.g. via DATA + DATAR rather
* than DATAW) would silently divergent even with green semantics.
*
* Plus a cstage-vs-wwstage byte-id diff per row, mirroring 707's
* pattern.
*/
#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;
const char *sym;
const char *want_bytes; /* exact byte-payload inside the DATAW */
};
/* The .s emits each byte either as a printable ASCII glyph (b is 0x20-
* 0x7e and not '"'/'\\') or as `\xNN`. emit_data_byte's rules: 0x22 + 0x5c
* → `\"` / `\\`; printable → raw; everything else → `\xNN`. For the
* sign-extended pads we'll see `\xff` × N as a contiguous run. */
static const struct row rows[] = {
/* Scalar i8 = -1 → low byte 0xff, then 7 bytes of sign-extension. */
{ "i8_neg", "let x: i8 = -1i8;\n"
"fn main() i32 = { return 0; };\n",
"x",
"\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xff" },
/* Scalar i16 = -2 → 0xfe 0xff then 6 byte sign extension. */
{ "i16_neg", "let x: i16 = -2i16;\n"
"fn main() i32 = { return 0; };\n",
"x",
"\\xfe\\xff\\xff\\xff\\xff\\xff\\xff\\xff" },
/* Scalar i32 = -100 → 0x9C 0xFF 0xFF 0xFF then 4 byte sign extension. */
{ "i32_neg", "let x: i32 = -100i32;\n"
"fn main() i32 = { return 0; };\n",
"x",
"\\x9c\\xff\\xff\\xff\\xff\\xff\\xff\\xff" },
/* Scalar i64 = -1000 → 0xfffffffffffffc18 little-endian. */
{ "i64_neg", "let x: i64 = -1000i64;\n"
"fn main() i32 = { return 0; };\n",
"x",
"\\x18\\xfc\\xff\\xff\\xff\\xff\\xff\\xff" },
/* Array [4]i8 = [1, -2, 3, -4] → 0x01 0xfe 0x03 0xfc. */
{ "i8_arr", "let a: [4]i8 = [1i8, -2i8, 3i8, -4i8];\n"
"fn main() i32 = { return 0; };\n",
"a",
"\\x01\\xfe\\x03\\xfc" },
/* Array [4]i16 = [1, -2, 3, -4] → LE16 each. */
{ "i16_arr", "let a: [4]i16 = [1i16, -2i16, 3i16, -4i16];\n"
"fn main() i32 = { return 0; };\n",
"a",
"\\x01\\x00\\xfe\\xff\\x03\\x00\\xfc\\xff" },
/* Array [3]i32 = [10, -20, 30] → LE32 each. */
{ "i32_arr", "let a: [3]i32 = [10i32, -20i32, 30i32];\n"
"fn main() i32 = { return 0; };\n",
"a",
"\\x0a\\x00\\x00\\x00\\xec\\xff\\xff\\xff\\x1e\\x00\\x00\\x00" },
/* Array [2]i64 = [100, -200] → LE64 each. Note 100 == 'd' and
* 56 == '8' are printable ASCII so emit_data_byte writes them
* raw rather than as `\xNN`. */
{ "i64_arr", "let a: [2]i64 = [100i64, -200i64];\n"
"fn main() i32 = { return 0; };\n",
"a",
"d\\x00\\x00\\x00\\x00\\x00\\x00\\x00"
"8\\xff\\xff\\xff\\xff\\xff\\xff\\xff" },
};
static int
slurp(const char *path, char *buf, size_t cap)
{
FILE *f = fopen(path, "rb");
if (!f) return -1;
size_t n = fread(buf, 1, cap - 1, f);
fclose(f);
buf[n] = '\0';
return (int)n;
}
/* Check that the .s contains a DATAW row for `sym` whose payload
* matches `want_bytes`. Returns 0 on match. */
static int
check_dataw(const char *spath, const struct row *r)
{
char buf[1 << 16];
if (slurp(spath, buf, sizeof buf) < 0) return -1;
char needle[256];
snprintf(needle, sizeof needle, "DATAW %s(SB),\"%s\"",
r->sym, r->want_bytes);
return strstr(buf, needle) ? 0 : -1;
}
static int
emit_s(const char *w6c, const struct row *r, int i, char *out_s, size_t cap)
{
char src[64], cmd[1024];
snprintf(src, sizeof src, "/tmp/sde_asm_%d_%d.ww", getpid(), i);
snprintf(out_s, cap, "/tmp/sde_asm_%d_%d_%s.s",
getpid(), i, w6c[strlen(w6c) - 1] == 'w' ? "ww" : "c");
FILE *f = fopen(src, "wb");
if (!f) return -1;
fputs(r->src, f);
fclose(f);
snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null", w6c, out_s, src);
int rc = runwait(cmd);
unlink(src);
return rc;
}
int
main(void)
{
const char *bin = getenv("BIN");
if (!bin) bin = "out/bin";
char absbin[512];
if (bin[0] != '/') {
char cwd[256];
if (getcwd(cwd, sizeof cwd) == NULL) return 1;
snprintf(absbin, sizeof absbin, "%s/%s", cwd, bin);
bin = absbin;
}
char w6c[640], w6c_ww[640];
snprintf(w6c, sizeof w6c, "%s/w6c", bin);
snprintf(w6c_ww, sizeof w6c_ww, "%s/w6c_ww", bin);
int have_ww = (access(w6c_ww, X_OK) == 0);
int n = (int)(sizeof rows / sizeof rows[0]);
int total = 0, fail = 0;
for (int i = 0; i < n; i++) {
char cs_path[128], ws_path[128];
/* cstage row: DATAW emit + byte payload. */
if (emit_s(w6c, &rows[i], i, cs_path, sizeof cs_path) != 0) {
fprintf(stderr,
"signed_data_emit[cstage][%s]: w6c failed\n",
rows[i].label);
fail++; total++; continue;
}
total++;
if (check_dataw(cs_path, &rows[i]) != 0) {
fprintf(stderr,
"signed_data_emit[cstage][%s]: DATAW %s(SB),\"%s\" missing\n",
rows[i].label, rows[i].sym, rows[i].want_bytes);
fail++;
}
if (!have_ww) { unlink(cs_path); continue; }
/* wwstage row: same DATAW emit. */
if (emit_s(w6c_ww, &rows[i], i, ws_path, sizeof ws_path) != 0) {
fprintf(stderr,
"signed_data_emit[wwstage][%s]: w6c_ww failed\n",
rows[i].label);
fail++; total++;
unlink(cs_path);
continue;
}
total++;
if (check_dataw(ws_path, &rows[i]) != 0) {
fprintf(stderr,
"signed_data_emit[wwstage][%s]: DATAW %s(SB),\"%s\" missing\n",
rows[i].label, rows[i].sym, rows[i].want_bytes);
fail++;
}
/* Byte-id diff between stages. The bootstrap byte-id covers
* cross-stage drift globally; per-row diff here surfaces a
* focused regression in the emit_lets / emitletdataw arms. */
total++;
char cmd[512];
snprintf(cmd, sizeof cmd, "cmp -s %s %s", cs_path, ws_path);
if (runwait(cmd) != 0) {
fprintf(stderr,
"signed_data_emit[%s]: cstage vs wwstage asm differs\n",
rows[i].label);
fail++;
}
unlink(cs_path); unlink(ws_path);
}
if (fail) {
fprintf(stderr,
"signed_data_emit: %d/%d fixtures failed\n", fail, total);
return 1;
}
printf("signed_data_emit: %d/%d ok\n", total, total);
return 0;
}

View File

@@ -0,0 +1,227 @@
/*
* 923_signed_data_emit_run — top-level `let` bindings of signed
* integer types must emit a module-scope DATA slot whose initialiser
* is the literal's two's-complement bytes (#19).
*
* Pre-fix both stages skipped the scalar DATAW arm when the rhs was
* N_UN(TK_MINUS, INTLIT). cstage's emit_lets fell through to the
* default `else continue;` so `let x: i8 = -1i8;` produced no DATAW
* row and the link failed with "undefined reference to x". Wwstage's
* emitletdataw silently left the slot at zero — link succeeded but
* the read returned 0 instead of -1, a corpus-coverage-blind bug.
*
* The array arm carried the same defect on both stages. cstage's
* walker bailed on the first non-foldable element (then dropped the
* whole DATAW row for the array via the `is_array continue` fall-
* through); wwstage emitted zeros at every unfoldable index, again
* silently. The utf8 DFA shape (`let dfa: [8]i8 = [0i8, -1i8, ...]`)
* is the canonical real-source trigger.
*
* Sister to #24, which fixed the same N_UN-fold gap on the `def`-
* emit side (emit_defs / emitdefconstants) via the shared
* fold_int_literal / foldintliteral helper. Task #19 routes the
* `let`-emit paths (scalar + array) through the same helper.
*
* Rows pin the full signed-int matrix (i8/i16/i32/i64) for both
* scalar and 1D array shapes, plus positive controls so a future
* regression of the gate that affects only the negative arm still
* leaves the positive rows green. The negative-zero (TK_MINUS over
* 0) and TK_TILDE rows pin the other two N_UN ops the helper covers.
*
* Stage matrix: cstage always; wwstage gated on `ww_ww` existing.
* Runtime checks all read through cast-strip + literal compare so a
* cgen-side narrow-load regression surfaces here rather than hiding
* behind the DATA slot's wider-than-elem padding.
*/
#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; };
static const struct row rows[] = {
{ "i8_neg_scalar",
"let x: i8 = -1i8;\n"
"fn main() i32 = { if (x != -1i8) { return 1; }; return 0; };\n",
0 },
{ "i8_pos_scalar",
"let x: i8 = 42i8;\n"
"fn main() i32 = { if (x != 42i8) { return 1; }; return 0; };\n",
0 },
{ "i16_neg_scalar",
"let x: i16 = -2i16;\n"
"fn main() i32 = { if (x != -2i16) { return 1; }; return 0; };\n",
0 },
{ "i16_pos_scalar",
"let x: i16 = 1000i16;\n"
"fn main() i32 = { if (x != 1000i16) { return 1; }; return 0; };\n",
0 },
{ "i32_neg_scalar",
"let x: i32 = -100i32;\n"
"fn main() i32 = { if (x != -100i32) { return 1; }; return 0; };\n",
0 },
{ "i32_pos_scalar",
"let x: i32 = 100000i32;\n"
"fn main() i32 = { if (x != 100000i32) { return 1; }; return 0; };\n",
0 },
{ "i64_neg_scalar",
"let x: i64 = -1000i64;\n"
"fn main() i32 = { if (x != -1000i64) { return 1; }; return 0; };\n",
0 },
{ "i64_pos_scalar",
"let x: i64 = 1000000i64;\n"
"fn main() i32 = { if (x != 1000000i64) { return 1; }; return 0; };\n",
0 },
/* TK_TILDE over a literal — the other N_UN op fold_int_literal
* covers. ~0u64 == 0xFFFFFFFFFFFFFFFF == -1i64. */
{ "i64_tilde_scalar",
"let x: i64 = (~0u64): i64;\n"
"fn main() i32 = { if (x != -1i64) { return 1; }; return 0; };\n",
0 },
/* Array of i8: utf8 DFA shape. The negative entries silently
* zeroed pre-fix on wwstage; cstage emitted nothing at all and
* the link failed before this row could even build. */
{ "i8_arr_dfa",
"let dfa: [8]i8 = [0i8, -1i8, 1i8, 2i8, 0i8, 0i8, -1i8, -1i8];\n"
"fn main() i32 = {\n"
" if (dfa[0] != 0i8) { return 1; };\n"
" if (dfa[1] != -1i8) { return 2; };\n"
" if (dfa[2] != 1i8) { return 3; };\n"
" if (dfa[3] != 2i8) { return 4; };\n"
" if (dfa[6] != -1i8) { return 5; };\n"
" if (dfa[7] != -1i8) { return 6; };\n"
" return 0;\n"
"};\n",
0 },
{ "i16_arr_mixed",
"let a: [4]i16 = [1i16, -2i16, 3i16, -4i16];\n"
"fn main() i32 = {\n"
" if (a[0] != 1i16) { return 1; };\n"
" if (a[1] != -2i16) { return 2; };\n"
" if (a[2] != 3i16) { return 3; };\n"
" if (a[3] != -4i16) { return 4; };\n"
" return 0;\n"
"};\n",
0 },
{ "i32_arr_mixed",
"let a: [4]i32 = [10i32, -20i32, 30i32, -40i32];\n"
"fn main() i32 = {\n"
" if (a[0] != 10i32) { return 1; };\n"
" if (a[1] != -20i32) { return 2; };\n"
" if (a[2] != 30i32) { return 3; };\n"
" if (a[3] != -40i32) { return 4; };\n"
" return 0;\n"
"};\n",
0 },
{ "i64_arr_mixed",
"let a: [4]i64 = [100i64, -200i64, 300i64, -400i64];\n"
"fn main() i32 = {\n"
" if (a[0] != 100i64) { return 1; };\n"
" if (a[1] != -200i64) { return 2; };\n"
" if (a[2] != 300i64) { return 3; };\n"
" if (a[3] != -400i64) { return 4; };\n"
" return 0;\n"
"};\n",
0 },
};
static int
run_driver(const char *driver, const struct row *r, int i)
{
char src[64], tmpdir[64], cmd[1024];
snprintf(src, sizeof src, "/tmp/sde_%d_%d.ww", getpid(), i);
snprintf(tmpdir, sizeof tmpdir, "/tmp/sde_%d_d_%d", getpid(), i);
FILE *f = fopen(src, "wb");
if (!f) return -1;
fputs(r->src, f);
fclose(f);
mkdir(tmpdir, 0755);
snprintf(cmd, sizeof cmd, "cd %s && %s build %s",
tmpdir, driver, src);
if (runwait(cmd) != 0) {
fprintf(stderr, "row[%s]: build via %s failed\n",
r->label, driver);
unlink(src); rmdir(tmpdir);
return -1;
}
const char *base = strrchr(src, '/');
base = base ? base + 1 : src;
char outbin[128];
snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base);
char *dot = strrchr(outbin, '.');
if (dot && strcmp(dot, ".ww") == 0) *dot = '\0';
int got = runwait(outbin);
unlink(src); unlink(outbin); rmdir(tmpdir);
return got;
}
int
main(void)
{
const char *bin = getenv("BIN");
if (!bin) bin = "out/bin";
char absbin[512];
if (bin[0] != '/') {
char cwd[256];
if (getcwd(cwd, sizeof cwd) == NULL) return 1;
snprintf(absbin, sizeof absbin, "%s/%s", cwd, bin);
bin = absbin;
}
char cdrv[640];
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
char wdrv[640];
snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin);
struct { const char *name; const char *path; int gated_on_existence; }
drivers[] = {
{ "cstage", cdrv, 0 },
{ "wwstage", wdrv, 1 },
{ NULL, NULL, 0 },
};
int n = (int)(sizeof rows / sizeof rows[0]);
int total = 0, fail = 0;
for (int d = 0; drivers[d].name; d++) {
if (drivers[d].gated_on_existence
&& access(drivers[d].path, X_OK) != 0) {
fprintf(stderr, "signed_data_emit: skip %s (no %s)\n",
drivers[d].name, drivers[d].path);
continue;
}
for (int i = 0; i < n; i++) {
int got = run_driver(drivers[d].path, &rows[i], i);
total++;
if (got != rows[i].want) {
fprintf(stderr,
"signed_data_emit[%s][%s]: exit=%d want=%d\n",
drivers[d].name, rows[i].label,
got, rows[i].want);
fail++;
}
}
}
if (fail) {
fprintf(stderr,
"signed_data_emit: %d/%d fixtures failed\n", fail, total);
return 1;
}
printf("signed_data_emit: %d/%d ok\n", total, total);
return 0;
}