From 1140a590bfff50414a5ef2f4e836e691d5dfeaaf Mon Sep 17 00:00:00 2001 From: Hojun-Cho Date: Sun, 24 May 2026 06:40:59 +0900 Subject: [PATCH] wcc: str -> 24B {ptr,len,cap}, 3-reg ABI -- parity with []u8 (both stages) A ww `str` becomes a 24-byte {ptr,len,cap} value, identical in layout to []u8 -- the enabling prerequisite for the Phase 2 `str == []u8` collapse. Both stages, atomically: - ty_str 16->24B; str value flows 3-reg AX/BX/CX (was 2-reg); str literals emit cap (=len). - str in a tagged union grows to a 32B slot, using the AX/DX/CX/R8 4th-word path already used by 32B slice-variant unions -- str-variant is now structurally identical. - tuple (scalar,str) return: 4-reg AX/DX/CX/R8 + 32B receive, extending the existing type-keyed return (no sret). - str == []u8 for index and .ptr/.len/.cap, kind-gated where size-based dispatch collided at 24B; cstage and wwstage mirror exactly. - table-driven runtime coverage: test/wcc/928_str_abi_run.c. Cannot be split (rule 10/11): a 24B str and a 16B str cannot coexist across the two compiler stages without breaking byte-identity, so the size change and every dependent ABI/codegen site land in one atomic commit, both stages. Known follow-ups (zero corpus impact, tracked): str-literal global .cap static-init; >16B struct by-value (pre-existing); tagged-union match-scrutinee stage divergence (pre-existing). --- Makefile | 7 + cmd/w6c/cgen.c | 288 ++++++++++++------ cmd/wcc/type.c | 6 +- lib/ww/typ.ww | 15 +- selfhost/cmd/w6c/main.combined.ww | 423 ++++++++++++++++++--------- selfhost/cmd/wcc/cgendecl.ww | 25 +- selfhost/cmd/wcc/cgenexpr.ww | 237 +++++++++------ selfhost/cmd/wcc/cgenstmt.ww | 71 +++-- selfhost/cmd/wcc/cgenutil.ww | 71 ++++- selfhost/cmd/wcc/check.ww | 4 +- selfhost/cmd/wwdump/main.combined.ww | 423 ++++++++++++++++++--------- selfhost/test/smoke.combined.ww | 2 +- selfhost/test/smoke.ww | 2 +- test/wcc/741_dotbase_chained.c | 13 +- test/wcc/758_cgalloc_str_field.c | 8 +- test/wcc/928_str_abi_run.c | 236 +++++++++++++++ 16 files changed, 1337 insertions(+), 494 deletions(-) create mode 100644 test/wcc/928_str_abi_run.c diff --git a/Makefile b/Makefile index b3117918..05435038 100644 --- a/Makefile +++ b/Makefile @@ -253,6 +253,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \ $(BIN)/test_sret_narrow_field_run \ $(BIN)/test_match_slice_variant \ $(BIN)/test_match_slice_variant_run \ + $(BIN)/test_str_abi_run \ $(BIN)/test_composite_call_arg \ $(BIN)/test_composite_call_arg_run \ $(BIN)/test_letdecl_zeroinit \ @@ -613,6 +614,12 @@ $(BIN)/test_match_slice_variant_run: test/wcc/926_match_slice_variant_run.c \ $(LIB)/libwwrt.a | $(BIN) $(CC) $(CFLAGS) -o $@ $< +$(BIN)/test_str_abi_run: test/wcc/928_str_abi_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_composite_call_arg: test/wcc/723_composite_call_arg.c \ $(BIN)/w6c $(BIN)/w6c_ww | $(BIN) $(CC) $(CFLAGS) -o $@ $< diff --git a/cmd/w6c/cgen.c b/cmd/w6c/cgen.c index f9af7d23..420c41a0 100644 --- a/cmd/w6c/cgen.c +++ b/cmd/w6c/cgen.c @@ -1399,10 +1399,14 @@ cg_widen_tagged_store(Cg *c, Local **locals_p, Type *dst, Node *src, Type *fu = (ftype && ftype->kind == TY_NAMED) ? ftype->under : ftype; if (fu && fu->kind == TY_STR) { + /* str IS []u8: 3-word field (ptr,len,cap) + * from cgexpr's AX/BX/CX (#1/Phase 3). */ ins2(c, A_MOVQ, areg(D_AX), amem(D_BP, write_off + 8 + (int)foff + 0)); ins2(c, A_MOVQ, areg(D_BX), amem(D_BP, write_off + 8 + (int)foff + 8)); + ins2(c, A_MOVQ, areg(D_CX), + amem(D_BP, write_off + 8 + (int)foff + 16)); continue; } int op = A_MOVQ; @@ -1417,11 +1421,14 @@ cg_widen_tagged_store(Cg *c, Local **locals_p, Type *dst, Node *src, if (via_outer) goto copy_out; return; } - /* str payload: AX=ptr, BX=len from cgexpr. */ + /* str IS []u8: AX=ptr, BX=len, CX=cap from cgexpr. Slot layout + * tag@+0, ptr@+8, len@+16, cap@+24 — same 32B shape as the slice + * payload below (#1/Phase 3). */ if (type_isstr(st) || (su && su->kind == TY_STR)) { cgexpr(c, src, *locals_p); ins2(c, A_MOVQ, areg(D_AX), amem(D_BP, write_off + 8)); ins2(c, A_MOVQ, areg(D_BX), amem(D_BP, write_off + 16)); + ins2(c, A_MOVQ, areg(D_CX), amem(D_BP, write_off + 24)); int tag = cg_tag_for_variant(du, st); ins2(c, A_MOVQ, aimm(tag < 0 ? 0 : tag), amem(D_BP, write_off + 0)); @@ -1511,8 +1518,11 @@ cg_widen_tagged_push(Cg *c, Local **locals_p, Type *dst, Node *src, int sz) int tag = cg_tag_for_variant(du, st); if (tag < 0) tag = 0; if (type_isstr(st) || (su && su->kind == TY_STR)) { - /* slot 24: [+0]=tag, [+8]=ptr, [+16]=len. Push len, - * ptr, tag (high→low so pop drains tag first). */ + /* str IS []u8: slot 32 [+0]=tag, [+8]=ptr, [+16]=len, + * [+24]=cap — same shape as the slice arm below. Push + * cap, len, ptr, tag (high→low so pop drains tag first) + * (#1/Phase 3). */ + ins1(c, A_PUSHQ, areg(D_CX)); /* cap */ ins1(c, A_PUSHQ, areg(D_BX)); /* len */ ins1(c, A_PUSHQ, areg(D_AX)); /* ptr */ ins2(c, A_MOVQ, aimm(tag), areg(D_AX)); @@ -1731,6 +1741,35 @@ cg_structlit_fill(Cg *c, Local **locals_p, Type *lu, Node *lit, } continue; } + /* str IS []u8: 3-word field (ptr,len,cap). cgexpr leaves + * AX/BX/CX; for non-BP modes the dst base goes in DX to dodge + * BX=len / CX=cap (the generic store below reloads BX, which + * would clobber len) (#1/Phase 3). */ + if (fu && fu->kind == TY_STR) { + cgexpr(c, f->lhs, *locals_p); + if (mode == DST_BP) { + ins2(c, A_MOVQ, areg(D_AX), + amem(D_BP, disp + (int)foff + 0)); + ins2(c, A_MOVQ, areg(D_BX), + amem(D_BP, disp + (int)foff + 8)); + ins2(c, A_MOVQ, areg(D_CX), + amem(D_BP, disp + (int)foff + 16)); + } else { + if (mode == DST_PTR_LOCAL) + ins2(c, A_MOVQ, amem(D_BP, srcoff), + areg(D_DX)); + else + ins2(c, A_LEAQ, masym(c, name), + areg(D_DX)); + ins2(c, A_MOVQ, areg(D_AX), + amem(D_DX, disp + (int)foff + 0)); + ins2(c, A_MOVQ, areg(D_BX), + amem(D_DX, disp + (int)foff + 8)); + ins2(c, A_MOVQ, areg(D_CX), + amem(D_DX, disp + (int)foff + 16)); + } + continue; + } cgexpr(c, f->lhs, *locals_p); /* For non-BP modes, cgexpr just clobbered BX; reload it * before the store. */ @@ -1784,11 +1823,13 @@ cgexpr(Cg *c, Node *n, Local *locals) break; } case N_STRLIT: { - /* result lives as the (ptr, len) pair: ptr in AX, len in BX. - * Call sites that pass a str arg pick these up directly. */ + /* str IS []u8: the (ptr, len, cap) triple — ptr in AX, len in + * BX, cap in CX. A static literal has no spare storage, so + * cap = len (#1/Phase 3, task (b)). */ const char *lab = intern_strlit(c, n->str, n->strlen); ins2(c, A_LEAQ, asym(lab), areg(D_AX)); ins2(c, A_MOVQ, aimm((long long)n->strlen), areg(D_BX)); + ins2(c, A_MOVQ, aimm((long long)n->strlen), areg(D_CX)); break; } case N_TRUE: cgexpr_int(c, 1); break; @@ -1802,11 +1843,11 @@ cgexpr(Cg *c, Node *n, Local *locals) int op = op_for(n, A_MOVSD, A_MOVSS); ins2(c, op, amem(D_BP, off), areg(D_X0)); } else if (node_isstr(n)) { - /* str values flow as (AX=ptr, BX=len) so they - * can be returned in AX:DX or pushed to the - * call-arg stack uniformly. */ - ins2(c, A_MOVQ, amem(D_BP, off), areg(D_AX)); + /* str IS []u8: flow as (AX=ptr, BX=len, CX=cap), + * mirroring the slice local load below (#1/Phase 3). */ + ins2(c, A_MOVQ, amem(D_BP, off + 0), areg(D_AX)); ins2(c, A_MOVQ, amem(D_BP, off + 8), areg(D_BX)); + ins2(c, A_MOVQ, amem(D_BP, off + 16), areg(D_CX)); } else if (node_isslice(n)) { /* slice values flow as (AX=ptr, BX=len, CX=cap) * — mirror the global-slice load so a slice @@ -1864,23 +1905,26 @@ cgexpr(Cg *c, Node *n, Local *locals) ins2(c, A_MOVQ, aimm((long long)s->len), areg(D_BX)); + /* str IS []u8: cap = len for a static + * def literal (#1/Phase 3). */ + ins2(c, A_MOVQ, + aimm((long long)s->len), + areg(D_CX)); goto ident_done; } } if (let_islet(n->str) && (let_isstr(n->type) || let_isslice(n->type))) { - /* Top-level str/slice global: load each half + /* Top-level str/slice global: load each word * via its address (the asm has no `name+8(SB)` - * operand form). Slice has a third 8B (cap) - * — the address holder CX gets overwritten by - * the cap as the last step, after we no longer - * need it. */ - int is_slice = let_isslice(n->type); + * operand form). str IS []u8 now — both carry a + * third 8B (cap); the address holder CX gets + * overwritten by the cap as the last step, after + * we no longer need it (#1/Phase 3). */ ins2(c, A_LEAQ, masym(c, n->str), areg(D_CX)); ins2(c, A_MOVQ, amem(D_CX, 0), areg(D_AX)); ins2(c, A_MOVQ, amem(D_CX, 8), areg(D_BX)); - if (is_slice) - ins2(c, A_MOVQ, amem(D_CX, 16), areg(D_CX)); + ins2(c, A_MOVQ, amem(D_CX, 16), areg(D_CX)); goto ident_done; } if (let_islet(n->str) && let_isfloat(n->type)) { @@ -2539,27 +2583,31 @@ cgexpr(Cg *c, Node *n, Local *locals) int is_global = (boff == 0 && !via_ptr && let_islet(base->str)); int foff = (int)f->offset; - /* str-typed field: rhs cgexpr leaves (AX=ptr, BX=len); - * store both halves at field+0 and field+8. The 8/16 - * trailing-padding bytes are left untouched, which - * matches the let-init shape elsewhere in cgen. Only - * plain `=` is wired; compound on a str field is not - * meaningful. */ + /* str IS []u8: rhs cgexpr leaves (AX=ptr, BX=len, + * CX=cap); store all three at field+0/+8/+16, + * mirroring the slice-field arm below. Address + * scratch must dodge CX (holds cap), so via_ptr/ + * is_global stage the struct base in DX (#1/Phase 3). + * Only plain `=` is wired; compound on a str field is + * not meaningful. */ Type *str_fu = (f->type && f->type->kind == TY_NAMED) ? f->type->under : f->type; if (n->op == TK_ASSIGN && str_fu && str_fu->kind == TY_STR) { cgexpr(c, n->rhs, locals); if (via_ptr) { - ins2(c, A_MOVQ, amem(D_BP, boff), areg(D_CX)); - ins2(c, A_MOVQ, areg(D_AX), amem(D_CX, foff + 0)); - ins2(c, A_MOVQ, areg(D_BX), amem(D_CX, foff + 8)); + ins2(c, A_MOVQ, amem(D_BP, boff), areg(D_DX)); + ins2(c, A_MOVQ, areg(D_AX), amem(D_DX, foff + 0)); + ins2(c, A_MOVQ, areg(D_BX), amem(D_DX, foff + 8)); + ins2(c, A_MOVQ, areg(D_CX), amem(D_DX, foff + 16)); } else if (is_global) { - ins2(c, A_LEAQ, masym(c, base->str), areg(D_CX)); - ins2(c, A_MOVQ, areg(D_AX), amem(D_CX, foff + 0)); - ins2(c, A_MOVQ, areg(D_BX), amem(D_CX, foff + 8)); + ins2(c, A_LEAQ, masym(c, base->str), areg(D_DX)); + ins2(c, A_MOVQ, areg(D_AX), amem(D_DX, foff + 0)); + ins2(c, A_MOVQ, areg(D_BX), amem(D_DX, foff + 8)); + ins2(c, A_MOVQ, areg(D_CX), amem(D_DX, foff + 16)); } else { ins2(c, A_MOVQ, areg(D_AX), amem(D_BP, boff + foff + 0)); ins2(c, A_MOVQ, areg(D_BX), amem(D_BP, boff + foff + 8)); + ins2(c, A_MOVQ, areg(D_CX), amem(D_BP, boff + foff + 16)); } break; } @@ -3259,25 +3307,34 @@ cgexpr(Cg *c, Node *n, Local *locals) ? leaf_type->size : 8); int store_op = fldstoreop(leaf_type, fsz); if (fu && fu->kind == TY_STR) { + /* str IS []u8: store ptr/len/cap. cgexpr + * leaves CX=cap, so the via_cx base goes in + * DX (not CX) to avoid clobbering it — same + * as the single-dot str field store + * (#1/Phase 3). */ cgexpr(c, n->rhs, locals); if (via_cx) { if (ptr_root) ins2(c, A_MOVQ, amem(D_BP, base_disp), - areg(D_CX)); + areg(D_DX)); else ins2(c, A_LEAQ, masym(c, cur->str), - areg(D_CX)); + areg(D_DX)); ins2(c, A_MOVQ, areg(D_AX), - amem(D_CX, total_off + 0)); + amem(D_DX, total_off + 0)); ins2(c, A_MOVQ, areg(D_BX), - amem(D_CX, total_off + 8)); + amem(D_DX, total_off + 8)); + ins2(c, A_MOVQ, areg(D_CX), + amem(D_DX, total_off + 16)); } else { ins2(c, A_MOVQ, areg(D_AX), amem(D_BP, base_disp + total_off + 0)); ins2(c, A_MOVQ, areg(D_BX), amem(D_BP, base_disp + total_off + 8)); + ins2(c, A_MOVQ, areg(D_CX), + amem(D_BP, base_disp + total_off + 16)); } break; } @@ -3583,10 +3640,13 @@ cgexpr(Cg *c, Node *n, Local *locals) break; } if (is_arr || is_sl || is_ptr) { - cgexpr(c, n->rhs, locals); /* AX (and BX if str) */ - /* str element: also stash len so we can store both */ - if (elem_is_str) - ins1(c, A_PUSHQ, areg(D_BX)); + cgexpr(c, n->rhs, locals); /* AX=ptr (BX=len,CX=cap if str) */ + /* str IS []u8: stash cap+len so all three store + * (#1/Phase 3). */ + if (elem_is_str) { + ins1(c, A_PUSHQ, areg(D_CX)); /* cap */ + ins1(c, A_PUSHQ, areg(D_BX)); /* len */ + } ins1(c, A_PUSHQ, areg(D_AX)); cgexpr(c, n->lhs->rhs, locals); /* idx → AX */ if (esz > 1) { @@ -3626,9 +3686,12 @@ cgexpr(Cg *c, Node *n, Local *locals) ins2(c, A_ADDQ, areg(D_AX), areg(D_BX)); ins1(c, A_POPQ, areg(D_AX)); /* value (ptr if str) */ if (elem_is_str) { + /* str IS []u8: store ptr/len/cap (#1/Phase 3). */ ins2(c, A_MOVQ, areg(D_AX), amem(D_BX, 0)); ins1(c, A_POPQ, areg(D_CX)); ins2(c, A_MOVQ, areg(D_CX), amem(D_BX, 8)); + ins1(c, A_POPQ, areg(D_CX)); + ins2(c, A_MOVQ, areg(D_CX), amem(D_BX, 16)); break; } int store_op = fldstoreop(esub, esz); @@ -3678,12 +3741,19 @@ cgexpr(Cg *c, Node *n, Local *locals) ins2(c, mov, areg(D_X0), amem(D_BX, 0)); break; } - cgexpr(c, n->rhs, locals); /* AX = value (BX too if str) */ + cgexpr(c, n->rhs, locals); /* AX=ptr (BX=len, CX=cap if str) */ ins1(c, A_PUSHQ, areg(D_AX)); - if (vt && vt->kind == TY_STR) ins1(c, A_PUSHQ, areg(D_BX)); + if (vt && vt->kind == TY_STR) { + /* str IS []u8: also stash len + cap across the + * pointer eval, which clobbers BX/CX (#1/Phase 3). */ + ins1(c, A_PUSHQ, areg(D_BX)); /* len */ + ins1(c, A_PUSHQ, areg(D_CX)); /* cap */ + } cgexpr(c, n->lhs->lhs, locals); /* AX = pointer */ ins2(c, A_MOVQ, areg(D_AX), areg(D_BX)); if (vt && vt->kind == TY_STR) { + ins1(c, A_POPQ, areg(D_CX)); /* cap */ + ins2(c, A_MOVQ, areg(D_CX), amem(D_BX, 16)); ins1(c, A_POPQ, areg(D_CX)); /* len */ ins1(c, A_POPQ, areg(D_AX)); /* ptr */ ins2(c, A_MOVQ, areg(D_AX), amem(D_BX, 0)); @@ -3752,11 +3822,11 @@ cgexpr(Cg *c, Node *n, Local *locals) break; } } - /* Plain `name = strexpr;` for a str-typed local. cgexpr leaves - * (AX=ptr, BX=len); store both halves at off+0 and off+8. - * Mirrors the let-init shape so reassignment doesn't truncate. - * Top-level str globals follow the same shape but go through - * &name(SB) since the asm has no `name+8(SB)` operand form. */ + /* Plain `name = strexpr;` for a str-typed local. str IS []u8: + * cgexpr leaves (AX=ptr, BX=len, CX=cap); store all three at + * off+0/+8/+16, identical to the slice arm below. Top-level + * str globals go through &name(SB) → DI scratch (CX holds cap) + * since the asm has no `name+8(SB)` operand form (#1/Phase 3). */ if (n->lhs && n->lhs->kind == N_IDENT && n->op == TK_ASSIGN && n->lhs->type) { Type *lt = n->lhs->type; @@ -3767,14 +3837,17 @@ cgexpr(Cg *c, Node *n, Local *locals) cgexpr(c, n->rhs, locals); ins2(c, A_MOVQ, areg(D_AX), amem(D_BP, off + 0)); ins2(c, A_MOVQ, areg(D_BX), amem(D_BP, off + 8)); + ins2(c, A_MOVQ, areg(D_CX), amem(D_BP, off + 16)); break; } if (let_islet(n->lhs->str)) { cgexpr(c, n->rhs, locals); + ins2(c, A_MOVQ, areg(D_CX), areg(D_DI)); ins2(c, A_LEAQ, masym(c, n->lhs->str), areg(D_CX)); ins2(c, A_MOVQ, areg(D_AX), amem(D_CX, 0)); ins2(c, A_MOVQ, areg(D_BX), amem(D_CX, 8)); + ins2(c, A_MOVQ, areg(D_DI), amem(D_CX, 16)); break; } break; @@ -4176,17 +4249,20 @@ cgexpr(Cg *c, Node *n, Local *locals) 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. */ + /* str IS []u8: cgexpr leaves (AX=ptr, BX=len, + * CX=cap). Route the heap base through DX so all + * three survive — CX now holds cap, BX holds len + * (#1/Phase 3). */ 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, amem(D_SP, 0), areg(D_DX)); ins2(c, A_MOVQ, areg(D_AX), - amem(D_CX, (int)foff + 0)); + amem(D_DX, (int)foff + 0)); ins2(c, A_MOVQ, areg(D_BX), - amem(D_CX, (int)foff + 8)); + amem(D_DX, (int)foff + 8)); + ins2(c, A_MOVQ, areg(D_CX), + amem(D_DX, (int)foff + 16)); continue; } ins2(c, A_MOVQ, amem(D_SP, 0), areg(D_BX)); @@ -4606,6 +4682,10 @@ cgexpr(Cg *c, Node *n, Local *locals) ins2(c, A_SUBQ, aimm(8), areg(D_SP)); ins2(c, A_MOVSD, areg(D_X0), amem(D_SP, 0)); } else if (node_isstr(args[i])) { + /* str IS []u8: cgexpr left (AX=ptr, BX=len, + * CX=cap). Push the triple, same as slice + * (#1/Phase 3). */ + ins1(c, A_PUSHQ, areg(D_CX)); /* cap */ ins1(c, A_PUSHQ, areg(D_BX)); /* len */ ins1(c, A_PUSHQ, areg(D_AX)); /* ptr — top */ } else if (node_isslice(args[i])) { @@ -4711,7 +4791,8 @@ cgexpr(Cg *c, Node *n, Local *locals) stackslots++; /* leave on stack */ } } else if (node_isstr(args[i])) { - for (int k = 0; k < 2; k++) { + /* str IS []u8: 3-word arg, same as slice (#1/Phase 3). */ + for (int k = 0; k < 3; k++) { if (ii < 6) ins1(c, A_POPQ, areg(sysv_argregs[ii++])); else @@ -4824,9 +4905,8 @@ cgexpr(Cg *c, Node *n, Local *locals) /* SysV: caller cleans stack args. */ if (stackslots > 0) ins2(c, A_ADDQ, aimm(stackslots * 8), areg(D_SP)); - /* If callee returns a str (16B → AX:DX per SysV), shuffle - * len from DX into BX so str values stay in (AX, BX). */ - if (node_isstr(n)) ins2(c, A_MOVQ, areg(D_DX), areg(D_BX)); + /* str IS []u8: callee returns AX=ptr, BX=len, CX=cap — + * same as a slice, no receive-side shuffle (#1/Phase 3). */ break; } case N_MATCH: { @@ -5089,8 +5169,13 @@ cgexpr(Cg *c, Node *n, Local *locals) ins1(c, A_POPQ, areg(D_BP)); ins0(c, A_RET); label(c, cont); - if (success_is_str) + if (success_is_str) { + /* str IS []u8: success value arrives in the tagged + * ABI as DX=ptr, CX=len, R8=cap (slot 32B). Move len + * out before cap overwrites CX (#1/Phase 3). */ ins2(c, A_MOVQ, areg(D_CX), areg(D_BX)); + ins2(c, A_MOVQ, areg(D_R8), areg(D_CX)); + } ins2(c, A_MOVQ, areg(D_DX), areg(D_AX)); break; } @@ -5126,8 +5211,13 @@ cgexpr(Cg *c, Node *n, Local *locals) ins2(c, A_MOVQ, aimm(60), areg(D_AX)); ins0(c, A_SYSCALL); label(c, cont); - if (success_is_str) + if (success_is_str) { + /* str IS []u8: success arrives DX=ptr, CX=len, R8=cap + * (slot 32B). Move len out before cap clobbers CX + * (#1/Phase 3). */ ins2(c, A_MOVQ, areg(D_CX), areg(D_BX)); + ins2(c, A_MOVQ, areg(D_R8), areg(D_CX)); + } ins2(c, A_MOVQ, areg(D_DX), areg(D_AX)); break; } @@ -6437,13 +6527,15 @@ cgstmt(Cg *c, Node *n, Local **locals, int *frame) break; } } - /* str initialiser: cgexpr produces (AX=ptr, BX=len). - * #43: gate width via ty_str->size so a future str-layout - * bump (#1) propagates without touching this site. */ + /* str IS []u8: cgexpr produces (AX=ptr, BX=len, CX=cap); + * store all three, same as the slice initialiser below. + * #43 gate via ty_str->size already tracks the 24B bump + * (#1/Phase 3). */ if (n->rhs && type_isstr(lt) && sz == (int)ty_str->size) { cgexpr(c, n->rhs, *locals); ins2(c, A_MOVQ, areg(D_AX), amem(D_BP, off + 0)); ins2(c, A_MOVQ, areg(D_BX), amem(D_BP, off + 8)); + ins2(c, A_MOVQ, areg(D_CX), amem(D_BP, off + 16)); break; } /* 2-tuple initialiser from a function call: SysV returns @@ -6455,13 +6547,14 @@ cgstmt(Cg *c, Node *n, Local **locals, int *frame) ins2(c, A_MOVQ, areg(D_DX), amem(D_BP, off + 8)); break; } - /* 24B tuple initialiser for `(scalar, str)` / `(str, scalar)`. - * Per the AX:DX:CX return convention: AX = scalar elem, - * DX = str.ptr, CX = str.len. The slot is laid out positionally - * (e0 at +0, e1 at +8 for scalars; str takes 16B starting at - * its position), so we route each register to the slot dictated - * by the element's type, not by AX/DX position. */ - if (n->rhs && lu && lu->kind == TY_TUPLE && sz == 24) { + /* 32B tuple initialiser for `(scalar, str)` / `(str, scalar)`. + * Per the AX:DX:CX:R8 return convention: AX = scalar elem, + * DX = str.ptr, CX = str.len, R8 = str.cap. The slot is laid + * out positionally (str takes 24B at its position), so we route + * each register to the slot dictated by the element's type, not + * by AX/DX position. str IS []u8 (24B) → 32B tuple (#1/Phase 3, + * task #5). */ + if (n->rhs && lu && lu->kind == TY_TUPLE && sz == 32) { Tparam *p0 = lu->params; Tparam *p1 = p0 ? p0->next : NULL; Type *t0 = p0 ? p0->type : NULL; @@ -6473,15 +6566,17 @@ cgstmt(Cg *c, Node *n, Local **locals, int *frame) if (e0_str ^ e1_str) { cgexpr(c, n->rhs, *locals); if (e0_str) { - /* layout: str@+0 (16B), scalar@+16. */ + /* layout: str@+0 (24B), scalar@+24. */ ins2(c, A_MOVQ, areg(D_DX), amem(D_BP, off + 0)); ins2(c, A_MOVQ, areg(D_CX), amem(D_BP, off + 8)); - ins2(c, A_MOVQ, areg(D_AX), amem(D_BP, off + 16)); + ins2(c, A_MOVQ, areg(D_R8), amem(D_BP, off + 16)); + ins2(c, A_MOVQ, areg(D_AX), amem(D_BP, off + 24)); } else { - /* layout: scalar@+0 (8B), str@+8 (16B). */ + /* layout: scalar@+0 (8B), str@+8 (24B). */ ins2(c, A_MOVQ, areg(D_AX), amem(D_BP, off + 0)); ins2(c, A_MOVQ, areg(D_DX), amem(D_BP, off + 8)); ins2(c, A_MOVQ, areg(D_CX), amem(D_BP, off + 16)); + ins2(c, A_MOVQ, areg(D_R8), amem(D_BP, off + 24)); } break; } @@ -6846,15 +6941,17 @@ cgstmt(Cg *c, Node *n, Local **locals, int *frame) ins2(c, A_MOVQ, areg(D_AX), areg(D_DX)); } else if (type_isstr(vt)) { + /* str IS []u8: cgexpr leaves + * (AX=ptr, BX=len, CX=cap). Same + * shuffle as the slice arm above — + * DX=ptr, CX=len, R8=cap + * (#1/Phase 3). */ + ins2(c, A_MOVQ, areg(D_CX), + areg(D_R8)); ins2(c, A_MOVQ, areg(D_BX), areg(D_CX)); ins2(c, A_MOVQ, areg(D_AX), areg(D_DX)); - /* str fills DX,CX. Zero R8 if dst - * slot covers slot+24. */ - if (rsz > 24) - ins2(c, A_MOVQ, aimm(0), - areg(D_R8)); } else { ins2(c, A_MOVQ, areg(D_AX), areg(D_DX)); @@ -7140,22 +7237,28 @@ cgstmt(Cg *c, Node *n, Local **locals, int *frame) } } if (n->lhs && node_isstr(n->lhs)) { - cgexpr(c, n->lhs, *locals); /* AX=ptr, BX=len */ - ins2(c, A_MOVQ, areg(D_BX), areg(D_DX)); + /* str IS []u8: AX=ptr, BX=len, CX=cap from cgexpr — + * no AX:DX shuffle, same as a slice (#1/Phase 3). */ + cgexpr(c, n->lhs, *locals); ins2(c, A_MOVQ, areg(D_BP), areg(D_SP)); ins1(c, A_POPQ, areg(D_BP)); ins0(c, A_RET); break; } if (n->lhs && n->lhs->kind == N_TUPLE) { - /* 2-tuple ABI: + /* 2-tuple ABI, word-indexed AX→DX→CX→R8 (the SAME + * register sequence as the tagged-union return; the + * tuple just fills it positionally): * (scalar, scalar) — AX = e0, DX = e1. (16B, fits SysV.) - * (scalar, str) — AX = scalar elem, - * DX = str.ptr, CX = str.len. (24B custom.) + * (scalar, str) — AX = scalar elem, DX = str.ptr, + * CX = str.len, R8 = str.cap. (32B.) * (str, scalar) — same regs, type-keyed not position-keyed. * - * The 24B convention mirrors the existing tagged-union return - * (AX:DX:CX); receive sites destructure off the same regs. */ + * str IS []u8 (24B), so a (scalar, str) tuple is 32B and + * rides AX:DX:CX:R8 — the cap is the 4th word, matching the + * tagged-union return that already uses R8 for slot+24 + * (#1/Phase 3, task #5). Receive sites destructure off the + * same regs. */ Node *e0 = n->lhs->list; Node *e1 = e0 ? e0->next : NULL; if (e1 && e1->next == NULL) { @@ -7166,7 +7269,8 @@ cgstmt(Cg *c, Node *n, Local **locals, int *frame) Node *scaln = e0_is_str ? e1 : e0; cgexpr(c, scaln, *locals); /* AX = scalar */ ins1(c, A_PUSHQ, areg(D_AX)); - cgexpr(c, strn, *locals); /* AX=ptr, BX=len */ + cgexpr(c, strn, *locals); /* AX=ptr, BX=len, CX=cap */ + ins2(c, A_MOVQ, areg(D_CX), areg(D_R8)); ins2(c, A_MOVQ, areg(D_BX), areg(D_CX)); ins2(c, A_MOVQ, areg(D_AX), areg(D_DX)); ins1(c, A_POPQ, areg(D_AX)); @@ -7346,11 +7450,12 @@ cgstmt(Cg *c, Node *n, Local **locals, int *frame) case N_MLET: { /* eval rhs; consume the per-type return-ABI registers. * (scalar, scalar) — AX → l0, DX → l1. - * (scalar, str) — AX → scalar slot, (DX, CX) → str slot - * as (.ptr, .len). Position-agnostic. + * (scalar, str) — AX → scalar slot, (DX, CX, R8) → str slot + * as (.ptr, .len, .cap). Position-agnostic. * Local sizing comes from each l->type so the str slot gets - * the full 16B; without this, only DX would land and the - * len half (CX) would have nowhere to go. */ + * the full 24B; without this, only DX would land and the + * len/cap halves (CX/R8) would have nowhere to go. + * str IS []u8 (24B): the cap rides R8 (#1/Phase 3, task #5). */ cgexpr(c, n->rhs, *locals); Node *l0 = n->list; Node *l1 = l0 ? l0->next : NULL; @@ -7368,15 +7473,17 @@ cgstmt(Cg *c, Node *n, Local **locals, int *frame) int off0 = localoff(c, locals, l0->str, sz0, frame); int off1 = localoff(c, locals, l1->str, sz1, frame); if (s0_is_str) { - /* l0 is str: ptr=DX, len=CX. l1 is scalar: l1 = AX. */ + /* l0 is str: ptr=DX, len=CX, cap=R8. l1 scalar = AX. */ ins2(c, A_MOVQ, areg(D_DX), amem(D_BP, off0 + 0)); ins2(c, A_MOVQ, areg(D_CX), amem(D_BP, off0 + 8)); + ins2(c, A_MOVQ, areg(D_R8), amem(D_BP, off0 + 16)); ins2(c, A_MOVQ, areg(D_AX), amem(D_BP, off1)); } else { /* l0 is scalar; l1 is str. */ ins2(c, A_MOVQ, areg(D_AX), amem(D_BP, off0)); ins2(c, A_MOVQ, areg(D_DX), amem(D_BP, off1 + 0)); ins2(c, A_MOVQ, areg(D_CX), amem(D_BP, off1 + 8)); + ins2(c, A_MOVQ, areg(D_R8), amem(D_BP, off1 + 16)); } break; } @@ -7554,10 +7661,11 @@ cgfn(Cg *c, FILE *out, Node *fn) * offsets, no spill needed. */ int struct_eb = is_struct ? ((pu->size > 8) ? 2 : 1) : 0; int tagged_eb = is_tagged ? (tagged_sz / 8) : 0; - int eightbytes = slice ? 3 : - (is_str ? 2 : + /* str IS []u8: 3 eightbytes (ptr,len,cap), same as a slice + * — the caller pushes the triple (#1/Phase 3). */ + int eightbytes = (slice || is_str) ? 3 : (is_struct ? struct_eb : - (is_tagged ? tagged_eb : 1))); + (is_tagged ? tagged_eb : 1)); int regs_left = isf ? (8 - fargi) : (6 - argi); if (regs_left >= eightbytes) { /* #60: route slice/str slot widths through Type.size SSoT diff --git a/cmd/wcc/type.c b/cmd/wcc/type.c index 06e97fa6..ebafc41d 100644 --- a/cmd/wcc/type.c +++ b/cmd/wcc/type.c @@ -60,8 +60,10 @@ typesinit(Arena *a) ty_uintptr= prim(a, TY_UINTPTR,"uintptr", 8, 8); ty_f32 = prim(a, TY_F32, "f32", 4, 4); ty_f64 = prim(a, TY_F64, "f64", 8, 8); - /* str is { *u8, len } — 16 bytes on amd64. ABI: pointer + u64. */ - ty_str = prim(a, TY_STR, "str", 16, 8); /* sizelint-ok: SSoT for ty_str (#64) */ + /* str IS []u8: { *u8, len, cap } — 24 bytes, 3-reg ABI (#1/Phase 3). + * Size sourced from the slice SSoT (type_slice) so str and []u8 can + * never drift; no second hardcoded 24. */ + ty_str = prim(a, TY_STR, "str", type_slice(a, ty_u8)->size, 8); ty_err = prim(a, TY_ERR, "", 0, 1); ty_never = prim(a, TY_NEVER, "never", 0, 1); /* #29: predeclared `type nomem = !void;`. NAMED so variant_match diff --git a/lib/ww/typ.ww b/lib/ww/typ.ww index ce0033d7..ad492bbc 100644 --- a/lib/ww/typ.ww +++ b/lib/ww/typ.ww @@ -205,7 +205,20 @@ export fn typesinit(c: *tctx) void = { c.tyuintptr= prim(tykind.TY_UINTPTR, "uintptr", 8u64, 8u64); c.tyf32 = prim(tykind.TY_F32, "f32", 4u64, 4u64); c.tyf64 = prim(tykind.TY_F64, "f64", 8u64, 8u64); - c.tystr = prim(tykind.TY_STR, "str", 16u64, 8u64); // sizelint-ok: SSoT for tystr (#64) + // str IS []u8: { *u8, len, cap } — 24B, 3-reg ABI (#1/Phase 3). + // Size sourced from a u8-slice's size (typeslice SSoT) so str and + // []u8 can never drift; no second hardcoded 24. Mirrors cstage + // type.c `type_slice(a, ty_u8)->size`. + // + // The slice tinfo MUST land in a local first: the inline form + // `typeslice(c.tyu8).size` triggers a cgen bug — `call().field` + // where the call returns a *pointer* emits no deref (it uses the + // returned pointer AS the field value), so tystr.size would become + // a heap address → runaway slot-size loops. Filed as task #6 + // (cstage cgen.c N_DOT base=N_CALL-returning-pointer + wwstage + // cgdot mirror); retained here as a local until that lands. + let u8slice: *tinfo = typeslice(c.tyu8); + c.tystr = prim(tykind.TY_STR, "str", u8slice.size, 8u64); c.tyerr = prim(tykind.TY_ERR, "", 0u64, 1u64); c.tynever = prim(tykind.TY_NEVER, "never", 0u64, 1u64); diff --git a/selfhost/cmd/w6c/main.combined.ww b/selfhost/cmd/w6c/main.combined.ww index 8f4f1d10..b98aeca4 100644 --- a/selfhost/cmd/w6c/main.combined.ww +++ b/selfhost/cmd/w6c/main.combined.ww @@ -6535,7 +6535,20 @@ export fn typesinit(c: *tctx) void = { c.tyuintptr= prim(tykind.TY_UINTPTR, "uintptr", 8u64, 8u64); c.tyf32 = prim(tykind.TY_F32, "f32", 4u64, 4u64); c.tyf64 = prim(tykind.TY_F64, "f64", 8u64, 8u64); - c.tystr = prim(tykind.TY_STR, "str", 16u64, 8u64); // sizelint-ok: SSoT for tystr (#64) + // str IS []u8: { *u8, len, cap } — 24B, 3-reg ABI (#1/Phase 3). + // Size sourced from a u8-slice's size (typeslice SSoT) so str and + // []u8 can never drift; no second hardcoded 24. Mirrors cstage + // type.c `type_slice(a, ty_u8)->size`. + // + // The slice tinfo MUST land in a local first: the inline form + // `typeslice(c.tyu8).size` triggers a cgen bug — `call().field` + // where the call returns a *pointer* emits no deref (it uses the + // returned pointer AS the field value), so tystr.size would become + // a heap address → runaway slot-size loops. Filed as task #6 + // (cstage cgen.c N_DOT base=N_CALL-returning-pointer + wwstage + // cgdot mirror); retained here as a local until that lands. + let u8slice: *tinfo = typeslice(c.tyu8); + c.tystr = prim(tykind.TY_STR, "str", u8slice.size, 8u64); c.tyerr = prim(tykind.TY_ERR, "", 0u64, 1u64); c.tynever = prim(tykind.TY_NEVER, "never", 0u64, 1u64); @@ -7811,7 +7824,9 @@ fn primtypesize(nm: str) i64 = { if (streq(nm, "i32") || streq(nm, "u32") || streq(nm, "f32") || streq(nm, "rune")) { return 4i64; }; if (streq(nm, "i64") || streq(nm, "u64") || streq(nm, "f64")) { return 8i64; }; if (streq(nm, "int") || streq(nm, "uint") || streq(nm, "uintptr")) { return 8i64; }; - if (streq(nm, "str")) { return 16i64; }; // sizelint-ok: SSoT for ty_str primtype (#64) + // str IS []u8: 24B, sourced from the slice header SSoT so str and + // []u8 can never drift; no second hardcoded 24 (#1/Phase 3). + if (streq(nm, "str")) { return tyslicesize(); }; return -1i64; }; @@ -10794,8 +10809,11 @@ fn pushargsrev(c: *cgen, arg: *node, param: *node) i32 = { emitline(", AX\n"); emitline("\tPUSHQ\tAX\n"); } else { if (nodeisstr(c, arg)) { - // slot 24: [+0]=tag,[+8]=ptr,[+16]=len. Push high→low - // so pop drains tag first into arg-reg[0]. + // str IS []u8: slot 32 [+0]=tag,[+8]=ptr,[+16]=len, + // [+24]=cap — same shape as the slice arm above. Push + // cap, len, ptr, tag high→low so pop drains tag first + // into arg-reg[0] (#1/Phase 3). + emitline("\tPUSHQ\tCX\n"); emitline("\tPUSHQ\tBX\n"); emitline("\tPUSHQ\tAX\n"); emitline("\tMOVQ\t$"); @@ -11013,9 +11031,12 @@ fn pushargsrev(c: *cgen, arg: *node, param: *node) i32 = { return rest + 3; }; if (nodeisstr(c, arg)) { + // str IS []u8: cgexpr left (AX=ptr, BX=len, CX=cap). Push + // the triple, same as the slice arm above (#1/Phase 3). + emitline("\tPUSHQ\tCX\n"); emitline("\tPUSHQ\tBX\n"); emitline("\tPUSHQ\tAX\n"); - return rest + 2; + return rest + 3; }; // #21: CALL returning a tagged-union — the aistagged guard // above kept us out of the widening path. Push the tagged- @@ -12865,7 +12886,7 @@ fn cgloadtaggedfield(c: *cgen, basereg: str, foff: i32, slot_sz: i32) void = { // would need a reversed direction we don't currently emit). // - struct src (literal or ident): zero slot, write fields at +8+foff, // tag last. -// - str src: tag@+0, ptr@+8, len@+16. +// - str src: tag@+0, ptr@+8, len@+16, cap@+24 (str IS []u8, #1/Phase 3). // - scalar src: tag@+0, value@+8. fn cgwidentaggedstore(c: *cgen, dst: *tinfo, src: *node, basereg: str, slot_off: i32, slot_sz: i32) void = { @@ -13083,12 +13104,18 @@ fn cgwidentaggedstorebp(c: *cgen, dst: *tinfo, src: *node, slot_off: i32, slot_s emitoff((slot_off + 8 + fi.foff): i64); emitline("(BP)\n"); } else { if (isstrtype(c, fi.tnode)) { + // str IS []u8: 3-word field + // (ptr,len,cap) from cgexpr's + // AX/BX/CX (#1/Phase 3). emitline("\tMOVQ\tAX, "); emitoff((slot_off + 8 + fi.foff): i64); emitline("(BP)\n"); emitline("\tMOVQ\tBX, "); emitoff((slot_off + 8 + fi.foff + 8): i64); emitline("(BP)\n"); + emitline("\tMOVQ\tCX, "); + emitoff((slot_off + 8 + fi.foff + 16): i64); + emitline("(BP)\n"); } else { let sop: str = fieldstoreop(c, fi); emitline("\t"); @@ -13146,7 +13173,9 @@ fn cgwidentaggedstorebp(c: *cgen, dst: *tinfo, src: *node, slot_off: i32, slot_s return; }; }; - // Str payload. + // str IS []u8: AX=ptr, BX=len, CX=cap from cgexpr. Slot layout + // tag@+0, ptr@+8, len@+16, cap@+24 — same 32B shape as the slice + // payload below (#1/Phase 3). if (nodeisstr(c, src)) { cgexpr(c, src); emitline("\tMOVQ\tAX, "); @@ -13155,6 +13184,9 @@ fn cgwidentaggedstorebp(c: *cgen, dst: *tinfo, src: *node, slot_off: i32, slot_s emitline("\tMOVQ\tBX, "); emitoff((slot_off + 16): i64); emitline("(BP)\n"); + emitline("\tMOVQ\tCX, "); + emitoff((slot_off + 24): i64); + emitline("(BP)\n"); let tag: i32 = taggedvariantindext(c, dt, src); if (tag < 0) { tag = 0; }; emitline("\tMOVQ\t$"); @@ -13382,11 +13414,15 @@ export fn dotchainresolve(c: *cgen, n: *node, for (ft != nil && ft.kind == tykind.TY_NAMED) { ft = ft.under; }; if (ft == nil) { return false; }; if (ft.kind == tykind.TY_STR) { + // str IS []u8: .cap is the third header word, same as + // the TY_SLICE leaf below — cstage treats str≡slice for + // .ptr/.len/.cap (cmd/w6c/cgen.c:2478) (#1/Phase 3, #11). if (i != 1) { return false; }; let pseudo: str = stk[0].str; let delta: i32 = -1; if (streq(pseudo, "ptr")) { delta = 0; } - else { if (streq(pseudo, "len")) { delta = 8; }; }; + else { if (streq(pseudo, "len")) { delta = 8; } + else { if (streq(pseudo, "cap")) { delta = 16; }; }; }; if (delta < 0) { return false; }; *outtotaloff = *outtotaloff + foff; *outslicedelta = delta; @@ -13699,6 +13735,44 @@ fn cgstructlitfill(c: *cgen, si: *structinfo, lit: *node, fi = nil; } else if (callwhole) { fi = nil; + } else if (isstrtype(c, fi.tnode)) { + // str IS []u8: 3-word field (ptr,len,cap). + // cgexpr leaves AX/BX/CX; for non-BP modes + // the dst base goes in DX to dodge BX=len / + // CX=cap (the generic store reloads BX, which + // would clobber len) (#1/Phase 3). + cgexpr(c, fieldnode.lhs); + if (mode == 0) { + emitline("\tMOVQ\tAX, "); + emitoff((disp + fi.foff): i64); + emitline("(BP)\n"); + emitline("\tMOVQ\tBX, "); + emitoff((disp + fi.foff + 8): i64); + emitline("(BP)\n"); + emitline("\tMOVQ\tCX, "); + emitoff((disp + fi.foff + 16): i64); + emitline("(BP)\n"); + } else { + if (mode == 1) { + emitline("\tMOVQ\t"); + emitoff(srcoff: i64); + emitline("(BP), DX\n"); + } else { + emitline("\tLEAQ\t"); + emitsymname(c, srcname); + emitline("(SB), DX\n"); + }; + emitline("\tMOVQ\tAX, "); + emitdispreg((disp + fi.foff): i64, "DX"); + emitline("\n"); + emitline("\tMOVQ\tBX, "); + emitdispreg((disp + fi.foff + 8): i64, "DX"); + emitline("\n"); + emitline("\tMOVQ\tCX, "); + emitdispreg((disp + fi.foff + 16): i64, "DX"); + emitline("\n"); + }; + fi = nil; } else { cgexpr(c, fieldnode.lhs); // For non-BP modes, cgexpr just clobbered @@ -13956,7 +14030,11 @@ fn cgtryprop(c: *cgen, n: *node) void = { }; }; if (succisstr) { + // str IS []u8: success arrives DX=ptr, CX=len, R8=cap + // (slot 32B). Move len out before cap overwrites CX + // (#1/Phase 3). emitline("\tMOVQ\tCX, BX\n"); + emitline("\tMOVQ\tR8, CX\n"); }; emitline("\tMOVQ\tDX, AX\n"); return; @@ -14012,7 +14090,11 @@ fn cgtryunw(c: *cgen, n: *node) void = { }; }; if (succisstr) { + // str IS []u8: success arrives DX=ptr, CX=len, R8=cap + // (slot 32B). Move len out before cap overwrites CX + // (#1/Phase 3). emitline("\tMOVQ\tCX, BX\n"); + emitline("\tMOVQ\tR8, CX\n"); }; emitline("\tMOVQ\tDX, AX\n"); return; @@ -14285,8 +14367,9 @@ fn cgcast(c: *cgen, n: *node) void = { }; fn cgstrlit(c: *cgen, n: *node) void = { - // Result is the (ptr, len) pair: ptr in AX, len in BX. Call - // sites that expect a str arg pick these up directly. + // str IS []u8: the (ptr, len, cap) triple — ptr in AX, len in BX, + // cap in CX. A static literal has no spare storage, so cap = len + // (#1/Phase 3). Call sites that expect a str arg pick these up. let nstr: str = n.str; let lab: str = internstrlit(c, nstr); emitline("\tLEAQ\t"); @@ -14295,6 +14378,9 @@ fn cgstrlit(c: *cgen, n: *node) void = { emitline("\tMOVQ\t$"); emitint(nstr.len: i64); emitline(", BX\n"); + emitline("\tMOVQ\t$"); + emitint(nstr.len: i64); + emitline(", CX\n"); return; }; @@ -14330,9 +14416,14 @@ fn cgident(c: *cgen, n: *node) void = { emitoff(off: i64); emitline("(BP), AX\n"); if (isstr) { + // str IS []u8: load (ptr,len,cap) into AX/BX/CX, + // identical to the slice arm below (#1/Phase 3). emitline("\tMOVQ\t"); emitoff((off + 8): i64); emitline("(BP), BX\n"); + emitline("\tMOVQ\t"); + emitoff((off + 16): i64); + emitline("(BP), CX\n"); }; if (issl) { emitline("\tMOVQ\t"); @@ -14362,6 +14453,11 @@ fn cgident(c: *cgen, n: *node) void = { emitline("\tMOVQ\t$"); emitint(bytes.len: i64); emitline(", BX\n"); + // str IS []u8: cap = len for a static def literal + // (#1/Phase 3). + emitline("\tMOVQ\t$"); + emitint(bytes.len: i64); + emitline(", CX\n"); return; }; }; @@ -14393,17 +14489,16 @@ fn cgident(c: *cgen, n: *node) void = { let isstr: bool = letvarisstr(c, nm); let issl: bool = letvarisslice(c, nm); if (isstr || issl) { + // str IS []u8: both str and slice carry a third 8B + // (cap); load it unconditionally. The address holder CX + // is overwritten by the cap as the last step, after + // ptr/len are already loaded (#1/Phase 3). emitline("\tLEAQ\t"); emitsymname(c, nm); emitline("(SB), CX\n"); emitline("\tMOVQ\t(CX), AX\n"); emitline("\tMOVQ\t8(CX), BX\n"); - if (issl) { - // Overwrites the address holder with the - // cap as the last step — CX is no longer - // needed once both ptr/len are loaded. - emitline("\tMOVQ\t16(CX), CX\n"); - }; + emitline("\tMOVQ\t16(CX), CX\n"); return; }; // Float global: same LEAQ-indirect shape, since MOVSS/ @@ -14457,6 +14552,12 @@ fn cgindex(c: *cgen, n: *node) void = { let idx: *node = n.rhs; let esz: i32 = 8; let signed_elem: bool = false; + // #1/Phase 3: str=24B collides with slice=24B, so the str-element + // branches below MUST gate on kind (mirroring cstage's elem_is_str), + // not a bare `esz == primtypesize("str")` size check — otherwise a + // []u8 element (also 24B) misfires into the str 2-word load and + // diverges from cstage (#60 collision class; sentinel 754). + let elemisstr: bool = false; let baselocal: *local = nil; // Global `[N]T` array or `*T` pointer used as an index base. // The local-ident lookup above misses it; we need LEAQ name(SB) @@ -14498,7 +14599,7 @@ fn cgindex(c: *cgen, n: *node) void = { // cgen.c:3517-18). esz-only — N_DOT-base signedness // stays unset, as before. let dt: *tinfo = n.type_: *tinfo; - if (dt != nil) { esz = dt.size: i32; }; + if (dt != nil) { esz = dt.size: i32; elemisstr = typeisstr(dt); }; } else { if (base.kind == nkind.N_INDEX) { // #60: chained `names[i][k]` — n.type_ is the checker- // stamped outer element tinfo (indexresult over the inner @@ -14547,6 +14648,7 @@ fn cgindex(c: *cgen, n: *node) void = { esz = elem_slot_sz; }; }; + elemisstr = isstrtype(c, etn); }; }; cgexpr(c, idx); @@ -14582,7 +14684,7 @@ fn cgindex(c: *cgen, n: *node) void = { }; // #43: str element-stride routes through primtypesize so the // 16-vs-24 dispatch tracks ty_str.size for #1. - if (esz == primtypesize("str"): i32) { + if (elemisstr) { emitline("\tMOVQ\t8(BX), CX\n"); emitline("\tMOVQ\t(BX), AX\n"); emitline("\tMOVQ\tCX, BX\n"); @@ -14624,7 +14726,7 @@ fn cgindex(c: *cgen, n: *node) void = { // str element (16B today): load (ptr, len) into (AX, BX) so // the value flows through the str-rhs convention. // #43: route via primtypesize so the stride tracks #1. - if (esz == primtypesize("str"): i32) { + if (elemisstr) { emitline("\tMOVQ\t8(BX), CX\n"); emitline("\tMOVQ\t(BX), AX\n"); emitline("\tMOVQ\tCX, BX\n"); @@ -14656,7 +14758,7 @@ fn cgindex(c: *cgen, n: *node) void = { }; // #43: str element-stride routes through primtypesize so the // 16-vs-24 dispatch tracks ty_str.size for #1. - if (esz == primtypesize("str"): i32) { + if (elemisstr) { emitline("\tMOVQ\t8(AX), BX\n"); emitline("\tMOVQ\t(AX), AX\n"); return; @@ -15251,10 +15353,11 @@ fn cgdot(c: *cgen, n: *node) void = { }; // Hare-style tuple positional access: `t.0`, `t.1`. // Walk the tuple element type list summing slotsize - // (matches the (scalar, str) init layout which puts - // the scalar in an 8B slot and the str in 16B). For - // a str element, load both halves into (AX, BX) so - // chains like `t.1.len` propagate correctly. + // (matches the (scalar, str) init layout: scalar in an + // 8B slot, str in 24B — str IS []u8, #1/Phase 3). For a + // str element, load (ptr, len) into (AX, BX); the cap + // stays in the slot (the 2-word str-field read, like + // every other chained/dot str leaf read — task #14). if (lkind == nkind.N_TTUPLE) { let idx: i32 = fldnumidx(fld); if (idx >= 0) { @@ -15369,9 +15472,9 @@ fn cgdot(c: *cgen, n: *node) void = { let delta: i32 = -1; if (streq(fld, "ptr")) { delta = 0; }; if (streq(fld, "len")) { delta = 8; }; - if (issl) { - if (streq(fld, "cap")) { delta = 16; }; - }; + // str IS []u8: .cap is valid on a str global too, + // not slice-only — mirrors cstage (#1/Phase 3, #11). + if (streq(fld, "cap")) { delta = 16; }; if (delta >= 0) { emitline("\tLEAQ\t"); emitsymname(c, lhs.str); @@ -16227,7 +16330,9 @@ fn cgun(c: *cgen, n: *node) void = { let gdelta: i32 = -1; if (streq(fld, "ptr")) { gdelta = 0; }; if (streq(fld, "len")) { gdelta = 8; }; - if (issl) { if (streq(fld, "cap")) { gdelta = 16; }; }; + // str IS []u8: &str.cap is valid too, not slice-only + // — mirrors cstage (#1/Phase 3, #11). + if (streq(fld, "cap")) { gdelta = 16; }; if (gdelta >= 0) { emitline("\tLEAQ\t"); emitsymname(c, basenm); @@ -16599,16 +16704,19 @@ fn cgalloc(c: *cgen, n: *node) void = { emitline("\n"); fi = nil; } else { if (isstrtype(c, fi.tnode)) { - // alloc(T{ fval = s }) for str field: cgexpr - // leaves (AX=ptr, BX=len). Use CX for the heap - // base so BX=len survives both stores. Mirrors - // cmd/w6c/cgen.c:4184-4190. - emitline("\tMOVQ\t(SP), CX\n"); + // str IS []u8: cgexpr leaves (AX=ptr, + // BX=len, CX=cap). Route the heap base + // through DX so all three survive — CX + // holds cap, BX holds len (#1/Phase 3). + emitline("\tMOVQ\t(SP), DX\n"); emitline("\tMOVQ\tAX, "); - emitdispreg(fi.foff: i64, "CX"); + emitdispreg(fi.foff: i64, "DX"); emitline("\n"); emitline("\tMOVQ\tBX, "); - emitdispreg((fi.foff + 8): i64, "CX"); + emitdispreg((fi.foff + 8): i64, "DX"); + emitline("\n"); + emitline("\tMOVQ\tCX, "); + emitdispreg((fi.foff + 16): i64, "DX"); emitline("\n"); fi = nil; } else { @@ -17068,7 +17176,8 @@ fn cgcall(c: *cgen, n: *node) void = { popped += 1; } else { let extra: i32 = 0; - if (nodeisstr(c, a)) { extra = 1; }; + // str IS []u8: 3-word arg, same as slice (#1/Phase 3). + if (nodeisstr(c, a)) { extra = 2; }; if (nodeisslice(c, a)) { extra = 2; }; // #21: tagged-CALL arg was pushed AX/DX/CX/R8 high→low // by pushargsrev; size the per-arg pop to match so the @@ -17238,31 +17347,9 @@ fn cgcall(c: *cgen, n: *node) void = { emitint((stackslots * 8): i64); emitline(", SP\n"); }; - // SysV returns 16-byte aggregates in (AX, DX). Our str - // convention is (AX, BX), so shuffle for str-returning calls. - // Route through fnretlookupmod: for N_DOT cross-module callees, - // the bare-leaf fnretlookup's same-module-first walk (#4e) would - // pick the caller-module's same-leaf fn — a str-returning - // caller-side `slice` over a []u8-returning `mod.slice` then - // emits a phantom MOVQ DX, BX after the cross-module CALL (#34). - if (calleename.len > 0) { - let cmod: str; - cmod.ptr = nil; cmod.len = 0; - if (callee != nil) { - if (callee.kind == nkind.N_IDENT) { cmod = c.curmod; }; - if (callee.kind == nkind.N_DOT) { - if (callee.lhs != nil) { - if (callee.lhs.kind == nkind.N_IDENT) { - cmod = callee.lhs.str; - }; - }; - }; - }; - let rtyp: *node = fnretlookupmod(c, calleename, cmod); - if (isstrtype(c, rtyp)) { - emitline("\tMOVQ\tDX, BX\n"); - }; - }; + // str IS []u8: a str-returning callee leaves AX=ptr, BX=len, + // CX=cap — same as a slice, so there is no receive-side shuffle + // (#1/Phase 3). return; }; @@ -17361,17 +17448,21 @@ fn cgassign(c: *cgen, n: *node) void = { emitline("\tX0, (BX)\n"); return; }; - // Push order matches C cgen - // (cmd/w6c/cgen.c:1033-1041): PUSHQ AX - // (ptr) first, then PUSHQ BX (len) if - // str, so the pop sequence is POP CX - // (len) → POP AX (ptr) → MOVQ AX, - // (BX) → MOVQ CX, 8(BX). + // str IS []u8: PUSHQ AX (ptr) first, then + // PUSHQ BX (len) + PUSHQ CX (cap) across the + // pointer eval which clobbers BX/CX. Pop drains + // cap (top) → 16(BX), then len, then ptr → 0(BX) + // with len → 8(BX) (#1/Phase 3). emitline("\tPUSHQ\tAX\n"); - if (elemstr) { emitline("\tPUSHQ\tBX\n"); }; + if (elemstr) { + emitline("\tPUSHQ\tBX\n"); + emitline("\tPUSHQ\tCX\n"); + }; cgexpr(c, inner); emitline("\tMOVQ\tAX, BX\n"); if (elemstr) { + emitline("\tPOPQ\tCX\n"); + emitline("\tMOVQ\tCX, 16(BX)\n"); emitline("\tPOPQ\tCX\n"); emitline("\tPOPQ\tAX\n"); emitline("\tMOVQ\tAX, (BX)\n"); @@ -17625,10 +17716,19 @@ fn cgassign(c: *cgen, n: *node) void = { }; }; cgexpr(c, n.rhs); // value → AX - // #43: spill BX (str.len) before computing - // the index so the post-index store can pop - // it; the stride gate tracks ty_str.size. - if (esz == primtypesize("str"): i32) { emitline("\tPUSHQ\tBX\n"); }; + // str IS []u8: spill cap (CX) + len (BX) before + // computing the index so the post-index store can + // pop all three. #1/Phase 3: str=24B collides with + // slice=24B, so this MUST gate on kind (cstage's + // elem_is_str, cmd/w6c/cgen.c:3576) — not a bare + // `esz == primtypesize("str")` — or a []u8 element + // (also 24B) misfires the str 3-word store and + // diverges from cstage. Write-side mirror of the + // cgindex read-path gate (#7/754). + if (isstrtype(c, elemtn)) { + emitline("\tPUSHQ\tCX\n"); + emitline("\tPUSHQ\tBX\n"); + }; emitline("\tPUSHQ\tAX\n"); cgexpr(c, idx); // idx → AX if (esz > 1) { @@ -17666,13 +17766,15 @@ fn cgassign(c: *cgen, n: *node) void = { emitline("\tPOPQ\tAX\n"); // scaled idx emitline("\tADDQ\tAX, BX\n"); emitline("\tPOPQ\tAX\n"); // value - // #43: str-element write — pop the saved - // .len and store both halves. Stride gate - // routes through primtypesize for #1. - if (esz == primtypesize("str"): i32) { + // str IS []u8: pop the saved len + cap and store + // all three words. Kind-gate, not size — see the + // spill site above (#1/Phase 3, #7/754). + if (isstrtype(c, elemtn)) { emitline("\tMOVQ\tAX, (BX)\n"); emitline("\tPOPQ\tCX\n"); emitline("\tMOVQ\tCX, 8(BX)\n"); + emitline("\tPOPQ\tCX\n"); + emitline("\tMOVQ\tCX, 16(BX)\n"); return; }; let isop: str = tnodestoreop(c, elemtn, esz); @@ -18099,20 +18201,24 @@ fn cgassign(c: *cgen, n: *node) void = { emitline("\tMOVQ\tBX, AX\n"); }; }; - // str field via *struct: rhs left - // (AX=ptr, BX=len). Use CX as the - // address scratch so we don't clobber - // the len half before storing it. + // str IS []u8: rhs left (AX=ptr, + // BX=len, CX=cap). CX holds cap, so + // stage the struct addr in DX and + // store all three words — identical + // to the slice arm below (#1/Phase 3). if (n.op == tkind.TK_ASSIGN) { if (isstrtype(c, fi.tnode)) { emitline("\tMOVQ\t"); emitoff(lc.off: i64); - emitline("(BP), CX\n"); + emitline("(BP), DX\n"); emitline("\tMOVQ\tAX, "); - emitdispreg(fi.foff: i64, "CX"); + emitdispreg(fi.foff: i64, "DX"); emitline("\n"); emitline("\tMOVQ\tBX, "); - emitdispreg((fi.foff + 8): i64, "CX"); + emitdispreg((fi.foff + 8): i64, "DX"); + emitline("\n"); + emitline("\tMOVQ\tCX, "); + emitdispreg((fi.foff + 16): i64, "DX"); emitline("\n"); return; }; @@ -18303,11 +18409,11 @@ fn cgassign(c: *cgen, n: *node) void = { };}; }; cgexpr(c, n.rhs); - // str field: cgexpr left (AX=ptr, BX=len); - // store both halves at +0/+8. Without this, - // `L.src = s` would only write the ptr and - // `L.src.len` would carry whatever was on the - // stack. + // str IS []u8: cgexpr left (AX=ptr, + // BX=len, CX=cap); store all three at + // +0/+8/+16, identical to the slice + // arm below. BP base, no scratch + // reload needed (#1/Phase 3). if (isstrtype(c, fi.tnode)) { emitline("\tMOVQ\tAX, "); emitoff((lc.off + fi.foff): i64); @@ -18315,6 +18421,9 @@ fn cgassign(c: *cgen, n: *node) void = { emitline("\tMOVQ\tBX, "); emitoff((lc.off + fi.foff + 8): i64); emitline("(BP)\n"); + emitline("\tMOVQ\tCX, "); + emitoff((lc.off + fi.foff + 16): i64); + emitline("(BP)\n"); return; }; // slice field direct: cgexpr left @@ -18562,14 +18671,22 @@ fn cgassign(c: *cgen, n: *node) void = { if (n.op == tkind.TK_ASSIGN) { cgexpr(c, n.rhs); if (isstrtype(c, fi.tnode)) { + // str IS []u8: cgexpr left + // (AX=ptr, BX=len, CX=cap). CX + // holds cap, so stage the base + // addr in DX and store all three + // words (#1/Phase 3). emitline("\tLEAQ\t"); emitsymname(c, bn); - emitline("(SB), CX\n"); + emitline("(SB), DX\n"); emitline("\tMOVQ\tAX, "); - emitdispreg(fi.foff: i64, "CX"); + emitdispreg(fi.foff: i64, "DX"); emitline("\n"); emitline("\tMOVQ\tBX, "); - emitdispreg((fi.foff + 8): i64, "CX"); + emitdispreg((fi.foff + 8): i64, "DX"); + emitline("\n"); + emitline("\tMOVQ\tCX, "); + emitdispreg((fi.foff + 16): i64, "DX"); emitline("\n"); return; }; @@ -18808,22 +18925,29 @@ fn cgassign(c: *cgen, n: *node) void = { return; }; if (typeisstr(leaftype)) { + // str IS []u8: store ptr/len/cap. cgexpr leaves + // CX=cap, so the viacx base goes in DX (not CX) to + // avoid clobbering it — same as the single-dot str + // field store (#1/Phase 3). cgexpr(c, n.rhs); if (viacx) { if (ptrroot) { emitline("\tMOVQ\t"); emitoff(rootoff: i64); - emitline("(BP), CX\n"); + emitline("(BP), DX\n"); } else { emitline("\tLEAQ\t"); emitsymname(c, rootname); - emitline("(SB), CX\n"); + emitline("(SB), DX\n"); }; emitline("\tMOVQ\tAX, "); - emitdispreg(totaloff: i64, "CX"); + emitdispreg(totaloff: i64, "DX"); emitline("\n"); emitline("\tMOVQ\tBX, "); - emitdispreg((totaloff + 8): i64, "CX"); + emitdispreg((totaloff + 8): i64, "DX"); + emitline("\n"); + emitline("\tMOVQ\tCX, "); + emitdispreg((totaloff + 16): i64, "DX"); emitline("\n"); } else { emitline("\tMOVQ\tAX, "); @@ -18832,6 +18956,9 @@ fn cgassign(c: *cgen, n: *node) void = { emitline("\tMOVQ\tBX, "); emitoff((rootoff + totaloff + 8): i64); emitline("(BP)\n"); + emitline("\tMOVQ\tCX, "); + emitoff((rootoff + totaloff + 16): i64); + emitline("(BP)\n"); }; return; }; @@ -19319,11 +19446,17 @@ fn cgassign(c: *cgen, n: *node) void = { cgexpr(c, n.rhs); if (n.op == tkind.TK_ASSIGN) { if (letvarisstr(c, nm)) { + // str IS []u8: stash cap in DI before LEAQ + // overwrites CX, then store ptr/len/cap — + // identical to the slice arm below + // (#1/Phase 3). + emitline("\tMOVQ\tCX, DI\n"); emitline("\tLEAQ\t"); emitsymname(c, nm); emitline("(SB), CX\n"); emitline("\tMOVQ\tAX, (CX)\n"); emitline("\tMOVQ\tBX, 8(CX)\n"); + emitline("\tMOVQ\tDI, 16(CX)\n"); return; }; if (letvarisslice(c, nm)) { @@ -19609,7 +19742,9 @@ fn cgassign(c: *cgen, n: *node) void = { emitoff((off + 8): i64); emitline("(BP)\n"); }; - if (lcsl) { + // str IS []u8: store the cap word too, identical to + // the slice store (#1/Phase 3). + if (lcstr || lcsl) { emitline("\tMOVQ\tCX, "); emitoff((off + 16): i64); emitline("(BP)\n"); @@ -19812,12 +19947,16 @@ fn cgreturn(c: *cgen, n: *node) void = { rundefers(c); let rhs: *node = n.lhs; if (rhs != nil) { - // Tuple return `return a, b;`: + // Tuple return `return a, b;`, word-indexed AX→DX→CX→R8 (the + // SAME register sequence as the tagged-union return below; the + // tuple just fills it positionally): // (scalar, scalar) — AX = v0, DX = v1. // (scalar, str) / (str, scalar) — AX = scalar elem, - // DX = str.ptr, CX = str.len. - // 24B convention mirrors the tagged-union return below; receive - // sites destructure off the same regs regardless of position. + // DX = str.ptr, CX = str.len, R8 = str.cap. + // str IS []u8 (24B) → 32B tuple; cap rides R8, matching the + // tagged-union return that already uses R8 for slot+24 + // (#1/Phase 3, task #5). Receive sites destructure off the + // same regs regardless of position. if (rhs.kind == nkind.N_TUPLE) { let v: *node = rhs.list; if (v != nil) { @@ -19832,6 +19971,7 @@ fn cgreturn(c: *cgen, n: *node) void = { cgexpr(c, scaln); emitline("\tPUSHQ\tAX\n"); cgexpr(c, strn); + emitline("\tMOVQ\tCX, R8\n"); emitline("\tMOVQ\tBX, CX\n"); emitline("\tMOVQ\tAX, DX\n"); emitline("\tPOPQ\tAX\n"); @@ -19984,12 +20124,12 @@ fn cgreturn(c: *cgen, n: *node) void = { emitline("\tMOVQ\tBX, CX\n"); emitline("\tMOVQ\tAX, DX\n"); } else { if (nodeisstr(c, rhs)) { + // str IS []u8: cgexpr leaves (AX=ptr, BX=len, + // CX=cap). Same shuffle as the slice arm above — + // DX=ptr, CX=len, R8=cap (#1/Phase 3). + emitline("\tMOVQ\tCX, R8\n"); emitline("\tMOVQ\tBX, CX\n"); emitline("\tMOVQ\tAX, DX\n"); - // str fills DX,CX. Zero R8 if dst covers slot+24. - if (rsz > 24) { - emitline("\tMOVQ\t$0, R8\n"); - }; } else { emitline("\tMOVQ\tAX, DX\n"); // scalar fills DX only. Zero CX / R8 if dst @@ -20245,11 +20385,8 @@ fn cgreturn(c: *cgen, n: *node) void = { }; emitline("\tMOVQ\t$0, AX\n"); }; - // SysV: 16-byte aggregates (str, 2-tuple) return in (AX, DX). - // cgexpr leaves str in (AX, BX); shuffle BX→DX. - if (isstrtype(c, c.fnret)) { - emitline("\tMOVQ\tBX, DX\n"); - }; + // str IS []u8: cgexpr leaves AX=ptr, BX=len, CX=cap — str now + // returns exactly like a slice, no AX:DX shuffle (#1/Phase 3). emitline("\tMOVQ\tBP, SP\n"); emitline("\tPOPQ\tBP\n"); emitline("\tRET\n"); @@ -20420,11 +20557,13 @@ fn cglet(c: *cgen, n: *node) void = { c.lastwasreturn = 0; return; }; - // 24B tuple init for `let t: (scalar, str) = call()` / - // `let t: (str, scalar) = call()`. Per the AX:DX:CX return - // convention: AX = scalar elem, DX = str.ptr, CX = str.len. - // Layout is positional, so we route each register to the - // slot dictated by element type, not by AX/DX position. + // 32B tuple init for `let t: (scalar, str) = call()` / + // `let t: (str, scalar) = call()`. Per the AX:DX:CX:R8 return + // convention: AX = scalar elem, DX = str.ptr, CX = str.len, + // R8 = str.cap. Layout is positional (str takes 24B at its + // position), so we route each register to the slot dictated by + // element type, not by AX/DX position. str IS []u8 (24B) → 32B + // tuple (#1/Phase 3, task #5). if (n.lhs != nil) { if (n.lhs.kind == nkind.N_TTUPLE) { let p0: *node = n.lhs.list; @@ -20447,9 +20586,12 @@ fn cglet(c: *cgen, n: *node) void = { emitline("\tMOVQ\tCX, "); emitoff((off + 8): i64); emitline("(BP)\n"); - emitline("\tMOVQ\tAX, "); + emitline("\tMOVQ\tR8, "); emitoff((off + 16): i64); emitline("(BP)\n"); + emitline("\tMOVQ\tAX, "); + emitoff((off + 24): i64); + emitline("(BP)\n"); } else { emitline("\tMOVQ\tAX, "); emitoff(off: i64); @@ -20460,6 +20602,9 @@ fn cglet(c: *cgen, n: *node) void = { emitline("\tMOVQ\tCX, "); emitoff((off + 16): i64); emitline("(BP)\n"); + emitline("\tMOVQ\tR8, "); + emitoff((off + 24): i64); + emitline("(BP)\n"); }; c.lastwasreturn = 0; return; @@ -20750,15 +20895,19 @@ fn cglet(c: *cgen, n: *node) void = { emitline("\tMOVQ\tAX, "); emitoff(off: i64); emitline("(BP)\n"); - // str init: cgexpr also leaves len in BX; store both. + // str IS []u8: cgexpr leaves (ptr,len,cap) in AX/BX/CX; store + // all three, same as the slice arm below (#1/Phase 3). // #60: gate by kind too — under #1's str=24 bump, sizeof(str) // and sizeof(slice) collide, so a bare `sz ==` check fires - // both branches for one let. Mirrors cstage cgen.c:6439's + // both branches for one let. Mirrors cstage cgen.c's // `type_isstr(lt) && sz == ty_str->size` shape. if (isstrtype(c, tn) && sz == primtypesize("str"): i32) { emitline("\tMOVQ\tBX, "); emitoff((off + 8): i64); emitline("(BP)\n"); + emitline("\tMOVQ\tCX, "); + emitoff((off + 16): i64); + emitline("(BP)\n"); }; // slice init: ptr/len/cap in AX/BX/CX. Same kind+size gate as // the str arm — without the kind check this fires on a str let @@ -20919,11 +21068,12 @@ fn cgmassign(c: *cgen, n: *node) void = { // type is taken from its explicit annotation (l.lhs) when present // or inferred from the called fn's return-type tuple element. // -// Per the AX:DX:CX return convention (mirrors C cgen nkind.N_MLET): +// Per the AX:DX:CX:R8 return convention (mirrors C cgen nkind.N_MLET): // (scalar, scalar) — AX → l0, DX → l1. -// (scalar, str) — AX → scalar slot, (DX, CX) → str slot -// as (.ptr, .len). Position-agnostic — the +// (scalar, str) — AX → scalar slot, (DX, CX, R8) → str slot +// as (.ptr, .len, .cap). Position-agnostic — the // regs are routed by element type, not by AX/DX. +// str IS []u8 (24B): cap rides R8 (#1/Phase 3, task #5). fn cgmlet(c: *cgen, n: *node) void = { let rhs: *node = n.rhs; if (rhs == nil) { return; }; @@ -20992,16 +21142,21 @@ fn cgmlet(c: *cgen, n: *node) void = { let off0: i32 = localadd(c, l0.str, sz0, t0); let off1: i32 = localadd(c, l1.str, sz1, t1); if (s0_is_str) { + // l0 str: ptr=DX, len=CX, cap=R8. l1 scalar = AX. emitline("\tMOVQ\tDX, "); emitoff(off0: i64); emitline("(BP)\n"); emitline("\tMOVQ\tCX, "); emitoff((off0 + 8): i64); emitline("(BP)\n"); + emitline("\tMOVQ\tR8, "); + emitoff((off0 + 16): i64); + emitline("(BP)\n"); emitline("\tMOVQ\tAX, "); emitoff(off1: i64); emitline("(BP)\n"); } else { + // l0 scalar; l1 str: ptr=DX, len=CX, cap=R8. emitline("\tMOVQ\tAX, "); emitoff(off0: i64); emitline("(BP)\n"); @@ -21011,6 +21166,9 @@ fn cgmlet(c: *cgen, n: *node) void = { emitline("\tMOVQ\tCX, "); emitoff((off1 + 8): i64); emitline("(BP)\n"); + emitline("\tMOVQ\tR8, "); + emitoff((off1 + 16): i64); + emitline("(BP)\n"); }; c.lastwasreturn = 0; return; @@ -21597,11 +21755,11 @@ fn cgfnparams(c: *cgen, params: *node) void = { stkcursor += 3; };}; } else { if (isstrtype(c, p.lhs)) { - if (idx + 2 <= 6) { - // #60: route str-param slot width through the - // primtypesize SSoT so #1's ty_str bump propagates - // here (parent #43 covered the reg-fill site only - // inside cgexpr). + if (idx + 3 <= 6) { + // str IS []u8: 3-word param (ptr,len,cap), same as + // the slice arm above (#1/Phase 3). #60: route slot + // width through the primtypesize SSoT so #1's ty_str + // bump propagates here. let off: i32 = localadd(c, nm, primtypesize("str"): i32, p.lhs); emitline("\tMOVQ\t"); emitline(argregname(idx)); @@ -21615,11 +21773,14 @@ fn cgfnparams(c: *cgen, params: *node) void = { emitoff((off + 8): i64); emitline("(BP)\n"); idx += 1; + emitline("\tMOVQ\t"); + emitline(argregname(idx)); + emitline(", "); + emitoff((off + 16): i64); + emitline("(BP)\n"); + idx += 1; } else { if (idx < 6) { - // Partial-fit stitch — mirrors tagged at lines - // 440-469. Only idx=5 hits this (nw=2, - // regs_left=1): ptr lands in R9, len at - // +16+stkcursor*8(BP). + // Partial-fit stitch — mirrors the slice arm above. // #60: same SSoT routing as the regs-fit arm above. let off: i32 = localadd(c, nm, primtypesize("str"): i32, p.lhs); let regs_left: i32 = 6 - idx; @@ -21633,7 +21794,7 @@ fn cgfnparams(c: *cgen, params: *node) void = { idx += 1; w += 1; }; - for (w < 2) { + for (w < 3) { emitline("\tMOVQ\t"); emitoff((16 + stkcursor*8): i64); emitline("(BP), AX\n"); @@ -21645,7 +21806,7 @@ fn cgfnparams(c: *cgen, params: *node) void = { }; } else { localaddstack(c, nm, p.lhs, 16 + stkcursor*8); - stkcursor += 2; + stkcursor += 3; };}; } else { let stsz: i32 = structparamsize(c, p.lhs); if (stsz > 0) { diff --git a/selfhost/cmd/wcc/cgendecl.ww b/selfhost/cmd/wcc/cgendecl.ww index 745d79a3..39dc5448 100644 --- a/selfhost/cmd/wcc/cgendecl.ww +++ b/selfhost/cmd/wcc/cgendecl.ww @@ -222,11 +222,11 @@ fn cgfnparams(c: *cgen, params: *node) void = { stkcursor += 3; };}; } else { if (isstrtype(c, p.lhs)) { - if (idx + 2 <= 6) { - // #60: route str-param slot width through the - // primtypesize SSoT so #1's ty_str bump propagates - // here (parent #43 covered the reg-fill site only - // inside cgexpr). + if (idx + 3 <= 6) { + // str IS []u8: 3-word param (ptr,len,cap), same as + // the slice arm above (#1/Phase 3). #60: route slot + // width through the primtypesize SSoT so #1's ty_str + // bump propagates here. let off: i32 = localadd(c, nm, primtypesize("str"): i32, p.lhs); emitline("\tMOVQ\t"); emitline(argregname(idx)); @@ -240,11 +240,14 @@ fn cgfnparams(c: *cgen, params: *node) void = { emitoff((off + 8): i64); emitline("(BP)\n"); idx += 1; + emitline("\tMOVQ\t"); + emitline(argregname(idx)); + emitline(", "); + emitoff((off + 16): i64); + emitline("(BP)\n"); + idx += 1; } else { if (idx < 6) { - // Partial-fit stitch — mirrors tagged at lines - // 440-469. Only idx=5 hits this (nw=2, - // regs_left=1): ptr lands in R9, len at - // +16+stkcursor*8(BP). + // Partial-fit stitch — mirrors the slice arm above. // #60: same SSoT routing as the regs-fit arm above. let off: i32 = localadd(c, nm, primtypesize("str"): i32, p.lhs); let regs_left: i32 = 6 - idx; @@ -258,7 +261,7 @@ fn cgfnparams(c: *cgen, params: *node) void = { idx += 1; w += 1; }; - for (w < 2) { + for (w < 3) { emitline("\tMOVQ\t"); emitoff((16 + stkcursor*8): i64); emitline("(BP), AX\n"); @@ -270,7 +273,7 @@ fn cgfnparams(c: *cgen, params: *node) void = { }; } else { localaddstack(c, nm, p.lhs, 16 + stkcursor*8); - stkcursor += 2; + stkcursor += 3; };}; } else { let stsz: i32 = structparamsize(c, p.lhs); if (stsz > 0) { diff --git a/selfhost/cmd/wcc/cgenexpr.ww b/selfhost/cmd/wcc/cgenexpr.ww index 1abc205e..7a47fd38 100644 --- a/selfhost/cmd/wcc/cgenexpr.ww +++ b/selfhost/cmd/wcc/cgenexpr.ww @@ -185,7 +185,11 @@ fn cgtryprop(c: *cgen, n: *node) void = { }; }; if (succisstr) { + // str IS []u8: success arrives DX=ptr, CX=len, R8=cap + // (slot 32B). Move len out before cap overwrites CX + // (#1/Phase 3). emitline("\tMOVQ\tCX, BX\n"); + emitline("\tMOVQ\tR8, CX\n"); }; emitline("\tMOVQ\tDX, AX\n"); return; @@ -241,7 +245,11 @@ fn cgtryunw(c: *cgen, n: *node) void = { }; }; if (succisstr) { + // str IS []u8: success arrives DX=ptr, CX=len, R8=cap + // (slot 32B). Move len out before cap overwrites CX + // (#1/Phase 3). emitline("\tMOVQ\tCX, BX\n"); + emitline("\tMOVQ\tR8, CX\n"); }; emitline("\tMOVQ\tDX, AX\n"); return; @@ -514,8 +522,9 @@ fn cgcast(c: *cgen, n: *node) void = { }; fn cgstrlit(c: *cgen, n: *node) void = { - // Result is the (ptr, len) pair: ptr in AX, len in BX. Call - // sites that expect a str arg pick these up directly. + // str IS []u8: the (ptr, len, cap) triple — ptr in AX, len in BX, + // cap in CX. A static literal has no spare storage, so cap = len + // (#1/Phase 3). Call sites that expect a str arg pick these up. let nstr: str = n.str; let lab: str = internstrlit(c, nstr); emitline("\tLEAQ\t"); @@ -524,6 +533,9 @@ fn cgstrlit(c: *cgen, n: *node) void = { emitline("\tMOVQ\t$"); emitint(nstr.len: i64); emitline(", BX\n"); + emitline("\tMOVQ\t$"); + emitint(nstr.len: i64); + emitline(", CX\n"); return; }; @@ -559,9 +571,14 @@ fn cgident(c: *cgen, n: *node) void = { emitoff(off: i64); emitline("(BP), AX\n"); if (isstr) { + // str IS []u8: load (ptr,len,cap) into AX/BX/CX, + // identical to the slice arm below (#1/Phase 3). emitline("\tMOVQ\t"); emitoff((off + 8): i64); emitline("(BP), BX\n"); + emitline("\tMOVQ\t"); + emitoff((off + 16): i64); + emitline("(BP), CX\n"); }; if (issl) { emitline("\tMOVQ\t"); @@ -591,6 +608,11 @@ fn cgident(c: *cgen, n: *node) void = { emitline("\tMOVQ\t$"); emitint(bytes.len: i64); emitline(", BX\n"); + // str IS []u8: cap = len for a static def literal + // (#1/Phase 3). + emitline("\tMOVQ\t$"); + emitint(bytes.len: i64); + emitline(", CX\n"); return; }; }; @@ -622,17 +644,16 @@ fn cgident(c: *cgen, n: *node) void = { let isstr: bool = letvarisstr(c, nm); let issl: bool = letvarisslice(c, nm); if (isstr || issl) { + // str IS []u8: both str and slice carry a third 8B + // (cap); load it unconditionally. The address holder CX + // is overwritten by the cap as the last step, after + // ptr/len are already loaded (#1/Phase 3). emitline("\tLEAQ\t"); emitsymname(c, nm); emitline("(SB), CX\n"); emitline("\tMOVQ\t(CX), AX\n"); emitline("\tMOVQ\t8(CX), BX\n"); - if (issl) { - // Overwrites the address holder with the - // cap as the last step — CX is no longer - // needed once both ptr/len are loaded. - emitline("\tMOVQ\t16(CX), CX\n"); - }; + emitline("\tMOVQ\t16(CX), CX\n"); return; }; // Float global: same LEAQ-indirect shape, since MOVSS/ @@ -686,6 +707,12 @@ fn cgindex(c: *cgen, n: *node) void = { let idx: *node = n.rhs; let esz: i32 = 8; let signed_elem: bool = false; + // #1/Phase 3: str=24B collides with slice=24B, so the str-element + // branches below MUST gate on kind (mirroring cstage's elem_is_str), + // not a bare `esz == primtypesize("str")` size check — otherwise a + // []u8 element (also 24B) misfires into the str 2-word load and + // diverges from cstage (#60 collision class; sentinel 754). + let elemisstr: bool = false; let baselocal: *local = nil; // Global `[N]T` array or `*T` pointer used as an index base. // The local-ident lookup above misses it; we need LEAQ name(SB) @@ -727,7 +754,7 @@ fn cgindex(c: *cgen, n: *node) void = { // cgen.c:3517-18). esz-only — N_DOT-base signedness // stays unset, as before. let dt: *tinfo = n.type_: *tinfo; - if (dt != nil) { esz = dt.size: i32; }; + if (dt != nil) { esz = dt.size: i32; elemisstr = typeisstr(dt); }; } else { if (base.kind == nkind.N_INDEX) { // #60: chained `names[i][k]` — n.type_ is the checker- // stamped outer element tinfo (indexresult over the inner @@ -776,6 +803,7 @@ fn cgindex(c: *cgen, n: *node) void = { esz = elem_slot_sz; }; }; + elemisstr = isstrtype(c, etn); }; }; cgexpr(c, idx); @@ -811,7 +839,7 @@ fn cgindex(c: *cgen, n: *node) void = { }; // #43: str element-stride routes through primtypesize so the // 16-vs-24 dispatch tracks ty_str.size for #1. - if (esz == primtypesize("str"): i32) { + if (elemisstr) { emitline("\tMOVQ\t8(BX), CX\n"); emitline("\tMOVQ\t(BX), AX\n"); emitline("\tMOVQ\tCX, BX\n"); @@ -853,7 +881,7 @@ fn cgindex(c: *cgen, n: *node) void = { // str element (16B today): load (ptr, len) into (AX, BX) so // the value flows through the str-rhs convention. // #43: route via primtypesize so the stride tracks #1. - if (esz == primtypesize("str"): i32) { + if (elemisstr) { emitline("\tMOVQ\t8(BX), CX\n"); emitline("\tMOVQ\t(BX), AX\n"); emitline("\tMOVQ\tCX, BX\n"); @@ -885,7 +913,7 @@ fn cgindex(c: *cgen, n: *node) void = { }; // #43: str element-stride routes through primtypesize so the // 16-vs-24 dispatch tracks ty_str.size for #1. - if (esz == primtypesize("str"): i32) { + if (elemisstr) { emitline("\tMOVQ\t8(AX), BX\n"); emitline("\tMOVQ\t(AX), AX\n"); return; @@ -1480,10 +1508,11 @@ fn cgdot(c: *cgen, n: *node) void = { }; // Hare-style tuple positional access: `t.0`, `t.1`. // Walk the tuple element type list summing slotsize - // (matches the (scalar, str) init layout which puts - // the scalar in an 8B slot and the str in 16B). For - // a str element, load both halves into (AX, BX) so - // chains like `t.1.len` propagate correctly. + // (matches the (scalar, str) init layout: scalar in an + // 8B slot, str in 24B — str IS []u8, #1/Phase 3). For a + // str element, load (ptr, len) into (AX, BX); the cap + // stays in the slot (the 2-word str-field read, like + // every other chained/dot str leaf read — task #14). if (lkind == nkind.N_TTUPLE) { let idx: i32 = fldnumidx(fld); if (idx >= 0) { @@ -1598,9 +1627,9 @@ fn cgdot(c: *cgen, n: *node) void = { let delta: i32 = -1; if (streq(fld, "ptr")) { delta = 0; }; if (streq(fld, "len")) { delta = 8; }; - if (issl) { - if (streq(fld, "cap")) { delta = 16; }; - }; + // str IS []u8: .cap is valid on a str global too, + // not slice-only — mirrors cstage (#1/Phase 3, #11). + if (streq(fld, "cap")) { delta = 16; }; if (delta >= 0) { emitline("\tLEAQ\t"); emitsymname(c, lhs.str); @@ -2456,7 +2485,9 @@ fn cgun(c: *cgen, n: *node) void = { let gdelta: i32 = -1; if (streq(fld, "ptr")) { gdelta = 0; }; if (streq(fld, "len")) { gdelta = 8; }; - if (issl) { if (streq(fld, "cap")) { gdelta = 16; }; }; + // str IS []u8: &str.cap is valid too, not slice-only + // — mirrors cstage (#1/Phase 3, #11). + if (streq(fld, "cap")) { gdelta = 16; }; if (gdelta >= 0) { emitline("\tLEAQ\t"); emitsymname(c, basenm); @@ -2828,16 +2859,19 @@ fn cgalloc(c: *cgen, n: *node) void = { emitline("\n"); fi = nil; } else { if (isstrtype(c, fi.tnode)) { - // alloc(T{ fval = s }) for str field: cgexpr - // leaves (AX=ptr, BX=len). Use CX for the heap - // base so BX=len survives both stores. Mirrors - // cmd/w6c/cgen.c:4184-4190. - emitline("\tMOVQ\t(SP), CX\n"); + // str IS []u8: cgexpr leaves (AX=ptr, + // BX=len, CX=cap). Route the heap base + // through DX so all three survive — CX + // holds cap, BX holds len (#1/Phase 3). + emitline("\tMOVQ\t(SP), DX\n"); emitline("\tMOVQ\tAX, "); - emitdispreg(fi.foff: i64, "CX"); + emitdispreg(fi.foff: i64, "DX"); emitline("\n"); emitline("\tMOVQ\tBX, "); - emitdispreg((fi.foff + 8): i64, "CX"); + emitdispreg((fi.foff + 8): i64, "DX"); + emitline("\n"); + emitline("\tMOVQ\tCX, "); + emitdispreg((fi.foff + 16): i64, "DX"); emitline("\n"); fi = nil; } else { @@ -3297,7 +3331,8 @@ fn cgcall(c: *cgen, n: *node) void = { popped += 1; } else { let extra: i32 = 0; - if (nodeisstr(c, a)) { extra = 1; }; + // str IS []u8: 3-word arg, same as slice (#1/Phase 3). + if (nodeisstr(c, a)) { extra = 2; }; if (nodeisslice(c, a)) { extra = 2; }; // #21: tagged-CALL arg was pushed AX/DX/CX/R8 high→low // by pushargsrev; size the per-arg pop to match so the @@ -3467,31 +3502,9 @@ fn cgcall(c: *cgen, n: *node) void = { emitint((stackslots * 8): i64); emitline(", SP\n"); }; - // SysV returns 16-byte aggregates in (AX, DX). Our str - // convention is (AX, BX), so shuffle for str-returning calls. - // Route through fnretlookupmod: for N_DOT cross-module callees, - // the bare-leaf fnretlookup's same-module-first walk (#4e) would - // pick the caller-module's same-leaf fn — a str-returning - // caller-side `slice` over a []u8-returning `mod.slice` then - // emits a phantom MOVQ DX, BX after the cross-module CALL (#34). - if (calleename.len > 0) { - let cmod: str; - cmod.ptr = nil; cmod.len = 0; - if (callee != nil) { - if (callee.kind == nkind.N_IDENT) { cmod = c.curmod; }; - if (callee.kind == nkind.N_DOT) { - if (callee.lhs != nil) { - if (callee.lhs.kind == nkind.N_IDENT) { - cmod = callee.lhs.str; - }; - }; - }; - }; - let rtyp: *node = fnretlookupmod(c, calleename, cmod); - if (isstrtype(c, rtyp)) { - emitline("\tMOVQ\tDX, BX\n"); - }; - }; + // str IS []u8: a str-returning callee leaves AX=ptr, BX=len, + // CX=cap — same as a slice, so there is no receive-side shuffle + // (#1/Phase 3). return; }; @@ -3590,17 +3603,21 @@ fn cgassign(c: *cgen, n: *node) void = { emitline("\tX0, (BX)\n"); return; }; - // Push order matches C cgen - // (cmd/w6c/cgen.c:1033-1041): PUSHQ AX - // (ptr) first, then PUSHQ BX (len) if - // str, so the pop sequence is POP CX - // (len) → POP AX (ptr) → MOVQ AX, - // (BX) → MOVQ CX, 8(BX). + // str IS []u8: PUSHQ AX (ptr) first, then + // PUSHQ BX (len) + PUSHQ CX (cap) across the + // pointer eval which clobbers BX/CX. Pop drains + // cap (top) → 16(BX), then len, then ptr → 0(BX) + // with len → 8(BX) (#1/Phase 3). emitline("\tPUSHQ\tAX\n"); - if (elemstr) { emitline("\tPUSHQ\tBX\n"); }; + if (elemstr) { + emitline("\tPUSHQ\tBX\n"); + emitline("\tPUSHQ\tCX\n"); + }; cgexpr(c, inner); emitline("\tMOVQ\tAX, BX\n"); if (elemstr) { + emitline("\tPOPQ\tCX\n"); + emitline("\tMOVQ\tCX, 16(BX)\n"); emitline("\tPOPQ\tCX\n"); emitline("\tPOPQ\tAX\n"); emitline("\tMOVQ\tAX, (BX)\n"); @@ -3854,10 +3871,19 @@ fn cgassign(c: *cgen, n: *node) void = { }; }; cgexpr(c, n.rhs); // value → AX - // #43: spill BX (str.len) before computing - // the index so the post-index store can pop - // it; the stride gate tracks ty_str.size. - if (esz == primtypesize("str"): i32) { emitline("\tPUSHQ\tBX\n"); }; + // str IS []u8: spill cap (CX) + len (BX) before + // computing the index so the post-index store can + // pop all three. #1/Phase 3: str=24B collides with + // slice=24B, so this MUST gate on kind (cstage's + // elem_is_str, cmd/w6c/cgen.c:3576) — not a bare + // `esz == primtypesize("str")` — or a []u8 element + // (also 24B) misfires the str 3-word store and + // diverges from cstage. Write-side mirror of the + // cgindex read-path gate (#7/754). + if (isstrtype(c, elemtn)) { + emitline("\tPUSHQ\tCX\n"); + emitline("\tPUSHQ\tBX\n"); + }; emitline("\tPUSHQ\tAX\n"); cgexpr(c, idx); // idx → AX if (esz > 1) { @@ -3895,13 +3921,15 @@ fn cgassign(c: *cgen, n: *node) void = { emitline("\tPOPQ\tAX\n"); // scaled idx emitline("\tADDQ\tAX, BX\n"); emitline("\tPOPQ\tAX\n"); // value - // #43: str-element write — pop the saved - // .len and store both halves. Stride gate - // routes through primtypesize for #1. - if (esz == primtypesize("str"): i32) { + // str IS []u8: pop the saved len + cap and store + // all three words. Kind-gate, not size — see the + // spill site above (#1/Phase 3, #7/754). + if (isstrtype(c, elemtn)) { emitline("\tMOVQ\tAX, (BX)\n"); emitline("\tPOPQ\tCX\n"); emitline("\tMOVQ\tCX, 8(BX)\n"); + emitline("\tPOPQ\tCX\n"); + emitline("\tMOVQ\tCX, 16(BX)\n"); return; }; let isop: str = tnodestoreop(c, elemtn, esz); @@ -4328,20 +4356,24 @@ fn cgassign(c: *cgen, n: *node) void = { emitline("\tMOVQ\tBX, AX\n"); }; }; - // str field via *struct: rhs left - // (AX=ptr, BX=len). Use CX as the - // address scratch so we don't clobber - // the len half before storing it. + // str IS []u8: rhs left (AX=ptr, + // BX=len, CX=cap). CX holds cap, so + // stage the struct addr in DX and + // store all three words — identical + // to the slice arm below (#1/Phase 3). if (n.op == tkind.TK_ASSIGN) { if (isstrtype(c, fi.tnode)) { emitline("\tMOVQ\t"); emitoff(lc.off: i64); - emitline("(BP), CX\n"); + emitline("(BP), DX\n"); emitline("\tMOVQ\tAX, "); - emitdispreg(fi.foff: i64, "CX"); + emitdispreg(fi.foff: i64, "DX"); emitline("\n"); emitline("\tMOVQ\tBX, "); - emitdispreg((fi.foff + 8): i64, "CX"); + emitdispreg((fi.foff + 8): i64, "DX"); + emitline("\n"); + emitline("\tMOVQ\tCX, "); + emitdispreg((fi.foff + 16): i64, "DX"); emitline("\n"); return; }; @@ -4532,11 +4564,11 @@ fn cgassign(c: *cgen, n: *node) void = { };}; }; cgexpr(c, n.rhs); - // str field: cgexpr left (AX=ptr, BX=len); - // store both halves at +0/+8. Without this, - // `L.src = s` would only write the ptr and - // `L.src.len` would carry whatever was on the - // stack. + // str IS []u8: cgexpr left (AX=ptr, + // BX=len, CX=cap); store all three at + // +0/+8/+16, identical to the slice + // arm below. BP base, no scratch + // reload needed (#1/Phase 3). if (isstrtype(c, fi.tnode)) { emitline("\tMOVQ\tAX, "); emitoff((lc.off + fi.foff): i64); @@ -4544,6 +4576,9 @@ fn cgassign(c: *cgen, n: *node) void = { emitline("\tMOVQ\tBX, "); emitoff((lc.off + fi.foff + 8): i64); emitline("(BP)\n"); + emitline("\tMOVQ\tCX, "); + emitoff((lc.off + fi.foff + 16): i64); + emitline("(BP)\n"); return; }; // slice field direct: cgexpr left @@ -4791,14 +4826,22 @@ fn cgassign(c: *cgen, n: *node) void = { if (n.op == tkind.TK_ASSIGN) { cgexpr(c, n.rhs); if (isstrtype(c, fi.tnode)) { + // str IS []u8: cgexpr left + // (AX=ptr, BX=len, CX=cap). CX + // holds cap, so stage the base + // addr in DX and store all three + // words (#1/Phase 3). emitline("\tLEAQ\t"); emitsymname(c, bn); - emitline("(SB), CX\n"); + emitline("(SB), DX\n"); emitline("\tMOVQ\tAX, "); - emitdispreg(fi.foff: i64, "CX"); + emitdispreg(fi.foff: i64, "DX"); emitline("\n"); emitline("\tMOVQ\tBX, "); - emitdispreg((fi.foff + 8): i64, "CX"); + emitdispreg((fi.foff + 8): i64, "DX"); + emitline("\n"); + emitline("\tMOVQ\tCX, "); + emitdispreg((fi.foff + 16): i64, "DX"); emitline("\n"); return; }; @@ -5037,22 +5080,29 @@ fn cgassign(c: *cgen, n: *node) void = { return; }; if (typeisstr(leaftype)) { + // str IS []u8: store ptr/len/cap. cgexpr leaves + // CX=cap, so the viacx base goes in DX (not CX) to + // avoid clobbering it — same as the single-dot str + // field store (#1/Phase 3). cgexpr(c, n.rhs); if (viacx) { if (ptrroot) { emitline("\tMOVQ\t"); emitoff(rootoff: i64); - emitline("(BP), CX\n"); + emitline("(BP), DX\n"); } else { emitline("\tLEAQ\t"); emitsymname(c, rootname); - emitline("(SB), CX\n"); + emitline("(SB), DX\n"); }; emitline("\tMOVQ\tAX, "); - emitdispreg(totaloff: i64, "CX"); + emitdispreg(totaloff: i64, "DX"); emitline("\n"); emitline("\tMOVQ\tBX, "); - emitdispreg((totaloff + 8): i64, "CX"); + emitdispreg((totaloff + 8): i64, "DX"); + emitline("\n"); + emitline("\tMOVQ\tCX, "); + emitdispreg((totaloff + 16): i64, "DX"); emitline("\n"); } else { emitline("\tMOVQ\tAX, "); @@ -5061,6 +5111,9 @@ fn cgassign(c: *cgen, n: *node) void = { emitline("\tMOVQ\tBX, "); emitoff((rootoff + totaloff + 8): i64); emitline("(BP)\n"); + emitline("\tMOVQ\tCX, "); + emitoff((rootoff + totaloff + 16): i64); + emitline("(BP)\n"); }; return; }; @@ -5548,11 +5601,17 @@ fn cgassign(c: *cgen, n: *node) void = { cgexpr(c, n.rhs); if (n.op == tkind.TK_ASSIGN) { if (letvarisstr(c, nm)) { + // str IS []u8: stash cap in DI before LEAQ + // overwrites CX, then store ptr/len/cap — + // identical to the slice arm below + // (#1/Phase 3). + emitline("\tMOVQ\tCX, DI\n"); emitline("\tLEAQ\t"); emitsymname(c, nm); emitline("(SB), CX\n"); emitline("\tMOVQ\tAX, (CX)\n"); emitline("\tMOVQ\tBX, 8(CX)\n"); + emitline("\tMOVQ\tDI, 16(CX)\n"); return; }; if (letvarisslice(c, nm)) { @@ -5838,7 +5897,9 @@ fn cgassign(c: *cgen, n: *node) void = { emitoff((off + 8): i64); emitline("(BP)\n"); }; - if (lcsl) { + // str IS []u8: store the cap word too, identical to + // the slice store (#1/Phase 3). + if (lcstr || lcsl) { emitline("\tMOVQ\tCX, "); emitoff((off + 16): i64); emitline("(BP)\n"); diff --git a/selfhost/cmd/wcc/cgenstmt.ww b/selfhost/cmd/wcc/cgenstmt.ww index 80143f10..07bd06af 100644 --- a/selfhost/cmd/wcc/cgenstmt.ww +++ b/selfhost/cmd/wcc/cgenstmt.ww @@ -111,12 +111,16 @@ fn cgreturn(c: *cgen, n: *node) void = { rundefers(c); let rhs: *node = n.lhs; if (rhs != nil) { - // Tuple return `return a, b;`: + // Tuple return `return a, b;`, word-indexed AX→DX→CX→R8 (the + // SAME register sequence as the tagged-union return below; the + // tuple just fills it positionally): // (scalar, scalar) — AX = v0, DX = v1. // (scalar, str) / (str, scalar) — AX = scalar elem, - // DX = str.ptr, CX = str.len. - // 24B convention mirrors the tagged-union return below; receive - // sites destructure off the same regs regardless of position. + // DX = str.ptr, CX = str.len, R8 = str.cap. + // str IS []u8 (24B) → 32B tuple; cap rides R8, matching the + // tagged-union return that already uses R8 for slot+24 + // (#1/Phase 3, task #5). Receive sites destructure off the + // same regs regardless of position. if (rhs.kind == nkind.N_TUPLE) { let v: *node = rhs.list; if (v != nil) { @@ -131,6 +135,7 @@ fn cgreturn(c: *cgen, n: *node) void = { cgexpr(c, scaln); emitline("\tPUSHQ\tAX\n"); cgexpr(c, strn); + emitline("\tMOVQ\tCX, R8\n"); emitline("\tMOVQ\tBX, CX\n"); emitline("\tMOVQ\tAX, DX\n"); emitline("\tPOPQ\tAX\n"); @@ -283,12 +288,12 @@ fn cgreturn(c: *cgen, n: *node) void = { emitline("\tMOVQ\tBX, CX\n"); emitline("\tMOVQ\tAX, DX\n"); } else { if (nodeisstr(c, rhs)) { + // str IS []u8: cgexpr leaves (AX=ptr, BX=len, + // CX=cap). Same shuffle as the slice arm above — + // DX=ptr, CX=len, R8=cap (#1/Phase 3). + emitline("\tMOVQ\tCX, R8\n"); emitline("\tMOVQ\tBX, CX\n"); emitline("\tMOVQ\tAX, DX\n"); - // str fills DX,CX. Zero R8 if dst covers slot+24. - if (rsz > 24) { - emitline("\tMOVQ\t$0, R8\n"); - }; } else { emitline("\tMOVQ\tAX, DX\n"); // scalar fills DX only. Zero CX / R8 if dst @@ -544,11 +549,8 @@ fn cgreturn(c: *cgen, n: *node) void = { }; emitline("\tMOVQ\t$0, AX\n"); }; - // SysV: 16-byte aggregates (str, 2-tuple) return in (AX, DX). - // cgexpr leaves str in (AX, BX); shuffle BX→DX. - if (isstrtype(c, c.fnret)) { - emitline("\tMOVQ\tBX, DX\n"); - }; + // str IS []u8: cgexpr leaves AX=ptr, BX=len, CX=cap — str now + // returns exactly like a slice, no AX:DX shuffle (#1/Phase 3). emitline("\tMOVQ\tBP, SP\n"); emitline("\tPOPQ\tBP\n"); emitline("\tRET\n"); @@ -719,11 +721,13 @@ fn cglet(c: *cgen, n: *node) void = { c.lastwasreturn = 0; return; }; - // 24B tuple init for `let t: (scalar, str) = call()` / - // `let t: (str, scalar) = call()`. Per the AX:DX:CX return - // convention: AX = scalar elem, DX = str.ptr, CX = str.len. - // Layout is positional, so we route each register to the - // slot dictated by element type, not by AX/DX position. + // 32B tuple init for `let t: (scalar, str) = call()` / + // `let t: (str, scalar) = call()`. Per the AX:DX:CX:R8 return + // convention: AX = scalar elem, DX = str.ptr, CX = str.len, + // R8 = str.cap. Layout is positional (str takes 24B at its + // position), so we route each register to the slot dictated by + // element type, not by AX/DX position. str IS []u8 (24B) → 32B + // tuple (#1/Phase 3, task #5). if (n.lhs != nil) { if (n.lhs.kind == nkind.N_TTUPLE) { let p0: *node = n.lhs.list; @@ -746,9 +750,12 @@ fn cglet(c: *cgen, n: *node) void = { emitline("\tMOVQ\tCX, "); emitoff((off + 8): i64); emitline("(BP)\n"); - emitline("\tMOVQ\tAX, "); + emitline("\tMOVQ\tR8, "); emitoff((off + 16): i64); emitline("(BP)\n"); + emitline("\tMOVQ\tAX, "); + emitoff((off + 24): i64); + emitline("(BP)\n"); } else { emitline("\tMOVQ\tAX, "); emitoff(off: i64); @@ -759,6 +766,9 @@ fn cglet(c: *cgen, n: *node) void = { emitline("\tMOVQ\tCX, "); emitoff((off + 16): i64); emitline("(BP)\n"); + emitline("\tMOVQ\tR8, "); + emitoff((off + 24): i64); + emitline("(BP)\n"); }; c.lastwasreturn = 0; return; @@ -1049,15 +1059,19 @@ fn cglet(c: *cgen, n: *node) void = { emitline("\tMOVQ\tAX, "); emitoff(off: i64); emitline("(BP)\n"); - // str init: cgexpr also leaves len in BX; store both. + // str IS []u8: cgexpr leaves (ptr,len,cap) in AX/BX/CX; store + // all three, same as the slice arm below (#1/Phase 3). // #60: gate by kind too — under #1's str=24 bump, sizeof(str) // and sizeof(slice) collide, so a bare `sz ==` check fires - // both branches for one let. Mirrors cstage cgen.c:6439's + // both branches for one let. Mirrors cstage cgen.c's // `type_isstr(lt) && sz == ty_str->size` shape. if (isstrtype(c, tn) && sz == primtypesize("str"): i32) { emitline("\tMOVQ\tBX, "); emitoff((off + 8): i64); emitline("(BP)\n"); + emitline("\tMOVQ\tCX, "); + emitoff((off + 16): i64); + emitline("(BP)\n"); }; // slice init: ptr/len/cap in AX/BX/CX. Same kind+size gate as // the str arm — without the kind check this fires on a str let @@ -1218,11 +1232,12 @@ fn cgmassign(c: *cgen, n: *node) void = { // type is taken from its explicit annotation (l.lhs) when present // or inferred from the called fn's return-type tuple element. // -// Per the AX:DX:CX return convention (mirrors C cgen nkind.N_MLET): +// Per the AX:DX:CX:R8 return convention (mirrors C cgen nkind.N_MLET): // (scalar, scalar) — AX → l0, DX → l1. -// (scalar, str) — AX → scalar slot, (DX, CX) → str slot -// as (.ptr, .len). Position-agnostic — the +// (scalar, str) — AX → scalar slot, (DX, CX, R8) → str slot +// as (.ptr, .len, .cap). Position-agnostic — the // regs are routed by element type, not by AX/DX. +// str IS []u8 (24B): cap rides R8 (#1/Phase 3, task #5). fn cgmlet(c: *cgen, n: *node) void = { let rhs: *node = n.rhs; if (rhs == nil) { return; }; @@ -1291,16 +1306,21 @@ fn cgmlet(c: *cgen, n: *node) void = { let off0: i32 = localadd(c, l0.str, sz0, t0); let off1: i32 = localadd(c, l1.str, sz1, t1); if (s0_is_str) { + // l0 str: ptr=DX, len=CX, cap=R8. l1 scalar = AX. emitline("\tMOVQ\tDX, "); emitoff(off0: i64); emitline("(BP)\n"); emitline("\tMOVQ\tCX, "); emitoff((off0 + 8): i64); emitline("(BP)\n"); + emitline("\tMOVQ\tR8, "); + emitoff((off0 + 16): i64); + emitline("(BP)\n"); emitline("\tMOVQ\tAX, "); emitoff(off1: i64); emitline("(BP)\n"); } else { + // l0 scalar; l1 str: ptr=DX, len=CX, cap=R8. emitline("\tMOVQ\tAX, "); emitoff(off0: i64); emitline("(BP)\n"); @@ -1310,6 +1330,9 @@ fn cgmlet(c: *cgen, n: *node) void = { emitline("\tMOVQ\tCX, "); emitoff((off1 + 8): i64); emitline("(BP)\n"); + emitline("\tMOVQ\tR8, "); + emitoff((off1 + 16): i64); + emitline("(BP)\n"); }; c.lastwasreturn = 0; return; diff --git a/selfhost/cmd/wcc/cgenutil.ww b/selfhost/cmd/wcc/cgenutil.ww index ad87d2d4..bd068c89 100644 --- a/selfhost/cmd/wcc/cgenutil.ww +++ b/selfhost/cmd/wcc/cgenutil.ww @@ -249,8 +249,11 @@ fn pushargsrev(c: *cgen, arg: *node, param: *node) i32 = { emitline(", AX\n"); emitline("\tPUSHQ\tAX\n"); } else { if (nodeisstr(c, arg)) { - // slot 24: [+0]=tag,[+8]=ptr,[+16]=len. Push high→low - // so pop drains tag first into arg-reg[0]. + // str IS []u8: slot 32 [+0]=tag,[+8]=ptr,[+16]=len, + // [+24]=cap — same shape as the slice arm above. Push + // cap, len, ptr, tag high→low so pop drains tag first + // into arg-reg[0] (#1/Phase 3). + emitline("\tPUSHQ\tCX\n"); emitline("\tPUSHQ\tBX\n"); emitline("\tPUSHQ\tAX\n"); emitline("\tMOVQ\t$"); @@ -468,9 +471,12 @@ fn pushargsrev(c: *cgen, arg: *node, param: *node) i32 = { return rest + 3; }; if (nodeisstr(c, arg)) { + // str IS []u8: cgexpr left (AX=ptr, BX=len, CX=cap). Push + // the triple, same as the slice arm above (#1/Phase 3). + emitline("\tPUSHQ\tCX\n"); emitline("\tPUSHQ\tBX\n"); emitline("\tPUSHQ\tAX\n"); - return rest + 2; + return rest + 3; }; // #21: CALL returning a tagged-union — the aistagged guard // above kept us out of the widening path. Push the tagged- @@ -2320,7 +2326,7 @@ fn cgloadtaggedfield(c: *cgen, basereg: str, foff: i32, slot_sz: i32) void = { // would need a reversed direction we don't currently emit). // - struct src (literal or ident): zero slot, write fields at +8+foff, // tag last. -// - str src: tag@+0, ptr@+8, len@+16. +// - str src: tag@+0, ptr@+8, len@+16, cap@+24 (str IS []u8, #1/Phase 3). // - scalar src: tag@+0, value@+8. fn cgwidentaggedstore(c: *cgen, dst: *tinfo, src: *node, basereg: str, slot_off: i32, slot_sz: i32) void = { @@ -2538,12 +2544,18 @@ fn cgwidentaggedstorebp(c: *cgen, dst: *tinfo, src: *node, slot_off: i32, slot_s emitoff((slot_off + 8 + fi.foff): i64); emitline("(BP)\n"); } else { if (isstrtype(c, fi.tnode)) { + // str IS []u8: 3-word field + // (ptr,len,cap) from cgexpr's + // AX/BX/CX (#1/Phase 3). emitline("\tMOVQ\tAX, "); emitoff((slot_off + 8 + fi.foff): i64); emitline("(BP)\n"); emitline("\tMOVQ\tBX, "); emitoff((slot_off + 8 + fi.foff + 8): i64); emitline("(BP)\n"); + emitline("\tMOVQ\tCX, "); + emitoff((slot_off + 8 + fi.foff + 16): i64); + emitline("(BP)\n"); } else { let sop: str = fieldstoreop(c, fi); emitline("\t"); @@ -2601,7 +2613,9 @@ fn cgwidentaggedstorebp(c: *cgen, dst: *tinfo, src: *node, slot_off: i32, slot_s return; }; }; - // Str payload. + // str IS []u8: AX=ptr, BX=len, CX=cap from cgexpr. Slot layout + // tag@+0, ptr@+8, len@+16, cap@+24 — same 32B shape as the slice + // payload below (#1/Phase 3). if (nodeisstr(c, src)) { cgexpr(c, src); emitline("\tMOVQ\tAX, "); @@ -2610,6 +2624,9 @@ fn cgwidentaggedstorebp(c: *cgen, dst: *tinfo, src: *node, slot_off: i32, slot_s emitline("\tMOVQ\tBX, "); emitoff((slot_off + 16): i64); emitline("(BP)\n"); + emitline("\tMOVQ\tCX, "); + emitoff((slot_off + 24): i64); + emitline("(BP)\n"); let tag: i32 = taggedvariantindext(c, dt, src); if (tag < 0) { tag = 0; }; emitline("\tMOVQ\t$"); @@ -2837,11 +2854,15 @@ export fn dotchainresolve(c: *cgen, n: *node, for (ft != nil && ft.kind == tykind.TY_NAMED) { ft = ft.under; }; if (ft == nil) { return false; }; if (ft.kind == tykind.TY_STR) { + // str IS []u8: .cap is the third header word, same as + // the TY_SLICE leaf below — cstage treats str≡slice for + // .ptr/.len/.cap (cmd/w6c/cgen.c:2478) (#1/Phase 3, #11). if (i != 1) { return false; }; let pseudo: str = stk[0].str; let delta: i32 = -1; if (streq(pseudo, "ptr")) { delta = 0; } - else { if (streq(pseudo, "len")) { delta = 8; }; }; + else { if (streq(pseudo, "len")) { delta = 8; } + else { if (streq(pseudo, "cap")) { delta = 16; }; }; }; if (delta < 0) { return false; }; *outtotaloff = *outtotaloff + foff; *outslicedelta = delta; @@ -3154,6 +3175,44 @@ fn cgstructlitfill(c: *cgen, si: *structinfo, lit: *node, fi = nil; } else if (callwhole) { fi = nil; + } else if (isstrtype(c, fi.tnode)) { + // str IS []u8: 3-word field (ptr,len,cap). + // cgexpr leaves AX/BX/CX; for non-BP modes + // the dst base goes in DX to dodge BX=len / + // CX=cap (the generic store reloads BX, which + // would clobber len) (#1/Phase 3). + cgexpr(c, fieldnode.lhs); + if (mode == 0) { + emitline("\tMOVQ\tAX, "); + emitoff((disp + fi.foff): i64); + emitline("(BP)\n"); + emitline("\tMOVQ\tBX, "); + emitoff((disp + fi.foff + 8): i64); + emitline("(BP)\n"); + emitline("\tMOVQ\tCX, "); + emitoff((disp + fi.foff + 16): i64); + emitline("(BP)\n"); + } else { + if (mode == 1) { + emitline("\tMOVQ\t"); + emitoff(srcoff: i64); + emitline("(BP), DX\n"); + } else { + emitline("\tLEAQ\t"); + emitsymname(c, srcname); + emitline("(SB), DX\n"); + }; + emitline("\tMOVQ\tAX, "); + emitdispreg((disp + fi.foff): i64, "DX"); + emitline("\n"); + emitline("\tMOVQ\tBX, "); + emitdispreg((disp + fi.foff + 8): i64, "DX"); + emitline("\n"); + emitline("\tMOVQ\tCX, "); + emitdispreg((disp + fi.foff + 16): i64, "DX"); + emitline("\n"); + }; + fi = nil; } else { cgexpr(c, fieldnode.lhs); // For non-BP modes, cgexpr just clobbered diff --git a/selfhost/cmd/wcc/check.ww b/selfhost/cmd/wcc/check.ww index f7692c5c..fd3ae73c 100644 --- a/selfhost/cmd/wcc/check.ww +++ b/selfhost/cmd/wcc/check.ww @@ -714,7 +714,9 @@ fn primtypesize(nm: str) i64 = { if (streq(nm, "i32") || streq(nm, "u32") || streq(nm, "f32") || streq(nm, "rune")) { return 4i64; }; if (streq(nm, "i64") || streq(nm, "u64") || streq(nm, "f64")) { return 8i64; }; if (streq(nm, "int") || streq(nm, "uint") || streq(nm, "uintptr")) { return 8i64; }; - if (streq(nm, "str")) { return 16i64; }; // sizelint-ok: SSoT for ty_str primtype (#64) + // str IS []u8: 24B, sourced from the slice header SSoT so str and + // []u8 can never drift; no second hardcoded 24 (#1/Phase 3). + if (streq(nm, "str")) { return tyslicesize(); }; return -1i64; }; diff --git a/selfhost/cmd/wwdump/main.combined.ww b/selfhost/cmd/wwdump/main.combined.ww index bfc37de9..82aedd43 100644 --- a/selfhost/cmd/wwdump/main.combined.ww +++ b/selfhost/cmd/wwdump/main.combined.ww @@ -6535,7 +6535,20 @@ export fn typesinit(c: *tctx) void = { c.tyuintptr= prim(tykind.TY_UINTPTR, "uintptr", 8u64, 8u64); c.tyf32 = prim(tykind.TY_F32, "f32", 4u64, 4u64); c.tyf64 = prim(tykind.TY_F64, "f64", 8u64, 8u64); - c.tystr = prim(tykind.TY_STR, "str", 16u64, 8u64); // sizelint-ok: SSoT for tystr (#64) + // str IS []u8: { *u8, len, cap } — 24B, 3-reg ABI (#1/Phase 3). + // Size sourced from a u8-slice's size (typeslice SSoT) so str and + // []u8 can never drift; no second hardcoded 24. Mirrors cstage + // type.c `type_slice(a, ty_u8)->size`. + // + // The slice tinfo MUST land in a local first: the inline form + // `typeslice(c.tyu8).size` triggers a cgen bug — `call().field` + // where the call returns a *pointer* emits no deref (it uses the + // returned pointer AS the field value), so tystr.size would become + // a heap address → runaway slot-size loops. Filed as task #6 + // (cstage cgen.c N_DOT base=N_CALL-returning-pointer + wwstage + // cgdot mirror); retained here as a local until that lands. + let u8slice: *tinfo = typeslice(c.tyu8); + c.tystr = prim(tykind.TY_STR, "str", u8slice.size, 8u64); c.tyerr = prim(tykind.TY_ERR, "", 0u64, 1u64); c.tynever = prim(tykind.TY_NEVER, "never", 0u64, 1u64); @@ -7811,7 +7824,9 @@ fn primtypesize(nm: str) i64 = { if (streq(nm, "i32") || streq(nm, "u32") || streq(nm, "f32") || streq(nm, "rune")) { return 4i64; }; if (streq(nm, "i64") || streq(nm, "u64") || streq(nm, "f64")) { return 8i64; }; if (streq(nm, "int") || streq(nm, "uint") || streq(nm, "uintptr")) { return 8i64; }; - if (streq(nm, "str")) { return 16i64; }; // sizelint-ok: SSoT for ty_str primtype (#64) + // str IS []u8: 24B, sourced from the slice header SSoT so str and + // []u8 can never drift; no second hardcoded 24 (#1/Phase 3). + if (streq(nm, "str")) { return tyslicesize(); }; return -1i64; }; @@ -10794,8 +10809,11 @@ fn pushargsrev(c: *cgen, arg: *node, param: *node) i32 = { emitline(", AX\n"); emitline("\tPUSHQ\tAX\n"); } else { if (nodeisstr(c, arg)) { - // slot 24: [+0]=tag,[+8]=ptr,[+16]=len. Push high→low - // so pop drains tag first into arg-reg[0]. + // str IS []u8: slot 32 [+0]=tag,[+8]=ptr,[+16]=len, + // [+24]=cap — same shape as the slice arm above. Push + // cap, len, ptr, tag high→low so pop drains tag first + // into arg-reg[0] (#1/Phase 3). + emitline("\tPUSHQ\tCX\n"); emitline("\tPUSHQ\tBX\n"); emitline("\tPUSHQ\tAX\n"); emitline("\tMOVQ\t$"); @@ -11013,9 +11031,12 @@ fn pushargsrev(c: *cgen, arg: *node, param: *node) i32 = { return rest + 3; }; if (nodeisstr(c, arg)) { + // str IS []u8: cgexpr left (AX=ptr, BX=len, CX=cap). Push + // the triple, same as the slice arm above (#1/Phase 3). + emitline("\tPUSHQ\tCX\n"); emitline("\tPUSHQ\tBX\n"); emitline("\tPUSHQ\tAX\n"); - return rest + 2; + return rest + 3; }; // #21: CALL returning a tagged-union — the aistagged guard // above kept us out of the widening path. Push the tagged- @@ -12865,7 +12886,7 @@ fn cgloadtaggedfield(c: *cgen, basereg: str, foff: i32, slot_sz: i32) void = { // would need a reversed direction we don't currently emit). // - struct src (literal or ident): zero slot, write fields at +8+foff, // tag last. -// - str src: tag@+0, ptr@+8, len@+16. +// - str src: tag@+0, ptr@+8, len@+16, cap@+24 (str IS []u8, #1/Phase 3). // - scalar src: tag@+0, value@+8. fn cgwidentaggedstore(c: *cgen, dst: *tinfo, src: *node, basereg: str, slot_off: i32, slot_sz: i32) void = { @@ -13083,12 +13104,18 @@ fn cgwidentaggedstorebp(c: *cgen, dst: *tinfo, src: *node, slot_off: i32, slot_s emitoff((slot_off + 8 + fi.foff): i64); emitline("(BP)\n"); } else { if (isstrtype(c, fi.tnode)) { + // str IS []u8: 3-word field + // (ptr,len,cap) from cgexpr's + // AX/BX/CX (#1/Phase 3). emitline("\tMOVQ\tAX, "); emitoff((slot_off + 8 + fi.foff): i64); emitline("(BP)\n"); emitline("\tMOVQ\tBX, "); emitoff((slot_off + 8 + fi.foff + 8): i64); emitline("(BP)\n"); + emitline("\tMOVQ\tCX, "); + emitoff((slot_off + 8 + fi.foff + 16): i64); + emitline("(BP)\n"); } else { let sop: str = fieldstoreop(c, fi); emitline("\t"); @@ -13146,7 +13173,9 @@ fn cgwidentaggedstorebp(c: *cgen, dst: *tinfo, src: *node, slot_off: i32, slot_s return; }; }; - // Str payload. + // str IS []u8: AX=ptr, BX=len, CX=cap from cgexpr. Slot layout + // tag@+0, ptr@+8, len@+16, cap@+24 — same 32B shape as the slice + // payload below (#1/Phase 3). if (nodeisstr(c, src)) { cgexpr(c, src); emitline("\tMOVQ\tAX, "); @@ -13155,6 +13184,9 @@ fn cgwidentaggedstorebp(c: *cgen, dst: *tinfo, src: *node, slot_off: i32, slot_s emitline("\tMOVQ\tBX, "); emitoff((slot_off + 16): i64); emitline("(BP)\n"); + emitline("\tMOVQ\tCX, "); + emitoff((slot_off + 24): i64); + emitline("(BP)\n"); let tag: i32 = taggedvariantindext(c, dt, src); if (tag < 0) { tag = 0; }; emitline("\tMOVQ\t$"); @@ -13382,11 +13414,15 @@ export fn dotchainresolve(c: *cgen, n: *node, for (ft != nil && ft.kind == tykind.TY_NAMED) { ft = ft.under; }; if (ft == nil) { return false; }; if (ft.kind == tykind.TY_STR) { + // str IS []u8: .cap is the third header word, same as + // the TY_SLICE leaf below — cstage treats str≡slice for + // .ptr/.len/.cap (cmd/w6c/cgen.c:2478) (#1/Phase 3, #11). if (i != 1) { return false; }; let pseudo: str = stk[0].str; let delta: i32 = -1; if (streq(pseudo, "ptr")) { delta = 0; } - else { if (streq(pseudo, "len")) { delta = 8; }; }; + else { if (streq(pseudo, "len")) { delta = 8; } + else { if (streq(pseudo, "cap")) { delta = 16; }; }; }; if (delta < 0) { return false; }; *outtotaloff = *outtotaloff + foff; *outslicedelta = delta; @@ -13699,6 +13735,44 @@ fn cgstructlitfill(c: *cgen, si: *structinfo, lit: *node, fi = nil; } else if (callwhole) { fi = nil; + } else if (isstrtype(c, fi.tnode)) { + // str IS []u8: 3-word field (ptr,len,cap). + // cgexpr leaves AX/BX/CX; for non-BP modes + // the dst base goes in DX to dodge BX=len / + // CX=cap (the generic store reloads BX, which + // would clobber len) (#1/Phase 3). + cgexpr(c, fieldnode.lhs); + if (mode == 0) { + emitline("\tMOVQ\tAX, "); + emitoff((disp + fi.foff): i64); + emitline("(BP)\n"); + emitline("\tMOVQ\tBX, "); + emitoff((disp + fi.foff + 8): i64); + emitline("(BP)\n"); + emitline("\tMOVQ\tCX, "); + emitoff((disp + fi.foff + 16): i64); + emitline("(BP)\n"); + } else { + if (mode == 1) { + emitline("\tMOVQ\t"); + emitoff(srcoff: i64); + emitline("(BP), DX\n"); + } else { + emitline("\tLEAQ\t"); + emitsymname(c, srcname); + emitline("(SB), DX\n"); + }; + emitline("\tMOVQ\tAX, "); + emitdispreg((disp + fi.foff): i64, "DX"); + emitline("\n"); + emitline("\tMOVQ\tBX, "); + emitdispreg((disp + fi.foff + 8): i64, "DX"); + emitline("\n"); + emitline("\tMOVQ\tCX, "); + emitdispreg((disp + fi.foff + 16): i64, "DX"); + emitline("\n"); + }; + fi = nil; } else { cgexpr(c, fieldnode.lhs); // For non-BP modes, cgexpr just clobbered @@ -13956,7 +14030,11 @@ fn cgtryprop(c: *cgen, n: *node) void = { }; }; if (succisstr) { + // str IS []u8: success arrives DX=ptr, CX=len, R8=cap + // (slot 32B). Move len out before cap overwrites CX + // (#1/Phase 3). emitline("\tMOVQ\tCX, BX\n"); + emitline("\tMOVQ\tR8, CX\n"); }; emitline("\tMOVQ\tDX, AX\n"); return; @@ -14012,7 +14090,11 @@ fn cgtryunw(c: *cgen, n: *node) void = { }; }; if (succisstr) { + // str IS []u8: success arrives DX=ptr, CX=len, R8=cap + // (slot 32B). Move len out before cap overwrites CX + // (#1/Phase 3). emitline("\tMOVQ\tCX, BX\n"); + emitline("\tMOVQ\tR8, CX\n"); }; emitline("\tMOVQ\tDX, AX\n"); return; @@ -14285,8 +14367,9 @@ fn cgcast(c: *cgen, n: *node) void = { }; fn cgstrlit(c: *cgen, n: *node) void = { - // Result is the (ptr, len) pair: ptr in AX, len in BX. Call - // sites that expect a str arg pick these up directly. + // str IS []u8: the (ptr, len, cap) triple — ptr in AX, len in BX, + // cap in CX. A static literal has no spare storage, so cap = len + // (#1/Phase 3). Call sites that expect a str arg pick these up. let nstr: str = n.str; let lab: str = internstrlit(c, nstr); emitline("\tLEAQ\t"); @@ -14295,6 +14378,9 @@ fn cgstrlit(c: *cgen, n: *node) void = { emitline("\tMOVQ\t$"); emitint(nstr.len: i64); emitline(", BX\n"); + emitline("\tMOVQ\t$"); + emitint(nstr.len: i64); + emitline(", CX\n"); return; }; @@ -14330,9 +14416,14 @@ fn cgident(c: *cgen, n: *node) void = { emitoff(off: i64); emitline("(BP), AX\n"); if (isstr) { + // str IS []u8: load (ptr,len,cap) into AX/BX/CX, + // identical to the slice arm below (#1/Phase 3). emitline("\tMOVQ\t"); emitoff((off + 8): i64); emitline("(BP), BX\n"); + emitline("\tMOVQ\t"); + emitoff((off + 16): i64); + emitline("(BP), CX\n"); }; if (issl) { emitline("\tMOVQ\t"); @@ -14362,6 +14453,11 @@ fn cgident(c: *cgen, n: *node) void = { emitline("\tMOVQ\t$"); emitint(bytes.len: i64); emitline(", BX\n"); + // str IS []u8: cap = len for a static def literal + // (#1/Phase 3). + emitline("\tMOVQ\t$"); + emitint(bytes.len: i64); + emitline(", CX\n"); return; }; }; @@ -14393,17 +14489,16 @@ fn cgident(c: *cgen, n: *node) void = { let isstr: bool = letvarisstr(c, nm); let issl: bool = letvarisslice(c, nm); if (isstr || issl) { + // str IS []u8: both str and slice carry a third 8B + // (cap); load it unconditionally. The address holder CX + // is overwritten by the cap as the last step, after + // ptr/len are already loaded (#1/Phase 3). emitline("\tLEAQ\t"); emitsymname(c, nm); emitline("(SB), CX\n"); emitline("\tMOVQ\t(CX), AX\n"); emitline("\tMOVQ\t8(CX), BX\n"); - if (issl) { - // Overwrites the address holder with the - // cap as the last step — CX is no longer - // needed once both ptr/len are loaded. - emitline("\tMOVQ\t16(CX), CX\n"); - }; + emitline("\tMOVQ\t16(CX), CX\n"); return; }; // Float global: same LEAQ-indirect shape, since MOVSS/ @@ -14457,6 +14552,12 @@ fn cgindex(c: *cgen, n: *node) void = { let idx: *node = n.rhs; let esz: i32 = 8; let signed_elem: bool = false; + // #1/Phase 3: str=24B collides with slice=24B, so the str-element + // branches below MUST gate on kind (mirroring cstage's elem_is_str), + // not a bare `esz == primtypesize("str")` size check — otherwise a + // []u8 element (also 24B) misfires into the str 2-word load and + // diverges from cstage (#60 collision class; sentinel 754). + let elemisstr: bool = false; let baselocal: *local = nil; // Global `[N]T` array or `*T` pointer used as an index base. // The local-ident lookup above misses it; we need LEAQ name(SB) @@ -14498,7 +14599,7 @@ fn cgindex(c: *cgen, n: *node) void = { // cgen.c:3517-18). esz-only — N_DOT-base signedness // stays unset, as before. let dt: *tinfo = n.type_: *tinfo; - if (dt != nil) { esz = dt.size: i32; }; + if (dt != nil) { esz = dt.size: i32; elemisstr = typeisstr(dt); }; } else { if (base.kind == nkind.N_INDEX) { // #60: chained `names[i][k]` — n.type_ is the checker- // stamped outer element tinfo (indexresult over the inner @@ -14547,6 +14648,7 @@ fn cgindex(c: *cgen, n: *node) void = { esz = elem_slot_sz; }; }; + elemisstr = isstrtype(c, etn); }; }; cgexpr(c, idx); @@ -14582,7 +14684,7 @@ fn cgindex(c: *cgen, n: *node) void = { }; // #43: str element-stride routes through primtypesize so the // 16-vs-24 dispatch tracks ty_str.size for #1. - if (esz == primtypesize("str"): i32) { + if (elemisstr) { emitline("\tMOVQ\t8(BX), CX\n"); emitline("\tMOVQ\t(BX), AX\n"); emitline("\tMOVQ\tCX, BX\n"); @@ -14624,7 +14726,7 @@ fn cgindex(c: *cgen, n: *node) void = { // str element (16B today): load (ptr, len) into (AX, BX) so // the value flows through the str-rhs convention. // #43: route via primtypesize so the stride tracks #1. - if (esz == primtypesize("str"): i32) { + if (elemisstr) { emitline("\tMOVQ\t8(BX), CX\n"); emitline("\tMOVQ\t(BX), AX\n"); emitline("\tMOVQ\tCX, BX\n"); @@ -14656,7 +14758,7 @@ fn cgindex(c: *cgen, n: *node) void = { }; // #43: str element-stride routes through primtypesize so the // 16-vs-24 dispatch tracks ty_str.size for #1. - if (esz == primtypesize("str"): i32) { + if (elemisstr) { emitline("\tMOVQ\t8(AX), BX\n"); emitline("\tMOVQ\t(AX), AX\n"); return; @@ -15251,10 +15353,11 @@ fn cgdot(c: *cgen, n: *node) void = { }; // Hare-style tuple positional access: `t.0`, `t.1`. // Walk the tuple element type list summing slotsize - // (matches the (scalar, str) init layout which puts - // the scalar in an 8B slot and the str in 16B). For - // a str element, load both halves into (AX, BX) so - // chains like `t.1.len` propagate correctly. + // (matches the (scalar, str) init layout: scalar in an + // 8B slot, str in 24B — str IS []u8, #1/Phase 3). For a + // str element, load (ptr, len) into (AX, BX); the cap + // stays in the slot (the 2-word str-field read, like + // every other chained/dot str leaf read — task #14). if (lkind == nkind.N_TTUPLE) { let idx: i32 = fldnumidx(fld); if (idx >= 0) { @@ -15369,9 +15472,9 @@ fn cgdot(c: *cgen, n: *node) void = { let delta: i32 = -1; if (streq(fld, "ptr")) { delta = 0; }; if (streq(fld, "len")) { delta = 8; }; - if (issl) { - if (streq(fld, "cap")) { delta = 16; }; - }; + // str IS []u8: .cap is valid on a str global too, + // not slice-only — mirrors cstage (#1/Phase 3, #11). + if (streq(fld, "cap")) { delta = 16; }; if (delta >= 0) { emitline("\tLEAQ\t"); emitsymname(c, lhs.str); @@ -16227,7 +16330,9 @@ fn cgun(c: *cgen, n: *node) void = { let gdelta: i32 = -1; if (streq(fld, "ptr")) { gdelta = 0; }; if (streq(fld, "len")) { gdelta = 8; }; - if (issl) { if (streq(fld, "cap")) { gdelta = 16; }; }; + // str IS []u8: &str.cap is valid too, not slice-only + // — mirrors cstage (#1/Phase 3, #11). + if (streq(fld, "cap")) { gdelta = 16; }; if (gdelta >= 0) { emitline("\tLEAQ\t"); emitsymname(c, basenm); @@ -16599,16 +16704,19 @@ fn cgalloc(c: *cgen, n: *node) void = { emitline("\n"); fi = nil; } else { if (isstrtype(c, fi.tnode)) { - // alloc(T{ fval = s }) for str field: cgexpr - // leaves (AX=ptr, BX=len). Use CX for the heap - // base so BX=len survives both stores. Mirrors - // cmd/w6c/cgen.c:4184-4190. - emitline("\tMOVQ\t(SP), CX\n"); + // str IS []u8: cgexpr leaves (AX=ptr, + // BX=len, CX=cap). Route the heap base + // through DX so all three survive — CX + // holds cap, BX holds len (#1/Phase 3). + emitline("\tMOVQ\t(SP), DX\n"); emitline("\tMOVQ\tAX, "); - emitdispreg(fi.foff: i64, "CX"); + emitdispreg(fi.foff: i64, "DX"); emitline("\n"); emitline("\tMOVQ\tBX, "); - emitdispreg((fi.foff + 8): i64, "CX"); + emitdispreg((fi.foff + 8): i64, "DX"); + emitline("\n"); + emitline("\tMOVQ\tCX, "); + emitdispreg((fi.foff + 16): i64, "DX"); emitline("\n"); fi = nil; } else { @@ -17068,7 +17176,8 @@ fn cgcall(c: *cgen, n: *node) void = { popped += 1; } else { let extra: i32 = 0; - if (nodeisstr(c, a)) { extra = 1; }; + // str IS []u8: 3-word arg, same as slice (#1/Phase 3). + if (nodeisstr(c, a)) { extra = 2; }; if (nodeisslice(c, a)) { extra = 2; }; // #21: tagged-CALL arg was pushed AX/DX/CX/R8 high→low // by pushargsrev; size the per-arg pop to match so the @@ -17238,31 +17347,9 @@ fn cgcall(c: *cgen, n: *node) void = { emitint((stackslots * 8): i64); emitline(", SP\n"); }; - // SysV returns 16-byte aggregates in (AX, DX). Our str - // convention is (AX, BX), so shuffle for str-returning calls. - // Route through fnretlookupmod: for N_DOT cross-module callees, - // the bare-leaf fnretlookup's same-module-first walk (#4e) would - // pick the caller-module's same-leaf fn — a str-returning - // caller-side `slice` over a []u8-returning `mod.slice` then - // emits a phantom MOVQ DX, BX after the cross-module CALL (#34). - if (calleename.len > 0) { - let cmod: str; - cmod.ptr = nil; cmod.len = 0; - if (callee != nil) { - if (callee.kind == nkind.N_IDENT) { cmod = c.curmod; }; - if (callee.kind == nkind.N_DOT) { - if (callee.lhs != nil) { - if (callee.lhs.kind == nkind.N_IDENT) { - cmod = callee.lhs.str; - }; - }; - }; - }; - let rtyp: *node = fnretlookupmod(c, calleename, cmod); - if (isstrtype(c, rtyp)) { - emitline("\tMOVQ\tDX, BX\n"); - }; - }; + // str IS []u8: a str-returning callee leaves AX=ptr, BX=len, + // CX=cap — same as a slice, so there is no receive-side shuffle + // (#1/Phase 3). return; }; @@ -17361,17 +17448,21 @@ fn cgassign(c: *cgen, n: *node) void = { emitline("\tX0, (BX)\n"); return; }; - // Push order matches C cgen - // (cmd/w6c/cgen.c:1033-1041): PUSHQ AX - // (ptr) first, then PUSHQ BX (len) if - // str, so the pop sequence is POP CX - // (len) → POP AX (ptr) → MOVQ AX, - // (BX) → MOVQ CX, 8(BX). + // str IS []u8: PUSHQ AX (ptr) first, then + // PUSHQ BX (len) + PUSHQ CX (cap) across the + // pointer eval which clobbers BX/CX. Pop drains + // cap (top) → 16(BX), then len, then ptr → 0(BX) + // with len → 8(BX) (#1/Phase 3). emitline("\tPUSHQ\tAX\n"); - if (elemstr) { emitline("\tPUSHQ\tBX\n"); }; + if (elemstr) { + emitline("\tPUSHQ\tBX\n"); + emitline("\tPUSHQ\tCX\n"); + }; cgexpr(c, inner); emitline("\tMOVQ\tAX, BX\n"); if (elemstr) { + emitline("\tPOPQ\tCX\n"); + emitline("\tMOVQ\tCX, 16(BX)\n"); emitline("\tPOPQ\tCX\n"); emitline("\tPOPQ\tAX\n"); emitline("\tMOVQ\tAX, (BX)\n"); @@ -17625,10 +17716,19 @@ fn cgassign(c: *cgen, n: *node) void = { }; }; cgexpr(c, n.rhs); // value → AX - // #43: spill BX (str.len) before computing - // the index so the post-index store can pop - // it; the stride gate tracks ty_str.size. - if (esz == primtypesize("str"): i32) { emitline("\tPUSHQ\tBX\n"); }; + // str IS []u8: spill cap (CX) + len (BX) before + // computing the index so the post-index store can + // pop all three. #1/Phase 3: str=24B collides with + // slice=24B, so this MUST gate on kind (cstage's + // elem_is_str, cmd/w6c/cgen.c:3576) — not a bare + // `esz == primtypesize("str")` — or a []u8 element + // (also 24B) misfires the str 3-word store and + // diverges from cstage. Write-side mirror of the + // cgindex read-path gate (#7/754). + if (isstrtype(c, elemtn)) { + emitline("\tPUSHQ\tCX\n"); + emitline("\tPUSHQ\tBX\n"); + }; emitline("\tPUSHQ\tAX\n"); cgexpr(c, idx); // idx → AX if (esz > 1) { @@ -17666,13 +17766,15 @@ fn cgassign(c: *cgen, n: *node) void = { emitline("\tPOPQ\tAX\n"); // scaled idx emitline("\tADDQ\tAX, BX\n"); emitline("\tPOPQ\tAX\n"); // value - // #43: str-element write — pop the saved - // .len and store both halves. Stride gate - // routes through primtypesize for #1. - if (esz == primtypesize("str"): i32) { + // str IS []u8: pop the saved len + cap and store + // all three words. Kind-gate, not size — see the + // spill site above (#1/Phase 3, #7/754). + if (isstrtype(c, elemtn)) { emitline("\tMOVQ\tAX, (BX)\n"); emitline("\tPOPQ\tCX\n"); emitline("\tMOVQ\tCX, 8(BX)\n"); + emitline("\tPOPQ\tCX\n"); + emitline("\tMOVQ\tCX, 16(BX)\n"); return; }; let isop: str = tnodestoreop(c, elemtn, esz); @@ -18099,20 +18201,24 @@ fn cgassign(c: *cgen, n: *node) void = { emitline("\tMOVQ\tBX, AX\n"); }; }; - // str field via *struct: rhs left - // (AX=ptr, BX=len). Use CX as the - // address scratch so we don't clobber - // the len half before storing it. + // str IS []u8: rhs left (AX=ptr, + // BX=len, CX=cap). CX holds cap, so + // stage the struct addr in DX and + // store all three words — identical + // to the slice arm below (#1/Phase 3). if (n.op == tkind.TK_ASSIGN) { if (isstrtype(c, fi.tnode)) { emitline("\tMOVQ\t"); emitoff(lc.off: i64); - emitline("(BP), CX\n"); + emitline("(BP), DX\n"); emitline("\tMOVQ\tAX, "); - emitdispreg(fi.foff: i64, "CX"); + emitdispreg(fi.foff: i64, "DX"); emitline("\n"); emitline("\tMOVQ\tBX, "); - emitdispreg((fi.foff + 8): i64, "CX"); + emitdispreg((fi.foff + 8): i64, "DX"); + emitline("\n"); + emitline("\tMOVQ\tCX, "); + emitdispreg((fi.foff + 16): i64, "DX"); emitline("\n"); return; }; @@ -18303,11 +18409,11 @@ fn cgassign(c: *cgen, n: *node) void = { };}; }; cgexpr(c, n.rhs); - // str field: cgexpr left (AX=ptr, BX=len); - // store both halves at +0/+8. Without this, - // `L.src = s` would only write the ptr and - // `L.src.len` would carry whatever was on the - // stack. + // str IS []u8: cgexpr left (AX=ptr, + // BX=len, CX=cap); store all three at + // +0/+8/+16, identical to the slice + // arm below. BP base, no scratch + // reload needed (#1/Phase 3). if (isstrtype(c, fi.tnode)) { emitline("\tMOVQ\tAX, "); emitoff((lc.off + fi.foff): i64); @@ -18315,6 +18421,9 @@ fn cgassign(c: *cgen, n: *node) void = { emitline("\tMOVQ\tBX, "); emitoff((lc.off + fi.foff + 8): i64); emitline("(BP)\n"); + emitline("\tMOVQ\tCX, "); + emitoff((lc.off + fi.foff + 16): i64); + emitline("(BP)\n"); return; }; // slice field direct: cgexpr left @@ -18562,14 +18671,22 @@ fn cgassign(c: *cgen, n: *node) void = { if (n.op == tkind.TK_ASSIGN) { cgexpr(c, n.rhs); if (isstrtype(c, fi.tnode)) { + // str IS []u8: cgexpr left + // (AX=ptr, BX=len, CX=cap). CX + // holds cap, so stage the base + // addr in DX and store all three + // words (#1/Phase 3). emitline("\tLEAQ\t"); emitsymname(c, bn); - emitline("(SB), CX\n"); + emitline("(SB), DX\n"); emitline("\tMOVQ\tAX, "); - emitdispreg(fi.foff: i64, "CX"); + emitdispreg(fi.foff: i64, "DX"); emitline("\n"); emitline("\tMOVQ\tBX, "); - emitdispreg((fi.foff + 8): i64, "CX"); + emitdispreg((fi.foff + 8): i64, "DX"); + emitline("\n"); + emitline("\tMOVQ\tCX, "); + emitdispreg((fi.foff + 16): i64, "DX"); emitline("\n"); return; }; @@ -18808,22 +18925,29 @@ fn cgassign(c: *cgen, n: *node) void = { return; }; if (typeisstr(leaftype)) { + // str IS []u8: store ptr/len/cap. cgexpr leaves + // CX=cap, so the viacx base goes in DX (not CX) to + // avoid clobbering it — same as the single-dot str + // field store (#1/Phase 3). cgexpr(c, n.rhs); if (viacx) { if (ptrroot) { emitline("\tMOVQ\t"); emitoff(rootoff: i64); - emitline("(BP), CX\n"); + emitline("(BP), DX\n"); } else { emitline("\tLEAQ\t"); emitsymname(c, rootname); - emitline("(SB), CX\n"); + emitline("(SB), DX\n"); }; emitline("\tMOVQ\tAX, "); - emitdispreg(totaloff: i64, "CX"); + emitdispreg(totaloff: i64, "DX"); emitline("\n"); emitline("\tMOVQ\tBX, "); - emitdispreg((totaloff + 8): i64, "CX"); + emitdispreg((totaloff + 8): i64, "DX"); + emitline("\n"); + emitline("\tMOVQ\tCX, "); + emitdispreg((totaloff + 16): i64, "DX"); emitline("\n"); } else { emitline("\tMOVQ\tAX, "); @@ -18832,6 +18956,9 @@ fn cgassign(c: *cgen, n: *node) void = { emitline("\tMOVQ\tBX, "); emitoff((rootoff + totaloff + 8): i64); emitline("(BP)\n"); + emitline("\tMOVQ\tCX, "); + emitoff((rootoff + totaloff + 16): i64); + emitline("(BP)\n"); }; return; }; @@ -19319,11 +19446,17 @@ fn cgassign(c: *cgen, n: *node) void = { cgexpr(c, n.rhs); if (n.op == tkind.TK_ASSIGN) { if (letvarisstr(c, nm)) { + // str IS []u8: stash cap in DI before LEAQ + // overwrites CX, then store ptr/len/cap — + // identical to the slice arm below + // (#1/Phase 3). + emitline("\tMOVQ\tCX, DI\n"); emitline("\tLEAQ\t"); emitsymname(c, nm); emitline("(SB), CX\n"); emitline("\tMOVQ\tAX, (CX)\n"); emitline("\tMOVQ\tBX, 8(CX)\n"); + emitline("\tMOVQ\tDI, 16(CX)\n"); return; }; if (letvarisslice(c, nm)) { @@ -19609,7 +19742,9 @@ fn cgassign(c: *cgen, n: *node) void = { emitoff((off + 8): i64); emitline("(BP)\n"); }; - if (lcsl) { + // str IS []u8: store the cap word too, identical to + // the slice store (#1/Phase 3). + if (lcstr || lcsl) { emitline("\tMOVQ\tCX, "); emitoff((off + 16): i64); emitline("(BP)\n"); @@ -19812,12 +19947,16 @@ fn cgreturn(c: *cgen, n: *node) void = { rundefers(c); let rhs: *node = n.lhs; if (rhs != nil) { - // Tuple return `return a, b;`: + // Tuple return `return a, b;`, word-indexed AX→DX→CX→R8 (the + // SAME register sequence as the tagged-union return below; the + // tuple just fills it positionally): // (scalar, scalar) — AX = v0, DX = v1. // (scalar, str) / (str, scalar) — AX = scalar elem, - // DX = str.ptr, CX = str.len. - // 24B convention mirrors the tagged-union return below; receive - // sites destructure off the same regs regardless of position. + // DX = str.ptr, CX = str.len, R8 = str.cap. + // str IS []u8 (24B) → 32B tuple; cap rides R8, matching the + // tagged-union return that already uses R8 for slot+24 + // (#1/Phase 3, task #5). Receive sites destructure off the + // same regs regardless of position. if (rhs.kind == nkind.N_TUPLE) { let v: *node = rhs.list; if (v != nil) { @@ -19832,6 +19971,7 @@ fn cgreturn(c: *cgen, n: *node) void = { cgexpr(c, scaln); emitline("\tPUSHQ\tAX\n"); cgexpr(c, strn); + emitline("\tMOVQ\tCX, R8\n"); emitline("\tMOVQ\tBX, CX\n"); emitline("\tMOVQ\tAX, DX\n"); emitline("\tPOPQ\tAX\n"); @@ -19984,12 +20124,12 @@ fn cgreturn(c: *cgen, n: *node) void = { emitline("\tMOVQ\tBX, CX\n"); emitline("\tMOVQ\tAX, DX\n"); } else { if (nodeisstr(c, rhs)) { + // str IS []u8: cgexpr leaves (AX=ptr, BX=len, + // CX=cap). Same shuffle as the slice arm above — + // DX=ptr, CX=len, R8=cap (#1/Phase 3). + emitline("\tMOVQ\tCX, R8\n"); emitline("\tMOVQ\tBX, CX\n"); emitline("\tMOVQ\tAX, DX\n"); - // str fills DX,CX. Zero R8 if dst covers slot+24. - if (rsz > 24) { - emitline("\tMOVQ\t$0, R8\n"); - }; } else { emitline("\tMOVQ\tAX, DX\n"); // scalar fills DX only. Zero CX / R8 if dst @@ -20245,11 +20385,8 @@ fn cgreturn(c: *cgen, n: *node) void = { }; emitline("\tMOVQ\t$0, AX\n"); }; - // SysV: 16-byte aggregates (str, 2-tuple) return in (AX, DX). - // cgexpr leaves str in (AX, BX); shuffle BX→DX. - if (isstrtype(c, c.fnret)) { - emitline("\tMOVQ\tBX, DX\n"); - }; + // str IS []u8: cgexpr leaves AX=ptr, BX=len, CX=cap — str now + // returns exactly like a slice, no AX:DX shuffle (#1/Phase 3). emitline("\tMOVQ\tBP, SP\n"); emitline("\tPOPQ\tBP\n"); emitline("\tRET\n"); @@ -20420,11 +20557,13 @@ fn cglet(c: *cgen, n: *node) void = { c.lastwasreturn = 0; return; }; - // 24B tuple init for `let t: (scalar, str) = call()` / - // `let t: (str, scalar) = call()`. Per the AX:DX:CX return - // convention: AX = scalar elem, DX = str.ptr, CX = str.len. - // Layout is positional, so we route each register to the - // slot dictated by element type, not by AX/DX position. + // 32B tuple init for `let t: (scalar, str) = call()` / + // `let t: (str, scalar) = call()`. Per the AX:DX:CX:R8 return + // convention: AX = scalar elem, DX = str.ptr, CX = str.len, + // R8 = str.cap. Layout is positional (str takes 24B at its + // position), so we route each register to the slot dictated by + // element type, not by AX/DX position. str IS []u8 (24B) → 32B + // tuple (#1/Phase 3, task #5). if (n.lhs != nil) { if (n.lhs.kind == nkind.N_TTUPLE) { let p0: *node = n.lhs.list; @@ -20447,9 +20586,12 @@ fn cglet(c: *cgen, n: *node) void = { emitline("\tMOVQ\tCX, "); emitoff((off + 8): i64); emitline("(BP)\n"); - emitline("\tMOVQ\tAX, "); + emitline("\tMOVQ\tR8, "); emitoff((off + 16): i64); emitline("(BP)\n"); + emitline("\tMOVQ\tAX, "); + emitoff((off + 24): i64); + emitline("(BP)\n"); } else { emitline("\tMOVQ\tAX, "); emitoff(off: i64); @@ -20460,6 +20602,9 @@ fn cglet(c: *cgen, n: *node) void = { emitline("\tMOVQ\tCX, "); emitoff((off + 16): i64); emitline("(BP)\n"); + emitline("\tMOVQ\tR8, "); + emitoff((off + 24): i64); + emitline("(BP)\n"); }; c.lastwasreturn = 0; return; @@ -20750,15 +20895,19 @@ fn cglet(c: *cgen, n: *node) void = { emitline("\tMOVQ\tAX, "); emitoff(off: i64); emitline("(BP)\n"); - // str init: cgexpr also leaves len in BX; store both. + // str IS []u8: cgexpr leaves (ptr,len,cap) in AX/BX/CX; store + // all three, same as the slice arm below (#1/Phase 3). // #60: gate by kind too — under #1's str=24 bump, sizeof(str) // and sizeof(slice) collide, so a bare `sz ==` check fires - // both branches for one let. Mirrors cstage cgen.c:6439's + // both branches for one let. Mirrors cstage cgen.c's // `type_isstr(lt) && sz == ty_str->size` shape. if (isstrtype(c, tn) && sz == primtypesize("str"): i32) { emitline("\tMOVQ\tBX, "); emitoff((off + 8): i64); emitline("(BP)\n"); + emitline("\tMOVQ\tCX, "); + emitoff((off + 16): i64); + emitline("(BP)\n"); }; // slice init: ptr/len/cap in AX/BX/CX. Same kind+size gate as // the str arm — without the kind check this fires on a str let @@ -20919,11 +21068,12 @@ fn cgmassign(c: *cgen, n: *node) void = { // type is taken from its explicit annotation (l.lhs) when present // or inferred from the called fn's return-type tuple element. // -// Per the AX:DX:CX return convention (mirrors C cgen nkind.N_MLET): +// Per the AX:DX:CX:R8 return convention (mirrors C cgen nkind.N_MLET): // (scalar, scalar) — AX → l0, DX → l1. -// (scalar, str) — AX → scalar slot, (DX, CX) → str slot -// as (.ptr, .len). Position-agnostic — the +// (scalar, str) — AX → scalar slot, (DX, CX, R8) → str slot +// as (.ptr, .len, .cap). Position-agnostic — the // regs are routed by element type, not by AX/DX. +// str IS []u8 (24B): cap rides R8 (#1/Phase 3, task #5). fn cgmlet(c: *cgen, n: *node) void = { let rhs: *node = n.rhs; if (rhs == nil) { return; }; @@ -20992,16 +21142,21 @@ fn cgmlet(c: *cgen, n: *node) void = { let off0: i32 = localadd(c, l0.str, sz0, t0); let off1: i32 = localadd(c, l1.str, sz1, t1); if (s0_is_str) { + // l0 str: ptr=DX, len=CX, cap=R8. l1 scalar = AX. emitline("\tMOVQ\tDX, "); emitoff(off0: i64); emitline("(BP)\n"); emitline("\tMOVQ\tCX, "); emitoff((off0 + 8): i64); emitline("(BP)\n"); + emitline("\tMOVQ\tR8, "); + emitoff((off0 + 16): i64); + emitline("(BP)\n"); emitline("\tMOVQ\tAX, "); emitoff(off1: i64); emitline("(BP)\n"); } else { + // l0 scalar; l1 str: ptr=DX, len=CX, cap=R8. emitline("\tMOVQ\tAX, "); emitoff(off0: i64); emitline("(BP)\n"); @@ -21011,6 +21166,9 @@ fn cgmlet(c: *cgen, n: *node) void = { emitline("\tMOVQ\tCX, "); emitoff((off1 + 8): i64); emitline("(BP)\n"); + emitline("\tMOVQ\tR8, "); + emitoff((off1 + 16): i64); + emitline("(BP)\n"); }; c.lastwasreturn = 0; return; @@ -21597,11 +21755,11 @@ fn cgfnparams(c: *cgen, params: *node) void = { stkcursor += 3; };}; } else { if (isstrtype(c, p.lhs)) { - if (idx + 2 <= 6) { - // #60: route str-param slot width through the - // primtypesize SSoT so #1's ty_str bump propagates - // here (parent #43 covered the reg-fill site only - // inside cgexpr). + if (idx + 3 <= 6) { + // str IS []u8: 3-word param (ptr,len,cap), same as + // the slice arm above (#1/Phase 3). #60: route slot + // width through the primtypesize SSoT so #1's ty_str + // bump propagates here. let off: i32 = localadd(c, nm, primtypesize("str"): i32, p.lhs); emitline("\tMOVQ\t"); emitline(argregname(idx)); @@ -21615,11 +21773,14 @@ fn cgfnparams(c: *cgen, params: *node) void = { emitoff((off + 8): i64); emitline("(BP)\n"); idx += 1; + emitline("\tMOVQ\t"); + emitline(argregname(idx)); + emitline(", "); + emitoff((off + 16): i64); + emitline("(BP)\n"); + idx += 1; } else { if (idx < 6) { - // Partial-fit stitch — mirrors tagged at lines - // 440-469. Only idx=5 hits this (nw=2, - // regs_left=1): ptr lands in R9, len at - // +16+stkcursor*8(BP). + // Partial-fit stitch — mirrors the slice arm above. // #60: same SSoT routing as the regs-fit arm above. let off: i32 = localadd(c, nm, primtypesize("str"): i32, p.lhs); let regs_left: i32 = 6 - idx; @@ -21633,7 +21794,7 @@ fn cgfnparams(c: *cgen, params: *node) void = { idx += 1; w += 1; }; - for (w < 2) { + for (w < 3) { emitline("\tMOVQ\t"); emitoff((16 + stkcursor*8): i64); emitline("(BP), AX\n"); @@ -21645,7 +21806,7 @@ fn cgfnparams(c: *cgen, params: *node) void = { }; } else { localaddstack(c, nm, p.lhs, 16 + stkcursor*8); - stkcursor += 2; + stkcursor += 3; };}; } else { let stsz: i32 = structparamsize(c, p.lhs); if (stsz > 0) { diff --git a/selfhost/test/smoke.combined.ww b/selfhost/test/smoke.combined.ww index 8cb8f2c5..9780deda 100644 --- a/selfhost/test/smoke.combined.ww +++ b/selfhost/test/smoke.combined.ww @@ -3365,7 +3365,7 @@ export fn main() i32 = { // (#42). Each call folds to an N_INTLIT at check time; cgen // materialises the literal as a plain `MOVQ $N, AX`. Mirrors // cstage cmd/wcc/check.c:907-960 byte-for-byte on this corpus. - if (size(str) != 16) { return 23; }; + if (size(str) != 24) { return 23; }; // str IS []u8: {ptr,len,cap} 24B (#1/Phase 3) if (size(i64) != 8) { return 24; }; if (size(i32) != 4) { return 25; }; if (align(i64) != 8) { return 26; }; diff --git a/selfhost/test/smoke.ww b/selfhost/test/smoke.ww index bc940030..6892f821 100644 --- a/selfhost/test/smoke.ww +++ b/selfhost/test/smoke.ww @@ -185,7 +185,7 @@ export fn main() i32 = { // (#42). Each call folds to an N_INTLIT at check time; cgen // materialises the literal as a plain `MOVQ $N, AX`. Mirrors // cstage cmd/wcc/check.c:907-960 byte-for-byte on this corpus. - if (size(str) != 16) { return 23; }; + if (size(str) != 24) { return 23; }; // str IS []u8: {ptr,len,cap} 24B (#1/Phase 3) if (size(i64) != 8) { return 24; }; if (size(i32) != 4) { return 25; }; if (align(i64) != 8) { return 26; }; diff --git a/test/wcc/741_dotbase_chained.c b/test/wcc/741_dotbase_chained.c index a1b6f0af..337a1a1e 100644 --- a/test/wcc/741_dotbase_chained.c +++ b/test/wcc/741_dotbase_chained.c @@ -85,11 +85,14 @@ static const struct row rows[] = { "export fn main() i32 = { return 0; };\n", "\tMOVZBQ\t(AX), AX\n", "" }, - /* Write: `obj.arr[i] = v` where arr: [N](i64|str) — 24B tagged. + /* Write: `obj.arr[i] = v` where arr: [N](i64|str) — tagged element. * Post-fix wwstage: cgassign N_DOT arm sets elemtn → tagged-store - * path → IMULQ $24 + byte-copy from scratch. Scanlocals N_DOT arm - * pre-reserves @tagscr in the frame. Pre-fix: scalar `MOVQ AX, - * (BX)` over the 24B slot. */ + * path → IMULQ (stride) + byte-copy from scratch. Scanlocals N_DOT + * arm pre-reserves @tagscr in the frame. Pre-fix: scalar `MOVQ AX, + * (BX)` over the slot. + * #1/Phase 3: str IS []u8 (24B), so the (i64|str) slot is + * 8(tag)+24(str payload)=32B — stride is $32, not the 16B-world + * $24. Byte-identical across stages. */ { "dotbase_array_tagged_write", "type T = (i64 | str);\n" "type S = struct{ pad: i64, arr: [4]T };\n" @@ -98,7 +101,7 @@ static const struct row rows[] = { " s.arr[i] = 42i64;\n" "};\n" "export fn main() i32 = { return 0; };\n", - "\tMOVQ\t$24, CX\n", + "\tMOVQ\t$32, CX\n", "" }, }; diff --git a/test/wcc/758_cgalloc_str_field.c b/test/wcc/758_cgalloc_str_field.c index ee343c5c..357b3e93 100644 --- a/test/wcc/758_cgalloc_str_field.c +++ b/test/wcc/758_cgalloc_str_field.c @@ -239,7 +239,10 @@ static const struct asm_disp_row asm_disp_rows[] = { "package main;\n" "type holder = struct { s: str };\n" "fn dummy() *holder = { return alloc(holder { s = \"x\" })!; };\n", - "\tMOVQ\tAX, (CX)\n" }, + /* #1/Phase 3: str IS []u8 (24B), so the alloc-str-field store + * routes the heap base through DX (CX now holds the cap) and + * writes 3 words (ptr/len/cap). Was `(CX)` in the 16B world. */ + "\tMOVQ\tAX, (DX)\n" }, /* str at non-zero foff. Pins that the displacement IS emitted * (`8(CX)`) when foff != 0 — emitdispreg must not suppress * non-zero offsets too. Pre-fix and post-fix both pass this; it @@ -250,7 +253,8 @@ static const struct asm_disp_row asm_disp_rows[] = { "fn dummy() *holder = {\n" " return alloc(holder { pad = 0, s = \"x\" })!;\n" "};\n", - "\tMOVQ\tAX, 8(CX)\n" }, + /* #1/Phase 3: DX base (str IS []u8, cap in CX); was `8(CX)`. */ + "\tMOVQ\tAX, 8(DX)\n" }, /* Generic 8-byte field at foff=0 (non-str, non-float path). Covers * the `else` branch's `MOVQ AX, (BX)` store via fieldstoreop. */ { "alloc_int_at_offset0", diff --git a/test/wcc/928_str_abi_run.c b/test/wcc/928_str_abi_run.c new file mode 100644 index 00000000..f28e7059 --- /dev/null +++ b/test/wcc/928_str_abi_run.c @@ -0,0 +1,236 @@ +/* + * 928_str_abi_run — end-to-end runtime coverage for the str->24B + * {ptr,len,cap} 3-reg ABI (Commit #1 / Phase 3, str IS []u8). + * + * The scratch str-ABI probe matrix (task #3) only diffs asm byte-id; + * byte-identity proves the two stages agree, NOT that the emitted code + * is correct (a shared miscompile passes byte-id silently). This file + * pins the *runtime* contract: build each fixture through both the + * cstage `ww` and the wwstage `ww_ww` driver and confirm the program's + * own assertions hold (exit 0). + * + * Covers the ABI dimensions that exercise the new cap word and the + * AX/BX/CX value / AX:DX:CX:R8 tagged+tuple register layout: str + * literal (cap=len), str arg, str return, str struct field, the + * (i64,str) and (str,i64) tuple return shapes, deref-store `*p = s`, + * and []str index write+read. + * + * Deliberately NOT covered here (known, separately-tracked gaps found + * during Commit #1 review — both byte-identical across stages, so the + * byte-id gates stay green): + * - str-containing struct passed BY VALUE: now >16B, falls into the + * general ">16B struct byval" limitation (a non-str 24B struct + * byval mis-compiles the same way); not a str-specific defect. + * (task #10) + * - `.cap` VALUE of a top-level str GLOBAL reads 0, not len: the + * str-literal global DATAW emits only the 16B {ptr,len} payload, + * not the 24B header — byte-identical across stages, but the cap + * word is never initialised. Distinct from the .cap field/global + * link-error (task #11), which IS fixed: `.cap` on a str field + * (stored value) and the global field-read now compile and agree. + */ +#include +#include +#include +#include +#include +#include + +static int +runwait(const char *cmd) +{ + int rc = system(cmd); + if (rc == -1) return -1; + if (WIFEXITED(rc)) return WEXITSTATUS(rc); + return -1; +} + +struct row { const char *label; const char *src; int want; }; + +static const struct row rows[] = { + /* Literal: cap = len for a static literal (no spare storage), + * and .cap on a LOCAL str reads back. The new third word. */ + { "literal_len_cap", + "export fn main() i32 = {\n" + " let s: str = \"hello\";\n" + " if (s.len: i32 != 5) { return 1; };\n" + " if (s.cap: i32 != 5) { return 2; };\n" + " return 0;\n" + "};\n", + 0 }, + /* Arg: str passed as a 3-word arg (ptr,len,cap), len read in + * the callee. Both a let-bound str and a bare literal arg. */ + { "arg_len", + "fn slen(s: str) i32 = { return s.len: i32; };\n" + "export fn main() i32 = {\n" + " let s: str = \"hello\";\n" + " if (slen(s) != 5) { return 1; };\n" + " if (slen(\"hi\") != 2) { return 2; };\n" + " return 0;\n" + "};\n", + 0 }, + /* Return: callee returns a str in AX/BX/CX (no AX:DX shuffle — + * str returns exactly like a slice now). */ + { "return_str", + "fn greet() str = { return \"hello world\"; };\n" + "export fn main() i32 = {\n" + " let g: str = greet();\n" + " if (g.len: i32 != 11) { return 1; };\n" + " return 0;\n" + "};\n", + 0 }, + /* Struct field: store a str into a 3-word field, read .len and + * .cap back (field-store routes the base through DX to dodge + * CX=cap; the cap word is stored, so b.s.cap == len here). */ + { "struct_field_store_load", + "type box = struct { s: str, n: i32 };\n" + "export fn main() i32 = {\n" + " let b: box;\n" + " b.s = \"abcd\";\n" + " b.n = 7i32;\n" + " if (b.s.len: i32 != 4) { return 1; };\n" + " if (b.n != 7) { return 2; };\n" + " if (b.s.cap: i32 != 4) { return 3; };\n" + " return 0;\n" + "};\n", + 0 }, + /* Tuple (i64, str) return: AX=scalar, DX=ptr, CX=len, R8=cap; + * 32B receive slot. */ + { "tuple_int_str", + "fn pair() (i64, str) = { return (42i64, \"hello\"); };\n" + "export fn main() i32 = {\n" + " let n, s = pair();\n" + " if (n: i32 != 42) { return 1; };\n" + " if (s.len: i32 != 5) { return 2; };\n" + " return 0;\n" + "};\n", + 0 }, + /* Tuple (str, i64) return: reversed order, registers keyed by + * element type not position. */ + { "tuple_str_int", + "fn pair() (str, i64) = { return (\"hi\", 7i64); };\n" + "export fn main() i32 = {\n" + " let s, n = pair();\n" + " if (s.len: i32 != 2) { return 1; };\n" + " if (n: i32 != 7) { return 2; };\n" + " return 0;\n" + "};\n", + 0 }, + /* Deref-store: `*p = s` writes all three words through the + * pointer (cap stashed across the pointer eval). */ + { "deref_store", + "fn setit(p: *str, v: str) void = { *p = v; };\n" + "export fn main() i32 = {\n" + " let s: str = \"hello\";\n" + " let d: str;\n" + " setit(&d, s);\n" + " if (d.len: i32 != 5) { return 1; };\n" + " return 0;\n" + "};\n", + 0 }, + /* []str index write + read: the str-element store pushes + * cap/len and writes 3 words; the read loads them back. Guards + * the str-element gate against the slice=24B collision (#7/754, + * write-side). */ + { "index_write_read", + "export fn main() i32 = {\n" + " let xs: [2]str;\n" + " xs[0] = \"hi\";\n" + " xs[1] = \"abc\";\n" + " if (xs[0].len: i32 != 2) { return 1; };\n" + " if (xs[1].len: i32 != 3) { return 2; };\n" + " return 0;\n" + "};\n", + 0 }, +}; + +static int +run_driver(const char *driver, const struct row *r, int i) +{ + char src[96], tmpdir[96], cmd[1024]; + snprintf(src, sizeof src, "/tmp/strabi_run_%d_%d.ww", getpid(), i); + snprintf(tmpdir, sizeof tmpdir, "/tmp/strabi_run_%d_d_%d", getpid(), i); + + FILE *f = fopen(src, "wb"); + if (!f) return -1; + fputs(r->src, f); + fclose(f); + + mkdir(tmpdir, 0755); + snprintf(cmd, sizeof cmd, "cd %s && %s build %s", + tmpdir, driver, src); + if (runwait(cmd) != 0) { + fprintf(stderr, "row[%s]: build via %s failed\n", + r->label, driver); + unlink(src); rmdir(tmpdir); + return -1; + } + + const char *base = strrchr(src, '/'); + base = base ? base + 1 : src; + char outbin[160]; + snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base); + char *dot = strrchr(outbin, '.'); + if (dot && strcmp(dot, ".ww") == 0) *dot = '\0'; + int got = runwait(outbin); + + unlink(src); unlink(outbin); rmdir(tmpdir); + return got; +} + +int +main(void) +{ + const char *bin = getenv("BIN"); + if (!bin) bin = "out/bin"; + char absbin[512]; + if (bin[0] != '/') { + char cwd[256]; + if (getcwd(cwd, sizeof cwd) == NULL) return 1; + snprintf(absbin, sizeof absbin, "%s/%s", cwd, bin); + bin = absbin; + } + + char cdrv[640]; + snprintf(cdrv, sizeof cdrv, "%s/ww", bin); + char wdrv[640]; + snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin); + + struct { const char *name; const char *path; int gated_on_existence; } + drivers[] = { + { "cstage", cdrv, 0 }, + { "wwstage", wdrv, 1 }, + { NULL, NULL, 0 }, + }; + + int n = (int)(sizeof rows / sizeof rows[0]); + int total = 0, fail = 0; + for (int d = 0; drivers[d].name; d++) { + if (drivers[d].gated_on_existence + && access(drivers[d].path, X_OK) != 0) { + fprintf(stderr, + "str_abi_run: skip %s (no %s)\n", + drivers[d].name, drivers[d].path); + continue; + } + for (int i = 0; i < n; i++) { + int got = run_driver(drivers[d].path, &rows[i], i); + total++; + if (got != rows[i].want) { + fprintf(stderr, + "str_abi_run[%s][%s]: exit=%d want=%d\n", + drivers[d].name, rows[i].label, + got, rows[i].want); + fail++; + } + } + } + + if (fail) { + fprintf(stderr, "str_abi_run: %d/%d fixtures failed\n", + fail, total); + return 1; + } + printf("str_abi_run: %d/%d ok\n", total, total); + return 0; +}