/* * 530_w6a_parsenum — F14 #62: w6a's parsenum diverged from the C twin's * strtoll(s,end,0) (cmd/w6a/lex.c:30) on three hand-written-asm edge * shapes, all gate-blind (w6c emits the canonical $5 / $8 / -8(BP), never * these). The fix aligns wwstage's assembler to strtoll semantics: * (a) `$ 5` — leading whitespace after $: strtoll skips it (→5); ww * had no skip and silently encoded imm 0. * (b) `$08` — strtoll base-0 reads a leading 0 as octal and STOPS at * '8' (→0); ww parsed it as decimal 8. * (c) `-(BP)` — strtoll/cstage require a digit after the sign, so a bare * `-(` is `unrecognised operand` (loud reject); ww silently * accepted it as 0(BP). * * (a)/(b): both assemblers must emit a byte-identical .o. * (c): both assemblers must FAIL (loud reject). * Controls: the canonical $5 / $8 / -8(BP) shapes that w6c DOES emit must * stay byte-identical (no regression). * * Drives w6a (cstage) and w6a_ww (wwstage) directly; gated on w6a_ww. */ #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; } /* assemble `asm` with `tool` into `obj`; returns the build rc. */ static int assemble(const char *tool, const char *asmtext, const char *obj, int i) { char src[64], cmd[1024]; snprintf(src, sizeof src, "/tmp/w6apn_%d_%d.s", getpid(), i); FILE *f = fopen(src, "wb"); if (!f) return -2; fputs(asmtext, f); fclose(f); snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null", tool, obj, src); int rc = runwait(cmd); unlink(src); return rc; } static int files_equal(const char *a, const char *b) { FILE *fa = fopen(a, "rb"); FILE *fb = fopen(b, "rb"); int eq = (fa && fb); if (fa && fb) { for (;;) { int ca = fgetc(fa), cb = fgetc(fb); if (ca != cb) { eq = 0; break; } if (ca == EOF) break; } } if (fa) fclose(fa); if (fb) fclose(fb); return eq; } struct idrow { const char *label; const char *asmtext; }; /* shapes that must assemble byte-identically across the two assemblers. */ static const struct idrow idrows[] = { { "ws_after_dollar", "TEXT main,$16\n\tMOVQ\t$ 5, AX\n\tRET\n" }, { "leading_zero", "TEXT main,$16\n\tMOVQ\t$08, AX\n\tRET\n" }, /* controls — the canonical shapes w6c actually emits. */ { "plain_imm", "TEXT main,$16\n\tMOVQ\t$5, AX\n\tRET\n" }, { "plain_imm_eight", "TEXT main,$16\n\tMOVQ\t$8, AX\n\tRET\n" }, { "hex_imm", "TEXT main,$16\n\tMOVQ\t$0x1f, AX\n\tRET\n" }, { "zero_imm", "TEXT main,$16\n\tMOVQ\t$0, AX\n\tRET\n" }, { "neg_indir", "TEXT main,$16\n\tMOVQ\t-8(BP), AX\n\tRET\n" }, { "pos_indir", "TEXT main,$16\n\tMOVQ\t8(BP), AX\n\tRET\n" }, }; /* shapes both assemblers must reject. */ static const struct idrow rejrows[] = { { "bare_minus_indir", "TEXT main,$16\n\tMOVQ\t-(BP), AX\n\tRET\n" }, }; 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 ctool[1024], wtool[1024]; snprintf(ctool, sizeof ctool, "%s/w6a", bin); snprintf(wtool, sizeof wtool, "%s/w6a_ww", bin); if (access(wtool, X_OK) != 0) { fprintf(stderr, "w6a_parsenum: skip (no %s)\n", wtool); printf("w6a_parsenum: skipped\n"); return 0; } int nid = (int)(sizeof idrows / sizeof idrows[0]); int nrej = (int)(sizeof rejrows / sizeof rejrows[0]); int fail = 0; for (int i = 0; i < nid; i++) { char co[64], wo[64]; snprintf(co, sizeof co, "/tmp/w6apn_%d_%d_c.o", getpid(), i); snprintf(wo, sizeof wo, "/tmp/w6apn_%d_%d_w.o", getpid(), i); int cr = assemble(ctool, idrows[i].asmtext, co, i); int wr = assemble(wtool, idrows[i].asmtext, wo, 1000 + i); if (cr != 0 || wr != 0) { fprintf(stderr, "w6a_parsenum[%s]: assemble failed " "(c=%d w=%d)\n", idrows[i].label, cr, wr); fail++; } else if (!files_equal(co, wo)) { fprintf(stderr, "w6a_parsenum[%s]: .o differ across " "assemblers\n", idrows[i].label); fail++; } unlink(co); unlink(wo); } for (int i = 0; i < nrej; i++) { char co[64], wo[64]; snprintf(co, sizeof co, "/tmp/w6apn_%d_r%d_c.o", getpid(), i); snprintf(wo, sizeof wo, "/tmp/w6apn_%d_r%d_w.o", getpid(), i); int cr = assemble(ctool, rejrows[i].asmtext, co, 2000 + i); int wr = assemble(wtool, rejrows[i].asmtext, wo, 3000 + i); if (cr == 0) { fprintf(stderr, "w6a_parsenum[%s]: cstage accepted, " "expected reject\n", rejrows[i].label); fail++; } if (wr == 0) { fprintf(stderr, "w6a_parsenum[%s]: wwstage accepted, " "expected reject\n", rejrows[i].label); fail++; } unlink(co); unlink(wo); } if (fail) { fprintf(stderr, "w6a_parsenum: %d fixtures failed\n", fail); return 1; } printf("w6a_parsenum: %d/%d ok\n", nid + nrej, nid + nrej); return 0; }