commit c2cf94c

shrub  ·  2026-11-25 17:24:50 +0000 UTC
parent 770f648
fix stdlib impurities
10 files changed,  +103, -62
M kav.h
M qbe.c
+1, -2
 1@@ -420,8 +420,7 @@ apply(struct expr *e, struct type *want) {
 2 	return ft->ret;
 3 }
 4 
 5-
 6-static int
 7+int
 8 isnamed(struct type *t, struct binding *b) {
 9 	t = deref(t);
10 	return t->kind == YNAME && t->binding == b;
+3, -3
 1@@ -1,15 +1,15 @@
 2 use <prelude.kav>
 3 use <buffer.kav> as buf
 4 use <fs.kav> as fs
 5-use <either.kav> as eth
 6+use <result.kav> as res
 7 
 8 pub fn main(w: World) -> World {
 9     val _ = 
10     case fs::openread(w, "examples/hello.kav") of {
11-    | ~eth::Right(f) => buf::stdout(w) 
12+    | ~res::Ok(f) => buf::stdout(w) 
13                      |> buf::copy(buf::fromfile(f)) 
14                      |> buf::close()
15-    | ~eth::Left(e) => buf::stderr(w) 
16+    | ~res::Err(e) => buf::stderr(w) 
17                      |> buf::puts("unable to open file\n") 
18                      |> buf::close()
19     }
+6, -6
 1@@ -1,8 +1,8 @@
 2-extern fn putchar(c: i32) -> i32
 3+extern fn putchar(w: !World, c: i32) -> i32
 4 
 5-fn main() -> int {
 6-    val _ = putchar(104)
 7-    val _ = putchar(105)
 8-    val _ = putchar(10)
 9-    0
10+fn main(w: World) -> World {
11+    val _ = putchar(w, 104)
12+    val _ = putchar(w, 105)
13+    val _ = putchar(w, 10)
14+    w
15 }
M kav.h
+1, -0
1@@ -244,3 +244,4 @@ struct pat *mkpat(enum patkind, struct token);
2 struct binding *mkbinding(enum bindkind, struct token);
3 void declput(struct decl *);
4 int islinear(struct type *);
5+int isnamed(struct type *, struct binding *);
+31, -16
  1@@ -9,12 +9,19 @@ use <mem.kav> as mem
  2 extern fn kavstrlen(s: cstr) -> int
  3 
  4 pub abstract linear datatype Buffer {
  5-    | Buffer(int, int, bytes::Bytes[4096])
  6+    | Buffer(rt::Fd, int, bytes::Bytes[4096])
  7 }
  8 
  9 pub abstract linear datatype Reader {
 10     /* read, close, the fd, the next unread byte, where the data read ends, the storage */
 11-    | Reader(fn(int, ptr, int) -> int, fn(int) -> int, int, int, int, bytes::Bytes[4096])
 12+    | Reader(
 13+      fn(!rt::Fd, ptr, int) -> int, 
 14+      fn(rt::Fd) -> int, 
 15+      rt::Fd, 
 16+      int, 
 17+      int, 
 18+      bytes::Bytes[4096]
 19+    )
 20 }
 21 
 22 fn cap() -> int {
 23@@ -23,16 +30,12 @@ fn cap() -> int {
 24 
 25 /% create a new Buffer that writes to fd %/ 
 26 pub fn new(w: !World, fd: int) -> Buffer {
 27-    Buffer(fd, 0, bytes::alloc(cap()))
 28-}
 29-
 30-fn keep(fd: int) -> int {
 31-    0
 32+    Buffer(rt::fd(w, fd), 0, bytes::alloc(cap()))
 33 }
 34 
 35 /% read from fd, which the caller still owns %/
 36 pub fn reader(w: !World, fd: int) -> Reader {
 37-    Reader(rt::read, keep, fd, 0, 0, bytes::alloc(cap()))
 38+    Reader(rt::read, rt::forget, rt::fd(w, fd), 0, 0, bytes::alloc(cap()))
 39 }
 40 
 41 /% read from a File, which the Reader now owns and closes on release %/
 42@@ -97,14 +100,14 @@ pub fn stderr(w: !World) -> Buffer {
 43 }
 44 
 45 /% write everything out of the buffer %/
 46-fn allwrite(fd: int, m: !bytes::Bytes[4096], off: int, len: int) -> int {
 47+fn allwrite(fd: !rt::Fd, m: !bytes::Bytes[4096], off: int, len: int) -> int {
 48     if off >= len then 0 else {
 49         val r = rt::write(fd, mem::add(bytes::addr(m), off), len - off)
 50         if r <= 0 then r else allwrite(fd, m, off + r, len)
 51     }
 52 }
 53 
 54-fn drain(fd: int, m: bytes::Bytes[4096], len: int) -> bytes::Bytes[4096] {
 55+fn drain(fd: !rt::Fd, m: bytes::Bytes[4096], len: int) -> bytes::Bytes[4096] {
 56     val _ = allwrite(fd, m, 0, len)
 57     m
 58 }
 59@@ -112,9 +115,12 @@ fn drain(fd: int, m: bytes::Bytes[4096], len: int) -> bytes::Bytes[4096] {
 60 /%
 61 Move n bytes from address p into a Buffer's storage one byte a time, flushing when it fills up.
 62 %/
 63-fn copyaddr(fd: int, len: int, m: bytes::Bytes[4096], p: ptr, i: int, n: int) -> Buffer {
 64+fn copyaddr(fd: rt::Fd, len: int, m: bytes::Bytes[4096], p: ptr, i: int, n: int) -> Buffer {
 65     if i == n then Buffer(fd, len, m)
 66-    else if len == cap() then copyaddr(fd, 0, drain(fd, m, len), p, i, n)
 67+    else if len == cap() then {
 68+        val m = drain(fd, m, len)
 69+        copyaddr(fd, 0, m, p, i, n)
 70+    }
 71     else copyaddr(fd, len + 1, bytes::set(m, len, mem::load8(mem::add(p, i))), p, i + 1, n)
 72 }
 73 
 74@@ -138,14 +144,20 @@ pub fn putsa(b: Buffer, sa: !sa::Stralloc) -> Buffer {
 75 /% add one byte into the Buffer %/
 76 pub fn putc(b: Buffer, c: int) -> Buffer {
 77     val ~Buffer(fd, len, m) = b
 78-    if len == cap() then putc(Buffer(fd, 0, drain(fd, m, len)), c)
 79+    if len == cap() then {
 80+        val m = drain(fd, m, len)
 81+        putc(Buffer(fd, 0, m), c)
 82+    }
 83     else Buffer(fd, len + 1, bytes::set(m, len, c as u8))
 84 }
 85 
 86 /% write an integer into a Buffer %/
 87 pub fn putnum(b: Buffer, x: int) -> Buffer {
 88     val ~Buffer(fd, len, m) = b
 89-    if len + fmt::size() > cap() then putnum(Buffer(fd, 0, drain(fd, m, len)), x)
 90+    if len + fmt::size() > cap() then {
 91+        val m = drain(fd, m, len)
 92+        putnum(Buffer(fd, 0, m), x)
 93+    }
 94     else {
 95         val (m, k) = fmt::int(m, len, x)
 96         Buffer(fd, len + k, m)
 97@@ -170,11 +182,14 @@ pub fn getln(r: Reader, sa: sa::Stralloc, sep: int) -> (Reader, sa::Stralloc, in
 98 /% write out everything in the Buffer, but keep the Buffer %/
 99 pub fn flush(b: Buffer) -> Buffer {
100     val ~Buffer(fd, len, m) = b
101-    Buffer(fd, 0, drain(fd, m, len))
102+    val m = drain(fd, m, len)
103+    Buffer(fd, 0, m)
104 }
105 
106 /% write out everything in the buffer, and consume it %/
107 pub fn close(b: Buffer) -> one {
108     val ~Buffer(fd, len, m) = b
109-    bytes::free(drain(fd, m, len))
110+    val m = drain(fd, m, len)
111+    val _ = rt::forget(fd)
112+    bytes::free(m)
113 }
+8, -8
 1@@ -3,17 +3,17 @@ use <rt.kav> as rt
 2 use <result.kav> as res
 3 
 4 pub abstract linear datatype File {
 5-    | File(int)
 6+    | File(rt::Fd)
 7 }
 8 
 9 pub fn fd(f: !File) -> int {
10     val File(fd) = f
11-    fd
12+    rt::num(fd)
13 }
14 
15 fn open(w: !World, path: cstr, flags: int, mode: int) -> res::Result[File, int] {
16-    val fd = rt::open(path, flags, mode) 
17-    if fd < 0 then res::Err(0 - fd) else res::Ok(File(fd))
18+    val fd = rt::open(w, path, flags, mode) 
19+    if fd < 0 then res::Err(0 - fd) else res::Ok(File(rt::fd(w, fd)))
20 }
21 
22 pub fn openread(w: !World, path: cstr) -> res::Result[File, int] {
23@@ -28,8 +28,8 @@ pub fn openappend(w: !World, path: cstr) -> res::Result[File, int] {
24     open(w, path, 1 + 64 + 1024, 420)
25 }
26 
27-/% Release the File, returning it's fd. Now you have to close it. %/
28-pub fn intofd(f: File) -> int {
29+/% Release the File, returning its Fd. Now you have to close it. %/
30+pub fn intofd(f: File) -> rt::Fd {
31     val ~File(fd) = f
32     fd
33 }
34@@ -51,11 +51,11 @@ pub fn write(f: !File, p: ptr, n: int) -> int {
35 }
36 
37 pub fn mkdir(w: !World, path: cstr, mode: int) -> res::Result[one, int] {
38-    val r = rt::mkdir(path, mode)
39+    val r = rt::mkdir(w, path, mode)
40     if r < 0 then res::Err(0 - r) else res::Ok(one)
41 }
42 
43 pub fn rmdir(w: !World, path: cstr) -> res::Result[one, int] {
44-    val r = rt::rmdir(path)
45+    val r = rt::rmdir(w, path)
46     if r < 0 then res::Err(0 - r) else res::Ok(one)
47 }
+9, -9
 1@@ -3,38 +3,38 @@ use <list.kav>
 2 use <rt.kav> as rt
 3 use <result.kav> as res
 4 
 5-extern fn kavgetargc() -> int
 6-extern fn kavgetarg(i: int) -> cstr
 7+extern fn kavgetargc(w: !World) -> int
 8+extern fn kavgetarg(w: !World, i: int) -> cstr
 9 
10 /% Return the number of arguments %/
11 pub fn argc(w: !World) -> int {
12-    kavgetargc()
13+    kavgetargc(w)
14 }
15 
16 /% Return the argument at position i %/
17 pub fn arg(w: !World, i: int) -> cstr {
18     if i < 0 then ""
19     else if i >= argc(w) then ""
20-    else kavgetarg(i)
21+    else kavgetarg(w, i)
22 }
23 
24-fn build[k: nat](i: int, acc: List[cstr, k]) -> [m: nat] List[cstr, m] {
25+fn build[k: nat](w: !World, i: int, acc: List[cstr, k]) -> [m: nat] List[cstr, m] {
26     if i < 0 then acc
27-    else build(i - 1, Cons(kavgetarg(i), acc))
28+    else build(w, i - 1, Cons(kavgetarg(w, i), acc))
29 }
30 
31 /% Return a list of all of the arguments %/
32 pub fn args(w: !World) -> [n: nat] List[cstr, n] {
33-    build(argc(w) - 1, Nil)
34+    build(w, argc(w) - 1, Nil)
35 }
36 
37 /% Change the current directory %/
38 pub fn chdir(w: !World, path: cstr) -> res::Result[one, int] {
39-    val r = rt::chdir(path)
40+    val r = rt::chdir(w, path)
41     if r < 0 then res::Err(0 - r) else res::Ok(one)
42 }
43 
44 pub fn exit(w: World, code: int) -> World {
45-    val _ = rt::exit(code)
46+    val _ = rt::exit(w, code)
47     w
48 }
+39, -17
 1@@ -1,33 +1,55 @@
 2-extern fn kavsyscall3(n: int, a: int, b: int, c: int) -> int
 3+/% a linear handle on an open fd %/
 4+pub abstract linear newtype Fd = int
 5 
 6-pub fn write(fd: int, p: ptr, n: int) -> int {
 7-    kavsyscall3(1, fd, p as int, n)
 8+extern fn kavsyscall3(w: !World, n: int, a: int, b: int, c: int) -> int
 9+extern fn kavfdcall3(n: int, f: !Fd, b: int, c: int) -> int
10+
11+/% claim fd n %/
12+pub fn fd(w: !World, n: int) -> Fd {
13+    Fd(n)
14+}
15+
16+/% drop an Fd without closing it %/
17+pub fn forget(f: Fd) -> int {
18+    val ~Fd(n) = f
19+    0
20+}
21+
22+pub fn num(f: !Fd) -> int {
23+    val Fd(n) = f
24+    n
25+}
26+
27+pub fn write(f: !Fd, p: ptr, n: int) -> int {
28+    kavfdcall3(1, f, p as int, n)
29 }
30 
31-pub fn read(fd: int, p: ptr, n: int) -> int {
32-    kavsyscall3(0, fd, p as int, n)
33+pub fn read(f: !Fd, p: ptr, n: int) -> int {
34+    kavfdcall3(0, f, p as int, n)
35 }
36 
37-pub fn open(path: cstr, flags: int, mode: int) -> int {
38-    kavsyscall3(2, path as int, flags, mode)
39+pub fn close(f: Fd) -> int {
40+    val r = kavfdcall3(3, f, 0, 0)
41+    val _ = forget(f)
42+    r
43 }
44 
45-pub fn close(fd: int) -> int {
46-    kavsyscall3(3, fd, 0, 0)
47+pub fn open(w: !World, path: cstr, flags: int, mode: int) -> int {
48+    kavsyscall3(w, 2, path as int, flags, mode)
49 }
50 
51-pub fn mkdir(path: cstr, mode: int) -> int {
52-    kavsyscall3(83, path as int, mode, 0)
53+pub fn mkdir(w: !World, path: cstr, mode: int) -> int {
54+    kavsyscall3(w, 83, path as int, mode, 0)
55 }
56 
57-pub fn rmdir(path: cstr) -> int {
58-    kavsyscall3(84, path as int, 0, 0)
59+pub fn rmdir(w: !World, path: cstr) -> int {
60+    kavsyscall3(w, 84, path as int, 0, 0)
61 }
62 
63-pub fn chdir(path: cstr) -> int {
64-    kavsyscall3(80, path as int, 0, 0)
65+pub fn chdir(w: !World, path: cstr) -> int {
66+    kavsyscall3(w, 80, path as int, 0, 0)
67 }
68 
69-pub fn exit(code: int) -> int {
70-    kavsyscall3(231, code, 0, 0)
71+pub fn exit(w: !World, code: int) -> int {
72+    kavsyscall3(w, 231, code, 0, 0)
73 }
+2, -0
 1@@ -24,7 +24,9 @@ kavsyscall2:
 2 
 3 .section .text.kavsyscall3
 4 .global kavsyscall3
 5+.global kavfdcall3
 6 kavsyscall3:
 7+kavfdcall3:
 8 	movq %rdi, %rax
 9 	movq %rsi, %rdi
10 	movq %rdx, %rsi
M qbe.c
+3, -1
 1@@ -366,7 +366,9 @@ funcextcall(struct func *f, struct operand *callee, struct type *ft, struct oper
 2 	abi = abity(ft->ret);
 3 	r = funcinstabi(f, ICALL, strcmp(abi, "l") ? 'w' : 'l', abi, callee, nil);
 4 	for(i = 0, y = ft->args; i < n; i++, y = y->next)
 5-		funcinstabi(f, IARG, 0, abity(y), args[i], nil);
 6+		/* extern functions ignore world arguments. */
 7+		if(!isnamed(y, bworld))
 8+			funcinstabi(f, IARG, 0, abity(y), args[i], nil);
 9 	return funcwrap(f, ft->ret, r);
10 }
11