Files
ww/cmd/w6c/main.c
Hojun-Cho b795c320c9 wcc: -T test-mode collects @test fns + synthesizes entry, both stages (#15)
@test was parsed then dropped (no consumer); `ww test` needed a hand-written
main listing each test by hand, so adding a @test and forgetting the call
silently skipped it. -T makes the checker collect @test N_FNDECLs in source
order, loud-reject a user main, and append a synthetic
`export fn main() i32 { t0(); ...; return 0; }` at the install->body-check seam;
the existing cgfn emits it (cgen untouched) -> byte-identical by construction.
Mirrors harec's checker-side is_test placement.

Plan-9-lean reduction (user-sanctioned, reinstatable post-CSP): sequential,
abort/nonzero=fail; no setjmp isolation, no fnmatch filter, no file:line.

910/997 rewired from a regex scanner to driving `w6c -T` directly (thin trusted
drivers; the @test content stays ww), with a cross-stage byte-id assert on the
-T output. attest_userman/attest_badsig pin the user-main and bad-signature
rejects.
2026-06-10 02:01:20 +09:00

98 lines
2.0 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;
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, "-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;
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;
}