sbcl: the whole library of volume 2 Heap, Arena, Buf, Fmt, List, Map, Sort, Math, Files, Opt, Thread, Bytes and the rest of Sys: directories, the terminal, polling, programs, signals and sockets. Fixes: volatile scalars assign, FENCE alone, ^T(x) takes selectors, code bodies only after a signature.
| @@ -0,0 +1,54 @@ | ||
| 1 | +(* An allocator that hands out memory from one block: its free does | |
| 2 | + nothing, and reset forgets everything at once. *) | |
| 3 | +module Arena; | |
| 4 | +import Mem; | |
| 5 | + | |
| 6 | +type Arena* = record | |
| 7 | + base*: Mem.Allocator; (* first, so that ^Arena(a) finds the rest *) | |
| 8 | + mem: ^U8; | |
| 9 | + size, used: INT; | |
| 10 | +end; | |
| 11 | + | |
| 12 | +proc alloc(a: ^Mem.Allocator; size, align: INT): ^U8; | |
| 13 | +var ar: ^Arena; start: INT; | |
| 14 | +begin | |
| 15 | + ar := ^Arena(a); | |
| 16 | + start := (ar^.used + align - 1) div align * align; | |
| 17 | + if start + size > ar^.size then return nil; end; | |
| 18 | + ar^.used := start + size; | |
| 19 | + return ar^.mem + start; | |
| 20 | +end; | |
| 21 | + | |
| 22 | +proc free(a: ^Mem.Allocator; p: ^U8); | |
| 23 | +begin | |
| 24 | +end; | |
| 25 | + | |
| 26 | +(* an arena of size bytes; false if the system has none *) | |
| 27 | +proc init*(ar: ^Arena; size: INT): BOOL; | |
| 28 | +begin | |
| 29 | + ar^.base.alloc := alloc; | |
| 30 | + ar^.base.free := free; | |
| 31 | + ar^.mem := Mem.pages(size); | |
| 32 | + ar^.size := size; | |
| 33 | + ar^.used := 0; | |
| 34 | + return ar^.mem # nil; | |
| 35 | +end; | |
| 36 | + | |
| 37 | +(* every allocation forgotten; the memory used again, zeroed *) | |
| 38 | +proc reset*(ar: ^Arena); | |
| 39 | +var i: INT; | |
| 40 | +begin | |
| 41 | + for i := 0 to ar^.used - 1 do ar^.mem[i] := 0; end; | |
| 42 | + ar^.used := 0; | |
| 43 | +end; | |
| 44 | + | |
| 45 | +(* its memory given back *) | |
| 46 | +proc done*(ar: ^Arena); | |
| 47 | +begin | |
| 48 | + if ar^.mem # nil then Mem.unmap(ar^.mem, ar^.size); end; | |
| 49 | + ar^.mem := nil; | |
| 50 | + ar^.size := 0; | |
| 51 | + ar^.used := 0; | |
| 52 | +end; | |
| 53 | + | |
| 54 | +end Arena. |
| @@ -0,0 +1,75 @@ | ||
| 1 | +(* A growable byte buffer, its memory from the allocator init gives it. *) | |
| 2 | +module Buf; | |
| 3 | +import Mem; | |
| 4 | + | |
| 5 | +type Buf* = record | |
| 6 | + len*: INT; | |
| 7 | + data: ^U8; | |
| 8 | + cap: INT; | |
| 9 | + a: ^Mem.Allocator; | |
| 10 | +end; | |
| 11 | + | |
| 12 | +proc init*(b: ^Buf; a: ^Mem.Allocator); | |
| 13 | +begin | |
| 14 | + b^.len := 0; | |
| 15 | + b^.data := nil; | |
| 16 | + b^.cap := 0; | |
| 17 | + b^.a := a; | |
| 18 | +end; | |
| 19 | + | |
| 20 | +(* room for n more bytes; false if the allocator has none *) | |
| 21 | +proc room*(b: ^Buf; n: INT): BOOL; | |
| 22 | +var cap: INT; p: ^U8; | |
| 23 | +begin | |
| 24 | + if b^.len + n <= b^.cap then return true; end; | |
| 25 | + cap := b^.cap * 2; | |
| 26 | + if cap < 64 then cap := 64; end; | |
| 27 | + while cap < b^.len + n do cap := cap * 2; end; | |
| 28 | + p := Mem.alloc(b^.a, cap, 1); | |
| 29 | + if p = nil then return false; end; | |
| 30 | + if b^.len > 0 then COPY(p[0..b^.len], b^.data[0..b^.len]); end; | |
| 31 | + if b^.data # nil then Mem.free(b^.a, b^.data); end; | |
| 32 | + b^.data := p; | |
| 33 | + b^.cap := cap; | |
| 34 | + return true; | |
| 35 | +end; | |
| 36 | + | |
| 37 | +proc add*(b: ^Buf; c: U8); | |
| 38 | +begin | |
| 39 | + if room(b, 1) then | |
| 40 | + b^.data[b^.len] := c; | |
| 41 | + INC(b^.len); | |
| 42 | + end; | |
| 43 | +end; | |
| 44 | + | |
| 45 | +proc str*(b: ^Buf; s: []U8); | |
| 46 | +begin | |
| 47 | + if room(b, LEN(s)) then | |
| 48 | + COPY(b^.data[b^.len..b^.len + LEN(s)], s); | |
| 49 | + b^.len := b^.len + LEN(s); | |
| 50 | + end; | |
| 51 | +end; | |
| 52 | + | |
| 53 | +(* what it holds, until it next grows *) | |
| 54 | +proc bytes*(b: ^Buf): []U8; | |
| 55 | +begin | |
| 56 | + if b^.data = nil then return ""; end; | |
| 57 | + return b^.data[0..b^.len]; | |
| 58 | +end; | |
| 59 | + | |
| 60 | +(* emptied, its memory kept *) | |
| 61 | +proc reset*(b: ^Buf); | |
| 62 | +begin | |
| 63 | + b^.len := 0; | |
| 64 | +end; | |
| 65 | + | |
| 66 | +(* its memory given back *) | |
| 67 | +proc done*(b: ^Buf); | |
| 68 | +begin | |
| 69 | + if b^.data # nil then Mem.free(b^.a, b^.data); end; | |
| 70 | + b^.data := nil; | |
| 71 | + b^.len := 0; | |
| 72 | + b^.cap := 0; | |
| 73 | +end; | |
| 74 | + | |
| 75 | +end Buf. |
| @@ -0,0 +1,44 @@ | ||
| 1 | +(* Searches of bytes in a []U8. *) | |
| 2 | +module Bytes; | |
| 3 | + | |
| 4 | +(* where b first is in s, or -1 *) | |
| 5 | +proc index*(s: []U8; b: U8): INT; | |
| 6 | +var i: INT; | |
| 7 | +begin | |
| 8 | + for i := 0 to LEN(s) - 1 do | |
| 9 | + if s[i] = b then return i; end; | |
| 10 | + end; | |
| 11 | + return -1; | |
| 12 | +end; | |
| 13 | + | |
| 14 | +(* where b last is in s, or -1 *) | |
| 15 | +proc last*(s: []U8; b: U8): INT; | |
| 16 | +var i: INT; | |
| 17 | +begin | |
| 18 | + for i := LEN(s) - 1 to 0 by -1 do | |
| 19 | + if s[i] = b then return i; end; | |
| 20 | + end; | |
| 21 | + return -1; | |
| 22 | +end; | |
| 23 | + | |
| 24 | +(* how many times b is in s *) | |
| 25 | +proc count*(s: []U8; b: U8): INT; | |
| 26 | +var i, n: INT; | |
| 27 | +begin | |
| 28 | + for i := 0 to LEN(s) - 1 do | |
| 29 | + if s[i] = b then INC(n); end; | |
| 30 | + end; | |
| 31 | + return n; | |
| 32 | +end; | |
| 33 | + | |
| 34 | +(* where sub first starts in s, or -1 *) | |
| 35 | +proc find*(s, sub: []U8): INT; | |
| 36 | +var i: INT; | |
| 37 | +begin | |
| 38 | + for i := 0 to LEN(s) - LEN(sub) do | |
| 39 | + if s[i..i + LEN(sub)] = sub then return i; end; | |
| 40 | + end; | |
| 41 | + return -1; | |
| 42 | +end; | |
| 43 | + | |
| 44 | +end Bytes. |
| @@ -0,0 +1,46 @@ | ||
| 1 | +(* An argument as a string, and a whole file read or written. *) | |
| 2 | +module Files; | |
| 3 | +import Sys, Buf; | |
| 4 | + | |
| 5 | +(* argument i; 0 is the program's name *) | |
| 6 | +proc arg*(i: INT): []U8; | |
| 7 | +var s: ^U8; n: INT; | |
| 8 | +begin | |
| 9 | + if i < 0 or i >= ARGC() then return ""; end; | |
| 10 | + s := ARGV()[i]; | |
| 11 | + while s[n] # 0 do INC(n); end; | |
| 12 | + return s[0..n]; | |
| 13 | +end; | |
| 14 | + | |
| 15 | +(* the whole file added to b *) | |
| 16 | +proc read*(path: []U8; b: ^Buf.Buf): BOOL; | |
| 17 | +var fd, n: INT; chunk: [4096]U8; | |
| 18 | +begin | |
| 19 | + fd := Sys.openRead(path); | |
| 20 | + if fd < 0 then return false; end; | |
| 21 | + defer Sys.close(fd); | |
| 22 | + n := Sys.read(fd, chunk); | |
| 23 | + while n > 0 do | |
| 24 | + if not Buf.room(b, n) then return false; end; | |
| 25 | + Buf.str(b, chunk[0..n]); | |
| 26 | + n := Sys.read(fd, chunk); | |
| 27 | + end; | |
| 28 | + return n = 0; | |
| 29 | +end; | |
| 30 | + | |
| 31 | +(* the file made to hold data *) | |
| 32 | +proc write*(path, data: []U8): BOOL; | |
| 33 | +var fd, n, done: INT; | |
| 34 | +begin | |
| 35 | + fd := Sys.create(path); | |
| 36 | + if fd < 0 then return false; end; | |
| 37 | + defer Sys.close(fd); | |
| 38 | + while done < LEN(data) do | |
| 39 | + n := Sys.write(fd, data[done..LEN(data)]); | |
| 40 | + if n <= 0 then return false; end; | |
| 41 | + done := done + n; | |
| 42 | + end; | |
| 43 | + return true; | |
| 44 | +end; | |
| 45 | + | |
| 46 | +end Files. |
| @@ -0,0 +1,146 @@ | ||
| 1 | +(* Formatted output: Fmt.print("{} of {}\n", [Fmt.int(n), Fmt.real(x, 2)]), | |
| 2 | + or into a Buf with Fmt.write. Each {} takes the next argument; {{ and | |
| 3 | + }} stand for { and }. *) | |
| 4 | +module Fmt; | |
| 5 | +import Out, Buf; | |
| 6 | + | |
| 7 | +type Arg* = variant | |
| 8 | + int(i: I64); | |
| 9 | + uint(u: U64); | |
| 10 | + hex(h: U64); | |
| 11 | + str(s: []U8); | |
| 12 | + char(c: U8); | |
| 13 | + bool(b: BOOL); | |
| 14 | + real(r: F64; places: INT); | |
| 15 | +end; | |
| 16 | + | |
| 17 | +const digits = "0123456789ABCDEF"; | |
| 18 | + | |
| 19 | +(* Text is made in a Piece before it goes out, a piece at a time. *) | |
| 20 | +type Piece = record bytes: [64]U8; len: INT; end; | |
| 21 | + | |
| 22 | +proc add(p: ^Piece; c: U8); | |
| 23 | +begin | |
| 24 | + p^.bytes[p^.len] := c; | |
| 25 | + INC(p^.len); | |
| 26 | +end; | |
| 27 | + | |
| 28 | +(* u in base, at least min digits *) | |
| 29 | +proc unsigned(p: ^Piece; u: U64; base: U64; min: INT); | |
| 30 | +var d: [20]U8; n: INT; | |
| 31 | +begin | |
| 32 | + repeat | |
| 33 | + d[n] := digits[INT(u mod base)]; | |
| 34 | + u := u div base; | |
| 35 | + INC(n); | |
| 36 | + until u = 0; | |
| 37 | + while n < min do d[n] := '0'; INC(n); end; | |
| 38 | + while n > 0 do DEC(n); add(p, d[n]); end; | |
| 39 | +end; | |
| 40 | + | |
| 41 | +proc signed(p: ^Piece; i: I64); | |
| 42 | +begin | |
| 43 | + if i < 0 then add(p, '-'); unsigned(p, 0 - U64(i), 10, 1); | |
| 44 | + else unsigned(p, U64(i), 10, 1); | |
| 45 | + end; | |
| 46 | +end; | |
| 47 | + | |
| 48 | +(* x, at least 0 and below 1e15, with places digits after the point *) | |
| 49 | +proc fixed(p: ^Piece; x: F64; places: INT); | |
| 50 | +var whole, scale: F64; ip, frac, one: U64; i: INT; | |
| 51 | +begin | |
| 52 | + one := 1; | |
| 53 | + for i := 1 to places do one := one * 10; end; | |
| 54 | + scale := F64(one); | |
| 55 | + ip := U64(x); | |
| 56 | + whole := F64(ip); | |
| 57 | + frac := U64((x - whole) * scale + 0.5); | |
| 58 | + if frac >= one then INC(ip); frac := frac - one; end; | |
| 59 | + unsigned(p, ip, 10, 1); | |
| 60 | + if places > 0 then | |
| 61 | + add(p, '.'); | |
| 62 | + unsigned(p, frac, 10, places); | |
| 63 | + end; | |
| 64 | +end; | |
| 65 | + | |
| 66 | +proc writeReal(p: ^Piece; x: F64; places: INT); | |
| 67 | +var e, i: INT; m: F64; q: Piece; | |
| 68 | +begin | |
| 69 | + if places < 0 then places := 0; end; | |
| 70 | + if places > 9 then places := 9; end; | |
| 71 | + if x # x then add(p, 'n'); add(p, 'a'); add(p, 'n'); return; end; | |
| 72 | + if x < 0.0 or (x = 0.0 and 1.0 / x < 0.0) then add(p, '-'); x := -x; end; | |
| 73 | + if x > 1.7976931348623157E308 then add(p, 'i'); add(p, 'n'); add(p, 'f'); return; end; | |
| 74 | + if x < 1.0E15 then fixed(p, x, places); return; end; | |
| 75 | + (* m times ten to the e, m from 1 to 10 *) | |
| 76 | + m := x; | |
| 77 | + while m >= 10.0 do m := m / 10.0; INC(e); end; | |
| 78 | + fixed(ADR(q), m, places); | |
| 79 | + if q.bytes[0] = '1' and q.bytes[1] = '0' then | |
| 80 | + (* m rounded up to 10 *) | |
| 81 | + q.len := 0; | |
| 82 | + fixed(ADR(q), m / 10.0, places); | |
| 83 | + INC(e); | |
| 84 | + end; | |
| 85 | + for i := 0 to q.len - 1 do add(p, q.bytes[i]); end; | |
| 86 | + add(p, 'e'); | |
| 87 | + signed(p, I64(e)); | |
| 88 | +end; | |
| 89 | + | |
| 90 | +(* The text of each piece of f, and of each argument, given to put. *) | |
| 91 | +proc format(f: []U8; args: []Arg; out: proc(s: []U8; b: ^Buf.Buf); b: ^Buf.Buf); | |
| 92 | +var i, start, next: INT; p: Piece; | |
| 93 | +begin | |
| 94 | + while i < LEN(f) do | |
| 95 | + if (f[i] = '{' or f[i] = '}') and i + 1 < LEN(f) and f[i + 1] = f[i] then | |
| 96 | + out(f[start..i + 1], b); | |
| 97 | + i := i + 2; | |
| 98 | + start := i; | |
| 99 | + elsif f[i] = '{' and i + 1 < LEN(f) and f[i + 1] = '}' then | |
| 100 | + out(f[start..i], b); | |
| 101 | + if next < LEN(args) then | |
| 102 | + p.len := 0; | |
| 103 | + match args[next] of | |
| 104 | + | int(n): signed(ADR(p), n); | |
| 105 | + | uint(u): unsigned(ADR(p), u, 10, 1); | |
| 106 | + | hex(h): unsigned(ADR(p), h, 16, 1); | |
| 107 | + | str(s): out(s, b); | |
| 108 | + | char(c): add(ADR(p), c); | |
| 109 | + | bool(v): if v then out("true", b); else out("false", b); end; | |
| 110 | + | real(x, places): writeReal(ADR(p), x, places); | |
| 111 | + end; | |
| 112 | + out(p.bytes[0..p.len], b); | |
| 113 | + INC(next); | |
| 114 | + end; | |
| 115 | + i := i + 2; | |
| 116 | + start := i; | |
| 117 | + else | |
| 118 | + INC(i); | |
| 119 | + end; | |
| 120 | + end; | |
| 121 | + out(f[start..LEN(f)], b); | |
| 122 | +end; | |
| 123 | + | |
| 124 | +proc toOut(s: []U8; b: ^Buf.Buf); | |
| 125 | +begin | |
| 126 | + Out.str(s); | |
| 127 | +end; | |
| 128 | + | |
| 129 | +proc toBuf(s: []U8; b: ^Buf.Buf); | |
| 130 | +begin | |
| 131 | + Buf.str(b, s); | |
| 132 | +end; | |
| 133 | + | |
| 134 | +(* to standard output *) | |
| 135 | +proc print*(f: []U8; args: []Arg); | |
| 136 | +begin | |
| 137 | + format(f, args, toOut, nil); | |
| 138 | +end; | |
| 139 | + | |
| 140 | +(* added to b *) | |
| 141 | +proc write*(b: ^Buf.Buf; f: []U8; args: []Arg); | |
| 142 | +begin | |
| 143 | + format(f, args, toBuf, b); | |
| 144 | +end; | |
| 145 | + | |
| 146 | +end Fmt. |
| @@ -0,0 +1,117 @@ | ||
| 1 | +(* A general allocator, Heap.heap, safe to call from several threads. | |
| 2 | + | |
| 3 | + A small block comes from one of eight free lists, by its size: 16 to | |
| 4 | + 2048 bytes, each twice the one before, a header of 16 bytes included. | |
| 5 | + The lists are filled from chunks of pages. A big block, or one aligned | |
| 6 | + to more than 16 bytes, has pages of its own. The header says which | |
| 7 | + list a block belongs to, or how many bytes of pages it has, and | |
| 8 | + whether it is given out: freeing a block twice, or one the heap never | |
| 9 | + gave, stops the program. *) | |
| 10 | +module Heap; | |
| 11 | +import Mem, Lock, Rt; | |
| 12 | + | |
| 13 | +const | |
| 14 | + classes = 8; (* the free lists: blocks of 16 shl i bytes *) | |
| 15 | + chunkSize = 65536; (* the pages a list is filled from at once *) | |
| 16 | + given = 0A110CA7EDH; (* a header's mark for a block given out *) | |
| 17 | + freed = 0F7EEDH; (* and for one on a free list *) | |
| 18 | + | |
| 19 | +type | |
| 20 | + Header = record | |
| 21 | + size: INT; (* the list, from 0; or minus the bytes of the block's own pages *) | |
| 22 | + mark: INT; (* given or freed *) | |
| 23 | + end; | |
| 24 | + Free = record next: ^Free; end; (* a block on a free list, past its header *) | |
| 25 | + | |
| 26 | +var | |
| 27 | + allocator: Mem.Allocator; | |
| 28 | + heap*: ^Mem.Allocator; | |
| 29 | + lists: [classes]^Free; | |
| 30 | + lock: Lock.Lock; | |
| 31 | + | |
| 32 | +(* A block with pages of its own: the header sits just before the | |
| 33 | + aligned start. *) | |
| 34 | +proc big(size, align: INT): ^U8; | |
| 35 | +var bytes, lead: INT; p: ^U8; h: ^Header; base: ^^U8; | |
| 36 | +begin | |
| 37 | + lead := 16; | |
| 38 | + if align > 16 then lead := align; end; | |
| 39 | + bytes := (size + lead + Mem.Page - 1) div Mem.Page * Mem.Page; | |
| 40 | + p := Mem.pages(bytes); | |
| 41 | + if p = nil then return nil; end; | |
| 42 | + p := p + lead; | |
| 43 | + (* past an alignment's lead, the pages' start, for free *) | |
| 44 | + if lead > 16 then base := ^^U8(p - 24); base^ := p - lead; end; | |
| 45 | + h := ^Header(p - 16); | |
| 46 | + h^.size := -bytes; | |
| 47 | + h^.mark := given; | |
| 48 | + return p; | |
| 49 | +end; | |
| 50 | + | |
| 51 | +(* List c filled from a new chunk; false if the system has no room. *) | |
| 52 | +proc fill(c: INT): BOOL; | |
| 53 | +var chunk: ^U8; size, i: INT; f: ^Free; h: ^Header; | |
| 54 | +begin | |
| 55 | + chunk := Mem.pages(chunkSize); | |
| 56 | + if chunk = nil then return false; end; | |
| 57 | + size := 16 shl c; | |
| 58 | + for i := 0 to chunkSize div size - 1 do | |
| 59 | + h := ^Header(chunk + i * size); | |
| 60 | + h^.size := c; | |
| 61 | + h^.mark := freed; | |
| 62 | + f := ^Free(chunk + i * size + 16); | |
| 63 | + f^.next := lists[c]; | |
| 64 | + lists[c] := f; | |
| 65 | + end; | |
| 66 | + return true; | |
| 67 | +end; | |
| 68 | + | |
| 69 | +proc alloc(a: ^Mem.Allocator; size, align: INT): ^U8; | |
| 70 | +var c: INT; f: ^Free; h: ^Header; | |
| 71 | +begin | |
| 72 | + (* a size the memory can't hold, or an alignment that isn't a power | |
| 73 | + of 2 up to a page, is refused *) | |
| 74 | + if size < 0 or size > 1 shl 46 or align <= 0 or align > Mem.Page or align & (align - 1) # 0 then | |
| 75 | + return nil; | |
| 76 | + end; | |
| 77 | + if size + 16 > 16 shl (classes - 1) or align > 16 then return big(size, align); end; | |
| 78 | + c := 0; | |
| 79 | + while 16 shl c < size + 16 do INC(c); end; | |
| 80 | + Lock.take(ADR(lock)); | |
| 81 | + if lists[c] = nil and not fill(c) then Lock.give(ADR(lock)); return nil; end; | |
| 82 | + f := lists[c]; | |
| 83 | + lists[c] := f^.next; | |
| 84 | + h := ^Header(^U8(f) - 16); | |
| 85 | + h^.mark := given; | |
| 86 | + Lock.give(ADR(lock)); | |
| 87 | + return ^U8(f); | |
| 88 | +end; | |
| 89 | + | |
| 90 | +proc free(a: ^Mem.Allocator; p: ^U8); | |
| 91 | +var h: ^Header; f: ^Free; | |
| 92 | +begin | |
| 93 | + if p = nil then return; end; | |
| 94 | + h := ^Header(p - 16); | |
| 95 | + if h^.mark # given then Rt.stop("Heap: a block freed twice, or one the heap never gave"); end; | |
| 96 | + if h^.size < 0 then | |
| 97 | + h^.mark := 0; | |
| 98 | + if INT(p) mod Mem.Page = 16 then | |
| 99 | + Mem.unmap(p - 16, -h^.size); | |
| 100 | + else | |
| 101 | + Mem.unmap(^^U8(p - 24)^, -h^.size); | |
| 102 | + end; | |
| 103 | + return; | |
| 104 | + end; | |
| 105 | + Lock.take(ADR(lock)); | |
| 106 | + h^.mark := freed; | |
| 107 | + f := ^Free(p); | |
| 108 | + f^.next := lists[h^.size]; | |
| 109 | + lists[h^.size] := f; | |
| 110 | + Lock.give(ADR(lock)); | |
| 111 | +end; | |
| 112 | + | |
| 113 | +begin | |
| 114 | + allocator.alloc := alloc; | |
| 115 | + allocator.free := free; | |
| 116 | + heap := ADR(allocator); | |
| 117 | +end Heap. |
| @@ -0,0 +1,68 @@ | ||
| 1 | +(* A generic singly linked list, its nodes from an allocator. *) | |
| 2 | +module List(T); | |
| 3 | +import Mem; | |
| 4 | + | |
| 5 | +type | |
| 6 | + Node* = record next*: ^Node; val*: T; end; | |
| 7 | + List* = record head*: ^Node; len*: INT; a: ^Mem.Allocator; end; | |
| 8 | + | |
| 9 | +proc init*(l: ^List; a: ^Mem.Allocator); | |
| 10 | +begin | |
| 11 | + l^.head := nil; | |
| 12 | + l^.len := 0; | |
| 13 | + l^.a := a; | |
| 14 | +end; | |
| 15 | + | |
| 16 | +(* v at the front; false if the allocator has no room *) | |
| 17 | +proc push*(l: ^List; v: T): BOOL; | |
| 18 | +var n: ^Node; | |
| 19 | +begin | |
| 20 | + NEW(n, l^.a); | |
| 21 | + if n = nil then return false; end; | |
| 22 | + n^.val := v; | |
| 23 | + n^.next := l^.head; | |
| 24 | + l^.head := n; | |
| 25 | + INC(l^.len); | |
| 26 | + return true; | |
| 27 | +end; | |
| 28 | + | |
| 29 | +(* the front value, taken off; false if the list is empty *) | |
| 30 | +proc pop*(l: ^List): (T, BOOL); | |
| 31 | +var n: ^Node; v: T; | |
| 32 | +begin | |
| 33 | + n := l^.head; | |
| 34 | + if n = nil then return v, false; end; | |
| 35 | + v := n^.val; | |
| 36 | + l^.head := n^.next; | |
| 37 | + DEC(l^.len); | |
| 38 | + Mem.free(l^.a, ^U8(n)); | |
| 39 | + return v, true; | |
| 40 | +end; | |
| 41 | + | |
| 42 | +proc reverse*(l: ^List); | |
| 43 | +var prev, n, next: ^Node; | |
| 44 | +begin | |
| 45 | + n := l^.head; | |
| 46 | + while n # nil do | |
| 47 | + next := n^.next; | |
| 48 | + n^.next := prev; | |
| 49 | + prev := n; | |
| 50 | + n := next; | |
| 51 | + end; | |
| 52 | + l^.head := prev; | |
| 53 | +end; | |
| 54 | + | |
| 55 | +proc clear*(l: ^List); | |
| 56 | +var n, next: ^Node; | |
| 57 | +begin | |
| 58 | + n := l^.head; | |
| 59 | + while n # nil do | |
| 60 | + next := n^.next; | |
| 61 | + Mem.free(l^.a, ^U8(n)); | |
| 62 | + n := next; | |
| 63 | + end; | |
| 64 | + l^.head := nil; | |
| 65 | + l^.len := 0; | |
| 66 | +end; | |
| 67 | + | |
| 68 | +end List. |
| @@ -0,0 +1,124 @@ | ||
| 1 | +(* A generic hash map, open addressing with linear probing; it hashes and | |
| 2 | + compares its keys with the procedures it is given. *) | |
| 3 | +module Map(K, V; hash: proc(k: K): U64; eq: proc(a, b: K): BOOL); | |
| 4 | +import Mem; | |
| 5 | + | |
| 6 | +type | |
| 7 | + Entry = record key: K; val: V; used: BOOL; end; | |
| 8 | + Map* = record | |
| 9 | + len*: INT; | |
| 10 | + slots: ^Entry; (* cap of them; cap is 0 or a power of 2 *) | |
| 11 | + cap: INT; | |
| 12 | + a: ^Mem.Allocator; | |
| 13 | + end; | |
| 14 | + | |
| 15 | +proc init*(m: ^Map; a: ^Mem.Allocator); | |
| 16 | +begin | |
| 17 | + m^.len := 0; | |
| 18 | + m^.slots := nil; | |
| 19 | + m^.cap := 0; | |
| 20 | + m^.a := a; | |
| 21 | +end; | |
| 22 | + | |
| 23 | +(* the slot of k: where it is, or the free slot it would go in *) | |
| 24 | +proc slot(m: ^Map; k: K): INT; | |
| 25 | +var i: INT; | |
| 26 | +begin | |
| 27 | + i := INT(hash(k) & U64(m^.cap - 1)); | |
| 28 | + while m^.slots[i].used and not eq(m^.slots[i].key, k) do | |
| 29 | + i := (i + 1) & (m^.cap - 1); | |
| 30 | + end; | |
| 31 | + return i; | |
| 32 | +end; | |
| 33 | + | |
| 34 | +(* twice the slots, every entry moved; false if there is no room *) | |
| 35 | +proc grow(m: ^Map): BOOL; | |
| 36 | +var old: ^Entry; n, i, j, cap: INT; p: ^U8; | |
| 37 | +begin | |
| 38 | + cap := m^.cap * 2; | |
| 39 | + if cap = 0 then cap := 8; end; | |
| 40 | + p := Mem.alloc(m^.a, cap * SIZE(Entry), 8); | |
| 41 | + if p = nil then return false; end; | |
| 42 | + for i := 0 to cap * SIZE(Entry) - 1 do p[i] := 0; end; | |
| 43 | + old := m^.slots; | |
| 44 | + n := m^.cap; | |
| 45 | + m^.slots := ^Entry(p); | |
| 46 | + m^.cap := cap; | |
| 47 | + for i := 0 to n - 1 do | |
| 48 | + if old[i].used then | |
| 49 | + j := slot(m, old[i].key); | |
| 50 | + m^.slots[j] := old[i]; | |
| 51 | + end; | |
| 52 | + end; | |
| 53 | + if old # nil then Mem.free(m^.a, ^U8(old)); end; | |
| 54 | + return true; | |
| 55 | +end; | |
| 56 | + | |
| 57 | +(* k now maps to v; false if there was no room *) | |
| 58 | +proc put*(m: ^Map; k: K; v: V): BOOL; | |
| 59 | +var i: INT; | |
| 60 | +begin | |
| 61 | + if (m^.len + 1) * 4 > m^.cap * 3 and not grow(m) then return false; end; | |
| 62 | + i := slot(m, k); | |
| 63 | + if not m^.slots[i].used then | |
| 64 | + m^.slots[i].used := true; | |
| 65 | + m^.slots[i].key := k; | |
| 66 | + INC(m^.len); | |
| 67 | + end; | |
| 68 | + m^.slots[i].val := v; | |
| 69 | + return true; | |
| 70 | +end; | |
| 71 | + | |
| 72 | +(* the slot k's hash points at *) | |
| 73 | +proc home(m: ^Map; k: K): INT; | |
| 74 | +begin | |
| 75 | + return INT(hash(k) & U64(m^.cap - 1)); | |
| 76 | +end; | |
| 77 | + | |
| 78 | +(* false if k wasn't there *) | |
| 79 | +proc remove*(m: ^Map; k: K): BOOL; | |
| 80 | +var i, j, h: INT; | |
| 81 | +begin | |
| 82 | + if m^.cap = 0 then return false; end; | |
| 83 | + i := slot(m, k); | |
| 84 | + if not m^.slots[i].used then return false; end; | |
| 85 | + m^.slots[i].used := false; | |
| 86 | + DEC(m^.len); | |
| 87 | + (* each entry after it in the run moves back into the hole, unless its | |
| 88 | + home is between the hole and where it is *) | |
| 89 | + j := (i + 1) & (m^.cap - 1); | |
| 90 | + while m^.slots[j].used do | |
| 91 | + h := home(m, m^.slots[j].key); | |
| 92 | + if (j > i and (h <= i or h > j)) or (j < i and h <= i and h > j) then | |
| 93 | + m^.slots[i] := m^.slots[j]; | |
| 94 | + m^.slots[j].used := false; | |
| 95 | + i := j; | |
| 96 | + end; | |
| 97 | + j := (j + 1) & (m^.cap - 1); | |
| 98 | + end; | |
| 99 | + return true; | |
| 100 | +end; | |
| 101 | + | |
| 102 | +(* what k maps to, and whether it maps to anything *) | |
| 103 | +proc get*(m: ^Map; k: K): (V, BOOL); | |
| 104 | +var i: INT; v: V; | |
| 105 | +begin | |
| 106 | + if m^.cap = 0 then return v, false; end; | |
| 107 | + i := slot(m, k); | |
| 108 | + if not m^.slots[i].used then return v, false; end; | |
| 109 | + return m^.slots[i].val, true; | |
| 110 | +end; | |
| 111 | + | |
| 112 | +(* the entry after i: from 0, the next entry and the number to pass for | |
| 113 | + the one after it; -1 when there are no more *) | |
| 114 | +proc next*(m: ^Map; i: INT): (INT, K, V); | |
| 115 | +var k: K; v: V; | |
| 116 | +begin | |
| 117 | + while i < m^.cap do | |
| 118 | + if m^.slots[i].used then return i + 1, m^.slots[i].key, m^.slots[i].val; end; | |
| 119 | + INC(i); | |
| 120 | + end; | |
| 121 | + return -1, k, v; | |
| 122 | +end; | |
| 123 | + | |
| 124 | +end Map. |
| @@ -0,0 +1,93 @@ | ||
| 1 | +(* Roots, powers, logarithms, rounding and trigonometry, for F64: C's | |
| 2 | + math.h, called through the run time. *) | |
| 3 | +module Math; | |
| 4 | + | |
| 5 | +const pi* = 3.141592653589793; | |
| 6 | + | |
| 7 | +proc sqrt*(x: F64): F64; | |
| 8 | +code | |
| 9 | + (c-call "sqrt" double-float double-float x); | |
| 10 | +end; | |
| 11 | + | |
| 12 | +(* x to the power y *) | |
| 13 | +proc pow*(x, y: F64): F64; | |
| 14 | +code | |
| 15 | + (c-call "pow" double-float double-float x double-float y); | |
| 16 | +end; | |
| 17 | + | |
| 18 | +proc floor*(x: F64): F64; | |
| 19 | +code | |
| 20 | + (c-call "floor" double-float double-float x); | |
| 21 | +end; | |
| 22 | + | |
| 23 | +proc ceil*(x: F64): F64; | |
| 24 | +code | |
| 25 | + (c-call "ceil" double-float double-float x); | |
| 26 | +end; | |
| 27 | + | |
| 28 | +(* to the nearest whole number; a half away from 0 *) | |
| 29 | +proc round*(x: F64): F64; | |
| 30 | +code | |
| 31 | + (c-call "round" double-float double-float x); | |
| 32 | +end; | |
| 33 | + | |
| 34 | +proc exp*(x: F64): F64; | |
| 35 | +code | |
| 36 | + (c-call "exp" double-float double-float x); | |
| 37 | +end; | |
| 38 | + | |
| 39 | +proc ln*(x: F64): F64; | |
| 40 | +code | |
| 41 | + (c-call "log" double-float double-float x); | |
| 42 | +end; | |
| 43 | + | |
| 44 | +proc log2*(x: F64): F64; | |
| 45 | +code | |
| 46 | + (c-call "log2" double-float double-float x); | |
| 47 | +end; | |
| 48 | + | |
| 49 | +proc log10*(x: F64): F64; | |
| 50 | +code | |
| 51 | + (c-call "log10" double-float double-float x); | |
| 52 | +end; | |
| 53 | + | |
| 54 | +proc sin*(x: F64): F64; | |
| 55 | +code | |
| 56 | + (c-call "sin" double-float double-float x); | |
| 57 | +end; | |
| 58 | + | |
| 59 | +proc cos*(x: F64): F64; | |
| 60 | +code | |
| 61 | + (c-call "cos" double-float double-float x); | |
| 62 | +end; | |
| 63 | + | |
| 64 | +proc tan*(x: F64): F64; | |
| 65 | +code | |
| 66 | + (c-call "tan" double-float double-float x); | |
| 67 | +end; | |
| 68 | + | |
| 69 | +(* from -pi/2 to pi/2 *) | |
| 70 | +proc asin*(x: F64): F64; | |
| 71 | +code | |
| 72 | + (c-call "asin" double-float double-float x); | |
| 73 | +end; | |
| 74 | + | |
| 75 | +(* from 0 to pi *) | |
| 76 | +proc acos*(x: F64): F64; | |
| 77 | +code | |
| 78 | + (c-call "acos" double-float double-float x); | |
| 79 | +end; | |
| 80 | + | |
| 81 | +(* from -pi/2 to pi/2 *) | |
| 82 | +proc atan*(x: F64): F64; | |
| 83 | +code | |
| 84 | + (c-call "atan" double-float double-float x); | |
| 85 | +end; | |
| 86 | + | |
| 87 | +(* the angle of (x, y) from the x axis, from -pi to pi *) | |
| 88 | +proc atan2*(y, x: F64): F64; | |
| 89 | +code | |
| 90 | + (c-call "atan2" double-float double-float y double-float x); | |
| 91 | +end; | |
| 92 | + | |
| 93 | +end Math. |
| @@ -0,0 +1,362 @@ | ||
| 1 | +(* A command line read through one table, by getopt's rules with GNU's | |
| 2 | + long options; the help and the mistakes are made from the table. | |
| 3 | + | |
| 4 | + The parser walks the arguments with arg, the next word, and pos, the | |
| 5 | + next letter in a group such as -lw (0 outside one). Each step is read | |
| 6 | + by step, which never ends the program: init and scope use it to look | |
| 7 | + ahead for -h and --help, and next to read the options one by one. *) | |
| 8 | +module Opt; | |
| 9 | +import Out, Sys, Rt, Str, Buf, Heap, Files; | |
| 10 | + | |
| 11 | +type | |
| 12 | + Row* = variant | |
| 13 | + usage(scope, line: []U8); (* a synopsis line, without "usage: " *) | |
| 14 | + sub(scope, name, help: []U8); (* a subcommand *) | |
| 15 | + option(scope: []U8; letter: U8; long, value, help: []U8); | |
| 16 | + end; | |
| 17 | + Parser* = record | |
| 18 | + letter*: U8; (* the option just read *) | |
| 19 | + value*: []U8; (* its value; "" for a flag *) | |
| 20 | + long*: BOOL; (* it was written by its long name *) | |
| 21 | + name: []U8; (* the program's *) | |
| 22 | + table: []Row; | |
| 23 | + scope: []U8; | |
| 24 | + arg, pos: INT; | |
| 25 | + ended: BOOL; (* the options have ended: what is left are operands *) | |
| 26 | + end; | |
| 27 | + Step = (anOption, theEnd, aHelp, aMistake); (* what step read *) | |
| 28 | + | |
| 29 | +var text: Buf.Buf; (* a mistake's sentence, as step makes it *) | |
| 30 | + | |
| 31 | +(* What an option row says, if row r is an option of scope s. *) | |
| 32 | +proc optionOf(r: Row; s: []U8): (BOOL, U8, []U8, []U8); | |
| 33 | +begin | |
| 34 | + match r of | |
| 35 | + | option(scope, letter, long, value, help): | |
| 36 | + if scope = s then return true, letter, long, value; end; | |
| 37 | + | else: | |
| 38 | + end; | |
| 39 | + return false, 0, "", ""; | |
| 40 | +end; | |
| 41 | + | |
| 42 | +proc say(s: []U8); | |
| 43 | +begin | |
| 44 | + Buf.str(ADR(text), s); | |
| 45 | +end; | |
| 46 | + | |
| 47 | +(* The option of p's scope with the letter c (or, if c is 0, the long | |
| 48 | + name n): its row, or -1. *) | |
| 49 | +proc find(p: ^Parser; c: U8; n: []U8): INT; | |
| 50 | +var i: INT; is: BOOL; letter: U8; long, value: []U8; | |
| 51 | +begin | |
| 52 | + for i := 0 to LEN(p^.table) - 1 do | |
| 53 | + is, letter, long, value := optionOf(p^.table[i], p^.scope); | |
| 54 | + if is and ((c # 0 and letter = c) or (c = 0 and LEN(n) > 0 and long = n)) then return i; end; | |
| 55 | + end; | |
| 56 | + return -1; | |
| 57 | +end; | |
| 58 | + | |
| 59 | +(* The next option of p, by getopt's rules. *) | |
| 60 | +proc step(p: ^Parser): Step; | |
| 61 | +var w, n: []U8; i, eq: INT; is: BOOL; letter: U8; long, value: []U8; | |
| 62 | +begin | |
| 63 | + Buf.reset(ADR(text)); | |
| 64 | + p^.value := ""; | |
| 65 | + p^.long := false; | |
| 66 | + if p^.ended or p^.arg >= ARGC() then return theEnd; end; | |
| 67 | + w := Files.arg(p^.arg); | |
| 68 | + if p^.pos = 0 then | |
| 69 | + if w = "--" then INC(p^.arg); p^.ended := true; return theEnd; end; | |
| 70 | + if LEN(w) < 2 or w[0] # '-' then return theEnd; end; | |
| 71 | + if w[1] = '-' then | |
| 72 | + (* --name, --name=value or --name value *) | |
| 73 | + INC(p^.arg); | |
| 74 | + eq := 2; | |
| 75 | + while eq < LEN(w) and w[eq] # '=' do INC(eq); end; | |
| 76 | + n := w[2..eq]; | |
| 77 | + if n = "help" then return aHelp; end; | |
| 78 | + i := find(p, 0, n); | |
| 79 | + if i < 0 then say("unknown option --"); say(n); return aMistake; end; | |
| 80 | + is, letter, long, value := optionOf(p^.table[i], p^.scope); | |
| 81 | + p^.letter := letter; | |
| 82 | + p^.long := true; | |
| 83 | + if LEN(value) = 0 then | |
| 84 | + if eq < LEN(w) then say("--"); say(n); say(" takes no value"); return aMistake; end; | |
| 85 | + elsif eq < LEN(w) then | |
| 86 | + p^.value := w[eq + 1..LEN(w)]; | |
| 87 | + elsif p^.arg < ARGC() then | |
| 88 | + p^.value := Files.arg(p^.arg); | |
| 89 | + INC(p^.arg); | |
| 90 | + else | |
| 91 | + say("--"); say(n); say(" needs a "); say(value); return aMistake; | |
| 92 | + end; | |
| 93 | + return anOption; | |
| 94 | + end; | |
| 95 | + p^.pos := 1; | |
| 96 | + end; | |
| 97 | + (* a letter of a group *) | |
| 98 | + letter := w[p^.pos]; | |
| 99 | + INC(p^.pos); | |
| 100 | + if p^.pos = LEN(w) then p^.pos := 0; INC(p^.arg); end; | |
| 101 | + if letter = 'h' then return aHelp; end; | |
| 102 | + i := find(p, letter, ""); | |
| 103 | + if i < 0 or letter < '!' then say("unknown option -"); Buf.add(ADR(text), letter); return aMistake; end; | |
| 104 | + is, letter, long, value := optionOf(p^.table[i], p^.scope); | |
| 105 | + p^.letter := letter; | |
| 106 | + if LEN(value) > 0 then | |
| 107 | + (* the rest of the word, or the whole next word *) | |
| 108 | + if p^.pos > 0 then | |
| 109 | + p^.value := w[p^.pos..LEN(w)]; | |
| 110 | + p^.pos := 0; | |
| 111 | + INC(p^.arg); | |
| 112 | + elsif p^.arg < ARGC() then | |
| 113 | + p^.value := Files.arg(p^.arg); | |
| 114 | + INC(p^.arg); | |
| 115 | + else | |
| 116 | + say("-"); Buf.add(ADR(text), letter); say(" needs a "); say(value); return aMistake; | |
| 117 | + end; | |
| 118 | + end; | |
| 119 | + return anOption; | |
| 120 | +end; | |
| 121 | + | |
| 122 | +(* The words of the scope's name, "remote.add" as "remote add". *) | |
| 123 | +proc scopeWords(p: ^Parser; b: ^Buf.Buf); | |
| 124 | +var i: INT; | |
| 125 | +begin | |
| 126 | + for i := 0 to LEN(p^.scope) - 1 do | |
| 127 | + if p^.scope[i] = '.' then Buf.add(b, ' '); else Buf.add(b, p^.scope[i]); end; | |
| 128 | + end; | |
| 129 | +end; | |
| 130 | + | |
| 131 | +(* The scope's synopsis lines, as the help and the mistakes begin. *) | |
| 132 | +proc synopsis(p: ^Parser; b: ^Buf.Buf); | |
| 133 | +var i: INT; first: BOOL; | |
| 134 | +begin | |
| 135 | + first := true; | |
| 136 | + for i := 0 to LEN(p^.table) - 1 do | |
| 137 | + match p^.table[i] of | |
| 138 | + | usage(scope, line): | |
| 139 | + if scope = p^.scope then | |
| 140 | + if first then Buf.str(b, "usage: "); else Buf.str(b, " "); end; | |
| 141 | + Buf.str(b, line); | |
| 142 | + Buf.add(b, '\n'); | |
| 143 | + first := false; | |
| 144 | + end; | |
| 145 | + | else: | |
| 146 | + end; | |
| 147 | + end; | |
| 148 | + if first then | |
| 149 | + Buf.str(b, "usage: "); | |
| 150 | + Buf.str(b, p^.name); | |
| 151 | + if LEN(p^.scope) > 0 then Buf.add(b, ' '); scopeWords(p, b); end; | |
| 152 | + Buf.add(b, '\n'); | |
| 153 | + end; | |
| 154 | +end; | |
| 155 | + | |
| 156 | +(* The left column of a help line for an option. *) | |
| 157 | +proc left(b: ^Buf.Buf; letter: U8; long, value: []U8); | |
| 158 | +begin | |
| 159 | + Buf.str(b, " "); | |
| 160 | + if letter >= '!' then | |
| 161 | + Buf.add(b, '-'); Buf.add(b, letter); | |
| 162 | + if LEN(long) > 0 then Buf.str(b, ", "); end; | |
| 163 | + else | |
| 164 | + Buf.str(b, " "); | |
| 165 | + end; | |
| 166 | + if LEN(long) > 0 then Buf.str(b, "--"); Buf.str(b, long); end; | |
| 167 | + if LEN(value) > 0 then Buf.add(b, ' '); Buf.str(b, value); end; | |
| 168 | +end; | |
| 169 | + | |
| 170 | +(* The help: the synopsis, a blank line, and each subcommand and option | |
| 171 | + of the scope, and the help itself, each with its line of help, the | |
| 172 | + helps in a column as far right as the longest left column needs. *) | |
| 173 | +proc printHelp(p: ^Parser); | |
| 174 | +var out, line: Buf.Buf; width, i, k, round: INT; help: []U8; | |
| 175 | +begin | |
| 176 | + Buf.init(ADR(out), Heap.heap); | |
| 177 | + Buf.init(ADR(line), Heap.heap); | |
| 178 | + synopsis(p, ADR(out)); | |
| 179 | + Buf.add(ADR(out), '\n'); | |
| 180 | + (* round 0 measures the left column, round 1 writes the lines *) | |
| 181 | + for round := 0 to 1 do | |
| 182 | + for i := 0 to LEN(p^.table) do | |
| 183 | + Buf.reset(ADR(line)); | |
| 184 | + help := ""; | |
| 185 | + if i = LEN(p^.table) then | |
| 186 | + left(ADR(line), 'h', "help", ""); | |
| 187 | + help := "print this and exit"; | |
| 188 | + else | |
| 189 | + match p^.table[i] of | |
| 190 | + | sub(scope, name, h): | |
| 191 | + if scope = p^.scope then Buf.str(ADR(line), " "); Buf.str(ADR(line), name); help := h; end; | |
| 192 | + | option(scope, letter, long, value, h): | |
| 193 | + if scope = p^.scope then left(ADR(line), letter, long, value); help := h; end; | |
| 194 | + | else: | |
| 195 | + end; | |
| 196 | + end; | |
| 197 | + if line.len > 0 then | |
| 198 | + if round = 0 then | |
| 199 | + if line.len > width then width := line.len; end; | |
| 200 | + else | |
| 201 | + Buf.str(ADR(out), Buf.bytes(ADR(line))); | |
| 202 | + for k := line.len to width + 2 do Buf.add(ADR(out), ' '); end; | |
| 203 | + Buf.str(ADR(out), help); | |
| 204 | + Buf.add(ADR(out), '\n'); | |
| 205 | + end; | |
| 206 | + end; | |
| 207 | + end; | |
| 208 | + end; | |
| 209 | + Out.str(Buf.bytes(ADR(out))); | |
| 210 | + Rt.exit(0); | |
| 211 | +end; | |
| 212 | + | |
| 213 | +(* "name: sentence" on standard error, the synopsis after it if wanted, | |
| 214 | + and the end, with status. *) | |
| 215 | +proc complain(p: ^Parser; sentence: []U8; withSynopsis: BOOL; status: INT); | |
| 216 | +var b: Buf.Buf; | |
| 217 | +begin | |
| 218 | + Buf.init(ADR(b), Heap.heap); | |
| 219 | + Buf.str(ADR(b), p^.name); | |
| 220 | + Buf.str(ADR(b), ": "); | |
| 221 | + Buf.str(ADR(b), sentence); | |
| 222 | + Buf.add(ADR(b), '\n'); | |
| 223 | + if withSynopsis then synopsis(p, ADR(b)); end; | |
| 224 | + Out.flush; | |
| 225 | + _ := Sys.write(Sys.stderr, Buf.bytes(ADR(b))); | |
| 226 | + Rt.exit(status); | |
| 227 | +end; | |
| 228 | + | |
| 229 | +(* -h or --help among the scope's options, before the program sees any: | |
| 230 | + read ahead by a copy of the parser *) | |
| 231 | +proc lookForHelp(p: ^Parser); | |
| 232 | +var q: Parser; s: Step; | |
| 233 | +begin | |
| 234 | + q := p^; | |
| 235 | + repeat | |
| 236 | + s := step(ADR(q)); | |
| 237 | + if s = aHelp then printHelp(p); end; | |
| 238 | + until s = theEnd; | |
| 239 | +end; | |
| 240 | + | |
| 241 | +proc decimal(b: ^Buf.Buf; n: I64); | |
| 242 | +var d: [20]U8; i: INT; u: U64; | |
| 243 | +begin | |
| 244 | + u := U64(n); | |
| 245 | + if n < 0 then Buf.add(b, '-'); u := 0 - u; end; | |
| 246 | + i := LEN(d); | |
| 247 | + repeat DEC(i); d[i] := U8(u mod 10) + '0'; u := u div 10; until u = 0; | |
| 248 | + Buf.str(b, d[i..LEN(d)]); | |
| 249 | +end; | |
| 250 | + | |
| 251 | +(* The table checked before a word is read: a mistake in it is the | |
| 252 | + program's, status 1. *) | |
| 253 | +proc checkTable(p: ^Parser); | |
| 254 | +var i, j, k: INT; is, other: BOOL; letter, l2: U8; long, value, l3, v3, why: []U8; b: Buf.Buf; | |
| 255 | +begin | |
| 256 | + for i := 0 to LEN(p^.table) - 1 do | |
| 257 | + why := ""; | |
| 258 | + match p^.table[i] of | |
| 259 | + | option(scope, letter, long, value, help): | |
| 260 | + if letter = 0 and LEN(long) = 0 then why := "the option has neither a letter nor a long name"; | |
| 261 | + elsif letter = '-' then why := "the letter is -"; | |
| 262 | + elsif letter = 'h' or long = "help" then why := "-h and --help are Opt's"; | |
| 263 | + else | |
| 264 | + for k := 0 to LEN(long) - 1 do | |
| 265 | + if long[k] = '=' then why := "the long name has = in it"; end; | |
| 266 | + end; | |
| 267 | + for j := 0 to i - 1 do | |
| 268 | + other, l2, l3, v3 := optionOf(p^.table[j], scope); | |
| 269 | + if other and ((letter # 0 and l2 = letter) or (LEN(long) > 0 and l3 = long)) then | |
| 270 | + why := "the option is in the scope already"; | |
| 271 | + end; | |
| 272 | + end; | |
| 273 | + end; | |
| 274 | + | else: | |
| 275 | + end; | |
| 276 | + if LEN(why) > 0 then | |
| 277 | + Buf.init(ADR(b), Heap.heap); | |
| 278 | + Buf.str(ADR(b), "option table, row "); | |
| 279 | + decimal(ADR(b), I64(i + 1)); | |
| 280 | + Buf.str(ADR(b), ": "); | |
| 281 | + Buf.str(ADR(b), why); | |
| 282 | + complain(p, Buf.bytes(ADR(b)), false, 1); | |
| 283 | + end; | |
| 284 | + end; | |
| 285 | +end; | |
| 286 | + | |
| 287 | +(* the arguments from 1 on, by table *) | |
| 288 | +proc init*(p: ^Parser; name: []U8; table: []Row); | |
| 289 | +begin | |
| 290 | + if text.len = 0 then Buf.init(ADR(text), Heap.heap); end; | |
| 291 | + p^.letter := 0; p^.value := ""; p^.long := false; | |
| 292 | + p^.name := name; | |
| 293 | + p^.table := table; | |
| 294 | + p^.scope := ""; | |
| 295 | + p^.arg := 1; | |
| 296 | + p^.pos := 0; | |
| 297 | + p^.ended := false; | |
| 298 | + checkTable(p); | |
| 299 | + lookForHelp(p); | |
| 300 | +end; | |
| 301 | + | |
| 302 | +(* the next option; false when they end *) | |
| 303 | +proc next*(p: ^Parser): BOOL; | |
| 304 | +begin | |
| 305 | + match step(p) of | |
| 306 | + | anOption: return true; | |
| 307 | + | theEnd: return false; | |
| 308 | + | aHelp: printHelp(p); | |
| 309 | + | aMistake: complain(p, Buf.bytes(ADR(text)), true, 2); | |
| 310 | + end; | |
| 311 | + return false; | |
| 312 | +end; | |
| 313 | + | |
| 314 | +(* the next operand; false when there are no more *) | |
| 315 | +proc operand*(p: ^Parser): ([]U8, BOOL); | |
| 316 | +begin | |
| 317 | + while next(p) do end; | |
| 318 | + p^.ended := true; | |
| 319 | + if p^.arg >= ARGC() then return "", false; end; | |
| 320 | + INC(p^.arg); | |
| 321 | + return Files.arg(p^.arg - 1), true; | |
| 322 | +end; | |
| 323 | + | |
| 324 | +(* subcommand name's options, from the word after it *) | |
| 325 | +proc scope*(p: ^Parser; name: []U8); | |
| 326 | +begin | |
| 327 | + p^.scope := name; | |
| 328 | + p^.ended := false; | |
| 329 | + p^.pos := 0; | |
| 330 | + lookForHelp(p); | |
| 331 | +end; | |
| 332 | + | |
| 333 | +(* the value just read, a number from lo to hi *) | |
| 334 | +proc number*(p: ^Parser; lo, hi: I64): I64; | |
| 335 | +var n: I64; ok: BOOL; b: Buf.Buf; i: INT; is: BOOL; letter: U8; long, value: []U8; | |
| 336 | +begin | |
| 337 | + n, ok := Str.toInt(p^.value); | |
| 338 | + if ok and n >= lo and n <= hi then return n; end; | |
| 339 | + Buf.init(ADR(b), Heap.heap); | |
| 340 | + i := find(p, p^.letter, ""); | |
| 341 | + is, letter, long, value := optionOf(p^.table[i], p^.scope); | |
| 342 | + if p^.long then Buf.str(ADR(b), "--"); Buf.str(ADR(b), long); | |
| 343 | + else Buf.add(ADR(b), '-'); Buf.add(ADR(b), letter); | |
| 344 | + end; | |
| 345 | + Buf.str(ADR(b), " wants a number from "); | |
| 346 | + decimal(ADR(b), lo); | |
| 347 | + Buf.str(ADR(b), " to "); | |
| 348 | + decimal(ADR(b), hi); | |
| 349 | + Buf.str(ADR(b), ", not '"); | |
| 350 | + Buf.str(ADR(b), p^.value); | |
| 351 | + Buf.str(ADR(b), "'"); | |
| 352 | + complain(p, Buf.bytes(ADR(b)), true, 2); | |
| 353 | + return 0; | |
| 354 | +end; | |
| 355 | + | |
| 356 | +(* the program's own mistake *) | |
| 357 | +proc fail*(p: ^Parser; text: []U8); | |
| 358 | +begin | |
| 359 | + complain(p, text, false, 2); | |
| 360 | +end; | |
| 361 | + | |
| 362 | +end Opt. |
| @@ -0,0 +1,30 @@ | ||
| 1 | +(* Sorting a slice in place, by heapsort: n log n at most, and no memory | |
| 2 | + of its own. *) | |
| 3 | +module Sort(T; less: proc(a, b: T): BOOL); | |
| 4 | + | |
| 5 | +(* the element at i moved down the heap of the first n elements *) | |
| 6 | +proc sift(s: []T; i, n: INT); | |
| 7 | +var child: INT; t: T; | |
| 8 | +begin | |
| 9 | + child := 2 * i + 1; | |
| 10 | + while child < n do | |
| 11 | + if child + 1 < n and less(s[child], s[child + 1]) then INC(child); end; | |
| 12 | + if not less(s[i], s[child]) then return; end; | |
| 13 | + t := s[i]; s[i] := s[child]; s[child] := t; | |
| 14 | + i := child; | |
| 15 | + child := 2 * i + 1; | |
| 16 | + end; | |
| 17 | +end; | |
| 18 | + | |
| 19 | +proc sort*(s: []T); | |
| 20 | +var i, n: INT; t: T; | |
| 21 | +begin | |
| 22 | + n := LEN(s); | |
| 23 | + for i := n div 2 - 1 to 0 by -1 do sift(s, i, n); end; | |
| 24 | + for i := n - 1 to 1 by -1 do | |
| 25 | + t := s[0]; s[0] := s[i]; s[i] := t; | |
| 26 | + sift(s, 0, i); | |
| 27 | + end; | |
| 28 | +end; | |
| 29 | + | |
| 30 | +end Sort. |
| @@ -18,8 +18,21 @@ | ||
| 18 | 18 | sysGetcwd = 79; sysChdir = 80; sysRename = 82; sysMkdir = 83; |
| 19 | 19 | sysRmdir = 84; sysUnlink = 87; sysSymlink = 88; sysReadlink = 89; |
| 20 | 20 | sysChmod = 90; sysClockGettime = 228; sysExitGroup = 231; |
| 21 | - sysOpenat = 257; sysGetrandom = 318; | |
| 21 | + sysOpenat = 257; sysGetrandom = 318; sysPoll = 7; sysDup2 = 33; | |
| 22 | + sysSocket = 41; sysConnect = 42; sysAccept4 = 288; sysSendto = 44; | |
| 23 | + sysBind = 49; sysListen = 50; sysSetsockopt = 54; sysFork = 57; | |
| 24 | + sysExecve = 59; sysExit = 60; sysWait4 = 61; sysFcntl = 72; | |
| 25 | + sysSetpgid = 109; sysSetsid = 112; sysStatfs = 137; | |
| 26 | + sysGetdents64 = 217; sysPipe2 = 293; sysCloseRange = 436; | |
| 27 | + sysRtSigaction = 13; sysRtSigprocmask = 14; | |
| 28 | + | |
| 29 | + (* ioctls of terminals *) | |
| 30 | + tcgets = 5401H; tcsets = 5402H; tiocgwinsz = 5413H; tiocswinsz = 5414H; | |
| 31 | + tiocspgrp = 5410H; tiocgpgrp = 540FH; tiocsctty = 540EH; | |
| 32 | + tiocsptlck = 40045431H; tiocgptn = 80045430H; | |
| 22 | 33 | |
| 34 | + cloexec = 80000H; nonblock = 800H; | |
| 35 | + | |
| 23 | 36 | atFdCwd = -100; |
| 24 | 37 | pathMax = 4096; |
| 25 | 38 | |
| @@ -35,8 +48,29 @@ | ||
| 35 | 48 | symlink*: BOOL; (* the path itself is a symbolic link *) |
| 36 | 49 | end; |
| 37 | 50 | Path = [pathMax]U8; (* a path with a zero after it, as Linux takes it *) |
| 51 | + Dir* = record (* an open directory *) | |
| 52 | + fd: INT; | |
| 53 | + buf: [4096]U8; (* entries as getdents64 gives them *) | |
| 54 | + pos, len: INT; | |
| 55 | + end; | |
| 56 | + Program* = record | |
| 57 | + path*: []U8; | |
| 58 | + args*: [][]U8; (* the first is its name *) | |
| 59 | + env*: [][]U8; (* NAME=value, each added to this program's environment *) | |
| 60 | + onlyEnv*: BOOL; (* env is its whole environment *) | |
| 61 | + dir*: []U8; (* its working directory; "" for this program's *) | |
| 62 | + fds*: [3]INT; (* its standard input, output and error; 0 for this program's own *) | |
| 63 | + group*: INT; (* the process group it joins: 0 for this program's, -1 a new one it leads *) | |
| 64 | + foreground*: BOOL; (* its group takes the terminal on standard input *) | |
| 65 | + tty*: INT; (* a pseudo-terminal's fd, to start a session on; 0 for none *) | |
| 66 | + end; | |
| 67 | + Status* = (running, exited, killed, stopped); | |
| 68 | + Termios = [64]U8; (* the kernel's struct termios, with room *) | |
| 38 | 69 | |
| 39 | 70 | var |
| 71 | + saved: Termios; (* the terminal's mode before rawMode *) | |
| 72 | + savedFd: INT; | |
| 73 | + haveSaved: BOOL; | |
| 40 | 74 | linkBuf: [pathMax]U8; |
| 41 | 75 | dirBuf: [pathMax]U8; |
| 42 | 76 | envp: ^^U8; |
| @@ -52,6 +86,7 @@ | ||
| 52 | 86 | |
| 53 | 87 | proc write*(fd: INT; s: []U8): INT; |
| 54 | 88 | begin |
| 89 | + if LEN(s) = 0 then return 0; end; | |
| 55 | 90 | return SYSCALL(sysWrite, fd, ADR(s[0]) , LEN(s)); |
| 56 | 91 | end; |
| 57 | 92 | |
| @@ -288,6 +323,513 @@ | ||
| 288 | 323 | n := SYSCALL(sysGetrandom, ADR(b[done]), LEN(b) - done, 0); |
| 289 | 324 | if n > 0 then done := done + n; end; |
| 290 | 325 | end; |
| 326 | +end; | |
| 327 | + | |
| 328 | +(* the bytes free and in all on path's file system *) | |
| 329 | +proc space*(path: []U8): (I64, I64, INT); | |
| 330 | +var c: Path; st: [16]I64; r: INT; | |
| 331 | +begin | |
| 332 | + if not cPath(path, c) then return 0, 0, -36; end; | |
| 333 | + r := SYSCALL(sysStatfs, ADR(c), ADR(st)); | |
| 334 | + if r < 0 then return 0, 0, r; end; | |
| 335 | + return st[4] * st[1], st[2] * st[1], 0; | |
| 336 | +end; | |
| 337 | + | |
| 338 | +(* Directories: getdents64's entries, read a buffer at a time. *) | |
| 339 | + | |
| 340 | +proc openDir*(d: ^Dir; path: []U8): INT; | |
| 341 | +var fd: INT; | |
| 342 | +begin | |
| 343 | + fd := open(path, {reading}); | |
| 344 | + if fd < 0 then return fd; end; | |
| 345 | + d^.fd := fd; | |
| 346 | + d^.pos := 0; | |
| 347 | + d^.len := 0; | |
| 348 | + return 0; | |
| 349 | +end; | |
| 350 | + | |
| 351 | +(* a name, good until the next call; false at the end *) | |
| 352 | +proc nextEntry*(d: ^Dir): ([]U8, BOOL); | |
| 353 | +var n, reclen, i: INT; name: []U8; | |
| 354 | +begin | |
| 355 | + while true do | |
| 356 | + if d^.pos >= d^.len then | |
| 357 | + n := SYSCALL(sysGetdents64, d^.fd, ADR(d^.buf), LEN(d^.buf)); | |
| 358 | + if n <= 0 then return "", false; end; | |
| 359 | + d^.pos := 0; | |
| 360 | + d^.len := n; | |
| 361 | + end; | |
| 362 | + reclen := INT(^U16(ADR(d^.buf[d^.pos + 16]))^); | |
| 363 | + i := d^.pos + 19; | |
| 364 | + while d^.buf[i] # 0 do INC(i); end; | |
| 365 | + name := d^.buf[d^.pos + 19..i]; | |
| 366 | + d^.pos := d^.pos + reclen; | |
| 367 | + if name # "." and name # ".." then return name, true; end; | |
| 368 | + end; | |
| 369 | + return "", false; | |
| 370 | +end; | |
| 371 | + | |
| 372 | +proc closeDir*(d: ^Dir); | |
| 373 | +begin | |
| 374 | + close(d^.fd); | |
| 375 | +end; | |
| 376 | + | |
| 377 | +(* The terminal. *) | |
| 378 | + | |
| 379 | +(* the terminal on fd: no echo, no lines, no signals *) | |
| 380 | +proc rawMode*(fd: INT): BOOL; | |
| 381 | +var t: Termios; iflag, oflag, cflag, lflag: ^U32; | |
| 382 | +begin | |
| 383 | + if SYSCALL(sysIoctl, fd, tcgets, ADR(t)) # 0 then return false; end; | |
| 384 | + if not haveSaved then saved := t; savedFd := fd; haveSaved := true; end; | |
| 385 | + iflag := ^U32(ADR(t[0])); oflag := ^U32(ADR(t[4])); | |
| 386 | + cflag := ^U32(ADR(t[8])); lflag := ^U32(ADR(t[12])); | |
| 387 | + iflag^ := iflag^ & ~U32(1H | 2H | 8H | 20H | 40H | 80H | 100H | 400H); | |
| 388 | + oflag^ := oflag^ & ~U32(1H); | |
| 389 | + lflag^ := lflag^ & ~U32(8H | 40H | 2H | 1H | 8000H); | |
| 390 | + cflag^ := (cflag^ & ~U32(30H | 100H)) | 30H; | |
| 391 | + t[17 + 6] := 1; (* VMIN *) | |
| 392 | + t[17 + 5] := 0; (* VTIME *) | |
| 393 | + return SYSCALL(sysIoctl, fd, tcsets, ADR(t)) = 0; | |
| 394 | +end; | |
| 395 | + | |
| 396 | +(* as rawMode found it *) | |
| 397 | +proc restoreMode*(fd: INT); | |
| 398 | +begin | |
| 399 | + if haveSaved then _ := SYSCALL(sysIoctl, fd, tcsets, ADR(saved)); end; | |
| 400 | +end; | |
| 401 | + | |
| 402 | +(* columns and rows; 80 and 24 if unknown *) | |
| 403 | +proc termSize*(fd: INT): (INT, INT); | |
| 404 | +var w: [4]U16; | |
| 405 | +begin | |
| 406 | + if SYSCALL(sysIoctl, fd, tiocgwinsz, ADR(w)) # 0 or w[1] = 0 then return 80, 24; end; | |
| 407 | + return INT(w[1]), INT(w[0]); | |
| 408 | +end; | |
| 409 | + | |
| 410 | +(* the size its programs are told of *) | |
| 411 | +proc setTermSize*(fd, cols, rows: INT): INT; | |
| 412 | +var w: [4]U16; | |
| 413 | +begin | |
| 414 | + w[0] := U16(rows); | |
| 415 | + w[1] := U16(cols); | |
| 416 | + return SYSCALL(sysIoctl, fd, tiocswinsz, ADR(w)); | |
| 417 | +end; | |
| 418 | + | |
| 419 | +(* Waiting: poll on up to 64 descriptors. *) | |
| 420 | + | |
| 421 | +type PollFd = record fd: I32; events, revents: I16; end; | |
| 422 | + | |
| 423 | +(* poll on n of fds, again if a signal interrupts it *) | |
| 424 | +proc poll(fds: ^PollFd; n, ms: INT): INT; | |
| 425 | +var r: INT; | |
| 426 | +begin | |
| 427 | + repeat | |
| 428 | + r := SYSCALL(sysPoll, fds, n, ms); | |
| 429 | + until r # -4; | |
| 430 | + return r; | |
| 431 | +end; | |
| 432 | + | |
| 433 | +(* something to read within ms, or ever if ms < 0 *) | |
| 434 | +proc ready*(fd, ms: INT): BOOL; | |
| 435 | +var p: PollFd; | |
| 436 | +begin | |
| 437 | + p.fd := I32(fd); | |
| 438 | + p.events := 1; | |
| 439 | + return poll(ADR(p), 1, ms) > 0; | |
| 440 | +end; | |
| 441 | + | |
| 442 | +(* readyOf, and the first of writes with room to write *) | |
| 443 | +proc readyFor*(reads, writes: []INT; ms: INT): INT; | |
| 444 | +var p: [64]PollFd; n, i, j: INT; found: BOOL; | |
| 445 | +begin | |
| 446 | + for i := 0 to LEN(reads) - 1 do | |
| 447 | + if n < LEN(p) then p[n].fd := I32(reads[i]); p[n].events := 1; INC(n); end; | |
| 448 | + end; | |
| 449 | + for i := 0 to LEN(writes) - 1 do | |
| 450 | + if n < LEN(p) then | |
| 451 | + found := false; | |
| 452 | + for j := 0 to n - 1 do | |
| 453 | + if p[j].fd = I32(writes[i]) then p[j].events := p[j].events | 4; found := true; end; | |
| 454 | + end; | |
| 455 | + if not found then p[n].fd := I32(writes[i]); p[n].events := 4; INC(n); end; | |
| 456 | + end; | |
| 457 | + end; | |
| 458 | + if poll(ADR(p[0]), n, ms) <= 0 then return -1; end; | |
| 459 | + for i := 0 to n - 1 do | |
| 460 | + if p[i].revents # 0 then return INT(p[i].fd); end; | |
| 461 | + end; | |
| 462 | + return -1; | |
| 463 | +end; | |
| 464 | + | |
| 465 | +(* the first of fds with something to read within ms, or -1 *) | |
| 466 | +proc readyOf*(fds: []INT; ms: INT): INT; | |
| 467 | +var none: [1]INT; | |
| 468 | +begin | |
| 469 | + return readyFor(fds, none[0..0], ms); | |
| 470 | +end; | |
| 471 | + | |
| 472 | +(* if not yes, a read or write that would wait is -11 at once *) | |
| 473 | +proc setBlocking*(fd: INT; yes: BOOL): INT; | |
| 474 | +var flags: INT; | |
| 475 | +begin | |
| 476 | + flags := SYSCALL(sysFcntl, fd, 3); | |
| 477 | + if flags < 0 then return flags; end; | |
| 478 | + if yes then flags := flags & ~nonblock; else flags := flags | nonblock; end; | |
| 479 | + return SYSCALL(sysFcntl, fd, 4, flags); | |
| 480 | +end; | |
| 481 | + | |
| 482 | +(* Programs. *) | |
| 483 | + | |
| 484 | +(* the fd to read and the fd to write; or minus an errno and -1 *) | |
| 485 | +proc pipe*: (INT, INT); | |
| 486 | +var fds: [2]I32; r: INT; | |
| 487 | +begin | |
| 488 | + r := SYSCALL(sysPipe2, ADR(fds), cloexec); | |
| 489 | + if r < 0 then return r, -1; end; | |
| 490 | + return INT(fds[0]), INT(fds[1]); | |
| 491 | +end; | |
| 492 | + | |
| 493 | +(* a pseudo-terminal: the fd of its controlling side *) | |
| 494 | +proc openPty*: INT; | |
| 495 | +var fd: INT; zero: I32; | |
| 496 | +begin | |
| 497 | + fd := open("/dev/ptmx", {reading, writing}); | |
| 498 | + if fd < 0 then return fd; end; | |
| 499 | + _ := SYSCALL(sysIoctl, fd, tiocsptlck, ADR(zero)); | |
| 500 | + return fd; | |
| 501 | +end; | |
| 502 | + | |
| 503 | +(* s with a zero after it, in memory of its own, for the new program *) | |
| 504 | +proc cString(s: []U8): ^U8; | |
| 505 | +var p: ^U8; | |
| 506 | +begin | |
| 507 | + p := mmap(LEN(s) + 1); | |
| 508 | + if p # nil and LEN(s) > 0 then COPY(p[0..LEN(s)], s); end; | |
| 509 | + return p; | |
| 510 | +end; | |
| 511 | + | |
| 512 | +(* The new program's environment: this one's with p.env put in, or p.env alone. *) | |
| 513 | +proc environment(p: Program): ^^U8; | |
| 514 | +var e: ^^U8; n, i, j, k, eq: INT; s: ^U8; replaced: BOOL; | |
| 515 | +begin | |
| 516 | + if envp = nil then envp := environ(); end; | |
| 517 | + while envp[n] # nil do INC(n); end; | |
| 518 | + e := ^^U8(mmap((n + LEN(p.env) + 1) * 8)); | |
| 519 | + k := 0; | |
| 520 | + if not p.onlyEnv then | |
| 521 | + for i := 0 to n - 1 do | |
| 522 | + s := envp[i]; | |
| 523 | + replaced := false; | |
| 524 | + for j := 0 to LEN(p.env) - 1 do | |
| 525 | + eq := 0; | |
| 526 | + while eq < LEN(p.env[j]) and p.env[j][eq] # '=' do INC(eq); end; | |
| 527 | + if s[0..eq + 1] = p.env[j][0..eq + 1] then replaced := true; end; | |
| 528 | + end; | |
| 529 | + if not replaced then e[k] := s; INC(k); end; | |
| 530 | + end; | |
| 531 | + end; | |
| 532 | + for j := 0 to LEN(p.env) - 1 do e[k] := cString(p.env[j]); INC(k); end; | |
| 533 | + e[k] := nil; | |
| 534 | + return e; | |
| 535 | +end; | |
| 536 | + | |
| 537 | +(* In the child, after fork: what spawn says, then the program. *) | |
| 538 | +proc child(p: Program; path: ^U8; argv, env: ^^U8); | |
| 539 | +var i, n, slave: INT; zero: [2]U64; act: [4]U64; pts: Path; pgrp: I32; | |
| 540 | +begin | |
| 541 | + if p.tty # 0 then | |
| 542 | + (* a session on the pseudo-terminal: its controlling terminal and | |
| 543 | + standard input, output and error *) | |
| 544 | + _ := SYSCALL(sysSetsid); | |
| 545 | + _ := SYSCALL(sysIoctl, p.tty, tiocgptn, ADR(n)); | |
| 546 | + pts := "/dev/pts/"; | |
| 547 | + i := 9; | |
| 548 | + if n = 0 then pts[i] := '0'; INC(i); end; | |
| 549 | + slave := n; | |
| 550 | + while slave > 0 do INC(i); slave := slave div 10; end; | |
| 551 | + slave := i; | |
| 552 | + while n > 0 do DEC(i); pts[i] := U8(n mod 10) + '0'; n := n div 10; end; | |
| 553 | + pts[slave] := 0; | |
| 554 | + slave := SYSCALL(sysOpenat, atFdCwd, ADR(pts), 2); | |
| 555 | + _ := SYSCALL(sysIoctl, slave, tiocsctty, 0); | |
| 556 | + for i := 0 to 2 do _ := SYSCALL(sysDup2, slave, i); end; | |
| 557 | + else | |
| 558 | + if p.group = -1 then _ := SYSCALL(sysSetpgid, 0, 0); | |
| 559 | + elsif p.group > 0 then _ := SYSCALL(sysSetpgid, 0, p.group); | |
| 560 | + end; | |
| 561 | + if p.foreground then | |
| 562 | + pgrp := I32(SYSCALL(sysGetpid)); | |
| 563 | + if p.group > 0 then pgrp := I32(p.group); end; | |
| 564 | + _ := SYSCALL(sysIoctl, 0, tiocspgrp, ADR(pgrp)); | |
| 565 | + end; | |
| 566 | + for i := 0 to 2 do | |
| 567 | + if p.fds[i] # 0 then _ := SYSCALL(sysDup2, p.fds[i], i); end; | |
| 568 | + end; | |
| 569 | + end; | |
| 570 | + (* every signal as it is at first, and none blocked *) | |
| 571 | + for i := 1 to 31 do | |
| 572 | + if i # 9 and i # 19 then _ := SYSCALL(sysRtSigaction, i, ADR(act), 0, 8); end; | |
| 573 | + end; | |
| 574 | + _ := SYSCALL(sysRtSigprocmask, 2, ADR(zero), 0, 8); | |
| 575 | + _ := SYSCALL(sysCloseRange, 3, 0FFFFFFFFH, 0); | |
| 576 | + if LEN(p.dir) > 0 and setWorkDir(p.dir) < 0 then _ := SYSCALL(sysExit, 127); end; | |
| 577 | + _ := SYSCALL(sysExecve, path, argv, env); | |
| 578 | + _ := SYSCALL(sysExit, 127); | |
| 579 | +end; | |
| 580 | + | |
| 581 | +(* a program started: its process id *) | |
| 582 | +proc spawn*(p: Program): INT; | |
| 583 | +var path: ^U8; argv, env: ^^U8; i, pid: INT; | |
| 584 | +begin | |
| 585 | + path := cString(p.path); | |
| 586 | + argv := ^^U8(mmap((LEN(p.args) + 1) * 8)); | |
| 587 | + if path = nil or argv = nil then return -12; end; | |
| 588 | + for i := 0 to LEN(p.args) - 1 do argv[i] := cString(p.args[i]); end; | |
| 589 | + env := environment(p); | |
| 590 | + pid := SYSCALL(sysFork); | |
| 591 | + if pid = 0 then child(p, path, argv, env); end; | |
| 592 | + (* the parent's copies given back *) | |
| 593 | + for i := 0 to LEN(p.args) - 1 do munmap(argv[i], LEN(p.args[i]) + 1); end; | |
| 594 | + munmap(^U8(argv), (LEN(p.args) + 1) * 8); | |
| 595 | + munmap(path, LEN(p.path) + 1); | |
| 596 | + return pid; | |
| 597 | +end; | |
| 598 | + | |
| 599 | +(* which program, how, and its code *) | |
| 600 | +proc wait*(pid: INT; block: BOOL): (INT, Status, INT); | |
| 601 | +var st: I32; r, options: INT; | |
| 602 | +begin | |
| 603 | + options := 2; (* WUNTRACED *) | |
| 604 | + if not block then options := options | 1; end; | |
| 605 | + repeat | |
| 606 | + r := SYSCALL(sysWait4, pid, ADR(st), options, 0); | |
| 607 | + until r # -4; | |
| 608 | + if r <= 0 then return r, running, 0; end; | |
| 609 | + if st & 7FH = 0 then return r, exited, INT((st shr 8) & 0FFH); end; | |
| 610 | + if st & 0FFH = 7FH then return r, stopped, INT((st shr 8) & 0FFH); end; | |
| 611 | + return r, killed, INT(st & 7FH); | |
| 612 | +end; | |
| 613 | + | |
| 614 | +(* pid 0 is this program; group 0 a new one pid leads *) | |
| 615 | +proc setGroup*(pid, group: INT): INT; | |
| 616 | +begin | |
| 617 | + return SYSCALL(sysSetpgid, pid, group); | |
| 618 | +end; | |
| 619 | + | |
| 620 | +(* the terminal on fd gives its keys to group *) | |
| 621 | +proc foreground*(fd, group: INT): INT; | |
| 622 | +var g: I32; | |
| 623 | +begin | |
| 624 | + g := I32(group); | |
| 625 | + return SYSCALL(sysIoctl, fd, tiocspgrp, ADR(g)); | |
| 626 | +end; | |
| 627 | + | |
| 628 | +(* the group the terminal on fd gives its keys to *) | |
| 629 | +proc foregroundOf*(fd: INT): INT; | |
| 630 | +var g: I32; r: INT; | |
| 631 | +begin | |
| 632 | + r := SYSCALL(sysIoctl, fd, tiocgpgrp, ADR(g)); | |
| 633 | + if r < 0 then return r; end; | |
| 634 | + return INT(g); | |
| 635 | +end; | |
| 636 | + | |
| 637 | +(* Signals: SBCL's run time catches them and writes each to a pipe, | |
| 638 | + whose end to read is the fd catch gives. *) | |
| 639 | + | |
| 640 | +proc catchSignal(sig: INT): INT; | |
| 641 | +code | |
| 642 | + (catch-signal sig); | |
| 643 | +end; | |
| 644 | + | |
| 645 | +proc ignoreSignal(sig: INT; yes: BOOL); | |
| 646 | +code | |
| 647 | + (ignore-signal sig yes); | |
| 648 | +end; | |
| 649 | + | |
| 650 | +(* sigs caught: an fd to wait on *) | |
| 651 | +proc catch*(sigs: []INT): INT; | |
| 652 | +var i, fd: INT; | |
| 653 | +begin | |
| 654 | + for i := 0 to LEN(sigs) - 1 do | |
| 655 | + if sigs[i] = sigKill or sigs[i] = sigStop or sigs[i] = 11 or sigs[i] = 8 | |
| 656 | + or sigs[i] < 1 or sigs[i] > 64 then | |
| 657 | + return -22; | |
| 658 | + end; | |
| 659 | + end; | |
| 660 | + fd := -22; | |
| 661 | + for i := 0 to LEN(sigs) - 1 do fd := catchSignal(sigs[i]); end; | |
| 662 | + return fd; | |
| 663 | +end; | |
| 664 | + | |
| 665 | +(* the next signal caught, or 0 if none is waiting *) | |
| 666 | +proc caught*(fd: INT): INT; | |
| 667 | +var b: [1]U8; | |
| 668 | +begin | |
| 669 | + if SYSCALL(sysRead, fd, ADR(b), 1) = 1 then return INT(b[0]); end; | |
| 670 | + return 0; | |
| 671 | +end; | |
| 672 | + | |
| 673 | +(* sig ignored; or, if not yes, back to what it does at first *) | |
| 674 | +proc ignore*(sig: INT; yes: BOOL): INT; | |
| 675 | +begin | |
| 676 | + if sig = sigKill or sig = sigStop or sig = 11 or sig = 8 or sig < 1 or sig > 64 then return -22; end; | |
| 677 | + ignoreSignal(sig, yes); | |
| 678 | + return 0; | |
| 679 | +end; | |
| 680 | + | |
| 681 | +(* Sockets. An address is a sockaddr, made in a [128]U8. *) | |
| 682 | + | |
| 683 | +type Address = [128]U8; | |
| 684 | + | |
| 685 | +(* path as a Unix socket's address, and its length; -36 if too long *) | |
| 686 | +proc unixAddress(path: []U8; var a: Address): INT; | |
| 687 | +begin | |
| 688 | + if LEN(path) > 107 then return -36; end; | |
| 689 | + a[0] := 1; (* AF_UNIX *) | |
| 690 | + COPY(a[2..2 + LEN(path)], path); | |
| 691 | + a[2 + LEN(path)] := 0; | |
| 692 | + return 2 + LEN(path) + 1; | |
| 693 | +end; | |
| 694 | + | |
| 695 | +(* the number at s[i..], hexadecimal or decimal, and where it ends *) | |
| 696 | +proc number(s: []U8; i: INT; hex: BOOL): (INT, INT, BOOL); | |
| 697 | +var v, d, start: INT; | |
| 698 | +begin | |
| 699 | + start := i; | |
| 700 | + while i < LEN(s) do | |
| 701 | + if s[i] >= '0' and s[i] <= '9' then d := INT(s[i] - '0'); | |
| 702 | + elsif hex and s[i] >= 'a' and s[i] <= 'f' then d := INT(s[i] - 'a') + 10; | |
| 703 | + elsif hex and s[i] >= 'A' and s[i] <= 'F' then d := INT(s[i] - 'A') + 10; | |
| 704 | + else return v, i, i > start; | |
| 705 | + end; | |
| 706 | + if hex then v := v * 16 + d; else v := v * 10 + d; end; | |
| 707 | + if v > 0FFFFH then return 0, i, false; end; | |
| 708 | + INC(i); | |
| 709 | + end; | |
| 710 | + return v, i, i > start; | |
| 711 | +end; | |
| 712 | + | |
| 713 | +(* addr, IPv4 or IPv6 numbers, and port as an address, and its length; -22 if addr isn't one *) | |
| 714 | +proc inetAddress(addr: []U8; port: INT; var a: Address): INT; | |
| 715 | +var i, k, v, gap, n: INT; ok: BOOL; groups: [8]INT; | |
| 716 | +begin | |
| 717 | + a[2] := U8(port shr 8); | |
| 718 | + a[3] := U8(port & 0FFH); | |
| 719 | + while i < LEN(addr) and addr[i] # ':' do INC(i); end; | |
| 720 | + if i = LEN(addr) then | |
| 721 | + i := 0; | |
| 722 | + a[0] := 2; (* AF_INET *) | |
| 723 | + for k := 0 to 3 do | |
| 724 | + v, i, ok := number(addr, i, false); | |
| 725 | + if not ok or v > 255 then return -22; end; | |
| 726 | + a[4 + k] := U8(v); | |
| 727 | + if k < 3 then | |
| 728 | + if i >= LEN(addr) or addr[i] # '.' then return -22; end; | |
| 729 | + INC(i); | |
| 730 | + end; | |
| 731 | + end; | |
| 732 | + if i # LEN(addr) then return -22; end; | |
| 733 | + return 16; | |
| 734 | + end; | |
| 735 | + a[0] := 10; (* AF_INET6 *) | |
| 736 | + gap := -1; | |
| 737 | + i := 0; | |
| 738 | + if LEN(addr) >= 2 and addr[0..2] = "::" then gap := 0; i := 2; end; | |
| 739 | + while i < LEN(addr) do | |
| 740 | + if n = 8 then return -22; end; | |
| 741 | + v, i, ok := number(addr, i, true); | |
| 742 | + if not ok then return -22; end; | |
| 743 | + groups[n] := v; | |
| 744 | + INC(n); | |
| 745 | + if i < LEN(addr) then | |
| 746 | + if addr[i] # ':' then return -22; end; | |
| 747 | + INC(i); | |
| 748 | + if i < LEN(addr) and addr[i] = ':' then | |
| 749 | + if gap >= 0 then return -22; end; | |
| 750 | + gap := n; | |
| 751 | + INC(i); | |
| 752 | + end; | |
| 753 | + end; | |
| 754 | + end; | |
| 755 | + if gap < 0 and n # 8 then return -22; end; | |
| 756 | + (* the groups after the gap move to the end *) | |
| 757 | + for k := 0 to 7 do | |
| 758 | + if gap < 0 or k < gap then v := groups[k]; | |
| 759 | + elsif k >= 8 - (n - gap) then v := groups[k - (8 - n)]; | |
| 760 | + else v := 0; | |
| 761 | + end; | |
| 762 | + a[8 + 2 * k] := U8(v shr 8); | |
| 763 | + a[9 + 2 * k] := U8(v & 0FFH); | |
| 764 | + end; | |
| 765 | + return 28; | |
| 766 | +end; | |
| 767 | + | |
| 768 | +(* a socket of type kind for address a, connected or bound and listening *) | |
| 769 | +proc socket(a: Address; n, kind: INT; serve: BOOL): INT; | |
| 770 | +var fd, r: INT; one: I32; | |
| 771 | +begin | |
| 772 | + if n < 0 then return n; end; | |
| 773 | + fd := SYSCALL(sysSocket, INT(a[0]), kind | cloexec, 0); | |
| 774 | + if fd < 0 then return fd; end; | |
| 775 | + if serve then | |
| 776 | + one := 1; | |
| 777 | + _ := SYSCALL(sysSetsockopt, fd, 1, 2, ADR(one), 4); | |
| 778 | + r := SYSCALL(sysBind, fd, ADR(a), n); | |
| 779 | + if r = 0 then r := SYSCALL(sysListen, fd, 128); end; | |
| 780 | + else | |
| 781 | + r := SYSCALL(sysConnect, fd, ADR(a), n); | |
| 782 | + end; | |
| 783 | + if r < 0 then close(fd); return r; end; | |
| 784 | + return fd; | |
| 785 | +end; | |
| 786 | + | |
| 787 | +(* a stream socket, connected to the Unix socket at path: its fd *) | |
| 788 | +proc connect*(path: []U8): INT; | |
| 789 | +var a: Address; | |
| 790 | +begin | |
| 791 | + return socket(a, unixAddress(path, a), 1, false); | |
| 792 | +end; | |
| 793 | + | |
| 794 | +(* addr an IPv4 or IPv6 address, as numbers *) | |
| 795 | +proc connectTcp*(addr: []U8; port: INT): INT; | |
| 796 | +var a: Address; | |
| 797 | +begin | |
| 798 | + return socket(a, inetAddress(addr, port, a), 1, false); | |
| 799 | +end; | |
| 800 | + | |
| 801 | +proc connectUdp*(addr: []U8; port: INT): INT; | |
| 802 | +var a: Address; | |
| 803 | +begin | |
| 804 | + return socket(a, inetAddress(addr, port, a), 2, false); | |
| 805 | +end; | |
| 806 | + | |
| 807 | +(* a Unix socket made at path, taking connections *) | |
| 808 | +proc listen*(path: []U8): INT; | |
| 809 | +var a: Address; | |
| 810 | +begin | |
| 811 | + return socket(a, unixAddress(path, a), 1, true); | |
| 812 | +end; | |
| 813 | + | |
| 814 | +proc listenTcp*(addr: []U8; port: INT): INT; | |
| 815 | +var a: Address; | |
| 816 | +begin | |
| 817 | + return socket(a, inetAddress(addr, port, a), 1, true); | |
| 818 | +end; | |
| 819 | + | |
| 820 | +(* the next connection, waiting for one *) | |
| 821 | +proc accept*(fd: INT): INT; | |
| 822 | +var r: INT; | |
| 823 | +begin | |
| 824 | + repeat r := SYSCALL(sysAccept4, fd, 0, 0, cloexec); until r # -4; | |
| 825 | + return r; | |
| 826 | +end; | |
| 827 | + | |
| 828 | +(* write, to a socket: -32 (EPIPE), not a signal, if it is closed *) | |
| 829 | +proc send*(fd: INT; s: []U8): INT; | |
| 830 | +begin | |
| 831 | + if LEN(s) = 0 then return 0; end; | |
| 832 | + return SYSCALL(sysSendto, fd, ADR(s[0]), LEN(s), 4000H, 0, 0); | |
| 291 | 833 | end; |
| 292 | 834 | |
| 293 | 835 | end Sys. |
| @@ -0,0 +1,60 @@ | ||
| 1 | +(* Threads for a program without C: spawn, join, and Mutex. Here each is a | |
| 2 | + thread of SBCL's, on a raz stack of its own. *) | |
| 3 | +module Thread; | |
| 4 | +import Heap, Mem; | |
| 5 | +link static; | |
| 6 | + | |
| 7 | +const Failed*: ERROR = "can't start a thread"; | |
| 8 | + | |
| 9 | +type | |
| 10 | + Thread* = record id: INT; end; | |
| 11 | + Mutex* = record state: INT; end; (* zero is free; 1 taken; 2 taken, with waiters *) | |
| 12 | + | |
| 13 | +const | |
| 14 | + sysFutex = 202; | |
| 15 | + futexWait = 128; (* FUTEX_WAIT, private *) | |
| 16 | + futexWake = 129; | |
| 17 | + | |
| 18 | +proc start(p: proc(arg: ^U8); arg: ^U8): INT; | |
| 19 | +code | |
| 20 | + (thread-spawn p arg); | |
| 21 | +end; | |
| 22 | + | |
| 23 | +proc wait(id: INT); | |
| 24 | +code | |
| 25 | + (thread-join id); | |
| 26 | +end; | |
| 27 | + | |
| 28 | +(* p(arg) in a new thread *) | |
| 29 | +proc spawn*(p: proc(arg: ^U8); arg: ^U8): (^Thread, ERROR); | |
| 30 | +var t: ^Thread; | |
| 31 | +begin | |
| 32 | + NEW(t, Heap.heap); | |
| 33 | + if t = nil then return nil, Failed; end; | |
| 34 | + t^.id := start(p, arg); | |
| 35 | + return t, nil; | |
| 36 | +end; | |
| 37 | + | |
| 38 | +(* waits for t to end, then frees what it had *) | |
| 39 | +proc join*(t: ^Thread); | |
| 40 | +begin | |
| 41 | + wait(t^.id); | |
| 42 | + Mem.free(Heap.heap, ^U8(t)); | |
| 43 | +end; | |
| 44 | + | |
| 45 | +proc lock*(m: ^Mutex); | |
| 46 | +begin | |
| 47 | + if CAS(ADR(m^.state), 0, 1) then return; end; | |
| 48 | + while XCHG(ADR(m^.state), 2) # 0 do | |
| 49 | + _ := SYSCALL(sysFutex, ADR(m^.state), futexWait, 2, 0); | |
| 50 | + end; | |
| 51 | +end; | |
| 52 | + | |
| 53 | +proc unlock*(m: ^Mutex); | |
| 54 | +begin | |
| 55 | + if XCHG(ADR(m^.state), 0) = 2 then | |
| 56 | + _ := SYSCALL(sysFutex, ADR(m^.state), futexWake, 1); | |
| 57 | + end; | |
| 58 | +end; | |
| 59 | + | |
| 60 | +end Thread. |
| @@ -80,7 +80,7 @@ | ||
| 80 | 80 | |
| 81 | 81 | (defun pointer-type-rest () |
| 82 | 82 | "A pointer type, after its ^." |
| 83 | - (let ((vol (accept :volatile))) | |
| 83 | + (let ((vol (and (accept :volatile) t))) | |
| 84 | 84 | (if (and (listp *forward-types*) (eq (kind) :ident) (not (eq (kind 1) :|.|)) |
| 85 | 85 | (not (lookup (tok-val (tok))))) |
| 86 | 86 | ;; a type declared later in this section (section 4) |
| @@ -743,13 +743,13 @@ | ||
| 743 | 743 | (let ((tk (item-tok a))) |
| 744 | 744 | (unless (eq (item-mode a) :var) (fail-tok tk "a var parameter takes a variable")) |
| 745 | 745 | (check-writable a tk "passed as a var argument") |
| 746 | + (when (item-vol a) (fail-tok tk "a volatile variable isn't passed as a var argument")) | |
| 746 | 747 | (when (item-noaddr a) (fail-tok tk "a field of a packed record has no address")) |
| 747 | 748 | (unless (same-type-p (item-type a) ty) |
| 748 | 749 | (fail-tok tk "a var parameter of ~a takes a variable of it, not ~a" (type-name ty) (type-name (item-type a)))) |
| 749 | 750 | (item-form a))) |
| 750 | 751 | |
| 751 | 752 | (defun check-writable (a tk what) |
| 752 | - (when (item-vol a) (fail-tok tk "a volatile variable can't be ~a" what)) | |
| 753 | 753 | (case (item-ro a) |
| 754 | 754 | (:export (fail-tok tk "another module's variable can't be ~a" what)) |
| 755 | 755 | (:const (fail-tok tk "a constant can't be ~a" what)) |
| @@ -893,7 +893,7 @@ | ||
| 893 | 893 | (unless (eq (item-mode x) :var) (fail-tok tk "ADR takes a variable")) |
| 894 | 894 | (check-writable x tk "given to ADR") |
| 895 | 895 | (when (item-noaddr x) (fail-tok tk "a field of a packed record has no address")) |
| 896 | - (val-item (make-ptr-type (item-type x) nil) (item-form x) tk))) | |
| 896 | + (val-item (make-ptr-type (item-type x) (item-vol x)) (item-form x) tk))) | |
| 897 | 897 | ((string= name "LEN") |
| 898 | 898 | (arity args 1) |
| 899 | 899 | (let* ((x (need-value (arg 0))) (ty (item-type x))) |
| @@ -307,11 +307,36 @@ | ||
| 307 | 307 | (sb-thread:join-thread th :default nil) |
| 308 | 308 | (sb-thread:with-mutex (*threads-lock*) (remhash id *threads*))))) |
| 309 | 309 | |
| 310 | +;;; Signals, for the library's Sys.catch and Sys.ignore: SBCL's run time | |
| 311 | +;;; handles each signal caught, writing its number to a pipe. | |
| 312 | + | |
| 313 | +(sb-ext:defglobal *signal-pipe* nil "The fds of the pipe, read and write, once made.") | |
| 314 | + | |
| 315 | +(defun catch-signal (sig) | |
| 316 | + (unless *signal-pipe* | |
| 317 | + (let ((buf (c-call "malloc" sb-alien:unsigned-long sb-alien:unsigned-long 8))) | |
| 318 | + ;; pipe2(buf, O_CLOEXEC | O_NONBLOCK) | |
| 319 | + (syscall 293 buf (logior #x80000 #x800)) | |
| 320 | + (setf *signal-pipe* (cons (ld :i32 buf) (ld :i32 (+ buf 4)))))) | |
| 321 | + (let ((w (cdr *signal-pipe*))) | |
| 322 | + (sb-sys:enable-interrupt sig (lambda (&rest args) | |
| 323 | + (declare (ignore args)) | |
| 324 | + (let ((b (c-call "malloc" sb-alien:unsigned-long sb-alien:unsigned-long 1))) | |
| 325 | + (st :u8 b sig) | |
| 326 | + (syscall 1 w b 1) | |
| 327 | + (c-call "free" sb-alien:void sb-alien:unsigned-long b))))) | |
| 328 | + (car *signal-pipe*)) | |
| 329 | + | |
| 330 | +(defun ignore-signal (sig yes) | |
| 331 | + (sb-sys:enable-interrupt sig (if yes :ignore :default))) | |
| 332 | + | |
| 310 | 333 | ;;; The start. The saved program begins here: the data block mapped and |
| 311 | 334 | ;;; filled, then the program run on the main thread's raz stack, and its |
| 312 | 335 | ;;; status given to Rt.exit. |
| 313 | 336 | |
| 314 | 337 | (defun start () |
| 338 | + ;; the signals SBCL handles for itself do what they do in any program | |
| 339 | + (dolist (sig '(1 2 3 13 15)) (sb-sys:enable-interrupt sig :default)) | |
| 315 | 340 | (let ((size (length *data-image*))) |
| 316 | 341 | (when (> size 0) |
| 317 | 342 | (map-memory (round-up size 4096) +data-base+) |
| @@ -130,7 +130,10 @@ | ||
| 130 | 130 | (cond ((string= text "_") (add :blank nil l c)) |
| 131 | 131 | ((member text *reserved* :test #'string=) |
| 132 | 132 | (add (keyword-of (string-upcase text)) nil l c) |
| 133 | - (when (string= text "code") (code-body))) | |
| 133 | + ;; a code body follows `code` after a signature's `;` | |
| 134 | + (when (and (string= text "code") (rest toks) | |
| 135 | + (eq (tok-kind (second toks)) :|;|)) | |
| 136 | + (code-body))) | |
| 134 | 137 | (t (add :ident text l c)))))) |
| 135 | 138 | (number (l c) |
| 136 | 139 | ;; digits and capitals A-F; then H for hexadecimal, or a |
| @@ -120,6 +120,9 @@ | ||
| 120 | 120 | (case (item-mode x) |
| 121 | 121 | (:proc (item-form (call-proc (item-obj x) nil tk))) |
| 122 | 122 | ((:none :multi) (item-form x)) |
| 123 | + (:builtin (if (string= (item-val x) "FENCE") | |
| 124 | + '(sb-thread:barrier (:memory)) | |
| 125 | + (fail-tok tk "~a takes arguments" (item-val x)))) | |
| 123 | 126 | (t (if (consp (item-obj x)) |
| 124 | 127 | (item-form x) ; a call with one result |
| 125 | 128 | (if (and (member (item-mode x) '(:var :val)) (is (item-type x) :proc)) |