wcc/cgen: #145 slice-copy-assign LHS s.arr[lo:hi]=bs — N_SLICE-LHS arm, runtime byte-copy loop, esz via type table (both stages)

Probe-first find for the path c2 appendlit (buf.buf[lo..hi]=bs): a
slice-copy-assign into a struct-field array sub-range emitted ZERO code —
silent NO-OP, both stages, both-wrong-identical (#263), so runtime is the
only net. N_ASSIGN gains an N_SLICE-LHS arm (cgen.c + cgenexpr.ww
slicebaseesz twin) reusing the N_SLICE-read base/esz cascade and copying
(hi-lo)*esz bytes from rhs.ptr via a runtime loop (len is runtime; no
REP/MOVSB). esz routed through the type table (rule 13; [N]u8->1). Hare
len(bs)==hi-lo assert deferred to #149.
This commit is contained in:
2026-06-08 09:58:22 +09:00
parent f1dcd4ecae
commit 6e1d958d9b
6 changed files with 541 additions and 0 deletions

View File

@@ -23835,6 +23835,39 @@ fn cgbasecap(c: *cgen, base: *node, dst: str) bool = {
// remaining to the base's end (#20, Go/Hare-identical) via cgbasecap.
// ptr advances by BYTES (lo*esz, #76; ref/hare/rt/ensure.ha:30
// membsz-unit); esz from the type table, mirroring the cgindex idiom.
// slicebaseesz — element width of a sliceable base, exactly mirroring the
// esz cascade cgslice computes inline for ptr-scaling (baselocal/globaltn →
// elemsizeofc; N_DOT field → dotbu.sub.size; N_ARRLIT → elemsizeofc; alias-
// NAMED override → chased sub.size). The #145 slice-copy-assign arm scales
// the COUNT by this same width, so it MUST match cgslice's ptr scaling
// byte-for-byte (cgslice supplies the dst ptr). Cstage twin: the N_SLICE-LHS
// arm in cgen.c N_ASSIGN reuses the read-path one-liner directly.
fn slicebaseesz(c: *cgen, base: *node) i32 = {
if (base == nil) { return 1; };
let esz: i32 = 1;
if (base.kind == nkind.N_IDENT) {
let bl: *local = localfindnode(c, base.str);
if (bl != nil) {
esz = elemsizeofc(c, bl.tnode);
} else {
let gt: *node = letvartnode(c, base.str);
if (gt != nil) { esz = elemsizeofc(c, gt); };
};
let bt: *tinfo = base.type_: *tinfo;
if (bt != nil) { if (bt.kind == tykind.TY_NAMED) {
let bu60: *tinfo = tichase(bt);
let es60: *tinfo = tichase(bu60.sub);
if (es60 != nil) { esz = es60.size: i32; };
};};
} else { if (base.kind == nkind.N_DOT) {
let db: *tinfo = tichase(base.type_: *tinfo);
if (db != nil) { if (db.sub != nil) { esz = db.sub.size: i32; }; };
} else { if (base.kind == nkind.N_ARRLIT) {
esz = elemsizeofc(c, base.lhs);
};};};
return esz;
};
fn cgslice(c: *cgen, n: *node) void = {
let base: *node = n.lhs;
let lo: *node = n.rhs;
@@ -29259,6 +29292,47 @@ fn cgcall(c: *cgen, n: *node) void = {
fn cgassign(c: *cgen, n: *node) void = {
let lhs: *node = n.lhs;
// #145 (c1.5a): bulk slice-copy-assign `s.arr[lo:hi] = bs` (LHS is
// N_SLICE). No legacy cgassign arm catches N_SLICE — the statement
// silently emitted nothing (both stages, byte-id-green, #263-class).
// Twin of cstage cgen.c N_ASSIGN N_SLICE arm (full WHY there).
// cgexpr(lhs) routes to cgslice and leaves AX = dst ptr (base+lo*esz),
// BX = hi-lo (element count), CX = cap; slicebaseesz gives the SAME
// esz cgslice scaled the ptr by, so BX*esz is the byte count. Then a
// runtime-counted byte-granular copy from bs.ptr — byte loop because
// the length is RUNTIME (w6a has no REP/MOVSB). Only plain `=`. The
// Hare len(bs)==hi-lo assert is task #149 (rule-7: documented, not a
// c2 blocker — appendlit's lengths are equal by construction).
if (lhs != nil) { if (lhs.kind == nkind.N_SLICE
&& n.op == tkind.TK_ASSIGN) {
let esz: i32 = slicebaseesz(c, lhs.lhs);
cgexpr(c, lhs);
if (esz > 1) {
emitline("\tMOVQ\t$");
emitint(esz: i64);
emitline(", DX\n");
emitline("\tIMULQ\tDX, BX\n");
};
emitline("\tPUSHQ\tAX\n");
emitline("\tPUSHQ\tBX\n");
cgexpr(c, n.rhs);
emitline("\tMOVQ\tAX, SI\n");
emitline("\tPOPQ\tCX\n");
emitline("\tPOPQ\tDI\n");
let top: str = mklabel(c, "scpy");
let end: str = mklabel(c, "scpe");
emitlabel(top);
emitline("\tCMPQ\t$0, CX\n");
emitline("\tJLE\t"); emitline(end); emitline("\n");
emitline("\tMOVB\t(SI), AX\n");
emitline("\tMOVB\tAX, (DI)\n");
emitline("\tADDQ\t$1, SI\n");
emitline("\tADDQ\t$1, DI\n");
emitline("\tSUBQ\t$1, CX\n");
emitline("\tJMP\t"); emitline(top); emitline("\n");
emitlabel(end);
return;
};};
// #20 (task): struct-lit rhs into an INDEXED struct element —
// `a[i] = pt{...}`, `(*ts)[i].caps[k] = capture{...}` — a
// DEREF place (`*p = pt{...}`) or an indexed-base FIELD place