selfhost/cmd/6c/main.ww is a thin packaging of the wwc cgen — slurp a .ww file, run lex+parse+cgen, write Plan 9 amd64 asm to the path given by -o. The cgen routines in selfhost/cmd/wwc/cgen.ww write directly to fd 1, so we use dup2 to redirect stdout into the output file rather than thread an fd through every emit helper. Adds the SYS_DUP2=33 wrapper in lib/os. Makefile wires $(BIN)/6c_ww alongside the other wwstage tools and adds $(BIN)/test_6c_ww to the TESTS list. test/wwc/994_6c_ww.c diffs 6c_ww byte-for-byte against `wwdump_ww -c` on five in-source programs plus the four selfhost main.combined.ww files: same cgen reached through two binaries, so any divergence is a packaging bug in selfhost/cmd/6c. We deliberately don't diff against C-side 6c here — 990 probe 5 already covers that on the subset the ww cgen handles today.
132 lines
2.9 KiB
Plaintext
132 lines
2.9 KiB
Plaintext
// selfhost/cmd/6c/main.ww — port of cmd/6c/main.c.
|
|
//
|
|
// 6c = amd64 compiler. Read .ww, parse, codegen, emit Plan 9 amd64
|
|
// asm to stdout (or the file given by -o).
|
|
//
|
|
// 6c_ww -o file.s file.ww
|
|
//
|
|
// The cgen routines in selfhost/cmd/wwc/cgen.ww write directly to
|
|
// fd 1 via os.write(1, ...). For -o, we open the output file and
|
|
// dup2 it onto fd 1 before invoking cg_file. This is the same trick
|
|
// the bootstrap uses with shell redirection, just in-process.
|
|
|
|
use os;
|
|
use mem;
|
|
use tok;
|
|
use lex;
|
|
use ast;
|
|
use parse;
|
|
use typ;
|
|
use sym;
|
|
use check;
|
|
use cgen;
|
|
|
|
fn streq_cs(a: *u8, lit: str) bool = {
|
|
let n: u64 = lit.len: u64;
|
|
let i: u64 = 0u64;
|
|
for (i < n) {
|
|
let li: i32 = i: i32;
|
|
if (a[i] != lit[li]) { return false; };
|
|
i += 1u64;
|
|
};
|
|
if (a[i] != 0u8) { return false; };
|
|
return true;
|
|
};
|
|
|
|
fn cstrlen(p: *u8) u64 = {
|
|
let n: u64 = 0u64;
|
|
for (p[n] != 0u8) { n += 1u64; };
|
|
return n;
|
|
};
|
|
|
|
fn slurp(path_cs: *u8) (*u8, u64) = {
|
|
let fd: i32 = os.open(path_cs, os.O_RDONLY, 0i32);
|
|
if (fd < 0) { return nil, 0u64; };
|
|
let n: i64 = os.filesize(fd);
|
|
if (n < 0i64) { os.close(fd); return nil, 0u64; };
|
|
let nz: u64 = n: u64;
|
|
let buf: *u8 = os.alloc(nz + 1u64): *u8;
|
|
let got: i64 = os.readfull(fd, buf, nz);
|
|
os.close(fd);
|
|
if (got != n) { return nil, 0u64; };
|
|
buf[nz] = 0u8;
|
|
return buf, nz;
|
|
};
|
|
|
|
export fn main(argc: i32, argv: **u8) i32 = {
|
|
let src_cs: *u8 = nil;
|
|
let out_cs: *u8 = nil;
|
|
|
|
let i: i32 = 1;
|
|
for (i < argc) {
|
|
let a: *u8 = argv[i];
|
|
if (streq_cs(a, "-o")) {
|
|
i += 1;
|
|
if (i >= argc) {
|
|
os.write(2, "6c: -o requires arg\n".ptr, 20u64);
|
|
return 2;
|
|
};
|
|
out_cs = argv[i];
|
|
} else { if (a[0u64] == 45u8) {
|
|
os.write(2, "6c: unknown flag\n".ptr, 17u64);
|
|
return 2;
|
|
} else {
|
|
if (src_cs != nil) {
|
|
os.write(2, "6c: only one input\n".ptr, 19u64);
|
|
return 2;
|
|
};
|
|
src_cs = a;
|
|
}; };
|
|
i += 1;
|
|
};
|
|
|
|
if (src_cs == nil) {
|
|
os.write(2, "usage: 6c_ww [-o out.s] file.ww\n".ptr, 32u64);
|
|
return 2;
|
|
};
|
|
|
|
let buf: *u8;
|
|
let blen: u64;
|
|
buf, blen = slurp(src_cs);
|
|
if (buf == nil) {
|
|
os.write(2, "6c: cannot read input\n".ptr, 22u64);
|
|
return 1;
|
|
};
|
|
|
|
// Redirect fd 1 to the output file before any cgen emit runs.
|
|
// cgen.ww writes directly to fd 1; dup2 lets us reuse it without
|
|
// threading a file descriptor through the emit helpers.
|
|
if (out_cs != nil) {
|
|
let ofd: i32 = os.open(out_cs,
|
|
os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 420i32); // 0o644
|
|
if (ofd < 0) {
|
|
os.write(2, "6c: cannot open output\n".ptr, 23u64);
|
|
return 1;
|
|
};
|
|
if (os.dup2(ofd, 1i32) < 0) {
|
|
os.write(2, "6c: dup2 failed\n".ptr, 16u64);
|
|
os.close(ofd);
|
|
return 1;
|
|
};
|
|
os.close(ofd);
|
|
};
|
|
|
|
let ar: *arena = newarena();
|
|
let nlen: u64 = cstrlen(src_cs);
|
|
let fname: str = astrndup(ar, src_cs, nlen);
|
|
|
|
let l: lex;
|
|
lexinit(&l, ar, fname, buf, blen);
|
|
|
|
let ps: parser;
|
|
parserinit(&ps, ar, &l);
|
|
let f: *node = parsefile(&ps);
|
|
|
|
let cg: cgen;
|
|
cgen_init(&cg, ar);
|
|
cg_file(&cg, f);
|
|
|
|
if (l.errs > 0) { return 1; };
|
|
return 0;
|
|
};
|