Commit Graph

1078 Commits

Author SHA1 Message Date
ebcc8f2d09 wcc/cgen: lift helpers→cgen_util, fn/file→cgen_decl 2026-05-11 15:39:39 +09:00
328bcd743c wcc/cgen: split cgexpr→cgen_expr.ww, cgstmt→cgen_stmt.ww (rob pike #5) 2026-05-11 15:35:16 +09:00
e301a198f4 wcc: c-side mangles private decls; main exempt as entry-point convention 2026-05-11 15:29:29 +09:00
895f221b6c wcc: mangle private decls as <module>.<name> via // MODULE: marker 2026-05-11 15:16:22 +09:00
d7036be0e5 ww+wcc: emit and lex // MODULE: <name> directive in combined.ww 2026-05-11 14:54:56 +09:00
5d91ee778e selfhost/cmd/w6l: drop d_/rd_/wr_ prefixes on dyn/dynout/obj helpers 2026-05-11 14:40:05 +09:00
fc09320eb7 selfhost/cmd/{ww,wwdump}: drop snake_case from main.ww helpers 2026-05-11 14:38:59 +09:00
7ed6b39744 selfhost/cmd/wcc/typ: drop ty_ prefix underscore on tctx primitives 2026-05-11 14:38:20 +09:00
6204c4cd27 selfhost/cmd/wcc: drop snake_case from 119 cross-cutting identifiers 2026-05-11 14:37:45 +09:00
fce0a54d62 selfhost/cmd/w6l: drop l_* prefix from exports + snake fields/helpers 2026-05-11 14:34:11 +09:00
f250bcfb3a selfhost/cmd/w6a: drop snake_case from lex/parse/asm/obj exports 2026-05-11 14:28:27 +09:00
4f6f1d8edf selfhost/cmd/{w6c,w6a,w6l}: drop snake_case from main.ww drivers 2026-05-11 14:22:09 +09:00
2918013c1a lib: drop snake_case from io/types/bufio/net/os exports 2026-05-11 14:15:53 +09:00
2c33228b7e 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.
2026-05-11 13:49:27 +09:00
e217cd32d1 6l: port ET_DYN dynamic linking to the ww side
Ports cmd/6l/{dyn,dynout}.c into selfhost/cmd/6l/{dyn,dynout}.ww:
ET_DYN .so loading + PT_INTERP/PT_DYNAMIC ELF emission with .rela.plt,
.gnu.version_r, BIND_NOW. lsym grows dyn fields; pass.ww promotes
undefs to dyn; out.ww dispatches; main.ww takes -L/-l. The ww driver
forwards -L/-l to 6l_ww so 'ww_ww build snake.ww -L /usr/lib -l ncurses
-l c' runs without cc.

Test 996 pins byte-identical output to C-6l on snake.

'make bootstrap' gains a fourth stage with cmp ww3 == ww4, proving
ww3 is byte-stable when used as a compiler — not just a coincidental
two-stage equilibrium.

Four wwstage 6c cgen quirks surfaced and are documented in dynout.ww's
header (two-level field-write through a pointer field, (scalar, str)
tuple returns, def : str, ≤6 arg calling convention).
2026-05-11 12:47:36 +09:00
edfc4273f6 nocc: stage-0 bootstrap path (binaries gitignored)
Sets up the cc-free fresh-checkout flow without yet committing the
stage-0 binaries. The pieces are in place; flipping the switch is
one `git add -f` away when the compiler is judged stable.

  bootstrap/<arch>/{ww,6c,6a,6l}   stage-0 binaries (gitignored)
  bootstrap/README.md              layout + workflow

Two new targets:

  make bootstrap-snapshot   populates bootstrap/$(ARCH)/ from the
                            currently-built wwstage
  make nocc                 starts from bootstrap/$(ARCH)/, rebuilds
                            the wwstage from source, gates on
                            stage-0 == rebuilt byte-for-byte. Lands
                            in $(OUT)/nocc/ so $(OUT)/ stays
                            untouched. cc is never invoked.

The fixed-point gate has the same shape as `make bootstrap`, just
the entry point flipped: that target trusts cstage; nocc trusts
the checked-in (or local-snapshot) stage-0 binaries.

Implementation notes:

- libwwrt.a is assembled with stage-0 6a, members ordered to match
  $(RT_OBJ) so the embedded symbol table reproduces the cstage
  build byte-for-byte.
- The driver's default lib path is $self_dir/../../lib, which under
  $(NOCC_BIN) resolves to $(NOCC_OUT)/lib (libwwrt only). Each
  ww-build invocation passes -I $(CURDIR)/lib so `use os` etc.
  resolve to source.
- Stage-0 binaries are duplicated under both natural names (ww/6c/
  6a/6l) and _ww-suffixed names so the wwstage driver's
  hard-coded join_path_lit(self_dir, "6c_ww") still finds them.
  When cmd/wwc/ gets deleted at v1.0, that duplication and the
  _ww suffix in the driver both go away.
2026-05-11 11:51:34 +09:00
37bffa5284 test: 995_self_rebuild — wwstage rebuilds itself byte-identical
Drives ww_ww (which already shells to 6c_ww/6a_ww/6l_ww) over each
wwstage tool's source and diffs the resulting binary against the
cstage-built canonical in $BIN. A green run means the toolchain
can recompile itself end-to-end without invoking cc, modulo the
cold-start binary that brings the wwstage into existence.

Stricter than `make bootstrap`: that loop pins wwdump's cgen
self-stabilising; this pins all five wwstage tools (6c, 6a, 6l,
ww, wwdump) round-tripping through the wwstage pipeline.

The .combined.ww refreshes are the expander picking up the
parser/cgen changes from the prior commit. selfhost/cmd/6c/
gains its main.combined.ww for the first time — 995 builds it,
994 reads it.
2026-05-11 11:41:12 +09:00
af8836cc8e parse/cgen: tuple-destructure assign + extra type-inference holes
Closes the gaps that kept ww_ww from rebuilding the wwstage byte-
for-byte. The pre-existing parser silently produced a broken AST
on `a, b = fn();` (a no-op exprstmt + a single assign that lost
the second tuple slot); the cgen leaned on a handful of cases
that the surface type-walker didn't yet cover, so 6a/6l/ww built
through wwstage drifted by a handful of bytes per file.

Parser:

- Multi-assign in parsestmt mirrors cmd/wwc/parse.c:1015-1031. If
  parseexpr is followed by `,`, switch into N_MASSIGN: collect
  the chained lvalues with parsebin(parseunary, 1) so they don't
  eat the trailing `=`, then absorb `= rhs;` and emit the node.

Cgen:

- N_MASSIGN handler stores AX into l0's slot, pops DX into l1's
  slot. Same shape as cmd/6c/cgen.c:2424-2440. Lvalues beyond
  two are dropped (C drops them too).
- N_INDEX added to node_isunsigned: `p[i]` where p is *u8/[]u8/
  [N]u8 now flags unsigned, so `p[i] >= 48u8` emits JAE instead
  of JGE. Bit 6a's parsenum.
- node_isstr's N_DOT branch now handles chained dots via
  dot_inner_struct_ptr, so `p.to.asym` (Adr.asym is str) flags
  as str and push_args_rev pushes both halves.
- index_base_esz returns 1 for str-typed struct fields. Was
  defaulting to 8, so `node.s[i]` scaled by 8 and used MOVQ
  instead of MOVZBQ. Bit ww/visit_seen.
- N_LET with no initializer zero-inits the slot when the
  underlying type is an 8-byte primitive (pointer, fn-ptr,
  i64/u64, ...) — matches cmd/6c/cgen.c:2181. Structs,
  strings, slices, etc. are left for per-field writes, even
  when their slot rounds up to 8 in scan_locals. New helper
  type_is_8byte_primitive walks the type AST to make the
  same call C's checker would.

Tests stay 17/17; new self-rebuild test (995) lands in the next
commit and gates on these fixes.
2026-05-11 11:40:59 +09:00
a52a32e0ba build: ignore example + probe build artifacts
examples/snake/ leaves snake.s/.o/.combined.ww and the stripped
binary alongside its Makefile after a build; selfhost/test/
probes drop .combined.ww next to each .ww. None of these are
intended bootstrap inputs.
2026-05-11 11:21:44 +09:00
502b304841 ww: driver shells to wwstage tools
build_one now invokes 6c_ww / 6a_ww / 6l_ww from $self_dir, not
the C-built binaries that share the directory. After this change
`ww_ww build foo.ww` touches no cstage code at runtime — the
fresh-checkout cstage is still needed to bring the wwstage into
existence, but day-to-day work runs on the ww toolchain end to
end. The C `ww` driver in cmd/ww/ still drives the C 6c/6a/6l.

Test 993 (which used to be trivial — both drivers invoked the
same C tools) now meaningfully compares the cstage pipeline
against the wwstage pipeline on hello + wwdump and confirms
byte-identical exes.

The .combined.ww files for 6a/6l/ww/wwdump and smoke are
regenerated by the ww driver's `expand()` step; their diff is
the lib/os dup2 wrapper and the cgen.ww port from the prior two
commits, propagating into the bootstrap inputs.
2026-05-11 11:20:23 +09:00
218d8469ff 6c: ww-side compiler binary, dup2 syscall, test 994
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.
2026-05-11 11:20:06 +09:00
607cc8d330 cgen: name-based slot reuse, fn-addr ffi_resolve, indirect call
Closes the byte-identity gap between selfhost/cmd/wwc/cgen.ww and
the C cgen, so a ww_ww-built binary matches the cstage-built binary
on the same input. The bootstrap fixed point was already green;
these are the bytes inside that fixed point that diverged from
what cmd/6c emits.

Slot allocation:

- local_add dedups by name (mirror cmd/6c/cgen.c:localoff). Two
  `let cp: pos;` in disjoint if-branches share one slot. The
  stored tnode is refreshed on each hit so a later `let m: *node`
  shadowing an earlier `let m: i32` sees its own type when
  emitting `m.next` — without this the N_DOT cgen fell into the
  SB-symbol fallback and the linker complained about undefined
  `next`.
- scan_locals dedups at frame-size time to keep the prologue SUBQ
  in sync. cgfn seeds c.locals with param-name stubs before the
  scan so a body's `let <param-name>` reuses the param slot, then
  resets c.locals before emission so real offsets get installed.
- local_alloc (no dedup) for N_MCASE bindings: C cgen handles a
  match as an expression with by-value `locals`, so two separate
  matches each get fresh slots for `v`/`e`.

Per-instruction matching:

- `return;` in a void fn zeros AX (C cgen falls through to
  cgexpr_int(c, 0)).
- N_INTLIT prints i64 (signed), not u64. FNV-1a's offset basis
  now prints as `$-3750763034362895579`, matching `$%lld`.
- *p = strexpr push order swapped to PUSH AX / PUSH BX → POP CX
  / POP AX (cgen.c:1033-1041).

Feature port from 635818e (the half that the wwdump corpus
actually exercises):

- N_IDENT used as a value with fn type now LEAQs through
  ffi_resolve, so `let fp = some_ffi_fn;` emits the C symbol.
- N_CALL on a bare ident checks local_find_node first; a local
  fn-pointer dispatches as `cgexpr(callee); CALL AX` instead of
  `CALL ident(SB)`.

Tests 990 probe 6 and 994 are byte-identical on mem/err/tok/smoke
+ the four selfhost main.combined.ww files. Bootstrap still
ww2 == ww3.
2026-05-11 11:19:42 +09:00
78ddc360aa examples/snake: ncurses snake demo
Small interactive demo wired through the new dynamic linker.
Uses libncurses and libc via @symbol bindings; the Makefile
runs the unmodified `ww build` pipeline with -l ncurses -l c
-L /usr/lib. Resulting binary has libncurses.so.6 + libc.so.6
in NEEDED.

Workaround: state struct keeps no array fields and threads the
xs/ys body buffers through main()'s frame, because the current
6c codegen segfaults on `&struct.field`. Once that's fixed the
arrays can move into state and place_food / step / render lose
their *i32 parameters.
2026-05-11 09:42:42 +09:00
ecf0a84127 6l: dynamic linking with symbol versioning
Teach the linker to consume ET_DYN shared objects and emit a
dynamically-linked ELF executable. Snake et al. can now link
against libncurses + libc through the system dynamic loader.

Pipeline additions:

- dyn.c: read ET_DYN, parse .dynsym + DT_SONAME, walk
  .gnu.version_d / .gnu.version to learn each export's default
  version (skip hidden entries).
- pass.c: when an undefined sym is provided by some Lso,
  promote it to dynamic, assign a PLT slot, record the
  matched version on the Lsym.
- dynout.c: emit PT_INTERP + PT_DYNAMIC, .dynsym/.dynstr/.hash,
  .plt + .got.plt + .rela.plt, .gnu.version + .gnu.version_r,
  and the full DT_* set with DT_BIND_NOW. Patch PC32/PLT32
  references against dyn syms to point at their PLT stubs.
- main.c: -L<dir> and -l<name> flag parsing; resolve <name>
  via .so / .so.<N> / .a in libdir order, skipping GNU ld
  linker scripts (libc.so on most distros).
- ww driver: collect -l/-L (joined and split forms) and pass
  through to 6l.

Design choices:

- DT_BIND_NOW so the loader resolves all PLT slots at startup;
  no PLT0 lazy resolver stub.
- SysV .hash, not .gnu.hash. One bucket; loader scans the
  chain. Slow at scale, fine for snake-class binaries.
- Non-PIE at fixed 0x400000.
- No section headers — loader uses program headers, but
  readelf -V/-S won't display anything.

Symbol versioning is the only correctness item beyond the
basic PLT/GOT machinery: glibc symbols default to versions
later than GLIBC_2.2.5 (e.g. clock_gettime → GLIBC_2.17 for
the vDSO impl), and the loader rejects unversioned references
to those without a matching Vernaux entry.

test/wwc/810_dyn covers four cases: bare libc dyn call,
multi-PLT, clock_gettime versioning, and fn-pointer to FFI
binding (which exercises the codegen fixes from the parent
commit alongside the new linker path).
2026-05-11 09:42:27 +09:00
635818eb13 6c: float compare, fn-address @symbol, indirect call
Three N_BIN/N_IDENT/N_CALL sites missed cases that bit ncurses
demos and dyn-linker exercises:

- Float compare emitted CMPQ + signed Jcc on f64 bits. Now goes
  through UCOMISD/UCOMISS + the unsigned Jcc family, mirroring
  Plan 9 6c (txt.c around AUCOMISD).
- LEAQ-of-fn-ident (taking the address of a function as a value)
  used n->str without ffi_resolve, so binding a *fn from an
  @symbol("name") fn declaration produced a reference to the
  ww ident rather than the C symbol.
- N_CALL on a bare ident with no localfind hit was a direct
  CALL ident(SB). Now checks localfind first and indirects
  through AX when the callee names a local fn-pointer slot.

Hare/QBE handles both shapes the same way (sort/search.ha:15
calls cmp(...) where cmp is a *cmpfunc parameter).
2026-05-11 09:42:02 +09:00
cb57cd795d build: split cstage/wwstage targets, document bootstrap
Phase 10 step 8 (delete the C trees) is deferred to v1.0 — until the
compiler stops churning we keep Cstage as the fresh-checkout entry
point. Split the Makefile so the two stages are named, and add
BOOTSTRAP.md describing the cstage → ww1 → ww2 → ww3 fixed-point
flow. PLAN.md gets a status note pointing at it.
2026-05-11 02:36:46 +09:00
1657bdeda3 ww: import toolchain — C bootstrap + ww-side self-host (phases 0-10)
C bootstrap (phases 0-9):
  cmd/wwc, cmd/6c, cmd/6a, cmd/6l, cmd/ww, rt, lib/*.

ww-side self-host (phase 10):
  selfhost/cmd/wwc — ww-cgen frontend; bootstrap fixed point.
  selfhost/cmd/6a  — assembler; byte-identical to C 6a (test 991).
  selfhost/cmd/6l  — linker w/ archive (.a) support; byte-identical
                     to C 6l (test 992).
  selfhost/cmd/ww  — driver (build/run/version); byte-identical to
                     C ww (test 993).

make test: 15/15. make bootstrap: ww2.s == ww3.s, ww2.o == ww3.o,
ww2 == ww3 byte-identical, with the full ww-tooled chain.
2026-05-11 02:17:47 +09:00
4c8fc59ca1 first commit 2026-05-10 01:24:58 +09:00