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.
This commit is contained in:
2026-05-11 02:17:47 +09:00
parent 4c8fc59ca1
commit 1657bdeda3
106 changed files with 35654 additions and 15 deletions

17
rt/abort.s Normal file
View File

@@ -0,0 +1,17 @@
// rt/abort.s write a string to stderr and exit(1).
//
// Args (SysV str-by-value): DI = msg.ptr, SI = msg.len.
// We never return.
TEXT rt_abort,$0
// reorder args for the write syscall: rdi=fd, rsi=ptr, rdx=len.
MOVQ SI, DX // DX = len
MOVQ DI, SI // SI = ptr
MOVQ $2, DI // DI = stderr
MOVQ $1, AX // AX = sys_write
SYSCALL
// exit(1)
MOVQ $1, DI
MOVQ $60, AX // sys_exit
SYSCALL
RET

24
rt/alloc.s Normal file
View File

@@ -0,0 +1,24 @@
// rt/alloc.s page allocator via the mmap syscall.
//
// rt_alloc(n: u64) returns a *void aligned at a page boundary, sized
// to the next page multiple. Pair with rt_free(p, n).
//
// We pin to PROT_READ|PROT_WRITE and MAP_PRIVATE|MAP_ANONYMOUS so
// callers never have to plumb file descriptors through.
TEXT rt_alloc,$0
MOVQ DI, SI // arg 1: length = caller's n
MOVQ $0, DI // arg 0: addr = NULL (kernel chooses)
MOVQ $3, DX // arg 2: prot = R|W
MOVQ $34, R10 // arg 3: flags = MAP_PRIVATE|MAP_ANON
MOVQ $-1, R8 // arg 4: fd = -1
MOVQ $0, R9 // arg 5: offset = 0
MOVQ $9, AX // syscall: mmap
SYSCALL
RET
TEXT rt_free,$0
// DI already holds ptr, SI already holds length
MOVQ $11, AX // syscall: munmap
SYSCALL
RET

16
rt/start.s Normal file
View File

@@ -0,0 +1,16 @@
// rt/start.s process entry. Linux sets up the stack so that on
// entry [SP] holds argc, [SP+8] starts argv (NULL-terminated array
// of *u8), [SP+8+(argc+1)*8] starts envp.
//
// We pull argc into DI and the argv pointer into SI, then jump to
// `main`. ww programs that declare `fn main(argc: i32, argv: **u8)
// i32` see them; programs declaring `fn main() i32` simply ignore
// the regs. Either way, main's return value is fed to the exit
// syscall.
TEXT _start,$0
MOVQ (SP), DI
LEAQ 8(SP), SI
CALL main(SB)
MOVQ AX, DI
MOVQ $60, AX
SYSCALL

27
rt/streq.s Normal file
View File

@@ -0,0 +1,27 @@
// rt/streq.s string equality at the runtime layer.
//
// Called from cgen for `a == b` / `a != b` when both operands are str.
// SysV ABI: a.ptr in DI, a.len in SI, b.ptr in DX, b.len in CX.
// Returns 1 in AX if equal, 0 otherwise. Caller flips for !=.
TEXT rt_streq,$0
CMPQ CX, SI // lengths must match
JNE rt_streq_no
// SI now holds the (shared) length; iterate while SI > 0
rt_streq_loop:
CMPQ $0, SI
JE rt_streq_yes
MOVZBQ (DI), AX
MOVZBQ (DX), R8
CMPQ R8, AX
JNE rt_streq_no
ADDQ $1, DI
ADDQ $1, DX
SUBQ $1, SI
JMP rt_streq_loop
rt_streq_yes:
MOVQ $1, AX
RET
rt_streq_no:
MOVQ $0, AX
RET

16
rt/syscall.s Normal file
View File

@@ -0,0 +1,16 @@
// rt/syscall.s Linux amd64 syscall trampoline. Args go in
// DI, SI, DX, R10, R8, R9 (the kernel ABI; userspace 4th is CX). We
// keep things minimal: rt_syscall(num, a1, a2, a3, a4, a5) result.
//
// The first ww arg lands in DI (caller side); to use it as the
// syscall number we move it into AX. The 4th C-ABI arg comes in CX
// and must be moved to R10 for the kernel.
TEXT rt_syscall,$0
MOVQ DI, AX
MOVQ SI, DI
MOVQ DX, SI
MOVQ CX, DX
MOVQ R8, R10
MOVQ R9, R8
SYSCALL
RET