diff --git a/selfhost/cmd/w6c/main.combined.ww b/selfhost/cmd/w6c/main.combined.ww index ef0a9140..d402bfc0 100644 --- a/selfhost/cmd/w6c/main.combined.ww +++ b/selfhost/cmd/w6c/main.combined.ww @@ -20285,12 +20285,14 @@ fn cgtypetest(c: *cgen, n: *node) void = { }; }; let want: i32 = 0; + let nullcarrier: *node = scrutt; if (nonident) { // Variant index from the STAMPED scrutinee type (cstage: // u = n->lhs->type) — matchscrutt's node-shape walk can't // carry N_DOT (returns the scrut node, which the // cgtagvariantidx N_TTAGGED gate rejects). flatvariantidx / // flatslicevariantidx read .type_ off any stamped carrier. + nullcarrier = lhs; if (n.rhs != nil) { if (n.rhs.kind == nkind.N_TSLICE) { want = flatslicevariantidx(c, lhs, n.rhs.lhs); @@ -20301,7 +20303,6 @@ fn cgtypetest(c: *cgen, n: *node) void = { } else { want = cgtagvariantidx(c, scrutt, n.rhs); }; - if (want < 0) { want = 0; }; if (!nonident) { emitline("\tMOVQ\t"); emitoff(scrutoff: i64); @@ -20309,10 +20310,25 @@ fn cgtypetest(c: *cgen, n: *node) void = { }; let nel: str = mklabel(c, "is_ne"); let dnl: str = mklabel(c, "is_done"); - emitline("\tCMPQ\t$"); - emitint(want: i64); - emitline(", AX\n"); - emitline("\tJNE\t"); + if (isnullabletype(nullcarrier)) { + // Nullable `(*T | void)` fold: the word in AX IS the + // pointer — discriminate pointer-vs-null, not tag-vs-index + // (cstage cgen.c N_TYPETEST nullable arm). `want` stays RAW + // here: cstage tests tag == ptr_tag unclamped, so a + // no-match (-1) takes the void polarity. + emitline("\tCMPQ\t$0, AX\n"); + if (want == nullableptrtag(nullcarrier)) { + emitline("\tJE\t"); + } else { + emitline("\tJNE\t"); + }; + } else { + if (want < 0) { want = 0; }; + emitline("\tCMPQ\t$"); + emitint(want: i64); + emitline(", AX\n"); + emitline("\tJNE\t"); + }; emitline(nel); emitline("\n\tMOVQ\t$1, AX\n\tJMP\t"); emitline(dnl); diff --git a/selfhost/cmd/wcc/cgenexpr.ww b/selfhost/cmd/wcc/cgenexpr.ww index c5aa0936..de0f08bf 100644 --- a/selfhost/cmd/wcc/cgenexpr.ww +++ b/selfhost/cmd/wcc/cgenexpr.ww @@ -426,12 +426,14 @@ fn cgtypetest(c: *cgen, n: *node) void = { }; }; let want: i32 = 0; + let nullcarrier: *node = scrutt; if (nonident) { // Variant index from the STAMPED scrutinee type (cstage: // u = n->lhs->type) — matchscrutt's node-shape walk can't // carry N_DOT (returns the scrut node, which the // cgtagvariantidx N_TTAGGED gate rejects). flatvariantidx / // flatslicevariantidx read .type_ off any stamped carrier. + nullcarrier = lhs; if (n.rhs != nil) { if (n.rhs.kind == nkind.N_TSLICE) { want = flatslicevariantidx(c, lhs, n.rhs.lhs); @@ -442,7 +444,6 @@ fn cgtypetest(c: *cgen, n: *node) void = { } else { want = cgtagvariantidx(c, scrutt, n.rhs); }; - if (want < 0) { want = 0; }; if (!nonident) { emitline("\tMOVQ\t"); emitoff(scrutoff: i64); @@ -450,10 +451,25 @@ fn cgtypetest(c: *cgen, n: *node) void = { }; let nel: str = mklabel(c, "is_ne"); let dnl: str = mklabel(c, "is_done"); - emitline("\tCMPQ\t$"); - emitint(want: i64); - emitline(", AX\n"); - emitline("\tJNE\t"); + if (isnullabletype(nullcarrier)) { + // Nullable `(*T | void)` fold: the word in AX IS the + // pointer — discriminate pointer-vs-null, not tag-vs-index + // (cstage cgen.c N_TYPETEST nullable arm). `want` stays RAW + // here: cstage tests tag == ptr_tag unclamped, so a + // no-match (-1) takes the void polarity. + emitline("\tCMPQ\t$0, AX\n"); + if (want == nullableptrtag(nullcarrier)) { + emitline("\tJE\t"); + } else { + emitline("\tJNE\t"); + }; + } else { + if (want < 0) { want = 0; }; + emitline("\tCMPQ\t$"); + emitint(want: i64); + emitline(", AX\n"); + emitline("\tJNE\t"); + }; emitline(nel); emitline("\n\tMOVQ\t$1, AX\n\tJMP\t"); emitline(dnl); diff --git a/selfhost/cmd/wwdump/main.combined.ww b/selfhost/cmd/wwdump/main.combined.ww index 7dc051df..23a7a90a 100644 --- a/selfhost/cmd/wwdump/main.combined.ww +++ b/selfhost/cmd/wwdump/main.combined.ww @@ -20285,12 +20285,14 @@ fn cgtypetest(c: *cgen, n: *node) void = { }; }; let want: i32 = 0; + let nullcarrier: *node = scrutt; if (nonident) { // Variant index from the STAMPED scrutinee type (cstage: // u = n->lhs->type) — matchscrutt's node-shape walk can't // carry N_DOT (returns the scrut node, which the // cgtagvariantidx N_TTAGGED gate rejects). flatvariantidx / // flatslicevariantidx read .type_ off any stamped carrier. + nullcarrier = lhs; if (n.rhs != nil) { if (n.rhs.kind == nkind.N_TSLICE) { want = flatslicevariantidx(c, lhs, n.rhs.lhs); @@ -20301,7 +20303,6 @@ fn cgtypetest(c: *cgen, n: *node) void = { } else { want = cgtagvariantidx(c, scrutt, n.rhs); }; - if (want < 0) { want = 0; }; if (!nonident) { emitline("\tMOVQ\t"); emitoff(scrutoff: i64); @@ -20309,10 +20310,25 @@ fn cgtypetest(c: *cgen, n: *node) void = { }; let nel: str = mklabel(c, "is_ne"); let dnl: str = mklabel(c, "is_done"); - emitline("\tCMPQ\t$"); - emitint(want: i64); - emitline(", AX\n"); - emitline("\tJNE\t"); + if (isnullabletype(nullcarrier)) { + // Nullable `(*T | void)` fold: the word in AX IS the + // pointer — discriminate pointer-vs-null, not tag-vs-index + // (cstage cgen.c N_TYPETEST nullable arm). `want` stays RAW + // here: cstage tests tag == ptr_tag unclamped, so a + // no-match (-1) takes the void polarity. + emitline("\tCMPQ\t$0, AX\n"); + if (want == nullableptrtag(nullcarrier)) { + emitline("\tJE\t"); + } else { + emitline("\tJNE\t"); + }; + } else { + if (want < 0) { want = 0; }; + emitline("\tCMPQ\t$"); + emitint(want: i64); + emitline(", AX\n"); + emitline("\tJNE\t"); + }; emitline(nel); emitline("\n\tMOVQ\t$1, AX\n\tJMP\t"); emitline(dnl); diff --git a/test/wcc/927_is_nonident_run.c b/test/wcc/927_is_nonident_run.c index cfcee24c..e8d2e535 100644 --- a/test/wcc/927_is_nonident_run.c +++ b/test/wcc/927_is_nonident_run.c @@ -14,8 +14,12 @@ * * Rows pin every scrutinee shape: indexed (constant and len-1 index), * dot-field, call-result, the `is []T` slice-variant axis on an - * indexed scrutinee, and the ident control (asm hand-checked - * unchanged vs the pre-#45 compiler when the fix landed). Per row: + * indexed scrutinee, the nullable `(*T | void)` pointer-vs-null + * discriminator on both ident and non-ident scrutinees (review + * finding: the first non-ident arm tag-compared the pointer; the + * ident half was missing the nullable arm since before #45), and the + * ident control (asm hand-checked unchanged vs the pre-#45 compiler + * when the fix landed). Per row: * w6c vs w6c_ww byte-id (rule 10 — cstage is the runtime-correct * reference shape) + runtime via both the ww and ww_ww drivers. */ @@ -117,6 +121,45 @@ static const struct row rows[] = { " return 0;\n" "};\n", 0 }, + /* Nullable `(*T | void)` fold on a NON-IDENT scrutinee: the + * discriminator is pointer-vs-null, not tag-vs-index (cstage + * N_TYPETEST nullable arm emits CMPQ $0 + JE/JNE polarity). + * Surfaced in #45 review: the first non-ident arm compared the + * pointer against the variant index — `h.m is *t` on a non-null + * pointer answered FALSE (silent cs≠ww, wwstage exit 1). Both + * polarities, both states (ptr and void). */ + { "nullable_dot_field", + "type t = struct { a: i64 };\n" + "type maybe = (*t | void);\n" + "type holder = struct { m: maybe, k: i64 };\n" + "export fn main() i32 = {\n" + " let v: t = t { a = 5 };\n" + " let h: holder = holder { m = &v, k = 1 };\n" + " if (!(h.m is *t)) { return 1; };\n" + " if (h.m is void) { return 2; };\n" + " let h2: holder = holder { m = void, k = 2 };\n" + " if (h2.m is *t) { return 3; };\n" + " if (!(h2.m is void)) { return 4; };\n" + " return 0;\n" + "};\n", + 0 }, + /* Nullable IDENT scrutinee: the same missing nullable arm hit + * the ident path too (pre-existing at master, closed by the same + * shared compare choke-point). */ + { "nullable_ident", + "type t = struct { a: i64 };\n" + "type maybe = (*t | void);\n" + "export fn main() i32 = {\n" + " let v: t = t { a = 5 };\n" + " let m: maybe = &v;\n" + " if (!(m is *t)) { return 1; };\n" + " if (m is void) { return 2; };\n" + " let m2: maybe = void;\n" + " if (m2 is *t) { return 3; };\n" + " if (!(m2 is void)) { return 4; };\n" + " return 0;\n" + "};\n", + 0 }, /* Ident control: the pre-existing slot-load path must be * untouched (also hand-cmp'd vs the pre-#45 w6c_ww). */ { "ident_control",