cstage+selfhost+test: fold unary-over-literal in def DATA emit (#24)
Top-level `def NEG: i32 = -100;` skipped DATA emission in both stages — cstage's emit_defs and wwstage's emitdefconstants each carried a literal-leaf whitelist that excluded N_UN nodes. Same gap in check.c's eval_enum_value cstage-side. Surfaced during #10 (lib/os forced an `at` enum for AT_FDCWD=-100 etc. as workaround). Factor a single fold_int_literal helper (cstage check.c; wwstage cgen.ww). Handles N_INTLIT / N_RUNELIT / N_TRUE / N_FALSE / N_NIL plus N_UN with TK_MINUS / TK_TILDE / TK_PLUS recursively. Consume from eval_enum_value, emit_defs, emitdefconstants, enumevalmember — single source of truth for "is this a literal-leaf foldable". Side effect: cstage's def-emit set widens from {INTLIT, RUNELIT, TRUE} to match wwstage's pre-existing 5-shape set plus the new unary peel. Bootstrap byte-id holds (995_self_rebuild green). Test 631 (def_neg_global): 6 rows × cstage/wwstage run + asm byte-identity diff. Covers all three unary arms (-, ~, +), positive regression-pin, i32 + i64 + u32 slots. Follows up #26: revert lib/os.ww `at` enum to three top-level defs.
This commit is contained in:
@@ -6919,23 +6919,24 @@ emit_lets(Cg *c, FILE *out, Node *file)
|
||||
}
|
||||
}
|
||||
|
||||
/* Emit DATA directives for top-level `def` constants whose value is
|
||||
* an integer/rune literal. The w6a side stores the bytes inside .text
|
||||
* and accesses are RIP-relative.
|
||||
*/
|
||||
/* Emit DATA directives for top-level `def` constants whose value
|
||||
* folds to an integer literal. The w6a side stores the bytes inside
|
||||
* .text and accesses are RIP-relative.
|
||||
*
|
||||
* fold_int_literal (cmd/wcc/check.c) gates: int/rune literal,
|
||||
* true/false/nil, and a unary +/-/~ over the same. `def NEG: i32 =
|
||||
* -100;` arrives as N_UN(TK_MINUS, N_INTLIT) — the unary peel is
|
||||
* exactly what the gate is for. Anything richer (sibling refs,
|
||||
* arithmetic) falls through; emit_defs has no scope to resolve
|
||||
* names. */
|
||||
static void
|
||||
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 = 0;
|
||||
if (d->rhs->kind == N_INTLIT || d->rhs->kind == N_RUNELIT) {
|
||||
v = d->rhs->uval;
|
||||
} else if (d->rhs->kind == N_TRUE) {
|
||||
v = 1;
|
||||
} else {
|
||||
continue; /* skip non-integer-literal defs */
|
||||
}
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user