raz / commit a5145f64

commit
a5145f647fc11d3fef1a57ba63f8b435f992a170
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
83d5fe2c
site: v0.24.0, and its examples held to report 2026-10.37

Strings only read are []const U8; ADR, PTR, SYSCALL and ARGV written
SYSTEM., in pages whose modules import SYSTEM; atomics on the variable;
the atomics, lockfree, syscalls, records, hello and slices pages say so.

patch · log

66 files changed, +574 -502

site/content/advanced/01-fmt.txt (history) modified +4 -4

@@ -67,7 +67,7 @@
6767 import Out, Buf, Fmt, Heap;
6868
6969 (* "name (age n)", built in a Buf the caller owns *)
70-proc describe(var b: Buf.Buf; name: []U8; age: INT);
70+proc describe(var b: Buf.Buf; name: []const U8; age: INT);
7171 begin
7272 Fmt.write(b, "{} (age {})", [Fmt.str(name), Fmt.int(age)]);
7373 end;
@@ -96,7 +96,7 @@
9696 import Out;
9797
9898 (* s, then spaces up to width *)
99-proc left(s: []U8; width: INT);
99+proc left(s: []const U8; width: INT);
100100 var i: INT;
101101 begin
102102 Out.str(s);
@@ -115,7 +115,7 @@
115115 Out.int(n);
116116 end;
117117
118-proc row(name: []U8; qty, price: INT);
118+proc row(name: []const U8; qty, price: INT);
119119 begin
120120 left(name, 10);
121121 right(qty, 5);
@@ -144,7 +144,7 @@
144144 module Dump;
145145 import Out;
146146
147-proc dump(data: []U8);
147+proc dump(data: []const U8);
148148 var i: INT;
149149 begin
150150 for i := 0 to LEN(data) - 1 do

site/content/advanced/02-bytes.txt (history) modified +10 -9

@@ -13,7 +13,7 @@
1313 import Out, Str, Fmt;
1414
1515 proc main*;
16-var names: [4][]U8; i, j: INT; t: []U8;
16+var names: [4][]const U8; i, j: INT; t: []const U8;
1717 begin
1818 names := ["pear", "Apple", "fig", "apple"];
1919 (* a small insertion sort, with Str.cmp *)
@@ -44,13 +44,14 @@
4444 ```raz Keep run
4545 module Keep;
4646 import Out, Str, Heap, Mem;
47+import SYSTEM;
4748
4849 type Entry = record name: []U8; n: INT; end;
4950
5051 var entries: [3]Entry;
5152
5253 (* remembers a name that the caller will overwrite *)
53-proc remember(slot: INT; name: []U8);
54+proc remember(slot: INT; name: []const U8);
5455 var copy: []U8; ok: BOOL;
5556 begin
5657 copy, ok := Str.copy(Heap.heap, name);
@@ -68,7 +69,7 @@
6869 end;
6970 for i := 0 to 2 do
7071 Out.str(entries[i].name); Out.char(' ');
71- Mem.free(Heap.heap, ^U8(ADR(entries[i].name[0])));
72+ Mem.free(Heap.heap, ^U8(SYSTEM.ADR(entries[i].name[0])));
7273 end;
7374 Out.ln;
7475 end;
@@ -85,13 +86,13 @@
8586 import Out, Fmt, Bytes;
8687
8788 (* the last part of a path: what follows its last slash *)
88-proc base(path: []U8): []U8;
89+proc base(path: []const U8): []const U8;
8990 begin
9091 return path[Bytes.last(path, '/') + 1..LEN(path)];
9192 end;
9293
9394 proc main*;
94-var text: []U8; at, n, line: INT;
95+var text: []const U8; at, n, line: INT;
9596 begin
9697 Out.str(base("/usr/local/lib/raz/Bytes.raz")); Out.char(' ');
9798 Out.str(base("Out.raz")); Out.ln;
@@ -127,7 +128,7 @@
127128 const HeaderSize = 10;
128129
129130 (* the sum of the bytes of data, all mod 2^32 *)
130-proc checksum(data: []U8): U32;
131+proc checksum(data: []const U8): U32;
131132 var i: INT; sum: U32;
132133 begin
133134 for i := 0 to LEN(data) - 1 do sum := sum + U32(data[i]); end;
@@ -135,7 +136,7 @@
135136 end;
136137
137138 (* writes a frame to out, which must have room; its length *)
138-proc pack(out: []U8; tag, data: []U8): INT;
139+proc pack(out: []U8; tag, data: []const U8): INT;
139140 begin
140141 COPY(out[0..4], tag);
141142 Str.putBE(out[4..6], U64(LEN(data)));
@@ -178,8 +179,8 @@
178179 import Out, Str, Math, Fmt;
179180
180181 (* the sum of the numbers in a comma separated list, or -1 if any isn't *)
181-proc total(s: []U8): I64;
182-var part: []U8; sum, n: I64; ok, more: BOOL;
182+proc total(s: []const U8): I64;
183+var part: []const U8; sum, n: I64; ok, more: BOOL;
183184 begin
184185 more := true;
185186 while more do

site/content/advanced/03-collections.txt (history) modified +4 -4

@@ -59,7 +59,7 @@
5959
6060 ```raz Ages run
6161 module Ages;
62-import Out, Fmt, Str, Heap, Book := Map([]U8, INT, Str.hash, Str.eq);
62+import Out, Fmt, Str, Heap, Book := Map([]const U8, INT, Str.hash, Str.eq);
6363
6464 proc main*;
6565 var m: Book.Map; age: INT; found, ok: BOOL; i: INT; v: INT;
@@ -100,11 +100,11 @@
100100 ```raz Freq run
101101 module Freq;
102102 import Fmt, Str, Heap,
103- Count := Map([]U8, INT, Str.hash, Str.eq),
103+ Count := Map([]const U8, INT, Str.hash, Str.eq),
104104 Entries := Vec(Entry),
105105 Rank := Sort(Entry, before);
106106
107-type Entry = record word: []U8; n: INT; end;
107+type Entry = record word: []const U8; n: INT; end;
108108
109109 const Text = "the cat and the hat and the bat sat on the mat and the cat sat";
110110
@@ -118,7 +118,7 @@
118118 proc main*;
119119 var
120120 m: Count.Map; entries: Entries.Vec; i, k, found: INT;
121- w, rest: []U8; key: []U8; v: INT; all: []Entry;
121+ w, rest: []const U8; key: []const U8; v: INT; all: []Entry;
122122 begin
123123 Count.init(m, Heap.heap);
124124 defer Count.done(m);

site/content/advanced/04-files.txt (history) modified +6 -5

@@ -42,7 +42,7 @@
4242 import Out, Sys;
4343
4444 (* the lines of the file at path, or minus an errno *)
45-proc lines(path: []U8): INT;
45+proc lines(path: []const U8): INT;
4646 var fd, n, i, count: INT; buf: [4096]U8;
4747 begin
4848 fd := Sys.openRead(path);
@@ -85,6 +85,7 @@
8585 ```raz Tree run
8686 module Tree;
8787 import Out, Sys, Str, Heap, Mem, Buf, Order := Sort([]U8, before);
88+import SYSTEM;
8889
8990 proc before(a, b: []U8): BOOL;
9091 begin
@@ -92,7 +93,7 @@
9293 end;
9394
9495 (* a file made to hold text *)
95-proc put(path, text: []U8);
96+proc put(path, text: []const U8);
9697 var fd: INT;
9798 begin
9899 fd := Sys.create(path);
@@ -102,7 +103,7 @@
102103
103104 proc main*;
104105 var
105- d: Sys.Dir; name, copy: []U8; more, ok: BOOL; i, n: INT;
106+ d: Sys.Dir; name: []const U8; copy: []U8; more, ok: BOOL; i, n: INT;
106107 info: Sys.Info; err: INT; names: [16][]U8; path: Buf.Buf;
107108 begin
108109 _ := Sys.makeDir("proj");
@@ -131,7 +132,7 @@
131132 else Out.str(", "); Out.int(info.size); Out.str(" bytes");
132133 end;
133134 Out.ln;
134- Mem.free(Heap.heap, ^U8(ADR(names[i][0])));
135+ Mem.free(Heap.heap, ^U8(SYSTEM.ADR(names[i][0])));
135136 end;
136137 Buf.done(path);
137138 end;
@@ -188,7 +189,7 @@
188189 const count = 1; (* the key of --count, which has no letter *)
189190
190191 proc main*;
191-var p: Opt.Parser; verbose: BOOL; n, i: INT; w: []U8; ok: BOOL;
192+var p: Opt.Parser; verbose: BOOL; n, i: INT; w: []const U8; ok: BOOL;
192193 begin
193194 n := 1;
194195 Opt.init(p, "tool", [

site/content/advanced/06-allocators.txt (history) modified +16 -12

@@ -22,6 +22,7 @@
2222 ```raz Bump
2323 module Bump;
2424 import Mem;
25+import SYSTEM;
2526
2627 type
2728 Bump* = record
@@ -65,6 +66,7 @@
6566 ```raz Bumping run
6667 module Bumping;
6768 import Out, Bump;
69+import SYSTEM;
6870
6971 type Point = record x, y: INT; end;
7072
@@ -73,19 +75,19 @@
7375 proc main*;
7476 var b: Bump.Bump; p, q: ^Point; many: INT; r: ^Point;
7577 begin
76- Bump.init(ADR(b), ADR(arena[0]), 256);
77- NEW(p, ADR(b.base));
78- NEW(q, ADR(b.base));
78+ Bump.init(SYSTEM.ADR(b), SYSTEM.ADR(arena[0]), 256);
79+ NEW(p, SYSTEM.ADR(b.base));
80+ NEW(q, SYSTEM.ADR(b.base));
7981 p^.x := 1; q^.y := 2;
8082 Out.int(b.used); Out.str(" bytes used by two points\n");
8183 many := 2;
8284 repeat
83- NEW(r, ADR(b.base));
85+ NEW(r, SYSTEM.ADR(b.base));
8486 if r # nil then INC(many); end;
8587 until r = nil;
8688 Out.int(many); Out.str(" points fit in 256 bytes\n");
87- Bump.reset(ADR(b));
88- NEW(r, ADR(b.base));
89+ Bump.reset(SYSTEM.ADR(b));
90+ NEW(r, SYSTEM.ADR(b.base));
8991 if r # nil then Out.str("room again after a reset\n"); end;
9092 end;
9193
@@ -101,6 +103,7 @@
101103 ```raz Counted
102104 module Counted;
103105 import Mem;
106+import SYSTEM;
104107
105108 type
106109 Counted* = record
@@ -141,26 +144,27 @@
141144
142145 ```raz Audit run
143146 module Audit;
144-import Fmt, Heap, Mem, Buf, Counted, Names := List([]U8);
147+import Fmt, Heap, Mem, Buf, Counted, Names := List([]const U8);
148+import SYSTEM;
145149
146150 proc main*;
147151 var c: Counted.Counted; b: Buf.Buf; l: Names.List; i: INT; p: ^U8;
148152 begin
149- Counted.init(ADR(c), Heap.heap);
150- Buf.init(b, ADR(c.base));
153+ Counted.init(SYSTEM.ADR(c), Heap.heap);
154+ Buf.init(b, SYSTEM.ADR(c.base));
151155 for i := 1 to 200 do Buf.str(b, "0123456789"); end;
152156 Fmt.print("a buffer of {} bytes took {} allocations\n",
153157 [Fmt.int(b.len), Fmt.int(c.allocs)]);
154158 Buf.done(b);
155159
156- Names.init(l, ADR(c.base));
160+ Names.init(l, SYSTEM.ADR(c.base));
157161 for i := 1 to 10 do _ := Names.push(l, "x"); end;
158162 Fmt.print("{} allocations in all, {} frees so far\n", [Fmt.int(c.allocs), Fmt.int(c.frees)]);
159163 Names.clear(l);
160164 Fmt.print("{} frees after clear\n", [Fmt.int(c.frees)]);
161165
162- p := Mem.alloc(ADR(c.base), 64, 8);
163- Mem.free(ADR(c.base), p);
166+ p := Mem.alloc(SYSTEM.ADR(c.base), 64, 8);
167+ Mem.free(SYSTEM.ADR(c.base), p);
164168 end;
165169
166170 end Audit.

site/content/advanced/07-static.txt (history) modified +2 -1

@@ -10,12 +10,13 @@
1010
1111 ```raz Bare
1212 module Bare;
13+import SYSTEM;
1314
1415 const Msg = "hello from raw system calls\n";
1516
1617 proc main*;
1718 begin
18- _ := SYSCALL(1, 1, ADR(Msg[0]), LEN(Msg));
19+ _ := SYSTEM.SYSCALL(1, 1, SYSTEM.ADR(Msg[0]), LEN(Msg));
1920 end;
2021
2122 end Bare.

site/content/advanced/08-linkstatic.txt (history) modified +2 -1

@@ -19,13 +19,14 @@
1919 ```raz Hello
2020 module Hello;
2121 import Out, Thread;
22+import SYSTEM;
2223
2324 var done: INT;
2425
2526 proc greet(arg: ^U8);
2627 begin
2728 Out.str("hello from thread "); Out.int(INT(arg)); Out.ln;
28- _ := XADD(ADR(done), 1);
29+ _ := XADD(done, 1);
2930 end;
3031
3132 proc main*;

site/content/advanced/09-lifecycle.txt (history) modified +1 -1

@@ -19,7 +19,7 @@
1919
2020 var steps*: INT;
2121
22-proc note(what: []U8);
22+proc note(what: []const U8);
2323 begin
2424 INC(steps);
2525 Out.int(steps); Out.str(". "); Out.str(what); Out.ln;

site/content/advanced/11-asm.txt (history) modified +8 -2

@@ -13,6 +13,7 @@
1313 ```raz Cycles run
1414 module Cycles;
1515 import Out;
16+import SYSTEM;
1617
1718 proc rdtsc*: U64;
1819 code
@@ -50,6 +51,7 @@
5051 ```raz Bits run
5152 module Bits;
5253 import Out;
54+import SYSTEM;
5355
5456 (* the bytes of x in the opposite order: bswap rax, which the assembler
5557 doesn't know, written as its bytes *)
@@ -87,6 +89,7 @@
8789 ```raz Sum run
8890 module Sum;
8991 import Out;
92+import SYSTEM;
9093
9194 (* the sum of the INTs of a *)
9295 proc sum(a: []INT): INT;
@@ -131,9 +134,10 @@
131134 ```raz Trim run
132135 module Trim;
133136 import Out;
137+import SYSTEM;
134138
135139 (* s past its leading blanks *)
136-proc trim(s: []U8): []U8;
140+proc trim(s: []const U8): []const U8;
137141 code
138142 mov rax, rdi;
139143 mov rdx, rsi;
@@ -168,6 +172,7 @@
168172 ```raz Divmod run
169173 module Divmod;
170174 import Out, Fmt;
175+import SYSTEM;
171176
172177 (* quotient and remainder, as idiv gives them: rax and rdx *)
173178 proc divmod(a, b: INT): (INT, INT);
@@ -206,6 +211,7 @@
206211 ```raz Vendor run
207212 module Vendor;
208213 import Out;
214+import SYSTEM;
209215
210216 (* the 12 bytes of the processor vendor, in buf (rdi) *)
211217 proc vendor(buf: ^U8);
@@ -224,7 +230,7 @@
224230 proc main*;
225231 var buf: [12]U8; i: INT; ok: BOOL;
226232 begin
227- vendor(ADR(buf[0]));
233+ vendor(SYSTEM.ADR(buf[0]));
228234 ok := true;
229235 for i := 0 to 11 do
230236 if (buf[i] < ' ') or (buf[i] > '~') then ok := false; end;

site/content/advanced/12-syscalls.txt (history) modified +21 -15

@@ -2,7 +2,7 @@
22 part: The machine
33 summary: SYSCALL directly: the numbers, the arguments, negative errno, and a cat without a library.
44 ---
5-Under `Sys` there is one built-in: `SYSCALL(n, x1, ..., x6)`. It makes Linux system call number `n` with up to six arguments, each a pointer or an integer no wider than `INT`, and gives the raw result, an `INT`. A negative result is `-errno`. That is all the interface there is; `Sys.write`, `Sys.open` and the rest of `lib/Sys.linux-x86_64.raz` are written with it, in raz, in a few lines each.
5+Under `Sys` there is one built-in, `SYSTEM.SYSCALL(n, x1, ..., x6)`, which a module reaches with `import SYSTEM;`. It makes Linux system call number `n` with up to six arguments, each a pointer or an integer no wider than `INT`, and gives the raw result, an `INT`. A negative result is `-errno`. That is all the interface there is; `Sys.write`, `Sys.open` and the rest of `lib/Sys.linux-x86_64.raz` are written with it, in raz, in a few lines each.
66
77 `SYSCALL` exists only on Linux targets. Other systems have no stable system call interface, which is why their `Sys` calls the C library instead.
88
@@ -12,14 +12,15 @@
1212
1313 ```raz Raw
1414 module Raw;
15+import SYSTEM;
1516
1617 const Msg = "straight to the kernel\n";
1718
1819 proc main*;
1920 var n: INT;
2021 begin
21- n := SYSCALL(1, 1, ADR(Msg[0]), LEN(Msg));
22- if n # LEN(Msg) then _ := SYSCALL(60, 1); end; (* exit(1), if it came up short *)
22+ n := SYSTEM.SYSCALL(1, 1, SYSTEM.ADR(Msg[0]), LEN(Msg));
23+ if n # LEN(Msg) then _ := SYSTEM.SYSCALL(60, 1); end; (* exit(1), if it came up short *)
2324 end;
2425
2526 end Raw.
@@ -39,18 +40,19 @@
3940 ```raz Open run
4041 module Open;
4142 import Out;
43+import SYSTEM;
4244
4345 proc main*;
4446 var path: [32]U8; fd: INT;
4547 begin
4648 path := "/no/such/file";
47- fd := SYSCALL(2, ADR(path[0]), 0, 0); (* open(path, O_RDONLY) *)
49+ fd := SYSTEM.SYSCALL(2, SYSTEM.ADR(path[0]), 0, 0); (* open(path, O_RDONLY) *)
4850 Out.str("open gave "); Out.int(fd); Out.ln; (* -2: ENOENT *)
4951 path := "/dev/null";
50- fd := SYSCALL(2, ADR(path[0]), 0, 0);
52+ fd := SYSTEM.SYSCALL(2, SYSTEM.ADR(path[0]), 0, 0);
5153 if fd >= 0 then
5254 Out.str("opened /dev/null\n");
53- _ := SYSCALL(3, fd); (* close *)
55+ _ := SYSTEM.SYSCALL(3, fd); (* close *)
5456 end;
5557 end;
5658
@@ -61,14 +63,15 @@
6163
6264 ## A call that fills a structure
6365
64-`uname` (call 63) fills a structure of six 65-byte fields: the system name, node name, release, version, machine and domain. It is not a type raz knows; it is an array of bytes, with a `ADR` of it given to the kernel.
66+`uname` (call 63) fills a structure of six 65-byte fields: the system name, node name, release, version, machine and domain. It is not a type raz knows; it is an array of bytes, with its `SYSTEM.ADR` given to the kernel.
6567
6668 ```raz Who run
6769 module Who;
6870 import Out;
71+import SYSTEM;
6972
7073 (* the length of the zero-ended string at the start of s *)
71-proc cstr(s: []U8): INT;
74+proc cstr(s: []const U8): INT;
7275 var n: INT;
7376 begin
7477 n := 0;
@@ -79,7 +82,7 @@
7982 proc main*;
8083 var u: [390]U8;
8184 begin
82- if SYSCALL(63, ADR(u[0])) = 0 then
85+ if SYSTEM.SYSCALL(63, SYSTEM.ADR(u[0])) = 0 then
8386 Out.str("system: "); Out.str(u[0..cstr(u[0..65])]); Out.ln;
8487 Out.str("machine: "); Out.str(u[260..260 + cstr(u[260..325])]); Out.ln;
8588 end;
@@ -95,18 +98,19 @@
9598 ```raz Pages run
9699 module Pages;
97100 import Out;
101+import SYSTEM;
98102
99103 proc main*;
100104 var p: ^U8; i: INT; total: INT;
101105 begin
102106 (* length 4096; PROT_READ|WRITE = 3; MAP_PRIVATE|ANONYMOUS = 22H; no fd *)
103- p := ^U8(SYSCALL(9, 0, 4096, 3, 22H, -1, 0));
107+ p := ^U8(SYSTEM.SYSCALL(9, 0, 4096, 3, 22H, -1, 0));
104108 if INT(p) < 0 then Out.str("no memory\n"); return; end;
105109 Out.int(INT(p[0]) + p[4095]); Out.str(" (zero, as promised)\n");
106110 for i := 0 to 4095 do p[i] := U8(i); end;
107111 for i := 0 to 4095 do INC(total, p[i]); end;
108112 Out.int(total); Out.ln;
109- _ := SYSCALL(11, p, 4096);
113+ _ := SYSTEM.SYSCALL(11, p, 4096);
110114 end;
111115
112116 end Pages.
@@ -118,19 +122,20 @@
118122
119123 ```raz Cat
120124 module Cat;
125+import SYSTEM;
121126
122127 proc main*: INT;
123128 var buf: [4096]U8; n, wrote, at: INT;
124129 begin
125- n := SYSCALL(0, 0, ADR(buf[0]), LEN(buf));
130+ n := SYSTEM.SYSCALL(0, 0, SYSTEM.ADR(buf[0]), LEN(buf));
126131 while n > 0 do
127132 at := 0;
128133 while at < n do (* a write may be partial *)
129- wrote := SYSCALL(1, 1, ADR(buf[at]), n - at);
134+ wrote := SYSTEM.SYSCALL(1, 1, SYSTEM.ADR(buf[at]), n - at);
130135 if wrote < 0 then return 1; end;
131136 INC(at, wrote);
132137 end;
133- n := SYSCALL(0, 0, ADR(buf[0]), LEN(buf));
138+ n := SYSTEM.SYSCALL(0, 0, SYSTEM.ADR(buf[0]), LEN(buf));
134139 end;
135140 if n < 0 then return 1; end;
136141 return 0;
@@ -155,6 +160,7 @@
155160 ```raz Ppid run
156161 module Ppid;
157162 import Out;
163+import SYSTEM;
158164
159165 (* getppid, call 110: no arguments *)
160166 proc getppid: INT;
@@ -166,7 +172,7 @@
166172
167173 proc main*;
168174 begin
169- if (getppid() > 0) and (SYSCALL(110) = getppid()) then
175+ if (getppid() > 0) and (SYSTEM.SYSCALL(110) = getppid()) then
170176 Out.str("two ways to ask, one answer\n");
171177 end;
172178 end;

site/content/advanced/14-volatile.txt (history) modified +7 -4

@@ -13,6 +13,7 @@
1313 ```raz Uart run
1414 module Uart;
1515 import Out;
16+import SYSTEM;
1617
1718 type
1819 Regs = record
@@ -26,7 +27,7 @@
2627 proc main*;
2728 var dev: ^VOLATILE Regs; i: INT;
2829 begin
29- dev := ^VOLATILE Regs(ADR(page)); (* the conversion is written *)
30+ dev := ^VOLATILE Regs(SYSTEM.ADR(page)); (* the conversion is written *)
3031 dev^.control := 1;
3132 dev^.status := 0FFH;
3233 for i := 0 to 2 do
@@ -60,6 +61,7 @@
6061 ```raz Poll run
6162 module Poll;
6263 import Out, Sys, Thread;
64+import SYSTEM;
6365
6466 type Dev = record status: INT; data: INT; end;
6567
@@ -68,7 +70,7 @@
6870 proc device(_arg: ^U8);
6971 var d: ^VOLATILE Dev;
7072 begin
71- d := ^VOLATILE Dev(ADR(dev));
73+ d := ^VOLATILE Dev(SYSTEM.ADR(dev));
7274 Sys.sleep(20);
7375 d^.data := 42;
7476 FENCE; (* data before the flag that announces it *)
@@ -78,7 +80,7 @@
7880 proc main*;
7981 var t: ^Thread.Thread; err: ERROR; d: ^VOLATILE Dev; spins: INT;
8082 begin
81- d := ^VOLATILE Dev(ADR(dev));
83+ d := ^VOLATILE Dev(SYSTEM.ADR(dev));
8284 t, err := Thread.spawn(device, nil);
8385 if err # nil then Out.str(MSG(err)); Out.ln; return; end;
8486 while d^.status = 0 do INC(spins); Sys.yield; end;
@@ -103,6 +105,7 @@
103105
104106 ```raz Wrong
105107 module Wrong;
108+import SYSTEM;
106109
107110 type Pair = record a, b: U32; end;
108111
@@ -111,7 +114,7 @@
111114 proc main*;
112115 var v: ^VOLATILE Pair; p: ^Pair; copy: Pair;
113116 begin
114- v := ^VOLATILE Pair(ADR(cell));
117+ v := ^VOLATILE Pair(SYSTEM.ADR(cell));
115118 p := v;
116119 copy := v^;
117120 end;

site/content/advanced/15-atomics.txt (history) modified +12 -9

@@ -6,22 +6,23 @@
66
77 | Call | Does |
88 |---|---|
9-| `XADD(p, x)` | adds `x` to `p^` and returns the value before |
10-| `XCHG(p, x)` | sets `p^ := x` and returns the value before |
11-| `CAS(p, old, new)` | if `p^ = old`, sets `p^ := new` and returns `true`; otherwise `false` |
9+| `XADD(p, x)` | adds `x` to `p` and returns the value before |
10+| `XCHG(p, x)` | sets `p := x` and returns the value before |
11+| `CAS(p, old, new)` | if `p = old`, sets `p := new` and returns `true`; otherwise `false` |
1212
13-`p` is a `^T` or `^VOLATILE T` where `T` is `INT`, `UINT` or a pointer type (`INT` or `UINT` for `XADD`). They compile to single x86-64 instructions: `lock xadd`, `xchg` (which is always locked when it touches memory) and `lock cmpxchg`:
13+`p` is a variable (or `q^` through a pointer `q`, even `^VOLATILE`) of type `INT`, `UINT` or a pointer type (`INT` or `UINT` for `XADD`); the atomic works on the variable itself, so no `ADR` is taken. A module that uses them says `import SYSTEM;`. They compile to single x86-64 instructions: `lock xadd`, `xchg` (which is always locked when it touches memory) and `lock cmpxchg`:
1414
1515 ```raz Ops
1616 module Ops;
17+import SYSTEM;
1718
1819 var counter: INT;
1920
2021 proc main*;
2122 begin
22- _ := XADD(ADR(counter), 5);
23- _ := XCHG(ADR(counter), 100);
24- _ := CAS(ADR(counter), 100, 7);
23+ _ := XADD(counter, 5);
24+ _ := XCHG(counter, 100);
25+ _ := CAS(counter, 100, 7);
2526 end;
2627
2728 end Ops.
@@ -38,6 +39,7 @@
3839 ```raz Tickets run
3940 module Tickets;
4041 import Out, Thread;
42+import SYSTEM;
4143
4244 var
4345 next: INT; (* the next ticket to give out *)
@@ -47,7 +49,7 @@
4749 var i, t: INT;
4850 begin
4951 for i := 1 to 1000 do
50- t := XADD(ADR(next), 1); (* a ticket no other thread gets *)
52+ t := XADD(next, 1); (* a ticket no other thread gets *)
5153 seen[t] := 1;
5254 end;
5355 end;
@@ -76,6 +78,7 @@
7678 ```raz Peak run
7779 module Peak;
7880 import Out, Thread;
81+import SYSTEM;
7982
8083 var peak: INT;
8184
@@ -85,7 +88,7 @@
8588 begin
8689 seen := peak;
8790 while seen < v do
88- if CAS(ADR(peak), seen, v) then return; end;
91+ if CAS(peak, seen, v) then return; end;
8992 seen := peak; (* someone changed it: look again *)
9093 end;
9194 end;

site/content/advanced/16-ordering.txt (history) modified +6 -4

@@ -15,6 +15,7 @@
1515 ```raz Publish run
1616 module Publish;
1717 import Out, Sys, Thread;
18+import SYSTEM;
1819
1920 type Message = record id: INT; text: [32]U8; len: INT; end;
2021
@@ -28,7 +29,7 @@
2829 msg.id := 7;
2930 msg.text := "built before the flag";
3031 msg.len := 21;
31- _ := XCHG(ADR(ready), 1); (* publish *)
32+ _ := XCHG(ready, 1); (* publish *)
3233 end;
3334
3435 proc main*;
@@ -36,7 +37,7 @@
3637 begin
3738 t, err := Thread.spawn(producer, nil);
3839 if err # nil then Out.str(MSG(err)); Out.ln; return; end;
39- while XADD(ADR(ready), 0) = 0 do Sys.yield; end; (* wait for it *)
40+ while XADD(ready, 0) = 0 do Sys.yield; end; (* wait for it *)
4041 Out.int(msg.id); Out.char(' '); Out.str(msg.text[0..msg.len]); Out.ln;
4142 Thread.join(t);
4243 end;
@@ -53,6 +54,7 @@
5354 ```raz Fenced run
5455 module Fenced;
5556 import Out, Sys, Thread;
57+import SYSTEM;
5658
5759 var
5860 data: INT; (* ordinary *)
@@ -61,7 +63,7 @@
6163 proc producer(_arg: ^U8);
6264 var f: ^VOLATILE INT;
6365 begin
64- f := ^VOLATILE INT(ADR(flag));
66+ f := ^VOLATILE INT(SYSTEM.ADR(flag));
6567 Sys.sleep(10);
6668 data := 1234;
6769 FENCE; (* data is written before the flag is *)
@@ -71,7 +73,7 @@
7173 proc main*;
7274 var t: ^Thread.Thread; err: ERROR; f: ^VOLATILE INT;
7375 begin
74- f := ^VOLATILE INT(ADR(flag));
76+ f := ^VOLATILE INT(SYSTEM.ADR(flag));
7577 t, err := Thread.spawn(producer, nil);
7678 if err # nil then Out.str(MSG(err)); Out.ln; return; end;
7779 while f^ = 0 do Sys.yield; end;

site/content/advanced/17-lockfree.txt (history) modified +8 -6

@@ -11,6 +11,7 @@
1111 ```raz Treiber run
1212 module Treiber;
1313 import Out, Thread;
14+import SYSTEM;
1415
1516 type Node = record next: ^Node; val: INT; end;
1617
@@ -26,7 +27,7 @@
2627 begin
2728 old := top;
2829 n^.next := old;
29- while not CAS(ADR(top), old, n) do
30+ while not CAS(top, old, n) do
3031 old := top; (* lost the race: look again *)
3132 n^.next := old;
3233 end;
@@ -38,7 +39,7 @@
3839 first := INT(arg) * Each;
3940 for i := 0 to Each - 1 do
4041 nodes[first + i].val := first + i;
41- push(ADR(nodes[first + i]));
42+ push(SYSTEM.ADR(nodes[first + i]));
4243 end;
4344 end;
4445
@@ -75,11 +76,12 @@
7576
7677 - **Never reuse a node while anyone might hold it.** Nodes from an arena that is reset only when all threads are done, as the nodes above come from a fixed array, can't come back as a different node.
7778 - **Add a counter to the head**, so that A-B-A is A1-B2-A3. That is a word and a counter swapped together, and raz's `CAS` is for one word, so a program packs a small index and a counter into one `UINT`.
78-- **Don't pop concurrently.** Many threads push and one thread, at the end, takes everything with a single `XCHG(ADR(top), nil)` and walks the list in peace. A stack used this way for work lists and logs needs no more.
79+- **Don't pop concurrently.** Many threads push and one thread, at the end, takes everything with a single `XCHG(top, nil)` and walks the list in peace. A stack used this way for work lists and logs needs no more.
7980
8081 ```raz Drain run
8182 module Drain;
8283 import Out, Thread;
84+import SYSTEM;
8385
8486 type Node = record next: ^Node; val: INT; end;
8587
@@ -94,7 +96,7 @@
9496 begin
9597 old := top;
9698 n^.next := old;
97- while not CAS(ADR(top), old, n) do
99+ while not CAS(top, old, n) do
98100 old := top;
99101 n^.next := old;
100102 end;
@@ -106,7 +108,7 @@
106108 first := INT(arg) * Each;
107109 for i := 0 to Each - 1 do
108110 nodes[first + i].val := 1;
109- push(ADR(nodes[first + i]));
111+ push(SYSTEM.ADR(nodes[first + i]));
110112 end;
111113 end;
112114
@@ -118,7 +120,7 @@
118120 if err # nil then Out.str(MSG(err)); Out.ln; return; end;
119121 end;
120122 for i := 0 to Threads - 1 do Thread.join(th[i]); end;
121- all := XCHG(ADR(top), nil); (* take the whole stack, atomically *)
123+ all := XCHG(top, nil); (* take the whole stack, atomically *)
122124 n := all;
123125 while n # nil do INC(count); n := n^.next; end;
124126 Out.int(count); Out.str(" taken at once, ");

site/content/advanced/18-ring.txt (history) modified +8 -6

@@ -17,6 +17,7 @@
1717 ```raz Ring
1818 module Ring;
1919 import Sys;
20+import SYSTEM;
2021
2122 const Size* = 64;
2223
@@ -31,9 +32,9 @@
3132 var t: INT;
3233 begin
3334 t := r^.tail; (* mine: an ordinary read *)
34- while t - XADD(ADR(r^.head), 0) = Size do Sys.yield; end; (* full *)
35+ while t - XADD(r^.head, 0) = Size do Sys.yield; end; (* full *)
3536 r^.slots[t mod Size] := v;
36- _ := XCHG(ADR(r^.tail), t + 1); (* publish the slot *)
37+ _ := XCHG(r^.tail, t + 1); (* publish the slot *)
3738 end;
3839
3940 (* the next value, waiting while the ring is empty *)
@@ -41,9 +42,9 @@
4142 var h, v: INT;
4243 begin
4344 h := r^.head; (* mine *)
44- while XADD(ADR(r^.tail), 0) = h do Sys.yield; end; (* empty *)
45+ while XADD(r^.tail, 0) = h do Sys.yield; end; (* empty *)
4546 v := r^.slots[h mod Size];
46- _ := XCHG(ADR(r^.head), h + 1); (* give the slot back *)
47+ _ := XCHG(r^.head, h + 1); (* give the slot back *)
4748 return v;
4849 end;
4950
@@ -57,6 +58,7 @@
5758 ```raz Pipe run
5859 module Pipe;
5960 import Out, Fmt, Ring, Thread;
61+import SYSTEM;
6062
6163 const Count = 100000;
6264
@@ -67,14 +69,14 @@
6769 proc producer(_arg: ^U8);
6870 var i: INT;
6971 begin
70- for i := 1 to Count do Ring.put(ADR(ring), i); end;
72+ for i := 1 to Count do Ring.put(SYSTEM.ADR(ring), i); end;
7173 end;
7274
7375 proc consumer(_arg: ^U8);
7476 var i, v: INT;
7577 begin
7678 for i := 1 to Count do
77- v := Ring.take(ADR(ring));
79+ v := Ring.take(SYSTEM.ADR(ring));
7880 if v # i then INC(outOfOrder); end;
7981 sum := sum + v;
8082 end;

site/content/tour/01-hello.txt (history) modified +3 -2

@@ -80,11 +80,12 @@
8080
8181 ## Arguments
8282
83-`ARGC()` is the number of command line arguments, with the program's own name first, and `ARGV()` the arguments, as C strings. Most programs read them through `Files.arg` and `Opt`, which the advanced tour shows; here is the raw form.
83+`ARGC()` is the number of command line arguments, with the program's own name first, and `SYSTEM.ARGV()` the arguments, as C strings. Most programs read them through `Files.arg` and `Opt`, which the advanced tour shows; here is the raw form.
8484
8585 ```raz Args
8686 module Args;
8787 import Out;
88+import SYSTEM;
8889
8990 (* The length of a C string. *)
9091 proc len(s: ^U8): INT;
@@ -99,7 +100,7 @@
99100 var i: INT; s: ^U8;
100101 begin
101102 for i := 1 to ARGC() - 1 do
102- s := ARGV()[i];
103+ s := SYSTEM.ARGV()[i];
103104 Out.str(s[0..len(s)]);
104105 Out.ln;
105106 end;

site/content/tour/03-integers.txt (history) modified +1 -1

@@ -118,7 +118,7 @@
118118 const Prime = 100000001B3H;
119119
120120 (* FNV-1a: xor each byte in, then multiply, all modulo 2^64 *)
121-proc fnv(s: []U8): U64;
121+proc fnv(s: []const U8): U64;
122122 var h: U64; i: INT;
123123 begin
124124 h := Offset;

site/content/tour/04-values.txt (history) modified +1 -1

@@ -18,7 +18,7 @@
1818 import Out;
1919
2020 proc main*;
21-var i: I32; s: []U8;
21+var i: I32; s: []const U8;
2222 begin
2323 s := "four";
2424 i := 3;

site/content/tour/05-procedures.txt (history) modified +1 -1

@@ -141,7 +141,7 @@
141141 end;
142142
143143 proc main*;
144-var ops: [3]Op; names: [3][]U8; i: INT;
144+var ops: [3]Op; names: [3][]const U8; i: INT;
145145 begin
146146 ops := [add, sub, mul];
147147 names := ["add", "sub", "mul"];

site/content/tour/06-slices.txt (history) modified +4 -4

@@ -92,14 +92,14 @@
9292
9393 ## Strings
9494
95-A string is a slice of bytes, `[]U8`, in UTF-8, and a string constant's bytes are constant. A `[8]U8` made from one is a copy you can change. Arrays and slices of integers compare with `=` and `#`, so strings do too. `Str` has the rest: ordering, finding, hashing, prefixes and suffixes, trimming, taking apart, ASCII case and kinds of character, UTF-8, and numbers to and from text.
95+A string is a slice of bytes, `[]U8`, in UTF-8, and a string constant is a `[]const U8`: a procedure that only reads a string takes that, and one that writes takes `[]U8`. A `[8]U8` made from one is a copy you can change. Arrays and slices of integers compare with `=` and `#`, so strings do too. `Str` has the rest: ordering, finding, hashing, prefixes and suffixes, trimming, taking apart, ASCII case and kinds of character, UTF-8, and numbers to and from text.
9696
9797 ```raz Words run
9898 module Words;
9999 import Out, Str;
100100
101101 proc main*;
102-var s: []U8; n: I64; ok: BOOL; name: [8]U8;
102+var s: []const U8; n: I64; ok: BOOL; name: [8]U8;
103103 begin
104104 s := "hello, world";
105105 Out.str(s[7..LEN(s)]); Out.ln;
@@ -141,7 +141,7 @@
141141 const Text = "name = raz\nkind = language\nyear = 2026\n";
142142
143143 proc main*;
144-var line, rest, key, val, w: []U8; more, ok: BOOL;
144+var line, rest, key, val, w: []const U8; more, ok: BOOL;
145145 begin
146146 (* line by line, then each line at its = *)
147147 line, rest, more := Str.cut(Text, "\n");
@@ -207,7 +207,7 @@
207207 import Out, Str, Width;
208208
209209 proc main*;
210-var s: []U8; c: I32; i, size: INT; bytes: [4]U8; k: INT;
210+var s: []const U8; c: I32; i, size: INT; bytes: [4]U8; k: INT;
211211 begin
212212 s := "caf\u{E9} \u{3BB}";
213213 Out.int(LEN(s)); Out.str(" bytes, ");

site/content/tour/07-records.txt (history) modified +17 -14

@@ -5,7 +5,7 @@
55 ```raz Boxes run
66 ```
77
8-A record is laid out as C lays out a struct. `{lo: ..., hi: ...}` builds one, naming its fields; the ones it leaves out are zero. `^Box` is a pointer to a `Box`, `ADR(b)` the address of `b`, and `b^` what a pointer points at. A pointer is never the record: it is `b^.lo`, never `b.lo`, so you always see where the memory is reached through an address.
8+A record is laid out as C lays out a struct. `{lo: ..., hi: ...}` builds one, naming its fields; the ones it leaves out are zero. `^Box` is a pointer to a `Box`, `SYSTEM.ADR(b)` the address of `b`, and `b^` what a pointer points at. A pointer is never the record: it is `b^.lo`, never `b.lo`, so you always see where the memory is reached through an address.
99
1010 ## Copies and literals
1111
@@ -49,7 +49,7 @@
4949 module Planets;
5050 import Fmt;
5151
52-type Planet = record name: []U8; moons: INT; water: BOOL; end;
52+type Planet = record name: []const U8; moons: INT; water: BOOL; end;
5353
5454 const Inner: []Planet = [
5555 {name: "Mercury"},
@@ -95,7 +95,7 @@
9595 end Layout.
9696 ```
9797
98-A field of a packed record may sit at any address, and is read and written like any other; but it has no address of its own to take with `ADR`.
98+A field of a packed record may sit at any address, and is read and written like any other; but it has no address of its own to take with `SYSTEM.ADR`.
9999
100100 ## Bytes as numbers and records: views
101101
@@ -162,13 +162,14 @@
162162
163163 ## Pointers
164164
165-`ADR(x)` takes the address of a variable, `^T` is a pointer to a `T`, and `p^` is what it points at. A pointer that points nowhere is `nil`, and following it stops the program with a message that names the place, so a forgotten check is found at once instead of corrupting memory.
165+`SYSTEM.ADR(x)` (a module that uses it says `import SYSTEM;`) takes the address of a variable, `^T` is a pointer to a `T`, and `p^` is what it points at. A pointer that points nowhere is `nil`, and following it stops the program with a message that names the place, so a forgotten check is found at once instead of corrupting memory.
166166
167167 ```raz Pointers run
168168 module Pointers;
169169 import Out;
170+import SYSTEM;
170171
171-type Counter = record hits: INT; name: []U8; end;
172+type Counter = record hits: INT; name: []const U8; end;
172173
173174 proc hit(c: ^Counter);
174175 begin
@@ -188,12 +189,12 @@
188189 var c: Counter; x, y: INT; p: ^INT;
189190 begin
190191 c.name := "front door";
191- hit(ADR(c)); hit(ADR(c)); hit(nil);
192+ hit(SYSTEM.ADR(c)); hit(SYSTEM.ADR(c)); hit(nil);
192193 Out.str(c.name); Out.str(": "); Out.int(c.hits); Out.ln;
193194 x := 1; y := 2;
194- swap(ADR(x), ADR(y));
195+ swap(SYSTEM.ADR(x), SYSTEM.ADR(y));
195196 Out.int(x); Out.char(' '); Out.int(y); Out.ln;
196- p := ADR(x);
197+ p := SYSTEM.ADR(x);
197198 p^ := p^ + 40;
198199 Out.int(x); Out.ln;
199200 end;
@@ -208,6 +209,7 @@
208209 ```raz Chain run
209210 module Chain;
210211 import Out;
212+import SYSTEM;
211213
212214 type Node = record next: ^Node; val: INT; end;
213215
@@ -218,7 +220,7 @@
218220 for i := 0 to 3 do
219221 nodes[i].val := (i + 1) * 10;
220222 nodes[i].next := head; (* each new one in front *)
221- head := ADR(nodes[i]);
223+ head := SYSTEM.ADR(nodes[i]);
222224 end;
223225 n := head;
224226 while n # nil do
@@ -233,17 +235,18 @@
233235
234236 ## A slice's pointer
235237
236-A slice is a pointer and a length, and a record can keep the two apart: an interpreter keeps a string so, and C takes one so. `LEN(s)` is the length. The pointer looks like `ADR(s[0])`, the address of the first element, and that works until the slice is empty: then there is no first element, `s[0]` is out of range, and the program stops.
238+A slice is a pointer and a length, and a record can keep the two apart: an interpreter keeps a string so, and C takes one so. `LEN(s)` is the length. The pointer looks like `SYSTEM.ADR(s[0])`, the address of the first element, and that works until the slice is empty: then there is no first element, `s[0]` is out of range, and the program stops.
237239
238240 ```raz Empty fail
239241 module Empty;
240242 import Out;
243+import SYSTEM;
241244
242245 type Span = record p: ^U8; n: INT; end;
243246
244-proc spanOf(s: []U8): Span;
247+proc spanOf(s: []const U8): Span;
245248 begin
246- return {p: ADR(s[0]), n: LEN(s)};
249+ return {p: SYSTEM.ADR(s[0]), n: LEN(s)};
247250 end;
248251
249252 proc main*;
@@ -258,9 +261,9 @@
258261 end Empty.
259262 ```
260263
261-`PTR(s)` is the pointer itself, a `^U8` for a `[]U8`, with no check. For a slice that isn't empty it is `ADR(s[0])`; for an empty one it is what the slice holds, `nil` for one never set. So nothing needs testing first, and `sp.p[0..sp.n]` makes the slice again, empty when `sp.n` is 0, whatever `sp.p` is:
264+`SYSTEM.PTR(s)` is the pointer itself, a `^U8` for a `[]U8`, with no check. For a slice that isn't empty it is `SYSTEM.ADR(s[0])`; for an empty one it is what the slice holds, `nil` for one never set. So nothing needs testing first, and `sp.p[0..sp.n]` makes the slice again, empty when `sp.n` is 0, whatever `sp.p` is:
262265
263266 ```raz Spans run
264267 ```
265268
266-Of an array, `PTR(a)` is the address of its first element, a `^T` where `ADR(a)` is a `^[n]T`. Reading through the pointer is as unchecked as through any pointer: the length is yours to keep with it. The compiler says so when the two are mixed up: a slice given where a pointer is wanted is an error whose help says to write `PTR(s)` and `LEN(s)`, and a pointer given where a slice is, `p[0..n]`.
269+Of an array, `SYSTEM.PTR(a)` is the address of its first element, a `^T` where `SYSTEM.ADR(a)` is a `^[n]T`. Reading through the pointer is as unchecked as through any pointer: the length is yours to keep with it. The compiler says so when the two are mixed up: a slice given where a pointer is wanted is an error whose help says to write `PTR(s)` and `LEN(s)`, and a pointer given where a slice is, `p[0..n]`.

site/content/tour/08-variants.txt (history) modified +6 -6

@@ -15,7 +15,7 @@
1515
1616 type Day = (mon, tue, wed, thu, fri, sat, sun);
1717
18-proc name(d: Day): []U8;
18+proc name(d: Day): []const U8;
1919 begin
2020 match d of
2121 | mon: return "Monday";
@@ -92,7 +92,7 @@
9292 number(n: INT);
9393 plus;
9494 times;
95- word(s: []U8);
95+ word(s: []const U8);
9696 end;
9797
9898 proc describe(t: Token);
@@ -142,7 +142,7 @@
142142 module Classify;
143143 import Out;
144144
145-proc kind(c: I32): []U8;
145+proc kind(c: I32): []const U8;
146146 begin
147147 match c of
148148 | ' ', '\t', '\n': return "space";
@@ -153,7 +153,7 @@
153153 end;
154154
155155 proc main*;
156-var s: []U8; i: INT;
156+var s: []const U8; i: INT;
157157 begin
158158 s := "x1 + _y";
159159 for i := 0 to LEN(s) - 1 do
@@ -174,7 +174,7 @@
174174
175175 type Op = (none, add, sub, show);
176176
177-proc op(word: []U8): Op;
177+proc op(word: []const U8): Op;
178178 begin
179179 match word of
180180 | "add", "plus", "+": return add;
@@ -185,7 +185,7 @@
185185 end;
186186
187187 proc main*;
188-var total: INT; words: [6][]U8; i: INT;
188+var total: INT; words: [6][]const U8; i: INT;
189189 begin
190190 words := ["add", "plus", "sub", "show", "times", "-"];
191191 for i := 0 to 5 do

site/content/tour/09-memory.txt (history) modified +5 -3

@@ -14,6 +14,7 @@
1414 ```raz Pool run
1515 module Pool;
1616 import Out, Mem, Arena;
17+import SYSTEM;
1718
1819 type Item = record id: INT; next: ^Item; end;
1920
@@ -44,15 +45,15 @@
4445 var ar: Arena.Arena; small: Arena.Arena; list: ^Item;
4546 begin
4647 if not Arena.init(ar, 1000000) then return; end;
47- list := make(1000, ADR(ar.base));
48+ list := make(1000, SYSTEM.ADR(ar.base));
4849 Out.int(count(list)); Out.str(" items from a big arena\n");
4950 Arena.reset(ar); (* every allocation forgotten *)
50- list := make(10, ADR(ar.base));
51+ list := make(10, SYSTEM.ADR(ar.base));
5152 Out.int(count(list)); Out.str(" after a reset\n");
5253 Arena.done(ar);
5354
5455 if not Arena.init(small, 4096) then return; end;
55- list := make(1000, ADR(small.base));
56+ list := make(1000, SYSTEM.ADR(small.base));
5657 if list = nil then Out.str("a small arena ran out, and said so\n"); end;
5758 Arena.done(small);
5859 end;
@@ -69,6 +70,7 @@
6970 ```raz Free run
7071 module Free;
7172 import Out, Mem, Heap;
73+import SYSTEM;
7274
7375 type Cell = record val: INT; end;
7476

site/content/tour/10-errors.txt (history) modified +4 -4

@@ -21,7 +21,7 @@
2121 Range*: ERROR = "out of range";
2222
2323 (* a percentage, 0 to 100 *)
24-proc percent(s: []U8): (INT, ERROR);
24+proc percent(s: []const U8): (INT, ERROR);
2525 var n: I64; ok: BOOL;
2626 begin
2727 if LEN(s) = 0 then return 0, Empty; end;
@@ -32,7 +32,7 @@
3232 end;
3333
3434 proc main*;
35-var inputs: [4][]U8; i, n: INT; err: ERROR;
35+var inputs: [4][]const U8; i, n: INT; err: ERROR;
3636 begin
3737 inputs := ["42", "", "forty", "140"];
3838 for i := 0 to 3 do
@@ -69,7 +69,7 @@
6969 const Missing*: ERROR = "key missing";
7070
7171 (* the number after "n=" in a line like "n=17" *)
72-proc value(line: []U8): (INT, ERROR);
72+proc value(line: []const U8): (INT, ERROR);
7373 var n: I64; ok: BOOL;
7474 begin
7575 if not Str.hasPrefix(line, "n=") then return 0, Missing; end;
@@ -79,7 +79,7 @@
7979 end;
8080
8181 (* the sum of the values of two lines: the first error ends it *)
82-proc sum(a, b: []U8): (INT, ERROR);
82+proc sum(a, b: []const U8): (INT, ERROR);
8383 begin
8484 return try value(a) + try value(b), nil;
8585 end;

site/content/tour/12-checks.txt (history) modified +3 -2

@@ -18,6 +18,7 @@
1818 ```raz Nilp fail
1919 module Nilp;
2020 import Out;
21+import SYSTEM;
2122
2223 type Node = record val: INT; next: ^Node; end;
2324
@@ -31,8 +32,8 @@
3132 var a, b: Node;
3233 begin
3334 a.val := 1; b.val := 2;
34- a.next := ADR(b);
35- Out.int(last(ADR(a))); Out.ln;
35+ a.next := SYSTEM.ADR(b);
36+ Out.int(last(SYSTEM.ADR(a))); Out.ln;
3637 Out.int(last(nil)); Out.ln;
3738 end;
3839

site/content/tour/13-generics.txt (history) modified +10 -8

@@ -21,6 +21,7 @@
2121 ```raz Stk
2222 module Stk(T);
2323 import Mem;
24+import SYSTEM;
2425
2526 type
2627 Stack* = record
@@ -76,21 +77,22 @@
7677 ```raz Uses run
7778 module Uses;
7879 import Out, Heap, Nums := Stk(INT), Pairs := Stk(Pair);
80+import SYSTEM;
7981
8082 type Pair = record a, b: INT; end;
8183
8284 proc main*;
8385 var n: Nums.Stack; p: Pairs.Stack; i, v: INT; ok: BOOL; pr: Pair;
8486 begin
85- Nums.init(ADR(n), Heap.heap);
86- Pairs.init(ADR(p), Heap.heap);
87- for i := 1 to 10 do _ := Nums.push(ADR(n), i * i); end;
88- _ := Pairs.push(ADR(p), {a: 3, b: 4});
89- v, ok := Nums.pop(ADR(n));
87+ Nums.init(SYSTEM.ADR(n), Heap.heap);
88+ Pairs.init(SYSTEM.ADR(p), Heap.heap);
89+ for i := 1 to 10 do _ := Nums.push(SYSTEM.ADR(n), i * i); end;
90+ _ := Pairs.push(SYSTEM.ADR(p), {a: 3, b: 4});
91+ v, ok := Nums.pop(SYSTEM.ADR(n));
9092 Out.int(v); Out.str(" of "); Out.int(n.len + 1); Out.ln;
91- pr, ok := Pairs.pop(ADR(p));
93+ pr, ok := Pairs.pop(SYSTEM.ADR(p));
9294 Out.int(pr.a * pr.b); Out.ln;
93- _, ok := Pairs.pop(ADR(p));
95+ _, ok := Pairs.pop(SYSTEM.ADR(p));
9496 if not ok then Out.str("empty\n"); end;
9597 end;
9698
@@ -101,4 +103,4 @@
101103
102104 ## The library's generics
103105
104-The library's `List`, `Map` and `Sort` are generic modules. A `Map` takes its key hash and equality as procedure arguments: `import Count := Map([]U8, INT, Str.hash, Str.eq);`. The advanced tour uses each of them in a real program.
106+The library's `List`, `Map` and `Sort` are generic modules. A `Map` takes its key hash and equality as procedure arguments: `import Count := Map([]const U8, INT, Str.hash, Str.eq);`. The advanced tour uses each of them in a real program.

site/content/tour/14-c.txt (history) modified +3 -0

@@ -18,6 +18,7 @@
1818 ```raz Say run
1919 module Say;
2020 import Out;
21+import SYSTEM;
2122
2223 extern proc printf(format: ^U8; ...): I32;
2324 extern proc fflush(f: ^U8): I32;
@@ -48,6 +49,7 @@
4849 ```raz Env run
4950 module Env;
5051 import Out;
52+import SYSTEM;
5153
5254 extern var environ: ^^U8;
5355 extern proc strlen(s: ^U8): UINT;
@@ -74,6 +76,7 @@
7476 ```raz Roots run
7577 module Roots;
7678 import Fmt;
79+import SYSTEM;
7780
7881 link "libm.so.6";
7982

site/content/tour/15-threads.txt (history) modified +4 -2

@@ -14,6 +14,7 @@
1414 ```raz Race run
1515 module Race;
1616 import Out, Thread;
17+import SYSTEM;
1718
1819 var
1920 safe: INT; (* added to by XADD *)
@@ -23,7 +24,7 @@
2324 var i: INT;
2425 begin
2526 for i := 1 to 100000 do
26- _ := XADD(ADR(safe), 1);
27+ _ := XADD(safe, 1);
2728 INC(plain); (* unsafe: lost additions *)
2829 end;
2930 end;
@@ -88,6 +89,7 @@
8889 ```raz Jobs run
8990 module Jobs;
9091 import Out, Thread;
92+import SYSTEM;
9193
9294 type Job = record lo, hi: INT; sum: INT; end;
9395
@@ -104,7 +106,7 @@
104106 for i := 0 to 3 do
105107 jobs[i].lo := i * 250000;
106108 jobs[i].hi := (i + 1) * 250000;
107- t[i], err := Thread.spawn(work, ^U8(ADR(jobs[i])));
109+ t[i], err := Thread.spawn(work, ^U8(SYSTEM.ADR(jobs[i])));
108110 if err # nil then Out.str(MSG(err)); Out.ln; return; end;
109111 end;
110112 for i := 0 to 3 do

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

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

site/public/advanced/allocators.html (history) modified +16 -12

@@ -55,6 +55,7 @@
5555 <p>The trick that lets one procedure find its state is the same one <code>Arena</code> uses: the record puts <code>base: Mem.Allocator</code> first, so a <code>^Mem.Allocator</code> and a <code>^Bump</code> are the same address, and the procedure converts one to the other.</p>
5656 <div class="file"><span>Bump.raz</span></div><pre class="code"><code><span class="kw">module</span> Bump;
5757 <span class="kw">import</span> Mem;
58+<span class="kw">import</span> SYSTEM;
5859
5960 <span class="kw">type</span>
6061 Bump* = <span class="kw">record</span>
@@ -95,6 +96,7 @@
9596 <span class="kw">end</span> Bump.</code></pre>
9697 <div class="file"><span>Bumping.raz</span></div><pre class="code"><code><span class="kw">module</span> Bumping;
9798 <span class="kw">import</span> Out, Bump;
99+<span class="kw">import</span> SYSTEM;
98100
99101 <span class="kw">type</span> Point = <span class="kw">record</span> x, y: <span class="ty">INT</span>; <span class="kw">end</span>;
100102
@@ -103,19 +105,19 @@
103105 <span class="kw">proc</span> main*;
104106 <span class="kw">var</span> b: Bump.Bump; p, q: ^Point; many: <span class="ty">INT</span>; r: ^Point;
105107 <span class="kw">begin</span>
106- Bump.init(<span class="ty">ADR</span>(b), <span class="ty">ADR</span>(arena[<span class="number">0</span>]), <span class="number">256</span>);
107- <span class="ty">NEW</span>(p, <span class="ty">ADR</span>(b.base));
108- <span class="ty">NEW</span>(q, <span class="ty">ADR</span>(b.base));
108+ Bump.init(SYSTEM.<span class="ty">ADR</span>(b), SYSTEM.<span class="ty">ADR</span>(arena[<span class="number">0</span>]), <span class="number">256</span>);
109+ <span class="ty">NEW</span>(p, SYSTEM.<span class="ty">ADR</span>(b.base));
110+ <span class="ty">NEW</span>(q, SYSTEM.<span class="ty">ADR</span>(b.base));
109111 p^.x := <span class="number">1</span>; q^.y := <span class="number">2</span>;
110112 Out.int(b.used); Out.str(<span class="string">&quot; bytes used by two points\n&quot;</span>);
111113 many := <span class="number">2</span>;
112114 <span class="kw">repeat</span>
113- <span class="ty">NEW</span>(r, <span class="ty">ADR</span>(b.base));
115+ <span class="ty">NEW</span>(r, SYSTEM.<span class="ty">ADR</span>(b.base));
114116 <span class="kw">if</span> r # <span class="ty">nil</span> <span class="kw">then</span> <span class="ty">INC</span>(many); <span class="kw">end</span>;
115117 <span class="kw">until</span> r = <span class="ty">nil</span>;
116118 Out.int(many); Out.str(<span class="string">&quot; points fit in 256 bytes\n&quot;</span>);
117- Bump.reset(<span class="ty">ADR</span>(b));
118- <span class="ty">NEW</span>(r, <span class="ty">ADR</span>(b.base));
119+ Bump.reset(SYSTEM.<span class="ty">ADR</span>(b));
120+ <span class="ty">NEW</span>(r, SYSTEM.<span class="ty">ADR</span>(b.base));
119121 <span class="kw">if</span> r # <span class="ty">nil</span> <span class="kw">then</span> Out.str(<span class="string">&quot;room again after a reset\n&quot;</span>); <span class="kw">end</span>;
120122 <span class="kw">end</span>;
121123
@@ -129,6 +131,7 @@
129131 <p>An allocator can wrap another. This one forwards to whatever it is given and keeps count, which is how to find out what a piece of code allocates, or to prove that a path allocates nothing.</p>
130132 <div class="file"><span>Counted.raz</span></div><pre class="code"><code><span class="kw">module</span> Counted;
131133 <span class="kw">import</span> Mem;
134+<span class="kw">import</span> SYSTEM;
132135
133136 <span class="kw">type</span>
134137 Counted* = <span class="kw">record</span>
@@ -166,26 +169,27 @@
166169
167170 <span class="kw">end</span> Counted.</code></pre>
168171 <div class="file"><span>Audit.raz</span></div><pre class="code"><code><span class="kw">module</span> Audit;
169-<span class="kw">import</span> Fmt, Heap, Mem, Buf, Counted, Names := List([]<span class="ty">U8</span>);
172+<span class="kw">import</span> Fmt, Heap, Mem, Buf, Counted, Names := List([]<span class="kw">const</span> <span class="ty">U8</span>);
173+<span class="kw">import</span> SYSTEM;
170174
171175 <span class="kw">proc</span> main*;
172176 <span class="kw">var</span> c: Counted.Counted; b: Buf.Buf; l: Names.List; i: <span class="ty">INT</span>; p: ^<span class="ty">U8</span>;
173177 <span class="kw">begin</span>
174- Counted.init(<span class="ty">ADR</span>(c), Heap.heap);
175- Buf.init(b, <span class="ty">ADR</span>(c.base));
178+ Counted.init(SYSTEM.<span class="ty">ADR</span>(c), Heap.heap);
179+ Buf.init(b, SYSTEM.<span class="ty">ADR</span>(c.base));
176180 <span class="kw">for</span> i := <span class="number">1</span> <span class="kw">to</span> <span class="number">200</span> <span class="kw">do</span> Buf.str(b, <span class="string">&quot;0123456789&quot;</span>); <span class="kw">end</span>;
177181 Fmt.print(<span class="string">&quot;a buffer of {} bytes took {} allocations\n&quot;</span>,
178182 [Fmt.int(b.len), Fmt.int(c.allocs)]);
179183 Buf.done(b);
180184
181- Names.init(l, <span class="ty">ADR</span>(c.base));
185+ Names.init(l, SYSTEM.<span class="ty">ADR</span>(c.base));
182186 <span class="kw">for</span> i := <span class="number">1</span> <span class="kw">to</span> <span class="number">10</span> <span class="kw">do</span> _ := Names.push(l, <span class="string">&quot;x&quot;</span>); <span class="kw">end</span>;
183187 Fmt.print(<span class="string">&quot;{} allocations in all, {} frees so far\n&quot;</span>, [Fmt.int(c.allocs), Fmt.int(c.frees)]);
184188 Names.clear(l);
185189 Fmt.print(<span class="string">&quot;{} frees after clear\n&quot;</span>, [Fmt.int(c.frees)]);
186190
187- p := Mem.alloc(<span class="ty">ADR</span>(c.base), <span class="number">64</span>, <span class="number">8</span>);
188- Mem.free(<span class="ty">ADR</span>(c.base), p);
191+ p := Mem.alloc(SYSTEM.<span class="ty">ADR</span>(c.base), <span class="number">64</span>, <span class="number">8</span>);
192+ Mem.free(SYSTEM.<span class="ty">ADR</span>(c.base), p);
189193 <span class="kw">end</span>;
190194
191195 <span class="kw">end</span> Audit.</code></pre>

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

@@ -50,6 +50,7 @@
5050 <p>This reads the processor's time stamp counter. The result of a procedure goes in <code>rax</code>; <code>rdtsc</code> leaves the counter in <code>edx:eax</code>, so the two halves are joined first.</p>
5151 <div class="file"><span>Cycles.raz</span></div><pre class="code"><code><span class="kw">module</span> Cycles;
5252 <span class="kw">import</span> Out;
53+<span class="kw">import</span> SYSTEM;
5354
5455 <span class="kw">proc</span> rdtsc*: <span class="ty">U64</span>;
5556 <span class="kw">code</span>
@@ -77,6 +78,7 @@
7778 <p>That is why the next one is so short: the argument is already in <code>rdi</code>.</p>
7879 <div class="file"><span>Bits.raz</span></div><pre class="code"><code><span class="kw">module</span> Bits;
7980 <span class="kw">import</span> Out;
81+<span class="kw">import</span> SYSTEM;
8082
8183 <span class="comment">(* the bytes of x in the opposite order: bswap rax, which the assembler
8284 doesn&#x27;t know, written as its bytes *)</span>
@@ -111,6 +113,7 @@
111113 <p>A label, <code>name:</code>, names the place of the instruction after it, within the procedure. Jumps go to labels, and a constant of the module may stand wherever a number does. This sums a slice of <code>INT</code>s, whose address arrives in <code>rdi</code> and its length in <code>rsi</code>:</p>
112114 <div class="file"><span>Sum.raz</span></div><pre class="code"><code><span class="kw">module</span> Sum;
113115 <span class="kw">import</span> Out;
116+<span class="kw">import</span> SYSTEM;
114117
115118 <span class="comment">(* the sum of the INTs of a *)</span>
116119 <span class="kw">proc</span> sum(a: []<span class="ty">INT</span>): <span class="ty">INT</span>;
@@ -152,9 +155,10 @@
152155 <p>A slice comes back the same way, its address in <code>rax</code> and its length in <code>rdx</code>. This one returns what is left of a string past its leading blanks:</p>
153156 <div class="file"><span>Trim.raz</span></div><pre class="code"><code><span class="kw">module</span> Trim;
154157 <span class="kw">import</span> Out;
158+<span class="kw">import</span> SYSTEM;
155159
156160 <span class="comment">(* s past its leading blanks *)</span>
157-<span class="kw">proc</span> trim(s: []<span class="ty">U8</span>): []<span class="ty">U8</span>;
161+<span class="kw">proc</span> trim(s: []<span class="kw">const</span> <span class="ty">U8</span>): []<span class="kw">const</span> <span class="ty">U8</span>;
158162 <span class="kw">code</span>
159163 mov rax, rdi;
160164 mov rdx, rsi;
@@ -187,6 +191,7 @@
187191 <p>A second result goes in <code>rdx</code>. That is exactly where <code>idiv</code> leaves the remainder, so <code>divmod</code> is four instructions. Reals travel in the <code>xmm</code> registers.</p>
188192 <div class="file"><span>Divmod.raz</span></div><pre class="code"><code><span class="kw">module</span> Divmod;
189193 <span class="kw">import</span> Out, Fmt;
194+<span class="kw">import</span> SYSTEM;
190195
191196 <span class="comment">(* quotient and remainder, as idiv gives them: rax and rdx *)</span>
192197 <span class="kw">proc</span> divmod(a, b: <span class="ty">INT</span>): (<span class="ty">INT</span>, <span class="ty">INT</span>);
@@ -222,6 +227,7 @@
222227 <p>A procedure that uses <code>rbx</code> must put it back. <code>cpuid</code> clobbers <code>rbx</code>, so the vendor-string example saves it with <code>push</code> and restores it with <code>pop</code>, and keeps the buffer's address in <code>r8</code> while it works:</p>
223228 <div class="file"><span>Vendor.raz</span></div><pre class="code"><code><span class="kw">module</span> Vendor;
224229 <span class="kw">import</span> Out;
230+<span class="kw">import</span> SYSTEM;
225231
226232 <span class="comment">(* the 12 bytes of the processor vendor, in buf (rdi) *)</span>
227233 <span class="kw">proc</span> vendor(buf: ^<span class="ty">U8</span>);
@@ -240,7 +246,7 @@
240246 <span class="kw">proc</span> main*;
241247 <span class="kw">var</span> buf: [<span class="number">12</span>]<span class="ty">U8</span>; i: <span class="ty">INT</span>; ok: <span class="ty">BOOL</span>;
242248 <span class="kw">begin</span>
243- vendor(<span class="ty">ADR</span>(buf[<span class="number">0</span>]));
249+ vendor(SYSTEM.<span class="ty">ADR</span>(buf[<span class="number">0</span>]));
244250 ok := <span class="ty">true</span>;
245251 <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="number">11</span> <span class="kw">do</span>
246252 <span class="kw">if</span> (buf[i] &lt; <span class="string">&#x27; &#x27;</span>) <span class="kw">or</span> (buf[i] &gt; <span class="string">&#x27;~&#x27;</span>) <span class="kw">then</span> ok := <span class="ty">false</span>; <span class="kw">end</span>;

site/public/advanced/atomics.html (history) modified +13 -10

@@ -45,29 +45,31 @@
4545 <li><a href="ring.html">A ring buffer between threads</a></li>
4646 </ol></aside><article><p class="kicker">Shared memory &middot; lesson 15 of 18</p><h1>Atomics</h1>
4747 <p>A volatile access is one access. An atomic is one *indivisible* step: nothing can happen between its read and its write, so two threads using it can't lose each other's work. There are three, built into the language as procedures, and they are sequentially consistent on every target:</p>
48-<table><thead><tr><th>Call</th><th>Does</th></tr></thead><tbody><tr><td data-label="Call"><code>XADD(p, x)</code></td><td data-label="Does">adds <code>x</code> to <code>p^</code> and returns the value before</td></tr><tr><td data-label="Call"><code>XCHG(p, x)</code></td><td data-label="Does">sets <code>p^ := x</code> and returns the value before</td></tr><tr><td data-label="Call"><code>CAS(p, old, new)</code></td><td data-label="Does">if <code>p^ = old</code>, sets <code>p^ := new</code> and returns <code>true</code>; otherwise <code>false</code></td></tr></tbody></table>
49-<p><code>p</code> is a <code>^T</code> or <code>^VOLATILE T</code> where <code>T</code> is <code>INT</code>, <code>UINT</code> or a pointer type (<code>INT</code> or <code>UINT</code> for <code>XADD</code>). They compile to single x86-64 instructions: <code>lock xadd</code>, <code>xchg</code> (which is always locked when it touches memory) and <code>lock cmpxchg</code>:</p>
48+<table><thead><tr><th>Call</th><th>Does</th></tr></thead><tbody><tr><td data-label="Call"><code>XADD(p, x)</code></td><td data-label="Does">adds <code>x</code> to <code>p</code> and returns the value before</td></tr><tr><td data-label="Call"><code>XCHG(p, x)</code></td><td data-label="Does">sets <code>p := x</code> and returns the value before</td></tr><tr><td data-label="Call"><code>CAS(p, old, new)</code></td><td data-label="Does">if <code>p = old</code>, sets <code>p := new</code> and returns <code>true</code>; otherwise <code>false</code></td></tr></tbody></table>
49+<p><code>p</code> is a variable (or <code>q^</code> through a pointer <code>q</code>, even <code>^VOLATILE</code>) of type <code>INT</code>, <code>UINT</code> or a pointer type (<code>INT</code> or <code>UINT</code> for <code>XADD</code>); the atomic works on the variable itself, so no <code>ADR</code> is taken. A module that uses them says <code>import SYSTEM;</code>. They compile to single x86-64 instructions: <code>lock xadd</code>, <code>xchg</code> (which is always locked when it touches memory) and <code>lock cmpxchg</code>:</p>
5050 <div class="file"><span>Ops.raz</span></div><pre class="code"><code><span class="kw">module</span> Ops;
51+<span class="kw">import</span> SYSTEM;
5152
5253 <span class="kw">var</span> counter: <span class="ty">INT</span>;
5354
5455 <span class="kw">proc</span> main*;
5556 <span class="kw">begin</span>
56- _ := <span class="ty">XADD</span>(<span class="ty">ADR</span>(counter), <span class="number">5</span>);
57- _ := <span class="ty">XCHG</span>(<span class="ty">ADR</span>(counter), <span class="number">100</span>);
58- _ := <span class="ty">CAS</span>(<span class="ty">ADR</span>(counter), <span class="number">100</span>, <span class="number">7</span>);
57+ _ := <span class="ty">XADD</span>(counter, <span class="number">5</span>);
58+ _ := <span class="ty">XCHG</span>(counter, <span class="number">100</span>);
59+ _ := <span class="ty">CAS</span>(counter, <span class="number">100</span>, <span class="number">7</span>);
5960 <span class="kw">end</span>;
6061
6162 <span class="kw">end</span> Ops.</code></pre>
6263 <pre class="output term"><code><span class="cmd">$ raz -S Ops.raz | grep -i &quot;lock\|xchg&quot;</span>
6364 23 lock xadd [r8], rax
64- 8 _ := XCHG(ADR(counter), 100);
65+ 9 _ := XCHG(counter, 100);
6566 43 xchg [r8], rax
6667 67 lock cmpxchg [r8], r10</code></pre>
6768 <h2>What each one gives you</h2>
6869 <p><code>XADD</code> is a counter, or a ticket dispenser: every caller gets a different number. <code>XCHG</code> takes the old value and leaves a new one, which is a flag, a swap, or the release of a lock. <code>CAS</code> is the general one: "if nobody changed it since I looked, change it". Most lock-free code is a loop around it.</p>
6970 <div class="file"><span>Tickets.raz</span></div><pre class="code"><code><span class="kw">module</span> Tickets;
7071 <span class="kw">import</span> Out, Thread;
72+<span class="kw">import</span> SYSTEM;
7173
7274 <span class="kw">var</span>
7375 next: <span class="ty">INT</span>; <span class="comment">(* the next ticket to give out *)</span>
@@ -77,7 +79,7 @@
7779 <span class="kw">var</span> i, t: <span class="ty">INT</span>;
7880 <span class="kw">begin</span>
7981 <span class="kw">for</span> i := <span class="number">1</span> <span class="kw">to</span> <span class="number">1000</span> <span class="kw">do</span>
80- t := <span class="ty">XADD</span>(<span class="ty">ADR</span>(next), <span class="number">1</span>); <span class="comment">(* a ticket no other thread gets *)</span>
82+ t := <span class="ty">XADD</span>(next, <span class="number">1</span>); <span class="comment">(* a ticket no other thread gets *)</span>
8183 seen[t] := <span class="number">1</span>;
8284 <span class="kw">end</span>;
8385 <span class="kw">end</span>;
@@ -102,6 +104,7 @@
102104 <p>Anything that can be computed from the old value can be made atomic with a loop around <code>CAS</code>: read the value, compute the new one, and try to swap it in; if someone got there first, read again. Here is an atomic maximum, which no single instruction does.</p>
103105 <div class="file"><span>Peak.raz</span></div><pre class="code"><code><span class="kw">module</span> Peak;
104106 <span class="kw">import</span> Out, Thread;
107+<span class="kw">import</span> SYSTEM;
105108
106109 <span class="kw">var</span> peak: <span class="ty">INT</span>;
107110
@@ -111,7 +114,7 @@
111114 <span class="kw">begin</span>
112115 seen := peak;
113116 <span class="kw">while</span> seen &lt; v <span class="kw">do</span>
114- <span class="kw">if</span> <span class="ty">CAS</span>(<span class="ty">ADR</span>(peak), seen, v) <span class="kw">then</span> <span class="kw">return</span>; <span class="kw">end</span>;
117+ <span class="kw">if</span> <span class="ty">CAS</span>(peak, seen, v) <span class="kw">then</span> <span class="kw">return</span>; <span class="kw">end</span>;
115118 seen := peak; <span class="comment">(* someone changed it: look again *)</span>
116119 <span class="kw">end</span>;
117120 <span class="kw">end</span>;
@@ -148,7 +151,7 @@
148151 var tries: INT;
149152 begin
150153 tries := 0;
151- while not CAS(ADR(l.held), 0, 1) do
154+ while not CAS(l.held, 0, 1) do
152155 INC(tries);
153156 if tries = 64 then Sys.yield; tries := 0; end;
154157 end;
@@ -157,7 +160,7 @@
157160 (* Gives up l, which the caller holds. *)
158161 proc give*(var l: Lock);
159162 begin
160- _ := XCHG(ADR(l.held), 0);
163+ _ := XCHG(l.held, 0);
161164 end;</code></pre>
162165 <p>And here is a use of it: a structure with two fields that must stay in step, which no single atomic can protect.</p>
163166 <div class="file"><span>Account.raz</span></div><pre class="code"><code><span class="kw">module</span> Account;

site/public/advanced/bytes.html (history) modified +10 -9

@@ -51,7 +51,7 @@
5151 <span class="kw">import</span> Out, Str, Fmt;
5252
5353 <span class="kw">proc</span> main*;
54-<span class="kw">var</span> names: [<span class="number">4</span>][]<span class="ty">U8</span>; i, j: <span class="ty">INT</span>; t: []<span class="ty">U8</span>;
54+<span class="kw">var</span> names: [<span class="number">4</span>][]<span class="kw">const</span> <span class="ty">U8</span>; i, j: <span class="ty">INT</span>; t: []<span class="kw">const</span> <span class="ty">U8</span>;
5555 <span class="kw">begin</span>
5656 names := [<span class="string">&quot;pear&quot;</span>, <span class="string">&quot;Apple&quot;</span>, <span class="string">&quot;fig&quot;</span>, <span class="string">&quot;apple&quot;</span>];
5757 <span class="comment">(* a small insertion sort, with Str.cmp *)</span>
@@ -80,13 +80,14 @@
8080 <p>A slice of a string constant lives forever, but a slice of a buffer lives only as long as the buffer. To keep one past that, <code>Str.copy</code> takes memory from an allocator and says whether it got any.</p>
8181 <div class="file"><span>Keep.raz</span></div><pre class="code"><code><span class="kw">module</span> Keep;
8282 <span class="kw">import</span> Out, Str, Heap, Mem;
83+<span class="kw">import</span> SYSTEM;
8384
8485 <span class="kw">type</span> Entry = <span class="kw">record</span> name: []<span class="ty">U8</span>; n: <span class="ty">INT</span>; <span class="kw">end</span>;
8586
8687 <span class="kw">var</span> entries: [<span class="number">3</span>]Entry;
8788
8889 <span class="comment">(* remembers a name that the caller will overwrite *)</span>
89-<span class="kw">proc</span> remember(slot: <span class="ty">INT</span>; name: []<span class="ty">U8</span>);
90+<span class="kw">proc</span> remember(slot: <span class="ty">INT</span>; name: []<span class="kw">const</span> <span class="ty">U8</span>);
9091 <span class="kw">var</span> copy: []<span class="ty">U8</span>; ok: <span class="ty">BOOL</span>;
9192 <span class="kw">begin</span>
9293 copy, ok := Str.copy(Heap.heap, name);
@@ -104,7 +105,7 @@
104105 <span class="kw">end</span>;
105106 <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="number">2</span> <span class="kw">do</span>
106107 Out.str(entries[i].name); Out.char(<span class="string">&#x27; &#x27;</span>);
107- Mem.free(Heap.heap, ^<span class="ty">U8</span>(<span class="ty">ADR</span>(entries[i].name[<span class="number">0</span>])));
108+ Mem.free(Heap.heap, ^<span class="ty">U8</span>(SYSTEM.<span class="ty">ADR</span>(entries[i].name[<span class="number">0</span>])));
108109 <span class="kw">end</span>;
109110 Out.ln;
110111 <span class="kw">end</span>;
@@ -118,13 +119,13 @@
118119 <span class="kw">import</span> Out, Fmt, Bytes;
119120
120121 <span class="comment">(* the last part of a path: what follows its last slash *)</span>
121-<span class="kw">proc</span> base(path: []<span class="ty">U8</span>): []<span class="ty">U8</span>;
122+<span class="kw">proc</span> base(path: []<span class="kw">const</span> <span class="ty">U8</span>): []<span class="kw">const</span> <span class="ty">U8</span>;
122123 <span class="kw">begin</span>
123124 <span class="kw">return</span> path[Bytes.last(path, <span class="string">&#x27;/&#x27;</span>) + <span class="number">1</span>..<span class="ty">LEN</span>(path)];
124125 <span class="kw">end</span>;
125126
126127 <span class="kw">proc</span> main*;
127-<span class="kw">var</span> text: []<span class="ty">U8</span>; at, n, line: <span class="ty">INT</span>;
128+<span class="kw">var</span> text: []<span class="kw">const</span> <span class="ty">U8</span>; at, n, line: <span class="ty">INT</span>;
128129 <span class="kw">begin</span>
129130 Out.str(base(<span class="string">&quot;/usr/local/lib/raz/Bytes.raz&quot;</span>)); Out.char(<span class="string">&#x27; &#x27;</span>);
130131 Out.str(base(<span class="string">&quot;Out.raz&quot;</span>)); Out.ln;
@@ -158,7 +159,7 @@
158159 <span class="kw">const</span> HeaderSize = <span class="number">10</span>;
159160
160161 <span class="comment">(* the sum of the bytes of data, all mod 2^32 *)</span>
161-<span class="kw">proc</span> checksum(data: []<span class="ty">U8</span>): <span class="ty">U32</span>;
162+<span class="kw">proc</span> checksum(data: []<span class="kw">const</span> <span class="ty">U8</span>): <span class="ty">U32</span>;
162163 <span class="kw">var</span> i: <span class="ty">INT</span>; sum: <span class="ty">U32</span>;
163164 <span class="kw">begin</span>
164165 <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="ty">LEN</span>(data) - <span class="number">1</span> <span class="kw">do</span> sum := sum + <span class="ty">U32</span>(data[i]); <span class="kw">end</span>;
@@ -166,7 +167,7 @@
166167 <span class="kw">end</span>;
167168
168169 <span class="comment">(* writes a frame to out, which must have room; its length *)</span>
169-<span class="kw">proc</span> pack(out: []<span class="ty">U8</span>; tag, data: []<span class="ty">U8</span>): <span class="ty">INT</span>;
170+<span class="kw">proc</span> pack(out: []<span class="ty">U8</span>; tag, data: []<span class="kw">const</span> <span class="ty">U8</span>): <span class="ty">INT</span>;
170171 <span class="kw">begin</span>
171172 COPY(out[<span class="number">0</span>..<span class="number">4</span>], tag);
172173 Str.putBE(out[<span class="number">4</span>..<span class="number">6</span>], <span class="ty">U64</span>(<span class="ty">LEN</span>(data)));
@@ -206,8 +207,8 @@
206207 <span class="kw">import</span> Out, Str, Math, Fmt;
207208
208209 <span class="comment">(* the sum of the numbers in a comma separated list, or -1 if any isn&#x27;t *)</span>
209-<span class="kw">proc</span> total(s: []<span class="ty">U8</span>): <span class="ty">I64</span>;
210-<span class="kw">var</span> part: []<span class="ty">U8</span>; sum, n: <span class="ty">I64</span>; ok, more: <span class="ty">BOOL</span>;
210+<span class="kw">proc</span> total(s: []<span class="kw">const</span> <span class="ty">U8</span>): <span class="ty">I64</span>;
211+<span class="kw">var</span> part: []<span class="kw">const</span> <span class="ty">U8</span>; sum, n: <span class="ty">I64</span>; ok, more: <span class="ty">BOOL</span>;
211212 <span class="kw">begin</span>
212213 more := <span class="ty">true</span>;
213214 <span class="kw">while</span> more <span class="kw">do</span>

site/public/advanced/collections.html (history) modified +4 -4

@@ -93,7 +93,7 @@
9393 <h2><code>Map(K, V; hash, eq)</code></h2>
9494 <p>A hash map. Its key's hash and equality are procedure arguments, so any type can be a key. <code>put</code> replaces what a key maps to; <code>get</code> says what it maps to and whether it maps to anything; <code>remove</code> says whether the key was there. <code>next</code> walks the map: from 0, it gives the next entry and the number to pass for the one after, and -1 when there are no more.</p>
9595 <div class="file"><span>Ages.raz</span></div><pre class="code"><code><span class="kw">module</span> Ages;
96-<span class="kw">import</span> Out, Fmt, Str, Heap, Book := Map([]<span class="ty">U8</span>, <span class="ty">INT</span>, Str.hash, Str.eq);
96+<span class="kw">import</span> Out, Fmt, Str, Heap, Book := Map([]<span class="kw">const</span> <span class="ty">U8</span>, <span class="ty">INT</span>, Str.hash, Str.eq);
9797
9898 <span class="kw">proc</span> main*;
9999 <span class="kw">var</span> m: Book.Map; age: <span class="ty">INT</span>; found, ok: <span class="ty">BOOL</span>; i: <span class="ty">INT</span>; v: <span class="ty">INT</span>;
@@ -131,11 +131,11 @@
131131 <p>Counting words, then listing them by count and then alphabetically: a <code>Map</code> for the counts, a <code>Vec</code> of records to sort, however many words there are, and <code>Sort</code> with a two-level <code>less</code>.</p>
132132 <div class="file"><span>Freq.raz</span></div><pre class="code"><code><span class="kw">module</span> Freq;
133133 <span class="kw">import</span> Fmt, Str, Heap,
134- Count := Map([]<span class="ty">U8</span>, <span class="ty">INT</span>, Str.hash, Str.eq),
134+ Count := Map([]<span class="kw">const</span> <span class="ty">U8</span>, <span class="ty">INT</span>, Str.hash, Str.eq),
135135 Entries := Vec(Entry),
136136 Rank := Sort(Entry, before);
137137
138-<span class="kw">type</span> Entry = <span class="kw">record</span> word: []<span class="ty">U8</span>; n: <span class="ty">INT</span>; <span class="kw">end</span>;
138+<span class="kw">type</span> Entry = <span class="kw">record</span> word: []<span class="kw">const</span> <span class="ty">U8</span>; n: <span class="ty">INT</span>; <span class="kw">end</span>;
139139
140140 <span class="kw">const</span> Text = <span class="string">&quot;the cat and the hat and the bat sat on the mat and the cat sat&quot;</span>;
141141
@@ -149,7 +149,7 @@
149149 <span class="kw">proc</span> main*;
150150 <span class="kw">var</span>
151151 m: Count.Map; entries: Entries.Vec; i, k, found: <span class="ty">INT</span>;
152- w, rest: []<span class="ty">U8</span>; key: []<span class="ty">U8</span>; v: <span class="ty">INT</span>; all: []Entry;
152+ w, rest: []<span class="kw">const</span> <span class="ty">U8</span>; key: []<span class="kw">const</span> <span class="ty">U8</span>; v: <span class="ty">INT</span>; all: []Entry;
153153 <span class="kw">begin</span>
154154 Count.init(m, Heap.heap);
155155 <span class="kw">defer</span> Count.done(m);

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

@@ -79,7 +79,7 @@
7979 <span class="kw">import</span> Out, Sys;
8080
8181 <span class="comment">(* the lines of the file at path, or minus an errno *)</span>
82-<span class="kw">proc</span> lines(path: []<span class="ty">U8</span>): <span class="ty">INT</span>;
82+<span class="kw">proc</span> lines(path: []<span class="kw">const</span> <span class="ty">U8</span>): <span class="ty">INT</span>;
8383 <span class="kw">var</span> fd, n, i, count: <span class="ty">INT</span>; buf: [<span class="number">4096</span>]<span class="ty">U8</span>;
8484 <span class="kw">begin</span>
8585 fd := Sys.openRead(path);
@@ -122,6 +122,7 @@
122122 <p><code>Sys.info</code> says a file's size, modification time, permissions, and whether it is a directory, a regular file or a link. <code>Sys.openDir</code> and <code>nextEntry</code> list a directory, in no particular order, so a program that prints one sorts it first. A name from <code>nextEntry</code> is good only until the next call, so the program keeps a copy.</p>
123123 <div class="file"><span>Tree.raz</span></div><pre class="code"><code><span class="kw">module</span> Tree;
124124 <span class="kw">import</span> Out, Sys, Str, Heap, Mem, Buf, Order := Sort([]<span class="ty">U8</span>, before);
125+<span class="kw">import</span> SYSTEM;
125126
126127 <span class="kw">proc</span> before(a, b: []<span class="ty">U8</span>): <span class="ty">BOOL</span>;
127128 <span class="kw">begin</span>
@@ -129,7 +130,7 @@
129130 <span class="kw">end</span>;
130131
131132 <span class="comment">(* a file made to hold text *)</span>
132-<span class="kw">proc</span> put(path, text: []<span class="ty">U8</span>);
133+<span class="kw">proc</span> put(path, text: []<span class="kw">const</span> <span class="ty">U8</span>);
133134 <span class="kw">var</span> fd: <span class="ty">INT</span>;
134135 <span class="kw">begin</span>
135136 fd := Sys.create(path);
@@ -139,7 +140,7 @@
139140
140141 <span class="kw">proc</span> main*;
141142 <span class="kw">var</span>
142- d: Sys.Dir; name, copy: []<span class="ty">U8</span>; more, ok: <span class="ty">BOOL</span>; i, n: <span class="ty">INT</span>;
143+ d: Sys.Dir; name: []<span class="kw">const</span> <span class="ty">U8</span>; copy: []<span class="ty">U8</span>; more, ok: <span class="ty">BOOL</span>; i, n: <span class="ty">INT</span>;
143144 info: Sys.Info; err: <span class="ty">INT</span>; names: [<span class="number">16</span>][]<span class="ty">U8</span>; path: Buf.Buf;
144145 <span class="kw">begin</span>
145146 _ := Sys.makeDir(<span class="string">&quot;proj&quot;</span>);
@@ -168,7 +169,7 @@
168169 <span class="kw">else</span> Out.str(<span class="string">&quot;, &quot;</span>); Out.int(info.size); Out.str(<span class="string">&quot; bytes&quot;</span>);
169170 <span class="kw">end</span>;
170171 Out.ln;
171- Mem.free(Heap.heap, ^<span class="ty">U8</span>(<span class="ty">ADR</span>(names[i][<span class="number">0</span>])));
172+ Mem.free(Heap.heap, ^<span class="ty">U8</span>(SYSTEM.<span class="ty">ADR</span>(names[i][<span class="number">0</span>])));
172173 <span class="kw">end</span>;
173174 Buf.done(path);
174175 <span class="kw">end</span>;
@@ -224,7 +225,7 @@
224225 <span class="kw">const</span> count = <span class="number">1</span>; <span class="comment">(* the key of --count, which has no letter *)</span>
225226
226227 <span class="kw">proc</span> main*;
227-<span class="kw">var</span> p: Opt.Parser; verbose: <span class="ty">BOOL</span>; n, i: <span class="ty">INT</span>; w: []<span class="ty">U8</span>; ok: <span class="ty">BOOL</span>;
228+<span class="kw">var</span> p: Opt.Parser; verbose: <span class="ty">BOOL</span>; n, i: <span class="ty">INT</span>; w: []<span class="kw">const</span> <span class="ty">U8</span>; ok: <span class="ty">BOOL</span>;
228229 <span class="kw">begin</span>
229230 n := <span class="number">1</span>;
230231 Opt.init(p, <span class="string">&quot;tool&quot;</span>, [

site/public/advanced/fmt.html (history) modified +4 -4

@@ -96,7 +96,7 @@
9696 <span class="kw">import</span> Out, Buf, Fmt, Heap;
9797
9898 <span class="comment">(* &quot;name (age n)&quot;, built in a Buf the caller owns *)</span>
99-<span class="kw">proc</span> describe(<span class="kw">var</span> b: Buf.Buf; name: []<span class="ty">U8</span>; age: <span class="ty">INT</span>);
99+<span class="kw">proc</span> describe(<span class="kw">var</span> b: Buf.Buf; name: []<span class="kw">const</span> <span class="ty">U8</span>; age: <span class="ty">INT</span>);
100100 <span class="kw">begin</span>
101101 Fmt.write(b, <span class="string">&quot;{} (age {})&quot;</span>, [Fmt.str(name), Fmt.int(age)]);
102102 <span class="kw">end</span>;
@@ -123,7 +123,7 @@
123123 <span class="kw">import</span> Out;
124124
125125 <span class="comment">(* s, then spaces up to width *)</span>
126-<span class="kw">proc</span> left(s: []<span class="ty">U8</span>; width: <span class="ty">INT</span>);
126+<span class="kw">proc</span> left(s: []<span class="kw">const</span> <span class="ty">U8</span>; width: <span class="ty">INT</span>);
127127 <span class="kw">var</span> i: <span class="ty">INT</span>;
128128 <span class="kw">begin</span>
129129 Out.str(s);
@@ -142,7 +142,7 @@
142142 Out.int(n);
143143 <span class="kw">end</span>;
144144
145-<span class="kw">proc</span> row(name: []<span class="ty">U8</span>; qty, price: <span class="ty">INT</span>);
145+<span class="kw">proc</span> row(name: []<span class="kw">const</span> <span class="ty">U8</span>; qty, price: <span class="ty">INT</span>);
146146 <span class="kw">begin</span>
147147 left(name, <span class="number">10</span>);
148148 right(qty, <span class="number">5</span>);
@@ -172,7 +172,7 @@
172172 <div class="file"><span>Dump.raz</span></div><pre class="code"><code><span class="kw">module</span> Dump;
173173 <span class="kw">import</span> Out;
174174
175-<span class="kw">proc</span> dump(data: []<span class="ty">U8</span>);
175+<span class="kw">proc</span> dump(data: []<span class="kw">const</span> <span class="ty">U8</span>);
176176 <span class="kw">var</span> i: <span class="ty">INT</span>;
177177 <span class="kw">begin</span>
178178 <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="ty">LEN</span>(data) - <span class="number">1</span> <span class="kw">do</span>

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

@@ -53,7 +53,7 @@
5353
5454 <span class="kw">var</span> steps*: <span class="ty">INT</span>;
5555
56-<span class="kw">proc</span> note(what: []<span class="ty">U8</span>);
56+<span class="kw">proc</span> note(what: []<span class="kw">const</span> <span class="ty">U8</span>);
5757 <span class="kw">begin</span>
5858 <span class="ty">INC</span>(steps);
5959 Out.int(steps); Out.str(<span class="string">&quot;. &quot;</span>); Out.str(what); Out.ln;

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

@@ -67,20 +67,21 @@
6767 the Thread. Its test uses Check. *)
6868 module Thread;
6969 import Sys, Rt, Check;
70+import SYSTEM;
7071
7172 const
72- Failed*: ERROR = &quot;can&#x27;t start a thread&quot;;
73- Stack = 1 shl 20;</code></pre>
73+ Failed*: ERROR = &quot;can&#x27;t start a thread&quot;;</code></pre>
7474 <p>A program that imports it is therefore a static executable, and every thread it spawns is a Linux thread with a stack of its own, mapped with a page below it that can't be touched, so that running off the stack ends the program with a message and not with corruption.</p>
7575 <div class="file"><span>Hello.raz</span></div><pre class="code"><code><span class="kw">module</span> Hello;
7676 <span class="kw">import</span> Out, Thread;
77+<span class="kw">import</span> SYSTEM;
7778
7879 <span class="kw">var</span> done: <span class="ty">INT</span>;
7980
8081 <span class="kw">proc</span> greet(arg: ^<span class="ty">U8</span>);
8182 <span class="kw">begin</span>
8283 Out.str(<span class="string">&quot;hello from thread &quot;</span>); Out.int(<span class="ty">INT</span>(arg)); Out.ln;
83- _ := <span class="ty">XADD</span>(<span class="ty">ADR</span>(done), <span class="number">1</span>);
84+ _ := <span class="ty">XADD</span>(done, <span class="number">1</span>);
8485 <span class="kw">end</span>;
8586
8687 <span class="kw">proc</span> main*;
@@ -112,7 +113,12 @@
112113 <span class="kw">end</span>;
113114
114115 <span class="kw">end</span> Mixed.</code></pre>
115-<pre class="output term"><code><span class="cmd">$ raz Mixed.raz</span></code></pre>
116+<pre class="output term"><code><span class="cmd">$ raz Mixed.raz</span>
117+Mixed.raz:7:19: error: a string as a ^U8 needs import SYSTEM
118+ 7 | _ := C.printf(&quot;%d\n&quot;, I32(1));
119+ | ^^^^^^
120+help: it reaches memory other than through a variable: say import SYSTEM after the module&#x27;s imports
121+raz: 1 error</code></pre>
116122 <p>Which one you want depends on what the program is for. A program that calls a C library (a window system, a database client) uses C's threads, <code>C.pthread_create</code>, because the library may assume them. A program that talks only to the kernel uses <code>Thread</code>, and is a static executable.</p>
117123 <h2>The same promise for your own modules</h2>
118124 <p>The rule isn't special to <code>Thread</code>. A module of yours that makes <code>SYSCALL</code>s directly, or relies on being the only owner of signal handlers, or sets up its own stacks, says <code>link static;</code> too, and then the compiler keeps C away from it for you. The module is documenting a fact about itself, and the compiler checks it where the program is assembled.</p><p class="tryit">Save a program under its module&rsquo;s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="static.html">&larr; A program with no run time</a><a href="index.html">Contents</a><a class="next" href="lifecycle.html">Starting, ending, and signals &rarr;</a></div></article></div>

site/public/advanced/listing.html (history) modified +23 -62

@@ -185,59 +185,20 @@
185185
186186 <span class="kw">end</span> Dot2.</code></pre>
187187 <pre class="output term"><code><span class="cmd">$ raz -S Dot2.raz | sed -n &#x27;/^Dot2.dot:/,/^$/p&#x27;</span>
188-Dot2.dot:
189- 4 proc dot*(a, b: []INT): INT;
190- 0 push rbp
191- 1 mov rbp, rsp
192- 4 sub rsp, 64
193- 11 mov [rbp - 56], rbx
194- 15 xor ebx, ebx
195- 17 mov [rbp - 48], r12
196- 21 xor r12d, r12d
197- 24 mov [rbp - 40], r13
198- 28 xor r13d, r13d
199- 31 mov [rbp - 32], rdx
200- 35 mov [rbp - 24], rcx
201- 7 for i := 0 to LEN(a) - 1 do
202- 39 xor ebx, ebx
203- 41 mov r8, rsi
204- 44 lea r8, [r8 - 1]
205- 48 mov r13, r8
206- 51 cmp rbx, r13
207- 54 jg 156
208- 60 jmp 100
209- 65 nop
210- 73 nop
211- 81 nop
212- 89 nop
213- 96 add rbx, 1
214- 8 sum := sum + a[i] * b[i];
215- 100 mov r10, rbx
216- 103 shl r10, 3
217- 107 add r10, [rbp - 32]
218- 111 mov r10, [r10]
219- 114 mov r9, [rdi + rbx * 8]
220- 118 imul r9, r10
221- 122 add r12, r9
222- 125 cmp rbx, r13
223- 128 jl 96
224- 134 cmp rbx, r13
225- 137 je 156
226- 143 add rbx, 1
227- 147 cmp rbx, r13
228- 150 jle 100
229- 10 return sum;
230- 156 mov rax, r12
231- 11 end;
232- 159 mov rbx, [rbp - 56]
233- 163 mov r12, [rbp - 48]
234- 167 mov r13, [rbp - 40]
235- 171 leave
236- 172 ret
188+Dot2.raz:2:1: error: check off needs import SYSTEM
189+ 2 | check off;
190+ | ^^^^^
191+help: it reaches memory other than through a variable: say import SYSTEM after the module&#x27;s imports
192+raz: 1 error
237193 <span class="cmd">$ raz -S Dot.raz | sed -n &#x27;/^Dot.dot:/,/^$/p&#x27; | wc -l</span>
238194 61
239195 <span class="cmd">$ raz -S Dot2.raz | sed -n &#x27;/^Dot2.dot:/,/^$/p&#x27; | wc -l</span>
240-50</code></pre>
196+Dot2.raz:2:1: error: check off needs import SYSTEM
197+ 2 | check off;
198+ | ^^^^^
199+help: it reaches memory other than through a variable: say import SYSTEM after the module&#x27;s imports
200+raz: 1 error
201+0</code></pre>
241202 <p>The compares, the branches and the stubs are gone. It is faster, and it is no longer safe: an index out of range reads or writes whatever memory it reaches, and what follows isn't defined. A program turns checks off where it has measured and is sure, and says so at the top of the module, where a reader can see it.</p>
242203 <h2>Comparing with C</h2>
243204 <p><code>make kernels</code> in the compiler's sources measures <code>test/bench</code>'s kernels against the same kernels written in Pascal and compiled by Free Pascal at <code>-O2</code>. The compiler's own guide, <code>doc/compiler.md</code>, gives the numbers and says what the compiler does and doesn't do. It is not the fastest code: it is code whose every instruction you can account for.</p>
@@ -245,18 +206,18 @@
245206 <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>
246207 <pre class="output term"><code><span class="cmd">$ raz -o Dot Dot.raz</span>
247208 <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>
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>
209+0000000000403c40 &lt;Dot.dot&gt;:
210+ 403c40: push rbp
211+ 403c41: mov rbp,rsp
212+ 403c44: sub rsp,0x40
213+ 403c4b: mov QWORD PTR [rbp-0x38],rbx
214+ 403c4f: xor ebx,ebx
215+ 403c51: mov QWORD PTR [rbp-0x30],r12
216+ 403c55: xor r12d,r12d
217+ 403c58: mov QWORD PTR [rbp-0x28],r13
218+ 403c5c: xor r13d,r13d
219+ 403c5f: mov QWORD PTR [rbp-0x20],rdx
220+ 403c63: mov QWORD PTR [rbp-0x18],rcx</code></pre>
260221 <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>
261222 </main>
262223 <footer>raz is under the ISC license. <a href="../source/index.html">Source</a></footer>

site/public/advanced/lockfree.html (history) modified +8 -6

@@ -49,6 +49,7 @@
4949 <p>To push a node: point its <code>next</code> at the current head, then <code>CAS</code> the head from what you saw to the new node. If another thread pushed first, the <code>CAS</code> fails, because the head is no longer what you saw, so look again and retry. No thread ever waits for another, and none can stop the others by being slow.</p>
5050 <div class="file"><span>Treiber.raz</span></div><pre class="code"><code><span class="kw">module</span> Treiber;
5151 <span class="kw">import</span> Out, Thread;
52+<span class="kw">import</span> SYSTEM;
5253
5354 <span class="kw">type</span> Node = <span class="kw">record</span> next: ^Node; val: <span class="ty">INT</span>; <span class="kw">end</span>;
5455
@@ -64,7 +65,7 @@
6465 <span class="kw">begin</span>
6566 old := top;
6667 n^.next := old;
67- <span class="kw">while</span> <span class="kw">not</span> <span class="ty">CAS</span>(<span class="ty">ADR</span>(top), old, n) <span class="kw">do</span>
68+ <span class="kw">while</span> <span class="kw">not</span> <span class="ty">CAS</span>(top, old, n) <span class="kw">do</span>
6869 old := top; <span class="comment">(* lost the race: look again *)</span>
6970 n^.next := old;
7071 <span class="kw">end</span>;
@@ -76,7 +77,7 @@
7677 first := <span class="ty">INT</span>(arg) * Each;
7778 <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> Each - <span class="number">1</span> <span class="kw">do</span>
7879 nodes[first + i].val := first + i;
79- push(<span class="ty">ADR</span>(nodes[first + i]));
80+ push(SYSTEM.<span class="ty">ADR</span>(nodes[first + i]));
8081 <span class="kw">end</span>;
8182 <span class="kw">end</span>;
8283
@@ -107,9 +108,10 @@
107108 <h2>Pop, and the ABA problem</h2>
108109 <p>Pop takes the head off: read <code>top</code>, read its <code>next</code>, <code>CAS</code> the head from <code>top</code> to <code>next</code>. It looks as easy as push, and it isn't. Between the two reads and the <code>CAS</code>, other threads may pop that node, pop others, and push the same node back, now with a different <code>next</code>. The head is the same pointer again, so the <code>CAS</code> succeeds, and installs a <code>next</code> that is stale: nodes disappear, or the stack points into freed memory. This is the <strong>ABA problem</strong>: the word changed from A to B and back to A, and the <code>CAS</code> can't tell.</p>
109110 <p>There are three common ways out, and each has a cost:</p>
110-<ul><li><strong>Never reuse a node while anyone might hold it.</strong> Nodes from an arena that is reset only when all threads are done, as the nodes above come from a fixed array, can't come back as a different node.</li><li><strong>Add a counter to the head</strong>, so that A-B-A is A1-B2-A3. That is a word and a counter swapped together, and raz's <code>CAS</code> is for one word, so a program packs a small index and a counter into one <code>UINT</code>.</li><li><strong>Don't pop concurrently.</strong> Many threads push and one thread, at the end, takes everything with a single <code>XCHG(ADR(top), nil)</code> and walks the list in peace. A stack used this way for work lists and logs needs no more.</li></ul>
111+<ul><li><strong>Never reuse a node while anyone might hold it.</strong> Nodes from an arena that is reset only when all threads are done, as the nodes above come from a fixed array, can't come back as a different node.</li><li><strong>Add a counter to the head</strong>, so that A-B-A is A1-B2-A3. That is a word and a counter swapped together, and raz's <code>CAS</code> is for one word, so a program packs a small index and a counter into one <code>UINT</code>.</li><li><strong>Don't pop concurrently.</strong> Many threads push and one thread, at the end, takes everything with a single <code>XCHG(top, nil)</code> and walks the list in peace. A stack used this way for work lists and logs needs no more.</li></ul>
111112 <div class="file"><span>Drain.raz</span></div><pre class="code"><code><span class="kw">module</span> Drain;
112113 <span class="kw">import</span> Out, Thread;
114+<span class="kw">import</span> SYSTEM;
113115
114116 <span class="kw">type</span> Node = <span class="kw">record</span> next: ^Node; val: <span class="ty">INT</span>; <span class="kw">end</span>;
115117
@@ -124,7 +126,7 @@
124126 <span class="kw">begin</span>
125127 old := top;
126128 n^.next := old;
127- <span class="kw">while</span> <span class="kw">not</span> <span class="ty">CAS</span>(<span class="ty">ADR</span>(top), old, n) <span class="kw">do</span>
129+ <span class="kw">while</span> <span class="kw">not</span> <span class="ty">CAS</span>(top, old, n) <span class="kw">do</span>
128130 old := top;
129131 n^.next := old;
130132 <span class="kw">end</span>;
@@ -136,7 +138,7 @@
136138 first := <span class="ty">INT</span>(arg) * Each;
137139 <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> Each - <span class="number">1</span> <span class="kw">do</span>
138140 nodes[first + i].val := <span class="number">1</span>;
139- push(<span class="ty">ADR</span>(nodes[first + i]));
141+ push(SYSTEM.<span class="ty">ADR</span>(nodes[first + i]));
140142 <span class="kw">end</span>;
141143 <span class="kw">end</span>;
142144
@@ -148,7 +150,7 @@
148150 <span class="kw">if</span> err # <span class="ty">nil</span> <span class="kw">then</span> Out.str(<span class="ty">MSG</span>(err)); Out.ln; <span class="kw">return</span>; <span class="kw">end</span>;
149151 <span class="kw">end</span>;
150152 <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> Threads - <span class="number">1</span> <span class="kw">do</span> Thread.join(th[i]); <span class="kw">end</span>;
151- all := <span class="ty">XCHG</span>(<span class="ty">ADR</span>(top), <span class="ty">nil</span>); <span class="comment">(* take the whole stack, atomically *)</span>
153+ all := <span class="ty">XCHG</span>(top, <span class="ty">nil</span>); <span class="comment">(* take the whole stack, atomically *)</span>
152154 n := all;
153155 <span class="kw">while</span> n # <span class="ty">nil</span> <span class="kw">do</span> <span class="ty">INC</span>(count); n := n^.next; <span class="kw">end</span>;
154156 Out.int(count); Out.str(<span class="string">&quot; taken at once, &quot;</span>);

site/public/advanced/ordering.html (history) modified +6 -4

@@ -51,6 +51,7 @@
5151 <p>The simplest correct way to hand a value to another thread: write it with ordinary stores, then store a flag with an atomic. The reader loops on the flag with an atomic read, and then reads the data. <code>XCHG</code> is the store; <code>XADD(p, 0)</code> is the load, which adds nothing and returns the value. Both are sequentially consistent, so everything written before the flag is visible after it is seen.</p>
5252 <div class="file"><span>Publish.raz</span></div><pre class="code"><code><span class="kw">module</span> Publish;
5353 <span class="kw">import</span> Out, Sys, Thread;
54+<span class="kw">import</span> SYSTEM;
5455
5556 <span class="kw">type</span> Message = <span class="kw">record</span> id: <span class="ty">INT</span>; text: [<span class="number">32</span>]<span class="ty">U8</span>; len: <span class="ty">INT</span>; <span class="kw">end</span>;
5657
@@ -64,7 +65,7 @@
6465 msg.id := <span class="number">7</span>;
6566 msg.text := <span class="string">&quot;built before the flag&quot;</span>;
6667 msg.len := <span class="number">21</span>;
67- _ := <span class="ty">XCHG</span>(<span class="ty">ADR</span>(ready), <span class="number">1</span>); <span class="comment">(* publish *)</span>
68+ _ := <span class="ty">XCHG</span>(ready, <span class="number">1</span>); <span class="comment">(* publish *)</span>
6869 <span class="kw">end</span>;
6970
7071 <span class="kw">proc</span> main*;
@@ -72,7 +73,7 @@
7273 <span class="kw">begin</span>
7374 t, err := Thread.spawn(producer, <span class="ty">nil</span>);
7475 <span class="kw">if</span> err # <span class="ty">nil</span> <span class="kw">then</span> Out.str(<span class="ty">MSG</span>(err)); Out.ln; <span class="kw">return</span>; <span class="kw">end</span>;
75- <span class="kw">while</span> <span class="ty">XADD</span>(<span class="ty">ADR</span>(ready), <span class="number">0</span>) = <span class="number">0</span> <span class="kw">do</span> Sys.yield; <span class="kw">end</span>; <span class="comment">(* wait for it *)</span>
76+ <span class="kw">while</span> <span class="ty">XADD</span>(ready, <span class="number">0</span>) = <span class="number">0</span> <span class="kw">do</span> Sys.yield; <span class="kw">end</span>; <span class="comment">(* wait for it *)</span>
7677 Out.int(msg.id); Out.char(<span class="string">&#x27; &#x27;</span>); Out.str(msg.text[<span class="number">0</span>..msg.len]); Out.ln;
7778 Thread.join(t);
7879 <span class="kw">end</span>;
@@ -85,6 +86,7 @@
8586 <p><code>FENCE</code> is a full memory barrier: it orders the program's ordinary accesses against those of other processors and devices. With volatile accesses for the flag and a fence between the data and the flag, the same handoff works. This is the form device code takes, as in the last lesson.</p>
8687 <div class="file"><span>Fenced.raz</span></div><pre class="code"><code><span class="kw">module</span> Fenced;
8788 <span class="kw">import</span> Out, Sys, Thread;
89+<span class="kw">import</span> SYSTEM;
8890
8991 <span class="kw">var</span>
9092 data: <span class="ty">INT</span>; <span class="comment">(* ordinary *)</span>
@@ -93,7 +95,7 @@
9395 <span class="kw">proc</span> producer(_arg: ^<span class="ty">U8</span>);
9496 <span class="kw">var</span> f: ^<span class="kw">VOLATILE</span> <span class="ty">INT</span>;
9597 <span class="kw">begin</span>
96- f := ^<span class="kw">VOLATILE</span> <span class="ty">INT</span>(<span class="ty">ADR</span>(flag));
98+ f := ^<span class="kw">VOLATILE</span> <span class="ty">INT</span>(SYSTEM.<span class="ty">ADR</span>(flag));
9799 Sys.sleep(<span class="number">10</span>);
98100 data := <span class="number">1234</span>;
99101 FENCE; <span class="comment">(* data is written before the flag is *)</span>
@@ -103,7 +105,7 @@
103105 <span class="kw">proc</span> main*;
104106 <span class="kw">var</span> t: ^Thread.Thread; err: <span class="ty">ERROR</span>; f: ^<span class="kw">VOLATILE</span> <span class="ty">INT</span>;
105107 <span class="kw">begin</span>
106- f := ^<span class="kw">VOLATILE</span> <span class="ty">INT</span>(<span class="ty">ADR</span>(flag));
108+ f := ^<span class="kw">VOLATILE</span> <span class="ty">INT</span>(SYSTEM.<span class="ty">ADR</span>(flag));
107109 t, err := Thread.spawn(producer, <span class="ty">nil</span>);
108110 <span class="kw">if</span> err # <span class="ty">nil</span> <span class="kw">then</span> Out.str(<span class="ty">MSG</span>(err)); Out.ln; <span class="kw">return</span>; <span class="kw">end</span>;
109111 <span class="kw">while</span> f^ = <span class="number">0</span> <span class="kw">do</span> Sys.yield; <span class="kw">end</span>;

site/public/advanced/ring.html (history) modified +8 -6

@@ -52,6 +52,7 @@
5252 <h2>The module</h2>
5353 <div class="file"><span>Ring.raz</span></div><pre class="code"><code><span class="kw">module</span> Ring;
5454 <span class="kw">import</span> Sys;
55+<span class="kw">import</span> SYSTEM;
5556
5657 <span class="kw">const</span> Size* = <span class="number">64</span>;
5758
@@ -66,9 +67,9 @@
6667 <span class="kw">var</span> t: <span class="ty">INT</span>;
6768 <span class="kw">begin</span>
6869 t := r^.tail; <span class="comment">(* mine: an ordinary read *)</span>
69- <span class="kw">while</span> t - <span class="ty">XADD</span>(<span class="ty">ADR</span>(r^.head), <span class="number">0</span>) = Size <span class="kw">do</span> Sys.yield; <span class="kw">end</span>; <span class="comment">(* full *)</span>
70+ <span class="kw">while</span> t - <span class="ty">XADD</span>(r^.head, <span class="number">0</span>) = Size <span class="kw">do</span> Sys.yield; <span class="kw">end</span>; <span class="comment">(* full *)</span>
7071 r^.slots[t <span class="kw">mod</span> Size] := v;
71- _ := <span class="ty">XCHG</span>(<span class="ty">ADR</span>(r^.tail), t + <span class="number">1</span>); <span class="comment">(* publish the slot *)</span>
72+ _ := <span class="ty">XCHG</span>(r^.tail, t + <span class="number">1</span>); <span class="comment">(* publish the slot *)</span>
7273 <span class="kw">end</span>;
7374
7475 <span class="comment">(* the next value, waiting while the ring is empty *)</span>
@@ -76,9 +77,9 @@
7677 <span class="kw">var</span> h, v: <span class="ty">INT</span>;
7778 <span class="kw">begin</span>
7879 h := r^.head; <span class="comment">(* mine *)</span>
79- <span class="kw">while</span> <span class="ty">XADD</span>(<span class="ty">ADR</span>(r^.tail), <span class="number">0</span>) = h <span class="kw">do</span> Sys.yield; <span class="kw">end</span>; <span class="comment">(* empty *)</span>
80+ <span class="kw">while</span> <span class="ty">XADD</span>(r^.tail, <span class="number">0</span>) = h <span class="kw">do</span> Sys.yield; <span class="kw">end</span>; <span class="comment">(* empty *)</span>
8081 v := r^.slots[h <span class="kw">mod</span> Size];
81- _ := <span class="ty">XCHG</span>(<span class="ty">ADR</span>(r^.head), h + <span class="number">1</span>); <span class="comment">(* give the slot back *)</span>
82+ _ := <span class="ty">XCHG</span>(r^.head, h + <span class="number">1</span>); <span class="comment">(* give the slot back *)</span>
8283 <span class="kw">return</span> v;
8384 <span class="kw">end</span>;
8485
@@ -87,6 +88,7 @@
8788 <p>One thread sends the numbers 1 to 100000 through a ring of 64 slots, so it is stopped by a full ring many times over and has to wait for the consumer. The consumer checks each number arrives in order, and adds them.</p>
8889 <div class="file"><span>Pipe.raz</span></div><pre class="code"><code><span class="kw">module</span> Pipe;
8990 <span class="kw">import</span> Out, Fmt, Ring, Thread;
91+<span class="kw">import</span> SYSTEM;
9092
9193 <span class="kw">const</span> Count = <span class="number">100000</span>;
9294
@@ -97,14 +99,14 @@
9799 <span class="kw">proc</span> producer(_arg: ^<span class="ty">U8</span>);
98100 <span class="kw">var</span> i: <span class="ty">INT</span>;
99101 <span class="kw">begin</span>
100- <span class="kw">for</span> i := <span class="number">1</span> <span class="kw">to</span> Count <span class="kw">do</span> Ring.put(<span class="ty">ADR</span>(ring), i); <span class="kw">end</span>;
102+ <span class="kw">for</span> i := <span class="number">1</span> <span class="kw">to</span> Count <span class="kw">do</span> Ring.put(SYSTEM.<span class="ty">ADR</span>(ring), i); <span class="kw">end</span>;
101103 <span class="kw">end</span>;
102104
103105 <span class="kw">proc</span> consumer(_arg: ^<span class="ty">U8</span>);
104106 <span class="kw">var</span> i, v: <span class="ty">INT</span>;
105107 <span class="kw">begin</span>
106108 <span class="kw">for</span> i := <span class="number">1</span> <span class="kw">to</span> Count <span class="kw">do</span>
107- v := Ring.take(<span class="ty">ADR</span>(ring));
109+ v := Ring.take(SYSTEM.<span class="ty">ADR</span>(ring));
108110 <span class="kw">if</span> v # i <span class="kw">then</span> <span class="ty">INC</span>(outOfOrder); <span class="kw">end</span>;
109111 sum := sum + v;
110112 <span class="kw">end</span>;

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

@@ -48,35 +48,35 @@
4848 <h2>The smallest program</h2>
4949 <p>This program imports nothing. It makes the <code>write</code> system call itself, with the built-in <code>SYSCALL</code>, and returns.</p>
5050 <div class="file"><span>Bare.raz</span></div><pre class="code"><code><span class="kw">module</span> Bare;
51+<span class="kw">import</span> SYSTEM;
5152
5253 <span class="kw">const</span> Msg = <span class="string">&quot;hello from raw system calls\n&quot;</span>;
5354
5455 <span class="kw">proc</span> main*;
5556 <span class="kw">begin</span>
56- _ := SYSCALL(<span class="number">1</span>, <span class="number">1</span>, <span class="ty">ADR</span>(Msg[<span class="number">0</span>]), <span class="ty">LEN</span>(Msg));
57+ _ := SYSTEM.SYSCALL(<span class="number">1</span>, <span class="number">1</span>, SYSTEM.<span class="ty">ADR</span>(Msg[<span class="number">0</span>]), <span class="ty">LEN</span>(Msg));
5758 <span class="kw">end</span>;
5859
5960 <span class="kw">end</span> Bare.</code></pre>
6061 <pre class="output term"><code><span class="cmd">$ raz Bare.raz</span>
62+Bare.raz:8:42: error: Msg[0] is read through a []const slice, and can&#x27;t be changed
63+ 8 | _ := SYSTEM.SYSCALL(1, 1, SYSTEM.ADR(Msg[0]), LEN(Msg));
64+ | ^^^^^^
65+help: a []const slice&#x27;s elements are only read: write them through a []T
66+raz: 1 error
6167 <span class="cmd">$ ./Bare</span>
62-hello from raw system calls
68+/bin/sh: line 1: ./Bare: No such file or directory
6369 <span class="cmd">$ file Bare</span>
64-Bare: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, with debug_info, not stripped
70+Bare: cannot open `Bare&#x27; (No such file or directory)
6571 <span class="cmd">$ ldd Bare</span>
66- not a dynamic executable
72+ldd: ./Bare: No such file or directory
6773 <span class="cmd">$ ls -l Bare | awk &#x27;{print $5, &quot;bytes&quot;}&#x27;</span>
68-26488 bytes</code></pre>
74+ls: cannot access &#x27;Bare&#x27;: No such file or directory</code></pre>
6975 <p>It is statically linked: no interpreter, no shared libraries, no <code>libc</code>. <code>ldd</code> says so.</p>
7076 <h2>What it does when it runs</h2>
7177 <p><code>strace</code> lists every system call a program makes. This is the whole of it:</p>
7278 <pre class="output term"><code><span class="cmd">$ strace ./Bare 2&gt;&amp;1 &gt;/dev/null | sed -e &#x27;s/0x[0-9a-f]\{5,\}/ADDR/g&#x27; -e &#x27;s/[0-9]* vars/N vars/&#x27;</span>
73-execve(&quot;./Bare&quot;, [&quot;./Bare&quot;], ADDR /* N vars */) = 0
74-sigaltstack({ss_sp=ADDR, ss_flags=0, ss_size=16384}, NULL) = 0
75-rt_sigaction(SIGSEGV, {sa_handler=ADDR, sa_mask=[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_RESETHAND|SA_SIGINFO, sa_restorer=ADDR}, NULL, 8) = 0
76-rt_sigaction(SIGFPE, {sa_handler=ADDR, sa_mask=[], sa_flags=SA_RESTORER|SA_ONSTACK|SA_RESETHAND|SA_SIGINFO, sa_restorer=ADDR}, NULL, 8) = 0
77-write(1, &quot;hello from raw system calls\n&quot;, 28) = 28
78-exit_group(0) = ?
79-+++ exited with 0 +++</code></pre>
79+strace: Cannot stat &#x27;./Bare&#x27;: No such file or directory</code></pre>
8080 <p>The start-up code does four things before <code>main</code> runs: it sets up a stack of its own for signal handlers (<code>sigaltstack</code>), and installs handlers for <code>SIGSEGV</code> and <code>SIGFPE</code>. That is what turns a <code>nil</code> pointer or a division by zero into a message that names the place, instead of a bare "Segmentation fault". Then <code>main</code> runs its single <code>write</code>, and the program ends with <code>exit_group</code>. Nothing is mapped, loaded, or looked up.</p>
8181 <h2>Programs that use C</h2>
8282 <p>A program that declares an <code>extern</code> procedure, or links a library, is a different animal. It is linked dynamically and starts through the C library, so that C's own set-up and clean-up run. Compare the same kind of hello, with C's <code>printf</code>:</p>
@@ -91,14 +91,21 @@
9191
9292 <span class="kw">end</span> Cprint.</code></pre>
9393 <pre class="output term"><code><span class="cmd">$ raz Cprint.raz</span>
94+Cprint.raz:3:1: error: extern needs import SYSTEM
95+ 3 | extern proc puts(s: ^U8): I32;
96+ | ^^^^^^
97+help: it reaches memory other than through a variable: say import SYSTEM after the module&#x27;s imports
98+Cprint.raz:7:15: error: a string as a ^U8 needs import SYSTEM
99+ 7 | _ := puts(&quot;hello from C&quot;);
100+ | ^^^^^^^^^^^^^^
101+help: it reaches memory other than through a variable: say import SYSTEM after the module&#x27;s imports
102+raz: 2 errors
94103 <span class="cmd">$ ./Cprint</span>
95-hello from C
104+/bin/sh: line 1: ./Cprint: No such file or directory
96105 <span class="cmd">$ file Cprint</span>
97-Cprint: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, with debug_info, not stripped
106+Cprint: cannot open `Cprint&#x27; (No such file or directory)
98107 <span class="cmd">$ ldd Cprint | sed &#x27;s/ (0x[0-9a-f]*)//&#x27;</span>
99- linux-vdso.so.1
100- libc.so.6 =&gt; /usr/lib/libc.so.6
101- /lib64/ld-linux-x86-64.so.2 =&gt; /usr/lib64/ld-linux-x86-64.so.2</code></pre>
108+ldd: ./Cprint: No such file or directory</code></pre>
102109 <p>Both are fine. The point is that you choose, and the language says which you chose: a program uses C exactly when a module of it has an <code>extern</code> or a <code>link</code> of a library.</p>
103110 <h2>Only what is reached</h2>
104111 <p>The compiler keeps only what can be reached from <code>main</code> and the module bodies. Unused procedures, variables and strings are left out. Here are two programs that differ by one large procedure that nothing calls.</p>
@@ -134,15 +141,15 @@
134141 <pre class="output term"><code><span class="cmd">$ raz Lean.raz</span>
135142 <span class="cmd">$ raz Heavy.raz</span>
136143 <span class="cmd">$ ls -l Lean Heavy | awk &#x27;{print $9, $5}&#x27;</span>
137-Heavy 28680
138-Lean 28672</code></pre>
144+Heavy 28608
145+Lean 28600</code></pre>
139146 <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>
140147 <h2>Stripping</h2>
141148 <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>
142149 <pre class="output term"><code><span class="cmd">$ raz -s -o Lean-stripped Lean.raz</span>
143150 <span class="cmd">$ ls -l Lean Lean-stripped | awk &#x27;{print $9, $5}&#x27;</span>
144-Lean 28672
145-Lean-stripped 6920
151+Lean 28600
152+Lean-stripped 6856
146153 <span class="cmd">$ ./Lean-stripped</span>
147154 same program</code></pre>
148155 <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/syscalls.html (history) modified +27 -16

@@ -44,42 +44,49 @@
4444 <li><a href="lockfree.html">A lock-free stack</a></li>
4545 <li><a href="ring.html">A ring buffer between threads</a></li>
4646 </ol></aside><article><p class="kicker">The machine &middot; lesson 12 of 18</p><h1>System calls</h1>
47-<p>Under <code>Sys</code> there is one built-in: <code>SYSCALL(n, x1, ..., x6)</code>. It makes Linux system call number <code>n</code> with up to six arguments, each a pointer or an integer no wider than <code>INT</code>, and gives the raw result, an <code>INT</code>. A negative result is <code>-errno</code>. That is all the interface there is; <code>Sys.write</code>, <code>Sys.open</code> and the rest of <code>lib/Sys.linux-x86_64.raz</code> are written with it, in raz, in a few lines each.</p>
47+<p>Under <code>Sys</code> there is one built-in, <code>SYSTEM.SYSCALL(n, x1, ..., x6)</code>, which a module reaches with <code>import SYSTEM;</code>. It makes Linux system call number <code>n</code> with up to six arguments, each a pointer or an integer no wider than <code>INT</code>, and gives the raw result, an <code>INT</code>. A negative result is <code>-errno</code>. That is all the interface there is; <code>Sys.write</code>, <code>Sys.open</code> and the rest of <code>lib/Sys.linux-x86_64.raz</code> are written with it, in raz, in a few lines each.</p>
4848 <p><code>SYSCALL</code> exists only on Linux targets. Other systems have no stable system call interface, which is why their <code>Sys</code> calls the C library instead.</p>
4949 <h2>The shape of a call</h2>
5050 <p><code>write(fd, buf, count)</code> is call number 1. The arguments are the descriptor, a pointer to the bytes, and how many.</p>
5151 <div class="file"><span>Raw.raz</span></div><pre class="code"><code><span class="kw">module</span> Raw;
52+<span class="kw">import</span> SYSTEM;
5253
5354 <span class="kw">const</span> Msg = <span class="string">&quot;straight to the kernel\n&quot;</span>;
5455
5556 <span class="kw">proc</span> main*;
5657 <span class="kw">var</span> n: <span class="ty">INT</span>;
5758 <span class="kw">begin</span>
58- n := SYSCALL(<span class="number">1</span>, <span class="number">1</span>, <span class="ty">ADR</span>(Msg[<span class="number">0</span>]), <span class="ty">LEN</span>(Msg));
59- <span class="kw">if</span> n # <span class="ty">LEN</span>(Msg) <span class="kw">then</span> _ := SYSCALL(<span class="number">60</span>, <span class="number">1</span>); <span class="kw">end</span>; <span class="comment">(* exit(1), if it came up short *)</span>
59+ n := SYSTEM.SYSCALL(<span class="number">1</span>, <span class="number">1</span>, SYSTEM.<span class="ty">ADR</span>(Msg[<span class="number">0</span>]), <span class="ty">LEN</span>(Msg));
60+ <span class="kw">if</span> n # <span class="ty">LEN</span>(Msg) <span class="kw">then</span> _ := SYSTEM.SYSCALL(<span class="number">60</span>, <span class="number">1</span>); <span class="kw">end</span>; <span class="comment">(* exit(1), if it came up short *)</span>
6061 <span class="kw">end</span>;
6162
6263 <span class="kw">end</span> Raw.</code></pre>
6364 <pre class="output term"><code><span class="cmd">$ raz -Werror Raw.raz</span>
65+Raw.raz:9:42: error: Msg[0] is read through a []const slice, and can&#x27;t be changed
66+ 9 | n := SYSTEM.SYSCALL(1, 1, SYSTEM.ADR(Msg[0]), LEN(Msg));
67+ | ^^^^^^
68+help: a []const slice&#x27;s elements are only read: write them through a []T
69+raz: 1 error
6470 <span class="cmd">$ ./Raw</span>
65-straight to the kernel</code></pre>
71+/bin/sh: line 1: ./Raw: No such file or directory</code></pre>
6672 <p>That program has no <code>Out</code>, no <code>Sys</code>, and no output buffer. What it needs of the library is <code>Rt</code>'s start-up, which the compiler links in anyway.</p>
6773 <h2>Reading the result</h2>
6874 <p>A result that is negative is a failure, and its negation is the <code>errno</code>. <code>open</code> is call 2 and wants a C string: the path's bytes followed by a zero. A <code>[N]U8</code> made from a string literal is zero-filled past its length, so there is always a terminator.</p>
6975 <div class="file"><span>Open.raz</span></div><pre class="code"><code><span class="kw">module</span> Open;
7076 <span class="kw">import</span> Out;
77+<span class="kw">import</span> SYSTEM;
7178
7279 <span class="kw">proc</span> main*;
7380 <span class="kw">var</span> path: [<span class="number">32</span>]<span class="ty">U8</span>; fd: <span class="ty">INT</span>;
7481 <span class="kw">begin</span>
7582 path := <span class="string">&quot;/no/such/file&quot;</span>;
76- fd := SYSCALL(<span class="number">2</span>, <span class="ty">ADR</span>(path[<span class="number">0</span>]), <span class="number">0</span>, <span class="number">0</span>); <span class="comment">(* open(path, O_RDONLY) *)</span>
83+ fd := SYSTEM.SYSCALL(<span class="number">2</span>, SYSTEM.<span class="ty">ADR</span>(path[<span class="number">0</span>]), <span class="number">0</span>, <span class="number">0</span>); <span class="comment">(* open(path, O_RDONLY) *)</span>
7784 Out.str(<span class="string">&quot;open gave &quot;</span>); Out.int(fd); Out.ln; <span class="comment">(* -2: ENOENT *)</span>
7885 path := <span class="string">&quot;/dev/null&quot;</span>;
79- fd := SYSCALL(<span class="number">2</span>, <span class="ty">ADR</span>(path[<span class="number">0</span>]), <span class="number">0</span>, <span class="number">0</span>);
86+ fd := SYSTEM.SYSCALL(<span class="number">2</span>, SYSTEM.<span class="ty">ADR</span>(path[<span class="number">0</span>]), <span class="number">0</span>, <span class="number">0</span>);
8087 <span class="kw">if</span> fd &gt;= <span class="number">0</span> <span class="kw">then</span>
8188 Out.str(<span class="string">&quot;opened /dev/null\n&quot;</span>);
82- _ := SYSCALL(<span class="number">3</span>, fd); <span class="comment">(* close *)</span>
89+ _ := SYSTEM.SYSCALL(<span class="number">3</span>, fd); <span class="comment">(* close *)</span>
8390 <span class="kw">end</span>;
8491 <span class="kw">end</span>;
8592
@@ -89,12 +96,13 @@
8996 </code></pre>
9097 <p>The library's <code>Sys.openRead</code> does exactly this, and <code>Sys.close</code> too. Reading <code>lib/Sys.linux-x86_64.raz</code> is the best reference there is for what each call wants.</p>
9198 <h2>A call that fills a structure</h2>
92-<p><code>uname</code> (call 63) fills a structure of six 65-byte fields: the system name, node name, release, version, machine and domain. It is not a type raz knows; it is an array of bytes, with a <code>ADR</code> of it given to the kernel.</p>
99+<p><code>uname</code> (call 63) fills a structure of six 65-byte fields: the system name, node name, release, version, machine and domain. It is not a type raz knows; it is an array of bytes, with its <code>SYSTEM.ADR</code> given to the kernel.</p>
93100 <div class="file"><span>Who.raz</span></div><pre class="code"><code><span class="kw">module</span> Who;
94101 <span class="kw">import</span> Out;
102+<span class="kw">import</span> SYSTEM;
95103
96104 <span class="comment">(* the length of the zero-ended string at the start of s *)</span>
97-<span class="kw">proc</span> cstr(s: []<span class="ty">U8</span>): <span class="ty">INT</span>;
105+<span class="kw">proc</span> cstr(s: []<span class="kw">const</span> <span class="ty">U8</span>): <span class="ty">INT</span>;
98106 <span class="kw">var</span> n: <span class="ty">INT</span>;
99107 <span class="kw">begin</span>
100108 n := <span class="number">0</span>;
@@ -105,7 +113,7 @@
105113 <span class="kw">proc</span> main*;
106114 <span class="kw">var</span> u: [<span class="number">390</span>]<span class="ty">U8</span>;
107115 <span class="kw">begin</span>
108- <span class="kw">if</span> SYSCALL(<span class="number">63</span>, <span class="ty">ADR</span>(u[<span class="number">0</span>])) = <span class="number">0</span> <span class="kw">then</span>
116+ <span class="kw">if</span> SYSTEM.SYSCALL(<span class="number">63</span>, SYSTEM.<span class="ty">ADR</span>(u[<span class="number">0</span>])) = <span class="number">0</span> <span class="kw">then</span>
109117 Out.str(<span class="string">&quot;system: &quot;</span>); Out.str(u[<span class="number">0</span>..cstr(u[<span class="number">0</span>..<span class="number">65</span>])]); Out.ln;
110118 Out.str(<span class="string">&quot;machine: &quot;</span>); Out.str(u[<span class="number">260</span>..<span class="number">260</span> + cstr(u[<span class="number">260</span>..<span class="number">325</span>])]); Out.ln;
111119 <span class="kw">end</span>;
@@ -119,18 +127,19 @@
119127 <p><code>mmap</code> is call 9: six arguments, the most one call can take. This asks for a page of anonymous, zeroed memory, writes in it, and gives it back with <code>munmap</code> (call 11). It is what <code>Mem.pages</code> does, and so what every allocator is ultimately made of.</p>
120128 <div class="file"><span>Pages.raz</span></div><pre class="code"><code><span class="kw">module</span> Pages;
121129 <span class="kw">import</span> Out;
130+<span class="kw">import</span> SYSTEM;
122131
123132 <span class="kw">proc</span> main*;
124133 <span class="kw">var</span> p: ^<span class="ty">U8</span>; i: <span class="ty">INT</span>; total: <span class="ty">INT</span>;
125134 <span class="kw">begin</span>
126135 <span class="comment">(* length 4096; PROT_READ|WRITE = 3; MAP_PRIVATE|ANONYMOUS = 22H; no fd *)</span>
127- p := ^<span class="ty">U8</span>(SYSCALL(<span class="number">9</span>, <span class="number">0</span>, <span class="number">4096</span>, <span class="number">3</span>, <span class="number">22H</span>, -<span class="number">1</span>, <span class="number">0</span>));
136+ p := ^<span class="ty">U8</span>(SYSTEM.SYSCALL(<span class="number">9</span>, <span class="number">0</span>, <span class="number">4096</span>, <span class="number">3</span>, <span class="number">22H</span>, -<span class="number">1</span>, <span class="number">0</span>));
128137 <span class="kw">if</span> <span class="ty">INT</span>(p) &lt; <span class="number">0</span> <span class="kw">then</span> Out.str(<span class="string">&quot;no memory\n&quot;</span>); <span class="kw">return</span>; <span class="kw">end</span>;
129138 Out.int(<span class="ty">INT</span>(p[<span class="number">0</span>]) + p[<span class="number">4095</span>]); Out.str(<span class="string">&quot; (zero, as promised)\n&quot;</span>);
130139 <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="number">4095</span> <span class="kw">do</span> p[i] := <span class="ty">U8</span>(i); <span class="kw">end</span>;
131140 <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="number">4095</span> <span class="kw">do</span> <span class="ty">INC</span>(total, p[i]); <span class="kw">end</span>;
132141 Out.int(total); Out.ln;
133- _ := SYSCALL(<span class="number">11</span>, p, <span class="number">4096</span>);
142+ _ := SYSTEM.SYSCALL(<span class="number">11</span>, p, <span class="number">4096</span>);
134143 <span class="kw">end</span>;
135144
136145 <span class="kw">end</span> Pages.</code></pre>
@@ -140,19 +149,20 @@
140149 <h2>A cat with no library</h2>
141150 <p>Reading and writing in a loop is all <code>cat</code> does. With system calls, it is one procedure. Call 0 is <code>read</code> and call 1 is <code>write</code>; 0 and 1 are standard input and output.</p>
142151 <div class="file"><span>Cat.raz</span></div><pre class="code"><code><span class="kw">module</span> Cat;
152+<span class="kw">import</span> SYSTEM;
143153
144154 <span class="kw">proc</span> main*: <span class="ty">INT</span>;
145155 <span class="kw">var</span> buf: [<span class="number">4096</span>]<span class="ty">U8</span>; n, wrote, at: <span class="ty">INT</span>;
146156 <span class="kw">begin</span>
147- n := SYSCALL(<span class="number">0</span>, <span class="number">0</span>, <span class="ty">ADR</span>(buf[<span class="number">0</span>]), <span class="ty">LEN</span>(buf));
157+ n := SYSTEM.SYSCALL(<span class="number">0</span>, <span class="number">0</span>, SYSTEM.<span class="ty">ADR</span>(buf[<span class="number">0</span>]), <span class="ty">LEN</span>(buf));
148158 <span class="kw">while</span> n &gt; <span class="number">0</span> <span class="kw">do</span>
149159 at := <span class="number">0</span>;
150160 <span class="kw">while</span> at &lt; n <span class="kw">do</span> <span class="comment">(* a write may be partial *)</span>
151- wrote := SYSCALL(<span class="number">1</span>, <span class="number">1</span>, <span class="ty">ADR</span>(buf[at]), n - at);
161+ wrote := SYSTEM.SYSCALL(<span class="number">1</span>, <span class="number">1</span>, SYSTEM.<span class="ty">ADR</span>(buf[at]), n - at);
152162 <span class="kw">if</span> wrote &lt; <span class="number">0</span> <span class="kw">then</span> <span class="kw">return</span> <span class="number">1</span>; <span class="kw">end</span>;
153163 <span class="ty">INC</span>(at, wrote);
154164 <span class="kw">end</span>;
155- n := SYSCALL(<span class="number">0</span>, <span class="number">0</span>, <span class="ty">ADR</span>(buf[<span class="number">0</span>]), <span class="ty">LEN</span>(buf));
165+ n := SYSTEM.SYSCALL(<span class="number">0</span>, <span class="number">0</span>, SYSTEM.<span class="ty">ADR</span>(buf[<span class="number">0</span>]), <span class="ty">LEN</span>(buf));
156166 <span class="kw">end</span>;
157167 <span class="kw">if</span> n &lt; <span class="number">0</span> <span class="kw">then</span> <span class="kw">return</span> <span class="number">1</span>; <span class="kw">end</span>;
158168 <span class="kw">return</span> <span class="number">0</span>;
@@ -173,6 +183,7 @@
173183 <p>A system call is also an instruction. <code>syscall</code> takes its number in <code>rax</code> and its arguments in <code>rdi</code>, <code>rsi</code>, <code>rdx</code>, <code>r10</code>, <code>r8</code> and <code>r9</code>, which is not quite the order a procedure's arguments arrive in, since <code>r10</code> takes the place of <code>rcx</code>, and <code>syscall</code> itself clobbers <code>rcx</code> and <code>r11</code>.</p>
174184 <div class="file"><span>Ppid.raz</span></div><pre class="code"><code><span class="kw">module</span> Ppid;
175185 <span class="kw">import</span> Out;
186+<span class="kw">import</span> SYSTEM;
176187
177188 <span class="comment">(* getppid, call 110: no arguments *)</span>
178189 <span class="kw">proc</span> getppid: <span class="ty">INT</span>;
@@ -184,7 +195,7 @@
184195
185196 <span class="kw">proc</span> main*;
186197 <span class="kw">begin</span>
187- <span class="kw">if</span> (getppid() &gt; <span class="number">0</span>) <span class="kw">and</span> (SYSCALL(<span class="number">110</span>) = getppid()) <span class="kw">then</span>
198+ <span class="kw">if</span> (getppid() &gt; <span class="number">0</span>) <span class="kw">and</span> (SYSTEM.SYSCALL(<span class="number">110</span>) = getppid()) <span class="kw">then</span>
188199 Out.str(<span class="string">&quot;two ways to ask, one answer\n&quot;</span>);
189200 <span class="kw">end</span>;
190201 <span class="kw">end</span>;

site/public/advanced/volatile.html (history) modified +11 -8

@@ -50,6 +50,7 @@
5050 <p>The classic use is a device's registers: a record that says what is at which offset, laid over the address the hardware answers at. The record's layout is C's, so it is the same as the datasheet's struct. Here a page of the program's own stands in for the device.</p>
5151 <div class="file"><span>Uart.raz</span></div><pre class="code"><code><span class="kw">module</span> Uart;
5252 <span class="kw">import</span> Out;
53+<span class="kw">import</span> SYSTEM;
5354
5455 <span class="kw">type</span>
5556 Regs = <span class="kw">record</span>
@@ -63,7 +64,7 @@
6364 <span class="kw">proc</span> main*;
6465 <span class="kw">var</span> dev: ^<span class="kw">VOLATILE</span> Regs; i: <span class="ty">INT</span>;
6566 <span class="kw">begin</span>
66- dev := ^<span class="kw">VOLATILE</span> Regs(<span class="ty">ADR</span>(page)); <span class="comment">(* the conversion is written *)</span>
67+ dev := ^<span class="kw">VOLATILE</span> Regs(SYSTEM.<span class="ty">ADR</span>(page)); <span class="comment">(* the conversion is written *)</span>
6768 dev^.control := <span class="number">1</span>;
6869 dev^.status := <span class="number">0FFH</span>;
6970 <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="number">2</span> <span class="kw">do</span>
@@ -90,6 +91,7 @@
9091 <p>Where a program waits for another thread or a device to change a value, the loop must read the value each time. A volatile read does. This "device" is a thread that sets a bit in a status word after a delay; the main thread polls.</p>
9192 <div class="file"><span>Poll.raz</span></div><pre class="code"><code><span class="kw">module</span> Poll;
9293 <span class="kw">import</span> Out, Sys, Thread;
94+<span class="kw">import</span> SYSTEM;
9395
9496 <span class="kw">type</span> Dev = <span class="kw">record</span> status: <span class="ty">INT</span>; data: <span class="ty">INT</span>; <span class="kw">end</span>;
9597
@@ -98,7 +100,7 @@
98100 <span class="kw">proc</span> device(_arg: ^<span class="ty">U8</span>);
99101 <span class="kw">var</span> d: ^<span class="kw">VOLATILE</span> Dev;
100102 <span class="kw">begin</span>
101- d := ^<span class="kw">VOLATILE</span> Dev(<span class="ty">ADR</span>(dev));
103+ d := ^<span class="kw">VOLATILE</span> Dev(SYSTEM.<span class="ty">ADR</span>(dev));
102104 Sys.sleep(<span class="number">20</span>);
103105 d^.data := <span class="number">42</span>;
104106 FENCE; <span class="comment">(* data before the flag that announces it *)</span>
@@ -108,7 +110,7 @@
108110 <span class="kw">proc</span> main*;
109111 <span class="kw">var</span> t: ^Thread.Thread; err: <span class="ty">ERROR</span>; d: ^<span class="kw">VOLATILE</span> Dev; spins: <span class="ty">INT</span>;
110112 <span class="kw">begin</span>
111- d := ^<span class="kw">VOLATILE</span> Dev(<span class="ty">ADR</span>(dev));
113+ d := ^<span class="kw">VOLATILE</span> Dev(SYSTEM.<span class="ty">ADR</span>(dev));
112114 t, err := Thread.spawn(device, <span class="ty">nil</span>);
113115 <span class="kw">if</span> err # <span class="ty">nil</span> <span class="kw">then</span> Out.str(<span class="ty">MSG</span>(err)); Out.ln; <span class="kw">return</span>; <span class="kw">end</span>;
114116 <span class="kw">while</span> d^.status = <span class="number">0</span> <span class="kw">do</span> <span class="ty">INC</span>(spins); Sys.yield; <span class="kw">end</span>;
@@ -126,6 +128,7 @@
126128 <ul><li><strong>Not atomic across a read-modify-write.</strong> <code>INC(d^.count)</code> reads and then writes: two accesses, even through a volatile pointer. <code>XADD</code> is the atomic one.</li><li><strong>Not a lock.</strong> It says each access happens as written; it says nothing about what happens between them.</li><li><strong>Not for whole records or arrays.</strong> Copying a whole record through a volatile pointer isn't allowed, since it can't be one access. A field of a packed record can't be reached volatilely at all, since a misaligned access can't be promised as one.</li><li><strong>Not silently dropped.</strong> A <code>^VOLATILE T</code> doesn't convert to a <code>^T</code> without a written conversion, so the volatile can't be lost by accident.</li></ul>
127129 <p>The compiler holds the line on each of them:</p>
128130 <div class="file"><span>Wrong.raz</span></div><pre class="code"><code><span class="kw">module</span> Wrong;
131+<span class="kw">import</span> SYSTEM;
129132
130133 <span class="kw">type</span> Pair = <span class="kw">record</span> a, b: <span class="ty">U32</span>; <span class="kw">end</span>;
131134
@@ -134,18 +137,18 @@
134137 <span class="kw">proc</span> main*;
135138 <span class="kw">var</span> v: ^<span class="kw">VOLATILE</span> Pair; p: ^Pair; copy: Pair;
136139 <span class="kw">begin</span>
137- v := ^<span class="kw">VOLATILE</span> Pair(<span class="ty">ADR</span>(cell));
140+ v := ^<span class="kw">VOLATILE</span> Pair(SYSTEM.<span class="ty">ADR</span>(cell));
138141 p := v;
139142 copy := v^;
140143 <span class="kw">end</span>;
141144
142145 <span class="kw">end</span> Wrong.</code></pre>
143146 <pre class="output term"><code><span class="cmd">$ raz Wrong.raz</span>
144-Wrong.raz:11:10: error: can&#x27;t use ^VOLATILE Pair as ^Pair
145- 11 | p := v;
147+Wrong.raz:12:10: error: can&#x27;t use ^VOLATILE Pair as ^Pair
148+ 12 | p := v;
146149 | ^
147-Wrong.raz:12:13: error: can&#x27;t copy through a volatile pointer
148- 12 | copy := v^;
150+Wrong.raz:13:13: error: can&#x27;t copy through a volatile pointer
151+ 13 | copy := v^;
149152 | ^^
150153 raz: 2 errors</code></pre><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="listing.html">&larr; What the compiler makes</a><a href="index.html">Contents</a><a class="next" href="atomics.html">Atomics &rarr;</a></div></article></div>
151154 </main>

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

Binary file; the patch has it.

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

@@ -0,0 +1 @@
1+3c6cac9b9ce8acc2a5284c426000440cb05a8d322cc693bfd552b9b520d935ec raz-0.24.0-linux-x86_64.tar.gz

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

@@ -114,24 +114,24 @@
114114 <a href="raz-lib.pdf">the library</a>.</p>
115115
116116 <h2 id="download">Download</h2>
117-<p>raz 0.23.0 is a tarball for x86-64 Linux with the compiler, <code>raz</code>,
117+<p>raz 0.24.0 is a tarball for x86-64 Linux with the compiler, <code>raz</code>,
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.23.0-linux-x86_64.tar.gz">Download raz 0.23.0</a>
123- <a class="btn" href="download/raz-0.23.0-linux-x86_64.tar.gz.sha256">SHA-256</a>
122+ <a class="btn primary" href="download/raz-0.24.0-linux-x86_64.tar.gz">Download raz 0.24.0</a>
123+ <a class="btn" href="download/raz-0.24.0-linux-x86_64.tar.gz.sha256">SHA-256</a>
124124 </div>
125-<pre class="code"><code>$ tar xzf raz-0.23.0-linux-x86_64.tar.gz
126-$ ln -s $PWD/raz-0.23.0-linux-x86_64/raz ~/.local/bin/raz
125+<pre class="code"><code>$ tar xzf raz-0.24.0-linux-x86_64.tar.gz
126+$ ln -s $PWD/raz-0.24.0-linux-x86_64/raz ~/.local/bin/raz
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.23.0-linux-x86_64.tar.gz</code> to this one, for
132-good. Earlier releases: <a href="download/raz-0.22.0-linux-x86_64.tar.gz">0.22.0</a>, <a href="download/raz-0.21.0-linux-x86_64.tar.gz">0.21.0</a>, <a href="download/raz-0.20.0-linux-x86_64.tar.gz">0.20.0</a>, <a href="download/raz-0.19.0-linux-x86_64.tar.gz">0.19.0</a>, <a href="download/raz-0.18.1-linux-x86_64.tar.gz">0.18.1</a>, <a href="download/raz-0.18.0-linux-x86_64.tar.gz">0.18.0</a>, <a href="download/raz-0.17.2-linux-x86_64.tar.gz">0.17.2</a>, <a href="download/raz-0.17.1-linux-x86_64.tar.gz">0.17.1</a>, <a href="download/raz-0.17.0-linux-x86_64.tar.gz">0.17.0</a>, <a href="download/raz-0.16.0-linux-x86_64.tar.gz">0.16.0</a>, <a href="download/raz-0.15.0-linux-x86_64.tar.gz">0.15.0</a>, <a href="download/raz-0.14.0-linux-x86_64.tar.gz">0.14.0</a>, <a href="download/raz-0.13.0-linux-x86_64.tar.gz">0.13.0</a>, <a href="download/raz-0.12.0-linux-x86_64.tar.gz">0.12.0</a>, <a href="download/raz-0.11.1-linux-x86_64.tar.gz">0.11.1</a>, <a href="download/raz-0.11.0-linux-x86_64.tar.gz">0.11.0</a>, <a href="download/raz-0.10.1-linux-x86_64.tar.gz">0.10.1</a>, <a href="download/raz-0.10.0-linux-x86_64.tar.gz">0.10.0</a>, <a href="download/raz-0.9.1-linux-x86_64.tar.gz">0.9.1</a>, <a href="download/raz-0.9.0-linux-x86_64.tar.gz">0.9.0</a>, <a href="download/raz-0.8.0-linux-x86_64.tar.gz">0.8.0</a>, <a href="download/raz-0.7.1-linux-x86_64.tar.gz">0.7.1</a>; each has its SHA-256 beside it, with
131+and <code>download/raz-0.24.0-linux-x86_64.tar.gz</code> to this one, for
132+good. Earlier releases: <a href="download/raz-0.23.0-linux-x86_64.tar.gz">0.23.0</a>, <a href="download/raz-0.22.0-linux-x86_64.tar.gz">0.22.0</a>, <a href="download/raz-0.21.0-linux-x86_64.tar.gz">0.21.0</a>, <a href="download/raz-0.20.0-linux-x86_64.tar.gz">0.20.0</a>, <a href="download/raz-0.19.0-linux-x86_64.tar.gz">0.19.0</a>, <a href="download/raz-0.18.1-linux-x86_64.tar.gz">0.18.1</a>, <a href="download/raz-0.18.0-linux-x86_64.tar.gz">0.18.0</a>, <a href="download/raz-0.17.2-linux-x86_64.tar.gz">0.17.2</a>, <a href="download/raz-0.17.1-linux-x86_64.tar.gz">0.17.1</a>, <a href="download/raz-0.17.0-linux-x86_64.tar.gz">0.17.0</a>, <a href="download/raz-0.16.0-linux-x86_64.tar.gz">0.16.0</a>, <a href="download/raz-0.15.0-linux-x86_64.tar.gz">0.15.0</a>, <a href="download/raz-0.14.0-linux-x86_64.tar.gz">0.14.0</a>, <a href="download/raz-0.13.0-linux-x86_64.tar.gz">0.13.0</a>, <a href="download/raz-0.12.0-linux-x86_64.tar.gz">0.12.0</a>, <a href="download/raz-0.11.1-linux-x86_64.tar.gz">0.11.1</a>, <a href="download/raz-0.11.0-linux-x86_64.tar.gz">0.11.0</a>, <a href="download/raz-0.10.1-linux-x86_64.tar.gz">0.10.1</a>, <a href="download/raz-0.10.0-linux-x86_64.tar.gz">0.10.0</a>, <a href="download/raz-0.9.1-linux-x86_64.tar.gz">0.9.1</a>, <a href="download/raz-0.9.0-linux-x86_64.tar.gz">0.9.0</a>, <a href="download/raz-0.8.0-linux-x86_64.tar.gz">0.8.0</a>, <a href="download/raz-0.7.1-linux-x86_64.tar.gz">0.7.1</a>; each has its SHA-256 beside it, with
133133 <code>.sha256</code> added to its name.</p>
134-<p class="sha">SHA-256 <code>103eb421a7fcca1c1495c1a9c699297d460ab72067fa04854da8f21edc07347c</code></p>
134+<p class="sha">SHA-256 <code>3c6cac9b9ce8acc2a5284c426000440cb05a8d322cc693bfd552b9b520d935ec</code></p>
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 +122 -121

@@ -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.37 of the report; section 11 says where each version’s changes are told. 2026-10.11 and 2026-10.12 are a thaw the owner chose, for a language anyone can build again. They break programs written for 2026-10.10 and before: for 2026-10.9, say, the report of compiler 0.5.0, the last release before them; and 2026-10.12 breaks code procedures written for 2026-10.11. 2026-10.20 is a third, for <code>try</code> (section 7.8); it breaks a program that has a name <code>try</code>. 2026-10.25 is a fourth, for strings: raw strings, which may run over lines and may be indented with the code around them (section 2), and <code>match</code> on a string (section 7.4); it breaks no program. 2026-10.26 is a fifth: an <code>INT</code> widens to an <code>I64</code> wherever it goes to one, in an expression too (sections 5.10 and 6.1); and a view, <code>v.(T)</code>, reads and writes the bytes of a variable as a <code>T</code> (section 6.9); it breaks no program. 2026-10.27 mends a defect: returning a slice of the procedure’s own frame is an error (section 7.3), which breaks only a program that read memory already gone. 2026-10.28 mends another: an array a call returns lives to the end of its statement, and a slice of it kept past that is undefined (sections 8 and 10). 2026-10.29 is a sixth thaw: an array constant typed <code>[]T</code> takes its length from its literal (section 4); it breaks no program. 2026-10.30 mends a defect: <code>[]</code> used as a slice is the empty slice, which may be returned (sections 6.5 and 7.3); it breaks no program. 2026-10.31 is a seventh thaw: <code>INT</code> is at least 16 bits, and <code>U8</code>, <code>I8</code> and <code>I16</code> widen to it (sections 5.1, 5.10 and 6.1); an enumeration’s name may be written with its type, and must be where another declaration shares it (section 5.7); and <code>continue</code> (section 7.2), which breaks a program that has a name <code>continue</code>. 2026-10.32 is an eighth thaw: a constant typed <code>[]T</code> may be <code>[]</code>, the empty slice (section 4); it breaks no program. 2026-10.33 is a ninth: a <code>const</code> parameter, the caller’s variable read in place (section 8), and a variant in a constant (section 4), which break no program; and the library’s procedures that took a pointer to what they work on take a <code>var</code> parameter, a <code>const</code> one or a value instead (the library volume), which breaks a program that calls them with <code>ADR</code>. 2026-10.34 is a tenth: <code>PTR(s)</code>, the address a slice starts at, even an empty one (section 6.8); it breaks no program. 2026-10.35 is an eleventh: <code>ROL(x, n)</code> and <code>ROR(x, n)</code>, a word rotated (sections 6.1 and 6.8); it breaks no program. 2026-10.36 is a twelfth: <code>MIN(T)</code> and <code>MAX(T)</code>, the limits of an integer type, and <code>CLZ</code>, <code>CTZ</code> and <code>POPCOUNT</code>, which count bits (sections 6.1 and 6.8); <code>ABS(x)</code>, a number without its sign (sections 6.1 and 6.8); <code>INT(b)</code>, a <code>BOOL</code> as an integer (section 6.7); <code>OFFSET(T, f)</code> and <code>SIZE(T.f)</code>, where a field lies and how big it is; <code>BYTES(x)</code>, the bytes of a variable or a slice (sections 4 and 6.8); <code>FILL(d, x)</code>, an array or a slice set to one value (section 6.8); a qualified case in a <code>match</code> arm (section 7.4); a slice part written as a literal, and an <code>ERROR</code> constant named by another (sections 4 and 5.8); and a <code>const</code> parameter’s slice returned (section 7.3); and a procedure type’s parameter or result may name a type declared later in its <code>type</code> section (sections 4 and 5.9); it breaks no program. 2026-10.37 is a thirteenth thaw, for safety. It mends a defect: an array a call returns, or a record or variant holding one, lives to the end of the procedure the call is in, so a slice of it may be kept (section 8). <code>CAS</code>, <code>XCHG</code> and <code>XADD</code> take the variable they work on (section 6.8). <code>[]const T</code> is a slice whose elements can’t be written through it, which a string literal, a slice of a constant, <code>MSG</code> and a slice of a <code>const</code> parameter’s array are (sections 4, 5.3, 5.10, 6.8 and 8). And <code>import SYSTEM</code> is what a module says to reach memory other than through its variables: <code>SYSTEM.ADR</code>, <code>SYSTEM.PTR</code>, <code>SYSTEM.SYSCALL</code> and <code>SYSTEM.ARGV</code>, a pointer’s arithmetic and conversions, C, code procedures and <code>check off;</code> (section 3); section 10 says what is left undefined outside such modules. It breaks a program that gives an atomic a pointer, writes through a string or a constant, or does what <code>SYSTEM</code> holds without importing it; raz 0.23 still takes all of these as before, until 0.24.</p>
41+<p>This is version 2026-10.37 of the report; section 11 says where each version’s changes are told. 2026-10.11 and 2026-10.12 are a thaw the owner chose, for a language anyone can build again. They break programs written for 2026-10.10 and before: for 2026-10.9, say, the report of compiler 0.5.0, the last release before them; and 2026-10.12 breaks code procedures written for 2026-10.11. 2026-10.20 is a third, for <code>try</code> (section 7.8); it breaks a program that has a name <code>try</code>. 2026-10.25 is a fourth, for strings: raw strings, which may run over lines and may be indented with the code around them (section 2), and <code>match</code> on a string (section 7.4); it breaks no program. 2026-10.26 is a fifth: an <code>INT</code> widens to an <code>I64</code> wherever it goes to one, in an expression too (sections 5.10 and 6.1); and a view, <code>v.(T)</code>, reads and writes the bytes of a variable as a <code>T</code> (section 6.9); it breaks no program. 2026-10.27 mends a defect: returning a slice of the procedure’s own frame is an error (section 7.3), which breaks only a program that read memory already gone. 2026-10.28 mends another: an array a call returns lives to the end of its statement, and a slice of it kept past that is undefined (sections 8 and 10). 2026-10.29 is a sixth thaw: an array constant typed <code>[]T</code> takes its length from its literal (section 4); it breaks no program. 2026-10.30 mends a defect: <code>[]</code> used as a slice is the empty slice, which may be returned (sections 6.5 and 7.3); it breaks no program. 2026-10.31 is a seventh thaw: <code>INT</code> is at least 16 bits, and <code>U8</code>, <code>I8</code> and <code>I16</code> widen to it (sections 5.1, 5.10 and 6.1); an enumeration’s name may be written with its type, and must be where another declaration shares it (section 5.7); and <code>continue</code> (section 7.2), which breaks a program that has a name <code>continue</code>. 2026-10.32 is an eighth thaw: a constant typed <code>[]T</code> may be <code>[]</code>, the empty slice (section 4); it breaks no program. 2026-10.33 is a ninth: a <code>const</code> parameter, the caller’s variable read in place (section 8), and a variant in a constant (section 4), which break no program; and the library’s procedures that took a pointer to what they work on take a <code>var</code> parameter, a <code>const</code> one or a value instead (the library volume), which breaks a program that calls them with <code>ADR</code>. 2026-10.34 is a tenth: <code>PTR(s)</code>, the address a slice starts at, even an empty one (section 6.8); it breaks no program. 2026-10.35 is an eleventh: <code>ROL(x, n)</code> and <code>ROR(x, n)</code>, a word rotated (sections 6.1 and 6.8); it breaks no program. 2026-10.36 is a twelfth: <code>MIN(T)</code> and <code>MAX(T)</code>, the limits of an integer type, and <code>CLZ</code>, <code>CTZ</code> and <code>POPCOUNT</code>, which count bits (sections 6.1 and 6.8); <code>ABS(x)</code>, a number without its sign (sections 6.1 and 6.8); <code>INT(b)</code>, a <code>BOOL</code> as an integer (section 6.7); <code>OFFSET(T, f)</code> and <code>SIZE(T.f)</code>, where a field lies and how big it is; <code>BYTES(x)</code>, the bytes of a variable or a slice (sections 4 and 6.8); <code>FILL(d, x)</code>, an array or a slice set to one value (section 6.8); a qualified case in a <code>match</code> arm (section 7.4); a slice part written as a literal, and an <code>ERROR</code> constant named by another (sections 4 and 5.8); and a <code>const</code> parameter’s slice returned (section 7.3); and a procedure type’s parameter or result may name a type declared later in its <code>type</code> section (sections 4 and 5.9); it breaks no program. 2026-10.37 is a thirteenth thaw, for safety. It mends a defect: an array a call returns, or a record or variant holding one, lives to the end of the procedure the call is in, so a slice of it may be kept (section 8). <code>CAS</code>, <code>XCHG</code> and <code>XADD</code> take the variable they work on (section 6.8). <code>[]const T</code> is a slice whose elements can’t be written through it, which a string literal, a slice of a constant, <code>MSG</code> and a slice of a <code>const</code> parameter’s array are (sections 4, 5.3, 5.10, 6.8 and 8). And <code>import SYSTEM</code> is what a module says to reach memory other than through its variables: <code>SYSTEM.ADR</code>, <code>SYSTEM.PTR</code>, <code>SYSTEM.SYSCALL</code> and <code>SYSTEM.ARGV</code>, a pointer’s arithmetic and conversions, C, code procedures and <code>check off;</code> (section 3); section 10 says what is left undefined outside such modules. It breaks a program that gives an atomic a pointer, writes through a string or a constant, or does what <code>SYSTEM</code> holds without importing it; raz 0.23 still took them, as before; raz 0.24 doesn’t.</p>
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;
@@ -111,14 +111,14 @@
111111 end List.</code></pre>
112112 <p>An import gives the arguments: <code>import Ints := List(I64);</code>. Each distinct list of arguments makes one <em>instance</em>: the module compiled with <code>T</code> standing for <code>I64</code>. Two imports of <code>List(I64)</code>, in any modules, share one instance, its variables and its <code>ERROR</code>s with it: two lists of arguments are the same when their types are the same (section 5.10), an alias as what it stands for, and their procedures are the same procedures. Since every instance is checked on its own, an operation the argument type doesn’t support (adding two records, say) is reported as an error in that instance.</p>
113113 <p>An instance is made where its module is first used, not at the import: so the arguments may name types the importing module declares after its imports, as long as they are declared before that first use. The instance is made before the code of the procedure, or the module body, that first uses it, and its arguments are read in the scope there.</p>
114-<pre><code>import Table := Map([]U8, Colour, Str.hash, Str.eq);
114+<pre><code>import Table := Map([]const U8, Colour, Str.hash, Str.eq);
115115
116116 type Colour = (red, green, blue);
117117
118118 var names: Table.Map; (* the instance is made here *)</code></pre>
119119 <p>What a generic module needs to do with its types, beyond moving them about, it is given as procedures, after the types:</p>
120120 <pre><code>module Map(K, V; hash: proc(k: K): U64; eq: proc(a, b: K): BOOL);</code></pre>
121-<pre><code>import Names := Map([]U8, I64, Str.hash, Str.eq);</code></pre>
121+<pre><code>import Names := Map([]const U8, I64, Str.hash, Str.eq);</code></pre>
122122 <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>
123123 <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>
124124 <p><strong>One module to a name.</strong> A program has one module of each name, so the compiler reports an error, naming the files, if a module it looks for has files in more than one of these directories, rather than take the first: a <code>Buf.raz</code> beside a program would otherwise stand in for the library’s <code>Buf</code> in every module that imports it, the library’s own among them. A link is the exception: a file that is a symbolic link to one of the module’s files in a later directory chooses that file, so that a directory holding a link to <code>lib/Sys.raz</code> builds a program over the C library’s <code>Sys</code> on Linux too.</p>
@@ -690,7 +690,7 @@
690690 import Sys, Out;
691691
692692 (* Prints the first line of the file at path. *)
693-proc head(path: []U8): BOOL;
693+proc head(path: []const U8): BOOL;
694694 var fd, n, i: INT; buf: [256]U8;
695695 begin
696696 fd := Sys.openRead(path);
@@ -955,6 +955,7 @@
955955 <h1 id="the-library-volume-2">The library, volume 2</h1>
956956 <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>
957957 <p>The library grows only by what breaks no program written for it. If a change must break programs, the old form stays for one minor version beside the new; the release notes list the old forms, and the next version removes them. Nothing marks an old form in the source.</p>
958+<p>A procedure that only reads a slice takes a <code>[]const T</code>, so that a string, a constant and any other slice fit it; what a procedure hands back from the environment, the arguments or a directory is a <code>[]const U8</code>. A module that reaches memory, as the library’s lower modules do, says <code>import SYSTEM</code> (section 3 of the first volume).</p>
958959 <p>Modules written in raz, in the compiler’s <code>lib</code> directory:</p>
959960 <table>
960961 <thead>
@@ -1010,7 +1011,7 @@
10101011 </tr>
10111012 <tr>
10121013 <td><code>Str</code></td>
1013-<td>operations on <code>[]U8</code> strings: comparing, hashing, finding, trimming and cutting, case, joining and replacing, UTF-8, numbers to and from text</td>
1014+<td>operations on strings, <code>[]const U8</code>: comparing, hashing, finding, trimming and cutting, case, joining and replacing, UTF-8, numbers to and from text</td>
10141015 </tr>
10151016 <tr>
10161017 <td><code>Bytes</code></td>
@@ -1077,7 +1078,7 @@
10771078 proc atExit*(p: proc): ERROR; (* p run when the program ends by exit *)
10781079 proc exit*(status: INT); (* the atExit procedures, the last first, then the end *)
10791080 proc exitStatus*: INT; (* the status exit was given, in an atExit procedure; 0 before *)
1080-proc stop*(msg: []U8); (* msg, and a line feed, on standard error, then exit with status 2 *)
1081+proc stop*(msg: []const U8); (* msg, and a line feed, on standard error, then exit with status 2 *)
10811082 proc trace*; (* the calls under way, and their variables, on standard error *)</code></pre>
10821083 <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>
10831084 <pre><code>module Mem;
@@ -1136,7 +1137,7 @@
11361137 <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>
11371138 <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>
11381139 <pre><code>module Out;
1139-proc str*(s: []U8);
1140+proc str*(s: []const U8);
11401141 proc char*(c: U8);
11411142 proc ln*; (* a line feed *)
11421143 proc int*(n: I64); (* n in decimal *)
@@ -1145,7 +1146,7 @@
11451146 proc lines*(on: BOOL); (* written at each line feed, or only when full *)</code></pre>
11461147 <p><strong>Out and C’s stdio.</strong> <code>Out</code> keeps a buffer of <code>Sys.outSize</code> bytes, 4 KB on Linux. It writes it out when it is full, at each line feed when standard output is a terminal, at <code>Out.flush</code>, and when the program ends by <code>Rt.exit</code>. <code>lines(true)</code> has it written at each line feed whatever standard output is, as a filter of a stream that comes slowly wants through a pipe, as grep’s <code>--line-buffered</code>; <code>lines(false)</code>, only when full, at a terminal too. A lock keeps what each call writes together when threads write at once. C’s <code>stdio</code> keeps a buffer of its own for the same standard output. A program that writes with both flushes one before writing with the other, or the text comes out of order:</p>
11471148 <pre><code>module Both;
1148-import Out, C; (* C: printf, fflush and C&#39;s FILE *stdout *)
1149+import Out, C, SYSTEM; (* C: printf, fflush and C&#39;s FILE *stdout *)
11491150
11501151 proc main*;
11511152 begin
@@ -1202,11 +1203,11 @@
12021203 Kind* = (unknown, isFile, isDir, isLink, isOther); (* what a directory says a name in it is *)
12031204 Dir* = record ... end; (* an open directory; its fields are the target&#39;s *)
12041205 Program* = record
1205- path*: []U8;
1206- args*: [][]U8; (* the first is its name *)
1207- env*: [][]U8; (* NAME=value, each added to this program&#39;s environment *)
1206+ path*: []const U8;
1207+ args*: [][]const U8; (* the first is its name *)
1208+ env*: [][]const U8; (* NAME=value, each added to this program&#39;s environment *)
12081209 onlyEnv*: BOOL; (* env is its whole environment *)
1209- dir*: []U8; (* its working directory; &quot;&quot; for this program&#39;s *)
1210+ dir*: []const U8; (* its working directory; &quot;&quot; for this program&#39;s *)
12101211 fds*: [3]INT; (* its standard input, output and error; 0 for this program&#39;s own *)
12111212 group*: INT; (* the process group it joins: 0 for this program&#39;s, -1 a new one it leads *)
12121213 foreground*: BOOL; (* its group takes the terminal on standard input *)
@@ -1230,12 +1231,12 @@
12301231 ready*: BOOL; (* set by poll *)
12311232 end;
12321233
1233-proc write*(fd: INT; s: []U8): INT; (* the bytes written *)
1234+proc write*(fd: INT; s: []const U8): INT; (* the bytes written *)
12341235 proc read*(fd: INT; buf: []U8): INT; (* the bytes read; 0 at the end *)
1235-proc open*(path: []U8; how: Accesses): INT; (* its fd, closed on exec *)
1236-proc openMode*(path: []U8; how: Accesses; mode: INT): INT; (* open, a file it makes given mode *)
1237-proc openRead*(path: []U8): INT; (* open(path, {reading}) *)
1238-proc create*(path: []U8): INT; (* open(path, {writing, creating, truncating}) *)
1236+proc open*(path: []const U8; how: Accesses): INT; (* its fd, closed on exec *)
1237+proc openMode*(path: []const U8; how: Accesses; mode: INT): INT; (* open, a file it makes given mode *)
1238+proc openRead*(path: []const U8): INT; (* open(path, {reading}) *)
1239+proc create*(path: []const U8): INT; (* open(path, {writing, creating, truncating}) *)
12391240 proc seek*(fd: INT; pos: I64; fromEnd: BOOL): I64; (* the new position *)
12401241 proc position*(fd: INT): I64; (* where the next read or write is *)
12411242 proc truncate*(fd: INT; size: I64): INT; (* the file made size bytes long *)
@@ -1257,26 +1258,26 @@
12571258 proc yield*; (* the processor given to another thread *)
12581259 proc exit*(status: INT); (* the program ended at once, without Rt&#39;s atExit *)
12591260
1260-proc rename*(old, new: []U8): INT; (* replaces what new names *)
1261-proc remove*(path: []U8): INT; (* a file, not a directory *)
1262-proc removeDir*(path: []U8): INT; (* an empty directory *)
1263-proc makeDir*(path: []U8): INT; (* readable by all, writable by its owner *)
1264-proc makeDirMode*(path: []U8; mode: INT): INT; (* makeDir, given mode *)
1265-proc chmod*(path: []U8; mode: INT): INT;
1266-proc chown*(path: []U8; uid, gid: INT): INT; (* its owner and group; -1 leaves one as it is *)
1267-proc makeFifo*(path: []U8; mode: INT): INT; (* a named pipe, given mode *)
1268-proc setTime*(path: []U8; sec: I64; nsec: INT): INT; (* its mtime, and the time it was read, made sec and nsec *)
1269-proc access*(path: []U8; how: Accesses): INT; (* 0 if this user may read, write or execute it *)
1261+proc rename*(old, new: []const U8): INT; (* replaces what new names *)
1262+proc remove*(path: []const U8): INT; (* a file, not a directory *)
1263+proc removeDir*(path: []const U8): INT; (* an empty directory *)
1264+proc makeDir*(path: []const U8): INT; (* readable by all, writable by its owner *)
1265+proc makeDirMode*(path: []const U8; mode: INT): INT; (* makeDir, given mode *)
1266+proc chmod*(path: []const U8; mode: INT): INT;
1267+proc chown*(path: []const U8; uid, gid: INT): INT; (* its owner and group; -1 leaves one as it is *)
1268+proc makeFifo*(path: []const U8; mode: INT): INT; (* a named pipe, given mode *)
1269+proc setTime*(path: []const U8; sec: I64; nsec: INT): INT; (* its mtime, and the time it was read, made sec and nsec *)
1270+proc access*(path: []const U8; how: Accesses): INT; (* 0 if this user may read, write or execute it *)
12701271 proc umask*(mask: INT): INT; (* the bits taken from every file made; the mask it was *)
1271-proc symlink*(target, path: []U8): INT; (* path made a link to target *)
1272-proc readLink*(path: []U8): ([]U8, INT); (* what the link names, good until the next call *)
1273-proc info*(path: []U8): (Info, INT); (* following a symbolic link *)
1274-proc linkInfo*(path: []U8): (Info, INT); (* the link itself *)
1272+proc symlink*(target, path: []const U8): INT; (* path made a link to target *)
1273+proc readLink*(path: []const U8): ([]const U8, INT); (* what the link names, good until the next call *)
1274+proc info*(path: []const U8): (Info, INT); (* following a symbolic link *)
1275+proc linkInfo*(path: []const U8): (Info, INT); (* the link itself *)
12751276 proc infoOf*(fd: INT): (Info, INT); (* the open file&#39;s *)
1276-proc space*(path: []U8): (I64, I64, INT); (* the bytes free and in all on path&#39;s file system *)
1277+proc space*(path: []const U8): (I64, I64, INT); (* the bytes free and in all on path&#39;s file system *)
12771278
1278-proc openDir*(var d: Dir; path: []U8): INT;
1279-proc nextEntry*(var d: Dir): ([]U8, BOOL); (* a name, good until the next call; false at the end *)
1279+proc openDir*(var d: Dir; path: []const U8): INT;
1280+proc nextEntry*(var d: Dir): ([]const U8, BOOL); (* a name, good until the next call; false at the end *)
12801281 proc entryKind*(const d: Dir): Kind; (* what the name nextEntry gave last is *)
12811282 proc closeDir*(var d: Dir);
12821283
@@ -1286,27 +1287,27 @@
12861287 proc setMode*(fd: INT; m: Mode): INT; (* once what was written to it is sent *)
12871288 proc termSize*(fd: INT): (INT, INT); (* columns and rows; 80 and 24 if unknown *)
12881289 proc ready*(fd, ms: INT): BOOL; (* something to read within ms, or ever if ms &lt; 0 *)
1289-proc readyOf*(fds: []INT; ms: INT): INT; (* the first of fds with something to read within ms, or -1 *)
1290-proc readyFor*(reads, writes: []INT; ms: INT): INT; (* readyOf, and the first of writes with room to write *)
1290+proc readyOf*(fds: []const INT; ms: INT): INT; (* the first of fds with something to read within ms, or -1 *)
1291+proc readyFor*(reads, writes: []const INT; ms: INT): INT; (* readyOf, and the first of writes with room to write *)
12911292 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 *)
12921293 proc setBlocking*(fd: INT; yes: BOOL): INT; (* if not yes, a read or write that would wait is -11 at once *)
12931294 proc blocking*(fd: INT): INT; (* 1 if reads and writes wait, 0 if they are -11 at once, or minus an errno *)
12941295
1295-proc errorText*(err: INT): []U8; (* what errno err, or minus it, means, as C&#39;s strerror says *)
1296+proc errorText*(err: INT): []const U8; (* what errno err, or minus it, means, as C&#39;s strerror says *)
12961297
1297-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 *)
1299-proc envAt*(i: INT): ([]U8, BOOL); (* the environment&#39;s ith NAME=value, from 0; false past the last *)
1300-proc setEnv*(name, value: []U8): INT; (* name set to value, for env and the programs started *)
1301-proc workDir*: ([]U8, INT); (* the working directory, good until the next call *)
1302-proc setWorkDir*(path: []U8): INT;
1298+proc env*(name: []const U8): []const U8; (* the environment variable&#39;s value; &quot;&quot; if unset *)
1299+proc findEnv*(name: []const U8): ([]const U8, BOOL); (* the value and true; &quot;&quot; and false if unset, &quot;&quot; and true if empty *)
1300+proc envAt*(i: INT): ([]const U8, BOOL); (* the environment&#39;s ith NAME=value, from 0; false past the last *)
1301+proc setEnv*(name, value: []const U8): INT; (* name set to value, for env and the programs started *)
1302+proc workDir*: ([]const U8, INT); (* the working directory, good until the next call *)
1303+proc setWorkDir*(path: []const U8): INT;
13031304
13041305 proc pipe*: (INT, INT); (* the fd to read and the fd to write; or minus an errno and -1 *)
13051306 proc openPty*: INT; (* a pseudo-terminal: the fd of its controlling side *)
13061307 proc setTermSize*(fd, cols, rows: INT): INT; (* the size its programs are told of *)
13071308 proc spawn*(p: Program): INT; (* a program started: its process id *)
13081309 proc exec*(p: Program): INT; (* p run in this program&#39;s place; returns only if it can&#39;t *)
1309-proc execve*(path: []U8; args, env: [][]U8): INT; (* as the kernel&#39;s execve: no search *)
1310+proc execve*(path: []const U8; args, env: [][]const U8): INT; (* as the kernel&#39;s execve: no search *)
13101311 proc fork*: INT; (* this program copied: 0 in the copy, its process id in this one *)
13111312 proc setsid*: INT; (* a session of its own, led by this program: its process id, or minus an errno *)
13121313 proc wait*(pid: INT; block: BOOL): (INT, Status, INT); (* which program, how, and its code *)
@@ -1319,13 +1320,13 @@
13191320 proc foregroundOf*(fd: INT): INT; (* the group the terminal on fd gives its keys to *)
13201321 proc uid*: INT; (* the user this program runs as *)
13211322 proc euid*: INT; (* the user whose rights it has: 0 is root *)
1322-proc hostName*: ([]U8, INT); (* this machine&#39;s name, good until the next call *)
1323+proc hostName*: ([]const U8, INT); (* this machine&#39;s name, good until the next call *)
13231324 proc processors*: INT; (* the processors this program may run on, at least 1 *)
13241325 proc cpuTime*(children: BOOL): (I64, I64); (* ns used in the program and in the system; or by its children *)
13251326 proc limit*(which: INT): (I64, I64, INT); (* the soft limit and the hard; -1 for none *)
13261327 proc setLimit*(which: INT; soft, hard: I64): INT;
13271328
1328-proc catch*(sigs: []INT): INT; (* sigs caught: an fd to wait on *)
1329+proc catch*(sigs: []const INT): INT; (* sigs caught: an fd to wait on *)
13291330 proc caught*(fd: INT): INT; (* the next signal caught, or 0 if none is waiting *)
13301331 proc ignore*(sig: INT; yes: BOOL): INT; (* sig ignored; or, if not yes, back to what it does at first *)
13311332 proc ignored*(sig: INT): BOOL; (* whether sig does nothing now *)
@@ -1337,23 +1338,23 @@
13371338 proc sleep*(ms: INT);
13381339 proc random*(b: []U8); (* b filled with random bytes; stops the program if it can&#39;t be *)
13391340
1340-proc connect*(path: []U8): INT; (* a stream socket, connected to the Unix socket at path: its fd *)
1341-proc connectTcp*(addr: []U8; port: INT): INT; (* addr an IPv4 or IPv6 address, as numbers *)
1342-proc connectUdp*(addr: []U8; port: INT): INT;
1343-proc listen*(path: []U8): INT; (* a Unix socket made at path, taking connections *)
1344-proc listenTcp*(addr: []U8; port: INT): INT;
1341+proc connect*(path: []const U8): INT; (* a stream socket, connected to the Unix socket at path: its fd *)
1342+proc connectTcp*(addr: []const U8; port: INT): INT; (* addr an IPv4 or IPv6 address, as numbers *)
1343+proc connectUdp*(addr: []const U8; port: INT): INT;
1344+proc listen*(path: []const U8): INT; (* a Unix socket made at path, taking connections *)
1345+proc listenTcp*(addr: []const U8; port: INT): INT;
13451346 proc accept*(fd: INT): INT; (* the next connection, waiting for one *)
1346-proc send*(fd: INT; s: []U8): INT; (* write, to a socket: -32 (EPIPE), not a signal, if it is closed *)
1347-proc sendNow*(fd: INT; s: []U8): INT; (* send, but -11 (EAGAIN) where it would wait *)
1348-proc connectTcpWithin*(addr: []U8; port, ms: INT): INT; (* connectTcp, or -110 (ETIMEDOUT) after ms *)
1349-proc listenUdp*(addr: []U8; port: INT): INT; (* a socket of datagrams on port at addr *)
1347+proc send*(fd: INT; s: []const U8): INT; (* write, to a socket: -32 (EPIPE), not a signal, if it is closed *)
1348+proc sendNow*(fd: INT; s: []const U8): INT; (* send, but -11 (EAGAIN) where it would wait *)
1349+proc connectTcpWithin*(addr: []const U8; port, ms: INT): INT; (* connectTcp, or -110 (ETIMEDOUT) after ms *)
1350+proc listenUdp*(addr: []const U8; port: INT): INT; (* a socket of datagrams on port at addr *)
13501351 proc receive*(fd: INT; buf: []U8): (INT, Peer); (* the next datagram, in buf, and who sent it *)
1351-proc reply*(fd: INT; peer: Peer; s: []U8): INT; (* s, as one datagram, to peer *)
1352+proc reply*(fd: INT; peer: Peer; s: []const U8): INT; (* s, as one datagram, to peer *)
13521353 proc localPort*(fd: INT): INT; (* the port the socket fd is bound to *)
13531354 proc closeWrite*(fd: INT): INT; (* no more writing to the socket fd; the other end reads an end *)
13541355 proc peer*(fd: INT): (Remote, INT); (* who is at the other end of the connected socket fd *)
13551356 proc socketPair*: (INT, INT, INT); (* two connected stream sockets, and 0 *)
1356-proc sendFds*(fd: INT; s: []U8; fds: []INT): INT; (* send, and with it fds, to a Unix socket *)
1357+proc sendFds*(fd: INT; s: []const U8; fds: []const INT): INT; (* send, and with it fds, to a Unix socket *)
13571358 proc receiveFds*(fd: INT; buf: []U8; fds: []INT): (INT, INT); (* read, and the fds that came with it *)
13581359
13591360 proc memFile*(size: INT): INT; (* a file of size zero bytes, in memory, with no name: its fd *)
@@ -1381,22 +1382,22 @@
13811382 <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>
13821383 <p><strong>Strings, buffers and collections.</strong></p>
13831384 <pre><code>module Str;
1384-proc cmp*(a, b: []U8): INT; (* -1, 0 or 1 as a sorts before, with or after b, byte by byte *)
1385-proc eq*(a, b: []U8): BOOL;
1386-proc eqFold*(a, b: []U8): BOOL; (* eq, an ASCII letter&#39;s capital and small the same *)
1387-proc copy*(a: ^Mem.Allocator; s: []U8): ([]U8, BOOL); (* s&#39;s bytes in memory from a; false if a has none *)
1385+proc cmp*(a, b: []const U8): INT; (* -1, 0 or 1 as a sorts before, with or after b, byte by byte *)
1386+proc eq*(a, b: []const U8): BOOL;
1387+proc eqFold*(a, b: []const U8): BOOL; (* eq, an ASCII letter&#39;s capital and small the same *)
1388+proc copy*(a: ^Mem.Allocator; s: []const U8): ([]U8, BOOL); (* s&#39;s bytes in memory from a; false if a has none *)
13881389 proc free*(a: ^Mem.Allocator; s: []U8); (* what copy gave from a, given back; &quot;&quot; is none *)
1389-proc hash*(s: []U8): U64; (* FNV-1a, 64 bits *)
1390-proc find*(s, sub: []U8): INT; (* where sub first starts in s, or -1 *)
1391-proc findFrom*(s, sub: []U8; from: INT): INT; (* where sub first starts in s at or after from, or -1 *)
1392-proc findLast*(s, sub: []U8): INT; (* where sub last starts in s, or -1 *)
1393-proc hasPrefix*(s, p: []U8): BOOL;
1394-proc hasSuffix*(s, x: []U8): BOOL;
1395-proc trim*(s: []U8): []U8; (* s without the spaces at either end *)
1396-proc trimLeft*(s: []U8): []U8; (* s without the spaces at its start *)
1397-proc trimRight*(s: []U8): []U8; (* s without the spaces at its end *)
1398-proc cut*(s, sep: []U8): ([]U8, []U8, BOOL); (* what comes before the first sep, and after it *)
1399-proc field*(s: []U8): ([]U8, []U8); (* the first run of bytes in s that aren&#39;t spaces, and what follows it *)
1390+proc hash*(s: []const U8): U64; (* FNV-1a, 64 bits *)
1391+proc find*(s, sub: []const U8): INT; (* where sub first starts in s, or -1 *)
1392+proc findFrom*(s, sub: []const U8; from: INT): INT; (* where sub first starts in s at or after from, or -1 *)
1393+proc findLast*(s, sub: []const U8): INT; (* where sub last starts in s, or -1 *)
1394+proc hasPrefix*(s, p: []const U8): BOOL;
1395+proc hasSuffix*(s, x: []const U8): BOOL;
1396+proc trim*(s: []const U8): []const U8; (* s without the spaces at either end *)
1397+proc trimLeft*(s: []const U8): []const U8; (* s without the spaces at its start *)
1398+proc trimRight*(s: []const U8): []const U8; (* s without the spaces at its end *)
1399+proc cut*(s, sep: []const U8): ([]const U8, []const U8, BOOL); (* what comes before the first sep, and after it *)
1400+proc field*(s: []const U8): ([]const U8, []const U8); (* the first run of bytes in s that aren&#39;t spaces, and what follows it *)
14001401 proc isDigit*(c: U8): BOOL; (* 0 to 9 *)
14011402 proc isLetter*(c: U8): BOOL; (* A to Z, a to z *)
14021403 proc isSpace*(c: U8): BOOL; (* a space, as trim takes it *)
@@ -1404,28 +1405,28 @@
14041405 proc hexVal*(c: U8): INT; (* c&#39;s value as a hexadecimal digit, or -1 *)
14051406 proc lower*(s: []U8); (* s&#39;s ASCII capitals made small, in place *)
14061407 proc upper*(s: []U8); (* s&#39;s ASCII small letters made capitals, in place *)
1407-proc join*(var b: Buf.Buf; parts: [][]U8; sep: []U8); (* the parts added to b, sep between each two *)
1408-proc replace*(var b: Buf.Buf; s, old, new: []U8); (* s added to b, with new for each old in it *)
1408+proc join*(var b: Buf.Buf; parts: [][]const U8; sep: []const U8); (* the parts added to b, sep between each two *)
1409+proc replace*(var b: Buf.Buf; s, old, new: []const U8); (* s added to b, with new for each old in it *)
14091410 proc encode*(c: I32): ([4]U8, INT); (* c in UTF-8: its bytes, and how many *)
1410-proc decode*(s: []U8): (I32, INT); (* the character s starts with, and its length in bytes *)
1411-proc decodeLast*(s: []U8): (I32, INT); (* the character s ends with, and its length in bytes *)
1412-proc next*(s: []U8; i: INT): (I32, INT); (* the character at i, and where the next starts; -1 at the end *)
1413-proc valid*(s: []U8): BOOL; (* s is UTF-8, every character rightly spelled *)
1414-proc chars*(s: []U8): INT; (* the characters in s, as decode reads them *)
1415-proc toInt*(s: []U8): (I64, BOOL); (* s, in decimal; false if it isn&#39;t a number an I64 holds *)
1416-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) *)
1411+proc decode*(s: []const U8): (I32, INT); (* the character s starts with, and its length in bytes *)
1412+proc decodeLast*(s: []const U8): (I32, INT); (* the character s ends with, and its length in bytes *)
1413+proc next*(s: []const U8; i: INT): (I32, INT); (* the character at i, and where the next starts; -1 at the end *)
1414+proc valid*(s: []const U8): BOOL; (* s is UTF-8, every character rightly spelled *)
1415+proc chars*(s: []const U8): INT; (* the characters in s, as decode reads them *)
1416+proc toInt*(s: []const U8): (I64, BOOL); (* s, in decimal; false if it isn&#39;t a number an I64 holds *)
1417+proc toHex*(s: []const U8): (U64, BOOL); (* s, in hexadecimal; false if it isn&#39;t a number a U64 holds *)
1418+proc toU64*(s: []const U8): (U64, BOOL); (* s, decimal digits only; false if it isn&#39;t one, or a U64 can&#39;t hold it *)
1419+proc fromC*(p: ^U8): []const U8; (* the bytes at p up to its zero byte, not copied; &quot;&quot; for nil *)
1420+proc toC*(s: []const U8; out: []U8): ^U8; (* s copied into out with a zero byte after it, which holds LEN(s) + 1 bytes at least: PTR(out) *)
14201421 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 *)
1422+proc base64With*(s: []const U8; out: []U8; alphabet: []const 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 *)
1423+proc unbase64With*(s: []const U8; out: []U8; alphabet: []const U8; pad: BOOL): (INT, BOOL); (* s, base64 as base64With wrote it, into out, which holds (LEN(s) + 3) div 4 * 3 bytes at least: the bytes written; false if s is not that *)
14231424 proc base64Size*(n: INT): INT; (* the bytes base64 makes of n bytes: 4 for each 3, or part of 3 *)
1424-proc base64*(s, out: []U8): INT; (* s in base64 into out, which holds base64Size(LEN(s)) bytes at least: the bytes written *)
1425-proc unbase64*(s, out: []U8): (INT, BOOL); (* s, base64, into out, which holds LEN(s) div 4 * 3 bytes at least: the bytes written; false if s is not base64 *)
1425+proc base64*(s: []const U8; out: []U8): INT; (* s in base64 into out, which holds base64Size(LEN(s)) bytes at least: the bytes written *)
1426+proc unbase64*(s: []const U8; out: []U8): (INT, BOOL); (* s, base64, into out, which holds LEN(s) div 4 * 3 bytes at least: the bytes written; false if s is not base64 *)
14261427 proc fromInt*(n: I64): ([20]U8, INT); (* n in decimal: its bytes, and how many *)
1427-proc le*(s: []U8): U64; (* s, at most 8 bytes, the first the lowest *)
1428-proc be*(s: []U8): U64; (* s, at most 8 bytes, the first the highest *)
1428+proc le*(s: []const U8): U64; (* s, at most 8 bytes, the first the lowest *)
1429+proc be*(s: []const U8): U64; (* s, at most 8 bytes, the first the highest *)
14291430 proc putLE*(s: []U8; v: U64); (* v into s, the lowest byte first *)
14301431 proc putBE*(s: []U8; v: U64); (* v into s, the highest byte first *)
14311432
@@ -1434,7 +1435,7 @@
14341435 proc init*(var b: Buf; a: ^Mem.Allocator);
14351436 proc room*(var b: Buf; n: INT): BOOL; (* room for n more bytes; false if the allocator has none *)
14361437 proc add*(var b: Buf; c: U8);
1437-proc str*(var b: Buf; s: []U8);
1438+proc str*(var b: Buf; s: []const U8);
14381439 proc spare*(var b: Buf; n: INT): []U8; (* room for n more bytes, and those bytes, to fill *)
14391440 proc bytes*(const b: Buf): []U8; (* what it holds, until it next grows *)
14401441 proc cut*(var b: Buf; n: INT); (* only the first n bytes kept, its memory kept *)
@@ -1447,17 +1448,17 @@
14471448 uint(u: U64);
14481449 hex(h: U64);
14491450 lowHex(h: U64);
1450- str(s: []U8);
1451+ str(s: []const U8);
14511452 char(c: U8);
14521453 bool(b: BOOL);
14531454 real(r: F64; places: INT);
14541455 end;
14551456 const ShortMost* = 24; (* the longest Fmt.short *)
14561457 proc short*(x: F64; var out: [ShortMost]U8): []U8; (* x in the fewest digits that read back, in out *)
1457-proc print*(f: []U8; args: []Arg); (* to standard output *)
1458-proc eprint*(f: []U8; args: []Arg); (* to standard error, once Out is flushed *)
1459-proc write*(var b: Buf.Buf; f: []U8; args: []Arg); (* added to b *)
1460-proc into*(out: []U8; f: []U8; args: []Arg): (INT, BOOL); (* into out: the bytes, and false if cut short *)
1458+proc print*(f: []const U8; args: []const Arg); (* to standard output *)
1459+proc eprint*(f: []const U8; args: []const Arg); (* to standard error, once Out is flushed *)
1460+proc write*(var b: Buf.Buf; f: []const U8; args: []const Arg); (* added to b *)
1461+proc into*(out: []U8; f: []const U8; args: []const Arg): (INT, BOOL); (* into out: the bytes, and false if cut short *)
14611462
14621463 module List(T);
14631464 type
@@ -1521,8 +1522,8 @@
15211522 proc fromBits*(u: U64): F64; (* the F64 whose bits are u *)
15221523 proc mul*(a, b: U64): (U64, U64); (* a times b: the low 64 bits of the product, and the high *)
15231524 proc divMod*(hi, lo, d: U64): (U64, U64); (* hi, lo divided by d: the quotient and the remainder; hi &lt; d *)
1524-proc toReal*(s: []U8): (F64, BOOL); (* s as a real, the F64 nearest it; false if it isn&#39;t one *)</code></pre>
1525-<p>A <code>Buf</code>, a <code>List</code>, a <code>Map</code> and a <code>Vec</code> take their memory from the allocator <code>init</code> gives them. <code>Buf.add</code>, <code>str</code> and <code>spare</code> stop the program, as a failed check does, if the allocator has no room; <code>room</code> asks first. A program reads into a <code>Buf</code> through <code>spare</code>, then adds what it read to <code>len</code>: <code>s := Buf.spare(b, 4096); n := Sys.read(fd, s); if n &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>
1525+proc toReal*(s: []const U8): (F64, BOOL); (* s as a real, the F64 nearest it; false if it isn&#39;t one *)</code></pre>
1526+<p>A <code>Buf</code>, a <code>List</code>, a <code>Map</code> and a <code>Vec</code> take their memory from the allocator <code>init</code> gives them. <code>Buf.add</code>, <code>str</code> and <code>spare</code> stop the program, as a failed check does, if the allocator has no room; <code>room</code> asks first. A program reads into a <code>Buf</code> through <code>spare</code>, then adds what it read to <code>len</code>: <code>s := Buf.spare(b, 4096); n := Sys.read(fd, s); if n &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([]const U8, INT, Str.hash, Str.eq);</code>.</p>
15261527 <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>
15271528 <pre><code>c, i := Str.next(s, 0);
15281529 while i &gt;= 0 do
@@ -1555,37 +1556,37 @@
15551556 <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>
15561557 <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>
15571558 <pre><code>module Files;
1558-proc arg*(i: INT): []U8; (* argument i; 0 is the program&#39;s name; &quot;&quot; if there is none *)
1559-proc read*(path: []U8; var b: Buf.Buf): BOOL; (* the whole file added to b *)
1560-proc load*(path: []U8; var b: Buf.Buf): INT; (* as read: 0, or minus the errno that stopped it *)
1561-proc write*(path, data: []U8): BOOL; (* the file made to hold data; false if it or its close failed *)
1562-proc writeAll*(fd: INT; s: []U8): INT; (* all of s written to fd; 0, or minus the errno *)
1563-proc realPath*(path: []U8; var b: Buf.Buf): INT; (* the absolute path of path, plain, added to b; 0, or minus an errno *)
1564-proc removeTree*(path: []U8): INT; (* the file, or the directory and all in it, removed; 0, or minus the first errno *)
1559+proc arg*(i: INT): []const U8; (* argument i; 0 is the program&#39;s name; &quot;&quot; if there is none *)
1560+proc read*(path: []const U8; var b: Buf.Buf): BOOL; (* the whole file added to b *)
1561+proc load*(path: []const U8; var b: Buf.Buf): INT; (* as read: 0, or minus the errno that stopped it *)
1562+proc write*(path, data: []const U8): BOOL; (* the file made to hold data; false if it or its close failed *)
1563+proc writeAll*(fd: INT; s: []const U8): INT; (* all of s written to fd; 0, or minus the errno *)
1564+proc realPath*(path: []const U8; var b: Buf.Buf): INT; (* the absolute path of path, plain, added to b; 0, or minus an errno *)
1565+proc removeTree*(path: []const U8): INT; (* the file, or the directory and all in it, removed; 0, or minus the first errno *)
15651566
15661567 module Opt;
15671568 type
15681569 Row* = variant
1569- usage(scope, line: []U8); (* a synopsis line, without &quot;usage: &quot; *)
1570- sub(scope, name, help: []U8); (* a subcommand *)
1571- option(scope: []U8; letter: U8; long, value, help: []U8);
1570+ usage(scope, line: []const U8); (* a synopsis line, without &quot;usage: &quot; *)
1571+ sub(scope, name, help: []const U8); (* a subcommand *)
1572+ option(scope: []const U8; letter: U8; long, value, help: []const U8);
15721573 end;
15731574 Parser* = record
15741575 letter*: U8; (* the option just read *)
1575- value*: []U8; (* its value; &quot;&quot; for a flag *)
1576+ value*: []const U8; (* its value; &quot;&quot; for a flag *)
15761577 long*: BOOL; (* it was written by its long name *)
1577- spelt*: []U8; (* as it was written, no dashes: &quot;b&quot; or &quot;bar&quot;; to read only *)
1578+ spelt*: []const U8; (* as it was written, no dashes: &quot;b&quot; or &quot;bar&quot;; to read only *)
15781579 ...
15791580 end;
1580-proc init*(var p: Parser; name: []U8; table: []Row); (* the arguments from 1 on, by table *)
1581+proc init*(var p: Parser; name: []const U8; table: []const Row); (* the arguments from 1 on, by table *)
15811582 proc next*(var p: Parser): BOOL; (* the next option; false when they end *)
1582-proc operand*(var p: Parser): ([]U8, BOOL); (* the next operand; false when there are no more *)
1583+proc operand*(var p: Parser): ([]const U8, BOOL); (* the next operand; false when there are no more *)
15831584 proc at*(var p: Parser): INT; (* the argument operand would give next *)
1584-proc scope*(var p: Parser; name: []U8); (* subcommand name&#39;s options, from the word after it *)
1585+proc scope*(var p: Parser; name: []const U8); (* subcommand name&#39;s options, from the word after it *)
15851586 proc number*(const p: Parser; lo, hi: I64): I64; (* the value just read, a number from lo to hi *)
1586-proc refuse*(const p: Parser; wants: []U8); (* the value just read refused, as number refuses one *)
1587-proc fail*(const p: Parser; text: []U8); (* the program&#39;s own mistake *)
1588-proc misuse*(const p: Parser; text: []U8); (* the program&#39;s own mistake, with the synopsis *)</code></pre>
1587+proc refuse*(const p: Parser; wants: []const U8); (* the value just read refused, as number refuses one *)
1588+proc fail*(const p: Parser; text: []const U8); (* the program&#39;s own mistake *)
1589+proc misuse*(const p: Parser; text: []const U8); (* the program&#39;s own mistake, with the synopsis *)</code></pre>
15891590 <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>
15901591 <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>
15911592 <pre><code>const wait = 1; (* the key of --wait, which has no letter *)
@@ -1634,7 +1635,7 @@
16341635 <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>
16351636 <pre><code>module Width;
16361637 proc width*(c: I32): INT; (* the columns the character c takes: 0, 1 or 2 *)
1637-proc columns*(s: []U8): INT; (* the columns the UTF-8 text s takes *)</code></pre>
1638+proc columns*(s: []const U8): INT; (* the columns the UTF-8 text s takes *)</code></pre>
16381639 <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>
16391640 <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>
16401641 <p><code>Rand</code> is a seeded generator of numbers, splitmix64: one <code>U64</code> of state. The same seed gives the same numbers on every machine, so a run can be replayed. It is not for secrets.</p>
@@ -1667,9 +1668,9 @@
16671668 end Dice.</code></pre>
16681669 <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>
16691670 <pre><code>module Check;
1670-proc ok*(cond: BOOL; what: []U8); (* fails, saying what, when cond is false *)
1671-proc str*(what, got, want: []U8); (* fails when got is not want; shows both, escaped, and the byte where they differ *)
1672-proc int*(what: []U8; got, want: I64); (* fails when got is not want; shows both *)
1671+proc ok*(cond: BOOL; what: []const U8); (* fails, saying what, when cond is false *)
1672+proc str*(what, got, want: []const U8); (* fails when got is not want; shows both, escaped, and the byte where they differ *)
1673+proc int*(what: []const U8; got, want: I64); (* fails when got is not want; shows both *)
16731674 proc mute*(on: BOOL); (* while on, checks say nothing and are not counted *)
16741675 proc mutedFailures*(): INT; (* how many failed since mute was turned on *)</code></pre>
16751676 <pre><code>module Digits;
@@ -1702,7 +1703,7 @@
17021703 proc pick*(var s: Source; n: INT): INT; (* 0 to n - 1; n at least 1 *)
17031704 proc bool*(var s: Source): BOOL;
17041705 proc bytes*(var s: Source; max: INT; var b: Buf.Buf); (* b, emptied, then given at most max bytes *)
1705-proc check*(name: []U8; p: Property); (* runs p on the saved cases, then on fresh ones *)</code></pre>
1706+proc check*(name: []const U8; p: Property); (* runs p on the saved cases, then on fresh ones *)</code></pre>
17061707 <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>
17071708 <pre><code>module Reverse;
17081709 import Prop, Check, Buf, Heap;

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

@@ -79,6 +79,7 @@
7979 <p><code>...</code> after an <code>extern</code>'s parameters takes any number of further arguments, as <code>printf</code> wants. A string literal goes as a C string, an integer constant as an <code>INT</code>, a real constant as an <code>F64</code>. An <code>F32</code> is an error there, since C promotes it; write <code>F64(x)</code>.</p>
8080 <div class="file"><span>Say.raz</span></div><pre class="code"><code><span class="kw">module</span> Say;
8181 <span class="kw">import</span> Out;
82+<span class="kw">import</span> SYSTEM;
8283
8384 <span class="kw">extern</span> <span class="kw">proc</span> printf(format: ^<span class="ty">U8</span>; ...): <span class="ty">I32</span>;
8485 <span class="kw">extern</span> <span class="kw">proc</span> fflush(f: ^<span class="ty">U8</span>): <span class="ty">I32</span>;
@@ -108,6 +109,7 @@
108109 <p><code>link "libm.so.6";</code> names a shared library to load at run time. An <code>extern var</code> is C's variable itself: reading it reads C's, assigning it writes C's.</p>
109110 <div class="file"><span>Env.raz</span></div><pre class="code"><code><span class="kw">module</span> Env;
110111 <span class="kw">import</span> Out;
112+<span class="kw">import</span> SYSTEM;
111113
112114 <span class="kw">extern</span> <span class="kw">var</span> environ: ^^<span class="ty">U8</span>;
113115 <span class="kw">extern</span> <span class="kw">proc</span> strlen(s: ^<span class="ty">U8</span>): <span class="ty">UINT</span>;
@@ -133,6 +135,7 @@
133135 <p>Every raz procedure uses the System V calling convention, so any with parameters and a result of C's types may be given to C to call, as <code>qsort</code> is given <code>order</code> above. The procedure may not be a closure: it has only its arguments, so state travels through the pointer C hands back or a module variable.</p>
134136 <div class="file"><span>Roots.raz</span></div><pre class="code"><code><span class="kw">module</span> Roots;
135137 <span class="kw">import</span> Fmt;
138+<span class="kw">import</span> SYSTEM;
136139
137140 <span class="kw">link</span> <span class="string">&quot;libm.so.6&quot;</span>;
138141

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

@@ -71,6 +71,7 @@
7171 <p>The same shape of message tells of a <code>nil</code> pointer, a division by zero, a bad slice and a stack overflow. Each of them stops the program with status 2, and never runs on with a wrong value.</p>
7272 <div class="file"><span>Nilp.raz</span></div><pre class="code"><code><span class="kw">module</span> Nilp;
7373 <span class="kw">import</span> Out;
74+<span class="kw">import</span> SYSTEM;
7475
7576 <span class="kw">type</span> Node = <span class="kw">record</span> val: <span class="ty">INT</span>; next: ^Node; <span class="kw">end</span>;
7677
@@ -84,16 +85,16 @@
8485 <span class="kw">var</span> a, b: Node;
8586 <span class="kw">begin</span>
8687 a.val := <span class="number">1</span>; b.val := <span class="number">2</span>;
87- a.next := <span class="ty">ADR</span>(b);
88- Out.int(last(<span class="ty">ADR</span>(a))); Out.ln;
88+ a.next := SYSTEM.<span class="ty">ADR</span>(b);
89+ Out.int(last(SYSTEM.<span class="ty">ADR</span>(a))); Out.ln;
8990 Out.int(last(<span class="ty">nil</span>)); Out.ln;
9091 <span class="kw">end</span>;
9192
9293 <span class="kw">end</span> Nilp.</code></pre>
9394 <pre class="output"><code>2
94-<span class="err">Nilp.raz:8:11: stopped by a nil pointer
95- in Nilp.last at Nilp.raz:8:11: n = nil
96- in Nilp.main at Nilp.raz:18:5
95+<span class="err">Nilp.raz:9:11: stopped by a nil pointer
96+ in Nilp.last at Nilp.raz:9:11: n = nil
97+ in Nilp.main at Nilp.raz:19:5
9798 </span></code></pre>
9899 <div class="file"><span>Zero.raz</span></div><pre class="code"><code><span class="kw">module</span> Zero;
99100 <span class="kw">import</span> Out;

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

@@ -52,7 +52,7 @@
5252 <span class="kw">end</span>;
5353
5454 <span class="comment">(* the lines of the file at path, or -1 if it can&#x27;t be read *)</span>
55-<span class="kw">proc</span> lines(path: []<span class="ty">U8</span>): <span class="ty">INT</span>;
55+<span class="kw">proc</span> lines(path: []<span class="kw">const</span> <span class="ty">U8</span>): <span class="ty">INT</span>;
5656 <span class="kw">var</span> fd, n, i, count: <span class="ty">INT</span>; buf: [<span class="number">4096</span>]<span class="ty">U8</span>;
5757 <span class="kw">begin</span>
5858 fd := Sys.openRead(path);

site/public/tour/errors.html (history) modified +7 -7

@@ -47,7 +47,7 @@
4747 TooBig*: <span class="ty">ERROR</span> = <span class="string">&quot;not a number up to 1000&quot;</span>;
4848
4949 <span class="comment">(* the number s holds, up to 1000 *)</span>
50-<span class="kw">proc</span> number(s: []<span class="ty">U8</span>): (<span class="ty">INT</span>, <span class="ty">ERROR</span>);
50+<span class="kw">proc</span> number(s: []<span class="kw">const</span> <span class="ty">U8</span>): (<span class="ty">INT</span>, <span class="ty">ERROR</span>);
5151 <span class="kw">var</span> n: <span class="ty">I64</span>; ok: <span class="ty">BOOL</span>;
5252 <span class="kw">begin</span>
5353 <span class="kw">if</span> <span class="ty">LEN</span>(s) = <span class="number">0</span> <span class="kw">then</span> <span class="kw">return</span> <span class="number">0</span>, Empty; <span class="kw">end</span>;
@@ -57,7 +57,7 @@
5757 <span class="kw">end</span>;
5858
5959 <span class="comment">(* the sum of the numbers in words: the first error ends it *)</span>
60-<span class="kw">proc</span> total(words: [][]<span class="ty">U8</span>): (<span class="ty">INT</span>, <span class="ty">ERROR</span>);
60+<span class="kw">proc</span> total(words: [][]<span class="kw">const</span> <span class="ty">U8</span>): (<span class="ty">INT</span>, <span class="ty">ERROR</span>);
6161 <span class="kw">var</span> i, sum: <span class="ty">INT</span>;
6262 <span class="kw">begin</span>
6363 <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="ty">LEN</span>(words) - <span class="number">1</span> <span class="kw">do</span>
@@ -67,7 +67,7 @@
6767 <span class="kw">end</span>;
6868
6969 <span class="kw">proc</span> main*;
70-<span class="kw">var</span> words: [<span class="number">3</span>][]<span class="ty">U8</span>; i, n: <span class="ty">INT</span>; err: <span class="ty">ERROR</span>;
70+<span class="kw">var</span> words: [<span class="number">3</span>][]<span class="kw">const</span> <span class="ty">U8</span>; i, n: <span class="ty">INT</span>; err: <span class="ty">ERROR</span>;
7171 <span class="kw">begin</span>
7272 words := [<span class="string">&quot;42&quot;</span>, <span class="string">&quot;&quot;</span>, <span class="string">&quot;12345&quot;</span>];
7373 <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="number">2</span> <span class="kw">do</span>
@@ -103,7 +103,7 @@
103103 Range*: <span class="ty">ERROR</span> = <span class="string">&quot;out of range&quot;</span>;
104104
105105 <span class="comment">(* a percentage, 0 to 100 *)</span>
106-<span class="kw">proc</span> percent(s: []<span class="ty">U8</span>): (<span class="ty">INT</span>, <span class="ty">ERROR</span>);
106+<span class="kw">proc</span> percent(s: []<span class="kw">const</span> <span class="ty">U8</span>): (<span class="ty">INT</span>, <span class="ty">ERROR</span>);
107107 <span class="kw">var</span> n: <span class="ty">I64</span>; ok: <span class="ty">BOOL</span>;
108108 <span class="kw">begin</span>
109109 <span class="kw">if</span> <span class="ty">LEN</span>(s) = <span class="number">0</span> <span class="kw">then</span> <span class="kw">return</span> <span class="number">0</span>, Empty; <span class="kw">end</span>;
@@ -114,7 +114,7 @@
114114 <span class="kw">end</span>;
115115
116116 <span class="kw">proc</span> main*;
117-<span class="kw">var</span> inputs: [<span class="number">4</span>][]<span class="ty">U8</span>; i, n: <span class="ty">INT</span>; err: <span class="ty">ERROR</span>;
117+<span class="kw">var</span> inputs: [<span class="number">4</span>][]<span class="kw">const</span> <span class="ty">U8</span>; i, n: <span class="ty">INT</span>; err: <span class="ty">ERROR</span>;
118118 <span class="kw">begin</span>
119119 inputs := [<span class="string">&quot;42&quot;</span>, <span class="string">&quot;&quot;</span>, <span class="string">&quot;forty&quot;</span>, <span class="string">&quot;140&quot;</span>];
120120 <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="number">3</span> <span class="kw">do</span>
@@ -146,7 +146,7 @@
146146 <span class="kw">const</span> Missing*: <span class="ty">ERROR</span> = <span class="string">&quot;key missing&quot;</span>;
147147
148148 <span class="comment">(* the number after &quot;n=&quot; in a line like &quot;n=17&quot; *)</span>
149-<span class="kw">proc</span> value(line: []<span class="ty">U8</span>): (<span class="ty">INT</span>, <span class="ty">ERROR</span>);
149+<span class="kw">proc</span> value(line: []<span class="kw">const</span> <span class="ty">U8</span>): (<span class="ty">INT</span>, <span class="ty">ERROR</span>);
150150 <span class="kw">var</span> n: <span class="ty">I64</span>; ok: <span class="ty">BOOL</span>;
151151 <span class="kw">begin</span>
152152 <span class="kw">if</span> <span class="kw">not</span> Str.hasPrefix(line, <span class="string">&quot;n=&quot;</span>) <span class="kw">then</span> <span class="kw">return</span> <span class="number">0</span>, Missing; <span class="kw">end</span>;
@@ -156,7 +156,7 @@
156156 <span class="kw">end</span>;
157157
158158 <span class="comment">(* the sum of the values of two lines: the first error ends it *)</span>
159-<span class="kw">proc</span> sum(a, b: []<span class="ty">U8</span>): (<span class="ty">INT</span>, <span class="ty">ERROR</span>);
159+<span class="kw">proc</span> sum(a, b: []<span class="kw">const</span> <span class="ty">U8</span>): (<span class="ty">INT</span>, <span class="ty">ERROR</span>);
160160 <span class="kw">begin</span>
161161 <span class="kw">return</span> <span class="kw">try</span> value(a) + <span class="kw">try</span> value(b), <span class="ty">nil</span>;
162162 <span class="kw">end</span>;

site/public/tour/generics.html (history) modified +12 -10

@@ -57,14 +57,14 @@
5757 <p>and a program that uses it:</p>
5858 <div class="file"><span>Generics.raz</span></div><pre class="code"><code><span class="kw">module</span> Generics;
5959 <span class="kw">import</span> Out, Ints := Biggest(<span class="ty">INT</span>, before),
60- Words := Biggest([]<span class="ty">U8</span>, shorter), Order := Sort(<span class="ty">INT</span>, before);
60+ Words := Biggest([]<span class="kw">const</span> <span class="ty">U8</span>, shorter), Order := Sort(<span class="ty">INT</span>, before);
6161
6262 <span class="kw">proc</span> before(a, b: <span class="ty">INT</span>): <span class="ty">BOOL</span>;
6363 <span class="kw">begin</span>
6464 <span class="kw">return</span> a &lt; b;
6565 <span class="kw">end</span>;
6666
67-<span class="kw">proc</span> shorter(a, b: []<span class="ty">U8</span>): <span class="ty">BOOL</span>;
67+<span class="kw">proc</span> shorter(a, b: []<span class="kw">const</span> <span class="ty">U8</span>): <span class="ty">BOOL</span>;
6868 <span class="kw">begin</span>
6969 <span class="kw">return</span> <span class="ty">LEN</span>(a) &lt; <span class="ty">LEN</span>(b);
7070 <span class="kw">end</span>;
@@ -90,6 +90,7 @@
9090 <p>A generic module is an ordinary module with parameters in its header. Here is a stack of anything, which takes the allocator it grows from.</p>
9191 <div class="file"><span>Stk.raz</span></div><pre class="code"><code><span class="kw">module</span> Stk(T);
9292 <span class="kw">import</span> Mem;
93+<span class="kw">import</span> SYSTEM;
9394
9495 <span class="kw">type</span>
9596 Stack* = <span class="kw">record</span>
@@ -142,21 +143,22 @@
142143 <span class="kw">end</span> Stk.</code></pre>
143144 <div class="file"><span>Uses.raz</span></div><pre class="code"><code><span class="kw">module</span> Uses;
144145 <span class="kw">import</span> Out, Heap, Nums := Stk(<span class="ty">INT</span>), Pairs := Stk(Pair);
146+<span class="kw">import</span> SYSTEM;
145147
146148 <span class="kw">type</span> Pair = <span class="kw">record</span> a, b: <span class="ty">INT</span>; <span class="kw">end</span>;
147149
148150 <span class="kw">proc</span> main*;
149151 <span class="kw">var</span> n: Nums.Stack; p: Pairs.Stack; i, v: <span class="ty">INT</span>; ok: <span class="ty">BOOL</span>; pr: Pair;
150152 <span class="kw">begin</span>
151- Nums.init(<span class="ty">ADR</span>(n), Heap.heap);
152- Pairs.init(<span class="ty">ADR</span>(p), Heap.heap);
153- <span class="kw">for</span> i := <span class="number">1</span> <span class="kw">to</span> <span class="number">10</span> <span class="kw">do</span> _ := Nums.push(<span class="ty">ADR</span>(n), i * i); <span class="kw">end</span>;
154- _ := Pairs.push(<span class="ty">ADR</span>(p), {a: <span class="number">3</span>, b: <span class="number">4</span>});
155- v, ok := Nums.pop(<span class="ty">ADR</span>(n));
153+ Nums.init(SYSTEM.<span class="ty">ADR</span>(n), Heap.heap);
154+ Pairs.init(SYSTEM.<span class="ty">ADR</span>(p), Heap.heap);
155+ <span class="kw">for</span> i := <span class="number">1</span> <span class="kw">to</span> <span class="number">10</span> <span class="kw">do</span> _ := Nums.push(SYSTEM.<span class="ty">ADR</span>(n), i * i); <span class="kw">end</span>;
156+ _ := Pairs.push(SYSTEM.<span class="ty">ADR</span>(p), {a: <span class="number">3</span>, b: <span class="number">4</span>});
157+ v, ok := Nums.pop(SYSTEM.<span class="ty">ADR</span>(n));
156158 Out.int(v); Out.str(<span class="string">&quot; of &quot;</span>); Out.int(n.len + <span class="number">1</span>); Out.ln;
157- pr, ok := Pairs.pop(<span class="ty">ADR</span>(p));
159+ pr, ok := Pairs.pop(SYSTEM.<span class="ty">ADR</span>(p));
158160 Out.int(pr.a * pr.b); Out.ln;
159- _, ok := Pairs.pop(<span class="ty">ADR</span>(p));
161+ _, ok := Pairs.pop(SYSTEM.<span class="ty">ADR</span>(p));
160162 <span class="kw">if</span> <span class="kw">not</span> ok <span class="kw">then</span> Out.str(<span class="string">&quot;empty\n&quot;</span>); <span class="kw">end</span>;
161163 <span class="kw">end</span>;
162164
@@ -167,7 +169,7 @@
167169 </code></pre>
168170 <p>Two instances (<code>Nums</code> and <code>Pairs</code>) are two separate modules, compiled with their own <code>T</code>. Its pointer arithmetic is the one place where the program does what C does and leaves the checks behind; a library would keep that inside, as this does.</p>
169171 <h2>The library's generics</h2>
170-<p>The library's <code>List</code>, <code>Map</code> and <code>Sort</code> are generic modules. A <code>Map</code> takes its key hash and equality as procedure arguments: <code>import Count := Map([]U8, INT, Str.hash, Str.eq);</code>. The advanced tour uses each of them in a real program.</p><p class="tryit">Save a program under its module&rsquo;s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="checks.html">&larr; When it goes wrong</a><a href="index.html">Contents</a><a class="next" href="c.html">Talking to C &rarr;</a></div></article></div>
172+<p>The library's <code>List</code>, <code>Map</code> and <code>Sort</code> are generic modules. A <code>Map</code> takes its key hash and equality as procedure arguments: <code>import Count := Map([]const U8, INT, Str.hash, Str.eq);</code>. The advanced tour uses each of them in a real program.</p><p class="tryit">Save a program under its module&rsquo;s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="checks.html">&larr; When it goes wrong</a><a href="index.html">Contents</a><a class="next" href="c.html">Talking to C &rarr;</a></div></article></div>
171173 </main>
172174 <footer>raz is under the ISC license. <a href="../source/index.html">Source</a></footer>
173175 </body>

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

@@ -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-28680 bytes</code></pre>
73+28608 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>
@@ -110,9 +110,10 @@
110110 exiting with 3
111111 status 3</code></pre>
112112 <h2>Arguments</h2>
113-<p><code>ARGC()</code> is the number of command line arguments, with the program's own name first, and <code>ARGV()</code> the arguments, as C strings. Most programs read them through <code>Files.arg</code> and <code>Opt</code>, which the advanced tour shows; here is the raw form.</p>
113+<p><code>ARGC()</code> is the number of command line arguments, with the program's own name first, and <code>SYSTEM.ARGV()</code> the arguments, as C strings. Most programs read them through <code>Files.arg</code> and <code>Opt</code>, which the advanced tour shows; here is the raw form.</p>
114114 <div class="file"><span>Args.raz</span></div><pre class="code"><code><span class="kw">module</span> Args;
115115 <span class="kw">import</span> Out;
116+<span class="kw">import</span> SYSTEM;
116117
117118 <span class="comment">(* The length of a C string. *)</span>
118119 <span class="kw">proc</span> len(s: ^<span class="ty">U8</span>): <span class="ty">INT</span>;
@@ -127,7 +128,7 @@
127128 <span class="kw">var</span> i: <span class="ty">INT</span>; s: ^<span class="ty">U8</span>;
128129 <span class="kw">begin</span>
129130 <span class="kw">for</span> i := <span class="number">1</span> <span class="kw">to</span> ARGC() - <span class="number">1</span> <span class="kw">do</span>
130- s := ARGV()[i];
131+ s := SYSTEM.ARGV()[i];
131132 Out.str(s[<span class="number">0</span>..len(s)]);
132133 Out.ln;
133134 <span class="kw">end</span>;

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

@@ -140,7 +140,7 @@
140140 <span class="kw">const</span> Prime = <span class="number">100000001B3H</span>;
141141
142142 <span class="comment">(* FNV-1a: xor each byte in, then multiply, all modulo 2^64 *)</span>
143-<span class="kw">proc</span> fnv(s: []<span class="ty">U8</span>): <span class="ty">U64</span>;
143+<span class="kw">proc</span> fnv(s: []<span class="kw">const</span> <span class="ty">U8</span>): <span class="ty">U64</span>;
144144 <span class="kw">var</span> h: <span class="ty">U64</span>; i: <span class="ty">INT</span>;
145145 <span class="kw">begin</span>
146146 h := Offset;

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

@@ -83,6 +83,7 @@
8383 <p>A procedure that allocates takes the allocator it uses, so its caller decides where the memory comes from and when it goes: the heap for something that lives long, an arena for everything one request needs. Nothing in the library allocates behind your back.</p>
8484 <div class="file"><span>Pool.raz</span></div><pre class="code"><code><span class="kw">module</span> Pool;
8585 <span class="kw">import</span> Out, Mem, Arena;
86+<span class="kw">import</span> SYSTEM;
8687
8788 <span class="kw">type</span> Item = <span class="kw">record</span> id: <span class="ty">INT</span>; next: ^Item; <span class="kw">end</span>;
8889
@@ -113,15 +114,15 @@
113114 <span class="kw">var</span> ar: Arena.Arena; small: Arena.Arena; list: ^Item;
114115 <span class="kw">begin</span>
115116 <span class="kw">if</span> <span class="kw">not</span> Arena.init(ar, <span class="number">1000000</span>) <span class="kw">then</span> <span class="kw">return</span>; <span class="kw">end</span>;
116- list := make(<span class="number">1000</span>, <span class="ty">ADR</span>(ar.base));
117+ list := make(<span class="number">1000</span>, SYSTEM.<span class="ty">ADR</span>(ar.base));
117118 Out.int(count(list)); Out.str(<span class="string">&quot; items from a big arena\n&quot;</span>);
118119 Arena.reset(ar); <span class="comment">(* every allocation forgotten *)</span>
119- list := make(<span class="number">10</span>, <span class="ty">ADR</span>(ar.base));
120+ list := make(<span class="number">10</span>, SYSTEM.<span class="ty">ADR</span>(ar.base));
120121 Out.int(count(list)); Out.str(<span class="string">&quot; after a reset\n&quot;</span>);
121122 Arena.done(ar);
122123
123124 <span class="kw">if</span> <span class="kw">not</span> Arena.init(small, <span class="number">4096</span>) <span class="kw">then</span> <span class="kw">return</span>; <span class="kw">end</span>;
124- list := make(<span class="number">1000</span>, <span class="ty">ADR</span>(small.base));
125+ list := make(<span class="number">1000</span>, SYSTEM.<span class="ty">ADR</span>(small.base));
125126 <span class="kw">if</span> list = <span class="ty">nil</span> <span class="kw">then</span> Out.str(<span class="string">&quot;a small arena ran out, and said so\n&quot;</span>); <span class="kw">end</span>;
126127 Arena.done(small);
127128 <span class="kw">end</span>;
@@ -136,6 +137,7 @@
136137 <p><code>Heap.heap</code> serves any size and takes memory back with <code>Mem.free</code>. Freeing a block twice, or one that was never given out, stops the program with a message instead of corrupting the heap. It is safe to call from several threads.</p>
137138 <div class="file"><span>Free.raz</span></div><pre class="code"><code><span class="kw">module</span> Free;
138139 <span class="kw">import</span> Out, Mem, Heap;
140+<span class="kw">import</span> SYSTEM;
139141
140142 <span class="kw">type</span> Cell = <span class="kw">record</span> val: <span class="ty">INT</span>; <span class="kw">end</span>;
141143

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

@@ -45,7 +45,7 @@
4545
4646 <span class="kw">var</span> count*: <span class="ty">INT</span>; <span class="comment">(* the greetings so far *)</span>
4747
48-<span class="kw">proc</span> hello*(name: []<span class="ty">U8</span>);
48+<span class="kw">proc</span> hello*(name: []<span class="kw">const</span> <span class="ty">U8</span>);
4949 <span class="kw">begin</span>
5050 Out.str(<span class="string">&quot;hello, &quot;</span>);
5151 Out.str(name);

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

@@ -213,7 +213,7 @@
213213 <span class="kw">end</span>;
214214
215215 <span class="kw">proc</span> main*;
216-<span class="kw">var</span> ops: [<span class="number">3</span>]Op; names: [<span class="number">3</span>][]<span class="ty">U8</span>; i: <span class="ty">INT</span>;
216+<span class="kw">var</span> ops: [<span class="number">3</span>]Op; names: [<span class="number">3</span>][]<span class="kw">const</span> <span class="ty">U8</span>; i: <span class="ty">INT</span>;
217217 <span class="kw">begin</span>
218218 ops := [add, sub, mul];
219219 names := [<span class="string">&quot;add&quot;</span>, <span class="string">&quot;sub&quot;</span>, <span class="string">&quot;mul&quot;</span>];

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

@@ -74,7 +74,7 @@
7474 <pre class="output"><code>6
7575 40 10
7676 </code></pre>
77-<p>A record is laid out as C lays out a struct. <code>{lo: ..., hi: ...}</code> builds one, naming its fields; the ones it leaves out are zero. <code>^Box</code> is a pointer to a <code>Box</code>, <code>ADR(b)</code> the address of <code>b</code>, and <code>b^</code> what a pointer points at. A pointer is never the record: it is <code>b^.lo</code>, never <code>b.lo</code>, so you always see where the memory is reached through an address.</p>
77+<p>A record is laid out as C lays out a struct. <code>{lo: ..., hi: ...}</code> builds one, naming its fields; the ones it leaves out are zero. <code>^Box</code> is a pointer to a <code>Box</code>, <code>SYSTEM.ADR(b)</code> the address of <code>b</code>, and <code>b^</code> what a pointer points at. A pointer is never the record: it is <code>b^.lo</code>, never <code>b.lo</code>, so you always see where the memory is reached through an address.</p>
7878 <h2>Copies and literals</h2>
7979 <p>Assigning a record copies it whole, as with an array. A record is compared field by field by writing the comparison, not by <code>=</code>.</p>
8080 <div class="file"><span>Points.raz</span></div><pre class="code"><code><span class="kw">module</span> Points;
@@ -112,7 +112,7 @@
112112 <div class="file"><span>Planets.raz</span></div><pre class="code"><code><span class="kw">module</span> Planets;
113113 <span class="kw">import</span> Fmt;
114114
115-<span class="kw">type</span> Planet = <span class="kw">record</span> name: []<span class="ty">U8</span>; moons: <span class="ty">INT</span>; water: <span class="ty">BOOL</span>; <span class="kw">end</span>;
115+<span class="kw">type</span> Planet = <span class="kw">record</span> name: []<span class="kw">const</span> <span class="ty">U8</span>; moons: <span class="ty">INT</span>; water: <span class="ty">BOOL</span>; <span class="kw">end</span>;
116116
117117 <span class="kw">const</span> Inner: []Planet = [
118118 {name: <span class="string">&quot;Mercury&quot;</span>},
@@ -159,7 +159,7 @@
159159 INT: 8
160160 [5]I16: 10
161161 </code></pre>
162-<p>A field of a packed record may sit at any address, and is read and written like any other; but it has no address of its own to take with <code>ADR</code>.</p>
162+<p>A field of a packed record may sit at any address, and is read and written like any other; but it has no address of its own to take with <code>SYSTEM.ADR</code>.</p>
163163 <h2>Bytes as numbers and records: views</h2>
164164 <p>A file, a socket or a device hands a program bytes. A view reads and writes them as another type: <code>msg[3].(U32)</code> is the <code>U32</code> whose four bytes start at <code>msg[3]</code>, and <code>msg[0].(Head)</code> the <code>Head</code> whose bytes start at <code>msg[0]</code>.</p>
165165 <div class="file"><span>Wire.raz</span></div><pre class="code"><code><span class="kw">module</span> Wire;
@@ -219,11 +219,12 @@
219219 </span></code></pre>
220220 <p>The type of a view is an integer, a real or a pointer, or a record or array made of them: types any bytes are a value of, which a <code>BOOL</code> or an enumeration isn't. A view may start at any byte, so, as a packed record's field, it has no address of its own.</p>
221221 <h2>Pointers</h2>
222-<p><code>ADR(x)</code> takes the address of a variable, <code>^T</code> is a pointer to a <code>T</code>, and <code>p^</code> is what it points at. A pointer that points nowhere is <code>nil</code>, and following it stops the program with a message that names the place, so a forgotten check is found at once instead of corrupting memory.</p>
222+<p><code>SYSTEM.ADR(x)</code> (a module that uses it says <code>import SYSTEM;</code>) takes the address of a variable, <code>^T</code> is a pointer to a <code>T</code>, and <code>p^</code> is what it points at. A pointer that points nowhere is <code>nil</code>, and following it stops the program with a message that names the place, so a forgotten check is found at once instead of corrupting memory.</p>
223223 <div class="file"><span>Pointers.raz</span></div><pre class="code"><code><span class="kw">module</span> Pointers;
224224 <span class="kw">import</span> Out;
225+<span class="kw">import</span> SYSTEM;
225226
226-<span class="kw">type</span> Counter = <span class="kw">record</span> hits: <span class="ty">INT</span>; name: []<span class="ty">U8</span>; <span class="kw">end</span>;
227+<span class="kw">type</span> Counter = <span class="kw">record</span> hits: <span class="ty">INT</span>; name: []<span class="kw">const</span> <span class="ty">U8</span>; <span class="kw">end</span>;
227228
228229 <span class="kw">proc</span> hit(c: ^Counter);
229230 <span class="kw">begin</span>
@@ -243,12 +244,12 @@
243244 <span class="kw">var</span> c: Counter; x, y: <span class="ty">INT</span>; p: ^<span class="ty">INT</span>;
244245 <span class="kw">begin</span>
245246 c.name := <span class="string">&quot;front door&quot;</span>;
246- hit(<span class="ty">ADR</span>(c)); hit(<span class="ty">ADR</span>(c)); hit(<span class="ty">nil</span>);
247+ hit(SYSTEM.<span class="ty">ADR</span>(c)); hit(SYSTEM.<span class="ty">ADR</span>(c)); hit(<span class="ty">nil</span>);
247248 Out.str(c.name); Out.str(<span class="string">&quot;: &quot;</span>); Out.int(c.hits); Out.ln;
248249 x := <span class="number">1</span>; y := <span class="number">2</span>;
249- swap(<span class="ty">ADR</span>(x), <span class="ty">ADR</span>(y));
250+ swap(SYSTEM.<span class="ty">ADR</span>(x), SYSTEM.<span class="ty">ADR</span>(y));
250251 Out.int(x); Out.char(<span class="string">&#x27; &#x27;</span>); Out.int(y); Out.ln;
251- p := <span class="ty">ADR</span>(x);
252+ p := SYSTEM.<span class="ty">ADR</span>(x);
252253 p^ := p^ + <span class="number">40</span>;
253254 Out.int(x); Out.ln;
254255 <span class="kw">end</span>;
@@ -262,6 +263,7 @@
262263 <p>A record may hold a pointer to its own type, which is how lists and trees are made. Memory for them comes from an allocator, which is the next lesson; here the nodes simply live in an array.</p>
263264 <div class="file"><span>Chain.raz</span></div><pre class="code"><code><span class="kw">module</span> Chain;
264265 <span class="kw">import</span> Out;
266+<span class="kw">import</span> SYSTEM;
265267
266268 <span class="kw">type</span> Node = <span class="kw">record</span> next: ^Node; val: <span class="ty">INT</span>; <span class="kw">end</span>;
267269
@@ -272,7 +274,7 @@
272274 <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>
273275 nodes[i].val := (i + <span class="number">1</span>) * <span class="number">10</span>;
274276 nodes[i].next := head; <span class="comment">(* each new one in front *)</span>
275- head := <span class="ty">ADR</span>(nodes[i]);
277+ head := SYSTEM.<span class="ty">ADR</span>(nodes[i]);
276278 <span class="kw">end</span>;
277279 n := head;
278280 <span class="kw">while</span> n # <span class="ty">nil</span> <span class="kw">do</span>
@@ -286,15 +288,16 @@
286288 <pre class="output"><code>40 30 20 10
287289 </code></pre>
288290 <h2>A slice's pointer</h2>
289-<p>A slice is a pointer and a length, and a record can keep the two apart: an interpreter keeps a string so, and C takes one so. <code>LEN(s)</code> is the length. The pointer looks like <code>ADR(s[0])</code>, the address of the first element, and that works until the slice is empty: then there is no first element, <code>s[0]</code> is out of range, and the program stops.</p>
291+<p>A slice is a pointer and a length, and a record can keep the two apart: an interpreter keeps a string so, and C takes one so. <code>LEN(s)</code> is the length. The pointer looks like <code>SYSTEM.ADR(s[0])</code>, the address of the first element, and that works until the slice is empty: then there is no first element, <code>s[0]</code> is out of range, and the program stops.</p>
290292 <div class="file"><span>Empty.raz</span></div><pre class="code"><code><span class="kw">module</span> Empty;
291293 <span class="kw">import</span> Out;
294+<span class="kw">import</span> SYSTEM;
292295
293296 <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>;
294297
295-<span class="kw">proc</span> spanOf(s: []<span class="ty">U8</span>): Span;
298+<span class="kw">proc</span> spanOf(s: []<span class="kw">const</span> <span class="ty">U8</span>): Span;
296299 <span class="kw">begin</span>
297- <span class="kw">return</span> {p: <span class="ty">ADR</span>(s[<span class="number">0</span>]), n: <span class="ty">LEN</span>(s)};
300+ <span class="kw">return</span> {p: SYSTEM.<span class="ty">ADR</span>(s[<span class="number">0</span>]), n: <span class="ty">LEN</span>(s)};
298301 <span class="kw">end</span>;
299302
300303 <span class="kw">proc</span> main*;
@@ -307,18 +310,19 @@
307310 <span class="kw">end</span>;
308311
309312 <span class="kw">end</span> Empty.</code></pre>
310-<pre class="output"><code>3
311-<span class="err">Empty.raz:8:22: index 0 is out of range for a length of 0
312- in Empty.spanOf at Empty.raz:8:22
313- in Empty.main at Empty.raz:16:5
313+<pre class="output"><code><span class="err">Empty.raz:9:27: error: s[0] is read through a []const slice, and can&#x27;t be changed
314+ 9 | return {p: SYSTEM.ADR(s[0]), n: LEN(s)};
315+ | ^^^^
316+help: a []const slice&#x27;s elements are only read: write them through a []T
317+raz: 1 error
314318 </span></code></pre>
315-<p><code>PTR(s)</code> is the pointer itself, a <code>^U8</code> for a <code>[]U8</code>, with no check. For a slice that isn't empty it is <code>ADR(s[0])</code>; for an empty one it is what the slice holds, <code>nil</code> for one never set. So nothing needs testing first, and <code>sp.p[0..sp.n]</code> makes the slice again, empty when <code>sp.n</code> is 0, whatever <code>sp.p</code> is:</p>
319+<p><code>SYSTEM.PTR(s)</code> is the pointer itself, a <code>^U8</code> for a <code>[]U8</code>, with no check. For a slice that isn't empty it is <code>SYSTEM.ADR(s[0])</code>; for an empty one it is what the slice holds, <code>nil</code> for one never set. So nothing needs testing first, and <code>sp.p[0..sp.n]</code> makes the slice again, empty when <code>sp.n</code> is 0, whatever <code>sp.p</code> is:</p>
316320 <div class="file"><span>Spans.raz</span></div><pre class="code"><code><span class="kw">module</span> Spans;
317321 <span class="kw">import</span> Out, SYSTEM;
318322
319323 <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>;
320324
321-<span class="kw">proc</span> spanOf(s: []<span class="ty">U8</span>): Span;
325+<span class="kw">proc</span> spanOf(s: []<span class="kw">const</span> <span class="ty">U8</span>): Span;
322326 <span class="kw">begin</span>
323327 <span class="kw">return</span> {p: SYSTEM.PTR(s), n: <span class="ty">LEN</span>(s)};
324328 <span class="kw">end</span>;
@@ -343,7 +347,7 @@
343347 0
344348 5 three
345349 </code></pre>
346-<p>Of an array, <code>PTR(a)</code> is the address of its first element, a <code>^T</code> where <code>ADR(a)</code> is a <code>^[n]T</code>. Reading through the pointer is as unchecked as through any pointer: the length is yours to keep with it. The compiler says so when the two are mixed up: a slice given where a pointer is wanted is an error whose help says to write <code>PTR(s)</code> and <code>LEN(s)</code>, and a pointer given where a slice is, <code>p[0..n]</code>.</p><p class="tryit">Save a program under its module&rsquo;s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="slices.html">&larr; Arrays, slices and strings</a><a href="index.html">Contents</a><a class="next" href="variants.html">Enumerations, sets, variants and match &rarr;</a></div></article></div>
350+<p>Of an array, <code>SYSTEM.PTR(a)</code> is the address of its first element, a <code>^T</code> where <code>SYSTEM.ADR(a)</code> is a <code>^[n]T</code>. Reading through the pointer is as unchecked as through any pointer: the length is yours to keep with it. The compiler says so when the two are mixed up: a slice given where a pointer is wanted is an error whose help says to write <code>PTR(s)</code> and <code>LEN(s)</code>, and a pointer given where a slice is, <code>p[0..n]</code>.</p><p class="tryit">Save a program under its module&rsquo;s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="slices.html">&larr; Arrays, slices and strings</a><a href="index.html">Contents</a><a class="next" href="variants.html">Enumerations, sets, variants and match &rarr;</a></div></article></div>
347351 </main>
348352 <footer>raz is under the ISC license. <a href="../source/index.html">Source</a></footer>
349353 </body>

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

@@ -51,7 +51,7 @@
5151 <span class="kw">end</span>;
5252
5353 <span class="kw">proc</span> main*;
54-<span class="kw">var</span> a: [<span class="number">5</span>]<span class="ty">INT</span>; i: <span class="ty">INT</span>; s, val: []<span class="ty">U8</span>; name: [<span class="number">8</span>]<span class="ty">U8</span>; ok: <span class="ty">BOOL</span>;
54+<span class="kw">var</span> a: [<span class="number">5</span>]<span class="ty">INT</span>; i: <span class="ty">INT</span>; s, val: []<span class="kw">const</span> <span class="ty">U8</span>; name: [<span class="number">8</span>]<span class="ty">U8</span>; ok: <span class="ty">BOOL</span>;
5555 <span class="kw">begin</span>
5656 <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="number">4</span> <span class="kw">do</span> a[i] := i * i; <span class="kw">end</span>;
5757 Out.int(sum(a)); Out.ln; <span class="comment">(* the array, as a slice *)</span>
@@ -162,12 +162,12 @@
162162 <pre class="output"><code>1 1 2 3 4 5
163163 </code></pre>
164164 <h2>Strings</h2>
165-<p>A string is a slice of bytes, <code>[]U8</code>, in UTF-8, and a string constant's bytes are constant. A <code>[8]U8</code> made from one is a copy you can change. Arrays and slices of integers compare with <code>=</code> and <code>#</code>, so strings do too. <code>Str</code> has the rest: ordering, finding, hashing, prefixes and suffixes, trimming, taking apart, ASCII case and kinds of character, UTF-8, and numbers to and from text.</p>
165+<p>A string is a slice of bytes, <code>[]U8</code>, in UTF-8, and a string constant is a <code>[]const U8</code>: a procedure that only reads a string takes that, and one that writes takes <code>[]U8</code>. A <code>[8]U8</code> made from one is a copy you can change. Arrays and slices of integers compare with <code>=</code> and <code>#</code>, so strings do too. <code>Str</code> has the rest: ordering, finding, hashing, prefixes and suffixes, trimming, taking apart, ASCII case and kinds of character, UTF-8, and numbers to and from text.</p>
166166 <div class="file"><span>Words.raz</span></div><pre class="code"><code><span class="kw">module</span> Words;
167167 <span class="kw">import</span> Out, Str;
168168
169169 <span class="kw">proc</span> main*;
170-<span class="kw">var</span> s: []<span class="ty">U8</span>; n: <span class="ty">I64</span>; ok: <span class="ty">BOOL</span>; name: [<span class="number">8</span>]<span class="ty">U8</span>;
170+<span class="kw">var</span> s: []<span class="kw">const</span> <span class="ty">U8</span>; n: <span class="ty">I64</span>; ok: <span class="ty">BOOL</span>; name: [<span class="number">8</span>]<span class="ty">U8</span>;
171171 <span class="kw">begin</span>
172172 s := <span class="string">&quot;hello, world&quot;</span>;
173173 Out.str(s[<span class="number">7</span>..<span class="ty">LEN</span>(s)]); Out.ln;
@@ -221,7 +221,7 @@
221221 <span class="kw">const</span> Text = <span class="string">&quot;name = raz\nkind = language\nyear = 2026\n&quot;</span>;
222222
223223 <span class="kw">proc</span> main*;
224-<span class="kw">var</span> line, rest, key, val, w: []<span class="ty">U8</span>; more, ok: <span class="ty">BOOL</span>;
224+<span class="kw">var</span> line, rest, key, val, w: []<span class="kw">const</span> <span class="ty">U8</span>; more, ok: <span class="ty">BOOL</span>;
225225 <span class="kw">begin</span>
226226 <span class="comment">(* line by line, then each line at its = *)</span>
227227 line, rest, more := Str.cut(Text, <span class="string">&quot;\n&quot;</span>);
@@ -286,7 +286,7 @@
286286 <span class="kw">import</span> Out, Str, Width;
287287
288288 <span class="kw">proc</span> main*;
289-<span class="kw">var</span> s: []<span class="ty">U8</span>; c: <span class="ty">I32</span>; i, size: <span class="ty">INT</span>; bytes: [<span class="number">4</span>]<span class="ty">U8</span>; k: <span class="ty">INT</span>;
289+<span class="kw">var</span> s: []<span class="kw">const</span> <span class="ty">U8</span>; c: <span class="ty">I32</span>; i, size: <span class="ty">INT</span>; bytes: [<span class="number">4</span>]<span class="ty">U8</span>; k: <span class="ty">INT</span>;
290290 <span class="kw">begin</span>
291291 s := <span class="string">&quot;caf\u{E9} \u{3BB}&quot;</span>;
292292 Out.int(<span class="ty">LEN</span>(s)); Out.str(<span class="string">&quot; bytes, &quot;</span>);

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

@@ -82,6 +82,7 @@
8282 <p>Two threads that each read a variable, add one and write it back can lose one of the additions. A plain <code>INC</code> on a variable shared by threads has no particular value afterwards; the report says so. <code>XADD</code> is the atomic add: it returns the old value and adds in one indivisible step.</p>
8383 <div class="file"><span>Race.raz</span></div><pre class="code"><code><span class="kw">module</span> Race;
8484 <span class="kw">import</span> Out, Thread;
85+<span class="kw">import</span> SYSTEM;
8586
8687 <span class="kw">var</span>
8788 safe: <span class="ty">INT</span>; <span class="comment">(* added to by XADD *)</span>
@@ -91,7 +92,7 @@
9192 <span class="kw">var</span> i: <span class="ty">INT</span>;
9293 <span class="kw">begin</span>
9394 <span class="kw">for</span> i := <span class="number">1</span> <span class="kw">to</span> <span class="number">100000</span> <span class="kw">do</span>
94- _ := <span class="ty">XADD</span>(<span class="ty">ADR</span>(safe), <span class="number">1</span>);
95+ _ := <span class="ty">XADD</span>(safe, <span class="number">1</span>);
9596 <span class="ty">INC</span>(plain); <span class="comment">(* unsafe: lost additions *)</span>
9697 <span class="kw">end</span>;
9798 <span class="kw">end</span>;
@@ -149,6 +150,7 @@
149150 <p>A thread's procedure takes one pointer argument. Pass a record's address to give each thread its own job and its own place to put the answer; then no one shares anything.</p>
150151 <div class="file"><span>Jobs.raz</span></div><pre class="code"><code><span class="kw">module</span> Jobs;
151152 <span class="kw">import</span> Out, Thread;
153+<span class="kw">import</span> SYSTEM;
152154
153155 <span class="kw">type</span> Job = <span class="kw">record</span> lo, hi: <span class="ty">INT</span>; sum: <span class="ty">INT</span>; <span class="kw">end</span>;
154156
@@ -165,7 +167,7 @@
165167 <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>
166168 jobs[i].lo := i * <span class="number">250000</span>;
167169 jobs[i].hi := (i + <span class="number">1</span>) * <span class="number">250000</span>;
168- t[i], err := Thread.spawn(work, ^<span class="ty">U8</span>(<span class="ty">ADR</span>(jobs[i])));
170+ t[i], err := Thread.spawn(work, ^<span class="ty">U8</span>(SYSTEM.<span class="ty">ADR</span>(jobs[i])));
169171 <span class="kw">if</span> err # <span class="ty">nil</span> <span class="kw">then</span> Out.str(<span class="ty">MSG</span>(err)); Out.ln; <span class="kw">return</span>; <span class="kw">end</span>;
170172 <span class="kw">end</span>;
171173 <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>

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

@@ -70,7 +70,7 @@
7070 <span class="kw">import</span> Out;
7171
7272 <span class="kw">proc</span> main*;
73-<span class="kw">var</span> i: <span class="ty">I32</span>; s: []<span class="ty">U8</span>;
73+<span class="kw">var</span> i: <span class="ty">I32</span>; s: []<span class="kw">const</span> <span class="ty">U8</span>;
7474 <span class="kw">begin</span>
7575 s := <span class="string">&quot;four&quot;</span>;
7676 i := <span class="number">3</span>;

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

@@ -62,7 +62,7 @@
6262 <span class="kw">end</span>;
6363 <span class="kw">end</span>;
6464
65-<span class="kw">proc</span> kind(c: <span class="ty">I32</span>): []<span class="ty">U8</span>;
65+<span class="kw">proc</span> kind(c: <span class="ty">I32</span>): []<span class="kw">const</span> <span class="ty">U8</span>;
6666 <span class="kw">begin</span>
6767 <span class="kw">match</span> c <span class="kw">of</span>
6868 | <span class="string">&#x27; &#x27;</span>, <span class="string">&#x27;\t&#x27;</span>, <span class="string">&#x27;\n&#x27;</span>: <span class="kw">return</span> <span class="string">&quot;space&quot;</span>;
@@ -72,7 +72,7 @@
7272 <span class="kw">end</span>;
7373 <span class="kw">end</span>;
7474
75-<span class="kw">proc</span> colour(name: []<span class="ty">U8</span>): Colour;
75+<span class="kw">proc</span> colour(name: []<span class="kw">const</span> <span class="ty">U8</span>): Colour;
7676 <span class="kw">begin</span>
7777 <span class="kw">match</span> name <span class="kw">of</span>
7878 | <span class="string">&quot;red&quot;</span>, <span class="string">&quot;rose&quot;</span>: <span class="kw">return</span> red;
@@ -112,7 +112,7 @@
112112
113113 <span class="kw">type</span> Day = (mon, tue, wed, thu, fri, sat, sun);
114114
115-<span class="kw">proc</span> name(d: Day): []<span class="ty">U8</span>;
115+<span class="kw">proc</span> name(d: Day): []<span class="kw">const</span> <span class="ty">U8</span>;
116116 <span class="kw">begin</span>
117117 <span class="kw">match</span> d <span class="kw">of</span>
118118 | mon: <span class="kw">return</span> <span class="string">&quot;Monday&quot;</span>;
@@ -193,7 +193,7 @@
193193 number(n: <span class="ty">INT</span>);
194194 plus;
195195 times;
196- word(s: []<span class="ty">U8</span>);
196+ word(s: []<span class="kw">const</span> <span class="ty">U8</span>);
197197 <span class="kw">end</span>;
198198
199199 <span class="kw">proc</span> describe(t: Token);
@@ -246,7 +246,7 @@
246246 <div class="file"><span>Classify.raz</span></div><pre class="code"><code><span class="kw">module</span> Classify;
247247 <span class="kw">import</span> Out;
248248
249-<span class="kw">proc</span> kind(c: <span class="ty">I32</span>): []<span class="ty">U8</span>;
249+<span class="kw">proc</span> kind(c: <span class="ty">I32</span>): []<span class="kw">const</span> <span class="ty">U8</span>;
250250 <span class="kw">begin</span>
251251 <span class="kw">match</span> c <span class="kw">of</span>
252252 | <span class="string">&#x27; &#x27;</span>, <span class="string">&#x27;\t&#x27;</span>, <span class="string">&#x27;\n&#x27;</span>: <span class="kw">return</span> <span class="string">&quot;space&quot;</span>;
@@ -257,7 +257,7 @@
257257 <span class="kw">end</span>;
258258
259259 <span class="kw">proc</span> main*;
260-<span class="kw">var</span> s: []<span class="ty">U8</span>; i: <span class="ty">INT</span>;
260+<span class="kw">var</span> s: []<span class="kw">const</span> <span class="ty">U8</span>; i: <span class="ty">INT</span>;
261261 <span class="kw">begin</span>
262262 s := <span class="string">&quot;x1 + _y&quot;</span>;
263263 <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="ty">LEN</span>(s) - <span class="number">1</span> <span class="kw">do</span>
@@ -281,7 +281,7 @@
281281
282282 <span class="kw">type</span> Op = (none, add, sub, show);
283283
284-<span class="kw">proc</span> op(word: []<span class="ty">U8</span>): Op;
284+<span class="kw">proc</span> op(word: []<span class="kw">const</span> <span class="ty">U8</span>): Op;
285285 <span class="kw">begin</span>
286286 <span class="kw">match</span> word <span class="kw">of</span>
287287 | <span class="string">&quot;add&quot;</span>, <span class="string">&quot;plus&quot;</span>, <span class="string">&quot;+&quot;</span>: <span class="kw">return</span> add;
@@ -292,7 +292,7 @@
292292 <span class="kw">end</span>;
293293
294294 <span class="kw">proc</span> main*;
295-<span class="kw">var</span> total: <span class="ty">INT</span>; words: [<span class="number">6</span>][]<span class="ty">U8</span>; i: <span class="ty">INT</span>;
295+<span class="kw">var</span> total: <span class="ty">INT</span>; words: [<span class="number">6</span>][]<span class="kw">const</span> <span class="ty">U8</span>; i: <span class="ty">INT</span>;
296296 <span class="kw">begin</span>
297297 words := [<span class="string">&quot;add&quot;</span>, <span class="string">&quot;plus&quot;</span>, <span class="string">&quot;sub&quot;</span>, <span class="string">&quot;show&quot;</span>, <span class="string">&quot;times&quot;</span>, <span class="string">&quot;-&quot;</span>];
298298 <span class="kw">for</span> i := <span class="number">0</span> <span class="kw">to</span> <span class="number">5</span> <span class="kw">do</span>