ref: 54366e8a221b4fc485eae0b376fee39ec897d1ca
parent: 4a5814d74531579135e015781e869ad47bb58c6e
author: Ori Bernstein <[email protected]>
date: Sat Jul 21 20:03:43 EDT 2012
Split OSX/Linux syscall stubs.
--- a/bld.sh
+++ b/bld.sh
@@ -9,6 +9,10 @@
export MU=muse
export CC=cc
export ASOPT="-g"
+case `uname` in
+ Darwin) export SYS=osx;;
+ Linux) export SYS=linux;;
+esac
function use {
N=`basename $1 .myr`
@@ -28,11 +32,12 @@
$CC $ASOPT -m32 -c $1
}
-use sys.myr
+
+use sys-$SYS.myr
use types.myr
use die.myr
-assem syscall.s
-build sys.myr
+assem syscall-$SYS.s
+build sys-$SYS.myr
build hello.myr
build alloc.myr
build die.myr
--- /dev/null
+++ b/sys-linux.myr
@@ -1,0 +1,98 @@
+use "types.use"
+
+pkg std =
+ type scno = int
+ type fdopt = int
+ type mprot = int
+ type mopt = int
+ type statbuf = struct
+ dev : uint
+ ino : uint
+ mode : uint16
+ nlink : uint16
+ uid : uint16
+ gid : uint16
+ rdev : uint
+ size : uint
+ blksize : uint
+ blocks : uint
+ atime : uint
+ atimens : uint
+ mtime : uint
+ mtimens : uint
+ ctime : uint
+ ctimens : uint
+ _unused1: uint
+ _unused2: uint
+ ;;
+
+ /* open options */
+ const Rdonly : fdopt = 0x0
+ const Wronly : fdopt = 0x1
+ const Rdwr : fdopt = 0x2
+ const Append : fdopt = 0x80
+ const Creat : fdopt = 0x40
+ const Nofollow : fdopt = 0x20000
+ const Ndelay : fdopt = 0x800
+ const Trunc : fdopt = 0x200
+
+ /* mmap protection */
+ const Mprotexec : mprot = 0x1
+ const Mprotrd : mprot = 0x2
+ const Mprotwr : mprot = 0x4
+ const Mprotrw : mprot = 0x6
+ const Mprotnone : mprot = 0x0
+
+ /* mmap options */
+ const Mshared : mopt = 0x1
+ const Mpriv : mopt = 0x2
+ const Mfixed : mopt = 0x10
+ const Mfile : mopt = 0x0
+ const Manon : mopt = 0x20
+ const M32bit : mopt = 0x40
+
+ /* return value for a failed mapping */
+ const Mapbad : byte* = -1 castto(byte*)
+
+ /* syscalls */
+ const Sysexit : scno = 1
+ const Sysread : scno = 3
+ const Syswrite : scno = 4
+ const Sysopen : scno = 5
+ const Sysclose : scno = 6
+ const Syscreat : scno = 8
+ const Syslseek : scno = 19
+ const Sysfstat : scno = 108
+ const Syskill : scno = 37
+ const Sysgetpid : scno = 20
+ const Sysmmap2 : scno = 192
+ const Sysmunmap : scno = 91
+
+ extern const syscall : (sc:scno, args:... -> int)
+
+ const exit : (status:int -> int)
+ const getpid : ( -> int)
+ const kill : (pid:int, sig:int -> int)
+ const open : (path:byte[,], opts:fdopt -> int)
+ const close : (fd:int -> int)
+ const creat : (path:byte[,], mode:int -> int)
+ const read : (fd:int, buf:byte[,] -> int)
+ const write : (fd:int, buf:byte[,] -> int)
+ const lseek : (fd:int, off:uint, whence:int -> int)
+ const fstat : (fd:int, sb:statbuf* -> int)
+ const munmap : (addr:byte*, len:uint -> int)
+ const mmap : (addr:byte*, len:size, prot:mprot, flags:mopt, fd:int, off:off -> byte*)
+;;
+
+const exit = {status; -> syscall(Sysexit, 1);}
+const getpid = {; -> syscall(Sysgetpid, 1);}
+const kill = {pid, sig; -> syscall(Syskill, pid, sig);}
+const open = {path, opts:fdopt; -> syscall(Sysopen, path castto(char*), opts);}
+const close = {fd; -> syscall(Sysclose, fd);}
+const creat = {path, mode; -> syscall(Syscreat, path castto(char*), mode);}
+const read = {fd, buf; -> syscall(Sysread, fd, buf castto(char*), buf.len);}
+const write = {fd, buf; -> syscall(Syswrite, fd, buf castto(char*), buf.len);}
+const lseek = {fd, off, whence; -> syscall(Syslseek, fd, off, whence);}
+const fstat = {fd, sb; -> syscall(Sysfstat, fd, sb);}
+const munmap = {addr, len; -> syscall(Sysmunmap, addr, len);}
+const mmap = {addr, len, prot, flags, fd, off; -> syscall(Sysmmap2, addr, len, prot, flags, fd, off) castto(byte*);}
--- /dev/null
+++ b/sys-osx.myr
@@ -1,0 +1,98 @@
+use "types.use"
+
+pkg std =
+ type scno = int
+ type fdopt = int
+ type mprot = int
+ type mopt = int
+ type statbuf = struct
+ dev : uint
+ ino : uint
+ mode : uint16
+ nlink : uint16
+ uid : uint16
+ gid : uint16
+ rdev : uint
+ size : uint
+ blksize : uint
+ blocks : uint
+ atime : uint
+ atimens : uint
+ mtime : uint
+ mtimens : uint
+ ctime : uint
+ ctimens : uint
+ _unused1: uint
+ _unused2: uint
+ ;;
+
+ /* open options */
+ const Rdonly : fdopt = 0x0
+ const Wronly : fdopt = 0x1
+ const Rdwr : fdopt = 0x2
+ const Append : fdopt = 0x80
+ const Creat : fdopt = 0x40
+ const Nofollow : fdopt = 0x20000
+ const Ndelay : fdopt = 0x800
+ const Trunc : fdopt = 0x200
+
+ /* mmap protection */
+ const Mprotexec : mprot = 0x1
+ const Mprotrd : mprot = 0x2
+ const Mprotwr : mprot = 0x4
+ const Mprotrw : mprot = 0x6
+ const Mprotnone : mprot = 0x0
+
+ /* mmap options */
+ const Mshared : mopt = 0x1
+ const Mpriv : mopt = 0x2
+ const Mfixed : mopt = 0x10
+ const Mfile : mopt = 0x0
+ const Manon : mopt = 0x20
+ const M32bit : mopt = 0x40
+
+ /* return value for a failed mapping */
+ const Mapbad : byte* = -1 castto(byte*)
+
+ /* syscalls */
+ const Sysexit : scno = 1
+ const Sysread : scno = 3
+ const Syswrite : scno = 4
+ const Sysopen : scno = 5
+ const Sysclose : scno = 6
+ const Syscreat : scno = 8
+ const Syslseek : scno = 19
+ const Sysfstat : scno = 108
+ const Syskill : scno = 37
+ const Sysgetpid : scno = 20
+ const Sysmmap2 : scno = 192
+ const Sysmunmap : scno = 91
+
+ extern const syscall : (sc:scno, args:... -> int)
+
+ const exit : (status:int -> int)
+ const getpid : ( -> int)
+ const kill : (pid:int, sig:int -> int)
+ const open : (path:byte[,], opts:fdopt -> int)
+ const close : (fd:int -> int)
+ const creat : (path:byte[,], mode:int -> int)
+ const read : (fd:int, buf:byte[,] -> int)
+ const write : (fd:int, buf:byte[,] -> int)
+ const lseek : (fd:int, off:uint, whence:int -> int)
+ const fstat : (fd:int, sb:statbuf* -> int)
+ const munmap : (addr:byte*, len:uint -> int)
+ const mmap : (addr:byte*, len:size, prot:mprot, flags:mopt, fd:int, off:off -> byte*)
+;;
+
+const exit = {status; -> syscall(Sysexit, 1);}
+const getpid = {; -> syscall(Sysgetpid, 1);}
+const kill = {pid, sig; -> syscall(Syskill, pid, sig);}
+const open = {path, opts:fdopt; -> syscall(Sysopen, path castto(char*), opts);}
+const close = {fd; -> syscall(Sysclose, fd);}
+const creat = {path, mode; -> syscall(Syscreat, path castto(char*), mode);}
+const read = {fd, buf; -> syscall(Sysread, fd, buf castto(char*), buf.len);}
+const write = {fd, buf; -> syscall(Syswrite, fd, buf castto(char*), buf.len);}
+const lseek = {fd, off, whence; -> syscall(Syslseek, fd, off, whence);}
+const fstat = {fd, sb; -> syscall(Sysfstat, fd, sb);}
+const munmap = {addr, len; -> syscall(Sysmunmap, addr, len);}
+const mmap = {addr, len, prot, flags, fd, off; -> syscall(Sysmmap2, addr, len, prot, flags, fd, off) castto(byte*);}
--- a/sys.myr
+++ /dev/null
@@ -1,98 +1,0 @@
-use "types.use"
-
-pkg std =
- type scno = int
- type fdopt = int
- type mprot = int
- type mopt = int
- type statbuf = struct
- dev : uint
- ino : uint
- mode : uint16
- nlink : uint16
- uid : uint16
- gid : uint16
- rdev : uint
- size : uint
- blksize : uint
- blocks : uint
- atime : uint
- atimens : uint
- mtime : uint
- mtimens : uint
- ctime : uint
- ctimens : uint
- _unused1: uint
- _unused2: uint
- ;;
-
- /* open options */
- const Rdonly : fdopt = 0x0
- const Wronly : fdopt = 0x1
- const Rdwr : fdopt = 0x2
- const Append : fdopt = 0x80
- const Creat : fdopt = 0x40
- const Nofollow : fdopt = 0x20000
- const Ndelay : fdopt = 0x800
- const Trunc : fdopt = 0x200
-
- /* mmap protection */
- const Mprotexec : mprot = 0x1
- const Mprotrd : mprot = 0x2
- const Mprotwr : mprot = 0x4
- const Mprotrw : mprot = 0x6
- const Mprotnone : mprot = 0x0
-
- /* mmap options */
- const Mshared : mopt = 0x1
- const Mpriv : mopt = 0x2
- const Mfixed : mopt = 0x10
- const Mfile : mopt = 0x0
- const Manon : mopt = 0x20
- const M32bit : mopt = 0x40
-
- /* return value for a failed mapping */
- const Mapbad : byte* = -1 castto(byte*)
-
- /* syscalls */
- const Sysexit : scno = 1
- const Sysread : scno = 3
- const Syswrite : scno = 4
- const Sysopen : scno = 5
- const Sysclose : scno = 6
- const Syscreat : scno = 8
- const Syslseek : scno = 19
- const Sysfstat : scno = 108
- const Syskill : scno = 37
- const Sysgetpid : scno = 20
- const Sysmmap2 : scno = 192
- const Sysmunmap : scno = 91
-
- extern const syscall : (sc:scno, args:... -> int)
-
- const exit : (status:int -> int)
- const getpid : ( -> int)
- const kill : (pid:int, sig:int -> int)
- const open : (path:byte[,], opts:fdopt -> int)
- const close : (fd:int -> int)
- const creat : (path:byte[,], mode:int -> int)
- const read : (fd:int, buf:byte[,] -> int)
- const write : (fd:int, buf:byte[,] -> int)
- const lseek : (fd:int, off:uint, whence:int -> int)
- const fstat : (fd:int, sb:statbuf* -> int)
- const munmap : (addr:byte*, len:uint -> int)
- const mmap : (addr:byte*, len:size, prot:mprot, flags:mopt, fd:int, off:off -> byte*)
-;;
-
-const exit = {status; -> syscall(Sysexit, 1);}
-const getpid = {; -> syscall(Sysgetpid, 1);}
-const kill = {pid, sig; -> syscall(Syskill, pid, sig);}
-const open = {path, opts:fdopt; -> syscall(Sysopen, path castto(char*), opts);}
-const close = {fd; -> syscall(Sysclose, fd);}
-const creat = {path, mode; -> syscall(Syscreat, path castto(char*), mode);}
-const read = {fd, buf; -> syscall(Sysread, fd, buf castto(char*), buf.len);}
-const write = {fd, buf; -> syscall(Syswrite, fd, buf castto(char*), buf.len);}
-const lseek = {fd, off, whence; -> syscall(Syslseek, fd, off, whence);}
-const fstat = {fd, sb; -> syscall(Sysfstat, fd, sb);}
-const munmap = {addr, len; -> syscall(Sysmunmap, addr, len);}
-const mmap = {addr, len, prot, flags, fd, off; -> syscall(Sysmmap2, addr, len, prot, flags, fd, off) castto(byte*);}
--- /dev/null
+++ b/syscall-linux.s
@@ -1,0 +1,27 @@
+.globl std$syscall
+std$syscall:
+ pushl %ebp
+ movl %esp,%ebp
+ movl 12(%ebp),%eax #count
+ shl $2,%eax
+ /*
+ hack: 6 args uses %ebp, so we index
+ relative to %esp.
+
+ hack: We load 6 args regardless of
+ how many we actually have. This may
+ load junk values, but if the syscall
+ doesn't use them, it's going to be
+ harmless.
+ */
+ movl 32(%esp),%ebp
+ movl 28(%esp),%edi
+ movl 24(%esp),%esi
+ movl 20(%esp),%edx
+ movl 16(%esp),%ecx
+ movl 12(%esp),%ebx
+ movl 8(%esp),%eax
+ int $0x80
+ movl %ebp,%esp
+ popl %ebp
+ ret
--- /dev/null
+++ b/syscall-osx.s
@@ -1,0 +1,8 @@
+.globl _std$syscall
+_std$syscall:
+ popl %ecx /* return address */
+ popl %eax /* call num */
+ pushl %ecx
+ int $0x80
+ pushl %ecx /* put the return address back */
+ ret