Files
ww/cmd/w6c/main.c
Hojun-Cho e8d3d89fef wcc/ww: .wwi export-data producer + check_exported_type (#22 M2)
New `w6c -I <out.wwi>` flag (both stages) writes a re-parseable
ww-prototype rendering of a package's EXPORTED surface. M2 dead-code:
nothing consumes .wwi yet (combined.ww stays the live path); the flag is
off on every existing invocation, so the 990-997 byte-id gates and all
prior tests are unperturbed.

The unparse walks the AST type-expr subtree (N_T* nodes), not the
tinfo Type* (which collapses nominal pkg.Name identity). Deterministic
output: package line, byte-sorted imports, byte-sorted decls — a pure
function of the exported API. cmd/w6c/wwi.c + selfhost/cmd/wcc/wwi.ww
emit byte-identical .wwi (new cross-stage byte-id substrate, rule 10).

check_exported_type (drew) rides the producer entry, flag-gated: an
exported signature naming a non-exported nominal is loud-rejected before
any byte is written, identically on both stages. Ports harec
check.c:4092-4168, recursing the type-AST and gating on the resolved
SK_TYPE sym's decl export flag (Sym.exported is vestigial in both
stages; the predeclared synthetic `nomem` decl carries no source
position and is treated as a builtin leaf — cstage parity).

Two wwstage checker AST-mutations are normalized to cstage's pristine
view for byte-id: the N_TPARAM tuple-element wrapper (unwrapped) and the
variadic `T...`→`[]T` param desugar (peeled).

Gate 989_m2wwi_run: ascii/strings/getopt each produce a .wwi that
re-parses (wwdump -a) and is cs==ww byte-identical; a private-type-leak
fixture is rejected identically by both stages (non-vacuous check).
2026-06-15 20:58:08 +09:00

114 lines
2.5 KiB
C

/*
* w6c — amd64 compiler driver. Reads a .ww source file, runs the
* libwcc frontend (lex → parse → check), then walks the typed AST
* via cgen.c and writes Plan 9-flavoured amd64 asm to stdout (or
* the file given by -o).
*/
#include "gc.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int
slurp(const char *path, char **outbuf, u64 *outlen)
{
FILE *f = fopen(path, "rb");
if (f == NULL) return -1;
fseek(f, 0, SEEK_END);
long n = ftell(f);
fseek(f, 0, SEEK_SET);
if (n < 0) { fclose(f); return -1; }
char *b = malloc((size_t)n + 1);
if (b == NULL) { fclose(f); return -1; }
if (fread(b, 1, (size_t)n, f) != (size_t)n) { free(b); fclose(f); return -1; }
b[n] = '\0';
fclose(f);
*outbuf = b;
*outlen = (u64)n;
return 0;
}
int
main(int argc, char **argv)
{
const char *src = NULL;
const char *out = NULL;
const char *wwiout = NULL; /* -I <out.wwi>: M2 export-data producer */
int testmode = 0;
for (int i = 1; i < argc; i++) {
const char *a = argv[i];
if (strcmp(a, "-o") == 0 && i + 1 < argc) {
out = argv[++i];
} else if (strcmp(a, "-I") == 0 && i + 1 < argc) {
wwiout = argv[++i];
} else if (strcmp(a, "-T") == 0) {
testmode = 1;
} else if (a[0] == '-') {
fprintf(stderr, "w6c: unknown flag %s\n", a);
return 2;
} else if (src == NULL) {
src = a;
} else {
fprintf(stderr, "w6c: only one input supported\n");
return 2;
}
}
if (src == NULL) {
fputs("usage: w6c [-T] [-o out.s] file.ww\n", stderr);
return 2;
}
char *buf;
u64 len;
if (slurp(src, &buf, &len) < 0) {
fprintf(stderr, "w6c: %s: cannot read\n", src);
return 1;
}
Arena *a = newarena();
Lex l;
Parser p;
Checker c;
Cg cg;
lexinit(&l, a, src, buf, len);
parserinit(&p, a, &l);
Node *file = parsefile(&p);
if (l.errs || p.errs) return 1;
check_init(&c, a);
c.is_test = testmode;
check_file(&c, file);
if (c.errs) return 1;
/* M2 export-data: write the `.wwi` after a clean check, before cgen.
* Dead on the live path (no existing invocation passes -I); the
* producer's check_exported_type may reject a dangling export. */
if (wwiout) {
FILE *wf = fopen(wwiout, "wb");
if (wf == NULL) {
fprintf(stderr, "w6c: cannot open %s\n", wwiout);
return 1;
}
if (wwi_emit(&c, wf, file) != 0) { fclose(wf); return 1; }
fclose(wf);
}
FILE *of = stdout;
if (out) {
of = fopen(out, "wb");
if (of == NULL) {
fprintf(stderr, "w6c: cannot open %s\n", out);
return 1;
}
}
cg_init(&cg, a);
cg_file(&cg, of, file);
if (of != stdout) fclose(of);
freearena(a);
free(buf);
return 0;
}