wcc: nodeisunsigned N_CALL reads stamped type_ (#168)
Twin of #134 (N_INDEX arm): the wwstage signedness classifier did not consult the checker-stamped type_ for an N_CALL result, so an unsigned-returning call got signed IDIVQ/SARQ instead of DIVQ/SHRQ. cstage was already correct (reads the stamped operand type; check.c:1433), so this is a wwstage-only arm — symmetric outcome both stages. Gate-blind (the bootstrap lacks the shape) — covered by table-driven runtime probe 906, which also asserts w6c==w6c_ww .s byte-identity.
This commit is contained in:
7
Makefile
7
Makefile
@@ -334,6 +334,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \
|
||||
$(BIN)/test_idx_compound_run \
|
||||
$(BIN)/test_dotbase_arr_run \
|
||||
$(BIN)/test_continue_run \
|
||||
$(BIN)/test_callret_unsigned_arith_run \
|
||||
$(BIN)/test_sar_shr_run \
|
||||
$(BIN)/test_def_mangle_run \
|
||||
$(BIN)/test_arr_u16_store_run \
|
||||
@@ -1141,6 +1142,12 @@ $(BIN)/test_sar_shr_run: test/wcc/912_sar_shr_run.c $(BIN)/ww \
|
||||
$(LIB)/libwwrt.a | $(BIN)
|
||||
$(CC) $(CFLAGS) -o $@ $<
|
||||
|
||||
$(BIN)/test_callret_unsigned_arith_run: \
|
||||
test/wcc/906_callret_unsigned_arith_run.c $(BIN)/ww \
|
||||
$(BIN)/w6c $(BIN)/w6c_ww $(BIN)/w6a $(BIN)/w6l \
|
||||
$(LIB)/libwwrt.a | $(BIN)
|
||||
$(CC) $(CFLAGS) -o $@ $<
|
||||
|
||||
$(BIN)/test_def_mangle_run: test/wcc/913_def_mangle_run.c $(BIN)/ww \
|
||||
$(BIN)/w6c $(BIN)/w6c_ww $(BIN)/w6a $(BIN)/w6l \
|
||||
$(LIB)/libwwrt.a | $(BIN)
|
||||
|
||||
@@ -14898,6 +14898,16 @@ fn nodeisunsigned(c: *cgen, n: *node) bool = {
|
||||
if (k == nkind.N_INDEX) {
|
||||
return typeisunsigned(n.type_: *tinfo);
|
||||
};
|
||||
// nkind.N_CALL: a call returning an unsigned type (e.g. `fn f() u64`)
|
||||
// is unsigned. Read the checker-stamped result type directly — the
|
||||
// N_CALL twin of the #134 N_INDEX arm above. cstage reads the same
|
||||
// stamp via `type_isunsigned(n->lhs->type)`, stamped at check.c N_CALL
|
||||
// `n->type = u->ret`; without this arm the wwstage fell through to
|
||||
// `return false`, picking signed IDIV/SAR over unsigned DIV/SHR on a
|
||||
// call-result div/mod/shift operand. #168.
|
||||
if (k == nkind.N_CALL) {
|
||||
return typeisunsigned(n.type_: *tinfo);
|
||||
};
|
||||
return false;
|
||||
};
|
||||
|
||||
|
||||
@@ -1071,6 +1071,16 @@ fn nodeisunsigned(c: *cgen, n: *node) bool = {
|
||||
if (k == nkind.N_INDEX) {
|
||||
return typeisunsigned(n.type_: *tinfo);
|
||||
};
|
||||
// nkind.N_CALL: a call returning an unsigned type (e.g. `fn f() u64`)
|
||||
// is unsigned. Read the checker-stamped result type directly — the
|
||||
// N_CALL twin of the #134 N_INDEX arm above. cstage reads the same
|
||||
// stamp via `type_isunsigned(n->lhs->type)`, stamped at check.c N_CALL
|
||||
// `n->type = u->ret`; without this arm the wwstage fell through to
|
||||
// `return false`, picking signed IDIV/SAR over unsigned DIV/SHR on a
|
||||
// call-result div/mod/shift operand. #168.
|
||||
if (k == nkind.N_CALL) {
|
||||
return typeisunsigned(n.type_: *tinfo);
|
||||
};
|
||||
return false;
|
||||
};
|
||||
|
||||
|
||||
@@ -14898,6 +14898,16 @@ fn nodeisunsigned(c: *cgen, n: *node) bool = {
|
||||
if (k == nkind.N_INDEX) {
|
||||
return typeisunsigned(n.type_: *tinfo);
|
||||
};
|
||||
// nkind.N_CALL: a call returning an unsigned type (e.g. `fn f() u64`)
|
||||
// is unsigned. Read the checker-stamped result type directly — the
|
||||
// N_CALL twin of the #134 N_INDEX arm above. cstage reads the same
|
||||
// stamp via `type_isunsigned(n->lhs->type)`, stamped at check.c N_CALL
|
||||
// `n->type = u->ret`; without this arm the wwstage fell through to
|
||||
// `return false`, picking signed IDIV/SAR over unsigned DIV/SHR on a
|
||||
// call-result div/mod/shift operand. #168.
|
||||
if (k == nkind.N_CALL) {
|
||||
return typeisunsigned(n.type_: *tinfo);
|
||||
};
|
||||
return false;
|
||||
};
|
||||
|
||||
|
||||
263
test/wcc/906_callret_unsigned_arith_run.c
Normal file
263
test/wcc/906_callret_unsigned_arith_run.c
Normal file
@@ -0,0 +1,263 @@
|
||||
/*
|
||||
* 906_callret_unsigned_arith_run — runtime + byte-id net for #168, the
|
||||
* N_CALL twin of #134. The cgen signedness classifier must treat a
|
||||
* div/mod/shift whose operand is a *call result* (`f() / d`, `f() % d`,
|
||||
* `f() >> n`) as unsigned iff the callee's return type is unsigned —
|
||||
* picking DIVQ/SHRQ over IDIVQ/SARQ.
|
||||
*
|
||||
* The wwstage `nodeisunsigned` (cgenutil.ww) walked surface nodes but
|
||||
* had no N_CALL arm, so a call-result operand fell through to
|
||||
* `return false` (signed) → signed IDIVQ/SARQ on an unsigned-returning
|
||||
* call → silent wrong arithmetic. The cstage was already correct: it
|
||||
* reads `type_isunsigned(n->lhs->type)` on the operand's stamped tinfo
|
||||
* (cgen.c TK_SLASH/TK_PERCENT/TK_RSHIFT), and the N_CALL result type is
|
||||
* stamped at check.c N_CALL (`n->type = u->ret`). #168 brings the
|
||||
* wwstage arm up to read the same stamp — exactly as #134 did for the
|
||||
* N_INDEX arm (which also touched no C).
|
||||
*
|
||||
* Gate-blind: the bootstrap never divides/shifts a call result by an
|
||||
* unsigned type (strconv dodged it via local-bind, which lands on the
|
||||
* N_IDENT arm). So only a direct call-result probe catches it — a
|
||||
* `let a = f();` bind would hide the bug.
|
||||
*
|
||||
* Discrimination:
|
||||
* - byte-id (.s cmp, the primary #168 gate): for ANY unsigned-
|
||||
* returning call, w6c picks DIVQ/SHRQ but pre-fix w6c_ww picks
|
||||
* IDIVQ/SARQ → .s DIFFER. Holds for every unsigned width (the
|
||||
* classifier diverges regardless of the runtime value).
|
||||
* - cstage runtime: validates the unsigned *semantics*. Only the
|
||||
* machine-word rows (u64/uint, high bit set) differ at runtime
|
||||
* between signed/unsigned div on the 64-bit register; the u32 row
|
||||
* is a byte-id discriminator + runtime control (zero-extends to a
|
||||
* positive 64-bit value, so the value alone can't tell the ops
|
||||
* apart — the .s cmp does).
|
||||
* - signed rows (i64/i32/int) guard against an over-broad fix: a
|
||||
* signed-returning call must STILL pick IDIVQ/SARQ in both stages.
|
||||
*
|
||||
* Same dual-driver shape as 912_sar_shr_run.c: each row is (a) cstage
|
||||
* `ww build` + run asserting exit 42, and (b) w6c vs w6c_ww `.s` cmp
|
||||
* (rule-10 byte-id). Hard-fails if w6c_ww is missing so the wwstage
|
||||
* side — the side that carried the bug — can never silently skip.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
static int
|
||||
runwait(const char *cmd)
|
||||
{
|
||||
int rc = system(cmd);
|
||||
if (rc == -1) return -1;
|
||||
if (WIFEXITED(rc)) return WEXITSTATUS(rc);
|
||||
return -1;
|
||||
}
|
||||
|
||||
struct row { const char *label; const char *src; int want_exit; };
|
||||
|
||||
static const struct row rows[] = {
|
||||
/* ---- unsigned: call result drives DIVQ/SHRQ ------------------- */
|
||||
/* u64 div: 0x8000000000000001 / 2. unsigned = 0x4000000000000000;
|
||||
* signed IDIVQ (CQO sign-extends the high-bit-set dividend) =
|
||||
* 0xC000000000000001. */
|
||||
{ "u64_div_callret",
|
||||
"package main;\n"
|
||||
"fn uval() u64 = { return 0x8000000000000001u64; };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" if (uval() / 2u64 == 0x4000000000000000u64) { return 42; };\n"
|
||||
" return 0;\n"
|
||||
"};\n", 42 },
|
||||
/* u64 mod: unsigned rem = 1; signed rem = -1. */
|
||||
{ "u64_mod_callret",
|
||||
"package main;\n"
|
||||
"fn uval() u64 = { return 0x8000000000000001u64; };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" if (uval() % 2u64 == 1u64) { return 42; };\n"
|
||||
" return 0;\n"
|
||||
"};\n", 42 },
|
||||
/* u64 shr: SHRQ (logical) = 0x4000000000000000; SARQ (arithmetic,
|
||||
* sign-fills the set MSB) = 0xC000000000000000. */
|
||||
{ "u64_shr_callret",
|
||||
"package main;\n"
|
||||
"fn uval() u64 = { return 0x8000000000000001u64; };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" if (uval() >> 1u64 == 0x4000000000000000u64) { return 42; };\n"
|
||||
" return 0;\n"
|
||||
"};\n", 42 },
|
||||
/* u32 div: byte-id discriminator (w6c DIVQ vs pre-fix w6c_ww
|
||||
* IDIVQ); runtime control — 0x80000001 zero-extends to a positive
|
||||
* 64-bit value, so signed/unsigned div agree at runtime. */
|
||||
{ "u32_div_callret",
|
||||
"package main;\n"
|
||||
"fn uval() u32 = { return 0x80000001u32; };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" if (uval() / 2u32 == 0x40000000u32) { return 42; };\n"
|
||||
" return 0;\n"
|
||||
"};\n", 42 },
|
||||
/* uint (machine word): exercises the TY_UINT arm of typeisunsigned
|
||||
* on a call result. High bit set → discriminates like u64. */
|
||||
{ "uint_div_callret",
|
||||
"package main;\n"
|
||||
"fn uval() uint = { return (0x8000000000000001u64): uint; };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" if (uval() / 2 == (0x4000000000000000u64): uint) { return 42; };\n"
|
||||
" return 0;\n"
|
||||
"};\n", 42 },
|
||||
|
||||
/* ---- signed guards: call result STAYS IDIVQ/SARQ -------------- */
|
||||
/* i64 div: -100 / 7 = -14 (toward zero). If wrongly unsigned, -100
|
||||
* as u64 is huge → quotient huge ≠ -14. */
|
||||
{ "i64_div_callret",
|
||||
"package main;\n"
|
||||
"fn sval() i64 = { return -100i64; };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" if (sval() / 7i64 == -14i64) { return 42; };\n"
|
||||
" return 0;\n"
|
||||
"};\n", 42 },
|
||||
/* i64 shr: -8 >> 1 = -4 via SARQ (sign-fill). SHRQ would zero-fill
|
||||
* → large positive. */
|
||||
{ "i64_shr_callret",
|
||||
"package main;\n"
|
||||
"fn sval() i64 = { return -8i64; };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" if (sval() >> 1i64 == -4i64) { return 42; };\n"
|
||||
" return 0;\n"
|
||||
"};\n", 42 },
|
||||
/* i32 div: -100 / 7 = -14. */
|
||||
{ "i32_div_callret",
|
||||
"package main;\n"
|
||||
"fn sval() i32 = { return -100i32; };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" if (sval() / 7i32 == -14i32) { return 42; };\n"
|
||||
" return 0;\n"
|
||||
"};\n", 42 },
|
||||
/* int (machine word, signed): -100 / 7 = -14; exercises the signed
|
||||
* machine-word path on a call result. */
|
||||
{ "int_div_callret",
|
||||
"package main;\n"
|
||||
"fn sval() int = { return -100; };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" if (sval() / 7 == -14) { return 42; };\n"
|
||||
" return 0;\n"
|
||||
"};\n", 42 },
|
||||
{ NULL, NULL, 0 }
|
||||
};
|
||||
|
||||
static int
|
||||
slurp_eq(const char *a, const char *b)
|
||||
{
|
||||
FILE *fa = fopen(a, "rb");
|
||||
FILE *fb = fopen(b, "rb");
|
||||
if (!fa || !fb) { if (fa) fclose(fa); if (fb) fclose(fb); return -1; }
|
||||
int rc = 0;
|
||||
for (;;) {
|
||||
int ca = fgetc(fa);
|
||||
int cb = fgetc(fb);
|
||||
if (ca != cb) { rc = -1; break; }
|
||||
if (ca == EOF) break;
|
||||
}
|
||||
fclose(fa); fclose(fb);
|
||||
return rc;
|
||||
}
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
const char *bin = getenv("BIN");
|
||||
if (!bin) bin = "out/bin";
|
||||
char absbin[1024];
|
||||
if (bin[0] != '/') {
|
||||
char cwd[1024];
|
||||
if (getcwd(cwd, sizeof cwd) == NULL) return 1;
|
||||
snprintf(absbin, sizeof absbin, "%s/%s", cwd, bin);
|
||||
bin = absbin;
|
||||
}
|
||||
|
||||
char w6c[1100], w6c_ww[1100];
|
||||
snprintf(w6c, sizeof w6c, "%s/w6c", bin);
|
||||
snprintf(w6c_ww, sizeof w6c_ww, "%s/w6c_ww", bin);
|
||||
if (access(w6c_ww, X_OK) != 0) {
|
||||
fprintf(stderr, "callret_unsigned: w6c_ww missing — cannot run "
|
||||
"the cs==ww byte-id gate (the whole point of this test)\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
int n = 0, fail = 0;
|
||||
for (int i = 0; rows[i].src; i++, n++) {
|
||||
char src[64];
|
||||
snprintf(src, sizeof src, "/tmp/wwcr_%d_%d.ww", getpid(), i);
|
||||
FILE *f = fopen(src, "wb");
|
||||
if (f == NULL) { fail++; continue; }
|
||||
fputs(rows[i].src, f);
|
||||
fclose(f);
|
||||
|
||||
char tmpdir[64];
|
||||
snprintf(tmpdir, sizeof tmpdir, "/tmp/wwcr_%d_d_%d",
|
||||
getpid(), i);
|
||||
mkdir(tmpdir, 0755);
|
||||
|
||||
char cmd[2048];
|
||||
snprintf(cmd, sizeof cmd, "cd %s && %s/ww build %s",
|
||||
tmpdir, bin, src);
|
||||
if (runwait(cmd) != 0) {
|
||||
fprintf(stderr, "row[%s]: cstage build failed\n",
|
||||
rows[i].label);
|
||||
fail++;
|
||||
unlink(src); rmdir(tmpdir);
|
||||
continue;
|
||||
}
|
||||
|
||||
char outbin[128];
|
||||
const char *base = strrchr(src, '/');
|
||||
base = base ? base + 1 : src;
|
||||
snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base);
|
||||
char *dot = strrchr(outbin, '.');
|
||||
if (dot && strcmp(dot, ".ww") == 0) *dot = '\0';
|
||||
|
||||
int got = runwait(outbin);
|
||||
if (got != rows[i].want_exit) {
|
||||
fprintf(stderr, "row[%s]: cstage exit %d, want %d\n",
|
||||
rows[i].label, got, rows[i].want_exit);
|
||||
fail++;
|
||||
}
|
||||
unlink(outbin); rmdir(tmpdir);
|
||||
|
||||
char cs_s[64], ws_s[64];
|
||||
snprintf(cs_s, sizeof cs_s, "/tmp/wwcr_%d_%d_cs.s",
|
||||
getpid(), i);
|
||||
snprintf(ws_s, sizeof ws_s, "/tmp/wwcr_%d_%d_ww.s",
|
||||
getpid(), i);
|
||||
|
||||
snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null",
|
||||
w6c, cs_s, src);
|
||||
if (runwait(cmd) != 0) {
|
||||
fprintf(stderr, "row[%s]: w6c failed\n", rows[i].label);
|
||||
fail++; unlink(src); continue;
|
||||
}
|
||||
snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null",
|
||||
w6c_ww, ws_s, src);
|
||||
if (runwait(cmd) != 0) {
|
||||
fprintf(stderr, "row[%s]: w6c_ww failed\n",
|
||||
rows[i].label);
|
||||
fail++; unlink(src); unlink(cs_s); continue;
|
||||
}
|
||||
if (slurp_eq(cs_s, ws_s) != 0) {
|
||||
fprintf(stderr,
|
||||
"row[%s]: cstage/wwstage .s DIFFER (rule-10 "
|
||||
"byte-id violation)\n", rows[i].label);
|
||||
fail++;
|
||||
}
|
||||
unlink(src); unlink(cs_s); unlink(ws_s);
|
||||
}
|
||||
|
||||
if (fail) {
|
||||
fprintf(stderr, "%d/%d callret_unsigned tests failed\n",
|
||||
fail, n);
|
||||
return 1;
|
||||
}
|
||||
printf("callret_unsigned: %d/%d ok (cstage run + cs==ww byte-id)\n",
|
||||
n, n);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user