site: v0.24.0, and its examples held to report 2026-10.37 Strings only read are []const U8; ADR, PTR, SYSCALL and ARGV written SYSTEM., in pages whose modules import SYSTEM; atomics on the variable; the atomics, lockfree, syscalls, records, hello and slices pages say so.
| @@ -67,7 +67,7 @@ | ||
| 67 | 67 | import Out, Buf, Fmt, Heap; |
| 68 | 68 | |
| 69 | 69 | (* "name (age n)", built in a Buf the caller owns *) |
| 70 | -proc describe(var b: Buf.Buf; name: []U8; age: INT); | |
| 70 | +proc describe(var b: Buf.Buf; name: []const U8; age: INT); | |
| 71 | 71 | begin |
| 72 | 72 | Fmt.write(b, "{} (age {})", [Fmt.str(name), Fmt.int(age)]); |
| 73 | 73 | end; |
| @@ -96,7 +96,7 @@ | ||
| 96 | 96 | import Out; |
| 97 | 97 | |
| 98 | 98 | (* s, then spaces up to width *) |
| 99 | -proc left(s: []U8; width: INT); | |
| 99 | +proc left(s: []const U8; width: INT); | |
| 100 | 100 | var i: INT; |
| 101 | 101 | begin |
| 102 | 102 | Out.str(s); |
| @@ -115,7 +115,7 @@ | ||
| 115 | 115 | Out.int(n); |
| 116 | 116 | end; |
| 117 | 117 | |
| 118 | -proc row(name: []U8; qty, price: INT); | |
| 118 | +proc row(name: []const U8; qty, price: INT); | |
| 119 | 119 | begin |
| 120 | 120 | left(name, 10); |
| 121 | 121 | right(qty, 5); |
| @@ -144,7 +144,7 @@ | ||
| 144 | 144 | module Dump; |
| 145 | 145 | import Out; |
| 146 | 146 | |
| 147 | -proc dump(data: []U8); | |
| 147 | +proc dump(data: []const U8); | |
| 148 | 148 | var i: INT; |
| 149 | 149 | begin |
| 150 | 150 | for i := 0 to LEN(data) - 1 do |
| @@ -13,7 +13,7 @@ | ||
| 13 | 13 | import Out, Str, Fmt; |
| 14 | 14 | |
| 15 | 15 | proc main*; |
| 16 | -var names: [4][]U8; i, j: INT; t: []U8; | |
| 16 | +var names: [4][]const U8; i, j: INT; t: []const U8; | |
| 17 | 17 | begin |
| 18 | 18 | names := ["pear", "Apple", "fig", "apple"]; |
| 19 | 19 | (* a small insertion sort, with Str.cmp *) |
| @@ -44,13 +44,14 @@ | ||
| 44 | 44 | ```raz Keep run |
| 45 | 45 | module Keep; |
| 46 | 46 | import Out, Str, Heap, Mem; |
| 47 | +import SYSTEM; | |
| 47 | 48 | |
| 48 | 49 | type Entry = record name: []U8; n: INT; end; |
| 49 | 50 | |
| 50 | 51 | var entries: [3]Entry; |
| 51 | 52 | |
| 52 | 53 | (* remembers a name that the caller will overwrite *) |
| 53 | -proc remember(slot: INT; name: []U8); | |
| 54 | +proc remember(slot: INT; name: []const U8); | |
| 54 | 55 | var copy: []U8; ok: BOOL; |
| 55 | 56 | begin |
| 56 | 57 | copy, ok := Str.copy(Heap.heap, name); |
| @@ -68,7 +69,7 @@ | ||
| 68 | 69 | end; |
| 69 | 70 | for i := 0 to 2 do |
| 70 | 71 | Out.str(entries[i].name); Out.char(' '); |
| 71 | - Mem.free(Heap.heap, ^U8(ADR(entries[i].name[0]))); | |
| 72 | + Mem.free(Heap.heap, ^U8(SYSTEM.ADR(entries[i].name[0]))); | |
| 72 | 73 | end; |
| 73 | 74 | Out.ln; |
| 74 | 75 | end; |
| @@ -85,13 +86,13 @@ | ||
| 85 | 86 | import Out, Fmt, Bytes; |
| 86 | 87 | |
| 87 | 88 | (* the last part of a path: what follows its last slash *) |
| 88 | -proc base(path: []U8): []U8; | |
| 89 | +proc base(path: []const U8): []const U8; | |
| 89 | 90 | begin |
| 90 | 91 | return path[Bytes.last(path, '/') + 1..LEN(path)]; |
| 91 | 92 | end; |
| 92 | 93 | |
| 93 | 94 | proc main*; |
| 94 | -var text: []U8; at, n, line: INT; | |
| 95 | +var text: []const U8; at, n, line: INT; | |
| 95 | 96 | begin |
| 96 | 97 | Out.str(base("/usr/local/lib/raz/Bytes.raz")); Out.char(' '); |
| 97 | 98 | Out.str(base("Out.raz")); Out.ln; |
| @@ -127,7 +128,7 @@ | ||
| 127 | 128 | const HeaderSize = 10; |
| 128 | 129 | |
| 129 | 130 | (* the sum of the bytes of data, all mod 2^32 *) |
| 130 | -proc checksum(data: []U8): U32; | |
| 131 | +proc checksum(data: []const U8): U32; | |
| 131 | 132 | var i: INT; sum: U32; |
| 132 | 133 | begin |
| 133 | 134 | for i := 0 to LEN(data) - 1 do sum := sum + U32(data[i]); end; |
| @@ -135,7 +136,7 @@ | ||
| 135 | 136 | end; |
| 136 | 137 | |
| 137 | 138 | (* writes a frame to out, which must have room; its length *) |
| 138 | -proc pack(out: []U8; tag, data: []U8): INT; | |
| 139 | +proc pack(out: []U8; tag, data: []const U8): INT; | |
| 139 | 140 | begin |
| 140 | 141 | COPY(out[0..4], tag); |
| 141 | 142 | Str.putBE(out[4..6], U64(LEN(data))); |
| @@ -178,8 +179,8 @@ | ||
| 178 | 179 | import Out, Str, Math, Fmt; |
| 179 | 180 | |
| 180 | 181 | (* the sum of the numbers in a comma separated list, or -1 if any isn't *) |
| 181 | -proc total(s: []U8): I64; | |
| 182 | -var part: []U8; sum, n: I64; ok, more: BOOL; | |
| 182 | +proc total(s: []const U8): I64; | |
| 183 | +var part: []const U8; sum, n: I64; ok, more: BOOL; | |
| 183 | 184 | begin |
| 184 | 185 | more := true; |
| 185 | 186 | while more do |
| @@ -59,7 +59,7 @@ | ||
| 59 | 59 | |
| 60 | 60 | ```raz Ages run |
| 61 | 61 | module Ages; |
| 62 | -import Out, Fmt, Str, Heap, Book := Map([]U8, INT, Str.hash, Str.eq); | |
| 62 | +import Out, Fmt, Str, Heap, Book := Map([]const U8, INT, Str.hash, Str.eq); | |
| 63 | 63 | |
| 64 | 64 | proc main*; |
| 65 | 65 | var m: Book.Map; age: INT; found, ok: BOOL; i: INT; v: INT; |
| @@ -100,11 +100,11 @@ | ||
| 100 | 100 | ```raz Freq run |
| 101 | 101 | module Freq; |
| 102 | 102 | import Fmt, Str, Heap, |
| 103 | - Count := Map([]U8, INT, Str.hash, Str.eq), | |
| 103 | + Count := Map([]const U8, INT, Str.hash, Str.eq), | |
| 104 | 104 | Entries := Vec(Entry), |
| 105 | 105 | Rank := Sort(Entry, before); |
| 106 | 106 | |
| 107 | -type Entry = record word: []U8; n: INT; end; | |
| 107 | +type Entry = record word: []const U8; n: INT; end; | |
| 108 | 108 | |
| 109 | 109 | const Text = "the cat and the hat and the bat sat on the mat and the cat sat"; |
| 110 | 110 | |
| @@ -118,7 +118,7 @@ | ||
| 118 | 118 | proc main*; |
| 119 | 119 | var |
| 120 | 120 | m: Count.Map; entries: Entries.Vec; i, k, found: INT; |
| 121 | - w, rest: []U8; key: []U8; v: INT; all: []Entry; | |
| 121 | + w, rest: []const U8; key: []const U8; v: INT; all: []Entry; | |
| 122 | 122 | begin |
| 123 | 123 | Count.init(m, Heap.heap); |
| 124 | 124 | defer Count.done(m); |
| @@ -42,7 +42,7 @@ | ||
| 42 | 42 | import Out, Sys; |
| 43 | 43 | |
| 44 | 44 | (* the lines of the file at path, or minus an errno *) |
| 45 | -proc lines(path: []U8): INT; | |
| 45 | +proc lines(path: []const U8): INT; | |
| 46 | 46 | var fd, n, i, count: INT; buf: [4096]U8; |
| 47 | 47 | begin |
| 48 | 48 | fd := Sys.openRead(path); |
| @@ -85,6 +85,7 @@ | ||
| 85 | 85 | ```raz Tree run |
| 86 | 86 | module Tree; |
| 87 | 87 | import Out, Sys, Str, Heap, Mem, Buf, Order := Sort([]U8, before); |
| 88 | +import SYSTEM; | |
| 88 | 89 | |
| 89 | 90 | proc before(a, b: []U8): BOOL; |
| 90 | 91 | begin |
| @@ -92,7 +93,7 @@ | ||
| 92 | 93 | end; |
| 93 | 94 | |
| 94 | 95 | (* a file made to hold text *) |
| 95 | -proc put(path, text: []U8); | |
| 96 | +proc put(path, text: []const U8); | |
| 96 | 97 | var fd: INT; |
| 97 | 98 | begin |
| 98 | 99 | fd := Sys.create(path); |
| @@ -102,7 +103,7 @@ | ||
| 102 | 103 | |
| 103 | 104 | proc main*; |
| 104 | 105 | var |
| 105 | - d: Sys.Dir; name, copy: []U8; more, ok: BOOL; i, n: INT; | |
| 106 | + d: Sys.Dir; name: []const U8; copy: []U8; more, ok: BOOL; i, n: INT; | |
| 106 | 107 | info: Sys.Info; err: INT; names: [16][]U8; path: Buf.Buf; |
| 107 | 108 | begin |
| 108 | 109 | _ := Sys.makeDir("proj"); |
| @@ -131,7 +132,7 @@ | ||
| 131 | 132 | else Out.str(", "); Out.int(info.size); Out.str(" bytes"); |
| 132 | 133 | end; |
| 133 | 134 | Out.ln; |
| 134 | - Mem.free(Heap.heap, ^U8(ADR(names[i][0]))); | |
| 135 | + Mem.free(Heap.heap, ^U8(SYSTEM.ADR(names[i][0]))); | |
| 135 | 136 | end; |
| 136 | 137 | Buf.done(path); |
| 137 | 138 | end; |
| @@ -188,7 +189,7 @@ | ||
| 188 | 189 | const count = 1; (* the key of --count, which has no letter *) |
| 189 | 190 | |
| 190 | 191 | proc main*; |
| 191 | -var p: Opt.Parser; verbose: BOOL; n, i: INT; w: []U8; ok: BOOL; | |
| 192 | +var p: Opt.Parser; verbose: BOOL; n, i: INT; w: []const U8; ok: BOOL; | |
| 192 | 193 | begin |
| 193 | 194 | n := 1; |
| 194 | 195 | Opt.init(p, "tool", [ |
| @@ -22,6 +22,7 @@ | ||
| 22 | 22 | ```raz Bump |
| 23 | 23 | module Bump; |
| 24 | 24 | import Mem; |
| 25 | +import SYSTEM; | |
| 25 | 26 | |
| 26 | 27 | type |
| 27 | 28 | Bump* = record |
| @@ -65,6 +66,7 @@ | ||
| 65 | 66 | ```raz Bumping run |
| 66 | 67 | module Bumping; |
| 67 | 68 | import Out, Bump; |
| 69 | +import SYSTEM; | |
| 68 | 70 | |
| 69 | 71 | type Point = record x, y: INT; end; |
| 70 | 72 | |
| @@ -73,19 +75,19 @@ | ||
| 73 | 75 | proc main*; |
| 74 | 76 | var b: Bump.Bump; p, q: ^Point; many: INT; r: ^Point; |
| 75 | 77 | begin |
| 76 | - Bump.init(ADR(b), ADR(arena[0]), 256); | |
| 77 | - NEW(p, ADR(b.base)); | |
| 78 | - NEW(q, ADR(b.base)); | |
| 78 | + Bump.init(SYSTEM.ADR(b), SYSTEM.ADR(arena[0]), 256); | |
| 79 | + NEW(p, SYSTEM.ADR(b.base)); | |
| 80 | + NEW(q, SYSTEM.ADR(b.base)); | |
| 79 | 81 | p^.x := 1; q^.y := 2; |
| 80 | 82 | Out.int(b.used); Out.str(" bytes used by two points\n"); |
| 81 | 83 | many := 2; |
| 82 | 84 | repeat |
| 83 | - NEW(r, ADR(b.base)); | |
| 85 | + NEW(r, SYSTEM.ADR(b.base)); | |
| 84 | 86 | if r # nil then INC(many); end; |
| 85 | 87 | until r = nil; |
| 86 | 88 | Out.int(many); Out.str(" points fit in 256 bytes\n"); |
| 87 | - Bump.reset(ADR(b)); | |
| 88 | - NEW(r, ADR(b.base)); | |
| 89 | + Bump.reset(SYSTEM.ADR(b)); | |
| 90 | + NEW(r, SYSTEM.ADR(b.base)); | |
| 89 | 91 | if r # nil then Out.str("room again after a reset\n"); end; |
| 90 | 92 | end; |
| 91 | 93 | |
| @@ -101,6 +103,7 @@ | ||
| 101 | 103 | ```raz Counted |
| 102 | 104 | module Counted; |
| 103 | 105 | import Mem; |
| 106 | +import SYSTEM; | |
| 104 | 107 | |
| 105 | 108 | type |
| 106 | 109 | Counted* = record |
| @@ -141,26 +144,27 @@ | ||
| 141 | 144 | |
| 142 | 145 | ```raz Audit run |
| 143 | 146 | module Audit; |
| 144 | -import Fmt, Heap, Mem, Buf, Counted, Names := List([]U8); | |
| 147 | +import Fmt, Heap, Mem, Buf, Counted, Names := List([]const U8); | |
| 148 | +import SYSTEM; | |
| 145 | 149 | |
| 146 | 150 | proc main*; |
| 147 | 151 | var c: Counted.Counted; b: Buf.Buf; l: Names.List; i: INT; p: ^U8; |
| 148 | 152 | begin |
| 149 | - Counted.init(ADR(c), Heap.heap); | |
| 150 | - Buf.init(b, ADR(c.base)); | |
| 153 | + Counted.init(SYSTEM.ADR(c), Heap.heap); | |
| 154 | + Buf.init(b, SYSTEM.ADR(c.base)); | |
| 151 | 155 | for i := 1 to 200 do Buf.str(b, "0123456789"); end; |
| 152 | 156 | Fmt.print("a buffer of {} bytes took {} allocations\n", |
| 153 | 157 | [Fmt.int(b.len), Fmt.int(c.allocs)]); |
| 154 | 158 | Buf.done(b); |
| 155 | 159 | |
| 156 | - Names.init(l, ADR(c.base)); | |
| 160 | + Names.init(l, SYSTEM.ADR(c.base)); | |
| 157 | 161 | for i := 1 to 10 do _ := Names.push(l, "x"); end; |
| 158 | 162 | Fmt.print("{} allocations in all, {} frees so far\n", [Fmt.int(c.allocs), Fmt.int(c.frees)]); |
| 159 | 163 | Names.clear(l); |
| 160 | 164 | Fmt.print("{} frees after clear\n", [Fmt.int(c.frees)]); |
| 161 | 165 | |
| 162 | - p := Mem.alloc(ADR(c.base), 64, 8); | |
| 163 | - Mem.free(ADR(c.base), p); | |
| 166 | + p := Mem.alloc(SYSTEM.ADR(c.base), 64, 8); | |
| 167 | + Mem.free(SYSTEM.ADR(c.base), p); | |
| 164 | 168 | end; |
| 165 | 169 | |
| 166 | 170 | end Audit. |
| @@ -10,12 +10,13 @@ | ||
| 10 | 10 | |
| 11 | 11 | ```raz Bare |
| 12 | 12 | module Bare; |
| 13 | +import SYSTEM; | |
| 13 | 14 | |
| 14 | 15 | const Msg = "hello from raw system calls\n"; |
| 15 | 16 | |
| 16 | 17 | proc main*; |
| 17 | 18 | begin |
| 18 | - _ := SYSCALL(1, 1, ADR(Msg[0]), LEN(Msg)); | |
| 19 | + _ := SYSTEM.SYSCALL(1, 1, SYSTEM.ADR(Msg[0]), LEN(Msg)); | |
| 19 | 20 | end; |
| 20 | 21 | |
| 21 | 22 | end Bare. |
| @@ -19,13 +19,14 @@ | ||
| 19 | 19 | ```raz Hello |
| 20 | 20 | module Hello; |
| 21 | 21 | import Out, Thread; |
| 22 | +import SYSTEM; | |
| 22 | 23 | |
| 23 | 24 | var done: INT; |
| 24 | 25 | |
| 25 | 26 | proc greet(arg: ^U8); |
| 26 | 27 | begin |
| 27 | 28 | Out.str("hello from thread "); Out.int(INT(arg)); Out.ln; |
| 28 | - _ := XADD(ADR(done), 1); | |
| 29 | + _ := XADD(done, 1); | |
| 29 | 30 | end; |
| 30 | 31 | |
| 31 | 32 | proc main*; |
| @@ -19,7 +19,7 @@ | ||
| 19 | 19 | |
| 20 | 20 | var steps*: INT; |
| 21 | 21 | |
| 22 | -proc note(what: []U8); | |
| 22 | +proc note(what: []const U8); | |
| 23 | 23 | begin |
| 24 | 24 | INC(steps); |
| 25 | 25 | Out.int(steps); Out.str(". "); Out.str(what); Out.ln; |
| @@ -13,6 +13,7 @@ | ||
| 13 | 13 | ```raz Cycles run |
| 14 | 14 | module Cycles; |
| 15 | 15 | import Out; |
| 16 | +import SYSTEM; | |
| 16 | 17 | |
| 17 | 18 | proc rdtsc*: U64; |
| 18 | 19 | code |
| @@ -50,6 +51,7 @@ | ||
| 50 | 51 | ```raz Bits run |
| 51 | 52 | module Bits; |
| 52 | 53 | import Out; |
| 54 | +import SYSTEM; | |
| 53 | 55 | |
| 54 | 56 | (* the bytes of x in the opposite order: bswap rax, which the assembler |
| 55 | 57 | doesn't know, written as its bytes *) |
| @@ -87,6 +89,7 @@ | ||
| 87 | 89 | ```raz Sum run |
| 88 | 90 | module Sum; |
| 89 | 91 | import Out; |
| 92 | +import SYSTEM; | |
| 90 | 93 | |
| 91 | 94 | (* the sum of the INTs of a *) |
| 92 | 95 | proc sum(a: []INT): INT; |
| @@ -131,9 +134,10 @@ | ||
| 131 | 134 | ```raz Trim run |
| 132 | 135 | module Trim; |
| 133 | 136 | import Out; |
| 137 | +import SYSTEM; | |
| 134 | 138 | |
| 135 | 139 | (* s past its leading blanks *) |
| 136 | -proc trim(s: []U8): []U8; | |
| 140 | +proc trim(s: []const U8): []const U8; | |
| 137 | 141 | code |
| 138 | 142 | mov rax, rdi; |
| 139 | 143 | mov rdx, rsi; |
| @@ -168,6 +172,7 @@ | ||
| 168 | 172 | ```raz Divmod run |
| 169 | 173 | module Divmod; |
| 170 | 174 | import Out, Fmt; |
| 175 | +import SYSTEM; | |
| 171 | 176 | |
| 172 | 177 | (* quotient and remainder, as idiv gives them: rax and rdx *) |
| 173 | 178 | proc divmod(a, b: INT): (INT, INT); |
| @@ -206,6 +211,7 @@ | ||
| 206 | 211 | ```raz Vendor run |
| 207 | 212 | module Vendor; |
| 208 | 213 | import Out; |
| 214 | +import SYSTEM; | |
| 209 | 215 | |
| 210 | 216 | (* the 12 bytes of the processor vendor, in buf (rdi) *) |
| 211 | 217 | proc vendor(buf: ^U8); |
| @@ -224,7 +230,7 @@ | ||
| 224 | 230 | proc main*; |
| 225 | 231 | var buf: [12]U8; i: INT; ok: BOOL; |
| 226 | 232 | begin |
| 227 | - vendor(ADR(buf[0])); | |
| 233 | + vendor(SYSTEM.ADR(buf[0])); | |
| 228 | 234 | ok := true; |
| 229 | 235 | for i := 0 to 11 do |
| 230 | 236 | if (buf[i] < ' ') or (buf[i] > '~') then ok := false; end; |
| @@ -2,7 +2,7 @@ | ||
| 2 | 2 | part: The machine |
| 3 | 3 | summary: SYSCALL directly: the numbers, the arguments, negative errno, and a cat without a library. |
| 4 | 4 | --- |
| 5 | -Under `Sys` there is one built-in: `SYSCALL(n, x1, ..., x6)`. It makes Linux system call number `n` with up to six arguments, each a pointer or an integer no wider than `INT`, and gives the raw result, an `INT`. A negative result is `-errno`. That is all the interface there is; `Sys.write`, `Sys.open` and the rest of `lib/Sys.linux-x86_64.raz` are written with it, in raz, in a few lines each. | |
| 5 | +Under `Sys` there is one built-in, `SYSTEM.SYSCALL(n, x1, ..., x6)`, which a module reaches with `import SYSTEM;`. It makes Linux system call number `n` with up to six arguments, each a pointer or an integer no wider than `INT`, and gives the raw result, an `INT`. A negative result is `-errno`. That is all the interface there is; `Sys.write`, `Sys.open` and the rest of `lib/Sys.linux-x86_64.raz` are written with it, in raz, in a few lines each. | |
| 6 | 6 | |
| 7 | 7 | `SYSCALL` exists only on Linux targets. Other systems have no stable system call interface, which is why their `Sys` calls the C library instead. |
| 8 | 8 | |
| @@ -12,14 +12,15 @@ | ||
| 12 | 12 | |
| 13 | 13 | ```raz Raw |
| 14 | 14 | module Raw; |
| 15 | +import SYSTEM; | |
| 15 | 16 | |
| 16 | 17 | const Msg = "straight to the kernel\n"; |
| 17 | 18 | |
| 18 | 19 | proc main*; |
| 19 | 20 | var n: INT; |
| 20 | 21 | begin |
| 21 | - n := SYSCALL(1, 1, ADR(Msg[0]), LEN(Msg)); | |
| 22 | - if n # LEN(Msg) then _ := SYSCALL(60, 1); end; (* exit(1), if it came up short *) | |
| 22 | + n := SYSTEM.SYSCALL(1, 1, SYSTEM.ADR(Msg[0]), LEN(Msg)); | |
| 23 | + if n # LEN(Msg) then _ := SYSTEM.SYSCALL(60, 1); end; (* exit(1), if it came up short *) | |
| 23 | 24 | end; |
| 24 | 25 | |
| 25 | 26 | end Raw. |
| @@ -39,18 +40,19 @@ | ||
| 39 | 40 | ```raz Open run |
| 40 | 41 | module Open; |
| 41 | 42 | import Out; |
| 43 | +import SYSTEM; | |
| 42 | 44 | |
| 43 | 45 | proc main*; |
| 44 | 46 | var path: [32]U8; fd: INT; |
| 45 | 47 | begin |
| 46 | 48 | path := "/no/such/file"; |
| 47 | - fd := SYSCALL(2, ADR(path[0]), 0, 0); (* open(path, O_RDONLY) *) | |
| 49 | + fd := SYSTEM.SYSCALL(2, SYSTEM.ADR(path[0]), 0, 0); (* open(path, O_RDONLY) *) | |
| 48 | 50 | Out.str("open gave "); Out.int(fd); Out.ln; (* -2: ENOENT *) |
| 49 | 51 | path := "/dev/null"; |
| 50 | - fd := SYSCALL(2, ADR(path[0]), 0, 0); | |
| 52 | + fd := SYSTEM.SYSCALL(2, SYSTEM.ADR(path[0]), 0, 0); | |
| 51 | 53 | if fd >= 0 then |
| 52 | 54 | Out.str("opened /dev/null\n"); |
| 53 | - _ := SYSCALL(3, fd); (* close *) | |
| 55 | + _ := SYSTEM.SYSCALL(3, fd); (* close *) | |
| 54 | 56 | end; |
| 55 | 57 | end; |
| 56 | 58 | |
| @@ -61,14 +63,15 @@ | ||
| 61 | 63 | |
| 62 | 64 | ## A call that fills a structure |
| 63 | 65 | |
| 64 | -`uname` (call 63) fills a structure of six 65-byte fields: the system name, node name, release, version, machine and domain. It is not a type raz knows; it is an array of bytes, with a `ADR` of it given to the kernel. | |
| 66 | +`uname` (call 63) fills a structure of six 65-byte fields: the system name, node name, release, version, machine and domain. It is not a type raz knows; it is an array of bytes, with its `SYSTEM.ADR` given to the kernel. | |
| 65 | 67 | |
| 66 | 68 | ```raz Who run |
| 67 | 69 | module Who; |
| 68 | 70 | import Out; |
| 71 | +import SYSTEM; | |
| 69 | 72 | |
| 70 | 73 | (* the length of the zero-ended string at the start of s *) |
| 71 | -proc cstr(s: []U8): INT; | |
| 74 | +proc cstr(s: []const U8): INT; | |
| 72 | 75 | var n: INT; |
| 73 | 76 | begin |
| 74 | 77 | n := 0; |
| @@ -79,7 +82,7 @@ | ||
| 79 | 82 | proc main*; |
| 80 | 83 | var u: [390]U8; |
| 81 | 84 | begin |
| 82 | - if SYSCALL(63, ADR(u[0])) = 0 then | |
| 85 | + if SYSTEM.SYSCALL(63, SYSTEM.ADR(u[0])) = 0 then | |
| 83 | 86 | Out.str("system: "); Out.str(u[0..cstr(u[0..65])]); Out.ln; |
| 84 | 87 | Out.str("machine: "); Out.str(u[260..260 + cstr(u[260..325])]); Out.ln; |
| 85 | 88 | end; |
| @@ -95,18 +98,19 @@ | ||
| 95 | 98 | ```raz Pages run |
| 96 | 99 | module Pages; |
| 97 | 100 | import Out; |
| 101 | +import SYSTEM; | |
| 98 | 102 | |
| 99 | 103 | proc main*; |
| 100 | 104 | var p: ^U8; i: INT; total: INT; |
| 101 | 105 | begin |
| 102 | 106 | (* length 4096; PROT_READ|WRITE = 3; MAP_PRIVATE|ANONYMOUS = 22H; no fd *) |
| 103 | - p := ^U8(SYSCALL(9, 0, 4096, 3, 22H, -1, 0)); | |
| 107 | + p := ^U8(SYSTEM.SYSCALL(9, 0, 4096, 3, 22H, -1, 0)); | |
| 104 | 108 | if INT(p) < 0 then Out.str("no memory\n"); return; end; |
| 105 | 109 | Out.int(INT(p[0]) + p[4095]); Out.str(" (zero, as promised)\n"); |
| 106 | 110 | for i := 0 to 4095 do p[i] := U8(i); end; |
| 107 | 111 | for i := 0 to 4095 do INC(total, p[i]); end; |
| 108 | 112 | Out.int(total); Out.ln; |
| 109 | - _ := SYSCALL(11, p, 4096); | |
| 113 | + _ := SYSTEM.SYSCALL(11, p, 4096); | |
| 110 | 114 | end; |
| 111 | 115 | |
| 112 | 116 | end Pages. |
| @@ -118,19 +122,20 @@ | ||
| 118 | 122 | |
| 119 | 123 | ```raz Cat |
| 120 | 124 | module Cat; |
| 125 | +import SYSTEM; | |
| 121 | 126 | |
| 122 | 127 | proc main*: INT; |
| 123 | 128 | var buf: [4096]U8; n, wrote, at: INT; |
| 124 | 129 | begin |
| 125 | - n := SYSCALL(0, 0, ADR(buf[0]), LEN(buf)); | |
| 130 | + n := SYSTEM.SYSCALL(0, 0, SYSTEM.ADR(buf[0]), LEN(buf)); | |
| 126 | 131 | while n > 0 do |
| 127 | 132 | at := 0; |
| 128 | 133 | while at < n do (* a write may be partial *) |
| 129 | - wrote := SYSCALL(1, 1, ADR(buf[at]), n - at); | |
| 134 | + wrote := SYSTEM.SYSCALL(1, 1, SYSTEM.ADR(buf[at]), n - at); | |
| 130 | 135 | if wrote < 0 then return 1; end; |
| 131 | 136 | INC(at, wrote); |
| 132 | 137 | end; |
| 133 | - n := SYSCALL(0, 0, ADR(buf[0]), LEN(buf)); | |
| 138 | + n := SYSTEM.SYSCALL(0, 0, SYSTEM.ADR(buf[0]), LEN(buf)); | |
| 134 | 139 | end; |
| 135 | 140 | if n < 0 then return 1; end; |
| 136 | 141 | return 0; |
| @@ -155,6 +160,7 @@ | ||
| 155 | 160 | ```raz Ppid run |
| 156 | 161 | module Ppid; |
| 157 | 162 | import Out; |
| 163 | +import SYSTEM; | |
| 158 | 164 | |
| 159 | 165 | (* getppid, call 110: no arguments *) |
| 160 | 166 | proc getppid: INT; |
| @@ -166,7 +172,7 @@ | ||
| 166 | 172 | |
| 167 | 173 | proc main*; |
| 168 | 174 | begin |
| 169 | - if (getppid() > 0) and (SYSCALL(110) = getppid()) then | |
| 175 | + if (getppid() > 0) and (SYSTEM.SYSCALL(110) = getppid()) then | |
| 170 | 176 | Out.str("two ways to ask, one answer\n"); |
| 171 | 177 | end; |
| 172 | 178 | end; |
| @@ -13,6 +13,7 @@ | ||
| 13 | 13 | ```raz Uart run |
| 14 | 14 | module Uart; |
| 15 | 15 | import Out; |
| 16 | +import SYSTEM; | |
| 16 | 17 | |
| 17 | 18 | type |
| 18 | 19 | Regs = record |
| @@ -26,7 +27,7 @@ | ||
| 26 | 27 | proc main*; |
| 27 | 28 | var dev: ^VOLATILE Regs; i: INT; |
| 28 | 29 | begin |
| 29 | - dev := ^VOLATILE Regs(ADR(page)); (* the conversion is written *) | |
| 30 | + dev := ^VOLATILE Regs(SYSTEM.ADR(page)); (* the conversion is written *) | |
| 30 | 31 | dev^.control := 1; |
| 31 | 32 | dev^.status := 0FFH; |
| 32 | 33 | for i := 0 to 2 do |
| @@ -60,6 +61,7 @@ | ||
| 60 | 61 | ```raz Poll run |
| 61 | 62 | module Poll; |
| 62 | 63 | import Out, Sys, Thread; |
| 64 | +import SYSTEM; | |
| 63 | 65 | |
| 64 | 66 | type Dev = record status: INT; data: INT; end; |
| 65 | 67 | |
| @@ -68,7 +70,7 @@ | ||
| 68 | 70 | proc device(_arg: ^U8); |
| 69 | 71 | var d: ^VOLATILE Dev; |
| 70 | 72 | begin |
| 71 | - d := ^VOLATILE Dev(ADR(dev)); | |
| 73 | + d := ^VOLATILE Dev(SYSTEM.ADR(dev)); | |
| 72 | 74 | Sys.sleep(20); |
| 73 | 75 | d^.data := 42; |
| 74 | 76 | FENCE; (* data before the flag that announces it *) |
| @@ -78,7 +80,7 @@ | ||
| 78 | 80 | proc main*; |
| 79 | 81 | var t: ^Thread.Thread; err: ERROR; d: ^VOLATILE Dev; spins: INT; |
| 80 | 82 | begin |
| 81 | - d := ^VOLATILE Dev(ADR(dev)); | |
| 83 | + d := ^VOLATILE Dev(SYSTEM.ADR(dev)); | |
| 82 | 84 | t, err := Thread.spawn(device, nil); |
| 83 | 85 | if err # nil then Out.str(MSG(err)); Out.ln; return; end; |
| 84 | 86 | while d^.status = 0 do INC(spins); Sys.yield; end; |
| @@ -103,6 +105,7 @@ | ||
| 103 | 105 | |
| 104 | 106 | ```raz Wrong |
| 105 | 107 | module Wrong; |
| 108 | +import SYSTEM; | |
| 106 | 109 | |
| 107 | 110 | type Pair = record a, b: U32; end; |
| 108 | 111 | |
| @@ -111,7 +114,7 @@ | ||
| 111 | 114 | proc main*; |
| 112 | 115 | var v: ^VOLATILE Pair; p: ^Pair; copy: Pair; |
| 113 | 116 | begin |
| 114 | - v := ^VOLATILE Pair(ADR(cell)); | |
| 117 | + v := ^VOLATILE Pair(SYSTEM.ADR(cell)); | |
| 115 | 118 | p := v; |
| 116 | 119 | copy := v^; |
| 117 | 120 | end; |
| @@ -6,22 +6,23 @@ | ||
| 6 | 6 | |
| 7 | 7 | | Call | Does | |
| 8 | 8 | |---|---| |
| 9 | -| `XADD(p, x)` | adds `x` to `p^` and returns the value before | | |
| 10 | -| `XCHG(p, x)` | sets `p^ := x` and returns the value before | | |
| 11 | -| `CAS(p, old, new)` | if `p^ = old`, sets `p^ := new` and returns `true`; otherwise `false` | | |
| 9 | +| `XADD(p, x)` | adds `x` to `p` and returns the value before | | |
| 10 | +| `XCHG(p, x)` | sets `p := x` and returns the value before | | |
| 11 | +| `CAS(p, old, new)` | if `p = old`, sets `p := new` and returns `true`; otherwise `false` | | |
| 12 | 12 | |
| 13 | -`p` is a `^T` or `^VOLATILE T` where `T` is `INT`, `UINT` or a pointer type (`INT` or `UINT` for `XADD`). They compile to single x86-64 instructions: `lock xadd`, `xchg` (which is always locked when it touches memory) and `lock cmpxchg`: | |
| 13 | +`p` is a variable (or `q^` through a pointer `q`, even `^VOLATILE`) of type `INT`, `UINT` or a pointer type (`INT` or `UINT` for `XADD`); the atomic works on the variable itself, so no `ADR` is taken. A module that uses them says `import SYSTEM;`. They compile to single x86-64 instructions: `lock xadd`, `xchg` (which is always locked when it touches memory) and `lock cmpxchg`: | |
| 14 | 14 | |
| 15 | 15 | ```raz Ops |
| 16 | 16 | module Ops; |
| 17 | +import SYSTEM; | |
| 17 | 18 | |
| 18 | 19 | var counter: INT; |
| 19 | 20 | |
| 20 | 21 | proc main*; |
| 21 | 22 | begin |
| 22 | - _ := XADD(ADR(counter), 5); | |
| 23 | - _ := XCHG(ADR(counter), 100); | |
| 24 | - _ := CAS(ADR(counter), 100, 7); | |
| 23 | + _ := XADD(counter, 5); | |
| 24 | + _ := XCHG(counter, 100); | |
| 25 | + _ := CAS(counter, 100, 7); | |
| 25 | 26 | end; |
| 26 | 27 | |
| 27 | 28 | end Ops. |
| @@ -38,6 +39,7 @@ | ||
| 38 | 39 | ```raz Tickets run |
| 39 | 40 | module Tickets; |
| 40 | 41 | import Out, Thread; |
| 42 | +import SYSTEM; | |
| 41 | 43 | |
| 42 | 44 | var |
| 43 | 45 | next: INT; (* the next ticket to give out *) |
| @@ -47,7 +49,7 @@ | ||
| 47 | 49 | var i, t: INT; |
| 48 | 50 | begin |
| 49 | 51 | for i := 1 to 1000 do |
| 50 | - t := XADD(ADR(next), 1); (* a ticket no other thread gets *) | |
| 52 | + t := XADD(next, 1); (* a ticket no other thread gets *) | |
| 51 | 53 | seen[t] := 1; |
| 52 | 54 | end; |
| 53 | 55 | end; |
| @@ -76,6 +78,7 @@ | ||
| 76 | 78 | ```raz Peak run |
| 77 | 79 | module Peak; |
| 78 | 80 | import Out, Thread; |
| 81 | +import SYSTEM; | |
| 79 | 82 | |
| 80 | 83 | var peak: INT; |
| 81 | 84 | |
| @@ -85,7 +88,7 @@ | ||
| 85 | 88 | begin |
| 86 | 89 | seen := peak; |
| 87 | 90 | while seen < v do |
| 88 | - if CAS(ADR(peak), seen, v) then return; end; | |
| 91 | + if CAS(peak, seen, v) then return; end; | |
| 89 | 92 | seen := peak; (* someone changed it: look again *) |
| 90 | 93 | end; |
| 91 | 94 | end; |
| @@ -15,6 +15,7 @@ | ||
| 15 | 15 | ```raz Publish run |
| 16 | 16 | module Publish; |
| 17 | 17 | import Out, Sys, Thread; |
| 18 | +import SYSTEM; | |
| 18 | 19 | |
| 19 | 20 | type Message = record id: INT; text: [32]U8; len: INT; end; |
| 20 | 21 | |
| @@ -28,7 +29,7 @@ | ||
| 28 | 29 | msg.id := 7; |
| 29 | 30 | msg.text := "built before the flag"; |
| 30 | 31 | msg.len := 21; |
| 31 | - _ := XCHG(ADR(ready), 1); (* publish *) | |
| 32 | + _ := XCHG(ready, 1); (* publish *) | |
| 32 | 33 | end; |
| 33 | 34 | |
| 34 | 35 | proc main*; |
| @@ -36,7 +37,7 @@ | ||
| 36 | 37 | begin |
| 37 | 38 | t, err := Thread.spawn(producer, nil); |
| 38 | 39 | if err # nil then Out.str(MSG(err)); Out.ln; return; end; |
| 39 | - while XADD(ADR(ready), 0) = 0 do Sys.yield; end; (* wait for it *) | |
| 40 | + while XADD(ready, 0) = 0 do Sys.yield; end; (* wait for it *) | |
| 40 | 41 | Out.int(msg.id); Out.char(' '); Out.str(msg.text[0..msg.len]); Out.ln; |
| 41 | 42 | Thread.join(t); |
| 42 | 43 | end; |
| @@ -53,6 +54,7 @@ | ||
| 53 | 54 | ```raz Fenced run |
| 54 | 55 | module Fenced; |
| 55 | 56 | import Out, Sys, Thread; |
| 57 | +import SYSTEM; | |
| 56 | 58 | |
| 57 | 59 | var |
| 58 | 60 | data: INT; (* ordinary *) |
| @@ -61,7 +63,7 @@ | ||
| 61 | 63 | proc producer(_arg: ^U8); |
| 62 | 64 | var f: ^VOLATILE INT; |
| 63 | 65 | begin |
| 64 | - f := ^VOLATILE INT(ADR(flag)); | |
| 66 | + f := ^VOLATILE INT(SYSTEM.ADR(flag)); | |
| 65 | 67 | Sys.sleep(10); |
| 66 | 68 | data := 1234; |
| 67 | 69 | FENCE; (* data is written before the flag is *) |
| @@ -71,7 +73,7 @@ | ||
| 71 | 73 | proc main*; |
| 72 | 74 | var t: ^Thread.Thread; err: ERROR; f: ^VOLATILE INT; |
| 73 | 75 | begin |
| 74 | - f := ^VOLATILE INT(ADR(flag)); | |
| 76 | + f := ^VOLATILE INT(SYSTEM.ADR(flag)); | |
| 75 | 77 | t, err := Thread.spawn(producer, nil); |
| 76 | 78 | if err # nil then Out.str(MSG(err)); Out.ln; return; end; |
| 77 | 79 | while f^ = 0 do Sys.yield; end; |
| @@ -11,6 +11,7 @@ | ||
| 11 | 11 | ```raz Treiber run |
| 12 | 12 | module Treiber; |
| 13 | 13 | import Out, Thread; |
| 14 | +import SYSTEM; | |
| 14 | 15 | |
| 15 | 16 | type Node = record next: ^Node; val: INT; end; |
| 16 | 17 | |
| @@ -26,7 +27,7 @@ | ||
| 26 | 27 | begin |
| 27 | 28 | old := top; |
| 28 | 29 | n^.next := old; |
| 29 | - while not CAS(ADR(top), old, n) do | |
| 30 | + while not CAS(top, old, n) do | |
| 30 | 31 | old := top; (* lost the race: look again *) |
| 31 | 32 | n^.next := old; |
| 32 | 33 | end; |
| @@ -38,7 +39,7 @@ | ||
| 38 | 39 | first := INT(arg) * Each; |
| 39 | 40 | for i := 0 to Each - 1 do |
| 40 | 41 | nodes[first + i].val := first + i; |
| 41 | - push(ADR(nodes[first + i])); | |
| 42 | + push(SYSTEM.ADR(nodes[first + i])); | |
| 42 | 43 | end; |
| 43 | 44 | end; |
| 44 | 45 | |
| @@ -75,11 +76,12 @@ | ||
| 75 | 76 | |
| 76 | 77 | - **Never reuse a node while anyone might hold it.** Nodes from an arena that is reset only when all threads are done, as the nodes above come from a fixed array, can't come back as a different node. |
| 77 | 78 | - **Add a counter to the head**, so that A-B-A is A1-B2-A3. That is a word and a counter swapped together, and raz's `CAS` is for one word, so a program packs a small index and a counter into one `UINT`. |
| 78 | -- **Don't pop concurrently.** Many threads push and one thread, at the end, takes everything with a single `XCHG(ADR(top), nil)` and walks the list in peace. A stack used this way for work lists and logs needs no more. | |
| 79 | +- **Don't pop concurrently.** Many threads push and one thread, at the end, takes everything with a single `XCHG(top, nil)` and walks the list in peace. A stack used this way for work lists and logs needs no more. | |
| 79 | 80 | |
| 80 | 81 | ```raz Drain run |
| 81 | 82 | module Drain; |
| 82 | 83 | import Out, Thread; |
| 84 | +import SYSTEM; | |
| 83 | 85 | |
| 84 | 86 | type Node = record next: ^Node; val: INT; end; |
| 85 | 87 | |
| @@ -94,7 +96,7 @@ | ||
| 94 | 96 | begin |
| 95 | 97 | old := top; |
| 96 | 98 | n^.next := old; |
| 97 | - while not CAS(ADR(top), old, n) do | |
| 99 | + while not CAS(top, old, n) do | |
| 98 | 100 | old := top; |
| 99 | 101 | n^.next := old; |
| 100 | 102 | end; |
| @@ -106,7 +108,7 @@ | ||
| 106 | 108 | first := INT(arg) * Each; |
| 107 | 109 | for i := 0 to Each - 1 do |
| 108 | 110 | nodes[first + i].val := 1; |
| 109 | - push(ADR(nodes[first + i])); | |
| 111 | + push(SYSTEM.ADR(nodes[first + i])); | |
| 110 | 112 | end; |
| 111 | 113 | end; |
| 112 | 114 | |
| @@ -118,7 +120,7 @@ | ||
| 118 | 120 | if err # nil then Out.str(MSG(err)); Out.ln; return; end; |
| 119 | 121 | end; |
| 120 | 122 | for i := 0 to Threads - 1 do Thread.join(th[i]); end; |
| 121 | - all := XCHG(ADR(top), nil); (* take the whole stack, atomically *) | |
| 123 | + all := XCHG(top, nil); (* take the whole stack, atomically *) | |
| 122 | 124 | n := all; |
| 123 | 125 | while n # nil do INC(count); n := n^.next; end; |
| 124 | 126 | Out.int(count); Out.str(" taken at once, "); |
| @@ -17,6 +17,7 @@ | ||
| 17 | 17 | ```raz Ring |
| 18 | 18 | module Ring; |
| 19 | 19 | import Sys; |
| 20 | +import SYSTEM; | |
| 20 | 21 | |
| 21 | 22 | const Size* = 64; |
| 22 | 23 | |
| @@ -31,9 +32,9 @@ | ||
| 31 | 32 | var t: INT; |
| 32 | 33 | begin |
| 33 | 34 | t := r^.tail; (* mine: an ordinary read *) |
| 34 | - while t - XADD(ADR(r^.head), 0) = Size do Sys.yield; end; (* full *) | |
| 35 | + while t - XADD(r^.head, 0) = Size do Sys.yield; end; (* full *) | |
| 35 | 36 | r^.slots[t mod Size] := v; |
| 36 | - _ := XCHG(ADR(r^.tail), t + 1); (* publish the slot *) | |
| 37 | + _ := XCHG(r^.tail, t + 1); (* publish the slot *) | |
| 37 | 38 | end; |
| 38 | 39 | |
| 39 | 40 | (* the next value, waiting while the ring is empty *) |
| @@ -41,9 +42,9 @@ | ||
| 41 | 42 | var h, v: INT; |
| 42 | 43 | begin |
| 43 | 44 | h := r^.head; (* mine *) |
| 44 | - while XADD(ADR(r^.tail), 0) = h do Sys.yield; end; (* empty *) | |
| 45 | + while XADD(r^.tail, 0) = h do Sys.yield; end; (* empty *) | |
| 45 | 46 | v := r^.slots[h mod Size]; |
| 46 | - _ := XCHG(ADR(r^.head), h + 1); (* give the slot back *) | |
| 47 | + _ := XCHG(r^.head, h + 1); (* give the slot back *) | |
| 47 | 48 | return v; |
| 48 | 49 | end; |
| 49 | 50 | |
| @@ -57,6 +58,7 @@ | ||
| 57 | 58 | ```raz Pipe run |
| 58 | 59 | module Pipe; |
| 59 | 60 | import Out, Fmt, Ring, Thread; |
| 61 | +import SYSTEM; | |
| 60 | 62 | |
| 61 | 63 | const Count = 100000; |
| 62 | 64 | |
| @@ -67,14 +69,14 @@ | ||
| 67 | 69 | proc producer(_arg: ^U8); |
| 68 | 70 | var i: INT; |
| 69 | 71 | begin |
| 70 | - for i := 1 to Count do Ring.put(ADR(ring), i); end; | |
| 72 | + for i := 1 to Count do Ring.put(SYSTEM.ADR(ring), i); end; | |
| 71 | 73 | end; |
| 72 | 74 | |
| 73 | 75 | proc consumer(_arg: ^U8); |
| 74 | 76 | var i, v: INT; |
| 75 | 77 | begin |
| 76 | 78 | for i := 1 to Count do |
| 77 | - v := Ring.take(ADR(ring)); | |
| 79 | + v := Ring.take(SYSTEM.ADR(ring)); | |
| 78 | 80 | if v # i then INC(outOfOrder); end; |
| 79 | 81 | sum := sum + v; |
| 80 | 82 | end; |
| @@ -80,11 +80,12 @@ | ||
| 80 | 80 | |
| 81 | 81 | ## Arguments |
| 82 | 82 | |
| 83 | -`ARGC()` is the number of command line arguments, with the program's own name first, and `ARGV()` the arguments, as C strings. Most programs read them through `Files.arg` and `Opt`, which the advanced tour shows; here is the raw form. | |
| 83 | +`ARGC()` is the number of command line arguments, with the program's own name first, and `SYSTEM.ARGV()` the arguments, as C strings. Most programs read them through `Files.arg` and `Opt`, which the advanced tour shows; here is the raw form. | |
| 84 | 84 | |
| 85 | 85 | ```raz Args |
| 86 | 86 | module Args; |
| 87 | 87 | import Out; |
| 88 | +import SYSTEM; | |
| 88 | 89 | |
| 89 | 90 | (* The length of a C string. *) |
| 90 | 91 | proc len(s: ^U8): INT; |
| @@ -99,7 +100,7 @@ | ||
| 99 | 100 | var i: INT; s: ^U8; |
| 100 | 101 | begin |
| 101 | 102 | for i := 1 to ARGC() - 1 do |
| 102 | - s := ARGV()[i]; | |
| 103 | + s := SYSTEM.ARGV()[i]; | |
| 103 | 104 | Out.str(s[0..len(s)]); |
| 104 | 105 | Out.ln; |
| 105 | 106 | end; |
| @@ -118,7 +118,7 @@ | ||
| 118 | 118 | const Prime = 100000001B3H; |
| 119 | 119 | |
| 120 | 120 | (* FNV-1a: xor each byte in, then multiply, all modulo 2^64 *) |
| 121 | -proc fnv(s: []U8): U64; | |
| 121 | +proc fnv(s: []const U8): U64; | |
| 122 | 122 | var h: U64; i: INT; |
| 123 | 123 | begin |
| 124 | 124 | h := Offset; |
| @@ -18,7 +18,7 @@ | ||
| 18 | 18 | import Out; |
| 19 | 19 | |
| 20 | 20 | proc main*; |
| 21 | -var i: I32; s: []U8; | |
| 21 | +var i: I32; s: []const U8; | |
| 22 | 22 | begin |
| 23 | 23 | s := "four"; |
| 24 | 24 | i := 3; |
| @@ -141,7 +141,7 @@ | ||
| 141 | 141 | end; |
| 142 | 142 | |
| 143 | 143 | proc main*; |
| 144 | -var ops: [3]Op; names: [3][]U8; i: INT; | |
| 144 | +var ops: [3]Op; names: [3][]const U8; i: INT; | |
| 145 | 145 | begin |
| 146 | 146 | ops := [add, sub, mul]; |
| 147 | 147 | names := ["add", "sub", "mul"]; |
| @@ -92,14 +92,14 @@ | ||
| 92 | 92 | |
| 93 | 93 | ## Strings |
| 94 | 94 | |
| 95 | -A string is a slice of bytes, `[]U8`, in UTF-8, and a string constant's bytes are constant. A `[8]U8` made from one is a copy you can change. Arrays and slices of integers compare with `=` and `#`, so strings do too. `Str` has the rest: ordering, finding, hashing, prefixes and suffixes, trimming, taking apart, ASCII case and kinds of character, UTF-8, and numbers to and from text. | |
| 95 | +A string is a slice of bytes, `[]U8`, in UTF-8, and a string constant is a `[]const U8`: a procedure that only reads a string takes that, and one that writes takes `[]U8`. A `[8]U8` made from one is a copy you can change. Arrays and slices of integers compare with `=` and `#`, so strings do too. `Str` has the rest: ordering, finding, hashing, prefixes and suffixes, trimming, taking apart, ASCII case and kinds of character, UTF-8, and numbers to and from text. | |
| 96 | 96 | |
| 97 | 97 | ```raz Words run |
| 98 | 98 | module Words; |
| 99 | 99 | import Out, Str; |
| 100 | 100 | |
| 101 | 101 | proc main*; |
| 102 | -var s: []U8; n: I64; ok: BOOL; name: [8]U8; | |
| 102 | +var s: []const U8; n: I64; ok: BOOL; name: [8]U8; | |
| 103 | 103 | begin |
| 104 | 104 | s := "hello, world"; |
| 105 | 105 | Out.str(s[7..LEN(s)]); Out.ln; |
| @@ -141,7 +141,7 @@ | ||
| 141 | 141 | const Text = "name = raz\nkind = language\nyear = 2026\n"; |
| 142 | 142 | |
| 143 | 143 | proc main*; |
| 144 | -var line, rest, key, val, w: []U8; more, ok: BOOL; | |
| 144 | +var line, rest, key, val, w: []const U8; more, ok: BOOL; | |
| 145 | 145 | begin |
| 146 | 146 | (* line by line, then each line at its = *) |
| 147 | 147 | line, rest, more := Str.cut(Text, "\n"); |
| @@ -207,7 +207,7 @@ | ||
| 207 | 207 | import Out, Str, Width; |
| 208 | 208 | |
| 209 | 209 | proc main*; |
| 210 | -var s: []U8; c: I32; i, size: INT; bytes: [4]U8; k: INT; | |
| 210 | +var s: []const U8; c: I32; i, size: INT; bytes: [4]U8; k: INT; | |
| 211 | 211 | begin |
| 212 | 212 | s := "caf\u{E9} \u{3BB}"; |
| 213 | 213 | Out.int(LEN(s)); Out.str(" bytes, "); |
| @@ -5,7 +5,7 @@ | ||
| 5 | 5 | ```raz Boxes run |
| 6 | 6 | ``` |
| 7 | 7 | |
| 8 | -A record is laid out as C lays out a struct. `{lo: ..., hi: ...}` builds one, naming its fields; the ones it leaves out are zero. `^Box` is a pointer to a `Box`, `ADR(b)` the address of `b`, and `b^` what a pointer points at. A pointer is never the record: it is `b^.lo`, never `b.lo`, so you always see where the memory is reached through an address. | |
| 8 | +A record is laid out as C lays out a struct. `{lo: ..., hi: ...}` builds one, naming its fields; the ones it leaves out are zero. `^Box` is a pointer to a `Box`, `SYSTEM.ADR(b)` the address of `b`, and `b^` what a pointer points at. A pointer is never the record: it is `b^.lo`, never `b.lo`, so you always see where the memory is reached through an address. | |
| 9 | 9 | |
| 10 | 10 | ## Copies and literals |
| 11 | 11 | |
| @@ -49,7 +49,7 @@ | ||
| 49 | 49 | module Planets; |
| 50 | 50 | import Fmt; |
| 51 | 51 | |
| 52 | -type Planet = record name: []U8; moons: INT; water: BOOL; end; | |
| 52 | +type Planet = record name: []const U8; moons: INT; water: BOOL; end; | |
| 53 | 53 | |
| 54 | 54 | const Inner: []Planet = [ |
| 55 | 55 | {name: "Mercury"}, |
| @@ -95,7 +95,7 @@ | ||
| 95 | 95 | end Layout. |
| 96 | 96 | ``` |
| 97 | 97 | |
| 98 | -A field of a packed record may sit at any address, and is read and written like any other; but it has no address of its own to take with `ADR`. | |
| 98 | +A field of a packed record may sit at any address, and is read and written like any other; but it has no address of its own to take with `SYSTEM.ADR`. | |
| 99 | 99 | |
| 100 | 100 | ## Bytes as numbers and records: views |
| 101 | 101 | |
| @@ -162,13 +162,14 @@ | ||
| 162 | 162 | |
| 163 | 163 | ## Pointers |
| 164 | 164 | |
| 165 | -`ADR(x)` takes the address of a variable, `^T` is a pointer to a `T`, and `p^` is what it points at. A pointer that points nowhere is `nil`, and following it stops the program with a message that names the place, so a forgotten check is found at once instead of corrupting memory. | |
| 165 | +`SYSTEM.ADR(x)` (a module that uses it says `import SYSTEM;`) takes the address of a variable, `^T` is a pointer to a `T`, and `p^` is what it points at. A pointer that points nowhere is `nil`, and following it stops the program with a message that names the place, so a forgotten check is found at once instead of corrupting memory. | |
| 166 | 166 | |
| 167 | 167 | ```raz Pointers run |
| 168 | 168 | module Pointers; |
| 169 | 169 | import Out; |
| 170 | +import SYSTEM; | |
| 170 | 171 | |
| 171 | -type Counter = record hits: INT; name: []U8; end; | |
| 172 | +type Counter = record hits: INT; name: []const U8; end; | |
| 172 | 173 | |
| 173 | 174 | proc hit(c: ^Counter); |
| 174 | 175 | begin |
| @@ -188,12 +189,12 @@ | ||
| 188 | 189 | var c: Counter; x, y: INT; p: ^INT; |
| 189 | 190 | begin |
| 190 | 191 | c.name := "front door"; |
| 191 | - hit(ADR(c)); hit(ADR(c)); hit(nil); | |
| 192 | + hit(SYSTEM.ADR(c)); hit(SYSTEM.ADR(c)); hit(nil); | |
| 192 | 193 | Out.str(c.name); Out.str(": "); Out.int(c.hits); Out.ln; |
| 193 | 194 | x := 1; y := 2; |
| 194 | - swap(ADR(x), ADR(y)); | |
| 195 | + swap(SYSTEM.ADR(x), SYSTEM.ADR(y)); | |
| 195 | 196 | Out.int(x); Out.char(' '); Out.int(y); Out.ln; |
| 196 | - p := ADR(x); | |
| 197 | + p := SYSTEM.ADR(x); | |
| 197 | 198 | p^ := p^ + 40; |
| 198 | 199 | Out.int(x); Out.ln; |
| 199 | 200 | end; |
| @@ -208,6 +209,7 @@ | ||
| 208 | 209 | ```raz Chain run |
| 209 | 210 | module Chain; |
| 210 | 211 | import Out; |
| 212 | +import SYSTEM; | |
| 211 | 213 | |
| 212 | 214 | type Node = record next: ^Node; val: INT; end; |
| 213 | 215 | |
| @@ -218,7 +220,7 @@ | ||
| 218 | 220 | for i := 0 to 3 do |
| 219 | 221 | nodes[i].val := (i + 1) * 10; |
| 220 | 222 | nodes[i].next := head; (* each new one in front *) |
| 221 | - head := ADR(nodes[i]); | |
| 223 | + head := SYSTEM.ADR(nodes[i]); | |
| 222 | 224 | end; |
| 223 | 225 | n := head; |
| 224 | 226 | while n # nil do |
| @@ -233,17 +235,18 @@ | ||
| 233 | 235 | |
| 234 | 236 | ## A slice's pointer |
| 235 | 237 | |
| 236 | -A slice is a pointer and a length, and a record can keep the two apart: an interpreter keeps a string so, and C takes one so. `LEN(s)` is the length. The pointer looks like `ADR(s[0])`, the address of the first element, and that works until the slice is empty: then there is no first element, `s[0]` is out of range, and the program stops. | |
| 238 | +A slice is a pointer and a length, and a record can keep the two apart: an interpreter keeps a string so, and C takes one so. `LEN(s)` is the length. The pointer looks like `SYSTEM.ADR(s[0])`, the address of the first element, and that works until the slice is empty: then there is no first element, `s[0]` is out of range, and the program stops. | |
| 237 | 239 | |
| 238 | 240 | ```raz Empty fail |
| 239 | 241 | module Empty; |
| 240 | 242 | import Out; |
| 243 | +import SYSTEM; | |
| 241 | 244 | |
| 242 | 245 | type Span = record p: ^U8; n: INT; end; |
| 243 | 246 | |
| 244 | -proc spanOf(s: []U8): Span; | |
| 247 | +proc spanOf(s: []const U8): Span; | |
| 245 | 248 | begin |
| 246 | - return {p: ADR(s[0]), n: LEN(s)}; | |
| 249 | + return {p: SYSTEM.ADR(s[0]), n: LEN(s)}; | |
| 247 | 250 | end; |
| 248 | 251 | |
| 249 | 252 | proc main*; |
| @@ -258,9 +261,9 @@ | ||
| 258 | 261 | end Empty. |
| 259 | 262 | ``` |
| 260 | 263 | |
| 261 | -`PTR(s)` is the pointer itself, a `^U8` for a `[]U8`, with no check. For a slice that isn't empty it is `ADR(s[0])`; for an empty one it is what the slice holds, `nil` for one never set. So nothing needs testing first, and `sp.p[0..sp.n]` makes the slice again, empty when `sp.n` is 0, whatever `sp.p` is: | |
| 264 | +`SYSTEM.PTR(s)` is the pointer itself, a `^U8` for a `[]U8`, with no check. For a slice that isn't empty it is `SYSTEM.ADR(s[0])`; for an empty one it is what the slice holds, `nil` for one never set. So nothing needs testing first, and `sp.p[0..sp.n]` makes the slice again, empty when `sp.n` is 0, whatever `sp.p` is: | |
| 262 | 265 | |
| 263 | 266 | ```raz Spans run |
| 264 | 267 | ``` |
| 265 | 268 | |
| 266 | -Of an array, `PTR(a)` is the address of its first element, a `^T` where `ADR(a)` is a `^[n]T`. Reading through the pointer is as unchecked as through any pointer: the length is yours to keep with it. The compiler says so when the two are mixed up: a slice given where a pointer is wanted is an error whose help says to write `PTR(s)` and `LEN(s)`, and a pointer given where a slice is, `p[0..n]`. | |
| 269 | +Of an array, `SYSTEM.PTR(a)` is the address of its first element, a `^T` where `SYSTEM.ADR(a)` is a `^[n]T`. Reading through the pointer is as unchecked as through any pointer: the length is yours to keep with it. The compiler says so when the two are mixed up: a slice given where a pointer is wanted is an error whose help says to write `PTR(s)` and `LEN(s)`, and a pointer given where a slice is, `p[0..n]`. |
| @@ -15,7 +15,7 @@ | ||
| 15 | 15 | |
| 16 | 16 | type Day = (mon, tue, wed, thu, fri, sat, sun); |
| 17 | 17 | |
| 18 | -proc name(d: Day): []U8; | |
| 18 | +proc name(d: Day): []const U8; | |
| 19 | 19 | begin |
| 20 | 20 | match d of |
| 21 | 21 | | mon: return "Monday"; |
| @@ -92,7 +92,7 @@ | ||
| 92 | 92 | number(n: INT); |
| 93 | 93 | plus; |
| 94 | 94 | times; |
| 95 | - word(s: []U8); | |
| 95 | + word(s: []const U8); | |
| 96 | 96 | end; |
| 97 | 97 | |
| 98 | 98 | proc describe(t: Token); |
| @@ -142,7 +142,7 @@ | ||
| 142 | 142 | module Classify; |
| 143 | 143 | import Out; |
| 144 | 144 | |
| 145 | -proc kind(c: I32): []U8; | |
| 145 | +proc kind(c: I32): []const U8; | |
| 146 | 146 | begin |
| 147 | 147 | match c of |
| 148 | 148 | | ' ', '\t', '\n': return "space"; |
| @@ -153,7 +153,7 @@ | ||
| 153 | 153 | end; |
| 154 | 154 | |
| 155 | 155 | proc main*; |
| 156 | -var s: []U8; i: INT; | |
| 156 | +var s: []const U8; i: INT; | |
| 157 | 157 | begin |
| 158 | 158 | s := "x1 + _y"; |
| 159 | 159 | for i := 0 to LEN(s) - 1 do |
| @@ -174,7 +174,7 @@ | ||
| 174 | 174 | |
| 175 | 175 | type Op = (none, add, sub, show); |
| 176 | 176 | |
| 177 | -proc op(word: []U8): Op; | |
| 177 | +proc op(word: []const U8): Op; | |
| 178 | 178 | begin |
| 179 | 179 | match word of |
| 180 | 180 | | "add", "plus", "+": return add; |
| @@ -185,7 +185,7 @@ | ||
| 185 | 185 | end; |
| 186 | 186 | |
| 187 | 187 | proc main*; |
| 188 | -var total: INT; words: [6][]U8; i: INT; | |
| 188 | +var total: INT; words: [6][]const U8; i: INT; | |
| 189 | 189 | begin |
| 190 | 190 | words := ["add", "plus", "sub", "show", "times", "-"]; |
| 191 | 191 | for i := 0 to 5 do |
| @@ -14,6 +14,7 @@ | ||
| 14 | 14 | ```raz Pool run |
| 15 | 15 | module Pool; |
| 16 | 16 | import Out, Mem, Arena; |
| 17 | +import SYSTEM; | |
| 17 | 18 | |
| 18 | 19 | type Item = record id: INT; next: ^Item; end; |
| 19 | 20 | |
| @@ -44,15 +45,15 @@ | ||
| 44 | 45 | var ar: Arena.Arena; small: Arena.Arena; list: ^Item; |
| 45 | 46 | begin |
| 46 | 47 | if not Arena.init(ar, 1000000) then return; end; |
| 47 | - list := make(1000, ADR(ar.base)); | |
| 48 | + list := make(1000, SYSTEM.ADR(ar.base)); | |
| 48 | 49 | Out.int(count(list)); Out.str(" items from a big arena\n"); |
| 49 | 50 | Arena.reset(ar); (* every allocation forgotten *) |
| 50 | - list := make(10, ADR(ar.base)); | |
| 51 | + list := make(10, SYSTEM.ADR(ar.base)); | |
| 51 | 52 | Out.int(count(list)); Out.str(" after a reset\n"); |
| 52 | 53 | Arena.done(ar); |
| 53 | 54 | |
| 54 | 55 | if not Arena.init(small, 4096) then return; end; |
| 55 | - list := make(1000, ADR(small.base)); | |
| 56 | + list := make(1000, SYSTEM.ADR(small.base)); | |
| 56 | 57 | if list = nil then Out.str("a small arena ran out, and said so\n"); end; |
| 57 | 58 | Arena.done(small); |
| 58 | 59 | end; |
| @@ -69,6 +70,7 @@ | ||
| 69 | 70 | ```raz Free run |
| 70 | 71 | module Free; |
| 71 | 72 | import Out, Mem, Heap; |
| 73 | +import SYSTEM; | |
| 72 | 74 | |
| 73 | 75 | type Cell = record val: INT; end; |
| 74 | 76 |
| @@ -21,7 +21,7 @@ | ||
| 21 | 21 | Range*: ERROR = "out of range"; |
| 22 | 22 | |
| 23 | 23 | (* a percentage, 0 to 100 *) |
| 24 | -proc percent(s: []U8): (INT, ERROR); | |
| 24 | +proc percent(s: []const U8): (INT, ERROR); | |
| 25 | 25 | var n: I64; ok: BOOL; |
| 26 | 26 | begin |
| 27 | 27 | if LEN(s) = 0 then return 0, Empty; end; |
| @@ -32,7 +32,7 @@ | ||
| 32 | 32 | end; |
| 33 | 33 | |
| 34 | 34 | proc main*; |
| 35 | -var inputs: [4][]U8; i, n: INT; err: ERROR; | |
| 35 | +var inputs: [4][]const U8; i, n: INT; err: ERROR; | |
| 36 | 36 | begin |
| 37 | 37 | inputs := ["42", "", "forty", "140"]; |
| 38 | 38 | for i := 0 to 3 do |
| @@ -69,7 +69,7 @@ | ||
| 69 | 69 | const Missing*: ERROR = "key missing"; |
| 70 | 70 | |
| 71 | 71 | (* the number after "n=" in a line like "n=17" *) |
| 72 | -proc value(line: []U8): (INT, ERROR); | |
| 72 | +proc value(line: []const U8): (INT, ERROR); | |
| 73 | 73 | var n: I64; ok: BOOL; |
| 74 | 74 | begin |
| 75 | 75 | if not Str.hasPrefix(line, "n=") then return 0, Missing; end; |
| @@ -79,7 +79,7 @@ | ||
| 79 | 79 | end; |
| 80 | 80 | |
| 81 | 81 | (* the sum of the values of two lines: the first error ends it *) |
| 82 | -proc sum(a, b: []U8): (INT, ERROR); | |
| 82 | +proc sum(a, b: []const U8): (INT, ERROR); | |
| 83 | 83 | begin |
| 84 | 84 | return try value(a) + try value(b), nil; |
| 85 | 85 | end; |
| @@ -18,6 +18,7 @@ | ||
| 18 | 18 | ```raz Nilp fail |
| 19 | 19 | module Nilp; |
| 20 | 20 | import Out; |
| 21 | +import SYSTEM; | |
| 21 | 22 | |
| 22 | 23 | type Node = record val: INT; next: ^Node; end; |
| 23 | 24 | |
| @@ -31,8 +32,8 @@ | ||
| 31 | 32 | var a, b: Node; |
| 32 | 33 | begin |
| 33 | 34 | a.val := 1; b.val := 2; |
| 34 | - a.next := ADR(b); | |
| 35 | - Out.int(last(ADR(a))); Out.ln; | |
| 35 | + a.next := SYSTEM.ADR(b); | |
| 36 | + Out.int(last(SYSTEM.ADR(a))); Out.ln; | |
| 36 | 37 | Out.int(last(nil)); Out.ln; |
| 37 | 38 | end; |
| 38 | 39 |
| @@ -21,6 +21,7 @@ | ||
| 21 | 21 | ```raz Stk |
| 22 | 22 | module Stk(T); |
| 23 | 23 | import Mem; |
| 24 | +import SYSTEM; | |
| 24 | 25 | |
| 25 | 26 | type |
| 26 | 27 | Stack* = record |
| @@ -76,21 +77,22 @@ | ||
| 76 | 77 | ```raz Uses run |
| 77 | 78 | module Uses; |
| 78 | 79 | import Out, Heap, Nums := Stk(INT), Pairs := Stk(Pair); |
| 80 | +import SYSTEM; | |
| 79 | 81 | |
| 80 | 82 | type Pair = record a, b: INT; end; |
| 81 | 83 | |
| 82 | 84 | proc main*; |
| 83 | 85 | var n: Nums.Stack; p: Pairs.Stack; i, v: INT; ok: BOOL; pr: Pair; |
| 84 | 86 | begin |
| 85 | - Nums.init(ADR(n), Heap.heap); | |
| 86 | - Pairs.init(ADR(p), Heap.heap); | |
| 87 | - for i := 1 to 10 do _ := Nums.push(ADR(n), i * i); end; | |
| 88 | - _ := Pairs.push(ADR(p), {a: 3, b: 4}); | |
| 89 | - v, ok := Nums.pop(ADR(n)); | |
| 87 | + Nums.init(SYSTEM.ADR(n), Heap.heap); | |
| 88 | + Pairs.init(SYSTEM.ADR(p), Heap.heap); | |
| 89 | + for i := 1 to 10 do _ := Nums.push(SYSTEM.ADR(n), i * i); end; | |
| 90 | + _ := Pairs.push(SYSTEM.ADR(p), {a: 3, b: 4}); | |
| 91 | + v, ok := Nums.pop(SYSTEM.ADR(n)); | |
| 90 | 92 | Out.int(v); Out.str(" of "); Out.int(n.len + 1); Out.ln; |
| 91 | - pr, ok := Pairs.pop(ADR(p)); | |
| 93 | + pr, ok := Pairs.pop(SYSTEM.ADR(p)); | |
| 92 | 94 | Out.int(pr.a * pr.b); Out.ln; |
| 93 | - _, ok := Pairs.pop(ADR(p)); | |
| 95 | + _, ok := Pairs.pop(SYSTEM.ADR(p)); | |
| 94 | 96 | if not ok then Out.str("empty\n"); end; |
| 95 | 97 | end; |
| 96 | 98 | |
| @@ -101,4 +103,4 @@ | ||
| 101 | 103 | |
| 102 | 104 | ## The library's generics |
| 103 | 105 | |
| 104 | -The library's `List`, `Map` and `Sort` are generic modules. A `Map` takes its key hash and equality as procedure arguments: `import Count := Map([]U8, INT, Str.hash, Str.eq);`. The advanced tour uses each of them in a real program. | |
| 106 | +The library's `List`, `Map` and `Sort` are generic modules. A `Map` takes its key hash and equality as procedure arguments: `import Count := Map([]const U8, INT, Str.hash, Str.eq);`. The advanced tour uses each of them in a real program. |
| @@ -18,6 +18,7 @@ | ||
| 18 | 18 | ```raz Say run |
| 19 | 19 | module Say; |
| 20 | 20 | import Out; |
| 21 | +import SYSTEM; | |
| 21 | 22 | |
| 22 | 23 | extern proc printf(format: ^U8; ...): I32; |
| 23 | 24 | extern proc fflush(f: ^U8): I32; |
| @@ -48,6 +49,7 @@ | ||
| 48 | 49 | ```raz Env run |
| 49 | 50 | module Env; |
| 50 | 51 | import Out; |
| 52 | +import SYSTEM; | |
| 51 | 53 | |
| 52 | 54 | extern var environ: ^^U8; |
| 53 | 55 | extern proc strlen(s: ^U8): UINT; |
| @@ -74,6 +76,7 @@ | ||
| 74 | 76 | ```raz Roots run |
| 75 | 77 | module Roots; |
| 76 | 78 | import Fmt; |
| 79 | +import SYSTEM; | |
| 77 | 80 | |
| 78 | 81 | link "libm.so.6"; |
| 79 | 82 |
| @@ -14,6 +14,7 @@ | ||
| 14 | 14 | ```raz Race run |
| 15 | 15 | module Race; |
| 16 | 16 | import Out, Thread; |
| 17 | +import SYSTEM; | |
| 17 | 18 | |
| 18 | 19 | var |
| 19 | 20 | safe: INT; (* added to by XADD *) |
| @@ -23,7 +24,7 @@ | ||
| 23 | 24 | var i: INT; |
| 24 | 25 | begin |
| 25 | 26 | for i := 1 to 100000 do |
| 26 | - _ := XADD(ADR(safe), 1); | |
| 27 | + _ := XADD(safe, 1); | |
| 27 | 28 | INC(plain); (* unsafe: lost additions *) |
| 28 | 29 | end; |
| 29 | 30 | end; |
| @@ -88,6 +89,7 @@ | ||
| 88 | 89 | ```raz Jobs run |
| 89 | 90 | module Jobs; |
| 90 | 91 | import Out, Thread; |
| 92 | +import SYSTEM; | |
| 91 | 93 | |
| 92 | 94 | type Job = record lo, hi: INT; sum: INT; end; |
| 93 | 95 | |
| @@ -104,7 +106,7 @@ | ||
| 104 | 106 | for i := 0 to 3 do |
| 105 | 107 | jobs[i].lo := i * 250000; |
| 106 | 108 | jobs[i].hi := (i + 1) * 250000; |
| 107 | - t[i], err := Thread.spawn(work, ^U8(ADR(jobs[i]))); | |
| 109 | + t[i], err := Thread.spawn(work, ^U8(SYSTEM.ADR(jobs[i]))); | |
| 108 | 110 | if err # nil then Out.str(MSG(err)); Out.ln; return; end; |
| 109 | 111 | end; |
| 110 | 112 | for i := 0 to 3 do |
| @@ -1 +1 @@ | ||
| 1 | -/download/latest /download/raz-0.23.0-linux-x86_64.tar.gz 302 | |
| 1 | +/download/latest /download/raz-0.24.0-linux-x86_64.tar.gz 302 |
| @@ -55,6 +55,7 @@ | ||
| 55 | 55 | <p>The trick that lets one procedure find its state is the same one <code>Arena</code> uses: the record puts <code>base: Mem.Allocator</code> first, so a <code>^Mem.Allocator</code> and a <code>^Bump</code> are the same address, and the procedure converts one to the other.</p> |
| 56 | 56 | <div class="file"><span>Bump.raz</span></div><pre class="code"><code><span class="kw">module</span> Bump; |
| 57 | 57 | <span class="kw">import</span> Mem; |
| 58 | +<span class="kw">import</span> SYSTEM; | |
| 58 | 59 | |
| 59 | 60 | <span class="kw">type</span> |
| 60 | 61 | Bump* = <span class="kw">record</span> |
| @@ -95,6 +96,7 @@ | ||
| 95 | 96 | <span class="kw">end</span> Bump.</code></pre> |
| 96 | 97 | <div class="file"><span>Bumping.raz</span></div><pre class="code"><code><span class="kw">module</span> Bumping; |
| 97 | 98 | <span class="kw">import</span> Out, Bump; |
| 99 | +<span class="kw">import</span> SYSTEM; | |
| 98 | 100 | |
| 99 | 101 | <span class="kw">type</span> Point = <span class="kw">record</span> x, y: <span class="ty">INT</span>; <span class="kw">end</span>; |
| 100 | 102 | |
| @@ -103,19 +105,19 @@ | ||
| 103 | 105 | <span class="kw">proc</span> main*; |
| 104 | 106 | <span class="kw">var</span> b: Bump.Bump; p, q: ^Point; many: <span class="ty">INT</span>; r: ^Point; |
| 105 | 107 | <span class="kw">begin</span> |
| 106 | - Bump.init(<span class="ty">ADR</span>(b), <span class="ty">ADR</span>(arena[<span class="number">0</span>]), <span class="number">256</span>); | |
| 107 | - <span class="ty">NEW</span>(p, <span class="ty">ADR</span>(b.base)); | |
| 108 | - <span class="ty">NEW</span>(q, <span class="ty">ADR</span>(b.base)); | |
| 108 | + Bump.init(SYSTEM.<span class="ty">ADR</span>(b), SYSTEM.<span class="ty">ADR</span>(arena[<span class="number">0</span>]), <span class="number">256</span>); | |
| 109 | + <span class="ty">NEW</span>(p, SYSTEM.<span class="ty">ADR</span>(b.base)); | |
| 110 | + <span class="ty">NEW</span>(q, SYSTEM.<span class="ty">ADR</span>(b.base)); | |
| 109 | 111 | p^.x := <span class="number">1</span>; q^.y := <span class="number">2</span>; |
| 110 | 112 | Out.int(b.used); Out.str(<span class="string">" bytes used by two points\n"</span>); |
| 111 | 113 | many := <span class="number">2</span>; |
| 112 | 114 | <span class="kw">repeat</span> |
| 113 | - <span class="ty">NEW</span>(r, <span class="ty">ADR</span>(b.base)); | |
| 115 | + <span class="ty">NEW</span>(r, SYSTEM.<span class="ty">ADR</span>(b.base)); | |
| 114 | 116 | <span class="kw">if</span> r # <span class="ty">nil</span> <span class="kw">then</span> <span class="ty">INC</span>(many); <span class="kw">end</span>; |
| 115 | 117 | <span class="kw">until</span> r = <span class="ty">nil</span>; |
| 116 | 118 | Out.int(many); Out.str(<span class="string">" points fit in 256 bytes\n"</span>); |
| 117 | - Bump.reset(<span class="ty">ADR</span>(b)); | |
| 118 | - <span class="ty">NEW</span>(r, <span class="ty">ADR</span>(b.base)); | |
| 119 | + Bump.reset(SYSTEM.<span class="ty">ADR</span>(b)); | |
| 120 | + <span class="ty">NEW</span>(r, SYSTEM.<span class="ty">ADR</span>(b.base)); | |
| 119 | 121 | <span class="kw">if</span> r # <span class="ty">nil</span> <span class="kw">then</span> Out.str(<span class="string">"room again after a reset\n"</span>); <span class="kw">end</span>; |
| 120 | 122 | <span class="kw">end</span>; |
| 121 | 123 | |
| @@ -129,6 +131,7 @@ | ||
| 129 | 131 | <p>An allocator can wrap another. This one forwards to whatever it is given and keeps count, which is how to find out what a piece of code allocates, or to prove that a path allocates nothing.</p> |
| 130 | 132 | <div class="file"><span>Counted.raz</span></div><pre class="code"><code><span class="kw">module</span> Counted; |
| 131 | 133 | <span class="kw">import</span> Mem; |
| 134 | +<span class="kw">import</span> SYSTEM; | |
| 132 | 135 | |
| 133 | 136 | <span class="kw">type</span> |
| 134 | 137 | Counted* = <span class="kw">record</span> |
| @@ -166,26 +169,27 @@ | ||
| 166 | 169 | |
| 167 | 170 | <span class="kw">end</span> Counted.</code></pre> |
| 168 | 171 | <div class="file"><span>Audit.raz</span></div><pre class="code"><code><span class="kw">module</span> Audit; |
| 169 | -<span class="kw">import</span> Fmt, Heap, Mem, Buf, Counted, Names := List([]<span class="ty">U8</span>); | |
| 172 | +<span class="kw">import</span> Fmt, Heap, Mem, Buf, Counted, Names := List([]<span class="kw">const</span> <span class="ty">U8</span>); | |
| 173 | +<span class="kw">import</span> SYSTEM; | |
| 170 | 174 | |
| 171 | 175 | <span class="kw">proc</span> main*; |
| 172 | 176 | <span class="kw">var</span> c: Counted.Counted; b: Buf.Buf; l: Names.List; i: <span class="ty">INT</span>; p: ^<span class="ty">U8</span>; |
| 173 | 177 | <span class="kw">begin</span> |
| 174 | - Counted.init(<span class="ty">ADR</span>(c), Heap.heap); | |
| 175 | - Buf.init(b, <span class="ty">ADR</span>(c.base)); | |
| 178 | + Counted.init(SYSTEM.<span class="ty">ADR</span>(c), Heap.heap); | |
| 179 | + Buf.init(b, SYSTEM.<span class="ty">ADR</span>(c.base)); | |
| 176 | 180 | <span class="kw">for</span> i := <span class="number">1</span> <span class="kw">to</span> <span class="number">200</span> <span class="kw">do</span> Buf.str(b, <span class="string">"0123456789"</span>); <span class="kw">end</span>; |
| 177 | 181 | Fmt.print(<span class="string">"a buffer of {} bytes took {} allocations\n"</span>, |
| 178 | 182 | [Fmt.int(b.len), Fmt.int(c.allocs)]); |
| 179 | 183 | Buf.done(b); |
| 180 | 184 | |
| 181 | - Names.init(l, <span class="ty">ADR</span>(c.base)); | |
| 185 | + Names.init(l, SYSTEM.<span class="ty">ADR</span>(c.base)); | |
| 182 | 186 | <span class="kw">for</span> i := <span class="number">1</span> <span class="kw">to</span> <span class="number">10</span> <span class="kw">do</span> _ := Names.push(l, <span class="string">"x"</span>); <span class="kw">end</span>; |
| 183 | 187 | Fmt.print(<span class="string">"{} allocations in all, {} frees so far\n"</span>, [Fmt.int(c.allocs), Fmt.int(c.frees)]); |
| 184 | 188 | Names.clear(l); |
| 185 | 189 | Fmt.print(<span class="string">"{} frees after clear\n"</span>, [Fmt.int(c.frees)]); |
| 186 | 190 | |
| 187 | - p := Mem.alloc(<span class="ty">ADR</span>(c.base), <span class="number">64</span>, <span class="number">8</span>); | |
| 188 | - Mem.free(<span class="ty">ADR</span>(c.base), p); | |
| 191 | + p := Mem.alloc(SYSTEM.<span class="ty">ADR</span>(c.base), <span class="number">64</span>, <span class="number">8</span>); | |
| 192 | + Mem.free(SYSTEM.<span class="ty">ADR</span>(c.base), p); | |
| 189 | 193 | <span class="kw">end</span>; |
| 190 | 194 | |
| 191 | 195 | <span class="kw">end</span> Audit.</code></pre> |
| @@ -50,6 +50,7 @@ | ||
| 50 | 50 | <p>This reads the processor's time stamp counter. The result of a procedure goes in <code>rax</code>; <code>rdtsc</code> leaves the counter in <code>edx:eax</code>, so the two halves are joined first.</p> |
| 51 | 51 | <div class="file"><span>Cycles.raz</span></div><pre class="code"><code><span class="kw">module</span> Cycles; |
| 52 | 52 | <span class="kw">import</span> Out; |
| 53 | +<span class="kw">import</span> SYSTEM; | |
| 53 | 54 | |
| 54 | 55 | <span class="kw">proc</span> rdtsc*: <span class="ty">U64</span>; |
| 55 | 56 | <span class="kw">code</span> |
| @@ -77,6 +78,7 @@ | ||
| 77 | 78 | <p>That is why the next one is so short: the argument is already in <code>rdi</code>.</p> |
| 78 | 79 | <div class="file"><span>Bits.raz</span></div><pre class="code"><code><span class="kw">module</span> Bits; |
| 79 | 80 | <span class="kw">import</span> Out; |
| 81 | +<span class="kw">import</span> SYSTEM; | |
| 80 | 82 | |
| 81 | 83 | <span class="comment">(* the bytes of x in the opposite order: bswap rax, which the assembler |
| 82 | 84 | doesn't know, written as its bytes *)</span> |
| @@ -111,6 +113,7 @@ | ||
| 111 | 113 | <p>A label, <code>name:</code>, names the place of the instruction after it, within the procedure. Jumps go to labels, and a constant of the module may stand wherever a number does. This sums a slice of <code>INT</code>s, whose address arrives in <code>rdi</code> and its length in <code>rsi</code>:</p> |
| 112 | 114 | <div class="file"><span>Sum.raz</span></div><pre class="code"><code><span class="kw">module</span> Sum; |
| 113 | 115 | <span class="kw">import</span> Out; |
| 116 | +<span class="kw">import</span> SYSTEM; | |
| 114 | 117 | |
| 115 | 118 | <span class="comment">(* the sum of the INTs of a *)</span> |
| 116 | 119 | <span class="kw">proc</span> sum(a: []<span class="ty">INT</span>): <span class="ty">INT</span>; |
| @@ -152,9 +155,10 @@ | ||
| 152 | 155 | <p>A slice comes back the same way, its address in <code>rax</code> and its length in <code>rdx</code>. This one returns what is left of a string past its leading blanks:</p> |
| 153 | 156 | <div class="file"><span>Trim.raz</span></div><pre class="code"><code><span class="kw">module</span> Trim; |
| 154 | 157 | <span class="kw">import</span> Out; |
| 158 | +<span class="kw">import</span> SYSTEM; | |
| 155 | 159 | |
| 156 | 160 | <span class="comment">(* s past its leading blanks *)</span> |
| 157 | -<span class="kw">proc</span> trim(s: []<span class="ty">U8</span>): []<span class="ty">U8</span>; | |
| 161 | +<span class="kw">proc</span> trim(s: []<span class="kw">const</span> <span class="ty">U8</span>): []<span class="kw">const</span> <span class="ty">U8</span>; | |
| 158 | 162 | <span class="kw">code</span> |
| 159 | 163 | mov rax, rdi; |
| 160 | 164 | mov rdx, rsi; |
| @@ -187,6 +191,7 @@ | ||
| 187 | 191 | <p>A second result goes in <code>rdx</code>. That is exactly where <code>idiv</code> leaves the remainder, so <code>divmod</code> is four instructions. Reals travel in the <code>xmm</code> registers.</p> |
| 188 | 192 | <div class="file"><span>Divmod.raz</span></div><pre class="code"><code><span class="kw">module</span> Divmod; |
| 189 | 193 | <span class="kw">import</span> Out, Fmt; |
| 194 | +<span class="kw">import</span> SYSTEM; | |
| 190 | 195 | |
| 191 | 196 | <span class="comment">(* quotient and remainder, as idiv gives them: rax and rdx *)</span> |
| 192 | 197 | <span class="kw">proc</span> divmod(a, b: <span class="ty">INT</span>): (<span class="ty">INT</span>, <span class="ty">INT</span>); |
| @@ -222,6 +227,7 @@ | ||
| 222 | 227 | <p>A procedure that uses <code>rbx</code> must put it back. <code>cpuid</code> clobbers <code>rbx</code>, so the vendor-string example saves it with <code>push</code> and restores it with <code>pop</code>, and keeps the buffer's address in <code>r8</code> while it works:</p> |
| 223 | 228 | <div class="file"><span>Vendor.raz</span></div><pre class="code"><code><span class="kw">module</span> Vendor; |
| 224 | 229 | <span class="kw">import</span> Out; |
| 230 | +<span class="kw">import</span> SYSTEM; | |
| 225 | 231 | |
| 226 | 232 | <span class="comment">(* the 12 bytes of the processor vendor, in buf (rdi) *)</span> |
| 227 | 233 | <span class="kw">proc</span> vendor(buf: ^<span class="ty">U8</span>); |
| @@ -240,7 +246,7 @@ | ||
| 240 | 246 | <span class="kw">proc</span> main*; |
| 241 | 247 | <span class="kw">var</span> buf: [<span class="number">12</span>]<span class="ty">U8</span>; i: <span class="ty">INT</span>; ok: <span class="ty">BOOL</span>; |
| 242 | 248 | <span class="kw">begin</span> |
| 243 | - vendor(<span class="ty">ADR</span>(buf[<span class="number">0</span>])); | |
| 249 | + vendor(SYSTEM.<span class="ty">ADR</span>(buf[<span class="number">0</span>])); | |
| 244 | 250 | ok := <span class="ty">true</span>; |
| 245 | 251 | <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="number">11</span> <span class="kw">do</span> |
| 246 | 252 | <span class="kw">if</span> (buf[i] < <span class="string">' '</span>) <span class="kw">or</span> (buf[i] > <span class="string">'~'</span>) <span class="kw">then</span> ok := <span class="ty">false</span>; <span class="kw">end</span>; |
| @@ -45,29 +45,31 @@ | ||
| 45 | 45 | <li><a href="ring.html">A ring buffer between threads</a></li> |
| 46 | 46 | </ol></aside><article><p class="kicker">Shared memory · lesson 15 of 18</p><h1>Atomics</h1> |
| 47 | 47 | <p>A volatile access is one access. An atomic is one *indivisible* step: nothing can happen between its read and its write, so two threads using it can't lose each other's work. There are three, built into the language as procedures, and they are sequentially consistent on every target:</p> |
| 48 | -<table><thead><tr><th>Call</th><th>Does</th></tr></thead><tbody><tr><td data-label="Call"><code>XADD(p, x)</code></td><td data-label="Does">adds <code>x</code> to <code>p^</code> and returns the value before</td></tr><tr><td data-label="Call"><code>XCHG(p, x)</code></td><td data-label="Does">sets <code>p^ := x</code> and returns the value before</td></tr><tr><td data-label="Call"><code>CAS(p, old, new)</code></td><td data-label="Does">if <code>p^ = old</code>, sets <code>p^ := new</code> and returns <code>true</code>; otherwise <code>false</code></td></tr></tbody></table> | |
| 49 | -<p><code>p</code> is a <code>^T</code> or <code>^VOLATILE T</code> where <code>T</code> is <code>INT</code>, <code>UINT</code> or a pointer type (<code>INT</code> or <code>UINT</code> for <code>XADD</code>). They compile to single x86-64 instructions: <code>lock xadd</code>, <code>xchg</code> (which is always locked when it touches memory) and <code>lock cmpxchg</code>:</p> | |
| 48 | +<table><thead><tr><th>Call</th><th>Does</th></tr></thead><tbody><tr><td data-label="Call"><code>XADD(p, x)</code></td><td data-label="Does">adds <code>x</code> to <code>p</code> and returns the value before</td></tr><tr><td data-label="Call"><code>XCHG(p, x)</code></td><td data-label="Does">sets <code>p := x</code> and returns the value before</td></tr><tr><td data-label="Call"><code>CAS(p, old, new)</code></td><td data-label="Does">if <code>p = old</code>, sets <code>p := new</code> and returns <code>true</code>; otherwise <code>false</code></td></tr></tbody></table> | |
| 49 | +<p><code>p</code> is a variable (or <code>q^</code> through a pointer <code>q</code>, even <code>^VOLATILE</code>) of type <code>INT</code>, <code>UINT</code> or a pointer type (<code>INT</code> or <code>UINT</code> for <code>XADD</code>); the atomic works on the variable itself, so no <code>ADR</code> is taken. A module that uses them says <code>import SYSTEM;</code>. They compile to single x86-64 instructions: <code>lock xadd</code>, <code>xchg</code> (which is always locked when it touches memory) and <code>lock cmpxchg</code>:</p> | |
| 50 | 50 | <div class="file"><span>Ops.raz</span></div><pre class="code"><code><span class="kw">module</span> Ops; |
| 51 | +<span class="kw">import</span> SYSTEM; | |
| 51 | 52 | |
| 52 | 53 | <span class="kw">var</span> counter: <span class="ty">INT</span>; |
| 53 | 54 | |
| 54 | 55 | <span class="kw">proc</span> main*; |
| 55 | 56 | <span class="kw">begin</span> |
| 56 | - _ := <span class="ty">XADD</span>(<span class="ty">ADR</span>(counter), <span class="number">5</span>); | |
| 57 | - _ := <span class="ty">XCHG</span>(<span class="ty">ADR</span>(counter), <span class="number">100</span>); | |
| 58 | - _ := <span class="ty">CAS</span>(<span class="ty">ADR</span>(counter), <span class="number">100</span>, <span class="number">7</span>); | |
| 57 | + _ := <span class="ty">XADD</span>(counter, <span class="number">5</span>); | |
| 58 | + _ := <span class="ty">XCHG</span>(counter, <span class="number">100</span>); | |
| 59 | + _ := <span class="ty">CAS</span>(counter, <span class="number">100</span>, <span class="number">7</span>); | |
| 59 | 60 | <span class="kw">end</span>; |
| 60 | 61 | |
| 61 | 62 | <span class="kw">end</span> Ops.</code></pre> |
| 62 | 63 | <pre class="output term"><code><span class="cmd">$ raz -S Ops.raz | grep -i "lock\|xchg"</span> |
| 63 | 64 | 23 lock xadd [r8], rax |
| 64 | - 8 _ := XCHG(ADR(counter), 100); | |
| 65 | + 9 _ := XCHG(counter, 100); | |
| 65 | 66 | 43 xchg [r8], rax |
| 66 | 67 | 67 lock cmpxchg [r8], r10</code></pre> |
| 67 | 68 | <h2>What each one gives you</h2> |
| 68 | 69 | <p><code>XADD</code> is a counter, or a ticket dispenser: every caller gets a different number. <code>XCHG</code> takes the old value and leaves a new one, which is a flag, a swap, or the release of a lock. <code>CAS</code> is the general one: "if nobody changed it since I looked, change it". Most lock-free code is a loop around it.</p> |
| 69 | 70 | <div class="file"><span>Tickets.raz</span></div><pre class="code"><code><span class="kw">module</span> Tickets; |
| 70 | 71 | <span class="kw">import</span> Out, Thread; |
| 72 | +<span class="kw">import</span> SYSTEM; | |
| 71 | 73 | |
| 72 | 74 | <span class="kw">var</span> |
| 73 | 75 | next: <span class="ty">INT</span>; <span class="comment">(* the next ticket to give out *)</span> |
| @@ -77,7 +79,7 @@ | ||
| 77 | 79 | <span class="kw">var</span> i, t: <span class="ty">INT</span>; |
| 78 | 80 | <span class="kw">begin</span> |
| 79 | 81 | <span class="kw">for</span> i := <span class="number">1</span> <span class="kw">to</span> <span class="number">1000</span> <span class="kw">do</span> |
| 80 | - t := <span class="ty">XADD</span>(<span class="ty">ADR</span>(next), <span class="number">1</span>); <span class="comment">(* a ticket no other thread gets *)</span> | |
| 82 | + t := <span class="ty">XADD</span>(next, <span class="number">1</span>); <span class="comment">(* a ticket no other thread gets *)</span> | |
| 81 | 83 | seen[t] := <span class="number">1</span>; |
| 82 | 84 | <span class="kw">end</span>; |
| 83 | 85 | <span class="kw">end</span>; |
| @@ -102,6 +104,7 @@ | ||
| 102 | 104 | <p>Anything that can be computed from the old value can be made atomic with a loop around <code>CAS</code>: read the value, compute the new one, and try to swap it in; if someone got there first, read again. Here is an atomic maximum, which no single instruction does.</p> |
| 103 | 105 | <div class="file"><span>Peak.raz</span></div><pre class="code"><code><span class="kw">module</span> Peak; |
| 104 | 106 | <span class="kw">import</span> Out, Thread; |
| 107 | +<span class="kw">import</span> SYSTEM; | |
| 105 | 108 | |
| 106 | 109 | <span class="kw">var</span> peak: <span class="ty">INT</span>; |
| 107 | 110 | |
| @@ -111,7 +114,7 @@ | ||
| 111 | 114 | <span class="kw">begin</span> |
| 112 | 115 | seen := peak; |
| 113 | 116 | <span class="kw">while</span> seen < v <span class="kw">do</span> |
| 114 | - <span class="kw">if</span> <span class="ty">CAS</span>(<span class="ty">ADR</span>(peak), seen, v) <span class="kw">then</span> <span class="kw">return</span>; <span class="kw">end</span>; | |
| 117 | + <span class="kw">if</span> <span class="ty">CAS</span>(peak, seen, v) <span class="kw">then</span> <span class="kw">return</span>; <span class="kw">end</span>; | |
| 115 | 118 | seen := peak; <span class="comment">(* someone changed it: look again *)</span> |
| 116 | 119 | <span class="kw">end</span>; |
| 117 | 120 | <span class="kw">end</span>; |
| @@ -148,7 +151,7 @@ | ||
| 148 | 151 | var tries: INT; |
| 149 | 152 | begin |
| 150 | 153 | tries := 0; |
| 151 | - while not CAS(ADR(l.held), 0, 1) do | |
| 154 | + while not CAS(l.held, 0, 1) do | |
| 152 | 155 | INC(tries); |
| 153 | 156 | if tries = 64 then Sys.yield; tries := 0; end; |
| 154 | 157 | end; |
| @@ -157,7 +160,7 @@ | ||
| 157 | 160 | (* Gives up l, which the caller holds. *) |
| 158 | 161 | proc give*(var l: Lock); |
| 159 | 162 | begin |
| 160 | - _ := XCHG(ADR(l.held), 0); | |
| 163 | + _ := XCHG(l.held, 0); | |
| 161 | 164 | end;</code></pre> |
| 162 | 165 | <p>And here is a use of it: a structure with two fields that must stay in step, which no single atomic can protect.</p> |
| 163 | 166 | <div class="file"><span>Account.raz</span></div><pre class="code"><code><span class="kw">module</span> Account; |
| @@ -51,7 +51,7 @@ | ||
| 51 | 51 | <span class="kw">import</span> Out, Str, Fmt; |
| 52 | 52 | |
| 53 | 53 | <span class="kw">proc</span> main*; |
| 54 | -<span class="kw">var</span> names: [<span class="number">4</span>][]<span class="ty">U8</span>; i, j: <span class="ty">INT</span>; t: []<span class="ty">U8</span>; | |
| 54 | +<span class="kw">var</span> names: [<span class="number">4</span>][]<span class="kw">const</span> <span class="ty">U8</span>; i, j: <span class="ty">INT</span>; t: []<span class="kw">const</span> <span class="ty">U8</span>; | |
| 55 | 55 | <span class="kw">begin</span> |
| 56 | 56 | names := [<span class="string">"pear"</span>, <span class="string">"Apple"</span>, <span class="string">"fig"</span>, <span class="string">"apple"</span>]; |
| 57 | 57 | <span class="comment">(* a small insertion sort, with Str.cmp *)</span> |
| @@ -80,13 +80,14 @@ | ||
| 80 | 80 | <p>A slice of a string constant lives forever, but a slice of a buffer lives only as long as the buffer. To keep one past that, <code>Str.copy</code> takes memory from an allocator and says whether it got any.</p> |
| 81 | 81 | <div class="file"><span>Keep.raz</span></div><pre class="code"><code><span class="kw">module</span> Keep; |
| 82 | 82 | <span class="kw">import</span> Out, Str, Heap, Mem; |
| 83 | +<span class="kw">import</span> SYSTEM; | |
| 83 | 84 | |
| 84 | 85 | <span class="kw">type</span> Entry = <span class="kw">record</span> name: []<span class="ty">U8</span>; n: <span class="ty">INT</span>; <span class="kw">end</span>; |
| 85 | 86 | |
| 86 | 87 | <span class="kw">var</span> entries: [<span class="number">3</span>]Entry; |
| 87 | 88 | |
| 88 | 89 | <span class="comment">(* remembers a name that the caller will overwrite *)</span> |
| 89 | -<span class="kw">proc</span> remember(slot: <span class="ty">INT</span>; name: []<span class="ty">U8</span>); | |
| 90 | +<span class="kw">proc</span> remember(slot: <span class="ty">INT</span>; name: []<span class="kw">const</span> <span class="ty">U8</span>); | |
| 90 | 91 | <span class="kw">var</span> copy: []<span class="ty">U8</span>; ok: <span class="ty">BOOL</span>; |
| 91 | 92 | <span class="kw">begin</span> |
| 92 | 93 | copy, ok := Str.copy(Heap.heap, name); |
| @@ -104,7 +105,7 @@ | ||
| 104 | 105 | <span class="kw">end</span>; |
| 105 | 106 | <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="number">2</span> <span class="kw">do</span> |
| 106 | 107 | Out.str(entries[i].name); Out.char(<span class="string">' '</span>); |
| 107 | - Mem.free(Heap.heap, ^<span class="ty">U8</span>(<span class="ty">ADR</span>(entries[i].name[<span class="number">0</span>]))); | |
| 108 | + Mem.free(Heap.heap, ^<span class="ty">U8</span>(SYSTEM.<span class="ty">ADR</span>(entries[i].name[<span class="number">0</span>]))); | |
| 108 | 109 | <span class="kw">end</span>; |
| 109 | 110 | Out.ln; |
| 110 | 111 | <span class="kw">end</span>; |
| @@ -118,13 +119,13 @@ | ||
| 118 | 119 | <span class="kw">import</span> Out, Fmt, Bytes; |
| 119 | 120 | |
| 120 | 121 | <span class="comment">(* the last part of a path: what follows its last slash *)</span> |
| 121 | -<span class="kw">proc</span> base(path: []<span class="ty">U8</span>): []<span class="ty">U8</span>; | |
| 122 | +<span class="kw">proc</span> base(path: []<span class="kw">const</span> <span class="ty">U8</span>): []<span class="kw">const</span> <span class="ty">U8</span>; | |
| 122 | 123 | <span class="kw">begin</span> |
| 123 | 124 | <span class="kw">return</span> path[Bytes.last(path, <span class="string">'/'</span>) + <span class="number">1</span>..<span class="ty">LEN</span>(path)]; |
| 124 | 125 | <span class="kw">end</span>; |
| 125 | 126 | |
| 126 | 127 | <span class="kw">proc</span> main*; |
| 127 | -<span class="kw">var</span> text: []<span class="ty">U8</span>; at, n, line: <span class="ty">INT</span>; | |
| 128 | +<span class="kw">var</span> text: []<span class="kw">const</span> <span class="ty">U8</span>; at, n, line: <span class="ty">INT</span>; | |
| 128 | 129 | <span class="kw">begin</span> |
| 129 | 130 | Out.str(base(<span class="string">"/usr/local/lib/raz/Bytes.raz"</span>)); Out.char(<span class="string">' '</span>); |
| 130 | 131 | Out.str(base(<span class="string">"Out.raz"</span>)); Out.ln; |
| @@ -158,7 +159,7 @@ | ||
| 158 | 159 | <span class="kw">const</span> HeaderSize = <span class="number">10</span>; |
| 159 | 160 | |
| 160 | 161 | <span class="comment">(* the sum of the bytes of data, all mod 2^32 *)</span> |
| 161 | -<span class="kw">proc</span> checksum(data: []<span class="ty">U8</span>): <span class="ty">U32</span>; | |
| 162 | +<span class="kw">proc</span> checksum(data: []<span class="kw">const</span> <span class="ty">U8</span>): <span class="ty">U32</span>; | |
| 162 | 163 | <span class="kw">var</span> i: <span class="ty">INT</span>; sum: <span class="ty">U32</span>; |
| 163 | 164 | <span class="kw">begin</span> |
| 164 | 165 | <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="ty">LEN</span>(data) - <span class="number">1</span> <span class="kw">do</span> sum := sum + <span class="ty">U32</span>(data[i]); <span class="kw">end</span>; |
| @@ -166,7 +167,7 @@ | ||
| 166 | 167 | <span class="kw">end</span>; |
| 167 | 168 | |
| 168 | 169 | <span class="comment">(* writes a frame to out, which must have room; its length *)</span> |
| 169 | -<span class="kw">proc</span> pack(out: []<span class="ty">U8</span>; tag, data: []<span class="ty">U8</span>): <span class="ty">INT</span>; | |
| 170 | +<span class="kw">proc</span> pack(out: []<span class="ty">U8</span>; tag, data: []<span class="kw">const</span> <span class="ty">U8</span>): <span class="ty">INT</span>; | |
| 170 | 171 | <span class="kw">begin</span> |
| 171 | 172 | COPY(out[<span class="number">0</span>..<span class="number">4</span>], tag); |
| 172 | 173 | Str.putBE(out[<span class="number">4</span>..<span class="number">6</span>], <span class="ty">U64</span>(<span class="ty">LEN</span>(data))); |
| @@ -206,8 +207,8 @@ | ||
| 206 | 207 | <span class="kw">import</span> Out, Str, Math, Fmt; |
| 207 | 208 | |
| 208 | 209 | <span class="comment">(* the sum of the numbers in a comma separated list, or -1 if any isn't *)</span> |
| 209 | -<span class="kw">proc</span> total(s: []<span class="ty">U8</span>): <span class="ty">I64</span>; | |
| 210 | -<span class="kw">var</span> part: []<span class="ty">U8</span>; sum, n: <span class="ty">I64</span>; ok, more: <span class="ty">BOOL</span>; | |
| 210 | +<span class="kw">proc</span> total(s: []<span class="kw">const</span> <span class="ty">U8</span>): <span class="ty">I64</span>; | |
| 211 | +<span class="kw">var</span> part: []<span class="kw">const</span> <span class="ty">U8</span>; sum, n: <span class="ty">I64</span>; ok, more: <span class="ty">BOOL</span>; | |
| 211 | 212 | <span class="kw">begin</span> |
| 212 | 213 | more := <span class="ty">true</span>; |
| 213 | 214 | <span class="kw">while</span> more <span class="kw">do</span> |
| @@ -93,7 +93,7 @@ | ||
| 93 | 93 | <h2><code>Map(K, V; hash, eq)</code></h2> |
| 94 | 94 | <p>A hash map. Its key's hash and equality are procedure arguments, so any type can be a key. <code>put</code> replaces what a key maps to; <code>get</code> says what it maps to and whether it maps to anything; <code>remove</code> says whether the key was there. <code>next</code> walks the map: from 0, it gives the next entry and the number to pass for the one after, and -1 when there are no more.</p> |
| 95 | 95 | <div class="file"><span>Ages.raz</span></div><pre class="code"><code><span class="kw">module</span> Ages; |
| 96 | -<span class="kw">import</span> Out, Fmt, Str, Heap, Book := Map([]<span class="ty">U8</span>, <span class="ty">INT</span>, Str.hash, Str.eq); | |
| 96 | +<span class="kw">import</span> Out, Fmt, Str, Heap, Book := Map([]<span class="kw">const</span> <span class="ty">U8</span>, <span class="ty">INT</span>, Str.hash, Str.eq); | |
| 97 | 97 | |
| 98 | 98 | <span class="kw">proc</span> main*; |
| 99 | 99 | <span class="kw">var</span> m: Book.Map; age: <span class="ty">INT</span>; found, ok: <span class="ty">BOOL</span>; i: <span class="ty">INT</span>; v: <span class="ty">INT</span>; |
| @@ -131,11 +131,11 @@ | ||
| 131 | 131 | <p>Counting words, then listing them by count and then alphabetically: a <code>Map</code> for the counts, a <code>Vec</code> of records to sort, however many words there are, and <code>Sort</code> with a two-level <code>less</code>.</p> |
| 132 | 132 | <div class="file"><span>Freq.raz</span></div><pre class="code"><code><span class="kw">module</span> Freq; |
| 133 | 133 | <span class="kw">import</span> Fmt, Str, Heap, |
| 134 | - Count := Map([]<span class="ty">U8</span>, <span class="ty">INT</span>, Str.hash, Str.eq), | |
| 134 | + Count := Map([]<span class="kw">const</span> <span class="ty">U8</span>, <span class="ty">INT</span>, Str.hash, Str.eq), | |
| 135 | 135 | Entries := Vec(Entry), |
| 136 | 136 | Rank := Sort(Entry, before); |
| 137 | 137 | |
| 138 | -<span class="kw">type</span> Entry = <span class="kw">record</span> word: []<span class="ty">U8</span>; n: <span class="ty">INT</span>; <span class="kw">end</span>; | |
| 138 | +<span class="kw">type</span> Entry = <span class="kw">record</span> word: []<span class="kw">const</span> <span class="ty">U8</span>; n: <span class="ty">INT</span>; <span class="kw">end</span>; | |
| 139 | 139 | |
| 140 | 140 | <span class="kw">const</span> Text = <span class="string">"the cat and the hat and the bat sat on the mat and the cat sat"</span>; |
| 141 | 141 | |
| @@ -149,7 +149,7 @@ | ||
| 149 | 149 | <span class="kw">proc</span> main*; |
| 150 | 150 | <span class="kw">var</span> |
| 151 | 151 | m: Count.Map; entries: Entries.Vec; i, k, found: <span class="ty">INT</span>; |
| 152 | - w, rest: []<span class="ty">U8</span>; key: []<span class="ty">U8</span>; v: <span class="ty">INT</span>; all: []Entry; | |
| 152 | + w, rest: []<span class="kw">const</span> <span class="ty">U8</span>; key: []<span class="kw">const</span> <span class="ty">U8</span>; v: <span class="ty">INT</span>; all: []Entry; | |
| 153 | 153 | <span class="kw">begin</span> |
| 154 | 154 | Count.init(m, Heap.heap); |
| 155 | 155 | <span class="kw">defer</span> Count.done(m); |
| @@ -79,7 +79,7 @@ | ||
| 79 | 79 | <span class="kw">import</span> Out, Sys; |
| 80 | 80 | |
| 81 | 81 | <span class="comment">(* the lines of the file at path, or minus an errno *)</span> |
| 82 | -<span class="kw">proc</span> lines(path: []<span class="ty">U8</span>): <span class="ty">INT</span>; | |
| 82 | +<span class="kw">proc</span> lines(path: []<span class="kw">const</span> <span class="ty">U8</span>): <span class="ty">INT</span>; | |
| 83 | 83 | <span class="kw">var</span> fd, n, i, count: <span class="ty">INT</span>; buf: [<span class="number">4096</span>]<span class="ty">U8</span>; |
| 84 | 84 | <span class="kw">begin</span> |
| 85 | 85 | fd := Sys.openRead(path); |
| @@ -122,6 +122,7 @@ | ||
| 122 | 122 | <p><code>Sys.info</code> says a file's size, modification time, permissions, and whether it is a directory, a regular file or a link. <code>Sys.openDir</code> and <code>nextEntry</code> list a directory, in no particular order, so a program that prints one sorts it first. A name from <code>nextEntry</code> is good only until the next call, so the program keeps a copy.</p> |
| 123 | 123 | <div class="file"><span>Tree.raz</span></div><pre class="code"><code><span class="kw">module</span> Tree; |
| 124 | 124 | <span class="kw">import</span> Out, Sys, Str, Heap, Mem, Buf, Order := Sort([]<span class="ty">U8</span>, before); |
| 125 | +<span class="kw">import</span> SYSTEM; | |
| 125 | 126 | |
| 126 | 127 | <span class="kw">proc</span> before(a, b: []<span class="ty">U8</span>): <span class="ty">BOOL</span>; |
| 127 | 128 | <span class="kw">begin</span> |
| @@ -129,7 +130,7 @@ | ||
| 129 | 130 | <span class="kw">end</span>; |
| 130 | 131 | |
| 131 | 132 | <span class="comment">(* a file made to hold text *)</span> |
| 132 | -<span class="kw">proc</span> put(path, text: []<span class="ty">U8</span>); | |
| 133 | +<span class="kw">proc</span> put(path, text: []<span class="kw">const</span> <span class="ty">U8</span>); | |
| 133 | 134 | <span class="kw">var</span> fd: <span class="ty">INT</span>; |
| 134 | 135 | <span class="kw">begin</span> |
| 135 | 136 | fd := Sys.create(path); |
| @@ -139,7 +140,7 @@ | ||
| 139 | 140 | |
| 140 | 141 | <span class="kw">proc</span> main*; |
| 141 | 142 | <span class="kw">var</span> |
| 142 | - d: Sys.Dir; name, copy: []<span class="ty">U8</span>; more, ok: <span class="ty">BOOL</span>; i, n: <span class="ty">INT</span>; | |
| 143 | + d: Sys.Dir; name: []<span class="kw">const</span> <span class="ty">U8</span>; copy: []<span class="ty">U8</span>; more, ok: <span class="ty">BOOL</span>; i, n: <span class="ty">INT</span>; | |
| 143 | 144 | info: Sys.Info; err: <span class="ty">INT</span>; names: [<span class="number">16</span>][]<span class="ty">U8</span>; path: Buf.Buf; |
| 144 | 145 | <span class="kw">begin</span> |
| 145 | 146 | _ := Sys.makeDir(<span class="string">"proj"</span>); |
| @@ -168,7 +169,7 @@ | ||
| 168 | 169 | <span class="kw">else</span> Out.str(<span class="string">", "</span>); Out.int(info.size); Out.str(<span class="string">" bytes"</span>); |
| 169 | 170 | <span class="kw">end</span>; |
| 170 | 171 | Out.ln; |
| 171 | - Mem.free(Heap.heap, ^<span class="ty">U8</span>(<span class="ty">ADR</span>(names[i][<span class="number">0</span>]))); | |
| 172 | + Mem.free(Heap.heap, ^<span class="ty">U8</span>(SYSTEM.<span class="ty">ADR</span>(names[i][<span class="number">0</span>]))); | |
| 172 | 173 | <span class="kw">end</span>; |
| 173 | 174 | Buf.done(path); |
| 174 | 175 | <span class="kw">end</span>; |
| @@ -224,7 +225,7 @@ | ||
| 224 | 225 | <span class="kw">const</span> count = <span class="number">1</span>; <span class="comment">(* the key of --count, which has no letter *)</span> |
| 225 | 226 | |
| 226 | 227 | <span class="kw">proc</span> main*; |
| 227 | -<span class="kw">var</span> p: Opt.Parser; verbose: <span class="ty">BOOL</span>; n, i: <span class="ty">INT</span>; w: []<span class="ty">U8</span>; ok: <span class="ty">BOOL</span>; | |
| 228 | +<span class="kw">var</span> p: Opt.Parser; verbose: <span class="ty">BOOL</span>; n, i: <span class="ty">INT</span>; w: []<span class="kw">const</span> <span class="ty">U8</span>; ok: <span class="ty">BOOL</span>; | |
| 228 | 229 | <span class="kw">begin</span> |
| 229 | 230 | n := <span class="number">1</span>; |
| 230 | 231 | Opt.init(p, <span class="string">"tool"</span>, [ |
| @@ -96,7 +96,7 @@ | ||
| 96 | 96 | <span class="kw">import</span> Out, Buf, Fmt, Heap; |
| 97 | 97 | |
| 98 | 98 | <span class="comment">(* "name (age n)", built in a Buf the caller owns *)</span> |
| 99 | -<span class="kw">proc</span> describe(<span class="kw">var</span> b: Buf.Buf; name: []<span class="ty">U8</span>; age: <span class="ty">INT</span>); | |
| 99 | +<span class="kw">proc</span> describe(<span class="kw">var</span> b: Buf.Buf; name: []<span class="kw">const</span> <span class="ty">U8</span>; age: <span class="ty">INT</span>); | |
| 100 | 100 | <span class="kw">begin</span> |
| 101 | 101 | Fmt.write(b, <span class="string">"{} (age {})"</span>, [Fmt.str(name), Fmt.int(age)]); |
| 102 | 102 | <span class="kw">end</span>; |
| @@ -123,7 +123,7 @@ | ||
| 123 | 123 | <span class="kw">import</span> Out; |
| 124 | 124 | |
| 125 | 125 | <span class="comment">(* s, then spaces up to width *)</span> |
| 126 | -<span class="kw">proc</span> left(s: []<span class="ty">U8</span>; width: <span class="ty">INT</span>); | |
| 126 | +<span class="kw">proc</span> left(s: []<span class="kw">const</span> <span class="ty">U8</span>; width: <span class="ty">INT</span>); | |
| 127 | 127 | <span class="kw">var</span> i: <span class="ty">INT</span>; |
| 128 | 128 | <span class="kw">begin</span> |
| 129 | 129 | Out.str(s); |
| @@ -142,7 +142,7 @@ | ||
| 142 | 142 | Out.int(n); |
| 143 | 143 | <span class="kw">end</span>; |
| 144 | 144 | |
| 145 | -<span class="kw">proc</span> row(name: []<span class="ty">U8</span>; qty, price: <span class="ty">INT</span>); | |
| 145 | +<span class="kw">proc</span> row(name: []<span class="kw">const</span> <span class="ty">U8</span>; qty, price: <span class="ty">INT</span>); | |
| 146 | 146 | <span class="kw">begin</span> |
| 147 | 147 | left(name, <span class="number">10</span>); |
| 148 | 148 | right(qty, <span class="number">5</span>); |
| @@ -172,7 +172,7 @@ | ||
| 172 | 172 | <div class="file"><span>Dump.raz</span></div><pre class="code"><code><span class="kw">module</span> Dump; |
| 173 | 173 | <span class="kw">import</span> Out; |
| 174 | 174 | |
| 175 | -<span class="kw">proc</span> dump(data: []<span class="ty">U8</span>); | |
| 175 | +<span class="kw">proc</span> dump(data: []<span class="kw">const</span> <span class="ty">U8</span>); | |
| 176 | 176 | <span class="kw">var</span> i: <span class="ty">INT</span>; |
| 177 | 177 | <span class="kw">begin</span> |
| 178 | 178 | <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="ty">LEN</span>(data) - <span class="number">1</span> <span class="kw">do</span> |
| @@ -53,7 +53,7 @@ | ||
| 53 | 53 | |
| 54 | 54 | <span class="kw">var</span> steps*: <span class="ty">INT</span>; |
| 55 | 55 | |
| 56 | -<span class="kw">proc</span> note(what: []<span class="ty">U8</span>); | |
| 56 | +<span class="kw">proc</span> note(what: []<span class="kw">const</span> <span class="ty">U8</span>); | |
| 57 | 57 | <span class="kw">begin</span> |
| 58 | 58 | <span class="ty">INC</span>(steps); |
| 59 | 59 | Out.int(steps); Out.str(<span class="string">". "</span>); Out.str(what); Out.ln; |
| @@ -67,20 +67,21 @@ | ||
| 67 | 67 | the Thread. Its test uses Check. *) |
| 68 | 68 | module Thread; |
| 69 | 69 | import Sys, Rt, Check; |
| 70 | +import SYSTEM; | |
| 70 | 71 | |
| 71 | 72 | const |
| 72 | - Failed*: ERROR = "can't start a thread"; | |
| 73 | - Stack = 1 shl 20;</code></pre> | |
| 73 | + Failed*: ERROR = "can't start a thread";</code></pre> | |
| 74 | 74 | <p>A program that imports it is therefore a static executable, and every thread it spawns is a Linux thread with a stack of its own, mapped with a page below it that can't be touched, so that running off the stack ends the program with a message and not with corruption.</p> |
| 75 | 75 | <div class="file"><span>Hello.raz</span></div><pre class="code"><code><span class="kw">module</span> Hello; |
| 76 | 76 | <span class="kw">import</span> Out, Thread; |
| 77 | +<span class="kw">import</span> SYSTEM; | |
| 77 | 78 | |
| 78 | 79 | <span class="kw">var</span> done: <span class="ty">INT</span>; |
| 79 | 80 | |
| 80 | 81 | <span class="kw">proc</span> greet(arg: ^<span class="ty">U8</span>); |
| 81 | 82 | <span class="kw">begin</span> |
| 82 | 83 | Out.str(<span class="string">"hello from thread "</span>); Out.int(<span class="ty">INT</span>(arg)); Out.ln; |
| 83 | - _ := <span class="ty">XADD</span>(<span class="ty">ADR</span>(done), <span class="number">1</span>); | |
| 84 | + _ := <span class="ty">XADD</span>(done, <span class="number">1</span>); | |
| 84 | 85 | <span class="kw">end</span>; |
| 85 | 86 | |
| 86 | 87 | <span class="kw">proc</span> main*; |
| @@ -112,7 +113,12 @@ | ||
| 112 | 113 | <span class="kw">end</span>; |
| 113 | 114 | |
| 114 | 115 | <span class="kw">end</span> Mixed.</code></pre> |
| 115 | -<pre class="output term"><code><span class="cmd">$ raz Mixed.raz</span></code></pre> | |
| 116 | +<pre class="output term"><code><span class="cmd">$ raz Mixed.raz</span> | |
| 117 | +Mixed.raz:7:19: error: a string as a ^U8 needs import SYSTEM | |
| 118 | + 7 | _ := C.printf("%d\n", I32(1)); | |
| 119 | + | ^^^^^^ | |
| 120 | +help: it reaches memory other than through a variable: say import SYSTEM after the module's imports | |
| 121 | +raz: 1 error</code></pre> | |
| 116 | 122 | <p>Which one you want depends on what the program is for. A program that calls a C library (a window system, a database client) uses C's threads, <code>C.pthread_create</code>, because the library may assume them. A program that talks only to the kernel uses <code>Thread</code>, and is a static executable.</p> |
| 117 | 123 | <h2>The same promise for your own modules</h2> |
| 118 | 124 | <p>The rule isn't special to <code>Thread</code>. A module of yours that makes <code>SYSCALL</code>s directly, or relies on being the only owner of signal handlers, or sets up its own stacks, says <code>link static;</code> too, and then the compiler keeps C away from it for you. The module is documenting a fact about itself, and the compiler checks it where the program is assembled.</p><p class="tryit">Save a program under its module’s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="static.html">← A program with no run time</a><a href="index.html">Contents</a><a class="next" href="lifecycle.html">Starting, ending, and signals →</a></div></article></div> |
| @@ -185,59 +185,20 @@ | ||
| 185 | 185 | |
| 186 | 186 | <span class="kw">end</span> Dot2.</code></pre> |
| 187 | 187 | <pre class="output term"><code><span class="cmd">$ raz -S Dot2.raz | sed -n '/^Dot2.dot:/,/^$/p'</span> |
| 188 | -Dot2.dot: | |
| 189 | - 4 proc dot*(a, b: []INT): INT; | |
| 190 | - 0 push rbp | |
| 191 | - 1 mov rbp, rsp | |
| 192 | - 4 sub rsp, 64 | |
| 193 | - 11 mov [rbp - 56], rbx | |
| 194 | - 15 xor ebx, ebx | |
| 195 | - 17 mov [rbp - 48], r12 | |
| 196 | - 21 xor r12d, r12d | |
| 197 | - 24 mov [rbp - 40], r13 | |
| 198 | - 28 xor r13d, r13d | |
| 199 | - 31 mov [rbp - 32], rdx | |
| 200 | - 35 mov [rbp - 24], rcx | |
| 201 | - 7 for i := 0 to LEN(a) - 1 do | |
| 202 | - 39 xor ebx, ebx | |
| 203 | - 41 mov r8, rsi | |
| 204 | - 44 lea r8, [r8 - 1] | |
| 205 | - 48 mov r13, r8 | |
| 206 | - 51 cmp rbx, r13 | |
| 207 | - 54 jg 156 | |
| 208 | - 60 jmp 100 | |
| 209 | - 65 nop | |
| 210 | - 73 nop | |
| 211 | - 81 nop | |
| 212 | - 89 nop | |
| 213 | - 96 add rbx, 1 | |
| 214 | - 8 sum := sum + a[i] * b[i]; | |
| 215 | - 100 mov r10, rbx | |
| 216 | - 103 shl r10, 3 | |
| 217 | - 107 add r10, [rbp - 32] | |
| 218 | - 111 mov r10, [r10] | |
| 219 | - 114 mov r9, [rdi + rbx * 8] | |
| 220 | - 118 imul r9, r10 | |
| 221 | - 122 add r12, r9 | |
| 222 | - 125 cmp rbx, r13 | |
| 223 | - 128 jl 96 | |
| 224 | - 134 cmp rbx, r13 | |
| 225 | - 137 je 156 | |
| 226 | - 143 add rbx, 1 | |
| 227 | - 147 cmp rbx, r13 | |
| 228 | - 150 jle 100 | |
| 229 | - 10 return sum; | |
| 230 | - 156 mov rax, r12 | |
| 231 | - 11 end; | |
| 232 | - 159 mov rbx, [rbp - 56] | |
| 233 | - 163 mov r12, [rbp - 48] | |
| 234 | - 167 mov r13, [rbp - 40] | |
| 235 | - 171 leave | |
| 236 | - 172 ret | |
| 188 | +Dot2.raz:2:1: error: check off needs import SYSTEM | |
| 189 | + 2 | check off; | |
| 190 | + | ^^^^^ | |
| 191 | +help: it reaches memory other than through a variable: say import SYSTEM after the module's imports | |
| 192 | +raz: 1 error | |
| 237 | 193 | <span class="cmd">$ raz -S Dot.raz | sed -n '/^Dot.dot:/,/^$/p' | wc -l</span> |
| 238 | 194 | 61 |
| 239 | 195 | <span class="cmd">$ raz -S Dot2.raz | sed -n '/^Dot2.dot:/,/^$/p' | wc -l</span> |
| 240 | -50</code></pre> | |
| 196 | +Dot2.raz:2:1: error: check off needs import SYSTEM | |
| 197 | + 2 | check off; | |
| 198 | + | ^^^^^ | |
| 199 | +help: it reaches memory other than through a variable: say import SYSTEM after the module's imports | |
| 200 | +raz: 1 error | |
| 201 | +0</code></pre> | |
| 241 | 202 | <p>The compares, the branches and the stubs are gone. It is faster, and it is no longer safe: an index out of range reads or writes whatever memory it reaches, and what follows isn't defined. A program turns checks off where it has measured and is sure, and says so at the top of the module, where a reader can see it.</p> |
| 242 | 203 | <h2>Comparing with C</h2> |
| 243 | 204 | <p><code>make kernels</code> in the compiler's sources measures <code>test/bench</code>'s kernels against the same kernels written in Pascal and compiled by Free Pascal at <code>-O2</code>. The compiler's own guide, <code>doc/compiler.md</code>, gives the numbers and says what the compiler does and doesn't do. It is not the fastest code: it is code whose every instruction you can account for.</p> |
| @@ -245,18 +206,18 @@ | ||
| 245 | 206 | <ul><li><code>gdb</code> finds procedures by name and lines by number, since the executable carries them: <code>break Dot.dot</code> and <code>list</code> work, and so does stepping a line at a time.</li><li><code>RAZ_PROFILE=1</code> samples the program every millisecond and says which procedures and lines took the time.</li><li><code>objdump -d</code> disassembles what is really in the file, if you want to see the bytes behind a listing.</li></ul> |
| 246 | 207 | <pre class="output term"><code><span class="cmd">$ raz -o Dot Dot.raz</span> |
| 247 | 208 | <span class="cmd">$ objdump -d -M intel --no-show-raw-insn Dot | sed -n '/<Dot.dot>:/,/ret/p' | head -12</span> |
| 248 | -0000000000403c60 <Dot.dot>: | |
| 249 | - 403c60: push rbp | |
| 250 | - 403c61: mov rbp,rsp | |
| 251 | - 403c64: sub rsp,0x40 | |
| 252 | - 403c6b: mov QWORD PTR [rbp-0x38],rbx | |
| 253 | - 403c6f: xor ebx,ebx | |
| 254 | - 403c71: mov QWORD PTR [rbp-0x30],r12 | |
| 255 | - 403c75: xor r12d,r12d | |
| 256 | - 403c78: mov QWORD PTR [rbp-0x28],r13 | |
| 257 | - 403c7c: xor r13d,r13d | |
| 258 | - 403c7f: mov QWORD PTR [rbp-0x20],rdx | |
| 259 | - 403c83: mov QWORD PTR [rbp-0x18],rcx</code></pre> | |
| 209 | +0000000000403c40 <Dot.dot>: | |
| 210 | + 403c40: push rbp | |
| 211 | + 403c41: mov rbp,rsp | |
| 212 | + 403c44: sub rsp,0x40 | |
| 213 | + 403c4b: mov QWORD PTR [rbp-0x38],rbx | |
| 214 | + 403c4f: xor ebx,ebx | |
| 215 | + 403c51: mov QWORD PTR [rbp-0x30],r12 | |
| 216 | + 403c55: xor r12d,r12d | |
| 217 | + 403c58: mov QWORD PTR [rbp-0x28],r13 | |
| 218 | + 403c5c: xor r13d,r13d | |
| 219 | + 403c5f: mov QWORD PTR [rbp-0x20],rdx | |
| 220 | + 403c63: mov QWORD PTR [rbp-0x18],rcx</code></pre> | |
| 260 | 221 | <p>The listing is what the encoder meant; <code>objdump</code> is what the processor will read. They tell the same story.</p><p class="tryit">Save a program under its module’s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="syscalls.html">← System calls</a><a href="index.html">Contents</a><a class="next" href="volatile.html">Volatile →</a></div></article></div> |
| 261 | 222 | </main> |
| 262 | 223 | <footer>raz is under the ISC license. <a href="../source/index.html">Source</a></footer> |
| @@ -49,6 +49,7 @@ | ||
| 49 | 49 | <p>To push a node: point its <code>next</code> at the current head, then <code>CAS</code> the head from what you saw to the new node. If another thread pushed first, the <code>CAS</code> fails, because the head is no longer what you saw, so look again and retry. No thread ever waits for another, and none can stop the others by being slow.</p> |
| 50 | 50 | <div class="file"><span>Treiber.raz</span></div><pre class="code"><code><span class="kw">module</span> Treiber; |
| 51 | 51 | <span class="kw">import</span> Out, Thread; |
| 52 | +<span class="kw">import</span> SYSTEM; | |
| 52 | 53 | |
| 53 | 54 | <span class="kw">type</span> Node = <span class="kw">record</span> next: ^Node; val: <span class="ty">INT</span>; <span class="kw">end</span>; |
| 54 | 55 | |
| @@ -64,7 +65,7 @@ | ||
| 64 | 65 | <span class="kw">begin</span> |
| 65 | 66 | old := top; |
| 66 | 67 | n^.next := old; |
| 67 | - <span class="kw">while</span> <span class="kw">not</span> <span class="ty">CAS</span>(<span class="ty">ADR</span>(top), old, n) <span class="kw">do</span> | |
| 68 | + <span class="kw">while</span> <span class="kw">not</span> <span class="ty">CAS</span>(top, old, n) <span class="kw">do</span> | |
| 68 | 69 | old := top; <span class="comment">(* lost the race: look again *)</span> |
| 69 | 70 | n^.next := old; |
| 70 | 71 | <span class="kw">end</span>; |
| @@ -76,7 +77,7 @@ | ||
| 76 | 77 | first := <span class="ty">INT</span>(arg) * Each; |
| 77 | 78 | <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> Each - <span class="number">1</span> <span class="kw">do</span> |
| 78 | 79 | nodes[first + i].val := first + i; |
| 79 | - push(<span class="ty">ADR</span>(nodes[first + i])); | |
| 80 | + push(SYSTEM.<span class="ty">ADR</span>(nodes[first + i])); | |
| 80 | 81 | <span class="kw">end</span>; |
| 81 | 82 | <span class="kw">end</span>; |
| 82 | 83 | |
| @@ -107,9 +108,10 @@ | ||
| 107 | 108 | <h2>Pop, and the ABA problem</h2> |
| 108 | 109 | <p>Pop takes the head off: read <code>top</code>, read its <code>next</code>, <code>CAS</code> the head from <code>top</code> to <code>next</code>. It looks as easy as push, and it isn't. Between the two reads and the <code>CAS</code>, other threads may pop that node, pop others, and push the same node back, now with a different <code>next</code>. The head is the same pointer again, so the <code>CAS</code> succeeds, and installs a <code>next</code> that is stale: nodes disappear, or the stack points into freed memory. This is the <strong>ABA problem</strong>: the word changed from A to B and back to A, and the <code>CAS</code> can't tell.</p> |
| 109 | 110 | <p>There are three common ways out, and each has a cost:</p> |
| 110 | -<ul><li><strong>Never reuse a node while anyone might hold it.</strong> Nodes from an arena that is reset only when all threads are done, as the nodes above come from a fixed array, can't come back as a different node.</li><li><strong>Add a counter to the head</strong>, so that A-B-A is A1-B2-A3. That is a word and a counter swapped together, and raz's <code>CAS</code> is for one word, so a program packs a small index and a counter into one <code>UINT</code>.</li><li><strong>Don't pop concurrently.</strong> Many threads push and one thread, at the end, takes everything with a single <code>XCHG(ADR(top), nil)</code> and walks the list in peace. A stack used this way for work lists and logs needs no more.</li></ul> | |
| 111 | +<ul><li><strong>Never reuse a node while anyone might hold it.</strong> Nodes from an arena that is reset only when all threads are done, as the nodes above come from a fixed array, can't come back as a different node.</li><li><strong>Add a counter to the head</strong>, so that A-B-A is A1-B2-A3. That is a word and a counter swapped together, and raz's <code>CAS</code> is for one word, so a program packs a small index and a counter into one <code>UINT</code>.</li><li><strong>Don't pop concurrently.</strong> Many threads push and one thread, at the end, takes everything with a single <code>XCHG(top, nil)</code> and walks the list in peace. A stack used this way for work lists and logs needs no more.</li></ul> | |
| 111 | 112 | <div class="file"><span>Drain.raz</span></div><pre class="code"><code><span class="kw">module</span> Drain; |
| 112 | 113 | <span class="kw">import</span> Out, Thread; |
| 114 | +<span class="kw">import</span> SYSTEM; | |
| 113 | 115 | |
| 114 | 116 | <span class="kw">type</span> Node = <span class="kw">record</span> next: ^Node; val: <span class="ty">INT</span>; <span class="kw">end</span>; |
| 115 | 117 | |
| @@ -124,7 +126,7 @@ | ||
| 124 | 126 | <span class="kw">begin</span> |
| 125 | 127 | old := top; |
| 126 | 128 | n^.next := old; |
| 127 | - <span class="kw">while</span> <span class="kw">not</span> <span class="ty">CAS</span>(<span class="ty">ADR</span>(top), old, n) <span class="kw">do</span> | |
| 129 | + <span class="kw">while</span> <span class="kw">not</span> <span class="ty">CAS</span>(top, old, n) <span class="kw">do</span> | |
| 128 | 130 | old := top; |
| 129 | 131 | n^.next := old; |
| 130 | 132 | <span class="kw">end</span>; |
| @@ -136,7 +138,7 @@ | ||
| 136 | 138 | first := <span class="ty">INT</span>(arg) * Each; |
| 137 | 139 | <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> Each - <span class="number">1</span> <span class="kw">do</span> |
| 138 | 140 | nodes[first + i].val := <span class="number">1</span>; |
| 139 | - push(<span class="ty">ADR</span>(nodes[first + i])); | |
| 141 | + push(SYSTEM.<span class="ty">ADR</span>(nodes[first + i])); | |
| 140 | 142 | <span class="kw">end</span>; |
| 141 | 143 | <span class="kw">end</span>; |
| 142 | 144 | |
| @@ -148,7 +150,7 @@ | ||
| 148 | 150 | <span class="kw">if</span> err # <span class="ty">nil</span> <span class="kw">then</span> Out.str(<span class="ty">MSG</span>(err)); Out.ln; <span class="kw">return</span>; <span class="kw">end</span>; |
| 149 | 151 | <span class="kw">end</span>; |
| 150 | 152 | <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> Threads - <span class="number">1</span> <span class="kw">do</span> Thread.join(th[i]); <span class="kw">end</span>; |
| 151 | - all := <span class="ty">XCHG</span>(<span class="ty">ADR</span>(top), <span class="ty">nil</span>); <span class="comment">(* take the whole stack, atomically *)</span> | |
| 153 | + all := <span class="ty">XCHG</span>(top, <span class="ty">nil</span>); <span class="comment">(* take the whole stack, atomically *)</span> | |
| 152 | 154 | n := all; |
| 153 | 155 | <span class="kw">while</span> n # <span class="ty">nil</span> <span class="kw">do</span> <span class="ty">INC</span>(count); n := n^.next; <span class="kw">end</span>; |
| 154 | 156 | Out.int(count); Out.str(<span class="string">" taken at once, "</span>); |
| @@ -51,6 +51,7 @@ | ||
| 51 | 51 | <p>The simplest correct way to hand a value to another thread: write it with ordinary stores, then store a flag with an atomic. The reader loops on the flag with an atomic read, and then reads the data. <code>XCHG</code> is the store; <code>XADD(p, 0)</code> is the load, which adds nothing and returns the value. Both are sequentially consistent, so everything written before the flag is visible after it is seen.</p> |
| 52 | 52 | <div class="file"><span>Publish.raz</span></div><pre class="code"><code><span class="kw">module</span> Publish; |
| 53 | 53 | <span class="kw">import</span> Out, Sys, Thread; |
| 54 | +<span class="kw">import</span> SYSTEM; | |
| 54 | 55 | |
| 55 | 56 | <span class="kw">type</span> Message = <span class="kw">record</span> id: <span class="ty">INT</span>; text: [<span class="number">32</span>]<span class="ty">U8</span>; len: <span class="ty">INT</span>; <span class="kw">end</span>; |
| 56 | 57 | |
| @@ -64,7 +65,7 @@ | ||
| 64 | 65 | msg.id := <span class="number">7</span>; |
| 65 | 66 | msg.text := <span class="string">"built before the flag"</span>; |
| 66 | 67 | msg.len := <span class="number">21</span>; |
| 67 | - _ := <span class="ty">XCHG</span>(<span class="ty">ADR</span>(ready), <span class="number">1</span>); <span class="comment">(* publish *)</span> | |
| 68 | + _ := <span class="ty">XCHG</span>(ready, <span class="number">1</span>); <span class="comment">(* publish *)</span> | |
| 68 | 69 | <span class="kw">end</span>; |
| 69 | 70 | |
| 70 | 71 | <span class="kw">proc</span> main*; |
| @@ -72,7 +73,7 @@ | ||
| 72 | 73 | <span class="kw">begin</span> |
| 73 | 74 | t, err := Thread.spawn(producer, <span class="ty">nil</span>); |
| 74 | 75 | <span class="kw">if</span> err # <span class="ty">nil</span> <span class="kw">then</span> Out.str(<span class="ty">MSG</span>(err)); Out.ln; <span class="kw">return</span>; <span class="kw">end</span>; |
| 75 | - <span class="kw">while</span> <span class="ty">XADD</span>(<span class="ty">ADR</span>(ready), <span class="number">0</span>) = <span class="number">0</span> <span class="kw">do</span> Sys.yield; <span class="kw">end</span>; <span class="comment">(* wait for it *)</span> | |
| 76 | + <span class="kw">while</span> <span class="ty">XADD</span>(ready, <span class="number">0</span>) = <span class="number">0</span> <span class="kw">do</span> Sys.yield; <span class="kw">end</span>; <span class="comment">(* wait for it *)</span> | |
| 76 | 77 | Out.int(msg.id); Out.char(<span class="string">' '</span>); Out.str(msg.text[<span class="number">0</span>..msg.len]); Out.ln; |
| 77 | 78 | Thread.join(t); |
| 78 | 79 | <span class="kw">end</span>; |
| @@ -85,6 +86,7 @@ | ||
| 85 | 86 | <p><code>FENCE</code> is a full memory barrier: it orders the program's ordinary accesses against those of other processors and devices. With volatile accesses for the flag and a fence between the data and the flag, the same handoff works. This is the form device code takes, as in the last lesson.</p> |
| 86 | 87 | <div class="file"><span>Fenced.raz</span></div><pre class="code"><code><span class="kw">module</span> Fenced; |
| 87 | 88 | <span class="kw">import</span> Out, Sys, Thread; |
| 89 | +<span class="kw">import</span> SYSTEM; | |
| 88 | 90 | |
| 89 | 91 | <span class="kw">var</span> |
| 90 | 92 | data: <span class="ty">INT</span>; <span class="comment">(* ordinary *)</span> |
| @@ -93,7 +95,7 @@ | ||
| 93 | 95 | <span class="kw">proc</span> producer(_arg: ^<span class="ty">U8</span>); |
| 94 | 96 | <span class="kw">var</span> f: ^<span class="kw">VOLATILE</span> <span class="ty">INT</span>; |
| 95 | 97 | <span class="kw">begin</span> |
| 96 | - f := ^<span class="kw">VOLATILE</span> <span class="ty">INT</span>(<span class="ty">ADR</span>(flag)); | |
| 98 | + f := ^<span class="kw">VOLATILE</span> <span class="ty">INT</span>(SYSTEM.<span class="ty">ADR</span>(flag)); | |
| 97 | 99 | Sys.sleep(<span class="number">10</span>); |
| 98 | 100 | data := <span class="number">1234</span>; |
| 99 | 101 | FENCE; <span class="comment">(* data is written before the flag is *)</span> |
| @@ -103,7 +105,7 @@ | ||
| 103 | 105 | <span class="kw">proc</span> main*; |
| 104 | 106 | <span class="kw">var</span> t: ^Thread.Thread; err: <span class="ty">ERROR</span>; f: ^<span class="kw">VOLATILE</span> <span class="ty">INT</span>; |
| 105 | 107 | <span class="kw">begin</span> |
| 106 | - f := ^<span class="kw">VOLATILE</span> <span class="ty">INT</span>(<span class="ty">ADR</span>(flag)); | |
| 108 | + f := ^<span class="kw">VOLATILE</span> <span class="ty">INT</span>(SYSTEM.<span class="ty">ADR</span>(flag)); | |
| 107 | 109 | t, err := Thread.spawn(producer, <span class="ty">nil</span>); |
| 108 | 110 | <span class="kw">if</span> err # <span class="ty">nil</span> <span class="kw">then</span> Out.str(<span class="ty">MSG</span>(err)); Out.ln; <span class="kw">return</span>; <span class="kw">end</span>; |
| 109 | 111 | <span class="kw">while</span> f^ = <span class="number">0</span> <span class="kw">do</span> Sys.yield; <span class="kw">end</span>; |
| @@ -52,6 +52,7 @@ | ||
| 52 | 52 | <h2>The module</h2> |
| 53 | 53 | <div class="file"><span>Ring.raz</span></div><pre class="code"><code><span class="kw">module</span> Ring; |
| 54 | 54 | <span class="kw">import</span> Sys; |
| 55 | +<span class="kw">import</span> SYSTEM; | |
| 55 | 56 | |
| 56 | 57 | <span class="kw">const</span> Size* = <span class="number">64</span>; |
| 57 | 58 | |
| @@ -66,9 +67,9 @@ | ||
| 66 | 67 | <span class="kw">var</span> t: <span class="ty">INT</span>; |
| 67 | 68 | <span class="kw">begin</span> |
| 68 | 69 | t := r^.tail; <span class="comment">(* mine: an ordinary read *)</span> |
| 69 | - <span class="kw">while</span> t - <span class="ty">XADD</span>(<span class="ty">ADR</span>(r^.head), <span class="number">0</span>) = Size <span class="kw">do</span> Sys.yield; <span class="kw">end</span>; <span class="comment">(* full *)</span> | |
| 70 | + <span class="kw">while</span> t - <span class="ty">XADD</span>(r^.head, <span class="number">0</span>) = Size <span class="kw">do</span> Sys.yield; <span class="kw">end</span>; <span class="comment">(* full *)</span> | |
| 70 | 71 | r^.slots[t <span class="kw">mod</span> Size] := v; |
| 71 | - _ := <span class="ty">XCHG</span>(<span class="ty">ADR</span>(r^.tail), t + <span class="number">1</span>); <span class="comment">(* publish the slot *)</span> | |
| 72 | + _ := <span class="ty">XCHG</span>(r^.tail, t + <span class="number">1</span>); <span class="comment">(* publish the slot *)</span> | |
| 72 | 73 | <span class="kw">end</span>; |
| 73 | 74 | |
| 74 | 75 | <span class="comment">(* the next value, waiting while the ring is empty *)</span> |
| @@ -76,9 +77,9 @@ | ||
| 76 | 77 | <span class="kw">var</span> h, v: <span class="ty">INT</span>; |
| 77 | 78 | <span class="kw">begin</span> |
| 78 | 79 | h := r^.head; <span class="comment">(* mine *)</span> |
| 79 | - <span class="kw">while</span> <span class="ty">XADD</span>(<span class="ty">ADR</span>(r^.tail), <span class="number">0</span>) = h <span class="kw">do</span> Sys.yield; <span class="kw">end</span>; <span class="comment">(* empty *)</span> | |
| 80 | + <span class="kw">while</span> <span class="ty">XADD</span>(r^.tail, <span class="number">0</span>) = h <span class="kw">do</span> Sys.yield; <span class="kw">end</span>; <span class="comment">(* empty *)</span> | |
| 80 | 81 | v := r^.slots[h <span class="kw">mod</span> Size]; |
| 81 | - _ := <span class="ty">XCHG</span>(<span class="ty">ADR</span>(r^.head), h + <span class="number">1</span>); <span class="comment">(* give the slot back *)</span> | |
| 82 | + _ := <span class="ty">XCHG</span>(r^.head, h + <span class="number">1</span>); <span class="comment">(* give the slot back *)</span> | |
| 82 | 83 | <span class="kw">return</span> v; |
| 83 | 84 | <span class="kw">end</span>; |
| 84 | 85 | |
| @@ -87,6 +88,7 @@ | ||
| 87 | 88 | <p>One thread sends the numbers 1 to 100000 through a ring of 64 slots, so it is stopped by a full ring many times over and has to wait for the consumer. The consumer checks each number arrives in order, and adds them.</p> |
| 88 | 89 | <div class="file"><span>Pipe.raz</span></div><pre class="code"><code><span class="kw">module</span> Pipe; |
| 89 | 90 | <span class="kw">import</span> Out, Fmt, Ring, Thread; |
| 91 | +<span class="kw">import</span> SYSTEM; | |
| 90 | 92 | |
| 91 | 93 | <span class="kw">const</span> Count = <span class="number">100000</span>; |
| 92 | 94 | |
| @@ -97,14 +99,14 @@ | ||
| 97 | 99 | <span class="kw">proc</span> producer(_arg: ^<span class="ty">U8</span>); |
| 98 | 100 | <span class="kw">var</span> i: <span class="ty">INT</span>; |
| 99 | 101 | <span class="kw">begin</span> |
| 100 | - <span class="kw">for</span> i := <span class="number">1</span> <span class="kw">to</span> Count <span class="kw">do</span> Ring.put(<span class="ty">ADR</span>(ring), i); <span class="kw">end</span>; | |
| 102 | + <span class="kw">for</span> i := <span class="number">1</span> <span class="kw">to</span> Count <span class="kw">do</span> Ring.put(SYSTEM.<span class="ty">ADR</span>(ring), i); <span class="kw">end</span>; | |
| 101 | 103 | <span class="kw">end</span>; |
| 102 | 104 | |
| 103 | 105 | <span class="kw">proc</span> consumer(_arg: ^<span class="ty">U8</span>); |
| 104 | 106 | <span class="kw">var</span> i, v: <span class="ty">INT</span>; |
| 105 | 107 | <span class="kw">begin</span> |
| 106 | 108 | <span class="kw">for</span> i := <span class="number">1</span> <span class="kw">to</span> Count <span class="kw">do</span> |
| 107 | - v := Ring.take(<span class="ty">ADR</span>(ring)); | |
| 109 | + v := Ring.take(SYSTEM.<span class="ty">ADR</span>(ring)); | |
| 108 | 110 | <span class="kw">if</span> v # i <span class="kw">then</span> <span class="ty">INC</span>(outOfOrder); <span class="kw">end</span>; |
| 109 | 111 | sum := sum + v; |
| 110 | 112 | <span class="kw">end</span>; |
| @@ -48,35 +48,35 @@ | ||
| 48 | 48 | <h2>The smallest program</h2> |
| 49 | 49 | <p>This program imports nothing. It makes the <code>write</code> system call itself, with the built-in <code>SYSCALL</code>, and returns.</p> |
| 50 | 50 | <div class="file"><span>Bare.raz</span></div><pre class="code"><code><span class="kw">module</span> Bare; |
| 51 | +<span class="kw">import</span> SYSTEM; | |
| 51 | 52 | |
| 52 | 53 | <span class="kw">const</span> Msg = <span class="string">"hello from raw system calls\n"</span>; |
| 53 | 54 | |
| 54 | 55 | <span class="kw">proc</span> main*; |
| 55 | 56 | <span class="kw">begin</span> |
| 56 | - _ := SYSCALL(<span class="number">1</span>, <span class="number">1</span>, <span class="ty">ADR</span>(Msg[<span class="number">0</span>]), <span class="ty">LEN</span>(Msg)); | |
| 57 | + _ := SYSTEM.SYSCALL(<span class="number">1</span>, <span class="number">1</span>, SYSTEM.<span class="ty">ADR</span>(Msg[<span class="number">0</span>]), <span class="ty">LEN</span>(Msg)); | |
| 57 | 58 | <span class="kw">end</span>; |
| 58 | 59 | |
| 59 | 60 | <span class="kw">end</span> Bare.</code></pre> |
| 60 | 61 | <pre class="output term"><code><span class="cmd">$ raz Bare.raz</span> |
| 62 | +Bare.raz:8:42: error: Msg[0] is read through a []const slice, and can't be changed | |
| 63 | + 8 | _ := SYSTEM.SYSCALL(1, 1, SYSTEM.ADR(Msg[0]), LEN(Msg)); | |
| 64 | + | ^^^^^^ | |
| 65 | +help: a []const slice's elements are only read: write them through a []T | |
| 66 | +raz: 1 error | |
| 61 | 67 | <span class="cmd">$ ./Bare</span> |
| 62 | -hello from raw system calls | |
| 68 | +/bin/sh: line 1: ./Bare: No such file or directory | |
| 63 | 69 | <span class="cmd">$ file Bare</span> |
| 64 | -Bare: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, with debug_info, not stripped | |
| 70 | +Bare: cannot open `Bare' (No such file or directory) | |
| 65 | 71 | <span class="cmd">$ ldd Bare</span> |
| 66 | - not a dynamic executable | |
| 72 | +ldd: ./Bare: No such file or directory | |
| 67 | 73 | <span class="cmd">$ ls -l Bare | awk '{print $5, "bytes"}'</span> |
| 68 | -26488 bytes</code></pre> | |
| 74 | +ls: cannot access 'Bare': No such file or directory</code></pre> | |
| 69 | 75 | <p>It is statically linked: no interpreter, no shared libraries, no <code>libc</code>. <code>ldd</code> says so.</p> |
| 70 | 76 | <h2>What it does when it runs</h2> |
| 71 | 77 | <p><code>strace</code> lists every system call a program makes. This is the whole of it:</p> |
| 72 | 78 | <pre class="output term"><code><span class="cmd">$ strace ./Bare 2>&1 >/dev/null | sed -e 's/0x[0-9a-f]\{5,\}/ADDR/g' -e 's/[0-9]* vars/N vars/'</span> |
| 73 | -execve("./Bare", ["./Bare"], ADDR /* N vars */) = 0 | |
| 74 | -sigaltstack({ss_sp=ADDR, ss_flags=0, ss_size=16384}, NULL) = 0 | |
| 75 | -rt_sigaction(SIGSEGV, {sa_handler=ADDR, sa_mask=[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_RESETHAND|SA_SIGINFO, sa_restorer=ADDR}, NULL, 8) = 0 | |
| 76 | -rt_sigaction(SIGFPE, {sa_handler=ADDR, sa_mask=[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_RESETHAND|SA_SIGINFO, sa_restorer=ADDR}, NULL, 8) = 0 | |
| 77 | -write(1, "hello from raw system calls\n", 28) = 28 | |
| 78 | -exit_group(0) = ? | |
| 79 | -+++ exited with 0 +++</code></pre> | |
| 79 | +strace: Cannot stat './Bare': No such file or directory</code></pre> | |
| 80 | 80 | <p>The start-up code does four things before <code>main</code> runs: it sets up a stack of its own for signal handlers (<code>sigaltstack</code>), and installs handlers for <code>SIGSEGV</code> and <code>SIGFPE</code>. That is what turns a <code>nil</code> pointer or a division by zero into a message that names the place, instead of a bare "Segmentation fault". Then <code>main</code> runs its single <code>write</code>, and the program ends with <code>exit_group</code>. Nothing is mapped, loaded, or looked up.</p> |
| 81 | 81 | <h2>Programs that use C</h2> |
| 82 | 82 | <p>A program that declares an <code>extern</code> procedure, or links a library, is a different animal. It is linked dynamically and starts through the C library, so that C's own set-up and clean-up run. Compare the same kind of hello, with C's <code>printf</code>:</p> |
| @@ -91,14 +91,21 @@ | ||
| 91 | 91 | |
| 92 | 92 | <span class="kw">end</span> Cprint.</code></pre> |
| 93 | 93 | <pre class="output term"><code><span class="cmd">$ raz Cprint.raz</span> |
| 94 | +Cprint.raz:3:1: error: extern needs import SYSTEM | |
| 95 | + 3 | extern proc puts(s: ^U8): I32; | |
| 96 | + | ^^^^^^ | |
| 97 | +help: it reaches memory other than through a variable: say import SYSTEM after the module's imports | |
| 98 | +Cprint.raz:7:15: error: a string as a ^U8 needs import SYSTEM | |
| 99 | + 7 | _ := puts("hello from C"); | |
| 100 | + | ^^^^^^^^^^^^^^ | |
| 101 | +help: it reaches memory other than through a variable: say import SYSTEM after the module's imports | |
| 102 | +raz: 2 errors | |
| 94 | 103 | <span class="cmd">$ ./Cprint</span> |
| 95 | -hello from C | |
| 104 | +/bin/sh: line 1: ./Cprint: No such file or directory | |
| 96 | 105 | <span class="cmd">$ file Cprint</span> |
| 97 | -Cprint: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, with debug_info, not stripped | |
| 106 | +Cprint: cannot open `Cprint' (No such file or directory) | |
| 98 | 107 | <span class="cmd">$ ldd Cprint | sed 's/ (0x[0-9a-f]*)//'</span> |
| 99 | - linux-vdso.so.1 | |
| 100 | - libc.so.6 => /usr/lib/libc.so.6 | |
| 101 | - /lib64/ld-linux-x86-64.so.2 => /usr/lib64/ld-linux-x86-64.so.2</code></pre> | |
| 108 | +ldd: ./Cprint: No such file or directory</code></pre> | |
| 102 | 109 | <p>Both are fine. The point is that you choose, and the language says which you chose: a program uses C exactly when a module of it has an <code>extern</code> or a <code>link</code> of a library.</p> |
| 103 | 110 | <h2>Only what is reached</h2> |
| 104 | 111 | <p>The compiler keeps only what can be reached from <code>main</code> and the module bodies. Unused procedures, variables and strings are left out. Here are two programs that differ by one large procedure that nothing calls.</p> |
| @@ -134,15 +141,15 @@ | ||
| 134 | 141 | <pre class="output term"><code><span class="cmd">$ raz Lean.raz</span> |
| 135 | 142 | <span class="cmd">$ raz Heavy.raz</span> |
| 136 | 143 | <span class="cmd">$ ls -l Lean Heavy | awk '{print $9, $5}'</span> |
| 137 | -Heavy 28680 | |
| 138 | -Lean 28672</code></pre> | |
| 144 | +Heavy 28608 | |
| 145 | +Lean 28600</code></pre> | |
| 139 | 146 | <p>The sizes differ by a few bytes, not by the hundreds of kilobytes that the table and the procedure would take: they are not in the executable at all. You can link in a library module without worrying what else it offers.</p> |
| 140 | 147 | <h2>Stripping</h2> |
| 141 | 148 | <p><code>-s</code> leaves out what is there for finding faults: the symbols and line numbers, the places a failure is told by, and the profiler. It is for the final build of a size-conscious program; the default keeps them so that gdb and failure messages work.</p> |
| 142 | 149 | <pre class="output term"><code><span class="cmd">$ raz -s -o Lean-stripped Lean.raz</span> |
| 143 | 150 | <span class="cmd">$ ls -l Lean Lean-stripped | awk '{print $9, $5}'</span> |
| 144 | -Lean 28672 | |
| 145 | -Lean-stripped 6920 | |
| 151 | +Lean 28600 | |
| 152 | +Lean-stripped 6856 | |
| 146 | 153 | <span class="cmd">$ ./Lean-stripped</span> |
| 147 | 154 | same program</code></pre> |
| 148 | 155 | <p><code>raz -S Lean.raz</code> is the other direction: it prints the machine code the compiler made, one source line at a time. The next two parts of the tour use it.</p><p class="tryit">Save a program under its module’s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="allocators.html">← Writing an allocator</a><a href="index.html">Contents</a><a class="next" href="linkstatic.html">Threads without C, and keeping the two apart →</a></div></article></div> |
| @@ -44,42 +44,49 @@ | ||
| 44 | 44 | <li><a href="lockfree.html">A lock-free stack</a></li> |
| 45 | 45 | <li><a href="ring.html">A ring buffer between threads</a></li> |
| 46 | 46 | </ol></aside><article><p class="kicker">The machine · lesson 12 of 18</p><h1>System calls</h1> |
| 47 | -<p>Under <code>Sys</code> there is one built-in: <code>SYSCALL(n, x1, ..., x6)</code>. It makes Linux system call number <code>n</code> with up to six arguments, each a pointer or an integer no wider than <code>INT</code>, and gives the raw result, an <code>INT</code>. A negative result is <code>-errno</code>. That is all the interface there is; <code>Sys.write</code>, <code>Sys.open</code> and the rest of <code>lib/Sys.linux-x86_64.raz</code> are written with it, in raz, in a few lines each.</p> | |
| 47 | +<p>Under <code>Sys</code> there is one built-in, <code>SYSTEM.SYSCALL(n, x1, ..., x6)</code>, which a module reaches with <code>import SYSTEM;</code>. It makes Linux system call number <code>n</code> with up to six arguments, each a pointer or an integer no wider than <code>INT</code>, and gives the raw result, an <code>INT</code>. A negative result is <code>-errno</code>. That is all the interface there is; <code>Sys.write</code>, <code>Sys.open</code> and the rest of <code>lib/Sys.linux-x86_64.raz</code> are written with it, in raz, in a few lines each.</p> | |
| 48 | 48 | <p><code>SYSCALL</code> exists only on Linux targets. Other systems have no stable system call interface, which is why their <code>Sys</code> calls the C library instead.</p> |
| 49 | 49 | <h2>The shape of a call</h2> |
| 50 | 50 | <p><code>write(fd, buf, count)</code> is call number 1. The arguments are the descriptor, a pointer to the bytes, and how many.</p> |
| 51 | 51 | <div class="file"><span>Raw.raz</span></div><pre class="code"><code><span class="kw">module</span> Raw; |
| 52 | +<span class="kw">import</span> SYSTEM; | |
| 52 | 53 | |
| 53 | 54 | <span class="kw">const</span> Msg = <span class="string">"straight to the kernel\n"</span>; |
| 54 | 55 | |
| 55 | 56 | <span class="kw">proc</span> main*; |
| 56 | 57 | <span class="kw">var</span> n: <span class="ty">INT</span>; |
| 57 | 58 | <span class="kw">begin</span> |
| 58 | - n := SYSCALL(<span class="number">1</span>, <span class="number">1</span>, <span class="ty">ADR</span>(Msg[<span class="number">0</span>]), <span class="ty">LEN</span>(Msg)); | |
| 59 | - <span class="kw">if</span> n # <span class="ty">LEN</span>(Msg) <span class="kw">then</span> _ := SYSCALL(<span class="number">60</span>, <span class="number">1</span>); <span class="kw">end</span>; <span class="comment">(* exit(1), if it came up short *)</span> | |
| 59 | + n := SYSTEM.SYSCALL(<span class="number">1</span>, <span class="number">1</span>, SYSTEM.<span class="ty">ADR</span>(Msg[<span class="number">0</span>]), <span class="ty">LEN</span>(Msg)); | |
| 60 | + <span class="kw">if</span> n # <span class="ty">LEN</span>(Msg) <span class="kw">then</span> _ := SYSTEM.SYSCALL(<span class="number">60</span>, <span class="number">1</span>); <span class="kw">end</span>; <span class="comment">(* exit(1), if it came up short *)</span> | |
| 60 | 61 | <span class="kw">end</span>; |
| 61 | 62 | |
| 62 | 63 | <span class="kw">end</span> Raw.</code></pre> |
| 63 | 64 | <pre class="output term"><code><span class="cmd">$ raz -Werror Raw.raz</span> |
| 65 | +Raw.raz:9:42: error: Msg[0] is read through a []const slice, and can't be changed | |
| 66 | + 9 | n := SYSTEM.SYSCALL(1, 1, SYSTEM.ADR(Msg[0]), LEN(Msg)); | |
| 67 | + | ^^^^^^ | |
| 68 | +help: a []const slice's elements are only read: write them through a []T | |
| 69 | +raz: 1 error | |
| 64 | 70 | <span class="cmd">$ ./Raw</span> |
| 65 | -straight to the kernel</code></pre> | |
| 71 | +/bin/sh: line 1: ./Raw: No such file or directory</code></pre> | |
| 66 | 72 | <p>That program has no <code>Out</code>, no <code>Sys</code>, and no output buffer. What it needs of the library is <code>Rt</code>'s start-up, which the compiler links in anyway.</p> |
| 67 | 73 | <h2>Reading the result</h2> |
| 68 | 74 | <p>A result that is negative is a failure, and its negation is the <code>errno</code>. <code>open</code> is call 2 and wants a C string: the path's bytes followed by a zero. A <code>[N]U8</code> made from a string literal is zero-filled past its length, so there is always a terminator.</p> |
| 69 | 75 | <div class="file"><span>Open.raz</span></div><pre class="code"><code><span class="kw">module</span> Open; |
| 70 | 76 | <span class="kw">import</span> Out; |
| 77 | +<span class="kw">import</span> SYSTEM; | |
| 71 | 78 | |
| 72 | 79 | <span class="kw">proc</span> main*; |
| 73 | 80 | <span class="kw">var</span> path: [<span class="number">32</span>]<span class="ty">U8</span>; fd: <span class="ty">INT</span>; |
| 74 | 81 | <span class="kw">begin</span> |
| 75 | 82 | path := <span class="string">"/no/such/file"</span>; |
| 76 | - fd := SYSCALL(<span class="number">2</span>, <span class="ty">ADR</span>(path[<span class="number">0</span>]), <span class="number">0</span>, <span class="number">0</span>); <span class="comment">(* open(path, O_RDONLY) *)</span> | |
| 83 | + fd := SYSTEM.SYSCALL(<span class="number">2</span>, SYSTEM.<span class="ty">ADR</span>(path[<span class="number">0</span>]), <span class="number">0</span>, <span class="number">0</span>); <span class="comment">(* open(path, O_RDONLY) *)</span> | |
| 77 | 84 | Out.str(<span class="string">"open gave "</span>); Out.int(fd); Out.ln; <span class="comment">(* -2: ENOENT *)</span> |
| 78 | 85 | path := <span class="string">"/dev/null"</span>; |
| 79 | - fd := SYSCALL(<span class="number">2</span>, <span class="ty">ADR</span>(path[<span class="number">0</span>]), <span class="number">0</span>, <span class="number">0</span>); | |
| 86 | + fd := SYSTEM.SYSCALL(<span class="number">2</span>, SYSTEM.<span class="ty">ADR</span>(path[<span class="number">0</span>]), <span class="number">0</span>, <span class="number">0</span>); | |
| 80 | 87 | <span class="kw">if</span> fd >= <span class="number">0</span> <span class="kw">then</span> |
| 81 | 88 | Out.str(<span class="string">"opened /dev/null\n"</span>); |
| 82 | - _ := SYSCALL(<span class="number">3</span>, fd); <span class="comment">(* close *)</span> | |
| 89 | + _ := SYSTEM.SYSCALL(<span class="number">3</span>, fd); <span class="comment">(* close *)</span> | |
| 83 | 90 | <span class="kw">end</span>; |
| 84 | 91 | <span class="kw">end</span>; |
| 85 | 92 | |
| @@ -89,12 +96,13 @@ | ||
| 89 | 96 | </code></pre> |
| 90 | 97 | <p>The library's <code>Sys.openRead</code> does exactly this, and <code>Sys.close</code> too. Reading <code>lib/Sys.linux-x86_64.raz</code> is the best reference there is for what each call wants.</p> |
| 91 | 98 | <h2>A call that fills a structure</h2> |
| 92 | -<p><code>uname</code> (call 63) fills a structure of six 65-byte fields: the system name, node name, release, version, machine and domain. It is not a type raz knows; it is an array of bytes, with a <code>ADR</code> of it given to the kernel.</p> | |
| 99 | +<p><code>uname</code> (call 63) fills a structure of six 65-byte fields: the system name, node name, release, version, machine and domain. It is not a type raz knows; it is an array of bytes, with its <code>SYSTEM.ADR</code> given to the kernel.</p> | |
| 93 | 100 | <div class="file"><span>Who.raz</span></div><pre class="code"><code><span class="kw">module</span> Who; |
| 94 | 101 | <span class="kw">import</span> Out; |
| 102 | +<span class="kw">import</span> SYSTEM; | |
| 95 | 103 | |
| 96 | 104 | <span class="comment">(* the length of the zero-ended string at the start of s *)</span> |
| 97 | -<span class="kw">proc</span> cstr(s: []<span class="ty">U8</span>): <span class="ty">INT</span>; | |
| 105 | +<span class="kw">proc</span> cstr(s: []<span class="kw">const</span> <span class="ty">U8</span>): <span class="ty">INT</span>; | |
| 98 | 106 | <span class="kw">var</span> n: <span class="ty">INT</span>; |
| 99 | 107 | <span class="kw">begin</span> |
| 100 | 108 | n := <span class="number">0</span>; |
| @@ -105,7 +113,7 @@ | ||
| 105 | 113 | <span class="kw">proc</span> main*; |
| 106 | 114 | <span class="kw">var</span> u: [<span class="number">390</span>]<span class="ty">U8</span>; |
| 107 | 115 | <span class="kw">begin</span> |
| 108 | - <span class="kw">if</span> SYSCALL(<span class="number">63</span>, <span class="ty">ADR</span>(u[<span class="number">0</span>])) = <span class="number">0</span> <span class="kw">then</span> | |
| 116 | + <span class="kw">if</span> SYSTEM.SYSCALL(<span class="number">63</span>, SYSTEM.<span class="ty">ADR</span>(u[<span class="number">0</span>])) = <span class="number">0</span> <span class="kw">then</span> | |
| 109 | 117 | Out.str(<span class="string">"system: "</span>); Out.str(u[<span class="number">0</span>..cstr(u[<span class="number">0</span>..<span class="number">65</span>])]); Out.ln; |
| 110 | 118 | Out.str(<span class="string">"machine: "</span>); Out.str(u[<span class="number">260</span>..<span class="number">260</span> + cstr(u[<span class="number">260</span>..<span class="number">325</span>])]); Out.ln; |
| 111 | 119 | <span class="kw">end</span>; |
| @@ -119,18 +127,19 @@ | ||
| 119 | 127 | <p><code>mmap</code> is call 9: six arguments, the most one call can take. This asks for a page of anonymous, zeroed memory, writes in it, and gives it back with <code>munmap</code> (call 11). It is what <code>Mem.pages</code> does, and so what every allocator is ultimately made of.</p> |
| 120 | 128 | <div class="file"><span>Pages.raz</span></div><pre class="code"><code><span class="kw">module</span> Pages; |
| 121 | 129 | <span class="kw">import</span> Out; |
| 130 | +<span class="kw">import</span> SYSTEM; | |
| 122 | 131 | |
| 123 | 132 | <span class="kw">proc</span> main*; |
| 124 | 133 | <span class="kw">var</span> p: ^<span class="ty">U8</span>; i: <span class="ty">INT</span>; total: <span class="ty">INT</span>; |
| 125 | 134 | <span class="kw">begin</span> |
| 126 | 135 | <span class="comment">(* length 4096; PROT_READ|WRITE = 3; MAP_PRIVATE|ANONYMOUS = 22H; no fd *)</span> |
| 127 | - p := ^<span class="ty">U8</span>(SYSCALL(<span class="number">9</span>, <span class="number">0</span>, <span class="number">4096</span>, <span class="number">3</span>, <span class="number">22H</span>, -<span class="number">1</span>, <span class="number">0</span>)); | |
| 136 | + p := ^<span class="ty">U8</span>(SYSTEM.SYSCALL(<span class="number">9</span>, <span class="number">0</span>, <span class="number">4096</span>, <span class="number">3</span>, <span class="number">22H</span>, -<span class="number">1</span>, <span class="number">0</span>)); | |
| 128 | 137 | <span class="kw">if</span> <span class="ty">INT</span>(p) < <span class="number">0</span> <span class="kw">then</span> Out.str(<span class="string">"no memory\n"</span>); <span class="kw">return</span>; <span class="kw">end</span>; |
| 129 | 138 | Out.int(<span class="ty">INT</span>(p[<span class="number">0</span>]) + p[<span class="number">4095</span>]); Out.str(<span class="string">" (zero, as promised)\n"</span>); |
| 130 | 139 | <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="number">4095</span> <span class="kw">do</span> p[i] := <span class="ty">U8</span>(i); <span class="kw">end</span>; |
| 131 | 140 | <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="number">4095</span> <span class="kw">do</span> <span class="ty">INC</span>(total, p[i]); <span class="kw">end</span>; |
| 132 | 141 | Out.int(total); Out.ln; |
| 133 | - _ := SYSCALL(<span class="number">11</span>, p, <span class="number">4096</span>); | |
| 142 | + _ := SYSTEM.SYSCALL(<span class="number">11</span>, p, <span class="number">4096</span>); | |
| 134 | 143 | <span class="kw">end</span>; |
| 135 | 144 | |
| 136 | 145 | <span class="kw">end</span> Pages.</code></pre> |
| @@ -140,19 +149,20 @@ | ||
| 140 | 149 | <h2>A cat with no library</h2> |
| 141 | 150 | <p>Reading and writing in a loop is all <code>cat</code> does. With system calls, it is one procedure. Call 0 is <code>read</code> and call 1 is <code>write</code>; 0 and 1 are standard input and output.</p> |
| 142 | 151 | <div class="file"><span>Cat.raz</span></div><pre class="code"><code><span class="kw">module</span> Cat; |
| 152 | +<span class="kw">import</span> SYSTEM; | |
| 143 | 153 | |
| 144 | 154 | <span class="kw">proc</span> main*: <span class="ty">INT</span>; |
| 145 | 155 | <span class="kw">var</span> buf: [<span class="number">4096</span>]<span class="ty">U8</span>; n, wrote, at: <span class="ty">INT</span>; |
| 146 | 156 | <span class="kw">begin</span> |
| 147 | - n := SYSCALL(<span class="number">0</span>, <span class="number">0</span>, <span class="ty">ADR</span>(buf[<span class="number">0</span>]), <span class="ty">LEN</span>(buf)); | |
| 157 | + n := SYSTEM.SYSCALL(<span class="number">0</span>, <span class="number">0</span>, SYSTEM.<span class="ty">ADR</span>(buf[<span class="number">0</span>]), <span class="ty">LEN</span>(buf)); | |
| 148 | 158 | <span class="kw">while</span> n > <span class="number">0</span> <span class="kw">do</span> |
| 149 | 159 | at := <span class="number">0</span>; |
| 150 | 160 | <span class="kw">while</span> at < n <span class="kw">do</span> <span class="comment">(* a write may be partial *)</span> |
| 151 | - wrote := SYSCALL(<span class="number">1</span>, <span class="number">1</span>, <span class="ty">ADR</span>(buf[at]), n - at); | |
| 161 | + wrote := SYSTEM.SYSCALL(<span class="number">1</span>, <span class="number">1</span>, SYSTEM.<span class="ty">ADR</span>(buf[at]), n - at); | |
| 152 | 162 | <span class="kw">if</span> wrote < <span class="number">0</span> <span class="kw">then</span> <span class="kw">return</span> <span class="number">1</span>; <span class="kw">end</span>; |
| 153 | 163 | <span class="ty">INC</span>(at, wrote); |
| 154 | 164 | <span class="kw">end</span>; |
| 155 | - n := SYSCALL(<span class="number">0</span>, <span class="number">0</span>, <span class="ty">ADR</span>(buf[<span class="number">0</span>]), <span class="ty">LEN</span>(buf)); | |
| 165 | + n := SYSTEM.SYSCALL(<span class="number">0</span>, <span class="number">0</span>, SYSTEM.<span class="ty">ADR</span>(buf[<span class="number">0</span>]), <span class="ty">LEN</span>(buf)); | |
| 156 | 166 | <span class="kw">end</span>; |
| 157 | 167 | <span class="kw">if</span> n < <span class="number">0</span> <span class="kw">then</span> <span class="kw">return</span> <span class="number">1</span>; <span class="kw">end</span>; |
| 158 | 168 | <span class="kw">return</span> <span class="number">0</span>; |
| @@ -173,6 +183,7 @@ | ||
| 173 | 183 | <p>A system call is also an instruction. <code>syscall</code> takes its number in <code>rax</code> and its arguments in <code>rdi</code>, <code>rsi</code>, <code>rdx</code>, <code>r10</code>, <code>r8</code> and <code>r9</code>, which is not quite the order a procedure's arguments arrive in, since <code>r10</code> takes the place of <code>rcx</code>, and <code>syscall</code> itself clobbers <code>rcx</code> and <code>r11</code>.</p> |
| 174 | 184 | <div class="file"><span>Ppid.raz</span></div><pre class="code"><code><span class="kw">module</span> Ppid; |
| 175 | 185 | <span class="kw">import</span> Out; |
| 186 | +<span class="kw">import</span> SYSTEM; | |
| 176 | 187 | |
| 177 | 188 | <span class="comment">(* getppid, call 110: no arguments *)</span> |
| 178 | 189 | <span class="kw">proc</span> getppid: <span class="ty">INT</span>; |
| @@ -184,7 +195,7 @@ | ||
| 184 | 195 | |
| 185 | 196 | <span class="kw">proc</span> main*; |
| 186 | 197 | <span class="kw">begin</span> |
| 187 | - <span class="kw">if</span> (getppid() > <span class="number">0</span>) <span class="kw">and</span> (SYSCALL(<span class="number">110</span>) = getppid()) <span class="kw">then</span> | |
| 198 | + <span class="kw">if</span> (getppid() > <span class="number">0</span>) <span class="kw">and</span> (SYSTEM.SYSCALL(<span class="number">110</span>) = getppid()) <span class="kw">then</span> | |
| 188 | 199 | Out.str(<span class="string">"two ways to ask, one answer\n"</span>); |
| 189 | 200 | <span class="kw">end</span>; |
| 190 | 201 | <span class="kw">end</span>; |
| @@ -50,6 +50,7 @@ | ||
| 50 | 50 | <p>The classic use is a device's registers: a record that says what is at which offset, laid over the address the hardware answers at. The record's layout is C's, so it is the same as the datasheet's struct. Here a page of the program's own stands in for the device.</p> |
| 51 | 51 | <div class="file"><span>Uart.raz</span></div><pre class="code"><code><span class="kw">module</span> Uart; |
| 52 | 52 | <span class="kw">import</span> Out; |
| 53 | +<span class="kw">import</span> SYSTEM; | |
| 53 | 54 | |
| 54 | 55 | <span class="kw">type</span> |
| 55 | 56 | Regs = <span class="kw">record</span> |
| @@ -63,7 +64,7 @@ | ||
| 63 | 64 | <span class="kw">proc</span> main*; |
| 64 | 65 | <span class="kw">var</span> dev: ^<span class="kw">VOLATILE</span> Regs; i: <span class="ty">INT</span>; |
| 65 | 66 | <span class="kw">begin</span> |
| 66 | - dev := ^<span class="kw">VOLATILE</span> Regs(<span class="ty">ADR</span>(page)); <span class="comment">(* the conversion is written *)</span> | |
| 67 | + dev := ^<span class="kw">VOLATILE</span> Regs(SYSTEM.<span class="ty">ADR</span>(page)); <span class="comment">(* the conversion is written *)</span> | |
| 67 | 68 | dev^.control := <span class="number">1</span>; |
| 68 | 69 | dev^.status := <span class="number">0FFH</span>; |
| 69 | 70 | <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="number">2</span> <span class="kw">do</span> |
| @@ -90,6 +91,7 @@ | ||
| 90 | 91 | <p>Where a program waits for another thread or a device to change a value, the loop must read the value each time. A volatile read does. This "device" is a thread that sets a bit in a status word after a delay; the main thread polls.</p> |
| 91 | 92 | <div class="file"><span>Poll.raz</span></div><pre class="code"><code><span class="kw">module</span> Poll; |
| 92 | 93 | <span class="kw">import</span> Out, Sys, Thread; |
| 94 | +<span class="kw">import</span> SYSTEM; | |
| 93 | 95 | |
| 94 | 96 | <span class="kw">type</span> Dev = <span class="kw">record</span> status: <span class="ty">INT</span>; data: <span class="ty">INT</span>; <span class="kw">end</span>; |
| 95 | 97 | |
| @@ -98,7 +100,7 @@ | ||
| 98 | 100 | <span class="kw">proc</span> device(_arg: ^<span class="ty">U8</span>); |
| 99 | 101 | <span class="kw">var</span> d: ^<span class="kw">VOLATILE</span> Dev; |
| 100 | 102 | <span class="kw">begin</span> |
| 101 | - d := ^<span class="kw">VOLATILE</span> Dev(<span class="ty">ADR</span>(dev)); | |
| 103 | + d := ^<span class="kw">VOLATILE</span> Dev(SYSTEM.<span class="ty">ADR</span>(dev)); | |
| 102 | 104 | Sys.sleep(<span class="number">20</span>); |
| 103 | 105 | d^.data := <span class="number">42</span>; |
| 104 | 106 | FENCE; <span class="comment">(* data before the flag that announces it *)</span> |
| @@ -108,7 +110,7 @@ | ||
| 108 | 110 | <span class="kw">proc</span> main*; |
| 109 | 111 | <span class="kw">var</span> t: ^Thread.Thread; err: <span class="ty">ERROR</span>; d: ^<span class="kw">VOLATILE</span> Dev; spins: <span class="ty">INT</span>; |
| 110 | 112 | <span class="kw">begin</span> |
| 111 | - d := ^<span class="kw">VOLATILE</span> Dev(<span class="ty">ADR</span>(dev)); | |
| 113 | + d := ^<span class="kw">VOLATILE</span> Dev(SYSTEM.<span class="ty">ADR</span>(dev)); | |
| 112 | 114 | t, err := Thread.spawn(device, <span class="ty">nil</span>); |
| 113 | 115 | <span class="kw">if</span> err # <span class="ty">nil</span> <span class="kw">then</span> Out.str(<span class="ty">MSG</span>(err)); Out.ln; <span class="kw">return</span>; <span class="kw">end</span>; |
| 114 | 116 | <span class="kw">while</span> d^.status = <span class="number">0</span> <span class="kw">do</span> <span class="ty">INC</span>(spins); Sys.yield; <span class="kw">end</span>; |
| @@ -126,6 +128,7 @@ | ||
| 126 | 128 | <ul><li><strong>Not atomic across a read-modify-write.</strong> <code>INC(d^.count)</code> reads and then writes: two accesses, even through a volatile pointer. <code>XADD</code> is the atomic one.</li><li><strong>Not a lock.</strong> It says each access happens as written; it says nothing about what happens between them.</li><li><strong>Not for whole records or arrays.</strong> Copying a whole record through a volatile pointer isn't allowed, since it can't be one access. A field of a packed record can't be reached volatilely at all, since a misaligned access can't be promised as one.</li><li><strong>Not silently dropped.</strong> A <code>^VOLATILE T</code> doesn't convert to a <code>^T</code> without a written conversion, so the volatile can't be lost by accident.</li></ul> |
| 127 | 129 | <p>The compiler holds the line on each of them:</p> |
| 128 | 130 | <div class="file"><span>Wrong.raz</span></div><pre class="code"><code><span class="kw">module</span> Wrong; |
| 131 | +<span class="kw">import</span> SYSTEM; | |
| 129 | 132 | |
| 130 | 133 | <span class="kw">type</span> Pair = <span class="kw">record</span> a, b: <span class="ty">U32</span>; <span class="kw">end</span>; |
| 131 | 134 | |
| @@ -134,18 +137,18 @@ | ||
| 134 | 137 | <span class="kw">proc</span> main*; |
| 135 | 138 | <span class="kw">var</span> v: ^<span class="kw">VOLATILE</span> Pair; p: ^Pair; copy: Pair; |
| 136 | 139 | <span class="kw">begin</span> |
| 137 | - v := ^<span class="kw">VOLATILE</span> Pair(<span class="ty">ADR</span>(cell)); | |
| 140 | + v := ^<span class="kw">VOLATILE</span> Pair(SYSTEM.<span class="ty">ADR</span>(cell)); | |
| 138 | 141 | p := v; |
| 139 | 142 | copy := v^; |
| 140 | 143 | <span class="kw">end</span>; |
| 141 | 144 | |
| 142 | 145 | <span class="kw">end</span> Wrong.</code></pre> |
| 143 | 146 | <pre class="output term"><code><span class="cmd">$ raz Wrong.raz</span> |
| 144 | -Wrong.raz:11:10: error: can't use ^VOLATILE Pair as ^Pair | |
| 145 | - 11 | p := v; | |
| 147 | +Wrong.raz:12:10: error: can't use ^VOLATILE Pair as ^Pair | |
| 148 | + 12 | p := v; | |
| 146 | 149 | | ^ |
| 147 | -Wrong.raz:12:13: error: can't copy through a volatile pointer | |
| 148 | - 12 | copy := v^; | |
| 150 | +Wrong.raz:13:13: error: can't copy through a volatile pointer | |
| 151 | + 13 | copy := v^; | |
| 149 | 152 | | ^^ |
| 150 | 153 | raz: 2 errors</code></pre><p class="tryit">Save a program under its module’s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="listing.html">← What the compiler makes</a><a href="index.html">Contents</a><a class="next" href="atomics.html">Atomics →</a></div></article></div> |
| 151 | 154 | </main> |
Binary file; the patch has it.
| @@ -0,0 +1 @@ | ||
| 1 | +3c6cac9b9ce8acc2a5284c426000440cb05a8d322cc693bfd552b9b520d935ec raz-0.24.0-linux-x86_64.tar.gz |
| @@ -114,24 +114,24 @@ | ||
| 114 | 114 | <a href="raz-lib.pdf">the library</a>.</p> |
| 115 | 115 | |
| 116 | 116 | <h2 id="download">Download</h2> |
| 117 | -<p>raz 0.23.0 is a tarball for x86-64 Linux with the compiler, <code>raz</code>, | |
| 117 | +<p>raz 0.24.0 is a tarball for x86-64 Linux with the compiler, <code>raz</code>, | |
| 118 | 118 | its library, <code>lib/</code>, which it finds beside itself, the formatter |
| 119 | 119 | and the documentation tool as sources in <code>tools/</code>, and the manual |
| 120 | 120 | pages. It needs nothing else installed.</p> |
| 121 | 121 | <div class="cta"> |
| 122 | - <a class="btn primary" href="download/raz-0.23.0-linux-x86_64.tar.gz">Download raz 0.23.0</a> | |
| 123 | - <a class="btn" href="download/raz-0.23.0-linux-x86_64.tar.gz.sha256">SHA-256</a> | |
| 122 | + <a class="btn primary" href="download/raz-0.24.0-linux-x86_64.tar.gz">Download raz 0.24.0</a> | |
| 123 | + <a class="btn" href="download/raz-0.24.0-linux-x86_64.tar.gz.sha256">SHA-256</a> | |
| 124 | 124 | </div> |
| 125 | -<pre class="code"><code>$ tar xzf raz-0.23.0-linux-x86_64.tar.gz | |
| 126 | -$ ln -s $PWD/raz-0.23.0-linux-x86_64/raz ~/.local/bin/raz | |
| 125 | +<pre class="code"><code>$ tar xzf raz-0.24.0-linux-x86_64.tar.gz | |
| 126 | +$ ln -s $PWD/raz-0.24.0-linux-x86_64/raz ~/.local/bin/raz | |
| 127 | 127 | $ raz run Hello.raz</code></pre> |
| 128 | 128 | <p>The link works since raz finds <code>lib/</code> beside its real path. The |
| 129 | 129 | manual pages are in <code>man/</code>: <code>man ./man/raz-tutorial.7</code>. |
| 130 | 130 | The address <code>download/latest</code> always leads to the newest release, |
| 131 | -and <code>download/raz-0.23.0-linux-x86_64.tar.gz</code> to this one, for | |
| 132 | -good. Earlier releases: <a href="download/raz-0.22.0-linux-x86_64.tar.gz">0.22.0</a>, <a href="download/raz-0.21.0-linux-x86_64.tar.gz">0.21.0</a>, <a href="download/raz-0.20.0-linux-x86_64.tar.gz">0.20.0</a>, <a href="download/raz-0.19.0-linux-x86_64.tar.gz">0.19.0</a>, <a href="download/raz-0.18.1-linux-x86_64.tar.gz">0.18.1</a>, <a href="download/raz-0.18.0-linux-x86_64.tar.gz">0.18.0</a>, <a href="download/raz-0.17.2-linux-x86_64.tar.gz">0.17.2</a>, <a href="download/raz-0.17.1-linux-x86_64.tar.gz">0.17.1</a>, <a href="download/raz-0.17.0-linux-x86_64.tar.gz">0.17.0</a>, <a href="download/raz-0.16.0-linux-x86_64.tar.gz">0.16.0</a>, <a href="download/raz-0.15.0-linux-x86_64.tar.gz">0.15.0</a>, <a href="download/raz-0.14.0-linux-x86_64.tar.gz">0.14.0</a>, <a href="download/raz-0.13.0-linux-x86_64.tar.gz">0.13.0</a>, <a href="download/raz-0.12.0-linux-x86_64.tar.gz">0.12.0</a>, <a href="download/raz-0.11.1-linux-x86_64.tar.gz">0.11.1</a>, <a href="download/raz-0.11.0-linux-x86_64.tar.gz">0.11.0</a>, <a href="download/raz-0.10.1-linux-x86_64.tar.gz">0.10.1</a>, <a href="download/raz-0.10.0-linux-x86_64.tar.gz">0.10.0</a>, <a href="download/raz-0.9.1-linux-x86_64.tar.gz">0.9.1</a>, <a href="download/raz-0.9.0-linux-x86_64.tar.gz">0.9.0</a>, <a href="download/raz-0.8.0-linux-x86_64.tar.gz">0.8.0</a>, <a href="download/raz-0.7.1-linux-x86_64.tar.gz">0.7.1</a>; each has its SHA-256 beside it, with | |
| 131 | +and <code>download/raz-0.24.0-linux-x86_64.tar.gz</code> to this one, for | |
| 132 | +good. Earlier releases: <a href="download/raz-0.23.0-linux-x86_64.tar.gz">0.23.0</a>, <a href="download/raz-0.22.0-linux-x86_64.tar.gz">0.22.0</a>, <a href="download/raz-0.21.0-linux-x86_64.tar.gz">0.21.0</a>, <a href="download/raz-0.20.0-linux-x86_64.tar.gz">0.20.0</a>, <a href="download/raz-0.19.0-linux-x86_64.tar.gz">0.19.0</a>, <a href="download/raz-0.18.1-linux-x86_64.tar.gz">0.18.1</a>, <a href="download/raz-0.18.0-linux-x86_64.tar.gz">0.18.0</a>, <a href="download/raz-0.17.2-linux-x86_64.tar.gz">0.17.2</a>, <a href="download/raz-0.17.1-linux-x86_64.tar.gz">0.17.1</a>, <a href="download/raz-0.17.0-linux-x86_64.tar.gz">0.17.0</a>, <a href="download/raz-0.16.0-linux-x86_64.tar.gz">0.16.0</a>, <a href="download/raz-0.15.0-linux-x86_64.tar.gz">0.15.0</a>, <a href="download/raz-0.14.0-linux-x86_64.tar.gz">0.14.0</a>, <a href="download/raz-0.13.0-linux-x86_64.tar.gz">0.13.0</a>, <a href="download/raz-0.12.0-linux-x86_64.tar.gz">0.12.0</a>, <a href="download/raz-0.11.1-linux-x86_64.tar.gz">0.11.1</a>, <a href="download/raz-0.11.0-linux-x86_64.tar.gz">0.11.0</a>, <a href="download/raz-0.10.1-linux-x86_64.tar.gz">0.10.1</a>, <a href="download/raz-0.10.0-linux-x86_64.tar.gz">0.10.0</a>, <a href="download/raz-0.9.1-linux-x86_64.tar.gz">0.9.1</a>, <a href="download/raz-0.9.0-linux-x86_64.tar.gz">0.9.0</a>, <a href="download/raz-0.8.0-linux-x86_64.tar.gz">0.8.0</a>, <a href="download/raz-0.7.1-linux-x86_64.tar.gz">0.7.1</a>; each has its SHA-256 beside it, with | |
| 133 | 133 | <code>.sha256</code> added to its name.</p> |
| 134 | -<p class="sha">SHA-256 <code>103eb421a7fcca1c1495c1a9c699297d460ab72067fa04854da8f21edc07347c</code></p> | |
| 134 | +<p class="sha">SHA-256 <code>3c6cac9b9ce8acc2a5284c426000440cb05a8d322cc693bfd552b9b520d935ec</code></p> | |
| 135 | 135 | <p><a href="source/releases/index.html">Release pages</a> give each |
| 136 | 136 | version's commit, notes, compiler and source archives, with checksums. |
| 137 | 137 | Browse the <a href="source/index.html">source and history</a>, or clone: |
Binary file; the patch has it.
Binary file; the patch has it.
| @@ -38,7 +38,7 @@ | ||
| 38 | 38 | <p>raz is a small systems language, first made for x86-64 Linux. A raz program can lay a record over a device’s registers, read and write a device’s memory exactly as written, take part in lock-free algorithms, make system calls and call C libraries. It has no garbage collector and no hidden run time: what the program does is what its text says.</p> |
| 39 | 39 | <p>This is the first volume of the report: the language, what an implementation chooses, and the grammar. The second, <code>doc/raz-lib.md</code>, is the library, which sections 3 and 10 and appendix A name.</p> |
| 40 | 40 | <p>This report defines the language. It is short on purpose. Whatever it doesn’t allow, raz doesn’t have.</p> |
| 41 | -<p>This is version 2026-10.37 of the report; section 11 says where each version’s changes are told. 2026-10.11 and 2026-10.12 are a thaw the owner chose, for a language anyone can build again. They break programs written for 2026-10.10 and before: for 2026-10.9, say, the report of compiler 0.5.0, the last release before them; and 2026-10.12 breaks code procedures written for 2026-10.11. 2026-10.20 is a third, for <code>try</code> (section 7.8); it breaks a program that has a name <code>try</code>. 2026-10.25 is a fourth, for strings: raw strings, which may run over lines and may be indented with the code around them (section 2), and <code>match</code> on a string (section 7.4); it breaks no program. 2026-10.26 is a fifth: an <code>INT</code> widens to an <code>I64</code> wherever it goes to one, in an expression too (sections 5.10 and 6.1); and a view, <code>v.(T)</code>, reads and writes the bytes of a variable as a <code>T</code> (section 6.9); it breaks no program. 2026-10.27 mends a defect: returning a slice of the procedure’s own frame is an error (section 7.3), which breaks only a program that read memory already gone. 2026-10.28 mends another: an array a call returns lives to the end of its statement, and a slice of it kept past that is undefined (sections 8 and 10). 2026-10.29 is a sixth thaw: an array constant typed <code>[]T</code> takes its length from its literal (section 4); it breaks no program. 2026-10.30 mends a defect: <code>[]</code> used as a slice is the empty slice, which may be returned (sections 6.5 and 7.3); it breaks no program. 2026-10.31 is a seventh thaw: <code>INT</code> is at least 16 bits, and <code>U8</code>, <code>I8</code> and <code>I16</code> widen to it (sections 5.1, 5.10 and 6.1); an enumeration’s name may be written with its type, and must be where another declaration shares it (section 5.7); and <code>continue</code> (section 7.2), which breaks a program that has a name <code>continue</code>. 2026-10.32 is an eighth thaw: a constant typed <code>[]T</code> may be <code>[]</code>, the empty slice (section 4); it breaks no program. 2026-10.33 is a ninth: a <code>const</code> parameter, the caller’s variable read in place (section 8), and a variant in a constant (section 4), which break no program; and the library’s procedures that took a pointer to what they work on take a <code>var</code> parameter, a <code>const</code> one or a value instead (the library volume), which breaks a program that calls them with <code>ADR</code>. 2026-10.34 is a tenth: <code>PTR(s)</code>, the address a slice starts at, even an empty one (section 6.8); it breaks no program. 2026-10.35 is an eleventh: <code>ROL(x, n)</code> and <code>ROR(x, n)</code>, a word rotated (sections 6.1 and 6.8); it breaks no program. 2026-10.36 is a twelfth: <code>MIN(T)</code> and <code>MAX(T)</code>, the limits of an integer type, and <code>CLZ</code>, <code>CTZ</code> and <code>POPCOUNT</code>, which count bits (sections 6.1 and 6.8); <code>ABS(x)</code>, a number without its sign (sections 6.1 and 6.8); <code>INT(b)</code>, a <code>BOOL</code> as an integer (section 6.7); <code>OFFSET(T, f)</code> and <code>SIZE(T.f)</code>, where a field lies and how big it is; <code>BYTES(x)</code>, the bytes of a variable or a slice (sections 4 and 6.8); <code>FILL(d, x)</code>, an array or a slice set to one value (section 6.8); a qualified case in a <code>match</code> arm (section 7.4); a slice part written as a literal, and an <code>ERROR</code> constant named by another (sections 4 and 5.8); and a <code>const</code> parameter’s slice returned (section 7.3); and a procedure type’s parameter or result may name a type declared later in its <code>type</code> section (sections 4 and 5.9); it breaks no program. 2026-10.37 is a thirteenth thaw, for safety. It mends a defect: an array a call returns, or a record or variant holding one, lives to the end of the procedure the call is in, so a slice of it may be kept (section 8). <code>CAS</code>, <code>XCHG</code> and <code>XADD</code> take the variable they work on (section 6.8). <code>[]const T</code> is a slice whose elements can’t be written through it, which a string literal, a slice of a constant, <code>MSG</code> and a slice of a <code>const</code> parameter’s array are (sections 4, 5.3, 5.10, 6.8 and 8). And <code>import SYSTEM</code> is what a module says to reach memory other than through its variables: <code>SYSTEM.ADR</code>, <code>SYSTEM.PTR</code>, <code>SYSTEM.SYSCALL</code> and <code>SYSTEM.ARGV</code>, a pointer’s arithmetic and conversions, C, code procedures and <code>check off;</code> (section 3); section 10 says what is left undefined outside such modules. It breaks a program that gives an atomic a pointer, writes through a string or a constant, or does what <code>SYSTEM</code> holds without importing it; raz 0.23 still takes all of these as before, until 0.24.</p> | |
| 41 | +<p>This is version 2026-10.37 of the report; section 11 says where each version’s changes are told. 2026-10.11 and 2026-10.12 are a thaw the owner chose, for a language anyone can build again. They break programs written for 2026-10.10 and before: for 2026-10.9, say, the report of compiler 0.5.0, the last release before them; and 2026-10.12 breaks code procedures written for 2026-10.11. 2026-10.20 is a third, for <code>try</code> (section 7.8); it breaks a program that has a name <code>try</code>. 2026-10.25 is a fourth, for strings: raw strings, which may run over lines and may be indented with the code around them (section 2), and <code>match</code> on a string (section 7.4); it breaks no program. 2026-10.26 is a fifth: an <code>INT</code> widens to an <code>I64</code> wherever it goes to one, in an expression too (sections 5.10 and 6.1); and a view, <code>v.(T)</code>, reads and writes the bytes of a variable as a <code>T</code> (section 6.9); it breaks no program. 2026-10.27 mends a defect: returning a slice of the procedure’s own frame is an error (section 7.3), which breaks only a program that read memory already gone. 2026-10.28 mends another: an array a call returns lives to the end of its statement, and a slice of it kept past that is undefined (sections 8 and 10). 2026-10.29 is a sixth thaw: an array constant typed <code>[]T</code> takes its length from its literal (section 4); it breaks no program. 2026-10.30 mends a defect: <code>[]</code> used as a slice is the empty slice, which may be returned (sections 6.5 and 7.3); it breaks no program. 2026-10.31 is a seventh thaw: <code>INT</code> is at least 16 bits, and <code>U8</code>, <code>I8</code> and <code>I16</code> widen to it (sections 5.1, 5.10 and 6.1); an enumeration’s name may be written with its type, and must be where another declaration shares it (section 5.7); and <code>continue</code> (section 7.2), which breaks a program that has a name <code>continue</code>. 2026-10.32 is an eighth thaw: a constant typed <code>[]T</code> may be <code>[]</code>, the empty slice (section 4); it breaks no program. 2026-10.33 is a ninth: a <code>const</code> parameter, the caller’s variable read in place (section 8), and a variant in a constant (section 4), which break no program; and the library’s procedures that took a pointer to what they work on take a <code>var</code> parameter, a <code>const</code> one or a value instead (the library volume), which breaks a program that calls them with <code>ADR</code>. 2026-10.34 is a tenth: <code>PTR(s)</code>, the address a slice starts at, even an empty one (section 6.8); it breaks no program. 2026-10.35 is an eleventh: <code>ROL(x, n)</code> and <code>ROR(x, n)</code>, a word rotated (sections 6.1 and 6.8); it breaks no program. 2026-10.36 is a twelfth: <code>MIN(T)</code> and <code>MAX(T)</code>, the limits of an integer type, and <code>CLZ</code>, <code>CTZ</code> and <code>POPCOUNT</code>, which count bits (sections 6.1 and 6.8); <code>ABS(x)</code>, a number without its sign (sections 6.1 and 6.8); <code>INT(b)</code>, a <code>BOOL</code> as an integer (section 6.7); <code>OFFSET(T, f)</code> and <code>SIZE(T.f)</code>, where a field lies and how big it is; <code>BYTES(x)</code>, the bytes of a variable or a slice (sections 4 and 6.8); <code>FILL(d, x)</code>, an array or a slice set to one value (section 6.8); a qualified case in a <code>match</code> arm (section 7.4); a slice part written as a literal, and an <code>ERROR</code> constant named by another (sections 4 and 5.8); and a <code>const</code> parameter’s slice returned (section 7.3); and a procedure type’s parameter or result may name a type declared later in its <code>type</code> section (sections 4 and 5.9); it breaks no program. 2026-10.37 is a thirteenth thaw, for safety. It mends a defect: an array a call returns, or a record or variant holding one, lives to the end of the procedure the call is in, so a slice of it may be kept (section 8). <code>CAS</code>, <code>XCHG</code> and <code>XADD</code> take the variable they work on (section 6.8). <code>[]const T</code> is a slice whose elements can’t be written through it, which a string literal, a slice of a constant, <code>MSG</code> and a slice of a <code>const</code> parameter’s array are (sections 4, 5.3, 5.10, 6.8 and 8). And <code>import SYSTEM</code> is what a module says to reach memory other than through its variables: <code>SYSTEM.ADR</code>, <code>SYSTEM.PTR</code>, <code>SYSTEM.SYSCALL</code> and <code>SYSTEM.ARGV</code>, a pointer’s arithmetic and conversions, C, code procedures and <code>check off;</code> (section 3); section 10 says what is left undefined outside such modules. It breaks a program that gives an atomic a pointer, writes through a string or a constant, or does what <code>SYSTEM</code> holds without importing it; raz 0.23 still took them, as before; raz 0.24 doesn’t.</p> | |
| 42 | 42 | <p>The language is frozen, softly, from this version on: it changes only to mend a defect, and its library grows only by what breaks no program written for it. What anyone wishes it had waits on a list, for a later thaw.</p> |
| 43 | 43 | <h1 id="a-first-look">1. A first look</h1> |
| 44 | 44 | <pre><code>module Hello; |
| @@ -111,14 +111,14 @@ | ||
| 111 | 111 | end List.</code></pre> |
| 112 | 112 | <p>An import gives the arguments: <code>import Ints := List(I64);</code>. Each distinct list of arguments makes one <em>instance</em>: the module compiled with <code>T</code> standing for <code>I64</code>. Two imports of <code>List(I64)</code>, in any modules, share one instance, its variables and its <code>ERROR</code>s with it: two lists of arguments are the same when their types are the same (section 5.10), an alias as what it stands for, and their procedures are the same procedures. Since every instance is checked on its own, an operation the argument type doesn’t support (adding two records, say) is reported as an error in that instance.</p> |
| 113 | 113 | <p>An instance is made where its module is first used, not at the import: so the arguments may name types the importing module declares after its imports, as long as they are declared before that first use. The instance is made before the code of the procedure, or the module body, that first uses it, and its arguments are read in the scope there.</p> |
| 114 | -<pre><code>import Table := Map([]U8, Colour, Str.hash, Str.eq); | |
| 114 | +<pre><code>import Table := Map([]const U8, Colour, Str.hash, Str.eq); | |
| 115 | 115 | |
| 116 | 116 | type Colour = (red, green, blue); |
| 117 | 117 | |
| 118 | 118 | var names: Table.Map; (* the instance is made here *)</code></pre> |
| 119 | 119 | <p>What a generic module needs to do with its types, beyond moving them about, it is given as procedures, after the types:</p> |
| 120 | 120 | <pre><code>module Map(K, V; hash: proc(k: K): U64; eq: proc(a, b: K): BOOL);</code></pre> |
| 121 | -<pre><code>import Names := Map([]U8, I64, Str.hash, Str.eq);</code></pre> | |
| 121 | +<pre><code>import Names := Map([]const U8, I64, Str.hash, Str.eq);</code></pre> | |
| 122 | 122 | <p>A procedure argument names a procedure, not a variable or an expression, and its signature, with the type arguments put in, must be the parameter’s. Inside the module, <code>hash(k)</code> calls it directly. Two imports make the same instance only if their types and their procedures are the same.</p> |
| 123 | 123 | <p><strong>Finding modules.</strong> The compiler looks for module <code>Name</code> in the main module’s directory, then in each directory given with <code>-I</code>, then in the user library directories, then in the <code>lib</code> directory beside the compiler. User libraries live in <code>$HOME/.raz/lib</code> by default. <code>RAZ_PATH</code>, when set, replaces that default with a colon-separated list of directories; an empty value disables user libraries. Empty entries are ignored. With no nonempty <code>HOME</code>, there is no default. Paths are not searched recursively; relative paths are relative to the compiler’s working directory, and <code>~</code> and variables inside them are not expanded. Install a library’s source files directly in one of these directories, with its target-specific files beside them. In each directory the compiler takes the first of <code>Name.OS-ARCH.raz</code>, <code>Name.OS.raz</code>, <code>Name.ARCH.raz</code> and <code>Name.raz</code> that exists, where <code>OS-ARCH</code> is the target, for now only <code>linux-x86_64</code>. So a module whose code depends on the machine or the system comes in one file per target, and the modules that import it don’t change. Machine code (section 8) and system calls belong in such files.</p> |
| 124 | 124 | <p><strong>One module to a name.</strong> A program has one module of each name, so the compiler reports an error, naming the files, if a module it looks for has files in more than one of these directories, rather than take the first: a <code>Buf.raz</code> beside a program would otherwise stand in for the library’s <code>Buf</code> in every module that imports it, the library’s own among them. A link is the exception: a file that is a symbolic link to one of the module’s files in a later directory chooses that file, so that a directory holding a link to <code>lib/Sys.raz</code> builds a program over the C library’s <code>Sys</code> on Linux too.</p> |
| @@ -690,7 +690,7 @@ | ||
| 690 | 690 | import Sys, Out; |
| 691 | 691 | |
| 692 | 692 | (* Prints the first line of the file at path. *) |
| 693 | -proc head(path: []U8): BOOL; | |
| 693 | +proc head(path: []const U8): BOOL; | |
| 694 | 694 | var fd, n, i: INT; buf: [256]U8; |
| 695 | 695 | begin |
| 696 | 696 | fd := Sys.openRead(path); |
| @@ -955,6 +955,7 @@ | ||
| 955 | 955 | <h1 id="the-library-volume-2">The library, volume 2</h1> |
| 956 | 956 | <p>The second volume of the report on raz. The first, <code>doc/raz.md</code>, defines the language; this one defines the modules that come with it, and refers to the first’s sections by number, as “section 6.8”. Only the core binds every implementation (appendix A of the first volume); the rest is the standard library, <code>Rand</code>, <code>Check</code> and <code>Prop</code> among it.</p> |
| 957 | 957 | <p>The library grows only by what breaks no program written for it. If a change must break programs, the old form stays for one minor version beside the new; the release notes list the old forms, and the next version removes them. Nothing marks an old form in the source.</p> |
| 958 | +<p>A procedure that only reads a slice takes a <code>[]const T</code>, so that a string, a constant and any other slice fit it; what a procedure hands back from the environment, the arguments or a directory is a <code>[]const U8</code>. A module that reaches memory, as the library’s lower modules do, says <code>import SYSTEM</code> (section 3 of the first volume).</p> | |
| 958 | 959 | <p>Modules written in raz, in the compiler’s <code>lib</code> directory:</p> |
| 959 | 960 | <table> |
| 960 | 961 | <thead> |
| @@ -1010,7 +1011,7 @@ | ||
| 1010 | 1011 | </tr> |
| 1011 | 1012 | <tr> |
| 1012 | 1013 | <td><code>Str</code></td> |
| 1013 | -<td>operations on <code>[]U8</code> strings: comparing, hashing, finding, trimming and cutting, case, joining and replacing, UTF-8, numbers to and from text</td> | |
| 1014 | +<td>operations on strings, <code>[]const U8</code>: comparing, hashing, finding, trimming and cutting, case, joining and replacing, UTF-8, numbers to and from text</td> | |
| 1014 | 1015 | </tr> |
| 1015 | 1016 | <tr> |
| 1016 | 1017 | <td><code>Bytes</code></td> |
| @@ -1077,7 +1078,7 @@ | ||
| 1077 | 1078 | proc atExit*(p: proc): ERROR; (* p run when the program ends by exit *) |
| 1078 | 1079 | proc exit*(status: INT); (* the atExit procedures, the last first, then the end *) |
| 1079 | 1080 | proc exitStatus*: INT; (* the status exit was given, in an atExit procedure; 0 before *) |
| 1080 | -proc stop*(msg: []U8); (* msg, and a line feed, on standard error, then exit with status 2 *) | |
| 1081 | +proc stop*(msg: []const U8); (* msg, and a line feed, on standard error, then exit with status 2 *) | |
| 1081 | 1082 | proc trace*; (* the calls under way, and their variables, on standard error *)</code></pre> |
| 1082 | 1083 | <p><code>Rt</code> also holds what the compiler’s checks call when they fail, and the handlers of the signals that stop a program (section 10). <code>exit</code> takes each procedure off the list before it runs it, so it runs once even if it fails or calls <code>exit</code> itself; in a program that uses C, it ends through C’s <code>exit</code>, so that C flushes its buffers too. <code>exitStatus</code> is the status <code>exit</code> was given, which a procedure <code>atExit</code> registered can read, to note a crash: a failed check and <code>stop</code> both end with 2. It is 0 before <code>exit</code> is called.</p> |
| 1083 | 1084 | <pre><code>module Mem; |
| @@ -1136,7 +1137,7 @@ | ||
| 1136 | 1137 | <p>A <code>Lock</code> is for short sections: a thread that finds it taken tries again, giving up the processor now and then. A <code>Mutex</code> sleeps. Each of <code>Thread</code>’s threads runs on a stack of its own, with a page below it that can’t be touched, so that running off the stack stops the program with a message, as in the main thread. In a program that uses C, <code>Thread</code> makes its threads with C’s <code>pthread_create</code>, so that C’s library knows of them; in one that doesn’t, with the system’s own calls. The program is the same either way.</p> |
| 1137 | 1138 | <p><code>Sys.kill</code> signals a program, and the system gives the signal to any of its threads; <code>signal</code> gives it to <code>t</code> alone, until <code>t</code> is joined, and is -3 (ESRCH) once <code>t</code> has ended. With <code>Sys.handle(sig)</code>, a call <code>t</code> waits in, a <code>read</code> or a <code>ready</code>, fails with -4, so that one thread can break into another’s wait, as <code>^C</code> breaks into the main thread’s. A signal sent before <code>t</code> begins its wait is only noted, so a program sends it again until <code>t</code> says it has stopped.</p> |
| 1138 | 1139 | <pre><code>module Out; |
| 1139 | -proc str*(s: []U8); | |
| 1140 | +proc str*(s: []const U8); | |
| 1140 | 1141 | proc char*(c: U8); |
| 1141 | 1142 | proc ln*; (* a line feed *) |
| 1142 | 1143 | proc int*(n: I64); (* n in decimal *) |
| @@ -1145,7 +1146,7 @@ | ||
| 1145 | 1146 | proc lines*(on: BOOL); (* written at each line feed, or only when full *)</code></pre> |
| 1146 | 1147 | <p><strong>Out and C’s stdio.</strong> <code>Out</code> keeps a buffer of <code>Sys.outSize</code> bytes, 4 KB on Linux. It writes it out when it is full, at each line feed when standard output is a terminal, at <code>Out.flush</code>, and when the program ends by <code>Rt.exit</code>. <code>lines(true)</code> has it written at each line feed whatever standard output is, as a filter of a stream that comes slowly wants through a pipe, as grep’s <code>--line-buffered</code>; <code>lines(false)</code>, only when full, at a terminal too. A lock keeps what each call writes together when threads write at once. C’s <code>stdio</code> keeps a buffer of its own for the same standard output. A program that writes with both flushes one before writing with the other, or the text comes out of order:</p> |
| 1147 | 1148 | <pre><code>module Both; |
| 1148 | -import Out, C; (* C: printf, fflush and C's FILE *stdout *) | |
| 1149 | +import Out, C, SYSTEM; (* C: printf, fflush and C's FILE *stdout *) | |
| 1149 | 1150 | |
| 1150 | 1151 | proc main*; |
| 1151 | 1152 | begin |
| @@ -1202,11 +1203,11 @@ | ||
| 1202 | 1203 | Kind* = (unknown, isFile, isDir, isLink, isOther); (* what a directory says a name in it is *) |
| 1203 | 1204 | Dir* = record ... end; (* an open directory; its fields are the target's *) |
| 1204 | 1205 | Program* = record |
| 1205 | - path*: []U8; | |
| 1206 | - args*: [][]U8; (* the first is its name *) | |
| 1207 | - env*: [][]U8; (* NAME=value, each added to this program's environment *) | |
| 1206 | + path*: []const U8; | |
| 1207 | + args*: [][]const U8; (* the first is its name *) | |
| 1208 | + env*: [][]const U8; (* NAME=value, each added to this program's environment *) | |
| 1208 | 1209 | onlyEnv*: BOOL; (* env is its whole environment *) |
| 1209 | - dir*: []U8; (* its working directory; "" for this program's *) | |
| 1210 | + dir*: []const U8; (* its working directory; "" for this program's *) | |
| 1210 | 1211 | fds*: [3]INT; (* its standard input, output and error; 0 for this program's own *) |
| 1211 | 1212 | group*: INT; (* the process group it joins: 0 for this program's, -1 a new one it leads *) |
| 1212 | 1213 | foreground*: BOOL; (* its group takes the terminal on standard input *) |
| @@ -1230,12 +1231,12 @@ | ||
| 1230 | 1231 | ready*: BOOL; (* set by poll *) |
| 1231 | 1232 | end; |
| 1232 | 1233 | |
| 1233 | -proc write*(fd: INT; s: []U8): INT; (* the bytes written *) | |
| 1234 | +proc write*(fd: INT; s: []const U8): INT; (* the bytes written *) | |
| 1234 | 1235 | proc read*(fd: INT; buf: []U8): INT; (* the bytes read; 0 at the end *) |
| 1235 | -proc open*(path: []U8; how: Accesses): INT; (* its fd, closed on exec *) | |
| 1236 | -proc openMode*(path: []U8; how: Accesses; mode: INT): INT; (* open, a file it makes given mode *) | |
| 1237 | -proc openRead*(path: []U8): INT; (* open(path, {reading}) *) | |
| 1238 | -proc create*(path: []U8): INT; (* open(path, {writing, creating, truncating}) *) | |
| 1236 | +proc open*(path: []const U8; how: Accesses): INT; (* its fd, closed on exec *) | |
| 1237 | +proc openMode*(path: []const U8; how: Accesses; mode: INT): INT; (* open, a file it makes given mode *) | |
| 1238 | +proc openRead*(path: []const U8): INT; (* open(path, {reading}) *) | |
| 1239 | +proc create*(path: []const U8): INT; (* open(path, {writing, creating, truncating}) *) | |
| 1239 | 1240 | proc seek*(fd: INT; pos: I64; fromEnd: BOOL): I64; (* the new position *) |
| 1240 | 1241 | proc position*(fd: INT): I64; (* where the next read or write is *) |
| 1241 | 1242 | proc truncate*(fd: INT; size: I64): INT; (* the file made size bytes long *) |
| @@ -1257,26 +1258,26 @@ | ||
| 1257 | 1258 | proc yield*; (* the processor given to another thread *) |
| 1258 | 1259 | proc exit*(status: INT); (* the program ended at once, without Rt's atExit *) |
| 1259 | 1260 | |
| 1260 | -proc rename*(old, new: []U8): INT; (* replaces what new names *) | |
| 1261 | -proc remove*(path: []U8): INT; (* a file, not a directory *) | |
| 1262 | -proc removeDir*(path: []U8): INT; (* an empty directory *) | |
| 1263 | -proc makeDir*(path: []U8): INT; (* readable by all, writable by its owner *) | |
| 1264 | -proc makeDirMode*(path: []U8; mode: INT): INT; (* makeDir, given mode *) | |
| 1265 | -proc chmod*(path: []U8; mode: INT): INT; | |
| 1266 | -proc chown*(path: []U8; uid, gid: INT): INT; (* its owner and group; -1 leaves one as it is *) | |
| 1267 | -proc makeFifo*(path: []U8; mode: INT): INT; (* a named pipe, given mode *) | |
| 1268 | -proc setTime*(path: []U8; sec: I64; nsec: INT): INT; (* its mtime, and the time it was read, made sec and nsec *) | |
| 1269 | -proc access*(path: []U8; how: Accesses): INT; (* 0 if this user may read, write or execute it *) | |
| 1261 | +proc rename*(old, new: []const U8): INT; (* replaces what new names *) | |
| 1262 | +proc remove*(path: []const U8): INT; (* a file, not a directory *) | |
| 1263 | +proc removeDir*(path: []const U8): INT; (* an empty directory *) | |
| 1264 | +proc makeDir*(path: []const U8): INT; (* readable by all, writable by its owner *) | |
| 1265 | +proc makeDirMode*(path: []const U8; mode: INT): INT; (* makeDir, given mode *) | |
| 1266 | +proc chmod*(path: []const U8; mode: INT): INT; | |
| 1267 | +proc chown*(path: []const U8; uid, gid: INT): INT; (* its owner and group; -1 leaves one as it is *) | |
| 1268 | +proc makeFifo*(path: []const U8; mode: INT): INT; (* a named pipe, given mode *) | |
| 1269 | +proc setTime*(path: []const U8; sec: I64; nsec: INT): INT; (* its mtime, and the time it was read, made sec and nsec *) | |
| 1270 | +proc access*(path: []const U8; how: Accesses): INT; (* 0 if this user may read, write or execute it *) | |
| 1270 | 1271 | proc umask*(mask: INT): INT; (* the bits taken from every file made; the mask it was *) |
| 1271 | -proc symlink*(target, path: []U8): INT; (* path made a link to target *) | |
| 1272 | -proc readLink*(path: []U8): ([]U8, INT); (* what the link names, good until the next call *) | |
| 1273 | -proc info*(path: []U8): (Info, INT); (* following a symbolic link *) | |
| 1274 | -proc linkInfo*(path: []U8): (Info, INT); (* the link itself *) | |
| 1272 | +proc symlink*(target, path: []const U8): INT; (* path made a link to target *) | |
| 1273 | +proc readLink*(path: []const U8): ([]const U8, INT); (* what the link names, good until the next call *) | |
| 1274 | +proc info*(path: []const U8): (Info, INT); (* following a symbolic link *) | |
| 1275 | +proc linkInfo*(path: []const U8): (Info, INT); (* the link itself *) | |
| 1275 | 1276 | proc infoOf*(fd: INT): (Info, INT); (* the open file's *) |
| 1276 | -proc space*(path: []U8): (I64, I64, INT); (* the bytes free and in all on path's file system *) | |
| 1277 | +proc space*(path: []const U8): (I64, I64, INT); (* the bytes free and in all on path's file system *) | |
| 1277 | 1278 | |
| 1278 | -proc openDir*(var d: Dir; path: []U8): INT; | |
| 1279 | -proc nextEntry*(var d: Dir): ([]U8, BOOL); (* a name, good until the next call; false at the end *) | |
| 1279 | +proc openDir*(var d: Dir; path: []const U8): INT; | |
| 1280 | +proc nextEntry*(var d: Dir): ([]const U8, BOOL); (* a name, good until the next call; false at the end *) | |
| 1280 | 1281 | proc entryKind*(const d: Dir): Kind; (* what the name nextEntry gave last is *) |
| 1281 | 1282 | proc closeDir*(var d: Dir); |
| 1282 | 1283 | |
| @@ -1286,27 +1287,27 @@ | ||
| 1286 | 1287 | proc setMode*(fd: INT; m: Mode): INT; (* once what was written to it is sent *) |
| 1287 | 1288 | proc termSize*(fd: INT): (INT, INT); (* columns and rows; 80 and 24 if unknown *) |
| 1288 | 1289 | proc ready*(fd, ms: INT): BOOL; (* something to read within ms, or ever if ms < 0 *) |
| 1289 | -proc readyOf*(fds: []INT; ms: INT): INT; (* the first of fds with something to read within ms, or -1 *) | |
| 1290 | -proc readyFor*(reads, writes: []INT; ms: INT): INT; (* readyOf, and the first of writes with room to write *) | |
| 1290 | +proc readyOf*(fds: []const INT; ms: INT): INT; (* the first of fds with something to read within ms, or -1 *) | |
| 1291 | +proc readyFor*(reads, writes: []const INT; ms: INT): INT; (* readyOf, and the first of writes with room to write *) | |
| 1291 | 1292 | proc poll*(var fds: []Poll; ms: INT): INT; (* sets each fd's ready, as each read and write say; the number ready, 0 on a timeout, or minus an errno *) |
| 1292 | 1293 | proc setBlocking*(fd: INT; yes: BOOL): INT; (* if not yes, a read or write that would wait is -11 at once *) |
| 1293 | 1294 | proc blocking*(fd: INT): INT; (* 1 if reads and writes wait, 0 if they are -11 at once, or minus an errno *) |
| 1294 | 1295 | |
| 1295 | -proc errorText*(err: INT): []U8; (* what errno err, or minus it, means, as C's strerror says *) | |
| 1296 | +proc errorText*(err: INT): []const U8; (* what errno err, or minus it, means, as C's strerror says *) | |
| 1296 | 1297 | |
| 1297 | -proc env*(name: []U8): []U8; (* the environment variable's value; "" if unset *) | |
| 1298 | -proc findEnv*(name: []U8): ([]U8, BOOL); (* the value and true; "" and false if unset, "" and true if empty *) | |
| 1299 | -proc envAt*(i: INT): ([]U8, BOOL); (* the environment's ith NAME=value, from 0; false past the last *) | |
| 1300 | -proc setEnv*(name, value: []U8): INT; (* name set to value, for env and the programs started *) | |
| 1301 | -proc workDir*: ([]U8, INT); (* the working directory, good until the next call *) | |
| 1302 | -proc setWorkDir*(path: []U8): INT; | |
| 1298 | +proc env*(name: []const U8): []const U8; (* the environment variable's value; "" if unset *) | |
| 1299 | +proc findEnv*(name: []const U8): ([]const U8, BOOL); (* the value and true; "" and false if unset, "" and true if empty *) | |
| 1300 | +proc envAt*(i: INT): ([]const U8, BOOL); (* the environment's ith NAME=value, from 0; false past the last *) | |
| 1301 | +proc setEnv*(name, value: []const U8): INT; (* name set to value, for env and the programs started *) | |
| 1302 | +proc workDir*: ([]const U8, INT); (* the working directory, good until the next call *) | |
| 1303 | +proc setWorkDir*(path: []const U8): INT; | |
| 1303 | 1304 | |
| 1304 | 1305 | proc pipe*: (INT, INT); (* the fd to read and the fd to write; or minus an errno and -1 *) |
| 1305 | 1306 | proc openPty*: INT; (* a pseudo-terminal: the fd of its controlling side *) |
| 1306 | 1307 | proc setTermSize*(fd, cols, rows: INT): INT; (* the size its programs are told of *) |
| 1307 | 1308 | proc spawn*(p: Program): INT; (* a program started: its process id *) |
| 1308 | 1309 | proc exec*(p: Program): INT; (* p run in this program's place; returns only if it can't *) |
| 1309 | -proc execve*(path: []U8; args, env: [][]U8): INT; (* as the kernel's execve: no search *) | |
| 1310 | +proc execve*(path: []const U8; args, env: [][]const U8): INT; (* as the kernel's execve: no search *) | |
| 1310 | 1311 | proc fork*: INT; (* this program copied: 0 in the copy, its process id in this one *) |
| 1311 | 1312 | proc setsid*: INT; (* a session of its own, led by this program: its process id, or minus an errno *) |
| 1312 | 1313 | proc wait*(pid: INT; block: BOOL): (INT, Status, INT); (* which program, how, and its code *) |
| @@ -1319,13 +1320,13 @@ | ||
| 1319 | 1320 | proc foregroundOf*(fd: INT): INT; (* the group the terminal on fd gives its keys to *) |
| 1320 | 1321 | proc uid*: INT; (* the user this program runs as *) |
| 1321 | 1322 | proc euid*: INT; (* the user whose rights it has: 0 is root *) |
| 1322 | -proc hostName*: ([]U8, INT); (* this machine's name, good until the next call *) | |
| 1323 | +proc hostName*: ([]const U8, INT); (* this machine's name, good until the next call *) | |
| 1323 | 1324 | proc processors*: INT; (* the processors this program may run on, at least 1 *) |
| 1324 | 1325 | proc cpuTime*(children: BOOL): (I64, I64); (* ns used in the program and in the system; or by its children *) |
| 1325 | 1326 | proc limit*(which: INT): (I64, I64, INT); (* the soft limit and the hard; -1 for none *) |
| 1326 | 1327 | proc setLimit*(which: INT; soft, hard: I64): INT; |
| 1327 | 1328 | |
| 1328 | -proc catch*(sigs: []INT): INT; (* sigs caught: an fd to wait on *) | |
| 1329 | +proc catch*(sigs: []const INT): INT; (* sigs caught: an fd to wait on *) | |
| 1329 | 1330 | proc caught*(fd: INT): INT; (* the next signal caught, or 0 if none is waiting *) |
| 1330 | 1331 | proc ignore*(sig: INT; yes: BOOL): INT; (* sig ignored; or, if not yes, back to what it does at first *) |
| 1331 | 1332 | proc ignored*(sig: INT): BOOL; (* whether sig does nothing now *) |
| @@ -1337,23 +1338,23 @@ | ||
| 1337 | 1338 | proc sleep*(ms: INT); |
| 1338 | 1339 | proc random*(b: []U8); (* b filled with random bytes; stops the program if it can't be *) |
| 1339 | 1340 | |
| 1340 | -proc connect*(path: []U8): INT; (* a stream socket, connected to the Unix socket at path: its fd *) | |
| 1341 | -proc connectTcp*(addr: []U8; port: INT): INT; (* addr an IPv4 or IPv6 address, as numbers *) | |
| 1342 | -proc connectUdp*(addr: []U8; port: INT): INT; | |
| 1343 | -proc listen*(path: []U8): INT; (* a Unix socket made at path, taking connections *) | |
| 1344 | -proc listenTcp*(addr: []U8; port: INT): INT; | |
| 1341 | +proc connect*(path: []const U8): INT; (* a stream socket, connected to the Unix socket at path: its fd *) | |
| 1342 | +proc connectTcp*(addr: []const U8; port: INT): INT; (* addr an IPv4 or IPv6 address, as numbers *) | |
| 1343 | +proc connectUdp*(addr: []const U8; port: INT): INT; | |
| 1344 | +proc listen*(path: []const U8): INT; (* a Unix socket made at path, taking connections *) | |
| 1345 | +proc listenTcp*(addr: []const U8; port: INT): INT; | |
| 1345 | 1346 | proc accept*(fd: INT): INT; (* the next connection, waiting for one *) |
| 1346 | -proc send*(fd: INT; s: []U8): INT; (* write, to a socket: -32 (EPIPE), not a signal, if it is closed *) | |
| 1347 | -proc sendNow*(fd: INT; s: []U8): INT; (* send, but -11 (EAGAIN) where it would wait *) | |
| 1348 | -proc connectTcpWithin*(addr: []U8; port, ms: INT): INT; (* connectTcp, or -110 (ETIMEDOUT) after ms *) | |
| 1349 | -proc listenUdp*(addr: []U8; port: INT): INT; (* a socket of datagrams on port at addr *) | |
| 1347 | +proc send*(fd: INT; s: []const U8): INT; (* write, to a socket: -32 (EPIPE), not a signal, if it is closed *) | |
| 1348 | +proc sendNow*(fd: INT; s: []const U8): INT; (* send, but -11 (EAGAIN) where it would wait *) | |
| 1349 | +proc connectTcpWithin*(addr: []const U8; port, ms: INT): INT; (* connectTcp, or -110 (ETIMEDOUT) after ms *) | |
| 1350 | +proc listenUdp*(addr: []const U8; port: INT): INT; (* a socket of datagrams on port at addr *) | |
| 1350 | 1351 | proc receive*(fd: INT; buf: []U8): (INT, Peer); (* the next datagram, in buf, and who sent it *) |
| 1351 | -proc reply*(fd: INT; peer: Peer; s: []U8): INT; (* s, as one datagram, to peer *) | |
| 1352 | +proc reply*(fd: INT; peer: Peer; s: []const U8): INT; (* s, as one datagram, to peer *) | |
| 1352 | 1353 | proc localPort*(fd: INT): INT; (* the port the socket fd is bound to *) |
| 1353 | 1354 | proc closeWrite*(fd: INT): INT; (* no more writing to the socket fd; the other end reads an end *) |
| 1354 | 1355 | proc peer*(fd: INT): (Remote, INT); (* who is at the other end of the connected socket fd *) |
| 1355 | 1356 | proc socketPair*: (INT, INT, INT); (* two connected stream sockets, and 0 *) |
| 1356 | -proc sendFds*(fd: INT; s: []U8; fds: []INT): INT; (* send, and with it fds, to a Unix socket *) | |
| 1357 | +proc sendFds*(fd: INT; s: []const U8; fds: []const INT): INT; (* send, and with it fds, to a Unix socket *) | |
| 1357 | 1358 | proc receiveFds*(fd: INT; buf: []U8; fds: []INT): (INT, INT); (* read, and the fds that came with it *) |
| 1358 | 1359 | |
| 1359 | 1360 | proc memFile*(size: INT): INT; (* a file of size zero bytes, in memory, with no name: its fd *) |
| @@ -1381,22 +1382,22 @@ | ||
| 1381 | 1382 | <p>A program that both reads from a descriptor and writes much to it, as a terminal writes what is pasted to its program while it reads what the program writes back, must not wait on either: the program may be waiting for its own writes to be read. <code>setBlocking(fd, false)</code> has a read or a write on <code>fd</code> that would wait return -11 (EAGAIN) at once, a write having written what there was room for; <code>readyFor</code> waits, as <code>readyOf</code> does, for something to read on any of <code>reads</code> or for room to write on any of <code>writes</code>. A descriptor in both is given once, when it is ready for either; the 64 it looks at are <code>reads</code>, then <code>writes</code>.</p> |
| 1382 | 1383 | <p><strong>Strings, buffers and collections.</strong></p> |
| 1383 | 1384 | <pre><code>module Str; |
| 1384 | -proc cmp*(a, b: []U8): INT; (* -1, 0 or 1 as a sorts before, with or after b, byte by byte *) | |
| 1385 | -proc eq*(a, b: []U8): BOOL; | |
| 1386 | -proc eqFold*(a, b: []U8): BOOL; (* eq, an ASCII letter's capital and small the same *) | |
| 1387 | -proc copy*(a: ^Mem.Allocator; s: []U8): ([]U8, BOOL); (* s's bytes in memory from a; false if a has none *) | |
| 1385 | +proc cmp*(a, b: []const U8): INT; (* -1, 0 or 1 as a sorts before, with or after b, byte by byte *) | |
| 1386 | +proc eq*(a, b: []const U8): BOOL; | |
| 1387 | +proc eqFold*(a, b: []const U8): BOOL; (* eq, an ASCII letter's capital and small the same *) | |
| 1388 | +proc copy*(a: ^Mem.Allocator; s: []const U8): ([]U8, BOOL); (* s's bytes in memory from a; false if a has none *) | |
| 1388 | 1389 | proc free*(a: ^Mem.Allocator; s: []U8); (* what copy gave from a, given back; "" is none *) |
| 1389 | -proc hash*(s: []U8): U64; (* FNV-1a, 64 bits *) | |
| 1390 | -proc find*(s, sub: []U8): INT; (* where sub first starts in s, or -1 *) | |
| 1391 | -proc findFrom*(s, sub: []U8; from: INT): INT; (* where sub first starts in s at or after from, or -1 *) | |
| 1392 | -proc findLast*(s, sub: []U8): INT; (* where sub last starts in s, or -1 *) | |
| 1393 | -proc hasPrefix*(s, p: []U8): BOOL; | |
| 1394 | -proc hasSuffix*(s, x: []U8): BOOL; | |
| 1395 | -proc trim*(s: []U8): []U8; (* s without the spaces at either end *) | |
| 1396 | -proc trimLeft*(s: []U8): []U8; (* s without the spaces at its start *) | |
| 1397 | -proc trimRight*(s: []U8): []U8; (* s without the spaces at its end *) | |
| 1398 | -proc cut*(s, sep: []U8): ([]U8, []U8, BOOL); (* what comes before the first sep, and after it *) | |
| 1399 | -proc field*(s: []U8): ([]U8, []U8); (* the first run of bytes in s that aren't spaces, and what follows it *) | |
| 1390 | +proc hash*(s: []const U8): U64; (* FNV-1a, 64 bits *) | |
| 1391 | +proc find*(s, sub: []const U8): INT; (* where sub first starts in s, or -1 *) | |
| 1392 | +proc findFrom*(s, sub: []const U8; from: INT): INT; (* where sub first starts in s at or after from, or -1 *) | |
| 1393 | +proc findLast*(s, sub: []const U8): INT; (* where sub last starts in s, or -1 *) | |
| 1394 | +proc hasPrefix*(s, p: []const U8): BOOL; | |
| 1395 | +proc hasSuffix*(s, x: []const U8): BOOL; | |
| 1396 | +proc trim*(s: []const U8): []const U8; (* s without the spaces at either end *) | |
| 1397 | +proc trimLeft*(s: []const U8): []const U8; (* s without the spaces at its start *) | |
| 1398 | +proc trimRight*(s: []const U8): []const U8; (* s without the spaces at its end *) | |
| 1399 | +proc cut*(s, sep: []const U8): ([]const U8, []const U8, BOOL); (* what comes before the first sep, and after it *) | |
| 1400 | +proc field*(s: []const U8): ([]const U8, []const U8); (* the first run of bytes in s that aren't spaces, and what follows it *) | |
| 1400 | 1401 | proc isDigit*(c: U8): BOOL; (* 0 to 9 *) |
| 1401 | 1402 | proc isLetter*(c: U8): BOOL; (* A to Z, a to z *) |
| 1402 | 1403 | proc isSpace*(c: U8): BOOL; (* a space, as trim takes it *) |
| @@ -1404,28 +1405,28 @@ | ||
| 1404 | 1405 | proc hexVal*(c: U8): INT; (* c's value as a hexadecimal digit, or -1 *) |
| 1405 | 1406 | proc lower*(s: []U8); (* s's ASCII capitals made small, in place *) |
| 1406 | 1407 | proc upper*(s: []U8); (* s's ASCII small letters made capitals, in place *) |
| 1407 | -proc join*(var b: Buf.Buf; parts: [][]U8; sep: []U8); (* the parts added to b, sep between each two *) | |
| 1408 | -proc replace*(var b: Buf.Buf; s, old, new: []U8); (* s added to b, with new for each old in it *) | |
| 1408 | +proc join*(var b: Buf.Buf; parts: [][]const U8; sep: []const U8); (* the parts added to b, sep between each two *) | |
| 1409 | +proc replace*(var b: Buf.Buf; s, old, new: []const U8); (* s added to b, with new for each old in it *) | |
| 1409 | 1410 | proc encode*(c: I32): ([4]U8, INT); (* c in UTF-8: its bytes, and how many *) |
| 1410 | -proc decode*(s: []U8): (I32, INT); (* the character s starts with, and its length in bytes *) | |
| 1411 | -proc decodeLast*(s: []U8): (I32, INT); (* the character s ends with, and its length in bytes *) | |
| 1412 | -proc next*(s: []U8; i: INT): (I32, INT); (* the character at i, and where the next starts; -1 at the end *) | |
| 1413 | -proc valid*(s: []U8): BOOL; (* s is UTF-8, every character rightly spelled *) | |
| 1414 | -proc chars*(s: []U8): INT; (* the characters in s, as decode reads them *) | |
| 1415 | -proc toInt*(s: []U8): (I64, BOOL); (* s, in decimal; false if it isn't a number an I64 holds *) | |
| 1416 | -proc toHex*(s: []U8): (U64, BOOL); (* s, in hexadecimal; false if it isn't a number a U64 holds *) | |
| 1417 | -proc toU64*(s: []U8): (U64, BOOL); (* s, decimal digits only; false if it isn't one, or a U64 can't hold it *) | |
| 1418 | -proc fromC*(p: ^U8): []U8; (* the bytes at p up to its zero byte, not copied; "" for nil *) | |
| 1419 | -proc toC*(s, out: []U8): ^U8; (* s copied into out with a zero byte after it, which holds LEN(s) + 1 bytes at least: PTR(out) *) | |
| 1411 | +proc decode*(s: []const U8): (I32, INT); (* the character s starts with, and its length in bytes *) | |
| 1412 | +proc decodeLast*(s: []const U8): (I32, INT); (* the character s ends with, and its length in bytes *) | |
| 1413 | +proc next*(s: []const U8; i: INT): (I32, INT); (* the character at i, and where the next starts; -1 at the end *) | |
| 1414 | +proc valid*(s: []const U8): BOOL; (* s is UTF-8, every character rightly spelled *) | |
| 1415 | +proc chars*(s: []const U8): INT; (* the characters in s, as decode reads them *) | |
| 1416 | +proc toInt*(s: []const U8): (I64, BOOL); (* s, in decimal; false if it isn't a number an I64 holds *) | |
| 1417 | +proc toHex*(s: []const U8): (U64, BOOL); (* s, in hexadecimal; false if it isn't a number a U64 holds *) | |
| 1418 | +proc toU64*(s: []const U8): (U64, BOOL); (* s, decimal digits only; false if it isn't one, or a U64 can't hold it *) | |
| 1419 | +proc fromC*(p: ^U8): []const U8; (* the bytes at p up to its zero byte, not copied; "" for nil *) | |
| 1420 | +proc toC*(s: []const U8; out: []U8): ^U8; (* s copied into out with a zero byte after it, which holds LEN(s) + 1 bytes at least: PTR(out) *) | |
| 1420 | 1421 | const Base64*, Base64Url*; (* the digits of RFC 4648, sections 4 and 5 *) |
| 1421 | -proc base64With*(s, out, alphabet: []U8; pad: BOOL): INT; (* s in base64 with alphabet's 64 digits into out, which holds base64Size(LEN(s)) bytes at least, padded with = if pad: the bytes written *) | |
| 1422 | -proc unbase64With*(s, out, alphabet: []U8; pad: BOOL): (INT, BOOL); (* s, base64 as base64With wrote it, into out, which holds (LEN(s) + 3) div 4 * 3 bytes at least: the bytes written; false if s is not that *) | |
| 1422 | +proc base64With*(s: []const U8; out: []U8; alphabet: []const U8; pad: BOOL): INT; (* s in base64 with alphabet's 64 digits into out, which holds base64Size(LEN(s)) bytes at least, padded with = if pad: the bytes written *) | |
| 1423 | +proc unbase64With*(s: []const U8; out: []U8; alphabet: []const U8; pad: BOOL): (INT, BOOL); (* s, base64 as base64With wrote it, into out, which holds (LEN(s) + 3) div 4 * 3 bytes at least: the bytes written; false if s is not that *) | |
| 1423 | 1424 | proc base64Size*(n: INT): INT; (* the bytes base64 makes of n bytes: 4 for each 3, or part of 3 *) |
| 1424 | -proc base64*(s, out: []U8): INT; (* s in base64 into out, which holds base64Size(LEN(s)) bytes at least: the bytes written *) | |
| 1425 | -proc unbase64*(s, out: []U8): (INT, BOOL); (* s, base64, into out, which holds LEN(s) div 4 * 3 bytes at least: the bytes written; false if s is not base64 *) | |
| 1425 | +proc base64*(s: []const U8; out: []U8): INT; (* s in base64 into out, which holds base64Size(LEN(s)) bytes at least: the bytes written *) | |
| 1426 | +proc unbase64*(s: []const U8; out: []U8): (INT, BOOL); (* s, base64, into out, which holds LEN(s) div 4 * 3 bytes at least: the bytes written; false if s is not base64 *) | |
| 1426 | 1427 | proc fromInt*(n: I64): ([20]U8, INT); (* n in decimal: its bytes, and how many *) |
| 1427 | -proc le*(s: []U8): U64; (* s, at most 8 bytes, the first the lowest *) | |
| 1428 | -proc be*(s: []U8): U64; (* s, at most 8 bytes, the first the highest *) | |
| 1428 | +proc le*(s: []const U8): U64; (* s, at most 8 bytes, the first the lowest *) | |
| 1429 | +proc be*(s: []const U8): U64; (* s, at most 8 bytes, the first the highest *) | |
| 1429 | 1430 | proc putLE*(s: []U8; v: U64); (* v into s, the lowest byte first *) |
| 1430 | 1431 | proc putBE*(s: []U8; v: U64); (* v into s, the highest byte first *) |
| 1431 | 1432 | |
| @@ -1434,7 +1435,7 @@ | ||
| 1434 | 1435 | proc init*(var b: Buf; a: ^Mem.Allocator); |
| 1435 | 1436 | proc room*(var b: Buf; n: INT): BOOL; (* room for n more bytes; false if the allocator has none *) |
| 1436 | 1437 | proc add*(var b: Buf; c: U8); |
| 1437 | -proc str*(var b: Buf; s: []U8); | |
| 1438 | +proc str*(var b: Buf; s: []const U8); | |
| 1438 | 1439 | proc spare*(var b: Buf; n: INT): []U8; (* room for n more bytes, and those bytes, to fill *) |
| 1439 | 1440 | proc bytes*(const b: Buf): []U8; (* what it holds, until it next grows *) |
| 1440 | 1441 | proc cut*(var b: Buf; n: INT); (* only the first n bytes kept, its memory kept *) |
| @@ -1447,17 +1448,17 @@ | ||
| 1447 | 1448 | uint(u: U64); |
| 1448 | 1449 | hex(h: U64); |
| 1449 | 1450 | lowHex(h: U64); |
| 1450 | - str(s: []U8); | |
| 1451 | + str(s: []const U8); | |
| 1451 | 1452 | char(c: U8); |
| 1452 | 1453 | bool(b: BOOL); |
| 1453 | 1454 | real(r: F64; places: INT); |
| 1454 | 1455 | end; |
| 1455 | 1456 | const ShortMost* = 24; (* the longest Fmt.short *) |
| 1456 | 1457 | proc short*(x: F64; var out: [ShortMost]U8): []U8; (* x in the fewest digits that read back, in out *) |
| 1457 | -proc print*(f: []U8; args: []Arg); (* to standard output *) | |
| 1458 | -proc eprint*(f: []U8; args: []Arg); (* to standard error, once Out is flushed *) | |
| 1459 | -proc write*(var b: Buf.Buf; f: []U8; args: []Arg); (* added to b *) | |
| 1460 | -proc into*(out: []U8; f: []U8; args: []Arg): (INT, BOOL); (* into out: the bytes, and false if cut short *) | |
| 1458 | +proc print*(f: []const U8; args: []const Arg); (* to standard output *) | |
| 1459 | +proc eprint*(f: []const U8; args: []const Arg); (* to standard error, once Out is flushed *) | |
| 1460 | +proc write*(var b: Buf.Buf; f: []const U8; args: []const Arg); (* added to b *) | |
| 1461 | +proc into*(out: []U8; f: []const U8; args: []const Arg): (INT, BOOL); (* into out: the bytes, and false if cut short *) | |
| 1461 | 1462 | |
| 1462 | 1463 | module List(T); |
| 1463 | 1464 | type |
| @@ -1521,8 +1522,8 @@ | ||
| 1521 | 1522 | proc fromBits*(u: U64): F64; (* the F64 whose bits are u *) |
| 1522 | 1523 | proc mul*(a, b: U64): (U64, U64); (* a times b: the low 64 bits of the product, and the high *) |
| 1523 | 1524 | proc divMod*(hi, lo, d: U64): (U64, U64); (* hi, lo divided by d: the quotient and the remainder; hi < d *) |
| 1524 | -proc toReal*(s: []U8): (F64, BOOL); (* s as a real, the F64 nearest it; false if it isn't one *)</code></pre> | |
| 1525 | -<p>A <code>Buf</code>, a <code>List</code>, a <code>Map</code> and a <code>Vec</code> take their memory from the allocator <code>init</code> gives them. <code>Buf.add</code>, <code>str</code> and <code>spare</code> stop the program, as a failed check does, if the allocator has no room; <code>room</code> asks first. A program reads into a <code>Buf</code> through <code>spare</code>, then adds what it read to <code>len</code>: <code>s := Buf.spare(b, 4096); n := Sys.read(fd, s); if n > 0 then INC(b.len, n); end;</code>. <code>cut</code> takes back what was added, to a length it had: <code>start := b.len; ...; Buf.cut(b, start);</code>; a length past <code>len</code> stops the program, as an index past the end does. A <code>Vec</code> is <code>Buf</code> for any <code>T</code>: one block, which doubles when it is full, so <code>push</code> costs the same on average however long it grows. <code>all</code> gives the elements as a slice, indexed and checked as any is, and <code>v.len</code> is their number; the slice, and what <code>add</code> gives, are good until the array next grows. <code>pop</code> takes the last element off, keeping the memory. <code>push</code>, <code>add</code> and <code>resize</code> stop the program, as <code>Buf.add</code> does, if the allocator has no room; <code>room</code> asks first: <code>import Lines := Vec([]U8);</code>. <code>Fmt</code>’s format takes the next argument at each <code>{}</code>; <code>{{</code> and <code>}}</code> stand for <code>{</code> and <code>}</code>. A <code>Map</code> hashes and compares its keys with the procedures it is given: <code>import Count := Map([]U8, INT, Str.hash, Str.eq);</code>.</p> | |
| 1525 | +proc toReal*(s: []const U8): (F64, BOOL); (* s as a real, the F64 nearest it; false if it isn't one *)</code></pre> | |
| 1526 | +<p>A <code>Buf</code>, a <code>List</code>, a <code>Map</code> and a <code>Vec</code> take their memory from the allocator <code>init</code> gives them. <code>Buf.add</code>, <code>str</code> and <code>spare</code> stop the program, as a failed check does, if the allocator has no room; <code>room</code> asks first. A program reads into a <code>Buf</code> through <code>spare</code>, then adds what it read to <code>len</code>: <code>s := Buf.spare(b, 4096); n := Sys.read(fd, s); if n > 0 then INC(b.len, n); end;</code>. <code>cut</code> takes back what was added, to a length it had: <code>start := b.len; ...; Buf.cut(b, start);</code>; a length past <code>len</code> stops the program, as an index past the end does. A <code>Vec</code> is <code>Buf</code> for any <code>T</code>: one block, which doubles when it is full, so <code>push</code> costs the same on average however long it grows. <code>all</code> gives the elements as a slice, indexed and checked as any is, and <code>v.len</code> is their number; the slice, and what <code>add</code> gives, are good until the array next grows. <code>pop</code> takes the last element off, keeping the memory. <code>push</code>, <code>add</code> and <code>resize</code> stop the program, as <code>Buf.add</code> does, if the allocator has no room; <code>room</code> asks first: <code>import Lines := Vec([]U8);</code>. <code>Fmt</code>’s format takes the next argument at each <code>{}</code>; <code>{{</code> and <code>}}</code> stand for <code>{</code> and <code>}</code>. A <code>Map</code> hashes and compares its keys with the procedures it is given: <code>import Count := Map([]const U8, INT, Str.hash, Str.eq);</code>.</p> | |
| 1526 | 1527 | <p><code>Str</code> reads and writes UTF-8. <code>encode</code> writes a character, of at most 10FFFFH, in one to four bytes; a number that isn’t one becomes FFFDH, the character that stands for a wrong one. <code>decode</code> reads the first character of <code>s</code>, and gives FFFDH and 1 for a byte that doesn’t start one rightly, and 0 and 0 for an empty <code>s</code>: a continuation, a byte followed by too few continuations, a surrogate, a character beyond 10FFFFH or one spelled in more bytes than it needs. <code>decodeLast</code> reads the last character in the same way, to walk a string backwards. <code>valid</code> says that <code>decode</code> finds no such byte in <code>s</code>, and <code>chars</code> counts the characters, each such byte one. <code>next</code> walks a string a character at a time, as <code>decode</code> reads them: from 0, it gives the character at <code>i</code> and where the one after it starts, and 0 and -1 when <code>i</code> is at the end, or past it:</p> |
| 1527 | 1528 | <pre><code>c, i := Str.next(s, 0); |
| 1528 | 1529 | while i >= 0 do |
| @@ -1555,37 +1556,37 @@ | ||
| 1555 | 1556 | <p><code>Sort.sort</code> puts <code>s</code> in the order <code>less</code> gives, in place, without memory of its own, in time <code>n log n</code> at most; two elements equal by <code>less</code> may change places. <code>Math</code>’s procedures are as C’s <code>math.h</code> has them, to within the last bit or so; <code>roundEven</code> is IEEE 754’s default rounding, as Pascal’s <code>Round</code>, where <code>round</code> is C’s. <code>Fmt.real(x, places)</code> writes <code>x</code> with <code>places</code> digits after the point, at most 9, rounded to the nearest; from 1E15 on, with an exponent, as <code>1.50E17</code>; and <code>nan</code>, <code>inf</code> and <code>-inf</code> as themselves. <code>Fmt.short(x, out)</code> writes <code>x</code> into <code>out</code>, from 0, and gives those bytes: the fewest significant digits that <code>Math.toReal</code> reads back as the same <code>F64</code>, and of two such the nearer <code>x</code>: <code>0.1</code>, not <code>0.10000000000000001</code>; <code>42</code>, with no point; <code>0.0001</code>; and below 1E-4 or from 1E15 on with an exponent, as <code>1.5E17</code> or <code>5E-324</code>. Zero keeps its sign, <code>0</code> or <code>-0</code>, and <code>nan</code> and <code>inf</code> are as <code>real</code> writes them. The longest is <code>Fmt.ShortMost</code> bytes, so a call is <code>var d: [Fmt.ShortMost]U8; Fmt.print("{}", [Fmt.str(Fmt.short(x, d))]);</code>, and a width goes with <code>str</code>. The bytes are <code>out</code>’s: a second call with the same <code>out</code> overwrites them. <code>hex</code> writes capitals and <code>lowHex</code> lower case, neither with a <code>0x</code>. A width between the braces pads an argument to that many bytes: <code>{2}</code> with spaces on the left, <code>{02}</code> with zeros on the left, after a sign (<code>{04}</code> of -7 is <code>-007</code>), <code>{-10}</code> with spaces on the right. A longer argument is written whole. A <code>{</code> followed by anything but <code>}</code> or a width is dropped, as before. <code>Fmt.print("{2}:{02} U+{04}", [Fmt.int(3), Fmt.int(7), Fmt.hex(0E9H)])</code> writes <code>3:07 U+00E9</code>. <code>Fmt.into</code> formats into a slice the caller gives, with no memory of its own, for a library that has no heap: it gives the bytes written, and false if the slice was too short and the text is cut there.</p> |
| 1556 | 1557 | <p><strong>Files and Opt</strong> are what most programs need of their command line. <code>Files.writeAll</code> writes again after a short write or a signal, as a pipe or a socket needs, and <code>write</code> is made over it. <code>realPath</code> adds to <code>b</code> the absolute path of what <code>path</code> names, with no <code>.</code>, <code>..</code> or symbolic link in it, and leaves the working directory alone: -2 (<code>ENOENT</code>) if a part isn’t there, -20 (<code>ENOTDIR</code>) if a file is used as a directory, and -40 (<code>ELOOP</code>) after 40 links. <code>b</code> is left as it was on an error. <code>removeTree</code> removes a file, or a directory with all it holds; a symbolic link is removed itself and never followed.</p> |
| 1557 | 1558 | <pre><code>module Files; |
| 1558 | -proc arg*(i: INT): []U8; (* argument i; 0 is the program's name; "" if there is none *) | |
| 1559 | -proc read*(path: []U8; var b: Buf.Buf): BOOL; (* the whole file added to b *) | |
| 1560 | -proc load*(path: []U8; var b: Buf.Buf): INT; (* as read: 0, or minus the errno that stopped it *) | |
| 1561 | -proc write*(path, data: []U8): BOOL; (* the file made to hold data; false if it or its close failed *) | |
| 1562 | -proc writeAll*(fd: INT; s: []U8): INT; (* all of s written to fd; 0, or minus the errno *) | |
| 1563 | -proc realPath*(path: []U8; var b: Buf.Buf): INT; (* the absolute path of path, plain, added to b; 0, or minus an errno *) | |
| 1564 | -proc removeTree*(path: []U8): INT; (* the file, or the directory and all in it, removed; 0, or minus the first errno *) | |
| 1559 | +proc arg*(i: INT): []const U8; (* argument i; 0 is the program's name; "" if there is none *) | |
| 1560 | +proc read*(path: []const U8; var b: Buf.Buf): BOOL; (* the whole file added to b *) | |
| 1561 | +proc load*(path: []const U8; var b: Buf.Buf): INT; (* as read: 0, or minus the errno that stopped it *) | |
| 1562 | +proc write*(path, data: []const U8): BOOL; (* the file made to hold data; false if it or its close failed *) | |
| 1563 | +proc writeAll*(fd: INT; s: []const U8): INT; (* all of s written to fd; 0, or minus the errno *) | |
| 1564 | +proc realPath*(path: []const U8; var b: Buf.Buf): INT; (* the absolute path of path, plain, added to b; 0, or minus an errno *) | |
| 1565 | +proc removeTree*(path: []const U8): INT; (* the file, or the directory and all in it, removed; 0, or minus the first errno *) | |
| 1565 | 1566 | |
| 1566 | 1567 | module Opt; |
| 1567 | 1568 | type |
| 1568 | 1569 | Row* = variant |
| 1569 | - usage(scope, line: []U8); (* a synopsis line, without "usage: " *) | |
| 1570 | - sub(scope, name, help: []U8); (* a subcommand *) | |
| 1571 | - option(scope: []U8; letter: U8; long, value, help: []U8); | |
| 1570 | + usage(scope, line: []const U8); (* a synopsis line, without "usage: " *) | |
| 1571 | + sub(scope, name, help: []const U8); (* a subcommand *) | |
| 1572 | + option(scope: []const U8; letter: U8; long, value, help: []const U8); | |
| 1572 | 1573 | end; |
| 1573 | 1574 | Parser* = record |
| 1574 | 1575 | letter*: U8; (* the option just read *) |
| 1575 | - value*: []U8; (* its value; "" for a flag *) | |
| 1576 | + value*: []const U8; (* its value; "" for a flag *) | |
| 1576 | 1577 | long*: BOOL; (* it was written by its long name *) |
| 1577 | - spelt*: []U8; (* as it was written, no dashes: "b" or "bar"; to read only *) | |
| 1578 | + spelt*: []const U8; (* as it was written, no dashes: "b" or "bar"; to read only *) | |
| 1578 | 1579 | ... |
| 1579 | 1580 | end; |
| 1580 | -proc init*(var p: Parser; name: []U8; table: []Row); (* the arguments from 1 on, by table *) | |
| 1581 | +proc init*(var p: Parser; name: []const U8; table: []const Row); (* the arguments from 1 on, by table *) | |
| 1581 | 1582 | proc next*(var p: Parser): BOOL; (* the next option; false when they end *) |
| 1582 | -proc operand*(var p: Parser): ([]U8, BOOL); (* the next operand; false when there are no more *) | |
| 1583 | +proc operand*(var p: Parser): ([]const U8, BOOL); (* the next operand; false when there are no more *) | |
| 1583 | 1584 | proc at*(var p: Parser): INT; (* the argument operand would give next *) |
| 1584 | -proc scope*(var p: Parser; name: []U8); (* subcommand name's options, from the word after it *) | |
| 1585 | +proc scope*(var p: Parser; name: []const U8); (* subcommand name's options, from the word after it *) | |
| 1585 | 1586 | proc number*(const p: Parser; lo, hi: I64): I64; (* the value just read, a number from lo to hi *) |
| 1586 | -proc refuse*(const p: Parser; wants: []U8); (* the value just read refused, as number refuses one *) | |
| 1587 | -proc fail*(const p: Parser; text: []U8); (* the program's own mistake *) | |
| 1588 | -proc misuse*(const p: Parser; text: []U8); (* the program's own mistake, with the synopsis *)</code></pre> | |
| 1587 | +proc refuse*(const p: Parser; wants: []const U8); (* the value just read refused, as number refuses one *) | |
| 1588 | +proc fail*(const p: Parser; text: []const U8); (* the program's own mistake *) | |
| 1589 | +proc misuse*(const p: Parser; text: []const U8); (* the program's own mistake, with the synopsis *)</code></pre> | |
| 1589 | 1590 | <p><code>Opt</code> reads a command line by the rules of the C library’s <code>getopt</code>, with GNU’s long options, so that a program’s options behave as every other Unix program’s. The program describes itself once, in a table of <code>Row</code>s: its synopsis lines, its subcommands and its options. The help and the messages for mistakes are made from the table, so they can’t drift from it; the program’s test of <code>p.letter</code> is the only other place an option appears. The parser is a record the program owns, and the table a slice it keeps as long as the parser: a literal written in the call to <code>init</code> lives as long as the procedure that makes it, and a constant, <code>const Rows: []Opt.Row = [...]</code>, as long as the program.</p> |
| 1590 | 1591 | <p>A row’s <code>scope</code> is <code>""</code> for the program itself, or a subcommand’s name: <code>"start"</code>, or <code>"remote.add"</code> for a subcommand of <code>remote</code>. An option has a <code>letter</code>, a <code>long</code> name, or both. One with a long name only has a key of its own below <code>'!'</code> for a letter, so that the program can tell it from the others. With a <code>value</code>, the name the help and the messages give it, the option takes a value; with <code>""</code> it is a flag. A value written in brackets, <code>"[when]"</code>, may be left out, and is <code>""</code> then: it is given only in the option’s own word, <code>--color=always</code> or <code>-calways</code>, never as the next, as <code>getopt</code>’s <code>::</code> has it, and the help shows it as <code>--color[=when]</code>.</p> |
| 1591 | 1592 | <pre><code>const wait = 1; (* the key of --wait, which has no letter *) |
| @@ -1634,7 +1635,7 @@ | ||
| 1634 | 1635 | <p><strong>Width</strong> says how many columns of a terminal text takes, for a program with a screen that wraps its lines and cuts them at the window’s edge.</p> |
| 1635 | 1636 | <pre><code>module Width; |
| 1636 | 1637 | proc width*(c: I32): INT; (* the columns the character c takes: 0, 1 or 2 *) |
| 1637 | -proc columns*(s: []U8): INT; (* the columns the UTF-8 text s takes *)</code></pre> | |
| 1638 | +proc columns*(s: []const U8): INT; (* the columns the UTF-8 text s takes *)</code></pre> | |
| 1638 | 1639 | <p>A combining mark, a format character, a control and Hangul’s medial and final jamo take 0 columns; an ideograph, a kana, most emoji and the rest that Unicode calls East Asian Wide or Fullwidth take 2; every other character takes 1, the ambiguous ones among them, as most terminals draw them. These are the C library’s <code>wcwidth</code>’s answers, but for a control, which is -1 there. <code>Width</code> knows characters, not clusters: an emoji joined to another by a zero width joiner, or a flag, counts as its parts. Its tables, about 4 KB, come from Unicode’s data, made again by <code>tools/Widths.raz</code> for a new version of Unicode; a program that imports <code>Width</code> and calls neither procedure carries none of them.</p> |
| 1639 | 1640 | <p><strong>Rand, Check and Prop</strong> are for testing a program, and are standard library, not core: a target may leave them out. <code>raztest(1)</code> builds and runs the tests that use them.</p> |
| 1640 | 1641 | <p><code>Rand</code> is a seeded generator of numbers, splitmix64: one <code>U64</code> of state. The same seed gives the same numbers on every machine, so a run can be replayed. It is not for secrets.</p> |
| @@ -1667,9 +1668,9 @@ | ||
| 1667 | 1668 | end Dice.</code></pre> |
| 1668 | 1669 | <p><code>Check</code> says what a test expects. A check that fails writes <code>FAIL</code> and its words on standard error, with what it got and what it wanted where they differ; the program goes on. At its end, if any check failed, it says how many and exits with status 1, and a run with none failed says nothing and keeps its own status. No call is needed at the end: the first check asks <code>Rt.atExit</code> to make it. A program that stops after a check failed, by <code>Rt.stop</code> or a failed check of the language, ends with status 1 too. <code>mute</code> is for <code>Prop</code>, which searches with the checks silenced.</p> |
| 1669 | 1670 | <pre><code>module Check; |
| 1670 | -proc ok*(cond: BOOL; what: []U8); (* fails, saying what, when cond is false *) | |
| 1671 | -proc str*(what, got, want: []U8); (* fails when got is not want; shows both, escaped, and the byte where they differ *) | |
| 1672 | -proc int*(what: []U8; got, want: I64); (* fails when got is not want; shows both *) | |
| 1671 | +proc ok*(cond: BOOL; what: []const U8); (* fails, saying what, when cond is false *) | |
| 1672 | +proc str*(what, got, want: []const U8); (* fails when got is not want; shows both, escaped, and the byte where they differ *) | |
| 1673 | +proc int*(what: []const U8; got, want: I64); (* fails when got is not want; shows both *) | |
| 1673 | 1674 | proc mute*(on: BOOL); (* while on, checks say nothing and are not counted *) |
| 1674 | 1675 | proc mutedFailures*(): INT; (* how many failed since mute was turned on *)</code></pre> |
| 1675 | 1676 | <pre><code>module Digits; |
| @@ -1702,7 +1703,7 @@ | ||
| 1702 | 1703 | proc pick*(var s: Source; n: INT): INT; (* 0 to n - 1; n at least 1 *) |
| 1703 | 1704 | proc bool*(var s: Source): BOOL; |
| 1704 | 1705 | proc bytes*(var s: Source; max: INT; var b: Buf.Buf); (* b, emptied, then given at most max bytes *) |
| 1705 | -proc check*(name: []U8; p: Property); (* runs p on the saved cases, then on fresh ones *)</code></pre> | |
| 1706 | +proc check*(name: []const U8; p: Property); (* runs p on the saved cases, then on fresh ones *)</code></pre> | |
| 1706 | 1707 | <p><code>check</code> runs <code>p</code> on each case saved in the directory <code>$PROP_DIR/name</code> (<code>test/seeds/name</code> if <code>PROP_DIR</code> is unset), then on <code>$PROP_CASES</code> fresh cases (100 if unset), from the seed <code>$PROP_SEED</code> or a new one, or for as many seconds as <code>$PROP_TIME</code> says, if it is set. Each is a whole number. A property that holds says nothing and writes no file. The first case that fails is shrunk and saved in a file named by its hash; standard error says the seed and the file; the shrunk case runs once more with <code>Check</code> heard, and the program ends with status 1. A saved case is run first the next time, so a failure stays found until it is mended; keep the directory with the tests. A case that stops the program, by <code>Rt.stop</code>, a failed check of the language or a signal, can’t be shrunk; it is saved as it was, when the program ends. <code>raztest --seed</code> and <code>--fuzz</code> set <code>PROP_SEED</code> and <code>PROP_TIME</code>.</p> |
| 1707 | 1708 | <pre><code>module Reverse; |
| 1708 | 1709 | import Prop, Check, Buf, Heap; |
| @@ -79,6 +79,7 @@ | ||
| 79 | 79 | <p><code>...</code> after an <code>extern</code>'s parameters takes any number of further arguments, as <code>printf</code> wants. A string literal goes as a C string, an integer constant as an <code>INT</code>, a real constant as an <code>F64</code>. An <code>F32</code> is an error there, since C promotes it; write <code>F64(x)</code>.</p> |
| 80 | 80 | <div class="file"><span>Say.raz</span></div><pre class="code"><code><span class="kw">module</span> Say; |
| 81 | 81 | <span class="kw">import</span> Out; |
| 82 | +<span class="kw">import</span> SYSTEM; | |
| 82 | 83 | |
| 83 | 84 | <span class="kw">extern</span> <span class="kw">proc</span> printf(format: ^<span class="ty">U8</span>; ...): <span class="ty">I32</span>; |
| 84 | 85 | <span class="kw">extern</span> <span class="kw">proc</span> fflush(f: ^<span class="ty">U8</span>): <span class="ty">I32</span>; |
| @@ -108,6 +109,7 @@ | ||
| 108 | 109 | <p><code>link "libm.so.6";</code> names a shared library to load at run time. An <code>extern var</code> is C's variable itself: reading it reads C's, assigning it writes C's.</p> |
| 109 | 110 | <div class="file"><span>Env.raz</span></div><pre class="code"><code><span class="kw">module</span> Env; |
| 110 | 111 | <span class="kw">import</span> Out; |
| 112 | +<span class="kw">import</span> SYSTEM; | |
| 111 | 113 | |
| 112 | 114 | <span class="kw">extern</span> <span class="kw">var</span> environ: ^^<span class="ty">U8</span>; |
| 113 | 115 | <span class="kw">extern</span> <span class="kw">proc</span> strlen(s: ^<span class="ty">U8</span>): <span class="ty">UINT</span>; |
| @@ -133,6 +135,7 @@ | ||
| 133 | 135 | <p>Every raz procedure uses the System V calling convention, so any with parameters and a result of C's types may be given to C to call, as <code>qsort</code> is given <code>order</code> above. The procedure may not be a closure: it has only its arguments, so state travels through the pointer C hands back or a module variable.</p> |
| 134 | 136 | <div class="file"><span>Roots.raz</span></div><pre class="code"><code><span class="kw">module</span> Roots; |
| 135 | 137 | <span class="kw">import</span> Fmt; |
| 138 | +<span class="kw">import</span> SYSTEM; | |
| 136 | 139 | |
| 137 | 140 | <span class="kw">link</span> <span class="string">"libm.so.6"</span>; |
| 138 | 141 |
| @@ -71,6 +71,7 @@ | ||
| 71 | 71 | <p>The same shape of message tells of a <code>nil</code> pointer, a division by zero, a bad slice and a stack overflow. Each of them stops the program with status 2, and never runs on with a wrong value.</p> |
| 72 | 72 | <div class="file"><span>Nilp.raz</span></div><pre class="code"><code><span class="kw">module</span> Nilp; |
| 73 | 73 | <span class="kw">import</span> Out; |
| 74 | +<span class="kw">import</span> SYSTEM; | |
| 74 | 75 | |
| 75 | 76 | <span class="kw">type</span> Node = <span class="kw">record</span> val: <span class="ty">INT</span>; next: ^Node; <span class="kw">end</span>; |
| 76 | 77 | |
| @@ -84,16 +85,16 @@ | ||
| 84 | 85 | <span class="kw">var</span> a, b: Node; |
| 85 | 86 | <span class="kw">begin</span> |
| 86 | 87 | a.val := <span class="number">1</span>; b.val := <span class="number">2</span>; |
| 87 | - a.next := <span class="ty">ADR</span>(b); | |
| 88 | - Out.int(last(<span class="ty">ADR</span>(a))); Out.ln; | |
| 88 | + a.next := SYSTEM.<span class="ty">ADR</span>(b); | |
| 89 | + Out.int(last(SYSTEM.<span class="ty">ADR</span>(a))); Out.ln; | |
| 89 | 90 | Out.int(last(<span class="ty">nil</span>)); Out.ln; |
| 90 | 91 | <span class="kw">end</span>; |
| 91 | 92 | |
| 92 | 93 | <span class="kw">end</span> Nilp.</code></pre> |
| 93 | 94 | <pre class="output"><code>2 |
| 94 | -<span class="err">Nilp.raz:8:11: stopped by a nil pointer | |
| 95 | - in Nilp.last at Nilp.raz:8:11: n = nil | |
| 96 | - in Nilp.main at Nilp.raz:18:5 | |
| 95 | +<span class="err">Nilp.raz:9:11: stopped by a nil pointer | |
| 96 | + in Nilp.last at Nilp.raz:9:11: n = nil | |
| 97 | + in Nilp.main at Nilp.raz:19:5 | |
| 97 | 98 | </span></code></pre> |
| 98 | 99 | <div class="file"><span>Zero.raz</span></div><pre class="code"><code><span class="kw">module</span> Zero; |
| 99 | 100 | <span class="kw">import</span> Out; |
| @@ -52,7 +52,7 @@ | ||
| 52 | 52 | <span class="kw">end</span>; |
| 53 | 53 | |
| 54 | 54 | <span class="comment">(* the lines of the file at path, or -1 if it can't be read *)</span> |
| 55 | -<span class="kw">proc</span> lines(path: []<span class="ty">U8</span>): <span class="ty">INT</span>; | |
| 55 | +<span class="kw">proc</span> lines(path: []<span class="kw">const</span> <span class="ty">U8</span>): <span class="ty">INT</span>; | |
| 56 | 56 | <span class="kw">var</span> fd, n, i, count: <span class="ty">INT</span>; buf: [<span class="number">4096</span>]<span class="ty">U8</span>; |
| 57 | 57 | <span class="kw">begin</span> |
| 58 | 58 | fd := Sys.openRead(path); |
| @@ -47,7 +47,7 @@ | ||
| 47 | 47 | TooBig*: <span class="ty">ERROR</span> = <span class="string">"not a number up to 1000"</span>; |
| 48 | 48 | |
| 49 | 49 | <span class="comment">(* the number s holds, up to 1000 *)</span> |
| 50 | -<span class="kw">proc</span> number(s: []<span class="ty">U8</span>): (<span class="ty">INT</span>, <span class="ty">ERROR</span>); | |
| 50 | +<span class="kw">proc</span> number(s: []<span class="kw">const</span> <span class="ty">U8</span>): (<span class="ty">INT</span>, <span class="ty">ERROR</span>); | |
| 51 | 51 | <span class="kw">var</span> n: <span class="ty">I64</span>; ok: <span class="ty">BOOL</span>; |
| 52 | 52 | <span class="kw">begin</span> |
| 53 | 53 | <span class="kw">if</span> <span class="ty">LEN</span>(s) = <span class="number">0</span> <span class="kw">then</span> <span class="kw">return</span> <span class="number">0</span>, Empty; <span class="kw">end</span>; |
| @@ -57,7 +57,7 @@ | ||
| 57 | 57 | <span class="kw">end</span>; |
| 58 | 58 | |
| 59 | 59 | <span class="comment">(* the sum of the numbers in words: the first error ends it *)</span> |
| 60 | -<span class="kw">proc</span> total(words: [][]<span class="ty">U8</span>): (<span class="ty">INT</span>, <span class="ty">ERROR</span>); | |
| 60 | +<span class="kw">proc</span> total(words: [][]<span class="kw">const</span> <span class="ty">U8</span>): (<span class="ty">INT</span>, <span class="ty">ERROR</span>); | |
| 61 | 61 | <span class="kw">var</span> i, sum: <span class="ty">INT</span>; |
| 62 | 62 | <span class="kw">begin</span> |
| 63 | 63 | <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="ty">LEN</span>(words) - <span class="number">1</span> <span class="kw">do</span> |
| @@ -67,7 +67,7 @@ | ||
| 67 | 67 | <span class="kw">end</span>; |
| 68 | 68 | |
| 69 | 69 | <span class="kw">proc</span> main*; |
| 70 | -<span class="kw">var</span> words: [<span class="number">3</span>][]<span class="ty">U8</span>; i, n: <span class="ty">INT</span>; err: <span class="ty">ERROR</span>; | |
| 70 | +<span class="kw">var</span> words: [<span class="number">3</span>][]<span class="kw">const</span> <span class="ty">U8</span>; i, n: <span class="ty">INT</span>; err: <span class="ty">ERROR</span>; | |
| 71 | 71 | <span class="kw">begin</span> |
| 72 | 72 | words := [<span class="string">"42"</span>, <span class="string">""</span>, <span class="string">"12345"</span>]; |
| 73 | 73 | <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="number">2</span> <span class="kw">do</span> |
| @@ -103,7 +103,7 @@ | ||
| 103 | 103 | Range*: <span class="ty">ERROR</span> = <span class="string">"out of range"</span>; |
| 104 | 104 | |
| 105 | 105 | <span class="comment">(* a percentage, 0 to 100 *)</span> |
| 106 | -<span class="kw">proc</span> percent(s: []<span class="ty">U8</span>): (<span class="ty">INT</span>, <span class="ty">ERROR</span>); | |
| 106 | +<span class="kw">proc</span> percent(s: []<span class="kw">const</span> <span class="ty">U8</span>): (<span class="ty">INT</span>, <span class="ty">ERROR</span>); | |
| 107 | 107 | <span class="kw">var</span> n: <span class="ty">I64</span>; ok: <span class="ty">BOOL</span>; |
| 108 | 108 | <span class="kw">begin</span> |
| 109 | 109 | <span class="kw">if</span> <span class="ty">LEN</span>(s) = <span class="number">0</span> <span class="kw">then</span> <span class="kw">return</span> <span class="number">0</span>, Empty; <span class="kw">end</span>; |
| @@ -114,7 +114,7 @@ | ||
| 114 | 114 | <span class="kw">end</span>; |
| 115 | 115 | |
| 116 | 116 | <span class="kw">proc</span> main*; |
| 117 | -<span class="kw">var</span> inputs: [<span class="number">4</span>][]<span class="ty">U8</span>; i, n: <span class="ty">INT</span>; err: <span class="ty">ERROR</span>; | |
| 117 | +<span class="kw">var</span> inputs: [<span class="number">4</span>][]<span class="kw">const</span> <span class="ty">U8</span>; i, n: <span class="ty">INT</span>; err: <span class="ty">ERROR</span>; | |
| 118 | 118 | <span class="kw">begin</span> |
| 119 | 119 | inputs := [<span class="string">"42"</span>, <span class="string">""</span>, <span class="string">"forty"</span>, <span class="string">"140"</span>]; |
| 120 | 120 | <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="number">3</span> <span class="kw">do</span> |
| @@ -146,7 +146,7 @@ | ||
| 146 | 146 | <span class="kw">const</span> Missing*: <span class="ty">ERROR</span> = <span class="string">"key missing"</span>; |
| 147 | 147 | |
| 148 | 148 | <span class="comment">(* the number after "n=" in a line like "n=17" *)</span> |
| 149 | -<span class="kw">proc</span> value(line: []<span class="ty">U8</span>): (<span class="ty">INT</span>, <span class="ty">ERROR</span>); | |
| 149 | +<span class="kw">proc</span> value(line: []<span class="kw">const</span> <span class="ty">U8</span>): (<span class="ty">INT</span>, <span class="ty">ERROR</span>); | |
| 150 | 150 | <span class="kw">var</span> n: <span class="ty">I64</span>; ok: <span class="ty">BOOL</span>; |
| 151 | 151 | <span class="kw">begin</span> |
| 152 | 152 | <span class="kw">if</span> <span class="kw">not</span> Str.hasPrefix(line, <span class="string">"n="</span>) <span class="kw">then</span> <span class="kw">return</span> <span class="number">0</span>, Missing; <span class="kw">end</span>; |
| @@ -156,7 +156,7 @@ | ||
| 156 | 156 | <span class="kw">end</span>; |
| 157 | 157 | |
| 158 | 158 | <span class="comment">(* the sum of the values of two lines: the first error ends it *)</span> |
| 159 | -<span class="kw">proc</span> sum(a, b: []<span class="ty">U8</span>): (<span class="ty">INT</span>, <span class="ty">ERROR</span>); | |
| 159 | +<span class="kw">proc</span> sum(a, b: []<span class="kw">const</span> <span class="ty">U8</span>): (<span class="ty">INT</span>, <span class="ty">ERROR</span>); | |
| 160 | 160 | <span class="kw">begin</span> |
| 161 | 161 | <span class="kw">return</span> <span class="kw">try</span> value(a) + <span class="kw">try</span> value(b), <span class="ty">nil</span>; |
| 162 | 162 | <span class="kw">end</span>; |
| @@ -57,14 +57,14 @@ | ||
| 57 | 57 | <p>and a program that uses it:</p> |
| 58 | 58 | <div class="file"><span>Generics.raz</span></div><pre class="code"><code><span class="kw">module</span> Generics; |
| 59 | 59 | <span class="kw">import</span> Out, Ints := Biggest(<span class="ty">INT</span>, before), |
| 60 | - Words := Biggest([]<span class="ty">U8</span>, shorter), Order := Sort(<span class="ty">INT</span>, before); | |
| 60 | + Words := Biggest([]<span class="kw">const</span> <span class="ty">U8</span>, shorter), Order := Sort(<span class="ty">INT</span>, before); | |
| 61 | 61 | |
| 62 | 62 | <span class="kw">proc</span> before(a, b: <span class="ty">INT</span>): <span class="ty">BOOL</span>; |
| 63 | 63 | <span class="kw">begin</span> |
| 64 | 64 | <span class="kw">return</span> a < b; |
| 65 | 65 | <span class="kw">end</span>; |
| 66 | 66 | |
| 67 | -<span class="kw">proc</span> shorter(a, b: []<span class="ty">U8</span>): <span class="ty">BOOL</span>; | |
| 67 | +<span class="kw">proc</span> shorter(a, b: []<span class="kw">const</span> <span class="ty">U8</span>): <span class="ty">BOOL</span>; | |
| 68 | 68 | <span class="kw">begin</span> |
| 69 | 69 | <span class="kw">return</span> <span class="ty">LEN</span>(a) < <span class="ty">LEN</span>(b); |
| 70 | 70 | <span class="kw">end</span>; |
| @@ -90,6 +90,7 @@ | ||
| 90 | 90 | <p>A generic module is an ordinary module with parameters in its header. Here is a stack of anything, which takes the allocator it grows from.</p> |
| 91 | 91 | <div class="file"><span>Stk.raz</span></div><pre class="code"><code><span class="kw">module</span> Stk(T); |
| 92 | 92 | <span class="kw">import</span> Mem; |
| 93 | +<span class="kw">import</span> SYSTEM; | |
| 93 | 94 | |
| 94 | 95 | <span class="kw">type</span> |
| 95 | 96 | Stack* = <span class="kw">record</span> |
| @@ -142,21 +143,22 @@ | ||
| 142 | 143 | <span class="kw">end</span> Stk.</code></pre> |
| 143 | 144 | <div class="file"><span>Uses.raz</span></div><pre class="code"><code><span class="kw">module</span> Uses; |
| 144 | 145 | <span class="kw">import</span> Out, Heap, Nums := Stk(<span class="ty">INT</span>), Pairs := Stk(Pair); |
| 146 | +<span class="kw">import</span> SYSTEM; | |
| 145 | 147 | |
| 146 | 148 | <span class="kw">type</span> Pair = <span class="kw">record</span> a, b: <span class="ty">INT</span>; <span class="kw">end</span>; |
| 147 | 149 | |
| 148 | 150 | <span class="kw">proc</span> main*; |
| 149 | 151 | <span class="kw">var</span> n: Nums.Stack; p: Pairs.Stack; i, v: <span class="ty">INT</span>; ok: <span class="ty">BOOL</span>; pr: Pair; |
| 150 | 152 | <span class="kw">begin</span> |
| 151 | - Nums.init(<span class="ty">ADR</span>(n), Heap.heap); | |
| 152 | - Pairs.init(<span class="ty">ADR</span>(p), Heap.heap); | |
| 153 | - <span class="kw">for</span> i := <span class="number">1</span> <span class="kw">to</span> <span class="number">10</span> <span class="kw">do</span> _ := Nums.push(<span class="ty">ADR</span>(n), i * i); <span class="kw">end</span>; | |
| 154 | - _ := Pairs.push(<span class="ty">ADR</span>(p), {a: <span class="number">3</span>, b: <span class="number">4</span>}); | |
| 155 | - v, ok := Nums.pop(<span class="ty">ADR</span>(n)); | |
| 153 | + Nums.init(SYSTEM.<span class="ty">ADR</span>(n), Heap.heap); | |
| 154 | + Pairs.init(SYSTEM.<span class="ty">ADR</span>(p), Heap.heap); | |
| 155 | + <span class="kw">for</span> i := <span class="number">1</span> <span class="kw">to</span> <span class="number">10</span> <span class="kw">do</span> _ := Nums.push(SYSTEM.<span class="ty">ADR</span>(n), i * i); <span class="kw">end</span>; | |
| 156 | + _ := Pairs.push(SYSTEM.<span class="ty">ADR</span>(p), {a: <span class="number">3</span>, b: <span class="number">4</span>}); | |
| 157 | + v, ok := Nums.pop(SYSTEM.<span class="ty">ADR</span>(n)); | |
| 156 | 158 | Out.int(v); Out.str(<span class="string">" of "</span>); Out.int(n.len + <span class="number">1</span>); Out.ln; |
| 157 | - pr, ok := Pairs.pop(<span class="ty">ADR</span>(p)); | |
| 159 | + pr, ok := Pairs.pop(SYSTEM.<span class="ty">ADR</span>(p)); | |
| 158 | 160 | Out.int(pr.a * pr.b); Out.ln; |
| 159 | - _, ok := Pairs.pop(<span class="ty">ADR</span>(p)); | |
| 161 | + _, ok := Pairs.pop(SYSTEM.<span class="ty">ADR</span>(p)); | |
| 160 | 162 | <span class="kw">if</span> <span class="kw">not</span> ok <span class="kw">then</span> Out.str(<span class="string">"empty\n"</span>); <span class="kw">end</span>; |
| 161 | 163 | <span class="kw">end</span>; |
| 162 | 164 | |
| @@ -167,7 +169,7 @@ | ||
| 167 | 169 | </code></pre> |
| 168 | 170 | <p>Two instances (<code>Nums</code> and <code>Pairs</code>) are two separate modules, compiled with their own <code>T</code>. Its pointer arithmetic is the one place where the program does what C does and leaves the checks behind; a library would keep that inside, as this does.</p> |
| 169 | 171 | <h2>The library's generics</h2> |
| 170 | -<p>The library's <code>List</code>, <code>Map</code> and <code>Sort</code> are generic modules. A <code>Map</code> takes its key hash and equality as procedure arguments: <code>import Count := Map([]U8, INT, Str.hash, Str.eq);</code>. The advanced tour uses each of them in a real program.</p><p class="tryit">Save a program under its module’s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="checks.html">← When it goes wrong</a><a href="index.html">Contents</a><a class="next" href="c.html">Talking to C →</a></div></article></div> | |
| 172 | +<p>The library's <code>List</code>, <code>Map</code> and <code>Sort</code> are generic modules. A <code>Map</code> takes its key hash and equality as procedure arguments: <code>import Count := Map([]const U8, INT, Str.hash, Str.eq);</code>. The advanced tour uses each of them in a real program.</p><p class="tryit">Save a program under its module’s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="checks.html">← When it goes wrong</a><a href="index.html">Contents</a><a class="next" href="c.html">Talking to C →</a></div></article></div> | |
| 171 | 173 | </main> |
| 172 | 174 | <footer>raz is under the ISC license. <a href="../source/index.html">Source</a></footer> |
| 173 | 175 | </body> |
| @@ -70,7 +70,7 @@ | ||
| 70 | 70 | <span class="cmd">$ raz run Quiet.raz</span> |
| 71 | 71 | hello, again |
| 72 | 72 | <span class="cmd">$ ls -l Quiet | awk '{print $5, "bytes"}'</span> |
| 73 | -28680 bytes</code></pre> | |
| 73 | +28608 bytes</code></pre> | |
| 74 | 74 | <p>The program is a static executable with no run time besides the code it uses, so it is a few kilobytes, and runs without any library installed.</p> |
| 75 | 75 | <h2>What <code>Out</code> can write</h2> |
| 76 | 76 | <p><code>Out</code> is the library's buffered output. It writes strings, single characters, numbers in decimal or hexadecimal, and a line feed. A string is written as it is, with <code>\n</code> for a line feed, <code>\t</code> for a tab, <code>\"</code> and <code>\\</code>; or between backquotes, as a raw string with no escapes at all, which may run over lines (lesson 6).</p> |
| @@ -110,9 +110,10 @@ | ||
| 110 | 110 | exiting with 3 |
| 111 | 111 | status 3</code></pre> |
| 112 | 112 | <h2>Arguments</h2> |
| 113 | -<p><code>ARGC()</code> is the number of command line arguments, with the program's own name first, and <code>ARGV()</code> the arguments, as C strings. Most programs read them through <code>Files.arg</code> and <code>Opt</code>, which the advanced tour shows; here is the raw form.</p> | |
| 113 | +<p><code>ARGC()</code> is the number of command line arguments, with the program's own name first, and <code>SYSTEM.ARGV()</code> the arguments, as C strings. Most programs read them through <code>Files.arg</code> and <code>Opt</code>, which the advanced tour shows; here is the raw form.</p> | |
| 114 | 114 | <div class="file"><span>Args.raz</span></div><pre class="code"><code><span class="kw">module</span> Args; |
| 115 | 115 | <span class="kw">import</span> Out; |
| 116 | +<span class="kw">import</span> SYSTEM; | |
| 116 | 117 | |
| 117 | 118 | <span class="comment">(* The length of a C string. *)</span> |
| 118 | 119 | <span class="kw">proc</span> len(s: ^<span class="ty">U8</span>): <span class="ty">INT</span>; |
| @@ -127,7 +128,7 @@ | ||
| 127 | 128 | <span class="kw">var</span> i: <span class="ty">INT</span>; s: ^<span class="ty">U8</span>; |
| 128 | 129 | <span class="kw">begin</span> |
| 129 | 130 | <span class="kw">for</span> i := <span class="number">1</span> <span class="kw">to</span> ARGC() - <span class="number">1</span> <span class="kw">do</span> |
| 130 | - s := ARGV()[i]; | |
| 131 | + s := SYSTEM.ARGV()[i]; | |
| 131 | 132 | Out.str(s[<span class="number">0</span>..len(s)]); |
| 132 | 133 | Out.ln; |
| 133 | 134 | <span class="kw">end</span>; |
| @@ -140,7 +140,7 @@ | ||
| 140 | 140 | <span class="kw">const</span> Prime = <span class="number">100000001B3H</span>; |
| 141 | 141 | |
| 142 | 142 | <span class="comment">(* FNV-1a: xor each byte in, then multiply, all modulo 2^64 *)</span> |
| 143 | -<span class="kw">proc</span> fnv(s: []<span class="ty">U8</span>): <span class="ty">U64</span>; | |
| 143 | +<span class="kw">proc</span> fnv(s: []<span class="kw">const</span> <span class="ty">U8</span>): <span class="ty">U64</span>; | |
| 144 | 144 | <span class="kw">var</span> h: <span class="ty">U64</span>; i: <span class="ty">INT</span>; |
| 145 | 145 | <span class="kw">begin</span> |
| 146 | 146 | h := Offset; |
| @@ -83,6 +83,7 @@ | ||
| 83 | 83 | <p>A procedure that allocates takes the allocator it uses, so its caller decides where the memory comes from and when it goes: the heap for something that lives long, an arena for everything one request needs. Nothing in the library allocates behind your back.</p> |
| 84 | 84 | <div class="file"><span>Pool.raz</span></div><pre class="code"><code><span class="kw">module</span> Pool; |
| 85 | 85 | <span class="kw">import</span> Out, Mem, Arena; |
| 86 | +<span class="kw">import</span> SYSTEM; | |
| 86 | 87 | |
| 87 | 88 | <span class="kw">type</span> Item = <span class="kw">record</span> id: <span class="ty">INT</span>; next: ^Item; <span class="kw">end</span>; |
| 88 | 89 | |
| @@ -113,15 +114,15 @@ | ||
| 113 | 114 | <span class="kw">var</span> ar: Arena.Arena; small: Arena.Arena; list: ^Item; |
| 114 | 115 | <span class="kw">begin</span> |
| 115 | 116 | <span class="kw">if</span> <span class="kw">not</span> Arena.init(ar, <span class="number">1000000</span>) <span class="kw">then</span> <span class="kw">return</span>; <span class="kw">end</span>; |
| 116 | - list := make(<span class="number">1000</span>, <span class="ty">ADR</span>(ar.base)); | |
| 117 | + list := make(<span class="number">1000</span>, SYSTEM.<span class="ty">ADR</span>(ar.base)); | |
| 117 | 118 | Out.int(count(list)); Out.str(<span class="string">" items from a big arena\n"</span>); |
| 118 | 119 | Arena.reset(ar); <span class="comment">(* every allocation forgotten *)</span> |
| 119 | - list := make(<span class="number">10</span>, <span class="ty">ADR</span>(ar.base)); | |
| 120 | + list := make(<span class="number">10</span>, SYSTEM.<span class="ty">ADR</span>(ar.base)); | |
| 120 | 121 | Out.int(count(list)); Out.str(<span class="string">" after a reset\n"</span>); |
| 121 | 122 | Arena.done(ar); |
| 122 | 123 | |
| 123 | 124 | <span class="kw">if</span> <span class="kw">not</span> Arena.init(small, <span class="number">4096</span>) <span class="kw">then</span> <span class="kw">return</span>; <span class="kw">end</span>; |
| 124 | - list := make(<span class="number">1000</span>, <span class="ty">ADR</span>(small.base)); | |
| 125 | + list := make(<span class="number">1000</span>, SYSTEM.<span class="ty">ADR</span>(small.base)); | |
| 125 | 126 | <span class="kw">if</span> list = <span class="ty">nil</span> <span class="kw">then</span> Out.str(<span class="string">"a small arena ran out, and said so\n"</span>); <span class="kw">end</span>; |
| 126 | 127 | Arena.done(small); |
| 127 | 128 | <span class="kw">end</span>; |
| @@ -136,6 +137,7 @@ | ||
| 136 | 137 | <p><code>Heap.heap</code> serves any size and takes memory back with <code>Mem.free</code>. Freeing a block twice, or one that was never given out, stops the program with a message instead of corrupting the heap. It is safe to call from several threads.</p> |
| 137 | 138 | <div class="file"><span>Free.raz</span></div><pre class="code"><code><span class="kw">module</span> Free; |
| 138 | 139 | <span class="kw">import</span> Out, Mem, Heap; |
| 140 | +<span class="kw">import</span> SYSTEM; | |
| 139 | 141 | |
| 140 | 142 | <span class="kw">type</span> Cell = <span class="kw">record</span> val: <span class="ty">INT</span>; <span class="kw">end</span>; |
| 141 | 143 |
| @@ -45,7 +45,7 @@ | ||
| 45 | 45 | |
| 46 | 46 | <span class="kw">var</span> count*: <span class="ty">INT</span>; <span class="comment">(* the greetings so far *)</span> |
| 47 | 47 | |
| 48 | -<span class="kw">proc</span> hello*(name: []<span class="ty">U8</span>); | |
| 48 | +<span class="kw">proc</span> hello*(name: []<span class="kw">const</span> <span class="ty">U8</span>); | |
| 49 | 49 | <span class="kw">begin</span> |
| 50 | 50 | Out.str(<span class="string">"hello, "</span>); |
| 51 | 51 | Out.str(name); |
| @@ -213,7 +213,7 @@ | ||
| 213 | 213 | <span class="kw">end</span>; |
| 214 | 214 | |
| 215 | 215 | <span class="kw">proc</span> main*; |
| 216 | -<span class="kw">var</span> ops: [<span class="number">3</span>]Op; names: [<span class="number">3</span>][]<span class="ty">U8</span>; i: <span class="ty">INT</span>; | |
| 216 | +<span class="kw">var</span> ops: [<span class="number">3</span>]Op; names: [<span class="number">3</span>][]<span class="kw">const</span> <span class="ty">U8</span>; i: <span class="ty">INT</span>; | |
| 217 | 217 | <span class="kw">begin</span> |
| 218 | 218 | ops := [add, sub, mul]; |
| 219 | 219 | names := [<span class="string">"add"</span>, <span class="string">"sub"</span>, <span class="string">"mul"</span>]; |
| @@ -74,7 +74,7 @@ | ||
| 74 | 74 | <pre class="output"><code>6 |
| 75 | 75 | 40 10 |
| 76 | 76 | </code></pre> |
| 77 | -<p>A record is laid out as C lays out a struct. <code>{lo: ..., hi: ...}</code> builds one, naming its fields; the ones it leaves out are zero. <code>^Box</code> is a pointer to a <code>Box</code>, <code>ADR(b)</code> the address of <code>b</code>, and <code>b^</code> what a pointer points at. A pointer is never the record: it is <code>b^.lo</code>, never <code>b.lo</code>, so you always see where the memory is reached through an address.</p> | |
| 77 | +<p>A record is laid out as C lays out a struct. <code>{lo: ..., hi: ...}</code> builds one, naming its fields; the ones it leaves out are zero. <code>^Box</code> is a pointer to a <code>Box</code>, <code>SYSTEM.ADR(b)</code> the address of <code>b</code>, and <code>b^</code> what a pointer points at. A pointer is never the record: it is <code>b^.lo</code>, never <code>b.lo</code>, so you always see where the memory is reached through an address.</p> | |
| 78 | 78 | <h2>Copies and literals</h2> |
| 79 | 79 | <p>Assigning a record copies it whole, as with an array. A record is compared field by field by writing the comparison, not by <code>=</code>.</p> |
| 80 | 80 | <div class="file"><span>Points.raz</span></div><pre class="code"><code><span class="kw">module</span> Points; |
| @@ -112,7 +112,7 @@ | ||
| 112 | 112 | <div class="file"><span>Planets.raz</span></div><pre class="code"><code><span class="kw">module</span> Planets; |
| 113 | 113 | <span class="kw">import</span> Fmt; |
| 114 | 114 | |
| 115 | -<span class="kw">type</span> Planet = <span class="kw">record</span> name: []<span class="ty">U8</span>; moons: <span class="ty">INT</span>; water: <span class="ty">BOOL</span>; <span class="kw">end</span>; | |
| 115 | +<span class="kw">type</span> Planet = <span class="kw">record</span> name: []<span class="kw">const</span> <span class="ty">U8</span>; moons: <span class="ty">INT</span>; water: <span class="ty">BOOL</span>; <span class="kw">end</span>; | |
| 116 | 116 | |
| 117 | 117 | <span class="kw">const</span> Inner: []Planet = [ |
| 118 | 118 | {name: <span class="string">"Mercury"</span>}, |
| @@ -159,7 +159,7 @@ | ||
| 159 | 159 | INT: 8 |
| 160 | 160 | [5]I16: 10 |
| 161 | 161 | </code></pre> |
| 162 | -<p>A field of a packed record may sit at any address, and is read and written like any other; but it has no address of its own to take with <code>ADR</code>.</p> | |
| 162 | +<p>A field of a packed record may sit at any address, and is read and written like any other; but it has no address of its own to take with <code>SYSTEM.ADR</code>.</p> | |
| 163 | 163 | <h2>Bytes as numbers and records: views</h2> |
| 164 | 164 | <p>A file, a socket or a device hands a program bytes. A view reads and writes them as another type: <code>msg[3].(U32)</code> is the <code>U32</code> whose four bytes start at <code>msg[3]</code>, and <code>msg[0].(Head)</code> the <code>Head</code> whose bytes start at <code>msg[0]</code>.</p> |
| 165 | 165 | <div class="file"><span>Wire.raz</span></div><pre class="code"><code><span class="kw">module</span> Wire; |
| @@ -219,11 +219,12 @@ | ||
| 219 | 219 | </span></code></pre> |
| 220 | 220 | <p>The type of a view is an integer, a real or a pointer, or a record or array made of them: types any bytes are a value of, which a <code>BOOL</code> or an enumeration isn't. A view may start at any byte, so, as a packed record's field, it has no address of its own.</p> |
| 221 | 221 | <h2>Pointers</h2> |
| 222 | -<p><code>ADR(x)</code> takes the address of a variable, <code>^T</code> is a pointer to a <code>T</code>, and <code>p^</code> is what it points at. A pointer that points nowhere is <code>nil</code>, and following it stops the program with a message that names the place, so a forgotten check is found at once instead of corrupting memory.</p> | |
| 222 | +<p><code>SYSTEM.ADR(x)</code> (a module that uses it says <code>import SYSTEM;</code>) takes the address of a variable, <code>^T</code> is a pointer to a <code>T</code>, and <code>p^</code> is what it points at. A pointer that points nowhere is <code>nil</code>, and following it stops the program with a message that names the place, so a forgotten check is found at once instead of corrupting memory.</p> | |
| 223 | 223 | <div class="file"><span>Pointers.raz</span></div><pre class="code"><code><span class="kw">module</span> Pointers; |
| 224 | 224 | <span class="kw">import</span> Out; |
| 225 | +<span class="kw">import</span> SYSTEM; | |
| 225 | 226 | |
| 226 | -<span class="kw">type</span> Counter = <span class="kw">record</span> hits: <span class="ty">INT</span>; name: []<span class="ty">U8</span>; <span class="kw">end</span>; | |
| 227 | +<span class="kw">type</span> Counter = <span class="kw">record</span> hits: <span class="ty">INT</span>; name: []<span class="kw">const</span> <span class="ty">U8</span>; <span class="kw">end</span>; | |
| 227 | 228 | |
| 228 | 229 | <span class="kw">proc</span> hit(c: ^Counter); |
| 229 | 230 | <span class="kw">begin</span> |
| @@ -243,12 +244,12 @@ | ||
| 243 | 244 | <span class="kw">var</span> c: Counter; x, y: <span class="ty">INT</span>; p: ^<span class="ty">INT</span>; |
| 244 | 245 | <span class="kw">begin</span> |
| 245 | 246 | c.name := <span class="string">"front door"</span>; |
| 246 | - hit(<span class="ty">ADR</span>(c)); hit(<span class="ty">ADR</span>(c)); hit(<span class="ty">nil</span>); | |
| 247 | + hit(SYSTEM.<span class="ty">ADR</span>(c)); hit(SYSTEM.<span class="ty">ADR</span>(c)); hit(<span class="ty">nil</span>); | |
| 247 | 248 | Out.str(c.name); Out.str(<span class="string">": "</span>); Out.int(c.hits); Out.ln; |
| 248 | 249 | x := <span class="number">1</span>; y := <span class="number">2</span>; |
| 249 | - swap(<span class="ty">ADR</span>(x), <span class="ty">ADR</span>(y)); | |
| 250 | + swap(SYSTEM.<span class="ty">ADR</span>(x), SYSTEM.<span class="ty">ADR</span>(y)); | |
| 250 | 251 | Out.int(x); Out.char(<span class="string">' '</span>); Out.int(y); Out.ln; |
| 251 | - p := <span class="ty">ADR</span>(x); | |
| 252 | + p := SYSTEM.<span class="ty">ADR</span>(x); | |
| 252 | 253 | p^ := p^ + <span class="number">40</span>; |
| 253 | 254 | Out.int(x); Out.ln; |
| 254 | 255 | <span class="kw">end</span>; |
| @@ -262,6 +263,7 @@ | ||
| 262 | 263 | <p>A record may hold a pointer to its own type, which is how lists and trees are made. Memory for them comes from an allocator, which is the next lesson; here the nodes simply live in an array.</p> |
| 263 | 264 | <div class="file"><span>Chain.raz</span></div><pre class="code"><code><span class="kw">module</span> Chain; |
| 264 | 265 | <span class="kw">import</span> Out; |
| 266 | +<span class="kw">import</span> SYSTEM; | |
| 265 | 267 | |
| 266 | 268 | <span class="kw">type</span> Node = <span class="kw">record</span> next: ^Node; val: <span class="ty">INT</span>; <span class="kw">end</span>; |
| 267 | 269 | |
| @@ -272,7 +274,7 @@ | ||
| 272 | 274 | <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="number">3</span> <span class="kw">do</span> |
| 273 | 275 | nodes[i].val := (i + <span class="number">1</span>) * <span class="number">10</span>; |
| 274 | 276 | nodes[i].next := head; <span class="comment">(* each new one in front *)</span> |
| 275 | - head := <span class="ty">ADR</span>(nodes[i]); | |
| 277 | + head := SYSTEM.<span class="ty">ADR</span>(nodes[i]); | |
| 276 | 278 | <span class="kw">end</span>; |
| 277 | 279 | n := head; |
| 278 | 280 | <span class="kw">while</span> n # <span class="ty">nil</span> <span class="kw">do</span> |
| @@ -286,15 +288,16 @@ | ||
| 286 | 288 | <pre class="output"><code>40 30 20 10 |
| 287 | 289 | </code></pre> |
| 288 | 290 | <h2>A slice's pointer</h2> |
| 289 | -<p>A slice is a pointer and a length, and a record can keep the two apart: an interpreter keeps a string so, and C takes one so. <code>LEN(s)</code> is the length. The pointer looks like <code>ADR(s[0])</code>, the address of the first element, and that works until the slice is empty: then there is no first element, <code>s[0]</code> is out of range, and the program stops.</p> | |
| 291 | +<p>A slice is a pointer and a length, and a record can keep the two apart: an interpreter keeps a string so, and C takes one so. <code>LEN(s)</code> is the length. The pointer looks like <code>SYSTEM.ADR(s[0])</code>, the address of the first element, and that works until the slice is empty: then there is no first element, <code>s[0]</code> is out of range, and the program stops.</p> | |
| 290 | 292 | <div class="file"><span>Empty.raz</span></div><pre class="code"><code><span class="kw">module</span> Empty; |
| 291 | 293 | <span class="kw">import</span> Out; |
| 294 | +<span class="kw">import</span> SYSTEM; | |
| 292 | 295 | |
| 293 | 296 | <span class="kw">type</span> Span = <span class="kw">record</span> p: ^<span class="ty">U8</span>; n: <span class="ty">INT</span>; <span class="kw">end</span>; |
| 294 | 297 | |
| 295 | -<span class="kw">proc</span> spanOf(s: []<span class="ty">U8</span>): Span; | |
| 298 | +<span class="kw">proc</span> spanOf(s: []<span class="kw">const</span> <span class="ty">U8</span>): Span; | |
| 296 | 299 | <span class="kw">begin</span> |
| 297 | - <span class="kw">return</span> {p: <span class="ty">ADR</span>(s[<span class="number">0</span>]), n: <span class="ty">LEN</span>(s)}; | |
| 300 | + <span class="kw">return</span> {p: SYSTEM.<span class="ty">ADR</span>(s[<span class="number">0</span>]), n: <span class="ty">LEN</span>(s)}; | |
| 298 | 301 | <span class="kw">end</span>; |
| 299 | 302 | |
| 300 | 303 | <span class="kw">proc</span> main*; |
| @@ -307,18 +310,19 @@ | ||
| 307 | 310 | <span class="kw">end</span>; |
| 308 | 311 | |
| 309 | 312 | <span class="kw">end</span> Empty.</code></pre> |
| 310 | -<pre class="output"><code>3 | |
| 311 | -<span class="err">Empty.raz:8:22: index 0 is out of range for a length of 0 | |
| 312 | - in Empty.spanOf at Empty.raz:8:22 | |
| 313 | - in Empty.main at Empty.raz:16:5 | |
| 313 | +<pre class="output"><code><span class="err">Empty.raz:9:27: error: s[0] is read through a []const slice, and can't be changed | |
| 314 | + 9 | return {p: SYSTEM.ADR(s[0]), n: LEN(s)}; | |
| 315 | + | ^^^^ | |
| 316 | +help: a []const slice's elements are only read: write them through a []T | |
| 317 | +raz: 1 error | |
| 314 | 318 | </span></code></pre> |
| 315 | -<p><code>PTR(s)</code> is the pointer itself, a <code>^U8</code> for a <code>[]U8</code>, with no check. For a slice that isn't empty it is <code>ADR(s[0])</code>; for an empty one it is what the slice holds, <code>nil</code> for one never set. So nothing needs testing first, and <code>sp.p[0..sp.n]</code> makes the slice again, empty when <code>sp.n</code> is 0, whatever <code>sp.p</code> is:</p> | |
| 319 | +<p><code>SYSTEM.PTR(s)</code> is the pointer itself, a <code>^U8</code> for a <code>[]U8</code>, with no check. For a slice that isn't empty it is <code>SYSTEM.ADR(s[0])</code>; for an empty one it is what the slice holds, <code>nil</code> for one never set. So nothing needs testing first, and <code>sp.p[0..sp.n]</code> makes the slice again, empty when <code>sp.n</code> is 0, whatever <code>sp.p</code> is:</p> | |
| 316 | 320 | <div class="file"><span>Spans.raz</span></div><pre class="code"><code><span class="kw">module</span> Spans; |
| 317 | 321 | <span class="kw">import</span> Out, SYSTEM; |
| 318 | 322 | |
| 319 | 323 | <span class="kw">type</span> Span = <span class="kw">record</span> p: ^<span class="ty">U8</span>; n: <span class="ty">INT</span>; <span class="kw">end</span>; |
| 320 | 324 | |
| 321 | -<span class="kw">proc</span> spanOf(s: []<span class="ty">U8</span>): Span; | |
| 325 | +<span class="kw">proc</span> spanOf(s: []<span class="kw">const</span> <span class="ty">U8</span>): Span; | |
| 322 | 326 | <span class="kw">begin</span> |
| 323 | 327 | <span class="kw">return</span> {p: SYSTEM.PTR(s), n: <span class="ty">LEN</span>(s)}; |
| 324 | 328 | <span class="kw">end</span>; |
| @@ -343,7 +347,7 @@ | ||
| 343 | 347 | 0 |
| 344 | 348 | 5 three |
| 345 | 349 | </code></pre> |
| 346 | -<p>Of an array, <code>PTR(a)</code> is the address of its first element, a <code>^T</code> where <code>ADR(a)</code> is a <code>^[n]T</code>. Reading through the pointer is as unchecked as through any pointer: the length is yours to keep with it. The compiler says so when the two are mixed up: a slice given where a pointer is wanted is an error whose help says to write <code>PTR(s)</code> and <code>LEN(s)</code>, and a pointer given where a slice is, <code>p[0..n]</code>.</p><p class="tryit">Save a program under its module’s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="slices.html">← Arrays, slices and strings</a><a href="index.html">Contents</a><a class="next" href="variants.html">Enumerations, sets, variants and match →</a></div></article></div> | |
| 350 | +<p>Of an array, <code>SYSTEM.PTR(a)</code> is the address of its first element, a <code>^T</code> where <code>SYSTEM.ADR(a)</code> is a <code>^[n]T</code>. Reading through the pointer is as unchecked as through any pointer: the length is yours to keep with it. The compiler says so when the two are mixed up: a slice given where a pointer is wanted is an error whose help says to write <code>PTR(s)</code> and <code>LEN(s)</code>, and a pointer given where a slice is, <code>p[0..n]</code>.</p><p class="tryit">Save a program under its module’s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="slices.html">← Arrays, slices and strings</a><a href="index.html">Contents</a><a class="next" href="variants.html">Enumerations, sets, variants and match →</a></div></article></div> | |
| 347 | 351 | </main> |
| 348 | 352 | <footer>raz is under the ISC license. <a href="../source/index.html">Source</a></footer> |
| 349 | 353 | </body> |
| @@ -51,7 +51,7 @@ | ||
| 51 | 51 | <span class="kw">end</span>; |
| 52 | 52 | |
| 53 | 53 | <span class="kw">proc</span> main*; |
| 54 | -<span class="kw">var</span> a: [<span class="number">5</span>]<span class="ty">INT</span>; i: <span class="ty">INT</span>; s, val: []<span class="ty">U8</span>; name: [<span class="number">8</span>]<span class="ty">U8</span>; ok: <span class="ty">BOOL</span>; | |
| 54 | +<span class="kw">var</span> a: [<span class="number">5</span>]<span class="ty">INT</span>; i: <span class="ty">INT</span>; s, val: []<span class="kw">const</span> <span class="ty">U8</span>; name: [<span class="number">8</span>]<span class="ty">U8</span>; ok: <span class="ty">BOOL</span>; | |
| 55 | 55 | <span class="kw">begin</span> |
| 56 | 56 | <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="number">4</span> <span class="kw">do</span> a[i] := i * i; <span class="kw">end</span>; |
| 57 | 57 | Out.int(sum(a)); Out.ln; <span class="comment">(* the array, as a slice *)</span> |
| @@ -162,12 +162,12 @@ | ||
| 162 | 162 | <pre class="output"><code>1 1 2 3 4 5 |
| 163 | 163 | </code></pre> |
| 164 | 164 | <h2>Strings</h2> |
| 165 | -<p>A string is a slice of bytes, <code>[]U8</code>, in UTF-8, and a string constant's bytes are constant. A <code>[8]U8</code> made from one is a copy you can change. Arrays and slices of integers compare with <code>=</code> and <code>#</code>, so strings do too. <code>Str</code> has the rest: ordering, finding, hashing, prefixes and suffixes, trimming, taking apart, ASCII case and kinds of character, UTF-8, and numbers to and from text.</p> | |
| 165 | +<p>A string is a slice of bytes, <code>[]U8</code>, in UTF-8, and a string constant is a <code>[]const U8</code>: a procedure that only reads a string takes that, and one that writes takes <code>[]U8</code>. A <code>[8]U8</code> made from one is a copy you can change. Arrays and slices of integers compare with <code>=</code> and <code>#</code>, so strings do too. <code>Str</code> has the rest: ordering, finding, hashing, prefixes and suffixes, trimming, taking apart, ASCII case and kinds of character, UTF-8, and numbers to and from text.</p> | |
| 166 | 166 | <div class="file"><span>Words.raz</span></div><pre class="code"><code><span class="kw">module</span> Words; |
| 167 | 167 | <span class="kw">import</span> Out, Str; |
| 168 | 168 | |
| 169 | 169 | <span class="kw">proc</span> main*; |
| 170 | -<span class="kw">var</span> s: []<span class="ty">U8</span>; n: <span class="ty">I64</span>; ok: <span class="ty">BOOL</span>; name: [<span class="number">8</span>]<span class="ty">U8</span>; | |
| 170 | +<span class="kw">var</span> s: []<span class="kw">const</span> <span class="ty">U8</span>; n: <span class="ty">I64</span>; ok: <span class="ty">BOOL</span>; name: [<span class="number">8</span>]<span class="ty">U8</span>; | |
| 171 | 171 | <span class="kw">begin</span> |
| 172 | 172 | s := <span class="string">"hello, world"</span>; |
| 173 | 173 | Out.str(s[<span class="number">7</span>..<span class="ty">LEN</span>(s)]); Out.ln; |
| @@ -221,7 +221,7 @@ | ||
| 221 | 221 | <span class="kw">const</span> Text = <span class="string">"name = raz\nkind = language\nyear = 2026\n"</span>; |
| 222 | 222 | |
| 223 | 223 | <span class="kw">proc</span> main*; |
| 224 | -<span class="kw">var</span> line, rest, key, val, w: []<span class="ty">U8</span>; more, ok: <span class="ty">BOOL</span>; | |
| 224 | +<span class="kw">var</span> line, rest, key, val, w: []<span class="kw">const</span> <span class="ty">U8</span>; more, ok: <span class="ty">BOOL</span>; | |
| 225 | 225 | <span class="kw">begin</span> |
| 226 | 226 | <span class="comment">(* line by line, then each line at its = *)</span> |
| 227 | 227 | line, rest, more := Str.cut(Text, <span class="string">"\n"</span>); |
| @@ -286,7 +286,7 @@ | ||
| 286 | 286 | <span class="kw">import</span> Out, Str, Width; |
| 287 | 287 | |
| 288 | 288 | <span class="kw">proc</span> main*; |
| 289 | -<span class="kw">var</span> s: []<span class="ty">U8</span>; c: <span class="ty">I32</span>; i, size: <span class="ty">INT</span>; bytes: [<span class="number">4</span>]<span class="ty">U8</span>; k: <span class="ty">INT</span>; | |
| 289 | +<span class="kw">var</span> s: []<span class="kw">const</span> <span class="ty">U8</span>; c: <span class="ty">I32</span>; i, size: <span class="ty">INT</span>; bytes: [<span class="number">4</span>]<span class="ty">U8</span>; k: <span class="ty">INT</span>; | |
| 290 | 290 | <span class="kw">begin</span> |
| 291 | 291 | s := <span class="string">"caf\u{E9} \u{3BB}"</span>; |
| 292 | 292 | Out.int(<span class="ty">LEN</span>(s)); Out.str(<span class="string">" bytes, "</span>); |
| @@ -82,6 +82,7 @@ | ||
| 82 | 82 | <p>Two threads that each read a variable, add one and write it back can lose one of the additions. A plain <code>INC</code> on a variable shared by threads has no particular value afterwards; the report says so. <code>XADD</code> is the atomic add: it returns the old value and adds in one indivisible step.</p> |
| 83 | 83 | <div class="file"><span>Race.raz</span></div><pre class="code"><code><span class="kw">module</span> Race; |
| 84 | 84 | <span class="kw">import</span> Out, Thread; |
| 85 | +<span class="kw">import</span> SYSTEM; | |
| 85 | 86 | |
| 86 | 87 | <span class="kw">var</span> |
| 87 | 88 | safe: <span class="ty">INT</span>; <span class="comment">(* added to by XADD *)</span> |
| @@ -91,7 +92,7 @@ | ||
| 91 | 92 | <span class="kw">var</span> i: <span class="ty">INT</span>; |
| 92 | 93 | <span class="kw">begin</span> |
| 93 | 94 | <span class="kw">for</span> i := <span class="number">1</span> <span class="kw">to</span> <span class="number">100000</span> <span class="kw">do</span> |
| 94 | - _ := <span class="ty">XADD</span>(<span class="ty">ADR</span>(safe), <span class="number">1</span>); | |
| 95 | + _ := <span class="ty">XADD</span>(safe, <span class="number">1</span>); | |
| 95 | 96 | <span class="ty">INC</span>(plain); <span class="comment">(* unsafe: lost additions *)</span> |
| 96 | 97 | <span class="kw">end</span>; |
| 97 | 98 | <span class="kw">end</span>; |
| @@ -149,6 +150,7 @@ | ||
| 149 | 150 | <p>A thread's procedure takes one pointer argument. Pass a record's address to give each thread its own job and its own place to put the answer; then no one shares anything.</p> |
| 150 | 151 | <div class="file"><span>Jobs.raz</span></div><pre class="code"><code><span class="kw">module</span> Jobs; |
| 151 | 152 | <span class="kw">import</span> Out, Thread; |
| 153 | +<span class="kw">import</span> SYSTEM; | |
| 152 | 154 | |
| 153 | 155 | <span class="kw">type</span> Job = <span class="kw">record</span> lo, hi: <span class="ty">INT</span>; sum: <span class="ty">INT</span>; <span class="kw">end</span>; |
| 154 | 156 | |
| @@ -165,7 +167,7 @@ | ||
| 165 | 167 | <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="number">3</span> <span class="kw">do</span> |
| 166 | 168 | jobs[i].lo := i * <span class="number">250000</span>; |
| 167 | 169 | jobs[i].hi := (i + <span class="number">1</span>) * <span class="number">250000</span>; |
| 168 | - t[i], err := Thread.spawn(work, ^<span class="ty">U8</span>(<span class="ty">ADR</span>(jobs[i]))); | |
| 170 | + t[i], err := Thread.spawn(work, ^<span class="ty">U8</span>(SYSTEM.<span class="ty">ADR</span>(jobs[i]))); | |
| 169 | 171 | <span class="kw">if</span> err # <span class="ty">nil</span> <span class="kw">then</span> Out.str(<span class="ty">MSG</span>(err)); Out.ln; <span class="kw">return</span>; <span class="kw">end</span>; |
| 170 | 172 | <span class="kw">end</span>; |
| 171 | 173 | <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="number">3</span> <span class="kw">do</span> |
| @@ -70,7 +70,7 @@ | ||
| 70 | 70 | <span class="kw">import</span> Out; |
| 71 | 71 | |
| 72 | 72 | <span class="kw">proc</span> main*; |
| 73 | -<span class="kw">var</span> i: <span class="ty">I32</span>; s: []<span class="ty">U8</span>; | |
| 73 | +<span class="kw">var</span> i: <span class="ty">I32</span>; s: []<span class="kw">const</span> <span class="ty">U8</span>; | |
| 74 | 74 | <span class="kw">begin</span> |
| 75 | 75 | s := <span class="string">"four"</span>; |
| 76 | 76 | i := <span class="number">3</span>; |
| @@ -62,7 +62,7 @@ | ||
| 62 | 62 | <span class="kw">end</span>; |
| 63 | 63 | <span class="kw">end</span>; |
| 64 | 64 | |
| 65 | -<span class="kw">proc</span> kind(c: <span class="ty">I32</span>): []<span class="ty">U8</span>; | |
| 65 | +<span class="kw">proc</span> kind(c: <span class="ty">I32</span>): []<span class="kw">const</span> <span class="ty">U8</span>; | |
| 66 | 66 | <span class="kw">begin</span> |
| 67 | 67 | <span class="kw">match</span> c <span class="kw">of</span> |
| 68 | 68 | | <span class="string">' '</span>, <span class="string">'\t'</span>, <span class="string">'\n'</span>: <span class="kw">return</span> <span class="string">"space"</span>; |
| @@ -72,7 +72,7 @@ | ||
| 72 | 72 | <span class="kw">end</span>; |
| 73 | 73 | <span class="kw">end</span>; |
| 74 | 74 | |
| 75 | -<span class="kw">proc</span> colour(name: []<span class="ty">U8</span>): Colour; | |
| 75 | +<span class="kw">proc</span> colour(name: []<span class="kw">const</span> <span class="ty">U8</span>): Colour; | |
| 76 | 76 | <span class="kw">begin</span> |
| 77 | 77 | <span class="kw">match</span> name <span class="kw">of</span> |
| 78 | 78 | | <span class="string">"red"</span>, <span class="string">"rose"</span>: <span class="kw">return</span> red; |
| @@ -112,7 +112,7 @@ | ||
| 112 | 112 | |
| 113 | 113 | <span class="kw">type</span> Day = (mon, tue, wed, thu, fri, sat, sun); |
| 114 | 114 | |
| 115 | -<span class="kw">proc</span> name(d: Day): []<span class="ty">U8</span>; | |
| 115 | +<span class="kw">proc</span> name(d: Day): []<span class="kw">const</span> <span class="ty">U8</span>; | |
| 116 | 116 | <span class="kw">begin</span> |
| 117 | 117 | <span class="kw">match</span> d <span class="kw">of</span> |
| 118 | 118 | | mon: <span class="kw">return</span> <span class="string">"Monday"</span>; |
| @@ -193,7 +193,7 @@ | ||
| 193 | 193 | number(n: <span class="ty">INT</span>); |
| 194 | 194 | plus; |
| 195 | 195 | times; |
| 196 | - word(s: []<span class="ty">U8</span>); | |
| 196 | + word(s: []<span class="kw">const</span> <span class="ty">U8</span>); | |
| 197 | 197 | <span class="kw">end</span>; |
| 198 | 198 | |
| 199 | 199 | <span class="kw">proc</span> describe(t: Token); |
| @@ -246,7 +246,7 @@ | ||
| 246 | 246 | <div class="file"><span>Classify.raz</span></div><pre class="code"><code><span class="kw">module</span> Classify; |
| 247 | 247 | <span class="kw">import</span> Out; |
| 248 | 248 | |
| 249 | -<span class="kw">proc</span> kind(c: <span class="ty">I32</span>): []<span class="ty">U8</span>; | |
| 249 | +<span class="kw">proc</span> kind(c: <span class="ty">I32</span>): []<span class="kw">const</span> <span class="ty">U8</span>; | |
| 250 | 250 | <span class="kw">begin</span> |
| 251 | 251 | <span class="kw">match</span> c <span class="kw">of</span> |
| 252 | 252 | | <span class="string">' '</span>, <span class="string">'\t'</span>, <span class="string">'\n'</span>: <span class="kw">return</span> <span class="string">"space"</span>; |
| @@ -257,7 +257,7 @@ | ||
| 257 | 257 | <span class="kw">end</span>; |
| 258 | 258 | |
| 259 | 259 | <span class="kw">proc</span> main*; |
| 260 | -<span class="kw">var</span> s: []<span class="ty">U8</span>; i: <span class="ty">INT</span>; | |
| 260 | +<span class="kw">var</span> s: []<span class="kw">const</span> <span class="ty">U8</span>; i: <span class="ty">INT</span>; | |
| 261 | 261 | <span class="kw">begin</span> |
| 262 | 262 | s := <span class="string">"x1 + _y"</span>; |
| 263 | 263 | <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="ty">LEN</span>(s) - <span class="number">1</span> <span class="kw">do</span> |
| @@ -281,7 +281,7 @@ | ||
| 281 | 281 | |
| 282 | 282 | <span class="kw">type</span> Op = (none, add, sub, show); |
| 283 | 283 | |
| 284 | -<span class="kw">proc</span> op(word: []<span class="ty">U8</span>): Op; | |
| 284 | +<span class="kw">proc</span> op(word: []<span class="kw">const</span> <span class="ty">U8</span>): Op; | |
| 285 | 285 | <span class="kw">begin</span> |
| 286 | 286 | <span class="kw">match</span> word <span class="kw">of</span> |
| 287 | 287 | | <span class="string">"add"</span>, <span class="string">"plus"</span>, <span class="string">"+"</span>: <span class="kw">return</span> add; |
| @@ -292,7 +292,7 @@ | ||
| 292 | 292 | <span class="kw">end</span>; |
| 293 | 293 | |
| 294 | 294 | <span class="kw">proc</span> main*; |
| 295 | -<span class="kw">var</span> total: <span class="ty">INT</span>; words: [<span class="number">6</span>][]<span class="ty">U8</span>; i: <span class="ty">INT</span>; | |
| 295 | +<span class="kw">var</span> total: <span class="ty">INT</span>; words: [<span class="number">6</span>][]<span class="kw">const</span> <span class="ty">U8</span>; i: <span class="ty">INT</span>; | |
| 296 | 296 | <span class="kw">begin</span> |
| 297 | 297 | words := [<span class="string">"add"</span>, <span class="string">"plus"</span>, <span class="string">"sub"</span>, <span class="string">"show"</span>, <span class="string">"times"</span>, <span class="string">"-"</span>]; |
| 298 | 298 | <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="number">5</span> <span class="kw">do</span> |