ref: f453ba04686f9ac1c3b3595c19ec7b519f940a71
parent: 1f34af29ac758ceba1d08cb4f8c0fca41dc35502
author: Ori Bernstein <[email protected]>
date: Thu Dec 25 10:32:34 EST 2014
Get more of libstd ported to plan9. Move stuff into syswrap.myr.
--- a/libstd/sys+plan9-x64.myr
+++ b/libstd/sys+plan9-x64.myr
@@ -155,8 +155,10 @@
const pread : (fd : fd, buf : byte[:], off : off -> size)
const pwrite : (fd : fd, buf : byte[:], off : off -> size)
const exec : (bin : byte[:], args : byte[:][:] -> int)
+ const brk_ : (endp : byte# -> int)
- extern const gettos : (-> tos#)
+ extern var tosptr : tos#
+ extern var curbrk : byte#
;;
/* asm stub from syscall.s */
@@ -163,7 +165,7 @@
extern const syscall : (scno : scno, args : ... -> int)
/* asm stubs from util+plan9.s */
extern const cstring : (str : byte[:] -> byte#)
-extern const alloca : (sz : size -> byte#)
+extern const alloca : (sz : size -> int)
/*
@@ -173,6 +175,7 @@
*/
generic a = {a : @t; -> a castto(uint64)}
generic s = {a : @t; -> a castto(int64)}
+generic p = {a : @t; -> a castto(byte#)}
const sysr1 = {; -> syscall(Syssysr1)}
const bind = {name, old; -> syscall(Sysbind, cstring(name), cstring(old))}
@@ -188,7 +191,7 @@
const rfork = {rflags; -> syscall(Sysrfork, a(rflags)) castto(pid)}
const pipe = {fds; -> syscall(Syspipe, a(fds))}
const create = {path, mode, perm; -> syscall(Syscreate, cstring(path), a(mode), a(perm)) castto(fd)}
-const fd2path = {fd, buf; -> syscall(Sysfd2path, a(fd), a(buf), a(buf.len))}
+const fd2path = {fd, buf; -> syscall(Sysfd2path, a(fd), p(buf), a(buf.len))}
const remove = {path; -> syscall(Sysremove, cstring(path))}
const notify = {fn; -> syscall(Sysnotify, fn)} /* FIXME: this is likely to break when we do closures... */
const noted = {v; -> syscall(Sysnoted, a(v))}
@@ -197,20 +200,22 @@
const segfree = {va, len; -> syscall(Syssegfree, a(va), a(len))}
const segflush = {va, len; -> syscall(Syssegfree, a(va), a(len))}
const unmount = {name, old; -> syscall(Sysunmount, cstring(name), cstring(old))}
-const errstr = {buf; -> syscall(Syserrstr, a(buf), a(buf.len))}
+const errstr = {buf; -> syscall(Syserrstr, p(buf), a(buf.len))}
const stat = {name, edir; -> syscall(Sysstat, cstring(name), a(edir), a(edir))}
const fstat = {fd, edir; -> syscall(Sysstat, a(fd), a(edir), a(edir))}
const wstat = {name, edir; -> syscall(Syswstat, cstring(name), a(edir), a(edir))}
const fwstat = {fd, edir; -> syscall(Sysfwstat, a(fd), a(edir), a(edir))}
const mount = {fd, afd, old, flag, aname; -> syscall(Sysmount, a(fd), a(afd), cstring(old), a(flag), cstring(aname))}
-const await = {buf; -> syscall(Sysawait, a(buf), a(buf.len))}
-const pread = {fd, buf, off; -> syscall(Syspread, a(fd), a(buf), a(buf.len), off) castto(size)}
-const pwrite = {fd, buf, off; -> syscall(Syspwrite, a(fd), a(buf), a(buf.len), s(off)) castto(size)}
+const pread = {fd, buf, off; -> syscall(Syspread, a(fd), p(buf), a(buf.len), off) castto(size)}
+const pwrite = {fd, buf, off; -> syscall(Syspwrite, a(fd), p(buf), a(buf.len), s(off)) castto(size)}
+const await = {buf; -> syscall(Sysawait, p(buf), a(buf.len))}
+const brk_ = {endp; -> syscall(Sysbrk_, p(endp))}
const seek = {fd, n, ty
var ret : off
syscall(Sysseek, a(&ret), a(fd), a(n), a(ty))
-> ret
}
+
const exec = {bin, args
var p, cargs, i
@@ -223,10 +228,9 @@
cargs[args.len] = 0 castto(byte#)
-> syscall(Sysexec, cstring(bin), a(cargs))
}
+
/* ??? do I care for now?
-const brk_ = { }
const fversion = {fd, bufsz, vers, nvers; -> syscall(Sysfversion, fd, bufsz, }
-const await = {;}
const rendezvous = {;}
const semacquire = {;}
const semrelease = {;}
--- /dev/null
+++ b/libstd/syswrap+plan9-x64.myr
@@ -1,0 +1,117 @@
+use sys
+use "types.use"
+
+pkg std =
+ type fd = sys.fd
+ type pid = sys.pid
+ type fdopt = sys.fdopt
+
+ const Failmem : byte# = -1 castto(byte#)
+
+ const Ordonly : fdopt = sys.Ordonly castto(fdopt)
+ const Owronly : fdopt = sys.Owronly castto(fdopt)
+ const Ordwr : fdopt = sys.Ordwr castto(fdopt)
+ const Otrunc : fdopt = sys.Otrunc castto(fdopt)
+ const Ocreat : fdopt = 0x1000000 /* emulated by redirecting to creat(). */
+ const Oappend : fdopt = 0x2000000 /* emulated by seeking to EOF */
+ const Odir : fdopt = 0x0 /* no-op on plan9 */
+
+ /* fd stuff */
+ const open : (path : byte[:], opts : fdopt -> fd)
+ const openmode : (path : byte[:], opts : fdopt, mode : int64 -> fd)
+ const close : (fd : fd -> int64)
+ const creat : (path : byte[:], mode : int64 -> fd)
+ const read : (fd : fd, buf : byte[:] -> size)
+ const write : (fd : fd, buf : byte[:] -> size)
+ const pipe : (fds : fd[2]# -> int64)
+
+ /* path manipulation */
+ const mkdir : (path : byte[:], mode : int64 -> int64)
+ const unlink : (path : byte[:] -> int)
+
+ /* process stuff */
+ const getpid : ( -> pid)
+ const suicide : ( -> void)
+ const fork : (-> pid)
+ const execv : (cmd : byte[:], args : byte[:][:] -> int64)
+ const execve : (cmd : byte[:], args : byte[:][:], env : byte[:][:] -> int64)
+ const exit : (status:int -> void)
+ const waitpid : (pid:pid, loc:int32#, opt : int64 -> pid)
+ const getmem : (sz : size -> byte#)
+ const freemem : (p : byte#, sz : size -> void)
+;;
+
+/* fd stuff */
+const open = {path, opts; -> sys.open(path, opts castto(sys.fdopt)) castto(fd)}
+const openmode = {path, opts, mode;
+ var fd
+
+
+ if opts & Ocreat != 0
+ fd = sys.create(path, opts castto(sys.fdopt), mode castto(int))
+ else
+ fd = sys.open(path, opts castto(sys.fdopt))
+ ;;
+ if opts & Oappend != 0
+ sys.seek(fd, 0, 2)
+ ;;
+ -> fd castto(fd)
+}
+
+const close = {fd; -> sys.close(fd castto(sys.fd)) castto(int64)}
+const read = {fd, buf; -> sys.pread(fd castto(sys.fd), buf, -1) castto(size)}
+const write = {fd, buf; -> sys.pwrite(fd castto(sys.fd), buf, -1) castto(size)}
+const pipe = {fds; -> sys.pipe(fds castto(sys.fd[2]#)) castto(int64)}
+
+/* path manipulation */
+const unlink = {path; -> sys.remove(path)}
+const mkdir = {path, mode;
+ var fd
+
+ fd = sys.create(path, sys.Ordonly, sys.Dmdir | (mode castto(int)))
+ if fd < 0
+ -> -1
+ ;;
+ sys.close(fd)
+ -> 0
+}
+
+/* process stuff */
+const getpid = {; -> sys.tosptr.pid castto(pid)}
+const suicide = {; (0 castto(byte#))#} /* let's happy segfault!! t */
+const fork = {; -> sys.rfork(sys.Rffdg | sys.Rfrend | sys.Rfproc) castto(pid)}
+const execv = {cmd, args; -> sys.exec(cmd, args) castto(int64)}
+const execve = {cmd, args, env; -> sys.exec(cmd, args) castto(int64)}
+const exit = {status;
+ if status == 0
+ sys.exits("")
+ else
+ sys.exits("failure")
+ ;;
+}
+
+/* FIXME: horribly broken. We wait for a pid to exit, and lie about which one. */
+const waitpid = {pid, loc, opt;
+ var buf : byte[512]
+ var n
+
+ loc# = 0
+ n = sys.await(buf[:])
+ -> pid
+}
+
+/* memory stuff */
+const getmem = {sz
+ var endp
+
+ endp = (sys.curbrk castto(intptr)) + (sz castto(intptr))
+ if sys.brk_(endp castto(byte#)) < 0
+ -> Failmem
+ ;;
+ sys.curbrk = endp castto(byte#)
+ -> sys.curbrk
+}
+
+const freemem = {p, sz
+ /* FIXME: don't leak */
+}
--- a/libstd/syswrap+plan9.myr
+++ /dev/null
@@ -1,94 +1,0 @@
-use sys
-use "types.use"
-
-pkg std =
- type fd = sys.fd
- type pid = sys.pid
- type fdopt = sys.fdopt
-
- const Ordonly : fdopt = sys.Ordonly castto(fdopt)
- const Owronly : fdopt = sys.Owronly castto(fdopt)
- const Ordwr : fdopt = sys.Ordwr castto(fdopt)
- const Otrunc : fdopt = sys.Otrunc castto(fdopt)
-
- const Ocreat : fdopt = 0x100000 /* Emulated by redirecting to create().*/
- const Oappend : fdopt = 0 /* FIXME: Plan 9 doesn't seem to have an append bit? */
- const Odir : fdopt = 0 /* Plan 9 doesn't need this */
-
- /* fd stuff */
- const open : (path : byte[:], opts : fdopt -> fd)
- const openmode : (path : byte[:], opts : fdopt, mode : int64 -> fd)
- const close : (fd : fd -> int64)
- const creat : (path : byte[:], mode : int64 -> fd)
- const read : (fd : fd, buf : byte[:] -> size)
- const write : (fd : fd, buf : byte[:] -> size)
- const pipe : (fds : fd[2]# -> int64)
-
- /* path manipulation */
- const mkdir : (path : byte[:], mode : int64 -> int64)
- const unlink : (path : byte[:] -> int)
-
- /* process stuff */
- const getpid : ( -> pid)
- const suicide : (-> void)
- const fork : (-> pid)
- const execv : (cmd : byte[:], args : byte[:][:] -> int64)
- const execve : (cmd : byte[:], args : byte[:][:], env : byte[:][:] -> int64)
- const exit : (status:int -> void)
- const waitpid : (pid:pid, loc:int32#, opt : int64 -> int64)
-;;
-
-/* fd stuff */
-const open = {path, opts; -> openmode(path, opts, 0o777)}
-const openmode = {path, opts, mode
- if opts & Ocreat != 0
- -> sys.create(path, opts castto(sys.fdopt), mode castto(int)) castto(fd)
- else
- -> sys.open(path, opts castto(sys.fdopt)) castto(fd)
- ;;
-}
-const close = {fd; -> sys.close(fd castto(sys.fd)) castto(int64)}
-const creat = {path, mode; -> sys.create(path, sys.Owronly | sys.Otrunc, mode castto(int)) castto(fd)}
-const read = {fd, buf; -> sys.pread(fd castto(sys.fd), buf, -1 castto(sys.off)) castto(size)}
-const write = {fd, buf; -> sys.pwrite(fd castto(sys.fd), buf, -1 castto(sys.off)) castto(size)}
-const pipe = {fds; -> sys.pipe(fds castto(sys.fd[2]#)) castto(int64)}
-
-/* path manipulation */
-const mkdir = {path, mode
- var fd
-
- fd = sys.create(path, sys.Ordonly, sys.Dmdir | (mode castto(int)))
- if fd >= 0
- sys.close(fd)
- -> 0
- else
- -> -1
- ;;
-}
-const unlink = {path; -> sys.remove(path)}
-
-/* process stuff */
-const getpid = {;
- var tos
-
- tos = sys.gettos()
- -> tos.pid castto(pid)
-}
-const suicide = {; (0 castto(int#))#}
-const fork = {; -> sys.rfork(sys.Rfproc | sys.Rffdg | sys.Rfrend) castto(pid)}
-const execv = {cmd, args; -> sys.exec(cmd, args) castto(int64)}
-const execve = {cmd, args, env; -> sys.exec(cmd, args) castto(int64)} /* FIXME: don't ignore env */
-const exit = {status
- if status == 0
- sys.exits("")
- else
- sys.exits("failure")
- ;;
-}
-const waitpid = {pid, loc, opt
- /* FIXME: this is very wrong. */
- var buf : byte[512]
- sys.await(buf[:])
- loc# = 0
- -> 0
-}
--- a/libstd/util+plan9-x64.s
+++ b/libstd/util+plan9-x64.s
@@ -68,6 +68,8 @@
MOVQ (BP),BP
RET
-TEXT sys$gettos+0(SB),$0
- LEAQ _tos+0(SB),AX
- RET
+GLOBL sys$tosptr+0(SB),$8
+DATA sys$tosptr+0(SB)/8,$_tos+0(SB)
+GLOBL sys$curbrk+0(SB),$8
+DATA sys$curbrk+0(SB)/8,$end+0(SB)
+