ww: rename toolchain to w-prefix + hare-style build/run/test driver

Plan 9-style w-prefix on the per-arch tools, disambiguating from the
real Plan 9 6c/6a/6l in ref/plan9front/:

    cmd/wwc/      → cmd/wcc/        libwwc.a → libwcc.a
    cmd/6{c,a,l}  → cmd/w6{c,a,l}   binary names too
    test/wwc/     → test/wcc/       6 test files w/ w6 prefix
    selfhost/cmd  mirror in lockstep
    bootstrap/amd64/{w6c,w6a,w6l}   snapshot binaries (gitignored)
    WW_6{C,A,L}   → WW_W6{C,A,L}    env-var overrides

Plan 9 source-tree refs ("Plan 9 6c shape", ref/plan9front/, etc.)
preserved. Hare-style driver, both C and ww sides:

    ww test [path]   discover *_test.ww in a directory module, run
                     each; single-file mode for `ww test foo.ww`
    Module-by-name   `ww build foo` resolves to foo.ww or foo/foo.ww
                     via search path (cwd : -I dirs : $WW_LIB)
    Default-to-cwd   `ww build` / `ww test` build the cwd module
    Run pass-through `ww run path arg1 arg2` reaches the program

lib/os: getcwd (79) and getdents64 (217) syscalls power `.` resolution
and directory enumeration on the ww side.

Makefile: wwstage tool deps now include lib/os/os.ww (+ lib/strconv
for wwdump_ww) so lib/* edits force their rebuild instead of leaving
stale binaries — surfaced when test 995 first failed against a stale
w6c_ww built before the lib/os additions.

Test 993 byte-identical parity gate (C-side ww vs ww-side ww_ww on a
build corpus) stays green; all 19 tests pass.
This commit is contained in:
2026-05-11 13:49:27 +09:00
parent e217cd32d1
commit 2c33228b7e
96 changed files with 2129 additions and 973 deletions

View File

@@ -1,8 +1,8 @@
/*
* a.h 6a-private header. Modelled on Plan 9 cmd/6a/a.h, trimmed
* to the instruction subset that 6c emits.
* a.h w6a-private header. Modelled on Plan 9 cmd/6a/a.h, trimmed
* to the instruction subset that w6c emits.
*
* 6a is line-oriented and has no preprocessor: each non-blank, non-
* w6a is line-oriented and has no preprocessor: each non-blank, non-
* label line is one instruction. We read the whole file into a list
* of `Aprog`s, then encode and emit ELF64.
*/

View File

@@ -387,7 +387,7 @@ a_encode(Asm *a)
Asym *s = a_intern(a, p->to.sym);
a_addreloc(a, reloff, 2, s, -4);
} else {
fprintf(stderr, "6a: line %d: unsupported MOVQ shape\n", p->line);
fprintf(stderr, "w6a: line %d: unsupported MOVQ shape\n", p->line);
a->errs++;
}
break;
@@ -407,7 +407,7 @@ a_encode(Asm *a)
emit_modrm_mem(a, rcode(p->to.type),
p->from.reg, p->from.offset);
} else {
fprintf(stderr, "6a: line %d: unsupported MOVB shape\n", p->line);
fprintf(stderr, "w6a: line %d: unsupported MOVB shape\n", p->line);
a->errs++;
}
break;
@@ -421,7 +421,7 @@ a_encode(Asm *a)
emit_modrm_mem(a, rcode(p->to.type),
p->from.reg, p->from.offset);
} else {
fprintf(stderr, "6a: line %d: unsupported MOVZBQ shape\n", p->line);
fprintf(stderr, "w6a: line %d: unsupported MOVZBQ shape\n", p->line);
a->errs++;
}
break;
@@ -449,7 +449,7 @@ a_encode(Asm *a)
a_emit_byte(a, modrm(3,
rcode(p->from.type), rcode(p->to.type)));
} else {
fprintf(stderr, "6a: line %d: unsupported MOVL shape\n", p->line);
fprintf(stderr, "w6a: line %d: unsupported MOVL shape\n", p->line);
a->errs++;
}
break;
@@ -462,7 +462,7 @@ a_encode(Asm *a)
emit_modrm_mem(a, rcode(p->to.type),
p->from.reg, p->from.offset);
} else {
fprintf(stderr, "6a: line %d: unsupported MOVSXD shape\n", p->line);
fprintf(stderr, "w6a: line %d: unsupported MOVSXD shape\n", p->line);
a->errs++;
}
break;
@@ -479,7 +479,7 @@ a_encode(Asm *a)
sse_mr_load(a, 0xF2, 0x11, p->from.type,
p->to.reg, p->to.offset);
} else {
fprintf(stderr, "6a: line %d: unsupported MOVSD shape\n", p->line);
fprintf(stderr, "w6a: line %d: unsupported MOVSD shape\n", p->line);
a->errs++;
}
break;
@@ -516,7 +516,7 @@ a_encode(Asm *a)
sse_mr_load(a, 0xF3, 0x11, p->from.type,
p->to.reg, p->to.offset);
} else {
fprintf(stderr, "6a: line %d: unsupported MOVSS shape\n", p->line);
fprintf(stderr, "w6a: line %d: unsupported MOVSS shape\n", p->line);
a->errs++;
}
break;
@@ -665,7 +665,7 @@ a_encode(Asm *a)
break;
}
default:
fprintf(stderr, "6a: unsupported opcode %d on line %d\n", p->as, p->line);
fprintf(stderr, "w6a: unsupported opcode %d on line %d\n", p->as, p->line);
a->errs++;
}
}
@@ -673,7 +673,7 @@ a_encode(Asm *a)
/* second pass: patch fixups */
for (Fixup *f = fixups; f; f = f->next) {
if (!label_defined(a, f->label)) {
fprintf(stderr, "6a: undefined label '%s'\n", f->label);
fprintf(stderr, "w6a: undefined label '%s'\n", f->label);
a->errs++;
continue;
}

View File

@@ -1,5 +1,5 @@
/*
* lex.c character-level helpers for 6a's line-oriented parser.
* lex.c character-level helpers for w6a's line-oriented parser.
* The parser itself lives in parse.c; here we keep the tokenisers
* for identifiers and numbers so parse.c stays focused on syntax.
*/

View File

@@ -1,5 +1,5 @@
/*
* 6a amd64 assembler driver. Read .s, parse, encode, emit ELF .o.
* w6a amd64 assembler driver. Read .s, parse, encode, emit ELF .o.
*/
#include "a.h"
#include <stdio.h>
@@ -32,18 +32,18 @@ main(int argc, char **argv)
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "-o") == 0 && i + 1 < argc) out = argv[++i];
else if (argv[i][0] == '-') {
fprintf(stderr, "6a: unknown flag %s\n", argv[i]); return 2;
fprintf(stderr, "w6a: unknown flag %s\n", argv[i]); return 2;
} else if (src == NULL) src = argv[i];
else { fprintf(stderr, "6a: only one input\n"); return 2; }
else { fprintf(stderr, "w6a: only one input\n"); return 2; }
}
if (src == NULL || out == NULL) {
fputs("usage: 6a -o file.o file.s\n", stderr);
fputs("usage: w6a -o file.o file.s\n", stderr);
return 2;
}
char *buf;
u64 len;
if (slurp(src, &buf, &len) < 0) {
fprintf(stderr, "6a: cannot read %s\n", src);
fprintf(stderr, "w6a: cannot read %s\n", src);
return 1;
}
Asm a;
@@ -51,7 +51,7 @@ main(int argc, char **argv)
if (a_parse(&a) != 0) return 1;
if (a_encode(&a) != 0) return 1;
FILE *f = fopen(out, "wb");
if (f == NULL) { fprintf(stderr, "6a: cannot open %s\n", out); return 1; }
if (f == NULL) { fprintf(stderr, "w6a: cannot open %s\n", out); return 1; }
int rc = a_emit_elf(&a, f);
fclose(f);
free(buf);

View File

@@ -1,5 +1,5 @@
/*
* parse.c line-oriented parser for the asm subset emitted by 6c.
* parse.c line-oriented parser for the asm subset emitted by w6c.
*
* Grammar:
* line := blank | comment | label | text | instr
@@ -36,7 +36,7 @@ a_init(Asm *a, const char *file, const char *src, u64 len)
static void
err(Asm *a, const char *msg)
{
fprintf(stderr, "6a: %s:%d: %s\n", a->file, a->line, msg);
fprintf(stderr, "w6a: %s:%d: %s\n", a->file, a->line, msg);
a->errs++;
}

View File

@@ -1,8 +1,8 @@
/*
* 6.out.h amd64 instruction enum + register names. Mirrors the
* Plan 9 6c shape (cmd/6c/6.out.h) but trimmed to the subset that
* 6c emits and 6a consumes in this bootstrap. Each new opcode added
* here must also gain encoding support in cmd/6a/asm.c.
* w6c emits and w6a consumes in this bootstrap. Each new opcode added
* here must also gain encoding support in cmd/w6a/asm.c.
*/
#ifndef SIX_OUT_H
#define SIX_OUT_H

View File

@@ -10,7 +10,7 @@
* (SysV amd64 ABI). We don't yet handle struct-by-value or
* floats; floats and slices are deferred.
*
* Calling our own functions: emit CALL <name>(SB), let 6a/6l resolve.
* Calling our own functions: emit CALL <name>(SB), let w6a/w6l resolve.
* Calling C externs: same extern symbols are just unresolved CALLs.
*/
#include "gc.h"
@@ -605,7 +605,7 @@ cgexpr(Cg *c, Node *n, Local *locals)
* inspect n->lhs. UCOMISD/UCOMISS sets ZF/CF as if an
* unsigned compare, so the JA family is the right Jcc set
* regardless of how the operand types are signed. Plan 9's
* own 6c picks the same pattern (txt.c around AUCOMISD).
* own w6c picks the same pattern (txt.c around AUCOMISD).
* NaN handling: UCOMI sets PF on unordered; we ignore it,
* which means NaN compares behave like Hare's default. */
if (n->lhs && node_isfloat(n->lhs) &&
@@ -1837,7 +1837,7 @@ cgexpr(Cg *c, Node *n, Local *locals)
* with a single MOVQ. Without this, returning `o.p.val`
* silently leaves AX = o.p (the pointer) and the outer
* cast/use sees the pointer instead of the dereferenced
* field. (Surfaced building ww-6l.) */
* field. (Surfaced building ww-w6l.) */
if (n->lhs->kind == N_DOT) {
Type *lt = n->lhs->type;
Type *lu = (lt && lt->kind == TY_NAMED) ? lt->under : lt;
@@ -1927,7 +1927,7 @@ cgexpr(Cg *c, Node *n, Local *locals)
* size `b.data[i]` on a *u8 must read 1 byte, not 8.
*
* Scale the index in a register before pushing, because
* IMULQ on a memory operand isn't currently encoded by 6a
* IMULQ on a memory operand isn't currently encoded by w6a
* (modrm bits use mod=3 register form). */
cgexpr(c, n->rhs, locals);
if (esz > 1) {
@@ -2614,7 +2614,7 @@ cgfn(Cg *c, FILE *out, Node *fn)
}
/* Emit DATA directives for top-level `def` constants whose value is
* an integer/rune literal. The 6a side stores the bytes inside .text
* an integer/rune literal. The w6a side stores the bytes inside .text
* and accesses are RIP-relative.
*/
static void

View File

@@ -1,5 +1,5 @@
/*
* gc.h 6c-private header: Prog/Adr structs, scratch register set,
* gc.h w6c-private header: Prog/Adr structs, scratch register set,
* stack-frame state. Plan 9 cmd/6c/gc.h shape, trimmed.
*/
#ifndef SIX_GC_H

View File

@@ -1,6 +1,6 @@
/*
* 6c amd64 compiler driver. Reads a .ww source file, runs the
* libwwc frontend (lex parse check), then walks the typed AST
* 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).
*/
@@ -38,24 +38,24 @@ main(int argc, char **argv)
if (strcmp(a, "-o") == 0 && i + 1 < argc) {
out = argv[++i];
} else if (a[0] == '-') {
fprintf(stderr, "6c: unknown flag %s\n", a);
fprintf(stderr, "w6c: unknown flag %s\n", a);
return 2;
} else if (src == NULL) {
src = a;
} else {
fprintf(stderr, "6c: only one input supported\n");
fprintf(stderr, "w6c: only one input supported\n");
return 2;
}
}
if (src == NULL) {
fputs("usage: 6c [-o out.s] file.ww\n", stderr);
fputs("usage: w6c [-o out.s] file.ww\n", stderr);
return 2;
}
char *buf;
u64 len;
if (slurp(src, &buf, &len) < 0) {
fprintf(stderr, "6c: %s: cannot read\n", src);
fprintf(stderr, "w6c: %s: cannot read\n", src);
return 1;
}
@@ -78,7 +78,7 @@ main(int argc, char **argv)
if (out) {
of = fopen(out, "wb");
if (of == NULL) {
fprintf(stderr, "6c: cannot open %s\n", out);
fprintf(stderr, "w6c: cannot open %s\n", out);
return 1;
}
}

View File

@@ -1,7 +1,7 @@
/*
* txt.c print a Prog list as Plan 9-flavoured amd64 asm text.
*
* Format we emit (and that 6a expects):
* Format we emit (and that w6a expects):
* TEXT name<framesize>
* MOVQ $1, AX
* MOVQ AX, name(SB) ; extern symbol

View File

@@ -86,11 +86,11 @@ l_load_so(Lnk *l, const char *path)
u8 *buf;
u64 len;
if (l_read_all(path, &buf, &len) < 0) {
fprintf(stderr, "6l: %s: cannot read\n", path);
fprintf(stderr, "w6l: %s: cannot read\n", path);
return -1;
}
if (len < sizeof(Ehdr)) {
fprintf(stderr, "6l: %s: short ELF\n", path);
fprintf(stderr, "w6l: %s: short ELF\n", path);
free(buf);
return -1;
}
@@ -99,12 +99,12 @@ l_load_so(Lnk *l, const char *path)
|| eh->e_ident[4] != 2
|| eh->e_machine != 62 /* EM_X86_64 */
|| eh->e_type != ET_DYN) {
fprintf(stderr, "6l: %s: not an amd64 ET_DYN\n", path);
fprintf(stderr, "w6l: %s: not an amd64 ET_DYN\n", path);
free(buf);
return -1;
}
if (eh->e_shoff == 0 || eh->e_shnum == 0) {
fprintf(stderr, "6l: %s: stripped .so unsupported\n", path);
fprintf(stderr, "w6l: %s: stripped .so unsupported\n", path);
free(buf);
return -1;
}
@@ -119,7 +119,7 @@ l_load_so(Lnk *l, const char *path)
if (sh[i].sh_type == SHT_GNU_VERDEF) idx_verdef = i;
}
if (idx_dynsym < 0) {
fprintf(stderr, "6l: %s: no .dynsym\n", path);
fprintf(stderr, "w6l: %s: no .dynsym\n", path);
free(buf);
return -1;
}

View File

@@ -157,7 +157,7 @@ l_emit_dyn_elf(Lnk *l, FILE *f, u64 base, u64 entry)
}
for (int i = 0; i < N; i++) {
if (dynsyms[i] == NULL) {
fprintf(stderr, "6l: dynout: no sym for plt_idx %d\n", i);
fprintf(stderr, "w6l: dynout: no sym for plt_idx %d\n", i);
free(dynsyms);
return 1;
}
@@ -288,7 +288,7 @@ l_emit_dyn_elf(Lnk *l, FILE *f, u64 base, u64 entry)
}
}
if (!matched) {
fprintf(stderr, "6l: dynout: unmatched version %s for %s\n",
fprintf(stderr, "w6l: dynout: unmatched version %s for %s\n",
vname, dynsyms[j]->name);
versym_for[j] = VER_NDX_GLOBAL;
}
@@ -529,7 +529,7 @@ l_emit_dyn_elf(Lnk *l, FILE *f, u64 base, u64 entry)
}
dynamic[k].d_tag = DT_NULL; dynamic[k].d_val = 0; k++;
if ((u64)k != ndyn) {
fprintf(stderr, "6l: dynamic entry count mismatch\n");
fprintf(stderr, "w6l: dynamic entry count mismatch\n");
return 1;
}
}
@@ -540,7 +540,7 @@ l_emit_dyn_elf(Lnk *l, FILE *f, u64 base, u64 entry)
for (Lrel *r = l->rels; r; r = r->next) {
if (r->sym == NULL || !r->sym->is_dyn) continue;
if (r->kind != R_X86_64_PC32 && r->kind != R_X86_64_PLT32) {
fprintf(stderr, "6l: dynamic reloc kind %d unsupported\n",
fprintf(stderr, "w6l: dynamic reloc kind %d unsupported\n",
r->kind);
free(dynsyms); free(sos_used); free(soname_str);
free(symname_str); free(dynstr); free(dynsym);

View File

@@ -1,6 +1,6 @@
/*
* l.h 6l-private header. Loads relocatable ELF64 .o files (the
* format produced by 6a) and links them into a static executable.
* l.h w6l-private header. Loads relocatable ELF64 .o files (the
* format produced by w6a) and links them into a static executable.
*
* No archives yet (phase 8). No dynamic linking ever.
*/

View File

@@ -1,10 +1,10 @@
/*
* 6l amd64 linker. Reads relocatable ELF .o files (from 6a) plus
* w6l amd64 linker. Reads relocatable ELF .o files (from w6a) plus
* .a archives, resolves, relocates, writes a static ELF executable.
* Dynamic linking against .so files is the next increment; the -L/-l
* flag plumbing here is its first step.
*
* 6l -o out [-L<dir>...] [-l<name>...] file1.o file2.o ...
* w6l -o out [-L<dir>...] [-l<name>...] file1.o file2.o ...
*
* The first symbol named "_start" defined among the inputs becomes
* the entry point. If none is found, fall back to "main".
@@ -80,14 +80,14 @@ main(int argc, char **argv)
} else if (strcmp(argv[i], "-l") == 0 && i + 1 < argc) {
lflags[n_lflags++] = argv[++i];
} else if (argv[i][0] == '-') {
fprintf(stderr, "6l: unknown flag %s\n", argv[i]);
fprintf(stderr, "w6l: unknown flag %s\n", argv[i]);
return 2;
} else {
inputs[ninputs++] = argv[i];
}
}
if (out == NULL || ninputs == 0) {
fputs("usage: 6l -o exe [-L<dir>...] [-l<name>...] "
fputs("usage: w6l -o exe [-L<dir>...] [-l<name>...] "
"file1.o [file2.o...]\n", stderr);
return 2;
}
@@ -98,7 +98,7 @@ main(int argc, char **argv)
for (int i = 0; i < n_lflags; i++) {
const char *p = resolve_lib(lflags[i], libdirs, n_libdirs);
if (p == NULL) {
fprintf(stderr, "6l: cannot find -l%s\n", lflags[i]);
fprintf(stderr, "w6l: cannot find -l%s\n", lflags[i]);
return 1;
}
inputs[ninputs++] = p;
@@ -120,13 +120,13 @@ main(int argc, char **argv)
Lsym *entry = l_lookup(&l, "_start");
if (entry == NULL || !entry->defined) entry = l_lookup(&l, "main");
if (entry == NULL || !entry->defined) {
fprintf(stderr, "6l: no _start or main symbol defined\n");
fprintf(stderr, "w6l: no _start or main symbol defined\n");
return 1;
}
FILE *f = fopen(out, "wb");
if (f == NULL) {
fprintf(stderr, "6l: cannot open %s\n", out);
fprintf(stderr, "w6l: cannot open %s\n", out);
return 1;
}
int rc = l_emit_elf(&l, f, base, base + 0x1000 + entry->val);

View File

@@ -1,5 +1,5 @@
/*
* obj.c load an ELF64 relocatable object emitted by 6a, append its
* obj.c load an ELF64 relocatable object emitted by w6a, append its
* .text bytes to the combined image, and pull its symbols and
* relocations into the global tables (with offsets adjusted to the
* combined section).
@@ -251,7 +251,7 @@ load_image(Lnk *l, const char *path, u8 *buf, u64 len)
Ehdr *eh = (Ehdr *)buf;
if (memcmp(eh->e_ident, "\x7f""ELF", 4) != 0 || eh->e_ident[4] != 2
|| eh->e_machine != EM_X86_64 || eh->e_type != ET_REL) {
fprintf(stderr, "6l: %s: not an amd64 ELF64 relocatable\n", path);
fprintf(stderr, "w6l: %s: not an amd64 ELF64 relocatable\n", path);
free(buf);
return -1;
}
@@ -271,7 +271,7 @@ load_image(Lnk *l, const char *path, u8 *buf, u64 len)
idx_rela = i;
}
if (idx_text < 0 || idx_symtab < 0) {
fprintf(stderr, "6l: %s: missing .text or .symtab\n", path);
fprintf(stderr, "w6l: %s: missing .text or .symtab\n", path);
free(buf);
return -1;
}
@@ -303,7 +303,7 @@ load_image(Lnk *l, const char *path, u8 *buf, u64 len)
if (symtab[i].st_shndx != 0 /* SHN_UNDEF */
&& symtab[i].st_shndx == idx_text) {
if (gs->defined) {
fprintf(stderr, "6l: %s: duplicate symbol %s\n",
fprintf(stderr, "w6l: %s: duplicate symbol %s\n",
path, nm);
l->errs++;
} else {

View File

@@ -50,7 +50,7 @@ l_resolve(Lnk *l)
for (Lrel *r = l->rels; r; r = r->next) {
if (r->sym == NULL) continue;
if (!r->sym->defined && !r->sym->is_dyn) {
fprintf(stderr, "6l: undefined reference to '%s'\n",
fprintf(stderr, "w6l: undefined reference to '%s'\n",
r->sym->name);
l->errs++;
}
@@ -86,7 +86,7 @@ l_relocate(Lnk *l, u64 base)
break;
}
default:
fprintf(stderr, "6l: unsupported reloc kind %d\n",
fprintf(stderr, "w6l: unsupported reloc kind %d\n",
r->kind);
l->errs++;
}

View File

@@ -460,7 +460,7 @@ cexpr(Checker *c, Node *n)
if (ft == ty_err) {
/* Walk args anyway so cgen sees real types. The
* common case is a module-qualified call whose leaf
* isn't in this scope (raw 6c on a single file with
* isn't in this scope (raw w6c on a single file with
* `use mod;` but no driver concatenation). */
for (Node *a = n->list; a; a = a->next)
(void)cexpr(c, a);

View File

@@ -1,5 +1,5 @@
/*
* ww.h central header for libwwc.a (the ww frontend library).
* ww.h central header for libwcc.a (the ww frontend library).
*
* Plan 9 in spirit. This file mirrors cc/cc.h's role: one shared
* header that declares everything every translation unit in the

View File

@@ -2,28 +2,36 @@
* ww — the user-facing driver. Plan 9 cc(1) / Hare hare(1) analogue.
*
* Pipeline:
* ww build foo.ww → 6c foo.ww > foo.s ; 6a foo.s > foo.o ;
* 6l -o foo foo.o <runtime.o>
* ww build foo.ww → w6c foo.ww > foo.s ; w6a foo.s > foo.o ;
* w6l -o foo foo.o <runtime.o>
* ww run foo.ww → build then exec ./foo
*
* Tool paths default to siblings of $0 (so a fresh build runs out of
* out/bin/), and can be overridden with WW_6C / WW_6A / WW_6L.
* out/bin/), and can be overridden with WW_W6C / WW_W6A / WW_W6L.
*/
#include "ww.h"
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <dirent.h>
#include <libgen.h>
static const char *usage =
"usage: ww [-V] <subcommand> [args...]\n"
" -V print version and exit\n"
" build <path> compile module to a static binary\n"
" run <path> build then exec\n"
" test <path> build and run module tests\n"
" fmt <path> reformat ww source\n"
" version print version and exit\n";
" -V print version and exit\n"
" build [path] compile module to a static binary (path defaults to cwd)\n"
" run [path] ... build then exec, passing extra args to the program\n"
" test [path] build and run *_test.ww in the module (path defaults to cwd)\n"
" fmt <path> reformat ww source\n"
" version print version and exit\n"
"\n"
" path forms:\n"
" foo.ww literal file\n"
" foo search cwd, -I dirs, then $WW_LIB for foo.ww or foo/foo.ww\n"
" lib/foo directory: build lib/foo/foo.ww\n"
" . build the cwd's <basename>.ww\n";
static char *self_dir; /* directory containing this binary */
@@ -159,9 +167,9 @@ static int
build_one(const char *src, const char *out, const char *extra_includes,
const char *extra_libs, const char *extra_libdirs)
{
const char *c6 = toolpath("WW_6C", "6c");
const char *a6 = toolpath("WW_6A", "6a");
const char *l6 = toolpath("WW_6L", "6l");
const char *c6 = toolpath("WW_W6C", "w6c");
const char *a6 = toolpath("WW_W6A", "w6a");
const char *l6 = toolpath("WW_W6L", "w6l");
const char *libdir = getenv("WW_LIB");
if (libdir == NULL || libdir[0] == 0) {
static char libbuf[1024];
@@ -216,12 +224,12 @@ build_one(const char *src, const char *out, const char *extra_includes,
char cmd[4096];
snprintf(cmd, sizeof cmd, "%s -o %s %s", c6, asmf, combined);
if (run(cmd) != 0) {
fprintf(stderr, "ww: 6c failed\n");
fprintf(stderr, "ww: w6c failed\n");
return 1;
}
snprintf(cmd, sizeof cmd, "%s -o %s %s", a6, obj, asmf);
if (run(cmd) != 0) {
fprintf(stderr, "ww: 6a failed\n");
fprintf(stderr, "ww: w6a failed\n");
return 1;
}
/* Link runtime: prefer libwwrt.a (selective archive pull) but
@@ -238,7 +246,7 @@ build_one(const char *src, const char *out, const char *extra_includes,
snprintf(a2, sizeof a2, "%s/../obj/rt/syscall.o", self_dir);
snprintf(rtargs, sizeof rtargs, "%s %s", a1, a2);
}
/* -L<dir> goes before -l<name> so 6l can resolve the latter. */
/* -L<dir> goes before -l<name> so w6l can resolve the latter. */
const char *libargs = (extra_libs && extra_libs[0]) ? extra_libs : "";
const char *libdirset = (extra_libdirs && extra_libdirs[0]) ? extra_libdirs : "";
snprintf(cmd, sizeof cmd, "%s -o %s %s %s%s%s%s%s",
@@ -246,7 +254,7 @@ build_one(const char *src, const char *out, const char *extra_includes,
libdirset[0] ? " " : "", libdirset,
libargs[0] ? " " : "", libargs);
if (run(cmd) != 0) {
fprintf(stderr, "ww: 6l failed\n");
fprintf(stderr, "ww: w6l failed\n");
return 1;
}
return 0;
@@ -259,70 +267,292 @@ do_version(void)
return 0;
}
/* Compose the standard module search path: cwd : <extra-includes> : $WW_LIB
* source dir. The `extra` string is colon-separated -I dirs from argv. */
static const char *
search_path(const char *extra, char *buf, size_t bufsz)
{
const char *libdir = getenv("WW_SRCLIB");
static char libbuf[1024];
if (libdir == NULL || libdir[0] == 0) {
libdir = getenv("WW_LIB");
}
if (libdir == NULL || libdir[0] == 0) {
snprintf(libbuf, sizeof libbuf, "%s/../../lib", self_dir);
if (access(libbuf, 0) == 0) libdir = libbuf;
else if (access("lib", 0) == 0) libdir = "lib";
else {
snprintf(libbuf, sizeof libbuf, "%s/../lib", self_dir);
libdir = libbuf;
}
}
if (extra && extra[0])
snprintf(buf, bufsz, ".:%s:%s", extra, libdir);
else
snprintf(buf, bufsz, ".:%s", libdir);
return buf;
}
/* basename_no_ext: last path segment with any trailing ".ww" stripped. */
static void
basename_no_ext(const char *path, char *out, size_t outsz)
{
const char *base = strrchr(path, '/');
base = base ? base + 1 : path;
snprintf(out, outsz, "%s", base);
char *dot = strrchr(out, '.');
if (dot && strcmp(dot, ".ww") == 0) *dot = '\0';
}
/* resolve_module: turn a name into a concrete .ww file path.
* foo.ww → use as-is if it exists
* <existing dir> → <dir>/<basename>.ww (Hare module convention)
* . → <cwd-basename>.ww in the cwd
* foo (bare) → walk cwd:incs:WW_LIB for foo.ww or foo/foo.ww
* Returns 1 on success and writes the path to `out`, 0 on failure. */
static int
resolve_module(const char *name, const char *incs, char *out, size_t outsz)
{
struct stat st;
if (stat(name, &st) == 0) {
if (S_ISREG(st.st_mode)) {
snprintf(out, outsz, "%s", name);
return 1;
}
if (S_ISDIR(st.st_mode)) {
char buf[1024];
const char *base;
if (strcmp(name, ".") == 0) {
if (getcwd(buf, sizeof buf) == NULL) return 0;
} else {
snprintf(buf, sizeof buf, "%s", name);
size_t bl = strlen(buf);
while (bl > 1 && buf[bl-1] == '/') buf[--bl] = '\0';
}
const char *b = strrchr(buf, '/');
base = b ? b + 1 : buf;
snprintf(out, outsz, "%s/%s.ww", name, base);
if (access(out, 0) == 0) return 1;
return 0;
}
}
char sp[4096];
search_path(incs, sp, sizeof sp);
if (locate_import(sp, name, out, outsz)) return 1;
return 0;
}
/* Append `path` to a heap string-array. Caller frees each entry + the array. */
static void
strarr_push(char ***arr, int *n, int *cap, const char *path)
{
if (*n + 1 > *cap) {
*cap = *cap ? *cap * 2 : 8;
*arr = realloc(*arr, *cap * sizeof **arr);
}
(*arr)[(*n)++] = strdup(path);
}
/* collect_tests: enumerate _test.ww files in <dir> (no recursion).
* Returns 0 on success and writes the file list + count, -1 on failure. */
static int
collect_tests(const char *dir, char ***files, int *n)
{
DIR *d = opendir(dir);
if (!d) return -1;
*files = NULL;
*n = 0;
int cap = 0;
struct dirent *ent;
while ((ent = readdir(d)) != NULL) {
const char *nm = ent->d_name;
size_t nl = strlen(nm);
const char *suf = "_test.ww";
size_t sl = strlen(suf);
if (nl <= sl) continue;
if (strcmp(nm + nl - sl, suf) != 0) continue;
char path[1024];
snprintf(path, sizeof path, "%s/%s", dir, nm);
strarr_push(files, n, &cap, path);
}
closedir(d);
return 0;
}
/* Parse the standard -I/-L/-l flags into incs/libdirs/libs. The first
* non-flag positional becomes *src_out. Returns the index past the last
* arg consumed for positionals (so callers can pick up trailing args). */
static int
parse_build_flags(int argc, char **argv,
char *incs, size_t incsz,
char *libdirs, size_t libdirsz,
char *libs, size_t libsz,
const char **src_out)
{
*src_out = NULL;
int i = 0;
for (; i < argc; i++) {
if (strncmp(argv[i], "-l", 2) == 0 && argv[i][2]) {
size_t n = strlen(libs);
snprintf(libs + n, libsz - n,
"%s%s", n ? " " : "", argv[i]);
} else if (strcmp(argv[i], "-l") == 0 && i + 1 < argc) {
size_t n = strlen(libs);
snprintf(libs + n, libsz - n,
"%s-l%s", n ? " " : "", argv[++i]);
} else if (strcmp(argv[i], "-L") == 0 && i + 1 < argc) {
size_t n = strlen(libdirs);
snprintf(libdirs + n, libdirsz - n,
"%s-L%s", n ? " " : "", argv[++i]);
} else if (strncmp(argv[i], "-L", 2) == 0 && argv[i][2]) {
size_t n = strlen(libdirs);
snprintf(libdirs + n, libdirsz - n,
"%s%s", n ? " " : "", argv[i]);
} else if (strcmp(argv[i], "-I") == 0 && i + 1 < argc) {
size_t n = strlen(incs);
snprintf(incs + n, incsz - n,
"%s%s", n ? ":" : "", argv[++i]);
} else if (strncmp(argv[i], "-I", 2) == 0 && argv[i][2]) {
size_t n = strlen(incs);
snprintf(incs + n, incsz - n,
"%s%s", n ? ":" : "", argv[i] + 2);
} else if (*src_out == NULL) {
*src_out = argv[i];
} else {
break; /* leave remaining argv to caller (run-args) */
}
}
return i;
}
static int
do_build(int argc, char **argv)
{
const char *src = NULL;
char libs[2048] = {0}; /* -l<name> entries, space-separated */
char libdirs[2048] = {0}; /* -L<dir> entries, space-separated */
char libs[2048] = {0};
char libdirs[2048] = {0};
char incs[2048] = {0};
for (int i = 0; i < argc; i++) {
if (strncmp(argv[i], "-l", 2) == 0 && argv[i][2]) {
size_t n = strlen(libs);
snprintf(libs + n, sizeof libs - n,
"%s%s", n ? " " : "", argv[i]);
} else if (strcmp(argv[i], "-l") == 0 && i + 1 < argc) {
size_t n = strlen(libs);
snprintf(libs + n, sizeof libs - n,
"%s-l%s", n ? " " : "", argv[++i]);
} else if (strcmp(argv[i], "-L") == 0 && i + 1 < argc) {
size_t n = strlen(libdirs);
snprintf(libdirs + n, sizeof libdirs - n,
"%s-L%s", n ? " " : "", argv[++i]);
} else if (strncmp(argv[i], "-L", 2) == 0 && argv[i][2]) {
size_t n = strlen(libdirs);
snprintf(libdirs + n, sizeof libdirs - n,
"%s%s", n ? " " : "", argv[i]);
} else if (strcmp(argv[i], "-I") == 0 && i + 1 < argc) {
size_t n = strlen(incs);
snprintf(incs + n, sizeof incs - n,
"%s%s", n ? ":" : "", argv[++i]);
} else if (strncmp(argv[i], "-I", 2) == 0 && argv[i][2]) {
size_t n = strlen(incs);
snprintf(incs + n, sizeof incs - n,
"%s%s", n ? ":" : "", argv[i] + 2);
} else if (src == NULL) {
src = argv[i];
}
parse_build_flags(argc, argv, incs, sizeof incs,
libdirs, sizeof libdirs, libs, sizeof libs, &src);
if (src == NULL) src = "."; /* default: build cwd */
char resolved[1024];
if (!resolve_module(src, incs, resolved, sizeof resolved)) {
fprintf(stderr, "ww build: cannot find module %s\n", src);
return 1;
}
if (src == NULL) { fputs("ww build: missing source\n", stderr); return 2; }
char out[1024];
const char *base = strrchr(src, '/');
base = base ? base + 1 : src;
snprintf(out, sizeof out, "%s", base);
char *dot = strrchr(out, '.');
if (dot && strcmp(dot, ".ww") == 0) *dot = '\0';
return build_one(src, out, incs, libs, libdirs);
basename_no_ext(resolved, out, sizeof out);
return build_one(resolved, out, incs, libs, libdirs);
}
static int
do_run(int argc, char **argv)
{
if (argc < 1) { fputs("ww run: missing source\n", stderr); return 2; }
const char *src = NULL;
char libs[2048] = {0};
char libdirs[2048] = {0};
char incs[2048] = {0};
int next = parse_build_flags(argc, argv, incs, sizeof incs,
libdirs, sizeof libdirs, libs, sizeof libs, &src);
if (src == NULL) src = ".";
char resolved[1024];
if (!resolve_module(src, incs, resolved, sizeof resolved)) {
fprintf(stderr, "ww run: cannot find module %s\n", src);
return 1;
}
char tmp[1024];
snprintf(tmp, sizeof tmp, "/tmp/ww_run_%d", getpid());
if (build_one(argv[0], tmp, "", "", "") != 0) return 1;
int rc = run(tmp);
if (build_one(resolved, tmp, incs, libs, libdirs) != 0) return 1;
/* exec the built binary with any trailing argv as its argv. */
pid_t pid = fork();
if (pid < 0) { perror("ww: fork"); unlink(tmp); return 1; }
if (pid == 0) {
int n_extra = argc - next;
char **xargv = calloc((size_t)n_extra + 2, sizeof *xargv);
xargv[0] = tmp;
for (int i = 0; i < n_extra; i++) xargv[i+1] = argv[next + i];
xargv[n_extra+1] = NULL;
execv(tmp, xargv);
perror("ww: exec");
_exit(127);
}
int status = 0;
waitpid(pid, &status, 0);
unlink(tmp);
return rc;
if (WIFEXITED(status)) return WEXITSTATUS(status);
return 1;
}
static int
do_test(int argc, char **argv)
{
(void)argc; (void)argv;
fputs("ww: test: not implemented in this phase\n", stderr);
return 1;
const char *target = (argc > 0) ? argv[0] : ".";
struct stat st;
if (stat(target, &st) != 0) {
/* not a literal path — try module resolution and run as
* a single test program. */
char resolved[1024];
if (!resolve_module(target, "", resolved, sizeof resolved)) {
fprintf(stderr, "ww test: cannot find %s\n", target);
return 1;
}
char tmp[1024];
snprintf(tmp, sizeof tmp, "/tmp/ww_test_%d", getpid());
if (build_one(resolved, tmp, "", "", "") != 0) return 1;
int rc = run(tmp);
unlink(tmp);
return rc;
}
if (S_ISREG(st.st_mode)) {
/* single .ww file — build+run it. */
char tmp[1024];
snprintf(tmp, sizeof tmp, "/tmp/ww_test_%d", getpid());
if (build_one(target, tmp, "", "", "") != 0) return 1;
int rc = run(tmp);
unlink(tmp);
return rc;
}
if (!S_ISDIR(st.st_mode)) {
fprintf(stderr, "ww test: %s is neither file nor directory\n", target);
return 1;
}
/* directory — run every *_test.ww inside. */
char **files = NULL;
int n = 0;
if (collect_tests(target, &files, &n) < 0) {
fprintf(stderr, "ww test: cannot read directory %s\n", target);
return 1;
}
if (n == 0) {
fprintf(stderr, "ww test: no *_test.ww files in %s\n", target);
return 1;
}
int pass = 0, fail = 0;
for (int i = 0; i < n; i++) {
char tmp[1024];
snprintf(tmp, sizeof tmp, "/tmp/ww_test_%d_%d", getpid(), i);
const char *label = strrchr(files[i], '/');
label = label ? label + 1 : files[i];
int rc = build_one(files[i], tmp, target, "", "");
if (rc != 0) {
fprintf(stderr, "FAIL %s (build)\n", label);
fail++;
} else {
int xrc = run(tmp);
if (xrc == 0) {
printf("ok %s\n", label);
pass++;
} else {
printf("FAIL %s (rc=%d)\n", label, xrc);
fail++;
}
}
unlink(tmp);
free(files[i]);
}
free(files);
printf("ww test: %d pass, %d fail\n", pass, fail);
return fail == 0 ? 0 : 1;
}
static int

View File

@@ -3,7 +3,7 @@
*
* Reads a .ww file and writes either a token stream or an AST
* s-expression to stdout, byte-for-byte stable across runs. It is the
* diff anchor for self-host: the C-side libwwc and the future ww-side
* diff anchor for self-host: the C-side libwcc and the future ww-side
* frontend must produce the same dump for the same input.
*
* wwdump -t file.ww tokens, one per line: "<file>:<l>:<c> <kind> [val]"