wcc: single-dot field compound assignment routes through one combine helper
s.f *= v silently became s.f = v (and the other non-+=/-= ops dropped likewise) in BOTH stages across five lvalue sub-arms: via-ptr field, direct local field, str/slice pseudo-field, and the two global-field forms. Funnel all five through a shared combine dispatch (cgdotfieldcombine / cg_dotfield_combine) emitting the load-OP-store sequence at field width, hard-erroring the unhandled kinds — close-by- construction so no arm stays on the old PLUSEQ-only path (the #133 BUS-routing lesson; #227 sites A/B are the closed siblings). The refactor routes the corpus's existing +=/-= sites through the same helper output-identically (byte-id held). Review item #34. Both stages move in one commit: one emission contract; splitting the halves would leave the byte-id gates red in between.
This commit is contained in:
7
Makefile
7
Makefile
@@ -323,6 +323,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \
|
||||
$(BIN)/test_nullable_try_run \
|
||||
$(BIN)/test_nullable_assert_run \
|
||||
$(BIN)/test_idxfield_compound_run \
|
||||
$(BIN)/test_dotfield_compound_run \
|
||||
$(BIN)/test_nested_union_widen_run \
|
||||
$(BIN)/test_sret_struct_return \
|
||||
$(BIN)/test_sret_struct_return_run \
|
||||
@@ -1785,6 +1786,12 @@ $(BIN)/test_idxfield_compound_run: test/wcc/949_idxfield_compound_run.c \
|
||||
$(LIB)/libwwrt.a | $(BIN)
|
||||
$(CC) $(CFLAGS) -o $@ $<
|
||||
|
||||
$(BIN)/test_dotfield_compound_run: test/wcc/949_dotfield_compound_run.c \
|
||||
$(BIN)/ww $(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \
|
||||
$(BIN)/ww_ww $(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \
|
||||
$(LIB)/libwwrt.a | $(BIN)
|
||||
$(CC) $(CFLAGS) -o $@ $<
|
||||
|
||||
$(BIN)/test_sret_struct_return: test/wcc/721_sret_struct_return.c \
|
||||
$(BIN)/w6c $(BIN)/w6c_ww | $(BIN)
|
||||
$(CC) $(CFLAGS) -o $@ $<
|
||||
|
||||
105
cmd/w6c/cgen.c
105
cmd/w6c/cgen.c
@@ -3905,6 +3905,89 @@ cg_arrlit_fill_bp(Cg *c, Local **locals, Type *lu, Node *arrlit, int off)
|
||||
}
|
||||
}
|
||||
|
||||
/* cg_dotfield_combine — single-dot field compound combine. The old
|
||||
* field value is in BX, the rhs in AX; the result is left in AX.
|
||||
* PLUSEQ/MINUSEQ preserve the pre-#34 emission (byte-id); the other 8
|
||||
* ops were silently DROPPED (the arm fell through to a plain store of
|
||||
* the rhs -> `s.f = rhs`, #34/#263). SLASHEQ/PERCENTEQ/LSHIFTEQ/RSHIFTEQ
|
||||
* need the lhs in AX and the divisor/count in CX, so swap (rhs AX->CX,
|
||||
* old BX->AX) first. Signed RSHIFTEQ uses SARQ, unsigned SHRQ (#136).
|
||||
* Mirrors wwstage cgdotfieldcombine — both stages emit identical asm. */
|
||||
static void
|
||||
cg_dotfield_combine(Cg *c, int op, int unsignd)
|
||||
{
|
||||
switch (op) {
|
||||
case TK_PLUSEQ: ins2(c, A_ADDQ, areg(D_BX), areg(D_AX)); break;
|
||||
case TK_MINUSEQ:
|
||||
ins2(c, A_SUBQ, areg(D_AX), areg(D_BX));
|
||||
ins2(c, A_MOVQ, areg(D_BX), areg(D_AX));
|
||||
break;
|
||||
case TK_STAREQ: ins2(c, A_IMULQ, areg(D_BX), areg(D_AX)); break;
|
||||
case TK_AMPEQ: ins2(c, A_ANDQ, areg(D_BX), areg(D_AX)); break;
|
||||
case TK_PIPEEQ: ins2(c, A_ORQ, areg(D_BX), areg(D_AX)); break;
|
||||
case TK_CARETEQ: ins2(c, A_XORQ, areg(D_BX), areg(D_AX)); break;
|
||||
case TK_SLASHEQ:
|
||||
ins2(c, A_MOVQ, areg(D_AX), areg(D_CX));
|
||||
ins2(c, A_MOVQ, areg(D_BX), areg(D_AX));
|
||||
if (unsignd) {
|
||||
ins2(c, A_MOVQ, aimm(0), areg(D_DX));
|
||||
ins1(c, A_DIVQ, areg(D_CX));
|
||||
} else {
|
||||
ins0(c, A_CQO);
|
||||
ins1(c, A_IDIVQ, areg(D_CX));
|
||||
}
|
||||
break;
|
||||
case TK_PERCENTEQ:
|
||||
ins2(c, A_MOVQ, areg(D_AX), areg(D_CX));
|
||||
ins2(c, A_MOVQ, areg(D_BX), areg(D_AX));
|
||||
if (unsignd) {
|
||||
ins2(c, A_MOVQ, aimm(0), areg(D_DX));
|
||||
ins1(c, A_DIVQ, areg(D_CX));
|
||||
} else {
|
||||
ins0(c, A_CQO);
|
||||
ins1(c, A_IDIVQ, areg(D_CX));
|
||||
}
|
||||
ins2(c, A_MOVQ, areg(D_DX), areg(D_AX));
|
||||
break;
|
||||
case TK_LSHIFTEQ:
|
||||
ins2(c, A_MOVQ, areg(D_AX), areg(D_CX));
|
||||
ins2(c, A_MOVQ, areg(D_BX), areg(D_AX));
|
||||
ins2(c, A_SHLQ, areg(D_CX), areg(D_AX));
|
||||
break;
|
||||
case TK_RSHIFTEQ:
|
||||
ins2(c, A_MOVQ, areg(D_AX), areg(D_CX));
|
||||
ins2(c, A_MOVQ, areg(D_BX), areg(D_AX));
|
||||
ins2(c, unsignd ? A_SHRQ : A_SARQ, areg(D_CX), areg(D_AX));
|
||||
break;
|
||||
default:
|
||||
fatal("single-dot field compound: unknown op tk=%d "
|
||||
"(#34/rule-7)", op);
|
||||
}
|
||||
}
|
||||
|
||||
/* cg_dotfield_hardstop — loud-stop a single-dot field compound on a
|
||||
* non-integer field (float/str/slice/tagged): the combine arm only
|
||||
* speaks integer ABI; pre-#34 these silently became `s.f = rhs`.
|
||||
* Mirrors the wwstage cgdotfieldhardstop gate. (#34/rule-7) */
|
||||
static void
|
||||
cg_dotfield_hardstop(Type *ft)
|
||||
{
|
||||
int isf32 = 0;
|
||||
Type *u = type_chase_named(ft);
|
||||
if (u && u->kind == TY_TAGGED)
|
||||
fatal("single-dot field compound on tagged field not wired "
|
||||
"(#34/rule-7)");
|
||||
if (u && u->kind == TY_STR)
|
||||
fatal("single-dot field compound on str field not wired "
|
||||
"(#34/rule-7)");
|
||||
if (u && u->kind == TY_SLICE)
|
||||
fatal("single-dot field compound on slice field not wired "
|
||||
"(#34/rule-7)");
|
||||
if (fld_isfloat(ft, &isf32))
|
||||
fatal("single-dot field compound on float field not wired "
|
||||
"(#34/rule-7)");
|
||||
}
|
||||
|
||||
static void
|
||||
cgexpr(Cg *c, Node *n, Local *locals)
|
||||
{
|
||||
@@ -4952,16 +5035,7 @@ cgexpr(Cg *c, Node *n, Local *locals)
|
||||
cgexpr(c, n->rhs, locals);
|
||||
if (n->op != TK_ASSIGN) {
|
||||
ins1(c, A_POPQ, areg(D_BX));
|
||||
switch (n->op) {
|
||||
case TK_PLUSEQ: ins2(c, A_ADDQ, areg(D_BX), areg(D_AX)); break;
|
||||
case TK_MINUSEQ:
|
||||
/* old in BX, rhs in AX; want AX = old-rhs.
|
||||
* SUBQ src,dst is dst -= src in Plan 9. */
|
||||
ins2(c, A_SUBQ, areg(D_AX), areg(D_BX));
|
||||
ins2(c, A_MOVQ, areg(D_BX), areg(D_AX));
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
cg_dotfield_combine(c, n->op, 0);
|
||||
}
|
||||
if (via_ptr) {
|
||||
ins2(c, A_MOVQ, amem(D_BP, boff), areg(D_BX));
|
||||
@@ -5226,15 +5300,8 @@ cgexpr(Cg *c, Node *n, Local *locals)
|
||||
cgexpr(c, n->rhs, locals); /* AX = rhs */
|
||||
if (n->op != TK_ASSIGN) {
|
||||
ins1(c, A_POPQ, areg(D_BX));
|
||||
switch (n->op) {
|
||||
case TK_PLUSEQ: ins2(c, A_ADDQ, areg(D_BX), areg(D_AX)); break;
|
||||
case TK_MINUSEQ:
|
||||
/* old in BX, rhs in AX; want AX=old-rhs */
|
||||
ins2(c, A_SUBQ, areg(D_AX), areg(D_BX));
|
||||
ins2(c, A_MOVQ, areg(D_BX), areg(D_AX));
|
||||
break;
|
||||
default: break; /* others rare */
|
||||
}
|
||||
cg_dotfield_hardstop(f->type);
|
||||
cg_dotfield_combine(c, n->op, type_isunsigned(f->type));
|
||||
}
|
||||
/* f64/f32 field, plain `=`: cgexpr left the value in
|
||||
* X0, not AX. Route the store via MOVSD/MOVSS.
|
||||
|
||||
@@ -23409,6 +23409,85 @@ fn cgtrytaggedshift(c: *cgen, n: *node) bool = {
|
||||
// INDEX/DOT) from the box address cgexpr leaves in AX. Both were
|
||||
// silent word0 unwraps pre-#35. >32B non-call stays loud (the cursor
|
||||
// cannot carry it; #40 family). Mirrors cstage N_TRYPROP/N_TRYUNW.
|
||||
// cgdotfieldcombine — single-dot field compound combine. The old
|
||||
// field value is in BX, the rhs in AX; the result is left in AX.
|
||||
// PLUSEQ/MINUSEQ preserve the pre-#34 emission (byte-id); the other 8
|
||||
// ops were silently DROPPED (the arm fell through to a plain store of
|
||||
// the rhs → `s.f = rhs`, #34/#263). SLASHEQ/PERCENTEQ/LSHIFTEQ/RSHIFTEQ
|
||||
// need the lhs in AX and the divisor/count in CX, so swap (rhs AX→CX,
|
||||
// old BX→AX) first. Signed RSHIFTEQ uses SARQ, unsigned SHRQ (#136).
|
||||
// Mirrors cstage cg_dotfield_combine — both stages emit identical asm.
|
||||
fn cgdotfieldcombine(c: *cgen, op: tkind, unsignd: bool) void = {
|
||||
if (op == tkind.TK_PLUSEQ) { emitline("\tADDQ\tBX, AX\n"); return; };
|
||||
if (op == tkind.TK_MINUSEQ) {
|
||||
emitline("\tSUBQ\tAX, BX\n");
|
||||
emitline("\tMOVQ\tBX, AX\n");
|
||||
return;
|
||||
};
|
||||
if (op == tkind.TK_STAREQ) { emitline("\tIMULQ\tBX, AX\n"); return; };
|
||||
if (op == tkind.TK_AMPEQ) { emitline("\tANDQ\tBX, AX\n"); return; };
|
||||
if (op == tkind.TK_PIPEEQ) { emitline("\tORQ\tBX, AX\n"); return; };
|
||||
if (op == tkind.TK_CARETEQ) { emitline("\tXORQ\tBX, AX\n"); return; };
|
||||
if (op == tkind.TK_SLASHEQ) {
|
||||
emitline("\tMOVQ\tAX, CX\n");
|
||||
emitline("\tMOVQ\tBX, AX\n");
|
||||
if (unsignd) { emitline("\tMOVQ\t$0, DX\n"); emitline("\tDIVQ\tCX\n"); }
|
||||
else { emitline("\tCQO\n"); emitline("\tIDIVQ\tCX\n"); };
|
||||
return;
|
||||
};
|
||||
if (op == tkind.TK_PERCENTEQ) {
|
||||
emitline("\tMOVQ\tAX, CX\n");
|
||||
emitline("\tMOVQ\tBX, AX\n");
|
||||
if (unsignd) { emitline("\tMOVQ\t$0, DX\n"); emitline("\tDIVQ\tCX\n"); }
|
||||
else { emitline("\tCQO\n"); emitline("\tIDIVQ\tCX\n"); };
|
||||
emitline("\tMOVQ\tDX, AX\n");
|
||||
return;
|
||||
};
|
||||
if (op == tkind.TK_LSHIFTEQ) {
|
||||
emitline("\tMOVQ\tAX, CX\n");
|
||||
emitline("\tMOVQ\tBX, AX\n");
|
||||
emitline("\tSHLQ\tCX, AX\n");
|
||||
return;
|
||||
};
|
||||
if (op == tkind.TK_RSHIFTEQ) {
|
||||
emitline("\tMOVQ\tAX, CX\n");
|
||||
emitline("\tMOVQ\tBX, AX\n");
|
||||
if (unsignd) { emitline("\tSHRQ\tCX, AX\n"); }
|
||||
else { emitline("\tSARQ\tCX, AX\n"); };
|
||||
return;
|
||||
};
|
||||
let m: str = "single-dot field compound: unknown op (#34/rule-7)\n";
|
||||
os.write(2, m.ptr, m.len: u64);
|
||||
os.exit(1);
|
||||
};
|
||||
|
||||
// cgdotfieldhardstop — loud-stop a single-dot field compound on a
|
||||
// non-integer field (float/str/slice/tagged): the combine arm only
|
||||
// speaks integer ABI; pre-#34 these silently became `s.f = rhs`.
|
||||
// Mirrors the cstage cg_dotfield_hardstop gate. (#34/rule-7)
|
||||
fn cgdotfieldhardstop(c: *cgen, ftn: *node) void = {
|
||||
if (istaggedtype(c, ftn)) {
|
||||
let m: str = "single-dot field compound on tagged field not wired (#34/rule-7)\n";
|
||||
os.write(2, m.ptr, m.len: u64);
|
||||
os.exit(1);
|
||||
};
|
||||
if (isstrtype(c, ftn)) {
|
||||
let m: str = "single-dot field compound on str field not wired (#34/rule-7)\n";
|
||||
os.write(2, m.ptr, m.len: u64);
|
||||
os.exit(1);
|
||||
};
|
||||
if (isslicetype(c, ftn)) {
|
||||
let m: str = "single-dot field compound on slice field not wired (#34/rule-7)\n";
|
||||
os.write(2, m.ptr, m.len: u64);
|
||||
os.exit(1);
|
||||
};
|
||||
if (isfloattype(c, ftn)) {
|
||||
let m: str = "single-dot field compound on float field not wired (#34/rule-7)\n";
|
||||
os.write(2, m.ptr, m.len: u64);
|
||||
os.exit(1);
|
||||
};
|
||||
};
|
||||
|
||||
fn cgtryunwcursor(c: *cgen, n: *node, opname: str) void = {
|
||||
let u: *tinfo = nil;
|
||||
if (n.lhs != nil) { u = n.lhs.type_: *tinfo; };
|
||||
@@ -33041,11 +33120,10 @@ fn cgassign(c: *cgen, n: *node) void = {
|
||||
// PLUSEQ is commutative; MINUSEQ
|
||||
// needs lhs - rhs (BX is old lhs,
|
||||
// AX is rhs).
|
||||
if (n.op == tkind.TK_PLUSEQ) { emitline("\tADDQ\tBX, AX\n"); };
|
||||
if (n.op == tkind.TK_MINUSEQ) {
|
||||
emitline("\tSUBQ\tAX, BX\n");
|
||||
emitline("\tMOVQ\tBX, AX\n");
|
||||
};
|
||||
cgdotfieldhardstop(c, fi.tnode);
|
||||
let uns34: bool = false;
|
||||
if (fi.tnode != nil) { if (fi.tnode.type_ != nil) { uns34 = typeisunsigned(fi.tnode.type_: *tinfo); }; };
|
||||
cgdotfieldcombine(c, n.op, uns34);
|
||||
};
|
||||
if (n.op == tkind.TK_ASSIGN) {
|
||||
// str/slice field via *struct: str IS []u8, so both
|
||||
@@ -33267,11 +33345,10 @@ fn cgassign(c: *cgen, n: *node) void = {
|
||||
emitline("\tPOPQ\tBX\n");
|
||||
// PLUSEQ commutes; MINUSEQ needs
|
||||
// lhs-rhs (BX old lhs, AX rhs).
|
||||
if (n.op == tkind.TK_PLUSEQ) { emitline("\tADDQ\tBX, AX\n"); };
|
||||
if (n.op == tkind.TK_MINUSEQ) {
|
||||
emitline("\tSUBQ\tAX, BX\n");
|
||||
emitline("\tMOVQ\tBX, AX\n");
|
||||
};
|
||||
cgdotfieldhardstop(c, fi.tnode);
|
||||
let uns34: bool = false;
|
||||
if (fi.tnode != nil) { if (fi.tnode.type_ != nil) { uns34 = typeisunsigned(fi.tnode.type_: *tinfo); }; };
|
||||
cgdotfieldcombine(c, n.op, uns34);
|
||||
};
|
||||
// str/slice field direct: str IS []u8, so both store the
|
||||
// full 3-word {ptr,len,cap} from (AX,BX,CX) at +0/+8/+16.
|
||||
@@ -33342,11 +33419,7 @@ fn cgassign(c: *cgen, n: *node) void = {
|
||||
emitline("\tPOPQ\tBX\n");
|
||||
// PLUSEQ is commutative; MINUSEQ
|
||||
// needs lhs - rhs.
|
||||
if (n.op == tkind.TK_PLUSEQ) { emitline("\tADDQ\tBX, AX\n"); };
|
||||
if (n.op == tkind.TK_MINUSEQ) {
|
||||
emitline("\tSUBQ\tAX, BX\n");
|
||||
emitline("\tMOVQ\tBX, AX\n");
|
||||
};
|
||||
cgdotfieldcombine(c, n.op, false);
|
||||
emitline("\tMOVQ\t");
|
||||
emitoff(lc.off: i64);
|
||||
emitline("(BP), BX\n");
|
||||
@@ -33375,11 +33448,7 @@ fn cgassign(c: *cgen, n: *node) void = {
|
||||
emitline("\tPUSHQ\tBX\n");
|
||||
cgexpr(c, n.rhs);
|
||||
emitline("\tPOPQ\tBX\n");
|
||||
if (n.op == tkind.TK_PLUSEQ) { emitline("\tADDQ\tBX, AX\n"); };
|
||||
if (n.op == tkind.TK_MINUSEQ) {
|
||||
emitline("\tSUBQ\tAX, BX\n");
|
||||
emitline("\tMOVQ\tBX, AX\n");
|
||||
};
|
||||
cgdotfieldcombine(c, n.op, false);
|
||||
emitline("\tMOVQ\tAX, ");
|
||||
emitoff((lc.off + delta): i64);
|
||||
emitline("(BP)\n");
|
||||
@@ -33630,11 +33699,10 @@ fn cgassign(c: *cgen, n: *node) void = {
|
||||
emitline("\tPUSHQ\tBX\n");
|
||||
cgexpr(c, n.rhs);
|
||||
emitline("\tPOPQ\tBX\n");
|
||||
if (n.op == tkind.TK_PLUSEQ) { emitline("\tADDQ\tBX, AX\n"); };
|
||||
if (n.op == tkind.TK_MINUSEQ) {
|
||||
emitline("\tSUBQ\tAX, BX\n");
|
||||
emitline("\tMOVQ\tBX, AX\n");
|
||||
};
|
||||
cgdotfieldhardstop(c, fi.tnode);
|
||||
let uns34: bool = false;
|
||||
if (fi.tnode != nil) { if (fi.tnode.type_ != nil) { uns34 = typeisunsigned(fi.tnode.type_: *tinfo); }; };
|
||||
cgdotfieldcombine(c, n.op, uns34);
|
||||
let sop: str = fieldstoreop(c, fi);
|
||||
emitline("\tLEAQ\t");
|
||||
emitsymname(c, bn);
|
||||
|
||||
@@ -258,6 +258,85 @@ fn cgtrytaggedshift(c: *cgen, n: *node) bool = {
|
||||
// INDEX/DOT) from the box address cgexpr leaves in AX. Both were
|
||||
// silent word0 unwraps pre-#35. >32B non-call stays loud (the cursor
|
||||
// cannot carry it; #40 family). Mirrors cstage N_TRYPROP/N_TRYUNW.
|
||||
// cgdotfieldcombine — single-dot field compound combine. The old
|
||||
// field value is in BX, the rhs in AX; the result is left in AX.
|
||||
// PLUSEQ/MINUSEQ preserve the pre-#34 emission (byte-id); the other 8
|
||||
// ops were silently DROPPED (the arm fell through to a plain store of
|
||||
// the rhs → `s.f = rhs`, #34/#263). SLASHEQ/PERCENTEQ/LSHIFTEQ/RSHIFTEQ
|
||||
// need the lhs in AX and the divisor/count in CX, so swap (rhs AX→CX,
|
||||
// old BX→AX) first. Signed RSHIFTEQ uses SARQ, unsigned SHRQ (#136).
|
||||
// Mirrors cstage cg_dotfield_combine — both stages emit identical asm.
|
||||
fn cgdotfieldcombine(c: *cgen, op: tkind, unsignd: bool) void = {
|
||||
if (op == tkind.TK_PLUSEQ) { emitline("\tADDQ\tBX, AX\n"); return; };
|
||||
if (op == tkind.TK_MINUSEQ) {
|
||||
emitline("\tSUBQ\tAX, BX\n");
|
||||
emitline("\tMOVQ\tBX, AX\n");
|
||||
return;
|
||||
};
|
||||
if (op == tkind.TK_STAREQ) { emitline("\tIMULQ\tBX, AX\n"); return; };
|
||||
if (op == tkind.TK_AMPEQ) { emitline("\tANDQ\tBX, AX\n"); return; };
|
||||
if (op == tkind.TK_PIPEEQ) { emitline("\tORQ\tBX, AX\n"); return; };
|
||||
if (op == tkind.TK_CARETEQ) { emitline("\tXORQ\tBX, AX\n"); return; };
|
||||
if (op == tkind.TK_SLASHEQ) {
|
||||
emitline("\tMOVQ\tAX, CX\n");
|
||||
emitline("\tMOVQ\tBX, AX\n");
|
||||
if (unsignd) { emitline("\tMOVQ\t$0, DX\n"); emitline("\tDIVQ\tCX\n"); }
|
||||
else { emitline("\tCQO\n"); emitline("\tIDIVQ\tCX\n"); };
|
||||
return;
|
||||
};
|
||||
if (op == tkind.TK_PERCENTEQ) {
|
||||
emitline("\tMOVQ\tAX, CX\n");
|
||||
emitline("\tMOVQ\tBX, AX\n");
|
||||
if (unsignd) { emitline("\tMOVQ\t$0, DX\n"); emitline("\tDIVQ\tCX\n"); }
|
||||
else { emitline("\tCQO\n"); emitline("\tIDIVQ\tCX\n"); };
|
||||
emitline("\tMOVQ\tDX, AX\n");
|
||||
return;
|
||||
};
|
||||
if (op == tkind.TK_LSHIFTEQ) {
|
||||
emitline("\tMOVQ\tAX, CX\n");
|
||||
emitline("\tMOVQ\tBX, AX\n");
|
||||
emitline("\tSHLQ\tCX, AX\n");
|
||||
return;
|
||||
};
|
||||
if (op == tkind.TK_RSHIFTEQ) {
|
||||
emitline("\tMOVQ\tAX, CX\n");
|
||||
emitline("\tMOVQ\tBX, AX\n");
|
||||
if (unsignd) { emitline("\tSHRQ\tCX, AX\n"); }
|
||||
else { emitline("\tSARQ\tCX, AX\n"); };
|
||||
return;
|
||||
};
|
||||
let m: str = "single-dot field compound: unknown op (#34/rule-7)\n";
|
||||
os.write(2, m.ptr, m.len: u64);
|
||||
os.exit(1);
|
||||
};
|
||||
|
||||
// cgdotfieldhardstop — loud-stop a single-dot field compound on a
|
||||
// non-integer field (float/str/slice/tagged): the combine arm only
|
||||
// speaks integer ABI; pre-#34 these silently became `s.f = rhs`.
|
||||
// Mirrors the cstage cg_dotfield_hardstop gate. (#34/rule-7)
|
||||
fn cgdotfieldhardstop(c: *cgen, ftn: *node) void = {
|
||||
if (istaggedtype(c, ftn)) {
|
||||
let m: str = "single-dot field compound on tagged field not wired (#34/rule-7)\n";
|
||||
os.write(2, m.ptr, m.len: u64);
|
||||
os.exit(1);
|
||||
};
|
||||
if (isstrtype(c, ftn)) {
|
||||
let m: str = "single-dot field compound on str field not wired (#34/rule-7)\n";
|
||||
os.write(2, m.ptr, m.len: u64);
|
||||
os.exit(1);
|
||||
};
|
||||
if (isslicetype(c, ftn)) {
|
||||
let m: str = "single-dot field compound on slice field not wired (#34/rule-7)\n";
|
||||
os.write(2, m.ptr, m.len: u64);
|
||||
os.exit(1);
|
||||
};
|
||||
if (isfloattype(c, ftn)) {
|
||||
let m: str = "single-dot field compound on float field not wired (#34/rule-7)\n";
|
||||
os.write(2, m.ptr, m.len: u64);
|
||||
os.exit(1);
|
||||
};
|
||||
};
|
||||
|
||||
fn cgtryunwcursor(c: *cgen, n: *node, opname: str) void = {
|
||||
let u: *tinfo = nil;
|
||||
if (n.lhs != nil) { u = n.lhs.type_: *tinfo; };
|
||||
@@ -9890,11 +9969,10 @@ fn cgassign(c: *cgen, n: *node) void = {
|
||||
// PLUSEQ is commutative; MINUSEQ
|
||||
// needs lhs - rhs (BX is old lhs,
|
||||
// AX is rhs).
|
||||
if (n.op == tkind.TK_PLUSEQ) { emitline("\tADDQ\tBX, AX\n"); };
|
||||
if (n.op == tkind.TK_MINUSEQ) {
|
||||
emitline("\tSUBQ\tAX, BX\n");
|
||||
emitline("\tMOVQ\tBX, AX\n");
|
||||
};
|
||||
cgdotfieldhardstop(c, fi.tnode);
|
||||
let uns34: bool = false;
|
||||
if (fi.tnode != nil) { if (fi.tnode.type_ != nil) { uns34 = typeisunsigned(fi.tnode.type_: *tinfo); }; };
|
||||
cgdotfieldcombine(c, n.op, uns34);
|
||||
};
|
||||
if (n.op == tkind.TK_ASSIGN) {
|
||||
// str/slice field via *struct: str IS []u8, so both
|
||||
@@ -10116,11 +10194,10 @@ fn cgassign(c: *cgen, n: *node) void = {
|
||||
emitline("\tPOPQ\tBX\n");
|
||||
// PLUSEQ commutes; MINUSEQ needs
|
||||
// lhs-rhs (BX old lhs, AX rhs).
|
||||
if (n.op == tkind.TK_PLUSEQ) { emitline("\tADDQ\tBX, AX\n"); };
|
||||
if (n.op == tkind.TK_MINUSEQ) {
|
||||
emitline("\tSUBQ\tAX, BX\n");
|
||||
emitline("\tMOVQ\tBX, AX\n");
|
||||
};
|
||||
cgdotfieldhardstop(c, fi.tnode);
|
||||
let uns34: bool = false;
|
||||
if (fi.tnode != nil) { if (fi.tnode.type_ != nil) { uns34 = typeisunsigned(fi.tnode.type_: *tinfo); }; };
|
||||
cgdotfieldcombine(c, n.op, uns34);
|
||||
};
|
||||
// str/slice field direct: str IS []u8, so both store the
|
||||
// full 3-word {ptr,len,cap} from (AX,BX,CX) at +0/+8/+16.
|
||||
@@ -10191,11 +10268,7 @@ fn cgassign(c: *cgen, n: *node) void = {
|
||||
emitline("\tPOPQ\tBX\n");
|
||||
// PLUSEQ is commutative; MINUSEQ
|
||||
// needs lhs - rhs.
|
||||
if (n.op == tkind.TK_PLUSEQ) { emitline("\tADDQ\tBX, AX\n"); };
|
||||
if (n.op == tkind.TK_MINUSEQ) {
|
||||
emitline("\tSUBQ\tAX, BX\n");
|
||||
emitline("\tMOVQ\tBX, AX\n");
|
||||
};
|
||||
cgdotfieldcombine(c, n.op, false);
|
||||
emitline("\tMOVQ\t");
|
||||
emitoff(lc.off: i64);
|
||||
emitline("(BP), BX\n");
|
||||
@@ -10224,11 +10297,7 @@ fn cgassign(c: *cgen, n: *node) void = {
|
||||
emitline("\tPUSHQ\tBX\n");
|
||||
cgexpr(c, n.rhs);
|
||||
emitline("\tPOPQ\tBX\n");
|
||||
if (n.op == tkind.TK_PLUSEQ) { emitline("\tADDQ\tBX, AX\n"); };
|
||||
if (n.op == tkind.TK_MINUSEQ) {
|
||||
emitline("\tSUBQ\tAX, BX\n");
|
||||
emitline("\tMOVQ\tBX, AX\n");
|
||||
};
|
||||
cgdotfieldcombine(c, n.op, false);
|
||||
emitline("\tMOVQ\tAX, ");
|
||||
emitoff((lc.off + delta): i64);
|
||||
emitline("(BP)\n");
|
||||
@@ -10479,11 +10548,10 @@ fn cgassign(c: *cgen, n: *node) void = {
|
||||
emitline("\tPUSHQ\tBX\n");
|
||||
cgexpr(c, n.rhs);
|
||||
emitline("\tPOPQ\tBX\n");
|
||||
if (n.op == tkind.TK_PLUSEQ) { emitline("\tADDQ\tBX, AX\n"); };
|
||||
if (n.op == tkind.TK_MINUSEQ) {
|
||||
emitline("\tSUBQ\tAX, BX\n");
|
||||
emitline("\tMOVQ\tBX, AX\n");
|
||||
};
|
||||
cgdotfieldhardstop(c, fi.tnode);
|
||||
let uns34: bool = false;
|
||||
if (fi.tnode != nil) { if (fi.tnode.type_ != nil) { uns34 = typeisunsigned(fi.tnode.type_: *tinfo); }; };
|
||||
cgdotfieldcombine(c, n.op, uns34);
|
||||
let sop: str = fieldstoreop(c, fi);
|
||||
emitline("\tLEAQ\t");
|
||||
emitsymname(c, bn);
|
||||
|
||||
@@ -23409,6 +23409,85 @@ fn cgtrytaggedshift(c: *cgen, n: *node) bool = {
|
||||
// INDEX/DOT) from the box address cgexpr leaves in AX. Both were
|
||||
// silent word0 unwraps pre-#35. >32B non-call stays loud (the cursor
|
||||
// cannot carry it; #40 family). Mirrors cstage N_TRYPROP/N_TRYUNW.
|
||||
// cgdotfieldcombine — single-dot field compound combine. The old
|
||||
// field value is in BX, the rhs in AX; the result is left in AX.
|
||||
// PLUSEQ/MINUSEQ preserve the pre-#34 emission (byte-id); the other 8
|
||||
// ops were silently DROPPED (the arm fell through to a plain store of
|
||||
// the rhs → `s.f = rhs`, #34/#263). SLASHEQ/PERCENTEQ/LSHIFTEQ/RSHIFTEQ
|
||||
// need the lhs in AX and the divisor/count in CX, so swap (rhs AX→CX,
|
||||
// old BX→AX) first. Signed RSHIFTEQ uses SARQ, unsigned SHRQ (#136).
|
||||
// Mirrors cstage cg_dotfield_combine — both stages emit identical asm.
|
||||
fn cgdotfieldcombine(c: *cgen, op: tkind, unsignd: bool) void = {
|
||||
if (op == tkind.TK_PLUSEQ) { emitline("\tADDQ\tBX, AX\n"); return; };
|
||||
if (op == tkind.TK_MINUSEQ) {
|
||||
emitline("\tSUBQ\tAX, BX\n");
|
||||
emitline("\tMOVQ\tBX, AX\n");
|
||||
return;
|
||||
};
|
||||
if (op == tkind.TK_STAREQ) { emitline("\tIMULQ\tBX, AX\n"); return; };
|
||||
if (op == tkind.TK_AMPEQ) { emitline("\tANDQ\tBX, AX\n"); return; };
|
||||
if (op == tkind.TK_PIPEEQ) { emitline("\tORQ\tBX, AX\n"); return; };
|
||||
if (op == tkind.TK_CARETEQ) { emitline("\tXORQ\tBX, AX\n"); return; };
|
||||
if (op == tkind.TK_SLASHEQ) {
|
||||
emitline("\tMOVQ\tAX, CX\n");
|
||||
emitline("\tMOVQ\tBX, AX\n");
|
||||
if (unsignd) { emitline("\tMOVQ\t$0, DX\n"); emitline("\tDIVQ\tCX\n"); }
|
||||
else { emitline("\tCQO\n"); emitline("\tIDIVQ\tCX\n"); };
|
||||
return;
|
||||
};
|
||||
if (op == tkind.TK_PERCENTEQ) {
|
||||
emitline("\tMOVQ\tAX, CX\n");
|
||||
emitline("\tMOVQ\tBX, AX\n");
|
||||
if (unsignd) { emitline("\tMOVQ\t$0, DX\n"); emitline("\tDIVQ\tCX\n"); }
|
||||
else { emitline("\tCQO\n"); emitline("\tIDIVQ\tCX\n"); };
|
||||
emitline("\tMOVQ\tDX, AX\n");
|
||||
return;
|
||||
};
|
||||
if (op == tkind.TK_LSHIFTEQ) {
|
||||
emitline("\tMOVQ\tAX, CX\n");
|
||||
emitline("\tMOVQ\tBX, AX\n");
|
||||
emitline("\tSHLQ\tCX, AX\n");
|
||||
return;
|
||||
};
|
||||
if (op == tkind.TK_RSHIFTEQ) {
|
||||
emitline("\tMOVQ\tAX, CX\n");
|
||||
emitline("\tMOVQ\tBX, AX\n");
|
||||
if (unsignd) { emitline("\tSHRQ\tCX, AX\n"); }
|
||||
else { emitline("\tSARQ\tCX, AX\n"); };
|
||||
return;
|
||||
};
|
||||
let m: str = "single-dot field compound: unknown op (#34/rule-7)\n";
|
||||
os.write(2, m.ptr, m.len: u64);
|
||||
os.exit(1);
|
||||
};
|
||||
|
||||
// cgdotfieldhardstop — loud-stop a single-dot field compound on a
|
||||
// non-integer field (float/str/slice/tagged): the combine arm only
|
||||
// speaks integer ABI; pre-#34 these silently became `s.f = rhs`.
|
||||
// Mirrors the cstage cg_dotfield_hardstop gate. (#34/rule-7)
|
||||
fn cgdotfieldhardstop(c: *cgen, ftn: *node) void = {
|
||||
if (istaggedtype(c, ftn)) {
|
||||
let m: str = "single-dot field compound on tagged field not wired (#34/rule-7)\n";
|
||||
os.write(2, m.ptr, m.len: u64);
|
||||
os.exit(1);
|
||||
};
|
||||
if (isstrtype(c, ftn)) {
|
||||
let m: str = "single-dot field compound on str field not wired (#34/rule-7)\n";
|
||||
os.write(2, m.ptr, m.len: u64);
|
||||
os.exit(1);
|
||||
};
|
||||
if (isslicetype(c, ftn)) {
|
||||
let m: str = "single-dot field compound on slice field not wired (#34/rule-7)\n";
|
||||
os.write(2, m.ptr, m.len: u64);
|
||||
os.exit(1);
|
||||
};
|
||||
if (isfloattype(c, ftn)) {
|
||||
let m: str = "single-dot field compound on float field not wired (#34/rule-7)\n";
|
||||
os.write(2, m.ptr, m.len: u64);
|
||||
os.exit(1);
|
||||
};
|
||||
};
|
||||
|
||||
fn cgtryunwcursor(c: *cgen, n: *node, opname: str) void = {
|
||||
let u: *tinfo = nil;
|
||||
if (n.lhs != nil) { u = n.lhs.type_: *tinfo; };
|
||||
@@ -33041,11 +33120,10 @@ fn cgassign(c: *cgen, n: *node) void = {
|
||||
// PLUSEQ is commutative; MINUSEQ
|
||||
// needs lhs - rhs (BX is old lhs,
|
||||
// AX is rhs).
|
||||
if (n.op == tkind.TK_PLUSEQ) { emitline("\tADDQ\tBX, AX\n"); };
|
||||
if (n.op == tkind.TK_MINUSEQ) {
|
||||
emitline("\tSUBQ\tAX, BX\n");
|
||||
emitline("\tMOVQ\tBX, AX\n");
|
||||
};
|
||||
cgdotfieldhardstop(c, fi.tnode);
|
||||
let uns34: bool = false;
|
||||
if (fi.tnode != nil) { if (fi.tnode.type_ != nil) { uns34 = typeisunsigned(fi.tnode.type_: *tinfo); }; };
|
||||
cgdotfieldcombine(c, n.op, uns34);
|
||||
};
|
||||
if (n.op == tkind.TK_ASSIGN) {
|
||||
// str/slice field via *struct: str IS []u8, so both
|
||||
@@ -33267,11 +33345,10 @@ fn cgassign(c: *cgen, n: *node) void = {
|
||||
emitline("\tPOPQ\tBX\n");
|
||||
// PLUSEQ commutes; MINUSEQ needs
|
||||
// lhs-rhs (BX old lhs, AX rhs).
|
||||
if (n.op == tkind.TK_PLUSEQ) { emitline("\tADDQ\tBX, AX\n"); };
|
||||
if (n.op == tkind.TK_MINUSEQ) {
|
||||
emitline("\tSUBQ\tAX, BX\n");
|
||||
emitline("\tMOVQ\tBX, AX\n");
|
||||
};
|
||||
cgdotfieldhardstop(c, fi.tnode);
|
||||
let uns34: bool = false;
|
||||
if (fi.tnode != nil) { if (fi.tnode.type_ != nil) { uns34 = typeisunsigned(fi.tnode.type_: *tinfo); }; };
|
||||
cgdotfieldcombine(c, n.op, uns34);
|
||||
};
|
||||
// str/slice field direct: str IS []u8, so both store the
|
||||
// full 3-word {ptr,len,cap} from (AX,BX,CX) at +0/+8/+16.
|
||||
@@ -33342,11 +33419,7 @@ fn cgassign(c: *cgen, n: *node) void = {
|
||||
emitline("\tPOPQ\tBX\n");
|
||||
// PLUSEQ is commutative; MINUSEQ
|
||||
// needs lhs - rhs.
|
||||
if (n.op == tkind.TK_PLUSEQ) { emitline("\tADDQ\tBX, AX\n"); };
|
||||
if (n.op == tkind.TK_MINUSEQ) {
|
||||
emitline("\tSUBQ\tAX, BX\n");
|
||||
emitline("\tMOVQ\tBX, AX\n");
|
||||
};
|
||||
cgdotfieldcombine(c, n.op, false);
|
||||
emitline("\tMOVQ\t");
|
||||
emitoff(lc.off: i64);
|
||||
emitline("(BP), BX\n");
|
||||
@@ -33375,11 +33448,7 @@ fn cgassign(c: *cgen, n: *node) void = {
|
||||
emitline("\tPUSHQ\tBX\n");
|
||||
cgexpr(c, n.rhs);
|
||||
emitline("\tPOPQ\tBX\n");
|
||||
if (n.op == tkind.TK_PLUSEQ) { emitline("\tADDQ\tBX, AX\n"); };
|
||||
if (n.op == tkind.TK_MINUSEQ) {
|
||||
emitline("\tSUBQ\tAX, BX\n");
|
||||
emitline("\tMOVQ\tBX, AX\n");
|
||||
};
|
||||
cgdotfieldcombine(c, n.op, false);
|
||||
emitline("\tMOVQ\tAX, ");
|
||||
emitoff((lc.off + delta): i64);
|
||||
emitline("(BP)\n");
|
||||
@@ -33630,11 +33699,10 @@ fn cgassign(c: *cgen, n: *node) void = {
|
||||
emitline("\tPUSHQ\tBX\n");
|
||||
cgexpr(c, n.rhs);
|
||||
emitline("\tPOPQ\tBX\n");
|
||||
if (n.op == tkind.TK_PLUSEQ) { emitline("\tADDQ\tBX, AX\n"); };
|
||||
if (n.op == tkind.TK_MINUSEQ) {
|
||||
emitline("\tSUBQ\tAX, BX\n");
|
||||
emitline("\tMOVQ\tBX, AX\n");
|
||||
};
|
||||
cgdotfieldhardstop(c, fi.tnode);
|
||||
let uns34: bool = false;
|
||||
if (fi.tnode != nil) { if (fi.tnode.type_ != nil) { uns34 = typeisunsigned(fi.tnode.type_: *tinfo); }; };
|
||||
cgdotfieldcombine(c, n.op, uns34);
|
||||
let sop: str = fieldstoreop(c, fi);
|
||||
emitline("\tLEAQ\t");
|
||||
emitsymname(c, bn);
|
||||
|
||||
390
test/wcc/949_dotfield_compound_run.c
Normal file
390
test/wcc/949_dotfield_compound_run.c
Normal file
@@ -0,0 +1,390 @@
|
||||
/*
|
||||
* 949_dotfield_compound_run — runtime + byte-id net for #34 (#263 both-
|
||||
* stages): a compound assign on a single-dot field lvalue
|
||||
* (`s.f OP= v`, `p.f OP= v`, `g.f OP= v`, `sl.len OP= v`) must do a real
|
||||
* LOAD-OP-STORE for ALL ten integer ops, not silently demote any op
|
||||
* other than += / -= to a plain `s.f = rhs`.
|
||||
*
|
||||
* Pre-fix: every single-dot field-compound arm in BOTH stages wired only
|
||||
* PLUSEQ/MINUSEQ; *= /= %= &= |= ^= <<= >>= left the old value in BX and
|
||||
* the rhs in AX, then fell into the generic store of AX — `s.f *= 3`
|
||||
* silently compiled to `s.f = 3` (gate-blind, .s byte-identical). The
|
||||
* five sub-arms (via-ptr-base local, direct struct local, str/slice
|
||||
* pseudo-field, global struct field) all shared the BX=old/AX=rhs
|
||||
* convention; the fix routes every one through one shared combine helper
|
||||
* (cgdotfieldcombine / cg_dotfield_combine) wiring all 10 ops and
|
||||
* hard-errors float/str/slice/tagged fields LOUD (rule-7). cs/ww move
|
||||
* together (#263 carve-out); byte-id witnesses rule-10.
|
||||
*
|
||||
* Each row carries (a) a cstage `ww build` + run asserting the exit code
|
||||
* and (b) a w6c vs w6c_ww `.s` cmp (rule-10 byte-id). builderr rows
|
||||
* assert BOTH stages fail loud with the cited diagnostic substring.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
static int
|
||||
runwait(const char *cmd)
|
||||
{
|
||||
int rc = system(cmd);
|
||||
if (rc == -1) return -1;
|
||||
if (WIFEXITED(rc)) return WEXITSTATUS(rc);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* builderr + experr: when builderr != 0 the cstage build MUST FAIL and
|
||||
* stderr MUST contain experr. wwstage builderr is verified via direct
|
||||
* w6c_ww invocation on the same source. Mirrors 945_tuple_nary's pattern
|
||||
* for loud-stop verification (rule 7 — never silent). */
|
||||
struct row { const char *label; const char *src; int want_exit;
|
||||
int builderr; const char *experr; };
|
||||
|
||||
static const struct row rows[] = {
|
||||
/* += control: the base op was already wired — byte-id surface
|
||||
* unchanged. 10 + 5 = 15. */
|
||||
{ "dotfld_pluseq_ctrl",
|
||||
"package main;\n"
|
||||
"type S = struct { f: int, g: int };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let s: S = S{f=10, g=0};\n"
|
||||
" s.f += 5;\n"
|
||||
" return s.f: i32;\n"
|
||||
"};\n", 15, 0, NULL },
|
||||
/* #34 newly-wired: direct struct local *= : 5 * 3 = 15. */
|
||||
{ "dotfld_stareq",
|
||||
"package main;\n"
|
||||
"type S = struct { f: int, g: int };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let s: S = S{f=5, g=0};\n"
|
||||
" s.f *= 3;\n"
|
||||
" return s.f: i32;\n"
|
||||
"};\n", 15, 0, NULL },
|
||||
/* signed /= : 100 / 4 = 25 (CQO+IDIVQ). */
|
||||
{ "dotfld_slasheq_signed",
|
||||
"package main;\n"
|
||||
"type S = struct { f: int, g: int };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let s: S = S{f=100, g=0};\n"
|
||||
" s.f /= 4;\n"
|
||||
" return s.f: i32;\n"
|
||||
"};\n", 25, 0, NULL },
|
||||
/* signed /= with a NEGATIVE dividend distinguishes IDIVQ from DIVQ:
|
||||
* -17 / 4 = -4 (CQO+IDIVQ, truncate toward zero); the positive signed
|
||||
* /= above cannot tell IDIVQ from DIVQ. return (-4 + 50) = 46. */
|
||||
{ "dotfld_slasheq_neg",
|
||||
"package main;\n"
|
||||
"type S = struct { f: i32, g: i32 };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let s: S = S{f=-17, g=0};\n"
|
||||
" s.f /= 4;\n"
|
||||
" return s.f + 50;\n"
|
||||
"};\n", 46, 0, NULL },
|
||||
/* unsigned u32 field /= : 100u32 / 4u32 = 25 (DIVQ + zero-DX). */
|
||||
{ "dotfld_slasheq_unsigned",
|
||||
"package main;\n"
|
||||
"type S = struct { f: u32, g: u32 };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let s: S = S{f=100u32, g=0u32};\n"
|
||||
" s.f /= 4u32;\n"
|
||||
" return s.f: i32;\n"
|
||||
"};\n", 25, 0, NULL },
|
||||
/* signed %= : 17 % 5 = 2 (result rides DX, MOVQ DX,AX). */
|
||||
{ "dotfld_percenteq",
|
||||
"package main;\n"
|
||||
"type S = struct { f: int, g: int };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let s: S = S{f=17, g=0};\n"
|
||||
" s.f %= 5;\n"
|
||||
" return s.f: i32;\n"
|
||||
"};\n", 2, 0, NULL },
|
||||
/* <<= : 3 << 4 = 48 (count swapped into CX, value into AX, SHLQ). */
|
||||
{ "dotfld_lshifteq",
|
||||
"package main;\n"
|
||||
"type S = struct { f: int, g: int };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let s: S = S{f=3, g=0};\n"
|
||||
" s.f <<= 4;\n"
|
||||
" return s.f: i32;\n"
|
||||
"};\n", 48, 0, NULL },
|
||||
/* signed >>= on a NEGATIVE i32 field: -16 >> 2 = -4 ARITHMETIC
|
||||
* (SARQ). return (-4 + 50) = 46 distinguishes from a logical shift. */
|
||||
{ "dotfld_rshifteq_neg",
|
||||
"package main;\n"
|
||||
"type S = struct { f: i32, g: i32 };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let s: S = S{f=-16, g=0};\n"
|
||||
" s.f >>= 2;\n"
|
||||
" return s.f + 50;\n"
|
||||
"};\n", 46, 0, NULL },
|
||||
/* unsigned u32 field >>= : 200u32 >> 2 = 50 (SHRQ). */
|
||||
{ "dotfld_rshifteq_unsigned",
|
||||
"package main;\n"
|
||||
"type S = struct { f: u32, g: u32 };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let s: S = S{f=200u32, g=0u32};\n"
|
||||
" s.f >>= 2u32;\n"
|
||||
" return s.f: i32;\n"
|
||||
"};\n", 50, 0, NULL },
|
||||
/* via-ptr base (`p.f` where p is *S fn param): the arm derefs the
|
||||
* pointer before the field load/store. 4 * 3 = 12. */
|
||||
{ "dotfld_viaptr_stareq",
|
||||
"package main;\n"
|
||||
"type S = struct { f: int, g: int };\n"
|
||||
"fn bump(p: *S) void = { p.f *= 3; };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let a: S = S{f=4, g=0};\n"
|
||||
" bump(&a);\n"
|
||||
" return a.f: i32;\n"
|
||||
"};\n", 12, 0, NULL },
|
||||
/* global struct field: gs.f /= 4 → 5 (LEAQ gs(SB) base). */
|
||||
{ "dotfld_global_slasheq",
|
||||
"package main;\n"
|
||||
"type S = struct { f: int, g: int };\n"
|
||||
"let gs: S = S{f=20, g=0};\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" gs.f /= 4;\n"
|
||||
" return gs.f: i32;\n"
|
||||
"};\n", 5, 0, NULL },
|
||||
/* str/slice pseudo-field compound (`sl.len -= 3`): the pseudo-field
|
||||
* arm (delta = 8 for .len) must combine, not store the rhs. 8-3 = 5. */
|
||||
{ "dotfld_pseudo_len_minuseq",
|
||||
"package main;\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let buf: [8]u8 = [1u8,2u8,3u8,4u8,5u8,6u8,7u8,8u8];\n"
|
||||
" let sl: []u8 = buf[0:8];\n"
|
||||
" sl.len -= 3;\n"
|
||||
" return sl.len: i32;\n"
|
||||
"};\n", 5, 0, NULL },
|
||||
/* pseudo-field *= (the silently-demoted op): 4 * 3 = 12. */
|
||||
{ "dotfld_pseudo_len_stareq",
|
||||
"package main;\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let buf: [8]u8 = [1u8,2u8,3u8,4u8,5u8,6u8,7u8,8u8];\n"
|
||||
" let sl: []u8 = buf[0:4];\n"
|
||||
" sl.len *= 3;\n"
|
||||
" return sl.len: i32;\n"
|
||||
"};\n", 12, 0, NULL },
|
||||
/* Plain `s.f = v` control: ASSIGN path byte-id unchanged. */
|
||||
{ "dotfld_plain_assign_ctrl",
|
||||
"package main;\n"
|
||||
"type S = struct { f: int, g: int };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let s: S = S{f=1, g=0};\n"
|
||||
" s.f = 99;\n"
|
||||
" return s.f: i32;\n"
|
||||
"};\n", 99, 0, NULL },
|
||||
/* HARD-ERROR rows (#34/rule-7): float/str/slice/tagged FIELD compound
|
||||
* builds MUST FAIL LOUD on both stages with the cited diagnostic. */
|
||||
{ "dotfld_he_float",
|
||||
"package main;\n"
|
||||
"type S = struct { f: f64, g: int };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let s: S = S{f=1.0, g=0};\n"
|
||||
" s.f *= 2.0;\n"
|
||||
" return 0;\n"
|
||||
"};\n", 0, 1, "single-dot field compound on float field" },
|
||||
{ "dotfld_he_str",
|
||||
"package main;\n"
|
||||
"type S = struct { f: str, g: int };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let s: S = S{f=\"a\", g=0};\n"
|
||||
" s.f += \"x\";\n"
|
||||
" return 0;\n"
|
||||
"};\n", 0, 1, "single-dot field compound on str field" },
|
||||
{ "dotfld_he_slice",
|
||||
"package main;\n"
|
||||
"type S = struct { f: []u8, g: int };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let b: [2]u8 = [1u8, 2u8];\n"
|
||||
" let s: S = S{f=b[0:2], g=0};\n"
|
||||
" s.f += b[0:1];\n"
|
||||
" return 0;\n"
|
||||
"};\n", 0, 1, "single-dot field compound on slice field" },
|
||||
{ "dotfld_he_tagged",
|
||||
"package main;\n"
|
||||
"type S = struct { f: (i32|str), g: int };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let s: S = S{f=1, g=0};\n"
|
||||
" s.f += 3;\n"
|
||||
" return 0;\n"
|
||||
"};\n", 0, 1, "single-dot field compound on tagged field" },
|
||||
{ NULL, NULL, 0, 0, NULL }
|
||||
};
|
||||
|
||||
static int
|
||||
slurp_eq(const char *a, const char *b)
|
||||
{
|
||||
FILE *fa = fopen(a, "rb");
|
||||
FILE *fb = fopen(b, "rb");
|
||||
if (!fa || !fb) { if (fa) fclose(fa); if (fb) fclose(fb); return -1; }
|
||||
int rc = 0;
|
||||
for (;;) {
|
||||
int ca = fgetc(fa);
|
||||
int cb = fgetc(fb);
|
||||
if (ca != cb) { rc = -1; break; }
|
||||
if (ca == EOF) break;
|
||||
}
|
||||
fclose(fa); fclose(fb);
|
||||
return rc;
|
||||
}
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
const char *bin = getenv("BIN");
|
||||
if (!bin) bin = "out/bin";
|
||||
char absbin[1024];
|
||||
if (bin[0] != '/') {
|
||||
char cwd[1024];
|
||||
if (getcwd(cwd, sizeof cwd) == NULL) return 1;
|
||||
snprintf(absbin, sizeof absbin, "%s/%s", cwd, bin);
|
||||
bin = absbin;
|
||||
}
|
||||
|
||||
char w6c[1100], w6c_ww[1100];
|
||||
snprintf(w6c, sizeof w6c, "%s/w6c", bin);
|
||||
snprintf(w6c_ww, sizeof w6c_ww, "%s/w6c_ww", bin);
|
||||
if (access(w6c_ww, X_OK) != 0) {
|
||||
fprintf(stderr, "idxcompound: w6c_ww missing — cannot run the "
|
||||
"cs==ww byte-id gate (the whole point of this test)\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
int n = 0, fail = 0;
|
||||
for (int i = 0; rows[i].src; i++, n++) {
|
||||
char src[64];
|
||||
snprintf(src, sizeof src, "/tmp/wwidx_%d_%d.ww", getpid(), i);
|
||||
FILE *f = fopen(src, "wb");
|
||||
if (f == NULL) { fail++; continue; }
|
||||
fputs(rows[i].src, f);
|
||||
fclose(f);
|
||||
|
||||
/* (a) cstage build + run, OR build-must-fail (builderr rows).
|
||||
* For builderr: stderr captured to a temp file; pass iff
|
||||
* exit-nonzero AND stderr contains experr substring. */
|
||||
char tmpdir[64];
|
||||
snprintf(tmpdir, sizeof tmpdir, "/tmp/wwidx_%d_d_%d",
|
||||
getpid(), i);
|
||||
mkdir(tmpdir, 0755);
|
||||
|
||||
char cmd[2048];
|
||||
if (rows[i].builderr) {
|
||||
char errf[96];
|
||||
snprintf(errf, sizeof errf, "/tmp/wwidx_%d_e_%d",
|
||||
getpid(), i);
|
||||
snprintf(cmd, sizeof cmd,
|
||||
"cd %s && %s/ww build %s >/dev/null 2>%s",
|
||||
tmpdir, bin, src, errf);
|
||||
int brc = runwait(cmd);
|
||||
FILE *ef = fopen(errf, "rb");
|
||||
char ebuf[4096];
|
||||
size_t en = 0;
|
||||
if (ef) { en = fread(ebuf, 1, sizeof ebuf - 1, ef);
|
||||
fclose(ef); }
|
||||
ebuf[en] = '\0';
|
||||
int ok = (brc != 0)
|
||||
&& (rows[i].experr == NULL
|
||||
|| strstr(ebuf, rows[i].experr) != NULL);
|
||||
if (!ok) {
|
||||
fprintf(stderr, "row[%s]: cstage expected "
|
||||
"builderr+'%s' (brc=%d, stderr='%s')\n",
|
||||
rows[i].label,
|
||||
rows[i].experr ? rows[i].experr : "(any)",
|
||||
brc, ebuf);
|
||||
fail++;
|
||||
}
|
||||
|
||||
/* Also verify wwstage hard-errors with the SAME message
|
||||
* (rule-10 — both stages identical diagnostic). Invoke
|
||||
* w6c_ww directly on the .ww source. */
|
||||
snprintf(cmd, sizeof cmd,
|
||||
"%s -o /dev/null %s >/dev/null 2>%s",
|
||||
w6c_ww, src, errf);
|
||||
int wrc = runwait(cmd);
|
||||
ef = fopen(errf, "rb"); en = 0;
|
||||
if (ef) { en = fread(ebuf, 1, sizeof ebuf - 1, ef);
|
||||
fclose(ef); }
|
||||
ebuf[en] = '\0';
|
||||
int wok = (wrc != 0)
|
||||
&& (rows[i].experr == NULL
|
||||
|| strstr(ebuf, rows[i].experr) != NULL);
|
||||
if (!wok) {
|
||||
fprintf(stderr, "row[%s]: wwstage expected "
|
||||
"builderr+'%s' (wrc=%d, stderr='%s')\n",
|
||||
rows[i].label,
|
||||
rows[i].experr ? rows[i].experr : "(any)",
|
||||
wrc, ebuf);
|
||||
fail++;
|
||||
}
|
||||
unlink(errf);
|
||||
unlink(src); rmdir(tmpdir);
|
||||
continue;
|
||||
}
|
||||
|
||||
snprintf(cmd, sizeof cmd, "cd %s && %s/ww build %s",
|
||||
tmpdir, bin, src);
|
||||
if (runwait(cmd) != 0) {
|
||||
fprintf(stderr, "row[%s]: cstage build failed\n",
|
||||
rows[i].label);
|
||||
fail++;
|
||||
unlink(src); rmdir(tmpdir);
|
||||
continue;
|
||||
}
|
||||
|
||||
char outbin[128];
|
||||
const char *base = strrchr(src, '/');
|
||||
base = base ? base + 1 : src;
|
||||
snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base);
|
||||
char *dot = strrchr(outbin, '.');
|
||||
if (dot && strcmp(dot, ".ww") == 0) *dot = '\0';
|
||||
|
||||
int got = runwait(outbin);
|
||||
if (got != rows[i].want_exit) {
|
||||
fprintf(stderr, "row[%s]: cstage exit %d, want %d\n",
|
||||
rows[i].label, got, rows[i].want_exit);
|
||||
fail++;
|
||||
}
|
||||
unlink(outbin); rmdir(tmpdir);
|
||||
|
||||
/* (b) cs==ww byte-id gate. */
|
||||
char cs_s[64], ws_s[64];
|
||||
snprintf(cs_s, sizeof cs_s, "/tmp/wwidx_%d_%d_cs.s",
|
||||
getpid(), i);
|
||||
snprintf(ws_s, sizeof ws_s, "/tmp/wwidx_%d_%d_ww.s",
|
||||
getpid(), i);
|
||||
|
||||
snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null",
|
||||
w6c, cs_s, src);
|
||||
if (runwait(cmd) != 0) {
|
||||
fprintf(stderr, "row[%s]: w6c failed\n", rows[i].label);
|
||||
fail++; unlink(src); continue;
|
||||
}
|
||||
snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null",
|
||||
w6c_ww, ws_s, src);
|
||||
if (runwait(cmd) != 0) {
|
||||
fprintf(stderr, "row[%s]: w6c_ww failed\n",
|
||||
rows[i].label);
|
||||
fail++; unlink(src); unlink(cs_s); continue;
|
||||
}
|
||||
if (slurp_eq(cs_s, ws_s) != 0) {
|
||||
fprintf(stderr,
|
||||
"row[%s]: cstage/wwstage .s DIFFER (rule-10 "
|
||||
"byte-id violation)\n", rows[i].label);
|
||||
fail++;
|
||||
}
|
||||
unlink(src); unlink(cs_s); unlink(ws_s);
|
||||
}
|
||||
|
||||
if (fail) {
|
||||
fprintf(stderr, "%d/%d dotfield-compound tests failed\n",
|
||||
fail, n);
|
||||
return 1;
|
||||
}
|
||||
printf("dotfield-compound: %d/%d ok (cstage run + cs==ww byte-id)\n",
|
||||
n, n);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user