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:
@@ -110,28 +110,6 @@ import time;
|
||||
@symbol("rt_syscall") fn syscall3(num: nr, a: i64, b: i64, c: i64) i64;
|
||||
@symbol("rt_syscall") fn syscall4(num: nr, a: i64, b: i64, c: i64, d: i64) i64;
|
||||
|
||||
// alloc / free — runtime mmap-backed page allocator. Untyped:
|
||||
// `alloc(n)` returns a `*void` and `free(p, n)` requires the byte
|
||||
// count back because rt_free is munmap-based and doesn't track
|
||||
// mapping sizes (the kernel needs the length to release the
|
||||
// reservation).
|
||||
//
|
||||
// Diverges from Hare. Hare exposes `alloc` / `free` as typed
|
||||
// language builtins (`alloc(value, cap)?` / `free(ptr)`) that the
|
||||
// compiler lowers to rt::malloc/rt::free; ww has no such builtins,
|
||||
// so the rt-symbol surface is exposed directly. Stdlib callers
|
||||
// that need a typed allocation pattern wrap this with a cast plus
|
||||
// a stored capacity (see [[strings.dup]], [[memio.dynamic]]).
|
||||
//
|
||||
// OOM: rt_alloc is a bare mmap(MAP_ANON|MAP_PRIVATE) wrapper with
|
||||
// no error path. The raw Linux mmap syscall returns a negative
|
||||
// errno cast to `*void` on failure (e.g. `(void*)-12` for ENOMEM);
|
||||
// the `MAP_FAILED` (`(void*)-1`) value is a libc-wrapper convention
|
||||
// that rt_alloc doesn't apply. Neither `== nil` nor `== (void*)-1`
|
||||
// catches it; any deref of such a return faults. Today the stdlib
|
||||
// does not check; OOM faults on first dereference. A typed
|
||||
// fallible variant is a future task.
|
||||
@symbol("rt_alloc") export fn alloc(n: u64) *void;
|
||||
@symbol("rt_free") export fn free(p: *void, n: u64) void;
|
||||
@symbol("rt_abort") fn abort(msg: str) void;
|
||||
|
||||
@@ -749,6 +727,29 @@ export fn exists(path: str) bool = {
|
||||
return r >= 0i64;
|
||||
};
|
||||
|
||||
// rt — runtime primitives exposed to ww programs.
|
||||
// Mirrors Hare's rt:: module placement (ref/hare/rt/).
|
||||
|
||||
package rt;
|
||||
|
||||
// alloc — mmap-backed page allocator. Untyped: `alloc(n)` returns a
|
||||
// `*void`; callers cast to the target type. Diverges from Hare: Hare
|
||||
// exposes `alloc` / `free` as typed language builtins that the
|
||||
// compiler lowers to rt::malloc/rt::free; ww has no such builtins,
|
||||
// so the rt-symbol surface is exposed directly. Stdlib callers that
|
||||
// need a typed allocation pattern wrap this with a cast plus a stored
|
||||
// capacity (see [[strings.dup]], [[memio.dynamic]]).
|
||||
//
|
||||
// OOM: rt_alloc is a bare mmap(MAP_ANON|MAP_PRIVATE) wrapper with no
|
||||
// error path. The raw Linux mmap syscall returns a negative errno cast
|
||||
// to `*void` on failure (e.g. `(void*)-12` for ENOMEM); the
|
||||
// `MAP_FAILED` (`(void*)-1`) value is a libc-wrapper convention that
|
||||
// rt_alloc doesn't apply. Neither `== nil` nor `== (void*)-1` catches
|
||||
// it; any deref of such a return faults. Today the stdlib does not
|
||||
// check; OOM faults on first dereference. A typed fallible variant is
|
||||
// a future task (task #39). ref/hare/rt/malloc.ha:27.
|
||||
@symbol("rt_alloc") export fn alloc(n: u64) *void;
|
||||
|
||||
// selfhost/cmd/wcc/mem.ww — port of cmd/wcc/mem.c.
|
||||
//
|
||||
// Bump arena allocator. Backed by the runtime page allocator
|
||||
@@ -762,6 +763,7 @@ export fn exists(path: str) bool = {
|
||||
package wcc;
|
||||
|
||||
import os;
|
||||
import rt;
|
||||
|
||||
def ALIGN: u64 = 16u64;
|
||||
def INIT_CHUNK: u64 = 65536u64;
|
||||
@@ -781,8 +783,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;
|
||||
@@ -799,14 +801,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;
|
||||
@@ -1864,6 +1866,7 @@ package strings;
|
||||
import bytes;
|
||||
import encoding.utf8;
|
||||
import os;
|
||||
import rt;
|
||||
import types;
|
||||
|
||||
// toutf8 — borrowed []u8 view of `s`. ref/hare/strings/utf8.ha:29.
|
||||
@@ -1935,7 +1938,7 @@ export fn dup(s: str) str = {
|
||||
r.ptr = nil;
|
||||
r.len = 0;
|
||||
if (s.len == 0) { return r; };
|
||||
let buf: *u8 = os.alloc(s.len: u64): *u8;
|
||||
let buf: *u8 = rt.alloc(s.len: u64): *u8;
|
||||
let i: i32 = 0;
|
||||
for (i < s.len) { buf[i] = s[i]; i += 1; };
|
||||
r.ptr = buf;
|
||||
@@ -2013,7 +2016,7 @@ export fn concat(strs: str...) str = {
|
||||
r.ptr = nil;
|
||||
r.len = 0;
|
||||
if (total == 0) { return r; };
|
||||
let buf: *u8 = os.alloc(total: u64): *u8;
|
||||
let buf: *u8 = rt.alloc(total: u64): *u8;
|
||||
let off: i32 = 0;
|
||||
i = 0;
|
||||
for (i < strs.len) {
|
||||
@@ -2046,7 +2049,7 @@ export fn join(delim: str, strs: str...) str = {
|
||||
r.ptr = nil;
|
||||
r.len = 0;
|
||||
if (total == 0) { return r; };
|
||||
let buf: *u8 = os.alloc(total: u64): *u8;
|
||||
let buf: *u8 = rt.alloc(total: u64): *u8;
|
||||
let off: i32 = 0;
|
||||
i = 0;
|
||||
for (i < strs.len) {
|
||||
@@ -2723,7 +2726,7 @@ export fn lpad(s: str, p: rune, maxlen: i32) str = {
|
||||
if (s.len >= maxlen) { return dup(s); };
|
||||
let scratch: [4]u8;
|
||||
let pad: []u8 = runebytes(scratch[0:4], p);
|
||||
let buf: *u8 = os.alloc(maxlen: u64): *u8;
|
||||
let buf: *u8 = rt.alloc(maxlen: u64): *u8;
|
||||
let padwrite: i32 = (maxlen - s.len) * pad.len;
|
||||
if (padwrite > maxlen) { padwrite = maxlen; };
|
||||
let off: i32 = 0;
|
||||
@@ -2806,7 +2809,7 @@ export fn rpad(s: str, p: rune, maxlen: i32) str = {
|
||||
if (s.len >= maxlen) { return dup(s); };
|
||||
let scratch: [4]u8;
|
||||
let pad: []u8 = runebytes(scratch[0:4], p);
|
||||
let buf: *u8 = os.alloc(maxlen: u64): *u8;
|
||||
let buf: *u8 = rt.alloc(maxlen: u64): *u8;
|
||||
let k: i32 = 0;
|
||||
for (k < s.len) {
|
||||
buf[k] = s[k];
|
||||
@@ -7901,15 +7904,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);
|
||||
@@ -9727,9 +9730,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 = {
|
||||
@@ -9758,8 +9761,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;
|
||||
@@ -9769,8 +9772,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;
|
||||
@@ -9885,8 +9888,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)
|
||||
@@ -9899,8 +9902,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;
|
||||
@@ -10816,8 +10819,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 = {
|
||||
@@ -11074,16 +11077,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
|
||||
@@ -11550,8 +11553,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;
|
||||
@@ -11745,9 +11748,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;
|
||||
};
|
||||
@@ -12266,9 +12269,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; };
|
||||
};
|
||||
};
|
||||
};
|
||||
@@ -13380,10 +13383,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;
|
||||
@@ -13436,10 +13439,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;
|
||||
@@ -13816,8 +13819,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");
|
||||
@@ -16630,8 +16633,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");
|
||||
};
|
||||
};
|
||||
@@ -19193,8 +19196,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; };
|
||||
};
|
||||
};
|
||||
};
|
||||
@@ -20234,10 +20237,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; };
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user