raz / commit 311d3f66

commit
311d3f661b13051426201c6829395bbeac37cbdc
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
9fc09480
site: v0.22.0 and v0.23.0

patch · log

19 files changed, +424 -161

site/public/_redirects (history) modified +1 -1

@@ -1 +1 @@
1-/download/latest /download/raz-0.21.0-linux-x86_64.tar.gz 302
1+/download/latest /download/raz-0.23.0-linux-x86_64.tar.gz 302

site/public/advanced/linkstatic.html (history) modified +2 -2

@@ -64,9 +64,9 @@
6464 stores C&#x27;s exit in Rt.cExit; so this module uses no C itself, and
6565 serves both kinds of program. C&#x27;s thread has a stack and a guard
6666 page of C&#x27;s; the mapping here holds only the handlers&#x27; stack and
67- the Thread. *)
67+ the Thread. Its test uses Check. *)
6868 module Thread;
69-import Sys, Rt;
69+import Sys, Rt, Check;
7070
7171 const
7272 Failed*: ERROR = &quot;can&#x27;t start a thread&quot;;

site/public/advanced/listing.html (history) modified +18 -31

@@ -61,32 +61,19 @@
6161 <span class="kw">end</span> Add.</code></pre>
6262 <pre class="output term"><code><span class="cmd">$ raz -S Add.raz</span>
6363
64-Add.add:
65- 3 proc add*(a, b: INT): INT;
66- 0 push rbp
67- 1 mov rbp, rsp
68- 4 sub rsp, 16
69- 5 return a + b;
70- 11 mov r8, rdi
71- 14 add r8, rsi
72- 17 mov rax, r8
73- 6 end;
74- 20 leave
75- 21 ret
76-
7764 Add.main:
7865 8 proc main*;
7966 0 push rbp
8067 1 mov rbp, rsp
8168 4 sub rsp, 16
8269 10 _ := add(1, 2);
83- 11 mov rdi, 1
84- 16 mov rsi, 2
85- 21 call Add.add
86- 26 mov r8, rax
70+ 11 mov qword [rbp - 8], 2
71+ 19 mov qword [rbp - 16], 1
72+ 27 mov r8, [rbp - 16]
73+ 31 add r8, [rbp - 8]
8774 11 end;
88- 29 leave
89- 30 ret</code></pre>
75+ 35 leave
76+ 36 ret</code></pre>
9077 <p>Every procedure starts by saving the caller's frame pointer and making its own (<code>push rbp; mov rbp, rsp</code>) and reserves room for what it needs. The arguments arrive in <code>rdi</code> and <code>rsi</code>, as the convention says, and the result leaves in <code>rax</code>. A call is <code>mov rdi, 1; mov rsi, 2; call Add.add</code>. Nothing is hidden: the frame pointer chain is what lets a failure show the calls under way with their variables.</p>
9178 <h2>A loop with its checks</h2>
9279 <p>This is the dot product of two slices. A slice is a pointer and a length, and it travels in two registers:</p>
@@ -258,18 +245,18 @@
258245 <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>
259246 <pre class="output term"><code><span class="cmd">$ raz -o Dot Dot.raz</span>
260247 <span class="cmd">$ objdump -d -M intel --no-show-raw-insn Dot | sed -n &#x27;/&lt;Dot.dot&gt;:/,/ret/p&#x27; | head -12</span>
261-0000000000403b20 &lt;Dot.dot&gt;:
262- 403b20: push rbp
263- 403b21: mov rbp,rsp
264- 403b24: sub rsp,0x40
265- 403b2b: mov QWORD PTR [rbp-0x38],rbx
266- 403b2f: xor ebx,ebx
267- 403b31: mov QWORD PTR [rbp-0x30],r12
268- 403b35: xor r12d,r12d
269- 403b38: mov QWORD PTR [rbp-0x28],r13
270- 403b3c: xor r13d,r13d
271- 403b3f: mov QWORD PTR [rbp-0x20],rdx
272- 403b43: mov QWORD PTR [rbp-0x18],rcx</code></pre>
248+0000000000403c60 &lt;Dot.dot&gt;:
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>
273260 <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&rsquo;s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="syscalls.html">&larr; System calls</a><a href="index.html">Contents</a><a class="next" href="volatile.html">Volatile &rarr;</a></div></article></div>
274261 </main>
275262 <footer>raz is under the ISC license. <a href="../source/index.html">Source</a></footer>

site/public/advanced/static.html (history) modified +5 -5

@@ -65,7 +65,7 @@
6565 <span class="cmd">$ ldd Bare</span>
6666 not a dynamic executable
6767 <span class="cmd">$ ls -l Bare | awk &#x27;{print $5, &quot;bytes&quot;}&#x27;</span>
68-26336 bytes</code></pre>
68+26488 bytes</code></pre>
6969 <p>It is statically linked: no interpreter, no shared libraries, no <code>libc</code>. <code>ldd</code> says so.</p>
7070 <h2>What it does when it runs</h2>
7171 <p><code>strace</code> lists every system call a program makes. This is the whole of it:</p>
@@ -134,15 +134,15 @@
134134 <pre class="output term"><code><span class="cmd">$ raz Lean.raz</span>
135135 <span class="cmd">$ raz Heavy.raz</span>
136136 <span class="cmd">$ ls -l Lean Heavy | awk &#x27;{print $9, $5}&#x27;</span>
137-Heavy 28488
138-Lean 28480</code></pre>
137+Heavy 28680
138+Lean 28672</code></pre>
139139 <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>
140140 <h2>Stripping</h2>
141141 <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>
142142 <pre class="output term"><code><span class="cmd">$ raz -s -o Lean-stripped Lean.raz</span>
143143 <span class="cmd">$ ls -l Lean Lean-stripped | awk &#x27;{print $9, $5}&#x27;</span>
144-Lean 28480
145-Lean-stripped 6856
144+Lean 28672
145+Lean-stripped 6920
146146 <span class="cmd">$ ./Lean-stripped</span>
147147 same program</code></pre>
148148 <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&rsquo;s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="allocators.html">&larr; Writing an allocator</a><a href="index.html">Contents</a><a class="next" href="linkstatic.html">Threads without C, and keeping the two apart &rarr;</a></div></article></div>

site/public/advanced/targets.html (history) modified +1 -1

@@ -52,7 +52,7 @@
5252 Sys.raz
5353 Thread.linux-x86_64.raz
5454 <span class="cmd">$ cd $RAZLIB &amp;&amp; grep -c SYSCALL Sys.linux-x86_64.raz Sys.raz</span>
55-Sys.linux-x86_64.raz:129
55+Sys.linux-x86_64.raz:151
5656 Sys.raz:0</code></pre>
5757 <h2>A module of your own</h2>
5858 <p>Here is a module with two files. <code>Clock.linux-x86_64.raz</code> is for this target; <code>Clock.raz</code> is what any other target would use. Both say what they are in <code>Name</code>, and both make <code>ticks</code>.</p>

site/public/download/raz-0.22.0-linux-x86_64.tar.gz (history) added

Binary file; the patch has it.

site/public/download/raz-0.22.0-linux-x86_64.tar.gz.sha256 (history) added +1 -0

@@ -0,0 +1 @@
1+451bca750e9222a08aaabe514a6db78e1f076ff12068a9a88d64ef6443fe1774 raz-0.22.0-linux-x86_64.tar.gz

site/public/download/raz-0.23.0-linux-x86_64.tar.gz (history) added

Binary file; the patch has it.

site/public/download/raz-0.23.0-linux-x86_64.tar.gz.sha256 (history) added +1 -0

@@ -0,0 +1 @@
1+103eb421a7fcca1c1495c1a9c699297d460ab72067fa04854da8f21edc07347c raz-0.23.0-linux-x86_64.tar.gz

site/public/index.html (history) modified +10 -10

@@ -43,7 +43,7 @@
4343 <div class="facts">
4444 <span><b>~10,700</b> lines of compiler</span>
4545 <span><b>~500K</b> lines compiled a second</span>
46- <span><b>1</b> report, <b>32</b> pages</span>
46+ <span><b>1</b> report, <b>35</b> pages</span>
4747 <span><b>static</b> executables, no run time to install</span>
4848 </div>
4949
@@ -110,28 +110,28 @@
110110 <p>The language is defined by <a href="spec.html">the report</a>, which is
111111 short on purpose: whatever it doesn&rsquo;t allow, raz doesn&rsquo;t have.
112112 Its second volume describes the library. Both are on one page, and each is
113-a PDF: <a href="raz.pdf">the language</a> (32 pages) and
113+a PDF: <a href="raz.pdf">the language</a> (35 pages) and
114114 <a href="raz-lib.pdf">the library</a>.</p>
115115
116116 <h2 id="download">Download</h2>
117-<p>raz 0.21.0 is a tarball for x86-64 Linux with the compiler, <code>raz</code>,
117+<p>raz 0.23.0 is a tarball for x86-64 Linux with the compiler, <code>raz</code>,
118118 its library, <code>lib/</code>, which it finds beside itself, the formatter
119119 and the documentation tool as sources in <code>tools/</code>, and the manual
120120 pages. It needs nothing else installed.</p>
121121 <div class="cta">
122- <a class="btn primary" href="download/raz-0.21.0-linux-x86_64.tar.gz">Download raz 0.21.0</a>
123- <a class="btn" href="download/raz-0.21.0-linux-x86_64.tar.gz.sha256">SHA-256</a>
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>
124124 </div>
125-<pre class="code"><code>$ tar xzf raz-0.21.0-linux-x86_64.tar.gz
126-$ ln -s $PWD/raz-0.21.0-linux-x86_64/raz ~/.local/bin/raz
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
127127 $ raz run Hello.raz</code></pre>
128128 <p>The link works since raz finds <code>lib/</code> beside its real path. The
129129 manual pages are in <code>man/</code>: <code>man ./man/raz-tutorial.7</code>.
130130 The address <code>download/latest</code> always leads to the newest release,
131-and <code>download/raz-0.21.0-linux-x86_64.tar.gz</code> to this one, for
132-good. Earlier releases: <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.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
133133 <code>.sha256</code> added to its name.</p>
134-<p class="sha">SHA-256 <code>a8e2e550e927050d36a7e9731e6de22b094189adea15a984cd921917cbd8d90f</code></p>
134+<p class="sha">SHA-256 <code>103eb421a7fcca1c1495c1a9c699297d460ab72067fa04854da8f21edc07347c</code></p>
135135 <p><a href="source/releases/index.html">Release pages</a> give each
136136 version's commit, notes, compiler and source archives, with checksums.
137137 Browse the <a href="source/index.html">source and history</a>, or clone:

site/public/raz-lib.pdf (history) modified

Binary file; the patch has it.

site/public/raz.pdf (history) modified

Binary file; the patch has it.

site/public/spec.html (history) modified +365 -90

@@ -38,7 +38,7 @@
3838 <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>
3939 <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>
4040 <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.34 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.</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>
4242 <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>
4343 <h1 id="a-first-look">1. A first look</h1>
4444 <pre><code>module Hello;
@@ -92,8 +92,17 @@
9292 Link = &quot;link&quot; ( string | &quot;static&quot; ) &quot;;&quot; .
9393 Check = &quot;check&quot; &quot;off&quot; &quot;;&quot; .</code></pre>
9494 <p>A module lives in a file of its own name: module <code>Out</code> is <code>Out.raz</code>. The name after <code>end</code> repeats the module’s name.</p>
95-<p><strong>Exports.</strong> A name declared at the top of a module with <code>*</code> after it is exported: other modules can use it as <code>Module.name</code>. Record fields are exported the same way. Everything else is private to the module. An exported variable can be read by the modules that import it, but only its own module changes it. Outside it, the variable is a value, not a variable: nothing that could write it, or let something else write it, may name it or a field or element of it. So it can’t be assigned, passed as a <code>var</code> argument, changed by <code>INC</code>, <code>DEC</code> or <code>NEW</code>, or given to <code>ADR</code>; an array in it can’t be sliced, or used where a slice is expected (the silent conversion of an array, section 5.10), though a slice in it may be sliced, since that makes a new slice and can’t change the variable; in <code>match</code>, its arms’ names stand for the fields of a copy. It can be read, copied and compared, and a copy is the importer’s own. What a pointer or a slice in it points at is another variable, and isn’t held to this: with <code>s: []U8</code> exported, <code>M.s[0] := 0</code> writes what <code>M.s</code> points at, not <code>M</code>. A module that means to share an array exports a pointer to it, or a procedure that returns a slice of it.</p>
95+<p><strong>Exports.</strong> A name declared at the top of a module with <code>*</code> after it is exported: other modules can use it as <code>Module.name</code>. Record fields are exported the same way. Everything else is private to the module. An exported variable can be read by the modules that import it, but only its own module changes it. Outside it, the variable is a value, not a variable: nothing that could write it, or let something else write it, may name it or a field or element of it. So it can’t be assigned, passed as a <code>var</code> argument, changed by <code>INC</code>, <code>DEC</code> or <code>NEW</code>, or given to <code>SYSTEM.ADR</code>; an array in it is sliced, or used where a slice is expected (the silent conversion of an array, section 5.10), only as a <code>[]const T</code>, whose elements can’t be written (section 5.3); a slice in it may be sliced, since that makes a new slice and can’t change the variable; in <code>match</code>, its arms’ names stand for the fields of a copy. It can be read, copied and compared, and a copy is the importer’s own. What a pointer or a slice in it points at is another variable, and isn’t held to this: with <code>s: []U8</code> exported, <code>M.s[0] := 0</code> writes what <code>M.s</code> points at, not <code>M</code>. A module that means to share an array for reading exports it; for writing, it exports a pointer to it, or a procedure that returns a slice of it.</p>
9696 <p><strong>Imports.</strong> <code>import Out;</code> makes <code>Out</code>’s exports available as <code>Out.x</code>. <code>import O := Out;</code> uses the name <code>O</code> instead. A module can’t import itself, directly or through others.</p>
97+<p><strong><code>import SYSTEM</code>.</strong> <code>SYSTEM</code> is no file but a module the language gives, of the operations that reach memory other than through the program’s variables, or let something outside the program do so. A module that does any of these says <code>import SYSTEM;</code>, and it is an error to do them in one that doesn’t:</p>
98+<ul>
99+<li><code>SYSTEM.ADR</code>, <code>SYSTEM.PTR</code>, <code>SYSTEM.SYSCALL</code> and <code>SYSTEM.ARGV</code> (section 6.8), which are written so, by that name;</li>
100+<li><code>p + i</code>, <code>p - i</code>, <code>p[i]</code> and <code>p[lo..hi]</code> on a pointer (section 6.4);</li>
101+<li>a conversion to a pointer or a procedure type from an integer or from another pointer or procedure type, or from a pointer or a procedure to an integer (section 6.7), and a string literal given where a <code>^U8</code> is wanted (section 5.10);</li>
102+<li>a view whose type holds a pointer, a view written over a variable whose type holds anything but integers and reals, and <code>BYTES</code> of such a variable (sections 6.8 and 6.9);</li>
103+<li><code>extern</code> and <code>link</code> of a library (section 9), code procedures (section 8) and <code>check off;</code> (below).</li>
104+</ul>
105+<p><code>SYSTEM</code> can’t be imported under another name, so that every module that reaches memory so says it by that name. What such a module does is its own to get right: section 10 says what isn’t defined. A module that imports one that says <code>import SYSTEM</code> needn’t say it itself; it trusts that module as it trusts the library, whose <code>Rt</code>, <code>Mem</code>, <code>Heap</code>, <code>Sys</code>, <code>Thread</code> and others say it.</p>
97106 <p><strong>Generic modules</strong> take types as parameters:</p>
98107 <pre><code>module List(T);
99108 type
@@ -111,9 +120,9 @@
111120 <pre><code>module Map(K, V; hash: proc(k: K): U64; eq: proc(a, b: K): BOOL);</code></pre>
112121 <pre><code>import Names := Map([]U8, I64, Str.hash, Str.eq);</code></pre>
113122 <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>
114-<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 <code>lib</code> directory beside the compiler. In each directory it 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>
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>
115124 <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>
116-<p><strong>Names are flat.</strong> The names of a program’s modules are one space, with no packages or paths. So the names of the library’s modules (the library, <code>doc/raz-lib.md</code>) are kept for it: a program or another library doesn’t name a module of its own <code>Arena</code>, <code>Buf</code>, <code>C</code>, <code>Files</code>, <code>Fmt</code>, <code>Heap</code>, <code>List</code>, <code>Lock</code>, <code>Map</code>, <code>Math</code>, <code>Mem</code>, <code>Opt</code>, <code>Out</code>, <code>Rt</code>, <code>Sort</code>, <code>Str</code>, <code>Sys</code> or <code>Thread</code>, nor any name the library takes later. The modules of any other library share a prefix of their own, as an X11 library’s <code>Xauth</code> and <code>Xkeys</code> do, and a program that wants a shorter name imports one: <code>import Auth := Xauth;</code>.</p>
125+<p><strong>Names are flat.</strong> The names of a program’s modules are one space, with no packages or paths. So the names of the library’s modules (the library, <code>doc/raz-lib.md</code>) are kept for it: a program or another library doesn’t name a module of its own <code>Arena</code>, <code>Buf</code>, <code>C</code>, <code>Files</code>, <code>Fmt</code>, <code>Guarded</code>, <code>Heap</code>, <code>List</code>, <code>Lock</code>, <code>Map</code>, <code>Math</code>, <code>Mem</code>, <code>Opt</code>, <code>Out</code>, <code>Rt</code>, <code>Sort</code>, <code>Str</code>, <code>Sys</code>, <code>SYSTEM</code>, <code>Task</code> or <code>Thread</code>, nor any name the library takes later. The modules of any other library share a prefix of their own, as an X11 library’s <code>Xauth</code> and <code>Xkeys</code> do, and a program that wants a shorter name imports one: <code>import Auth := Xauth;</code>.</p>
117126 <p>The compiler holds every file of such a module to the same interface. When it takes one of <code>Name.*.raz</code>, it also reads the declarations of the others beside it, and of <code>Name.raz</code> if there is one, and reports an error unless they all export the same names, each the same kind of thing:</p>
118127 <ul>
119128 <li>a constant of the same type (its value may differ: a system call number, say);</li>
@@ -123,8 +132,8 @@
123132 </ul>
124133 <p>Two types here are the same if they are built alike, a name in one file standing for the same name in another.</p>
125134 <p><strong>Module bodies.</strong> A module may end with <code>begin</code> and statements, its body. Every module’s body runs once, before the main module’s <code>main</code>: a module’s body runs after the bodies of the modules it imports, in the order of its <code>import</code>s, and once however many modules import it. Each instance of a generic module has its body run once. Module variables are zero before any body runs. <code>return;</code> ends a body early.</p>
126-<p><strong><code>link "libX11.so.6";</code></strong> names a shared library to load at run time, for the <code>extern</code> declarations of section 9. <strong><code>link static;</code></strong> says the module needs a program without C (section 10): it makes its own system calls, or its own threads, which the C library wouldn’t know about.</p>
127-<p><strong><code>check off;</code></strong>, after the links, leaves out of the module’s own code the checks of indexes and slice bounds of section 6.5, and that a slice of a pointer doesn’t run backwards (section 6.4), for speed where the program is sure of them. An index out of range there reads or writes whatever memory it reaches, and what follows isn’t defined. Every other check stays, and the module’s callers keep theirs. An instance of a generic module follows the generic module’s own <code>check</code> line, not that of the module that made it.</p>
135+<p><strong><code>link "libX11.so.6";</code></strong> names a shared library to load at run time, for the <code>extern</code> declarations of section 9, in a module that imports <code>SYSTEM</code>. <strong><code>link static;</code></strong> says the module needs a program without C (section 10): it makes its own system calls, or its own threads, which the C library wouldn’t know about.</p>
136+<p><strong><code>check off;</code></strong>, after the links, in a module that imports <code>SYSTEM</code>, leaves out of the module’s own code the checks of indexes and slice bounds of section 6.5, and that a slice of a pointer doesn’t run backwards (section 6.4), for speed where the program is sure of them. An index out of range there reads or writes whatever memory it reaches, and what follows isn’t defined. Every other check stays, and the module’s callers keep theirs. An instance of a generic module follows the generic module’s own <code>check</code> line, not that of the module that made it.</p>
128137 <h1 id="declarations">4. Declarations</h1>
129138 <pre><code>Declaration = &quot;const&quot; { IdentDef [ &quot;:&quot; Type ] &quot;=&quot; Expr &quot;;&quot; }
130139 | &quot;type&quot; { IdentDef &quot;=&quot; Type &quot;;&quot; }
@@ -133,20 +142,21 @@
133142 | ExternDeclaration &quot;;&quot; .
134143 IdentDef = ident [ &quot;*&quot; ] .
135144 IdentList = IdentDef { &quot;,&quot; IdentDef } .</code></pre>
136-<p>A name must be declared before it is used, with two exceptions: the base type of a pointer may name a type declared later in the same <code>type</code> section (so that two records can point at each other), and a procedure may be declared <code>forward</code> and defined later (section 8).</p>
137-<p><strong>Constants</strong> are evaluated by the compiler. Their expressions use only literals, other constants, operators, conversions, <code>SIZE</code>, the <code>LEN</code> of arrays and strings, and a variant’s cases. A constant is an integer, a real, a <code>BOOL</code>, a name of an enumeration, a set, a string, an array, a record, a variant or an <code>ERROR</code>. A constant of an integer type wraps as the program would: <code>U8(250) + 10</code> is 4. Dividing by zero in a constant is an error, and so is dividing the smallest value of a signed type by -1, with <code>div</code> or <code>mod</code>, which stops a program (section 6.1). A string constant can be indexed and sliced as an array of <code>U8</code> is, and its <code>LEN</code> is its length in bytes. <code>+</code> joins two string constants into one:</p>
145+<p>A name must be declared before it is used, with two exceptions: the base type of a pointer may name a type declared later in the same <code>type</code> section (so that two records can point at each other), and so may a parameter or a result of a procedure type there, however passed (so that a record’s callback can take the record, section 5.9); and a procedure may be declared <code>forward</code> and defined later (section 8).</p>
146+<p><strong>Constants</strong> are evaluated by the compiler. Their expressions use only literals, other constants, operators, conversions, <code>SIZE</code>, <code>OFFSET</code>, the <code>LEN</code> of arrays and strings, <code>MIN</code>, <code>MAX</code>, <code>ABS</code>, <code>ROL</code>, <code>ROR</code>, <code>CLZ</code>, <code>CTZ</code>, <code>POPCOUNT</code>, and a variant’s cases. A constant is an integer, a real, a <code>BOOL</code>, a name of an enumeration, a set, a string, an array, a record, a variant or an <code>ERROR</code>. A constant of an integer type wraps as the program would: <code>U8(250) + 10</code> is 4. Dividing by zero in a constant is an error, and so is dividing the smallest value of a signed type by -1, with <code>div</code> or <code>mod</code>, which stops a program (section 6.1). A string constant can be indexed and sliced as an array of <code>U8</code> is, and its <code>LEN</code> is its length in bytes. <code>+</code> joins two string constants into one:</p>
138147 <pre><code>const Text = &quot;the cat&quot;;
139148 Line = Text + &quot;\n&quot;;
140149 ...
141150 w := Text[4..7]; (* &quot;cat&quot; *)</code></pre>
142-<p>A constant with a type is an <code>ERROR</code> (section 5.8), an array, a record or a variant, and the value of an array or record constant is a literal (section 6.5) of constants, that of a variant one a case built from constants, as a program builds one (section 5.6). Its elements and fields are integers, reals, <code>BOOL</code>s, enumerations, sets, <code>ERROR</code>s, procedures, <code>nil</code>, a string for a <code>[]U8</code>, an array constant or the empty slice for a slice of its element type, and arrays, records and variants of these; no other pointer. A variant constant must have a type: <code>const D = dot;</code> is an error, <code>const D: Shape = dot;</code> isn’t.</p>
151+<p>A constant with a type is an <code>ERROR</code> (section 5.8), an array, a record or a variant, and the value of an array or record constant is a literal (section 6.5) of constants, that of a variant one a case built from constants, as a program builds one (section 5.6). Its elements and fields are integers, reals, <code>BOOL</code>s, enumerations, sets, <code>ERROR</code>s, procedures, <code>nil</code>, a string for a <code>[]const U8</code>, an array constant, an array literal or the empty slice for a <code>[]const T</code> of its element type, and arrays, records and variants of these; no other pointer. A named constant stands for its literal wherever an array, record or variant is wanted. A variant constant must have a type: <code>const D = dot;</code> is an error, <code>const D: Shape = dot;</code> isn’t.</p>
143152 <pre><code>const Primes: [5]I64 = [2, 3, 5, 7, 11];
144-type Rule = record class: []U8; tag: INT; float: BOOL; end;
153+type Rule = record class: []const U8; tag: INT; float: BOOL; end;
145154 const Rules: [2]Rule = [{class: &quot;firefox&quot;, tag: 2}, {class: &quot;mpv&quot;, float: true}];
146155 const Shapes: []Shape = [circle(1.0), rect(2.0, 3.0), dot]; (* Shape of section 5.6 *)
156+const Rows: [][]const INT = [[1, 2], [3]]; (* each row an array constant of its own *)
147157 ...
148158 p := Primes[i];</code></pre>
149-<p>It is used as a variable of that type is, but can’t be changed: an array constant can be indexed and sliced and its <code>LEN</code> is <code>n</code>, a record constant’s fields can be read, and either can be assigned, compared as its type allows, and passed. Its parts are constant as a string’s bytes are (section 5.10). A part of it isn’t a constant, even at a constant index.</p>
159+<p>It is used as a variable of that type is, but can’t be changed: an array constant can be indexed and sliced and its <code>LEN</code> is <code>n</code>, a record constant’s fields can be read, and either can be assigned, compared as its type allows, and passed. A slice of it, or of an array in it, is a <code>[]const T</code> (section 5.3), as a string’s is. A part of it isn’t a constant, even at a constant index.</p>
150160 <p>An array constant may be typed <code>[]T</code>, with no length: its length is then its literal’s, as a string constant’s is the string’s, and it is an array constant of that length, <code>[n]T</code>, in all else. If its literal is <code>[]</code>, it is no array but the empty slice, as a slice variable starts: a value of type <code>[]T</code>, used as a slice is, and <code>LEN</code> of it is 0 but not a constant.</p>
151161 <pre><code>const Wide: [][2]I32 = [[1100H, 115FH], [2E80H, 303EH], [0AC00H, 0D7A3H]];
152162 Count = LEN(Wide); (* 3, a constant *)
@@ -155,7 +165,7 @@
155165 <h1 id="types">5. Types</h1>
156166 <pre><code>Type = QualIdent
157167 | &quot;[&quot; Expr &quot;]&quot; Type (* array *)
158- | &quot;[&quot; &quot;]&quot; Type (* slice *)
168+ | &quot;[&quot; &quot;]&quot; [ &quot;const&quot; ] Type (* slice *)
159169 | &quot;^&quot; [ &quot;VOLATILE&quot; ] Type (* pointer *)
160170 | [ &quot;packed&quot; ] &quot;record&quot; { IdentList &quot;:&quot; Type &quot;;&quot; } &quot;end&quot;
161171 | &quot;variant&quot; { Case &quot;;&quot; } &quot;end&quot;
@@ -220,14 +230,17 @@
220230 <h2 id="slices">5.3 Slices</h2>
221231 <p><code>[]T</code> is a view of elements that live elsewhere: a pointer to the first and a count, two addresses’ worth, 16 bytes on x86-64.</p>
222232 <pre><code>Slice = record ptr: ^T; len: INT; end; (* its layout *)</code></pre>
223-<p>A slice is made from an array (<code>a[2..5]</code>, or an array used where a slice is wanted), from another slice, or from a pointer (<code>p[0..n]</code>). It doesn’t own what it points at. <code>LEN(s)</code> is its count, and <code>PTR(s)</code> its pointer (section 6.8).</p>
233+<p>A slice is made from an array (<code>a[2..5]</code>, or an array used where a slice is wanted), from another slice, or from a pointer (<code>p[0..n]</code>). It doesn’t own what it points at. <code>LEN(s)</code> is its count, and <code>SYSTEM.PTR(s)</code> its pointer (section 6.8).</p>
234+<p><code>[]const T</code> is a slice whose elements can’t be written through it. It is laid out as <code>[]T</code> is. Its elements are read, and it is sliced, and a slice of it is a <code>[]const T</code> too; but an element of it isn’t assigned, given to <code>COPY</code> or <code>FILL</code> as the destination, changed by <code>INC</code> or <code>DEC</code>, passed as a <code>var</code> argument, or written through a view, and <code>SYSTEM.ADR</code> of one is an error. A <code>[]T</code> and an array of <code>T</code> go silently where a <code>[]const T</code> is wanted, never the other way (section 5.10). So a procedure that only reads a slice takes a <code>[]const T</code>, and every caller’s slice or array fits it:</p>
235+<pre><code>proc count(s: []const U8; c: U8): INT; (* reads s, and says so *)</code></pre>
236+<p>A string literal and a slice of a constant are <code>[]const T</code> (sections 4 and 5.10). Another slice may still point at the same elements and write them: <code>const</code> says what this slice may do, not what the elements are.</p>
224237 <h2 id="pointers">5.4 Pointers</h2>
225238 <p><code>^T</code> is the address of a <code>T</code>, as big as an <code>INT</code>. <code>nil</code> is the pointer to nothing.</p>
226239 <p><code>^VOLATILE T</code> is the address of a <code>T</code> that something besides the program may read or change: a device register, memory shared with another process. Section 7.6 says what that promises.</p>
227240 <h2 id="records">5.5 Records</h2>
228241 <pre><code>type Cell = record ch: U8; attr: U8; end;</code></pre>
229242 <p>Fields are laid out in order as C lays them out: each at the next multiple of its own alignment, and the whole record padded to a multiple of its largest alignment. A record declared like a C struct has the same layout as that struct. A record may have no fields; its size is then 0.</p>
230-<p>A <code>packed record</code> has no padding: each field starts at the byte after the field before it, and the record’s alignment is 1. So a field of a packed record may sit at any address. It is read and written like any other, on every target, even one whose machine can’t reach a misaligned word in one access: the compiled code reaches it in parts there. But a field of a packed record whose type is aligned to more than 1 byte, and anything inside it, has no address: <code>ADR</code> of it, a <code>var</code> parameter of it and a slice of it are errors. Nor can it be reached through a <code>^VOLATILE</code> pointer, since a misaligned access can’t be promised as one access (section 7.6). Its value can be copied out and back in.</p>
243+<p>A <code>packed record</code> has no padding: each field starts at the byte after the field before it, and the record’s alignment is 1. So a field of a packed record may sit at any address. It is read and written like any other, on every target, even one whose machine can’t reach a misaligned word in one access: the compiled code reaches it in parts there. But a field of a packed record whose type is aligned to more than 1 byte, and anything inside it, has no address: <code>SYSTEM.ADR</code> of it, a <code>var</code> parameter of it and a slice of it are errors. Nor can it be reached through a <code>^VOLATILE</code> pointer, since a misaligned access can’t be promised as one access (section 7.6). Its value can be copied out and back in.</p>
231244 <p>The bytes of a value of more than one byte are in the target’s order (appendix A). A file or a message in an order of its own is read and written a byte at a time, as <code>Str.le</code>, <code>Str.be</code>, <code>Str.putLE</code> and <code>Str.putBE</code> do (the library, <code>doc/raz-lib.md</code>).</p>
232245 <h2 id="variants">5.6 Variants</h2>
233246 <p>A variant holds one of several cases, each with its own fields:</p>
@@ -259,8 +272,8 @@
259272 <h2 id="errors">5.8 Errors</h2>
260273 <pre><code>const NotFound*: ERROR = &quot;not found&quot;;</code></pre>
261274 <p>An <code>ERROR</code> is how a procedure says it failed: by convention the last of its results, and <code>nil</code> when nothing failed. A caller that can’t deal with a failure itself passes it on with <code>try</code> (section 7.8).</p>
262-<p><code>ERROR</code> is a basic type as big as an address. A value of it is made only by a constant declared as above, and each such declaration makes an error different from every other, whatever its message: two modules that both declare <code>"not found"</code> have two different errors. A generic module makes its errors once per instance. <code>nil</code> is no error.</p>
263-<p>Errors compare with <code>=</code> and <code>#</code> only, by which declaration made them, never by their message. <code>MSG(e)</code> is the message, a <code>[]U8</code>; <code>MSG(nil)</code> is <code>""</code>.</p>
275+<p><code>ERROR</code> is a basic type as big as an address. A value of it is made only by a constant declared as above, and each such declaration makes an error different from every other, whatever its message: two modules that both declare <code>"not found"</code> have two different errors. A generic module makes its errors once per instance. <code>nil</code> is no error. Only a string makes a new error: an <code>ERROR</code> constant whose value is another <code>ERROR</code> constant is that same error, so <code>const Short*: ERROR = PictBase.Short;</code> passes on an error of another module, and <code>Short = PictBase.Short</code> holds.</p>
276+<p>Errors compare with <code>=</code> and <code>#</code> only, by which declaration made them, never by their message. <code>MSG(e)</code> is the message, a <code>[]const U8</code>; <code>MSG(nil)</code> is <code>""</code>.</p>
264277 <pre><code>proc find*(key: I64): (^Node, ERROR);
265278 ...
266279 n, err := Table.find(7);
@@ -268,21 +281,24 @@
268281 <h2 id="procedure-types">5.9 Procedure types</h2>
269282 <pre><code>type Compare = proc(a, b: I64): BOOL;</code></pre>
270283 <p>A value of a procedure type is the address of a procedure with that signature, as big as an address, or <code>nil</code>. Two signatures are alike when their parameters have the same types, each <code>var</code>, <code>const</code> or neither alike, in order, and their results are the same types; the parameters’ names don’t matter.</p>
284+<p>A parameter or a result of a procedure type in a <code>type</code> section may name a type declared later in it, a record that is being declared too:</p>
285+<pre><code>type Lines = record each: proc(var r: Lines; s: []U8); n: INT; end;</code></pre>
286+<p>A procedure value is an address whatever its signature, so the record’s size doesn’t depend on it; the type is whole when a call is made. A field of the record’s own type, by value, is still an error, since nothing would be big enough.</p>
271287 <h2 id="same-and-assignable">5.10 Same and assignable</h2>
272-<p>Two named types are the same only if they are the same declaration. Two anonymous pointer, array, slice, set or procedure types are the same if they are built alike: <code>^I64</code> is <code>^I64</code> wherever it is written. A record, variant or enumeration, though, is a type of its own wherever it is written, named or not: two <code>record x: I64; end</code> are two types. A named and an anonymous type are the same if the named one is built like the anonymous one, so that <code>p := ADR(n)</code> works for <code>p: NodePtr</code> with <code>type NodePtr = ^Node</code>. Sameness doesn’t pass through the anonymous type: with <code>type P1 = ^Node; P2 = ^Node;</code>, each of <code>P1</code> and <code>P2</code> is the same as <code>^Node</code>, but they are two declarations, so not the same as each other.</p>
288+<p>Two named types are the same only if they are the same declaration. Two anonymous pointer, array, slice, set or procedure types are the same if they are built alike: <code>^I64</code> is <code>^I64</code> wherever it is written. A record, variant or enumeration, though, is a type of its own wherever it is written, named or not: two <code>record x: I64; end</code> are two types. A named and an anonymous type are the same if the named one is built like the anonymous one, so that <code>p := SYSTEM.ADR(n)</code> works for <code>p: NodePtr</code> with <code>type NodePtr = ^Node</code>. Sameness doesn’t pass through the anonymous type: with <code>type P1 = ^Node; P2 = ^Node;</code>, each of <code>P1</code> and <code>P2</code> is the same as <code>^Node</code>, but they are two declarations, so not the same as each other.</p>
273289 <p>A type declared with only a name on the right, <code>type Count = I64;</code>, is another name for that type, not a new one.</p>
274290 <p>A value of type <code>A</code> can be assigned to a variable of type <code>B</code> (and passed to a parameter of type <code>B</code>, and returned as a <code>B</code>) if they are the same type, or if:</p>
275291 <ul>
276292 <li><code>A</code> is an integer constant whose value fits in the integer type <code>B</code>;</li>
277293 <li><code>A</code> is <code>nil</code> and <code>B</code> is a pointer, procedure or <code>ERROR</code> type;</li>
278294 <li><code>A</code> is <code>^T</code> and <code>B</code> is <code>^VOLATILE T</code>;</li>
279-<li><code>A</code> is <code>[n]T</code> and <code>B</code> is <code>[]T</code>;</li>
280-<li><code>A</code> is a string literal and <code>B</code> is <code>[]U8</code>, <code>^U8</code> (a pointer to the bytes followed by a zero byte, as C wants), or <code>[n]U8</code> with <code>n</code> at least the string’s length (the rest is zero);</li>
295+<li><code>A</code> is <code>[n]T</code> and <code>B</code> is <code>[]T</code> or <code>[]const T</code>, or <code>A</code> is <code>[]T</code> and <code>B</code> is <code>[]const T</code>;</li>
296+<li><code>A</code> is a string literal and <code>B</code> is <code>[]const U8</code>, <code>[n]U8</code> with <code>n</code> at least the string’s length (the rest is zero), or, in a module that imports <code>SYSTEM</code>, <code>^U8</code> (a pointer to the bytes followed by a zero byte, as C wants);</li>
281297 <li><code>A</code> is a procedure and <code>B</code> a procedure type with the same signature;</li>
282298 <li><code>A</code> is <code>INT</code> and <code>B</code> is <code>I64</code>, or <code>A</code> is <code>UINT</code> and <code>B</code> is <code>U64</code>, which loses nothing on any target;</li>
283299 <li><code>A</code> is <code>U8</code>, <code>I8</code> or <code>I16</code> and <code>B</code> is <code>INT</code>, or <code>A</code> is <code>U8</code> or <code>U16</code> and <code>B</code> is <code>UINT</code>, which an <code>INT</code> or a <code>UINT</code> of 16 bits holds whole, so on every target; unless the value is what an operator gives (below).</li>
284300 </ul>
285-<p>A string’s bytes are constant. A <code>[]U8</code> or <code>^U8</code> made from a string literal, or a slice of a string constant, points at them, and writing through it isn’t defined: on a target with read-only memory, it may stop the program as a bad address (section 10). A <code>[n]U8</code> made from a string is a copy, the program’s to change.</p>
301+<p>A string’s bytes are constant. A string literal, or a slice of a string constant, is a <code>[]const U8</code>, so writing them is an error. A <code>[n]U8</code> made from a string is a copy, the program’s to change. A <code>^U8</code> made from one, for C, points at the constant bytes, and writing through it isn’t defined (section 10).</p>
286302 <p>Nothing else converts silently. In particular, there is no other conversion between integer types, between integers and reals, or between <code>F32</code> and <code>F64</code>, without a written conversion (section 6.7). An <code>INT</code> widens to <code>I64</code>, and a <code>UINT</code> to <code>U64</code>, wherever its value goes where an <code>I64</code> is wanted: in <code>a, b := f(x)</code> and <code>return f(x)</code> as in any other assignment, to the bounds of a <code>for</code> and the step of <code>INC</code> and <code>DEC</code> whose variable is an <code>I64</code>, and in an expression, where it meets an <code>I64</code> (section 6.1). The types themselves stay two: a <code>^INT</code> isn’t a <code>^I64</code>, an <code>INT</code> variable isn’t passed to a <code>var</code> parameter of type <code>I64</code>, and a procedure returning an <code>INT</code> isn’t of a procedure type returning an <code>I64</code>.</p>
287303 <p>A <code>U8</code>, <code>I8</code> or <code>I16</code> widens to <code>INT</code>, and a <code>U8</code> or <code>U16</code> to <code>UINT</code>, in the same places, as an <code>INT</code> to <code>I64</code>: assigned, passed, returned, in <code>a, b := f(x)</code>, to a <code>for</code>’s bounds and <code>INC</code> and <code>DEC</code>’s step, and in an expression, where it meets an <code>INT</code> or a <code>UINT</code> (section 6.1); not in <code>return f(x)</code>, which passes <code>f</code>’s results on as they are. They widen only that far: a <code>U8</code> meets an <code>I64</code> only as <code>INT(b)</code> or <code>I64(b)</code>. And they widen only as values that are moved: a variable, an element, a field, a call’s result or a conversion’s. What an operator gives on them doesn’t: it was computed in their type, and has wrapped there. With <code>a, b: U8</code> holding 200 and 100, <code>a + b</code> is 44, so <code>n := a + b</code>, for an <code>INT</code> <code>n</code>, is an error; <code>n := INT(a) + b</code> is 300, and says where the sum is made.</p>
288304 <pre><code>var s: []U8; n, d: INT;
@@ -315,9 +331,12 @@
315331 <h2 id="integers">6.1 Integers</h2>
316332 <p>Both operands of <code>+ - * div mod &amp; | xor</code>, and of a relation, have the same integer type, or one is an untyped constant that takes the other’s type, which it must fit, or one is an <code>INT</code> and the other an <code>I64</code> (a <code>UINT</code> and a <code>U64</code>), and the <code>INT</code> widens to <code>I64</code>, or one is an <code>INT</code> and the other a <code>U8</code>, <code>I8</code> or <code>I16</code> (a <code>UINT</code> and a <code>U8</code> or <code>U16</code>) that no operator gave, and it widens to <code>INT</code> (section 5.10). So do <code>v</code> and <code>k</code> in <code>INC(v, k)</code> and <code>DEC(v, k)</code>, where only <code>k</code> widens: an <code>I64</code> <code>k</code> isn’t added to an <code>INT</code> <code>v</code>. The result has that type and wraps around at its width: in <code>U8</code>, <code>200 + 100</code> is <code>44</code>. A unary minus wraps too: for <code>x</code> of an unsigned type, <code>-x</code> is <code>0 - x</code>. Other integer types are never mixed, whatever their sizes: with <code>i: I32</code>, <code>i &lt; LEN(s)</code> is an error, and <code>i</code> should be an <code>INT</code>; with <code>c: U8</code>, <code>c &lt; LEN(s)</code> is an <code>INT</code> comparison. With <code>pos: I64</code>, <code>pos + LEN(s)</code> is an <code>I64</code>.</p>
317333 <p>Where the report asks for an integer and names no type, as an index, a slice’s bound, a shift’s count, a pointer’s offset <code>i</code> in <code>p + i</code>, or a conversion’s operand, any integer type will do, and its value is taken. On a target whose <code>INT</code> is narrower, a pointer’s offset is cut to an <code>INT</code>.</p>
318-<p><code>SIZE(T)</code>, and <code>LEN</code> of an array or a string constant, are untyped constants, like a literal.</p>
334+<p><code>SIZE(T)</code>, <code>SIZE(T.f)</code>, <code>OFFSET(T, f)</code>, and <code>LEN</code> of an array or a string constant, are untyped constants, like a literal.</p>
319335 <p><code>div</code> rounds toward zero and <code>mod</code> takes the sign of the dividend, as in C: <code>-7 div 2</code> is <code>-3</code>, <code>-7 mod 2</code> is <code>-1</code>. Dividing by zero, and dividing the smallest value of a signed type by <code>-1</code>, stop the program (section 10), whatever the type and the target.</p>
320336 <p><code>&amp;</code> (and), <code>|</code> (or), <code>xor</code> and <code>~</code> (not) work on every bit. <code>x shl n</code> and <code>x shr n</code> shift by <code>n</code> modulo the width of <code>x</code>’s type, in bits: for <code>x: U8</code>, <code>x shl 9</code> is <code>x shl 1</code>. <code>shr</code> brings in copies of the sign bit for signed types and zeros for unsigned ones. <code>n</code> may be of any integer type. The result has <code>x</code>’s type, cut to its width: in <code>U8</code>, <code>0C0H shl 1</code> is <code>80H</code>. If <code>x</code> is an untyped constant, so is the result, and it doesn’t wrap: <code>1 shl 9</code> is 512, which doesn’t fit a <code>U8</code>.</p>
337+<p><code>ROL(x, n)</code> and <code>ROR(x, n)</code> (section 6.8) rotate <code>x</code> left and right by <code>n</code> bits modulo its width: the bits shifted out at one end come back in at the other. For <code>x: U8</code> holding <code>81H</code>, <code>ROL(x, 1)</code> is <code>03H</code> and <code>ROR(x, 1)</code> is <code>0C0H</code>. <code>x</code> is of an unsigned integer type, <code>U8</code>, <code>U16</code>, <code>U32</code>, <code>U64</code> or <code>UINT</code>, and the result has its type; <code>n</code> may be of any integer type. An untyped constant has no width, so <code>ROL(1, 3)</code> is an error, and <code>ROL(U32(1), 3)</code> is 8. Hashes and ciphers are written with rotations, which machines do in one instruction, and which two shifts and an <code>or</code> say only at more length: <code>ROR(x, 7)</code> is <code>x shr 7 | x shl 25</code> for a <code>U32</code> <code>x</code>.</p>
338+<p><code>CLZ(x)</code> and <code>CTZ(x)</code> (section 6.8) count the zero bits above the highest one in <code>x</code> and below the lowest one, and <code>POPCOUNT(x)</code> counts its one bits. <code>x</code> is of an unsigned integer type, and the result is an <code>INT</code>; for <code>x: U8</code>, <code>CLZ(x)</code> is 7 when <code>x</code> is 1, and <code>CLZ(x)</code> and <code>CTZ(x)</code> are 8 when <code>x</code> is 0. The width is the type’s, as for <code>ROL</code>; an untyped constant has none, so <code>CLZ(1)</code> is an error, and <code>CLZ(U64(1))</code> is 63. <code>POPCOUNT(x)</code> may also take a set, and counts its members. <code>MIN(T)</code> and <code>MAX(T)</code> are the smallest and the largest value of the integer type <code>T</code>, untyped constants: <code>MAX(U8)</code> is 255 and <code>MIN(I16)</code> is -32768; <code>MAX(U64)</code> and <code>MAX(UINT)</code>, above the largest <code>I64</code>, have their own type, as a hexadecimal number above it has (section 2). Each of the five, given constants, is a constant (section 4).</p>
339+<p><code>ABS(x)</code> (section 6.8) is <code>x</code> without its sign, of <code>x</code>’s type, for an integer or a real. For a signed integer it is <code>-x</code> if <code>x &lt; 0</code>, so the smallest value of the type gives itself, as <code>-x</code> does; for an unsigned type it is <code>x</code>. For a real it clears the sign bit: <code>ABS(-0.0)</code> is 0.0, <code>ABS</code> of a NaN is a NaN, and of minus infinity is infinity. Of a constant it is a constant; of an untyped one, an untyped one, <code>ABS(-5)</code> is 5, and a result beyond the <code>I64</code>s, <code>ABS(MIN(I64))</code>, is an error, as for any arithmetic on untyped constants (section 2).</p>
321340 <p>Relations compare as signed or unsigned according to the type.</p>
322341 <h2 id="booleans">6.2 Booleans</h2>
323342 <p><code>and</code> and <code>or</code> evaluate their right operand only when it decides the result. <code>not</code> negates, and <code>=</code> and <code>#</code> compare; <code>#</code> is “different”.</p>
@@ -332,12 +351,13 @@
332351 <li><code>p[i]</code> is <code>(p + i)^</code>. <code>p[lo..hi]</code> is the slice of the <code>hi - lo</code> elements from <code>p + lo</code>. Neither is checked against a length, which a pointer doesn’t have, but a slice of a pointer that runs backwards, <code>hi &lt; lo</code>, is a failed check (section 6.5). A pointer to an array isn’t indexed this way, since a step would be a whole array: its elements are <code>p^[i]</code>.</li>
333352 <li>Pointers compare with <code>= # &lt; &lt;= &gt; &gt;=</code> as unsigned addresses. Procedure values compare with <code>=</code> and <code>#</code> only.</li>
334353 </ul>
354+<p><code>p + i</code>, <code>p - i</code>, <code>p[i]</code> and <code>p[lo..hi]</code> reach memory no variable of the program names, and are allowed only in a module that imports <code>SYSTEM</code> (section 3). <code>p^</code>, <code>p - q</code> and comparing are allowed in any.</p>
335355 <h2 id="arrays-and-slices">6.5 Arrays and slices</h2>
336356 <p><code>a[i]</code> is an element of an array or slice, and <code>a[lo..hi]</code> is the slice of the elements from <code>lo</code> up to but not including <code>hi</code>. Both are checked: <code>i</code> must be below the length, and <code>0 &lt;= lo &lt;= hi &lt;= length</code>. A check that fails prints to standard error where it is, as file, line and column, and what failed, with the values involved (<code>index 7 is out of range for a length of 5</code>), and stops the program with status 2. An index or a bound may be of any integer type, and is checked by its value, whatever its type: a negative <code>I8</code>, or a <code>U64</code> past the length, fails like any other. A module that says <code>check off;</code> (section 3) leaves these checks out of its code.</p>
337357 <p><strong>Literals.</strong> <code>[e1, e2, ...]</code> builds an array, and <code>{f: e, g: e, ...}</code> a record, where the type it must have is known: assigned to a variable, passed to a parameter, returned, compared with a value of that type, or an element of a bigger literal. The elements must be assignable to the element type, and an array literal for <code>[n]T</code> has exactly <code>n</code> of them. A record literal names its fields, in any order, each at most once; the fields it leaves out are zero, so <code>{}</code> is the record with every field zero. An array literal used as a slice is a hidden variable of the procedure, and lives as long as its call; the empty one, <code>[]</code>, is the empty slice, as a slice variable starts (section 4), and no variable at all.</p>
338358 <pre><code>c := {ch: &#39;A&#39;, attr: 7};
339359 Fmt.print(&quot;{} of {}\n&quot;, [Fmt.int(n), Fmt.int(m)]);</code></pre>
340-<p><strong>Arrays and strings compare.</strong> Two operands that are each a <code>[]T</code> or a <code>[n]T</code>, of the same element type <code>T</code>, are <code>=</code> when they have the same length and the same elements, and <code>#</code> otherwise, where <code>T</code> is an integer type, <code>BOOL</code>, an enumeration, a set or a pointer type. A string literal compares as a <code>[]U8</code>. The comparison is code of its own where it stands, a check of the lengths and then of the bytes, not a call.</p>
360+<p><strong>Arrays and strings compare.</strong> Two operands that are each a <code>[]T</code> or a <code>[n]T</code>, of the same element type <code>T</code>, are <code>=</code> when they have the same length and the same elements, and <code>#</code> otherwise, where <code>T</code> is an integer type, <code>BOOL</code>, an enumeration, a set or a pointer type. A string literal compares as a <code>[]const U8</code>. The comparison is code of its own where it stands, a check of the lengths and then of the bytes, not a call.</p>
341361 <p>Arrays of reals don’t compare this way, since <code>-0.0 = 0.0</code> and a NaN is equal to nothing, though their bytes say otherwise; nor do arrays of records, variants or procedures, nor records and variants themselves. Write a loop. Ordering strings is the library’s <code>Str.cmp</code>.</p>
342362 <h2 id="enumerations-and-sets-1">6.6 Enumerations and sets</h2>
343363 <p>Enumerations compare with all six relations, by position, and an enumeration can be the variable of a <code>for</code> loop and the expression of a <code>match</code> (section 7.4).</p>
@@ -389,6 +409,11 @@
389409 <td>the name at that position; a position out of range is a failed check (6.5), and in a constant an error</td>
390410 </tr>
391411 <tr>
412+<td><code>BOOL</code></td>
413+<td>integer</td>
414+<td>0 for <code>false</code>, 1 for <code>true</code></td>
415+</tr>
416+<tr>
392417 <td>set</td>
393418 <td>integer</td>
394419 <td>its bits</td>
@@ -415,7 +440,7 @@
415440 </tr>
416441 </tbody>
417442 </table>
418-<p>There are no others: no conversion between <code>BOOL</code> and integers, none between pointers and integers other than <code>INT</code> and <code>UINT</code>, and none to or from records, arrays, slices, variants or <code>ERROR</code>. An untyped integer constant converts to a pointer as an <code>INT</code> does, as in <code>^VOLATILE U32(0FEE00000H)</code>; it isn’t assigned to a pointer without the conversion. A conversion of a constant is made by the compiler, by the same rules: <code>U8(300)</code> is 44.</p>
443+<p>There are no others: none from an integer to <code>BOOL</code> (<code>x # 0</code> says it), none between pointers and integers other than <code>INT</code> and <code>UINT</code>, and none to or from records, arrays, slices, variants or <code>ERROR</code>. The last three rows, from an integer to a pointer or a procedure, back, and from one pointer or procedure type to another, are allowed only in a module that imports <code>SYSTEM</code> (section 3). An untyped integer constant converts to a pointer as an <code>INT</code> does, as in <code>^VOLATILE U32(0FEE00000H)</code>; it isn’t assigned to a pointer without the conversion. A conversion of a constant is made by the compiler, by the same rules: <code>U8(300)</code> is 44. <code>U64(a = b)</code> is 0 or 1, and <code>0 - U64(a = b)</code> is a mask; this is how a comparison is used with no <code>if</code>.</p>
419444 <h2 id="built-in-procedures">6.8 Built-in procedures</h2>
420445 <table>
421446 <thead>
@@ -430,7 +455,15 @@
430455 <td>the size of type <code>T</code> in bytes, an untyped constant; <code>T</code> is a type, not a variable</td>
431456 </tr>
432457 <tr>
433-<td><code>ADR(x)</code></td>
458+<td><code>SIZE(T.f)</code></td>
459+<td>the size of field <code>f</code> of the record type <code>T</code>, an untyped constant</td>
460+</tr>
461+<tr>
462+<td><code>OFFSET(T, f)</code></td>
463+<td>the byte offset of field <code>f</code> in the record type <code>T</code>, an untyped constant</td>
464+</tr>
465+<tr>
466+<td><code>SYSTEM.ADR(x)</code></td>
434467 <td>the address of variable <code>x</code>, a <code>^T</code></td>
435468 </tr>
436469 <tr>
@@ -438,8 +471,12 @@
438471 <td>the number of elements of an array or a string constant (an untyped constant) or of a slice (an <code>INT</code>)</td>
439472 </tr>
440473 <tr>
441-<td><code>PTR(s)</code></td>
442-<td>the address the slice <code>s</code> starts at, a <code>^T</code> for <code>s: []T</code>, unchecked; for an array <code>a</code>, that of <code>a[0..LEN(a)]</code></td>
474+<td><code>SYSTEM.PTR(s)</code></td>
475+<td>the address the slice <code>s</code> starts at, a <code>^T</code> for <code>s: []T</code> or <code>[]const T</code>, unchecked; for an array <code>a</code>, that of <code>a[0..LEN(a)]</code></td>
476+</tr>
477+<tr>
478+<td><code>BYTES(x)</code></td>
479+<td>the bytes of <code>x</code> as a <code>[]U8</code>: for a slice <code>s: []T</code>, its <code>LEN(s) * SIZE(T)</code> bytes, a <code>[]const U8</code> for a <code>[]const T</code>; for a variable of any other type <code>T</code>, its <code>SIZE(T)</code> bytes</td>
443480 </tr>
444481 <tr>
445482 <td><code>NEW(p, a)</code></td>
@@ -447,14 +484,18 @@
447484 </tr>
448485 <tr>
449486 <td><code>MSG(e)</code></td>
450-<td>the message of the error <code>e</code>, a <code>[]U8</code></td>
487+<td>the message of the error <code>e</code>, a <code>[]const U8</code></td>
451488 </tr>
452489 <tr>
453490 <td><code>COPY(d, s)</code></td>
454491 <td>copies the elements of <code>s</code> to the start of <code>d</code>, both arrays or slices of one type <code>T</code>; <code>LEN(s)</code> must be at most <code>LEN(d)</code></td>
455492 </tr>
456493 <tr>
457-<td><code>SYSCALL(n, x1, ..., x6)</code></td>
494+<td><code>FILL(d, x)</code></td>
495+<td>sets every element of <code>d</code>, an array or a slice of elements of type <code>T</code>, to <code>x</code>, a value of <code>T</code></td>
496+</tr>
497+<tr>
498+<td><code>SYSTEM.SYSCALL(n, x1, ..., x6)</code></td>
458499 <td>Linux system call <code>n</code> with up to six arguments, each a pointer or an integer no wider than <code>INT</code>; the raw result, an <code>INT</code> (a negative number is <code>-errno</code>)</td>
459500 </tr>
460501 <tr>
@@ -470,40 +511,69 @@
470511 <td>a full memory barrier</td>
471512 </tr>
472513 <tr>
473-<td><code>CAS(p, old, new)</code></td>
474-<td>if <code>p^ = old</code>, sets <code>p^ := new</code> and returns <code>true</code>; atomically</td>
514+<td><code>CAS(v, old, new)</code></td>
515+<td>if <code>v = old</code>, sets <code>v := new</code> and returns <code>true</code>; atomically</td>
475516 </tr>
476517 <tr>
477-<td><code>XCHG(p, x)</code></td>
478-<td>sets <code>p^ := x</code> and returns the value before, atomically</td>
518+<td><code>XCHG(v, x)</code></td>
519+<td>sets <code>v := x</code> and returns the value before, atomically</td>
479520 </tr>
480521 <tr>
481-<td><code>XADD(p, x)</code></td>
482-<td>adds <code>x</code> to <code>p^</code> and returns the value before, atomically</td>
522+<td><code>XADD(v, x)</code></td>
523+<td>adds <code>x</code> to <code>v</code> and returns the value before, atomically</td>
483524 </tr>
484525 <tr>
526+<td><code>ABS(x)</code></td>
527+<td><code>x</code> without its sign, of <code>x</code>’s type; for an integer, <code>-x</code> if <code>x &lt; 0</code>, so the smallest <code>I64</code> gives itself, as <code>-x</code> does; for a real, the sign cleared; <code>x</code> of a numeric type (section 6.1)</td>
528+</tr>
529+<tr>
530+<td><code>ROL(x, n)</code>, <code>ROR(x, n)</code></td>
531+<td><code>x</code> rotated left, or right, by <code>n</code> bits modulo its width; <code>x</code> of an unsigned integer type (section 6.1)</td>
532+</tr>
533+<tr>
534+<td><code>CLZ(x)</code>, <code>CTZ(x)</code></td>
535+<td>the number of zero bits above the highest one in <code>x</code>, or below the lowest one, an <code>INT</code>; the width of <code>x</code>’s type if <code>x</code> is 0; <code>x</code> of an unsigned integer type</td>
536+</tr>
537+<tr>
538+<td><code>POPCOUNT(x)</code></td>
539+<td>the number of one bits in <code>x</code>, or of members of the set <code>x</code>, an <code>INT</code>; <code>x</code> of an unsigned integer type or a set</td>
540+</tr>
541+<tr>
542+<td><code>MIN(T)</code>, <code>MAX(T)</code></td>
543+<td>the smallest, or the largest, value of the integer type <code>T</code>, an untyped constant; any other type is an error</td>
544+</tr>
545+<tr>
485546 <td><code>ARGC()</code></td>
486547 <td>the number of command line arguments, an <code>INT</code></td>
487548 </tr>
488549 <tr>
489-<td><code>ARGV()</code></td>
550+<td><code>SYSTEM.ARGV()</code></td>
490551 <td>the arguments, a <code>^^U8</code> (C strings)</td>
491552 </tr>
492553 </tbody>
493554 </table>
494-<p><code>SYSCALL</code> exists only on Linux targets. Other systems have no stable system call interface, and their modules call the C library instead (section 9).</p>
555+<p><code>SYSTEM.SYSCALL</code> exists only on Linux targets. Other systems have no stable system call interface, and their modules call the C library instead (section 9).</p>
495556 <p><code>NEW(p, a)</code> calls <code>a^.alloc(a, SIZE(T), A)</code>, where <code>A</code> is the alignment of <code>T</code>, and zeroes the bytes it gets if they aren’t <code>nil</code>. <code>Mem.Allocator</code> (the library, <code>doc/raz-lib.md</code>) is the one type of the library the language names.</p>
496-<p><code>PTR(s)</code> is the first of the two words of <code>s</code> (section 5.3), and is not checked. For a slice that isn’t empty it is <code>ADR(s[0])</code>; for an empty one, where <code>s[0]</code> would stop the program, it is what the slice holds: <code>nil</code> for a slice variable as it starts (section 4) and for <code>[]</code>, and for one cut empty from another, <code>t[3..3]</code>, an address that mustn’t be read through, as the end of an array mustn’t. So a slice becomes a pointer and a length, for a record or for C, with no test for the empty one first:</p>
557+<p><code>SIZE(T.f)</code> and <code>OFFSET(T, f)</code> ask about a type’s field, not about storage, so <code>SIZE</code> still takes no variable. <code>f</code> is one field name: not a path, <code>a.b</code>, and not a case of a variant, whose cases share their place. A record handed to C is checked against C’s struct with them, as C checks one with <code>offsetof</code> and <code>sizeof</code>.</p>
558+<p><code>BYTES(x)</code> is the bytes of <code>x</code>, as a <code>[]U8</code> that can be read and written: for a slice <code>s: []T</code>, the <code>LEN(s) * SIZE(T)</code> bytes it holds; for a variable of any other type <code>T</code>, its <code>SIZE(T)</code> bytes.</p>
559+<pre><code>n := Sys.write(fd, BYTES(samples)); (* samples: []I32 *)
560+n := Sys.read(fd, BYTES(h)); (* fills h: Header, a record *)</code></pre>
561+<p><code>T</code> must be a type a view may take (section 6.9), since writing through the slice must not make a value mean nothing: a <code>BOOL</code>, a set, an enumeration, a slice, a variant, a procedure type, <code>ERROR</code>, or a record or array holding one, is an error; and, outside a module that imports <code>SYSTEM</code>, so is a pointer, or a record or array holding one. The slice is made, and checked, as any slice of the variable is, so its bytes follow the rules for it: the bytes of a local can’t be returned (section 7.3). Of a slice they are its elements, as <code>SYSTEM.PTR(s)</code> gives them, whatever holds the slice, and can be written only if the slice can. Of any other variable they are its own, as <code>SYSTEM.ADR</code> gives them: a <code>const</code> parameter, another module’s variable, a view and a field of a packed record are errors, as they are for <code>SYSTEM.ADR</code>, and so is a volatile variable. <code>x</code> must be a variable: a constant, a string constant too, is an error. The bytes are in the target’s order (appendix A).</p>
562+<p><code>SYSTEM.PTR(s)</code> is the first of the two words of <code>s</code> (section 5.3), and is not checked. For a slice that isn’t empty it is <code>SYSTEM.ADR(s[0])</code>; for an empty one, where <code>s[0]</code> would stop the program, it is what the slice holds: <code>nil</code> for a slice variable as it starts (section 4) and for <code>[]</code>, and for one cut empty from another, <code>t[3..3]</code>, an address that mustn’t be read through, as the end of an array mustn’t. So a slice becomes a pointer and a length, for a record or for C, with no test for the empty one first:</p>
497563 <pre><code>type Value = record n: INT; p: ^U8; end;
498564
499-proc valueOf(s: []U8): Value;
565+proc valueOf(s: []const U8): Value;
500566 begin
501- return {n: LEN(s), p: PTR(s)}; (* &quot;&quot; too *)
567+ return {n: LEN(s), p: SYSTEM.PTR(s)}; (* &quot;&quot; too *)
502568 end;</code></pre>
503-<p>and <code>v.p[0..v.n]</code> is the slice again (section 6.4), empty when <code>v.n</code> is 0, whatever <code>v.p</code> is. Reading through the pointer is as unchecked as through any pointer. <code>PTR(a)</code> of an array may be taken wherever the slice <code>a[0..LEN(a)]</code> may be made, and is an error wherever it may not.</p>
569+<p>and <code>v.p[0..v.n]</code> is the slice again (section 6.4), empty when <code>v.n</code> is 0, whatever <code>v.p</code> is. Reading through the pointer is as unchecked as through any pointer. <code>SYSTEM.PTR(a)</code> of an array may be taken wherever the slice <code>a[0..LEN(a)]</code> may be made, and is an error wherever it may not.</p>
504570 <p><code>COPY</code> is a statement, as <code>INC</code> is. It copies the bytes of <code>LEN(s)</code> elements as one block, as an assignment of an array does, and is right when <code>d</code> and <code>s</code> overlap, as in <code>COPY(a[1..n], a[0..n - 1])</code>. It is checked as a slice is: past <code>LEN(d)</code>, it stops the program, unless the module says <code>check off;</code>.</p>
571+<p><code>FILL</code> is a statement too. <code>d</code> is written as <code>COPY</code>’s destination is, and <code>x</code> is assigned to each element as by <code>:=</code>:</p>
572+<pre><code>FILL(row[a..b], U32(bg)); (* a row of pixels *)
573+FILL(window, 0.0); (* window: [576]F32 *)</code></pre>
574+<p>It is no wipe: it may be left out when nothing reads what it wrote, as any write may (section 7.6).</p>
505575 <p><code>INC</code> and <code>DEC</code> evaluate <code>v</code> once, so <code>INC(a[f(i)])</code> calls <code>f</code> once. They read <code>v</code> and then write it: two accesses, even through a <code>^VOLATILE</code> pointer, and not atomic; <code>XADD</code> is the atomic one.</p>
506-<p>For <code>CAS</code>, <code>XCHG</code> and <code>XADD</code>, <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>), on every target. They are sequentially consistent, on every target; how a machine without atomic instructions makes them so is the implementation’s choice (appendix A). ### 6.9 Views</p>
576+<p>For <code>CAS</code>, <code>XCHG</code> and <code>XADD</code>, <code>v</code> is a variable, as for <code>INC</code>, of type <code>INT</code>, <code>UINT</code> or a pointer type (<code>INT</code> or <code>UINT</code> for <code>XADD</code>), on every target, and may be volatile: a counter shared between threads is <code>XADD(count, 1)</code>, one reached through a pointer <code>XADD(p^, 1)</code>. It is evaluated once, and must have an address, so a field of a packed record and a view are errors, as they are for <code>SYSTEM.ADR</code>. They are sequentially consistent, on every target; how a machine without atomic instructions makes them so is the implementation’s choice (appendix A). ### 6.9 Views</p>
507577 <p>A view reads and writes the bytes of a variable as a value of another type. <code>v.(T)</code>, with <code>v</code> a variable, is the <code>T</code> whose bytes start where <code>v</code> starts:</p>
508578 <pre><code>var mem: [4096]U8; n: I64; x: F64; bits: U64;
509579
@@ -519,18 +589,19 @@
519589 INC(mem[a].(I64), 8);</code></pre>
520590 <p><strong>A view isn’t a conversion.</strong> A conversion works on a value, a view on bytes. With <code>x: F64</code> holding 1.5, <code>I64(x)</code> is 1, the number rounded toward zero, while <code>x.(I64)</code> is 3FF8000000000000H, the bits that hold 1.5. With <code>n: I64</code> holding 258, <code>U8(n)</code> is 2, the number cut to its low byte, while <code>n.(U8)</code> is the first byte of <code>n</code> in memory, which is the low one only on a target that puts its low byte first (appendix A). When the program means a number, it wants a conversion; when it means the bytes as they lie, a view.</p>
521591 <p><strong>The types.</strong> <code>T</code> is an integer type, a real type, a pointer type, or a record or array made of these alone, at any depth: the types for which any bytes at all are a value. <code>BOOL</code>, an enumeration, a set, a slice, a variant, a procedure type and <code>ERROR</code> are left out, since bytes that are none of their values would make the program mean nothing. To read one of them from bytes, view the integer and convert it: <code>Colour(mem[a])</code>. <code>v</code> may be of any type, and is a variable: a name of one, a field or element of one, or what a pointer points at; not a constant, nor the result of a call or of an operator.</p>
592+<p>A view makes values from bytes, so where those bytes could be made into an address the module must import <code>SYSTEM</code> (section 3): a view whose <code>T</code> holds a pointer, and a view written over a variable <code>v</code> whose type holds anything but integers and reals, which would change a pointer, a <code>BOOL</code> or a slice behind its type. Reading any variable through a view of integers and reals is allowed in any module.</p>
522593 <p><strong>Where its bytes lie.</strong> The <code>T</code> lies within the variable <code>v</code> is part of:</p>
523594 <ul>
524595 <li>for an element of an array or a slice, <code>a[i].(T)</code>, from that element to the end of <code>a</code>: <code>mem[4088].(I64)</code> is the last 8 bytes of <code>mem</code>, and <code>mem[4090].(I64)</code> would run 2 bytes past its end;</li>
525596 <li>for anything else, within <code>v</code> itself: <code>x.(U64)</code> is allowed only if <code>x</code> has at least 8 bytes.</li>
526597 </ul>
527598 <p>This is checked, as an index is: by the compiler when it can tell, otherwise while the program runs, where a view that runs past the end is a failed check (section 6.5), unless the module says <code>check off;</code>. An element reached through a pointer, <code>p[i].(T)</code>, is checked no more than <code>p[i]</code> is, since a pointer has no length.</p>
528-<p><strong>Any address.</strong> The <code>T</code> may start at any byte: <code>mem[3].(I64)</code> is an <code>I64</code> whose first byte is <code>mem[3]</code>. As a field of a packed record is (section 5.5), it is read and written on every target, in parts where the machine needs that; and, as such a field, a view has no address: <code>ADR</code> of it, a <code>var</code> argument of it and a slice of it are errors. Copy it out to a variable, and back in. A view of a volatile variable is an error too, since its access can’t be promised as one (section 7.6): a device’s registers are reached through a <code>^VOLATILE</code> pointer to a record of them.</p>
599+<p><strong>Any address.</strong> The <code>T</code> may start at any byte: <code>mem[3].(I64)</code> is an <code>I64</code> whose first byte is <code>mem[3]</code>. As a field of a packed record is (section 5.5), it is read and written on every target, in parts where the machine needs that; and, as such a field, a view has no address: <code>SYSTEM.ADR</code> of it, a <code>var</code> argument of it and a slice of it are errors. Copy it out to a variable, and back in. A view of a volatile variable is an error too, since its access can’t be promised as one (section 7.6): a device’s registers are reached through a <code>^VOLATILE</code> pointer to a record of them.</p>
529600 <p><strong>Byte order.</strong> The bytes are in the target’s order (appendix A). A file or a message whose format fixes an order of its own is read with <code>Str.le</code>, <code>Str.be</code> and the like (section 5.5), if the program is to read it right on every target.</p>
530601 <p><strong>Before views.</strong> A program used to make a pointer for each access, and may still: <code>mem[a].(I64) := n</code> does what</p>
531-<pre><code>p := ^I64(ADR(mem[a]));
602+<pre><code>p := ^I64(SYSTEM.ADR(mem[a]));
532603 p^ := n;</code></pre>
533-<p>does, but with <code>a</code> checked against the length of <code>mem</code>, no pointer variable, and no address of a word that may be misaligned.</p>
604+<p>does, but with <code>a</code> checked against the length of <code>mem</code>, no pointer variable, and no address of a word that may be misaligned, and no <code>import SYSTEM</code>.</p>
534605 <p><code>v</code> is evaluated once, and its check made, before the bytes are read or written.</p>
535606 <h1 id="statements">7. Statements</h1>
536607 <pre><code>Statements = { Statement &quot;;&quot; } .
@@ -548,7 +619,7 @@
548619 | &quot;continue&quot;
549620 | &quot;defer&quot; Statement
550621 | &quot;try&quot; Designator .
551-Arm = ( ident [ &quot;(&quot; ident { &quot;,&quot; ident } &quot;)&quot; ]
622+Arm = ( ident { &quot;.&quot; ident } [ &quot;(&quot; ident { &quot;,&quot; ident } &quot;)&quot; ]
552623 | Label { &quot;,&quot; Label } | &quot;else&quot; ) &quot;:&quot; Statements .
553624 Label = Expr [ &quot;..&quot; Expr ] .
554625 Target = Designator | &quot;_&quot; .</code></pre>
@@ -571,19 +642,19 @@
571642 end;</code></pre>
572643 <h2 id="return">7.3 Return</h2>
573644 <p><code>return e;</code> ends the procedure with the result <code>e</code>; <code>return a, b;</code> with several results; <code>return f(x);</code> with the several results of <code>f</code>, if they are the procedure’s own (section 8); <code>return;</code> from a procedure without results. A procedure with results that runs off its end returns zeros, each result as a variable of its type starts (section 4).</p>
574-<p>A slice returned must not point into the procedure’s own frame, which is gone once it returns. It is an error to return, as a slice, a local array, a value parameter that is an array, an array literal other than <code>[]</code>, or an array a call returns, or a slice of one, a field or an element of one included:</p>
645+<p>A slice returned must not point into the procedure’s own frame, which is gone once it returns. It is an error to return, as a slice, a local array, a value parameter that is an array, an array literal other than <code>[]</code>, or an array a call returns, or a slice of one, or the <code>BYTES</code> of a local variable, a field or an element of one included:</p>
575646 <pre><code>proc short(h: Hex): []U8; (* Hex = [64]U8 *)
576647 begin
577648 return h[0..8]; (* an error: h is the procedure&#39;s copy *)
578649 end;</code></pre>
579-<p>A slice of a <code>var</code> parameter, of a module variable, or of what a pointer points at may be returned: none of them is this procedure’s frame. A slice of a frame that leaves it some other way isn’t caught, and is the exception of section 10: one held in a local slice variable, or in a record, that is returned; one stored in a module variable or through a pointer; and one that came back from a call, as the slice of a <code>var</code> parameter does to the caller whose local it is.</p>
650+<p>A slice of a <code>var</code> or <code>const</code> parameter, of a module variable, or of what a pointer points at may be returned: none of them is this procedure’s frame. For a <code>const</code> parameter given a temporary, the slice lives to the end of the caller’s procedure (section 8). A slice of a frame that leaves it some other way isn’t caught, and is the exception of section 10: one held in a local slice variable, or in a record, that is returned; one stored in a module variable or through a pointer; and one that came back from a call, as the slice of a <code>var</code> parameter does to the caller whose local it is.</p>
580651 <h2 id="match">7.4 Match</h2>
581652 <pre><code>match s of
582653 | circle(r): area := 3.14159 * r * r;
583654 | rect(w, h): area := w * h;
584655 | dot: area := 0.0;
585656 end;</code></pre>
586-<p><code>match</code> takes a variant, an enumeration, an integer type (a <code>U8</code> included) or a string, a <code>[]U8</code>; other types are an error. It runs the arm of the case <code>s</code> holds. An arm may name the case’s fields, in order, all of them; the names stand for those fields of <code>s</code> itself, so assigning to one changes <code>s</code>; if <code>s</code> is a constant or a <code>const</code> parameter, they can’t be assigned; if <code>s</code> isn’t a variable, they stand for the fields of a copy. An arm may also leave the fields unnamed. Every case must have an arm, and only one, unless there is an <code>| else:</code> arm, which must come last.</p>
657+<p><code>match</code> takes a variant, an enumeration, an integer type (a <code>U8</code> included) or a string, a <code>[]U8</code> or a <code>[]const U8</code>; other types are an error. It runs the arm of the case <code>s</code> holds. An arm may name the case’s fields, in order, all of them; the names stand for those fields of <code>s</code> itself, so assigning to one changes <code>s</code>; if <code>s</code> is a constant or a <code>const</code> parameter, they can’t be assigned; if <code>s</code> isn’t a variable, they stand for the fields of a copy. An arm may also leave the fields unnamed. Every case must have an arm, and only one, unless there is an <code>| else:</code> arm, which must come last.</p>
587658 <p><code>match</code> on an enumeration works the same way: each arm names one of the enumeration’s names, or several, separated by commas, for the arm to run when the value is any of them:</p>
588659 <pre><code>match op of
589660 | sPlus, sMinus: kind := additive;
@@ -591,6 +662,7 @@
591662 | else:
592663 end;</code></pre>
593664 <p>Every name must have an arm, and only one, unless there is an <code>| else:</code> arm. A variant’s arm names one case: the fields it may name belong to that case alone.</p>
665+<p>An arm on an enumeration or a variant names its case alone or as it is written anywhere else, with its type, its module, or both (<code>done</code>, <code>Kind.done</code>, <code>Xml.done</code>, <code>Xml.Kind.done</code>), and a qualified name must be a case of the matched type.</p>
594666 <p><code>match</code> on an integer runs the arm whose labels hold its value:</p>
595667 <pre><code>match ch of
596668 | &#39; &#39;, &#39;\t&#39;: skip := true;
@@ -598,7 +670,7 @@
598670 | else:
599671 end;</code></pre>
600672 <p>A label is a constant, or <code>lo .. hi</code>, the constants from <code>lo</code> to <code>hi</code>, both included (unlike a slice, which leaves <code>hi</code> out; <code>lo</code> may not be above <code>hi</code>). Each must fit the integer’s type, and no value may be in two labels. An integer <code>match</code> must have an <code>| else:</code> arm, which may be empty.</p>
601-<p><code>match</code> on a string, a <code>[]U8</code> or what converts to one (section 5.10), runs the arm one of whose labels it equals, as <code>=</code> compares strings (section 6.5):</p>
673+<p><code>match</code> on a string, a <code>[]U8</code>, a <code>[]const U8</code> or what converts to one (section 5.10), runs the arm one of whose labels it equals, as <code>=</code> compares strings (section 6.5):</p>
602674 <pre><code>match word of
603675 | &quot;add&quot;, &quot;plus&quot;: op := add;
604676 | &quot;sub&quot;: op := sub;
@@ -611,7 +683,7 @@
611683 <p>A variable reached through a <code>^VOLATILE T</code> is volatile, and so is any field or element of it. Each read or write of a volatile variable of a basic, enumeration, set or pointer type is exactly one memory access, of exactly its size, and volatile accesses happen in the order the program writes them. Copying a whole record or array through a volatile pointer is not allowed, since it can’t be one access.</p>
612684 <p>A <code>^VOLATILE T</code> doesn’t convert to <code>^T</code> without a written conversion, and a volatile variable isn’t passed as a <code>var</code> argument, which would lose its volatility.</p>
613685 <p><code>FENCE</code> also orders the program’s ordinary accesses against those of other processors and devices.</p>
614-<p><strong>Ordinary variables.</strong> Only volatile accesses, <code>CAS</code>, <code>XCHG</code>, <code>XADD</code> and <code>FENCE</code> are exact. Any other access is ordinary: an implementation may keep a variable in a register, leave out a write that nothing reads, and change the order of ordinary accesses, as long as each thread, on its own, does what this report says. A variable that one thread writes while another reads or writes it, unless both use volatile accesses or the atomic built-ins, or a <code>FENCE</code> orders them, holds no particular value then: a read of it gives a value it had or was given, or a mixture of their bytes. Nothing else of the program becomes undefined by it.</p>
686+<p><strong>Ordinary variables.</strong> Only volatile accesses, <code>CAS</code>, <code>XCHG</code>, <code>XADD</code> and <code>FENCE</code> are exact. Any other access is ordinary: an implementation may keep a variable in a register, leave out a write that nothing reads, and change the order of ordinary accesses, as long as each thread, on its own, does what this report says. A variable that one thread writes while another reads or writes it, unless both use volatile accesses or the atomic built-ins, or a <code>FENCE</code> orders them, holds no particular value then: a read of it gives a value it had or was given, or a mixture of their bytes. For a variable of a basic, enumeration or set type, or a record or array of these, nothing else of the program becomes undefined by it. For one that holds a pointer, a procedure, a slice, an <code>ERROR</code> or a variant, a mixture may be an address no variable has, or a slice’s address with another’s length, or a case’s tag with another’s fields, and what follows isn’t defined (section 10). A program that shares such a variable between threads orders their accesses, with a lock or the atomic built-ins.</p>
615687 <h2 id="defer">7.7 Defer</h2>
616688 <p><code>defer s;</code> runs the statement <code>s</code> when the program leaves the statement list the <code>defer</code> stands in: at its end, or by a <code>return</code>, or by a <code>break</code> or <code>continue</code> out of it. Statements deferred in the same list run in the opposite order to their <code>defer</code>s, and only those whose <code>defer</code> has been reached run.</p>
617689 <pre><code>module Head;
@@ -687,7 +759,7 @@
687759 Results = Type | &quot;(&quot; Type { &quot;,&quot; Type } &quot;)&quot; .</code></pre>
688760 <p>A procedure without parameters may leave out the parentheses, both where it is declared (<code>proc main*;</code>) and where it is called (<code>Out.ln</code>). Used without parentheses in an expression, a procedure is a procedure value, not a call.</p>
689761 <p><strong>Parameters.</strong> A value parameter is a copy: the procedure may change it, and the caller’s variable stays as it was. A <code>var</code> parameter is the caller’s variable itself; its argument must be a variable of exactly the parameter’s type.</p>
690-<p>A <code>const</code> parameter is the caller’s variable too, read in place, whatever its size, with nothing copied; the procedure may only read it. It may not assign it or a part of it (a field, an element of an array in it), give it or a part of it to a <code>var</code> parameter, take the <code>ADR</code> of either, write it through a view (section 6.9), or assign the names a <code>match</code> arm gives its fields. What a slice or a pointer in it points at isn’t part of it: the procedure may write through them, as it may through a value parameter’s; and a slice it makes of an array in it, to read, is a slice like any other, which writes the caller’s array if written through. <code>const</code> keeps the procedure from changing the variable by its name, not every path to its bytes. Its argument is any expression of the parameter’s type: a variable or a constant is passed in place, anything else as a temporary of the statement, as an array a call returns is (below). If the variable changes while the procedure runs, through a <code>var</code> parameter, a pointer or another thread, the procedure sees the change.</p>
762+<p>A <code>const</code> parameter is the caller’s variable too, read in place, whatever its size, with nothing copied; the procedure may only read it. It may not assign it or a part of it (a field, an element of an array in it), give it or a part of it to a <code>var</code> parameter, take the <code>SYSTEM.ADR</code> of either, write it through a view (section 6.9), or assign the names a <code>match</code> arm gives its fields. What a slice or a pointer in it points at isn’t part of it: the procedure may write through them, as it may through a value parameter’s; but a slice it makes of an array in it is a <code>[]const T</code> (section 5.3), so it can’t write the caller’s array through it. <code>const</code> keeps the procedure from changing the variable, not what its slices and pointers point at. Its argument is any expression of the parameter’s type: a variable or a constant is passed in place, anything else as a temporary, of the procedure if it holds an array, as an array a call returns is (below). If the variable changes while the procedure runs, through a <code>var</code> parameter, a pointer or another thread, the procedure sees the change.</p>
691763 <pre><code>type Parser = record args: [][]U8; at: INT; (* ... *) end;
692764
693765 proc current(const p: Parser): []U8; (* reads p, many words, in place *)
@@ -701,9 +773,9 @@
701773 end;</code></pre>
702774 <p>A procedure takes what it reads as a value when it is a single value or a slice, and as <code>const</code> when it is a record or an array; what it changes, as <code>var</code>; and a pointer only for what it keeps past the call, what may be <code>nil</code>, an allocator, or what C or the system takes.</p>
703775 <p><strong>Results.</strong> A procedure returns nothing, one value, or several. A call that returns several values can only stand on the right of a multiple assignment, alone as a statement, or alone after <code>return</code> in a procedure whose results are of the same types, in the same order, which then returns them as they are: <code>return divmod(n, 2);</code>. A procedure that can fail has an <code>ERROR</code> as its last result (section 5.8), which <code>try</code> passes on (section 7.8).</p>
704-<p>An array a call returns lives to the end of the statement the call is in. A slice of it may be used there, indexed, passed or compared; one kept past the statement, in a variable or anywhere else, points at what is gone, and is the exception of section 10:</p>
705-<pre><code>s := f(&#39;a&#39;)[0..4]; (* f returns a [8]U8: s is undefined after this *)
706-Out.str(f(&#39;a&#39;)[0..4]); (* used in its statement: defined *)</code></pre>
776+<p>An array a call returns, or a record or variant holding one, is a hidden variable of the procedure the call is in, as an array literal used as a slice is (section 6.5), and lives to the end of that procedure’s call. A slice of it may be kept in a variable and used for the rest of the procedure; in a loop, the hidden variable holds what the latest call returned. Like any slice of the frame, it may not be returned (section 7.3):</p>
777+<pre><code>s := f(&#39;a&#39;)[0..4]; (* f returns a [8]U8: s is good to the end of the procedure *)
778+t := f(&#39;b&#39;)[0..4]; (* a hidden variable of its own: s is unchanged *)</code></pre>
707779 <pre><code>module Divide;
708780 import Out;
709781
@@ -723,7 +795,7 @@
723795 <p><strong>forward.</strong> <code>proc f(x: I64): I64; forward;</code> declares <code>f</code> so that it can be called before its body appears; the body comes later in the module, with the same signature, though its parameters may have other names; a <code>forward</code> with no body is an error.</p>
724796 <p>A name after a procedure’s <code>end</code>, if written, is the procedure’s own.</p>
725797 <p>Procedures don’t nest.</p>
726-<p><strong>Code procedures.</strong> A procedure whose body is <code>code</code> is machine code, written by hand in the target’s assembly language (appendix A): one instruction to each <code>;</code>, and labels for jumps, up to <code>end</code>:</p>
798+<p><strong>Code procedures.</strong> A procedure whose body is <code>code</code> is machine code, written by hand in the target’s assembly language (appendix A), in a module that imports <code>SYSTEM</code>: one instruction to each <code>;</code>, and labels for jumps, up to <code>end</code>:</p>
727799 <pre><code>proc rdtsc*: U64;
728800 code
729801 rdtsc;
@@ -740,15 +812,15 @@
740812 extern proc XOpenDisplay(name: ^U8): ^U8;
741813 extern proc printf(format: ^U8; ...): I32;
742814 extern var environ: ^^U8;</code></pre>
743-<p>An <code>extern</code> procedure or variable is found by its name in the C libraries the program links: the C library always, and each library named by <code>link</code>.</p>
815+<p>An <code>extern</code> procedure or variable is found by its name in the C libraries the program links, and is declared only in a module that imports <code>SYSTEM</code> (section 3), since C can reach any memory: the C library always, and each library named by <code>link</code>.</p>
744816 <p>An <code>extern</code> procedure’s parameters and result are integers, <code>BOOL</code>, <code>F32</code>, <code>F64</code>, pointers or procedures; a record goes to C by pointer, never by value. <code>...</code> accepts any number of further arguments of those types, and is allowed only in an <code>extern</code>. C takes no <code>float</code> there, so an <code>F32</code> given to <code>...</code> is an error: the program writes <code>F64(x)</code>. Past the parameters, a real constant goes as an <code>F64</code>, an integer constant as an <code>INT</code>, and a string literal as a pointer to its bytes followed by a zero byte.</p>
745817 <p>An <code>extern</code> variable is C’s variable itself: reading it reads C’s, assigning it writes C’s. It can be of any type, declared as C declares it, a record laid out as C lays it out (section 5.5); the compiler can’t check that it is. A C variable that is local to each thread, such as <code>errno</code>, can’t be reached this way; the C library has a procedure for it (<code>__errno_location</code>).</p>
746818 <p>A C union has no raz type. A program reads one as an array of words, and the member it wants through a pointer conversion:</p>
747819 <pre><code>var ev: [24]I64; (* XEvent: 192 bytes *)
748820 ...
749-XNextEvent(dpy, ADR(ev));
821+XNextEvent(dpy, SYSTEM.ADR(ev));
750822 if I32(ev[0]) = KeyPress then
751- key := ^XKeyEvent(ADR(ev));
823+ key := ^XKeyEvent(SYSTEM.ADR(ev));
752824 ...
753825 end;</code></pre>
754826 <p>Every raz procedure uses the System V calling convention, so a raz procedure whose parameters and result are of those types can be given to C as a callback.</p>
@@ -759,7 +831,7 @@
759831 proc exit*(status: INT);
760832 proc trace*; (* the calls under way, on standard error *)</code></pre>
761833 <p><code>Rt.trace</code> says on standard error the calls under way, from the one that calls it out, each with its variables, as a program that stops does.</p>
762-<p><strong>Defined behaviour.</strong> Every variable starts at zero, integers wrap, and indexing arrays and slices is checked, so a program’s behaviour is defined by this report, with one exception: memory reached other than through a checked variable. Dereferencing a stale or made-up pointer, indexing through a pointer, a slice kept after the call whose frame holds what it points at (section 7.3), a slice of an array a call returned, or of a temporary given to a <code>const</code> parameter, kept past its statement (section 8), an index out of range in a module that says <code>check off;</code>, writing a string’s bytes, a code procedure that breaks its rules, and C code can read or write any memory, and what follows isn’t defined. Dereferencing <code>nil</code>, dividing by zero, calling a <code>nil</code> procedure and running out of stack stop the program, on every target, with status 2, as a failed check does. Before it stops, the program prints to standard error where it was, as file, line and column, and why, as far as it can tell: a <code>nil</code> pointer, a <code>nil</code> procedure, a stack overflow, a bad address or a division fault; then the calls under way, each with its variables of a basic, enumeration, set or pointer type. Whether the machine catches these or the compiled code checks for them is the implementation’s choice (appendix A); that they stop the program isn’t.</p>
834+<p><strong>Defined behaviour.</strong> Every variable starts at zero, integers wrap, and indexing arrays and slices is checked, so a program’s behaviour is defined by this report, with one exception: memory reached other than through a checked variable. In any module, three things reach it: a pointer used after the memory it points at was given back (by <code>Heap</code>’s <code>free</code> or an arena’s reset, say), a slice kept after the call whose frame holds what it points at (section 7.3), and a race on a variable that holds a pointer, a slice or a variant (section 7.6). In a module that imports <code>SYSTEM</code>, so does what that module does there (section 3): a made-up pointer, indexing through a pointer, an index out of range under <code>check off;</code>, writing a string’s bytes through a <code>^U8</code>, a code procedure that breaks its rules, and C code. Each can read or write any memory, and what follows isn’t defined. Dereferencing <code>nil</code>, dividing by zero, calling a <code>nil</code> procedure and running out of stack stop the program, on every target, with status 2, as a failed check does. Before it stops, the program prints to standard error where it was, as file, line and column, and why, as far as it can tell: a <code>nil</code> pointer, a <code>nil</code> procedure, a stack overflow, a bad address or a division fault; then the calls under way, each with its variables of a basic, enumeration, set or pointer type. Whether the machine catches these or the compiled code checks for them is the implementation’s choice (appendix A); that they stop the program isn’t.</p>
763835 <p><strong>Separate compilation.</strong> An importer depends only on a module’s interface: what it exports, as an importer sees it, never its source or its code. An implementation that compiles modules on its own makes the same program as if every module were compiled together (appendix A).</p>
764836 <p><strong>Errors.</strong> A program that breaks a rule of this report that says it is an error isn’t a raz program, and an implementation must reject it: say what is wrong and make no executable. Such a rule is one whose breach would leave the program with no meaning this report can give: a mistake of types, a name that isn’t declared or is declared twice, an assignment to what is read-only, a call with the wrong number of arguments, a type name or a procedure with no result used as a value, type arguments given to a module that isn’t generic, or too many, an import that can’t be found, a procedure declared <code>forward</code> and never defined, a module that imports itself. A fault that is only a lint isn’t an error: a name declared and never used, or a result that a call standing on its own drops (<code>Sys.write(fd, s);</code>, where <code>_ := Sys.write(fd, s);</code> says it is meant). A program with one is valid, and an implementation may warn of it. The text of its messages, how many it reports and which warnings it gives are its own (appendix A).</p>
765837 <h1 id="versions">11. Versions</h1>
@@ -772,15 +844,16 @@
772844 <li><strong>Separate compilation.</strong> Whether modules are compiled on their own, and if so into what: the object file, its format, the form of the code in it, when one is out of date, and how an instance is named so that the program keeps one (section 10). This compiler compiles every module of a program together, and writes no object files.</li>
773845 <li><strong>Atomics.</strong> How <code>CAS</code>, <code>XCHG</code> and <code>XADD</code> are made sequentially consistent on a machine without atomic instructions (section 6.8): on one with a single processor, by holding off interrupts while they run, for one.</li>
774846 <li><strong>The calling convention.</strong> On x86-64 Linux it is System V (sections 8 and 9); elsewhere it is the C library’s own, so that <code>extern</code> procedures and callbacks work. How a procedure returns several results, and how its frame is laid out, are the implementation’s.</li>
775-<li><strong>What is the target’s.</strong> Code procedures (section 8) hold the target’s machine code, written in its assembly language, and exist only where the implementation says so; <code>SYSCALL</code> exists only on Linux targets (section 6.8). A record’s layout is the target’s C layout (section 5.5), and the order of a value’s bytes is the target’s (little-endian on x86-64).</li>
847+<li><strong>What is the target’s.</strong> Code procedures (section 8) hold the target’s machine code, written in its assembly language, and exist only where the implementation says so; <code>SYSTEM.SYSCALL</code> exists only on Linux targets (section 6.8). A record’s layout is the target’s C layout (section 5.5), and the order of a value’s bytes is the target’s (little-endian on x86-64).</li>
776848 <li><strong>The assembly language.</strong> How a code procedure’s instructions are written (section 8). On x86-64 it is Intel’s syntax, as NASM writes it, destination first. Which instructions it knows is the implementation’s; <code>db</code> gives any others.</li>
777849 <li><strong>Sizes in the library.</strong> How many bytes <code>Out</code> keeps before it writes them, <code>Sys.outSize</code> (the library volume): 4 KB on Linux, fewer on a small machine.</li>
778850 <li><strong>The executable.</strong> Its format, and the names and line numbers it carries for a debugger (section 10).</li>
779851 <li><strong>How a failure is caught.</strong> A <code>nil</code> pointer, a division by zero or a stack overflow may be caught by the machine or by checks in the compiled code; either way the program stops with its message (section 10).</li>
780852 <li><strong>Messages and warnings.</strong> The text of the compiler’s messages, how many errors it reports before it stops, and which warnings it gives, <code>-Werror</code> included (section 10). That a program that is an error in this report is rejected isn’t free; the words that say so are. So is what a program that fails says on standard error: conformance is judged by a program’s standard output and exit status, never by its standard error.</li>
853+<li><strong>Copies.</strong> A procedure’s frame may hold copies of its variables that the program can’t name: registers saved to it, temporaries of its statements, its value parameters, and arrays its calls return. An implementation may leave them where they are when the procedure returns, until the stack is used again. A wipe (section 7.6) reaches only the variables it is given. A secret is best kept in one variable, passed by <code>var</code> or <code>const</code> or by pointer, never by value, and wiped there.</li>
781854 <li><strong>The command line.</strong> How the compiler is run, its options, and where its library lives (sections 1 and 3).</li>
782855 </ul>
783-<p><strong>The core.</strong> A target may leave out what the system under it can’t give: <code>Sys</code> past writing, ending the program and getting memory, <code>Thread</code>, <code>link</code> and <code>extern</code>. What it must have, besides the whole language, reals aside, is the core of the library: <code>Rt</code>, <code>Out</code>, <code>Mem</code>, <code>Arena</code>, <code>Lock</code>, <code>Str</code> and <code>Buf</code>, and of <code>Sys</code> what they need. A program that keeps to the core runs on every target. The rest of the library (<code>Heap</code>, <code>Fmt</code>, <code>List</code>, <code>Map</code>, <code>Vec</code>, <code>Sort</code>, <code>Math</code>, <code>Files</code>, <code>Opt</code>, <code>Width</code> and the rest of <code>Sys</code>) is the standard library: a target may leave a module out, and one that has it gives what <code>doc/raz-lib.md</code> says. <code>conform/</code> tests the core only.</p>
856+<p><strong>The core.</strong> A target may leave out what the system under it can’t give: <code>Sys</code> past writing, ending the program and getting memory, <code>Thread</code>, <code>link</code> and <code>extern</code>. What it must have, besides the whole language, reals aside, is the core of the library: <code>Rt</code>, <code>Out</code>, <code>Mem</code>, <code>Arena</code>, <code>Lock</code>, <code>Str</code> and <code>Buf</code>, and of <code>Sys</code> what they need. A program that keeps to the core runs on every target. The rest of the library (<code>Heap</code>, <code>Fmt</code>, <code>List</code>, <code>Map</code>, <code>Vec</code>, <code>Sort</code>, <code>Math</code>, <code>Files</code>, <code>Opt</code>, <code>Width</code>, <code>Rand</code>, <code>Check</code>, <code>Prop</code> and the rest of <code>Sys</code>) is the standard library: a target may leave a module out, and one that has it gives what <code>doc/raz-lib.md</code> says. <code>conform/</code> tests the core only.</p>
784857 <h1 id="appendix-b.-the-grammar">Appendix B. The grammar</h1>
785858 <p>The whole syntax of raz, gathered from the sections above, with the symbols of section 2. <code>{ x }</code> is any number of <code>x</code>, <code>[ x ]</code> is <code>x</code> or nothing, and <code>( x | y )</code> is one of them.</p>
786859 <pre><code>ident = letter { letter | digit } .
@@ -816,7 +889,7 @@
816889
817890 Type = QualIdent
818891 | &quot;[&quot; Expr &quot;]&quot; Type
819- | &quot;[&quot; &quot;]&quot; Type
892+ | &quot;[&quot; &quot;]&quot; [ &quot;const&quot; ] Type
820893 | &quot;^&quot; [ &quot;VOLATILE&quot; ] Type
821894 | [ &quot;packed&quot; ] &quot;record&quot; { IdentList &quot;:&quot; Type &quot;;&quot; } &quot;end&quot;
822895 | &quot;variant&quot; { Case &quot;;&quot; } &quot;end&quot;
@@ -861,7 +934,7 @@
861934 | &quot;continue&quot;
862935 | &quot;defer&quot; Statement
863936 | &quot;try&quot; Designator .
864-Arm = ( ident [ &quot;(&quot; ident { &quot;,&quot; ident } &quot;)&quot; ]
937+Arm = ( ident { &quot;.&quot; ident } [ &quot;(&quot; ident { &quot;,&quot; ident } &quot;)&quot; ]
865938 | Label { &quot;,&quot; Label } | &quot;else&quot; ) &quot;:&quot; Statements .
866939 Label = Expr [ &quot;..&quot; Expr ] .
867940 Target = Designator | &quot;_&quot; .
@@ -880,7 +953,8 @@
880953 | &quot;var&quot; IdentDef &quot;:&quot; Type ) .</code></pre>
881954 <p><code>character</code> is any character but a line break, and in a string any but <code>"</code> and <code>\</code>, in a character any but <code>'</code> and <code>\</code>. <code>rawChar</code> is any character but <code>`</code>, line breaks included. A procedure argument of a generic module (section 3) is written as a <code>QualIdent</code>, which is a <code>Type</code> here. <code>...</code> comes last in a <code>Signature</code>, and only in an <code>extern</code>. An <code>Instruction</code> is written in the target’s assembly language (appendix A).</p>
882955 <h1 id="the-library-volume-2">The library, volume 2</h1>
883-<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.</p>
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+<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>
884958 <p>Modules written in raz, in the compiler’s <code>lib</code> directory:</p>
885959 <table>
886960 <thead>
@@ -892,11 +966,11 @@
892966 <tbody>
893967 <tr>
894968 <td><code>Rt</code></td>
895-<td>ending the program, <code>exit</code> and <code>atExit</code>; <code>stop</code>, which ends it with a message; <code>trace</code>, the calls under way; what the compiler’s checks call when they fail</td>
969+<td>ending the program, <code>exit</code>, <code>exitStatus</code> and <code>atExit</code>; <code>stop</code>, which ends it with a message; <code>trace</code>, the calls under way; what the compiler’s checks call when they fail</td>
896970 </tr>
897971 <tr>
898972 <td><code>Sys</code></td>
899-<td>system calls: <code>open</code>, <code>openMode</code>, <code>openRead</code>, <code>create</code>, <code>read</code>, <code>write</code>, <code>seek</code>, <code>position</code>, <code>truncate</code>, <code>sync</code>, <code>syncAll</code>, <code>lock</code>, <code>close</code>, <code>dup</code>, <code>dupTo</code>, <code>dupAbove</code>, <code>keepOnExec</code>, <code>mmap</code>, <code>munmap</code>, <code>isTerminal</code>, <code>yield</code>, <code>exit</code>; files and directories, <code>rename</code>, <code>remove</code>, <code>removeDir</code>, <code>makeDir</code>, <code>chmod</code>, <code>access</code>, <code>umask</code>, <code>symlink</code>, <code>readLink</code>, <code>info</code>, <code>linkInfo</code>, <code>infoOf</code>, <code>openDir</code>, <code>nextEntry</code>, <code>entryKind</code>, <code>closeDir</code>; the terminal, <code>rawMode</code>, <code>restoreMode</code>, <code>getMode</code>, <code>setMode</code>, <code>termSize</code>, <code>ready</code>; the environment, <code>env</code>, <code>envAt</code>, <code>setEnv</code>, <code>workDir</code>, <code>setWorkDir</code>; programs, <code>spawn</code>, <code>exec</code> and <code>execve</code>, pipes and signals; the user, the machine, time used and limits; time, <code>clock</code>, <code>now</code>, <code>sleep</code>; <code>random</code>; errors, by name, and what one means, <code>errorText</code>; sockets, among them datagrams served and files passed; and memory shared through a file. One file per target, and <code>Sys.raz</code> over the C library for the others, which takes what each C library fixes from <code>CAbi</code></td>
973+<td>system calls: <code>open</code>, <code>openMode</code>, <code>openRead</code>, <code>create</code>, <code>read</code>, <code>write</code>, <code>seek</code>, <code>position</code>, <code>truncate</code>, <code>sync</code>, <code>syncAll</code>, <code>lock</code>, <code>lockShared</code>, <code>close</code>, <code>closeChecked</code>, <code>dup</code>, <code>dupTo</code>, <code>dupAbove</code>, <code>keepOnExec</code>, <code>mmap</code>, <code>munmap</code>, <code>mapRead</code>, <code>protect</code>, <code>isTerminal</code>, <code>yield</code>, <code>exit</code>; files and directories, <code>rename</code>, <code>remove</code>, <code>removeDir</code>, <code>makeDir</code>, <code>makeDirMode</code>, <code>chmod</code>, <code>setTime</code>, <code>access</code>, <code>umask</code>, <code>symlink</code>, <code>readLink</code>, <code>info</code>, <code>linkInfo</code>, <code>infoOf</code>, <code>openDir</code>, <code>nextEntry</code>, <code>entryKind</code>, <code>closeDir</code>; the terminal, <code>rawMode</code>, <code>restoreMode</code>, <code>getMode</code>, <code>setMode</code>, <code>termSize</code>, <code>ready</code>; the environment, <code>env</code>, <code>findEnv</code>, <code>envAt</code>, <code>setEnv</code>, <code>workDir</code>, <code>setWorkDir</code>; programs, <code>spawn</code>, <code>exec</code> and <code>execve</code>, pipes and signals; the user, the machine, time used and limits; time, <code>clock</code>, <code>now</code>, <code>sleep</code>; <code>random</code>; errors, by name, and what one means, <code>errorText</code>; sockets, among them datagrams served and files passed; and memory shared through a file. One file per target, and <code>Sys.raz</code> over the C library for the others, which takes what each C library fixes from <code>CAbi</code></td>
900974 </tr>
901975 <tr>
902976 <td><code>Out</code></td>
@@ -913,6 +987,14 @@
913987 <tr>
914988 <td><code>Thread</code></td>
915989 <td>threads, <code>spawn</code>, <code>join</code> and <code>signal</code>, and <code>Mutex</code>, in any program; in one that uses C, made by C’s <code>pthread_create</code></td>
990+</tr>
991+<tr>
992+<td><code>Task</code></td>
993+<td>a thread given a value of its own, <code>Task(T)</code>: <code>spawn</code> copies it, with no cast</td>
994+</tr>
995+<tr>
996+<td><code>Guarded</code></td>
997+<td>a value with its lock, <code>Guarded(T)</code>: reached only through <code>with</code></td>
916998 </tr>
917999 <tr>
9181000 <td><code>Mem</code></td>
@@ -920,7 +1002,7 @@
9201002 </tr>
9211003 <tr>
9221004 <td><code>Heap</code></td>
923-<td>a general allocator, <code>Heap.heap</code>, safe to call from several threads; freeing a block twice, or one it never gave out, stops the program</td>
1005+<td>a general allocator, <code>Heap.heap</code>, safe to call from several threads; freeing a block twice, or one it never gave out, stops the program; <code>RAZ_HEAP=debug</code> finds writes past a block’s end and after free</td>
9241006 </tr>
9251007 <tr>
9261008 <td><code>Arena</code></td>
@@ -973,6 +1055,18 @@
9731055 <tr>
9741056 <td><code>Width</code></td>
9751057 <td>how many columns of a terminal a character, or a string, takes: 0, 1 or 2, as <code>wcwidth</code> says</td>
1058+</tr>
1059+<tr>
1060+<td><code>Rand</code></td>
1061+<td>a seeded random number generator, splitmix64: the same seed, the same numbers; for tests and simulations, not secrets</td>
1062+</tr>
1063+<tr>
1064+<td><code>Check</code></td>
1065+<td>checks for a test: <code>ok</code>, <code>str</code> and <code>int</code> say what failed on standard error, and the program ends with status 1 if any did</td>
1066+</tr>
1067+<tr>
1068+<td><code>Prop</code></td>
1069+<td>property tests and fuzzing: a property draws values, a failing case is shrunk and saved, and run first next time</td>
9761070 </tr>
9771071 </tbody>
9781072 </table>
@@ -982,9 +1076,10 @@
9821076 const Full*: ERROR = &quot;too many procedures to run at exit&quot;;
9831077 proc atExit*(p: proc): ERROR; (* p run when the program ends by exit *)
9841078 proc exit*(status: INT); (* the atExit procedures, the last first, then the end *)
1079+proc exitStatus*: INT; (* the status exit was given, in an atExit procedure; 0 before *)
9851080 proc stop*(msg: []U8); (* msg, and a line feed, on standard error, then exit with status 2 *)
9861081 proc trace*; (* the calls under way, and their variables, on standard error *)</code></pre>
987-<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.</p>
1082+<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>
9881083 <pre><code>module Mem;
9891084 type
9901085 Allocator* = record
@@ -998,7 +1093,9 @@
9981093 proc free*(a: ^Allocator; p: ^U8); (* a^.free(a, p) *)
9991094
10001095 module Heap;
1096+proc debug*(on: BOOL); (* debug mode on or off, for the blocks given from now on *)
10011097 var heap*: ^Mem.Allocator;
1098+var sure*: ^Mem.Allocator; (* heap, but running out of memory stops the program *)
10021099
10031100 module Arena;
10041101 type Arena* = record
@@ -1006,9 +1103,12 @@
10061103 ...
10071104 end;
10081105 proc init*(var ar: Arena; size: INT): BOOL; (* an arena of size bytes; false if the system has none *)
1106+proc mark*(const ar: Arena): INT; (* where ar stands now, for release *)
1107+proc release*(var ar: Arena; m: INT); (* all allocated since mark m freed; a mark past where it stands stops the program *)
10091108 proc reset*(var ar: Arena); (* every allocation forgotten; the memory used again *)
10101109 proc done*(var ar: Arena); (* its memory given back *)</code></pre>
1011-<p><code>NEW(p, a)</code> calls <code>a^.alloc</code>. <code>Heap</code> is the general allocator. An arena hands out memory from one block: its <code>free</code> does nothing, and <code>reset</code> forgets everything.</p>
1110+<p><code>NEW(p, a)</code> calls <code>a^.alloc</code>. <code>Heap</code> is the general allocator; <code>Heap.heap</code> gives <code>nil</code> when memory runs out, and <code>Heap.sure</code> stops the program instead, as <code>Buf</code> does, so that <code>NEW(p, Heap.sure)</code> needs no check.</p>
1111+<p><code>Heap</code> has a debug mode, a light AddressSanitizer, chosen by <code>RAZ_HEAP=debug</code> in the environment or by <code>Heap.debug(true)</code>. Normal mode pays nothing for it. In debug mode each block given has 8 guard bytes after its last, and a freed block is filled with <code>0DEH</code> bytes and held in a quarantine, the last 1,024 frees, before it may be given again. A broken guard, a write past the block’s end, is found when the block is freed; a block that leaves the quarantine with a byte not <code>0DEH</code>, a write after free, is found then. Either stops the program, saying what was found and the block’s address and size, with the calls under way, and status 2. A big block, which has pages of its own, is not quarantined: its guard is checked and its pages unmapped, so a later use of it faults. <code>debug</code> changes only the blocks given after it, so call it before the first allocation; a block is freed as it was given, in either mode. Only writes are found, not reads, and not those past the guard or after the quarantine. An arena hands out memory from one block: its <code>free</code> does nothing, and <code>reset</code> forgets everything.</p>
10121112 <pre><code>module Lock;
10131113 type Lock* = record ... end;
10141114 proc take*(var l: Lock);
@@ -1023,7 +1123,16 @@
10231123 proc join*(t: ^Thread); (* waits for t to end, then frees its stack *)
10241124 proc signal*(t: ^Thread; sig: INT): INT; (* sig sent to t alone: 0, or minus an errno *)
10251125 proc lock*(var m: Mutex);
1026-proc unlock*(var m: Mutex);</code></pre>
1126+proc unlock*(var m: Mutex);
1127+
1128+module Task(T);
1129+proc spawn*(p: proc(var x: T); x: T): (^Thread.Thread, ERROR); (* p run on a copy of x in a new thread *)
1130+
1131+module Guarded(T);
1132+type Guarded* = record ... end; (* a value and its Thread.Mutex; zero is ready *)
1133+proc with*(var g: Guarded; p: proc(var x: T)); (* p run on the value, under the lock *)</code></pre>
1134+<p><code>Task(T)</code> starts a thread without a cast: <code>import Jobs := Task(Job); t, e := Jobs.spawn(work, job);</code> is the safe form of <code>Thread.spawn(work, ^U8(ADR(job)))</code>. <code>spawn</code> copies <code>x</code> into a block from <code>Heap</code> before it returns, so the caller may change or drop its <code>x</code> at once. The thread owns the copy: <code>work</code> changes it freely, and the thread frees it when <code>work</code> returns. The thread is joined with <code>Thread.join</code>; <code>Thread.Failed</code> comes back if there is no memory for the copy or the thread. A result goes back through a pointer in <code>x</code>, read after <code>join</code>.</p>
1135+<p><code>Guarded(T)</code> is a <code>T</code> with its <code>Thread.Mutex</code>, for sharing a container between threads: the value is reached only through <code>with</code>, which holds the lock while <code>p</code> runs. A zeroed <code>Guarded</code> is ready. <code>p</code> must not call <code>with</code> on the same <code>Guarded</code>, nor keep a pointer into its <code>x</code>.</p>
10271136 <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>
10281137 <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>
10291138 <pre><code>module Out;
@@ -1068,16 +1177,17 @@
10681177 errNotDir* = -20; errIsDir* = -21; errInvalid* = -22;
10691178 errNotTerminal* = -25; errNoSpace* = -28; errPipe* = -32;
10701179 errNameTooLong* = -36; errNotEmpty* = -39; errLoop* = -40;
1071- errAddrInUse* = -98; errNetUnreach* = -101; errConnReset* = -104;
1180+ errAddrInUse* = -98; errNetUnreach* = -101; errConnAborted* = -103; errConnReset* = -104;
10721181 errTimedOut* = -110; errRefused* = -111; errHostUnreach* = -113;
10731182 errInProgress* = -115;
10741183
10751184 type
1076- Access* = (reading, writing, creating, exclusive, truncating, appending, executing);
1185+ Access* = (reading, writing, creating, exclusive, truncating, appending, executing, nonblocking);
10771186 Accesses* = set of Access;
10781187 Info* = record
10791188 size*: I64; (* in bytes *)
10801189 mtime*: I64; (* when its contents last changed, in nanoseconds since 1970 *)
1190+ seconds*: I64; (* mtime in whole seconds, rounded down: exact past 2262 and before 1677, where mtime wraps *)
10811191 mode*: INT; (* its permission bits, as chmod takes them: 1EDH is rwxr-xr-x *)
10821192 dir*: BOOL; (* a directory *)
10831193 file*: BOOL; (* a regular file: not a directory, device, pipe or socket *)
@@ -1085,6 +1195,9 @@
10851195 ino*: U64; (* its inode, which a rename keeps and a new file doesn&#39;t *)
10861196 dev*: U64; (* the file system it is on; with ino, it names the file *)
10871197 links*: U64; (* how many names it has: 1 for a file no hard link shares *)
1198+ special*: INT; (* its set-user-id, set-group-id and sticky bits, as chmod takes them: 800H, 400H, 200H *)
1199+ uid*: INT; (* its owner&#39;s user number *)
1200+ gid*: INT; (* its group&#39;s number *)
10881201 end;
10891202 Kind* = (unknown, isFile, isDir, isLink, isOther); (* what a directory says a name in it is *)
10901203 Dir* = record ... end; (* an open directory; its fields are the target&#39;s *)
@@ -1099,14 +1212,27 @@
10991212 foreground*: BOOL; (* its group takes the terminal on standard input *)
11001213 tty*: INT; (* a pseudo-terminal&#39;s fd, to start a session on; 0 for none *)
11011214 more*: []INT; (* fds above 2 passed on to it, at the same numbers *)
1215+ keepIgnored*: BOOL; (* signals this program ignores stay ignored in it *)
11021216 end;
11031217 Status* = (running, exited, killed, stopped);
11041218 Mode* = record ... end; (* a terminal&#39;s modes; its fields are the target&#39;s *)
11051219 Peer* = record ... end; (* whom a datagram came from *)
1220+ Remote* = record (* who is at the other end of a connected socket *)
1221+ unix*: BOOL; (* a Unix socket: uid and pid say who *)
1222+ ipv6*: BOOL; (* addr is 16 bytes of IPv6, else 4 bytes of IPv4 then zeros *)
1223+ addr*: [16]U8;
1224+ port*: INT;
1225+ uid*, pid*: INT;
1226+ end;
1227+ Poll* = record (* a descriptor for poll to wait on *)
1228+ fd*: INT;
1229+ read*, write*: BOOL; (* wait until it may be read, written *)
1230+ ready*: BOOL; (* set by poll *)
1231+ end;
11061232
11071233 proc write*(fd: INT; s: []U8): INT; (* the bytes written *)
11081234 proc read*(fd: INT; buf: []U8): INT; (* the bytes read; 0 at the end *)
1109-proc open*(path: []U8; how: Accesses): INT; (* its fd *)
1235+proc open*(path: []U8; how: Accesses): INT; (* its fd, closed on exec *)
11101236 proc openMode*(path: []U8; how: Accesses; mode: INT): INT; (* open, a file it makes given mode *)
11111237 proc openRead*(path: []U8): INT; (* open(path, {reading}) *)
11121238 proc create*(path: []U8): INT; (* open(path, {writing, creating, truncating}) *)
@@ -1116,12 +1242,16 @@
11161242 proc sync*(fd: INT): INT; (* what was written to it, on the disk *)
11171243 proc syncAll*(fd: INT): INT; (* all that was written to its file system, on the disk *)
11181244 proc lock*(fd: INT; wait: BOOL): INT; (* the file&#39;s lock, held until fd is closed *)
1245+proc lockShared*(fd: INT; wait: BOOL): INT; (* lock, shared with others that ask it so *)
11191246 proc close*(fd: INT);
1247+proc closeChecked*(fd: INT): INT; (* close, and 0 or minus the errno it failed with *)
11201248 proc dup*(fd: INT): INT; (* a new fd for the same file, the lowest free *)
11211249 proc dupTo*(fd, into: INT): INT; (* into made the same file as fd, closing what it was *)
11221250 proc dupAbove*(fd, least: INT): INT; (* a new fd for the same file, the lowest free from least on, closed on exec *)
1123-proc keepOnExec*(fd: INT; yes: BOOL): INT; (* fd left open, or not, in the program exec runs *)
1251+proc keepOnExec*(fd: INT; yes: BOOL): INT; (* fd left open, or not (as at first for a file made by open or pipe), in the program exec runs *)
11241252 proc isTerminal*(fd: INT): BOOL;
1253+proc mapRead*(fd, size: INT): ^U8; (* the first size bytes of fd, which may be open only to read, mapped read only; or nil *)
1254+proc protect*(p: ^U8; size: INT; read, write: BOOL): INT; (* the pages at p readable and writable as asked; neither, and touching them stops the program *)
11251255 proc mmap*(size: INT): ^U8; (* size bytes of zeroed memory, in whole pages; or nil *)
11261256 proc munmap*(p: ^U8; size: INT);
11271257 proc yield*; (* the processor given to another thread *)
@@ -1131,7 +1261,11 @@
11311261 proc remove*(path: []U8): INT; (* a file, not a directory *)
11321262 proc removeDir*(path: []U8): INT; (* an empty directory *)
11331263 proc makeDir*(path: []U8): INT; (* readable by all, writable by its owner *)
1264+proc makeDirMode*(path: []U8; mode: INT): INT; (* makeDir, given mode *)
11341265 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 *)
11351269 proc access*(path: []U8; how: Accesses): INT; (* 0 if this user may read, write or execute it *)
11361270 proc umask*(mask: INT): INT; (* the bits taken from every file made; the mask it was *)
11371271 proc symlink*(target, path: []U8): INT; (* path made a link to target *)
@@ -1154,11 +1288,14 @@
11541288 proc ready*(fd, ms: INT): BOOL; (* something to read within ms, or ever if ms &lt; 0 *)
11551289 proc readyOf*(fds: []INT; ms: INT): INT; (* the first of fds with something to read within ms, or -1 *)
11561290 proc readyFor*(reads, writes: []INT; ms: INT): INT; (* readyOf, and the first of writes with room to write *)
1291+proc poll*(var fds: []Poll; ms: INT): INT; (* sets each fd&#39;s ready, as each read and write say; the number ready, 0 on a timeout, or minus an errno *)
11571292 proc setBlocking*(fd: INT; yes: BOOL): INT; (* if not yes, a read or write that would wait is -11 at once *)
1293+proc blocking*(fd: INT): INT; (* 1 if reads and writes wait, 0 if they are -11 at once, or minus an errno *)
11581294
11591295 proc errorText*(err: INT): []U8; (* what errno err, or minus it, means, as C&#39;s strerror says *)
11601296
11611297 proc env*(name: []U8): []U8; (* the environment variable&#39;s value; &quot;&quot; if unset *)
1298+proc findEnv*(name: []U8): ([]U8, BOOL); (* the value and true; &quot;&quot; and false if unset, &quot;&quot; and true if empty *)
11621299 proc envAt*(i: INT): ([]U8, BOOL); (* the environment&#39;s ith NAME=value, from 0; false past the last *)
11631300 proc setEnv*(name, value: []U8): INT; (* name set to value, for env and the programs started *)
11641301 proc workDir*: ([]U8, INT); (* the working directory, good until the next call *)
@@ -1171,7 +1308,9 @@
11711308 proc exec*(p: Program): INT; (* p run in this program&#39;s place; returns only if it can&#39;t *)
11721309 proc execve*(path: []U8; args, env: [][]U8): INT; (* as the kernel&#39;s execve: no search *)
11731310 proc fork*: INT; (* this program copied: 0 in the copy, its process id in this one *)
1311+proc setsid*: INT; (* a session of its own, led by this program: its process id, or minus an errno *)
11741312 proc wait*(pid: INT; block: BOOL): (INT, Status, INT); (* which program, how, and its code *)
1313+proc waitExit*(pid: INT): (INT, Status, INT); (* as wait, but only for a program to end *)
11751314 proc kill*(pid, signal: INT): INT;
11761315 proc pid*: INT; (* this program&#39;s process id *)
11771316 proc group*: INT; (* the process group this program is in *)
@@ -1181,6 +1320,7 @@
11811320 proc uid*: INT; (* the user this program runs as *)
11821321 proc euid*: INT; (* the user whose rights it has: 0 is root *)
11831322 proc hostName*: ([]U8, INT); (* this machine&#39;s name, good until the next call *)
1323+proc processors*: INT; (* the processors this program may run on, at least 1 *)
11841324 proc cpuTime*(children: BOOL): (I64, I64); (* ns used in the program and in the system; or by its children *)
11851325 proc limit*(which: INT): (I64, I64, INT); (* the soft limit and the hard; -1 for none *)
11861326 proc setLimit*(which: INT; soft, hard: I64): INT;
@@ -1188,13 +1328,14 @@
11881328 proc catch*(sigs: []INT): INT; (* sigs caught: an fd to wait on *)
11891329 proc caught*(fd: INT): INT; (* the next signal caught, or 0 if none is waiting *)
11901330 proc ignore*(sig: INT; yes: BOOL): INT; (* sig ignored; or, if not yes, back to what it does at first *)
1331+proc ignored*(sig: INT): BOOL; (* whether sig does nothing now *)
11911332 proc handle*(sig: INT): INT; (* sig noted, for came; a system call it comes during is -4 *)
11921333 proc came*(sig: INT): BOOL; (* whether sig came since the last time it was asked *)
11931334
11941335 proc clock*: I64; (* nanoseconds, on a clock that only goes forward *)
11951336 proc now*: I64; (* nanoseconds since 1970-01-01, UTC *)
11961337 proc sleep*(ms: INT);
1197-proc random*(b: []U8); (* b filled with random bytes *)
1338+proc random*(b: []U8); (* b filled with random bytes; stops the program if it can&#39;t be *)
11981339
11991340 proc connect*(path: []U8): INT; (* a stream socket, connected to the Unix socket at path: its fd *)
12001341 proc connectTcp*(addr: []U8; port: INT): INT; (* addr an IPv4 or IPv6 address, as numbers *)
@@ -1203,11 +1344,15 @@
12031344 proc listenTcp*(addr: []U8; port: INT): INT;
12041345 proc accept*(fd: INT): INT; (* the next connection, waiting for one *)
12051346 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 *)
12061348 proc connectTcpWithin*(addr: []U8; port, ms: INT): INT; (* connectTcp, or -110 (ETIMEDOUT) after ms *)
12071349 proc listenUdp*(addr: []U8; port: INT): INT; (* a socket of datagrams on port at addr *)
12081350 proc receive*(fd: INT; buf: []U8): (INT, Peer); (* the next datagram, in buf, and who sent it *)
12091351 proc reply*(fd: INT; peer: Peer; s: []U8): INT; (* s, as one datagram, to peer *)
12101352 proc localPort*(fd: INT): INT; (* the port the socket fd is bound to *)
1353+proc closeWrite*(fd: INT): INT; (* no more writing to the socket fd; the other end reads an end *)
1354+proc peer*(fd: INT): (Remote, INT); (* who is at the other end of the connected socket fd *)
1355+proc socketPair*: (INT, INT, INT); (* two connected stream sockets, and 0 *)
12111356 proc sendFds*(fd: INT; s: []U8; fds: []INT): INT; (* send, and with it fds, to a Unix socket *)
12121357 proc receiveFds*(fd: INT; buf: []U8; fds: []INT): (INT, INT); (* read, and the fds that came with it *)
12131358
@@ -1215,21 +1360,24 @@
12151360 proc mapFile*(fd, size: INT): ^U8; (* the first size bytes of fd, shared; munmap gives them back; or nil *)</code></pre>
12161361 <p><code>info</code> follows a symbolic link: <code>size</code>, <code>dir</code> and <code>file</code> are those of what it names, and <code>symlink</code> says the path was a link, so that a program walking a tree can choose not to follow one back up it. A link to nothing is <code>-ENOENT</code>, as a missing file is. <code>file</code> lets a walk open only what holds data: opening a pipe would wait for a program to write to it.</p>
12171362 <p><code>nextEntry</code> gives every name in the directory except <code>.</code> and <code>..</code>, in no particular order. <code>entryKind</code> says what the name it gave last is, a file, a directory, a symbolic link (whatever it names) or something else, as the directory itself records it, so that a program walking a tree needn’t ask <code>linkInfo</code> of every name, a system call each; where the file system doesn’t record it, it is <code>unknown</code>, and <code>linkInfo</code> must be asked. <code>rawMode</code> turns off what the terminal does with keys before the program reads them (echo, line editing, signals, flow control), and NL to CR NL on output. A program that turns it on restores it before it ends: <code>Rt.atExit</code> is the place.</p>
1218-<p><strong>Files.</strong> <code>open</code> gives a file for what <code>how</code> asks: <code>reading</code>, <code>writing</code> or both; <code>creating</code> it if it is missing, readable by all and writable by its owner; with <code>exclusive</code>, failing with -17 (EEXIST) if it is there, as a program does that makes a lock or a file of its own; <code>truncating</code> it to nothing; <code>appending</code>, each write going to its end, however other programs write it too. <code>openMode</code> is <code>open</code> that gives a file it makes the bits <code>mode</code>, less those the umask takes away: 1EDH (755) for a program, 124H (444) for a file never to be written; <code>open</code> gives 1A4H (644). <code>seek</code> moves to <code>pos</code> from the start of the file, or from its end if <code>fromEnd</code>, where a negative <code>pos</code> reads back from the end; <code>position</code> says where the file is, without moving. <code>truncate</code> makes the file <code>size</code> bytes long, cutting it or adding zeros, and leaves the position where it was. <code>sync</code> returns once what was written to the file is on the disk, as a program needs before it says a file is saved; <code>syncAll</code> returns once everything written to the file system that holds the file is on the disk, at once rather than a file at a time, and may flush other file systems too. <code>lock</code> takes the file’s lock, which one open file holds at a time: another that asks for it waits if <code>wait</code>, and otherwise fails with -11 (EAGAIN). The lock goes when the file is closed or the program ends, however it ends, so it never goes stale as a file made with <code>exclusive</code> does. <code>infoOf</code> is <code>info</code> of an open file: its size is what it holds now, however it was written; <code>linkInfo</code> is <code>info</code> of a symbolic link itself, not what it names, so that <code>symlink</code> is true, <code>dir</code> and <code>file</code> false, and <code>size</code> the length of what it names, which need not be there. <code>Info.mtime</code> is what a program checks to see whether a file changed under it, and <code>Info.ino</code> whether it is still the same file, not one renamed over it; 0 on a target whose files have none. <code>Info.dev</code> is the file system the file is on: two files are one only if both <code>dev</code> and <code>ino</code> agree, which is how a program following links finds that one has led it back into a directory it is already in. <code>Info.links</code> is how many names the file has: more than 1 if a hard link shares it, so that writing to it writes to a file named elsewhere too; 0 on a target that doesn’t say. <code>Info.mode</code> holds the nine bits of <code>rwxrwxrwx</code>, and <code>chmod</code> sets them, 1EDH (755) for a program, 1A4H (644) for data.</p>
1363+<p><strong>Files.</strong> <code>open</code> gives a file for what <code>how</code> asks: <code>reading</code>, <code>writing</code> or both; <code>creating</code> it if it is missing, readable by all and writable by its owner; with <code>exclusive</code>, failing with -17 (EEXIST) if it is there, as a program does that makes a lock or a file of its own; <code>truncating</code> it to nothing; <code>appending</code>, each write going to its end, however other programs write it too. <code>openMode</code> is <code>open</code> that gives a file it makes the bits <code>mode</code>, less those the umask takes away: 1EDH (755) for a program, 124H (444) for a file never to be written; <code>open</code> gives 1A4H (644). <code>seek</code> moves to <code>pos</code> from the start of the file, or from its end if <code>fromEnd</code>, where a negative <code>pos</code> reads back from the end; <code>position</code> says where the file is, without moving. <code>truncate</code> makes the file <code>size</code> bytes long, cutting it or adding zeros, and leaves the position where it was. <code>sync</code> returns once what was written to the file is on the disk, as a program needs before it says a file is saved; <code>syncAll</code> returns once everything written to the file system that holds the file is on the disk, at once rather than a file at a time, and may flush other file systems too. <code>lock</code> takes the file’s lock for one open file alone: another that asks for it waits if <code>wait</code>, and otherwise fails with -11 (EAGAIN). The lock goes when the file is closed or the program ends, however it ends, so it never goes stale as a file made with <code>exclusive</code> does. <code>lockShared</code> takes it shared, as a program that only reads asks it: any number may hold it so at once, and <code>lock</code> waits, or fails, until the last has let it go, as a shared one waits for the one that <code>lock</code> took. Both are <code>flock</code>’s, so they agree with any other program that takes the file’s lock so. <code>closeChecked</code> closes as <code>close</code> does, and says if that failed: a file system may say only then that what was written never reached it, a full disk or a quota over NFS, so a program that must know its writes landed, as one that copies a backup, asks it. The fd is closed either way, so a failed <code>closeChecked</code>, -4 (EINTR) too, is never tried again. <code>infoOf</code> is <code>info</code> of an open file: its size is what it holds now, however it was written; <code>linkInfo</code> is <code>info</code> of a symbolic link itself, not what it names, so that <code>symlink</code> is true, <code>dir</code> and <code>file</code> false, and <code>size</code> the length of what it names, which need not be there. <code>Info.mtime</code> is what a program checks to see whether a file changed under it, and <code>Info.ino</code> whether it is still the same file, not one renamed over it; 0 on a target whose files have none. <code>Info.dev</code> is the file system the file is on: two files are one only if both <code>dev</code> and <code>ino</code> agree, which is how a program following links finds that one has led it back into a directory it is already in. <code>Info.links</code> is how many names the file has: more than 1 if a hard link shares it, so that writing to it writes to a file named elsewhere too; 0 on a target that doesn’t say. <code>Info.mode</code> holds the nine bits of <code>rwxrwxrwx</code>, and <code>chmod</code> sets them, 1EDH (755) for a program, 1A4H (644) for data; <code>Info.special</code> holds the three above them, set-user-id 800H, set-group-id 400H and sticky 200H, which <code>chmod</code> sets too, with <code>mode | special</code>, as a program that copies a file keeps them. <code>Info.uid</code> and <code>Info.gid</code> are the owner’s and the group’s numbers; <code>chown</code> sets them, with -1 for one to leave as it is, so that a program that saves a file by writing another and renaming it over the first can give it the first’s owner (only root may give a file to another user). <code>makeFifo</code> makes a named pipe, given the bits <code>mode</code> as <code>makeDirMode</code> is; <code>Info</code> of one has <code>file</code> and <code>dir</code> false, and <code>open</code> with <code>nonblocking</code> doesn’t wait for its other end. <code>makeDirMode</code> is <code>makeDir</code> that gives the directory the bits <code>mode</code>, less those the umask takes away, as <code>openMode</code> gives a file: 1C0H (700) for one only its owner may enter. Of the three above them, Linux gives a new directory only sticky; it takes set-group-id from the directory it is made in. <code>setTime</code> sets the file’s time, following a symbolic link, and the time it was last read with it, as a program that copies or restores a file keeps its time, so that a later run sees it unchanged: <code>sec</code> seconds and <code>nsec</code>, from 0 to 999999999, nanoseconds since 1970. <code>Info.mtime</code> holds the time in nanoseconds, which an <code>I64</code> holds from 1677 to 2262 only; <code>Info.seconds</code> holds it in whole seconds, rounded down, whatever it is, so a program that copies times exactly takes <code>Info.seconds</code>, and the nanoseconds as <code>mtime - seconds * 1000000000</code>, which is right in any year, since the wrapping cancels out (section 6.1).</p>
12191364 <p><strong>Programs.</strong> <code>openPty</code> makes a pseudo-terminal, a terminal that a program, a terminal emulator, plays the screen and the keyboard of: what it writes to the fd is typed, and what the programs on the terminal write it reads.</p>
1220-<p><code>spawn</code> starts the program at <code>p.path</code>, with <code>p.args</code> as its arguments, the first being its name. A <code>p.path</code> without a <code>/</code> is looked for as a shell looks for a command, and as C’s <code>execvp</code> does: in each directory of this program’s <code>PATH</code> in turn, an empty one being the working directory, or of <code>/bin:/usr/bin</code> if <code>PATH</code> is unset or empty; the first that runs is the program. Its environment is this program’s with each <code>NAME=value</code> of <code>p.env</code> added or put in place of the old value, or, if <code>p.onlyEnv</code>, <code>p.env</code> alone. It starts in <code>p.dir</code>. Its standard input, output and error are <code>p.fds</code>; a 0 gives it this program’s own, so <code>{path: "ls", args: ["ls"]}</code> runs <code>ls</code> as a shell runs it, with every other field zero. It gets no other open file but those of <code>p.more</code>, each at its own number, as a shell passes <code>3&lt;file</code> on, every signal does what it does at first, and none is blocked. If <code>p.tty</code> is a pseudo-terminal’s fd, the program starts a session on that terminal: it is its controlling terminal, and its standard input, output and error, and <code>p.fds</code> and <code>p.group</code> are not used.</p>
1365+<p><code>spawn</code> starts the program at <code>p.path</code>, with <code>p.args</code> as its arguments, the first being its name. A <code>p.path</code> without a <code>/</code> is looked for as a shell looks for a command, and as C’s <code>execvp</code> does: in each directory of this program’s <code>PATH</code> in turn, an empty one being the working directory, or of <code>/bin:/usr/bin</code> if <code>PATH</code> is unset or empty; the first that runs is the program. If there is none, <code>spawn</code> says so, -2 (ENOENT) or -13 (EACCES), before it makes a child: no program is started, and no process id given for a program that was never there. A <code>p.path</code> with a <code>/</code> is not looked at first, and a program that is there but that the kernel then refuses to run (not a program, a bad interpreter) is a child that ends with status 127. Its environment is this program’s with each <code>NAME=value</code> of <code>p.env</code> added or put in place of the old value, or, if <code>p.onlyEnv</code>, <code>p.env</code> alone. It starts in <code>p.dir</code>. Its standard input, output and error are <code>p.fds</code>; a 0 gives it this program’s own, so <code>{path: "ls", args: ["ls"]}</code> runs <code>ls</code> as a shell runs it, with every other field zero. It gets no other open file but those of <code>p.more</code>, each at its own number, as a shell passes <code>3&lt;file</code> on, every signal does what it does at first, and none is blocked; with <code>p.keepIgnored</code>, a signal this program ignores stays ignored in it, as the kernel’s <code>execve</code> leaves it, which is what a program run under <code>nohup</code> wants of the programs it starts. If <code>p.tty</code> is a pseudo-terminal’s fd, the program starts a session on that terminal: it is its controlling terminal, and its standard input, output and error, but that a <code>p.fds[i]</code> that is not 0 is that file instead, as for a job that reads <code>/dev/null</code>; <code>p.group</code> is not used.</p>
12211366 <p><code>p.group</code> puts the program in a process group, the programs that a shell’s job is made of: -1 makes it the first of a new group, whose number is its process id, and the other programs of the job join that number. The terminal gives its keys, and the signals of Ctrl-C and Ctrl-Z, to one group, the one in its foreground: <code>p.foreground</code> puts the program’s group there before it starts, and <code>foreground</code> puts any group there, as a shell does to take the terminal back. <code>setGroup</code> moves a program to a group, as the shell does for each program it starts, for whichever of the two runs first; a shell runs in a group of its own, <code>setGroup(0, 0)</code>.</p>
1222-<p><code>exec</code> runs the program <code>p</code> describes in this program’s place, as a shell’s <code>exec cmd</code> does, found and started as <code>spawn</code> would start it, <code>p.tty</code>, <code>p.group</code>, <code>p.foreground</code>, <code>p.fds</code> and <code>p.dir</code> included, except that it keeps this program’s other open files: each stays open in the program unless <code>keepOnExec(fd, false)</code> was said of it, as it is of <code>pipe</code>’s, and those of <code>p.more</code> are kept. It returns only if the program can’t run: -2 (ENOENT) or -13 (EACCES) if there is none to run, with nothing done; or, if the program is found but won’t start, minus the errno, with what <code>p</code> asks of the terminal, the group, the files and the directory done, and this program’s signals as they were.</p>
1367+<p><code>exec</code> runs the program <code>p</code> describes in this program’s place, as a shell’s <code>exec cmd</code> does, found and started as <code>spawn</code> would start it, <code>p.tty</code>, <code>p.group</code>, <code>p.foreground</code>, <code>p.fds</code> and <code>p.dir</code> included, except that it keeps this program’s other open files: each stays open in the program unless it is closed on exec, as the files <code>open</code>, <code>pipe</code>, a socket or <code>memFile</code> gives are, unless <code>keepOnExec(fd, true)</code> was said of them; <code>dup</code> and <code>dupTo</code> give files that stay open; and those of <code>p.more</code> are kept. It returns only if the program can’t run: -2 (ENOENT) or -13 (EACCES) if there is none to run, with nothing done; or, if the program is found but won’t start, minus the errno, with what <code>p</code> asks of the terminal, the group, the files and the directory done, and this program’s signals as they were.</p>
12231368 <p><code>execve(path, args, env)</code> is the kernel’s execve, for a shell, which has looked for the program along its own <code>PATH</code> and set up the child already: the program at <code>path</code> is run with <code>args</code> and with <code>env</code>, <code>NAME=value</code>, for its whole environment. Nothing is searched for, and every fd and signal stays as it is, but that a handled signal does what it does at first: an ignored one stays ignored, as <code>trap '' HUP</code> wants of the command it comes before. It returns only if it can’t: minus an errno, with nothing changed.</p>
1224-<p>A shell moves its own files with <code>dup</code> and <code>dupTo</code>, for <code>2&gt;&amp;1</code> and <code>exec &gt;log</code> and around a builtin: <code>dupTo(fd, into)</code> makes <code>into</code> the same open file as <code>fd</code>, closing what <code>into</code> was, and <code>dup</code> gives a new fd for it, the lowest free, as a shell saves a file to give it back. <code>dupAbove(fd, 10)</code> gives one at 10 or above, closed on exec, out of the way of the fds a script names (<code>3&gt;log</code>), as a shell keeps its own. <code>fork</code> copies this program, which goes on from there in both: it is 0 in the copy and the copy’s process id in this one, as a shell runs a builtin or a procedure as a stage of a pipe. Only the thread that calls it is copied, so a program with threads mustn’t; and what <code>Out</code> keeps is copied too, to be written twice, so <code>Out.flush</code> comes first. <code>group</code> is the process group this program is in: a shell starting on a terminal waits until it is <code>foregroundOf(0)</code> before it takes it. <code>access</code> says whether this user, by its real ids, may read, write or execute a file, as <code>test -r</code>, <code>-w</code> and <code>-x</code> ask, which <code>Info.mode</code> alone can’t say; an empty <code>how</code> asks only whether it is there. <code>open</code> refuses <code>executing</code>, with -22 (EINVAL). <code>umask</code> sets the bits taken away from the mode of every file this program, and those it starts, make, and says what they were. <code>envAt</code> gives the environment one variable at a time, from 0, for a shell to list it and to pass it on whole with <code>onlyEnv</code>. <code>setEnv</code> refuses a name that is empty or holds an <code>=</code>, and a 0 in either, with -22; as C’s <code>setenv</code>, it isn’t safe while another thread reads the environment, and what <code>env</code> gave for a variable may be gone once it sets that variable again. <code>getMode</code> reads a terminal’s modes, and <code>setMode</code> sets them again once what was written is sent, so that a shell keeps them for each job: a program stopped by Ctrl-Z with its terminal raw gets it back raw, and the shell its own between. <code>cpuTime</code> says how long the processor ran this program, in its own code and in the system for it, or, if <code>children</code>, the programs it started and has waited for, as <code>time</code> and <code>times</code> say. <code>limit</code> gives a limit, <code>limitFiles</code> say, soft and hard, and <code>setLimit</code> sets them, as <code>ulimit</code> does: a program may raise its soft limit up to the hard one, and lower either. <code>uid</code> is the user this program runs as, <code>euid</code> the one whose rights it has, 0 for root, as a prompt shows <code>#</code>; <code>hostName</code> is the machine’s name.</p>
1225-<p><code>wait</code> waits for the program <code>pid</code> to change, for any of this program’s if <code>pid</code> is -1, or for any of the group <code>g</code> if <code>pid</code> is <code>-g</code>, as a shell waits for a job’s programs, and says which one, and how: <code>exited</code>, with its exit status; <code>killed</code>, with the number of the signal that ended it; or <code>stopped</code>, by Ctrl-Z or a signal, with the signal’s number. Without <code>block</code> it doesn’t wait, and is 0 and <code>running</code> if no program changed. A program that has nothing to wait for is -10 (ECHILD). <code>kill</code> sends a program a signal, <code>sigTerm</code> to end it; to a group, if <code>pid</code> is minus its number.</p>
1226-<p><strong>Signals.</strong> <code>catch</code> takes the signals <code>sigs</code> away from what they would do, ending the program most of them, and gives an fd that is ready to read when one comes, so that a program waits for signals with <code>ready</code> and <code>readyOf</code> as it waits for keys or a server. <code>caught</code> gives each signal that came, in turn. A later <code>catch</code> adds to the signals caught and gives the same fd. Catching <code>sigKill</code> or <code>sigStop</code>, or the signals <code>Rt</code> reports, SIGSEGV and SIGFPE, is -22 (EINVAL). <code>ignore</code> makes a signal do nothing, as a shell ignores <code>sigTtou</code> so that it can take its terminal back, and <code>ignore(sig, false)</code> gives it back what it does at first. A program that <code>spawn</code> starts begins with neither.</p>
1369+<p>A shell moves its own files with <code>dup</code> and <code>dupTo</code>, for <code>2&gt;&amp;1</code> and <code>exec &gt;log</code> and around a builtin: <code>dupTo(fd, into)</code> makes <code>into</code> the same open file as <code>fd</code>, closing what <code>into</code> was, and <code>dup</code> gives a new fd for it, the lowest free, as a shell saves a file to give it back. <code>dupAbove(fd, 10)</code> gives one at 10 or above, closed on exec, out of the way of the fds a script names (<code>3&gt;log</code>), as a shell keeps its own. <code>fork</code> copies this program, which goes on from there in both: it is 0 in the copy and the copy’s process id in this one, as a shell runs a builtin or a procedure as a stage of a pipe. Only the thread that calls it is copied, so a program with threads mustn’t; and what <code>Out</code> keeps is copied too, to be written twice, so <code>Out.flush</code> comes first. <code>setsid</code> gives the copy a session of its own, with no controlling terminal, as a daemon does so that closing its terminal doesn’t reach it; it is -1 (EPERM) for a program that leads a process group already, which the copy doesn’t. <code>group</code> is the process group this program is in: a shell starting on a terminal waits until it is <code>foregroundOf(0)</code> before it takes it. <code>access</code> says whether this user, by its real ids, may read, write or execute a file, as <code>test -r</code>, <code>-w</code> and <code>-x</code> ask, which <code>Info.mode</code> alone can’t say; an empty <code>how</code> asks only whether it is there. <code>open</code> refuses <code>executing</code>, with -22 (EINVAL). <code>nonblocking</code> opens a file without waiting: a fifo opened to write waits for a reader and a fifo opened to read waits for a writer, but with <code>nonblocking</code> the open returns at once (-6, ENXIO, for a write with no reader), and a read or write that would wait is -11 later, as <code>setBlocking(fd, false)</code> makes it, and <code>blocking(fd)</code> says which: 1 if they wait, 0 if not. A file <code>open</code>, <code>openMode</code>, <code>openRead</code> or <code>create</code> gives is closed in a program <code>exec</code> or <code>execve</code> starts, as <code>pipe</code>’s are, so that a fifo or a log one program holds doesn’t reach every program it runs; <code>keepOnExec(fd, true)</code> keeps one open, and <code>spawn</code> is unchanged, since it passes its <code>fds</code> and <code>more</code> as before. <code>umask</code> sets the bits taken away from the mode of every file this program, and those it starts, make, and says what they were. <code>envAt</code> gives the environment one variable at a time, from 0, for a shell to list it and to pass it on whole with <code>onlyEnv</code>. <code>env</code> gives “” for a variable that is unset and for one set to nothing; <code>findEnv</code> tells them apart, as <code>TZ</code> needs, unset meaning the local time and empty meaning UTC. <code>setEnv</code> refuses a name that is empty or holds an <code>=</code>, and a 0 in either, with -22; as C’s <code>setenv</code>, it isn’t safe while another thread reads the environment, and what <code>env</code> gave for a variable may be gone once it sets that variable again. <code>getMode</code> reads a terminal’s modes, and <code>setMode</code> sets them again once what was written is sent, so that a shell keeps them for each job: a program stopped by Ctrl-Z with its terminal raw gets it back raw, and the shell its own between. <code>cpuTime</code> says how long the processor ran this program, in its own code and in the system for it, or, if <code>children</code>, the programs it started and has waited for, as <code>time</code> and <code>times</code> say. <code>limit</code> gives a limit, <code>limitFiles</code> say, soft and hard, and <code>setLimit</code> sets them, as <code>ulimit</code> does: a program may raise its soft limit up to the hard one, and lower either. <code>uid</code> is the user this program runs as, <code>euid</code> the one whose rights it has, 0 for root, as a prompt shows <code>#</code>; <code>hostName</code> is the machine’s name. <code>processors</code> counts the processors the program may run on, as the system’s affinity mask allows, for a program that makes a thread to each; it is never less than 1.</p>
1370+<p><code>wait</code> waits for the program <code>pid</code> to change, for any of this program’s if <code>pid</code> is -1, or for any of the group <code>g</code> if <code>pid</code> is <code>-g</code>, as a shell waits for a job’s programs, and says which one, and how: <code>exited</code>, with its exit status; <code>killed</code>, with the number of the signal that ended it; or <code>stopped</code>, by Ctrl-Z or a signal, with the signal’s number. Without <code>block</code> it doesn’t wait, and is 0 and <code>running</code> if no program changed. A program that has nothing to wait for is -10 (ECHILD). <code>waitExit(pid)</code> is <code>wait</code> that blocks until a program ends, and not for one that is stopped, so that a program waiting for its editor isn’t woken by Ctrl-Z. <code>kill</code> sends a program a signal, <code>sigTerm</code> to end it; to a group, if <code>pid</code> is minus its number.</p>
1371+<p><strong>Signals.</strong> <code>catch</code> takes the signals <code>sigs</code> away from what they would do, ending the program most of them, and gives an fd that is ready to read when one comes, so that a program waits for signals with <code>ready</code> and <code>readyOf</code> as it waits for keys or a server. <code>caught</code> gives each signal that came, in turn. A later <code>catch</code> adds to the signals caught and gives the same fd. Catching <code>sigKill</code> or <code>sigStop</code>, or the signals <code>Rt</code> reports, SIGSEGV and SIGFPE, is -22 (EINVAL). <code>ignore</code> makes a signal do nothing, as a shell ignores <code>sigTtou</code> so that it can take its terminal back, and <code>ignore(sig, false)</code> gives it back what it does at first. On a signal that <code>catch</code> took, either gives it up: the signal is no longer caught, with its default action or ignored, and <code>catch</code>’s fd gives the signals still caught. A program that <code>spawn</code> starts begins with neither. <code>ignored(sig)</code> says whether a signal does nothing now, because the program ignored it or because the one that started this one left it so, as a program in the background or under <code>nohup</code> finds <code>sigInt</code> and <code>sigHup</code>: it should leave those alone, and <code>handle</code> or <code>catch</code> the rest.</p>
1372+<p>To wait for a child, a signal or a time, whichever is first, <code>catch</code> <code>sigChld</code> with the other signals and wait in <code>ready</code> on <code>catch</code>’s fd: a child that ends makes <code>sigChld</code> ready there, as a signal that came before the wait does, since <code>catch</code> holds it until <code>caught</code> takes it, so none falls between a <code>wait</code> that found nothing and the sleep after it. When the fd is ready, call <code>caught</code>, then <code>wait(pid, false)</code>. <code>handle</code> can’t do this: a signal noted just before the call it should break isn’t seen by it.</p>
12271373 <p><code>handle</code> is for a program that must see a signal while it waits in a system call, as a shell waiting for its foreground program, or for a key, sees Ctrl-C: the signal is noted, and the call it comes during, <code>wait</code>, <code>read</code>, <code>ready</code> and the rest, fails with -4 (EINTR) rather than going on; <code>sleep</code> sleeps its time all the same. <code>came(sig)</code> says whether it came since the last time it was asked. Nothing else runs when the signal comes, so the program does what it does after it, in its own time; <code>Out</code> and <code>Files</code> try again after -4. <code>ignore(sig, false)</code> gives the signal back what it does at first, and the signals <code>catch</code> refuses, <code>handle</code> refuses too.</p>
12281374 <p><strong>Time.</strong> <code>clock</code> is for measuring how long something takes: it never goes back, and says nothing of the date. <code>now</code> is the date and time, which a person or the network may set back. <code>sleep</code> waits <code>ms</code> milliseconds.</p>
12291375 <p><strong>Sockets.</strong> <code>connect</code> is for talking to a server on the same machine, an X server at <code>/tmp/.X11-unix/X0</code> say; <code>connectTcp</code> for one across the network, given its address as numbers, <code>"127.0.0.1"</code> or <code>"::1"</code>: turning a name into an address is a library’s work over these. <code>connectUdp</code> gives a socket of datagrams, each <code>send</code> one message to that address and each <code>read</code> one message from it, as a program asks a name server. What a socket receives comes by <code>read</code>, what it sends goes by <code>send</code>, and <code>close</code> ends it. <code>send</code> is <code>write</code> for a socket, except when the other end has closed it: <code>write</code> would stop the program with the signal SIGPIPE, where <code>send</code> returns -32 (EPIPE), so that a program learns its server has gone and can end as it wants. <code>listen</code> and <code>listenTcp</code> are the other side: a server’s socket, on <code>path</code>, which must not be there yet, or on <code>port</code> at <code>addr</code>, <code>"0.0.0.0"</code> or <code>"::"</code> for every address the machine has; <code>accept</code> gives each connection that comes, as a socket of its own. A server started on port 0 gets a port the system chooses, which <code>localPort</code> says, as a test does that must not take a port in use. <code>connectTcp</code> waits as long as the system does, minutes for a host that never answers; <code>connectTcpWithin</code> gives up after <code>ms</code> milliseconds, with -110 (ETIMEDOUT).</p>
12301376 <p><code>listenUdp</code> is the server’s side of <code>connectUdp</code>: a socket of datagrams from anyone, as a name server has. <code>receive</code> gives the next datagram, at most <code>LEN(buf)</code> bytes of it, the rest lost, and a <code>Peer</code> that <code>reply</code> sends an answer to.</p>
1231-<p><strong>Passing files and sharing memory.</strong> A program can give another an open file over a Unix socket: <code>sendFds</code> sends <code>s</code>, at least one byte, and with it <code>fds</code>, at most 16, each a file the other program then has open too; <code>receiveFds</code> reads into <code>buf</code> as <code>read</code> does, puts the fds that came with it in <code>fds</code>, as many as there is room for, closing the rest, and says how many bytes and how many fds. Each is closed in a program exec starts. <code>memFile</code> makes a file that lives in memory, of <code>size</code> zeroed bytes, and <code>mapFile</code> maps it, or any file, so that what one program writes there another sees at once: so a program hands an X server or a Wayland compositor its pixels, by <code>sendFds</code> of a <code>memFile</code> both map, rather than by copying them down a socket.</p>
1232-<p><code>readyOf</code> waits on several file descriptors at once, as a program does that listens to a server and a terminal both; a descriptor at its end, or failed, counts as ready, since a read of it won’t wait. It looks at the first 64 of <code>fds</code>, and <code>connect</code> and <code>listen</code> take a path of at most 107 bytes, as <code>struct sockaddr_un</code> holds; a longer one is -36 (ENAMETOOLONG).</p>
1377+<p><code>closeWrite</code> ends this side’s writing to a socket, so that the other end reads an end of input and can still answer on the same socket: a client sends its request, says it is done, and reads the reply. <code>peer</code> says who is at the other end of a connected socket: for a TCP one its address and port; for a Unix one the user and process that made the connection, as the system recorded them, so that a server can check that its client is the same user instead of trusting a directory’s mode. <code>sendNow</code> is <code>send</code> that never waits: where the socket has no room it gives -11 (<code>errAgain</code>), without making reads on it non-blocking too. <code>socketPair</code> makes two connected stream sockets at once, as a <code>listen</code>, <code>connect</code> and <code>accept</code> would, with no name on disk. Each is closed in a program exec starts. <code>accept</code> gives -103 (<code>errConnAborted</code>) for a client that went before it was taken.</p>
1378+<p><strong>Passing files and sharing memory.</strong> A program can give another an open file over a Unix socket: <code>sendFds</code> sends <code>s</code>, at least one byte, and with it <code>fds</code>, at most 16, each a file the other program then has open too; <code>receiveFds</code> reads into <code>buf</code> as <code>read</code> does, puts the fds that came with it in <code>fds</code>, as many as there is room for, closing the rest, and says how many bytes and how many fds. Each is closed in a program exec starts. <code>memFile</code> makes a file that lives in memory, of <code>size</code> zeroed bytes, and <code>mapFile</code> maps it, or any file, so that what one program writes there another sees at once: so a program hands an X server or a Wayland compositor its pixels, by <code>sendFds</code> of a <code>memFile</code> both map, rather than by copying them down a socket. <code>mapFile</code> needs a file open to read and write; <code>mapRead</code> maps one that is open only to read, a dictionary that isn’t the user’s to write, for reading only: a write there stops the program. <code>protect</code> changes what may be done with pages <code>mmap</code> gave, from a page boundary: read, write, both, or neither, so that touching them stops the program, as a guard page after a buffer does, or a reserve mapped in full and opened as it fills. It takes the address as the system does, so with a C library it is that library’s whether an address not on a page boundary is -22 (EINVAL) or rounded down.</p>
1379+<p><code>readyOf</code> waits on several file descriptors at once, as a program does that listens to a server and a terminal both; a descriptor at its end, or failed, counts as ready, since a read of it won’t wait. It looks at the first 64 of <code>fds</code>, and <code>connect</code> and <code>listen</code> take a path of at most 107 bytes, as <code>struct sockaddr_un</code> holds; a longer one is -36 (ENAMETOOLONG). A path that starts with a zero byte is one of Linux’s abstract names (D-Bus’s <code>unix:abstract=</code>), which has no file: the address is as long as the name, with no zero after it.</p>
1380+<p><code>poll</code> is <code>readyFor</code> without its limits. Each <code>Poll</code> names a descriptor <code>fd</code> and whether to wait to <code>read</code> from it, to <code>write</code> to it, or both; <code>poll</code> waits up to <code>ms</code> milliseconds (as long as it takes if <code>ms &lt; 0</code>), sets every one’s <code>ready</code>, and gives how many are: all of them, in one call, not the first. It takes any number of descriptors (the list the system wants is on the stack for 64 or fewer, and in memory from <code>mmap</code>, given back before it returns, for more; -12 if there is none). A descriptor at its end or failed is ready, as for <code>readyOf</code>; so is one that isn’t open, and a read or a write of it is -9 (EBADF); a negative <code>fd</code> is never ready. A timeout is 0 and a failure minus an errno (-4, if a signal <code>handle</code> took broke in), where <code>readyFor</code> gives -1 for both. <code>readyFor</code> and <code>readyOf</code> are written over it.</p>
12331381 <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>
12341382 <p><strong>Strings, buffers and collections.</strong></p>
12351383 <pre><code>module Str;
@@ -1266,6 +1414,15 @@
12661414 proc chars*(s: []U8): INT; (* the characters in s, as decode reads them *)
12671415 proc toInt*(s: []U8): (I64, BOOL); (* s, in decimal; false if it isn&#39;t a number an I64 holds *)
12681416 proc toHex*(s: []U8): (U64, BOOL); (* s, in hexadecimal; false if it isn&#39;t a number a U64 holds *)
1417+proc toU64*(s: []U8): (U64, BOOL); (* s, decimal digits only; false if it isn&#39;t one, or a U64 can&#39;t hold it *)
1418+proc fromC*(p: ^U8): []U8; (* the bytes at p up to its zero byte, not copied; &quot;&quot; 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) *)
1420+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&#39;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 *)
1423+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 *)
12691426 proc fromInt*(n: I64): ([20]U8, INT); (* n in decimal: its bytes, and how many *)
12701427 proc le*(s: []U8): U64; (* s, at most 8 bytes, the first the lowest *)
12711428 proc be*(s: []U8): U64; (* s, at most 8 bytes, the first the highest *)
@@ -1289,14 +1446,18 @@
12891446 int(i: I64);
12901447 uint(u: U64);
12911448 hex(h: U64);
1449+ lowHex(h: U64);
12921450 str(s: []U8);
12931451 char(c: U8);
12941452 bool(b: BOOL);
12951453 real(r: F64; places: INT);
12961454 end;
1455+const ShortMost* = 24; (* the longest Fmt.short *)
1456+proc short*(x: F64; var out: [ShortMost]U8): []U8; (* x in the fewest digits that read back, in out *)
12971457 proc print*(f: []U8; args: []Arg); (* to standard output *)
12981458 proc eprint*(f: []U8; args: []Arg); (* to standard error, once Out is flushed *)
12991459 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 *)
13001461
13011462 module List(T);
13021463 type
@@ -1327,6 +1488,8 @@
13271488 proc tryPush*(var v: Vec; x: T): BOOL; (* push; false, and v as it was, if there is no room *)
13281489 proc tryAdd*(var v: Vec): (^T, BOOL); (* add; nil and false, and v as it was, if there is no room *)
13291490 proc resize*(var v: Vec; n: INT); (* n elements: cut short, or the new ones zero *)
1491+proc insert*(var v: Vec; i, n: INT); (* n zero elements opened at i; stops the program if there is no room *)
1492+proc remove*(var v: Vec; i, n: INT); (* the n elements from i taken out, those after moved down *)
13301493 proc all*(const v: Vec): []T; (* the elements, until it next grows *)
13311494 proc reset*(var v: Vec); (* emptied, its memory kept *)
13321495 proc done*(var v: Vec); (* its memory given back *)
@@ -1341,6 +1504,7 @@
13411504 proc floor*(x: F64): F64;
13421505 proc ceil*(x: F64): F64;
13431506 proc round*(x: F64): F64; (* to the nearest whole number; a half away from 0 *)
1507+proc roundEven*(x: F64): F64; (* to the nearest whole number; a half to the even one *)
13441508 proc exp*(x: F64): F64;
13451509 proc ln*(x: F64): F64;
13461510 proc log2*(x: F64): F64;
@@ -1348,6 +1512,7 @@
13481512 proc sin*(x: F64): F64;
13491513 proc cos*(x: F64): F64;
13501514 proc tan*(x: F64): F64;
1515+proc tanh*(x: F64): F64; (* from -1 to 1 *)
13511516 proc asin*(x: F64): F64; (* from -pi/2 to pi/2 *)
13521517 proc acos*(x: F64): F64; (* from 0 to pi *)
13531518 proc atan*(x: F64): F64; (* from -pi/2 to pi/2 *)
@@ -1355,6 +1520,7 @@
13551520 proc bits*(x: F64): U64; (* the bits of x, as IEEE 754 lays them out *)
13561521 proc fromBits*(u: U64): F64; (* the F64 whose bits are u *)
13571522 proc mul*(a, b: U64): (U64, U64); (* a times b: the low 64 bits of the product, and the high *)
1523+proc divMod*(hi, lo, d: U64): (U64, U64); (* hi, lo divided by d: the quotient and the remainder; hi &lt; d *)
13581524 proc toReal*(s: []U8): (F64, BOOL); (* s as a real, the F64 nearest it; false if it isn&#39;t one *)</code></pre>
13591525 <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 &gt; 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>
13601526 <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>
@@ -1373,7 +1539,9 @@
13731539 w, rest := Str.field(rest);
13741540 end;</code></pre>
13751541 <p><code>lower</code> and <code>upper</code> write <code>s</code>’s bytes, so they take a variable’s, not a string literal’s (section 5.10). <code>join</code> and <code>replace</code> add to a <code>Buf</code> as <code>Buf.str</code> does; an empty <code>old</code> adds <code>s</code> as it is.</p>
1376-<p><code>toInt</code> reads a decimal number, with a <code>-</code> or <code>+</code> before it if any, and nothing else. <code>toHex</code> reads hexadecimal digits, capitals or not, and nothing else: no sign, <code>0x</code> or <code>H</code>. <code>fromInt</code> writes a number as <code>Out.int</code> does, with a <code>-</code> if it is negative, its bytes the first of the 20 it gives.</p>
1542+<p><code>toInt</code> reads a decimal number, with a <code>-</code> or <code>+</code> before it if any, and nothing else. <code>toHex</code> reads hexadecimal digits, capitals or not, and nothing else: no sign, <code>0x</code> or <code>H</code>. <code>fromInt</code> writes a number as <code>Out.int</code> does, with a <code>-</code> if it is negative, its bytes the first of the 20 it gives. <code>toU64</code> reads a decimal number as <code>toInt</code> does, but digits only (no sign), up to the largest <code>U64</code>: what <code>Fmt.uint</code> writes.</p>
1543+<p><code>fromC</code> and <code>toC</code> are the two ends of a call to C. <code>fromC</code> makes a slice of the bytes at a pointer up to its zero byte, without copying them; <code>nil</code> gives <code>""</code>. <code>toC</code> copies <code>s</code> into <code>out</code>, puts a zero after it and gives <code>PTR(out)</code>, so a call reads <code>C.realpath(Str.toC(path, room), ...)</code>. <code>out</code> holds <code>LEN(s) + 1</code> bytes at least; a shorter one stops the program, as any slice does.</p>
1544+<p><code>base64With</code> and <code>unbase64With</code> take the 64 digits as <code>alphabet</code> and say with <code>pad</code> whether <code>=</code> pads a last group: <code>Base64Url</code> is section 5’s, with <code>-</code> and <code>_</code>, and <code>Base64</code> section 4’s. Without padding a last group has 2 or 3 digits, and a length 1 more than a multiple of 4 is no base64. <code>base64</code> and <code>unbase64</code> are <code>Base64</code> with padding. In detail, <code>base64</code> writes <code>s</code> as RFC 4648 (section 4) does: the digits <code>A</code> to <code>Z</code>, <code>a</code> to <code>z</code>, <code>0</code> to <code>9</code>, <code>+</code> and <code>/</code>, and <code>=</code> to pad a last group of 1 or 2 bytes to four. <code>unbase64</code> reads that and nothing else: its length a multiple of 4, <code>=</code> only as the last one or two bytes, and the bits of the digit before the padding that no byte uses all 0, so a byte string has the one spelling <code>base64</code> gives it. Line breaks, missing padding and the <code>-</code> and <code>_</code> of base64url are the caller’s to mend first; <code>unbase64</code> of such a string is false, and what it wrote into <code>out</code> is not to be used. The caller gives the room, as <code>Fmt</code> does: an <code>out</code> shorter than the comment says stops the program, as indexing past a slice does.</p>
13771545 <p><code>le</code> and <code>be</code> read an unsigned number of up to 8 bytes in a fixed order, little-endian or big-endian, whatever the machine’s own, and <code>putLE</code> and <code>putBE</code> write one; more than 8 bytes stops the program. They are how a file format or a network protocol is read and written:</p>
13781546 <pre><code>len := U32(Str.le(header[4..8]));
13791547 Str.putBE(packet[0..2], U64(port));</code></pre>
@@ -1384,13 +1552,16 @@
13841552 Fmt.print(&quot;{} is {}\n&quot;, [Fmt.str(k), Fmt.int(v)]);
13851553 i, k, v := Names.next(names, i);
13861554 end;</code></pre>
1387-<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>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.</p>
1388-<p><strong>Files and Opt</strong> are what most programs need of their command line.</p>
1555+<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+<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>
13891557 <pre><code>module Files;
1390-proc arg*(i: INT): []U8; (* argument i; 0 is the program&#39;s name *)
1558+proc arg*(i: INT): []U8; (* argument i; 0 is the program&#39;s name; &quot;&quot; if there is none *)
13911559 proc read*(path: []U8; var b: Buf.Buf): BOOL; (* the whole file added to b *)
13921560 proc load*(path: []U8; var b: Buf.Buf): INT; (* as read: 0, or minus the errno that stopped it *)
1393-proc write*(path, data: []U8): BOOL; (* the file made to hold data *)
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 *)
13941565
13951566 module Opt;
13961567 type
@@ -1413,7 +1584,8 @@
14131584 proc scope*(var p: Parser; name: []U8); (* subcommand name&#39;s options, from the word after it *)
14141585 proc number*(const p: Parser; lo, hi: I64): I64; (* the value just read, a number from lo to hi *)
14151586 proc refuse*(const p: Parser; wants: []U8); (* the value just read refused, as number refuses one *)
1416-proc fail*(const p: Parser; text: []U8); (* the program&#39;s own mistake *)</code></pre>
1587+proc fail*(const p: Parser; text: []U8); (* the program&#39;s own mistake *)
1588+proc misuse*(const p: Parser; text: []U8); (* the program&#39;s own mistake, with the synopsis *)</code></pre>
14171589 <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>
14181590 <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>
14191591 <pre><code>const wait = 1; (* the key of --wait, which has no letter *)
@@ -1442,6 +1614,7 @@
14421614 <li>Flags may share a dash: <code>-lw</code> is <code>-l -w</code>. A letter’s value is the rest of its word, <code>-bdev</code>, or the whole next word, <code>-b dev</code>, even one that starts with a dash or is empty; in a group, the first letter that takes a value takes the rest, <code>-qn12</code>.</li>
14431615 <li>A long option is written in full, with its value after a <code>=</code> or in the next word: <code>--bar=dev</code>, <code>--bar dev</code>. An abbreviation is unknown, not guessed, so that adding an option never breaks a script. A value given to a flag, <code>--follow=x</code>, is a mistake.</li>
14441616 <li><code>--</code> ends the options, and isn’t an operand; <code>-</code> alone is one. The options end at the first operand, so in <code>keep start site python -m http.server</code> the <code>-m</code> is python’s.</li>
1617+<li>In a scope with no option whose letter is a digit, a word of <code>-</code> and a digit, such as <code>-33.87</code>, is an operand, and so ends the options. A scope that gives a digit a letter, as <code>head -5</code> has, reads it as an option.</li>
14451618 <li>Each scope has its own options. An option given twice is read twice.</li>
14461619 </ul>
14471620 <p><code>number</code> reads the value as <code>Str.toInt</code> does. <code>operand</code> reads what options are left first, and the program doesn’t see them. So does <code>at</code>, which says where the operands are: the arguments from <code>operand</code>’s next on are <code>Files.arg(i)</code> for <code>i</code> from <code>at(p)</code> to <code>ARGC() - 1</code>, so that an interpreter hands a script its arguments as they are:</p>
@@ -1456,13 +1629,115 @@
14561629 <p>A mistake on the command line is a sentence on standard error, the program’s name first, then the scope’s synopsis lines, as the help begins, and status 2:</p>
14571630 <pre><code>keep: unknown option -x
14581631 usage: keep start [-b label] name command [argument ...]</code></pre>
1459-<p>The sentences are <code>unknown option -x</code> (or <code>--name</code>, up to its <code>=</code>), <code>-b needs a label</code>, <code>--follow takes no value</code> and <code>--wait wants a number from 1 to 600, not '0'</code>; for a number with no top, given the largest <code>I64</code> for <code>hi</code>, <code>--jobs wants a number from 1 up, not '0'</code>. <code>refuse</code> refuses a value the program reads itself, as <code>number</code> refuses one, naming the option as the user wrote it, <code>-b</code> or <code>--bar</code>: <code>Opt.refuse(p, "a label with no spaces")</code> writes <code>keep: -b wants a label with no spaces, not 'a b'</code>, and the synopsis lines. <code>p.spelt</code> is that spelling, for the program’s own sentences. <code>fail</code> writes the program’s own mistakes the same way, but alone, so that the sentence can say where the help is: <code>keep: no command; see keep -h</code>.</p>
1632+<p>The sentences are <code>unknown option -x</code> (or <code>--name</code>, up to its <code>=</code>), <code>-b needs a label</code>, <code>--follow takes no value</code> and <code>--wait wants a number from 1 to 600, not '0'</code>; for a number with no top, given the largest <code>I64</code> for <code>hi</code>, <code>--jobs wants a number from 1 up, not '0'</code>. <code>refuse</code> refuses a value the program reads itself, as <code>number</code> refuses one, naming the option as the user wrote it, <code>-b</code> or <code>--bar</code>: <code>Opt.refuse(p, "a label with no spaces")</code> writes <code>keep: -b wants a label with no spaces, not 'a b'</code>, and the synopsis lines. <code>p.spelt</code> is that spelling, for the program’s own sentences. <code>fail</code> writes the program’s own mistakes the same way, but alone, so that the sentence can say where the help is: <code>keep: no command; see keep -h</code>. <code>misuse</code> writes one with the scope’s synopsis lines after it, for a mistake in the operands: <code>Opt.misuse(p, "no tests named")</code>.</p>
14601633 <p>A mistake in the table is the program’s, not its user’s. <code>init</code> checks the table before it reads a word, and ends the program with status 1: <code>keep: option table, row 3: the option is in the scope already</code>. The others are an option with neither a letter nor a long name, a long name with <code>=</code> in it, a letter <code>-</code>, a value with one bracket but not the other, and <code>--help</code> in the table, since it is <code>Opt</code>’s.</p>
14611634 <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>
14621635 <pre><code>module Width;
14631636 proc width*(c: I32): INT; (* the columns the character c takes: 0, 1 or 2 *)
14641637 proc columns*(s: []U8): INT; (* the columns the UTF-8 text s takes *)</code></pre>
14651638 <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+<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+<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>
1641+<pre><code>module Rand;
1642+type Rand* = record ... end;
1643+proc init*(var r: Rand; seed: U64); (* starts r at seed; any seed, 0 too *)
1644+proc next*(var r: Rand): U64; (* the next number, all 64 bits of it *)
1645+proc below*(var r: Rand; n: U64): U64; (* 0 to n - 1, each as likely as another; n = 0 stops the program *)
1646+proc range*(var r: Rand; lo, hi: I64): I64; (* lo to hi, both included; lo must not exceed hi *)
1647+proc seed*(): U64; (* a fresh seed from the kernel, to print and give back to init *)</code></pre>
1648+<pre><code>module Dice;
1649+import Rand, Check;
1650+
1651+(* Five rolls of a die from seed, as the digits of a number. *)
1652+proc rolls(seed: U64): INT;
1653+var r: Rand.Rand; i, v: INT;
1654+begin
1655+ Rand.init(r, seed);
1656+ for i := 1 to 5 do v := v * 10 + INT(Rand.range(r, 1, 6)); end;
1657+ return v;
1658+end;
1659+
1660+proc main*;
1661+begin
1662+ Check.int(&quot;the same seed, the same rolls&quot;, rolls(7), rolls(7));
1663+ Check.ok(rolls(7) &gt;= 11111, &quot;every die shows 1 or more&quot;);
1664+ Check.ok(rolls(7) &lt;= 66666, &quot;every die shows 6 or less&quot;);
1665+end;
1666+
1667+end Dice.</code></pre>
1668+<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+<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 *)
1673+proc mute*(on: BOOL); (* while on, checks say nothing and are not counted *)
1674+proc mutedFailures*(): INT; (* how many failed since mute was turned on *)</code></pre>
1675+<pre><code>module Digits;
1676+import Check;
1677+
1678+(* How many decimal digits n, not negative, takes. *)
1679+proc count(n: INT): INT;
1680+var d: INT;
1681+begin
1682+ d := 1;
1683+ while n &gt;= 10 do n := n div 10; INC(d); end;
1684+ return d;
1685+end;
1686+
1687+proc main*;
1688+begin
1689+ Check.int(&quot;0 is one digit&quot;, count(0), 1);
1690+ Check.int(&quot;9 is one digit&quot;, count(9), 1);
1691+ Check.int(&quot;10 is two digits&quot;, count(10), 2);
1692+ Check.ok(count(1000000) = 7, &quot;a million is seven digits&quot;);
1693+end;
1694+
1695+end Digits.</code></pre>
1696+<p><code>Prop</code> tests a property: a procedure that draws values and checks them with <code>Check</code>. A case is the bytes the property drew, and <code>Source</code> gives them as a number, a choice, a flag or a string of bytes; fewer and smaller bytes give simpler values. When a case fails, <code>Prop</code> shrinks it by changing its bytes and running the property again, so no type needs a shrinker of its own, and it saves the case. A property that draws only bytes is a fuzzer.</p>
1697+<pre><code>module Prop;
1698+type Source* = record ... end;
1699+type Property* = proc(var s: Source); (* draws from s, checks with Check *)
1700+proc bits*(var s: Source): U64; (* 64 bits, from 8 bytes, the first the highest *)
1701+proc int*(var s: Source; lo, hi: I64): I64; (* lo to hi, both included; zeros give lo *)
1702+proc pick*(var s: Source; n: INT): INT; (* 0 to n - 1; n at least 1 *)
1703+proc bool*(var s: Source): BOOL;
1704+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+<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+<pre><code>module Reverse;
1708+import Prop, Check, Buf, Heap;
1709+
1710+var
1711+ b, was: Buf.Buf; (* the bytes drawn, and a copy of them *)
1712+
1713+(* Turns s round, in place. *)
1714+proc reverse(s: []U8);
1715+var i: INT; c: U8;
1716+begin
1717+ for i := 0 to LEN(s) div 2 - 1 do
1718+ c := s[i]; s[i] := s[LEN(s) - 1 - i]; s[LEN(s) - 1 - i] := c;
1719+ end;
1720+end;
1721+
1722+(* Reversing any string twice gives it back. *)
1723+proc twice(var s: Prop.Source);
1724+begin
1725+ Prop.bytes(s, 64, b);
1726+ Buf.reset(was);
1727+ Buf.str(was, Buf.bytes(b));
1728+ reverse(Buf.bytes(b));
1729+ reverse(Buf.bytes(b));
1730+ Check.str(&quot;reversed twice&quot;, Buf.bytes(b), Buf.bytes(was));
1731+end;
1732+
1733+proc main*;
1734+begin
1735+ Buf.init(b, Heap.heap);
1736+ Buf.init(was, Heap.heap);
1737+ Prop.check(&quot;reverse&quot;, twice);
1738+end;
1739+
1740+end Reverse.</code></pre>
14661741 </div>
14671742 </main>
14681743 <footer>raz is under the ISC license. <a href="source/index.html">Source</a></footer>

site/public/tour/c.html (history) modified +2 -2

@@ -40,7 +40,7 @@
4040 <li><a href="threads.html">Threads</a></li>
4141 </ol></aside><article><p class="kicker">Go &middot; lesson 14 of 15</p><h1>Talking to C</h1>
4242 <div class="file"><span>Qsort.raz</span></div><pre class="code"><code><span class="kw">module</span> Qsort;
43-<span class="kw">import</span> Out, Fmt;
43+<span class="kw">import</span> Out, Fmt, SYSTEM;
4444
4545 <span class="kw">link</span> <span class="string">&quot;libm.so.6&quot;</span>;
4646
@@ -62,7 +62,7 @@
6262 <span class="kw">var</span> a: [<span class="number">5</span>]<span class="ty">INT</span>; i: <span class="ty">INT</span>;
6363 <span class="kw">begin</span>
6464 a := [<span class="number">30</span>, <span class="number">10</span>, <span class="number">50</span>, <span class="number">20</span>, <span class="number">40</span>];
65- qsort(^<span class="ty">U8</span>(<span class="ty">ADR</span>(a)), <span class="ty">LEN</span>(a), <span class="ty">SIZE</span>(<span class="ty">INT</span>), order);
65+ qsort(^<span class="ty">U8</span>(SYSTEM.<span class="ty">ADR</span>(a)), <span class="ty">LEN</span>(a), <span class="ty">SIZE</span>(<span class="ty">INT</span>), order);
6666 <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> Out.int(a[i]); Out.char(<span class="string">&#x27; &#x27;</span>); <span class="kw">end</span>;
6767 Out.ln;
6868 Fmt.print(<span class="string">&quot;{}\n&quot;</span>, [Fmt.real(cbrt(<span class="number">27.0</span>), <span class="number">3</span>)]);

site/public/tour/checks.html (history) modified +8 -9

@@ -42,17 +42,17 @@
4242 <div class="file"><span>Fail.raz</span></div><pre class="code"><code><span class="kw">module</span> Fail;
4343 <span class="kw">import</span> Out;
4444
45-<span class="kw">proc</span> get(a: []<span class="ty">INT</span>; i: <span class="ty">INT</span>): <span class="ty">INT</span>;
45+<span class="kw">proc</span> show(a: []<span class="ty">INT</span>; i: <span class="ty">INT</span>);
4646 <span class="kw">begin</span>
47- <span class="kw">return</span> a[i];
47+ Out.int(a[i]);
48+ Out.ln;
4849 <span class="kw">end</span>;
4950
5051 <span class="kw">proc</span> main*;
5152 <span class="kw">var</span> a: [<span class="number">3</span>]<span class="ty">INT</span>; i: <span class="ty">INT</span>;
5253 <span class="kw">begin</span>
5354 <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>
54- Out.int(get(a, i));
55- Out.ln;
55+ show(a, i);
5656 <span class="kw">end</span>;
5757 <span class="kw">end</span>;
5858
@@ -60,9 +60,9 @@
6060 <pre class="output"><code>0
6161 0
6262 0
63-<span class="err">Fail.raz:6:14: index 3 is out of range for a length of 3
64- in Fail.get at Fail.raz:6:14: i = 3
65- in Fail.main at Fail.raz:13:9: i = 3
63+<span class="err">Fail.raz:6:15: index 3 is out of range for a length of 3
64+ in Fail.show at Fail.raz:6:15: i = 3
65+ in Fail.main at Fail.raz:14:9: i = 3
6666 </span></code></pre>
6767 <p>Every index and slice is checked. A failed check says where it failed and with what values, then the calls under way with their variables, and ends the program with status 2. The three <code>0</code>s are what it printed before the fourth pass went one past the end.</p>
6868 <h2>Reading the message</h2>
@@ -112,8 +112,7 @@
112112
113113 <span class="kw">end</span> Zero.</code></pre>
114114 <pre class="output"><code>3
115-<span class="err">Zero.raz:6:5: stopped by a division fault
116- in Zero.ratio at Zero.raz:6:5: a = 10, b = 0
115+<span class="err">Zero.raz:13:5: stopped by a division fault
117116 in Zero.main at Zero.raz:13:5: n = 0
118117 </span></code></pre>
119118 <div class="file"><span>Slicing.raz</span></div><pre class="code"><code><span class="kw">module</span> Slicing;

site/public/tour/hello.html (history) modified +1 -1

@@ -70,7 +70,7 @@
7070 <span class="cmd">$ raz run Quiet.raz</span>
7171 hello, again
7272 <span class="cmd">$ ls -l Quiet | awk &#x27;{print $5, &quot;bytes&quot;}&#x27;</span>
73-28480 bytes</code></pre>
73+28680 bytes</code></pre>
7474 <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>
7575 <h2>What <code>Out</code> can write</h2>
7676 <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>

site/public/tour/memory.html (history) modified +2 -2

@@ -40,7 +40,7 @@
4040 <li><a href="threads.html">Threads</a></li>
4141 </ol></aside><article><p class="kicker">Set &middot; lesson 9 of 15</p><h1>Memory and allocators</h1>
4242 <div class="file"><span>Stack.raz</span></div><pre class="code"><code><span class="kw">module</span> Stack;
43-<span class="kw">import</span> Out, Mem, Heap, Arena;
43+<span class="kw">import</span> Out, Mem, Heap, Arena, SYSTEM;
4444
4545 <span class="kw">type</span> Node = <span class="kw">record</span> next: ^Node; val: <span class="ty">INT</span>; <span class="kw">end</span>;
4646
@@ -69,7 +69,7 @@
6969 <span class="kw">end</span>;
7070 Out.ln;
7171 <span class="kw">if</span> <span class="kw">not</span> Arena.init(ar, <span class="number">100000</span>) <span class="kw">then</span> <span class="kw">return</span>; <span class="kw">end</span>;
72- <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> top := push(top, i, <span class="ty">ADR</span>(ar.base)); <span class="kw">end</span>;
72+ <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> top := push(top, i, SYSTEM.<span class="ty">ADR</span>(ar.base)); <span class="kw">end</span>;
7373 Out.int(top^.val); Out.ln;
7474 Arena.done(ar); <span class="comment">(* every node at once *)</span>
7575 <span class="kw">end</span>;

site/public/tour/records.html (history) modified +5 -5

@@ -40,7 +40,7 @@
4040 <li><a href="threads.html">Threads</a></li>
4141 </ol></aside><article><p class="kicker">Set &middot; lesson 7 of 15</p><h1>Records and pointers</h1>
4242 <div class="file"><span>Boxes.raz</span></div><pre class="code"><code><span class="kw">module</span> Boxes;
43-<span class="kw">import</span> Fmt;
43+<span class="kw">import</span> Fmt, SYSTEM;
4444
4545 <span class="kw">type</span>
4646 Point = <span class="kw">record</span> x, y: <span class="ty">INT</span>; <span class="kw">end</span>;
@@ -64,8 +64,8 @@
6464 <span class="kw">begin</span>
6565 b := {lo: {x: <span class="number">1</span>, y: <span class="number">1</span>}, hi: {x: <span class="number">4</span>, y: <span class="number">3</span>}};
6666 Fmt.print(<span class="string">&quot;{}\n&quot;</span>, [Fmt.int(area(b))]);
67- grow(<span class="ty">ADR</span>(b), <span class="number">1</span>);
68- p := <span class="ty">ADR</span>(b.hi);
67+ grow(SYSTEM.<span class="ty">ADR</span>(b), <span class="number">1</span>);
68+ p := SYSTEM.<span class="ty">ADR</span>(b.hi);
6969 p^.x := <span class="number">10</span>;
7070 Fmt.print(<span class="string">&quot;{} {}\n&quot;</span>, [Fmt.int(area(b)), Fmt.int(b.hi.x)]);
7171 <span class="kw">end</span>;
@@ -314,13 +314,13 @@
314314 </span></code></pre>
315315 <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>
316316 <div class="file"><span>Spans.raz</span></div><pre class="code"><code><span class="kw">module</span> Spans;
317-<span class="kw">import</span> Out;
317+<span class="kw">import</span> Out, SYSTEM;
318318
319319 <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>;
320320
321321 <span class="kw">proc</span> spanOf(s: []<span class="ty">U8</span>): Span;
322322 <span class="kw">begin</span>
323- <span class="kw">return</span> {p: PTR(s), n: <span class="ty">LEN</span>(s)};
323+ <span class="kw">return</span> {p: SYSTEM.PTR(s), n: <span class="ty">LEN</span>(s)};
324324 <span class="kw">end</span>;
325325
326326 <span class="kw">proc</span> text(sp: Span): []<span class="ty">U8</span>;

site/public/tour/threads.html (history) modified +2 -2

@@ -40,7 +40,7 @@
4040 <li><a href="threads.html" aria-current="page">Threads</a></li>
4141 </ol></aside><article><p class="kicker">Go &middot; lesson 15 of 15</p><h1>Threads</h1>
4242 <div class="file"><span>Hits.raz</span></div><pre class="code"><code><span class="kw">module</span> Hits;
43-<span class="kw">import</span> Out, Thread;
43+<span class="kw">import</span> Out, Thread, SYSTEM;
4444
4545 <span class="kw">const</span> Workers = <span class="number">4</span>;
4646
@@ -56,7 +56,7 @@
5656 sum := <span class="number">0</span>;
5757 <span class="kw">for</span> i := <span class="number">1</span> <span class="kw">to</span> n <span class="kw">do</span>
5858 sum := sum + i;
59- _ := <span class="ty">XADD</span>(<span class="ty">ADR</span>(hits), <span class="number">1</span>);
59+ _ := <span class="ty">XADD</span>(hits, <span class="number">1</span>);
6060 <span class="kw">end</span>;
6161 Thread.lock(m);
6262 total := total + sum;