Six fixes across the toolchain, surfaced by lib/lisp porting work.
1. f64 compound assigns (`acc += d`, `-=`, `*=`, `/=`). Both stages
load slot → X1, OP X0 into X1, store back (ADDSD/SUBSD/MULSD/
DIVSD are reg-reg only). Previous MOVSD-overwrite dropped the
OP. Locals and top-level lets.
2. Top-level `[N]u8` arrays + `&arr[i]`. let_emit_size grows a
TY_ARRAY branch so zero-init DATAW lands; cgindex / N_INDEX
store / `&base[i]` all detect a global array base and use
LEAQ name(SB) instead of LEAQ (BP). TK_AMP no longer pre-
evaluates the operand as a value-load — `&base[i]` computes
base + i*esz directly. Unblocks Hare's static-buffer pattern:
strconv.{u64,i64,f64}tos graduate to module-level `*_buf`
arrays and return owned views.
3. Cross-module `pkg.Enum.MEMBER`. Nested N_DOT chains that
don't fold to a known shape now emit `MOVQ <leaf>(SB), AX`
(mirrors the bare-IDENT unresolved fallback), so isolation
probes — and the test 990 cgen-match floor — stay consistent
across stages. strconv exposes `base` as a real `enum i32`;
callers updated. The `main` exemption (linker entry-point
keeps bare name even when not exported) mirrors C-side
collectmods into selfhost cgendecl.
4. Sum-typed parameter ABI. lib/bytes.{index,rindex} take
`(u8 | []u8)` needle; lib/strings.byteindex / rbyteindex take
`(str | rune)` needle (Hare-shaped; the byte-wise misnomer
`index` is dropped). tagged_arg_size cap bumps to 48 (6 int
regs), with a new partial-fit branch on the callee: when an
N-word tagged arg overflows remaining regs, fill what fits and
stitch the rest from positive BP offsets. scanlocals MCASE
handles slice binds (24B) and walks each arm with a saved /
restored seenmark set so two arms naming the same local each
get their own slot — matches cstage's per-arm scope reset.
5. 4-reg tagged-return ABI (AX=tag, DX=word0, CX=word1, R8=word2),
up from 3 regs. Slice-payload variants (`([]T | E)`, slot 32B)
round-trip ptr/len/cap end-to-end. Every receive site updates:
let-init via cgwidentaggedstore, match scrutinee spill, cgindex
tagged-element load (both N_IDENT and fallback bases),
pushargsrev tagged-ident arg (reads word count from slot size),
cgreturn slice variant in the shuffle path.
6. `expr: TaggedAlias` is a widening, not a re-interpret. C cgen +
selfhost cgwidentaggedstore peel an N_CAST whose destination IS
the union — so cgexpr's natural shape (str: AX=ptr, BX=len;
slice: AX=ptr, BX=len, CX=cap) is consumed by the matching
concrete-variant branch instead of being misread as a tagged
AX/DX/CX triple. Inner casts to a concrete variant (`7: i32`)
keep their type for proper tag lookup. `[N]Alias` arrays
resolve element size via slotsize + aliaslookup, and aliaslookup
strips a `pkg.` prefix so cross-module references work.
lib/fmt grows `formattable = (i64 | str | bool | rune)` plus
`printv` / `printlnv` taking an explicit `[]formattable` slice (the
receive side of Hare's `args: formattable...`). Call-site variadic
gather isn't wired — callers either hand-build the slice or compose
strconv.i64tos + strings.concat.
700_e2e: 114 → 123 rows (f64 compound, top-level u8 arrays + `&buf[i]`,
pkg.Enum.MEMBER, sum-typed (str|rune) and (u8|[]u8) params, 4-reg
slice-return ABI, formattable array). 26/26 tests, bootstrap stable
through ww4.
170 lines
5.2 KiB
Plaintext
170 lines
5.2 KiB
Plaintext
// selfhost/test/smoke.ww — end-to-end smoke for the selfhost path.
|
|
//
|
|
// Exercises the patterns the real ww-side compiler port will use:
|
|
// - bump arena allocator (mem.ww shape)
|
|
// - error idiom (T | str)
|
|
// - struct of fn pointers + ctx pointer (the io.stream-style
|
|
// polymorphism we use instead of interfaces)
|
|
// - byte-level scanning that mirrors the hot path inside lex.ww
|
|
// - strconv round-trip via the real stdlib
|
|
//
|
|
// `main` returns 42 when every check passes, 1..N on failure
|
|
// indicating which probe broke. The 990_selfhost test asserts 42.
|
|
//
|
|
// Note: only stack-local mutable state. Top-level `let` mutation
|
|
// requires a writable .data segment in w6l, which is a separate
|
|
// task; until then we exercise polymorphism via ctx pointers, which
|
|
// is what the real port wants anyway.
|
|
|
|
use os;
|
|
use strconv;
|
|
use ascii;
|
|
|
|
// --- bump arena ---------------------------------------------------------
|
|
|
|
type arena = struct {
|
|
buf: *u8,
|
|
off: u64,
|
|
cap: u64,
|
|
};
|
|
|
|
// In-place init. Returning a 24-byte struct by value isn't yet
|
|
// supported in w6c (SysV requires a hidden return-slot pointer for
|
|
// structs >16 bytes), so we initialize through a pointer like the
|
|
// real compiler does today.
|
|
fn arena_init(a: *arena, buf: *u8, cap: u64) void = {
|
|
a.buf = buf;
|
|
a.off = 0u64;
|
|
a.cap = cap;
|
|
};
|
|
|
|
fn arena_alloc(a: *arena, n: u64) *u8 = {
|
|
if (n > a.cap - a.off) { return nil; };
|
|
let p: *u8 = a.buf + a.off;
|
|
a.off += n;
|
|
return p;
|
|
};
|
|
|
|
// --- (i32 | str) error idiom -------------------------------------------
|
|
|
|
fn checked_div(num: i32, den: i32) (i32 | str) = {
|
|
if (den == 0) { return "div by zero"; };
|
|
return num / den;
|
|
};
|
|
|
|
// --- struct-of-fn-pointer polymorphism ---------------------------------
|
|
//
|
|
// A trivial "writer" abstraction: a function pointer plus a context.
|
|
// This mirrors how io.stream / Plan 9 Bio work. The ctx pointer lets
|
|
// the implementation own its own state without a global.
|
|
|
|
type counter = struct {
|
|
n: i32,
|
|
};
|
|
|
|
type writer = struct {
|
|
ctx: *void,
|
|
emit: fn(ctx: *void, b: u8) void,
|
|
};
|
|
|
|
fn count_emit(ctx: *void, b: u8) void = {
|
|
let c: *counter = ctx: *counter;
|
|
c.n += 1;
|
|
};
|
|
|
|
// --- byte scanner like lex.ww's hot path -------------------------------
|
|
|
|
fn count_digits(s: str) i32 = {
|
|
let i: i32 = 0;
|
|
let n: i32 = 0;
|
|
for (i < s.len) {
|
|
let c: u8 = s[i];
|
|
if (c >= 48u8) {
|
|
if (c <= 57u8) { n += 1; };
|
|
};
|
|
i += 1;
|
|
};
|
|
return n;
|
|
};
|
|
|
|
// --- entry --------------------------------------------------------------
|
|
|
|
export fn main() i32 = {
|
|
// Probe 1 — arena hands out distinct pointers, refuses oversize.
|
|
let buf: [256]u8;
|
|
let a: arena;
|
|
arena_init(&a, buf.ptr, 256u64);
|
|
let p1: *u8 = arena_alloc(&a, 32u64);
|
|
let p2: *u8 = arena_alloc(&a, 32u64);
|
|
if (p1 == nil) { return 1; };
|
|
if (p2 == nil) { return 2; };
|
|
if (p1 == p2) { return 3; };
|
|
let p3: *u8 = arena_alloc(&a, 1024u64);
|
|
if (p3 != nil) { return 4; };
|
|
|
|
// Probe 2 — error union both ways.
|
|
let r_ok: (i32 | str) = checked_div(84, 2);
|
|
let r_bad: (i32 | str) = checked_div(1, 0);
|
|
let acc: i32 = 0;
|
|
match (r_ok) {
|
|
case let v: i32 => acc = v;
|
|
case let e: str => return 5;
|
|
};
|
|
if (acc != 42) { return 6; };
|
|
match (r_bad) {
|
|
case let v: i32 => return 7;
|
|
case let e: str => acc = e.len: i32;
|
|
};
|
|
if (acc != 11) { return 8; }; // len("div by zero") == 11
|
|
|
|
// Probe 3 — struct-of-fn-pointer dispatch via ctx pointer.
|
|
let c: counter = counter { n = 0 };
|
|
let w: writer = writer { ctx = (&c): *void, emit = count_emit };
|
|
w.emit(w.ctx, 65u8);
|
|
w.emit(w.ctx, 66u8);
|
|
w.emit(w.ctx, 67u8);
|
|
if (c.n != 3) { return 9; };
|
|
|
|
// Probe 4 — byte scan over a literal.
|
|
let dn: i32 = count_digits("ww123abc");
|
|
if (dn != 3) { return 10; };
|
|
|
|
// Probe 5 — strconv round-trip via the real stdlib.
|
|
let s: str = strconv.i64tos(4242i64, strconv.base.DEC);
|
|
if (s.len != 4) { return 11; };
|
|
if (s.ptr[0] != 52u8) { return 12; }; // '4'
|
|
if (s.ptr[3] != 50u8) { return 13; }; // '2'
|
|
|
|
// Probe 6 — ascii classifications (rune-taking, Hare-shaped).
|
|
if (!ascii.isdigit(53)) { return 14; }; // '5'
|
|
if (ascii.isdigit(65)) { return 15; }; // 'A' is not a digit
|
|
if (!ascii.isalpha(122)) { return 16; }; // 'z'
|
|
if (!ascii.isxdigit(70)) { return 17; }; // 'F'
|
|
if (ascii.isxdigit(71)) { return 18; }; // 'G' is not hex
|
|
if (ascii.tolower(65) != 97) { return 19; }; // 'A' -> 'a'
|
|
if (ascii.toupper(122) != 90) { return 20; }; // 'z' -> 'Z'
|
|
|
|
// Probe 7 — file open/read via the new os APIs. /proc/self/cmdline
|
|
// always exists on Linux, no write side, and is non-empty.
|
|
let path: str = "/proc/self/cmdline";
|
|
// Use raw os.open here (returns i32 with -errno) for the same
|
|
// reason as os.read below: probe 6 in 990_selfhost compiles
|
|
// smoke.ww standalone (no `use` expansion), so cross-module type
|
|
// references like `os.oserror` and `os.flag` don't resolve at
|
|
// that step. RDONLY is 0; passing the literal keeps the call
|
|
// site standalone-compilable to byte-identical asm on both
|
|
// compilers.
|
|
let fd: i32 = os.open(path.ptr, 0, 0i32);
|
|
if (fd < 0) { return 21; };
|
|
let rbuf: [128]u8;
|
|
// Use raw os.read here (single syscall, plain i64) instead of
|
|
// os.readall: the 990 cgen-match probe compiles smoke.ww
|
|
// standalone without `use os;` expansion, so cross-module type
|
|
// references like `os.oserror` can't be resolved.
|
|
let n: i64 = os.read(fd, rbuf.ptr, 128u64);
|
|
os.close(fd);
|
|
if (n <= 0i64) { return 22; };
|
|
|
|
return 42;
|
|
};
|