commit ac52b03

shrub  ·  2026-11-21 23:30:41 +0000 UTC
parent 7b7c19c
basic stdlib, runtime, and build driver
23 files changed,  +539, -28
M TODO
M kav.h
M main.c
M qbe.c
M run.c
+10, -2
 1@@ -4,10 +4,12 @@ CC = cc
 2 CPP = $(CC) -E
 3 CFLAGS = -std=c11 -O2 -Wall -Wextra -pedantic -Wno-implicit-fallthrough -Wno-parentheses
 4 OBJ = main.o util.o lex.o parse.o ast.o resolve.o \
 5-      run.o check.o type.o module.o linear.o qbe.o
 6+      run.o check.o type.o module.o linear.o qbe.o compile.o
 7 HDR = util.h kav.h tokens.h punc.h kws.h exprs.h builtins.h ops.h
 8+PREFIX = /usr
 9+LIBDIR = $(PREFIX)/lib/kav
10 
11-.PHONY: all clean
12+.PHONY: all clean install
13 
14 all : kav
15 kav: $(OBJ)
16@@ -22,4 +24,10 @@ rules.re: rules.h kws.h punc.h
17 $(OBJ): $(HDR)
18 main.o: arg.h
19 
20+install: kav
21+	mkdir -p $(DESTDIR)$(PREFIX)/bin $(DESTDIR)$(LIBDIR)/rt
22+	cp -f kav $(DESTDIR)$(PREFIX)/bin/kav
23+	cp -f lib/*.kav $(DESTDIR)$(LIBDIR)/
24+	cp -f lib/rt/*.s $(DESTDIR)$(LIBDIR)/rt/
25+
26 clean: ; rm -f kav rules.re lex.c $(OBJ)
M TODO
+2, -0
1@@ -3,3 +3,5 @@ incremental
2 sort arrows
3 struct stuff
4 solver
5+fill out the stdlib
6+runtime for arm/BSD
+2, -0
 1@@ -6,8 +6,10 @@ BUILTIN(STYPE, BTYPE,      "u8",    bu8)
 2 BUILTIN(STYPE, BTYPE,     "u16",   bu16)
 3 BUILTIN(STYPE, BTYPE,     "u32",   bu32)
 4 BUILTIN(STYPE, BTYPE,     "u64",   bu64)
 5+BUILTIN(STYPE, BTYPE,    "cstr",  bcstr)
 6 BUILTIN(STYPE, BTYPE,    "bool",  bbool)
 7 BUILTIN(STYPE, BTYPE,     "one",   bone)
 8+BUILTIN(STYPE, BTYPE,   "World", bworld)
 9 
10 BUILTIN(SSORT, BSORT,    "type",  btype)
11 BUILTIN(SSORT, BSORT,   "utype", butype)
+24, -3
 1@@ -418,6 +418,12 @@ apply(struct expr *e, struct type *want) {
 2 }
 3 
 4 
 5+static int
 6+isnamed(struct type *t, struct binding *b) {
 7+	t = deref(t);
 8+	return t->kind == YNAME && t->binding == b;
 9+}
10+
11 static struct type *
12 trup(struct expr *e) {
13 	struct type *t;
14@@ -430,6 +436,8 @@ trup(struct expr *e) {
15 		e->type = mktype(YNAME, bint);
16 		checklit(e->tok, e->type);
17 		return e->type;
18+	case ESTRING:
19+		return e->type = mktype(YNAME, bcstr);
20 	case EIDENT:
21 		if(!e->binding->type)
22 			errorat(&e->tok.loc, "sorry, cant type this yet");
23@@ -451,12 +459,12 @@ trup(struct expr *e) {
24 		return e->type = t;
25 	case ECAST:
26 		t = trup(e->args);
27-		if(!isint(t))
28+		if(!isint(t) && !isnamed(t, bcstr))
29 			if(!unify(t, mktype(YNAME, bint)))
30-				errorat(&e->tok.loc, "can only cast intergers, got ", typestr(t));
31+				errorat(&e->tok.loc, "can only cast integers and cstrs, got ", typestr(t));
32 		to = totype(e->body);
33 		if(!isint(to))
34-			errorat(&e->tok.loc, "can only cast to an interger type, got ", typestr(to));
35+			errorat(&e->tok.loc, "can only cast to an integer type, got ", typestr(to));
36 		return e->type = to;
37 	default:
38 		errorat(&e->tok.loc, "sorry, can't type this yet");
39@@ -481,6 +489,17 @@ checkfn(struct decl *d) {
40 }
41 
42 
43+static void
44+checkmain(struct decl *d) {
45+	struct type *t = d->binding->type;
46+
47+	if(!t->args && isnamed(t->ret, bint))
48+		return;
49+	if(t->args && !t->args->next && isnamed(t->args, bworld) && isnamed(t->ret, bworld))
50+		return;
51+	errorat(&d->name.loc, "main must be fn main() -> int or fn main(w: World) -> World");
52+}
53+
54 void
55 check(struct module *m) {
56 	struct module *x;
57@@ -497,6 +516,8 @@ check(struct module *m) {
58 			if(d->kind != DFN)
59 				continue;
60 			blanks = nil;
61+			if(tokis(d->name, "main"))
62+				checkmain(d);
63 			checkfn(d);
64 			for(k = blanks; k; k = k->next) {
65 				if(deref(k->meta)->kind == YMETA)
+167, -0
  1@@ -0,0 +1,167 @@
  2+#define _XOPEN_SOURCE 700
  3+
  4+#include <errno.h>
  5+#include <fcntl.h>
  6+#include <signal.h>
  7+#include <spawn.h>
  8+#include <stdio.h>
  9+#include <stdlib.h>
 10+#include <string.h>
 11+#include <unistd.h>
 12+#include <sys/wait.h>
 13+
 14+#include "util.h"
 15+#include "kav.h"
 16+
 17+#define countof(a) (sizeof(a) / sizeof((a)[0]))
 18+
 19+extern char **environ;
 20+
 21+struct stage {
 22+	const char *name;
 23+	pid_t pid;
 24+};
 25+
 26+static struct stage stages[] = {{"qbe", 0}, {"cc", 0}};
 27+static const char *output;
 28+
 29+static char *
 30+rtpath(void) {
 31+	const char *dir = getenv("KAVPATH");
 32+	const char *colon;
 33+	stralloc sa = {0};
 34+	usize len;
 35+
 36+	if(!dir)
 37+		dir = "/usr/lib/kav";
 38+	for(;;) {
 39+		colon = strchr(dir, ':');
 40+		len = colon ? (usize)(colon - dir) : strlen(dir);
 41+		sa.len = 0;
 42+		sacatb(&sa, dir, len);
 43+		sacatl(&sa, "/rt/x86_64.s");
 44+		sa0(&sa);
 45+		if(len && access(sa.s, R_OK) == 0)
 46+			return sa.s;
 47+		if(!colon)
 48+			die("unable to find rt/x86_64.s");
 49+		dir = colon + 1;
 50+	}
 51+}
 52+
 53+static void
 54+mkpipe(int fd[2]) {
 55+	if(pipe(fd) < 0)
 56+		diesys("unable to create pipe");
 57+	if(fcntl(fd[0], F_SETFD, FD_CLOEXEC) < 0 || fcntl(fd[1], F_SETFD, FD_CLOEXEC) < 0)
 58+		diesys("unable to set close-on-exec");
 59+}
 60+
 61+/* run argv with in as its stdin and out as its stdout, -1 leaves them alone */
 62+static pid_t
 63+spawn(char *const *argv, int in, int out) {
 64+	posix_spawn_file_actions_t fa;
 65+	pid_t pid;
 66+	int err;
 67+
 68+	if((err = posix_spawn_file_actions_init(&fa)))
 69+		goto fail;
 70+	if(in != -1 && (err = posix_spawn_file_actions_adddup2(&fa, in, 0)))
 71+		goto fail;
 72+	if(out != -1 && (err = posix_spawn_file_actions_adddup2(&fa, out, 1)))
 73+		goto fail;
 74+	err = posix_spawnp(&pid, argv[0], &fa, nil, argv, environ);
 75+	posix_spawn_file_actions_destroy(&fa);
 76+	if(!err)
 77+		return pid;
 78+fail:
 79+	errno = err;
 80+	diesys("unable to spawn ", argv[0]);
 81+}
 82+
 83+static int
 84+succeeded(struct stage *s, int st) {
 85+	if(WIFEXITED(st)) {
 86+		if(WEXITSTATUS(st) == 0)
 87+			return 1;
 88+		fputs(PROG, stderr);
 89+		fputs(": ", stderr);
 90+		fputs(s->name, stderr);
 91+		fputs(" failed\n", stderr);
 92+	} else if(WIFSIGNALED(st)) {
 93+		fputs(PROG, stderr);
 94+		fputs(": ", stderr);
 95+		fputs(s->name, stderr);
 96+		fputs(": ", stderr);
 97+		fputs(strsignal(WTERMSIG(st)), stderr);
 98+		fputs("\n", stderr);
 99+	}
100+	return 0;
101+}
102+
103+static void
104+cleanup(void) {
105+	usize i;
106+	int st;
107+
108+	for(i = 0; i < countof(stages); i++) {
109+		if(!stages[i].pid)
110+			continue;
111+		kill(stages[i].pid, SIGTERM);
112+		waitpid(stages[i].pid, &st, 0);
113+		stages[i].pid = 0;
114+	}
115+	if(output)
116+		unlink(output);
117+}
118+
119+void
120+compile(struct module *m, const char *out) {
121+	int toqbe[2];
122+	int tocc[2];
123+	char *rt;
124+	int ok = 1;
125+	int st;
126+	pid_t pid;
127+	usize i;
128+	usize n;
129+
130+	rt = rtpath();
131+	output = out;
132+	signal(SIGPIPE, SIG_IGN);
133+	atexit(cleanup);
134+	mkpipe(toqbe);
135+	mkpipe(tocc);
136+	stages[0].pid = spawn((char *const[]){"qbe", nil}, toqbe[0], tocc[1]);
137+	stages[1].pid = spawn((char *const[]){"cc", "-o", (char *)out, rt,
138+		"-x", "assembler", "-", nil}, tocc[0], -1);
139+	close(toqbe[0]);
140+	close(tocc[0]);
141+	close(tocc[1]);
142+
143+	fflush(stdout);
144+	if(dup2(toqbe[1], 1) < 0)
145+		diesys("unable to redirect output");
146+	close(toqbe[1]);
147+	qbe(m);
148+	fflush(stdout);
149+	close(1);
150+
151+	for(n = countof(stages); n > 0; ) {
152+		pid = wait(&st);
153+		if(pid < 0)
154+			diesys("unable to wait");
155+		for(i = 0; i < countof(stages); i++)
156+			if(stages[i].pid == pid)
157+				break;
158+		if(i == countof(stages))
159+			continue;
160+		stages[i].pid = 0;
161+		n--;
162+		if(!succeeded(&stages[i], st))
163+			ok = 0;
164+	}
165+	if(!ok)
166+		exit(1);
167+	output = nil;
168+}
+1, -1
1@@ -1,4 +1,4 @@
2-use "prelude.kav"
3+use <prelude.kav>
4 use "int.kav"
5 
6 fn collatz(n: int) -> int {
+1, -1
1@@ -1,4 +1,4 @@
2-use "prelude.kav"
3+use <prelude.kav>
4 use "int.kav"
5 use "list.kav" as list
6 
+8, -0
1@@ -0,0 +1,8 @@
2+use <buffer.kav> as buf
3+
4+pub fn main(w: World) -> World {
5+    val out = buf::stdout(w)
6+    val out = buf::puts(out, "hello, world\n")
7+    val _   = buf::close(out)
8+    w
9+}
+1, -1
1@@ -1,4 +1,4 @@
2-use "prelude.kav"
3+use <prelude.kav>
4 use "bool.kav" as bool
5 use "pair.kav" as pair
6 use "maybe.kav" as maybe
+1, -1
1@@ -1,4 +1,4 @@
2-use "prelude.kav"
3+use <prelude.kav>
4 use "fun.kav" as fun
5 
6 pub datatype Maybe[a:type] {
+1, -1
1@@ -1,4 +1,4 @@
2-use "prelude.kav"
3+use <prelude.kav>
4 
5 newtype Id = int
6 
+8, -3
 1@@ -1,8 +1,13 @@
 2-use "prelude.kav"
 3+use <prelude.kav>
 4 use "list.kav" as list
 5 
 6-pub fn main() -> one {
 7+pub fn main() -> int {
 8     val l: list::List[int, 3] = list::Cons(1, list::Cons(2, list::Cons(3, list::Nil)))
 9     val reversed = list::reverse(l)
10-    list::free(reversed)
11+    case reversed of {
12+    | ~list::Cons(x, rest) => {
13+        val _ = list::free(rest)
14+        x
15+      }
16+    }
17 }
+1, -1
1@@ -1,4 +1,4 @@
2-use "prelude.kav"
3+use <prelude.kav>
4 use "list.kav" as list
5 use "bool.kav" as bool
6 
M kav.h
+1, -0
1@@ -58,6 +58,7 @@ void check(struct module *);
2 void linear(struct module *);
3 int run(struct decl *);
4 void qbe(struct module *);
5+void compile(struct module *, const char *);
6 noreturn void erroratv(const struct location *, const char *const *, usize);
7 char *tokdup(struct token);
8 i64 tonum(struct token);
+76, -0
 1@@ -0,0 +1,76 @@
 2+use <bytes.kav> as bytes
 3+use <rt.kav> as rt
 4+
 5+extern fn kavload8(p: int) -> u8
 6+extern fn kavstrlen(s: cstr) -> int
 7+
 8+pub linear datatype Buffer {
 9+    | Buffer(int, int, bytes::Bytes[4096])
10+}
11+
12+fn cap() -> int {
13+    4096
14+}
15+
16+/% create a new Buffer that writes to fd %/ 
17+pub fn new(w: !World, fd: int) -> Buffer {
18+    Buffer(fd, 0, bytes::alloc(cap()))
19+}
20+
21+/% create a new Buffer on stdout %/
22+pub fn stdout(w: !World) -> Buffer {
23+    new(w, 1)
24+}
25+
26+/% create a new Buffer on stderr %/
27+pub fn stderr(w: !World) -> Buffer {
28+    new(w, 2)
29+}
30+
31+/% write everything out of the buffer %/
32+fn allwrite(fd: int, m: !bytes::Bytes[4096], off: int, len: int) -> int {
33+    if off >= len then 0 else {
34+        val r = rt::write(fd, bytes::addr(m) + off, len - off)
35+        if r <= 0 then r else allwrite(fd, m, off + r, len)
36+    }
37+}
38+
39+fn drain(fd: int, m: bytes::Bytes[4096], len: int) -> bytes::Bytes[4096] {
40+    val _ = allwrite(fd, m, 0, len)
41+    m
42+}
43+
44+/%
45+Move bytes from a cstr into a Buffer's storage one byte a time, flushing when it fills up.
46+%/
47+fn copy(fd: int, len: int, m: bytes::Bytes[4096], s: cstr, i: int, n: int) -> Buffer {
48+    if i == n then Buffer(fd, len, m)
49+    else if len == cap() then copy(fd, 0, drain(fd, m, len), s, i, n)
50+    else copy(fd, len + 1, bytes::set(m, len, kavload8(s as int + i)), s, i + 1, n)
51+}
52+
53+/% add n bytes from s into a Buffer %/
54+pub fn put(b: Buffer, s: cstr, n: int) -> Buffer {
55+    case b of {
56+    | ~Buffer(fd, len, m) => copy(fd, len, m, s, 0, n)
57+    }
58+}
59+
60+/% add a whole cstr into the buffer %/
61+pub fn puts(b: Buffer, s: cstr) -> Buffer {
62+    put(b, s, kavstrlen(s))
63+}
64+
65+/% write out everything in the buffer, but keep the buffer %/
66+pub fn flush(b: Buffer) -> Buffer {
67+    case b of {
68+    | ~Buffer(fd, len, m) => Buffer(fd, 0, drain(fd, m, len))
69+    }
70+}
71+
72+/% write out everything in the buffer, and consume it %/
73+pub fn close(b: Buffer) -> one {
74+    case b of {
75+    | ~Buffer(fd, len, m) => bytes::free(drain(fd, m, len))
76+    }
77+}
+44, -0
 1@@ -0,0 +1,44 @@
 2+use <prelude.kav>
 3+pub linear datatype Bytes[n: nat] {}
 4+
 5+extern fn kavalloc[n: nat](len: int) -> Bytes[n]
 6+extern fn kavfree[n: nat](m: Bytes[n]) -> one
 7+extern fn kavaddr[n: nat](m: !Bytes[n]) -> int
 8+extern fn kavload8(p: int) -> u8
 9+extern fn kavstore8(p: int, v: u8) -> one
10+
11+/%
12+allocate a new Bytes block.
13+%/
14+pub fn alloc[n: nat](len: int) -> Bytes[n] {
15+    kavalloc(len)
16+}
17+
18+/%
19+free some Bytes.
20+%/
21+pub fn free[n: nat](m: Bytes[n]) -> one {
22+    kavfree(m)
23+}
24+
25+/%
26+return a Bytes block's address as an int.
27+%/
28+pub fn addr[n: nat](m: !Bytes[n]) -> int {
29+    kavaddr(m)
30+}
31+
32+/%
33+write byte v at position i in bytes block m.
34+%/
35+pub fn set[n: nat](m: Bytes[n], i: int, v: u8) -> Bytes[n] {
36+    val _ = kavstore8(kavaddr(m) + i, v)
37+    m
38+}
39+
40+/%
41+read byte v at position i in bytes block m.
42+%/
43+pub fn get[n: nat](m: !Bytes[n], i: int) -> u8 {
44+    kavload8(kavaddr(m) + i)
45+}
R examples/prelude.kav => lib/prelude.kav
+0, -0
+5, -0
1@@ -0,0 +1,5 @@
2+extern fn kavsyscall3(n: int, a: int, b: int, c: int) -> int
3+
4+pub fn write(fd: int, p: int, n: int) -> int {
5+    kavsyscall3(1, fd, p, n)
6+}
+71, -0
 1@@ -0,0 +1,71 @@
 2+.section .text.kavsyscall0
 3+.global kavsyscall0
 4+kavsyscall0:
 5+	movq %rdi, %rax
 6+	syscall
 7+	ret
 8+
 9+.section .text.kavsyscall1
10+.global kavsyscall1
11+kavsyscall1:
12+	movq %rdi, %rax
13+	movq %rsi, %rdi
14+	syscall
15+	ret
16+
17+.section .text.kavsyscall2
18+.global kavsyscall2
19+kavsyscall2:
20+	movq %rdi, %rax
21+	movq %rsi, %rdi
22+	movq %rdx, %rsi
23+	syscall
24+	ret
25+
26+.section .text.kavsyscall3
27+.global kavsyscall3
28+kavsyscall3:
29+	movq %rdi, %rax
30+	movq %rsi, %rdi
31+	movq %rdx, %rsi
32+	movq %rcx, %rdx
33+	syscall
34+	ret
35+
36+.section .text.kavsyscall4
37+.global kavsyscall4
38+kavsyscall4:
39+	movq %rdi, %rax
40+	movq %r8, %r10
41+	movq %rsi, %rdi
42+	movq %rdx, %rsi
43+	movq %rcx, %rdx
44+	syscall
45+	ret
46+
47+.section .text.kavsyscall5
48+.global kavsyscall5
49+kavsyscall5:
50+	movq %rdi, %rax
51+	movq %r8, %r10
52+	movq %rsi, %rdi
53+	movq %r9, %r8
54+	movq %rdx, %rsi
55+	movq %rcx, %rdx
56+	syscall
57+	ret
58+
59+.section .text.kavsyscall6
60+.global kavsyscall6
61+kavsyscall6:
62+	movq %rdi, %rax
63+	movq %r8, %r10
64+	movq %rsi, %rdi
65+	movq %r9, %r8
66+	movq %rdx, %rsi
67+	movq 8(%rsp), %r9
68+	movq %rcx, %rdx
69+	syscall
70+	ret
71+
72+.section .note.GNU-stack,"",@progbits
+4, -3
 1@@ -15,7 +15,7 @@ islinear(struct type *t) {
 2 	t = deref(t);
 3 	switch(t->kind) {
 4 	case YNAME:
 5-		return t->binding->decl && t->binding->decl->linear;
 6+		return t->binding->decl && t->binding->decl->linear || t->binding == bworld;
 7 	case YTUPLE:
 8 		/* all tuples are linear */
 9 		return 1;
10@@ -323,12 +323,13 @@ static void
11 linfn(struct decl *d) {
12 	struct param *p;
13 
14+	if(!d->body)
15+		return;
16 	vars = nil;
17 	for(p = d->params; p; p = p->next)
18 		if(islinear(p->binding->type))
19 			track(p->binding, p->binding->type->borrow);
20-	if(d->body) 
21-		use(d->body);
22+	use(d->body);
23 	endscope(nil);
24 }
25 
M main.c
+14, -9
 1@@ -9,7 +9,7 @@ static noreturn void
 2 usage(void) {
 3 	fputs("usage: ", stderr);
 4 	fputs(PROG, stderr);
 5-	fputs(" [-c|-l|-p|-q|-r] file\n", stderr);
 6+	fputs(" [-c|-l|-p|-q|-r] [-o out] file\n", stderr);
 7 	exit(100);
 8 }
 9 
10@@ -58,12 +58,6 @@ doparse(const char *path) {
11 	return 0;
12 }
13 
14-static int
15-doresolve(const char *path) {
16-	resolve(loadall(path));
17-	return 0;
18-}
19-
20 static struct module *
21 build(const char *path) {
22 	struct module *m;
23@@ -96,9 +90,17 @@ doqbe(const char *path) {
24 	return 0;
25 }
26 
27+static const char *out = "a.out";
28+
29+static int
30+docompile(const char *path) {
31+	compile(build(path), out);
32+	return 0;
33+}
34+
35 int
36 main(int argc, char **argv) {
37-	int (*mode)(const char *) = dorun;
38+	int (*mode)(const char *) = docompile;
39 
40 	PROG = argv[0];
41 	ARGBEGIN {
42@@ -115,7 +117,10 @@ main(int argc, char **argv) {
43 		mode = doqbe;
44 		break;
45 	case 'r':
46-		mode = doresolve;
47+		mode = dorun;
48+		break;
49+	case 'o':
50+		out = EARGF(usage());
51 		break;
52 	default:
53 		usage();
M qbe.c
+93, -2
  1@@ -10,6 +10,16 @@ enum instkind {
  2 #undef OP
  3 };
  4 
  5+struct strlit {
  6+	struct strlit *next;
  7+	struct token tok;
  8+	const char *name;
  9+};
 10+
 11+static struct strlit *strs;
 12+static struct strlit **strend = &strs;
 13+static usize nstrs;
 14+
 15 static const char *const instname[] = {
 16 #define OP(op, name) name,
 17 #include "ops.h"
 18@@ -272,6 +282,36 @@ functuple(struct func *f, struct expr *e) {
 19 }
 20 	
 21 
 22+static struct operand *
 23+funcstr(struct token tok) {
 24+	struct strlit *l;
 25+	stralloc sa = {0};
 26+	char buf[FMTUSIZE];
 27+
 28+	sacatl(&sa, "k.str.");
 29+	sacatb(&sa, buf, fmtusize(buf, nstrs++));
 30+	sa0(&sa);
 31+	l = zalloc(sizeof(*l));
 32+	l->tok = tok;
 33+	l->name = sa.s;
 34+	*strend = l;
 35+	strend = &l->next;
 36+	return mkglobal(l->name);
 37+}
 38+
 39+static void
 40+emitstrs(void) {
 41+	struct strlit *l;
 42+
 43+	for(l = strs; l; l = l->next) {
 44+		fputs("data $", stdout);
 45+		fputs(l->name, stdout);
 46+		fputs(" = { b ", stdout);
 47+		fwrite(l->tok.str, 1, l->tok.length, stdout);
 48+		fputs(", b 0 }\n", stdout);
 49+	}
 50+}
 51+
 52 static struct operand *
 53 funcwrap(struct func *f, struct type *t, struct operand *v) {
 54 	enum instkind op;
 55@@ -673,6 +713,8 @@ funcexpr(struct func *f, struct expr *e) {
 56 		return funccase(f, e, 0);
 57 	case ETUPLE:
 58 		return functuple(f, e);
 59+	case ESTRING:
 60+		return funcstr(e->tok);
 61 	case ECAST:
 62 		return funcwrap(f, e->type, funcexpr(f, e->args));
 63 	case ECALL:
 64@@ -866,6 +908,49 @@ static const char runtime[] =
 65 	"@box\n"
 66 	"\t%t =l loadl %p\n"
 67 	"\tret %t\n"
 68+	"}\n"
 69+	"\n"
 70+	"function l $kavstrlen(l %s) {\n"
 71+	"@start\n"
 72+	"\t%p =l copy %s\n"
 73+	"@loop\n"
 74+	"\t%c =w loadub %p\n"
 75+	"\tjnz %c, @next, @done\n"
 76+	"@next\n"
 77+	"\t%p =l add %p, 1\n"
 78+	"\tjmp @loop\n"
 79+	"@done\n"
 80+	"\t%n =l sub %p, %s\n"
 81+	"\tret %n\n"
 82+	"}\n"
 83+	"\n"
 84+	"function l $kavalloc(l %n) {\n"
 85+	"@start\n"
 86+	"\t%p =l call $k.alloc(l %n)\n"
 87+	"\tret %p\n"
 88+	"}\n"
 89+	"\n"
 90+	"function l $kavfree(l %p) {\n"
 91+	"@start\n"
 92+	"\tcall $k.free(l %p)\n"
 93+	"\tret 0\n"
 94+	"}\n"
 95+	"\n"
 96+	"function ub $kavload8(l %p) {\n"
 97+	"@start\n"
 98+	"\t%v =w loadub %p\n"
 99+	"\tret %v\n"
100+	"}\n"
101+	"\n"
102+	"function l $kavstore8(l %p, ub %v) {\n"
103+	"@start\n"
104+	"\tstoreb %v, %p\n"
105+	"\tret 0\n"
106+	"}\n"
107+	"\n"
108+	"function l $kavaddr(l %p) {\n"
109+	"@start\n"
110+	"\tret %p\n"
111 	"}\n";
112 
113 static void
114@@ -954,8 +1039,14 @@ qbe(struct module *m) {
115 	fputs("@start\n", stdout);
116 	fputs("\t%r =l call $", stdout);
117 	fputs(d->qname, stdout);
118-	fputs("()\n", stdout);
119-	fputs("\tret %r\n", stdout);
120+	if(d->params) {
121+		fputs("(l 0)\n", stdout);
122+		fputs("\tret 0\n", stdout);
123+	} else {
124+		fputs("()\n", stdout);
125+		fputs("\tret %r\n", stdout);
126+	}
127 	fputs("}\n\n", stdout);
128+	emitstrs();
129 	rt();
130 }
M run.c
+4, -0
 1@@ -285,6 +285,10 @@ run(struct decl *d) {
 2 			break;
 3 	if(!d)
 4 		die("no entry point found!?");
 5+	if(d->params) {
 6+		eval(bind(nil, d->params->binding, mkint(0)), d->body);
 7+		return 0;
 8+	}
 9 	v = eval(nil, d->body);
10 	if(v->kind == VINT)
11 		return v->num;