summaryrefslogtreecommitdiff
path: root/lib/std
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2015-08-26 12:20:58 -0700
committerOri Bernstein <ori@eigenstate.org>2015-08-26 12:20:58 -0700
commit2bc852bda98762d3bc01548bf972e3f1b137fbfb (patch)
tree74831deed3c9057c5fe0cbb8790d220e855bc792 /lib/std
parent3de952510eb2a23350d24ed926f19c0cf72a12f2 (diff)
downloadmc-2bc852bda98762d3bc01548bf972e3f1b137fbfb.tar.gz
Move Myrddin libs to lib/ subdirectory.
Diffstat (limited to 'lib/std')
-rw-r--r--lib/std/alloc.myr423
-rw-r--r--lib/std/bigint.myr742
-rw-r--r--lib/std/bitset.myr155
-rw-r--r--lib/std/blat.myr32
-rw-r--r--lib/std/bld.sub103
-rwxr-xr-xlib/std/build.sh8
-rw-r--r--lib/std/chartype.myr1242
-rw-r--r--lib/std/clear.myr12
-rw-r--r--lib/std/cmp.myr54
-rw-r--r--lib/std/cstrconv.myr40
-rw-r--r--lib/std/dial+plan9.myr165
-rw-r--r--lib/std/dial+posixy.myr147
-rw-r--r--lib/std/die.myr19
-rw-r--r--lib/std/dir+freebsd.myr0
-rw-r--r--lib/std/dir+linux.myr66
-rw-r--r--lib/std/dir+osx.myr60
-rw-r--r--lib/std/dir+plan9.myr66
-rw-r--r--lib/std/dirname.myr37
-rw-r--r--lib/std/endian.myr32
-rw-r--r--lib/std/env+plan9.myr56
-rw-r--r--lib/std/env+posixy.myr28
-rw-r--r--lib/std/errno+plan9.myr6
-rw-r--r--lib/std/errno.myr40
-rw-r--r--lib/std/execvp.myr57
-rw-r--r--lib/std/extremum.myr40
-rw-r--r--lib/std/fltbits.myr60
-rw-r--r--lib/std/fltfmt.myr239
-rw-r--r--lib/std/fmt.myr499
-rw-r--r--lib/std/fmtfuncs.myr54
-rw-r--r--lib/std/getcwd.myr31
-rw-r--r--lib/std/getint.myr64
-rw-r--r--lib/std/hashfuncs.myr106
-rw-r--r--lib/std/hasprefix.myr12
-rw-r--r--lib/std/hassuffix.myr15
-rw-r--r--lib/std/htab.myr209
-rw-r--r--lib/std/ifreq+freebsd.myr0
-rw-r--r--lib/std/ifreq+linux.myr67
-rw-r--r--lib/std/ifreq+osx.myr77
-rw-r--r--lib/std/ifreq+plan9.myr2
-rw-r--r--lib/std/intparse.myr70
-rw-r--r--lib/std/introspect.myr365
-rw-r--r--lib/std/ipparse.myr147
-rw-r--r--lib/std/mk.myr22
-rw-r--r--lib/std/mkfile107
-rw-r--r--lib/std/mkpath.myr22
-rw-r--r--lib/std/now.myr11
-rw-r--r--lib/std/option.myr7
-rw-r--r--lib/std/optparse.myr210
-rw-r--r--lib/std/pathjoin.myr105
-rw-r--r--lib/std/putint.myr44
-rw-r--r--lib/std/rand.myr185
-rw-r--r--lib/std/resolve+plan9.myr2
-rw-r--r--lib/std/resolve+posixy.myr443
-rw-r--r--lib/std/result.myr9
-rw-r--r--lib/std/search.myr43
-rw-r--r--lib/std/slcp.myr26
-rw-r--r--lib/std/sldup.myr15
-rw-r--r--lib/std/sleq.myr18
-rw-r--r--lib/std/slfill.myr12
-rw-r--r--lib/std/sljoin.myr15
-rw-r--r--lib/std/slpush.myr20
-rw-r--r--lib/std/slput.myr20
-rw-r--r--lib/std/slurp.myr43
-rw-r--r--lib/std/sort.myr61
-rw-r--r--lib/std/spork.myr62
-rw-r--r--lib/std/strbuf.myr107
-rw-r--r--lib/std/strfind.myr51
-rw-r--r--lib/std/strjoin.myr41
-rw-r--r--lib/std/strsplit.myr35
-rw-r--r--lib/std/strstrip.myr43
-rw-r--r--lib/std/swap.myr11
-rw-r--r--lib/std/sys+freebsd-x64.myr805
-rw-r--r--lib/std/sys+linux-x64.myr816
-rw-r--r--lib/std/sys+osx-x64.myr947
-rw-r--r--lib/std/sys+plan9-x64.myr242
-rw-r--r--lib/std/syscall+freebsd-x64.s21
-rw-r--r--lib/std/syscall+linux-x64.s22
-rw-r--r--lib/std/syscall+osx-x64.s96
-rw-r--r--lib/std/syscall+plan9-x64.s33
-rw-r--r--lib/std/syserrno+linux.myr38
-rw-r--r--lib/std/syserrno+osx.myr54
-rw-r--r--lib/std/syserrno+plan9.myr38
-rw-r--r--lib/std/systypes.myr7
-rw-r--r--lib/std/syswrap+plan9-x6.myr104
-rw-r--r--lib/std/syswrap+plan9.myr220
-rw-r--r--lib/std/syswrap+posixy.myr148
-rw-r--r--lib/std/syswrap-ss+linux.myr22
-rw-r--r--lib/std/syswrap-ss+osx.myr35
-rw-r--r--lib/std/syswrap-ss+plan9.myr56
-rw-r--r--lib/std/test/bigint.myr136
-rw-r--r--lib/std/test/bytebuf.myr72
-rw-r--r--lib/std/test/chartype.myr23
-rw-r--r--lib/std/test/fmt.myr34
-rw-r--r--lib/std/test/htab.myr104
-rw-r--r--lib/std/test/ipparse.myr64
-rw-r--r--lib/std/test/option.myr43
-rw-r--r--lib/std/test/pathjoin.myr61
-rw-r--r--lib/std/test/search.myr34
-rw-r--r--lib/std/test/slcp.myr14
-rw-r--r--lib/std/test/sort.myr38
-rw-r--r--lib/std/test/strbuf.myr40
-rw-r--r--lib/std/test/try.myr8
-rw-r--r--lib/std/try.myr38
-rw-r--r--lib/std/types.myr9
-rw-r--r--lib/std/units.myr11
-rw-r--r--lib/std/utf.myr103
-rw-r--r--lib/std/util+plan9-x64.s64
-rw-r--r--lib/std/util+posixy-x64.s72
-rw-r--r--lib/std/varargs.myr105
-rw-r--r--lib/std/wait+plan9.myr94
-rw-r--r--lib/std/wait+posixy.myr41
111 files changed, 12349 insertions, 0 deletions
diff --git a/lib/std/alloc.myr b/lib/std/alloc.myr
new file mode 100644
index 0000000..ed3b023
--- /dev/null
+++ b/lib/std/alloc.myr
@@ -0,0 +1,423 @@
+use "die.use"
+use "extremum.use"
+use "types.use"
+use "units.use"
+use "syswrap.use"
+
+/*
+The allocator implementation here is based on Bonwick's slab allocator.
+
+For small allocations (up to Bktmax), it works by requesting large,
+power of two aligned chunks from the operating system, and breaking
+them into a linked list of equal sized chunks. Allocations are then
+satisfied by taking the head of the list of chunks. Empty slabs
+are removed from the freelist.
+
+The data structure looks something like this:
+ Bkts:
+ [16 byte] -> [slab hdr | chunk -> chunk -> chunk] -> [slab hdr | chunk -> chunk -> chunk]
+ [32 byte] -> Zslab
+ [64 byte] -> [slab hdr | chunk -> chunk]
+ ...
+ [32k byte] -> ...
+
+Large allocations are simply satisfied by mmap().
+
+*/
+
+pkg std =
+ generic alloc : ( -> @a#)
+ generic zalloc : ( -> @a#)
+ generic free : (v:@a# -> void)
+
+ generic slalloc : (len : size -> @a[:])
+ generic slzalloc : (len : size -> @a[:])
+ generic slgrow : (sl : @a[:], len : size -> @a[:])
+ generic slzgrow : (sl : @a[:], len : size -> @a[:])
+ generic slfree : (sl : @a[:] -> void)
+
+ const bytealloc : (sz:size -> byte#)
+ const zbytealloc : (sz:size -> byte#)
+ const bytefree : (m:byte#, sz:size -> void)
+
+
+;;
+
+/* null pointers. only used internally. */
+const Zslab = 0 castto(slab#)
+const Zchunk = 0 castto(chunk#)
+
+const Slabsz = 1*MiB /* 1 meg slabs */
+const Cachemax = 16 /* maximum number of slabs in the cache */
+const Bktmax = 32*KiB /* Slabsz / 8; a balance. */
+const Pagesz = 4*KiB
+const Align = 16 /* minimum allocation alignment */
+
+var buckets : bucket[32] /* excessive */
+
+type slheader = struct
+ cap : size /* capacity in bytes */
+ magic : size /* magic check value */
+;;
+
+type bucket = struct
+ sz : size /* aligned size */
+ nper : size /* max number of elements per slab */
+ slabs : slab# /* partially filled or free slabs */
+ cache : slab# /* cache of empty slabs, to prevent thrashing */
+ ncache : size /* size of cache */
+;;
+
+type slab = struct
+ head : byte# /* head of virtual addresses, so we don't leak address space */
+ next : slab# /* the next slab on the chain */
+ freehd : chunk# /* the nodes we're allocating */
+ nfree : size /* the number of free nodes */
+;;
+
+type chunk = struct /* NB: must be smaller than sizeof(slab) */
+ next : chunk# /* the next chunk in the free list */
+;;
+
+const __init__ = {
+ var i
+
+ for i = 0; i < buckets.len && (Align << i) <= Bktmax; i++
+ bktinit(&buckets[i], Align << i)
+ ;;
+}
+
+/* Allocates an object of type @a, returning a pointer to it. */
+generic alloc = {-> @a#
+ -> bytealloc(sizeof(@a)) castto(@a#)
+}
+
+generic zalloc = {-> @a#
+ -> zbytealloc(sizeof(@a)) castto(@a#)
+}
+
+/* Frees a value of type @a */
+generic free = {v:@a# -> void
+ bytefree(v castto(byte#), sizeof(@a))
+}
+
+/* allocates a slice of 'len' elements. */
+generic slalloc = {len
+ var p, sz
+
+ if len == 0
+ -> [][:]
+ ;;
+ sz = len*sizeof(@a) + align(sizeof(slheader), Align)
+ p = bytealloc(sz)
+ p = inithdr(p, sz)
+ -> (p castto(@a#))[0:len]
+}
+
+generic slzalloc = {len
+ var p, sz
+
+ if len == 0
+ -> [][:]
+ ;;
+ sz = len*sizeof(@a) + align(sizeof(slheader), Align)
+ p = zbytealloc(sz)
+ p = inithdr(p, sz)
+ -> (p castto(@a#))[0:len]
+}
+
+const inithdr = {p, sz
+ var phdr, prest
+
+ phdr = p castto(slheader#)
+ phdr.cap = allocsz(sz) - align(sizeof(slheader), Align)
+ phdr.magic = (0xdeadbeefbadf00d castto(size))
+
+ prest = (p castto(size)) + align(sizeof(slheader), Align)
+ -> prest castto(byte#)
+}
+
+const checkhdr = {p
+ var phdr, addr
+
+ addr = p castto(size)
+ addr -= align(sizeof(slheader), Align)
+ phdr = addr castto(slheader#)
+ assert(phdr.magic == (0xdeadbeefbadf00d castto(size)), "corrupt memory\n")
+}
+
+/* Frees a slice */
+generic slfree = {sl
+ var head
+
+ if sl.len == 0
+ ->
+ ;;
+
+ checkhdr(sl castto(byte#))
+ head = (sl castto(byte#)) castto(size)
+ head -= align(sizeof(slheader), Align)
+ bytefree(head castto(byte#), slcap(sl castto(byte#)))
+}
+
+/* Grows a slice */
+generic slgrow = {sl : @a[:], len
+ var i, n
+ var new
+
+ /* if the slice doesn't need a bigger bucket, we don't need to realloc. */
+ if sl.len > 0 && slcap(sl castto(byte#)) >= allocsz(len*sizeof(@a))
+ -> (sl castto(@a#))[:len]
+ ;;
+
+ new = slalloc(len)
+ n = min(len, sl.len)
+ for i = 0; i < n; i++
+ new[i] = sl[i]
+ ;;
+ if sl.len > 0
+ slfree(sl)
+ ;;
+ -> new
+}
+
+/* Grows a slice, filling new entries with zero bytes */
+generic slzgrow = {sl : @a[:], len
+ var oldsz
+
+ oldsz = sl.len*sizeof(@a)
+ sl = slgrow(sl, len)
+ zfill((sl castto(byte#))[oldsz:len*sizeof(@a)])
+ -> sl
+}
+
+const slcap = {p
+ var phdr
+
+ phdr = (p castto(size)) - align(sizeof(slheader), Align) castto(slheader#)
+ -> phdr.cap
+}
+
+const zbytealloc = {sz
+ var p
+
+ p = bytealloc(sz)
+ zfill(p[0:sz])
+ -> p
+}
+
+const zfill = {sl
+ var i
+
+ for i = 0; i < sl.len; i++
+ sl[i] = 0
+ ;;
+}
+
+/* Allocates a blob that is 'sz' bytes long. Dies if the allocation fails */
+const bytealloc = {sz
+ var bkt, p
+
+ if (sz <= Bktmax)
+ bkt = &buckets[bktnum(sz)]
+ p = bktalloc(bkt)
+ else
+ p = getmem(sz)
+ if p == Failmem
+ die("could not get memory\n")
+ ;;
+ ;;
+ -> p
+}
+
+/* frees a blob that is 'sz' bytes long. */
+const bytefree = {p, sz
+ var bkt
+ var b, i
+
+ b = p[:sz]
+ for i = 0; i < sz; i++
+ b[i] = 0xa8
+ ;;
+ if (sz < Bktmax)
+ bkt = &buckets[bktnum(sz)]
+ bktfree(bkt, p)
+ else
+ freemem(p, sz)
+ ;;
+}
+
+/* Sets up a single empty bucket */
+const bktinit = {b, sz
+ b.sz = align(sz, Align)
+ b.nper = (Slabsz - sizeof(slab))/b.sz
+ b.slabs = Zslab
+ b.cache = Zslab
+ b.ncache = 0
+}
+
+/* Creates a slab for bucket 'bkt', and fills the chunk list */
+const mkslab = {bkt
+ var i, p, s
+ var b, bnext
+ var off /* offset of chunk head */
+
+ if bkt.ncache > 0
+ s = bkt.cache
+ bkt.cache = s.next
+ bkt.ncache--
+ ;;
+ /*
+ tricky: we need power of two alignment, so we allocate double the
+ needed size, chop off the unaligned ends, and waste the address
+ space. Since the OS is "smart enough", this shouldn't actually
+ cost us memory, and 64 bits of address space means that we're not
+ going to have issues with running out of address space for a
+ while. On a 32 bit system this would be a bad idea.
+ */
+ p = getmem(Slabsz*2)
+ if p == Failmem
+ die("Unable to mmap")
+ ;;
+
+ s = align(p castto(size), Slabsz) castto(slab#)
+ s.head = p
+ s.nfree = bkt.nper
+ /* skip past the slab header */
+ off = align(sizeof(slab), Align)
+ bnext = nextchunk(s castto(chunk#), off)
+ s.freehd = bnext
+ for i = 0; i < bkt.nper; i++
+ b = bnext
+ bnext = nextchunk(b, bkt.sz)
+ b.next = bnext
+ ;;
+ b.next = Zchunk
+ -> s
+}
+
+/*
+Allocates a node from bucket 'bkt', crashing if the
+allocation cannot be satisfied. Will create a new slab
+if there are no slabs on the freelist.
+*/
+const bktalloc = {bkt
+ var s
+ var b
+
+ /* find a slab */
+ s = bkt.slabs
+ if s == Zslab
+ s = mkslab(bkt)
+ if s == Zslab
+ die("No memory left")
+ ;;
+ bkt.slabs = s
+ ;;
+
+ /* grab the first chunk on the slab */
+ b = s.freehd
+ s.freehd = b.next
+ s.nfree--
+ if s.nfree == 0
+ bkt.slabs = s.next
+ s.next = Zslab
+ ;;
+
+ -> b castto(byte#)
+}
+
+/*
+Frees a chunk of memory 'm' into bucket 'bkt'.
+Assumes that the memory already came from a slab
+that was created for bucket 'bkt'. Will crash
+if this is not the case.
+*/
+const bktfree = {bkt, m
+ var s, b
+
+ s = mtrunc(m, Slabsz) castto(slab#)
+ b = m castto(chunk#)
+ if s.nfree == 0
+ s.next = bkt.slabs
+ bkt.slabs = s
+ elif s.nfree == bkt.nper
+ /*
+ HACK HACK HACK: if we can't unmap, keep an infinite cache per slab size.
+ We should solve this better somehow.
+ */
+ if bkt.ncache < Cachemax || !Canunmap
+ s.next = bkt.cache
+ bkt.cache = s
+ else
+ /* we mapped 2*Slabsz so we could align it,
+ so we need to unmap the same */
+ freemem(s.head, Slabsz*2)
+ ;;
+ ;;
+ s.nfree++
+ b.next = s.freehd
+ s.freehd = b
+}
+
+/*
+Finds the correct bucket index to allocate from
+for allocations of size 'sz'
+*/
+const bktnum = {sz
+ var i, bktsz
+
+ bktsz = Align
+ for i = 0; bktsz <= Bktmax; i++
+ if bktsz >= sz
+ -> i
+ ;;
+ bktsz *= 2
+ ;;
+ die("Size does not match any buckets")
+}
+
+/*
+returns the actual size we allocated for a given
+size request
+*/
+const allocsz = {sz
+ var i, bktsz
+
+ if sz <= Bktmax
+ bktsz = Align
+ for i = 0; bktsz <= Bktmax; i++
+ if bktsz >= sz
+ -> bktsz
+ ;;
+ bktsz *= 2
+ ;;
+ else
+ -> align(sz, Pagesz)
+ ;;
+ die("Size does not match any buckets")
+}
+
+/*
+aligns a size to a requested alignment.
+'align' must be a power of two
+*/
+const align = {v, align
+ -> (v + align - 1) & ~(align - 1)
+}
+
+/*
+chunks are variable sizes, so we can't just
+index to get to the next one
+*/
+const nextchunk = {b, sz : size
+ -> ((b castto(intptr)) + (sz castto(intptr))) castto(chunk#)
+}
+
+/*
+truncates a pointer to 'align'. 'align' must
+be a power of two.
+*/
+const mtrunc = {m, align
+ -> ((m castto(intptr)) & ~((align castto(intptr)) - 1)) castto(byte#)
+}
diff --git a/lib/std/bigint.myr b/lib/std/bigint.myr
new file mode 100644
index 0000000..719fd69
--- /dev/null
+++ b/lib/std/bigint.myr
@@ -0,0 +1,742 @@
+use "alloc.use"
+use "chartype.use"
+use "cmp.use"
+use "die.use"
+use "extremum.use"
+use "hasprefix.use"
+use "option.use"
+use "slcp.use"
+use "sldup.use"
+use "slfill.use"
+use "slpush.use"
+use "types.use"
+use "utf.use"
+use "errno.use"
+
+pkg std =
+ type bigint = struct
+ dig : uint32[:] /* little endian, no leading zeros. */
+ sign : int /* -1 for -ve, 0 for zero, 1 for +ve. */
+ ;;
+
+ /* administrivia */
+ generic mkbigint : (v : @a::(numeric,integral) -> bigint#)
+ const bigfree : (a : bigint# -> void)
+ const bigdup : (a : bigint# -> bigint#)
+ const bigassign : (d : bigint#, s : bigint# -> bigint#)
+ const bigmove : (d : bigint#, s : bigint# -> bigint#)
+ const bigparse : (s : byte[:] -> option(bigint#))
+ const bigclear : (a : bigint# -> bigint#)
+ const bigbfmt : (b : byte[:], a : bigint#, base : int -> size)
+ /*
+ const bigtoint : (a : bigint# -> @a::(numeric,integral))
+ */
+
+ /* some useful predicates */
+ const bigiszero : (a : bigint# -> bool)
+ const bigeq : (a : bigint#, b : bigint# -> bool)
+ generic bigeqi : (a : bigint#, b : @a::(numeric,integral) -> bool)
+ const bigcmp : (a : bigint#, b : bigint# -> order)
+
+ /* bigint*bigint -> bigint ops */
+ const bigadd : (a : bigint#, b : bigint# -> bigint#)
+ const bigsub : (a : bigint#, b : bigint# -> bigint#)
+ const bigmul : (a : bigint#, b : bigint# -> bigint#)
+ const bigdiv : (a : bigint#, b : bigint# -> bigint#)
+ const bigmod : (a : bigint#, b : bigint# -> bigint#)
+ const bigdivmod : (a : bigint#, b : bigint# -> (bigint#, bigint#))
+ const bigshl : (a : bigint#, b : bigint# -> bigint#)
+ const bigshr : (a : bigint#, b : bigint# -> bigint#)
+ const bigmodpow : (b : bigint#, e : bigint#, m : bigint# -> bigint#)
+ /*
+ const bigpow : (a : bigint#, b : bigint# -> bigint#)
+ */
+
+ /* bigint*int -> bigint ops */
+ generic bigaddi : (a : bigint#, b : @a::(integral,numeric) -> bigint#)
+ generic bigsubi : (a : bigint#, b : @a::(integral,numeric) -> bigint#)
+ generic bigmuli : (a : bigint#, b : @a::(integral,numeric) -> bigint#)
+ generic bigdivi : (a : bigint#, b : @a::(integral,numeric) -> bigint#)
+ generic bigshli : (a : bigint#, b : @a::(integral,numeric) -> bigint#)
+ generic bigshri : (a : bigint#, b : @a::(integral,numeric) -> bigint#)
+ /*
+ const bigpowi : (a : bigint#, b : uint64 -> bigint#)
+ */
+;;
+
+const Base = 0x100000000ul
+generic mkbigint = {v : @a::(integral,numeric)
+ var a
+ var val
+
+ a = zalloc()
+
+ if v < 0
+ a.sign = -1
+ v = -v
+ elif v > 0
+ a.sign = 1
+ ;;
+ val = v castto(uint64)
+ a.dig = slpush([][:], val castto(uint32))
+ if val > Base
+ a.dig = slpush(a.dig, (val/Base) castto(uint32))
+ ;;
+ -> trim(a)
+}
+
+const bigfree = {a
+ slfree(a.dig)
+ free(a)
+}
+
+const bigdup = {a
+ -> bigassign(zalloc(), a)
+}
+
+const bigassign = {d, s
+ slfree(d.dig)
+ d# = s#
+ d.dig = sldup(s.dig)
+ -> d
+}
+
+const bigmove = {d, s
+ slfree(d.dig)
+ d# = s#
+ s.dig = [][:]
+ s.sign = 0
+ -> d
+}
+
+const bigclear = {v
+ std.slfree(v.dig)
+ v.sign = 0
+ v.dig = [][:]
+ -> v
+}
+
+/* for now, just dump out something for debugging... */
+const bigbfmt = {buf, x, base
+ const digitchars = [
+ '0','1','2','3','4','5','6','7','8','9',
+ 'a','b','c','d','e','f', 'g', 'h', 'i', 'j',
+ 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
+ 't', 'u', 'v', 'w', 'x', 'y', 'z']
+ var v, val
+ var n, i
+ var tmp, rem, b
+
+ if base < 0 || base > 36
+ die("invalid base in bigbfmt\n")
+ ;;
+
+ if bigiszero(x)
+ n
+ ;;
+
+ if base == 0
+ b = mkbigint(10)
+ else
+ b = mkbigint(base)
+ ;;
+ n = 0
+ val = bigdup(x)
+ /* generate the digits in reverse order */
+ while !bigiszero(val)
+ (v, rem) = bigdivmod(val, b)
+ if rem.dig.len > 0
+ n += encode(buf[n:], digitchars[rem.dig[0]])
+ else
+ n += encode(buf[n:], '0')
+ ;;
+ bigfree(val)
+ bigfree(rem)
+ val = v
+ ;;
+ bigfree(val)
+ bigfree(b)
+
+ /* this is done last, so we get things right when we reverse the string */
+ if x.sign == 0
+ n += encode(buf[n:], '0')
+ elif x.sign == -1
+ n += encode(buf[n:], '-')
+ ;;
+
+ /* we only generated ascii digits, so this works for reversing. */
+ for i = 0; i < n/2; i++
+ tmp = buf[i]
+ buf[i] = buf[n - i - 1]
+ buf[n - i - 1] = tmp
+ ;;
+ -> n
+}
+
+const bigparse = {str
+ var c, val : int, base
+ var v, b
+ var a
+
+ if hasprefix(str, "0x") || hasprefix(str, "0X")
+ base = 16
+ elif hasprefix(str, "0o") || hasprefix(str, "0O")
+ base = 8
+ elif hasprefix(str, "0b") || hasprefix(str, "0B")
+ base = 2
+ else
+ base = 10
+ ;;
+ if base != 10
+ str = str[2:]
+ ;;
+
+ a = mkbigint(0)
+ b = mkbigint(base)
+ /*
+ efficiency hack: to save allocations,
+ just mutate v[0]. The value will always
+ fit in one digit.
+ */
+ v = mkbigint(1)
+ while str.len != 0
+ (c, str) = striter(str)
+ if c == '_'
+ continue
+ ;;
+ val = charval(c, base)
+ if val < 0 || val > base
+ bigfree(a)
+ bigfree(b)
+ bigfree(v)
+ -> `None
+ ;;
+ v.dig[0] = val castto(uint32)
+ if val == 0
+ v.sign = 0
+ else
+ v.sign = 1
+ ;;
+ bigmul(a, b)
+ bigadd(a, v)
+
+ ;;
+ -> `Some a
+}
+
+const bigiszero = {v
+ -> v.dig.len == 0
+}
+
+const bigeq = {a, b
+ var i
+
+ if a.sign != b.sign || a.dig.len != b.dig.len
+ -> false
+ ;;
+ for i = 0; i < a.dig.len; i++
+ if a.dig[i] != b.dig[i]
+ -> false
+ ;;
+ ;;
+ -> true
+}
+
+generic bigeqi = {a, b
+ var v
+ var dig : uint32[2]
+
+ bigdigit(&v, b < 0, b castto(uint64), dig[:])
+ -> bigeq(a, &v)
+}
+
+const bigcmp = {a, b
+ var i
+ var da, db, sa, sb
+
+ sa = a.sign castto(int64)
+ sb = b.sign castto(int64)
+ if sa < sb
+ -> `Before
+ elif sa > sb
+ -> `After
+ elif a.dig.len < b.dig.len
+ -> signedorder(-sa)
+ elif a.dig.len > b.dig.len
+ -> signedorder(sa)
+ else
+ /* otherwise, the one with the first larger digit is bigger */
+ for i = a.dig.len; i > 0; i--
+ da = a.dig[i - 1] castto(int64)
+ db = b.dig[i - 1] castto(int64)
+ -> signedorder(sa * (da - db))
+ ;;
+ ;;
+ -> `Equal
+}
+
+const signedorder = {sign
+ if sign < 0
+ -> `Before
+ elif sign == 0
+ -> `Equal
+ else
+ -> `After
+ ;;
+}
+
+/* a += b */
+const bigadd = {a, b
+ if a.sign == b.sign || a.sign == 0
+ a.sign = b.sign
+ -> uadd(a, b)
+ elif b.sign == 0
+ -> a
+ else
+ match bigcmp(a, b)
+ | `Before: /* a is negative */
+ a.sign = b.sign
+ -> usub(b, a)
+ | `After: /* b is negative */
+ -> usub(a, b)
+ | `Equal:
+ die("Impossible. Equal vals with different sign.")
+ ;;
+ ;;
+}
+
+/* adds two unsigned values together. */
+const uadd = {a, b
+ var v, i
+ var carry
+ var n
+
+ carry = 0
+ n = max(a.dig.len, b.dig.len)
+ /* guaranteed to carry no more than one value */
+ a.dig = slzgrow(a.dig, n + 1)
+ for i = 0; i < n; i++
+ v = (a.dig[i] castto(uint64)) + carry;
+ if i < b.dig.len
+ v += (b.dig[i] castto(uint64))
+ ;;
+
+ if v >= Base
+ carry = 1
+ else
+ carry = 0
+ ;;
+ a.dig[i] = v castto(uint32)
+ ;;
+ a.dig[i] += carry castto(uint32)
+ -> trim(a)
+}
+
+/* a -= b */
+const bigsub = {a, b
+ /* 0 - x = -x */
+ if a.sign == 0
+ bigassign(a, b)
+ a.sign = -b.sign
+ -> a
+ /* x - 0 = x */
+ elif b.sign == 0
+ -> a
+ elif a.sign != b.sign
+ -> uadd(a, b)
+ else
+ match bigcmp(a, b)
+ | `Before: /* a is negative */
+ a.sign = b.sign
+ -> usub(b, a)
+ | `After: /* b is negative */
+ -> usub(a, b)
+ | `Equal:
+ -> bigclear(a)
+ ;;
+ ;;
+ -> a
+}
+
+/* subtracts two unsigned values, where 'a' is strictly greater than 'b' */
+const usub = {a, b
+ var carry
+ var v, i
+
+ carry = 0
+ for i = 0; i < a.dig.len; i++
+ v = (a.dig[i] castto(int64)) - carry
+ if i < b.dig.len
+ v -= (b.dig[i] castto(int64))
+ ;;
+ if v < 0
+ carry = 1
+ else
+ carry = 0
+ ;;
+ a.dig[i] = v castto(uint32)
+ ;;
+ -> trim(a)
+}
+
+/* a *= b */
+const bigmul = {a, b
+ var i, j
+ var ai, bj, wij
+ var carry, t
+ var w
+
+ if a.sign == 0 || b.sign == 0
+ a.sign = 0
+ slfree(a.dig)
+ a.dig = [][:]
+ -> a
+ elif a.sign != b.sign
+ a.sign = -1
+ else
+ a.sign = 1
+ ;;
+ w = slzalloc(a.dig.len + b.dig.len)
+ for j = 0; j < b.dig.len; j++
+ carry = 0
+ for i = 0; i < a.dig.len; i++
+ ai = a.dig[i] castto(uint64)
+ bj = b.dig[j] castto(uint64)
+ wij = w[i+j] castto(uint64)
+ t = ai * bj + wij + carry
+ w[i + j] = (t castto(uint32))
+ carry = t >> 32
+ ;;
+ w[i+j] = carry castto(uint32)
+ ;;
+ slfree(a.dig)
+ a.dig = w
+ -> trim(a)
+}
+
+const bigdiv = {a : bigint#, b : bigint# -> bigint#
+ var q, r
+
+ (q, r) = bigdivmod(a, b)
+ bigfree(r)
+ -> bigmove(a, q)
+}
+const bigmod = {a : bigint#, b : bigint# -> bigint#
+ var q, r
+
+ (q, r) = bigdivmod(a, b)
+ bigfree(q)
+ -> bigmove(a, r)
+}
+
+/* a /= b */
+const bigdivmod = {a : bigint#, b : bigint# -> (bigint#, bigint#)
+ /*
+ Implements bigint division using Algorithm D from
+ Knuth: Seminumerical algorithms, Section 4.3.1.
+ */
+ var m : int64, n : int64
+ var qhat, rhat, carry, shift
+ var x, y, z, w, p, t /* temporaries */
+ var b0, aj
+ var u, v
+ var i, j : int64
+ var q
+
+ if bigiszero(b)
+ die("divide by zero\n")
+ ;;
+ /* if b > a, we trucate to 0, with remainder 'a' */
+ if a.dig.len < b.dig.len
+ -> (mkbigint(0), bigdup(a))
+ ;;
+
+ q = zalloc()
+ q.dig = slzalloc(max(a.dig.len, b.dig.len) + 1)
+ if a.sign != b.sign
+ q.sign = -1
+ else
+ q.sign = 1
+ ;;
+
+ /* handle single digit divisor separately: the knuth algorithm needs at least 2 digits. */
+ if b.dig.len == 1
+ carry = 0
+ b0 = (b.dig[0] castto(uint64))
+ for j = a.dig.len; j > 0; j--
+ aj = (a.dig[j - 1] castto(uint64))
+ q.dig[j - 1] = (((carry << 32) + aj)/b0) castto(uint32)
+ carry = (carry << 32) + aj - (q.dig[j-1] castto(uint64))*b0
+ ;;
+ q = trim(q)
+ -> (q, trim(mkbigint(carry castto(int32))))
+ ;;
+
+ u = bigdup(a)
+ v = bigdup(b)
+ m = u.dig.len
+ n = v.dig.len
+
+ shift = nlz(v.dig[n - 1])
+ bigshli(u, shift)
+ bigshli(v, shift)
+ u.dig = slzgrow(u.dig, u.dig.len + 1)
+
+ for j = m - n; j >= 0; j--
+ /* load a few temps */
+ x = u.dig[j + n] castto(uint64)
+ y = u.dig[j + n - 1] castto(uint64)
+ z = v.dig[n - 1] castto(uint64)
+ w = v.dig[n - 2] castto(uint64)
+ t = u.dig[j + n - 2] castto(uint64)
+
+ /* estimate qhat */
+ qhat = (x*Base + y)/z
+ rhat = (x*Base + y) - qhat*z
+:divagain
+ if qhat >= Base || (qhat * w) > (rhat*Base + t)
+ qhat--
+ rhat += z
+ if rhat < Base
+ goto divagain
+ ;;
+ ;;
+
+ /* multiply and subtract */
+ carry = 0
+ for i = 0; i < n; i++
+ p = qhat * (v.dig[i] castto(uint64))
+
+ t = (u.dig[i+j] castto(uint64)) - carry - (p % Base)
+ u.dig[i+j] = t castto(uint32)
+
+ carry = (((p castto(int64)) >> 32) - ((t castto(int64)) >> 32)) castto(uint64);
+ ;;
+ t = (u.dig[j + n] castto(uint64)) - carry
+ u.dig[j + n] = t castto(uint32)
+ q.dig[j] = qhat castto(uint32)
+ /* adjust */
+ if t castto(int64) < 0
+ q.dig[j]--
+ carry = 0
+ for i = 0; i < n; i++
+ t = (u.dig[i+j] castto(uint64)) + (v.dig[i] castto(uint64)) + carry
+ u.dig[i+j] = t castto(uint32)
+ carry = t >> 32
+ ;;
+ u.dig[j+n] = u.dig[j+n] + (carry castto(uint32));
+ ;;
+
+ ;;
+ /* undo the biasing for remainder */
+ u = bigshri(u, shift)
+ -> (trim(q), trim(u))
+}
+
+/* computes b^e % m */
+const bigmodpow = {base, exp, mod
+ var r, n
+
+ r = mkbigint(1)
+ n = 0
+ while !bigiszero(exp)
+ if (exp.dig[0] & 1) != 0
+ bigmul(r, base)
+ bigmod(r, mod)
+ ;;
+ bigshri(exp, 1)
+ bigmul(base, base)
+ bigmod(base, mod)
+ ;;
+ -> bigmove(base, r)
+}
+
+/* returns the number of leading zeros */
+const nlz = {a : uint32
+ var n
+
+ if a == 0
+ -> 32
+ ;;
+ n = 0
+ if a <= 0x0000ffff
+ n += 16
+ a <<= 16
+ ;;
+ if a <= 0x00ffffff
+ n += 8
+ a <<= 8
+ ;;
+ if a <= 0x0fffffff
+ n += 4
+ a <<= 4
+ ;;
+ if a <= 0x3fffffff
+ n += 2
+ a <<= 2
+ ;;
+ if a <= 0x7fffffff
+ n += 1
+ a <<= 1
+ ;;
+ -> n
+}
+
+
+/* a <<= b */
+const bigshl = {a, b
+ match b.dig.len
+ | 0: -> a
+ | 1: -> bigshli(a, b.dig[0] castto(uint64))
+ | n: die("shift by way too much\n")
+ ;;
+}
+
+/* a >>= b, unsigned */
+const bigshr = {a, b
+ match b.dig.len
+ | 0: -> a
+ | 1: -> bigshri(a, b.dig[0] castto(uint64))
+ | n: die("shift by way too much\n")
+ ;;
+}
+
+/* a + b, b is integer.
+FIXME: acually make this a performace improvement
+*/
+generic bigaddi = {a, b
+ var bigb : bigint
+ var dig : uint32[2]
+
+ bigdigit(&bigb, b < 0, b castto(uint64), dig[:])
+ bigadd(a, &bigb)
+ -> a
+}
+
+generic bigsubi = {a, b : @a::(numeric,integral)
+ var bigb : bigint
+ var dig : uint32[2]
+
+ bigdigit(&bigb, b < 0, b castto(uint64), dig[:])
+ bigsub(a, &bigb)
+ -> a
+}
+
+generic bigmuli = {a, b
+ var bigb : bigint
+ var dig : uint32[2]
+
+ bigdigit(&bigb, b < 0, b castto(uint64), dig[:])
+ bigmul(a, &bigb)
+ -> a
+}
+
+generic bigdivi = {a, b
+ var bigb : bigint
+ var dig : uint32[2]
+
+ bigdigit(&bigb, b < 0, b castto(uint64), dig[:])
+ bigdiv(a, &bigb)
+ -> a
+}
+
+/*
+ a << s, with integer arg.
+ logical left shift. any other type would be illogical.
+ */
+generic bigshli = {a, s : @a::(numeric,integral)
+ var off, shift
+ var t, carry
+ var i
+
+ assert(s >= 0, "shift amount must be positive")
+ off = (s castto(uint64)) / 32
+ shift = (s castto(uint64)) % 32
+
+ /* zero shifted by anything is zero */
+ if a.sign == 0
+ -> a
+ ;;
+ a.dig = slzgrow(a.dig, 1 + a.dig.len + off castto(size))
+ /* blit over the base values */
+ for i = a.dig.len; i > off; i--
+ a.dig[i - 1] = a.dig[i - 1 - off]
+ ;;
+ for i = 0; i < off; i++
+ a.dig[i] = 0
+ ;;
+ /* and shift over by the remainder */
+ carry = 0
+ for i = 0; i < a.dig.len; i++
+ t = (a.dig[i] castto(uint64)) << shift
+ a.dig[i] = (t | carry) castto(uint32)
+ carry = t >> 32
+ ;;
+ -> trim(a)
+}
+
+/* logical shift right, zero fills. sign remains untouched. */
+generic bigshri = {a, s
+ var off, shift
+ var t, carry
+ var i
+
+ assert(s >= 0, "shift amount must be positive")
+ off = (s castto(uint64)) / 32
+ shift = (s castto(uint64)) % 32
+
+ /* blit over the base values */
+ for i = 0; i < a.dig.len - off; i++
+ a.dig[i] = a.dig[i + off]
+ ;;
+ for i = a.dig.len - off; i < a.dig.len; i++
+ a.dig[i] = 0
+ ;;
+ /* and shift over by the remainder */
+ carry = 0
+ for i = a.dig.len; i > 0; i--
+ t = (a.dig[i - 1] castto(uint64))
+ a.dig[i - 1] = (carry | (t >> shift)) castto(uint32)
+ carry = t << (32 - shift)
+ ;;
+ -> trim(a)
+}
+
+/* creates a bigint on the stack; should not be modified. */
+const bigdigit = {v, isneg : bool, val : uint64, dig
+ v.sign = 1
+ if isneg
+ val = -val
+ v.sign = -1
+ ;;
+ if val == 0
+ v.sign = 0
+ v.dig = [][:]
+ elif val < Base
+ v.dig = dig[:1]
+ v.dig[0] = val castto(uint32)
+ else
+ v.dig = dig
+ v.dig[0] = val castto(uint32)
+ v.dig[1] = (val >> 32) castto(uint32)
+ ;;
+}
+
+/* trims leading zeros */
+const trim = {a
+ var i
+
+ for i = a.dig.len; i > 0; i--
+ if a.dig[i - 1] != 0
+ break
+ ;;
+ ;;
+ a.dig = slgrow(a.dig, i)
+ if i == 0
+ a.sign = 0
+ elif a.sign == 0
+ a.sign = 1
+ ;;
+ -> a
+}
+
diff --git a/lib/std/bitset.myr b/lib/std/bitset.myr
new file mode 100644
index 0000000..7843c7d
--- /dev/null
+++ b/lib/std/bitset.myr
@@ -0,0 +1,155 @@
+use "alloc.use"
+use "die.use"
+use "extremum.use"
+use "mk.use"
+use "slcp.use"
+use "sldup.use"
+use "slfill.use"
+use "types.use"
+
+pkg std =
+ type bitset = struct
+ bits : size[:]
+ ;;
+
+ const mkbs : (-> bitset#)
+ const bsdup : (bs : bitset# -> bitset#)
+ const bsfree : (bs : bitset# -> void)
+
+ const bsmax : (a : bitset# -> size)
+
+ generic bsput : (bs : bitset#, v : @a::(integral,numeric) -> void)
+ generic bsdel : (bs : bitset#, v : @a::(integral,numeric) -> void)
+ generic bshas : (bs : bitset#, v : @a::(integral,numeric) -> bool)
+
+ const bsdiff : (a : bitset#, b : bitset# -> void)
+ const bsintersect : (a : bitset#, b : bitset# -> void)
+ const bsunion : (a : bitset#, b : bitset# -> void)
+ const bseq : (a : bitset#, b : bitset# -> bool)
+ const bsissubset : (a : bitset#, b : bitset# -> bool)
+
+
+ const bsclear : (bs : bitset# -> bitset#)
+;;
+
+const mkbs = {
+ -> zalloc()
+}
+
+const bsdup = {bs
+ -> mk([.bits=sldup(bs.bits)])
+}
+
+const bsfree = {bs
+ slfree(bs.bits)
+ free(bs)
+}
+
+const bsclear = {bs
+ slfill(bs.bits, 0)
+ -> bs
+}
+
+const bsmax = {bs
+ -> bs.bits.len * sizeof(size) * 8
+}
+
+generic bsput = {bs, v
+ var idx
+ var off
+
+ idx = (v castto(size)) / (8*sizeof(size))
+ off = (v castto(size)) % (8*sizeof(size))
+ ensurelen(bs, idx)
+ bs.bits[idx] |= (1 << off)
+}
+
+generic bsdel = {bs, v
+ var idx
+ var off
+
+ idx = (v castto(size)) / (8*sizeof(size))
+ off = (v castto(size)) % (8*sizeof(size))
+ if idx >= bs.bits.len
+ ->
+ ;;
+ bs.bits[idx] &= ~(1 << off)
+}
+
+generic bshas = {bs, v
+ var idx
+ var off
+
+ idx = (v castto(size)) / (8*sizeof(size))
+ off = (v castto(size)) % (8*sizeof(size))
+ if idx >= bs.bits.len
+ -> false
+ ;;
+ -> (bs.bits[idx] & (1 << off)) != 0
+}
+
+const bsunion = {a, b
+ var i
+
+ eqsz(a, b)
+ for i = 0; i < b.bits.len; i++
+ a.bits[i] |= b.bits[i]
+ ;;
+}
+
+const bsintersect = {a, b
+ var i, n
+
+ n = min(a.bits.len, b.bits.len)
+ for i = 0; i < n; i++
+ a.bits[i] &= b.bits[i]
+ ;;
+}
+
+const bsdiff = {a, b
+ var i, n
+
+ n = min(b.bits.len, a.bits.len)
+ for i = 0; i < n; i++
+ a.bits[i] &= ~b.bits[i]
+ ;;
+}
+
+const bsissubset = {a, b
+ var i
+
+ eqsz(a, b);
+ for i = 0; i < a.bits.len; i++
+ if (b.bits[i] & a.bits[i]) != b.bits[i]
+ -> false
+ ;;
+ ;;
+ -> true
+}
+
+const bseq = {a, b
+ var i
+
+ eqsz(a, b)
+ for i = 0; i < a.bits.len; i++
+ if a.bits[i] != b.bits[i]
+ -> false
+ ;;
+ ;;
+ -> true
+}
+
+const ensurelen = {bs, len
+ if bs.bits.len <= len
+ bs.bits = slzgrow(bs.bits, len + 1)
+ ;;
+}
+
+const eqsz = {a, b
+ var sz
+
+ sz = max(a.bits.len, b.bits.len)
+ ensurelen(a, sz)
+ ensurelen(b, sz)
+}
+
diff --git a/lib/std/blat.myr b/lib/std/blat.myr
new file mode 100644
index 0000000..90c0f94
--- /dev/null
+++ b/lib/std/blat.myr
@@ -0,0 +1,32 @@
+use "syswrap.use"
+
+pkg std =
+ const blat : (path : byte[:], buf : byte[:], perm : int64 -> bool)
+ const fblat : (f : fd, buf : byte[:] -> bool)
+;;
+
+const blat = {path, buf, perm
+ var fd
+
+ fd = openmode(path, Ocreat|Owronly, perm)
+ if fd < 0
+ -> false
+ ;;
+ -> fblat(fd, buf)
+}
+
+
+const fblat = {fd, buf
+ var written, n
+
+ n = 0
+ while true
+ written = write(fd, buf[n:])
+ if written <= 0
+ goto done
+ ;;
+ n += written
+ ;;
+:done
+ -> written == 0 && n == buf.len
+}
diff --git a/lib/std/bld.sub b/lib/std/bld.sub
new file mode 100644
index 0000000..b84be64
--- /dev/null
+++ b/lib/std/bld.sub
@@ -0,0 +1,103 @@
+lib sys =
+ sys+freebsd-x64.myr
+ sys+linux-x64.myr
+ sys+osx-x64.myr
+ sys+plan9-x64.myr
+ syscall+freebsd-x64.s
+ syscall+linux-x64.s
+ syscall+osx-x64.s
+ syscall+plan9-x64.s
+ syserrno+linux.myr
+ syserrno+osx.myr
+ systypes.myr
+ ifreq+freebsd.myr
+ ifreq+linux.myr
+ ifreq+osx.myr
+ ifreq+plan9.myr
+ ifreq.myr # dummy file: plan9 doesn't have ifreq
+ util+plan9-x64.s
+ util+posixy-x64.s
+;;
+
+lib std {inc=.} =
+ lib sys
+
+ # portable files
+ alloc.myr
+ bigint.myr
+ bitset.myr
+ blat.myr
+ chartype.myr
+ clear.myr
+ cmp.myr
+ cstrconv.myr
+ die.myr
+ dirname.myr
+ endian.myr
+ errno.myr
+ execvp.myr
+ extremum.myr
+ fltbits.myr
+ fltfmt.myr
+ fmt.myr
+ fmtfuncs.myr
+ getcwd.myr
+ getint.myr
+ hashfuncs.myr
+ hasprefix.myr
+ hassuffix.myr
+ htab.myr
+ intparse.myr
+ introspect.myr
+ ipparse.myr
+ mk.myr
+ mkpath.myr
+ now.myr
+ option.myr
+ optparse.myr
+ pathjoin.myr
+ putint.myr
+ rand.myr
+ result.myr
+ search.myr
+ slcp.myr
+ sldup.myr
+ sleq.myr
+ slfill.myr
+ sljoin.myr
+ slpush.myr
+ slput.myr
+ slurp.myr
+ sort.myr
+ spork.myr
+ strbuf.myr
+ strfind.myr
+ strjoin.myr
+ strsplit.myr
+ strstrip.myr
+ swap.myr
+ try.myr
+ types.myr
+ units.myr
+ utf.myr
+ varargs.myr
+
+ # platform specific files
+ env+plan9.myr
+ env+posixy.myr
+ errno+plan9.myr
+ dir+freebsd.myr
+ dir+linux.myr
+ dir+osx.myr
+ wait+posixy.myr
+ wait+plan9.myr
+ dial+posixy.myr
+ dial+plan9.myr
+ resolve+posixy.myr
+ resolve+plan9.myr
+ syswrap-ss+linux.myr
+ syswrap-ss+osx.myr
+ syswrap-ss+plan9.myr
+ syswrap+plan9.myr
+ syswrap+posixy.myr
+;;
diff --git a/lib/std/build.sh b/lib/std/build.sh
new file mode 100755
index 0000000..3c1aa99
--- /dev/null
+++ b/lib/std/build.sh
@@ -0,0 +1,8 @@
+#!/bin/bash
+
+if [ -z "`which mbld`" ]; then
+ ../myrbuild/myrbuild -I. -C$MYR_MC -M$MYR_MUSE $@
+else
+ mbld
+fi
+
diff --git a/lib/std/chartype.myr b/lib/std/chartype.myr
new file mode 100644
index 0000000..fe59202
--- /dev/null
+++ b/lib/std/chartype.myr
@@ -0,0 +1,1242 @@
+use "die.use"
+use "types.use"
+
+/*
+ Tables adapted from plan 9's runetype.c,
+ which lives in sys/src/libc/port/runetype.c
+*/
+
+pkg std =
+ /* predicates */
+ const isalpha : (c : char -> bool)
+ const isdigit : (c : char -> bool)
+ const isxdigit : (c : char -> bool)
+ const isnum : (c : char -> bool)
+ const isalnum : (c : char -> bool)
+ const isspace : (c : char -> bool)
+ const isblank : (c : char -> bool)
+ const islower : (c : char -> bool)
+ const isupper : (c : char -> bool)
+ const istitle : (c : char -> bool)
+
+ /* transforms */
+ const tolower : (c : char -> char)
+ const toupper : (c : char -> char)
+ const totitle : (c : char -> char)
+
+ generic charval : (c : char, base : int -> @a::(integral,numeric))
+;;
+
+extern const put : (fmt : byte[:], args : ... -> size)
+
+/*
+ * alpha ranges -
+ * only covers ranges not in lower||upper
+ */
+const ralpha2 = [
+ 0x00d8, 0x00f6, /* Ø - ö */
+ 0x00f8, 0x01f5, /* ø - ǵ */
+ 0x0250, 0x02a8, /* ɐ - ʨ */
+ 0x038e, 0x03a1, /* Ύ - Ρ */
+ 0x03a3, 0x03ce, /* Σ - ώ */
+ 0x03d0, 0x03d6, /* ϐ - ϖ */
+ 0x03e2, 0x03f3, /* Ϣ - ϳ */
+ 0x0490, 0x04c4, /* Ґ - ӄ */
+ 0x0561, 0x0587, /* ա - և */
+ 0x05d0, 0x05ea, /* א - ת */
+ 0x05f0, 0x05f2, /* װ - ײ */
+ 0x0621, 0x063a, /* ء - غ */
+ 0x0640, 0x064a, /* ـ - ي */
+ 0x0671, 0x06b7, /* ٱ - ڷ */
+ 0x06ba, 0x06be, /* ں - ھ */
+ 0x06c0, 0x06ce, /* ۀ - ێ */
+ 0x06d0, 0x06d3, /* ې - ۓ */
+ 0x0905, 0x0939, /* अ - ह */
+ 0x0958, 0x0961, /* क़ - ॡ */
+ 0x0985, 0x098c, /* অ - ঌ */
+ 0x098f, 0x0990, /* এ - ঐ */
+ 0x0993, 0x09a8, /* ও - ন */
+ 0x09aa, 0x09b0, /* প - র */
+ 0x09b6, 0x09b9, /* শ - হ */
+ 0x09dc, 0x09dd, /* ড় - ঢ় */
+ 0x09df, 0x09e1, /* য় - ৡ */
+ 0x09f0, 0x09f1, /* ৰ - ৱ */
+ 0x0a05, 0x0a0a, /* ਅ - ਊ */
+ 0x0a0f, 0x0a10, /* ਏ - ਐ */
+ 0x0a13, 0x0a28, /* ਓ - ਨ */
+ 0x0a2a, 0x0a30, /* ਪ - ਰ */
+ 0x0a32, 0x0a33, /* ਲ - ਲ਼ */
+ 0x0a35, 0x0a36, /* ਵ - ਸ਼ */
+ 0x0a38, 0x0a39, /* ਸ - ਹ */
+ 0x0a59, 0x0a5c, /* ਖ਼ - ੜ */
+ 0x0a85, 0x0a8b, /* અ - ઋ */
+ 0x0a8f, 0x0a91, /* એ - ઑ */
+ 0x0a93, 0x0aa8, /* ઓ - ન */
+ 0x0aaa, 0x0ab0, /* પ - ર */
+ 0x0ab2, 0x0ab3, /* લ - ળ */
+ 0x0ab5, 0x0ab9, /* વ - હ */
+ 0x0b05, 0x0b0c, /* ଅ - ଌ */
+ 0x0b0f, 0x0b10, /* ଏ - ଐ */
+ 0x0b13, 0x0b28, /* ଓ - ନ */
+ 0x0b2a, 0x0b30, /* ପ - ର */
+ 0x0b32, 0x0b33, /* ଲ - ଳ */
+ 0x0b36, 0x0b39, /* ଶ - ହ */
+ 0x0b5c, 0x0b5d, /* ଡ଼ - ଢ଼ */
+ 0x0b5f, 0x0b61, /* ୟ - ୡ */
+ 0x0b85, 0x0b8a, /* அ - ஊ */
+ 0x0b8e, 0x0b90, /* எ - ஐ */
+ 0x0b92, 0x0b95, /* ஒ - க */
+ 0x0b99, 0x0b9a, /* ங - ச */
+ 0x0b9e, 0x0b9f, /* ஞ - ட */
+ 0x0ba3, 0x0ba4, /* ண - த */
+ 0x0ba8, 0x0baa, /* ந - ப */
+ 0x0bae, 0x0bb5, /* ம - வ */
+ 0x0bb7, 0x0bb9, /* ஷ - ஹ */
+ 0x0c05, 0x0c0c, /* అ - ఌ */
+ 0x0c0e, 0x0c10, /* ఎ - ఐ */
+ 0x0c12, 0x0c28, /* ఒ - న */
+ 0x0c2a, 0x0c33, /* ప - ళ */
+ 0x0c35, 0x0c39, /* వ - హ */
+ 0x0c60, 0x0c61, /* ౠ - ౡ */
+ 0x0c85, 0x0c8c, /* ಅ - ಌ */
+ 0x0c8e, 0x0c90, /* ಎ - ಐ */
+ 0x0c92, 0x0ca8, /* ಒ - ನ */
+ 0x0caa, 0x0cb3, /* ಪ - ಳ */
+ 0x0cb5, 0x0cb9, /* ವ - ಹ */
+ 0x0ce0, 0x0ce1, /* ೠ - ೡ */
+ 0x0d05, 0x0d0c, /* അ - ഌ */
+ 0x0d0e, 0x0d10, /* എ - ഐ */
+ 0x0d12, 0x0d28, /* ഒ - ന */
+ 0x0d2a, 0x0d39, /* പ - ഹ */
+ 0x0d60, 0x0d61, /* ൠ - ൡ */
+ 0x0e01, 0x0e30, /* ก - ะ */
+ 0x0e32, 0x0e33, /* า - ำ */
+ 0x0e40, 0x0e46, /* เ - ๆ */
+ 0x0e5a, 0x0e5b, /* ๚ - ๛ */
+ 0x0e81, 0x0e82, /* ກ - ຂ */
+ 0x0e87, 0x0e88, /* ງ - ຈ */
+ 0x0e94, 0x0e97, /* ດ - ທ */
+ 0x0e99, 0x0e9f, /* ນ - ຟ */
+ 0x0ea1, 0x0ea3, /* ມ - ຣ */
+ 0x0eaa, 0x0eab, /* ສ - ຫ */
+ 0x0ead, 0x0eae, /* ອ - ຮ */
+ 0x0eb2, 0x0eb3, /* າ - ຳ */
+ 0x0ec0, 0x0ec4, /* ເ - ໄ */
+ 0x0edc, 0x0edd, /* ໜ - ໝ */
+ 0x0f18, 0x0f19, /* ༘ - ༙ */
+ 0x0f40, 0x0f47, /* ཀ - ཇ */
+ 0x0f49, 0x0f69, /* ཉ - ཀྵ */
+ 0x10d0, 0x10f6, /* ა - ჶ */
+ 0x1100, 0x1159, /* ᄀ - ᅙ */
+ 0x115f, 0x11a2, /* ᅟ - ᆢ */
+ 0x11a8, 0x11f9, /* ᆨ - ᇹ */
+ 0x1e00, 0x1e9b, /* Ḁ - ẛ */
+ 0x1f50, 0x1f57, /* ὐ - ὗ */
+ 0x1f80, 0x1fb4, /* ᾀ - ᾴ */
+ 0x1fb6, 0x1fbc, /* ᾶ - ᾼ */
+ 0x1fc2, 0x1fc4, /* ῂ - ῄ */
+ 0x1fc6, 0x1fcc, /* ῆ - ῌ */
+ 0x1fd0, 0x1fd3, /* ῐ - ΐ */
+ 0x1fd6, 0x1fdb, /* ῖ - Ί */
+ 0x1fe0, 0x1fec, /* ῠ - Ῥ */
+ 0x1ff2, 0x1ff4, /* ῲ - ῴ */
+ 0x1ff6, 0x1ffc, /* ῶ - ῼ */
+ 0x210a, 0x2113, /* ℊ - ℓ */
+ 0x2115, 0x211d, /* ℕ - ℝ */
+ 0x2120, 0x2122, /* ℠ - ™ */
+ 0x212a, 0x2131, /* K - ℱ */
+ 0x2133, 0x2138, /* ℳ - ℸ */
+ 0x3041, 0x3094, /* ぁ - ゔ */
+ 0x30a1, 0x30fa, /* ァ - ヺ */
+ 0x3105, 0x312c, /* ㄅ - ㄬ */
+ 0x3131, 0x318e, /* ㄱ - ㆎ */
+ 0x3192, 0x319f, /* ㆒ - ㆟ */
+ 0x3260, 0x327b, /* ㉠ - ㉻ */
+ 0x328a, 0x32b0, /* ㊊ - ㊰ */
+ 0x32d0, 0x32fe, /* ㋐ - ㋾ */
+ 0x3300, 0x3357, /* ㌀ - ㍗ */
+ 0x3371, 0x3376, /* ㍱ - ㍶ */
+ 0x337b, 0x3394, /* ㍻ - ㎔ */
+ 0x3399, 0x339e, /* ㎙ - ㎞ */
+ 0x33a9, 0x33ad, /* ㎩ - ㎭ */
+ 0x33b0, 0x33c1, /* ㎰ - ㏁ */
+ 0x33c3, 0x33c5, /* ㏃ - ㏅ */
+ 0x33c7, 0x33d7, /* ㏇ - ㏗ */
+ 0x33d9, 0x33dd, /* ㏙ - ㏝ */
+ 0x4e00, 0x9fff, /* 一 - 鿿 */
+ 0xac00, 0xd7a3, /* 가 - 힣 */
+ 0xf900, 0xfb06, /* 豈 - st */
+ 0xfb13, 0xfb17, /* ﬓ - ﬗ */
+ 0xfb1f, 0xfb28, /* ײַ - ﬨ */
+ 0xfb2a, 0xfb36, /* שׁ - זּ */
+ 0xfb38, 0xfb3c, /* טּ - לּ */
+ 0xfb40, 0xfb41, /* נּ - סּ */
+ 0xfb43, 0xfb44, /* ףּ - פּ */
+ 0xfb46, 0xfbb1, /* צּ - ﮱ */
+ 0xfbd3, 0xfd3d, /* ﯓ - ﴽ */
+ 0xfd50, 0xfd8f, /* ﵐ - ﶏ */
+ 0xfd92, 0xfdc7, /* ﶒ - ﷇ */
+ 0xfdf0, 0xfdf9, /* ﷰ - ﷹ */
+ 0xfe70, 0xfe72, /* ﹰ - ﹲ */
+ 0xfe76, 0xfefc, /* ﹶ - ﻼ */
+ 0xff66, 0xff6f, /* ヲ - ッ */
+ 0xff71, 0xff9d, /* ア - ン */
+ 0xffa0, 0xffbe, /* ᅠ - ᄒ */
+ 0xffc2, 0xffc7, /* ᅡ - ᅦ */
+ 0xffca, 0xffcf, /* ᅧ - ᅬ */
+ 0xffd2, 0xffd7, /* ᅭ - ᅲ */
+ 0xffda, 0xffdc /* ᅳ - ᅵ */
+]
+
+/*
+ * alpha singlets -
+ * only covers ranges not in lower||upper
+ */
+const ralpha1 = [
+ 0x00aa, /* ª */
+ 0x00b5, /* µ */
+ 0x00ba, /* º */
+ 0x03da, /* Ϛ */
+ 0x03dc, /* Ϝ */
+ 0x03de, /* Ϟ */
+ 0x03e0, /* Ϡ */
+ 0x06d5, /* ە */
+ 0x09b2, /* ল */
+ 0x0a5e, /* ਫ਼ */
+ 0x0a8d, /* ઍ */
+ 0x0ae0, /* ૠ */
+ 0x0b9c, /* ஜ */
+ 0x0cde, /* ೞ */
+ 0x0e4f, /* ๏ */
+ 0x0e84, /* ຄ */
+ 0x0e8a, /* ຊ */
+ 0x0e8d, /* ຍ */
+ 0x0ea5, /* ລ */
+ 0x0ea7, /* ວ */
+ 0x0eb0, /* ະ */
+ 0x0ebd, /* ຽ */
+ 0x1fbe, /* ι */
+ 0x207f, /* ⁿ */
+ 0x20a8, /* ₨ */
+ 0x2102, /* ℂ */
+ 0x2107, /* ℇ */
+ 0x2124, /* ℤ */
+ 0x2126, /* Ω */
+ 0x2128, /* ℨ */
+ 0xfb3e, /* מּ */
+ 0xfe74 /* ﹴ */
+]
+
+/*
+ * space ranges
+ */
+const rspace2 = [
+ 0x0009, 0x0009, /* tab */
+ 0x0020, 0x0020, /* space */
+ 0x0085, 0x0085,
+ 0x00a0, 0x00a0, /* */
+ 0x1680, 0x1680,
+ 0x180e, 0x180e,
+ 0x2000, 0x200b, /*   - ​ */
+ 0x2028, 0x2029, /* 
 - 
 */
+ 0x202f, 0x202f,
+ 0x205f, 0x205f,
+ 0x3000, 0x3000, /*   */
+ 0xfeff, 0xfeff /*  */
+]
+
+/*
+ * lower case ranges
+ * 3rd col is conversion excess 500
+ */
+const rtoupper2 = [
+ 0x0061, 0x007a, 468, /* a-z A-Z */
+ 0x00e0, 0x00f6, 468, /* à-ö À-Ö */
+ 0x00f8, 0x00fe, 468, /* ø-þ Ø-Þ */
+ 0x0256, 0x0257, 295, /* ɖ-ɗ Ɖ-Ɗ */
+ 0x0258, 0x0259, 298, /* ɘ-ə Ǝ-Ə */
+ 0x028a, 0x028b, 283, /* ʊ-ʋ Ʊ-Ʋ */
+ 0x03ad, 0x03af, 463, /* έ-ί Έ-Ί */
+ 0x03b1, 0x03c1, 468, /* α-ρ Α-Ρ */
+ 0x03c3, 0x03cb, 468, /* σ-ϋ Σ-Ϋ */
+ 0x03cd, 0x03ce, 437, /* ύ-ώ Ύ-Ώ */
+ 0x0430, 0x044f, 468, /* а-я А-Я */
+ 0x0451, 0x045c, 420, /* ё-ќ Ё-Ќ */
+ 0x045e, 0x045f, 420, /* ў-џ Ў-Џ */
+ 0x0561, 0x0586, 452, /* ա-ֆ Ա-Ֆ */
+ 0x1f00, 0x1f07, 508, /* ἀ-ἇ Ἀ-Ἇ */
+ 0x1f10, 0x1f15, 508, /* ἐ-ἕ Ἐ-Ἕ */
+ 0x1f20, 0x1f27, 508, /* ἠ-ἧ Ἠ-Ἧ */
+ 0x1f30, 0x1f37, 508, /* ἰ-ἷ Ἰ-Ἷ */
+ 0x1f40, 0x1f45, 508, /* ὀ-ὅ Ὀ-Ὅ */
+ 0x1f60, 0x1f67, 508, /* ὠ-ὧ Ὠ-Ὧ */
+ 0x1f70, 0x1f71, 574, /* ὰ-ά Ὰ-Ά */
+ 0x1f72, 0x1f75, 586, /* ὲ-ή Ὲ-Ή */
+ 0x1f76, 0x1f77, 600, /* ὶ-ί Ὶ-Ί */
+ 0x1f78, 0x1f79, 628, /* ὸ-ό Ὸ-Ό */
+ 0x1f7a, 0x1f7b, 612, /* ὺ-ύ Ὺ-Ύ */
+ 0x1f7c, 0x1f7d, 626, /* ὼ-ώ Ὼ-Ώ */
+ 0x1f80, 0x1f87, 508, /* ᾀ-ᾇ ᾈ-ᾏ */
+ 0x1f90, 0x1f97, 508, /* ᾐ-ᾗ ᾘ-ᾟ */
+ 0x1fa0, 0x1fa7, 508, /* ᾠ-ᾧ ᾨ-ᾯ */
+ 0x1fb0, 0x1fb1, 508, /* ᾰ-ᾱ Ᾰ-Ᾱ */
+ 0x1fd0, 0x1fd1, 508, /* ῐ-ῑ Ῐ-Ῑ */
+ 0x1fe0, 0x1fe1, 508, /* ῠ-ῡ Ῠ-Ῡ */
+ 0x2170, 0x217f, 484, /* ⅰ-ⅿ Ⅰ-Ⅿ */
+ 0x24d0, 0x24e9, 474, /* ⓐ-ⓩ Ⓐ-Ⓩ */
+ 0xff41, 0xff5a, 468 /* a-z A-Z */
+]
+
+/*
+ * lower case singlets
+ * 2nd col is conversion excess 500
+ */
+const rtoupper1 = [
+ 0x00ff, 621, /* ÿ Ÿ */
+ 0x0101, 499, /* ā Ā */
+ 0x0103, 499, /* ă Ă */
+ 0x0105, 499, /* ą Ą */
+ 0x0107, 499, /* ć Ć */
+ 0x0109, 499, /* ĉ Ĉ */
+ 0x010b, 499, /* ċ Ċ */
+ 0x010d, 499, /* č Č */
+ 0x010f, 499, /* ď Ď */
+ 0x0111, 499, /* đ Đ */
+ 0x0113, 499, /* ē Ē */
+ 0x0115, 499, /* ĕ Ĕ */
+ 0x0117, 499, /* ė Ė */
+ 0x0119, 499, /* ę Ę */
+ 0x011b, 499, /* ě Ě */
+ 0x011d, 499, /* ĝ Ĝ */
+ 0x011f, 499, /* ğ Ğ */
+ 0x0121, 499, /* ġ Ġ */
+ 0x0123, 499, /* ģ Ģ */
+ 0x0125, 499, /* ĥ Ĥ */
+ 0x0127, 499, /* ħ Ħ */
+ 0x0129, 499, /* ĩ Ĩ */
+ 0x012b, 499, /* ī Ī */
+ 0x012d, 499, /* ĭ Ĭ */
+ 0x012f, 499, /* į Į */
+ 0x0131, 268, /* ı I */
+ 0x0133, 499, /* ij IJ */
+ 0x0135, 499, /* ĵ Ĵ */
+ 0x0137, 499, /* ķ Ķ */
+ 0x013a, 499, /* ĺ Ĺ */
+ 0x013c, 499, /* ļ Ļ */
+ 0x013e, 499, /* ľ Ľ */
+ 0x0140, 499, /* ŀ Ŀ */
+ 0x0142, 499, /* ł Ł */
+ 0x0144, 499, /* ń Ń */
+ 0x0146, 499, /* ņ Ņ */
+ 0x0148, 499, /* ň Ň */
+ 0x014b, 499, /* ŋ Ŋ */
+ 0x014d, 499, /* ō Ō */
+ 0x014f, 499, /* ŏ Ŏ */
+ 0x0151, 499, /* ő Ő */
+ 0x0153, 499, /* œ Œ */
+ 0x0155, 499, /* ŕ Ŕ */
+ 0x0157, 499, /* ŗ Ŗ */
+ 0x0159, 499, /* ř Ř */
+ 0x015b, 499, /* ś Ś */
+ 0x015d, 499, /* ŝ Ŝ */
+ 0x015f, 499, /* ş Ş */
+ 0x0161, 499, /* š Š */
+ 0x0163, 499, /* ţ Ţ */
+ 0x0165, 499, /* ť Ť */
+ 0x0167, 499, /* ŧ Ŧ */
+ 0x0169, 499, /* ũ Ũ */
+ 0x016b, 499, /* ū Ū */
+ 0x016d, 499, /* ŭ Ŭ */
+ 0x016f, 499, /* ů Ů */
+ 0x0171, 499, /* ű Ű */
+ 0x0173, 499, /* ų Ų */
+ 0x0175, 499, /* ŵ Ŵ */
+ 0x0177, 499, /* ŷ Ŷ */
+ 0x017a, 499, /* ź Ź */
+ 0x017c, 499, /* ż Ż */
+ 0x017e, 499, /* ž Ž */
+ 0x017f, 200, /* ſ S */
+ 0x0183, 499, /* ƃ Ƃ */
+ 0x0185, 499, /* ƅ Ƅ */
+ 0x0188, 499, /* ƈ Ƈ */
+ 0x018c, 499, /* ƌ Ƌ */
+ 0x0192, 499, /* ƒ Ƒ */
+ 0x0199, 499, /* ƙ Ƙ */
+ 0x01a1, 499, /* ơ Ơ */
+ 0x01a3, 499, /* ƣ Ƣ */
+ 0x01a5, 499, /* ƥ Ƥ */
+ 0x01a8, 499, /* ƨ Ƨ */
+ 0x01ad, 499, /* ƭ Ƭ */
+ 0x01b0, 499, /* ư Ư */
+ 0x01b4, 499, /* ƴ Ƴ */
+ 0x01b6, 499, /* ƶ Ƶ */
+ 0x01b9, 499, /* ƹ Ƹ */
+ 0x01bd, 499, /* ƽ Ƽ */
+ 0x01c5, 499, /* Dž DŽ */
+ 0x01c6, 498, /* dž DŽ */
+ 0x01c8, 499, /* Lj LJ */
+ 0x01c9, 498, /* lj LJ */
+ 0x01cb, 499, /* Nj NJ */
+ 0x01cc, 498, /* nj NJ */
+ 0x01ce, 499, /* ǎ Ǎ */
+ 0x01d0, 499, /* ǐ Ǐ */
+ 0x01d2, 499, /* ǒ Ǒ */
+ 0x01d4, 499, /* ǔ Ǔ */
+ 0x01d6, 499, /* ǖ Ǖ */
+ 0x01d8, 499, /* ǘ Ǘ */
+ 0x01da, 499, /* ǚ Ǚ */
+ 0x01dc, 499, /* ǜ Ǜ */
+ 0x01df, 499, /* ǟ Ǟ */
+ 0x01e1, 499, /* ǡ Ǡ */
+ 0x01e3, 499, /* ǣ Ǣ */
+ 0x01e5, 499, /* ǥ Ǥ */
+ 0x01e7, 499, /* ǧ Ǧ */
+ 0x01e9, 499, /* ǩ Ǩ */
+ 0x01eb, 499, /* ǫ Ǫ */
+ 0x01ed, 499, /* ǭ Ǭ */
+ 0x01ef, 499, /* ǯ Ǯ */
+ 0x01f2, 499, /* Dz DZ */
+ 0x01f3, 498, /* dz DZ */
+ 0x01f5, 499, /* ǵ Ǵ */
+ 0x01fb, 499, /* ǻ Ǻ */
+ 0x01fd, 499, /* ǽ Ǽ */
+ 0x01ff, 499, /* ǿ Ǿ */
+ 0x0201, 499, /* ȁ Ȁ */
+ 0x0203, 499, /* ȃ Ȃ */
+ 0x0205, 499, /* ȅ Ȅ */
+ 0x0207, 499, /* ȇ Ȇ */
+ 0x0209, 499, /* ȉ Ȉ */
+ 0x020b, 499, /* ȋ Ȋ */
+ 0x020d, 499, /* ȍ Ȍ */
+ 0x020f, 499, /* ȏ Ȏ */
+ 0x0211, 499, /* ȑ Ȑ */
+ 0x0213, 499, /* ȓ Ȓ */
+ 0x0215, 499, /* ȕ Ȕ */
+ 0x0217, 499, /* ȗ Ȗ */
+ 0x0253, 290, /* ɓ Ɓ */
+ 0x0254, 294, /* ɔ Ɔ */
+ 0x025b, 297, /* ɛ Ɛ */
+ 0x0260, 295, /* ɠ Ɠ */
+ 0x0263, 293, /* ɣ Ɣ */
+ 0x0268, 291, /* ɨ Ɨ */
+ 0x0269, 289, /* ɩ Ɩ */
+ 0x026f, 289, /* ɯ Ɯ */
+ 0x0272, 287, /* ɲ Ɲ */
+ 0x0283, 282, /* ʃ Ʃ */
+ 0x0288, 282, /* ʈ Ʈ */
+ 0x0292, 281, /* ʒ Ʒ */
+ 0x03ac, 462, /* ά Ά */
+ 0x03cc, 436, /* ό Ό */
+ 0x03d0, 438, /* ϐ Β */
+ 0x03d1, 443, /* ϑ Θ */
+ 0x03d5, 453, /* ϕ Φ */
+ 0x03d6, 446, /* ϖ Π */
+ 0x03e3, 499, /* ϣ Ϣ */
+ 0x03e5, 499, /* ϥ Ϥ */
+ 0x03e7, 499, /* ϧ Ϧ */
+ 0x03e9, 499, /* ϩ Ϩ */
+ 0x03eb, 499, /* ϫ Ϫ */
+ 0x03ed, 499, /* ϭ Ϭ */
+ 0x03ef, 499, /* ϯ Ϯ */
+ 0x03f0, 414, /* ϰ Κ */
+ 0x03f1, 420, /* ϱ Ρ */
+ 0x0461, 499, /* ѡ Ѡ */
+ 0x0463, 499, /* ѣ Ѣ */
+ 0x0465, 499, /* ѥ Ѥ */
+ 0x0467, 499, /* ѧ Ѧ */
+ 0x0469, 499, /* ѩ Ѩ */
+ 0x046b, 499, /* ѫ Ѫ */
+ 0x046d, 499, /* ѭ Ѭ */
+ 0x046f, 499, /* ѯ Ѯ */
+ 0x0471, 499, /* ѱ Ѱ */
+ 0x0473, 499, /* ѳ Ѳ */
+ 0x0475, 499, /* ѵ Ѵ */
+ 0x0477, 499, /* ѷ Ѷ */
+ 0x0479, 499, /* ѹ Ѹ */
+ 0x047b, 499, /* ѻ Ѻ */
+ 0x047d, 499, /* ѽ Ѽ */
+ 0x047f, 499, /* ѿ Ѿ */
+ 0x0481, 499, /* ҁ Ҁ */
+ 0x0491, 499, /* ґ Ґ */
+ 0x0493, 499, /* ғ Ғ */
+ 0x0495, 499, /* ҕ Ҕ */
+ 0x0497, 499, /* җ Җ */
+ 0x0499, 499, /* ҙ Ҙ */
+ 0x049b, 499, /* қ Қ */
+ 0x049d, 499, /* ҝ Ҝ */
+ 0x049f, 499, /* ҟ Ҟ */
+ 0x04a1, 499, /* ҡ Ҡ */
+ 0x04a3, 499, /* ң Ң */
+ 0x04a5, 499, /* ҥ Ҥ */
+ 0x04a7, 499, /* ҧ Ҧ */
+ 0x04a9, 499, /* ҩ Ҩ */
+ 0x04ab, 499, /* ҫ Ҫ */
+ 0x04ad, 499, /* ҭ Ҭ */
+ 0x04af, 499, /* ү Ү */
+ 0x04b1, 499, /* ұ Ұ */
+ 0x04b3, 499, /* ҳ Ҳ */
+ 0x04b5, 499, /* ҵ Ҵ */
+ 0x04b7, 499, /* ҷ Ҷ */
+ 0x04b9, 499, /* ҹ Ҹ */
+ 0x04bb, 499, /* һ Һ */
+ 0x04bd, 499, /* ҽ Ҽ */
+ 0x04bf, 499, /* ҿ Ҿ */
+ 0x04c2, 499, /* ӂ Ӂ */
+ 0x04c4, 499, /* ӄ Ӄ */
+ 0x04c8, 499, /* ӈ Ӈ */
+ 0x04cc, 499, /* ӌ Ӌ */
+ 0x04d1, 499, /* ӑ Ӑ */
+ 0x04d3, 499, /* ӓ Ӓ */
+ 0x04d5, 499, /* ӕ Ӕ */
+ 0x04d7, 499, /* ӗ Ӗ */
+ 0x04d9, 499, /* ә Ә */
+ 0x04db, 499, /* ӛ Ӛ */
+ 0x04dd, 499, /* ӝ Ӝ */
+ 0x04df, 499, /* ӟ Ӟ */
+ 0x04e1, 499, /* ӡ Ӡ */
+ 0x04e3, 499, /* ӣ Ӣ */
+ 0x04e5, 499, /* ӥ Ӥ */
+ 0x04e7, 499, /* ӧ Ӧ */
+ 0x04e9, 499, /* ө Ө */
+ 0x04eb, 499, /* ӫ Ӫ */
+ 0x04ef, 499, /* ӯ Ӯ */
+ 0x04f1, 499, /* ӱ Ӱ */
+ 0x04f3, 499, /* ӳ Ӳ */
+ 0x04f5, 499, /* ӵ Ӵ */
+ 0x04f9, 499, /* ӹ Ӹ */
+ 0x1e01, 499, /* ḁ Ḁ */
+ 0x1e03, 499, /* ḃ Ḃ */
+ 0x1e05, 499, /* ḅ Ḅ */
+ 0x1e07, 499, /* ḇ Ḇ */
+ 0x1e09, 499, /* ḉ Ḉ */
+ 0x1e0b, 499, /* ḋ Ḋ */
+ 0x1e0d, 499, /* ḍ Ḍ */
+ 0x1e0f, 499, /* ḏ Ḏ */
+ 0x1e11, 499, /* ḑ Ḑ */
+ 0x1e13, 499, /* ḓ Ḓ */
+ 0x1e15, 499, /* ḕ Ḕ */
+ 0x1e17, 499, /* ḗ Ḗ */
+ 0x1e19, 499, /* ḙ Ḙ */
+ 0x1e1b, 499, /* ḛ Ḛ */
+ 0x1e1d, 499, /* ḝ Ḝ */
+ 0x1e1f, 499, /* ḟ Ḟ */
+ 0x1e21, 499, /* ḡ Ḡ */
+ 0x1e23, 499, /* ḣ Ḣ */
+ 0x1e25, 499, /* ḥ Ḥ */
+ 0x1e27, 499, /* ḧ Ḧ */
+ 0x1e29, 499, /* ḩ Ḩ */
+ 0x1e2b, 499, /* ḫ Ḫ */
+ 0x1e2d, 499, /* ḭ Ḭ */
+ 0x1e2f, 499, /* ḯ Ḯ */
+ 0x1e31, 499, /* ḱ Ḱ */
+ 0x1e33, 499, /* ḳ Ḳ */
+ 0x1e35, 499, /* ḵ Ḵ */
+ 0x1e37, 499, /* ḷ Ḷ */
+ 0x1e39, 499, /* ḹ Ḹ */
+ 0x1e3b, 499, /* ḻ Ḻ */
+ 0x1e3d, 499, /* ḽ Ḽ */
+ 0x1e3f, 499, /* ḿ Ḿ */
+ 0x1e41, 499, /* ṁ Ṁ */
+ 0x1e43, 499, /* ṃ Ṃ */
+ 0x1e45, 499, /* ṅ Ṅ */
+ 0x1e47, 499, /* ṇ Ṇ */
+ 0x1e49, 499, /* ṉ Ṉ */
+ 0x1e4b, 499, /* ṋ Ṋ */
+ 0x1e4d, 499, /* ṍ Ṍ */
+ 0x1e4f, 499, /* ṏ Ṏ */
+ 0x1e51, 499, /* ṑ Ṑ */
+ 0x1e53, 499, /* ṓ Ṓ */
+ 0x1e55, 499, /* ṕ Ṕ */
+ 0x1e57, 499, /* ṗ Ṗ */
+ 0x1e59, 499, /* ṙ Ṙ */
+ 0x1e5b, 499, /* ṛ Ṛ */
+ 0x1e5d, 499, /* ṝ Ṝ */
+ 0x1e5f, 499, /* ṟ Ṟ */
+ 0x1e61, 499, /* ṡ Ṡ */
+ 0x1e63, 499, /* ṣ Ṣ */
+ 0x1e65, 499, /* ṥ Ṥ */
+ 0x1e67, 499, /* ṧ Ṧ */
+ 0x1e69, 499, /* ṩ Ṩ */
+ 0x1e6b, 499, /* ṫ Ṫ */
+ 0x1e6d, 499, /* ṭ Ṭ */
+ 0x1e6f, 499, /* ṯ Ṯ */
+ 0x1e71, 499, /* ṱ Ṱ */
+ 0x1e73, 499, /* ṳ Ṳ */
+ 0x1e75, 499, /* ṵ Ṵ */
+ 0x1e77, 499, /* ṷ Ṷ */
+ 0x1e79, 499, /* ṹ Ṹ */
+ 0x1e7b, 499, /* ṻ Ṻ */
+ 0x1e7d, 499, /* ṽ Ṽ */
+ 0x1e7f, 499, /* ṿ Ṿ */
+ 0x1e81, 499, /* ẁ Ẁ */
+ 0x1e83, 499, /* ẃ Ẃ */
+ 0x1e85, 499, /* ẅ Ẅ */
+ 0x1e87, 499, /* ẇ Ẇ */
+ 0x1e89, 499, /* ẉ Ẉ */
+ 0x1e8b, 499, /* ẋ Ẋ */
+ 0x1e8d, 499, /* ẍ Ẍ */
+ 0x1e8f, 499, /* ẏ Ẏ */
+ 0x1e91, 499, /* ẑ Ẑ */
+ 0x1e93, 499, /* ẓ Ẓ */
+ 0x1e95, 499, /* ẕ Ẕ */
+ 0x1ea1, 499, /* ạ Ạ */
+ 0x1ea3, 499, /* ả Ả */
+ 0x1ea5, 499, /* ấ Ấ */
+ 0x1ea7, 499, /* ầ Ầ */
+ 0x1ea9, 499, /* ẩ Ẩ */
+ 0x1eab, 499, /* ẫ Ẫ */
+ 0x1ead, 499, /* ậ Ậ */
+ 0x1eaf, 499, /* ắ Ắ */
+ 0x1eb1, 499, /* ằ Ằ */
+ 0x1eb3, 499, /* ẳ Ẳ */
+ 0x1eb5, 499, /* ẵ Ẵ */
+ 0x1eb7, 499, /* ặ Ặ */
+ 0x1eb9, 499, /* ẹ Ẹ */
+ 0x1ebb, 499, /* ẻ Ẻ */
+ 0x1ebd, 499, /* ẽ Ẽ */
+ 0x1ebf, 499, /* ế Ế */
+ 0x1ec1, 499, /* ề Ề */
+ 0x1ec3, 499, /* ể Ể */
+ 0x1ec5, 499, /* ễ Ễ */
+ 0x1ec7, 499, /* ệ Ệ */
+ 0x1ec9, 499, /* ỉ Ỉ */
+ 0x1ecb, 499, /* ị Ị */
+ 0x1ecd, 499, /* ọ Ọ */
+ 0x1ecf, 499, /* ỏ Ỏ */
+ 0x1ed1, 499, /* ố Ố */
+ 0x1ed3, 499, /* ồ Ồ */
+ 0x1ed5, 499, /* ổ Ổ */
+ 0x1ed7, 499, /* ỗ Ỗ */
+ 0x1ed9, 499, /* ộ Ộ */
+ 0x1edb, 499, /* ớ Ớ */
+ 0x1edd, 499, /* ờ Ờ */
+ 0x1edf, 499, /* ở Ở */
+ 0x1ee1, 499, /* ỡ Ỡ */
+ 0x1ee3, 499, /* ợ Ợ */
+ 0x1ee5, 499, /* ụ Ụ */
+ 0x1ee7, 499, /* ủ Ủ */
+ 0x1ee9, 499, /* ứ Ứ */
+ 0x1eeb, 499, /* ừ Ừ */
+ 0x1eed, 499, /* ử Ử */
+ 0x1eef, 499, /* ữ Ữ */
+ 0x1ef1, 499, /* ự Ự */
+ 0x1ef3, 499, /* ỳ Ỳ */
+ 0x1ef5, 499, /* ỵ Ỵ */
+ 0x1ef7, 499, /* ỷ Ỷ */
+ 0x1ef9, 499, /* ỹ Ỹ */
+ 0x1f51, 508, /* ὑ Ὑ */
+ 0x1f53, 508, /* ὓ Ὓ */
+ 0x1f55, 508, /* ὕ Ὕ */
+ 0x1f57, 508, /* ὗ Ὗ */
+ 0x1fb3, 509, /* ᾳ ᾼ */
+ 0x1fc3, 509, /* ῃ ῌ */
+ 0x1fe5, 507, /* ῥ Ῥ */
+ 0x1ff3, 509 /* ῳ ῼ */
+]
+
+const rnums = [
+ 0x0030, 0x0039,
+ 0x0660, 0x0669,
+ 0x06f0, 0x06f9,
+ 0x07c0, 0x07c9,
+ 0x0966, 0x096f,
+ 0x09e6, 0x09ef,
+ 0x0a66, 0x0a6f,
+ 0x0ae6, 0x0aef,
+ 0x0b66, 0x0b6f,
+ 0x0be6, 0x0bef,
+ 0x0c66, 0x0c6f,
+ 0x0ce6, 0x0cef,
+ 0x0d66, 0x0d6f,
+ 0x0e50, 0x0e59,
+ 0x0ed0, 0x0ed9,
+ 0x0f20, 0x0f29,
+ 0x1040, 0x1049,
+ 0x17e0, 0x17e9,
+ 0x1810, 0x1819,
+ 0x1946, 0x194f,
+ 0x19d0, 0x19d9,
+ 0x1b50, 0x1b59,
+ 0xff10, 0xff19,
+ 0x104a0, 0x104a9,
+ 0x1d7ce, 0x1d7ff
+]
+
+/*
+ * upper case ranges
+ * 3rd col is conversion excess 500
+ */
+const rtolower2 = [
+ 0x0041, 0x005a, 532, /* A-Z a-z */
+ 0x00c0, 0x00d6, 532, /* À-Ö à-ö */
+ 0x00d8, 0x00de, 532, /* Ø-Þ ø-þ */
+ 0x0189, 0x018a, 705, /* Ɖ-Ɗ ɖ-ɗ */
+ 0x018e, 0x018f, 702, /* Ǝ-Ə ɘ-ə */
+ 0x01b1, 0x01b2, 717, /* Ʊ-Ʋ ʊ-ʋ */
+ 0x0388, 0x038a, 537, /* Έ-Ί έ-ί */
+ 0x038e, 0x038f, 563, /* Ύ-Ώ ύ-ώ */
+ 0x0391, 0x03a1, 532, /* Α-Ρ α-ρ */
+ 0x03a3, 0x03ab, 532, /* Σ-Ϋ σ-ϋ */
+ 0x0401, 0x040c, 580, /* Ё-Ќ ё-ќ */
+ 0x040e, 0x040f, 580, /* Ў-Џ ў-џ */
+ 0x0410, 0x042f, 532, /* А-Я а-я */
+ 0x0531, 0x0556, 548, /* Ա-Ֆ ա-ֆ */
+ 0x10a0, 0x10c5, 548, /* Ⴀ-Ⴥ ა-ჵ */
+ 0x1f08, 0x1f0f, 492, /* Ἀ-Ἇ ἀ-ἇ */
+ 0x1f18, 0x1f1d, 492, /* Ἐ-Ἕ ἐ-ἕ */
+ 0x1f28, 0x1f2f, 492, /* Ἠ-Ἧ ἠ-ἧ */
+ 0x1f38, 0x1f3f, 492, /* Ἰ-Ἷ ἰ-ἷ */
+ 0x1f48, 0x1f4d, 492, /* Ὀ-Ὅ ὀ-ὅ */
+ 0x1f68, 0x1f6f, 492, /* Ὠ-Ὧ ὠ-ὧ */
+ 0x1f88, 0x1f8f, 492, /* ᾈ-ᾏ ᾀ-ᾇ */
+ 0x1f98, 0x1f9f, 492, /* ᾘ-ᾟ ᾐ-ᾗ */
+ 0x1fa8, 0x1faf, 492, /* ᾨ-ᾯ ᾠ-ᾧ */
+ 0x1fb8, 0x1fb9, 492, /* Ᾰ-Ᾱ ᾰ-ᾱ */
+ 0x1fba, 0x1fbb, 426, /* Ὰ-Ά ὰ-ά */
+ 0x1fc8, 0x1fcb, 414, /* Ὲ-Ή ὲ-ή */
+ 0x1fd8, 0x1fd9, 492, /* Ῐ-Ῑ ῐ-ῑ */
+ 0x1fda, 0x1fdb, 400, /* Ὶ-Ί ὶ-ί */
+ 0x1fe8, 0x1fe9, 492, /* Ῠ-Ῡ ῠ-ῡ */
+ 0x1fea, 0x1feb, 388, /* Ὺ-Ύ ὺ-ύ */
+ 0x1ff8, 0x1ff9, 372, /* Ὸ-Ό ὸ-ό */
+ 0x1ffa, 0x1ffb, 374, /* Ὼ-Ώ ὼ-ώ */
+ 0x2160, 0x216f, 516, /* Ⅰ-Ⅿ ⅰ-ⅿ */
+ 0x24b6, 0x24cf, 526, /* Ⓐ-Ⓩ ⓐ-ⓩ */
+ 0xff21, 0xff3a, 532 /* A-Z a-z */
+]
+
+/*
+ * upper case singlets
+ * 2nd col is conversion excess 500
+ */
+const rtolower1 = [
+ 0x0100, 501, /* Ā ā */
+ 0x0102, 501, /* Ă ă */
+ 0x0104, 501, /* Ą ą */
+ 0x0106, 501, /* Ć ć */
+ 0x0108, 501, /* Ĉ ĉ */
+ 0x010a, 501, /* Ċ ċ */
+ 0x010c, 501, /* Č č */
+ 0x010e, 501, /* Ď ď */
+ 0x0110, 501, /* Đ đ */
+ 0x0112, 501, /* Ē ē */
+ 0x0114, 501, /* Ĕ ĕ */
+ 0x0116, 501, /* Ė ė */
+ 0x0118, 501, /* Ę ę */
+ 0x011a, 501, /* Ě ě */
+ 0x011c, 501, /* Ĝ ĝ */
+ 0x011e, 501, /* Ğ ğ */
+ 0x0120, 501, /* Ġ ġ */
+ 0x0122, 501, /* Ģ ģ */
+ 0x0124, 501, /* Ĥ ĥ */
+ 0x0126, 501, /* Ħ ħ */
+ 0x0128, 501, /* Ĩ ĩ */
+ 0x012a, 501, /* Ī ī */
+ 0x012c, 501, /* Ĭ ĭ */
+ 0x012e, 501, /* Į į */
+ 0x0130, 301, /* İ i */
+ 0x0132, 501, /* IJ ij */
+ 0x0134, 501, /* Ĵ ĵ */
+ 0x0136, 501, /* Ķ ķ */
+ 0x0139, 501, /* Ĺ ĺ */
+ 0x013b, 501, /* Ļ ļ */
+ 0x013d, 501, /* Ľ ľ */
+ 0x013f, 501, /* Ŀ ŀ */
+ 0x0141, 501, /* Ł ł */
+ 0x0143, 501, /* Ń ń */
+ 0x0145, 501, /* Ņ ņ */
+ 0x0147, 501, /* Ň ň */
+ 0x014a, 501, /* Ŋ ŋ */
+ 0x014c, 501, /* Ō ō */
+ 0x014e, 501, /* Ŏ ŏ */
+ 0x0150, 501, /* Ő ő */
+ 0x0152, 501, /* Œ œ */
+ 0x0154, 501, /* Ŕ ŕ */
+ 0x0156, 501, /* Ŗ ŗ */
+ 0x0158, 501, /* Ř ř */
+ 0x015a, 501, /* Ś ś */
+ 0x015c, 501, /* Ŝ ŝ */
+ 0x015e, 501, /* Ş ş */
+ 0x0160, 501, /* Š š */
+ 0x0162, 501, /* Ţ ţ */
+ 0x0164, 501, /* Ť ť */
+ 0x0166, 501, /* Ŧ ŧ */
+ 0x0168, 501, /* Ũ ũ */
+ 0x016a, 501, /* Ū ū */
+ 0x016c, 501, /* Ŭ ŭ */
+ 0x016e, 501, /* Ů ů */
+ 0x0170, 501, /* Ű ű */
+ 0x0172, 501, /* Ų ų */
+ 0x0174, 501, /* Ŵ ŵ */
+ 0x0176, 501, /* Ŷ ŷ */
+ 0x0178, 379, /* Ÿ ÿ */
+ 0x0179, 501, /* Ź ź */
+ 0x017b, 501, /* Ż ż */
+ 0x017d, 501, /* Ž ž */
+ 0x0181, 710, /* Ɓ ɓ */
+ 0x0182, 501, /* Ƃ ƃ */
+ 0x0184, 501, /* Ƅ ƅ */
+ 0x0186, 706, /* Ɔ ɔ */
+ 0x0187, 501, /* Ƈ ƈ */
+ 0x018b, 501, /* Ƌ ƌ */
+ 0x0190, 703, /* Ɛ ɛ */
+ 0x0191, 501, /* Ƒ ƒ */
+ 0x0193, 705, /* Ɠ ɠ */
+ 0x0194, 707, /* Ɣ ɣ */
+ 0x0196, 711, /* Ɩ ɩ */
+ 0x0197, 709, /* Ɨ ɨ */
+ 0x0198, 501, /* Ƙ ƙ */
+ 0x019c, 711, /* Ɯ ɯ */
+ 0x019d, 713, /* Ɲ ɲ */
+ 0x01a0, 501, /* Ơ ơ */
+ 0x01a2, 501, /* Ƣ ƣ */
+ 0x01a4, 501, /* Ƥ ƥ */
+ 0x01a7, 501, /* Ƨ ƨ */
+ 0x01a9, 718, /* Ʃ ʃ */
+ 0x01ac, 501, /* Ƭ ƭ */
+ 0x01ae, 718, /* Ʈ ʈ */
+ 0x01af, 501, /* Ư ư */
+ 0x01b3, 501, /* Ƴ ƴ */
+ 0x01b5, 501, /* Ƶ ƶ */
+ 0x01b7, 719, /* Ʒ ʒ */
+ 0x01b8, 501, /* Ƹ ƹ */
+ 0x01bc, 501, /* Ƽ ƽ */
+ 0x01c4, 502, /* DŽ dž */
+ 0x01c5, 501, /* Dž dž */
+ 0x01c7, 502, /* LJ lj */
+ 0x01c8, 501, /* Lj lj */
+ 0x01ca, 502, /* NJ nj */
+ 0x01cb, 501, /* Nj nj */
+ 0x01cd, 501, /* Ǎ ǎ */
+ 0x01cf, 501, /* Ǐ ǐ */
+ 0x01d1, 501, /* Ǒ ǒ */
+ 0x01d3, 501, /* Ǔ ǔ */
+ 0x01d5, 501, /* Ǖ ǖ */
+ 0x01d7, 501, /* Ǘ ǘ */
+ 0x01d9, 501, /* Ǚ ǚ */
+ 0x01db, 501, /* Ǜ ǜ */
+ 0x01de, 501, /* Ǟ ǟ */
+ 0x01e0, 501, /* Ǡ ǡ */
+ 0x01e2, 501, /* Ǣ ǣ */
+ 0x01e4, 501, /* Ǥ ǥ */
+ 0x01e6, 501, /* Ǧ ǧ */
+ 0x01e8, 501, /* Ǩ ǩ */
+ 0x01ea, 501, /* Ǫ ǫ */
+ 0x01ec, 501, /* Ǭ ǭ */
+ 0x01ee, 501, /* Ǯ ǯ */
+ 0x01f1, 502, /* DZ dz */
+ 0x01f2, 501, /* Dz dz */
+ 0x01f4, 501, /* Ǵ ǵ */
+ 0x01fa, 501, /* Ǻ ǻ */
+ 0x01fc, 501, /* Ǽ ǽ */
+ 0x01fe, 501, /* Ǿ ǿ */
+ 0x0200, 501, /* Ȁ ȁ */
+ 0x0202, 501, /* Ȃ ȃ */
+ 0x0204, 501, /* Ȅ ȅ */
+ 0x0206, 501, /* Ȇ ȇ */
+ 0x0208, 501, /* Ȉ ȉ */
+ 0x020a, 501, /* Ȋ ȋ */
+ 0x020c, 501, /* Ȍ ȍ */
+ 0x020e, 501, /* Ȏ ȏ */
+ 0x0210, 501, /* Ȑ ȑ */
+ 0x0212, 501, /* Ȓ ȓ */
+ 0x0214, 501, /* Ȕ ȕ */
+ 0x0216, 501, /* Ȗ ȗ */
+ 0x0386, 538, /* Ά ά */
+ 0x038c, 564, /* Ό ό */
+ 0x03e2, 501, /* Ϣ ϣ */
+ 0x03e4, 501, /* Ϥ ϥ */
+ 0x03e6, 501, /* Ϧ ϧ */
+ 0x03e8, 501, /* Ϩ ϩ */
+ 0x03ea, 501, /* Ϫ ϫ */
+ 0x03ec, 501, /* Ϭ ϭ */
+ 0x03ee, 501, /* Ϯ ϯ */
+ 0x0460, 501, /* Ѡ ѡ */
+ 0x0462, 501, /* Ѣ ѣ */
+ 0x0464, 501, /* Ѥ ѥ */
+ 0x0466, 501, /* Ѧ ѧ */
+ 0x0468, 501, /* Ѩ ѩ */
+ 0x046a, 501, /* Ѫ ѫ */
+ 0x046c, 501, /* Ѭ ѭ */
+ 0x046e, 501, /* Ѯ ѯ */
+ 0x0470, 501, /* Ѱ ѱ */
+ 0x0472, 501, /* Ѳ ѳ */
+ 0x0474, 501, /* Ѵ ѵ */
+ 0x0476, 501, /* Ѷ ѷ */
+ 0x0478, 501, /* Ѹ ѹ */
+ 0x047a, 501, /* Ѻ ѻ */
+ 0x047c, 501, /* Ѽ ѽ */
+ 0x047e, 501, /* Ѿ ѿ */
+ 0x0480, 501, /* Ҁ ҁ */
+ 0x0490, 501, /* Ґ ґ */
+ 0x0492, 501, /* Ғ ғ */
+ 0x0494, 501, /* Ҕ ҕ */
+ 0x0496, 501, /* Җ җ */
+ 0x0498, 501, /* Ҙ ҙ */
+ 0x049a, 501, /* Қ қ */
+ 0x049c, 501, /* Ҝ ҝ */
+ 0x049e, 501, /* Ҟ ҟ */
+ 0x04a0, 501, /* Ҡ ҡ */
+ 0x04a2, 501, /* Ң ң */
+ 0x04a4, 501, /* Ҥ ҥ */
+ 0x04a6, 501, /* Ҧ ҧ */
+ 0x04a8, 501, /* Ҩ ҩ */
+ 0x04aa, 501, /* Ҫ ҫ */
+ 0x04ac, 501, /* Ҭ ҭ */
+ 0x04ae, 501, /* Ү ү */
+ 0x04b0, 501, /* Ұ ұ */
+ 0x04b2, 501, /* Ҳ ҳ */
+ 0x04b4, 501, /* Ҵ ҵ */
+ 0x04b6, 501, /* Ҷ ҷ */
+ 0x04b8, 501, /* Ҹ ҹ */
+ 0x04ba, 501, /* Һ һ */
+ 0x04bc, 501, /* Ҽ ҽ */
+ 0x04be, 501, /* Ҿ ҿ */
+ 0x04c1, 501, /* Ӂ ӂ */
+ 0x04c3, 501, /* Ӄ ӄ */
+ 0x04c7, 501, /* Ӈ ӈ */
+ 0x04cb, 501, /* Ӌ ӌ */
+ 0x04d0, 501, /* Ӑ ӑ */
+ 0x04d2, 501, /* Ӓ ӓ */
+ 0x04d4, 501, /* Ӕ ӕ */
+ 0x04d6, 501, /* Ӗ ӗ */
+ 0x04d8, 501, /* Ә ә */
+ 0x04da, 501, /* Ӛ ӛ */
+ 0x04dc, 501, /* Ӝ ӝ */
+ 0x04de, 501, /* Ӟ ӟ */
+ 0x04e0, 501, /* Ӡ ӡ */
+ 0x04e2, 501, /* Ӣ ӣ */
+ 0x04e4, 501, /* Ӥ ӥ */
+ 0x04e6, 501, /* Ӧ ӧ */
+ 0x04e8, 501, /* Ө ө */
+ 0x04ea, 501, /* Ӫ ӫ */
+ 0x04ee, 501, /* Ӯ ӯ */
+ 0x04f0, 501, /* Ӱ ӱ */
+ 0x04f2, 501, /* Ӳ ӳ */
+ 0x04f4, 501, /* Ӵ ӵ */
+ 0x04f8, 501, /* Ӹ ӹ */
+ 0x1e00, 501, /* Ḁ ḁ */
+ 0x1e02, 501, /* Ḃ ḃ */
+ 0x1e04, 501, /* Ḅ ḅ */
+ 0x1e06, 501, /* Ḇ ḇ */
+ 0x1e08, 501, /* Ḉ ḉ */
+ 0x1e0a, 501, /* Ḋ ḋ */
+ 0x1e0c, 501, /* Ḍ ḍ */
+ 0x1e0e, 501, /* Ḏ ḏ */
+ 0x1e10, 501, /* Ḑ ḑ */
+ 0x1e12, 501, /* Ḓ ḓ */
+ 0x1e14, 501, /* Ḕ ḕ */
+ 0x1e16, 501, /* Ḗ ḗ */
+ 0x1e18, 501, /* Ḙ ḙ */
+ 0x1e1a, 501, /* Ḛ ḛ */
+ 0x1e1c, 501, /* Ḝ ḝ */
+ 0x1e1e, 501, /* Ḟ ḟ */
+ 0x1e20, 501, /* Ḡ ḡ */
+ 0x1e22, 501, /* Ḣ ḣ */
+ 0x1e24, 501, /* Ḥ ḥ */
+ 0x1e26, 501, /* Ḧ ḧ */
+ 0x1e28, 501, /* Ḩ ḩ */
+ 0x1e2a, 501, /* Ḫ ḫ */
+ 0x1e2c, 501, /* Ḭ ḭ */
+ 0x1e2e, 501, /* Ḯ ḯ */
+ 0x1e30, 501, /* Ḱ ḱ */
+ 0x1e32, 501, /* Ḳ ḳ */
+ 0x1e34, 501, /* Ḵ ḵ */
+ 0x1e36, 501, /* Ḷ ḷ */
+ 0x1e38, 501, /* Ḹ ḹ */
+ 0x1e3a, 501, /* Ḻ ḻ */
+ 0x1e3c, 501, /* Ḽ ḽ */
+ 0x1e3e, 501, /* Ḿ ḿ */
+ 0x1e40, 501, /* Ṁ ṁ */
+ 0x1e42, 501, /* Ṃ ṃ */
+ 0x1e44, 501, /* Ṅ ṅ */
+ 0x1e46, 501, /* Ṇ ṇ */
+ 0x1e48, 501, /* Ṉ ṉ */
+ 0x1e4a, 501, /* Ṋ ṋ */
+ 0x1e4c, 501, /* Ṍ ṍ */
+ 0x1e4e, 501, /* Ṏ ṏ */
+ 0x1e50, 501, /* Ṑ ṑ */
+ 0x1e52, 501, /* Ṓ ṓ */
+ 0x1e54, 501, /* Ṕ ṕ */
+ 0x1e56, 501, /* Ṗ ṗ */
+ 0x1e58, 501, /* Ṙ ṙ */
+ 0x1e5a, 501, /* Ṛ ṛ */
+ 0x1e5c, 501, /* Ṝ ṝ */
+ 0x1e5e, 501, /* Ṟ ṟ */
+ 0x1e60, 501, /* Ṡ ṡ */
+ 0x1e62, 501, /* Ṣ ṣ */
+ 0x1e64, 501, /* Ṥ ṥ */
+ 0x1e66, 501, /* Ṧ ṧ */
+ 0x1e68, 501, /* Ṩ ṩ */
+ 0x1e6a, 501, /* Ṫ ṫ */
+ 0x1e6c, 501, /* Ṭ ṭ */
+ 0x1e6e, 501, /* Ṯ ṯ */
+ 0x1e70, 501, /* Ṱ ṱ */
+ 0x1e72, 501, /* Ṳ ṳ */
+ 0x1e74, 501, /* Ṵ ṵ */
+ 0x1e76, 501, /* Ṷ ṷ */
+ 0x1e78, 501, /* Ṹ ṹ */
+ 0x1e7a, 501, /* Ṻ ṻ */
+ 0x1e7c, 501, /* Ṽ ṽ */
+ 0x1e7e, 501, /* Ṿ ṿ */
+ 0x1e80, 501, /* Ẁ ẁ */
+ 0x1e82, 501, /* Ẃ ẃ */
+ 0x1e84, 501, /* Ẅ ẅ */
+ 0x1e86, 501, /* Ẇ ẇ */
+ 0x1e88, 501, /* Ẉ ẉ */
+ 0x1e8a, 501, /* Ẋ ẋ */
+ 0x1e8c, 501, /* Ẍ ẍ */
+ 0x1e8e, 501, /* Ẏ ẏ */
+ 0x1e90, 501, /* Ẑ ẑ */
+ 0x1e92, 501, /* Ẓ ẓ */
+ 0x1e94, 501, /* Ẕ ẕ */
+ 0x1ea0, 501, /* Ạ ạ */
+ 0x1ea2, 501, /* Ả ả */
+ 0x1ea4, 501, /* Ấ ấ */
+ 0x1ea6, 501, /* Ầ ầ */
+ 0x1ea8, 501, /* Ẩ ẩ */
+ 0x1eaa, 501, /* Ẫ ẫ */
+ 0x1eac, 501, /* Ậ ậ */
+ 0x1eae, 501, /* Ắ ắ */
+ 0x1eb0, 501, /* Ằ ằ */
+ 0x1eb2, 501, /* Ẳ ẳ */
+ 0x1eb4, 501, /* Ẵ ẵ */
+ 0x1eb6, 501, /* Ặ ặ */
+ 0x1eb8, 501, /* Ẹ ẹ */
+ 0x1eba, 501, /* Ẻ ẻ */
+ 0x1ebc, 501, /* Ẽ ẽ */
+ 0x1ebe, 501, /* Ế ế */
+ 0x1ec0, 501, /* Ề ề */
+ 0x1ec2, 501, /* Ể ể */
+ 0x1ec4, 501, /* Ễ ễ */
+ 0x1ec6, 501, /* Ệ ệ */
+ 0x1ec8, 501, /* Ỉ ỉ */
+ 0x1eca, 501, /* Ị ị */
+ 0x1ecc, 501, /* Ọ ọ */
+ 0x1ece, 501, /* Ỏ ỏ */
+ 0x1ed0, 501, /* Ố ố */
+ 0x1ed2, 501, /* Ồ ồ */
+ 0x1ed4, 501, /* Ổ ổ */
+ 0x1ed6, 501, /* Ỗ ỗ */
+ 0x1ed8, 501, /* Ộ ộ */
+ 0x1eda, 501, /* Ớ ớ */
+ 0x1edc, 501, /* Ờ ờ */
+ 0x1ede, 501, /* Ở ở */
+ 0x1ee0, 501, /* Ỡ ỡ */
+ 0x1ee2, 501, /* Ợ ợ */
+ 0x1ee4, 501, /* Ụ ụ */
+ 0x1ee6, 501, /* Ủ ủ */
+ 0x1ee8, 501, /* Ứ ứ */
+ 0x1eea, 501, /* Ừ ừ */
+ 0x1eec, 501, /* Ử ử */
+ 0x1eee, 501, /* Ữ ữ */
+ 0x1ef0, 501, /* Ự ự */
+ 0x1ef2, 501, /* Ỳ ỳ */
+ 0x1ef4, 501, /* Ỵ ỵ */
+ 0x1ef6, 501, /* Ỷ ỷ */
+ 0x1ef8, 501, /* Ỹ ỹ */
+ 0x1f59, 492, /* Ὑ ὑ */
+ 0x1f5b, 492, /* Ὓ ὓ */
+ 0x1f5d, 492, /* Ὕ ὕ */
+ 0x1f5f, 492, /* Ὗ ὗ */
+ 0x1fbc, 491, /* ᾼ ᾳ */
+ 0x1fcc, 491, /* ῌ ῃ */
+ 0x1fec, 493, /* Ῥ ῥ */
+ 0x1ffc, 491 /* ῼ ῳ */
+]
+
+/*
+ * title characters are those between
+ * upper and lower case. ie DZ Dz dz
+ */
+const rtotitle1 = [
+ 0x01c4, 501, /* DŽ Dž */
+ 0x01c6, 499, /* dž Dž */
+ 0x01c7, 501, /* LJ Lj */
+ 0x01c9, 499, /* lj Lj */
+ 0x01ca, 501, /* NJ Nj */
+ 0x01cc, 499, /* nj Nj */
+ 0x01f1, 501, /* DZ Dz */
+ 0x01f3, 499 /* dz Dz */
+]
+
+const findc = {c, t, n, nelt, ret
+ var p, m
+
+ /*
+ we're processing in chunks of size
+ nelt, so 1 chunk is of length 'nelt'
+ */
+ while n > 1
+ m = n/2
+ p = t[m*nelt:]
+ if c >= p[0]
+ t = p
+ n = n - m
+ else
+ n = m
+ ;;
+ ;;
+
+ if t.len != 0 && c >= t[0]
+ ret# = t
+ -> true
+ else
+ -> false
+ ;;
+}
+
+
+const isalpha = {c
+ var l
+
+ if isupper(c) || islower(c)
+ -> true
+ elif findc(c, ralpha2[:], ralpha2.len/2, 2, &l)
+ if (c >= l[0] && c <= l[1])
+ -> true
+ ;;
+ elif findc(c, ralpha1[:], ralpha1.len, 1, &l)
+ if (c == l[0])
+ -> true
+ ;;
+ ;;
+ -> false
+}
+
+const isdigit = {c
+ -> c >= '0' && c <= '9'
+}
+
+const isxdigit = {c
+ -> c >= '0' && c <= '9' || c >= 'a' && c <= 'f' || c >= 'A' && c <= 'F'
+}
+
+const isnum = {c
+ var l
+
+ if findc(c, rnums[:], rnums.len/2, 2, &l)
+ if(c >= l[0] && c <= l[1])
+ -> true
+ ;;
+ ;;
+ -> false
+}
+
+const isalnum = {c
+ -> isalpha(c) || isnum(c)
+}
+
+const isblank = {c
+ var l
+ var sl
+ var len
+
+ l = rspace2[:]
+ sl = rspace2[:]
+ len = rspace2.len/2
+ if findc(c, sl, len, 2, &l)
+ if(c >= l[0] && c <= l[1])
+ -> true
+ ;;
+ ;;
+ -> false
+}
+
+const isspace = {c
+ -> c == '\n' || isblank(c)
+}
+
+const islower = {c
+ var l
+
+ /* the first character in the toupper table is the lowercase char */
+ if findc(c, rtoupper2[:], rtoupper2.len/3, 3, &l)
+ if (c >= l[0] && c <= l[1])
+ -> true
+ ;;
+ elif findc(c, rtoupper1[:], rtoupper1.len/2, 2, &l)
+ if (c == l[0])
+ -> true
+ ;;
+ ;;
+ -> false
+}
+
+const isupper = {c
+ var l
+
+ /* the first character in the tolower table is the uppercase char */
+ if findc(c, rtolower2[:], rtolower2.len/3, 3, &l)
+ if (c >= l[0] && c <= l[1])
+ -> true
+ ;;
+ elif findc(c, rtolower1[:], rtolower1.len/2, 2, &l)
+ if (c == l[0])
+ -> true
+ ;;
+ ;;
+ -> false
+}
+
+const istitle = {c
+ -> isupper(c) && islower(c)
+}
+
+const tolower = {c
+ var l
+
+ if findc(c, rtolower2[:], rtolower2.len/3, 3, &l)
+ if c >= l[0] && c <= l[1]
+ -> c + l[2] - 500;
+ ;;
+ elif findc(c, rtolower1[:], rtolower1.len/2, 2, &l)
+ if c == l[0]
+ -> c + l[1] - 500;
+ ;;
+ ;;
+ -> c
+}
+
+const toupper = {c
+ var l
+
+ if findc(c, rtoupper2[:], rtoupper2.len/3, 3, &l);
+ if c >= l[0] && c <= l[1]
+ -> c + l[2] - 500;
+ ;;
+ elif findc(c, rtoupper1[:], rtoupper1.len/2, 2, &l);
+ if c == l[0]
+ -> c + l[1] - 500;
+ ;;
+ ;;
+ -> c
+}
+
+const totitle = {c
+ var l
+
+ if findc(c, rtotitle1[:], rtotitle1.len/2, 2, &l);
+ if c == l[0]
+ -> c + l[1] - 500;
+ ;;
+ ;;
+ -> c
+}
+
+generic charval = {c, base -> @a::(numeric,integral)
+ var v = -1
+
+ if c >= '0' && c <= '9'
+ v = (c - '0') castto(@a::(integral,numeric))
+ elif c >= 'a' && c <= 'z'
+ v = (c - 'a' + 10) castto(@a::(integral,numeric))
+ elif c >= 'A' && c <= 'Z'
+ v = (c - 'A' + 10) castto(@a::(integral,numeric))
+ ;;
+
+ if v < 0 || v > (base castto(@a::(integral,numeric)))
+ -> -1
+ ;;
+ -> v
+}
diff --git a/lib/std/clear.myr b/lib/std/clear.myr
new file mode 100644
index 0000000..c16c851
--- /dev/null
+++ b/lib/std/clear.myr
@@ -0,0 +1,12 @@
+use "slfill.use"
+pkg std =
+ generic clear : (p : @a# -> void)
+;;
+
+generic clear = {p : @a#
+ var bp
+
+ bp = p castto(byte#)
+ slfill(bp[:sizeof(@a)], 0)
+}
+
diff --git a/lib/std/cmp.myr b/lib/std/cmp.myr
new file mode 100644
index 0000000..edc5fe3
--- /dev/null
+++ b/lib/std/cmp.myr
@@ -0,0 +1,54 @@
+use "extremum.use"
+use "types.use"
+
+pkg std =
+ type order = union
+ `Before
+ `Equal
+ `After
+ ;;
+
+ generic numcmp : (a : @a, b : @a -> order)
+ const strcmp : (a : byte[:], b : byte[:] -> order)
+ const strncmp : (a : byte[:], b : byte[:], n : size -> order)
+;;
+
+generic numcmp = {a, b
+ if a < b
+ -> `Before
+ elif a == b
+ -> `Equal
+ else
+ -> `After
+ ;;
+}
+
+const strcmp = {a, b
+ var l
+ var i
+
+ l = min(a.len, b.len)
+ for i = 0; i < l; i++
+ if a[i] < b[i]
+ -> `Before
+ elif a[i] > b[i]
+ -> `After
+ ;;
+ ;;
+
+ if a.len < b.len
+ -> `Before
+ elif a.len > b.len
+ -> `After
+ else
+ -> `Equal
+ ;;
+
+}
+
+const strncmp = {a, b, n
+ a = a[:min(a.len, n)]
+ b = b[:min(b.len, n)]
+ -> strcmp(a, b)
+}
+
diff --git a/lib/std/cstrconv.myr b/lib/std/cstrconv.myr
new file mode 100644
index 0000000..fa9e0bf
--- /dev/null
+++ b/lib/std/cstrconv.myr
@@ -0,0 +1,40 @@
+use "types.use"
+
+pkg std =
+ const cstrlen : (buf : byte[:] -> size)
+ const cstrconv : (buf : byte[:] -> byte[:])
+ const cstrconvp : (p : byte# -> byte[:])
+;;
+
+const cstrconv = {buf
+ var i
+
+ for i = 0; i < buf.len; i++
+ if buf[i] == 0
+ -> buf[:i]
+ ;;
+ ;;
+ -> buf
+}
+
+const cstrconvp = {p
+ var i, base
+
+ i = 0
+ base = p castto(intptr)
+ while ((base + i) castto(byte#))# != 0
+ i++
+ ;;
+ -> p[:i]
+}
+
+const cstrlen = {buf
+ var i
+
+ for i = 0; i < buf.len; i++
+ if buf[i] == 0
+ break
+ ;;
+ ;;
+ -> i
+}
diff --git a/lib/std/dial+plan9.myr b/lib/std/dial+plan9.myr
new file mode 100644
index 0000000..35d97e9
--- /dev/null
+++ b/lib/std/dial+plan9.myr
@@ -0,0 +1,165 @@
+use sys
+
+use "alloc.use"
+use "die.use"
+use "fmt.use"
+use "option.use"
+use "pathjoin.use"
+use "result.use"
+use "sleq.use"
+use "strfind.use"
+use "strstrip.use"
+use "syswrap.use"
+use "utf.use"
+
+
+pkg std =
+ const dial : (dialstr : byte[:] -> result(fd, byte[:]))
+;;
+
+const Maxpath = 512
+
+const dial = {str
+ var netdir, proto, rem
+
+ (netdir, proto, rem) = parsedial(str)
+ if netdir.len != 0
+ -> csdial(netdir, proto, rem)
+ ;;
+
+ match csdial("/net", proto, rem)
+ | `Ok fd: -> `Ok fd
+ | `Fail m:
+ -> csdial("/net.alt", proto, rem)
+ ;;
+}
+
+const csdial = {netdir, proto, rem
+ var dir, clone, addr, csaddr
+ var ret, csfd, n
+ var buf : byte[Maxpath]
+
+ /* Try using the connection server */
+ dir = fmt("{}/cs", netdir)
+ csfd = open(dir, Ordwr)
+ if csfd < 0
+ clone = fmt("{}/{}/clone", netdir, proto)
+ ret = call(clone, rem, netdir)
+ slfree(clone)
+ if ret == -1
+ -> `Fail "unable to dial without cs"
+ else
+ -> `Ok ret
+ ;;
+ ;;
+ slfree(dir)
+
+ csaddr = fmt("{}!{}", proto, rem)
+ if write(csfd, csaddr) < 0
+ close(csfd)
+ -> `Fail "couldn't blah cs"
+ ;;
+ slfree(csaddr)
+
+ seek(csfd, 0, 0)
+ while true
+ n = read(csfd, buf[:])
+ if n <= 0
+ break
+ ;;
+
+ match strfind(buf[:n], " ")
+ | `None: continue
+ | `Some i:
+ clone = buf[:i]
+ addr = buf[i+1:n]
+ ;;
+
+ ret = call(clone, addr, netdir)
+ if ret >= 0
+ break
+ ;;
+ ;;
+
+ close(csfd)
+ if ret < 0
+ -> `Fail "unable to dial"
+ ;;
+ -> `Ok ret
+}
+
+const call = {clone, addr, netdir
+ var namebuf : byte[Maxpath]
+ var databuf : byte[Maxpath]
+ var name, base, dpath
+ var cfd, datafd
+ var c, n
+
+ datafd = -1
+ c = nsclonestr(clone, netdir)
+ cfd = open(c, Ordwr)
+ if cfd < 0
+ goto cleanup
+ ;;
+
+ n = read(cfd, namebuf[:])
+ if n < 0
+ goto cleanup
+ ;;
+ fput(cfd, "connect {}", addr)
+ name = strstrip(namebuf[:n])
+ match strrfind(c, "/")
+ | `None: die("there should be a '/' here\n")
+ | `Some i: base = c[:i]
+ ;;
+ dpath = bfmt(databuf[:], "{}/{}/data", base, name)
+ datafd = open(dpath, Ordwr)
+:cleanup
+ close(cfd)
+ slfree(c)
+ -> datafd
+}
+
+const nsclonestr = {clone, netdir
+ if decode(clone) == '#' || decode(clone) == '/'
+ match std.strfind(clone[1:], "/")
+ | `Some i: clone = clone[i+1:]
+ | `None: /* nothing */
+ ;;
+ ;;
+ -> pathcat(netdir, clone)
+}
+
+const parsedial = {str
+ var netdir, proto, rem, hd, tl
+
+ netdir=""
+ proto = ""
+ rem = ""
+ match strfind(str, "!")
+ | `None:
+ proto = "net"
+ rem = str
+ | `Some sep:
+ hd = str[:sep]
+ tl = str[sep+1:]
+ if decode(hd) == '#' || decode(hd) == '/'
+ match strrfind(hd, "/")
+ | `Some idx:
+ netdir = hd[:idx]
+ proto = hd[idx+1:]
+ | `None:
+ netdir = ""
+ proto = hd
+ ;;
+ else
+ netdir = ""
+ proto = hd
+
+ ;;
+ rem = tl
+ ;;
+
+ -> (netdir, proto, rem)
+}
+
diff --git a/lib/std/dial+posixy.myr b/lib/std/dial+posixy.myr
new file mode 100644
index 0000000..13f0257
--- /dev/null
+++ b/lib/std/dial+posixy.myr
@@ -0,0 +1,147 @@
+use sys
+
+use "alloc.use"
+use "chartype.use"
+use "die.use"
+use "endian.use"
+use "hasprefix.use"
+use "intparse.use"
+use "ipparse.use"
+use "option.use"
+use "resolve.use"
+use "result.use"
+use "sleq.use"
+use "strfind.use"
+use "syswrap.use"
+use "utf.use"
+
+pkg std =
+ const dial : (dialstr : byte[:] -> result(fd, byte[:]))
+;;
+
+/*
+ a map from service name to a list of (port,proto)
+ pairs in order of preference
+*/
+/* FIXME: implement
+var services : htab(byte[:], [int, byte[:]][:])#
+var inited = false
+*/
+
+/* takes a plan 9 style dial string */
+const dial = {str
+ var proto, host, port
+ var sa4 : sys.sockaddr_in
+ var sa6 : sys.sockaddr_in6
+ var sa : sys.sockaddr#
+ var sock
+
+ match parsedial(str)
+ | `Ok val: (proto, host, port) = val
+ | `Fail m: -> `Fail m
+ ;;
+
+ match getaddr(host)
+ | `Ipv4 bits:
+ sa4.fam = sys.Afinet
+ sa4.addr = bits
+ sa4.port = hosttonet(port)
+ sa = &sa4 castto(sys.sockaddr#)
+ | `Ipv6 bits:
+ sa6.fam = sys.Afinet6
+ sa6.addr = bits
+ sa6.port = hosttonet(port)
+ sa = &sa6 castto(sys.sockaddr#)
+ ;;
+ sock = sys.socket(sa.fam, proto, 0)
+
+ if sock < 0
+ -> `Fail "failed to connect to socket"
+ ;;
+ var err
+ err = sys.connect(sock, sa, sizeof(sys.sockaddr_in))
+ if err < 0
+ sys.close(sock)
+ -> `Fail "Failed to bind socket"
+ ;;
+
+ -> `Ok (sock castto(fd))
+}
+
+const parsedial = {str
+ var proto, host, port
+ var socktype, portnum
+
+ (proto, str) = nameseg(str)
+ (host, str) = nameseg(str)
+ (port, str) = nameseg(str)
+
+ if proto.len == 0
+ -> `Fail "missing proto"
+ elif host.len == 0
+ -> `Fail "missing host"
+ elif port.len == 0
+ -> `Fail "missing port"
+ ;;
+
+ if sleq(proto, "net")
+ -> `Fail "net wildcard proto not yet supported\n"
+ elif sleq(proto, "unix")
+ -> `Fail "net unix proto not yet supported\n"
+ elif sleq(proto, "tcp")
+ socktype = sys.Sockstream
+ elif sleq(proto, "udp")
+ socktype = sys.Sockdgram
+ ;;
+
+ match parseport(port)
+ | `Some n: portnum = n
+ | `None: -> `Fail "bad port"
+ ;;
+
+ -> `Ok (socktype, host, portnum)
+}
+
+const parseport = {port
+ match intparse(port)
+ | `Some n: -> `Some n
+ | `None:
+ /* a small number of hardcoded ports */
+ if sleq(port, "http")
+ -> `Some 80
+ elif sleq(port, "https")
+ -> `Some 443
+ elif sleq(port, "ircd")
+ -> `Some 6667
+ elif sleq(port, "dns")
+ -> `Some 53
+ ;;
+ ;;
+ -> `None
+}
+
+const getaddr = {addr
+ var ip
+
+ match ipparse(addr)
+ | `Some a: ip = a
+ | `None:
+ match resolve(addr)
+ | `Ok hi:
+ ip = hi[0].addr
+ slfree(hi)
+ | `Fail m:
+ ;;
+ ;;
+ -> ip
+}
+
+const nameseg = {str
+ match strfind(str, "!")
+ | `Some idx:
+ -> (str[:idx], str[idx+1:])
+ | `None:
+ -> (str, "")
+ ;;
+}
+
diff --git a/lib/std/die.myr b/lib/std/die.myr
new file mode 100644
index 0000000..87135f1
--- /dev/null
+++ b/lib/std/die.myr
@@ -0,0 +1,19 @@
+use "syswrap.use"
+use "types.use"
+
+pkg std =
+ $noret const die : (msg : byte[:] -> void)
+ const assert : (cond : bool, msg : byte[:] -> void)
+;;
+
+const die = {msg
+ write(2, msg)
+ suicide()
+}
+
+const assert = {cond, msg
+ if !cond
+ die(msg)
+ ;;
+}
+
diff --git a/lib/std/dir+freebsd.myr b/lib/std/dir+freebsd.myr
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/lib/std/dir+freebsd.myr
diff --git a/lib/std/dir+linux.myr b/lib/std/dir+linux.myr
new file mode 100644
index 0000000..60ef6f9
--- /dev/null
+++ b/lib/std/dir+linux.myr
@@ -0,0 +1,66 @@
+use sys
+
+use "alloc.use"
+use "die.use"
+use "option.use"
+use "result.use"
+use "slcp.use"
+use "sldup.use"
+use "types.use"
+
+pkg std =
+ type dir = struct
+ fd : sys.fd
+ buf : byte[16384]
+ len : int64
+ off : int64
+ ;;
+
+ const diropen : (p : byte[:] -> std.result(dir#, byte[:]))
+ const dirread : (d : dir# -> std.option(byte[:]))
+ const dirclose : (d : dir# -> void)
+;;
+
+const Direntoverhead = 20
+
+const diropen = {p
+ var fd
+ var dir
+
+ fd = sys.open(p, sys.Ordonly | sys.Odir)
+ if fd < 0
+ -> `Fail "couldn't open directory"
+ ;;
+ dir = zalloc()
+ dir.fd = fd
+ -> `Ok dir
+}
+
+const dirread = {d
+ var len
+ var dent
+ var namelen
+
+ if d.off >= d.len
+ len = sys.getdents64(d.fd, d.buf[:])
+ if len <= 0
+ -> `None
+ ;;
+ d.len = len
+ d.off = 0
+ ;;
+
+ dent = &d.buf[d.off] castto(sys.dirent64#)
+ namelen = 0
+ d.off += dent.reclen castto(int64)
+ while dent.name[namelen] != 0
+ namelen++
+ ;;
+ -> `Some sldup(dent.name[:namelen])
+}
+
+const dirclose = {d
+ sys.close(d.fd)
+ free(d)
+}
+
diff --git a/lib/std/dir+osx.myr b/lib/std/dir+osx.myr
new file mode 100644
index 0000000..28ef26b
--- /dev/null
+++ b/lib/std/dir+osx.myr
@@ -0,0 +1,60 @@
+use sys
+
+use "alloc.use"
+use "die.use"
+use "option.use"
+use "result.use"
+use "slcp.use"
+use "sldup.use"
+use "types.use"
+
+pkg std =
+ type dir = struct
+ fd : sys.fd
+ buf : byte[16384]
+ len : int64
+ off : int64
+ base : int64
+ ;;
+
+ const diropen : (p : byte[:] -> std.result(dir#, byte[:]))
+ const dirread : (d : dir# -> std.option(byte[:]))
+ const dirclose : (d : dir# -> void)
+;;
+
+const diropen = {p
+ var fd
+ var dir
+
+ fd = sys.open(p, sys.Ordonly | sys.Odir)
+ if fd < 0
+ -> `Fail "couldn't open directory"
+ ;;
+ dir = zalloc()
+ dir.fd = fd
+ -> `Ok dir
+}
+
+const dirread = {d
+ var len
+ var dent
+
+ if d.off >= d.len
+ len = sys.getdirentries64(d.fd, d.buf[:], &d.base)
+ if len <= 0
+ -> `None
+ ;;
+ d.len = len
+ d.off = 0
+ ;;
+
+ dent = &d.buf[d.off] castto(sys.dirent64#)
+ d.off += dent.reclen castto(int64)
+ -> `Some sldup(dent.name[:dent.namlen])
+}
+
+const dirclose = {d
+ sys.close(d.fd)
+ free(d)
+}
+
diff --git a/lib/std/dir+plan9.myr b/lib/std/dir+plan9.myr
new file mode 100644
index 0000000..41a579f
--- /dev/null
+++ b/lib/std/dir+plan9.myr
@@ -0,0 +1,66 @@
+use sys
+
+use "alloc.use"
+use "die.use"
+use "option.use"
+use "result.use"
+use "slcp.use"
+use "sldup.use"
+use "syswrap.use"
+use "types.use"
+
+pkg std =
+ type dir = struct
+ fd : fd
+ buf : byte[65536] /* a big big, but at least it will always hold a directory entry... */
+ len : int64
+ off : int64
+ ;;
+
+ const diropen : (p : byte[:] -> std.result(dir#, byte[:]))
+ const dirread : (d : dir# -> std.option(byte[:]))
+ const dirclose : (d : dir# -> void)
+;;
+
+const diropen = {p
+ var fd
+ var dir
+
+ fd = open(p, Ordonly)
+ if fd < 0
+ -> `Fail "couldn't open directory"
+ ;;
+
+ dir = zalloc()
+ dir.fd = fd
+ -> `Ok dir
+}
+
+
+const dirread = {d
+ var len : int64, name, base, namelen, dirlen
+
+ /* NB: On Plan 9, read(2) will always return whole directory entries */
+ if d.off >= d.len
+ len = read(d.fd, d.buf[:]) castto(int64)
+ if len <= 0
+ -> `None
+ ;;
+ d.len = len
+ d.off = 0
+ ;;
+
+ namelen = (d.buf[d.off + Stringsoff] castto(int64)) | \
+ ((d.buf[d.off + Stringsoff + 1] castto(int64)) << 8)
+ base = d.off + Stringsoff + 2
+ dirlen = (d.buf[d.off] castto(int64)) | \
+ ((d.buf[d.off + 1] castto(int64)) << 8)
+ name = d.buf[base:base + namelen]
+ d.off += dirlen + 2
+ -> `Some std.sldup(name)
+}
+
+const dirclose = {d
+ close(d.fd)
+ free(d)
+}
diff --git a/lib/std/dirname.myr b/lib/std/dirname.myr
new file mode 100644
index 0000000..5e80771
--- /dev/null
+++ b/lib/std/dirname.myr
@@ -0,0 +1,37 @@
+use "alloc.use"
+use "slcp.use"
+use "die.use"
+use "sldup.use"
+use "strfind.use"
+use "option.use"
+
+pkg std =
+ const dirname : (p : byte[:] -> byte[:])
+ const basename : (p : byte[:] -> byte[:])
+;;
+
+const dirname = {p
+ match std.strrfind(p, "/")
+ | `std.Some idx:
+ -> std.sldup(p[:idx])
+ | `std.None:
+ -> std.sldup(".")
+ ;;
+}
+
+const basename = {p
+:again
+ if p.len == 0
+ -> std.sldup(".")
+ ;;
+
+ match std.strrfind(p, "/")
+ | `std.Some idx:
+ if idx == p.len - 1
+ goto again
+ ;;
+ -> std.sldup(p[idx+1:])
+ | `std.None:
+ -> std.sldup(p)
+ ;;
+}
diff --git a/lib/std/endian.myr b/lib/std/endian.myr
new file mode 100644
index 0000000..8bac74f
--- /dev/null
+++ b/lib/std/endian.myr
@@ -0,0 +1,32 @@
+pkg std =
+ generic hosttonet : (v : @a -> @a)
+ generic nettohost : (v : @a -> @a)
+;;
+
+/* FIXME: we only support little endian platforms right now,
+ so we assume a little endian machine. FIX THIS. */
+generic hosttonet = {v : @a::(integral,numeric)
+ var i
+ var ret
+
+ ret = 0
+ for i = 0; i < sizeof(@a); i++
+ ret <<= 8
+ ret |= v & 0xff
+ v >>= 8
+ ;;
+ -> ret
+}
+
+generic nettohost = {v : @a::(integral,numeric)
+ var i
+ var ret
+
+ ret = 0
+ for i = 0; i < sizeof(@a); i++
+ ret <<= 8
+ ret |= v & 0xff
+ v >>= 8
+ ;;
+ -> ret
+}
diff --git a/lib/std/env+plan9.myr b/lib/std/env+plan9.myr
new file mode 100644
index 0000000..4a95ed7
--- /dev/null
+++ b/lib/std/env+plan9.myr
@@ -0,0 +1,56 @@
+use sys
+
+use "alloc.use"
+use "die.use"
+use "extremum.use"
+use "fmt.use"
+use "option.use"
+use "result.use"
+use "slcp.use"
+use "sldup.use"
+use "sleq.use"
+use "slpush.use"
+use "slurp.use"
+
+pkg std =
+ const getenv : (name : byte[:] -> option(byte[:]))
+ const getenvv : (name : byte[:], default : byte[:] -> byte[:])
+;;
+
+var envkey : byte[:][:]
+var envval : byte[:][:]
+
+const envfind = {key
+ var i
+ for i = 0; i < envkey.len; i++
+ if std.sleq(envkey[i], key)
+ -> `Some envval[i]
+ ;;
+ ;;
+ -> `None
+}
+
+const getenv = {name
+ var buf : byte[128]
+ var s
+
+ match envfind(name)
+ | `Some val: -> `Some val
+ | `None:
+ s = bfmt(buf[:], "/env/{}", name)
+ match std.slurp(s)
+ | `Fail m: -> `None
+ | `Ok data:
+ envkey = slpush(envkey, sldup(name))
+ envval = slpush(envval, data)
+ -> `Some data
+ ;;
+ ;;
+}
+
+const getenvv = {name, default
+ match getenv(name)
+ | `Some val: -> val
+ | `None: -> default
+ ;;
+}
diff --git a/lib/std/env+posixy.myr b/lib/std/env+posixy.myr
new file mode 100644
index 0000000..d7b0e5e
--- /dev/null
+++ b/lib/std/env+posixy.myr
@@ -0,0 +1,28 @@
+use sys
+
+use "extremum.use"
+use "option.use"
+use "sleq.use"
+
+pkg std =
+ const getenv : (name : byte[:] -> option(byte[:]))
+ const getenvv : (name : byte[:], default : byte[:] -> byte[:])
+;;
+
+const getenv = {name
+ var n
+ for env in sys.__environment
+ n = min(name.len, env.len)
+ if sleq(name, env[:n]) && sleq(env[n:n+1], "=")
+ -> `Some env[n+1:]
+ ;;
+ ;;
+ -> `None
+}
+
+const getenvv = {name, default
+ match getenv(name)
+ | `Some v: -> v
+ | `None: -> default
+ ;;
+}
diff --git a/lib/std/errno+plan9.myr b/lib/std/errno+plan9.myr
new file mode 100644
index 0000000..c911349
--- /dev/null
+++ b/lib/std/errno+plan9.myr
@@ -0,0 +1,6 @@
+pkg std =
+ type errno = int
+ const Erange : errno = 1
+ const Emisc : errno = 2
+ const Eexist : errno = 3
+;;
diff --git a/lib/std/errno.myr b/lib/std/errno.myr
new file mode 100644
index 0000000..c14c469
--- /dev/null
+++ b/lib/std/errno.myr
@@ -0,0 +1,40 @@
+use sys
+
+pkg std =
+ type errno = int
+
+ const Eperm : errno = sys.Eperm castto(errno)
+ const Enoent : errno = sys.Enoent castto(errno)
+ const Esrch : errno = sys.Esrch castto(errno)
+ const Eintr : errno = sys.Eintr castto(errno)
+ const Eio : errno = sys.Eio castto(errno)
+ const Enxio : errno = sys.Enxio castto(errno)
+ const E2big : errno = sys.E2big castto(errno)
+ const Enoexec : errno = sys.Enoexec castto(errno)
+ const Ebadf : errno = sys.Ebadf castto(errno)
+ const Echild : errno = sys.Echild castto(errno)
+ const Eagain : errno = sys.Eagain castto(errno)
+ const Enomem : errno = sys.Enomem castto(errno)
+ const Eacces : errno = sys.Eacces castto(errno)
+ const Efault : errno = sys.Efault castto(errno)
+ const Enotblk : errno = sys.Enotblk castto(errno)
+ const Ebusy : errno = sys.Ebusy castto(errno)
+ const Eexist : errno = sys.Eexist castto(errno)
+ const Exdev : errno = sys.Exdev castto(errno)
+ const Enodev : errno = sys.Enodev castto(errno)
+ const Enotdir : errno = sys.Enotdir castto(errno)
+ const Eisdir : errno = sys.Eisdir castto(errno)
+ const Einval : errno = sys.Einval castto(errno)
+ const Enfile : errno = sys.Enfile castto(errno)
+ const Emfile : errno = sys.Emfile castto(errno)
+ const Enotty : errno = sys.Enotty castto(errno)
+ const Etxtbsy : errno = sys.Etxtbsy castto(errno)
+ const Efbig : errno = sys.Efbig castto(errno)
+ const Enospc : errno = sys.Enospc castto(errno)
+ const Espipe : errno = sys.Espipe castto(errno)
+ const Erofs : errno = sys.Erofs castto(errno)
+ const Emlink : errno = sys.Emlink castto(errno)
+ const Epipe : errno = sys.Epipe castto(errno)
+ const Edom : errno = sys.Edom castto(errno)
+ const Erange : errno = sys.Erange castto(errno)
+;;
diff --git a/lib/std/execvp.myr b/lib/std/execvp.myr
new file mode 100644
index 0000000..38d9fc5
--- /dev/null
+++ b/lib/std/execvp.myr
@@ -0,0 +1,57 @@
+use "alloc.use"
+use "env.use"
+use "fmt.use"
+use "option.use"
+use "strfind.use"
+use "strsplit.use"
+use "syswrap.use"
+
+pkg std =
+ const execvp : (cmd : byte[:], args : byte[:][:] -> int64)
+ const execvpe : (cmd : byte[:], args : byte[:][:], env : byte[:][:] -> int64)
+;;
+
+const execvp = {cmd, args
+ var paths, binpath
+ var buf : byte[512]
+
+ match strfind(cmd, "/")
+ | `Some _:
+ -> execv(cmd, args)
+ | `None:
+ paths = getpaths()
+ for p in paths
+ binpath = bfmt(buf[:], "{}/{}", p, cmd)
+ execv(binpath, args)
+ ;;
+ slfree(paths)
+ ;;
+ -> -1
+}
+
+const execvpe = {cmd, args, env
+ var paths, binpath
+ var buf : byte[512]
+
+ match strfind(cmd, "/")
+ | `Some _:
+ -> execve(cmd, args, env)
+ | `None:
+ paths = getpaths()
+ for p in paths
+ binpath = bfmt(buf[:], "{}/{}", p, cmd)
+ execve(binpath, args, env)
+ ;;
+ slfree(paths)
+ ;;
+ -> -1
+}
+
+const getpaths = {
+ var path
+ match getenv("PATH")
+ | `Some p: path = p
+ | `None: path = "/usr/local/bin:/bin:/usr/bin"
+ ;;
+ -> strsplit(path, ":")
+}
diff --git a/lib/std/extremum.myr b/lib/std/extremum.myr
new file mode 100644
index 0000000..9675915
--- /dev/null
+++ b/lib/std/extremum.myr
@@ -0,0 +1,40 @@
+pkg std =
+ generic min : (a : @a::numeric, b : @a::numeric -> @a::numeric)
+ generic max : (a : @a::numeric, b : @a::numeric -> @a::numeric)
+ generic clamp : (a : @a::numeric, min : @a::numeric, max : @a::numeric -> @a::numeric)
+ generic abs : (a : @a::numeric -> @a::numeric)
+;;
+
+generic min = {a, b
+ if a < b
+ -> a
+ else
+ -> b
+ ;;
+}
+
+generic max = {a, b
+ if a > b
+ -> a
+ else
+ -> b
+ ;;
+}
+
+generic clamp = {a, min, max
+ if a < min
+ -> min
+ elif a > max
+ -> max
+ else
+ -> a
+ ;;
+}
+
+generic abs = {a : @a::numeric
+ if a < (0 castto(@a::numeric))
+ -> -a
+ else
+ -> a
+ ;;
+}
diff --git a/lib/std/fltbits.myr b/lib/std/fltbits.myr
new file mode 100644
index 0000000..2f77ee9
--- /dev/null
+++ b/lib/std/fltbits.myr
@@ -0,0 +1,60 @@
+pkg std =
+ const flt64bits : (flt : flt64 -> int64)
+ const flt32bits : (flt : flt32 -> int32)
+ const flt64frombits : (bits : uint64 -> flt64)
+ const flt32frombits : (bits : uint32 -> flt32)
+ const flt64explode : (flt : flt64 -> (bool, int64, int64))
+ const flt32explode : (flt : flt32 -> (bool, int32, int32))
+;;
+
+const flt64bits = {flt; -> (&flt castto(int64#))#}
+const flt32bits = {flt; -> (&flt castto(int32#))#}
+const flt64frombits = {bits; -> (&bits castto(flt64#))#}
+const flt32frombits = {bits; -> (&bits castto(flt32#))#}
+
+const flt64explode = {flt
+ var bits, isneg, mant, exp
+
+ bits = flt64bits(flt)
+ isneg = (bits >> 63) != 0 /* msb is sign bit */
+ exp = (bits >> 52) & 0x7ff /* exp is in bits [52..63] */
+ mant = bits & ((1l << 52) - 1) /* msb is in bits [..51] */
+
+ /* add back the implicit bit if this is not a denormal */
+ if exp != 0
+ mant |= 1l << 52
+ else
+ exp = 1
+ ;;
+ /*
+ adjust for exponent bias. nb: because we are
+ treating the mantissa as m.0 instead of 0.m,
+ our exponent bias needs to be offset by the
+ size of m
+ */
+ -> (isneg, mant, exp)
+}
+
+const flt32explode = {flt
+ var bits, isneg, mant, exp
+
+ bits = flt32bits(flt)
+ isneg = (bits >> 31) != 0 /* msb is sign bit */
+ exp = (bits >> 22) & 0xff /* exp is in bits [23..30] */
+ mant = bits & ((1 << 22) - 1) /* msb is in bits [0..22] */
+
+ /* add back the implicit bit if this is not a denormal */
+ if exp != 0
+ mant |= 1 << 22
+ else
+ exp = 1
+ ;;
+ /*
+ adjust for exponent bias. nb: because we are
+ treating the mantissa as m.0 instead of 0.m,
+ our exponent bias needs to be offset by the
+ size of m
+ */
+ -> (isneg, mant, exp)
+}
+
diff --git a/lib/std/fltfmt.myr b/lib/std/fltfmt.myr
new file mode 100644
index 0000000..395ade4
--- /dev/null
+++ b/lib/std/fltfmt.myr
@@ -0,0 +1,239 @@
+use "alloc.use"
+use "bigint.use"
+use "die.use"
+use "extremum.use"
+use "fltbits.use"
+use "slpush.use"
+use "strbuf.use"
+use "types.use"
+use "utf.use"
+
+pkg std =
+ pkglocal const MNormal = 0
+ pkglocal const MAbsolute = 1
+ pkglocal const MRelative = 2
+
+ pkglocal const flt64bfmt : (sb : strbuf#, val : flt64, mode : int, precision : int -> void)
+ pkglocal const flt32bfmt : (sb : strbuf#, val : flt32, mode : int, precision : int -> void)
+;;
+
+const Dblbias = 1023
+const Fltbias = 127
+
+const flt64bfmt = {sb, val, mode, precision
+ var isneg, exp, mant
+
+ (isneg, mant, exp) = flt64explode(val)
+ dragon4(sb, isneg, mant, (exp - 52) castto(int64), Dblbias, mode, precision)
+}
+
+const flt32bfmt = {sb, val, mode, precision
+ var isneg, exp, mant
+
+ (isneg, mant, exp) = flt32explode(val)
+ dragon4(sb, isneg, mant castto(int64), (exp - 52) castto(int64), Fltbias, mode, precision)
+}
+
+/*
+sb: output buffer
+e: exponent
+p: precision
+f: mantissa
+
+flting value: x = f^(e - p)
+*/
+const dragon4 = {sb, isneg, f, e, p, mode, cutoff
+ var r, s, t, u, v, y
+ var udig
+ var mm, mp /* margins above and below */
+ var roundup
+ var low, high
+ var k, n
+ var a, i
+
+ /* if we have zero for the mantissa, we can return early */
+ n = 0
+ if isneg
+ sbputs(sb, "-")
+ ;;
+ if f == 0
+ sbputs(sb, "0.0")
+ ;;
+
+ /* initialize */
+ roundup = false
+ r = mkbigint(f)
+ r = bigshli(r, max(e - p, 0))
+ s = bigshli(mkbigint(1), max(0, -(e - p)))
+ mm = bigshli(mkbigint(1), max((e - p), 0))
+ mp = bigdup(mm)
+
+ /* fixup: unequal gaps */
+ t = mkbigint(1)
+ bigshli(t, p - 1)
+ if bigeqi(t, f)
+ bigshli(mp, 1)
+ bigshli(r, 1)
+ bigshli(s, 1)
+ ;;
+ bigfree(t)
+
+ k = 0
+ while true
+ /* r < ceil(s/b) */
+ t = bigdup(s)
+ bigaddi(t, 9)
+ bigdivi(t, 10)
+ match bigcmp(r, t)
+ | `Before:
+ k--
+ bigmuli(r, 10)
+ bigmuli(mm, 10)
+ bigmuli(mp, 10)
+ | _:
+ bigfree(t)
+ break
+ ;;
+ bigfree(t)
+ ;;
+
+ while true
+ t = bigdup(r)
+ bigshli(t, 1)
+ bigadd(t, mp)
+ while true
+ u = bigdup(s)
+ bigshli(u, 1)
+ match bigcmp(t, u)
+ | `Before:
+ bigfree(u)
+ break
+ | _:
+ k++
+ bigmuli(s, 10)
+ bigfree(u)
+ ;;
+ ;;
+ if mode == MNormal
+ cutoff = k
+ else
+ if mode == MRelative
+ cutoff += k - 1
+ ;;
+ /* common between relative and absolute */
+ a = cutoff - k - 1
+ y = bigdup(s)
+ if a < 0
+ for i = 0; i < a; i++
+ bigmuli(y, 10)
+ ;;
+ else
+ for i = 0; i < -a; i++
+ bigaddi(y, 9)
+ bigdivi(y, 10)
+ ;;
+ ;;
+ match bigcmp(y, mm)
+ | `Before: /* nothing */
+ | _:
+ bigfree(mm)
+ mm = y
+ ;;
+ match bigcmp(y, mp)
+ | `Before: /* nothing */
+ | _:
+ bigfree(mp)
+ mp = y
+ roundup = true
+ ;;
+ ;;
+ u = bigdup(s)
+ bigshli(u, 1)
+ match bigcmp(t, u)
+ | `Before:
+ bigfree(t)
+ bigfree(u)
+ break
+ | _:
+ ;;
+ ;;
+
+ if k <= 0
+ sbputs(sb, "0.")
+ ;;
+ while true
+ k--
+ bigmuli(r, 10)
+ u = bigdup(r);
+ bigdiv(u, s)
+
+ bigmod(r, s)
+ bigmuli(mm, 10)
+ bigmuli(mp, 10)
+
+ low = false
+ t = bigdup(r)
+ bigshli(t, 1)
+ match bigcmp(t, mm)
+ | `Before: low = true
+ | _:
+ ;;
+ bigfree(t)
+
+ v = bigdup(r)
+ bigshli(v, 1)
+ t = bigdup(s)
+ bigshli(t, 1)
+ bigsub(t, mp)
+ match bigcmp(v, t)
+ | `After: high = true
+ | `Equal: high = roundup
+ | `Before: high = false
+ ;;
+ bigfree(v)
+ bigfree(t)
+ if low || high || k == cutoff
+ break
+ ;;
+ format(sb, lowdig(u), k)
+ bigfree(u)
+ ;;
+
+ /* format the last digit */
+ udig = lowdig(u)
+ if low && !high
+ format(sb, udig, k)
+ elif high && !low
+ format(sb, udig + 1, k)
+ else
+ bigmuli(r, 2)
+ match bigcmp(r, s)
+ | `Before: format(sb, udig, k)
+ | `Equal: format(sb, udig, k)
+ | `After: format(sb, udig + 1, k)
+ ;;
+ ;;
+ -> n
+}
+
+const lowdig = {u
+ if u.dig.len > 0
+ -> u.dig[0]
+ ;;
+ -> 0
+}
+
+const format = {sb, d, k
+ const dig = "0123456789"
+ var i
+
+ if k < 0
+ for i = 0; i < -k - 1; i++
+ sbputs(sb, "0")
+ ;;
+ ;;
+ sbputb(sb, dig[d])
+ if k == 0
+ sbputs(sb, ".")
+ ;;
+}
diff --git a/lib/std/fmt.myr b/lib/std/fmt.myr
new file mode 100644
index 0000000..b786e98
--- /dev/null
+++ b/lib/std/fmt.myr
@@ -0,0 +1,499 @@
+use "alloc.use"
+use "chartype.use"
+use "die.use"
+use "extremum.use"
+use "fltfmt.use"
+use "hashfuncs.use"
+use "hasprefix.use"
+use "htab.use"
+use "introspect.use"
+use "intparse.use"
+use "option.use"
+use "sleq.use"
+use "slpush.use"
+use "strbuf.use"
+use "strfind.use"
+use "strsplit.use"
+use "syswrap-ss.use"
+use "syswrap.use"
+use "types.use"
+use "utf.use"
+use "varargs.use"
+
+pkg std =
+ /* write to fd */
+ const put : (fmt : byte[:], args : ... -> size)
+ const fput : (fd : fd, fmt : byte[:], args : ... -> size)
+ const putv : (fmt : byte[:], ap : valist# -> size)
+ const fputv : (fd : fd, fmt : byte[:], ap : valist# -> size)
+
+ /* write to buffer */
+ const fmt : (fmt : byte[:], args : ... -> byte[:])
+ const fmtv : (fmt : byte[:], ap : valist# -> byte[:])
+ const bfmt : (buf : byte[:], fmt : byte[:], args : ... -> byte[:])
+ const bfmtv : (buf : byte[:], fmt : byte[:], ap : valist# -> byte[:])
+
+ /* write to strbuf */
+ const sbfmt : (buf : strbuf#, fmt : byte[:], args : ... -> size)
+ const sbfmtv : (buf : strbuf#, fmt : byte[:], ap : valist# -> size)
+
+ /* add a formatter function */
+ const fmtinstall : (ty : byte[:], \
+ fn : (sb : strbuf#, ap : valist#, opts : (byte[:],byte[:])[:] -> void), \
+ optdesc : (byte[:], bool)[:] \
+ -> void)
+
+ $noret const fatal : (fmt : byte[:], args : ... -> void)
+ $noret const fatalv : (fmt : byte[:], ap : valist# -> void)
+;;
+
+const __init__ = {
+ fmtmap = mkht(strhash, streq)
+}
+
+type fmtdesc = struct
+ fn : (sb : strbuf#, ap : valist#, opts : (byte[:],byte[:])[:] -> void)
+ optdesc : (byte[:], bool)[:]
+;;
+
+/* same as 'put', but exits the program after printing */
+const fatal = {fmt, args
+ var ap
+
+ ap = vastart(&args)
+ putv(fmt, &ap)
+ exit(1)
+}
+
+/* same as 'putv', but exits the program after printing */
+const fatalv = {fmt, ap
+ putv(fmt, ap)
+ exit(1)
+}
+
+var fmtmap : htab(byte[:], fmtdesc)#
+
+const fmtinstall = {ty, fn, optdesc
+ htput(fmtmap, ty, [.fn=fn, .optdesc=optdesc])
+}
+
+const put = {fmt, args
+ var ap
+
+ ap = vastart(&args)
+ -> fputv(1, fmt, &ap)
+}
+
+const putv = {fmt, ap
+ -> fputv(1, fmt, ap)
+}
+
+const fput = {fd, fmt, args
+ var ap
+
+ ap = vastart(&args)
+ -> fputv(fd, fmt, &ap)
+}
+
+const fputv = {fd, fmt, ap
+ var sb, s
+
+ sb = mksb()
+ sbfmtv(sb, fmt, ap)
+ s = sbfin(sb)
+ -> writeall(fd, s)
+}
+
+const fmt = {fmt, args
+ var ap
+
+ ap = vastart(&args)
+ -> fmtv(fmt, &ap)
+}
+
+const fmtv = {fmt, ap
+ var sb
+
+ sb = mksb()
+ sbfmtv(sb, fmt, ap)
+ -> sbfin(sb)
+}
+
+const bfmt = {buf, fmt, args
+ var ap
+
+ ap = vastart(&args)
+ -> bfmtv(buf, fmt, &ap)
+}
+
+const bfmtv = {buf, fmt, ap
+ var sb
+
+ sb = mkbufsb(buf)
+ sbfmtv(sb, fmt, ap)
+ -> sbfin(sb)
+}
+
+const sbfmt = {sb, fmt, args
+ var ap
+
+ ap = vastart(&args)
+ -> sbfmtv(sb, fmt, &ap)
+}
+
+const sbfmtv = {sb, fmt, ap -> size
+ var nfmt, nparams, pl, orig
+ var c, params, ty
+
+ orig = fmt
+ nparams = ap.tc.nelt
+ nfmt = 0
+ while fmt.len != 0
+ (c, fmt) = striter(fmt)
+ match c
+ | '{':
+ if decode(fmt) == '{'
+ (c, fmt) = striter(fmt)
+ sbputc(sb, '{')
+ else
+ (params, fmt) = getparams(fmt)
+ nfmt++
+ if nfmt > nparams
+ die("too few params for fmt\n")
+ ;;
+
+ ty = vatype(ap)
+ match htget(fmtmap, ty)
+ | `Some f:
+ pl = parseparams(params, f.optdesc)
+ f.fn(sb, ap, pl)
+ std.slfree(pl)
+ | `None:
+ fallbackfmt(sb, params, ty, ap)
+ ;;
+ ;;
+ | '}':
+ if decode(fmt) == '}'
+ sbputc(sb, '}')
+ ;;
+ | chr:
+ sbputc(sb, chr)
+ ;;
+:fmtdone
+ ;;
+ if nfmt != nparams
+ write(1, orig)
+ die("too many params for fmt\n")
+ ;;
+ -> sb.len
+}
+
+const parseparams = {paramstr, optdesc
+ var params, opts
+ var o, a, ha : bool, gotarg : bool
+
+ opts = [][:]
+ params = strsplit(paramstr, ",")
+ for p in params
+ /* parse out the key/value pair */
+ match std.strfind(p, "=")
+ | `std.Some idx:
+ o = p[:idx]
+ a = p[idx+1:]
+ gotarg = true
+ | `std.None:
+ o = p
+ a = ""
+ gotarg = false
+ ;;
+
+ /* verify and add the arg */
+ for (opt, hasarg) in optdesc
+ if !std.sleq(opt, o)
+ continue
+ ;;
+ ha = hasarg
+ if ha == gotarg
+ opts = std.slpush(opts, (o, a))
+ else
+ std.fatal("invalid option {}", o)
+ ;;
+ ;;
+ ;;
+ slfree(params)
+ -> opts
+}
+
+
+const fallbackfmt = {sb, params, tyenc, ap : valist# -> void
+ /* value types */
+ var t_val : bool
+ var b_val : int8, ub_val : uint8
+ var w_val : int16, uw_val : uint16
+ var i_val : int32, ui_val : uint32
+ var l_val : int64, ul_val : uint64
+ var z_val : size
+ var p_val : byte#
+ var c_val : char
+ var s_val : byte[:]
+ var f32_val : flt32, f64_val : flt64
+ var i8 : int8, i16: int16, i32 : int32
+ var by : byte
+ var i : int, i64 : int64, l : long
+ var ui8 : int8, ui16: int16, ui32 : int32
+ var ui : int, ui64 : int64, ul : long
+
+ match typedesc(tyenc)
+ | `Tynone: /* nothing */
+ /* atomic types */
+ | `Tyvoid:
+ sbputs(sb, "void")
+ | `Tybool:
+ t_val = vanext(ap)
+ if t_val
+ sbputs(sb ,"true")
+ else
+ sbputs(sb, "false")
+ ;;
+ | `Tychar:
+ c_val = vanext(ap)
+ sbputc(sb, c_val)
+ | `Tyint8:
+ b_val = vanext(ap)
+ intfmt(sb, intparams(params), true, b_val)
+ | `Tyint16:
+ w_val = vanext(ap)
+ intfmt(sb, intparams(params), true, w_val)
+ | `Tyint:
+ i_val = vanext(ap)
+ intfmt(sb, intparams(params), true, i_val)
+ | `Tyint32:
+ i_val = vanext(ap)
+ intfmt(sb, intparams(params), true, i_val)
+ | `Tyint64:
+ l_val = vanext(ap)
+ intfmt(sb, intparams(params), true, l_val)
+ | `Tylong:
+ l_val = vanext(ap)
+ intfmt(sb, intparams(params), true, l_val)
+
+ | `Tybyte:
+ ub_val = vanext(ap)
+ intfmt(sb, intparams(params), false, ub_val)
+ | `Tyuint8:
+ ub_val = vanext(ap)
+ intfmt(sb, intparams(params), false, ub_val)
+ | `Tyuint16:
+ uw_val = vanext(ap)
+ intfmt(sb, intparams(params), false, uw_val)
+ | `Tyuint:
+ ui_val = vanext(ap)
+ intfmt(sb, intparams(params), false, ui_val)
+ | `Tyuint32:
+ ui_val = vanext(ap)
+ intfmt(sb, intparams(params), false, ui_val)
+ | `Tyuint64:
+ ul_val = vanext(ap)
+ intfmt(sb, intparams(params), false, ul_val)
+ | `Tyulong:
+ ul_val = vanext(ap)
+ intfmt(sb, intparams(params), false, ul_val)
+ | `Tyflt32:
+ f32_val = vanext(ap)
+ flt32bfmt(sb, f32_val, MNormal, 0)
+ | `Tyflt64:
+ f64_val = vanext(ap)
+ flt64bfmt(sb, f64_val, MNormal, 0)
+ | `Tyvalist:
+ sbputs(sb, "...")
+
+ /* compound types */
+ | `Typtr desc:
+ p_val = vanext(ap)
+ sbputs(sb, "0x")
+ intfmt(sb, \
+ [.base=16, .padto=2*sizeof(void#), .padfill='0'], \
+ false, p_val castto(intptr))
+ | `Tyslice desc:
+ match typedesc(desc)
+ | `Tybyte:
+ s_val = vanext(ap)
+ strfmt(sb, s_val, params)
+ | _:
+ sbputs(sb, "slice[:]")
+ ;;
+ | `Tyfunc tc:
+ p_val = vanext(ap)
+ sbputs(sb, "func{")
+ intfmt(sb, \
+ [.base=16, .padto=2*sizeof(void#), .padfill='0'], \
+ false, p_val castto(intptr))
+ sbputs(sb, "}")
+ | `Tyarray (sz, data):
+ sbputs(sb, "array")
+ /* aggregate types */
+ | `Tytuple typecursor:
+ vabytes(ap)
+ sbputs(sb, "tuple")
+ | `Tystruct namecursor:
+ vabytes(ap)
+ sbputs(sb, "struct")
+ | `Tyunion namecursor:
+ vabytes(ap)
+ sbputs(sb, "union")
+ | `Tyname (name, desc):
+ fallbackfmt(sb, params, desc, ap)
+ ;;
+}
+
+const getparams = {fmt
+ var i
+
+ for i = 0; i < fmt.len; i++
+ if fmt[i] == '}' castto(byte)
+ goto foundparams
+ ;;
+ ;;
+ die("invalid format string")
+:foundparams
+ -> (fmt[:i], fmt[i+1:])
+}
+
+type intparams = struct
+ base : size
+ padto : size
+ padfill : char
+;;
+
+const intparams = {params
+ var ip : intparams
+ var opts
+
+ ip = [
+ .base = 10,
+ .padfill = ' ',
+ .padto = 0
+ ]
+
+ opts = parseparams(params, [
+ ("x", false),
+ ("w", true),
+ ("p", true)][:])
+ for o in opts
+ match o
+ | ("x", ""): ip.base = 16
+ | ("w", wid): ip.padto = getint(wid, "fmt: width must be integer")
+ | ("p", pad): ip.padfill = decode(pad)
+ | _: std.die("unreachable")
+ ;;
+ ;;
+ std.assert(ip.padto >= 0, "pad must be >= 0")
+ std.slfree(opts)
+ -> ip
+}
+
+const strfmt = {sb, str, params
+ var opts
+ var w, p, i
+
+ p = ' '
+ w = 0
+ opts = parseparams(params, [
+ ("w", true),
+ ("p", true)][:])
+ for o in opts
+ match o
+ | ("w", wid): w = getint(wid, "fmt: width must be integer")
+ | ("p", pad): p = decode(pad)
+ | _: std.die("unreachable")
+ ;;
+ ;;
+ std.assert(p >= 0, "pad must be >= 0")
+ std.slfree(opts)
+ for i = 0; i < w - graphemewidth(str); i++
+ sbputc(sb, p)
+ ;;
+ sbputs(sb, str)
+}
+
+/*
+Hah. like we're going to put in the work to actually
+count graphemes.
+*/
+const graphemewidth = {str
+ -> str.len
+}
+
+const digitchars = [
+ '0','1','2','3','4',
+ '5','6','7','8','9',
+ 'a','b','c','d','e','f'
+]
+generic intfmt = {sb, opts, signed, bits : @a::(integral,numeric)
+ var isneg
+ var val
+ var b : char[32]
+ var i, j, npad
+ var base
+
+ base = opts.base castto(uint64)
+ if signed && bits < 0
+ val = -bits castto(uint64)
+ isneg = true
+ else
+ val = bits castto(uint64)
+ val &= ~0 >> (8*(sizeof(uint64)-sizeof(@a)))
+ isneg = false
+ ;;
+
+ i = 0
+ if val == 0
+ b[0] = '0'
+ i++
+ ;;
+ while val != 0
+ b[i] = digitchars[val % base]
+ val /= base
+ i++
+ ;;
+
+ npad = clamp(opts.padto - i, 0, opts.padto)
+ if isneg
+ npad--
+ ;;
+ if opts.padfill == '0' && isneg
+ sbputc(sb, '-')
+ ;;
+ for j = 0; j < npad; j++
+ sbputc(sb, opts.padfill)
+ ;;
+ if opts.padfill != '0' && isneg
+ sbputc(sb, '-')
+ ;;
+ for j = i; j != 0; j--
+ sbputc(sb, b[j - 1])
+ ;;
+}
+
+const writeall = {fd, buf
+ var n, len
+
+ len = 0
+ while true
+ n = write(fd, buf)
+ if n <= 0 || n >= len
+ break
+ ;;
+ len += n
+ ;;
+ -> len
+}
+
+
+/* would use std.get(), but that's a dependency loop */
+const getint = {s, msg
+ match std.intparse(s)
+ | `Some w: -> w;
+ | `None: die(msg)
+ ;;
+}
diff --git a/lib/std/fmtfuncs.myr b/lib/std/fmtfuncs.myr
new file mode 100644
index 0000000..abe5e75
--- /dev/null
+++ b/lib/std/fmtfuncs.myr
@@ -0,0 +1,54 @@
+use "alloc.use"
+use "bigint.use"
+use "bitset.use"
+use "fmt.use"
+use "introspect.use"
+use "strbuf.use"
+use "varargs.use"
+
+pkg std =
+;;
+
+const __init__ = {
+ var bigint : bigint#
+ var bitset : bitset#
+
+ fmtinstall(typeof(bigint), bigfmt, [][:])
+ fmtinstall(typeof(bitset), bsfmt, [][:])
+}
+
+const bigfmt = {sb, ap, opts
+ var a, n, buf
+
+ a = vanext(ap)
+ /*
+ allocate a buffer guaranteed to be big enough.
+ that's
+ 2 + floor(nbits/(log_2(10)))
+ or
+ 2 + a.dig.len * 32/3.32...
+ or
+ 2 + a.dig.len * 10
+ plus one for the - sign.
+ */
+ buf = slalloc(3 + a.dig.len * 10)
+ n = bigbfmt(buf, a, 10)
+ sbputs(sb, buf[:n])
+ slfree(buf)
+}
+
+const bsfmt = {sb, ap, opts
+ var i, bs, sep
+
+ bs = vanext(ap)
+ sep = ""
+ sbputs(sb, "bitset{")
+ for i = 0; i < bsmax(bs); i++
+ if bshas(bs, i)
+ sbfmt(sb, sep)
+ sbfmt(sb, "{}", i)
+ sep = ", "
+ ;;
+ ;;
+ sbputs(sb, "}")
+}
diff --git a/lib/std/getcwd.myr b/lib/std/getcwd.myr
new file mode 100644
index 0000000..c9dbbea
--- /dev/null
+++ b/lib/std/getcwd.myr
@@ -0,0 +1,31 @@
+
+use "alloc.use"
+use "errno.use"
+use "extremum.use"
+use "syswrap.use"
+use "syswrap-ss.use"
+pkg std =
+ const getcwd : (-> byte[:])
+;;
+
+const getcwd = {
+ var len, n, buf
+
+ len = 128
+ while true
+ buf = std.slalloc(len)
+ n = bgetcwd(buf)
+ if n >= 0
+ /* n is the length of the nul terminated c string */
+ -> buf[:n]
+ elif n != Erange
+ std.slfree(buf)
+ -> ""
+ else
+ len *= 2
+ ;;
+ ;;
+ /* unreachable; shut up return without value analysis */
+ -> ""
+}
+
diff --git a/lib/std/getint.myr b/lib/std/getint.myr
new file mode 100644
index 0000000..cd3d50b
--- /dev/null
+++ b/lib/std/getint.myr
@@ -0,0 +1,64 @@
+pkg std =
+ generic getle64 : (buf : byte[:] -> @a::(numeric,integral))
+ generic getbe64 : (buf : byte[:] -> @a::(numeric,integral))
+ generic getle32 : (buf : byte[:] -> @a::(numeric,integral))
+ generic getbe32 : (buf : byte[:] -> @a::(numeric,integral))
+ generic getle16 : (buf : byte[:] -> @a::(numeric,integral))
+ generic getbe16 : (buf : byte[:] -> @a::(numeric,integral))
+ generic getle8 : (buf : byte[:] -> @a::(numeric,integral))
+ generic getbe8 : (buf : byte[:] -> @a::(numeric,integral))
+;;
+
+generic getbe64 = {buf -> @a::(numeric,integral)
+ -> ((buf[0] castto(@a::(numeric,integral))) << 56) | \
+ ((buf[1] castto(@a::(numeric,integral))) << 48) | \
+ ((buf[2] castto(@a::(numeric,integral))) << 40) | \
+ ((buf[3] castto(@a::(numeric,integral))) << 32) | \
+ ((buf[4] castto(@a::(numeric,integral))) << 24) | \
+ ((buf[5] castto(@a::(numeric,integral))) << 16) | \
+ ((buf[6] castto(@a::(numeric,integral))) << 8) | \
+ ((buf[7] castto(@a::(numeric,integral))) << 0)
+}
+
+generic getle64 = {buf
+ -> ((buf[0] castto(@a::(numeric,integral))) << 0) | \
+ ((buf[1] castto(@a::(numeric,integral))) << 8) | \
+ ((buf[2] castto(@a::(numeric,integral))) << 16) | \
+ ((buf[3] castto(@a::(numeric,integral))) << 24) | \
+ ((buf[4] castto(@a::(numeric,integral))) << 32) | \
+ ((buf[5] castto(@a::(numeric,integral))) << 40) | \
+ ((buf[6] castto(@a::(numeric,integral))) << 48) | \
+ ((buf[7] castto(@a::(numeric,integral))) << 56)
+}
+
+generic getbe32 = {buf
+ -> ((buf[0] castto(@a::(numeric,integral))) << 24) | \
+ ((buf[1] castto(@a::(numeric,integral))) << 16) | \
+ ((buf[2] castto(@a::(numeric,integral))) << 8) | \
+ ((buf[3] castto(@a::(numeric,integral))) << 0)
+}
+
+generic getle32 = {buf
+ -> ((buf[0] castto(@a::(numeric,integral))) << 0) | \
+ ((buf[1] castto(@a::(numeric,integral))) << 8) | \
+ ((buf[2] castto(@a::(numeric,integral))) << 16) | \
+ ((buf[3] castto(@a::(numeric,integral))) << 24)
+}
+
+generic getbe16 = {buf
+ -> ((buf[0] castto(@a::(numeric,integral))) << 8) | \
+ ((buf[1] castto(@a::(numeric,integral))) << 0)
+}
+
+generic getle16 = {buf
+ -> ((buf[0] castto(@a::(numeric,integral))) << 0) | \
+ ((buf[1] castto(@a::(numeric,integral))) << 8)
+}
+
+generic getbe8 = {buf
+ -> (buf[0] castto(@a::(numeric,integral))) << 0
+}
+
+generic getle8 = {buf
+ -> (buf[0] castto(@a::(numeric,integral))) << 0
+}
diff --git a/lib/std/hashfuncs.myr b/lib/std/hashfuncs.myr
new file mode 100644
index 0000000..00c44c4
--- /dev/null
+++ b/lib/std/hashfuncs.myr
@@ -0,0 +1,106 @@
+use "die.use"
+use "sleq.use"
+use "types.use"
+
+pkg std =
+ const strhash : (s : byte[:] -> uint32)
+ const streq : (a : byte[:], b : byte[:] -> bool)
+
+ generic ptrhash : (p : @a# -> uint32)
+ generic ptreq : (a : @a#, b : @a# -> bool)
+
+ generic inthash : (v : @a::(integral,numeric) -> uint32)
+ generic inteq : (a : @a::(integral,numeric), b : @a::(integral,numeric) -> bool)
+
+ const murmurhash2 : (data : byte[:], seed : uint32 -> uint32)
+
+ generic slhash : (sl : @a[:] -> uint32)
+ generic tobytes : (sl : @a[:] -> byte[:])
+;;
+
+const Seed = 1234
+
+generic slhash = {data : @a[:]
+ -> strhash(slbytes(data))
+}
+
+generic slbytes = {data : @a[:]
+ var n
+
+ n = data.len * sizeof(@a)
+ -> (data castto(byte#))[:n]
+}
+
+/* Supremely simple djb hash. */
+const strhash = {s
+ -> murmurhash2(s, Seed)
+}
+
+const streq = {a, b
+ -> sleq(a, b)
+}
+
+generic ptrhash = {p : @a#
+ var x
+
+ x = &p castto(byte#)
+ -> murmurhash2(x[0:sizeof(@a)], Seed)
+}
+
+generic ptreq = {a, b
+ -> a == b
+}
+
+generic inthash = {v : @a::(integral,numeric)
+ var p
+
+ p = &v castto(byte#)
+ -> murmurhash2(p[0:sizeof(@a)], Seed)
+}
+
+generic inteq = {a, b
+ -> a == b
+}
+
+const murmurhash2 = {data, seed
+ const m = 0x5bd1e995;
+ const r = 24
+ var h, k
+
+ h = seed ^ data.len
+ while data.len >= 4
+ k = (data[0] castto(uint32))
+ k |= (data[1] castto(uint32)) << 8
+ k |= (data[2] castto(uint32)) << 16
+ k |= (data[3] castto(uint32)) << 24
+
+ k *= m
+ k ^= k >> r
+ k *= m
+
+ h *= m
+ h ^= k
+ data = data[4:]
+ ;;
+
+ match data.len
+ | 3:
+ h ^= (data[2] castto(uint32)) << 16
+ h ^= (data[1] castto(uint32)) <<8
+ h ^= (data[0] castto(uint32))
+ | 2:
+ h ^= (data[1] castto(uint32)) <<8
+ h ^= (data[0] castto(uint32))
+ | 1:
+ h ^= (data[0] castto(uint32))
+ | 0: /* nothing */
+ | _: die("0 < len < 4 must be true")
+ ;;
+ h *= m
+
+ h ^= h >> 13
+ h *= m
+ h ^= h >> 15
+
+ -> h
+}
diff --git a/lib/std/hasprefix.myr b/lib/std/hasprefix.myr
new file mode 100644
index 0000000..c9749f6
--- /dev/null
+++ b/lib/std/hasprefix.myr
@@ -0,0 +1,12 @@
+use "cmp.use"
+pkg std =
+ const hasprefix : (s : byte[:], pre : byte[:] -> bool)
+;;
+
+const hasprefix = {s, pre
+ match strncmp(s, pre, pre.len)
+ | `Equal: -> true
+ | _: -> false
+ ;;
+}
+
diff --git a/lib/std/hassuffix.myr b/lib/std/hassuffix.myr
new file mode 100644
index 0000000..87369af
--- /dev/null
+++ b/lib/std/hassuffix.myr
@@ -0,0 +1,15 @@
+use "sleq.use"
+pkg std =
+ const hassuffix : (s : byte[:], suff : byte[:] -> bool)
+;;
+
+const hassuffix = {s, suff
+ var tail
+
+ if suff.len <= s.len
+ tail = s[s.len - suff.len:]
+ -> sleq(tail, suff)
+ ;;
+ -> false
+}
+
diff --git a/lib/std/htab.myr b/lib/std/htab.myr
new file mode 100644
index 0000000..ac2e7aa
--- /dev/null
+++ b/lib/std/htab.myr
@@ -0,0 +1,209 @@
+use "alloc.use"
+use "die.use"
+use "extremum.use"
+use "option.use"
+use "types.use"
+
+pkg std =
+ type htab(@k, @v) = struct
+ hash : (k : @k -> uint32)
+ eq : (a : @k, b : @k -> bool)
+
+ nelt : size
+ ndead : size
+ keys : @k[:]
+ vals : @v[:]
+ hashes : uint32[:]
+ dead : bool[:]
+ ;;
+
+ generic mkht : (h : (k : @k -> uint32), eq : (a : @k, b : @k -> bool) -> htab(@k, @v)#)
+ generic htfree : (ht : htab(@k, @v)# -> void)
+ generic htput : (ht : htab(@k, @v)#, k : @k, v : @v -> void)
+ generic htdel : (ht : htab(@k, @v)#, k : @k -> void)
+ generic htget : (ht : htab(@k, @v)#, k : @k -> option(@v))
+ generic htgetv : (ht : htab(@k, @v)#, k : @k, fallback : @v-> @v)
+ generic hthas : (ht : htab(@k, @v)#, k : @k -> bool)
+ generic htkeys : (ht : htab(@k, @v)# -> @k[:])
+;;
+
+const Initsz = 32
+
+extern const put : (fmt : byte[:], args : ... -> size)
+
+generic hash = {ht, k
+ var h
+
+ h = ht.hash(k)
+ if h == 0
+ -> 1
+ else
+ -> h
+ ;;
+}
+
+generic resize = {ht, sz
+ var oldk
+ var oldv
+ var oldh
+ var oldd
+ var i
+
+ oldk = ht.keys
+ oldv = ht.vals
+ oldh = ht.hashes
+ oldd = ht.dead
+ ht.keys = slalloc(sz)
+ ht.vals = slalloc(sz)
+ ht.hashes = slzalloc(sz)
+ ht.dead = slzalloc(sz)
+
+ ht.nelt = 0
+ ht.ndead = 0
+ for i = 0; i < oldk.len; i++
+ if oldh[i] != 0 && !oldd[i]
+ htput(ht, oldk[i], oldv[i])
+ ;;
+ ;;
+ slfree(oldk)
+ slfree(oldv)
+ slfree(oldh)
+ slfree(oldd)
+}
+
+generic idx = {ht, k
+ var i, di
+ var h
+
+ di = 0
+ h = hash(ht, k)
+ i = h & (ht.keys.len - 1)
+ while true
+ while ht.hashes[i] != 0 && !ht.dead[i] && ht.hashes[i] != h
+ di++
+ i = (h + di) & (ht.keys.len - 1)
+ ;;
+
+ if ht.hashes[i] == 0
+ -> `None
+ ;;
+ if ht.hashes[i] == h && !ht.dead[i] && ht.eq(ht.keys[i], k)
+ break
+ ;;
+ di++
+ i = (h + di) & (ht.keys.len - 1)
+ ;;
+ -> `Some i
+}
+
+generic mkht = {h, eq
+ var ht
+
+ ht = alloc()
+
+ ht.hash = h
+ ht.eq = eq
+
+ ht.nelt = 0
+ ht.ndead = 0
+ ht.keys = slalloc(Initsz)
+ ht.vals = slalloc(Initsz)
+ ht.hashes = slzalloc(Initsz)
+ ht.dead = slzalloc(Initsz)
+ -> ht
+}
+
+generic htfree = {ht
+ slfree(ht.keys)
+ slfree(ht.vals)
+ slfree(ht.hashes)
+ slfree(ht.dead)
+ free(ht)
+}
+
+generic htput = {ht, k, v
+ var i, di
+ var h
+ var neltincr
+
+ di = 0
+ h = hash(ht, k)
+ i = h & (ht.keys.len - 1)
+ neltincr = 1
+ while ht.hashes[i] != 0 && !ht.dead[i]
+ /* second insertion overwrites */
+ if ht.hashes[i] == h && !ht.dead[i]
+ /* dead key, we can just insert here */
+ if ht.dead[i]
+ break
+ /* replacing a key */
+ elif ht.eq(ht.keys[i], k)
+ neltincr = 0
+ break
+ ;;
+ ;;
+ di++
+ i = (h + di) & (ht.keys.len - 1)
+ ;;
+ ht.nelt += neltincr
+ ht.hashes[i] = h
+ ht.keys[i] = k
+ ht.vals[i] = v
+ ht.dead[i] = false
+ if ht.keys.len < ht.nelt * 2
+ resize(ht, 2*ht.keys.len)
+ ;;
+}
+
+generic htdel = {ht, k
+ match idx(ht, k)
+ | `Some i:
+ ht.dead[i] = true
+ ht.nelt--
+ ht.ndead++
+ std.put("ndead = {}\n", ht.ndead)
+ /* remove tombstones if we shrink enough */
+ if ht.keys.len < ht.ndead * 4
+ resize(ht, ht.keys.len)
+ ;;
+ | _:
+ /* do nothing */
+ ;;
+}
+
+generic htget = {ht, k
+ match idx(ht, k)
+ | `Some i: -> `Some ht.vals[i]
+ | `None: -> `None
+ ;;
+}
+
+generic htgetv = {ht, k, v
+ match idx(ht, k)
+ | `Some i: -> ht.vals[i]
+ | `None: -> v
+ ;;
+}
+
+generic hthas = {ht, k
+ match idx(ht, k)
+ | `Some i: -> true
+ | `None: -> false
+ ;;
+}
+
+generic htkeys = {ht
+ var keys
+ var i
+ var j
+
+ keys = slalloc(ht.nelt)
+ j = 0
+ for i = 0; i < ht.keys.len; i++
+ if ht.hashes[i] != 0 && !ht.dead[i]
+ keys[j++] = ht.keys[i]
+ ;;
+ ;;
+ -> keys
+}
+
diff --git a/lib/std/ifreq+freebsd.myr b/lib/std/ifreq+freebsd.myr
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/lib/std/ifreq+freebsd.myr
diff --git a/lib/std/ifreq+linux.myr b/lib/std/ifreq+linux.myr
new file mode 100644
index 0000000..62be8ee
--- /dev/null
+++ b/lib/std/ifreq+linux.myr
@@ -0,0 +1,67 @@
+use "sys.use"
+
+pkg sys =
+ const Ifnamesz = 16
+
+ type ifreq_addr = struct
+ name : byte[Ifnamesz]
+ addr : sockaddr
+ ;;
+
+ type ifreq_dstaddr = struct
+ name : byte[Ifnamesz]
+ dstaddr : sockaddr
+ ;;
+
+ type ifreq_broadaddr = struct
+ name : byte[Ifnamesz]
+ broadaddr : sockaddr
+ ;;
+
+ type ifreq_netmask = struct
+ name : byte[Ifnamesz]
+ netmask : sockaddr
+ ;;
+
+
+ type ifreq_hwaddr = struct
+ name : byte[Ifnamesz]
+ hwaddr : sockaddr
+ ;;
+
+ type ifreq_flags = struct
+ name : byte[Ifnamesz]
+ flags : int16
+ ;;
+
+ type ifreq_ifindex = struct
+ name : byte[Ifnamesz]
+ index : int32
+ ;;
+
+ type ifreq_metric = struct
+ name : byte[Ifnamesz]
+ metric : int32
+ ;;
+
+
+ type ifreq_mtu = struct
+ name : byte[Ifnamesz]
+ mtu : int32
+ ;;
+
+ type ifreq_slave = struct
+ name : byte[Ifnamesz]
+ slave : byte[Ifnamesz]
+ ;;
+
+ type ifreq_newname = struct
+ name : byte[Ifnamesz]
+ newname : byte[Ifnamesz]
+ ;;
+
+ type ifreq_data = struct
+ name : byte[Ifnamesz]
+ data : void#
+ ;;
+;;
diff --git a/lib/std/ifreq+osx.myr b/lib/std/ifreq+osx.myr
new file mode 100644
index 0000000..f5205bf
--- /dev/null
+++ b/lib/std/ifreq+osx.myr
@@ -0,0 +1,77 @@
+use "sys.use"
+
+pkg sys =
+ const Ifnamesz = 16
+
+ type ifreq_addr = struct
+ name : byte[Ifnamesz]
+ addr : sockaddr
+ ;;
+
+ type ifreq_dstaddr = struct
+ name : byte[Ifnamesz]
+ dstaddr : sockaddr
+ ;;
+
+ type ifreq_broadaddr = struct
+ name : byte[Ifnamesz]
+ broadaddr : sockaddr
+ ;;
+
+ type ifreq_flags = struct
+ name : byte[Ifnamesz]
+ flags : int16
+ ;;
+
+ type ifreq_metric = struct
+ name : byte[Ifnamesz]
+ metric : int32
+ ;;
+
+
+ type ifreq_phys = struct
+ name : byte[Ifnamesz]
+ phys : int32
+ ;;
+
+ type ifreq_media = struct
+ name : byte[Ifnamesz]
+ media : int32
+ ;;
+
+ type ifreq_data = struct
+ name : byte[Ifnamesz]
+ data : void#
+ ;;
+
+ type ifreq_devmtu = struct
+ name : byte[Ifnamesz]
+ cur : uint32
+ min : uint32
+ max : uint32
+ ;;
+
+ type ifreq_kpi = struct
+ name : byte[Ifnamesz]
+ modid : uint32
+ typeid : uint32
+ ptr : void#
+ ;;
+
+ type ifreq_wakeflg = struct
+ name : byte[Ifnamesz]
+ wakeflg : uint32
+ ;;
+
+ type ifreq_routerefs = struct
+ name : byte[Ifnamesz]
+ refs : uint32
+ ;;
+
+ type ifreq_icaps = struct
+ name : byte[Ifnamesz]
+ req : uint32
+ cur : uint32
+ ;;
+
+;;
diff --git a/lib/std/ifreq+plan9.myr b/lib/std/ifreq+plan9.myr
new file mode 100644
index 0000000..ef73843
--- /dev/null
+++ b/lib/std/ifreq+plan9.myr
@@ -0,0 +1,2 @@
+pkg std =
+;;
diff --git a/lib/std/intparse.myr b/lib/std/intparse.myr
new file mode 100644
index 0000000..4180f8c
--- /dev/null
+++ b/lib/std/intparse.myr
@@ -0,0 +1,70 @@
+use "chartype.use"
+use "die.use"
+use "hasprefix.use"
+use "option.use"
+use "types.use"
+use "utf.use"
+
+pkg std =
+ generic intparsebase : (s : byte[:], base : int -> option(@a::(integral,numeric)))
+ generic intparse : (s : byte[:] -> option(@a::(integral,numeric)))
+;;
+
+generic intparse = {s
+ var isneg
+
+ isneg = false
+ if hasprefix(s, "-")
+ s = s[1:]
+ isneg = true
+ ;;
+
+ if hasprefix(s, "0x")
+ -> doparse(s[2:], isneg, 16)
+ elif hasprefix(s, "0o")
+ -> doparse(s[2:], isneg, 8)
+ elif hasprefix(s, "0b")
+ -> doparse(s[2:], isneg, 2)
+ else
+ -> doparse(s, isneg, 10)
+ ;;
+}
+
+generic intparsebase = {s, base
+ var isneg
+
+ isneg = false
+ if hasprefix(s, "-")
+ s = s[1:]
+ isneg = true
+ ;;
+
+ -> doparse(s, isneg, base)
+}
+
+generic doparse = {s, isneg, base
+ var c
+ var v
+ var cv : int32
+
+ v = 0
+ while s.len != 0
+ (c, s) = striter(s)
+ if c == '_'
+ continue
+ ;;
+ cv = charval(c, base)
+ if cv >= 0
+ v *= (base castto(@a::(integral,numeric)))
+ v += cv castto(@a::(integral,numeric))
+ else
+ -> `None
+ ;;
+ ;;
+
+ if isneg
+ -> `Some -v
+ else
+ -> `Some v
+ ;;
+}
diff --git a/lib/std/introspect.myr b/lib/std/introspect.myr
new file mode 100644
index 0000000..1da7af0
--- /dev/null
+++ b/lib/std/introspect.myr
@@ -0,0 +1,365 @@
+use "types.use"
+use "die.use"
+
+pkg std =
+ type typedesc = union
+ `Tynone
+
+ /* atomic types */
+ `Tyvoid
+ `Tybool
+ `Tychar
+
+ `Tyint8
+ `Tyint16
+ `Tyint
+ `Tyint32
+ `Tyint64
+ `Tylong
+
+ `Tybyte
+ `Tyuint8
+ `Tyuint16
+ `Tyuint
+ `Tyuint32
+ `Tyuint64
+ `Tyulong
+ `Tyflt32
+ `Tyflt64
+ `Tyvalist
+
+ /* compound types */
+ `Typtr byte[:]
+ `Tyfunc typecursor
+ `Tyslice byte[:]
+ `Tyarray (size, byte[:])
+
+ /* aggregate types */
+ `Tytuple typecursor
+ `Tystruct namecursor
+ `Tyunion namecursor
+ /* name info */
+ `Tyname (byte[:], byte[:])
+ ;;
+
+ type typecursor = struct
+ nelt : size
+ rem : byte[:]
+ ;;
+
+ type namecursor = struct
+ nelt : size
+ rem : byte[:]
+ ;;
+
+ type typeinfo = struct
+ size : size
+ align : size
+ ;;
+
+ generic typeof : (v : @a -> byte[:])
+ const typeenc : (p : ...# -> typecursor)
+ const typedecode : (e : byte[:] -> typedesc)
+ const typedesc : (e : byte[:] -> typedesc)
+ const typeinfo : (e : byte[:] -> typeinfo)
+
+ const tcnext : (t : typecursor# -> byte[:])
+ const tcpeek : (t : typecursor# -> byte[:])
+ const ncnext : (t : namecursor# -> (byte[:], byte[:]))
+;;
+
+extern const put : (fmt : byte[:], args : ... -> size)
+
+const Encnone : byte = 0
+const Encvoid : byte = 1
+const Encbool : byte = 2
+const Encchar : byte = 3
+
+const Encint8 : byte = 4
+const Encint16 : byte = 5
+const Encint : byte = 6
+const Encint32 : byte = 7
+const Encint64 : byte = 8
+const Enclong : byte = 9
+
+const Encbyte : byte = 10
+const Encuint8 : byte = 11
+const Encuint16 : byte = 12
+const Encuint : byte = 13
+const Encuint32 : byte = 14
+const Encuint64 : byte = 15
+const Enculong : byte = 16
+const Encflt32 : byte = 17
+const Encflt64 : byte = 18
+const Encvalist : byte = 19
+
+/* compound types */
+const Encptr : byte = 20
+const Encfunc : byte = 21
+const Encslice : byte = 22
+const Encarray : byte = 23
+
+/* aggregate types */
+const Enctuple : byte = 24
+const Encstruct : byte = 25
+const Encunion : byte = 26
+const Encname : byte = 30
+const Encindname :byte = 30 | 0x80
+
+generic typeof = {v : @a -> byte[:]
+ var tc
+
+ tc = typesof(v)
+ -> tcnext(&tc)
+}
+
+const typeenc = {ap : ...#
+ var e
+
+ e = getenc(ap castto(byte##))
+ e = skiptypeinfo(e[1:])
+ -> lentypecursor(e)
+}
+
+const typesof : (a : ... -> typecursor) = {a : ...
+ -> typeenc(&a)
+}
+
+const tcnext = {tc
+ var n, sz, cur
+
+ if tc.rem.len == 0
+ -> ""
+ ;;
+ (n, sz) = getipacked(tc.rem)
+ cur = tc.rem[sz:sz+n]
+ tc.rem = tc.rem[sz+n:]
+ -> cur
+}
+
+const tcpeek = {tc
+ var n, sz
+
+ if tc.rem.len == 0
+ -> ""
+ ;;
+ (n, sz) = getipacked(tc.rem)
+ -> tc.rem[sz:sz+n]
+}
+
+const ncnext = {nc
+ var n, sz, name, enc
+
+ if nc.rem.len == 0
+ -> ("", "")
+ ;;
+
+ /* get the name */
+ (n, sz) = getipacked(nc.rem)
+ name = nc.rem[sz:sz+n]
+ nc.rem = nc.rem[sz+n:]
+
+ /* and the type */
+ (n, sz) = getipacked(nc.rem)
+ enc = nc.rem[sz:sz+n]
+ nc.rem = nc.rem[sz+n:]
+ -> (name, enc)
+}
+
+
+const getenc = {p : byte##
+ var val, sz, x
+
+ (val, sz) = getipacked(p#[:8])
+ x = &sz castto(byte#)
+ -> p#[sz:sz+val]
+}
+
+const typedesc = {ti
+ var len,sz, p
+
+ match ti[0]
+ | Encnone: -> `Tynone
+ | Encvoid: -> `Tyvoid
+ | Encbool: -> `Tybool
+ | Encchar: -> `Tychar
+
+ | Encint8: -> `Tyint8
+ | Encint16: -> `Tyint16
+ | Encint: -> `Tyint
+ | Encint32: -> `Tyint32
+ | Encint64: -> `Tyint64
+ | Enclong: -> `Tylong
+
+ | Encbyte: -> `Tybyte
+ | Encuint8: -> `Tyuint8
+ | Encuint16: -> `Tyuint16
+ | Encuint: -> `Tyuint
+ | Encuint32: -> `Tyuint32
+ | Encuint64: -> `Tyuint64
+ | Enculong: -> `Tyulong
+ | Encflt32: -> `Tyflt32
+ | Encflt64: -> `Tyflt64
+ | Encvalist: -> `Tyvalist
+
+ /* compound types */
+ | Encptr: -> `Typtr getsub(ti[1:])
+ | Encfunc: -> `Tyfunc lentypecursor(ti[1:])
+ | Encslice: -> `Tyslice getsub(ti[1:])
+ | Encarray:
+ ti = skiptypeinfo(ti[1:])
+ (len, sz) = getipacked(ti)
+ -> `Tyarray (len, getsub(ti[sz:]))
+
+
+ /* aggregate types */
+ | Enctuple:
+ ti = skiptypeinfo(ti[1:])
+ -> `Tytuple lentypecursor(ti)
+ | Encstruct:
+ ti = skiptypeinfo(ti[1:])
+ -> `Tystruct lennamecursor(ti)
+ | Encunion:
+ ti = skiptypeinfo(ti[1:])
+ -> `Tyunion lennamecursor(ti)
+ | Encname:
+ -> `Tyname namedesc(ti[1:])
+ | Encindname:
+ /*
+ ugly hack: the slice contains a pointer to the
+ value, so if we cast it to a byte##, we can
+ pull the indirect value out of the pointer.
+ */
+ p = ti[1:] castto(byte##)
+ -> typedesc(getenc(p))
+ | _:
+ std.die("unknown type encoding")
+ ;;
+}
+
+const typeinfo = {ti
+ var p
+
+ match ti[0]
+ | Encnone: -> [.size=0, .align=1]
+ | Encvoid: -> [.size=0, .align=1]
+ | Encbool: -> [.size=0, .align=1]
+ | Encchar: -> [.size=4, .align=4]
+
+ | Encint8: -> [.size=1, .align=1]
+ | Encint16: -> [.size=2, .align=2]
+ | Encint: -> [.size=4, .align=4]
+ | Encint32: -> [.size=4, .align=4]
+ | Encint64: -> [.size=8, .align=8]
+ | Enclong: -> [.size=8, .align=8]
+
+ | Encbyte: -> [.size=1, .align=1]
+ | Encuint8: -> [.size=1, .align=1]
+ | Encuint16: -> [.size=2, .align=2]
+ | Encuint: -> [.size=4, .align=4]
+ | Encuint32: -> [.size=4, .align=4]
+ | Encuint64: -> [.size=8, .align=8]
+ | Enculong: -> [.size=8, .align=8]
+ | Encflt32: -> [.size=4, .align=4]
+ | Encflt64: -> [.size=8, .align=8]
+ | Encvalist: -> [.size=8, .align=8]
+
+ /* compound types */
+ | Encptr: -> [.size=8, .align=8]
+ | Encfunc: -> [.size=8, .align=8]
+ | Encslice: -> [.size=16, .align=8]
+
+ | Encarray: -> gettypeinfo(ti[1:])
+ | Enctuple: -> gettypeinfo(ti[1:])
+ | Encstruct: -> gettypeinfo(ti[1:])
+ | Encunion: -> gettypeinfo(ti[1:])
+ | Encname: -> getnameinfo(ti[1:])
+ | Encindname:
+ p = ti[1:] castto(byte##)
+ -> typeinfo(getenc(p))
+ | _:
+ std.die("unknown type encoding")
+ ;;
+}
+
+const gettypeinfo = {e
+ var size, align, sz
+
+ (size, sz) = getipacked(e) /* size */
+ e = e[sz:]
+ (align, sz) = getipacked(e) /* align */
+ -> [.size = size, .align = align]
+}
+
+const skiptypeinfo = {e
+ var ignore, sz
+
+ (ignore, sz) = getipacked(e) /* size */
+ e = e[sz:]
+ (ignore, sz) = getipacked(e) /* align */
+ -> e[sz:]
+}
+
+const getnameinfo = {e
+ var n, name, sz, enc
+
+ (n, sz) = getipacked(e)
+ name = e[sz:n+sz]
+ e = e[n+sz:]
+ (n, sz) = getipacked(e)
+ enc = e[sz:n+sz]
+
+ -> typeinfo(enc)
+}
+
+const namedesc = {e
+ var n, sz, name, enc
+
+ (n, sz) = getipacked(e)
+ name = e[sz:n+sz]
+ e = e[n+sz:]
+ (n, sz) = getipacked(e)
+ enc = e[sz:n+sz]
+
+ -> (name, enc)
+}
+
+const lentypecursor = {e
+ var n, sz
+
+ (n, sz) = getipacked(e)
+ -> [.nelt=n, .rem=e[sz:]]
+}
+
+const lennamecursor = {e
+ var n, sz
+
+ (n, sz) = getipacked(e)
+ -> [.nelt=n, .rem=e[sz:]]
+}
+
+const getsub = {e
+ var n, sz
+
+ (n, sz) = getipacked(e)
+ -> e[sz:sz+n]
+}
+
+const getipacked : (p : byte[:] -> (size, size)) = {p : byte[:]
+ var mask, val, len, i
+
+ mask = 0x80
+ val = 0
+ len = 1
+ while p[0] & mask != mask << 1
+ len++
+ mask >>= 1
+ mask |= 0x80
+ ;;
+
+ val = (p[0] castto(size)) & ~(1 << (8 - len))
+ for i = 1; i < len; i++
+ val |= (p[i] castto(size)) << (i*8 - len)
+ ;;
+ -> (val, len)
+}
diff --git a/lib/std/ipparse.myr b/lib/std/ipparse.myr
new file mode 100644
index 0000000..17bc6f0
--- /dev/null
+++ b/lib/std/ipparse.myr
@@ -0,0 +1,147 @@
+use "die.use"
+use "intparse.use"
+use "option.use"
+use "strfind.use"
+use "types.use"
+use "chartype.use"
+use "fmt.use"
+use "slcp.use"
+use "slfill.use"
+use "sleq.use"
+
+ /* FIXME: needed for decls which should be pulled in as hidden */
+use "hasprefix.use"
+use "utf.use"
+
+pkg std =
+
+ type netaddr = union
+ `Ipv4 byte[4]
+ `Ipv6 byte[16]
+ ;;
+
+ const ipparse : (ip : byte[:] -> option(netaddr))
+ const ip4parse : (ip : byte[:] -> option(netaddr))
+ const ip6parse : (ip : byte[:] -> option(netaddr))
+;;
+
+const ipparse = {ip
+ match strfind(ip, ":")
+ | `Some _: -> ip6parse(ip)
+ | `None: -> ip4parse(ip)
+ ;;
+}
+
+const ip4parse = {ip
+ var a, b, c, d
+ var ok
+ /*
+ var addr
+ var last : size
+ var x : option(int32)
+ var val : int32 /* need int32 to check for overflow */
+ var i
+ var j : size
+ */
+
+ (a, ip, ok) = num(ip, 0, 255, 10, '.', true)
+ (ip, ok) = delim(ip, '.', ok)
+ (b, ip, ok) = num(ip, 0, 255, 10, '.', ok)
+ (ip, ok) = delim(ip, '.', ok)
+ (c, ip, ok) = num(ip, 0, 255, 10, '.', ok)
+ (ip, ok) = delim(ip, '.', ok)
+ (d, ip, ok) = num(ip, 0, 255, 10, '.', ok)
+
+ if ok && ip.len == 0
+ -> `Some (`Ipv4 [a, b, c, d])
+ else
+ -> `None
+ ;;
+}
+
+const ip6parse = {ip
+ var val : byte[16]
+ var expand, split
+ var v, ok
+ var i, nseg
+
+ ok = true
+ expand = false
+ split = 0
+ if ip.len > 2 && std.sleq(ip[:2], "::")
+ expand = true
+ split = 0
+ ;;
+ nseg = 0
+ for i = 0; ip.len > 0 && ok; i++
+ /* parse 'num' segment */
+ (v, ip, ok) = num(ip, 0, 65536, 16, ':', ok)
+ nseg++
+ if ip.len == 0 || nseg == 8
+ break
+ ;;
+ (ip, ok) = delim(ip, ':', ok)
+ /* only one '::' allowed once */
+ if ip.len > 0 && ip[0] == ':'castto(byte) && !expand
+ expand = true
+ split = i
+ (ip, ok) = delim(ip, ':', ok)
+ ;;
+
+ /* pack it into the bytes */
+ val[i*2] = ((v & 0xff00) >> 8) castto(byte)
+ val[i*2 + 1] = (v & 0xff) castto(byte)
+ ;;
+
+ if ok && ip.len == 0
+ if expand
+ expandsplit(val[:], split, i)
+ elif nseg != 8
+ -> `None
+ ;;
+ -> `Some `Ipv6 val
+ else
+ -> `None
+ ;;
+}
+
+/* take "a:b::c:d" and expand it to "a:b:0:0:...:0:c:d" */
+const expandsplit = {ip, split, len
+ var width
+
+ width = 16 - len
+ std.slcp(ip[split:len], ip[split+width:len+width])
+ std.slfill(ip[len:len+width], 0)
+}
+
+const delim = {ip, sep, ok
+ if ip.len > 0 && ip[0] == sep castto(byte)
+ -> (ip[1:], ok)
+ else
+ -> ("", false)
+ ;;
+}
+
+generic num = {ip, lo, hi, base, sep, ok -> (@a::(numeric,integral), byte[:], bool)
+ var len
+
+ if !ok
+ -> (0, "", false)
+ ;;
+
+ for len = 0; len < ip.len; len++
+ if ip[len] == sep castto(byte)
+ break
+ ;;
+ ;;
+ match intparsebase(ip[:len], base)
+ | `std.Some v:
+ if v < lo || v > hi
+ -> (0, "", false)
+ ;;
+ -> (v castto(@a::(numeric,integral)), ip[len:], true)
+ | `std.None:
+ -> (0, "", false)
+ ;;
+}
+
diff --git a/lib/std/mk.myr b/lib/std/mk.myr
new file mode 100644
index 0000000..19b46d2
--- /dev/null
+++ b/lib/std/mk.myr
@@ -0,0 +1,22 @@
+use "alloc.use"
+
+pkg std =
+ generic mk : (val : @a -> @a#)
+;;
+
+/* Takes a value, and heapifies it.
+
+FIXME: This depends on inlining and copy propagation
+in order to be efficient. Neither of those are
+currently implemented. That means that this function
+is not efficient.
+
+It's still damn convenient, though, so it's in.
+*/
+generic mk = {val
+ var p
+
+ p = alloc()
+ p# = val
+ -> p
+}
diff --git a/lib/std/mkfile b/lib/std/mkfile
new file mode 100644
index 0000000..36c6bb2
--- /dev/null
+++ b/lib/std/mkfile
@@ -0,0 +1,107 @@
+</$objtype/mkfile
+
+SYSLIB=sys
+SYSSRC= \
+ sys.myr \
+ systypes.myr \
+ ifreq.myr \
+
+SYSASMSRC= \
+ syscall.s \
+ util.s
+
+
+STDLIB=std
+STDSRC= \
+ alloc.myr \
+ bigint.myr \
+ bitset.myr \
+ blat.myr \
+ chartype.myr \
+ cmp.myr \
+ dial.myr \
+ die.myr \
+ dir.myr \
+ endian.myr \
+ env.myr \
+ execvp.myr \
+ extremum.myr \
+ fltbits.myr \
+ fmt.myr \
+ fltfmt.myr \
+ hashfuncs.myr \
+ hasprefix.myr \
+ hassuffix.myr \
+ htab.myr \
+ intparse.myr \
+ ipparse.myr \
+ mk.myr \
+ now.myr \
+ option.myr \
+ optparse.myr \
+ pathjoin.myr \
+ rand.myr \
+ resolve.myr \
+ result.myr \
+ search.myr \
+ slcp.myr \
+ sldup.myr \
+ sleq.myr \
+ slfill.myr \
+ sljoin.myr \
+ slpush.myr \
+ slput.myr \
+ slurp.myr \
+ sort.myr \
+ spork.myr \
+ strfind.myr \
+ strjoin.myr \
+ strsplit.myr \
+ strstrip.myr \
+ syswrap.myr \
+ swap.myr \
+ try.myr \
+ types.myr \
+ units.myr \
+ utf.myr \
+ varargs.myr \
+ wait.myr \
+
+all:V: lib$STDLIB.a lib$SYSLIB.a
+
+install:V: all
+ mkdir -p /$objtype/lib/myr/
+ cp lib$STDLIB.a lib$SYSLIB.a /$objtype/lib/myr/
+ cp $STDLIB $SYSLIB /$objtype/lib/myr/
+
+lib$STDLIB.a: $STDSRC $ASMSRC lib$SYSLIB.a
+ ../myrbuild/$O.out -I. -C../6/$O.out -M../muse/$O.out -l $STDLIB $STDSRC $STDASMSRC
+
+lib$SYSLIB.a: $SYSSRC $SYSASMSRC
+ ../myrbuild/$O.out -C../6/$O.out -M../muse/$O.out -l $SYSLIB $SYSSRC $SYSASMSRC
+
+%.myr: %+plan9-x64.myr
+ cp $stem+plan9-x64.myr $stem.myr
+
+%.myr: %+plan9.myr
+ cp $stem+plan9.myr $stem.myr
+
+%.myr: %+x64.myr
+ cp $stem+x64.myr $stem.myr
+
+%.s: %+plan9-x64.s
+ cp $stem+plan9-x64.s $stem.s
+
+%.s: %+x64.s
+ cp $stem+x64.s $stem.s
+
+OBJ=${STDSRC:%.myr=%.$O} ${SYSSRC:%.myr=%.$O} ${STDASMSRC:%.s=%.$O} ${SYSASMSRC:%.s=%.$O}
+USE=${STDSRC:%.myr=%.use} ${SYSSRC:%.myr=%.use} ${STDLIB}
+LIBS=lib$STDLIB.a lib$SYSLIB.a
+.PHONY: clean
+clean:V:
+ rm -f $OBJ
+ rm -f $USE
+ rm -f $LIBS $STDLIB $SYSLIB
+
+nuke:V: clean
diff --git a/lib/std/mkpath.myr b/lib/std/mkpath.myr
new file mode 100644
index 0000000..b7e8e24
--- /dev/null
+++ b/lib/std/mkpath.myr
@@ -0,0 +1,22 @@
+use "syswrap.use"
+use "errno.use"
+
+pkg std =
+ const mkpath : (p : byte[:] -> bool)
+;;
+
+const mkpath = {p
+ var st
+ var i
+
+ for i = 0; i < p.len; i++
+ if p[i] == '/' castto(byte) && i != 0
+ st = mkdir(p[:i], 0o777)
+ if st != 0 && (st castto(errno)) != Eexist
+ -> false
+ ;;
+ ;;
+ ;;
+
+ -> true
+}
diff --git a/lib/std/now.myr b/lib/std/now.myr
new file mode 100644
index 0000000..5f6d8d1
--- /dev/null
+++ b/lib/std/now.myr
@@ -0,0 +1,11 @@
+use "types.use"
+use "syswrap.use"
+
+pkg std =
+ const now : (-> time)
+;;
+
+/* microseconds since epoch */
+const now = {
+ -> curtime()
+}
diff --git a/lib/std/option.myr b/lib/std/option.myr
new file mode 100644
index 0000000..271355d
--- /dev/null
+++ b/lib/std/option.myr
@@ -0,0 +1,7 @@
+pkg std =
+ type option(@a) = union
+ `Some @a
+ `None
+ ;;
+;;
+
diff --git a/lib/std/optparse.myr b/lib/std/optparse.myr
new file mode 100644
index 0000000..ae65b77
--- /dev/null
+++ b/lib/std/optparse.myr
@@ -0,0 +1,210 @@
+use "alloc.use"
+use "die.use"
+use "extremum.use"
+use "fmt.use"
+use "strbuf.use"
+use "option.use"
+use "sleq.use"
+use "slpush.use"
+use "syswrap-ss.use"
+use "syswrap.use"
+use "types.use"
+use "utf.use"
+
+pkg std =
+ type optdef = struct
+ argdesc : byte[:] /* the description for the usage */
+ minargs : std.size /* the minimum number of positional args */
+ maxargs : std.size /* the maximum number of positional args (0 = unlimited) */
+ noargs : std.bool /* whether we accept args at all */
+ opts : optdesc[:] /* the description of the options */
+ ;;
+
+ type optdesc = struct
+ opt : char
+ arg : byte[:]
+ desc : byte[:]
+ optional : bool
+ ;;
+
+ type optparsed = struct
+ opts : (char, byte[:])[:]
+ args : byte[:][:]
+ ;;
+
+ const optparse : (optargs : byte[:][:], def : optdef# -> optparsed)
+ const optusage : (prog : byte[:], def : optdef# -> void)
+;;
+
+type optctx = struct
+ /* public variables */
+ args : byte[:][:]
+
+ /* data passed in */
+ optdef : optdef#
+ optargs : byte[:][:]
+
+ /* state */
+ argidx : size
+ curarg : byte[:]
+ optdone : bool /* if we've seen '--', everything's an arg */
+ finished : bool /* if we've processed all the optargs */
+;;
+
+
+const optparse = {args, def
+ var ctx : optctx
+ var parsed
+
+ parsed = [
+ .opts=[][:],
+ .args=[][:]
+ ]
+ optinit(&ctx, args, def)
+ while !optdone(&ctx)
+ parsed.opts = slpush(parsed.opts, optnext(&ctx))
+ ;;
+ if ctx.args.len < def.minargs
+ put("error: expected at least {} args, got {}\n", def.minargs, ctx.args.len)
+ optusage(ctx.optargs[0], ctx.optdef)
+ exit(1)
+ ;;
+ if def.maxargs > 0 && ctx.args.len < def.minargs
+ put("error: expected at most {} args, got {}\n", def.minargs, ctx.args.len)
+ optusage(ctx.optargs[0], ctx.optdef)
+ exit(1)
+ ;;
+ if def.noargs && ctx.args.len != 0
+ put("error: expected no args, got {}\n", ctx.args.len)
+ optusage(ctx.optargs[0], ctx.optdef)
+ exit(1)
+ ;;
+ parsed.args = ctx.args
+ -> parsed
+}
+
+const optinit = {ctx, args, def
+ ctx# = [
+ .optargs = args,
+ .optdef = def,
+ .optdone = false,
+ .finished = false,
+ .argidx = 0,
+ .curarg = [][:],
+ .args = [][:],
+ ]
+
+ next(ctx)
+ -> ctx
+}
+
+const optnext = {ctx
+ var c
+ var arg
+
+ (c, ctx.curarg) = striter(ctx.curarg)
+
+ match optinfo(ctx, c)
+ | `None:
+ if c == 'h' || c == '?'
+ optusage(ctx.optargs[0], ctx.optdef)
+ exit(0)
+ else
+ fatal("unexpected argument '{}'\n", c)
+ ;;
+ | `Some (true, needed):
+ /* -arg => '-a' 'rg' */
+ if ctx.curarg.len > 0
+ arg = ctx.curarg
+ ctx.curarg = ctx.curarg[ctx.curarg.len:]
+ next(ctx)
+ /* '-a rg' => '-a' 'rg' */
+ elif ctx.argidx < (ctx.optargs.len - 1)
+ arg = ctx.optargs[ctx.argidx + 1]
+ ctx.argidx++
+ next(ctx)
+ elif needed
+ put("Expected argument for {}\n", c)
+ exit(1)
+ ;;
+ | `Some (false, _):
+ arg = ""
+ if ctx.curarg.len == 0
+ next(ctx)
+ ;;
+ ;;
+
+
+ -> (c, arg)
+}
+
+const optdone = {ctx
+ -> ctx.curarg.len == 0 && ctx.finished
+}
+
+const optinfo = {ctx, opt
+ for o in ctx.optdef.opts
+ if o.opt == opt
+ -> `Some (o.arg.len != 0, !o.optional)
+ ;;
+ ;;
+ -> `None
+}
+
+const next = {ctx
+ var i
+
+ for i = ctx.argidx + 1; i < ctx.optargs.len; i++
+ if !ctx.optdone && decode(ctx.optargs[i]) == '-'
+ if sleq(ctx.optargs[i], "--")
+ ctx.optdone = true
+ else
+ goto foundopt
+ ;;
+ else
+ ctx.args = slpush(ctx.args, ctx.optargs[i])
+ ;;
+ ;;
+:finishedopt
+ ctx.finished = true
+ -> false
+:foundopt
+ ctx.argidx = i
+ ctx.curarg = ctx.optargs[i][1:]
+ -> true
+}
+
+const optusage = {prog, def
+ var sb, s
+
+ sb = mksb()
+ std.sbfmt(sb, "usage: {} [-h?", prog)
+ for o in def.opts
+ if o.arg.len == 0
+ std.sbfmt(sb, "{}", o.opt)
+ ;;
+ ;;
+ std.sbfmt(sb, "] ")
+ for o in def.opts
+ if o.arg.len != 0
+ std.sbfmt(sb, "[-{} {}] ", o.opt, o.arg)
+ ;;
+ ;;
+ std.sbfmt(sb, "{}\n", def.argdesc)
+ std.sbfmt(sb, "\t-h\tprint this help message\n")
+ std.sbfmt(sb, "\t-?\tprint this help message\n")
+ for o in def.opts
+ std.sbfmt(sb, "\t-{}{}{}\t{}\n", o.opt, sep(o.arg), o.arg, o.desc)
+ ;;
+ s = sbfin(sb)
+ write(1, s)
+ slfree(s)
+}
+
+const sep = {s
+ if s.len > 0
+ -> " "
+ else
+ -> ""
+ ;;
+}
diff --git a/lib/std/pathjoin.myr b/lib/std/pathjoin.myr
new file mode 100644
index 0000000..f9df8aa
--- /dev/null
+++ b/lib/std/pathjoin.myr
@@ -0,0 +1,105 @@
+use "alloc.use"
+use "extremum.use"
+use "strjoin.use"
+use "strsplit.use"
+use "sleq.use"
+use "sljoin.use"
+use "sldup.use"
+use "slcp.use"
+use "die.use"
+use "fmt.use"
+
+pkg std =
+ const pathcat : (a : byte[:], b : byte[:] -> byte[:])
+ const pathjoin : (p : byte[:][:] -> byte[:])
+ const pathnorm : (p : byte[:] -> byte[:])
+;;
+
+const pathcat = {a, b
+ -> pathjoin([a, b][:])
+}
+
+const pathjoin = {l
+ var p, i, q
+
+ for i = 0; i < l.len; i++
+ if l[i].len != 0
+ break
+ ;;
+ ;;
+ p = strjoin(l[i:], "/")
+ q = pathnorm(p)
+ slfree(p)
+ -> q
+}
+
+const pathnorm = {p
+ var comps
+ var i, del, dst
+ var s, ret
+
+ comps = strsplit(p, "/")
+ /*
+ "." is a no-op component, so we drop it. In order
+ to drop a component, we set it to the empty string,
+ and remove it later on in the code.
+ */
+ for i = 0; i < comps.len; i++
+ if sleq(comps[i], ".")
+ comps[i] = ""
+ ;;
+ ;;
+
+ /*
+ then, resolve '..' by cancelling out previous components. Scan
+ backwards in the component list for the first real component,
+ and delete both it and the '..' that lead to it.
+
+ Leave in extra '..' components, so that, eg, ../foo doesn't
+ get mangled.
+ */
+ for i = 0; i < comps.len; i++
+ if !sleq(comps[i], "..")
+ continue
+ ;;
+ for del = 1; del <= i; del++
+ if comps[i - del].len > 0 && !sleq(comps[i-del], "..")
+ comps[i - del] = ""
+ comps[i] = ""
+ break
+ ;;
+ ;;
+ ;;
+
+ /* clear out the path nodes we decided to drop */
+ dst = 0
+ for i = 0; i < comps.len; i++
+ if comps[i].len > 0
+ comps[dst++] = comps[i]
+ ;;
+ ;;
+ comps = comps[:dst]
+
+ /*
+ and reassemble. If we have an absolute path,
+ make it absolute. If we have an empty path, return
+ ".". Otherwise, just return the path.
+ */
+ if p.len > 0 && sleq(p[:1], "/")
+ for i = 0; i < comps.len; i++
+ if !sleq(comps[i], "..")
+ break
+ ;;
+ ;;
+ s = strjoin(comps[i:], "/")
+ ret = fmt("/{}", s)
+ slfree(s)
+ elif comps.len == 0
+ ret = sldup(".")
+ else
+ ret = strjoin(comps, "/")
+ ;;
+ slfree(comps)
+ -> ret
+}
+
diff --git a/lib/std/putint.myr b/lib/std/putint.myr
new file mode 100644
index 0000000..711001d
--- /dev/null
+++ b/lib/std/putint.myr
@@ -0,0 +1,44 @@
+use "types.use"
+use "die.use"
+
+pkg std =
+ generic putle64 : (buf : byte[:], v : @a::(numeric,integral) -> size)
+ generic putbe64 : (buf : byte[:], v : @a::(numeric,integral) -> size)
+ generic putle32 : (buf : byte[:], v : @a::(numeric,integral) -> size)
+ generic putbe32 : (buf : byte[:], v : @a::(numeric,integral) -> size)
+ generic putle16 : (buf : byte[:], v : @a::(numeric,integral) -> size)
+ generic putbe16 : (buf : byte[:], v : @a::(numeric,integral) -> size)
+ generic putle8 : (buf : byte[:], v : @a::(numeric,integral) -> size)
+ generic putbe8 : (buf : byte[:], v : @a::(numeric,integral) -> size)
+;;
+
+generic putle64 = {buf, v; -> putle(buf, v castto(uint64), 8)}
+generic putbe64 = {buf, v; -> putbe(buf, v castto(uint64), 8)}
+generic putle32 = {buf, v; -> putle(buf, v castto(uint64), 4)}
+generic putbe32 = {buf, v; -> putbe(buf, v castto(uint64), 4)}
+generic putle16 = {buf, v; -> putle(buf, v castto(uint64), 2)}
+generic putbe16 = {buf, v; -> putbe(buf, v castto(uint64), 2)}
+generic putle8 = {buf, v; -> putle(buf, v castto(uint64), 1)}
+generic putbe8 = {buf, v; -> putbe(buf, v castto(uint64), 1)}
+
+const putbe = {buf, val, n
+ var i, k
+
+ assert(buf.len >= n, "buffer too small")
+ for i = 0; i < n; i++
+ k = val >> (8*(n-i-1))
+ buf[i] = (k & 0xff) castto(byte)
+ ;;
+ -> n castto(size)
+}
+
+const putle = {buf, val, n
+ var i
+
+ assert(buf.len >= n, "buffer too small")
+ for i = 0; i < n; i++
+ buf[i] = (val & 0xff) castto(byte)
+ val >>= 8
+ ;;
+ -> n castto(size)
+}
diff --git a/lib/std/rand.myr b/lib/std/rand.myr
new file mode 100644
index 0000000..2e8c73e
--- /dev/null
+++ b/lib/std/rand.myr
@@ -0,0 +1,185 @@
+use "die.use"
+use "types.use"
+use "alloc.use"
+/*
+ Translated from C by Ori Bernstein
+ */
+
+/*
+ A C-program for MT19937, with initialization improved 2002/1/26.
+ Coded by Takuji Nishimura and Makoto Matsumoto.
+
+ Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
+ All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions
+ are met:
+
+ 1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+ 3. The names of its contributors may not be used to endorse or promote
+ products derived from this software without specific prior written
+ permission.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+ Any feedback is very welcome.
+ http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
+ email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
+ */
+
+pkg std =
+ type rng
+
+ const mksrng : (seed : uint32 -> rng#)
+ const delrng : (rng : rng# -> void)
+ generic rand : (rng : rng#, lo : @a::(numeric,integral), hi : @a::(numeric,integral) -> @a::(numeric,integral))
+ generic randN : (rng : rng# -> @a::(numeric,integral))
+ const randbytes : (rng : rng#, buf : byte[:] -> size)
+ const rand32 : (rng : rng# -> uint32)
+;;
+
+type rng = struct
+ state : uint32[624]
+ i : uint32
+;;
+
+/* allocates and initializes a random number generator */
+const mksrng = {seed
+ var rng
+
+ rng = alloc()
+ init(rng, seed)
+ -> rng
+}
+
+const delrng = {rng
+ free(rng)
+}
+
+/* initializes a random number generator from the seed `seed`. */
+const init = {rng, seed
+ var i
+
+ for i = 0; i < 624; i++
+ rng.state[i] = seed
+ seed = 1812433253 * (seed ^ (seed >> 30)) + i + 1
+ ;;
+ rng.i = i
+}
+
+/*
+ Generates a random integer from `rng` in the range [lo, hi),
+ returning the value. The range [lo, hi) must be positive,
+ nonempty, and the difference between hi and lo must be
+ less then 2^(type_bits - 1)
+*/
+generic rand = {rng, lo, hi -> @a::(integral,numeric)
+ var span, lim
+ var maxrand
+ var val
+
+ assert(hi - lo > 0, "rand.myr: range for random values must be >= 1")
+
+ span = hi - lo
+ maxrand = (1 << (8*sizeof(@a))) - 1 /* max for signed value */
+ if maxrand < 0 /* signed */
+ maxrand = (1 << (8*sizeof(@a)-1)) - 1 /* max for signed value */
+ ;;
+
+ lim = (maxrand/span)*span
+ val = (randN(rng) & maxrand)
+ while val > lim
+ val = (randN(rng) & maxrand)
+ ;;
+ -> val % span + lo
+}
+
+/*
+ Generates a random integer of any size from the
+ random number generator `rng`. The returned value
+ may be negative, if the type is signed.
+*/
+generic randN = {rng -> @a::(integral,numeric)
+ var i, val
+
+ val = 0
+ for i = 0; i < sizeof(@a)/4; i++
+ val <<= 8*sizeof(@a)
+ val |= rand32(rng) castto(@a::(integral,numeric))
+ ;;
+ -> val
+}
+
+/*
+ generates a 32 bit unsigned random number
+ from the random number generator `rng`.
+*/
+const rand32 = {rng
+ var x
+
+ if rng.i == 624
+ next(rng)
+ ;;
+ x = rng.state[rng.i]
+ rng.i++
+
+ x ^= x >> 11
+ x ^= (x << 7) & 0x9D2C5680
+ x ^= (x << 15) & 0xEFC60000
+ -> x ^ (x >> 18)
+}
+
+const randbytes = {rng, buf
+ var i, n, r
+
+ n = 0
+ for i = 0; i < buf.len/4; i++
+ r = rand32(rng)
+ buf[n++] = (r >> 0 & 0xff) castto(byte)
+ buf[n++] = (r >> 8 & 0xff) castto(byte)
+ buf[n++] = (r >> 16 & 0xff) castto(byte)
+ buf[n++] = (r >> 32 & 0xff) castto(byte)
+ ;;
+ r = rand32(rng)
+ for ; n != buf.len; n++
+ buf[n++] = (r & 0xff) castto(byte)
+ r >>= 8
+ ;;
+ -> n
+
+}
+
+/* updates random number generator state when we tick over. */
+const next = {rng
+ var k
+ var y
+
+ for k = 0; k < 227; k++
+ y = (rng.state[k] & 0x80000000) | (rng.state[k + 1] & 0x7FFFFFFF)
+ rng.state[k] = rng.state[k + 397] ^ (y >> 1) ^ ((y & 1) * 0x9908B0DF)
+ ;;
+ for ; k < 623; k++
+ y = (rng.state[k] & 0x80000000) | (rng.state[k + 1] & 0x7FFFFFFF)
+ rng.state[k] = rng.state[k - 227] ^ (y >> 1) ^ ((y & 1) * 0x9908B0DF);
+ ;;
+ y = (rng.state[623] & 0x80000000) | (rng.state[0] & 0x7FFFFFFF)
+ rng.state[623] = rng.state[396] ^ (y >> 1) ^ ((y & 1) * 0x9908B0DF);
+ rng.i = 0
+}
diff --git a/lib/std/resolve+plan9.myr b/lib/std/resolve+plan9.myr
new file mode 100644
index 0000000..ef73843
--- /dev/null
+++ b/lib/std/resolve+plan9.myr
@@ -0,0 +1,2 @@
+pkg std =
+;;
diff --git a/lib/std/resolve+posixy.myr b/lib/std/resolve+posixy.myr
new file mode 100644
index 0000000..185c9d7
--- /dev/null
+++ b/lib/std/resolve+posixy.myr
@@ -0,0 +1,443 @@
+use sys
+use "alloc.use"
+use "chartype.use"
+use "die.use"
+use "endian.use"
+use "result.use"
+use "extremum.use"
+use "hashfuncs.use"
+use "htab.use"
+use "ipparse.use"
+use "option.use"
+use "slcp.use"
+use "sleq.use"
+use "slpush.use"
+use "slurp.use"
+use "strfind.use"
+use "strsplit.use"
+use "strstrip.use"
+use "types.use"
+use "utf.use"
+
+pkg std =
+ type rectype = uint16
+
+ const DnsA : rectype = 1 /* host address */
+ const DnsNS : rectype = 2 /* authoritative name server */
+ const DnsMD : rectype = 3 /* mail destination (Obsolete - use MX) */
+ const DnsMF : rectype = 4 /* mail forwarder (Obsolete - use MX) */
+ const DnsCNAME : rectype = 5 /* canonical name for an alias */
+ const DnsSOA : rectype = 6 /* marks the start of a zone of authority */
+ const DnsMB : rectype = 7 /* mailbox domain name (EXPERIMENTAL) */
+ const DnsMG : rectype = 8 /* mail group member (EXPERIMENTAL) */
+ const DnsMR : rectype = 9 /* mail rename domain name (EXPERIMENTAL) */
+ const DnsNULL : rectype = 10 /* null RR (EXPERIMENTAL) */
+ const DnsWKS : rectype = 11 /* well known service description */
+ const DnsPTR : rectype = 12 /* domain name pointer */
+ const DnsHINFO : rectype = 13 /* host information */
+ const DnsMINFO : rectype = 14 /* mailbox or mail list information */
+ const DnsMX : rectype = 15 /* mail exchange */
+ const DnsTXT : rectype = 16 /* text strings */
+ const DnsAAAA : rectype = 28 /* ipv6 host address */
+
+
+ type resolveerr = union
+ `Badhost
+ `Badsrv
+ `Badquery
+ `Badresp
+ ;;
+
+ type hostinfo = struct
+ fam : sys.sockfam
+ stype : sys.socktype
+ ttl : uint32
+ addr : netaddr
+ /*
+ flags : uint32
+ addr : sockaddr[:]
+ canon : byte[:]
+ */
+ ;;
+
+ const resolve : (host : byte[:] -> result(hostinfo[:], resolveerr))
+ const resolvemx : (host : byte[:] -> result(hostinfo[:], resolveerr))
+ const resolverec : (host : byte[:], t : rectype -> result(hostinfo[:], resolveerr))
+;;
+
+const Hostfile = "/etc/hosts"
+const Resolvfile = "/etc/resolv.conf"
+
+var hostmap : htab(byte[:], hostinfo)#
+var search : byte[:][:]
+var nameservers : netaddr[:]
+
+const __init__ = {
+ hostmap = mkht(strhash, streq)
+ loadhosts()
+ loadresolv()
+}
+
+const resolve = {host
+ -> resolverec(host, DnsA)
+}
+
+const resolvemx = {host
+ -> resolverec(host, DnsMX)
+}
+
+const resolverec = {host, t
+ match hostfind(host)
+ | `Some hinf:
+ -> `Ok slpush([][:], hinf)
+ | `None:
+ -> dnsresolve(host, DnsA)
+ ;;
+}
+
+const hostfind = {host
+ -> htget(hostmap, host)
+}
+
+const loadhosts = {
+ var h
+ var lines
+
+ match slurp(Hostfile)
+ | `Ok d: h = d
+ | `Fail m: ->
+ ;;
+
+ lines = strsplit(h, "\n")
+ for l in lines
+ /* trim comment */
+ match strfind(l, "#")
+ | `Some idx: l = l[:idx]
+ | `None: /* whole line */
+ ;;
+
+ match word(l)
+ | `Some (ip, rest):
+ match ipparse(ip)
+ | `Some addr:
+ addhosts(addr, ip, rest)
+ | `None:
+ /*
+ invalid addresses are ignored: we don't want to break stuff
+ with invalid or unsupported addresses
+ */
+
+ ;;
+ | `None:
+ ;;
+ ;;
+ slfree(lines)
+}
+
+const addhosts = {addr, as, str
+ var hinf
+ var fam
+
+ match addr
+ | `Ipv4 _: fam = sys.Afinet
+ | `Ipv6 _: fam = sys.Afinet6
+ ;;
+ while true
+ match word(str)
+ | `Some (name, rest):
+ str = rest
+ if hthas(hostmap, name)
+ continue
+ ;;
+ hinf = [
+ .fam=fam,
+ .stype = 0,
+ .ttl = 0,
+ .addr = addr
+ ]
+ htput(hostmap, name, hinf)
+ | `None:
+ ->
+ ;;
+ ;;
+}
+
+const loadresolv = {
+ var h
+ var lines
+
+ match slurp(Resolvfile)
+ | `Ok d: h = d
+ | `Fail m: ->
+ ;;
+
+ lines = strsplit(h, "\n")
+ for l in lines
+ match strfind(l, "#")
+ | `Some _idx: l = l[:_idx]
+ | `None:
+ ;;
+
+ match word(l)
+ | `Some ("nameserver", srv):
+ addns(srv)
+ | `Some (_, rest):
+ /* invalid or unrecognized commands */
+ | `None:
+ /* unrecognized lines */
+ ;;
+ ;;
+ slfree(lines)
+}
+
+const addns = {rest
+ match word(rest)
+ | `Some (name, _):
+ match ipparse(name)
+ | `Some addr:
+ nameservers = slpush(nameservers, addr)
+ | `None:
+ /* nothing */
+ ;;
+ | `None:
+ /* nothing */
+ ;;
+}
+
+const word = {s
+ var c, len
+
+ len = 0
+ s = strfstrip(s)
+ for c = decode(s[len:]); c != Badchar && !isblank(c); c = decode(s[len:])
+ len += charlen(c)
+ ;;
+ if len == 0
+ -> `None
+ else
+ -> `Some (s[:len], s[len:])
+ ;;
+}
+
+
+const dnsresolve = {host, t
+ var nsrv
+
+ if !valid(host)
+ -> `Fail (`Badhost)
+ ;;
+ for ns in nameservers
+ nsrv = dnsconnect(ns)
+ if nsrv >= 0
+ -> dnsquery(nsrv, host, t)
+ ;;
+ ;;
+ -> `Fail (`Badsrv)
+}
+
+const dnsconnect = {ns
+ match ns
+ | `Ipv4 addr: -> dnsconnectv4(addr)
+ | `Ipv6 addr: die("don't support ipv6 yet\n")
+ ;;
+}
+
+const dnsconnectv4 = {addr
+ var sa : sys.sockaddr_in
+ var s
+ var status
+
+ s = sys.socket(sys.Afinet, sys.Sockdgram, 0)
+ if s < 0
+ -> -1
+ ;;
+ sa.fam = sys.Afinet
+ sa.port = hosttonet(53)
+ sa.addr = addr
+ status = sys.connect(s, (&sa) castto(sys.sockaddr#), sizeof(sys.sockaddr_in))
+ if status < 0
+ -> -1
+ ;;
+ -> s
+}
+
+const dnsquery = {srv, host, t
+ var id
+ var r
+
+ id = tquery(srv, host, t)
+ r = rquery(srv, id)
+ -> r
+}
+
+const Qr : uint16 = 1 << 0
+const Aa : uint16 = 1 << 5
+const Tc : uint16 = 1 << 6
+const Rd : uint16 = 1 << 7
+const Ra : uint16 = 1 << 8
+
+var nextid : uint16 = 42
+const tquery = {srv, host, t
+ var pkt : byte[512] /* big enough */
+ var off : size
+
+ /* header */
+ off = 0
+ off += pack16(pkt[:], off, nextid) /* id */
+ off += pack16(pkt[:], off, Ra) /* flags */
+ off += pack16(pkt[:], off, 1) /* qdcount */
+ off += pack16(pkt[:], off, 0) /* ancount */
+ off += pack16(pkt[:], off, 0) /* nscount */
+ off += pack16(pkt[:], off, 0) /* arcount */
+
+ /* query */
+ off += packname(pkt[:], off, host) /* host */
+ off += pack16(pkt[:], off, t castto(uint16)) /* qtype: a record */
+ off += pack16(pkt[:], off, 0x1) /* qclass: inet4 */
+
+ sys.write(srv, pkt[:off])
+ -> nextid++
+}
+
+const rquery = {srv, id
+ var pktbuf : byte[1024]
+ var pkt
+ var n
+
+ n = sys.read(srv, pktbuf[:])
+ if n < 0
+ ;;
+ pkt = pktbuf[:n]
+ -> hosts(pkt, id)
+}
+
+const hosts = {pkt, id : uint16
+ var off
+ var v, q, a
+ var i
+ var hinf : hostinfo[:]
+
+ off = 0
+ /* parse header */
+ (v, off) = unpack16(pkt, off) /* id */
+ if v != id
+ -> `Fail (`Badresp)
+ ;;
+ (v, off) = unpack16(pkt, off) /* flags */
+ (q, off) = unpack16(pkt, off) /* qdcount */
+ (a, off) = unpack16(pkt, off) /* ancount */
+ (v, off) = unpack16(pkt, off) /* nscount */
+ (v, off) = unpack16(pkt, off) /* arcount */
+
+ /* skip past query records */
+ for i = 0; i < q; i++
+ off = skipname(pkt, off) /* name */
+ (v, off) = unpack16(pkt, off) /* type */
+ (v, off) = unpack16(pkt, off) /* class */
+ ;;
+
+ /* parse answer records */
+ hinf = slalloc(a castto(size))
+ for i = 0; i < a; i++
+ off = skipname(pkt, off) /* name */
+ (v, off) = unpack16(pkt, off) /* type */
+ (v, off) = unpack16(pkt, off) /* class */
+ (hinf[i].ttl, off) = unpack32(pkt, off) /* ttl */
+ (v, off) = unpack16(pkt, off) /* rdatalen */
+ /* the thing we're interested in: our IP address */
+ hinf[i].addr = `Ipv4 [pkt[off], pkt[off+1], pkt[off+2], pkt[off+3]]
+ off += 4;
+ ;;
+ -> `Ok hinf
+}
+
+
+const skipname = {pkt, off
+ var sz
+
+ for sz = pkt[off] castto(size); sz != 0; sz = pkt[off] castto(size)
+ /* ptr is 2 bytes */
+ if sz & 0xC0 == 0xC0
+ -> off + 2
+ else
+ off += sz + 1
+ ;;
+ ;;
+ -> off + 1
+}
+
+
+const pack16 = {buf, off, v
+ buf[off] = (v & 0xff00) >> 8 castto(byte)
+ buf[off+1] = (v & 0x00ff) castto(byte)
+ -> sizeof(uint16) /* we always write one uint16 */
+}
+
+const unpack16 = {buf, off
+ var v
+
+ v = (buf[off] castto(uint16)) << 8
+ v |= (buf[off + 1] castto(uint16))
+ -> (v, off+sizeof(uint16))
+}
+
+const unpack32 = {buf, off
+ var v
+
+ v = (buf[off] castto(uint32)) << 24
+ v |= (buf[off+1] castto(uint32)) << 32
+ v |= (buf[off+2] castto(uint32)) << 8
+ v |= (buf[off+3] castto(uint32))
+ -> (v, off+sizeof(uint32))
+}
+
+const packname = {buf, off : size, host
+ var i
+ var start
+ var last
+
+ start = off
+ last = 0
+ for i = 0; i < host.len; i++
+ if host[i] == ('.' castto(byte))
+ off += addseg(buf, off, host[last:i])
+ last = i + 1
+ ;;
+ ;;
+ if host[host.len - 1] != ('.' castto(byte))
+ off += addseg(buf, off, host[last:])
+ ;;
+ off += addseg(buf, off, "") /* null terminating segment */
+ -> off - start
+}
+
+const addseg = {buf, off, str
+ buf[off] = str.len castto(byte)
+ slcp(buf[off + 1 : off + str.len + 1], str)
+ -> str.len + 1
+}
+
+const valid = {host : byte[:]
+ var i
+ var seglen
+
+ /* maximum length: 255 chars */
+ if host.len > 255
+ -> false
+ ;;
+
+ seglen = 0
+ for i = 0; i < host.len; i++
+ if host[i] == ('.' castto(byte))
+ seglen = 0
+ ;;
+ if seglen > 63
+ -> false
+ ;;
+ if host[i] & 0x80 != 0
+ -> false
+ ;;
+ ;;
+
+ -> true
+}
diff --git a/lib/std/result.myr b/lib/std/result.myr
new file mode 100644
index 0000000..58ef45b
--- /dev/null
+++ b/lib/std/result.myr
@@ -0,0 +1,9 @@
+use "die.use"
+
+pkg std =
+ type result(@a, @b) = union
+ `Ok @a
+ `Fail @b
+ ;;
+;;
+
diff --git a/lib/std/search.myr b/lib/std/search.myr
new file mode 100644
index 0000000..725e419
--- /dev/null
+++ b/lib/std/search.myr
@@ -0,0 +1,43 @@
+use "cmp.use"
+use "option.use"
+
+pkg std =
+ generic lsearch : (sl : @t[:], val : @t, cmp : (a : @t, b : @t -> order) -> option(@idx::(integral,numeric)))
+ generic bsearch : (sl : @t[:], val : @t, cmp : (a : @t, b : @t -> order) -> option(@idx::(integral,numeric)))
+;;
+
+/* linear search over a list of values */
+generic lsearch = {sl, val, cmp
+ var i
+
+ for i = 0; i < sl.len; i++
+ match cmp(sl[i], val)
+ | `Equal:
+ -> `Some i
+ | _:
+ /* nothing */
+ ;;
+ ;;
+ -> `None
+}
+
+/* binary search over a sorted list of values. */
+generic bsearch = {sl, val, cmp
+ var hi, lo, mid
+
+ lo = 0
+ hi = sl.len - 1
+
+ while lo <= hi
+ mid = (hi + lo) / 2
+ match cmp(val, sl[mid])
+ | `Before: hi = mid - 1
+ | `After: lo = mid + 1
+ | `Equal:
+ -> `Some mid
+ ;;
+ ;;
+ -> `None
+}
+
+
diff --git a/lib/std/slcp.myr b/lib/std/slcp.myr
new file mode 100644
index 0000000..1035a29
--- /dev/null
+++ b/lib/std/slcp.myr
@@ -0,0 +1,26 @@
+use "die.use"
+use "types.use"
+
+pkg std =
+ generic slcp : (a : @a[:], b : @a[:] -> void)
+;;
+
+generic slcp = {a : @a[:], b : @a[:]
+ var i
+ var addr_a, addr_b
+
+ assert(a.len == b.len, "arguments to slcp() must be of equal length")
+
+ addr_a = a castto(@a#) castto(intptr)
+ addr_b = b castto(@a#) castto(intptr)
+ if addr_a <= addr_b
+ for i = 0; i < a.len; i++
+ a[i] = b[i]
+ ;;
+ else
+ for i = a.len; i > 0; i--
+ a[i - 1] = b[i - 1]
+ ;;
+ ;;
+
+}
diff --git a/lib/std/sldup.myr b/lib/std/sldup.myr
new file mode 100644
index 0000000..70cbee4
--- /dev/null
+++ b/lib/std/sldup.myr
@@ -0,0 +1,15 @@
+use "alloc.use"
+use "die.use"
+use "slcp.use"
+
+pkg std =
+ generic sldup : (sl : @a[:] -> @a[:])
+;;
+
+generic sldup = {sl
+ var ret
+
+ ret = slalloc(sl.len)
+ slcp(ret, sl)
+ -> ret
+}
diff --git a/lib/std/sleq.myr b/lib/std/sleq.myr
new file mode 100644
index 0000000..cfc60b4
--- /dev/null
+++ b/lib/std/sleq.myr
@@ -0,0 +1,18 @@
+pkg std =
+ generic sleq : (a : @a[:], b : @a[:] -> bool)
+;;
+
+generic sleq = {a, b
+ var i
+
+ if a.len != b.len
+ -> false
+ ;;
+
+ for i = 0; i < a.len; i++
+ if a[i] != b[i]
+ -> false
+ ;;
+ ;;
+ -> true
+}
diff --git a/lib/std/slfill.myr b/lib/std/slfill.myr
new file mode 100644
index 0000000..d90e8f5
--- /dev/null
+++ b/lib/std/slfill.myr
@@ -0,0 +1,12 @@
+pkg std =
+ generic slfill : (sl : @a[:], v : @a -> @a[:])
+;;
+
+generic slfill = {sl, v
+ var i
+
+ for i = 0; i < sl.len; i++
+ sl[i] = v
+ ;;
+ -> sl
+}
diff --git a/lib/std/sljoin.myr b/lib/std/sljoin.myr
new file mode 100644
index 0000000..f1fe58a
--- /dev/null
+++ b/lib/std/sljoin.myr
@@ -0,0 +1,15 @@
+use "alloc.use"
+use "slcp.use"
+
+pkg std =
+ generic sljoin : (dst : @a[:], src : @a[:] -> @a[:])
+;;
+
+generic sljoin = {dst, src
+ var len
+
+ len = dst.len
+ dst = slgrow(dst, len + src.len)
+ slcp(dst[len:], src)
+ -> dst
+}
diff --git a/lib/std/slpush.myr b/lib/std/slpush.myr
new file mode 100644
index 0000000..b30b1e2
--- /dev/null
+++ b/lib/std/slpush.myr
@@ -0,0 +1,20 @@
+use "types.use"
+use "alloc.use"
+
+pkg std =
+ generic slpush : (sl : @a[:], elt : @a -> @a[:])
+;;
+
+generic slpush = {sl, elt
+ /*
+ slpush relies on implementation details
+ of slgrow for efficiency. Because bucket
+ sizes come in powers of two for all buckets
+ <= 32k, and we only reallocate when we hit
+ a bucket boundary, this is effectively
+ growing the slice by powers of two.
+ */
+ sl = slgrow(sl, sl.len + 1)
+ sl[sl.len - 1] = elt
+ -> sl
+}
diff --git a/lib/std/slput.myr b/lib/std/slput.myr
new file mode 100644
index 0000000..88dc875
--- /dev/null
+++ b/lib/std/slput.myr
@@ -0,0 +1,20 @@
+use "types.use"
+use "alloc.use"
+use "die.use"
+
+pkg std =
+ generic slput : (sl : @a[:], idx : size, elt : @a -> @a[:])
+;;
+
+generic slput = {sl, idx, elt
+ var i
+ var len
+
+ len = sl.len
+ sl = slgrow(sl, sl.len + 1)
+ for i = len - 1; i >= idx; i--
+ sl[i + 1] = sl[i]
+ ;;
+ sl[idx] = elt
+ -> sl
+}
diff --git a/lib/std/slurp.myr b/lib/std/slurp.myr
new file mode 100644
index 0000000..8636f40
--- /dev/null
+++ b/lib/std/slurp.myr
@@ -0,0 +1,43 @@
+use "alloc.use"
+use "die.use"
+use "result.use"
+use "extremum.use"
+use "syswrap.use"
+use "types.use"
+
+pkg std =
+ const slurp : (path : byte[:] -> result(byte[:], byte[:]))
+ const fslurp : (path : fd -> result(byte[:], byte[:]))
+;;
+
+const Bufstart = 4096
+
+const slurp = {path
+ var fd
+ fd = open(path, Ordonly)
+ if fd < 0
+ -> `Fail "Could not open file"
+ ;;
+ -> fslurp(fd)
+}
+
+const fslurp = {fd
+ var len, bufsz
+ var buf
+ var n
+
+ len = 0
+ bufsz = Bufstart
+ buf = slalloc(bufsz)
+ while true
+ n = read(fd, buf[len:])
+ if n <= 0
+ goto done
+ ;;
+ len += n
+ bufsz *= 2
+ buf = slgrow(buf, bufsz)
+ ;;
+:done
+ -> `Ok buf[:len]
+}
diff --git a/lib/std/sort.myr b/lib/std/sort.myr
new file mode 100644
index 0000000..dbf48d3
--- /dev/null
+++ b/lib/std/sort.myr
@@ -0,0 +1,61 @@
+use "cmp.use"
+
+pkg std =
+ generic sort : (sl:@a[:], cmp:(a:@a, b:@a -> order) -> @a[:])
+;;
+
+generic sort = {sl, cmp
+ var end
+ var tmp
+
+ heapify(sl, cmp)
+ end = sl.len - 1
+ while end > 0
+ tmp = sl[end]
+ sl[end] = sl[0]
+ sl[0] = tmp
+ end--
+ siftdown(sl[:end + 1], 0, cmp)
+ ;;
+ -> sl
+}
+
+generic heapify = {sl, cmp
+ var start
+
+ start = sl.len/2 - 1
+ while start >= 0
+ siftdown(sl, start, cmp)
+ start--
+ ;;
+}
+
+generic siftdown = {sl, start, cmp
+ var r, c, s
+ var tmp
+
+ r = start
+ while 2*r + 1 < sl.len
+ c = 2*r + 1
+ s = r
+ match cmp(sl[s], sl[c])
+ | `Before: s = c
+ | _: /* nothing */
+ ;;
+ if c + 1 < sl.len
+ match cmp(sl[s], sl[c + 1])
+ | `Before: s = c + 1
+ | _: /* nothing */
+ ;;
+ ;;
+ if s != r
+ tmp = sl[r]
+ sl[r] = sl[s]
+ sl[s] = tmp
+ r = s
+ else
+ ->
+ ;;
+ ;;
+}
+
diff --git a/lib/std/spork.myr b/lib/std/spork.myr
new file mode 100644
index 0000000..64e8fce
--- /dev/null
+++ b/lib/std/spork.myr
@@ -0,0 +1,62 @@
+use "die.use"
+use "execvp.use"
+use "fmt.use"
+use "result.use"
+use "syswrap.use"
+
+pkg std =
+ const spork : (cmd : byte[:][:] -> result((pid, fd, fd), int))
+ const sporkfd : (cmd : byte[:][:], infd : fd, outfd : fd -> result(pid, int))
+;;
+
+const spork = {cmd
+ var infds :fd[2], outfds : fd[2]
+ var err
+
+ /* open up pipes */
+ err = pipe(&infds)
+ if err != 0
+ -> `Fail (-err castto(int))
+ ;;
+ err = pipe(&outfds)
+ if err != 0
+ -> `Fail (-err castto(int))
+ ;;
+
+ match sporkfd(cmd, infds[0] castto(fd), outfds[1] castto(fd))
+ | `Ok pid:
+ /* close unused fd ends */
+ close(infds[0]);
+ close(outfds[1]);
+ -> `Ok (pid, infds[1], outfds[0])
+ | `Fail m:
+ -> `Fail m
+ ;;
+}
+
+const sporkfd = {cmd, infd, outfd
+ var pid
+
+ pid = fork()
+ /* error */
+ if pid == -1
+ -> `Fail -1
+ /* child */
+ elif pid == 0
+ /* stdin/stdout for our communication. */
+ if dup2(infd castto(fd), 0) != 0
+ fatal("unable to set stdin\n")
+ ;;
+ if dup2(outfd castto(fd), 1) != 1
+ fatal("unable to set stdout\n")
+ ;;
+ close(infd)
+ close(outfd)
+ execvp(cmd[0], cmd) < 0
+ fatal("failed to exec {}\n", cmd[0])
+ /* parent */
+ else
+ -> `Ok pid
+ ;;
+}
+
diff --git a/lib/std/strbuf.myr b/lib/std/strbuf.myr
new file mode 100644
index 0000000..097d1ce
--- /dev/null
+++ b/lib/std/strbuf.myr
@@ -0,0 +1,107 @@
+use "alloc.use"
+use "die.use"
+use "extremum.use"
+use "slcp.use"
+use "types.use"
+use "utf.use"
+
+pkg std =
+ type strbuf = struct
+ buf : byte[:]
+ len : size
+ fixed : bool
+ ;;
+
+ const mksb : (-> strbuf#)
+ const mkbufsb : (buf : byte[:] -> strbuf#)
+ const sbfin : (sb : strbuf# -> byte[:])
+ const sbfree : (sb : strbuf# -> void)
+ const sbpeek : (sb : strbuf# -> byte[:])
+
+ const sbputc : (sb : strbuf#, v : char -> bool)
+ const sbputs : (sb : strbuf#, v : byte[:] -> bool)
+ const sbputb : (sb : strbuf#, v : byte -> bool)
+ const sbtrim : (sb : strbuf#, len : size -> void)
+;;
+
+const mksb = {
+ var sb
+ sb = zalloc()
+ sb.buf = slalloc(1)
+ -> sb
+}
+
+const mkbufsb = {buf
+ var sb
+
+ sb = zalloc()
+ sb.buf = buf
+ sb.fixed = true
+ -> sb
+}
+
+const sbfin = {sb
+ var sl
+
+ sl = sb.buf[:sb.len]
+ free(sb)
+ -> sl
+}
+
+const sbfree = {sb
+ if !sb.fixed
+ slfree(sb.buf)
+ ;;
+ free(sb)
+}
+
+
+const sbpeek = {sb : strbuf#
+ -> sb.buf[:sb.len]
+}
+
+const sbputc = {sb, v
+ if !ensure(sb, charlen(v))
+ -> false
+ ;;
+ sb.len += encode(sb.buf[sb.len:], v)
+ -> true
+}
+const sbputs = {sb, v
+ var ok
+ var len
+
+ ok = ensure(sb, v.len)
+ /* copy what we can */
+ len = min(sb.buf.len - sb.len, v.len)
+ slcp(sb.buf[sb.len:sb.len + len], v[:len])
+ sb.len += len
+ -> ok
+}
+const sbputb = {sb, v
+ if !ensure(sb, 1)
+ -> false
+ ;;
+ sb.buf[sb.len++] = v
+ -> true
+}
+
+const sbtrim = {sb, len
+ assert(abs(len) <= sb.len, "trim out of range\n")
+ if len < 0
+ sb.len -= abs(len)
+ else
+ sb.len = len
+ ;;
+}
+
+const ensure = {sb, len
+ if sb.fixed && sb.len + len > sb.buf.len
+ -> false
+ ;;
+ while sb.buf.len < sb.len + len
+ sb.buf = slgrow(sb.buf, 2*sb.buf.len)
+ ;;
+ -> true
+}
+
diff --git a/lib/std/strfind.myr b/lib/std/strfind.myr
new file mode 100644
index 0000000..0ec0e85
--- /dev/null
+++ b/lib/std/strfind.myr
@@ -0,0 +1,51 @@
+use "types.use"
+use "option.use"
+
+pkg std =
+ const strfind : (haystack : byte[:], needle : byte[:] -> option(size))
+ const strrfind : (haystack : byte[:], needle : byte[:] -> option(size))
+ const strhas : (haystack : byte[:], needle : byte[:] -> bool)
+;;
+
+const strfind = {haystack, needle
+ -> strfindin(haystack, needle, 0, haystack.len)
+}
+
+const strrfind = {haystack, needle
+ -> strfindin(haystack, needle, haystack.len - 1, -1)
+}
+
+const strfindin = {haystack, needle, start, end
+ var i, j, inc : size
+
+ inc = 1
+ if start > end
+ inc = -1
+ ;;
+ for i = start; i != end; i += inc
+ /*
+ if we knew the direction we could terminate early,
+ but we allow the start and end to be passed in.
+ */
+ if i + needle.len > haystack.len
+ continue
+ ;;
+ if haystack[i] == needle[0]
+ for j = 0; j < needle.len; j++
+ if haystack[i + j] != needle[j]
+ goto nextiter
+ ;;
+ ;;
+ -> `Some i
+ ;;
+:nextiter
+ ;;
+ -> `None
+}
+
+const strhas = {haystack, needle
+ match strfind(haystack, needle)
+ | `Some _: -> true
+ | `None: -> false
+ ;;
+} \ No newline at end of file
diff --git a/lib/std/strjoin.myr b/lib/std/strjoin.myr
new file mode 100644
index 0000000..518a4dc
--- /dev/null
+++ b/lib/std/strjoin.myr
@@ -0,0 +1,41 @@
+use "alloc.use"
+use "die.use"
+use "slcp.use"
+
+pkg std =
+ const strcat : (a : byte[:], b : byte[:] -> byte[:])
+ const strjoin : (strings : byte[:][:], delim : byte[:] -> byte[:])
+;;
+
+const strcat = {a, b
+ -> strjoin([a, b][:], "")
+}
+
+const strjoin = {strings, delim
+ var len, off
+ var i
+ var s
+
+ len = 0
+ for i = 0; i < strings.len; i++
+ len += strings[i].len
+ ;;
+ if strings.len > 0
+ len += (strings.len - 1)*delim.len
+ ;;
+
+ s = slalloc(len)
+ off = 0
+ for i = 0; i < strings.len; i++
+ slcp(s[off:off + strings[i].len], strings[i])
+ off += strings[i].len
+ /* we don't want to terminate the last string with delim */
+ if i != strings.len - 1
+ slcp(s[off:off + delim.len], delim)
+ off += delim.len
+ ;;
+ ;;
+ -> s
+}
+
+
diff --git a/lib/std/strsplit.myr b/lib/std/strsplit.myr
new file mode 100644
index 0000000..e64d907
--- /dev/null
+++ b/lib/std/strsplit.myr
@@ -0,0 +1,35 @@
+use "alloc.use"
+use "die.use"
+use "extremum.use"
+use "option.use"
+use "slpush.use"
+use "strfind.use"
+use "types.use"
+
+pkg std =
+ const strsplit : (s : byte[:], delim : byte[:] -> byte[:][:])
+;;
+
+const strsplit = {s, delim
+ var last
+ var sp
+
+ sp = [][:]
+ if s.len == 0
+ -> sp
+ ;;
+ last = 0
+ while true
+ match strfind(s, delim)
+ | `Some i:
+ sp = slpush(sp, s[:i])
+ s = s[i + delim.len:]
+ | `None:
+ goto donesplit
+ ;;
+ ;;
+:donesplit
+ sp = slpush(sp, s[:])
+ -> sp
+}
+
diff --git a/lib/std/strstrip.myr b/lib/std/strstrip.myr
new file mode 100644
index 0000000..a3d54fa
--- /dev/null
+++ b/lib/std/strstrip.myr
@@ -0,0 +1,43 @@
+use "types.use"
+use "utf.use"
+use "chartype.use"
+
+pkg std =
+ const strstrip : (str : byte[:] -> byte[:])
+ const strfstrip : (str : byte[:] -> byte[:])
+ const strrstrip : (str : byte[:] -> byte[:])
+;;
+
+/* strip blanks from both head and tail of str */
+const strstrip = {str
+ -> strrstrip(strfstrip(str))
+}
+
+/* strip forward on str */
+const strfstrip = {str
+ var c
+
+ for c = decode(str); isblank(c); c = decode(str)
+ str = str[charlen(c):]
+ ;;
+ -> str
+
+}
+
+/* strip reverse on str */
+const strrstrip = {str
+ var i
+ var end
+
+ /* scan backwards for start of utf8 char */
+ end = str.len
+ for i = str.len; i != 0; i--
+ if str[i - 1] & 0x80 == 0 || str[i-1] & 0xc0 != 0x80
+ if !isspace(decode(str[i-1:]))
+ break
+ ;;
+ end = i - 1
+ ;;
+ ;;
+ -> str[:end]
+}
diff --git a/lib/std/swap.myr b/lib/std/swap.myr
new file mode 100644
index 0000000..e3923c9
--- /dev/null
+++ b/lib/std/swap.myr
@@ -0,0 +1,11 @@
+pkg std =
+ generic swap : (a : @a#, b : @a# -> void)
+;;
+
+generic swap = {a : @a#, b : @a#
+ var t
+
+ t = a#
+ a# = b#
+ b# = t
+}
diff --git a/lib/std/sys+freebsd-x64.myr b/lib/std/sys+freebsd-x64.myr
new file mode 100644
index 0000000..279d360
--- /dev/null
+++ b/lib/std/sys+freebsd-x64.myr
@@ -0,0 +1,805 @@
+use "systypes.use"
+
+pkg sys =
+ type scno = int64 /*syscall*/
+ type fdopt = int64 /* fd options */
+ type fd = int64 /* fd */
+ type mprot = int64 /* memory protection */
+ type mopt = int64 /* memory mapping options */
+ type socktype = int64 /* socket type */
+ type sockproto = int64 /* socket protocol */
+ type sockfam = uint8 /* socket family */
+ type filemode = uint16
+ type filetype = uint8
+
+ type clock = union
+ `Clockrealtime
+ `Clockrealtime_precise
+ `Clockrealtime_fast
+ `Clockmonotonic
+ `Clockmonotonic_precise
+ `Clockmonotonic_fast
+ `Clockuptime
+ `Clockuptime_precise
+ `Clockuptime_fast
+ `Clockvirtual
+ `Clockprof
+ `Clocksecond
+ ;;
+
+ type waitstatus = union
+ `Waitexit int32
+ `Waitsig int32
+ `Waitstop int32
+ ;;
+
+ type timespec = struct
+ sec : uint64
+ nsec : uint64
+ ;;
+
+ type timeval = struct
+ sec : uint64
+ usec : uint64
+ ;;
+
+ type rusage = struct
+ utime : timeval /* user time */
+ stime : timeval /* system time */
+ maxrss : uint64 /* max resident set size*/
+ ixrss : uint64 /* shared text size */
+ idrss : uint64 /* unshared data size */
+ isrss : uint64 /* unshared stack size */
+ minflt : uint64 /* page reclaims */
+ majflt : uint64 /* page faults */
+ nswap : uint64 /* swaps */
+ inblock : uint64 /* block input ops */
+ oublock : uint64 /* block output ops */
+ msgsnd : uint64 /* messages sent */
+ msgrcv : uint64 /* messages received */
+ nsignals : uint64 /* signals received */
+ nvcsw : uint64 /* voluntary context switches */
+ nivcsw : uint64 /* involuntary context switches */
+ ;;
+
+ type statbuf = struct
+ dev : uint32
+ ino : uint32
+ mode : filemode
+ nlink : uint16
+ uid : uint32
+ gid : uint32
+ rdev : uint32
+ atim : timespec
+ mtim : timespec
+ ctim : timespec
+ size : int64
+ blocks : int64
+ blksize : uint32
+ flags : uint32
+ gen : uint32
+ lspare : int32
+ birthtim : timespec
+ ;;
+
+ type utsname = struct
+ system : byte[256]
+ node : byte[256]
+ release : byte[256]
+ version : byte[256]
+ machine : byte[256]
+ ;;
+
+ type sockaddr = struct
+ len : byte
+ fam : sockfam
+ data : byte[14] /* what is the *actual* length? */
+ ;;
+
+ type sockaddr_in = struct
+ len : byte
+ fam : sockfam
+ port : uint16
+ addr : byte[4]
+ zero : byte[8]
+ ;;
+
+ type sockaddr_storage = struct
+ len : byte
+ fam : sockfam
+ __pad1 : byte[6]
+ __align : int64
+ __pad2 : byte[112]
+ ;;
+
+ type dirent = struct
+ fileno : uint32
+ reclen : uint16
+ ftype : filetype
+ namelen : uint8
+ name : byte[256]
+ ;;
+
+ /* open options */
+ const Ordonly : fdopt = 0x0
+ const Owronly : fdopt = 0x1
+ const Ordwr : fdopt = 0x2
+ const Oappend : fdopt = 0x8
+ const Ocreat : fdopt = 0x200
+ const Onofollow : fdopt = 0x100
+ const Ondelay : fdopt = 0x4
+ const Otrunc : fdopt = 0x400
+ const Odir : fdopt = 0x20000
+
+ /* stat modes */
+ const Sifmt : filemode = 0xf000
+ const Sififo : filemode = 0x1000
+ const Sifchr : filemode = 0x2000
+ const Sifdir : filemode = 0x4000
+ const Sifblk : filemode = 0x6000
+ const Sifreg : filemode = 0x8000
+ const Siflnk : filemode = 0xa000
+ const Sifsock : filemode = 0xc000
+
+ /* mmap protection */
+ const Mprotnone : mprot = 0x0
+ const Mprotrd : mprot = 0x1
+ const Mprotwr : mprot = 0x2
+ const Mprotexec : mprot = 0x4
+ const Mprotrw : mprot = 0x3
+
+ /* mmap options */
+ const Mshared : mopt = 0x1
+ const Mpriv : mopt = 0x2
+ const Mfixed : mopt = 0x10
+ const Mfile : mopt = 0x0
+ const Manon : mopt = 0x1000
+ const M32bit : mopt = 0x80000
+
+ /* file types */
+ const Dtunknown : filetype = 0
+ const Dtfifo : filetype = 1
+ const Dtchr : filetype = 2
+ const Dtdir : filetype = 4
+ const Dtblk : filetype = 6
+ const Dtreg : filetype = 8
+ const Dtlnk : filetype = 10
+ const Dtsock : filetype = 12
+ const Dtwht : filetype = 14
+
+ /* socket families. INCOMPLETE. */
+ const Afunspec : sockfam = 0
+ const Afunix : sockfam = 1
+ const Afinet : sockfam = 2
+ const Afinet6 : sockfam = 28
+
+ /* socket types. */
+ const Sockstream : socktype = 1
+ const Sockdgram : socktype = 2
+ const Sockraw : socktype = 3
+ const Sockrdm : socktype = 4
+ const Sockseqpacket : socktype = 5
+
+ /* network protocols */
+ const Ipproto_ip : sockproto = 0
+ const Ipproto_icmp : sockproto = 1
+ const Ipproto_tcp : sockproto = 6
+ const Ipproto_udp : sockproto = 17
+ const Ipproto_raw : sockproto = 255
+
+ /* return value for a failed mapping */
+ const Mapbad : byte# = -1 castto(byte#)
+
+ /* syscalls */
+ const Syssyscall : scno = 0
+ const Sysexit : scno = 1
+ const Sysfork : scno = 2
+ const Sysread : scno = 3
+ const Syswrite : scno = 4
+ const Sysopen : scno = 5
+ const Sysclose : scno = 6
+ const Syswait4 : scno = 7
+ const Syslink : scno = 9
+ const Sysunlink : scno = 10
+ const Syschdir : scno = 12
+ const Sysfchdir : scno = 13
+ const Sysmknod : scno = 14
+ const Syschmod : scno = 15
+ const Syschown : scno = 16
+ const Sysbreak : scno = 17
+ const Sysfreebsd4_getfsstat : scno = 18
+ const Sysgetpid : scno = 20
+ const Sysmount : scno = 21
+ const Sysunmount : scno = 22
+ const Syssetuid : scno = 23
+ const Sysgetuid : scno = 24
+ const Sysgeteuid : scno = 25
+ const Sysptrace : scno = 26
+ const Sysrecvmsg : scno = 27
+ const Syssendmsg : scno = 28
+ const Sysrecvfrom : scno = 29
+ const Sysaccept : scno = 30
+ const Sysgetpeername : scno = 31
+ const Sysgetsockname : scno = 32
+ const Sysaccess : scno = 33
+ const Syschflags : scno = 34
+ const Sysfchflags : scno = 35
+ const Syssync : scno = 36
+ const Syskill : scno = 37
+ const Sysgetppid : scno = 39
+ const Sysdup : scno = 41
+ const Syspipe : scno = 42
+ const Sysgetegid : scno = 43
+ const Sysprofil : scno = 44
+ const Sysktrace : scno = 45
+ const Sysgetgid : scno = 47
+ const Sysgetlogin : scno = 49
+ const Syssetlogin : scno = 50
+ const Sysacct : scno = 51
+ const Syssigaltstack : scno = 53
+ const Sysioctl : scno = 54
+ const Sysreboot : scno = 55
+ const Sysrevoke : scno = 56
+ const Syssymlink : scno = 57
+ const Sysreadlink : scno = 58
+ const Sysexecve : scno = 59
+ const Sysumask : scno = 60
+ const Syschroot : scno = 61
+ const Sysmsync : scno = 65
+ const Sysvfork : scno = 66
+ const Syssbrk : scno = 69
+ const Syssstk : scno = 70
+ const Sysvadvise : scno = 72
+ const Sysmunmap : scno = 73
+ const Sysmprotect : scno = 74
+ const Sysmadvise : scno = 75
+ const Sysmincore : scno = 78
+ const Sysgetgroups : scno = 79
+ const Syssetgroups : scno = 80
+ const Sysgetpgrp : scno = 81
+ const Syssetpgid : scno = 82
+ const Syssetitimer : scno = 83
+ const Sysswapon : scno = 85
+ const Sysgetitimer : scno = 86
+ const Sysgetdtablesize : scno = 89
+ const Sysdup2 : scno = 90
+ const Sysfcntl : scno = 92
+ const Sysselect : scno = 93
+ const Sysfsync : scno = 95
+ const Syssetpriority : scno = 96
+ const Syssocket : scno = 97
+ const Sysconnect : scno = 98
+ const Sysgetpriority : scno = 100
+ const Sysbind : scno = 104
+ const Syssetsockopt : scno = 105
+ const Syslisten : scno = 106
+ const Sysgettimeofday : scno = 116
+ const Sysgetrusage : scno = 117
+ const Sysgetsockopt : scno = 118
+ const Sysreadv : scno = 120
+ const Syswritev : scno = 121
+ const Syssettimeofday : scno = 122
+ const Sysfchown : scno = 123
+ const Sysfchmod : scno = 124
+ const Syssetreuid : scno = 126
+ const Syssetregid : scno = 127
+ const Sysrename : scno = 128
+ const Sysflock : scno = 131
+ const Sysmkfifo : scno = 132
+ const Syssendto : scno = 133
+ const Sysshutdown : scno = 134
+ const Syssocketpair : scno = 135
+ const Sysmkdir : scno = 136
+ const Sysrmdir : scno = 137
+ const Sysutimes : scno = 138
+ const Sysadjtime : scno = 140
+ const Syssetsid : scno = 147
+ const Sysquotactl : scno = 148
+ const Sysnlm_syscall : scno = 154
+ const Sysnfssvc : scno = 155
+ const Sysfreebsd4_statfs : scno = 157
+ const Sysfreebsd4_fstatfs : scno = 158
+ const Syslgetfh : scno = 160
+ const Sysgetfh : scno = 161
+ const Sysfreebsd4_getdomainname : scno = 162
+ const Sysfreebsd4_setdomainname : scno = 163
+ const Sysfreebsd4_uname : scno = 164
+ const Syssysarch : scno = 165
+ const Sysrtprio : scno = 166
+ const Syssemsys : scno = 169
+ const Sysmsgsys : scno = 170
+ const Sysshmsys : scno = 171
+ const Sysfreebsd6_pread : scno = 173
+ const Sysfreebsd6_pwrite : scno = 174
+ const Syssetfib : scno = 175
+ const Sysntp_adjtime : scno = 176
+ const Syssetgid : scno = 181
+ const Syssetegid : scno = 182
+ const Sysseteuid : scno = 183
+ const Sysstat : scno = 188
+ const Sysfstat : scno = 189
+ const Syslstat : scno = 190
+ const Syspathconf : scno = 191
+ const Sysfpathconf : scno = 192
+ const Sysgetrlimit : scno = 194
+ const Syssetrlimit : scno = 195
+ const Sysgetdirentries : scno = 196
+ const Sysfreebsd6_mmap : scno = 197
+ const Sys__syscall : scno = 198
+ const Sysfreebsd6_lseek : scno = 199
+ const Sysfreebsd6_truncate : scno = 200
+ const Sysfreebsd6_ftruncate : scno = 201
+ const Sys__sysctl : scno = 202
+ const Sysmlock : scno = 203
+ const Sysmunlock : scno = 204
+ const Sysundelete : scno = 205
+ const Sysfutimes : scno = 206
+ const Sysgetpgid : scno = 207
+ const Syspoll : scno = 209
+ const Sysfreebsd7___semctl : scno = 220
+ const Syssemget : scno = 221
+ const Syssemop : scno = 222
+ const Sysfreebsd7_msgctl : scno = 224
+ const Sysmsgget : scno = 225
+ const Sysmsgsnd : scno = 226
+ const Sysmsgrcv : scno = 227
+ const Sysshmat : scno = 228
+ const Sysfreebsd7_shmctl : scno = 229
+ const Sysshmdt : scno = 230
+ const Sysshmget : scno = 231
+ const Sysclock_gettime : scno = 232
+ const Sysclock_settime : scno = 233
+ const Sysclock_getres : scno = 234
+ const Sysktimer_create : scno = 235
+ const Sysktimer_delete : scno = 236
+ const Sysktimer_settime : scno = 237
+ const Sysktimer_gettime : scno = 238
+ const Sysktimer_getoverrun : scno = 239
+ const Sysnanosleep : scno = 240
+ const Sysffclock_getcounter : scno = 241
+ const Sysffclock_setestimate : scno = 242
+ const Sysffclock_getestimate : scno = 243
+ const Sysclock_getcpuclockid2 : scno = 247
+ const Sysntp_gettime : scno = 248
+ const Sysminherit : scno = 250
+ const Sysrfork : scno = 251
+ const Sysopenbsd_poll : scno = 252
+ const Sysissetugid : scno = 253
+ const Syslchown : scno = 254
+ const Sysaio_read : scno = 255
+ const Sysaio_write : scno = 256
+ const Syslio_listio : scno = 257
+ const Sysgetdents : scno = 272
+ const Syslchmod : scno = 274
+ const Sysnetbsd_lchown : scno = 275
+ const Syslutimes : scno = 276
+ const Sysnetbsd_msync : scno = 277
+ const Sysnstat : scno = 278
+ const Sysnfstat : scno = 279
+ const Sysnlstat : scno = 280
+ const Syspreadv : scno = 289
+ const Syspwritev : scno = 290
+ const Sysfreebsd4_fhstatfs : scno = 297
+ const Sysfhopen : scno = 298
+ const Sysfhstat : scno = 299
+ const Sysmodnext : scno = 300
+ const Sysmodstat : scno = 301
+ const Sysmodfnext : scno = 302
+ const Sysmodfind : scno = 303
+ const Syskldload : scno = 304
+ const Syskldunload : scno = 305
+ const Syskldfind : scno = 306
+ const Syskldnext : scno = 307
+ const Syskldstat : scno = 308
+ const Syskldfirstmod : scno = 309
+ const Sysgetsid : scno = 310
+ const Syssetresuid : scno = 311
+ const Syssetresgid : scno = 312
+ const Sysaio_return : scno = 314
+ const Sysaio_suspend : scno = 315
+ const Sysaio_cancel : scno = 316
+ const Sysaio_error : scno = 317
+ const Sysoaio_read : scno = 318
+ const Sysoaio_write : scno = 319
+ const Sysolio_listio : scno = 320
+ const Sysyield : scno = 321
+ const Sysmlockall : scno = 324
+ const Sysmunlockall : scno = 325
+ const Sys__getcwd : scno = 326
+ const Syssched_setparam : scno = 327
+ const Syssched_getparam : scno = 328
+ const Syssched_setscheduler : scno = 329
+ const Syssched_getscheduler : scno = 330
+ const Syssched_yield : scno = 331
+ const Syssched_get_priority_max : scno = 332
+ const Syssched_get_priority_min : scno = 333
+ const Syssched_rr_get_interval : scno = 334
+ const Sysutrace : scno = 335
+ const Sysfreebsd4_sendfile : scno = 336
+ const Syskldsym : scno = 337
+ const Sysjail : scno = 338
+ const Sysnnpfs_syscall : scno = 339
+ const Syssigprocmask : scno = 340
+ const Syssigsuspend : scno = 341
+ const Sysfreebsd4_sigaction : scno = 342
+ const Syssigpending : scno = 343
+ const Sysfreebsd4_sigreturn : scno = 344
+ const Syssigtimedwait : scno = 345
+ const Syssigwaitinfo : scno = 346
+ const Sys__acl_get_file : scno = 347
+ const Sys__acl_set_file : scno = 348
+ const Sys__acl_get_fd : scno = 349
+ const Sys__acl_set_fd : scno = 350
+ const Sys__acl_delete_file : scno = 351
+ const Sys__acl_delete_fd : scno = 352
+ const Sys__acl_aclcheck_file : scno = 353
+ const Sys__acl_aclcheck_fd : scno = 354
+ const Sysextattrctl : scno = 355
+ const Sysextattr_set_file : scno = 356
+ const Sysextattr_get_file : scno = 357
+ const Sysextattr_delete_file : scno = 358
+ const Sysaio_waitcomplete : scno = 359
+ const Sysgetresuid : scno = 360
+ const Sysgetresgid : scno = 361
+ const Syskqueue : scno = 362
+ const Syskevent : scno = 363
+ const Sysextattr_set_fd : scno = 371
+ const Sysextattr_get_fd : scno = 372
+ const Sysextattr_delete_fd : scno = 373
+ const Sys__setugid : scno = 374
+ const Syseaccess : scno = 376
+ const Sysafs3_syscall : scno = 377
+ const Sysnmount : scno = 378
+ const Sys__mac_get_proc : scno = 384
+ const Sys__mac_set_proc : scno = 385
+ const Sys__mac_get_fd : scno = 386
+ const Sys__mac_get_file : scno = 387
+ const Sys__mac_set_fd : scno = 388
+ const Sys__mac_set_file : scno = 389
+ const Syskenv : scno = 390
+ const Syslchflags : scno = 391
+ const Sysuuidgen : scno = 392
+ const Syssendfile : scno = 393
+ const Sysmac_syscall : scno = 394
+ const Sysgetfsstat : scno = 395
+ const Sysstatfs : scno = 396
+ const Sysfstatfs : scno = 397
+ const Sysfhstatfs : scno = 398
+ const Sysksem_close : scno = 400
+ const Sysksem_post : scno = 401
+ const Sysksem_wait : scno = 402
+ const Sysksem_trywait : scno = 403
+ const Sysksem_init : scno = 404
+ const Sysksem_open : scno = 405
+ const Sysksem_unlink : scno = 406
+ const Sysksem_getvalue : scno = 407
+ const Sysksem_destroy : scno = 408
+ const Sys__mac_get_pid : scno = 409
+ const Sys__mac_get_link : scno = 410
+ const Sys__mac_set_link : scno = 411
+ const Sysextattr_set_link : scno = 412
+ const Sysextattr_get_link : scno = 413
+ const Sysextattr_delete_link : scno = 414
+ const Sys__mac_execve : scno = 415
+ const Syssigaction : scno = 416
+ const Syssigreturn : scno = 417
+ const Sysgetcontext : scno = 421
+ const Syssetcontext : scno = 422
+ const Sysswapcontext : scno = 423
+ const Sysswapoff : scno = 424
+ const Sys__acl_get_link : scno = 425
+ const Sys__acl_set_link : scno = 426
+ const Sys__acl_delete_link : scno = 427
+ const Sys__acl_aclcheck_link : scno = 428
+ const Syssigwait : scno = 429
+ const Systhr_create : scno = 430
+ const Systhr_exit : scno = 431
+ const Systhr_self : scno = 432
+ const Systhr_kill : scno = 433
+ const Sys_umtx_lock : scno = 434
+ const Sys_umtx_unlock : scno = 435
+ const Sysjail_attach : scno = 436
+ const Sysextattr_list_fd : scno = 437
+ const Sysextattr_list_file : scno = 438
+ const Sysextattr_list_link : scno = 439
+ const Sysksem_timedwait : scno = 441
+ const Systhr_suspend : scno = 442
+ const Systhr_wake : scno = 443
+ const Syskldunloadf : scno = 444
+ const Sysaudit : scno = 445
+ const Sysauditon : scno = 446
+ const Sysgetauid : scno = 447
+ const Syssetauid : scno = 448
+ const Sysgetaudit : scno = 449
+ const Syssetaudit : scno = 450
+ const Sysgetaudit_addr : scno = 451
+ const Syssetaudit_addr : scno = 452
+ const Sysauditctl : scno = 453
+ const Sys_umtx_op : scno = 454
+ const Systhr_new : scno = 455
+ const Syssigqueue : scno = 456
+ const Syskmq_open : scno = 457
+ const Syskmq_setattr : scno = 458
+ const Syskmq_timedreceive : scno = 459
+ const Syskmq_timedsend : scno = 460
+ const Syskmq_notify : scno = 461
+ const Syskmq_unlink : scno = 462
+ const Sysabort2 : scno = 463
+ const Systhr_set_name : scno = 464
+ const Sysaio_fsync : scno = 465
+ const Sysrtprio_thread : scno = 466
+ const Syssctp_peeloff : scno = 471
+ const Syssctp_generic_sendmsg : scno = 472
+ const Syssctp_generic_sendmsg_iov : scno = 473
+ const Syssctp_generic_recvmsg : scno = 474
+ const Syspread : scno = 475
+ const Syspwrite : scno = 476
+ const Sysmmap : scno = 477
+ const Syslseek : scno = 478
+ const Systruncate : scno = 479
+ const Sysftruncate : scno = 480
+ const Systhr_kill2 : scno = 481
+ const Sysshm_open : scno = 482
+ const Sysshm_unlink : scno = 483
+ const Syscpuset : scno = 484
+ const Syscpuset_setid : scno = 485
+ const Syscpuset_getid : scno = 486
+ const Syscpuset_getaffinity : scno = 487
+ const Syscpuset_setaffinity : scno = 488
+ const Sysfaccessat : scno = 489
+ const Sysfchmodat : scno = 490
+ const Sysfchownat : scno = 491
+ const Sysfexecve : scno = 492
+ const Sysfstatat : scno = 493
+ const Sysfutimesat : scno = 494
+ const Syslinkat : scno = 495
+ const Sysmkdirat : scno = 496
+ const Sysmkfifoat : scno = 497
+ const Sysmknodat : scno = 498
+ const Sysopenat : scno = 499
+ const Sysreadlinkat : scno = 500
+ const Sysrenameat : scno = 501
+ const Syssymlinkat : scno = 502
+ const Sysunlinkat : scno = 503
+ const Sysposix_openpt : scno = 504
+ const Sysgssd_syscall : scno = 505
+ const Sysjail_get : scno = 506
+ const Sysjail_set : scno = 507
+ const Sysjail_remove : scno = 508
+ const Sysclosefrom : scno = 509
+ const Sys__semctl : scno = 510
+ const Sysmsgctl : scno = 511
+ const Sysshmctl : scno = 512
+ const Syslpathconf : scno = 513
+ const Sys__cap_rights_get : scno = 515
+ const Syscap_enter : scno = 516
+ const Syscap_getmode : scno = 517
+ const Syspdfork : scno = 518
+ const Syspdkill : scno = 519
+ const Syspdgetpid : scno = 520
+ const Syspselect : scno = 522
+ const Sysgetloginclass : scno = 523
+ const Syssetloginclass : scno = 524
+ const Sysrctl_get_racct : scno = 525
+ const Sysrctl_get_rules : scno = 526
+ const Sysrctl_get_limits : scno = 527
+ const Sysrctl_add_rule : scno = 528
+ const Sysrctl_remove_rule : scno = 529
+ const Sysposix_fallocate : scno = 530
+ const Sysposix_fadvise : scno = 531
+ const Syswait6 : scno = 532
+ const Syscap_rights_limit : scno = 533
+ const Syscap_ioctls_limit : scno = 534
+ const Syscap_ioctls_get : scno = 535
+ const Syscap_fcntls_limit : scno = 536
+ const Syscap_fcntls_get : scno = 537
+ const Sysbindat : scno = 538
+ const Sysconnectat : scno = 539
+ const Syschflagsat : scno = 540
+ const Sysaccept4 : scno = 541
+ const Syspipe2 : scno = 542
+ const Sysaio_mlock : scno = 543
+ const Sysprocctl : scno = 544
+
+ extern const syscall : (sc:scno, args:... -> int64)
+
+ /* process control */
+ const exit : (status:int -> void)
+ const getpid : ( -> int64)
+ const kill : (pid:int64, sig:int64 -> int64)
+ const fork : (-> int64)
+ const wait4 : (pid:int64, loc:int32#, opt : int64, usage:rusage# -> int64)
+ const waitpid : (pid:int64, loc:int32#, opt : int64 -> int64)
+ const execv : (cmd : byte[:], args : byte[:][:] -> int64)
+ const execve : (cmd : byte[:], args : byte[:][:], env : byte[:][:] -> int64)
+ /* wrappers to extract wait status */
+ const waitstatus : (st : int32 -> waitstatus)
+
+ /* fd manipulation */
+ 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 lseek : (fd:fd, off:uint64, whence:int64 -> int64)
+ const stat : (path:byte[:], sb:statbuf# -> int64)
+ const lstat : (path:byte[:], sb:statbuf# -> int64)
+ const fstat : (fd:fd, sb:statbuf# -> int64)
+ const mkdir : (path : byte[:], mode : int64 -> int64)
+ generic ioctl : (fd:fd, req : int64, arg:@a# -> int64)
+ const getdirentries64 : (fd : fd, buf : byte[:], basep : uint64# -> int64)
+
+ /* networking */
+ const socket : (dom : sockfam, stype : socktype, proto : sockproto -> fd)
+ const connect : (sock : fd, addr : sockaddr#, len : size -> int)
+ const accept : (sock : fd, addr : sockaddr#, len : size# -> fd)
+ const listen : (sock : fd, backlog : int -> int)
+ const bind : (sock : fd, addr : sockaddr#, len : size -> int)
+
+ /* memory mapping */
+ const munmap : (addr:byte#, len:size -> int64)
+ const mmap : (addr:byte#, len:size, prot:mprot, flags:mopt, fd:fd, off:off -> byte#)
+
+ /* time - doublecheck if this is right */
+ const clock_getres : (clk : clock, ts : timespec# -> int32)
+ const clock_gettime : (clk : clock, ts : timespec# -> int32)
+ const clock_settime : (clk : clock, ts : timespec# -> int32)
+ const sleep : (time : uint64 -> int32)
+ const nanosleep : (req : timespec#, rem : timespec# -> int32)
+
+ /* system information */
+ const uname : (buf : utsname# -> int)
+ const sysctl : (mib : int[:], old : byte[:]#, new : byte[:] -> int)
+;;
+
+/*
+wraps a syscall argument, converting it to 64 bits for the syscall function. This is
+the same as casting, but more concise than writing castto(int64)
+*/
+generic a = {x : @t
+ -> a(x)
+}
+
+extern const cstring : (str : byte[:] -> byte#)
+extern const alloca : (sz : size -> byte#)
+extern const __cenvp : byte##
+
+/* process management */
+const exit = {status; syscall(Sysexit, a(status))}
+const getpid = {; -> syscall(Sysgetpid, 1)}
+const kill = {pid, sig; -> syscall(Syskill, pid, sig)}
+const fork = {; -> syscall(Sysfork)}
+const wait4 = {pid, loc, opt, usage; -> syscall(Syswait4, pid, loc, opt, usage)}
+const waitpid = {pid, loc, opt;
+ -> wait4(pid, loc, opt, 0 castto(rusage#))
+}
+
+const execv = {cmd, args
+ var p, cargs, i
+
+ /* of course we fucking have to duplicate this code everywhere,
+ * since we want to stack allocate... */
+ p = alloca((args.len + 1)*sizeof(byte#))
+ cargs = (p castto(byte##))[:args.len]
+ for i = 0; i < args.len; i++
+ cargs[i] = cstring(args[i])
+ ;;
+ cargs[args.len] = 0 castto(byte#)
+ -> syscall(Sysexecve, cstring(cmd), a(p), a(__cenvp))
+}
+
+const execve = {cmd, args, env
+ var cargs, cenv, i
+ var p
+
+ /* copy the args */
+ p = alloca((args.len + 1)*sizeof(byte#))
+ cargs = (p castto(byte##))[:args.len]
+ for i = 0; i < args.len; i++
+ cargs[i] = cstring(args[i])
+ ;;
+ cargs[args.len] = 0 castto(byte#)
+
+ /*
+ copy the env.
+ of course we fucking have to duplicate this code everywhere,
+ since we want to stack allocate...
+ */
+ p = alloca((env.len + 1)*sizeof(byte#))
+ cenv = (p castto(byte##))[:env.len]
+ for i = 0; i < env.len; i++
+ cenv[i] = cstring(env[i])
+ ;;
+ cenv[env.len] = 0 castto(byte#)
+
+ -> syscall(Sysexecve, cstring(cmd), a(p), a(cenv))
+}
+
+/* fd manipulation */
+const open = {path, opts; -> syscall(Sysopen, cstring(path), a(opts), a(0o777)) castto(fd)}
+const openmode = {path, opts, mode; -> syscall(Sysopen, cstring(path), a(opts), a(mode)) castto(fd)}
+const close = {fd; -> syscall(Sysclose, a(fd))}
+const creat = {path, mode; -> openmode(path, Ocreat | Otrunc | Owronly, mode) castto(fd)}
+const read = {fd, buf; -> syscall(Sysread, a(fd), buf castto(byte#), a(buf.len)) castto(size)}
+const write = {fd, buf; -> syscall(Syswrite, a(fd), buf castto(byte#), a(buf.len)) castto(size)}
+const lseek = {fd, off, whence; -> syscall(Syslseek, a(fd), a(off), a(whence))}
+const stat = {path, sb; -> syscall(Sysstat, cstring(path), a(sb))}
+const lstat = {path, sb; -> syscall(Syslstat, cstring(path), a(sb))}
+const fstat = {fd, sb; -> syscall(Sysfstat, a(fd), a(sb))}
+const mkdir = {path, mode; -> syscall(Sysmkdir, cstring(path), a(mode)) castto(int64)}
+generic ioctl = {fd, req, arg; -> syscall(Sysioctl, a(fd), a(req), a(arg)) castto(int64)
+}
+const getdirentries64 = {fd, buf, basep; -> syscall(Sysgetdirentries, a(fd), buf castto(byte#), a(buf.len), a(basep))}
+
+/* networking */
+const socket = {dom, stype, proto; -> syscall(Syssocket, a(dom), a(stype), a(proto)) castto(fd) }
+const connect = {sock, addr, len; -> syscall(Sysconnect, a(sock), a(addr), a(len)) castto(int)}
+const accept = {sock, addr, len; -> syscall(Sysaccept, a(sock), a(addr), a(len)) castto(fd)}
+const listen = {sock, backlog; -> syscall(Syslisten, a(sock), a(backlog)) castto(int)}
+const bind = {sock, addr, len; -> syscall(Sysbind, a(sock), a(addr), a(len)) castto(int)}
+
+/* memory management */
+const munmap = {addr, len; -> syscall(Sysmunmap, a(addr), a(len))}
+const mmap = {addr, len, prot, flags, fd, off;
+ -> syscall(Sysmmap, a(addr), a(len), a(prot), a(flags), a(fd), a(off)) castto(byte#)}
+
+/* time */
+const clock_getres = {clk, ts; -> syscall(Sysclock_getres, clockid(clk), a(ts)) castto(int32)}
+const clock_gettime = {clk, ts; -> syscall(Sysclock_gettime, clockid(clk), a(ts)) castto(int32)}
+const clock_settime = {clk, ts; -> syscall(Sysclock_settime, clockid(clk), a(ts)) castto(int32)}
+
+/* system information */
+const uname = {buf; -> syscall(Sysfreebsd4_uname, a(buf)) castto(int)}
+
+const sysctl = {mib, old, new
+ var mibp
+ var mibsz
+ var o
+ var oldp
+ var oldsz
+ var newp
+ var newsz
+ var ret
+
+ mibp = mib castto(byte#)
+ mibsz = a(mib.len)
+ o = old#
+ oldp = o castto(byte#)
+ oldsz = a(o.len)
+ newp = new castto(byte#)
+ newsz = a(new.len)
+
+ /* all args already passed through a() or ar ptrs */
+ ret = syscall(Sys__sysctl, mibp, mibsz, oldp, oldsz, newp, newsz) castto(int)
+
+ old# = o[:oldsz]
+ -> ret
+}
+
+const clockid = {clk
+ match clk
+ | `Clockrealtime: -> 0
+ | `Clockvirtual: -> 1
+ | `Clockprof: -> 2
+ | `Clockmonotonic: -> 4
+ | `Clockuptime: -> 5
+ | `Clockuptime_precise: -> 7
+ | `Clockuptime_fast: -> 8
+ | `Clockrealtime_precise: -> 9
+ | `Clockrealtime_fast: -> 10
+ | `Clockmonotonic_precise: -> 11
+ | `Clockmonotonic_fast: -> 12
+ | `Clocksecond: -> 13
+ ;;
+ -> a(-1)
+}
+
+const waitstatus = {st
+ match st & 0o177
+ | 0: -> `Waitexit (st >> 8)
+ | 0x7f:-> `Waitstop (st >> 8)
+ | sig: -> `Waitsig sig
+ ;;
+}
+
diff --git a/lib/std/sys+linux-x64.myr b/lib/std/sys+linux-x64.myr
new file mode 100644
index 0000000..4860120
--- /dev/null
+++ b/lib/std/sys+linux-x64.myr
@@ -0,0 +1,816 @@
+use "systypes.use"
+
+pkg sys =
+ type scno = int64 /* syscall */
+
+ /* processes/threads */
+ type pid = int /* process id */
+ type tid = int /* thread id */
+ type cloneopt = int64 /* options for clone(2) */
+
+ /* file descriptor manipulation */
+ type fdopt = int64 /* fd options */
+ type fd = int32 /* fd */
+ type whence = uint64 /* seek from whence */
+ type filemode = uint32 /* file open mode */
+
+ type mprot = int64 /* memory protection */
+ type mopt = int64 /* memory mapping options */
+ type socktype = int64 /* socket type */
+ type sockproto = int64 /* socket protocol */
+ type sockfam = uint16 /* socket family */
+
+ type epollflags = uint32
+ type epollop = uint32
+ type epollevttype = uint32
+
+ type pollevt = uint16
+
+ type clock = union
+ `Clockrealtime
+ `Clockmonotonic
+ `Clockproccpu
+ `Clockthreadcpu
+ `Clockmonotonicraw
+ `Clockrealtimecoarse
+ `Clockmonotoniccoarse
+ `Clockboottime
+ `Clockrealtimealarm
+ `Clockboottimealarm
+ ;;
+
+ type waitstatus = union
+ `Waitexit int32
+ `Waitsig int32
+ `Waitstop int32
+ `Waitfail int32
+ ;;
+
+ type timespec = struct
+ sec : uint64
+ nsec : uint64
+ ;;
+
+ type timeval = struct
+ sec : uint64
+ usec : uint64
+ ;;
+
+ type rusage = struct
+ utime : timeval /* user time */
+ stime : timeval /* system time */
+ _opaque : uint64[14] /* padding (darwin-specific data) */
+ ;;
+
+ type statbuf = struct
+ dev : uint64
+ ino : uint64
+ nlink : uint64
+ mode : filemode
+ uid : uint32
+ gid : uint32
+ __pad0 : uint32
+ rdev : uint64
+ size : uint64
+ blksz : uint32
+ blocks : uint64
+ atime : timespec
+ mtime : timespec
+ ctime : timespec
+ __pad1 : uint64[3]
+ ;;
+
+ type dirent64 = struct
+ ino : uint64
+ off : uint64
+ reclen : uint16
+ etype : byte
+ name : byte[...] /* special case; zero length => unchecked indexing */
+ ;;
+
+ type utsname = struct
+ system : byte[65]
+ node : byte[65]
+ release : byte[65]
+ version : byte[65]
+ machine : byte[65]
+ domain : byte[65]
+ ;;
+
+ type sockaddr = struct
+ fam : sockfam
+ data : byte[14]
+ ;;
+
+ type sockaddr_in = struct
+ fam : sockfam
+ port : uint16
+ addr : byte[4]
+ zero : byte[8]
+ ;;
+
+ type sockaddr_in6 = struct
+ fam : sockfam
+ port : uint16
+ addr : byte[16]
+ scope : uint32
+ ;;
+
+ type sockaddr_storage = struct
+ fam : sockfam
+ __align : uint32
+ __pad : byte[112]
+ ;;
+
+ type epollevt = struct
+ events : epollevttype
+ data : byte[8]
+ ;;
+
+ type pollfd = struct
+ fd : fd
+ events : pollevt
+ revents : pollevt
+ ;;
+
+ /* clone options */
+ const Clonesignal : cloneopt = 0xff
+ const Clonevm : cloneopt = 0x100
+ const Clonefs : cloneopt = 0x200
+ const Clonefiles : cloneopt = 0x400
+ const Clonesighand : cloneopt = 0x800
+ const Cloneptrace : cloneopt = 0x2000
+ const Clonevfork : cloneopt = 0x4000
+ const Cloneparent : cloneopt = 0x8000
+ const Clonethread : cloneopt = 0x10000
+ const Clonenewns : cloneopt = 0x20000
+ const Clonesysvsem : cloneopt = 0x40000
+ const Clonesettls : cloneopt = 0x80000
+ const Cloneparentsettid : cloneopt = 0x100000
+ const Clonechildcleartid: cloneopt = 0x200000
+ const Clonedetached : cloneopt = 0x400000
+ const Cloneuntraced : cloneopt = 0x800000
+ const Clonechildsettid : cloneopt = 0x1000000
+ const Clonenewuts : cloneopt = 0x4000000
+ const Clonenewipc : cloneopt = 0x8000000
+ const Clonenewuser : cloneopt = 0x10000000
+ const Clonenewpid : cloneopt = 0x20000000
+ const Clonenewnet : cloneopt = 0x40000000
+ const Cloneio : cloneopt = 0x80000000
+
+ type ptregs = struct
+ ;;
+
+ /* open options */
+ const Ordonly : fdopt = 0x0
+ const Owronly : fdopt = 0x1
+ const Ordwr : fdopt = 0x2
+ const Ocreat : fdopt = 0x40
+ const Oexcl : fdopt = 0x80
+ const Otrunc : fdopt = 0x200
+ const Oappend : fdopt = 0x400
+ const Ondelay : fdopt = 0x800
+ const Odirect : fdopt = 0x4000
+ const Odir : fdopt = 0x10000
+ const Onofollow : fdopt = 0x20000
+
+ /* stat modes */
+ const Sifmt : filemode = 0xf000
+ const Sififo : filemode = 0x1000
+ const Sifchr : filemode = 0x2000
+ const Sifdir : filemode = 0x4000
+ const Sifblk : filemode = 0x6000
+ const Sifreg : filemode = 0x8000
+ const Siflnk : filemode = 0xa000
+ const Sifsock : filemode = 0xc000
+
+ /* mmap protection */
+ const Mprotnone : mprot = 0x0
+ const Mprotrd : mprot = 0x1
+ const Mprotwr : mprot = 0x2
+ const Mprotexec : mprot = 0x4
+ const Mprotrw : mprot = 0x3 /* convenience */
+
+ /* 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
+
+ /* socket families. INCOMPLETE. */
+ const Afunspec : sockfam = 0
+ const Afunix : sockfam = 1
+ const Afinet : sockfam = 2
+ const Afinet6 : sockfam = 10
+
+ /* socket types. */
+ const Sockstream : socktype = 1 /* sequenced, reliable byte stream */
+ const Sockdgram : socktype = 2 /* datagrams */
+ const Sockraw : socktype = 3 /* raw proto */
+ const Sockrdm : socktype = 4 /* reliably delivered messages */
+ const Sockseqpacket : socktype = 5 /* sequenced, reliable packets */
+ const Sockdccp : socktype = 6 /* data congestion control protocol */
+ const Sockpack : socktype = 10 /* linux specific packet */
+
+ /* network protocols */
+ const Ipproto_ip : sockproto = 0
+ const Ipproto_icmp : sockproto = 1
+ const Ipproto_tcp : sockproto = 6
+ const Ipproto_udp : sockproto = 17
+ const Ipproto_raw : sockproto = 255
+
+ /* epoll flags */
+ const Epollcloexec : epollflags = 0o2000000
+
+ /* epoll ops */
+ const Epollctladd : epollop = 0
+ const Epollctlmod : epollop = 1
+ const Epollctldel : epollop = 1
+
+ /* epoll events */
+ const Epollin : epollevttype = 0x001
+ const Epollpri : epollevttype = 0x002
+ const Epollout : epollevttype = 0x004
+ const Epollerr : epollevttype = 0x008
+ const Epollhup : epollevttype = 0x010
+ const Epollrdnorm : epollevttype = 0x040
+ const Epollrdband : epollevttype = 0x080
+ const Epollwrnorm : epollevttype = 0x100
+ const Epollwrband : epollevttype = 0x200
+ const Epollmsg : epollevttype = 0x400
+ const Epollrdhup : epollevttype = 0x2000
+ const Epollwakeup : epollevttype = 1 << 29
+ const Epolloneshot : epollevttype = 1 << 30
+ const Epolledge : epollevttype = 1 << 31
+
+ /* poll events : posix */
+ const Pollin : pollevt = 0x001 /* There is data to read. */
+ const Pollpri : pollevt = 0x002 /* There is urgent data to read. */
+ const Pollout : pollevt = 0x004 /* Writing now will not block. */
+
+ /* poll events: xopen */
+ const Pollrdnorm : pollevt = 0x040 /* Normal data may be read. */
+ const Pollrdband : pollevt = 0x080 /* Priority data may be read. */
+ const Pollwrnorm : pollevt = 0x100 /* Writing now will not block. */
+ const Pollwrband : pollevt = 0x200 /* Priority data may be written. */
+
+ /* poll events: linux */
+ const Pollmsg : pollevt = 0x400
+ const Pollremove : pollevt = 0x1000
+ const Pollrdhup : pollevt = 0x2000
+
+ const Seekset : whence = 0
+ const Seekcur : whence = 1
+ const Seekend : whence = 2
+
+ /* return value for a failed mapping */
+ const Mapbad : byte# = -1 castto(byte#)
+
+ /* syscalls */
+ const Sysread : scno = 0
+ const Syswrite : scno = 1
+ const Sysopen : scno = 2
+ const Sysclose : scno = 3
+ const Sysstat : scno = 4
+ const Sysfstat : scno = 5
+ const Syslstat : scno = 6
+ const Syspoll : scno = 7
+ const Syslseek : scno = 8
+ const Sysmmap : scno = 9
+ const Sysmprotect : scno = 10
+ const Sysmunmap : scno = 11
+ const Sysbrk : scno = 12
+ const Sysrt_sigaction : scno = 13
+ const Sysrt_sigprocmask : scno = 14
+ const Sysrt_sigreturn : scno = 15
+ const Sysioctl : scno = 16
+ const Syspread64 : scno = 17
+ const Syspwrite64 : scno = 18
+ const Sysreadv : scno = 19
+ const Syswritev : scno = 20
+ const Sysaccess : scno = 21
+ const Syspipe : scno = 22
+ const Sysselect : scno = 23
+ const Syssched_yield : scno = 24
+ const Sysmremap : scno = 25
+ const Sysmsync : scno = 26
+ const Sysmincore : scno = 27
+ const Sysmadvise : scno = 28
+ const Sysshmget : scno = 29
+ const Sysshmat : scno = 30
+ const Sysshmctl : scno = 31
+ const Sysdup : scno = 32
+ const Sysdup2 : scno = 33
+ const Syspause : scno = 34
+ const Sysnanosleep : scno = 35
+ const Sysgetitimer : scno = 36
+ const Sysalarm : scno = 37
+ const Syssetitimer : scno = 38
+ const Sysgetpid : scno = 39
+ const Syssendfile : scno = 40
+ const Syssocket : scno = 41
+ const Sysconnect : scno = 42
+ const Sysaccept : scno = 43
+ const Syssendto : scno = 44
+ const Sysrecvfrom : scno = 45
+ const Syssendmsg : scno = 46
+ const Sysrecvmsg : scno = 47
+ const Sysshutdown : scno = 48
+ const Sysbind : scno = 49
+ const Syslisten : scno = 50
+ const Sysgetsockname : scno = 51
+ const Sysgetpeername : scno = 52
+ const Syssocketpair : scno = 53
+ const Syssetsockopt : scno = 54
+ const Sysgetsockopt : scno = 55
+ const Sysclone : scno = 56
+ const Sysfork : scno = 57
+ const Sysvfork : scno = 58
+ const Sysexecve : scno = 59
+ const Sysexit : scno = 60
+ const Syswait4 : scno = 61
+ const Syskill : scno = 62
+ const Sysuname : scno = 63
+ const Syssemget : scno = 64
+ const Syssemop : scno = 65
+ const Syssemctl : scno = 66
+ const Sysshmdt : scno = 67
+ const Sysmsgget : scno = 68
+ const Sysmsgsnd : scno = 69
+ const Sysmsgrcv : scno = 70
+ const Sysmsgctl : scno = 71
+ const Sysfcntl : scno = 72
+ const Sysflock : scno = 73
+ const Sysfsync : scno = 74
+ const Sysfdatasync : scno = 75
+ const Systruncate : scno = 76
+ const Sysftruncate : scno = 77
+ const Sysgetdents : scno = 78
+ const Sysgetcwd : scno = 79
+ const Syschdir : scno = 80
+ const Sysfchdir : scno = 81
+ const Sysrename : scno = 82
+ const Sysmkdir : scno = 83
+ const Sysrmdir : scno = 84
+ const Syscreat : scno = 85
+ const Syslink : scno = 86
+ const Sysunlink : scno = 87
+ const Syssymlink : scno = 88
+ const Sysreadlink : scno = 89
+ const Syschmod : scno = 90
+ const Sysfchmod : scno = 91
+ const Syschown : scno = 92
+ const Sysfchown : scno = 93
+ const Syslchown : scno = 94
+ const Sysumask : scno = 95
+ const Sysgettimeofday : scno = 96
+ const Sysgetrlimit : scno = 97
+ const Sysgetrusage : scno = 98
+ const Syssysinfo : scno = 99
+ const Systimes : scno = 100
+ const Sysptrace : scno = 101
+ const Sysgetuid : scno = 102
+ const Syssyslog : scno = 103
+ const Sysgetgid : scno = 104
+ const Syssetuid : scno = 105
+ const Syssetgid : scno = 106
+ const Sysgeteuid : scno = 107
+ const Sysgetegid : scno = 108
+ const Syssetpgid : scno = 109
+ const Sysgetppid : scno = 110
+ const Sysgetpgrp : scno = 111
+ const Syssetsid : scno = 112
+ const Syssetreuid : scno = 113
+ const Syssetregid : scno = 114
+ const Sysgetgroups : scno = 115
+ const Syssetgroups : scno = 116
+ const Syssetresuid : scno = 117
+ const Sysgetresuid : scno = 118
+ const Syssetresgid : scno = 119
+ const Sysgetresgid : scno = 120
+ const Sysgetpgid : scno = 121
+ const Syssetfsuid : scno = 122
+ const Syssetfsgid : scno = 123
+ const Sysgetsid : scno = 124
+ const Syscapget : scno = 125
+ const Syscapset : scno = 126
+ const Sysrt_sigpending : scno = 127
+ const Sysrt_sigtimedwait : scno = 128
+ const Sysrt_sigqueueinfo : scno = 129
+ const Sysrt_sigsuspend : scno = 130
+ const Syssigaltstack : scno = 131
+ const Sysutime : scno = 132
+ const Sysmknod : scno = 133
+ const Sysuselib : scno = 134
+ const Syspersonality : scno = 135
+ const Sysustat : scno = 136
+ const Sysstatfs : scno = 137
+ const Sysfstatfs : scno = 138
+ const Syssysfs : scno = 139
+ const Sysgetpriority : scno = 140
+ const Syssetpriority : scno = 141
+ const Syssched_setparam : scno = 142
+ const Syssched_getparam : scno = 143
+ const Syssched_setscheduler : scno = 144
+ const Syssched_getscheduler : scno = 145
+ const Syssched_get_priority_max : scno = 146
+ const Syssched_get_priority_min : scno = 147
+ const Syssched_rr_get_interval : scno = 148
+ const Sysmlock : scno = 149
+ const Sysmunlock : scno = 150
+ const Sysmlockall : scno = 151
+ const Sysmunlockall : scno = 152
+ const Sysvhangup : scno = 153
+ const Sysmodify_ldt : scno = 154
+ const Syspivot_root : scno = 155
+ const Sys_sysctl : scno = 156
+ const Sysprctl : scno = 157
+ const Sysarch_prctl : scno = 158
+ const Sysadjtimex : scno = 159
+ const Syssetrlimit : scno = 160
+ const Syschroot : scno = 161
+ const Syssync : scno = 162
+ const Sysacct : scno = 163
+ const Syssettimeofday : scno = 164
+ const Sysmount : scno = 165
+ const Sysumount2 : scno = 166
+ const Sysswapon : scno = 167
+ const Sysswapoff : scno = 168
+ const Sysreboot : scno = 169
+ const Syssethostname : scno = 170
+ const Syssetdomainname : scno = 171
+ const Sysiopl : scno = 172
+ const Sysioperm : scno = 173
+ const Syscreate_module : scno = 174
+ const Sysinit_module : scno = 175
+ const Sysdelete_module : scno = 176
+ const Sysget_kernel_syms : scno = 177
+ const Sysquery_module : scno = 178
+ const Sysquotactl : scno = 179
+ const Sysnfsservctl : scno = 180
+ const Sysgetpmsg : scno = 181
+ const Sysputpmsg : scno = 182
+ const Sysafs_syscall : scno = 183
+ const Systuxcall : scno = 184
+ const Syssecurity : scno = 185
+ const Sysgettid : scno = 186
+ const Sysreadahead : scno = 187
+ const Syssetxattr : scno = 188
+ const Syslsetxattr : scno = 189
+ const Sysfsetxattr : scno = 190
+ const Sysgetxattr : scno = 191
+ const Syslgetxattr : scno = 192
+ const Sysfgetxattr : scno = 193
+ const Syslistxattr : scno = 194
+ const Sysllistxattr : scno = 195
+ const Sysflistxattr : scno = 196
+ const Sysremovexattr : scno = 197
+ const Syslremovexattr : scno = 198
+ const Sysfremovexattr : scno = 199
+ const Systkill : scno = 200
+ const Systime : scno = 201
+ const Sysfutex : scno = 202
+ const Syssched_setaffinity : scno = 203
+ const Syssched_getaffinity : scno = 204
+ const Sysset_thread_area : scno = 205
+ const Sysio_setup : scno = 206
+ const Sysio_destroy : scno = 207
+ const Sysio_getevents : scno = 208
+ const Sysio_submit : scno = 209
+ const Sysio_cancel : scno = 210
+ const Sysget_thread_area : scno = 211
+ const Syslookup_dcookie : scno = 212
+ const Sysepoll_create : scno = 213
+ const Sysepoll_ctl_old : scno = 214
+ const Sysepoll_wait_old : scno = 215
+ const Sysremap_file_pages : scno = 216
+ const Sysgetdents64 : scno = 217
+ const Sysset_tid_address : scno = 218
+ const Sysrestart_syscall : scno = 219
+ const Syssemtimedop : scno = 220
+ const Sysfadvise64 : scno = 221
+ const Systimer_create : scno = 222
+ const Systimer_settime : scno = 223
+ const Systimer_gettime : scno = 224
+ const Systimer_getoverrun : scno = 225
+ const Systimer_delete : scno = 226
+ const Sysclock_settime : scno = 227
+ const Sysclock_gettime : scno = 228
+ const Sysclock_getres : scno = 229
+ const Sysclock_nanosleep : scno = 230
+ const Sysexit_group : scno = 231
+ const Sysepoll_wait : scno = 232
+ const Sysepoll_ctl : scno = 233
+ const Systgkill : scno = 234
+ const Sysutimes : scno = 235
+ const Sysvserver : scno = 236
+ const Sysmbind : scno = 237
+ const Sysset_mempolicy : scno = 238
+ const Sysget_mempolicy : scno = 239
+ const Sysmq_open : scno = 240
+ const Sysmq_unlink : scno = 241
+ const Sysmq_timedsend : scno = 242
+ const Sysmq_timedreceive : scno = 243
+ const Sysmq_notify : scno = 244
+ const Sysmq_getsetattr : scno = 245
+ const Syskexec_load : scno = 246
+ const Syswaitid : scno = 247
+ const Sysadd_key : scno = 248
+ const Sysrequest_key : scno = 249
+ const Syskeyctl : scno = 250
+ const Sysioprio_set : scno = 251
+ const Sysioprio_get : scno = 252
+ const Sysinotify_init : scno = 253
+ const Sysinotify_add_watch : scno = 254
+ const Sysinotify_rm_watch : scno = 255
+ const Sysmigrate_pages : scno = 256
+ const Sysopenat : scno = 257
+ const Sysmkdirat : scno = 258
+ const Sysmknodat : scno = 259
+ const Sysfchownat : scno = 260
+ const Sysfutimesat : scno = 261
+ const Sysnewfstatat : scno = 262
+ const Sysunlinkat : scno = 263
+ const Sysrenameat : scno = 264
+ const Syslinkat : scno = 265
+ const Syssymlinkat : scno = 266
+ const Sysreadlinkat : scno = 267
+ const Sysfchmodat : scno = 268
+ const Sysfaccessat : scno = 269
+ const Syspselect6 : scno = 270
+ const Sysppoll : scno = 271
+ const Sysunshare : scno = 272
+ const Sysset_robust_list : scno = 273
+ const Sysget_robust_list : scno = 274
+ const Syssplice : scno = 275
+ const Systee : scno = 276
+ const Syssync_file_range : scno = 277
+ const Sysvmsplice : scno = 278
+ const Sysmove_pages : scno = 279
+ const Sysutimensat : scno = 280
+ const Sysepoll_pwait : scno = 281
+ const Syssignalfd : scno = 282
+ const Systimerfd_create : scno = 283
+ const Syseventfd : scno = 284
+ const Sysfallocate : scno = 285
+ const Systimerfd_settime : scno = 286
+ const Systimerfd_gettime : scno = 287
+ const Sysaccept4 : scno = 288
+ const Syssignalfd4 : scno = 289
+ const Syseventfd2 : scno = 290
+ const Sysepoll_create1 : scno = 291
+ const Sysdup3 : scno = 292
+ const Syspipe2 : scno = 293
+ const Sysinotify_init1 : scno = 294
+ const Syspreadv : scno = 295
+ const Syspwritev : scno = 296
+ const Sysrt_tgsigqueueinfo : scno = 297
+ const Sysperf_event_open : scno = 298
+ const Sysrecvmmsg : scno = 299
+ const Sysfanotify_init : scno = 300
+ const Sysfanotify_mark : scno = 301
+ const Sysprlimit64 : scno = 302
+ const Sysname_to_handle_at : scno = 303
+ const Sysopen_by_handle_at : scno = 304
+ const Sysclock_adjtime : scno = 305
+ const Syssyncfs : scno = 306
+ const Syssendmmsg : scno = 307
+ const Syssetns : scno = 308
+ const Sysgetcpu : scno = 309
+ const Sysprocess_vm_readv : scno = 310
+ const Sysprocess_vm_writev : scno = 311
+
+ /* getting to the os */
+ extern const syscall : (sc:scno, args:... -> int64)
+
+ /* process management */
+ const exit : (status:int -> void)
+ const exit_group : (status:int -> void)
+ const getpid : ( -> pid)
+ const kill : (pid:pid, sig:int64 -> int64)
+ const fork : (-> pid)
+ /* FIXME: where the fuck is 'struct pt_reg' defined?? */
+ const clone : (flags : cloneopt, stk : byte#, ptid : pid#, ctid : pid#, ptreg : byte# -> pid)
+ const wait4 : (pid:pid, loc:int32#, opt : int64, usage:rusage# -> int64)
+ const waitpid : (pid:pid, loc:int32#, opt : int64 -> int64)
+ const execv : (cmd : byte[:], args : byte[:][:] -> int64)
+ const execve : (cmd : byte[:], args : byte[:][:], env : byte[:][:] -> int64)
+ /* wrappers to extract wait status */
+ const waitstatus : (st : int32 -> waitstatus)
+
+ /* file manipulation */
+ 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 unlink : (path:byte[:] -> int)
+ const read : (fd:fd, buf:byte[:] -> size)
+ const write : (fd:fd, buf:byte[:] -> size)
+ const lseek : (fd:fd, off:off, whence:whence -> int64)
+ const stat : (path:byte[:], sb:statbuf# -> int64)
+ const lstat : (path:byte[:], sb:statbuf# -> int64)
+ const fstat : (fd:fd, sb:statbuf# -> int64)
+ const mkdir : (path : byte[:], mode : int64 -> int64)
+ generic ioctl : (fd:fd, req : int64, arg:@a# -> int64)
+ const getdents64 : (fd:fd, buf : byte[:] -> int64)
+ const chdir : (p : byte[:] -> int64)
+ const getcwd : (buf : byte[:] -> int64)
+
+ /* fd stuff */
+ const pipe : (fds : fd[2]# -> int64)
+ const dup : (fd : fd -> fd)
+ const dup2 : (src : fd, dst : fd -> fd)
+
+ /* threading */
+ const futex : (uaddr : int32#, op : int32, val : int32, \
+ ts : timespec#, uaddr2 : int#, val3 : int# -> int64)
+
+ /* polling */
+ const epollcreate : (flg : epollflags -> fd) /* actually epoll_create1 */
+ const epollctl : (epfd : fd, op : int, fd : fd, evt : epollevt# -> int)
+ const epollwait : (epfd : fd, evts : epollevt[:], timeout : int -> int)
+ const poll : (pfd : pollfd[:], timeout : int -> int)
+
+ /* networking */
+ const socket : (dom : sockfam, stype : socktype, proto : sockproto -> fd)
+ const connect : (sock : fd, addr : sockaddr#, len : size -> int)
+ const accept : (sock : fd, addr : sockaddr#, len : size# -> fd)
+ const listen : (sock : fd, backlog : int -> int)
+ const bind : (sock : fd, addr : sockaddr#, len : size -> int)
+
+ /* memory mapping */
+ const munmap : (addr:byte#, len:size -> int64)
+ const mmap : (addr:byte#, len:size, prot:mprot, flags:mopt, fd:fd, off:off -> byte#)
+
+ /* time */
+ const clock_getres : (clk : clock, ts : timespec# -> int32)
+ const clock_gettime : (clk : clock, ts : timespec# -> int32)
+ const clock_settime : (clk : clock, ts : timespec# -> int32)
+ const sleep : (time : uint64 -> int32)
+ const nanosleep : (req : timespec#, rem : timespec# -> int32)
+
+ /* system information */
+ const uname : (buf : utsname# -> int)
+
+ /* exported values: initialized by start code */
+ extern const __environment : byte[:][:]
+ extern const __cenvp : byte##
+;;
+
+/*
+wraps a syscall argument, converting it to 64 bits for the syscall function.
+This is the same as casting, but more concise than writing castto(int64).
+*/
+generic a = {x : @t; -> x castto(uint64)}
+
+/* asm stubs from util.s */
+extern const cstring : (str : byte[:] -> byte#)
+extern const alloca : (sz : size -> byte#)
+
+/* process management */
+const exit = {status; syscall(Sysexit, a(status))}
+const exit_group = {status; syscall(Sysexit_group, a(status))}
+const getpid = {; -> syscall(Sysgetpid) castto(pid)}
+const kill = {pid, sig; -> syscall(Syskill, a(pid), a(sig))}
+const fork = {; -> syscall(Sysfork) castto(pid)}
+const clone = {flags, stk, ptid, ctid, ptreg; -> syscall(Sysclone, a(flags), a(stk), a(ptid), a(ctid), a(ptreg)) castto(pid)}
+const wait4 = {pid, loc, opt, usage; -> syscall(Syswait4, a(pid), a(loc), a(opt), a(usage))}
+const waitpid = {pid, loc, opt;
+ var rusage
+ -> wait4(pid, loc, opt, &rusage)
+}
+
+const execv = {cmd, args
+ var p, cargs, i
+
+ /* of course we fucking have to duplicate this code everywhere,
+ * since we want to stack allocate... */
+ p = alloca((args.len + 1)*sizeof(byte#))
+ cargs = (p castto(byte##))[:args.len + 1]
+ for i = 0; i < args.len; i++
+ cargs[i] = cstring(args[i])
+ ;;
+ cargs[args.len] = 0 castto(byte#)
+ -> syscall(Sysexecve, cstring(cmd), a(p), a(__cenvp))
+}
+
+const execve = {cmd, args, env
+ var cargs, cenv, i
+ var p
+
+ /* copy the args */
+ p = alloca((args.len + 1)*sizeof(byte#))
+ cargs = (p castto(byte##))[:args.len + 1]
+ for i = 0; i < args.len; i++
+ cargs[i] = cstring(args[i])
+ ;;
+ cargs[args.len] = 0 castto(byte#)
+
+ /*
+ copy the env.
+ of course we fucking have to duplicate this code everywhere,
+ since we want to stack allocate...
+ */
+ p = alloca((env.len + 1)*sizeof(byte#))
+ cenv = (p castto(byte##))[:env.len]
+ for i = 0; i < env.len; i++
+ cenv[i] = cstring(env[i])
+ ;;
+ cenv[env.len] = 0 castto(byte#)
+
+ -> syscall(Sysexecve, cstring(cmd), a(p), a(cenv))
+}
+
+/* file manipulation */
+const open = {path, opts; -> syscall(Sysopen, cstring(path), a(opts), a(0o777)) castto(fd)}
+const openmode = {path, opts, mode; -> syscall(Sysopen, cstring(path), a(opts), a(mode)) castto(fd)}
+const close = {fd; -> syscall(Sysclose, a(fd))}
+const creat = {path, mode; -> syscall(Syscreat, cstring(path), a(mode)) castto(fd)}
+const unlink = {path; -> syscall(Sysunlink, cstring(path)) castto(int)}
+const read = {fd, buf; -> syscall(Sysread, a(fd), buf castto(byte#), a(buf.len)) castto(size)}
+const write = {fd, buf; -> syscall(Syswrite, a(fd), buf castto(byte#), a(buf.len)) castto(size)}
+const lseek = {fd, off, whence; -> syscall(Syslseek, a(fd), a(off), a(whence))}
+const stat = {path, sb; -> syscall(Sysstat, cstring(path), a(sb))}
+const lstat = {path, sb; -> syscall(Syslstat, cstring(path), a(sb))}
+const fstat = {fd, sb; -> syscall(Sysfstat, a(fd), a(sb))}
+const mkdir = {path, mode; -> syscall(Sysmkdir, cstring(path), a(mode)) castto(int64)}
+generic ioctl = {fd, req, arg; -> syscall(Sysioctl, a(fd), a(req), a(arg)) castto(int64)}
+const getdents64 = {fd, buf; -> syscall(Sysgetdents64, a(fd), buf castto(byte#), a(buf.len))}
+const chdir = {dir; -> syscall(Syschdir, cstring(dir))}
+const getcwd = {buf; -> syscall(Sysgetcwd, a(buf), a(buf.len))}
+
+/* file stuff */
+const pipe = {fds; -> syscall(Syspipe, a(fds))}
+const dup = {fd; -> syscall(Sysdup, a(fd)) castto(fd)}
+const dup2 = {src, dst; -> syscall(Sysdup2, a(src), a(dst)) castto(fd)}
+
+/* threading */
+const futex = {uaddr, op, val, timeout, uaddr2, val3
+ -> syscall(Sysfutex, a(uaddr), a(op), a(val), a(timeout), a(uaddr2), a(val3))}
+
+/* poll */
+const poll = {pfd, timeout; -> syscall(Syspoll, pfd castto(pollfd#), a(pfd.len), a(timeout)) castto(int)}
+const epollctl = {epfd, op, fd, evt;
+ -> syscall(Sysepoll_ctl, a(epfd), a(op), a(fd), a(evt)) castto(int)}
+const epollwait = {epfd, evts, timeout;
+ -> syscall(Sysepoll_wait, a(epfd), evts castto(epollevt#), a(evts.len), a(timeout)) castto(int)}
+const epollcreate = {flg; -> syscall(Sysepoll_create1, a(flg)) castto(fd)}
+
+/* networking */
+const socket = {dom, stype, proto; -> syscall(Syssocket, a(dom), a(stype), a(proto)) castto(fd)}
+const connect = {sock, addr, len; -> syscall(Sysconnect, a(sock), a(addr), a(len)) castto(int)}
+const bind = {sock, addr, len; -> syscall(Sysbind, a(sock), a(addr), a(len)) castto(int)}
+const listen = {sock, backlog; -> syscall(Syslisten, a(sock), a(backlog)) castto(int)}
+const accept = {sock, addr, lenp; -> syscall(Sysaccept, a(sock), a(addr), a(lenp)) castto(fd)}
+
+/* memory mapping */
+const munmap = {addr, len; -> syscall(Sysmunmap, a(addr), a(len))}
+const mmap = {addr, len, prot, flags, fd, off;
+ -> syscall(Sysmmap, a(addr), a(len), a(prot), a(flags), a(fd), a(off)) castto(byte#)
+}
+
+/* time */
+const clock_getres = {clk, ts; -> syscall(Sysclock_getres, clockid(clk), a(ts)) castto(int32)}
+const clock_gettime = {clk, ts; -> syscall(Sysclock_gettime, clockid(clk), a(ts)) castto(int32)}
+const clock_settime = {clk, ts; -> syscall(Sysclock_settime, clockid(clk), a(ts)) castto(int32)}
+
+const sleep = {time
+ var req, rem
+ req = [.sec = time, .nsec = 0]
+ -> nanosleep(&req, &rem)
+}
+
+const nanosleep = {req, rem; -> syscall(Sysnanosleep, a(req), a(rem)) castto(int32)}
+
+/* system information */
+const uname = {buf; -> syscall(Sysuname, buf) castto(int)}
+
+const clockid = {clk
+ match clk
+ | `Clockrealtime: -> 0
+ | `Clockmonotonic: -> 1
+ | `Clockproccpu: -> 2
+ | `Clockthreadcpu: -> 3
+ | `Clockmonotonicraw: -> 4
+ | `Clockrealtimecoarse: -> 5
+ | `Clockmonotoniccoarse:-> 6
+ | `Clockboottime: -> 7
+ | `Clockrealtimealarm: -> 8
+ | `Clockboottimealarm: -> 9
+ ;;
+ -> -1
+}
+
+
+const waitstatus = {st
+ if st & 0x7f == 0 /* if exited */
+ -> `Waitexit ((st & 0xff00) >> 8)
+ elif ((st & 0xffff)-1) < 0xff /* if signaled */
+ -> `Waitsig ((st) & 0x7f)
+ elif (((st & 0xffff)*0x10001)>>8) > 0x7f00
+ -> `Waitstop ((st & 0xff00) >> 8)
+ ;;
+ -> `Waitfail st /* wait failed to give a result */
+}
diff --git a/lib/std/sys+osx-x64.myr b/lib/std/sys+osx-x64.myr
new file mode 100644
index 0000000..ca2a63a
--- /dev/null
+++ b/lib/std/sys+osx-x64.myr
@@ -0,0 +1,947 @@
+use "systypes.use"
+
+pkg sys =
+ type scno = int64 /* syscall */
+ type fdopt = int64 /* fd options */
+ type fd = int32 /* fd */
+ type pid = int64 /* pid */
+ type mprot = int64 /* memory protection */
+ type mopt = int64 /* memory mapping options */
+ type socktype = int64 /* socket type */
+ type sockproto = int64 /* socket protocol */
+ type sockfam = uint8 /* socket family */
+ type filemode = uint16 /* file permission bits */
+ type kflags = uint16 /* kqueue flags */
+ type whence = int64
+ type fcntlcmd = int64
+
+ type timespec = struct
+ sec : uint64
+ nsec : uint32
+ ;;
+
+ type timeval = struct
+ sec : uint64
+ usec : uint32
+ ;;
+
+ type timezone = struct
+ minwest : int32 /* of greenwich */
+ dsttime : int32 /* nonzero if DST applies */
+ ;;
+
+ type clock = union
+ `Clockrealtime
+ `Clockmonotonic
+ ;;
+
+ type waitstatus = union
+ `Waitexit int32
+ `Waitsig int32
+ `Waitstop int32
+ `Waitfail int32
+ ;;
+
+ type statbuf = struct
+ dev : int32
+ mode : filemode
+ nlink : uint16
+ ino : uint64
+ uid : uint32
+ gid : uint32
+ rdev : uint32
+ atime : timespec
+ mtime : timespec
+ ctime : timespec
+ birthtimespec : timespec
+ size : off
+ blocks : int64
+ blksize : int32
+ flags : uint32
+ gen : uint32
+ _spare : uint32
+ _qspare : uint64[2]
+ ;;
+
+ type rusage = struct
+ utime : timeval /* user time */
+ stime : timeval /* system time */
+ _opaque : uint64[14] /* padding (darwin-specific data) */
+ ;;
+
+ type utsname = struct
+ system : byte[256]
+ node : byte[256]
+ release : byte[256]
+ version : byte[256]
+ machine : byte[256]
+ ;;
+
+ type sockaddr = struct
+ len : byte
+ fam : sockfam
+ data : byte[14] /* what is the *actual* length? */
+ ;;
+
+ type sockaddr_in = struct
+ len : byte
+ fam : sockfam
+ port : uint16
+ addr : byte[4]
+ zero : byte[8]
+ ;;
+
+ type sockaddr_in6 = struct
+ len : byte
+ fam : sockfam
+ port : uint16
+ flowinf : uint32
+ addr : byte[16]
+ scope : uint32
+ ;;
+
+
+ type sockaddr_storage = struct
+ len : byte
+ fam : sockfam
+ __pad1 : byte[6]
+ __align : uint64
+ __pad2 : byte[112]
+ ;;
+
+ type dirent64 = struct
+ ino : uint64
+ seekoff : uint64 /* seek offset (optional, used by servers) */
+ reclen : uint16 /* length of this record */
+ namlen : uint16 /* length of string in d_name */
+ typeid : uint8 /* file type, see below */
+ name : byte[...]
+ ;;
+
+ type kevent = struct
+ ident : intptr /* identifier for this event */
+ filter : int16 /* filter for event */
+ flags : uint16 /* general flags */
+ fflags : uint32 /* filter-specific flags */
+ data : intptr /* filter-specific data */
+ udata : byte# /* opaque user data identifier */
+ ;;
+
+ type kevent64 = struct
+ ident : uint64 /* identifier for this event */
+ filter : int16 /* filter for event */
+ flags : kflags /* general flags */
+ fflags : uint32 /* filter-specific flags */
+ data : int64 /* filter-specific data */
+ udata : uint64 /* opaque user data identifier */
+ ext : uint64[2] /* filter-specific extensions */
+ ;;
+
+ const Seekset : whence = 0
+ const Seekcur : whence = 1
+ const Seekend : whence = 2
+
+ /* system specific constants */
+ const Maxpathlen : size = 1024
+
+ /* fcntl constants */
+ const Fdupfd : fcntlcmd = 0 /* duplicate file descriptor */
+ const Fgetfd : fcntlcmd = 1 /* get file descriptor flags */
+ const Fsetfd : fcntlcmd = 2 /* set file descriptor flags */
+ const Fgetfl : fcntlcmd = 3 /* get file status flags */
+ const Fsetfl : fcntlcmd = 4 /* set file status flags */
+ const Fgetown : fcntlcmd = 5 /* get SIGIO/SIGURG proc/pgrp */
+ const Fsetown : fcntlcmd = 6 /* set SIGIO/SIGURG proc/pgrp */
+ const Fgetlk : fcntlcmd = 7 /* get record locking information */
+ const Fsetlk : fcntlcmd = 8 /* set record locking information */
+ const Fsetlkw : fcntlcmd = 9 /* F_SETLK; wait if blocked */
+ const Fsetlkwtimeout : fcntlcmd = 10 /* F_SETLK; wait if blocked, return on timeout */
+ const Fflush_data : fcntlcmd = 40
+ const Fchkclean : fcntlcmd = 41 /* Used for regression test */
+ const Fpreallocate : fcntlcmd = 42 /* Preallocate storage */
+ const Fsetsize : fcntlcmd = 43 /* Truncate a file without zeroing space */
+ const Frdadvise : fcntlcmd = 44 /* Issue an advisory read async with no copy to user */
+ const Frdahead : fcntlcmd = 45 /* turn read ahead off/on for this fd */
+ /* 46,47 used to be F_READBOOTSTRAP and F_WRITEBOOTSTRAP */
+ const Fnocache : fcntlcmd = 48 /* turn data caching off/on for this fd */
+ const Flog2phys : fcntlcmd = 49 /* file offset to device offset */
+ const Fgetpath : fcntlcmd = 50 /* return the full path of the fd */
+ const Ffullfsync : fcntlcmd = 51 /* fsync + ask the drive to flush to the media */
+ const Fpathpkg_check : fcntlcmd = 52 /* find which component (if any) is a package */
+ const Ffreeze_fs : fcntlcmd = 53 /* "freeze" all fs operations */
+ const Fthaw_fs : fcntlcmd = 54 /* "thaw" all fs operations */
+ const Fglobal_nocache : fcntlcmd = 55 /* turn data caching off/on (globally) for this file */
+ const Faddsigs : fcntlcmd = 59 /* add detached signatures */
+ const Faddfilesigs : fcntlcmd = 61 /* add signature from same file (used by dyld for shared libs) */
+ const Fgetprotclass : fcntlcmd = 63 /* Get the protection class of a file from the EA, returns int */
+ const Fsetprotclass : fcntlcmd = 64 /* Set the protection class of a file for the EA, requires int */
+ const Flog2phys_ext : fcntlcmd = 65 /* file offset to device offset, extended */
+ const Fgetlkpid : fcntlcmd = 66 /* get record locking information, per-process */
+ /* See F_DUPFD_CLOEXEC below for 67 */
+ const Fsetbacktore : fcntlcmd = 70 /* Mark the file as being the backing store for another filesystem */
+ const Fgetpath_mtminfo : fcntlcmd = 71 /* return the full path of the FD, but error in specific mtmd circumstances */
+ /* 72 is free. It used to be F_GETENCRYPTEDDATA, which is now removed. */
+ const Fsetnosigpipe : fcntlcmd = 73 /* No SIGPIPE generated on EPIPE */
+ const Fgetnosigpipe : fcntlcmd = 74 /* Status of SIGPIPE for this fd */
+
+ /* kqueue events */
+ const Kevadd : kflags = 0x0001 /* add event to kq (implies enable) */
+ const Kevdelete : kflags = 0x0002 /* delete event from kq */
+ const Kevenable : kflags = 0x0004 /* enable event */
+ const Kevdisable : kflags = 0x0008 /* disable event (not reported) */
+ const Kevreceipt : kflags = 0x0040 /* force EV_ERROR on success, data == 0 */
+
+ /* kqueue flags */
+ const Kevoneshot : kflags = 0x0010 /* only report one occurrence */
+ const Kevclear : kflags = 0x0020 /* clear event state after reporting */
+ const Kevdispatch : kflags = 0x0080 /* disable event after reporting */
+
+ const Kevsysflags : kflags = 0xf000 /* reserved by system */
+ const Kevflag0 : kflags = 0x1000 /* filter-specific flag */
+ const Kevflag1 : kflags = 0x2000 /* filter-specific flag */
+
+ /* kqueue returned values */
+ const Keveof : kflags = 0x8000 /* eof detected */
+ const Keverror : kflags = 0x4000 /* error, data contains errno */
+
+ /* open options */
+ const Ordonly : fdopt = 0x0
+ const Owronly : fdopt = 0x1
+ const Ordwr : fdopt = 0x2
+ const Ondelay : fdopt = 0x4
+ const Oappend : fdopt = 0x8
+ const Ocreat : fdopt = 0x200
+ const Onofollow : fdopt = 0x100
+ const Otrunc : fdopt = 0x400
+ const Odir : fdopt = 0x100000
+
+ /* stat modes */
+ const Sifmt : filemode = 0xf000
+ const Sififo : filemode = 0x1000
+ const Sifchr : filemode = 0x2000
+ const Sifdir : filemode = 0x4000
+ const Sifblk : filemode = 0x6000
+ const Sifreg : filemode = 0x8000
+ const Siflnk : filemode = 0xa000
+ const Sifsock : filemode = 0xc000
+
+ /* mmap protection */
+ const Mprotnone : mprot = 0x0
+ const Mprotrd : mprot = 0x1
+ const Mprotwr : mprot = 0x2
+ const Mprotexec : mprot = 0x4
+ const Mprotrw : mprot = 0x3
+
+ /* mmap options */
+ const Mshared : mopt = 0x1
+ const Mpriv : mopt = 0x2
+ const Mfixed : mopt = 0x10
+ const Mfile : mopt = 0x0
+ const Manon : mopt = 0x1000
+ /* Only on Linux
+ const M32bit : mopt = 0x40
+ */
+
+ /* socket families. INCOMPLETE. */
+ const Afunspec : sockfam = 0
+ const Afunix : sockfam = 1
+ const Afinet : sockfam = 2
+ const Afinet6 : sockfam = 30
+
+ /* socket types. */
+ const Sockstream : socktype = 1
+ const Sockdgram : socktype = 2
+ const Sockraw : socktype = 3
+ const Sockrdm : socktype = 4
+ const Sockseqpacket : socktype = 5
+
+ /* network protocols */
+ const Ipproto_ip : sockproto = 0
+ const Ipproto_icmp : sockproto = 1
+ const Ipproto_tcp : sockproto = 6
+ const Ipproto_udp : sockproto = 17
+ const Ipproto_raw : sockproto = 255
+
+ /* return value for a failed mapping */
+ const Mapbad : byte# = -1 castto(byte#)
+
+ /* syscalls.
+ note, creat() implemented as open(path, Creat|Trunc|Wronly) */
+ const Syssyscall : scno = 0x2000000
+ const Sysexit : scno = 0x2000001
+ const Sysfork : scno = 0x2000002
+ const Sysread : scno = 0x2000003
+ const Syswrite : scno = 0x2000004
+ const Sysopen : scno = 0x2000005
+ const Sysclose : scno = 0x2000006
+ const Syswait4 : scno = 0x2000007
+ const Syslink : scno = 0x2000009
+ const Sysunlink : scno = 0x200000a
+ const Syschdir : scno = 0x200000c
+ const Sysfchdir : scno = 0x200000d
+ const Sysmknod : scno = 0x200000e
+ const Syschmod : scno = 0x200000f
+ const Syschown : scno = 0x2000010
+ const Sysgetfsstat : scno = 0x2000012
+ const Sysgetpid : scno = 0x2000014
+ const Syssetuid : scno = 0x2000017
+ const Sysgetuid : scno = 0x2000018
+ const Sysgeteuid : scno = 0x2000019
+ const Sysptrace : scno = 0x200001a
+ const Sysrecvmsg : scno = 0x200001b
+ const Syssendmsg : scno = 0x200001c
+ const Sysrecvfrom : scno = 0x200001d
+ const Sysaccept : scno = 0x200001e
+ const Sysgetpeername : scno = 0x200001f
+ const Sysgetsockname : scno = 0x2000020
+ const Sysaccess : scno = 0x2000021
+ const Syschflags : scno = 0x2000022
+ const Sysfchflags : scno = 0x2000023
+ const Syssync : scno = 0x2000024
+ const Syskill : scno = 0x2000025
+ const Sysgetppid : scno = 0x2000027
+ const Sysdup : scno = 0x2000029
+ const Syspipe : scno = 0x200002a
+ const Sysgetegid : scno = 0x200002b
+ const Sysprofil : scno = 0x200002c
+ const Syssigaction : scno = 0x200002e
+ const Sysgetgid : scno = 0x200002f
+ const Syssigprocmask : scno = 0x2000030
+ const Sysgetlogin : scno = 0x2000031
+ const Syssetlogin : scno = 0x2000032
+ const Sysacct : scno = 0x2000033
+ const Syssigpending : scno = 0x2000034
+ const Syssigaltstack : scno = 0x2000035
+ const Sysioctl : scno = 0x2000036
+ const Sysreboot : scno = 0x2000037
+ const Sysrevoke : scno = 0x2000038
+ const Syssymlink : scno = 0x2000039
+ const Sysreadlink : scno = 0x200003a
+ const Sysexecve : scno = 0x200003b
+ const Sysumask : scno = 0x200003c
+ const Syschroot : scno = 0x200003d
+ const Sysmsync : scno = 0x2000041
+ const Sysvfork : scno = 0x2000042
+ const Sysmunmap : scno = 0x2000049
+ const Sysmprotect : scno = 0x200004a
+ const Sysmadvise : scno = 0x200004b
+ const Sysmincore : scno = 0x200004e
+ const Sysgetgroups : scno = 0x200004f
+ const Syssetgroups : scno = 0x2000050
+ const Sysgetpgrp : scno = 0x2000051
+ const Syssetpgid : scno = 0x2000052
+ const Syssetitimer : scno = 0x2000053
+ const Sysswapon : scno = 0x2000055
+ const Sysgetitimer : scno = 0x2000056
+ const Sysgetdtablesize : scno = 0x2000059
+ const Sysdup2 : scno = 0x200005a
+ const Sysfcntl : scno = 0x200005c
+ const Sysselect : scno = 0x200005d
+ const Sysfsync : scno = 0x200005f
+ const Syssetpriority : scno = 0x2000060
+ const Syssocket : scno = 0x2000061
+ const Sysconnect : scno = 0x2000062
+ const Sysgetpriority : scno = 0x2000064
+ const Sysbind : scno = 0x2000068
+ const Syssetsockopt : scno = 0x2000069
+ const Syslisten : scno = 0x200006a
+ const Syssigsuspend : scno = 0x200006f
+ const Sysgettimeofday : scno = 0x2000074
+ const Sysgetrusage : scno = 0x2000075
+ const Sysgetsockopt : scno = 0x2000076
+ const Sysreadv : scno = 0x2000078
+ const Syswritev : scno = 0x2000079
+ const Syssettimeofday : scno = 0x200007a
+ const Sysfchown : scno = 0x200007b
+ const Sysfchmod : scno = 0x200007c
+ const Syssetreuid : scno = 0x200007e
+ const Syssetregid : scno = 0x200007f
+ const Sysrename : scno = 0x2000080
+ const Sysflock : scno = 0x2000083
+ const Sysmkfifo : scno = 0x2000084
+ const Syssendto : scno = 0x2000085
+ const Sysshutdown : scno = 0x2000086
+ const Syssocketpair : scno = 0x2000087
+ const Sysmkdir : scno = 0x2000088
+ const Sysrmdir : scno = 0x2000089
+ const Sysutimes : scno = 0x200008a
+ const Sysfutimes : scno = 0x200008b
+ const Sysadjtime : scno = 0x200008c
+ const Sysgethostuuid : scno = 0x200008e
+ const Syssetsid : scno = 0x2000093
+ const Sysgetpgid : scno = 0x2000097
+ const Syssetprivexec : scno = 0x2000098
+ const Syspread : scno = 0x2000099
+ const Syspwrite : scno = 0x200009a
+ const Sysnfssvc : scno = 0x200009b
+ const Sysstatfs : scno = 0x200009d
+ const Sysfstatfs : scno = 0x200009e
+ const Sysunmount : scno = 0x200009f
+ const Sysgetfh : scno = 0x20000a1
+ const Sysquotactl : scno = 0x20000a5
+ const Sysmount : scno = 0x20000a7
+ const Syscsops : scno = 0x20000a9
+ const Syswaitid : scno = 0x20000ad
+ const Sysadd_profil : scno = 0x20000b0
+ const Syskdebug_trace : scno = 0x20000b4
+ const Syssetgid : scno = 0x20000b5
+ const Syssetegid : scno = 0x20000b6
+ const Sysseteuid : scno = 0x20000b7
+ const Syssigreturn : scno = 0x20000b8
+ const Syschud : scno = 0x20000b9
+ const Sysfdatasync : scno = 0x20000bb
+ const Sysstat : scno = 0x20000bc
+ const Sysfstat : scno = 0x20000bd
+ const Syslstat : scno = 0x20000be
+ const Syspathconf : scno = 0x20000bf
+ const Sysfpathconf : scno = 0x20000c0
+ const Sysgetrlimit : scno = 0x20000c2
+ const Syssetrlimit : scno = 0x20000c3
+ const Sysgetdirentries : scno = 0x20000c4
+ const Sysmmap : scno = 0x20000c5
+ const Syslseek : scno = 0x20000c7
+ const Systruncate : scno = 0x20000c8
+ const Sysftruncate : scno = 0x20000c9
+ const Sys__sysctl : scno = 0x20000ca
+ const Sysmlock : scno = 0x20000cb
+ const Sysmunlock : scno = 0x20000cc
+ const Sysundelete : scno = 0x20000cd
+ const SysATsocket : scno = 0x20000ce
+ const SysATgetmsg : scno = 0x20000cf
+ const SysATputmsg : scno = 0x20000d0
+ const SysATPsndreq : scno = 0x20000d1
+ const SysATPsndrsp : scno = 0x20000d2
+ const SysATPgetreq : scno = 0x20000d3
+ const SysATPgetrsp : scno = 0x20000d4
+ const Sysmkcomplex : scno = 0x20000d8
+ const Sysstatv : scno = 0x20000d9
+ const Syslstatv : scno = 0x20000da
+ const Sysfstatv : scno = 0x20000db
+ const Sysgetattrlist : scno = 0x20000dc
+ const Syssetattrlist : scno = 0x20000dd
+ const Sysgetdirentriesattr : scno = 0x20000de
+ const Sysexchangedata : scno = 0x20000df
+ const Syssearchfs : scno = 0x20000e1
+ const Sysdelete : scno = 0x20000e2
+ const Syscopyfile : scno = 0x20000e3
+ const Sysfgetattrlist : scno = 0x20000e4
+ const Sysfsetattrlist : scno = 0x20000e5
+ const Syspoll : scno = 0x20000e6
+ const Syswatchevent : scno = 0x20000e7
+ const Syswaitevent : scno = 0x20000e8
+ const Sysmodwatch : scno = 0x20000e9
+ const Sysgetxattr : scno = 0x20000ea
+ const Sysfgetxattr : scno = 0x20000eb
+ const Syssetxattr : scno = 0x20000ec
+ const Sysfsetxattr : scno = 0x20000ed
+ const Sysremovexattr : scno = 0x20000ee
+ const Sysfremovexattr : scno = 0x20000ef
+ const Syslistxattr : scno = 0x20000f0
+ const Sysflistxattr : scno = 0x20000f1
+ const Sysfsctl : scno = 0x20000f2
+ const Sysinitgroups : scno = 0x20000f3
+ const Sysposix_spawn : scno = 0x20000f4
+ const Sysffsctl : scno = 0x20000f5
+ const Sysnfsclnt : scno = 0x20000f7
+ const Sysfhopen : scno = 0x20000f8
+ const Sysminherit : scno = 0x20000fa
+ const Syssemsys : scno = 0x20000fb
+ const Sysmsgsys : scno = 0x20000fc
+ const Sysshmsys : scno = 0x20000fd
+ const Syssemctl : scno = 0x20000fe
+ const Syssemget : scno = 0x20000ff
+ const Syssemop : scno = 0x2000100
+ const Sysmsgctl : scno = 0x2000102
+ const Sysmsgget : scno = 0x2000103
+ const Sysmsgsnd : scno = 0x2000104
+ const Sysmsgrcv : scno = 0x2000105
+ const Sysshmat : scno = 0x2000106
+ const Sysshmctl : scno = 0x2000107
+ const Sysshmdt : scno = 0x2000108
+ const Sysshmget : scno = 0x2000109
+ const Sysshm_open : scno = 0x200010a
+ const Sysshm_unlink : scno = 0x200010b
+ const Syssem_open : scno = 0x200010c
+ const Syssem_close : scno = 0x200010d
+ const Syssem_unlink : scno = 0x200010e
+ const Syssem_wait : scno = 0x200010f
+ const Syssem_trywait : scno = 0x2000110
+ const Syssem_post : scno = 0x2000111
+ const Syssem_getvalue : scno = 0x2000112
+ const Syssem_init : scno = 0x2000113
+ const Syssem_destroy : scno = 0x2000114
+ const Sysopen_extended : scno = 0x2000115
+ const Sysumask_extended : scno = 0x2000116
+ const Sysstat_extended : scno = 0x2000117
+ const Syslstat_extended : scno = 0x2000118
+ const Sysfstat_extended : scno = 0x2000119
+ const Syschmod_extended : scno = 0x200011a
+ const Sysfchmod_extended : scno = 0x200011b
+ const Sysaccess_extended : scno = 0x200011c
+ const Syssettid : scno = 0x200011d
+ const Sysgettid : scno = 0x200011e
+ const Syssetsgroups : scno = 0x200011f
+ const Sysgetsgroups : scno = 0x2000120
+ const Syssetwgroups : scno = 0x2000121
+ const Sysgetwgroups : scno = 0x2000122
+ const Sysmkfifo_extended : scno = 0x2000123
+ const Sysmkdir_extended : scno = 0x2000124
+ const Sysidentitysvc : scno = 0x2000125
+ const Sysshared_region_check_np : scno = 0x2000126
+ const Sysshared_region_map_np : scno = 0x2000127
+ const Sysvm_pressure_monitor : scno = 0x2000128
+ const Syspsynch_rw_longrdlock : scno = 0x2000129
+ const Syspsynch_rw_yieldwrlock : scno = 0x200012a
+ const Syspsynch_rw_downgrade : scno = 0x200012b
+ const Syspsynch_rw_upgrade : scno = 0x200012c
+ const Syspsynch_mutexwait : scno = 0x200012d
+ const Syspsynch_mutexdrop : scno = 0x200012e
+ const Syspsynch_cvbroad : scno = 0x200012f
+ const Syspsynch_cvsignal : scno = 0x2000130
+ const Syspsynch_cvwait : scno = 0x2000131
+ const Syspsynch_rw_rdlock : scno = 0x2000132
+ const Syspsynch_rw_wrlock : scno = 0x2000133
+ const Syspsynch_rw_unlock : scno = 0x2000134
+ const Syspsynch_rw_unlock2 : scno = 0x2000135
+ const Sysgetsid : scno = 0x2000136
+ const Syssettid_with_pid : scno = 0x2000137
+ const Sysaio_fsync : scno = 0x2000139
+ const Sysaio_return : scno = 0x200013a
+ const Sysaio_suspend : scno = 0x200013b
+ const Sysaio_cancel : scno = 0x200013c
+ const Sysaio_error : scno = 0x200013d
+ const Sysaio_read : scno = 0x200013e
+ const Sysaio_write : scno = 0x200013f
+ const Syslio_listio : scno = 0x2000140
+ const Sysiopolicysys : scno = 0x2000142
+ const Sysmlockall : scno = 0x2000144
+ const Sysmunlockall : scno = 0x2000145
+ const Sysissetugid : scno = 0x2000147
+ const Sys__pthread_kill : scno = 0x2000148
+ const Sys__pthread_sigmask : scno = 0x2000149
+ const Sys__sigwait : scno = 0x200014a
+ const Sys__disable_threadsignal : scno = 0x200014b
+ const Sys__pthread_markcancel : scno = 0x200014c
+ const Sys__pthread_canceled : scno = 0x200014d
+ const Sys__semwait_signal : scno = 0x200014e
+ const Sysproc_info : scno = 0x2000150
+ const Syssendfile : scno = 0x2000151
+ const Sysstat64 : scno = 0x2000152
+ const Sysfstat64 : scno = 0x2000153
+ const Syslstat64 : scno = 0x2000154
+ const Sysstat64_extended : scno = 0x2000155
+ const Syslstat64_extended : scno = 0x2000156
+ const Sysfstat64_extended : scno = 0x2000157
+ const Sysgetdirentries64 : scno = 0x2000158
+ const Sysstatfs64 : scno = 0x2000159
+ const Sysfstatfs64 : scno = 0x200015a
+ const Sysgetfsstat64 : scno = 0x200015b
+ const Sys__pthread_chdir : scno = 0x200015c
+ const Sys__pthread_fchdir : scno = 0x200015d
+ const Sysaudit : scno = 0x200015e
+ const Sysauditon : scno = 0x200015f
+ const Sysgetauid : scno = 0x2000161
+ const Syssetauid : scno = 0x2000162
+ const Sysgetaudit : scno = 0x2000163
+ const Syssetaudit : scno = 0x2000164
+ const Sysgetaudit_addr : scno = 0x2000165
+ const Syssetaudit_addr : scno = 0x2000166
+ const Sysauditctl : scno = 0x2000167
+ const Sysbsdthread_create : scno = 0x2000168
+ const Sysbsdthread_terminate : scno = 0x2000169
+ const Syskqueue : scno = 0x200016a
+ const Syskevent : scno = 0x200016b
+ const Syslchown : scno = 0x200016c
+ const Sysstack_snapshot : scno = 0x200016d
+ const Sysbsdthread_register : scno = 0x200016e
+ const Sysworkq_open : scno = 0x200016f
+ const Sysworkq_kernreturn : scno = 0x2000170
+ const Syskevent64 : scno = 0x2000171
+ const Sys__old_semwait_signal : scno = 0x2000172
+ const Sys__old_semwait_signal_nocancel : scno = 0x2000173
+ const Systhread_selfid : scno = 0x2000174
+ const Sys__mac_execve : scno = 0x200017c
+ const Sys__mac_syscall : scno = 0x200017d
+ const Sys__mac_get_file : scno = 0x200017e
+ const Sys__mac_set_file : scno = 0x200017f
+ const Sys__mac_get_link : scno = 0x2000180
+ const Sys__mac_set_link : scno = 0x2000181
+ const Sys__mac_get_proc : scno = 0x2000182
+ const Sys__mac_set_proc : scno = 0x2000183
+ const Sys__mac_get_fd : scno = 0x2000184
+ const Sys__mac_set_fd : scno = 0x2000185
+ const Sys__mac_get_pid : scno = 0x2000186
+ const Sys__mac_get_lcid : scno = 0x2000187
+ const Sys__mac_get_lctx : scno = 0x2000188
+ const Sys__mac_set_lctx : scno = 0x2000189
+ const Syssetlcid : scno = 0x200018a
+ const Sysgetlcid : scno = 0x200018b
+ const Sysread_nocancel : scno = 0x200018c
+ const Syswrite_nocancel : scno = 0x200018d
+ const Sysopen_nocancel : scno = 0x200018e
+ const Sysclose_nocancel : scno = 0x200018f
+ const Syswait4_nocancel : scno = 0x2000190
+ const Sysrecvmsg_nocancel : scno = 0x2000191
+ const Syssendmsg_nocancel : scno = 0x2000192
+ const Sysrecvfrom_nocancel : scno = 0x2000193
+ const Sysaccept_nocancel : scno = 0x2000194
+ const Sysmsync_nocancel : scno = 0x2000195
+ const Sysfcntl_nocancel : scno = 0x2000196
+ const Sysselect_nocancel : scno = 0x2000197
+ const Sysfsync_nocancel : scno = 0x2000198
+ const Sysconnect_nocancel : scno = 0x2000199
+ const Syssigsuspend_nocancel : scno = 0x200019a
+ const Sysreadv_nocancel : scno = 0x200019b
+ const Syswritev_nocancel : scno = 0x200019c
+ const Syssendto_nocancel : scno = 0x200019d
+ const Syspread_nocancel : scno = 0x200019e
+ const Syspwrite_nocancel : scno = 0x200019f
+ const Syswaitid_nocancel : scno = 0x20001a0
+ const Syspoll_nocancel : scno = 0x20001a1
+ const Sysmsgsnd_nocancel : scno = 0x20001a2
+ const Sysmsgrcv_nocancel : scno = 0x20001a3
+ const Syssem_wait_nocancel : scno = 0x20001a4
+ const Sysaio_suspend_nocancel : scno = 0x20001a5
+ const Sys__sigwait_nocancel : scno = 0x20001a6
+ const Sys__semwait_signal_nocancel : scno = 0x20001a7
+ const Sys__mac_mount : scno = 0x20001a8
+ const Sys__mac_get_mount : scno = 0x20001a9
+ const Sys__mac_getfsstat : scno = 0x20001aa
+ const Sysfsgetpath : scno = 0x20001ab
+ const Sysaudit_session_self : scno = 0x20001ac
+ const Sysaudit_session_join : scno = 0x20001ad
+ const Syspid_suspend : scno = 0x20001ae
+ const Syspid_resume : scno = 0x20001af
+ const Sysfileport_makeport : scno = 0x20001b0
+ const Sysfileport_makefd : scno = 0x20001b1
+
+ extern const syscall : (sc:scno, args:... -> int64)
+
+ /* process control */
+ const exit : (status:int -> void)
+ const getpid : ( -> pid)
+ const kill : (pid : pid, sig:int64 -> int64)
+ const fork : (-> pid)
+ const wait4 : (pid : pid, loc:int32#, opt : int64, rusage:rusage# -> int64)
+ const waitpid : (pid : pid, loc:int32#, opt : int64 -> int64)
+ const execv : (cmd : byte[:], args : byte[:][:] -> int64)
+ const execve : (cmd : byte[:], args : byte[:][:], env : byte[:][:] -> int64)
+ /* wrappers to extract wait status */
+ const waitstatus : (st : int32 -> waitstatus)
+
+ /* file manipulation */
+ 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 unlink : (path:byte[:] -> int)
+ const read : (fd:fd, buf:byte[:] -> size)
+ const write : (fd:fd, buf:byte[:] -> size)
+ const lseek : (fd:fd, off:off, whence:whence -> off)
+ const stat : (path:byte[:], sb:statbuf# -> int64)
+ const lstat : (path:byte[:], sb:statbuf# -> int64)
+ const fstat : (fd:fd, sb:statbuf# -> int64)
+ const mkdir : (path : byte[:], mode : int64 -> int64)
+ generic ioctl : (fd:fd, req : int64, args:@a# -> int64)
+ const getdirentries64 : (fd : fd, buf : byte[:], basep : int64# -> int64)
+ const chdir : (p : byte[:] -> int64)
+
+ /* fd stuff */
+ const pipe : (fd : fd[2]# -> int64)
+ const dup : (fd : fd -> fd)
+ const dup2 : (src : fd, dst : fd -> fd)
+ /* NB: the C ABI uses '...' for the args. */
+ const fcntl : (fd : fd, cmd : fcntlcmd, args : byte# -> int64)
+
+ /* kqueue */
+ const kqueue : (-> fd)
+ const kevent : (q : fd, cl : kevent[:], el : kevent[:], flg : kflags, timeout : timespec# -> int64)
+ const kevent64 : (q : fd, cl : kevent64[:], el : kevent64[:], flg : kflags, timeout : timespec# -> int64)
+
+
+
+ /* networking */
+ const socket : (dom : sockfam, stype : socktype, proto : sockproto -> fd)
+ const connect : (sock : fd, addr : sockaddr#, len : size -> int)
+ const accept : (sock : fd, addr : sockaddr#, len : size# -> fd)
+ const listen : (sock : fd, backlog : int -> int)
+ const bind : (sock : fd, addr : sockaddr#, len : size -> int)
+
+
+ /* memory mapping */
+ const munmap : (addr:byte#, len:size -> int64)
+ const mmap : (addr:byte#, len:size, prot:mprot, flags:mopt, fd:fd, off:off -> byte#)
+
+ /* time */
+ const gettimeofday : (tv : timeval#, tz : timezone# -> int)
+ const settimeofday : (tv : timeval#, tz : timezone# -> int)
+ /* faked with gettimeofday */
+ const clock_getres : (clk : clock, ts : timespec# -> int)
+ const clock_gettime : (clk : clock, ts : timespec# -> int)
+ const clock_settime : (clk : clock, ts : timespec# -> int)
+ /* FIXME: HACK HACK HACK -- does nothing */
+ const sleep : (time : uint64 -> int32)
+
+ /* system information */
+ const uname : (buf : utsname# -> int)
+ const sysctl : (mib : int[:], old : byte[:]#, new : byte[:] -> int)
+
+ /* filled by start code */
+ extern const __cenvp : byte##
+ extern const __environment : byte[:][:]
+;;
+
+/*
+wraps a syscall argument, converting it to 64 bits for the syscall function. This is
+the same as casting, but more concise than writing castto(int64)
+*/
+generic a = {x : @t
+ -> x castto(uint64)
+}
+
+/* OSX has a number of funky syscalls */
+extern const __osx_fork : (-> pid)
+extern const __osx_pipe : (fd : fd[2]# -> int64)
+extern const __osx_getpid : (-> pid)
+extern const __osx_lseek : (fd:fd, off:off, whence:whence -> off)
+extern const __osx_gettimeofday : (tv : timeval#, tz : timezone# -> int)
+/*
+extern const __osx_ptrace
+extern const __osx_signalstack
+extern const __osx_sigreturn
+extern const __osx_thread_selfid
+extern const __osx_vfork
+*/
+
+extern const cstring : (str : byte[:] -> byte#)
+extern const alloca : (sz : size -> byte#)
+
+/* process control */
+const exit = {status; syscall(Sysexit, a(status))}
+const getpid = {; -> syscall(Sysgetpid) castto(pid)}
+const kill = {pid, sig; -> syscall(Syskill, a(pid), a(sig))}
+const fork = {; -> __osx_fork()}
+const wait4 = {pid, loc, opt, rusage; -> syscall(Syswait4, a(pid), a(loc), a(opt), a(rusage))}
+const waitpid = {pid, loc, opt;
+ -> wait4(pid, loc, opt, 0 castto(rusage#))
+}
+
+const sleep = {time; -> 0}
+
+const execv = {cmd, args
+ var p, cargs, i
+
+ /* doesn't just call execve() for efficiency's sake. */
+ p = alloca((args.len + 1)*sizeof(byte#))
+ cargs = (p castto(byte##))[:args.len + 1]
+ for i = 0; i < args.len; i++
+ cargs[i] = cstring(args[i])
+ ;;
+ cargs[args.len] = 0 castto(byte#)
+ -> syscall(Sysexecve, cstring(cmd), a(p), a(__cenvp))
+}
+
+const execve = {cmd, args, env
+ var cargs, cenv, i
+ var p
+
+ /* copy the args */
+ p = alloca((args.len + 1)*sizeof(byte#))
+ cargs = (p castto(byte##))[:args.len + 1]
+ for i = 0; i < args.len; i++
+ cargs[i] = cstring(args[i])
+ ;;
+ cargs[args.len] = 0 castto(byte#)
+
+ /*
+ copy the env.
+ of course we fucking have to duplicate this code everywhere,
+ since we want to stack allocate...
+ */
+ p = alloca((env.len + 1)*sizeof(byte#))
+ cenv = (p castto(byte##))[:env.len]
+ for i = 0; i < env.len; i++
+ cenv[i] = cstring(env[i])
+ ;;
+ cenv[env.len] = 0 castto(byte#)
+
+ -> syscall(Sysexecve, cstring(cmd), a(p), a(cenv))
+}
+
+
+/* fd manipulation */
+const open = {path, opts; -> syscall(Sysopen, cstring(path), a(opts), a(0o777)) castto(fd)}
+const openmode = {path, opts, mode; -> syscall(Sysopen, cstring(path), a(opts), a(mode)) castto(fd)}
+const close = {fd; -> syscall(Sysclose, a(fd))}
+const creat = {path, mode; -> openmode(path, Ocreat | Otrunc | Owronly, mode) castto(fd)}
+const unlink = {path; -> syscall(Sysunlink, cstring(path)) castto(int)}
+const read = {fd, buf; -> syscall(Sysread, a(fd), buf castto(byte#), a(buf.len)) castto(size)}
+const write = {fd, buf; -> syscall(Syswrite, a(fd), buf castto(byte#), a(buf.len)) castto(size)}
+const lseek = {fd, off, whence; -> __osx_lseek(fd, off, whence)}
+const stat = {path, sb; -> syscall(Sysstat64, cstring(path), a(sb))}
+const lstat = {path, sb; -> syscall(Syslstat64, cstring(path), a(sb))}
+const fstat = {fd, sb; -> syscall(Sysfstat64, a(fd), a(sb))}
+const mkdir = {path, mode; -> syscall(Sysmkdir, cstring(path), a(mode)) castto(int64)}
+generic ioctl = {fd, req, arg; -> syscall(Sysioctl, a(fd), a(req), a(arg)) castto(int64)}
+const getdirentries64 = {fd, buf, basep; -> syscall(Sysgetdirentries64, a(fd), buf castto(byte#), a(buf.len), a(basep))}
+const chdir = {dir; -> syscall(Syschdir, cstring(dir))}
+
+/* fd stuff */
+const pipe = {fd; -> __osx_pipe(fd)}
+const dup = {fd; -> syscall(Sysdup, a(fd)) castto(fd)}
+const dup2 = {src, dst; -> syscall(Sysdup2, a(src), a(dst)) castto(fd)}
+const fcntl = {fd, cmd, args; -> syscall(Sysfcntl, a(fd), a(cmd), a(args))}
+
+/* kqueueueueueueue */
+const kqueue = {; -> syscall(Syskqueue) castto(fd)}
+const kevent = {q, cl, el, flg, timeout
+ -> syscall(Syskevent, a(q), \
+ cl castto(kevent#), a(cl.len), \
+ el castto(kevent#), a(el.len), \
+ a(flg), \
+ timeout)
+}
+
+const kevent64 = {q, cl, el, flg, timeout
+ -> syscall(Syskevent, a(q), \
+ cl castto(kevent#), a(cl.len), \
+ el castto(kevent#), a(el.len), \
+ a(flg), \
+ timeout)
+}
+
+/* networking */
+const socket = {dom, stype, proto; -> syscall(Syssocket, a(dom), a(stype), a(proto)) castto(fd) }
+const connect = {sock, addr, len; -> syscall(Sysconnect, a(sock), a(addr), a(len)) castto(int)}
+const accept = {sock, addr, len; -> syscall(Sysaccept, a(sock), a(addr), a(len)) castto(fd)}
+const listen = {sock, backlog; -> syscall(Syslisten, a(sock), a(backlog)) castto(int)}
+const bind = {sock, addr, len; -> syscall(Sysbind, a(sock), a(addr), a(len)) castto(int)}
+
+/* memory management */
+const munmap = {addr, len; -> syscall(Sysmunmap, a(addr), a(len))}
+const mmap = {addr, len, prot, flags, fd, off;
+ -> syscall(Sysmmap, a(addr), a(len), a(prot), a(flags), a(fd), a(off)) castto(byte#)}
+
+/* time */
+const gettimeofday = {tv, tz; -> __osx_gettimeofday(tv, tz) castto(int)}
+const settimeofday = {tv, tz; -> syscall(Syssettimeofday, a(tv), a(tz)) castto(int)}
+
+/* faked with gettimeofday */
+const clock_getres = {clk, ts
+ ts.sec = 0
+ ts.nsec = 1000*10 /* 10ms is reasonable resolution */
+ -> 0
+}
+
+const clock_gettime = {clk, ts
+ var tv
+ var ret
+
+ ret = gettimeofday(&tv, 0 castto(timezone#))
+ ts.sec = tv.sec
+ ts.nsec = tv.usec * 1000
+ -> ret
+}
+
+const clock_settime = {clk, ts
+ var tv
+
+ tv.sec = ts.sec
+ tv.usec = ts.nsec / 1000
+ -> settimeofday(&tv, 0 castto(timezone#))
+}
+
+/* system information */
+const uname = {buf;
+ var mib : int[2]
+ var ret
+ var sys
+ var nod
+ var rel
+ var ver
+ var mach
+
+ ret = 0
+ mib[0] = 1 /* CTL_KERN */
+ mib[1] = 1 /* KERN_OSTYPE */
+ sys = buf.system[:]
+ ret = sysctl(mib[:], &sys, [][:])
+ if ret < 0
+ -> ret
+ ;;
+
+ mib[0] = 1 /* CTL_KERN */
+ mib[1] = 10 /* KERN_HOSTNAME */
+ nod = buf.node[:]
+ ret = sysctl(mib[:], &nod, [][:])
+ if ret < 0
+ -> ret
+ ;;
+
+ mib[0] = 1 /* CTL_KERN */
+ mib[1] = 2 /* KERN_OSRELEASE */
+ rel = buf.release[:]
+ ret = sysctl(mib[:], &rel, [][:])
+ if ret < 0
+ -> ret
+ ;;
+
+ mib[0] = 1 /* CTL_KERN */
+ mib[1] = 4 /* KERN_VERSION */
+ ver = buf.version[:]
+ ret = sysctl(mib[:], &ver, [][:])
+ if ret < 0
+ -> ret
+ ;;
+
+ mib[0] = 6 /* CTL_HW */
+ mib[1] = 1 /* HW_MACHINE */
+ mach = buf.machine[:]
+ ret = sysctl(mib[:], &mach, [][:])
+ if ret < 0
+ -> ret
+ ;;
+
+ -> 0
+}
+
+const sysctl = {mib, old, new
+ var mibp
+ var mibsz
+ var o
+ var oldp
+ var oldsz
+ var newp
+ var newsz
+ var ret
+
+ mibp = mib castto(byte#)
+ mibsz = mib.len castto(uint64)
+ o = old#
+ oldp = o castto(byte#)
+ oldsz = (o.len castto(uint64))
+ if new.len > 0
+ newp = new castto(byte#)
+ newsz = new.len castto(uint64)
+ else
+ newp = 0 castto(byte#)
+ newsz = 0
+ ;;
+
+ ret = syscall(Sys__sysctl, a(mibp), a(mibsz), a(oldp), a(&oldsz), a(newp), a(newsz)) castto(int)
+
+ old# = o[:oldsz]
+ -> ret
+}
+
+const waitstatus = {st
+ if st < 0
+ -> `Waitfail st
+ ;;
+ match st & 0o177
+ | 0: -> `Waitexit (st >> 8)
+ | 0o177:-> `Waitstop (st >> 8)
+ | sig: -> `Waitsig sig
+ ;;
+}
+
diff --git a/lib/std/sys+plan9-x64.myr b/lib/std/sys+plan9-x64.myr
new file mode 100644
index 0000000..f183dc2
--- /dev/null
+++ b/lib/std/sys+plan9-x64.myr
@@ -0,0 +1,242 @@
+use "systypes.use"
+
+pkg sys =
+ type scno = int64 /* syscall */
+ type pid = int32 /* process id */
+ type fdopt = int32 /* fd options */
+ type fd = int32 /* fd */
+ type rflags = int32 /* rfork flags */
+
+ type tos = struct
+ prof : prof
+ cyclefreq : uint64
+ kcycles : int64
+ pcycles : int64
+ pid : pid
+ clock : uint32
+ ;;
+
+ type prof = struct
+ pp : byte# /* plink */
+ next : byte# /* plink */
+ last : byte# /* plink */
+ first : byte# /* plink */
+ pid : pid /* plink */
+ what : uint32 /* plink */
+ ;;
+
+
+ const Ordonly : fdopt = 0
+ const Owronly : fdopt = 1
+ const Ordwr : fdopt = 2
+ const Oexec : fdopt = 3
+
+ const Otrunc : fdopt = 16
+ const Ocexec : fdopt = 32
+ const Orclose : fdopt = 64
+ const Oexcl : fdopt = 0x1000
+
+ const Qtdir : int = 0x80
+ const Qtappend : int = 0x40
+ const Qtexcl : int = 0x20
+ const Qtmount : int = 0x10
+ const Qtauth : int = 0x08
+ const Qttmp : int = 0x04
+ const Qtfile : int = 0x00
+
+ const Dmdir : int = 0x8000000
+ const Dmappend : int = 0x4000000
+ const Dmexcl : int = 0x2000000
+ const Dmmount : int = 0x1000000
+ const Dmauth : int = 0x0800000
+ const Dmtmp : int = 0x0400000
+ const Dmread : int = 0x4
+ const Dmwrite : int = 0x2
+ const Dmexec : int = 0x1
+
+ const Rfnameg : rflags = 1 << 0
+ const Rfenvg : rflags = 1 << 1
+ const Rffdg : rflags = 1 << 2
+ const Rfnoteg : rflags = 1 << 3
+ const Rfproc : rflags = 1 << 4
+ const Rfmem : rflags = 1 << 5
+ const Rfnowait : rflags = 1 << 6
+ const Rfcnameg : rflags = 1 << 10
+ const Rfcenvg : rflags = 1 << 11
+ const Rfcfdg : rflags = 1 << 12
+ const Rfrend : rflags = 1 << 13
+ const Rfnomnt : rflags = 1 << 14
+
+ const Syssysr1 : scno = 0
+ const Sys_errstr : scno = 1
+ const Sysbind : scno = 2
+ const Syschdir : scno = 3
+ const Sysclose : scno = 4
+ const Sysdup : scno = 5
+ const Sysalarm : scno = 6
+ const Sysexec : scno = 7
+ const Sysexits : scno = 8
+ const Sys_fsession : scno = 9
+ const Sysfauth : scno = 10
+ const Sys_fstat : scno = 11
+ const Syssegbrk : scno = 12
+ const Sys_mount : scno = 13
+ const Sysopen : scno = 14
+ const Sys_read : scno = 15
+ const Sysoseek : scno = 16
+ const Syssleep : scno = 17
+ const Sys_stat : scno = 18
+ const Sysrfork : scno = 19
+ const Sys_write : scno = 20
+ const Syspipe : scno = 21
+ const Syscreate : scno = 22
+ const Sysfd2path : scno = 23
+ const Sysbrk_ : scno = 24
+ const Sysremove : scno = 25
+ const Sys_wstat : scno = 26
+ const Sys_fwstat : scno = 27
+ const Sysnotify : scno = 28
+ const Sysnoted : scno = 29
+ const Syssegattach : scno = 30
+ const Syssegdetach : scno = 31
+ const Syssegfree : scno = 32
+ const Syssegflush : scno = 33
+ const Sysrendezvous : scno = 34
+ const Sysunmount : scno = 35
+ const Sys_wait : scno = 36
+ const Syssemacquire : scno = 37
+ const Syssemrelease : scno = 38
+ const Sysseek : scno = 39
+ const Sysfversion : scno = 40
+ const Syserrstr : scno = 41
+ const Sysstat : scno = 42
+ const Sysfstat : scno = 43
+ const Syswstat : scno = 44
+ const Sysfwstat : scno = 45
+ const Sysmount : scno = 46
+ const Sysawait : scno = 47
+ const Syspread : scno = 50
+ const Syspwrite : scno = 51
+ const Systsemacquire : scno = 52
+ const Sys_nsec : scno = 53
+
+
+ const sysr1 : (-> int64)
+ const bind : (nm : byte[:], old : byte[:] -> int64)
+ const chdir : (dir : byte[:] -> int64)
+ const close : (fd : fd -> int64)
+ const dup : (old : fd, new : fd -> fd)
+ const alarm : (msec : uint32 -> int64)
+ const exits : (msg : byte[:] -> int64)
+ const fauth : (fd : fd, name : byte[:] -> int64)
+ const segbrk : (saddr : void#, addr : void# -> int64)
+ const open : (path : byte[:], opt : fdopt -> fd)
+ const sleep : (msec : uint32 -> int64)
+ const rfork : (rflags : rflags -> pid)
+ const pipe : (fds : fd[2]# -> int64)
+ const create : (path : byte[:], opt : fdopt, perm : int -> fd)
+ const fd2path : (fd : fd, path : byte[:] -> int64)
+ const remove : (path : byte[:] -> int64)
+ const notify : (fn : (a : void#, c : char# -> int64) -> int64)
+ const noted : (v : int32 -> int64)
+ const segattach : (attr : int32, class : byte[:], va : void#, len : uint32 -> int64)
+ const segdetach : (va : void# -> int64)
+ const segfree : (va : byte#, len : size -> int64)
+ const segflush : (va : void#, len : uint32 -> int64)
+ const unmount : (name : byte[:], old : byte[:] -> int64)
+ const errstr : (buf : byte[:] -> int64)
+ const stat : (name : byte[:], edir : byte[:] -> int64)
+ const fstat : (fd : fd, edir : byte[:] -> int64)
+ const wstat : (name : byte[:], edir : byte[:] -> int64)
+ const fwstat : (fd : byte[:], edir : byte[:] -> int64)
+ const seek : (fd : fd, len : off, ty : int64 -> off)
+ const mount : (fd : fd, afd : fd, old : byte[:], flag : int32, aname : byte[:] -> int64)
+ const await : (buf : byte[:] -> int64)
+ const pread : (fd : fd, buf : byte[:], off : off -> size)
+ const pwrite : (fd : fd, buf : byte[:], off : off -> size)
+ const exec : (bin : byte[:], args : byte[:][:] -> int64)
+ const brk_ : (endp : byte# -> int64)
+ const nsec : (-> uint64)
+
+ extern const alloca : (sz : size -> byte#)
+
+ extern var tosptr : tos#
+ extern var curbrk : byte#
+;;
+
+/* asm stub from syscall.s */
+extern const syscall : (scno : scno, args : ... -> int64)
+/* asm stubs from util+plan9.s */
+extern const cstring : (str : byte[:] -> byte#)
+extern const alloca : (sz : size -> byte#)
+
+
+/*
+ABI mismatch: Plan 9 aligns all arguments individually to
+8 bytes, Myrddin uses natural alignment (min(sizeof(t), 16).
+Cast to a 64 bit type to paper over this.
+*/
+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))}
+const chdir = {dir; -> syscall(Syschdir, cstring(dir)) }
+const close = {fd; -> syscall(Sysclose, a(fd))}
+const dup = {ofd, nfd; -> syscall(Sysdup, a(ofd), a(nfd)) castto(fd)}
+const alarm = {msec; -> syscall(Sysalarm, a(msec))}
+const exits = {msg; -> syscall(Sysexits, cstring(msg))}
+const fauth = {fd, aname; -> syscall(Sysfauth, a(fd), cstring(aname))}
+const segbrk = {saddr, addr; -> syscall(Syssegbrk, a(saddr), a(addr))}
+const open = {path, opt; -> syscall(Sysopen, cstring(path), a(opt)) castto(fd)}
+const sleep = {msec; -> syscall(Syssleep, a(msec))}
+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), 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))}
+const segattach = {attr, class, va, len; -> syscall(Syssegattach, a(attr), cstring(class), a(va), a(len))}
+const segdetach = {va; -> syscall(Syssegdetach, a(va))}
+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, p(buf), a(buf.len))}
+const stat = {name, edir; -> syscall(Sysstat, cstring(name), p(edir), a(edir.len))}
+const fstat = {fd, edir; -> syscall(Sysstat, a(fd), p(edir), a(edir.len))}
+const wstat = {name, edir; -> syscall(Syswstat, cstring(name), p(edir), a(edir.len))}
+const fwstat = {fd, edir; -> syscall(Sysfwstat, a(fd), p(edir), a(edir.len))}
+const mount = {fd, afd, old, flag, aname; -> syscall(Sysmount, a(fd), a(afd), cstring(old), a(flag), cstring(aname))}
+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 nsec = {; -> syscall(Sys_nsec) castto(uint64)}
+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
+
+ /* we need an array of C strings. */
+ p = alloca((args.len + 1)*sizeof(byte#))
+ cargs = (a(p) castto(byte##))[:args.len + 1]
+ for i = 0; i < args.len; i++
+ cargs[i] = cstring(args[i])
+ ;;
+ cargs[args.len] = 0 castto(byte#)
+ -> syscall(Sysexec, cstring(bin), a(cargs))
+}
+
+/* ??? do I care for now?
+const fversion = {fd, bufsz, vers, nvers; -> syscall(Sysfversion, fd, bufsz, }
+const rendezvous = {;}
+const semacquire = {;}
+const semrelease = {;}
+const tsemacquire = {;}
+*/
diff --git a/lib/std/syscall+freebsd-x64.s b/lib/std/syscall+freebsd-x64.s
new file mode 100644
index 0000000..8002099
--- /dev/null
+++ b/lib/std/syscall+freebsd-x64.s
@@ -0,0 +1,21 @@
+.globl sys$syscall
+sys$syscall:
+ /*
+ 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.
+ */
+ movq %rdi,%rax
+ movq %rsi,%rdi
+ movq %rdx,%rsi
+ movq %rcx,%rdx
+ movq %r8,%r10
+ movq %r9,%r8
+ movq 8(%rsp),%r9
+
+ syscall
+
+ ret
+
diff --git a/lib/std/syscall+linux-x64.s b/lib/std/syscall+linux-x64.s
new file mode 100644
index 0000000..f7c020d
--- /dev/null
+++ b/lib/std/syscall+linux-x64.s
@@ -0,0 +1,22 @@
+.globl sys$syscall
+sys$syscall:
+ /*
+ 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.
+ */
+ movq %rdi,%rax
+ /* 8(%rsp): hidden type arg */
+ movq 16(%rsp),%rdi
+ movq 24(%rsp),%rsi
+ movq 32(%rsp),%rdx
+ movq 40(%rsp),%r10
+ movq 48(%rsp),%r8
+ movq 56(%rsp),%r9
+
+ syscall
+
+ ret
+
diff --git a/lib/std/syscall+osx-x64.s b/lib/std/syscall+osx-x64.s
new file mode 100644
index 0000000..5984c24
--- /dev/null
+++ b/lib/std/syscall+osx-x64.s
@@ -0,0 +1,96 @@
+.globl _sys$syscall
+_sys$syscall:
+
+ /*
+ 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.
+ */
+ movq %rdi,%rax
+ /* 8(%rsp): hidden type arg */
+ movq 16(%rsp),%rdi
+ movq 24(%rsp),%rsi
+ movq 32(%rsp),%rdx
+ movq 40(%rsp),%r10
+ movq 48(%rsp),%r8
+ movq 56(%rsp),%r9
+
+ syscall
+ jae .success
+ negq %rax
+
+.success:
+ ret
+
+/*
+ * OSX is strange about fork, and needs an assembly wrapper.
+ * The fork() syscall, when called directly, returns the pid in both
+ * processes, which means that both parent and child think they're
+ * the parent.
+ *
+ * checking this involves peeking in %edx, so we need to do this in asm.
+ */
+.globl _sys$__osx_fork
+_sys$__osx_fork:
+ movq $0x2000002,%rax
+ syscall
+
+ jae .forksuccess
+ negq %rax
+
+.forksuccess:
+ testl %edx,%edx
+ jz .isparent
+ xorq %rax,%rax
+.isparent:
+ ret
+
+/*
+ * OSX is strange about pipe, and needs an assembly wrapper.
+ * The pipe() syscall returns the pipes created in eax:edx, and
+ * needs to copy them to the destination locations manually.
+ */
+.globl _sys$__osx_pipe
+_sys$__osx_pipe:
+ movq $0x200002a,%rax
+ syscall
+
+ jae .pipesuccess
+ negq %rax
+
+.pipesuccess:
+ movl %eax,(%rdi)
+ movl %edx,4(%rdi)
+ xorq %rax,%rax
+ ret
+
+.globl _sys$__osx_lseek
+_sys$__osx_lseek:
+ movq $0x20000C7,%rax
+ syscall
+
+ jae .lseeksuccess
+ negq %rax
+
+.lseeksuccess:
+ shlq $32,%rdx
+ orq %rdx,%rax
+ ret
+
+
+.globl _sys$__osx_gettimeofday
+_sys$__osx_gettimeofday:
+ movq $0x2000074,%rax
+ syscall
+
+ jae .gettimeofdaysuccess
+ negq %rax
+
+.gettimeofdaysuccess:
+ movq %rax, (%rdi)
+ movl %edx,8(%rdi)
+ xorq %rax,%rax
+ ret
+
diff --git a/lib/std/syscall+plan9-x64.s b/lib/std/syscall+plan9-x64.s
new file mode 100644
index 0000000..4de6502
--- /dev/null
+++ b/lib/std/syscall+plan9-x64.s
@@ -0,0 +1,33 @@
+/*
+Ugly: Kernel is caller-save, Myrddin
+is callee-save. We need to preserve
+registers before entering the kernel.
+
+However, we also need SP to point to the
+start of the arguments.
+
+Luckily, the kernel doesn't touch our stack,
+and we have 256 bytes of gap if we get a note.
+*/
+TEXT sys$syscall+0(SB),1,$0
+ MOVQ R11,-16(SP)
+ MOVQ R12,-24(SP)
+ MOVQ R13,-32(SP)
+ MOVQ R14,-40(SP)
+ MOVQ R15,-48(SP)
+ MOVQ BP,-56(SP)
+ MOVQ 8(SP),RARG
+
+ ADDQ $8,SP
+ MOVQ DI, RARG
+ SYSCALL
+ SUBQ $8,SP
+
+ MOVQ -16(SP),R11
+ MOVQ -24(SP),R12
+ MOVQ -32(SP),R13
+ MOVQ -40(SP),R14
+ MOVQ -48(SP),R15
+ MOVQ -56(SP),BP
+ RET
+
diff --git a/lib/std/syserrno+linux.myr b/lib/std/syserrno+linux.myr
new file mode 100644
index 0000000..18a3fc2
--- /dev/null
+++ b/lib/std/syserrno+linux.myr
@@ -0,0 +1,38 @@
+pkg sys =
+ type errno = int
+
+ const Eperm : errno = -1 /* Operation not permitted */
+ const Enoent : errno = -2 /* No such file or directory */
+ const Esrch : errno = -3 /* No such process */
+ const Eintr : errno = -4 /* Interrupted system call */
+ const Eio : errno = -5 /* I/O error */
+ const Enxio : errno = -6 /* No such device or address */
+ const E2big : errno = -7 /* Argument list too long */
+ const Enoexec : errno = -8 /* Exec format error */
+ const Ebadf : errno = -9 /* Bad file number */
+ const Echild : errno = -10 /* No child processes */
+ const Eagain : errno = -11 /* Try again */
+ const Enomem : errno = -12 /* Out of memory */
+ const Eacces : errno = -13 /* Permission denied */
+ const Efault : errno = -14 /* Bad address */
+ const Enotblk : errno = -15 /* Block device required */
+ const Ebusy : errno = -16 /* Device or resource busy */
+ const Eexist : errno = -17 /* File exists */
+ const Exdev : errno = -18 /* Cross-device link */
+ const Enodev : errno = -19 /* No such device */
+ const Enotdir : errno = -20 /* Not a directory */
+ const Eisdir : errno = -21 /* Is a directory */
+ const Einval : errno = -22 /* Invalid argument */
+ const Enfile : errno = -23 /* File table overflow */
+ const Emfile : errno = -24 /* Too many open files */
+ const Enotty : errno = -25 /* Not a typewriter */
+ const Etxtbsy : errno = -26 /* Text file busy */
+ const Efbig : errno = -27 /* File too large */
+ const Enospc : errno = -28 /* No space left on device */
+ const Espipe : errno = -29 /* Illegal seek */
+ const Erofs : errno = -30 /* Read-only file system */
+ const Emlink : errno = -31 /* Too many links */
+ const Epipe : errno = -32 /* Broken pipe */
+ const Edom : errno = -33 /* Math argument out of domain of func */
+ const Erange : errno = -34 /* Math result not representable */
+;;
diff --git a/lib/std/syserrno+osx.myr b/lib/std/syserrno+osx.myr
new file mode 100644
index 0000000..7b5d888
--- /dev/null
+++ b/lib/std/syserrno+osx.myr
@@ -0,0 +1,54 @@
+pkg sys =
+ type errno = int
+
+ const Eperm : errno = -1 /* Operation not permitted */
+ const Enoent : errno = -2 /* No such file or directory */
+ const Esrch : errno = -3 /* No such process */
+ const Eintr : errno = -4 /* Interrupted system call */
+ const Eio : errno = -5 /* Input/output error */
+ const Enxio : errno = -6 /* Device not configured */
+ const E2big : errno = -7 /* Argument list too long */
+ const Enoexec : errno = -8 /* Exec format error */
+ const Ebadf : errno = -9 /* Bad file descriptor */
+ const Echild : errno = -10 /* No child processes */
+ const Edeadlk : errno = -11 /* Resource deadlock avoided */
+ /* 11 was EAGAIN */
+ const Enomem : errno = -12 /* Cannot allocate memory */
+ const Eacces : errno = -13 /* Permission denied */
+ const Efault : errno = -14 /* Bad address */
+ const Enotblk : errno = -15 /* Block device required */
+ const Ebusy : errno = -16 /* Device / Resource busy */
+ const Eexist : errno = -17 /* File exists */
+ const Exdev : errno = -18 /* Cross-device link */
+ const Enodev : errno = -19 /* Operation not supported by device */
+ const Enotdir : errno = -20 /* Not a directory */
+ const Eisdir : errno = -21 /* Is a directory */
+ const Einval : errno = -22 /* Invalid argument */
+ const Enfile : errno = -23 /* Too many open files in system */
+ const Emfile : errno = -24 /* Too many open files */
+ const Enotty : errno = -25 /* Inappropriate ioctl for device */
+ const Etxtbsy : errno = -26 /* Text file busy */
+ const Efbig : errno = -27 /* File too large */
+ const Enospc : errno = -28 /* No space left on device */
+ const Espipe : errno = -29 /* Illegal seek */
+ const Erofs : errno = -30 /* Read-only file system */
+ const Emlink : errno = -31 /* Too many links */
+ const Epipe : errno = -32 /* Broken pipe */
+
+ /* math software */
+ const Edom : errno = -33 /* Numerical argument out of domain */
+ const Erange : errno = -34 /* Result too large */
+
+ /* non-blocking and interrupt i/o */
+ const Eagain : errno = -35 /* Resource temporarily unavailable */
+ const Einprogress : errno = -36 /* Operation now in progress */
+ const Ealready : errno = -37 /* Operation already in progress */
+
+ /* ipc/network software -- argument errors */
+ const Enotsock : errno = -38 /* Socket operation on non-socket */
+ const Edestaddrreq : errno = -39 /* Destination address required */
+ const Emsgsize : errno = -40 /* Message too long */
+ const Eprototype : errno = -41 /* Protocol wrong type for socket */
+ const Enoprotoopt : errno = -42 /* Protocol not available */
+ const Eprotonosupport : errno = -43 /* Protocol not supported */
+;;
diff --git a/lib/std/syserrno+plan9.myr b/lib/std/syserrno+plan9.myr
new file mode 100644
index 0000000..18a3fc2
--- /dev/null
+++ b/lib/std/syserrno+plan9.myr
@@ -0,0 +1,38 @@
+pkg sys =
+ type errno = int
+
+ const Eperm : errno = -1 /* Operation not permitted */
+ const Enoent : errno = -2 /* No such file or directory */
+ const Esrch : errno = -3 /* No such process */
+ const Eintr : errno = -4 /* Interrupted system call */
+ const Eio : errno = -5 /* I/O error */
+ const Enxio : errno = -6 /* No such device or address */
+ const E2big : errno = -7 /* Argument list too long */
+ const Enoexec : errno = -8 /* Exec format error */
+ const Ebadf : errno = -9 /* Bad file number */
+ const Echild : errno = -10 /* No child processes */
+ const Eagain : errno = -11 /* Try again */
+ const Enomem : errno = -12 /* Out of memory */
+ const Eacces : errno = -13 /* Permission denied */
+ const Efault : errno = -14 /* Bad address */
+ const Enotblk : errno = -15 /* Block device required */
+ const Ebusy : errno = -16 /* Device or resource busy */
+ const Eexist : errno = -17 /* File exists */
+ const Exdev : errno = -18 /* Cross-device link */
+ const Enodev : errno = -19 /* No such device */
+ const Enotdir : errno = -20 /* Not a directory */
+ const Eisdir : errno = -21 /* Is a directory */
+ const Einval : errno = -22 /* Invalid argument */
+ const Enfile : errno = -23 /* File table overflow */
+ const Emfile : errno = -24 /* Too many open files */
+ const Enotty : errno = -25 /* Not a typewriter */
+ const Etxtbsy : errno = -26 /* Text file busy */
+ const Efbig : errno = -27 /* File too large */
+ const Enospc : errno = -28 /* No space left on device */
+ const Espipe : errno = -29 /* Illegal seek */
+ const Erofs : errno = -30 /* Read-only file system */
+ const Emlink : errno = -31 /* Too many links */
+ const Epipe : errno = -32 /* Broken pipe */
+ const Edom : errno = -33 /* Math argument out of domain of func */
+ const Erange : errno = -34 /* Math result not representable */
+;;
diff --git a/lib/std/systypes.myr b/lib/std/systypes.myr
new file mode 100644
index 0000000..e39f279
--- /dev/null
+++ b/lib/std/systypes.myr
@@ -0,0 +1,7 @@
+pkg sys =
+ type size = int64 /* spans entire address space */
+ type usize = int64 /* signed size */
+ type off = int64 /* file offsets */
+ type intptr = uint64/* can hold any pointer losslessly */
+ type time = int64 /* milliseconds since epoch */
+;;
diff --git a/lib/std/syswrap+plan9-x6.myr b/lib/std/syswrap+plan9-x6.myr
new file mode 100644
index 0000000..2559263
--- /dev/null
+++ b/lib/std/syswrap+plan9-x6.myr
@@ -0,0 +1,104 @@
+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 -> int64)
+ 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.gettos().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
+
+ n = sys.await(buf[:])
+ -> pid
+}
+
+/* memory stuff */
+const getmem = {sz; -> sys.mmap(0 castto(byte#), sz castto(sys.size), sys.Mprotrw, sys.Mpriv | sys.Manon, -1, 0)}
+const freemem = {p, sz; sys.munmap(p, sz castto(sys.size))}
diff --git a/lib/std/syswrap+plan9.myr b/lib/std/syswrap+plan9.myr
new file mode 100644
index 0000000..b18b8da
--- /dev/null
+++ b/lib/std/syswrap+plan9.myr
@@ -0,0 +1,220 @@
+use sys
+
+use "option.use"
+use "types.use"
+
+pkg std =
+ type fd = sys.fd
+ type pid = sys.pid
+ type fdopt = sys.fdopt
+ type whence = int64
+
+ type sysinfo = struct
+ system : byte[:]
+ version : byte[:]
+ release : byte[:]
+ arch : byte[:]
+ ;;
+
+ const Seekset : whence = 0
+ const Seekcur : whence = 1
+ const Seekend : whence = 2
+
+ 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 seek : (fd : fd, delta : off, whence : whence -> off)
+ const pipe : (fds : fd[2]# -> int64)
+ const dup2 : (ofd : fd, nfd : fd -> fd)
+
+ /* useful/portable bits of stat */
+ const fmtime : (f : byte[:] -> option(time))
+ const fsize : (f : byte[:] -> option(off))
+ const fexists : (f : byte[:] -> bool)
+
+ /* the important bits that uname provides */
+ const getsysinfo : (si : sysinfo# -> void)
+
+ /* path manipulation */
+ const mkdir : (path : byte[:], mode : int64 -> int64)
+ const chdir : (path : byte[:] -> bool)
+ const remove : (path : byte[:] -> bool)
+
+ /* 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 waitpid : (pid:pid, loc:int32#, opt : int64 -> pid)
+
+ pkglocal const Canunmap : bool = true
+ pkglocal const getmem : (sz : size -> byte#)
+ pkglocal const freemem : (p : byte#, sz : size -> void)
+ pkglocal const curtime : (-> time)
+ pkglocal const p9errstr : (buf : byte[:] -> byte[:])
+
+ /* statbuf offsets */
+ pkglocal const Sizeoff : int64 = 0
+ pkglocal const Typeoff : int64 = 2
+ pkglocal const Devoff : int64 = 4
+ pkglocal const Qidtypeoff : int64 =8
+ pkglocal const Qidversoff : int64 = 9
+ pkglocal const Qidpathoff : int64 = 13
+ pkglocal const Modeoff : int64 = 21
+ pkglocal const Atimeoff : int64 = 25
+ pkglocal const Mtimeoff : int64 = 29
+ pkglocal const Lengthoff : int64 = 31
+ pkglocal const Stringsoff : int64 = 39
+;;
+
+/* UGLY: circular dependency breaking... */
+extern const getenvv : (name : byte[:], default : byte[:] -> byte[:])
+
+/* 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 & ~Ocreat) 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)
+}
+
+
+/* useful/portable bits of stat */
+const fexists = {path
+ var buf : byte[4] /* big enough for size, nothing else. */
+ -> sys.stat(path, buf[:]) >= 0
+}
+
+const fmtime = {path
+ var buf : byte[Stringsoff + 512] /* enough space for some strings */
+
+ if sys.stat(path, buf[:]) < Stringsoff
+ -> `None
+ ;;
+ -> `Some (getle32(buf[Mtimeoff:Mtimeoff + 8]) castto(time))
+}
+
+const fsize = {path
+ var buf : byte[Stringsoff + 512] /* enough space for some strings */
+
+ if sys.stat(path, buf[:]) < Stringsoff
+ -> `None
+ ;;
+ -> `Some (getle64(buf[Lengthoff:Lengthoff + 8]) castto(off))
+}
+
+const getsysinfo = {si
+ si.system = getenvv("osname", "Plan9")
+ si.release = "4"
+ si.version = "0"
+ si.arch = getenvv("objtype", "amd64")
+}
+
+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 seek = {fd, off, whence; -> sys.seek(fd castto(sys.fd), off castto(sys.off), whence castto(int64)) castto(off)}
+const pipe = {fds; -> sys.pipe(fds castto(sys.fd[2]#)) castto(int64)}
+const dup2 = {ofd, nfd; -> sys.dup(ofd castto(sys.fd), nfd castto(sys.fd)) castto(fd)}
+
+/* path manipulation */
+const remove = {path; -> sys.remove(path) == 0}
+const chdir = {path; -> sys.chdir(path) == 0}
+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)}
+
+/* memory stuff */
+const getmem = {sz
+ var endp, oldp
+
+ oldp = sys.curbrk
+ endp = (sys.curbrk castto(intptr)) + (sz castto(intptr))
+ endp = (endp + 4095) & ~4095
+ if sys.brk_(endp castto(byte#)) < 0
+ -> Failmem
+ ;;
+ sys.curbrk = endp castto(byte#)
+ -> oldp
+}
+
+const freemem = {p, sz
+ /* FIXME: we leak address space */
+ sys.segfree(p, sz castto(sys.size))
+}
+
+const curtime = {
+ -> sys.nsec()/1000 castto(time)
+}
+
+const p9errstr = {errbuf
+ var i
+
+ sys.errstr(errbuf)
+ for i = 0; errbuf[i] != 0 && i < errbuf.len; i++
+ continue
+ ;;
+ -> errbuf[:i]
+}
+
+/* FIXME: will be needed when we resize stat bufs when statting.
+const statsz = {buf
+ -> (buf[0] castto(int64)) | ((buf[1] << 8) castto(int64))
+}
+*/
+
+const getle32 = {buf
+ -> (buf[0] castto(int32)) \
+ | ((buf[1] castto(int32)) << 8) \
+ | ((buf[2] castto(int32)) << 16) \
+ | ((buf[3] castto(int32)) << 24)
+}
+
+const getle64 = {buf
+ -> (buf[0] castto(int64)) \
+ | ((buf[1] castto(int64)) << 8) \
+ | ((buf[2] castto(int64)) << 16) \
+ | ((buf[3] castto(int64)) << 24) \
+ | ((buf[4] castto(int64)) << 64) \
+ | ((buf[5] castto(int64)) << 40) \
+ | ((buf[6] castto(int64)) << 48) \
+ | ((buf[7] castto(int64)) << 56)
+}
diff --git a/lib/std/syswrap+posixy.myr b/lib/std/syswrap+posixy.myr
new file mode 100644
index 0000000..31d98b5
--- /dev/null
+++ b/lib/std/syswrap+posixy.myr
@@ -0,0 +1,148 @@
+use sys
+use "cstrconv.use"
+use "option.use"
+use "types.use"
+use "errno.use"
+
+pkg std =
+ type fd = sys.fd
+ type pid = sys.pid
+ type fdopt = sys.fdopt
+ type whence = sys.whence
+
+ type sysinfo = struct
+ system : byte[:]
+ version : byte[:]
+ release : byte[:]
+ arch : byte[:]
+ uname : sys.utsname /* storage */
+ ;;
+
+ const Failmem : byte# = -1 castto(byte#)
+
+ const Seekset : whence = sys.Seekset castto(whence)
+ const Seekcur : whence = sys.Seekcur castto(whence)
+ const Seekend : whence = sys.Seekend castto(whence)
+
+ const Ordonly : fdopt = sys.Ordonly castto(fdopt)
+ const Owronly : fdopt = sys.Owronly castto(fdopt)
+ const Ordwr : fdopt = sys.Ordwr castto(fdopt)
+ const Ocreat : fdopt = sys.Ocreat castto(fdopt)
+ const Otrunc : fdopt = sys.Otrunc castto(fdopt)
+ const Oappend : fdopt = sys.Oappend castto(fdopt)
+ const Odir : fdopt = sys.Odir castto(fdopt)
+
+ /* 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)
+ const seek : (fd : fd, delta : off, whence : whence -> off)
+ const dup2 : (ofd : fd, nfd : fd -> fd)
+
+ /* useful/portable bits of stat */
+ const fmtime : (f : byte[:] -> option(time))
+ const fsize : (f : byte[:] -> option(off))
+ const fexists : (f : byte[:] -> bool)
+
+ /* useful/portable bits of uname */
+ const getsysinfo : (si : sysinfo# -> void)
+
+ /* path manipulation */
+ const mkdir : (path : byte[:], mode : int64 -> int64)
+ const remove : (path : byte[:] -> bool)
+ const chdir : (path : byte[:] -> bool)
+
+ /* 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 waitpid : (pid:pid, loc:int32#, opt : int64 -> int64)
+
+ pkglocal const Canunmap : bool = true
+ pkglocal const getmem : (sz : size -> byte#)
+ pkglocal const freemem : (p : byte#, sz : size -> void)
+ pkglocal const curtime : (-> time)
+;;
+
+/* fd stuff */
+const open = {path, opts; -> sys.open(path, opts castto(sys.fdopt)) castto(fd)}
+const openmode = {path, opts, mode; -> sys.openmode(path, opts castto(sys.fdopt), mode) castto(fd)}
+const close = {fd; -> sys.close(fd castto(sys.fd))}
+const creat = {path, mode; -> sys.creat(path, mode) castto(fd)}
+const read = {fd, buf; -> sys.read(fd castto(sys.fd), buf) castto(size)}
+const write = {fd, buf; -> sys.write(fd castto(sys.fd), buf) castto(size)}
+const pipe = {fds; -> sys.pipe(fds castto(sys.fd[2]#))}
+const seek = {fd, delta, whence; -> sys.lseek(fd castto(sys.fd), delta castto(sys.off), whence castto(sys.whence)) castto(off)}
+const dup2 = {ofd, nfd; -> sys.dup2(ofd castto(sys.fd), nfd castto(sys.fd)) castto(fd)}
+
+/* path manipulation */
+const mkdir = {path, mode; -> sys.mkdir(path, mode)}
+const chdir = {path; -> sys.chdir(path) == 0}
+const remove = {path; -> sys.unlink(path) == 0}
+
+/* useful/portable bits of uname */
+const getsysinfo = {si
+ sys.uname(&si.uname)
+ si.system = cstrconv(si.uname.system[:])
+ si.version = cstrconv(si.uname.version[:])
+ si.release = cstrconv(si.uname.release[:])
+ si.arch = cstrconv(si.uname.machine[:])
+}
+
+/* process stuff */
+const getpid = {; -> sys.getpid() castto(pid)}
+const suicide = {; sys.kill(sys.getpid(), 6)} /* kill self with sigabort */
+const fork = {; -> sys.fork() castto(pid)}
+const execv = {cmd, args; -> sys.execv(cmd, args)}
+const execve = {cmd, args, env; -> sys.execve(cmd, args, env)}
+const sleep = {time; sys.sleep(time)}
+
+/* memory stuff */
+const getmem = {sz; -> sys.mmap(0 castto(byte#), sz castto(sys.size), sys.Mprotrw, sys.Mpriv | sys.Manon, -1, 0)}
+const freemem = {p, sz; sys.munmap(p, sz castto(sys.size))}
+const curtime = {
+ var tm, sec, nsec
+
+ if sys.clock_gettime(`sys.Clockrealtime, &tm) == 0
+ sec = tm.sec
+ nsec = tm.nsec castto(uint64)
+ -> (sec*1_000_000 + nsec/1000) castto(time)
+ else
+ -> -1
+ ;;
+}
+
+const fexists = {path
+ var sb
+
+ -> sys.stat(path, &sb) == 0
+}
+
+const fmtime = {path
+ var sb
+ var sec, nsec
+
+ if sys.stat(path, &sb) == 0
+ sec = sb.mtime.sec castto(time)
+ nsec = sb.mtime.nsec castto(time)
+ -> `Some sec*1000 + nsec/1_000_000
+ else
+ -> `None
+ ;;
+}
+
+const fsize = {path
+ var sb
+
+ if sys.stat(path, &sb) == 0
+ -> `Some (sb.size castto(off))
+ else
+ -> `None
+ ;;
+}
diff --git a/lib/std/syswrap-ss+linux.myr b/lib/std/syswrap-ss+linux.myr
new file mode 100644
index 0000000..1982e54
--- /dev/null
+++ b/lib/std/syswrap-ss+linux.myr
@@ -0,0 +1,22 @@
+use sys
+use "errno.use"
+
+pkg std =
+ const exit : (status:int -> void)
+ pkglocal const bgetcwd : (buf : byte[:] -> errno)
+;;
+
+const exit = {status; sys.exit_group(status)}
+const bgetcwd = {buf;
+ var err
+ err = sys.getcwd(buf) castto(errno)
+ /*
+ if we got a length back, it includes
+ the nul byte. chop that off.
+ */
+ if err > 0
+ -> err - 1
+ else
+ -> err
+ ;;
+}
diff --git a/lib/std/syswrap-ss+osx.myr b/lib/std/syswrap-ss+osx.myr
new file mode 100644
index 0000000..5e7f001
--- /dev/null
+++ b/lib/std/syswrap-ss+osx.myr
@@ -0,0 +1,35 @@
+use sys
+use "errno.use"
+use "cstrconv.use"
+use "slcp.use"
+use "die.use"
+
+pkg std =
+ const exit : (status:int -> void)
+ pkglocal const bgetcwd : (buf : byte[:] -> errno)
+;;
+
+const exit = {status; sys.exit(status)}
+const bgetcwd = {buf
+ var path : byte[sys.Maxpathlen]
+ var fd, len, err
+
+ fd = sys.open(".", sys.Ordonly)
+ if fd < 0
+ -> fd castto(errno)
+ ;;
+ /*
+ FIXME: if the path is longer than sys.Pathmax, we should fall back to
+ the ugly method of finding the path.
+ */
+ err = sys.fcntl(fd, sys.Fgetpath, path[:] castto(byte#)) castto(errno)
+ if err < 0
+ -> err
+ ;;
+ len = cstrlen(path[:])
+ if len >= buf.len
+ -> Erange
+ ;;
+ slcp(buf[:len], path[:len])
+ -> len castto(errno)
+}
diff --git a/lib/std/syswrap-ss+plan9.myr b/lib/std/syswrap-ss+plan9.myr
new file mode 100644
index 0000000..9615a6e
--- /dev/null
+++ b/lib/std/syswrap-ss+plan9.myr
@@ -0,0 +1,56 @@
+use sys
+
+use "errno.use"
+use "cstrconv.use"
+
+pkg std =
+ const exit : (status : int -> void)
+ pkglocal const bgetcwd : (buf : byte[:] -> errno)
+;;
+
+const bgetcwd = {buf
+ var fd
+
+ fd = sys.open(".", sys.Ordonly)
+ if fd < 0
+ -> fd castto(errno)
+ ;;
+
+ if sys.fd2path(fd, buf) == 0
+ /*
+ Because we don't return the size, the best we can do is
+ assume that if the buffer is completely full, we have
+ truncated it. Since we truncate at utf8 characters, we
+ can have at most 3 bytes truncated (4 bytes will fit
+ any utf8 char), and one byte for the nul terminator.
+ */
+ if cstrlen(buf) + 5 == buf.len
+ -> Erange
+ else
+ -> cstrlen(buf) castto(errno)
+ ;;
+ ;;
+ -> Emisc
+}
+
+const digitchars = "0123456789"
+const exit = {status
+ var buf : byte[32] /* big enough for exit status numbers */
+ var n, i
+
+ if status == 0
+ sys.exits("")
+ else
+ status &= 255
+ i = 100
+ n = 0
+ while i > 0
+ if status >= i
+ buf[n++] = digitchars[(status/i)%10]
+ ;;
+ i /= 10
+ ;;
+ sys.exits(buf[:n])
+ ;;
+}
+
diff --git a/lib/std/test/bigint.myr b/lib/std/test/bigint.myr
new file mode 100644
index 0000000..582fe60
--- /dev/null
+++ b/lib/std/test/bigint.myr
@@ -0,0 +1,136 @@
+use std
+
+type cmd = union
+ `Add (cmd#, cmd#)
+ `Sub (cmd#, cmd#)
+ `Mul (cmd#, cmd#)
+ `Div (cmd#, cmd#)
+ `Mod (cmd#, cmd#)
+ `Shl (cmd#, cmd#)
+ `Shr (cmd#, cmd#)
+ `Modpow (cmd#, cmd#, cmd#)
+ `Val byte[:]
+;;
+
+const main = {
+ var a, b, c, d, e
+ var buf : byte[64], n
+
+ /* a few combined ops */
+ a = std.mkbigint(1234)
+ b = std.mkbigint(0x7fffffff)
+ c = std.mkbigint(7919)
+ d = std.mkbigint(113051)
+ e = std.mkbigint(11)
+
+ std.bigmul(a, b)
+ std.bigmul(a, b)
+ std.bigadd(a, c)
+ std.bigsub(a, d)
+ std.bigdiv(a, e)
+
+ std.bigfree(b)
+ std.bigfree(c)
+ std.bigfree(d)
+ std.bigfree(e)
+
+ n = std.bigbfmt(buf[:], a, 0)
+ std.assert(std.sleq(buf[:n], "517347321949036993306"), "simple smoke test failed")
+
+ /* make sure we format '0' correctly */
+ run(std.mk(`Val "0"), "0")
+ /* smoke test for division */
+ run(std.mk(`Div (\
+ std.mk(`Val "1234_5678_1234_6789_6666_7777_8888"), \
+ std.mk(`Val "1234_5678_1234_6789_6666_7777"))), \
+ "10000")
+ run(std.mk(`Div (\
+ std.mk(`Val "0xffff_1234_1234_1234_1234"), \
+ std.mk(`Val "0xf010_1234_2314"))), \
+ "4580035496")
+ run(std.mk(`Div (\
+ std.mk(`Val "5192296858534810493479828944327220"), \
+ std.mk(`Val "75557863709417659441940"))), \
+ "68719476751")
+ run(std.mk(`Div (\
+ std.mk(`Val "75557863709417659441940"), \
+ std.mk(`Val "5192296858534810493479828944327220"))), \
+ "0")
+
+ /* smoke test for mod */
+ run(std.mk(`Mod (\
+ std.mk(`Val "5192296858534810493479828944327220"), \
+ std.mk(`Val "75557863709417659441940"))),\
+ "257025710597479990280")
+
+ run(std.mk(`Modpow (\
+ std.mk(`Val "1"), \
+ std.mk(`Val "3"), \
+ std.mk(`Val "2"))), \
+ "1")
+
+ run(std.mk(`Modpow (\
+ std.mk(`Val "5192296858534810493479828944327220"), \
+ std.mk(`Val "75557863709417659441940"), \
+ std.mk(`Val "755578"))), \
+ "49054")
+ run(std.mk(`Modpow (\
+ std.mk(`Val "7220"), \
+ std.mk(`Val "755578"), \
+ std.mk(`Val "75557863709417659441940"))), \
+ "27076504425474791131220")
+
+}
+
+const run = {e : cmd#, res : byte[:]
+ var buf : byte[4096]
+ var v, n
+
+ v = eval(e)
+ n = std.bigbfmt(buf[:], v, 0)
+ if !std.sleq(buf[:n], res)
+ std.fatal("%s != %s\n", buf[:n], res)
+ ;;
+}
+
+const eval = {e : cmd#
+ var buf : byte[2048]
+ var a, b, c /* scratch vars */
+ var n /* buf len */
+
+ match e#
+ | `Add (x, y): -> binop("+", std.bigadd, x, y)
+ | `Sub (x, y): -> binop("-", std.bigsub, x, y)
+ | `Mul (x, y): -> binop("*", std.bigmul, x, y)
+ | `Div (x, y): -> binop("/", std.bigdiv, x, y)
+ | `Mod (x, y): -> binop("%", std.bigmod, x, y)
+ | `Shl (x, y): -> binop("<<", std.bigshl, x, y)
+ | `Shr (x, y): -> binop(">>", std.bigshr, x, y)
+ | `Val x:
+ a = try(std.bigparse(x))
+ n = std.bigbfmt(buf[:], a, 0)
+ -> a
+ | `Modpow (x, y, z):
+ a = eval(x)
+ b = eval(y)
+ c = eval(z)
+ -> std.bigmodpow(a, b, c)
+ ;;
+}
+
+const binop = {name, op, x, y
+ var a, b
+
+ a = eval(x)
+ b = eval(y)
+ op(a, b)
+ std.bigfree(b)
+ -> a
+}
+
+generic try = {x : std.option(@a)
+ match x
+ | `std.Some v: -> v
+ | `std.None: std.die("failed to get val")
+ ;;
+}
diff --git a/lib/std/test/bytebuf.myr b/lib/std/test/bytebuf.myr
new file mode 100644
index 0000000..48f6715
--- /dev/null
+++ b/lib/std/test/bytebuf.myr
@@ -0,0 +1,72 @@
+use "bytebuf.use"
+
+/* support */
+use "die.use"
+use "fmt.use"
+use "sleq.use"
+
+const main = {
+ var bb, v
+
+ bb = std.mkbytebuf()
+
+ std.bytebufputc(bb, 'a')
+ std.assert(bb.len == 1, "byte buf size wrong\n")
+ std.assert(std.sleq(bb.buf[:bb.len], "a"), \
+ "byte buf contents not \"a\"\n")
+
+ std.bytebufputs(bb, "bc")
+ std.assert(bb.len == 3, "byte buf size wrong\n")
+ std.assert(std.sleq(bb.buf[:bb.len], "abc"), \
+ "byte buf contents not \"abc\"\n")
+
+ std.bytebufputb(bb, 'd' castto(byte))
+ std.assert(bb.len == 4, "byte buf size wrong\n")
+ std.assert(std.sleq(bb.buf[:bb.len], "abcd"), \
+ "byte buf contents not \"abcd\"\n")
+
+ std.bytebufputle8(bb, 'e' castto(int64))
+ std.assert(bb.len == 5, "byte buf size wrong\n")
+ std.assert(std.sleq(bb.buf[:bb.len], "abcde"), \
+ "byte buf contents not \"abcde\"\n")
+
+ std.bytebufputbe8(bb, 'e' castto(int64))
+ std.assert(bb.len == 6, "byte buf size wrong\n")
+ std.assert(std.sleq(bb.buf[:bb.len], "abcdee"), \
+ "byte buf contents not \"abcdee\"\n")
+
+ std.bytebufputle16(bb, ('f' | ('g' << 8)) castto(int64))
+ std.assert(bb.len == 8, "byte buf size wrong\n")
+ std.assert(std.sleq(bb.buf[:bb.len], "abcdeefg"), \
+ "byte buf contents not \"abcdeefg\"\n")
+
+ std.bytebufputbe16(bb, ('f' | ('g' << 8)) castto(int64))
+ std.assert(bb.len == 10, "byte buf size wrong\n")
+ std.assert(std.sleq(bb.buf[:bb.len], "abcdeefggf"), \
+ "byte buf contents not \"abcdeefggf\"\n")
+
+ std.bytebufputle32(bb, ('h' | ('i' << 8) | ('j' << 16) | ('k' << 24)) castto(int64))
+ std.assert(bb.len == 14, "byte buf size wrong\n")
+ std.assert(std.sleq(bb.buf[:bb.len], "abcdeefggfhijk"), \
+ "byte buf contents not \"abcdeefggfhijk\"\n")
+
+ std.bytebufputbe32(bb, ('h' | ('i' << 8) | ('j' << 16) | ('k' << 24)) castto(int64))
+ std.assert(bb.len == 18, "byte buf size wrong\n")
+ std.assert(std.sleq(bb.buf[:bb.len], "abcdeefggfhijkkjih"), \
+ "byte buf contents not \"abcdeefggfhijkkji\"\n")
+
+ v = ('l' | ('m' << 8) | ('n' << 16) | ('o' << 24)) castto(int64)
+ v |= v << 32
+ std.bytebufputle64(bb, v)
+ std.assert(bb.len == 26, "byte buf size wrong\n")
+ std.assert(std.sleq(bb.buf[:bb.len], "abcdeefggfhijkkjihlmnolmno"), \
+ "byte buf contents not \"abcdeefggfhijkkjihlmnolmno\"\n")
+
+ v = ('l' | ('m' << 8) | ('n' << 16) | ('o' << 24)) castto(int64)
+ v |= v << 32
+ std.bytebufputbe64(bb, v)
+ std.assert(bb.len == 34, "byte buf size wrong\n")
+ std.assert(std.sleq(bb.buf[:bb.len], "abcdeefggfhijkkjihlmnolmnoonmlonml"), \
+ "byte buf contents not \"abcdeefggfhijkkjilmnolmnoonmlonml\"\n")
+}
+
diff --git a/lib/std/test/chartype.myr b/lib/std/test/chartype.myr
new file mode 100644
index 0000000..d0df1d2
--- /dev/null
+++ b/lib/std/test/chartype.myr
@@ -0,0 +1,23 @@
+use std
+
+const main = {
+ std.assert(std.isalpha('a'), "a should be alpha\n")
+ std.assert(std.isupper('A'), "A should be upper\n")
+ std.assert(std.islower('a'), "a should be lower\n")
+ std.assert(std.isdigit('0'), "0 should be isdigit\n")
+ std.assert(std.isnum('\u{0c66}'), "\u{0c66} should be isnum\n")
+ std.assert(std.isalnum('a'), "a should be isalnum\n")
+ std.assert(std.isalnum('0'), "0 should be isalnum\n")
+ std.assert(std.isspace(' '), "' ' should be isspace\n")
+ std.assert(std.isblank(' '), "' ' should be isblank\n")
+
+ std.assert(!std.isalpha('0'), "0 should not be alpha\n")
+ std.assert(!std.isupper('a'), "a should not be upper\n")
+ std.assert(!std.islower('A'), "A should not be lower\n")
+ std.assert(!std.isdigit('a'), "a should not be isdigit\n")
+ std.assert(!std.isnum('a'), " should not be isnum\n")
+ std.assert(!std.isalnum('}'), "a should not be isalnum\n")
+ std.assert(!std.isalnum('!'), "! should not be isalnum\n")
+ std.assert(!std.isspace('@'), "@ should not be isspace\n")
+ std.assert(!std.isblank('@'), "@ should not be isblank\n")
+}
diff --git a/lib/std/test/fmt.myr b/lib/std/test/fmt.myr
new file mode 100644
index 0000000..8de3890
--- /dev/null
+++ b/lib/std/test/fmt.myr
@@ -0,0 +1,34 @@
+use std
+
+const check = {expected, fmt, args : ...
+ var buf : byte[2048]
+ var sl, ap
+
+ ap = std.vastart(&args)
+ sl = std.bfmtv(buf[:], fmt, &ap)
+ if !std.sleq(expected, sl)
+ std.fatal("mismatched fmt: got \"{}\", expected \"{}\"\n", sl, expected)
+ ;;
+}
+
+const main = {
+ check(" abcd", "{w=10}", "abcd")
+ check("00000bdcae", "{p=0,w=10}", "bdcae")
+ check("abcdefghijkl", "{p=0,w=10}", "abcdefghijkl")
+ check("a", "{w=0,p=1}", "a")
+ check(" 10", "{w=10}", 10)
+ check("0000000010", "{p=0,w=10}", 10)
+ check("4294967295", "{p=0,w=10}", -1 castto(uint))
+ check("-000000001", "{p=0,w=10}", -1)
+ check("xxxxxxxx-1", "{p=x,w=10}", -1)
+ check(" -1", "{w=10}", -1)
+ check("100000" , "{3}", 100000)
+ check("foobarbaz", "{}bar{}", "foo", "baz")
+ check("{}barbaz", "{{}}bar{}", "baz")
+ check("{barbaz}", "{{bar{}}}", "baz")
+ check("abcd", "{}", "abcd")
+ check("123", "{}", 123)
+ check("7b", "{x}", 123)
+ check("0x7b", "0x{x}", 123)
+}
+
diff --git a/lib/std/test/htab.myr b/lib/std/test/htab.myr
new file mode 100644
index 0000000..1801d76
--- /dev/null
+++ b/lib/std/test/htab.myr
@@ -0,0 +1,104 @@
+use std
+
+const insertion = {
+ /*
+ var ht
+ var i
+
+ ht = std.mkht(idhash, ideq)
+ /* only a few values; shouldn't trigger growth */
+ for i = 0; i < 5; i++
+ std.htput(ht, i, i)
+ ;;
+ for i = 0; i < 5; i++
+ std.assert(std.htgetv(ht, i, -1) == i, "returned incorrect value from hash table")
+ ;;
+
+ /* and grow */
+ for i = 0; i < 5000; i++
+ std.htput(ht, i, i)
+ ;;
+ for i = 0; i < 5000; i++
+ std.assert(std.htgetv(ht, i, -1) == i, "returned incorrect value from hash table")
+ ;;
+ */
+}
+
+const deletion = {
+ var ht
+ var i
+
+ ht = std.mkht(idhash, ideq)
+ /* create a hash table with a few hundred values */
+ for i = 0; i < 4000; i++
+ std.htput(ht, i, i)
+ ;;
+ for i = 0; i < 200; i++
+ std.htdel(ht, i*2)
+ ;;
+ for i = 0; i < 200; i++
+ std.assert(!std.hthas(ht, i*2), "deleted item still present")
+ ;;
+ for i = 0; i < 200; i++
+ std.assert(std.hthas(ht, i*2+1), "undeleted item missing")
+ ;;
+ for i = 400; i < 4000; i++
+ std.assert(std.hthas(ht, i), "undeleted item missing")
+ ;;
+
+}
+
+const collision = {
+ var ht
+ var i
+
+ ht = std.mkht(idhash, ideq)
+ /* insert an element a few hundred times */
+ for i = 0; i < 500; i++
+ std.htput(ht, 0, i)
+ ;;
+ std.assert(std.hthas(ht, 0), "inserted element not present")
+ std.assert(std.htgetv(ht, 0, -1) == 499, "inserted element has wrong value")
+ std.htdel(ht, 0)
+ std.assert(!std.hthas(ht, 0), "element left in table")
+}
+
+const tombstonefill = {
+ var ht
+ var i
+
+ ht = std.mkht(idhash, ideq)
+ /*
+ insert an element into each slot in the hash table, and
+ delete it. With direct hashing, this is guaranteed to have
+ put a tombstone into each slot.
+ */
+ for i = 0; i <= ht.keys.len; i++
+ std.htput(ht, i, i)
+ std.htdel(ht, i)
+ ;;
+ /* make sure we haven't actually got anything in the table */
+ std.assert(ht.nelt == 0, "elements left in hash table")
+ std.assert(!std.hthas(ht, 1), "found phantom element")
+}
+
+const main = {
+ /* only a few elements */
+ std.put("insertion\n")
+ insertion()
+ std.put("deletion\n")
+ deletion()
+ std.put("collision\n")
+ collision()
+
+ /* what happens if we try to fill everything up with tombstones? */
+ tombstonefill()
+}
+
+const idhash = {x
+ -> x
+}
+
+const ideq = {a, b
+ -> a == b
+}
diff --git a/lib/std/test/ipparse.myr b/lib/std/test/ipparse.myr
new file mode 100644
index 0000000..bbf56e3
--- /dev/null
+++ b/lib/std/test/ipparse.myr
@@ -0,0 +1,64 @@
+use std
+
+const main = {
+ /* valid ipv4 address */
+ eq("1.2.3.4", `std.Some `std.Ipv4 [1,2,3,4])
+
+ /* invalid ipv4 address */
+ eq("1.3.4", `std.None) /* too short */
+ eq("1.2.3.4.5", `std.None) /* too long */
+ eq("1.3.4.256", `std.None) /* out of bounds 1 */
+ eq("260.2.3.4", `std.None) /* out of bounds 2 */
+
+ /* valid ipv6 addresses */
+ eq("2a03:2880:2110:df07:face:b00c:0:1", \
+ `std.Some `std.Ipv6 [0x2a, 0x03, 0x28, 0x80, 0x21, 0x10,
+ 0xdf, 0xfa, 0xce, 0xb0, 0x0c, 0x00, 0x00, 0x01, 0x01])
+ eq("abcd::dcba", \
+ `std.Some `std.Ipv6 [0xab, 0xcd, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])
+ eq("::abcd:dcba", \
+ `std.Some `std.Ipv6 [0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0xab, 0xcd, 0xdc, 0xba])
+ eq("::", `std.Some `std.Ipv6 [0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])
+
+ /* invalid ipv4 addresses */
+ eq("2a03:2880:2110:df07:face:b00c:0:1:abc", `std.None) /* too long */
+ eq("2a03:2880:2110:df07:face:b00c:0", `std.None) /* too short */
+ eq("2a03:2880:2110:df07:face:b00c:0:1:", `std.None) /* trailing ':' */
+}
+
+const eq = {ip, expected
+ var parsed
+ var p, e
+
+ parsed = std.ipparse(ip)
+ p = ipbytes(parsed)
+ e = ipbytes(expected)
+ if !std.sleq(p, e)
+ std.fput(1, "misparsed ip {}\n", ip)
+ std.put("parsed: ")
+ for b in p
+ std.put("{x}, ", b)
+ ;;
+ std.put("\nexpected: ")
+ for b in e
+ std.put("{x}, ", b)
+ ;;
+ std.put("\n")
+ std.fatal("failed\n")
+ ;;
+}
+
+const ipbytes = {ipopt
+ match ipopt
+ | `std.Some ip:
+ match ip
+ | `std.Ipv4 b: -> b[:]
+ | `std.Ipv6 b: -> b[:]
+ ;;
+ | `std.None: -> [][:]
+ ;;
+}
+
diff --git a/lib/std/test/option.myr b/lib/std/test/option.myr
new file mode 100644
index 0000000..fa8c5c0
--- /dev/null
+++ b/lib/std/test/option.myr
@@ -0,0 +1,43 @@
+use std
+
+const f = {x
+ if x == 123
+ -> `std.Some 42
+ else
+ -> `std.None
+ ;;
+}
+
+type t = struct
+ next : std.option(int)
+;;
+
+const main = {
+ var v, s : t
+
+ match `std.Some 42
+ | `std.Some x: std.assert(x == 42, "created wrong value\n")
+ | `std.None: std.assert(false, "should not be reached\n")
+ ;;
+
+ match `std.None
+ | `std.Some x: std.assert(x, "should not be reached\n")
+ | `std.None: /* everything ok */
+ ;;
+
+ v = f(123)
+ match v
+ | `std.Some x: std.assert(x == 42, "created wrong value\n")
+ | `std.None: std.assert(false, "should not be reached\n")
+ ;;
+
+ v = f(666)
+ match v
+ | `std.Some x: std.assert(false, "should not be reached\n")
+ | `std.None: /* everything ok */
+ ;;
+
+ s = [.next = `std.None]
+ s = [.next = `std.Some 123]
+}
+
diff --git a/lib/std/test/pathjoin.myr b/lib/std/test/pathjoin.myr
new file mode 100644
index 0000000..9cdc064
--- /dev/null
+++ b/lib/std/test/pathjoin.myr
@@ -0,0 +1,61 @@
+use std
+
+const main = {
+ /* untouched */
+ norm("foo", "foo")
+ norm("foo/bar", "foo/bar")
+ norm("/foo/bar", "/foo/bar")
+ norm(".", ".")
+
+ /* empty path becomes "." */
+ norm("", ".")
+
+ /* delete //, trailing / */
+ norm("foo/", "foo")
+ norm("foo//bar/baz", "foo/bar/baz")
+ norm("//foo//bar/", "/foo/bar")
+
+ /* delete '.' */
+ norm("foo/./bar", "foo/bar")
+ norm("/foo/bar/.", "/foo/bar")
+ norm("./foo/bar/.", "foo/bar")
+
+ /* elide '..' */
+ norm("/../foo/bar", "/foo/bar")
+ norm("../../foo/bar", "../../foo/bar")
+ norm("foo/bar/..", "foo")
+ norm("foo/bar/../..", ".")
+ norm("foo/../bar/../..", "..")
+ norm("/foo/../bar/../..", "/")
+
+ /* mix all of the above */
+ norm("/../foo//bar", "/foo/bar")
+ norm("..//../foo/bar", "../../foo/bar")
+ norm("foo//./bar/..", "foo")
+ norm("foo/bar/.././..", ".")
+ norm("//foo/../bar/../..", "/")
+ norm("foo/../bar/../..", "..")
+
+ /* vanilla pathjoin */
+ eq(std.pathcat("a", "b"), "a/b")
+ eq(std.pathjoin(["a", "b", "c"][:]), "a/b/c")
+ /* pathjoin with empty dirs */
+ eq(std.pathcat("", "foo"), "foo")
+ eq(std.pathjoin(["", "foo", "bar"][:]), "foo/bar")
+}
+
+const norm = {a, b
+ var p
+
+ p = std.pathnorm(a)
+ if !std.sleq(p, b)
+ std.fatal("mismatched paths: '{}' => '{}' != '{}'\n", a, p, b)
+ ;;
+ std.slfree(p)
+}
+
+const eq = {a, b
+ if !std.sleq(a, b)
+ std.fatal("mismatched paths: '{}' != '{}'\n", a, b)
+ ;;
+}
diff --git a/lib/std/test/search.myr b/lib/std/test/search.myr
new file mode 100644
index 0000000..342a406
--- /dev/null
+++ b/lib/std/test/search.myr
@@ -0,0 +1,34 @@
+use std
+
+const sl = [1, 3, 5, 8, 9, 33]
+
+const main = {
+
+ expect(std.lsearch(sl[:], 1, std.numcmp), `std.Some 0)
+ expect(std.lsearch(sl[:], 33, std.numcmp), `std.Some sl.len - 1)
+ expect(std.lsearch(sl[:], 5, std.numcmp), `std.Some 2)
+ expect(std.lsearch(sl[:], 6, std.numcmp), `std.None)
+
+ expect(std.bsearch(sl[:], 1, std.numcmp), `std.Some 0)
+ expect(std.bsearch(sl[:], 33, std.numcmp), `std.Some sl.len - 1)
+ expect(std.bsearch(sl[:], 5, std.numcmp), `std.Some 2)
+ expect(std.bsearch(sl[:], 6, std.numcmp), `std.None)
+}
+
+const expect = {a, b
+ match a
+ | `std.None:
+ match b
+ | `std.Some x: std.fatal("Expected `std.None, `std.None, got `std.None, `std.Some %i\n", x)
+ | `std.None: /* nothing */
+ ;;
+ | `std.Some x:
+ match b
+ | `std.None: std.fatal("Expected `std.Some %i, `std.Some %i, got `std.Some %i, `std.None\n", x, x, x)
+ | `std.Some y:
+ if x != y
+ std.fatal("Expected `std.Some %i, `std.Some %i, got `std.Some %i, `std.Some %i\n", x, x, x, y)
+ ;;
+ ;;
+ ;;
+}
diff --git a/lib/std/test/slcp.myr b/lib/std/test/slcp.myr
new file mode 100644
index 0000000..67c8298
--- /dev/null
+++ b/lib/std/test/slcp.myr
@@ -0,0 +1,14 @@
+use std
+
+const main = {
+ var a = [1,2,3,4,5]
+ var b = [6,7,8,9,10]
+ var a_cped = [3, 4, 5, 4, 5]
+ var b_cped = [6, 7, 6, 7, 8]
+
+
+ std.slcp(a[:a.len-2], a[2:])
+ std.slcp(b[2:], b[:b.len-2])
+ std.assert(std.sleq(a[:], a_cped[:]), "slcp of a failed")
+ std.assert(std.sleq(b[:], b_cped[:]), "slcp of a failed")
+}
diff --git a/lib/std/test/sort.myr b/lib/std/test/sort.myr
new file mode 100644
index 0000000..e814623
--- /dev/null
+++ b/lib/std/test/sort.myr
@@ -0,0 +1,38 @@
+use std
+
+const main = {
+ var i
+
+ var a = [ 3, 5, 4, 9, 7, 2, 6, 0, 1, 8, ]
+ var a_sorted = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
+ var b = [3, 4, 5, 1, 2, 6, 7, 8, 9, 10]
+ var b_sorted = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
+ var c = ["a", "aa", "b", "C", "Cc", "cC", "d", "f", "fuckit", "go",]
+ var c_sorted = ["C", "Cc", "a", "aa", "b", "cC", "d", "f", "fuckit", "go"]
+
+ /* with custom intcmp */
+ std.sort(a[:], intcmp)
+ std.assert(std.sleq(a[:], a_sorted[:]), "a was missorted")
+
+ /* with libstd generic numcmp */
+ std.sort(b[:], std.numcmp)
+ std.assert(std.sleq(b[:], b_sorted[:]), "b was missorted")
+
+ /* string sorting */
+ std.sort(c[:], std.strcmp)
+ for i = 0; i < c.len; i++
+ std.assert(std.sleq(c[i], c_sorted[i]), "c was missorted")
+ ;;
+}
+
+const intcmp = {a, b
+ if a < b
+ -> `std.Before
+ elif a == b
+ -> `std.Equal
+ else
+ -> `std.After
+ ;;
+}
+
+
diff --git a/lib/std/test/strbuf.myr b/lib/std/test/strbuf.myr
new file mode 100644
index 0000000..2643da5
--- /dev/null
+++ b/lib/std/test/strbuf.myr
@@ -0,0 +1,40 @@
+use std
+
+const main = {
+ var sb
+ var buf : byte[16]
+
+ sb = std.mksb()
+ std.assert(std.sleq(std.sbpeek(sb), ""), "mismatched empty str\n")
+ std.sbputs(sb, "hello")
+ std.assert(std.sleq(std.sbpeek(sb), "hello"), "mismatched hello\n")
+ std.sbputs(sb, ", hello")
+ std.assert(std.sleq(std.sbpeek(sb), "hello, hello"), "mismatched double hello\n")
+ std.sbtrim(sb, 7)
+ std.assert(std.sleq(std.sbpeek(sb), "hello, "), "mismatched trim\n")
+ std.sbputs(sb, "world")
+ std.assert(std.sleq(std.sbpeek(sb), "hello, world"), "mismatched hello world\n")
+ std.sbtrim(sb, -5)
+ std.assert(std.sleq(std.sbpeek(sb), "hello, "), "mismatched rtrim\n")
+ std.sbputc(sb, '世')
+ std.sbputc(sb, '界')
+ std.assert(std.sleq(std.sbpeek(sb), "hello, 世界"), "mismatched unicode\n")
+ std.sbputb(sb, 10)
+ std.assert(std.sleq(std.sbpeek(sb), "hello, 世界\n"), "mismatched byte\n")
+
+ sb = std.mkbufsb(buf[:])
+ std.assert(std.sbputs(sb, "hello"), "failed to add hello\n") /* 5 characters */
+ std.assert(std.sbputs(sb, "hello"), "failed to add hello\n") /* 10 characters */
+ std.assert(std.sbputs(sb, "hello"), "failed to add hello\n") /* 15 characters */
+ std.assert(!std.sbputs(sb, "hello"), "erronous success\n") /* 16 characters */
+ std.assert(std.sleq(std.sbpeek(sb), "hellohellohelloh"), "failed to copy as much as possible\n")
+ std.sbtrim(sb, -1)
+ std.assert(std.sleq(std.sbpeek(sb), "hellohellohello"), "failed rtrim\n")
+ std.sbputc(sb, '世')
+ std.assert(std.sleq(std.sbpeek(sb), "hellohellohello"), "modified overflowed putc\n")
+ std.sbtrim(sb, -2)
+ std.assert(std.sleq(std.sbpeek(sb), "hellohellohel"), "failed rtrim\n")
+ std.sbputc(sb, '世')
+ std.assert(std.sleq(std.sbpeek(sb), "hellohellohel世"), "failed to append with putc\n")
+}
+
diff --git a/lib/std/test/try.myr b/lib/std/test/try.myr
new file mode 100644
index 0000000..525b95c
--- /dev/null
+++ b/lib/std/test/try.myr
@@ -0,0 +1,8 @@
+use std
+
+const main = {
+ var x = `std.Some 123
+ var r : std.result(int, byte[:]) = `std.Ok 666
+ std.assert(std.get(x) == 123, "expected 123 from try")
+ std.assert(std.try(r) == 666, "expected 123 from try")
+}
diff --git a/lib/std/try.myr b/lib/std/try.myr
new file mode 100644
index 0000000..da960ac
--- /dev/null
+++ b/lib/std/try.myr
@@ -0,0 +1,38 @@
+use "result.use"
+use "option.use"
+use "fmt.use"
+
+pkg std =
+ generic try : (v : result(@a, @b) -> @a)
+ generic tryv : (v : result(@a, @b), d : @a -> @a)
+ generic get : (v : option(@a) -> @a)
+ generic getv : (v : option(@a), d : @a -> @a)
+;;
+
+generic try = {v
+ match v
+ | `Ok x: -> x
+ | `Fail m: fatal("error: {}\n", m)
+ ;;
+}
+
+generic tryv = {v, d
+ match v
+ | `Ok x: -> x
+ | `Fail m: -> d
+ ;;
+}
+
+generic get = {v
+ match v
+ | `Some x: -> x
+ | `None: fatal("error: option had `None\n")
+ ;;
+}
+
+generic getv = {v, d
+ match v
+ | `Some x: -> x
+ | `None: -> d
+ ;;
+}
diff --git a/lib/std/types.myr b/lib/std/types.myr
new file mode 100644
index 0000000..5ecf42c
--- /dev/null
+++ b/lib/std/types.myr
@@ -0,0 +1,9 @@
+use sys
+
+pkg std =
+ type size = sys.size /* spans entire address space */
+ type usize = sys.usize /* signed size */
+ type off = sys.off /* file offsets */
+ type intptr = sys.intptr /* can hold any pointer losslessly */
+ type time = sys.time /* milliseconds since epoch */
+;;
diff --git a/lib/std/units.myr b/lib/std/units.myr
new file mode 100644
index 0000000..f1a99e9
--- /dev/null
+++ b/lib/std/units.myr
@@ -0,0 +1,11 @@
+pkg std =
+ /* JEDEC 100B.1 memory sizes */
+ generic KiB : @a::(integral,numeric) = 1024
+ generic MiB : @a::(integral,numeric) = KiB*1024
+ generic GiB : @a::(integral,numeric) = MiB*1024
+ generic TiB : @a::(integral,numeric) = GiB*1024
+ generic PiB : @a::(integral,numeric) = TiB*1024
+ generic EiB : @a::(integral,numeric) = PiB*1024
+ generic ZiB : @a::(integral,numeric) = EiB*1024
+ generic YiB : @a::(integral,numeric) = ZiB*1024
+;;
diff --git a/lib/std/utf.myr b/lib/std/utf.myr
new file mode 100644
index 0000000..03cc8bb
--- /dev/null
+++ b/lib/std/utf.myr
@@ -0,0 +1,103 @@
+use "die.use"
+use "types.use"
+
+pkg std =
+ const Badchar : char = -1 castto(char)
+ const Maxcharlen : size = 4
+ const Maxcharval : char = 0x10FFFF
+
+ const charlen : (chr : char -> size)
+ const encode : (buf : byte[:], chr : char -> size)
+ const decode : (buf : byte[:] -> char)
+ const striter : (str : byte[:] -> (char, byte[:]))
+;;
+
+const charlen = {c
+ if c < 0x80
+ -> 1
+ elif c < 0x800
+ -> 2
+ elif c < 0x10000
+ -> 3
+ elif c < 0x200000
+ -> 4
+ else
+ -> -1
+ ;;
+}
+
+const encode = {buf, c
+ var len
+ var mark
+ var i
+
+ len = charlen(c)
+ if len < 0 || buf.len < len
+ -> -1
+ ;;
+
+ if (len == 1)
+ mark = 0
+ else
+ mark = (((1 << (8 - len)) - 1) ^ 0xff) castto(char)
+ ;;
+
+ for i = len - 1; i > 0; i--
+ buf[i] = (c & 0x3f | 0x80) castto(byte)
+ c >>= 6
+ ;;
+
+ buf[0] = (c | mark) castto(byte)
+ -> len
+}
+
+const decode = {buf
+ var c
+ var b
+
+ (c, b) = striter(buf)
+ -> c
+}
+
+const striter = {str
+ var len
+ var mask
+ var chr
+ var i
+ var c
+ var tmp
+
+ if str.len == 0
+ /* empty string: no resync needed */
+ -> (Badchar, str)
+ ;;
+ c = str[0]
+ len = 0
+ if c & 0x80 == 0 /* 0b0xxx_xxxx */
+ len = 1
+ elif c & 0xe0 == 0xc0 /* 0b110x_xxxx */
+ len = 2
+ elif c & 0xf0 == 0xe0 /* 0b1110_xxxx */
+ len = 3
+ elif c & 0xf8 == 0xf0 /* 0b1111_0xxx */
+ len = 4
+ else
+ /* skip one char forward so we can try
+ resyncing the character stream */
+ -> (Badchar, str[1:])
+ ;;
+
+ if len == 0 || len > str.len
+ /* again, we want to try to resync */
+ -> (Badchar, str[1:])
+ ;;
+
+ mask = (1 << (8 - len)) - 1
+ chr = (c castto(uint32)) & mask
+ for i = 1; i < len; i++
+ tmp = str[i] castto(uint32)
+ chr = (chr << 6) | (tmp & 0x3f)
+ ;;
+
+ -> (chr castto(char), str[len:])
+}
diff --git a/lib/std/util+plan9-x64.s b/lib/std/util+plan9-x64.s
new file mode 100644
index 0000000..f4b8586
--- /dev/null
+++ b/lib/std/util+plan9-x64.s
@@ -0,0 +1,64 @@
+/*
+ * Allocates a C string on the stack, for
+ * use within system calls, which is the only
+ * place the Myrddin stack should need nul-terminated
+ * strings.
+ *
+ * This is in assembly, because for efficiency we
+ * allocate the C strings on the stack, and don't adjust
+ * SP when returning.
+ */
+TEXT sys$cstring+0(SB),$0
+ /* save registers */
+ MOVQ SP,AX
+ SUBQ $40,SP
+ MOVQ BP,-8(AX)
+ MOVQ R15,-16(AX)
+ MOVQ SI,-24(AX)
+ MOVQ DI,-32(AX)
+ MOVQ CX,-40(AX)
+ MOVQ AX,BP
+
+ MOVQ (BP),R15 /* ret addr */
+ MOVQ 8(BP),SI /* src */
+ MOVQ 16(BP),CX /* len */
+
+ SUBQ CX,SP /* get stack */
+ SUBQ $1,SP /* +1 for nul */
+ MOVQ SP,DI /* dest */
+ MOVQ SP,AX /* ret val */
+ ANDQ $(~15),SP /* align */
+ SUBQ $32,SP /* "unpop" the args and make room for return addr */
+
+ CLD
+ REP
+ MOVSB
+ MOVB $0,(DI) /* terminate */
+
+ /* Restore registers */
+ MOVQ R15,0(SP) /* place ret addr */
+ MOVQ -40(BP),CX
+ MOVQ -32(BP),DI
+ MOVQ -24(BP),SI
+ MOVQ -16(BP),R15
+ MOVQ -8(BP) ,BP
+ RET
+
+TEXT sys$alloca+0(SB),$0
+ /* save registers */
+ MOVQ (SP),R10 /* ret addr */
+
+ /* get stack space */
+ SUBQ DI,SP /* get stack space */
+ MOVQ SP,AX /* top of stack (return value) */
+ ANDQ $(~15),SP /* align */
+ SUBQ $32,SP /* "unpop" the args, and make room for ret addr */
+
+ MOVQ R10,(SP) /* place ret addr */
+ 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)
+
diff --git a/lib/std/util+posixy-x64.s b/lib/std/util+posixy-x64.s
new file mode 100644
index 0000000..d44ab79
--- /dev/null
+++ b/lib/std/util+posixy-x64.s
@@ -0,0 +1,72 @@
+/*
+ * Allocates a C string on the stack, for
+ * use within system calls, which is the only
+ * place the Myrddin stack should need nul-terminated
+ * strings.
+ *
+ * This is in assembly, because for efficiency we
+ * allocate the C strings on the stack, and don't adjust
+ * %rsp when returning.
+ */
+.globl sys$cstring
+.globl _sys$cstring
+_sys$cstring:
+sys$cstring:
+ /* save registers */
+ pushq %rbp
+ movq %rsp,%rbp
+ pushq %r15
+ pushq %rsi
+ pushq %rdi
+ pushq %rcx
+
+ movq 8(%rbp),%r15 /* ret addr */
+ movq 16(%rbp),%rsi /* src */
+ movq 24(%rbp),%rcx /* len */
+
+ subq %rcx,%rsp /* get stack */
+ subq $1,%rsp /* +1 for nul */
+ movq %rsp,%rdi /* dest */
+ movq %rsp,%rax /* ret val */
+ subq $31,%rsp /* "unpop" the args */
+ andq $(~15),%rsp /* align */
+
+ cld
+ rep movsb
+ movb $0,(%rdi) /* terminate */
+
+ pushq %r15 /* ret addr */
+
+ /* restore registers */
+ movq -32(%rbp),%rcx
+ movq -24(%rbp),%rdi
+ movq -16(%rbp),%rsi
+ movq -8(%rbp),%r15
+ movq (%rbp),%rbp
+ ret
+
+.globl sys$alloca
+.globl _sys$alloca
+_sys$alloca:
+sys$alloca:
+ /* save registers */
+ pushq %rbp
+ movq %rsp,%rbp
+ pushq %r15
+ pushq %rbx
+
+ movq 8(%rbp),%r15 /* ret addr */
+
+ /* get stack space */
+ subq %rdi,%rsp /* get stack space */
+ movq %rsp,%rax /* top of stack (return value) */
+ subq $31,%rsp /* "unpop" the args for return */
+ andq $(~15),%rsp /* align */
+
+ pushq %r15 /* ret addr */
+
+ /* restore registers */
+ movq -16(%rbp),%rbx
+ movq -8(%rbp),%r15
+ movq (%rbp),%rbp
+ ret
diff --git a/lib/std/varargs.myr b/lib/std/varargs.myr
new file mode 100644
index 0000000..c2ed1b2
--- /dev/null
+++ b/lib/std/varargs.myr
@@ -0,0 +1,105 @@
+use "types.use"
+use "introspect.use"
+use "sleq.use"
+use "die.use"
+
+pkg std =
+ type valist
+
+ const vastart : (args : ...# -> valist)
+ const vatype : (ap : valist# -> byte[:])
+ const vabytes : (ap : valist# -> byte[:])
+ generic vanext : (ap : valist# -> @a)
+;;
+
+type valist = struct
+ args : byte#
+ tc : typecursor
+;;
+
+/*
+ * a valist is really just a pointer to the varargs.
+ * we assume that these sit on the stack nicely,
+ * and don't need special handling to get to.
+ *
+ * This will be a problem when we switch to a
+ * register based convention. We might want to
+ * force varargs onto the stack regardless.
+ */
+const vastart = {args
+ var tc, a, ip
+
+ /*
+ pull out the args. These are on the stacks like so:
+
+ [ required ]
+ [ args ]
+ ---variadic---
+ [ typeinfo ] --> type description
+ ------------
+ [ variadic ]
+ [ args ]
+ [ here ]
+
+ &args points to the typeinfo, &args + sizeof(void#)
+ points to the rest argument.
+ */
+
+ tc = typeenc(args)
+ ip = (args castto(intptr)) + sizeof(byte#)
+ a = ip castto(byte#)
+ -> [.args = a, .tc = tc]
+}
+
+const vatype = {ap
+ -> tcpeek(&ap.tc)
+}
+
+const vabytes = {ap
+ var sl
+ var ti, align, sz
+ var p
+
+ ti = typeinfo(tcpeek(&ap.tc))
+
+ /* apply the alignment to the arg pointer */
+ align = ti.align castto(intptr)
+ p = ap.args castto(intptr)
+ p = (p + align - 1) & ~(align - 1)
+ ap.args = p castto(byte#)
+
+ sl = ap.args[:ti.size]
+ tcnext(&ap.tc)
+
+ sz = ti.size castto(intptr)
+ ap.args = ((p castto(intptr)) + sz) castto(byte#)
+
+ -> sl
+}
+
+const inspectarg = {x
+}
+
+generic vanext = {ap -> @a
+ var v : @a
+ var ti
+ var align
+ var p
+
+ ti = typeinfo(tcpeek(&ap.tc))
+
+ /* apply the alignment to the arg pointer */
+ align = ti.align castto(intptr)
+ inspectarg(align)
+ p = ap.args castto(intptr)
+ p = (p + align - 1) & ~(align - 1)
+ ap.args = p castto(byte#)
+
+ v = (ap.args castto(@a#))#
+ /* TODO: check for type mismatch */
+ tcnext(&ap.tc)
+
+ /* only move on after we read through the value */
+ ap.args = ((p castto(intptr)) + sizeof(@a)) castto(byte#)
+ -> v
+}
diff --git a/lib/std/wait+plan9.myr b/lib/std/wait+plan9.myr
new file mode 100644
index 0000000..efe1a1c
--- /dev/null
+++ b/lib/std/wait+plan9.myr
@@ -0,0 +1,94 @@
+use sys
+
+use "alloc.use"
+use "chartype.use"
+use "die.use"
+use "extremum.use"
+use "hashfuncs.use"
+use "hasprefix.use"
+use "htab.use"
+use "intparse.use"
+use "option.use"
+use "strsplit.use"
+use "syswrap.use"
+use "utf.use"
+
+pkg std =
+ type waitstatus = union
+ `Wsuccess
+ `Wfailure
+ `Wsignalled
+ `Waiterror
+ ;;
+
+ const wait : (pid : pid -> waitstatus)
+;;
+
+var statusinit : bool = false
+var statusmap : htab(pid, waitstatus)#
+
+const wait = {pid
+ var buf : byte[512]
+ var xpid, status
+ var n
+
+ if !statusinit
+ statusmap = mkht(pidhash, pideq)
+ statusinit = true
+ ;;
+
+ match htget(statusmap, pid)
+ | `Some st:
+ htdel(statusmap, pid)
+ -> st
+ | `None: /* nothing */
+ ;;
+
+ while true
+ n = sys.await(buf[:])
+ if n < 0
+ -> `Waiterror
+ ;;
+
+ (status, xpid) = parsestatus(buf[:n])
+ if xpid == pid
+ -> status
+ else
+ htput(statusmap, pid, status)
+ ;;
+ ;;
+ /* impossible */
+ -> `Waiterror
+}
+
+const parsestatus = {status -> (waitstatus, pid)
+ var st : waitstatus, xpid, sp
+
+ sp = strsplit(status, " ")
+ if sp.len == 0
+ slfree(sp)
+ -> (`Wfailure, -1)
+ ;;
+
+ match intparse(sp[0])
+ | `Some pid:
+ xpid = pid
+ if sp.len == 4 || (sp.len == 5 && sp[4].len > 0) /* we exited with nil */
+ st = `Wsuccess
+ elif sp.len == 5 /* we have a status */
+ st = `Wfailure
+ else /* we have a malformed await message */
+ st = `Waiterror
+ ;;
+ | `None:
+ xpid = -1
+ st = `Waiterror
+ ;;
+
+ slfree(sp)
+ -> (st, xpid)
+
+}
+
+const pidhash = {x; -> inthash(x castto(int32))}
+const pideq = {a, b; -> a == b} \ No newline at end of file
diff --git a/lib/std/wait+posixy.myr b/lib/std/wait+posixy.myr
new file mode 100644
index 0000000..a690feb
--- /dev/null
+++ b/lib/std/wait+posixy.myr
@@ -0,0 +1,41 @@
+use sys
+
+use "die.use"
+use "syswrap.use"
+
+pkg std =
+ type waitstatus = union
+ `Wsuccess
+ `Wfailure
+ `Wsignalled
+ `Waiterror
+ ;;
+
+ const wait : (pid : pid -> waitstatus)
+;;
+
+const wait = {pid
+ var st
+
+:again
+ if sys.waitpid(pid castto(sys.pid), &st, 0) > 0
+ match sys.waitstatus(st)
+ /*
+ when a process stops, eg, if paused by a debugger,
+ wait() will return. This API is for waiting until
+ a process exits. Loop instead.
+ */
+ | `sys.Waitstop sig: goto again
+ | `sys.Waitfail fail: -> `Waiterror
+ | `sys.Waitsig sig: -> `Wsignalled
+ | `sys.Waitexit status:
+ if status == 0
+ -> `Wsuccess
+ else
+ -> `Wfailure
+ ;;
+ ;;
+ ;;
+ -> `Waiterror
+}
+