lib: extract rt module from os, sweep imports

Hare puts runtime allocation in rt::, not os:: (ref/hare/rt/malloc.ha:27,
README). ww's `@symbol("rt_alloc") fn alloc(n: u64) *void;` lived at
lib/os/os.ww as a historical bootstrap shortcut; this commit relocates
it to a new lib/rt/malloc.ww and sweeps every site that depended on
`import os` for the alloc decl over to `import rt`.

This is commit 1 of 3 in the lib/rt extraction (#35):
  1. (this) move decl, sweep imports — preserves shape
  2. rename rt_alloc → rt_malloc (#38)
  3. nullable return type + OOM-propagating builtin lowering (#39)

No rename here. Symbol stays rt_alloc, function stays `alloc`, return
stays *void. Behavior identical — same ffi resolution outcome, just
sourced from a different module file. The rt::ensure runtime helper at
selfhost/rt/ensure.ww is its own compilation unit with a local decl and
is untouched.

Side effect: every wcc cgen file used `rt` as a local *node variable
name for "return type." `import rt;` shadows the module, so each
selfhost/cmd/wcc/{check,cgenstmt,cgenexpr,cgenutil}.ww site renamed
to `rtyp`. Mechanical follow-through; only the wcc module-import was
forced to do this rename.

Verified 132/132 + 995_self_rebuild byte-identity (5 wwstage tools
round-trip byte-identical).
This commit is contained in:
2026-05-20 20:39:52 +09:00
parent a1ee817906
commit d68d3c7eb4
34 changed files with 530 additions and 488 deletions

View File

@@ -171,10 +171,10 @@ fn cgtryprop(c: *cgen, n: *node) void = {
};
};
if (cname.len > 0) {
let rt: *node = fnretlookupmod(c, cname, cmod);
if (rt != nil) {
if (rt.kind == nkind.N_TTAGGED) {
let first: *node = rt.list;
let rtyp: *node = fnretlookupmod(c, cname, cmod);
if (rtyp != nil) {
if (rtyp.kind == nkind.N_TTAGGED) {
let first: *node = rtyp.list;
if (first != nil) {
if (isstrtype(c, first)) {
succisstr = true;
@@ -227,10 +227,10 @@ fn cgtryunw(c: *cgen, n: *node) void = {
};
};
if (cname.len > 0) {
let rt: *node = fnretlookupmod(c, cname, cmod);
if (rt != nil) {
if (rt.kind == nkind.N_TTAGGED) {
let first: *node = rt.list;
let rtyp: *node = fnretlookupmod(c, cname, cmod);
if (rtyp != nil) {
if (rtyp.kind == nkind.N_TTAGGED) {
let first: *node = rtyp.list;
if (first != nil) {
if (isstrtype(c, first)) {
succisstr = true;
@@ -607,8 +607,8 @@ fn cgident(c: *cgen, n: *node) void = {
// in one go, so a body-less FFI binding emits the C symbol
// it was declared with via @symbol(), not the ww-side ident.
// Bare ident → same-module by ww's resolver, hint with c.curmod.
let rt: *node = fnretlookup(c, nm);
if (rt != nil) {
let rtyp: *node = fnretlookup(c, nm);
if (rtyp != nil) {
emitline("\tLEAQ\t");
emitfnname(c, nm, c.curmod);
emitline("(SB), AX\n");
@@ -3421,8 +3421,8 @@ fn cgcall(c: *cgen, n: *node) void = {
};
};
};
let rt: *node = fnretlookupmod(c, calleename, cmod);
if (isstrtype(c, rt)) {
let rtyp: *node = fnretlookupmod(c, calleename, cmod);
if (isstrtype(c, rtyp)) {
emitline("\tMOVQ\tDX, BX\n");
};
};

View File

@@ -184,8 +184,8 @@ fn cgreturn(c: *cgen, n: *node) void = {
};
};
if (calleename.len > 0) {
let rt: *node = fnretlookupmod(c, calleename, cmod);
if (istaggedtype(c, rt)) { forwardtagged = true; };
let rtyp: *node = fnretlookupmod(c, calleename, cmod);
if (istaggedtype(c, rtyp)) { forwardtagged = true; };
};
};
};
@@ -1225,10 +1225,10 @@ fn cgmlet(c: *cgen, n: *node) void = {
};
};
if (cnm.len > 0) {
let rt: *node = fnretlookupmod(c, cnm, cmod);
if (rt != nil) {
if (rt.kind == nkind.N_TTUPLE) {
p0t = rt.list;
let rtyp: *node = fnretlookupmod(c, cnm, cmod);
if (rtyp != nil) {
if (rtyp.kind == nkind.N_TTUPLE) {
p0t = rtyp.list;
if (p0t != nil) { p1t = p0t.next; };
};
};

View File

@@ -516,9 +516,9 @@ export fn taggedcallslot(c: *cgen, n: *node) i32 = {
let callee: *node = n.lhs;
if (callee == nil) { return 0; };
if (callee.kind != nkind.N_IDENT) { return 0; };
let rt: *node = fnretlookup(c, callee.str);
if (!istaggedtype(c, rt)) { return 0; };
return slotsize(c, rt);
let rtyp: *node = fnretlookup(c, callee.str);
if (!istaggedtype(c, rtyp)) { return 0; };
return slotsize(c, rtyp);
};
fn nodeisslice(c: *cgen, n: *node) bool = {
@@ -547,8 +547,8 @@ fn nodeisslice(c: *cgen, n: *node) bool = {
let callee: *node = n.lhs;
if (callee != nil) {
if (callee.kind == nkind.N_IDENT) {
let rt: *node = fnretlookupmod(c, callee.str, c.curmod);
return isslicetype(c, rt);
let rtyp: *node = fnretlookupmod(c, callee.str, c.curmod);
return isslicetype(c, rtyp);
};
if (callee.kind == nkind.N_DOT) {
let cmod: str;
@@ -558,8 +558,8 @@ fn nodeisslice(c: *cgen, n: *node) bool = {
cmod = callee.lhs.str;
};
};
let rt: *node = fnretlookupmod(c, callee.str, cmod);
return isslicetype(c, rt);
let rtyp: *node = fnretlookupmod(c, callee.str, cmod);
return isslicetype(c, rtyp);
};
};
return false;
@@ -674,8 +674,8 @@ fn nodeisstr(c: *cgen, n: *node) bool = {
let callee: *node = n.lhs;
if (callee != nil) {
if (callee.kind == nkind.N_IDENT) {
let rt: *node = fnretlookupmod(c, callee.str, c.curmod);
return isstrtype(c, rt);
let rtyp: *node = fnretlookupmod(c, callee.str, c.curmod);
return isstrtype(c, rtyp);
};
// #34: cross-module N_DOT — route through fnretlookupmod
// so a same-leaf caller-module fn (different return shape)
@@ -688,8 +688,8 @@ fn nodeisstr(c: *cgen, n: *node) bool = {
cmod = callee.lhs.str;
};
};
let rt: *node = fnretlookupmod(c, callee.str, cmod);
return isstrtype(c, rt);
let rtyp: *node = fnretlookupmod(c, callee.str, cmod);
return isstrtype(c, rtyp);
};
};
return false;
@@ -1605,8 +1605,8 @@ export fn callsretsize(c: *cgen, n: *node) i32 = {
};
};
if (cn.len == 0) { return 0; };
let rt: *node = fnretlookupmod(c, cn, cmod);
return sretretsize(c, rt);
let rtyp: *node = fnretlookupmod(c, cn, cmod);
return sretretsize(c, rtyp);
};
fn structlookup(c: *cgen, name: str) *structinfo = {
@@ -1863,16 +1863,16 @@ fn inferletcalltype(c: *cgen, rhs: *node) *node = {
};
};
if (cname.len == 0) { return nil; };
let rt: *node = fnretlookupmod(c, cname, cmod);
if (rt == nil) { return nil; };
let rtyp: *node = fnretlookupmod(c, cname, cmod);
if (rtyp == nil) { return nil; };
if (unwrap) {
// Strip error variants — success type is the first
// variant of the tagged return.
if (rt.kind != nkind.N_TTAGGED) { return nil; };
return rt.list;
if (rtyp.kind != nkind.N_TTAGGED) { return nil; };
return rtyp.list;
};
// Plain call: declared return type is the local's type.
return rt;
return rtyp;
};
// letslotsize — slot size for a `let` binding. Like slotsize, but
@@ -2339,8 +2339,8 @@ fn matchscrutt(c: *cgen, scrut: *node) *node = {
};
};
if (cnm.len > 0) {
let rt: *node = fnretlookupmod(c, cnm, cmod);
if (rt != nil) { return resolvetagged(c, rt); };
let rtyp: *node = fnretlookupmod(c, cnm, cmod);
if (rtyp != nil) { return resolvetagged(c, rtyp); };
};
};
return nil;
@@ -2534,9 +2534,9 @@ export fn exprfloatkind(c: *cgen, n: *node) i32 = {
if (n.lhs.kind == nkind.N_IDENT) { nm = n.lhs.str; };
};
if (nm.len > 0) {
let rt: *node = fnretlookup(c, nm);
if (isf32type(c, rt)) { return 1; };
if (isfloattype(c, rt)) { return 2; };
let rtyp: *node = fnretlookup(c, nm);
if (isf32type(c, rtyp)) { return 1; };
if (isfloattype(c, rtyp)) { return 2; };
};
return 0;
};
@@ -3055,9 +3055,9 @@ fn rhstaggedabicall(c: *cgen, src: *node) bool = {
};
};
if (calleename.len > 0) {
let rt: *node = fnretlookupmod(c, calleename, cmod);
if (rt != nil) {
if (istaggedtype(c, rt)) { return true; };
let rtyp: *node = fnretlookupmod(c, calleename, cmod);
if (rtyp != nil) {
if (istaggedtype(c, rtyp)) { return true; };
};
};
};

View File

@@ -812,15 +812,15 @@ fn astoffset(c: *checker, dot: *node) i64 = {
if (dot.kind != nkind.N_DOT) { return -1i64; };
let recv: *node = scruttype(c, dot.lhs);
if (recv == nil) { return -1i64; };
let rt: *node = resolvealias(c, unwrapbang(recv));
if (rt == nil) { return -1i64; };
if (rt.kind == nkind.N_TPTR) {
rt = resolvealias(c, unwrapbang(rt.lhs));
let rtyp: *node = resolvealias(c, unwrapbang(recv));
if (rtyp == nil) { return -1i64; };
if (rtyp.kind == nkind.N_TPTR) {
rtyp = resolvealias(c, unwrapbang(rtyp.lhs));
};
if (rt == nil) { return -1i64; };
if (rt.kind != nkind.N_TSTRUCT) { return -1i64; };
if (rtyp == nil) { return -1i64; };
if (rtyp.kind != nkind.N_TSTRUCT) { return -1i64; };
let off: i64 = 0i64;
let f: *node = rt.list;
let f: *node = rtyp.list;
for (f != nil) {
if (f.kind == nkind.N_TFIELD) {
let fa: i64 = astalign(c, f.lhs);

View File

@@ -11,6 +11,7 @@
package wcc;
import os;
import rt;
def ALIGN: u64 = 16u64;
def INIT_CHUNK: u64 = 65536u64;
@@ -30,8 +31,8 @@ fn roundup(n: u64, a: u64) u64 = {
};
export fn newarena() *arena = {
let a: *arena = os.alloc(ARENA_SZ): *arena;
a.buf = os.alloc(INIT_CHUNK): *u8;
let a: *arena = rt.alloc(ARENA_SZ): *arena;
a.buf = rt.alloc(INIT_CHUNK): *u8;
a.off = 0u64;
a.cap = INIT_CHUNK;
a.next = nil;
@@ -48,14 +49,14 @@ fn grow(a: *arena, need: u64) bool = {
if (want > MAX_CHUNK) { want = MAX_CHUNK; };
if (want < need) { return false; }; // single allocation too big
let old: *arena = os.alloc(ARENA_SZ): *arena;
let old: *arena = rt.alloc(ARENA_SZ): *arena;
old.buf = a.buf;
old.off = a.off;
old.cap = a.cap;
old.next = a.next;
old.total = 0u64;
a.buf = os.alloc(want): *u8;
a.buf = rt.alloc(want): *u8;
a.off = 0u64;
a.cap = want;
a.next = old;