w6c+selfhost: route f64/f32 struct-field load/store through X0

cgexpr leaves float results in X0, not AX, but the struct-field paths
emitted MOVQ AX,off(BX) and MOVQ off(BX),AX — so every store wrote
garbage and every load read garbage, except by accidental register
survival across an unrelated call. examples/lisp only worked because
parsef's X0 happened to live across the broken MOVQ shuffle into
vfloat; any inserted f64 op between them would silently corrupt.

Wire MOVSD/MOVSS X0,… (and the matching loads) into eight field
sites on both compilers: alloc(T{...}), p.x = v through local/ptr/
global, chained r.sub.x = v, *p = v for *f64, let v: T = T{...},
base.x reads, *T.x reads, and chained a.b.c.x reads.

83/83 lisp_test probes still pass; bootstrap reaches a byte-stable
fixed point at ww3 == ww4.
This commit is contained in:
2026-05-13 01:25:51 +09:00
parent 1c184ee6aa
commit da8d34e0d4
3 changed files with 322 additions and 21 deletions

View File

@@ -113,6 +113,24 @@ node_isf32(Node *n)
return n && type_isf32(n->type);
}
/* fld_isfloat — true iff f's underlying type is f32 or f64. The cgen
* passes float values in X0 (via MOVSD/MOVSS), integer/ptr values in
* AX (via MOVQ). Without this check, a field store/load on an f64 slot
* runs through AX and the bits never reach the SSE side — see the
* vfloat / L.curfval traps documented in examples/lisp/CLAUDE.md.
* Sets *isf32 to 1 for f32, 0 for f64. */
static int
fld_isfloat(Type *t, int *isf32)
{
if (isf32) *isf32 = 0;
if (t == NULL) return 0;
if (t->kind == TY_NAMED) t = t->under;
if (t == NULL) return 0;
if (t->kind == TY_F64) return 1;
if (t->kind == TY_F32) { if (isf32) *isf32 = 1; return 1; }
return 0;
}
/* struct ≤16B all-INTEGER: 1 or 2 eightbyte regs.
* Returns 0 if not a struct or too large. */
static int
@@ -1324,6 +1342,28 @@ cgexpr(Cg *c, Node *n, Local *locals)
default: break; /* others rare */
}
}
/* f64/f32 field, plain `=`: cgexpr left the value in
* X0, not AX. Route the store via MOVSD/MOVSS.
* Compound ops on float fields aren't wired here —
* see CLAUDE.md #8 in examples/lisp; same in the
* structlit-init path below. */
int b_isf32 = 0;
if (n->op == TK_ASSIGN
&& fld_isfloat(f->type, &b_isf32)) {
int mov = b_isf32 ? A_MOVSS : A_MOVSD;
if (via_ptr) {
ins2(c, A_MOVQ, amem(D_BP, boff), areg(D_BX));
ins2(c, mov, areg(D_X0), amem(D_BX, foff));
} else if (is_global) {
ins2(c, A_LEAQ,
masym(c, base->str), areg(D_BX));
ins2(c, mov, areg(D_X0), amem(D_BX, foff));
} else {
ins2(c, mov, areg(D_X0),
amem(D_BP, boff + foff));
}
break;
}
/* now store AX into target */
if (via_ptr) {
ins2(c, A_MOVQ, amem(D_BP, boff), areg(D_BX));
@@ -1370,6 +1410,28 @@ cgexpr(Cg *c, Node *n, Local *locals)
else if (fsz == 4) store_op = A_MOVL;
int foff = (int)f->offset;
if (n->op == TK_ASSIGN) {
int c_isf32 = 0;
if (fld_isfloat(ft, &c_isf32)) {
/* f64/f32 chained-store: cgexpr rhs
* left the value in X0. Spill to stack
* so cgexpr on the inner pointer can
* use AX, then reload into X0 and
* MOVSD/MOVSS into the slot. */
int mov = c_isf32 ? A_MOVSS : A_MOVSD;
cgexpr(c, n->rhs, locals);
ins2(c, A_SUBQ, aimm(8), areg(D_SP));
ins2(c, mov, areg(D_X0),
amem(D_SP, 0));
cgexpr(c, n->lhs->lhs, locals);
ins2(c, A_MOVQ, areg(D_AX),
areg(D_BX));
ins2(c, mov, amem(D_SP, 0),
areg(D_X0));
ins2(c, A_ADDQ, aimm(8), areg(D_SP));
ins2(c, mov, areg(D_X0),
amem(D_BX, foff));
break;
}
if (fu && fu->kind == TY_STR) {
/* str rhs: (AX=ptr, BX=len). Stash
* both, then load the struct ptr
@@ -1555,6 +1617,23 @@ cgexpr(Cg *c, Node *n, Local *locals)
Type *pu = (pt && pt->kind == TY_NAMED) ? pt->under : pt;
Type *vt = (pu && pu->kind == TY_PTR) ? pu->sub : NULL;
if (vt && vt->kind == TY_NAMED) vt = vt->under;
/* `*p = v` for *f64 / *f32: cgexpr leaves the value in X0,
* not AX. Spill X0 to the stack, evaluate the pointer
* (clobbers AX/BX freely), then reload X0 and MOVSD/MOVSS
* through the pointer. */
int deref_isf32 = 0;
if (vt && fld_isfloat(vt, &deref_isf32)) {
int mov = deref_isf32 ? A_MOVSS : A_MOVSD;
cgexpr(c, n->rhs, locals);
ins2(c, A_SUBQ, aimm(8), areg(D_SP));
ins2(c, mov, areg(D_X0), amem(D_SP, 0));
cgexpr(c, n->lhs->lhs, locals);
ins2(c, A_MOVQ, areg(D_AX), areg(D_BX));
ins2(c, mov, amem(D_SP, 0), areg(D_X0));
ins2(c, A_ADDQ, aimm(8), areg(D_SP));
ins2(c, mov, areg(D_X0), amem(D_BX, 0));
break;
}
cgexpr(c, n->rhs, locals); /* AX = value (BX too if str) */
ins1(c, A_PUSHQ, areg(D_AX));
if (vt && vt->kind == TY_STR) ins1(c, A_PUSHQ, areg(D_BX));
@@ -1839,14 +1918,37 @@ cgexpr(Cg *c, Node *n, Local *locals)
for (Node *f = v->list; f; f = f->next) {
u64 foff = 0;
int fsz = 8;
Type *ftype = NULL;
for (Tfield *fl = u->fields; fl; fl = fl->next) {
if (strcmp(fl->name, f->str) == 0) {
foff = fl->offset;
fsz = (int)(fl->type ? fl->type->size : 8);
ftype = fl->type;
break;
}
}
cgexpr(c, f->lhs, locals); /* AX = field val */
cgexpr(c, f->lhs, locals); /* AX or (AX,BX) or X0 */
int f_isf32 = 0;
if (fld_isfloat(ftype, &f_isf32)) {
int mov = f_isf32 ? A_MOVSS : A_MOVSD;
ins2(c, A_MOVQ, amem(D_SP, 0), areg(D_BX));
ins2(c, mov, areg(D_X0),
amem(D_BX, (int)foff));
continue;
}
/* str-typed field: cgexpr leaves (AX=ptr, BX=len).
* Route the heap base through CX so both halves
* survive — using BX would clobber len. */
Type *fu = (ftype && ftype->kind == TY_NAMED)
? ftype->under : ftype;
if (fu && fu->kind == TY_STR) {
ins2(c, A_MOVQ, amem(D_SP, 0), areg(D_CX));
ins2(c, A_MOVQ, areg(D_AX),
amem(D_CX, (int)foff + 0));
ins2(c, A_MOVQ, areg(D_BX),
amem(D_CX, (int)foff + 8));
continue;
}
ins2(c, A_MOVQ, amem(D_SP, 0), areg(D_BX));
int op = A_MOVQ;
if (fsz == 1) op = A_MOVB;
@@ -2765,6 +2867,19 @@ cgexpr(Cg *c, Node *n, Local *locals)
areg(D_BX));
break;
}
/* f64/f32 field: route through X0 (MOVSD/MOVSS).
* Loading via MOVQ AX would put the bits in the
* integer reg, and any downstream consumer that
* reads X0 (arg pass, return, arithmetic) would see
* stale data. */
int e_isf32 = 0;
if (fld_isfloat(f->type, &e_isf32)) {
int mov = e_isf32 ? A_MOVSS : A_MOVSD;
ins2(c, mov,
amem(base_reg, base_disp + (int)f->offset),
areg(D_X0));
break;
}
int fsz = (int)(f->type ? f->type->size : 8);
int signed_field = f->type && (
f->type->kind == TY_I8 ||
@@ -2822,6 +2937,17 @@ cgexpr(Cg *c, Node *n, Local *locals)
ins2(c, A_MOVQ, areg(D_CX), areg(D_BX));
break;
}
/* f64/f32 field via *struct: load into X0.
* BX already holds the struct pointer from
* the MOVQ amem(D_BP,off) above. */
int f_isf32 = 0;
if (fld_isfloat(f->type, &f_isf32)) {
int mov = f_isf32 ? A_MOVSS : A_MOVSD;
ins2(c, mov,
amem(D_BX, (int)f->offset),
areg(D_X0));
break;
}
int fsz = (int)(f->type ? f->type->size : 8);
int signed_field = f->type && (
f->type->kind == TY_I8 ||
@@ -2867,6 +2993,15 @@ cgexpr(Cg *c, Node *n, Local *locals)
areg(D_AX));
goto dot_done;
}
/* f64/f32 chained field: read into X0. */
int g_isf32 = 0;
if (fld_isfloat(ft, &g_isf32)) {
int mov = g_isf32 ? A_MOVSS : A_MOVSD;
ins2(c, mov,
amem(D_AX, (int)f->offset),
areg(D_X0));
goto dot_done;
}
int fsz = (int)(ft ? ft->size : 8);
int signed_field = ft && (
ft->kind == TY_I8 ||
@@ -3291,6 +3426,13 @@ cgstmt(Cg *c, Node *n, Local **locals, int *frame)
continue;
}
cgexpr(c, f->lhs, *locals);
int sl_isf32 = 0;
if (fld_isfloat(ft, &sl_isf32)) {
int mov = sl_isf32 ? A_MOVSS : A_MOVSD;
ins2(c, mov, areg(D_X0),
amem(D_BP, off + (int)foff));
continue;
}
int op = A_MOVQ;
if (fsz == 1) op = A_MOVB;
else if (fsz == 4) op = A_MOVL;