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:
2026-05-16 03:00:34 +09:00
parent 2f9d6dc43a
commit cf24af8b26
8 changed files with 457 additions and 48 deletions

View File

@@ -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);

View File

@@ -169,23 +169,55 @@ tagged_success_type(Type *u)
return u->params ? u->params->type : NULL;
}
/* eval_enum_value — fold an enum member-value expression to a u64
* constant. Sees prior siblings via the `prev` Tfield list (each
* carries the member's name and resolved value in .offset). Returns
* 1 on success; on failure emits the error and returns 0. The op set
* is the constant subset typical of Hare-style flag enums:
* literal, sibling-ident, + - * / % & | ^ << >>, unary - and ~. */
static int
eval_enum_value(Checker *c, Node *n, Tfield *prev, u64 *out)
/* fold_int_literal — fold the literal subset usable for top-level
* constant slots: int/rune literal, true/false/nil, and a unary
* +/-/~ over the same. No diagnostics; the caller decides what a
* miss means. Shared between eval_enum_value (literal leaves) and
* emit_defs (top-level def rhs).
*
* Whitelist kept tight on purpose: no N_IDENT (no sibling lookup,
* no symbol resolution), no N_BIN. Anything richer belongs in
* eval_enum_value, which calls this for its literal leaves and
* handles sibling/op recursion itself. */
int
fold_int_literal(Node *n, u64 *out)
{
if (n == NULL) return 0;
switch (n->kind) {
case N_INTLIT:
case N_RUNELIT:
*out = n->uval;
return 1;
*out = n->uval; return 1;
case N_TRUE: *out = 1; return 1;
case N_FALSE: *out = 0; return 1;
case N_FALSE:
case N_NIL: *out = 0; return 1;
case N_UN: {
u64 v;
if (!fold_int_literal(n->lhs, &v)) return 0;
switch (n->op) {
case TK_MINUS: *out = (u64)(-(i64)v); return 1;
case TK_TILDE: *out = ~v; return 1;
case TK_PLUS: *out = v; return 1;
default: return 0;
}
}
default: return 0;
}
}
/* eval_enum_value — fold an enum member-value expression to a u64
* constant. Sees prior siblings via the `prev` Tfield list (each
* carries the member's name and resolved value in .offset). Returns
* 1 on success; on failure emits the error and returns 0. The op set
* is the constant subset typical of Hare-style flag enums:
* literal, sibling-ident, + - * / % & | ^ << >>, unary - and ~.
* Literal leaves and unary-over-literal are delegated to
* fold_int_literal so the fold logic lives in one place. */
static int
eval_enum_value(Checker *c, Node *n, Tfield *prev, u64 *out)
{
if (n == NULL) return 0;
if (fold_int_literal(n, out)) return 1;
switch (n->kind) {
case N_IDENT: {
for (Tfield *f = prev; f; f = f->next) {
if (f->name && n->str &&

View File

@@ -531,4 +531,11 @@ struct Checker {
void check_init(Checker*, Arena*);
void check_file(Checker*, Node *file);
/* fold_int_literal — fold an integer-literal-leaf expression to its
* u64 value. Accepts int/rune literal, true/false/nil, and a unary
* +/-/~ over the same (any depth). No sibling-ident, no binary op.
* Returns 1 on success; the call site decides what a miss means
* (eval_enum_value's leaf delegation, emit_defs's DATA-row gate). */
int fold_int_literal(Node*, u64*);
#endif /* WW_H */