w6c+selfhost+lib: cgen quality batch + lib Hare-shape graduation
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.
This commit is contained in:
@@ -1,16 +1,82 @@
|
||||
// fmt — minimal formatting writers. All output goes through os.write
|
||||
// to a file descriptor. No printf-family yet — we don't have varargs
|
||||
// in the language proper — so callers compose with strconv.i64tos /
|
||||
// strings.concat to build the message and then call print / println.
|
||||
// Hare's `fmt::println(42)` becomes `fmt.println(strconv.i64tos(42,
|
||||
// strconv.DEC))`.
|
||||
// fmt — formatting writers. Goes through os.write to a file
|
||||
// descriptor. Hare's variadic call-site sugar (`fmt::println(42)`
|
||||
// gathering args into a `[]formattable`) isn't wired yet; until then,
|
||||
// callers either:
|
||||
//
|
||||
// 1. Hand-build the slice:
|
||||
// let args: [2]formattable;
|
||||
// args[0] = 42i64: formattable;
|
||||
// args[1] = " hi": formattable;
|
||||
// fmt.print(args[0:2]);
|
||||
//
|
||||
// 2. Compose a single str via strconv.i64tos / strings.concat:
|
||||
// fmt.println(strconv.i64tos(42, strconv.base.DEC));
|
||||
//
|
||||
// `print(s: str)` keeps the single-string form for the common case.
|
||||
|
||||
use os;
|
||||
use strconv;
|
||||
use strings;
|
||||
|
||||
// formattable — tagged union of types fmt can render. Mirrors
|
||||
// Hare's `fmt::formattable = (...types::numeric | uintptr | str |
|
||||
// rune | bool | nullable *opaque | void)`, narrowed to the set ww
|
||||
// actually has codegen for. Slot size is 24B (8 tag + 16 str
|
||||
// payload).
|
||||
export type formattable = (i64 | str | bool | rune);
|
||||
|
||||
// vprint — write the formatted form of each element of `args` to
|
||||
// `fd`. Returns total bytes written or the first negative os.write
|
||||
// result.
|
||||
fn vprint(fd: i32, args: []formattable) i64 = {
|
||||
let total: i64 = 0;
|
||||
let i: i32 = 0;
|
||||
for (i < args.len) {
|
||||
match (args[i]) {
|
||||
case let n: i64 => {
|
||||
let s: str = strconv.i64tos(n, strconv.base.DEC);
|
||||
let r: i64 = os.write(fd, s.ptr, s.len: u64);
|
||||
if (r < 0) { return r; };
|
||||
total += r;
|
||||
};
|
||||
case let s: str => {
|
||||
let r: i64 = os.write(fd, s.ptr, s.len: u64);
|
||||
if (r < 0) { return r; };
|
||||
total += r;
|
||||
};
|
||||
case let b: bool => {
|
||||
let s: str = "false";
|
||||
if (b) { s = "true"; };
|
||||
let r: i64 = os.write(fd, s.ptr, s.len: u64);
|
||||
if (r < 0) { return r; };
|
||||
total += r;
|
||||
};
|
||||
case let r: rune => {
|
||||
let buf: [4]u8;
|
||||
buf[0] = r: u8;
|
||||
let n: i64 = os.write(fd, &buf[0], 1u64);
|
||||
if (n < 0) { return n; };
|
||||
total += n;
|
||||
};
|
||||
};
|
||||
i += 1;
|
||||
};
|
||||
return total;
|
||||
};
|
||||
|
||||
// print(s: str) — single-string form for the common case. The
|
||||
// variadic-style `print(args: []formattable)` lives as `printv`
|
||||
// until call-site sugar lands.
|
||||
export fn print(s: str) i64 = {
|
||||
return os.write(1, s.ptr, s.len: u64);
|
||||
};
|
||||
|
||||
// printv — Hare-shaped `print(args: formattable...)` modulo the
|
||||
// call-site sugar. Callers pass an explicit `[]formattable` slice.
|
||||
export fn printv(args: []formattable) i64 = {
|
||||
return vprint(1, args);
|
||||
};
|
||||
|
||||
export fn println(s: str) i64 = {
|
||||
let n: i64 = os.write(1, s.ptr, s.len: u64);
|
||||
if (n < 0) { return n; };
|
||||
@@ -19,6 +85,15 @@ export fn println(s: str) i64 = {
|
||||
return n + m;
|
||||
};
|
||||
|
||||
// printlnv — like printv but adds a trailing newline.
|
||||
export fn printlnv(args: []formattable) i64 = {
|
||||
let n: i64 = vprint(1, args);
|
||||
if (n < 0) { return n; };
|
||||
let m: i64 = os.write(1, "\n".ptr, 1u64);
|
||||
if (m < 0) { return m; };
|
||||
return n + m;
|
||||
};
|
||||
|
||||
// errorln — write a message to stderr with a trailing newline.
|
||||
export fn errorln(s: str) i64 = {
|
||||
let n: i64 = os.write(2, s.ptr, s.len: u64);
|
||||
|
||||
Reference in New Issue
Block a user