wcc/cgen: #117 const slice-of-(str,*fn) DATA + &fn->DATAR reloc (both-stage)
This commit is contained in:
162
cmd/w6c/cgen.c
162
cmd/w6c/cgen.c
@@ -15010,6 +15010,22 @@ emit_tagged_data(FILE *out, Cg *c, const char *name, const char *module,
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* node_fnptr_sym — if `ev` (casts already peeled by the caller) is the
|
||||
* address-of a top-level fn (`&f`), return its mangled TEXT symbol; NULL
|
||||
* otherwise. The detect-half of the FIRST &fn→DATAR reloc machinery
|
||||
* (#117 slice-row + #119 scalar-global); mirrors the address-of-fn codegen
|
||||
* arm (TY_FN at the N_UN TK_AMP ident, cgen.c N_UN TK_AMP). */
|
||||
static const char *
|
||||
node_fnptr_sym(Cg *c, Node *ev)
|
||||
{
|
||||
if (ev == NULL || ev->kind != N_UN || ev->op != TK_AMP) return NULL;
|
||||
Node *opnd = ev->lhs;
|
||||
if (opnd == NULL || opnd->kind != N_IDENT) return NULL;
|
||||
Type *ou = type_chase_named(opnd->type);
|
||||
if (ou == NULL || ou->kind != TY_FN) return NULL;
|
||||
return mod_mangle_fn(c, opnd->str, c->cur_mod);
|
||||
}
|
||||
|
||||
/* tuple_row_foldable — validate that every cast-peeled element of `rhs`
|
||||
* (an N_TUPLE) reduces to a static row: an int literal (fold_int_literal)
|
||||
* or a str literal in a str/slice slot. A tagged element slot has no
|
||||
@@ -15038,6 +15054,8 @@ tuple_row_foldable(Cg *c, Type *u, Node *rhs)
|
||||
if (ev->kind != N_STRLIT) return 0;
|
||||
continue;
|
||||
}
|
||||
/* #117: a `&fn` element folds to an 8B reloc slot. */
|
||||
if (node_fnptr_sym(c, ev) != NULL) continue;
|
||||
u64 v;
|
||||
if (!fold_int_literal(ev, &v)) return 0;
|
||||
}
|
||||
@@ -15068,6 +15086,12 @@ emit_tuple_row_bytes(Cg *c, FILE *out, Type *u, Node *rhs)
|
||||
emit_data_byte(out, 0);
|
||||
continue;
|
||||
}
|
||||
/* #117: a `&fn` element is an 8B zero ptr placeholder; the
|
||||
* reloc is patched in emit_tuple_row_relocs. */
|
||||
if (node_fnptr_sym(c, ev) != NULL) {
|
||||
for (int i = 0; i < 8; i++) emit_data_byte(out, 0);
|
||||
continue;
|
||||
}
|
||||
u64 v = 0;
|
||||
(void)fold_int_literal(ev, &v);
|
||||
for (int i = 0; i < 8; i++)
|
||||
@@ -15091,11 +15115,21 @@ emit_tuple_row_relocs(Cg *c, FILE *out, const char *holder, int row_off,
|
||||
while (ev && ev->kind == N_CAST) ev = ev->lhs;
|
||||
int wide = tp && (type_isstr(tp->type)
|
||||
|| type_isslice(tp->type));
|
||||
if (wide && ev->strlen > 0) {
|
||||
const char *lab = intern_strlit(c, ev->str,
|
||||
ev->strlen);
|
||||
fprintf(out, "DATAR %s+%d(SB),%s(SB)\n",
|
||||
holder, foff, lab);
|
||||
if (wide) {
|
||||
if (ev->strlen > 0) {
|
||||
const char *lab = intern_strlit(c, ev->str,
|
||||
ev->strlen);
|
||||
fprintf(out, "DATAR %s+%d(SB),%s(SB)\n",
|
||||
holder, foff, lab);
|
||||
}
|
||||
} else {
|
||||
/* #117: the `&fn` element's reloc — the FIRST
|
||||
* &fn→DATAR in the emitter; patches the 8B slot at
|
||||
* holder+foff with the fn's TEXT VA. */
|
||||
const char *fsym = node_fnptr_sym(c, ev);
|
||||
if (fsym != NULL)
|
||||
fprintf(out, "DATAR %s+%d(SB),%s(SB)\n",
|
||||
holder, foff, fsym);
|
||||
}
|
||||
foff += tuple_eslot(tp ? tp->type : NULL);
|
||||
}
|
||||
@@ -15163,9 +15197,15 @@ emit_array_data(FILE *out, Cg *c, const char *directive,
|
||||
*
|
||||
* Scoped to a writable `let` — A_DATAR's holder must be a DATAW slot
|
||||
* (w6a asm.c:362), so a read-only `def []T = [...]` can't carry the ptr
|
||||
* reloc. That, a `...` repeat (a slice literal has no target length),
|
||||
* and slice-of-{str,slice,tagged} elements (per-element relocs / #17)
|
||||
* all loud-stop (rule 7) — #10 follow-ups, never silent fall-through.
|
||||
* reloc. That, a `...` repeat (a slice literal has no target length), and
|
||||
* slice-of-{str,slice,tagged} elements (per-element relocs / #17) all
|
||||
* loud-stop (rule 7) — #10 follow-ups, never silent fall-through.
|
||||
*
|
||||
* #117: a slice of (str,*fn)-style TUPLE rows builds the backing as k
|
||||
* tuple rows (emit_tuple_row_*) — str element = 24B header + ptr→char
|
||||
* DATAR, &fn element = 8B slot + fn→DATAR reloc, at tuple-slot offsets.
|
||||
* Bounded to TY_TUPLE rows (fold-6's charclass_map need); other aggregate
|
||||
* element kinds stay loud below (the honest construction boundary).
|
||||
* Returns 0 only on the early shape guards (not a slice / rhs not
|
||||
* N_ARRLIT) so the caller's gate stays the sole entry contract. */
|
||||
static int
|
||||
@@ -15181,10 +15221,6 @@ emit_slice_data(FILE *out, Cg *c, const char *directive, const char *name,
|
||||
"asm.c:362); read-only `def` unsupported (#10, rule 7)");
|
||||
Type *etype = u->sub;
|
||||
Type *eu = type_chase_named(etype);
|
||||
if (eu && (eu->kind == TY_STR || eu->kind == TY_SLICE
|
||||
|| eu->kind == TY_TAGGED))
|
||||
fatal("emit_slice_data: slice-of-{str,slice,tagged} literal "
|
||||
"static-init unsupported (#10 follow-up, rule 7)");
|
||||
int k = 0;
|
||||
for (Node *e = rhs->list; e; e = e->next) {
|
||||
if (e->kind == N_FIELD && e->str && strcmp(e->str, "...") == 0)
|
||||
@@ -15192,24 +15228,59 @@ emit_slice_data(FILE *out, Cg *c, const char *directive, const char *name,
|
||||
"length in a slice literal (#10, rule 7)");
|
||||
k++;
|
||||
}
|
||||
int esz = etype ? (int)etype->size : 1;
|
||||
/* Synthesize [k]T to ride the emit_array_lit_bytes choke-point. */
|
||||
Type arr;
|
||||
memset(&arr, 0, sizeof arr);
|
||||
arr.kind = TY_ARRAY;
|
||||
arr.sub = etype;
|
||||
arr.alen = (u64)k;
|
||||
arr.size = (u64)k * (u64)esz;
|
||||
if (!emit_array_lit_bytes(out, c, &arr, rhs, 0))
|
||||
fatal("emit_slice_data: slice-literal element not a foldable "
|
||||
"constant (#10, rule 7)");
|
||||
|
||||
const char *sym = mod_mangle_value(c, name, module);
|
||||
const char *bk = aprintf(c->a, "%s.d", sym);
|
||||
/* Writable backing data. */
|
||||
fprintf(out, "DATAW %s(SB),\"", bk);
|
||||
emit_array_lit_bytes(out, c, &arr, rhs, 1);
|
||||
fputs("\"\n", out);
|
||||
if (eu && eu->kind == TY_TUPLE) {
|
||||
/* #117 aggregate-element arm: k tuple rows. Validate every row
|
||||
* first (two-pass, partial-row safe). The backing is ONE DATAW
|
||||
* (w6a ignores +off on DATAW), then per-row relocs at the
|
||||
* row's backing-relative offset. */
|
||||
int stride = etype ? (int)etype->size : 0;
|
||||
for (Node *e = rhs->list; e; e = e->next) {
|
||||
Node *row = e;
|
||||
while (row && row->kind == N_CAST) row = row->lhs;
|
||||
if (row == NULL || row->kind != N_TUPLE
|
||||
|| !tuple_row_foldable(c, eu, row))
|
||||
fatal("emit_slice_data: tuple-row element not a "
|
||||
"foldable constant ((str,*fn) rows only; "
|
||||
"#117, rule 7)");
|
||||
}
|
||||
fprintf(out, "DATAW %s(SB),\"", bk);
|
||||
for (Node *e = rhs->list; e; e = e->next) {
|
||||
Node *row = e;
|
||||
while (row && row->kind == N_CAST) row = row->lhs;
|
||||
emit_tuple_row_bytes(c, out, eu, row);
|
||||
}
|
||||
fputs("\"\n", out);
|
||||
int row_off = 0;
|
||||
for (Node *e = rhs->list; e; e = e->next) {
|
||||
Node *row = e;
|
||||
while (row && row->kind == N_CAST) row = row->lhs;
|
||||
emit_tuple_row_relocs(c, out, bk, row_off, eu, row);
|
||||
row_off += stride;
|
||||
}
|
||||
} else {
|
||||
if (eu && (eu->kind == TY_STR || eu->kind == TY_SLICE
|
||||
|| eu->kind == TY_TAGGED))
|
||||
fatal("emit_slice_data: slice-of-{str,slice,tagged} "
|
||||
"literal static-init unsupported (#10 follow-up, "
|
||||
"rule 7)");
|
||||
int esz = etype ? (int)etype->size : 1;
|
||||
/* Synthesize [k]T to ride the emit_array_lit_bytes choke. */
|
||||
Type arr;
|
||||
memset(&arr, 0, sizeof arr);
|
||||
arr.kind = TY_ARRAY;
|
||||
arr.sub = etype;
|
||||
arr.alen = (u64)k;
|
||||
arr.size = (u64)k * (u64)esz;
|
||||
if (!emit_array_lit_bytes(out, c, &arr, rhs, 0))
|
||||
fatal("emit_slice_data: slice-literal element not a "
|
||||
"foldable constant (#10, rule 7)");
|
||||
/* Writable backing data. */
|
||||
fprintf(out, "DATAW %s(SB),\"", bk);
|
||||
emit_array_lit_bytes(out, c, &arr, rhs, 1);
|
||||
fputs("\"\n", out);
|
||||
}
|
||||
/* 24B header: ptr placeholder + LE len + LE cap (both = k). Word
|
||||
* sizes from the type table (rule 13). */
|
||||
fprintf(out, "DATAW %s(SB),\"", sym);
|
||||
@@ -15554,6 +15625,41 @@ let_pre_intern(Cg *c, Node *file)
|
||||
}
|
||||
continue;
|
||||
}
|
||||
/* #117: slice-of-tuple global — pre-intern each row's str-
|
||||
* element literals in row-then-element order so emit_slice_data's
|
||||
* per-row DATAR patches find their _S_ rodata rows (the #48
|
||||
* tuple-arm pattern, walked across k rows). */
|
||||
if (u != NULL && u->kind == TY_SLICE && r != NULL
|
||||
&& r->kind == N_ARRLIT) {
|
||||
Type *seu = type_chase_named(u->sub);
|
||||
if (seu != NULL && seu->kind == TY_TUPLE) {
|
||||
for (Node *row = r->list; row; row = row->next) {
|
||||
Node *rt = row;
|
||||
while (rt && rt->kind == N_CAST)
|
||||
rt = rt->lhs;
|
||||
if (rt == NULL || rt->kind != N_TUPLE)
|
||||
continue;
|
||||
Tparam *tp = seu->params;
|
||||
for (Node *e = rt->list; e;
|
||||
e = e->next,
|
||||
tp = tp ? tp->next : NULL) {
|
||||
Node *ev = e;
|
||||
while (ev && ev->kind == N_CAST)
|
||||
ev = ev->lhs;
|
||||
if (ev == NULL
|
||||
|| ev->kind != N_STRLIT)
|
||||
continue;
|
||||
if (!(tp && (type_isstr(tp->type)
|
||||
|| type_isslice(tp->type))))
|
||||
continue;
|
||||
if (ev->strlen > 0)
|
||||
(void)intern_strlit(c,
|
||||
ev->str, ev->strlen);
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
}
|
||||
/* #87: tagged global with a str/slice-variant literal init —
|
||||
* pre-intern so emit_tagged_data's DATAR (ptr@+8) finds its
|
||||
* _S_ rodata row (the #48 tuple-arm pattern). */
|
||||
|
||||
Reference in New Issue
Block a user