raz / commit f40a6b74

commit
f40a6b74f6ff75f0ab1808d8fc54230b9864818c
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
c1d8e857
PROPOSALS.md: var and keep at the call, IMMINENT - APPROVED

The owner approved marking at the call what a procedure does with an
argument: var x for a var parameter, keep x for a keep one. It joins
the library's parameters by var and ret/keep in headings as one thaw,
so programs break once. The section gives the table, the rules, the
examples, the compiler's messages, the measured noise, the
documentation the thaw must add, and the order of the work, the new
seed first.

patch · log

1 file changed, +505 -131

PROPOSALS.md modified +505 -131

@@ -4,101 +4,426 @@
44 None is built while the freeze holds; each waits for a later thaw, by
55 the owner's word, and goes into the report before any code.
66
7-## A `const` parameter
7+## IMMINENT - APPROVED: what a call does with its arguments, written at the call
88
9-`proc what(const x: Item): []U8;`: the callee may not change `x` (nor
10-take its address), so the caller may pass the address of a record or
11-array without a copy, and the program means the same.
9+**Status.** Approved by the owner, 2026-10-16, for the next thaw. It
10+is three parts, made in one thaw so that programs break once:
1211
13-Met in porting the compiler to raz, where the seed's `const x: TItem`
14-(about 130 bytes) became a value parameter, copied at every call.
15-Measured once the port built: not what stands out, the compiler's
16-profile is flat (`Scan.tokPos` is 9%).
12+1. **Marks at the call** (this section): an argument for a `var`
13+ parameter is written `var x`, and one for a `keep` parameter
14+ `keep x`.
15+2. **The library's parameters by `var`, not by pointer** (below): the
16+ `ADR` that stands at nearly every call to the library goes.
17+3. **`ret` and `keep`** (below): a heading says what a procedure does
18+ with what it is given, and the compiler holds the body to it.
1719
18-## A minus sign after an operator
20+Nothing is built until the owner starts the milestone. The report
21+changes first, as always; the documentation listed below comes with
22+it.
1923
20-`x & -8` and `n * -1` are errors: a unary minus belongs to the start
21-of a `SimpleExpr` (6), not to a factor, so after an operator it needs
22-parentheses, `x & (-8)`, or another spelling, `x & ~7`. This is the
23-report's grammar, Oberon's, and the compiler is right to refuse it;
24-but nearly every other language takes it, and the error ("expected an
25-expression, found -") surprises.
24+### The problem: a call hides what it does
2625
27-The proposal: a factor that follows an operator may be `"-" Factor`.
28-The minus at the start of a `SimpleExpr` stays as it is, applying to
29-the first term, so no program changes meaning: `-a & b` is still
30-`-(a & b)`, which isn't `(-a) & b`, nor is `-a shr n` the same as
31-`(-a) shr n`. Only text that is an error today, `x & -8`, gets one.
32-The cost is that a minus means two things by where it stands, which
33-the report would say in a line.
26+A heading says whether a parameter is changed, kept or lent; the call
27+doesn't. This line of `src/Scan.raz` reads a character:
3428
35-Met in tery's `aligned`, `(a + 7) & -8`.
29+```
30+v := utf8(cur.pos);
31+```
3632
37-## Dividing 128 bits by 64
33+It also moves `cur.pos` past it, since the heading is
34+`proc utf8(var pos: INT): INT`. Nothing at the call says so. Pascal
35+and Oberon leave it so; C#'s `ref`, Swift's `&` and Rust's `&mut`
36+write it at the call.
3837
39-`Math.mul` gives the 128-bit product of two `U64`s; there is nothing
40-to divide such a number back down. Forth's `UM/MOD` and `M*/`, and
41-any arithmetic on numbers wider than a word (a bignum, a fixed-point
42-product scaled back), need "this 128-bit number, given as its high
43-and low words, divided by a 64-bit one: the quotient and the
44-remainder". x86-64 does it in one instruction, `div`; a raz program
45-does it by a loop of 64 shifts and subtractions, or a code procedure
46-per target.
38+`keep` would hide its promise in the same way. Here the map keeps the
39+name it is given, which must outlive the call; a reader of the call
40+can't tell:
41+
42+```
43+_ := Names.put(table, name, 1);
44+```
45+
46+Today `ADR` stands in for a mark, badly. `STANDARDS.md` says `ADR` at a
47+call means the callee keeps what it is given, but the library takes
48+`^T` where it keeps nothing, so `ADR` stands at nearly every call
49+(`Buf.add(ADR(b), c)`), and says nothing.
50+
51+### What a procedure does with what it is given
52+
53+| Heading | The procedure | At the call | Example |
54+|---|---|---|---|
55+| no mark | reads it while the call runs, then lets go | nothing | `Str.eq(a, b)` |
56+| `var x` | may change the caller's variable | `var x` | `Buf.add(var b, c)` |
57+| `keep x` | may store it where it outlives the call | `keep x` | `Names.put(var table, keep name, 1)` |
58+| `ret x` | its results may point into it | nothing | `s := Str.trim(line[0..n])` |
59+| `^T` | keeps a pointer, or takes one for C, an allocator, or what may be `nil` | `ADR(x)`, or a pointer it holds | `Thread.spawn(work, ^U8(ADR(job)))` |
60+
61+`var` is in the language now; `keep` and `ret` come with part 3. The
62+table goes into the report (8), raz(7) and the tutorial as it is here.
63+
64+### The rules
4765
48-The proposal, in the library:
66+1. **`var` at the call.** An argument for a `var` parameter is written
67+ `var` and a variable: `var b`, `var p^`, `var a[i]`, `var r.f`.
68+ Leaving the mark out is an error; so is writing it for a parameter
69+ that isn't `var`. This holds for every call: of a procedure, of
70+ another module's, of a procedure value, of a generic module's
71+ instance.
72+2. **`keep` at the call.** An argument for a `keep` parameter is
73+ written `keep` and the argument, when the argument's type carries a
74+ slice (part 3, "Words"). For a type that carries none it isn't
75+ written, and writing it is an error: `Map(INT, INT).put(var m, 5, 7)`
76+ keeps nothing that could end. In a generic module's instance the
77+ argument's type is the instance's.
78+3. **Both.** An argument for a `keep var` parameter is written
79+ `keep var x`, in the heading's order.
80+4. **`ret`** is never written at the call. It is about the result,
81+ which the compiler checks where it is returned or stored (part 3);
82+ and the calls it is on, `Str.trim`, `cut` and `field`, are the most
83+ common ones.
84+5. **Built-in procedures** take no marks: `INC(v)`, `DEC(v)`,
85+ `NEW(p, a)`, `COPY(d, s)`. They are the report's own words, and
86+ their names say what they change. `extern` procedures have no `var`
87+ or `keep` parameters, and so take none.
88+6. **What follows `keep`.** `keep` is a mark, not a name, when the
89+ token after it is a name, a number, a string or character literal,
90+ or `{`. Otherwise it is the name `keep`: `keep[i]`, `keep(x)` and
91+ `keep^` are what they are today. An array literal is never the
92+ argument of a `keep` parameter, since it lives in the frame
93+ (part 3), so `keep [` is never needed.
94+7. **Procedure types** say `var` and `keep` as headings do (5.9), so a
95+ call through a procedure value is marked as a call by name.
4996
5097 ```
51-proc div*(hi, lo, d: U64): (U64, U64); (* hi, lo divided by d: the quotient and the remainder; hi < d *)
98+Arguments = "(" [ Argument { "," Argument } ] ")" .
99+Argument = [ "keep" ] [ "var" ] Expr .
52100 ```
53101
54-in `Math`, beside `mul`; `hi >= d`, whose quotient doesn't fit a word,
55-and `d = 0` stop the program as a division by zero does. It breaks no
56-program, so it could come with any release, not only a thaw.
102+Each rule looks at the call and the heading only, so a one-pass
103+compiler makes them as it reads the call: perhaps 40 lines, beside the
104+250 to 400 of part 3.
57105
58-Met in tery's `udivmod`, a loop of 64 rounds.
106+### Examples
59107
60-## Inlining a procedure that only returns
108+**Swapping**, from the tutorial (4):
61109
62-Not the language's but the compiler's: a call of a procedure whose
63-body is `return` and an expression, as `Grep.zeros` and
64-`Scan.tokPos` are, made in place of the call. One pass keeps no
65-tree to copy, but it can read source again, as `while` reads its
66-condition again (`saveScan`, `restoreScan`): the arguments go to
67-temporaries, each evaluated once and in order, a scope names the
68-parameters as them, and the expression is read again from where the
69-procedure's begins, in its own module's scope.
110+```
111+proc swap(var a, b: INT);
112+var t: INT;
113+begin
114+ t := a; a := b; b := t;
115+end;
70116
71-Measured by hand on the kernels: Grep with `zeros` and `lowest`
72-written in place is 20% faster, 0.88 times FPC `-O2`'s time where it
73-is 1.14 now. The compiler has 78 such procedures and the library 28;
74-the compiler's profile names the calls of small routines as what is
75-left (doc/compiler.md).
117+swap(var x, var y);
118+```
76119
77-Kept narrow: scalars passed by value, no recursion, no code
78-procedure (a square root has its own way), a procedure taken as a
79-value still called. What it costs: an inlined procedure is not in
80-the trap report's list of procedures, nor in gdb's backtrace, though
81-a failed check in it is still told at its line; 60 to 100 lines of
82-the budget; and, across modules, the imported module's
83-text kept to be read again, which is to be checked.
120+**A procedure that changes a place deep in a record**, from
121+`src/Scan.raz`:
84122
85-More cases, from sauce (`~/r/sauce`, RAZ-NOTES.md). Its SHA-256, with
86-the rotation a procedure, `return (x shr n) | (x shl (32 - n))`, and
87-each word read by `Str.be`, ran at 52 MB/s; with both written out in
88-the loop, 115 MB/s, where FPC `-O2` does about 200 and `check off`
89-adds only 9%. Most of the gap is calls. `ror` is the narrow case
90-above; `Str.be` is not, a loop over its bytes, and would want a
91-`Str.be32` of four bytes that only returns, or the loop unrolled where
92-it is. A rotate of the language's own, an operator or a built-in
93-procedure, would serve SHA, the other hashes and checksums without
94-any inlining, but it is a language change, for a thaw. On 100 MB of
95-new files `sauce commit` takes 1.2 s against the Pascal sauce's 0.7,
96-and `verify` 0.95 s against 0.48: the hashing.
123+```
124+v := utf8(var cur.pos); (* cur.pos moves past the character *)
125+bigMul(var num, 10, digit); (* num changed; 10 and digit read *)
126+bigSub(var num, d); (* num changed; d read *)
127+```
128+
129+**A buffer.** `examples/Sh.raz`'s `readWord`, today:
130+
131+```
132+Buf.init(ADR(b), ADR(arena.base));
133+while (at < LEN(s)) and not special(s[at]) do
134+ ...
135+ | 39: Buf.add(ADR(b), s[at]); INC(at);
136+ | '$': dollar(ADR(b));
137+ | else: Buf.add(ADR(b), s[at]); INC(at);
138+ end;
139+end;
140+text := Buf.bytes(ADR(b));
141+```
142+
143+and after the thaw:
144+
145+```
146+Buf.init(var b, ADR(arena.base)); (* b set up; the arena is an allocator: a pointer *)
147+while (at < LEN(s)) and not special(s[at]) do
148+ ...
149+ | 39: Buf.add(var b, s[at]); INC(at);
150+ | '$': dollar(var b);
151+ | else: Buf.add(var b, s[at]); INC(at);
152+ end;
153+end;
154+text := Buf.bytes(b); (* only read: no mark *)
155+```
156+
157+The marks are as many as the `ADR`s were, and shorter; now they are
158+true. `Buf.bytes(b)` has none, and the one pointer left,
159+`ADR(arena.base)`, stands out as what it is.
160+
161+**Passing on what the procedure was given.** A `var` parameter is a
162+variable, and is marked like any other:
163+
164+```
165+proc dollar(var b: Buf.Buf);
166+begin
167+ ...
168+ Buf.str(var b, Sys.env(s[start..at]));
169+end;
170+```
171+
172+**What the program holds by pointer** is given as `p^`:
173+
174+```
175+proc emit(w: ^Writer; c: U8);
176+begin
177+ Buf.add(var w^.out, c);
178+end;
179+```
180+
181+**A map that keeps its keys:**
182+
183+```
184+(* import Names := Map([]U8, INT, Str.hash, Str.eq);
185+ table: Names.Map and heap: Heap's allocator, module variables *)
186+proc count(name: []U8);
187+begin
188+ _ := Names.put(var table, keep Str.copy(heap, name), 1); (* a copy, on the heap *)
189+ _ := Names.put(var table, keep "main", 1); (* a literal never ends *)
190+end;
191+
192+proc lookup(name: []U8): INT;
193+var n: INT; found: BOOL;
194+begin
195+ n, found := Names.get(table, name); (* only read: no mark *)
196+ ...
197+end;
198+```
199+
200+**A map that keeps nothing that can end:**
201+
202+```
203+(* import Counts := Map(INT, INT, hashInt, eqInt) *)
204+_ := Counts.put(var counts, 5, 7); (* INTs carry no slice: no keep *)
205+```
206+
207+**A list of names a procedure keeps, passed on.** The parameter is
208+`keep` in turn, so the promise travels up:
209+
210+```
211+(* import Seen := List([]U8) *)
212+proc remember(var l: Seen.List; keep name: []U8);
213+begin
214+ _ := Seen.push(var l, keep name);
215+end;
216+
217+remember(var seen, keep "start");
218+```
219+
220+**`keep var`: a reader that keeps a slice of the caller's buffer.**
221+
222+```
223+proc start(var r: Reader; keep var page: [4096]U8);
224+begin
225+ r.rest := page[0..0];
226+ r.page := page[0..LEN(page)];
227+end;
228+
229+var page: [4096]U8; (* a module variable *)
230+...
231+start(var rd, keep var page);
232+```
233+
234+**A procedure value.**
235+
236+```
237+type Step = proc(var s: State): BOOL;
238+
239+proc run(step: Step; var s: State);
240+begin
241+ while step(var s) do end;
242+end;
243+```
244+
245+**What stays a pointer**, and so keeps `ADR`:
246+
247+```
248+NEW(node, ADR(arena.base)); (* an allocator *)
249+t, err := Thread.spawn(work, ^U8(ADR(job))); (* kept by the thread *)
250+XNextEvent(dpy, ADR(ev)); (* C *)
251+p := Sys.mmap(size); (* bytes, not a variable *)
252+```
253+
254+**`ret`, unmarked:**
255+
256+```
257+s := Str.trim(line[0..n]); (* s points into line *)
258+key, rest, ok := Str.cut(s, "="); (* both point into s *)
259+```
97260
98-## Marking what a procedure does with what it is given: `ret` and `keep`
261+### What the compiler says
99262
100-### The hole
263+Each message names the mending, at the argument:
101264
265+```
266+Sh.raz:94:21: b: write `var b`: Buf.add may change it
267+Sh.raz:115:23: b: Buf.bytes only reads it: no `var`
268+Count.raz:12:27: name: write `keep name`: Names.put keeps it
269+Count.raz:14:31: 5: INT carries no slice: no `keep`
270+Count.raz:9:27: key[0..4]: kept by Names.put, but it points into this procedure's frame
271+Sh.raz:88:14: ADR(b): Buf.init takes `var b`, not a pointer
272+Sh.raz:151:14: p: Buf.add takes `var p^`, not a pointer
273+```
274+
275+The last two are part 2's mending, `ADR(x)` to `var x` and `p` to
276+`var p^`.
277+
278+### How much noise
279+
280+Measured by a search of `src/`, `lib/`, `tools/` and `examples/`, about
281+23,300 lines of code (rough: a search, not the compiler):
282+
283+- about 630 `ADR`s; most are at calls to the library that part 2 turns
284+ into `var x`: the same count, shorter, and true;
285+- about 580 calls of our own procedures with `var` parameters, unmarked
286+ today: each gains `var`, about one line in 40;
287+- `keep`: a few dozen calls (`Map.put`, `List.push`, `Opt.init`, and
288+ procedures that pass a kept slice on).
289+
290+So most of the marks stand where an `ADR` stands today. What is new is
291+the 580, and those are the calls, like `utf8(cur.pos)`, that change a
292+variable with nothing to say so.
293+
294+### `ADR` afterwards
295+
296+`ADR` stays, for the pointers `STANDARDS.md` lists: an allocator, what
297+may be `nil`, what the program holds by pointer already, C and memory
298+that is bytes, and a pointer a procedure keeps (`Thread.spawn`'s `arg`).
299+The marks of part 3 don't follow a pointer, so `ADR` at a call still
300+says "kept, or not a variable", unchecked, as now; with the library on
301+`var`, it is rare enough in ordinary code to be read so.
302+
303+### What the documentation gains
304+
305+The thaw is not done until each of these is:
306+
307+- **The report, `doc/raz.md`.** 8: the grammar above; "Parameters"
308+ becomes "Parameters and arguments", with the table, rules 1 to 7, and
309+ the swap and map examples; `keep` and `ret` from part 3. 5.9:
310+ procedure types with marks. 7.3: `ret`. 10: the promise, whose
311+ exceptions shrink to `ADR`, C, code procedures and `check off`.
312+ 1, "A first look": its example marks its calls. Every example in the
313+ report is marked. About two pages; the report stays under 40 (31
314+ now), counted by `make pdf`.
315+- **`doc/report-history.md`**: the version's entry, with the mending:
316+ `ADR(x)` to `var x`, `p` to `var p^`, `var` and `keep` at each call
317+ the compiler names.
318+- **raz(7)**, the language at a glance: under PROCEDURES, the table, as
319+ a list, and one line for each of `var`, `keep` and `ret` at the call;
320+ THE GRAMMAR IN BRIEF gains `Argument`; BUILT-IN PROCEDURES says they
321+ take no marks.
322+- **The tutorial, raz-tutorial(7).** Section 4 marks `swap(var x, var y)`
323+ and says why. A new section after 6, "WHAT A CALL DOES WITH ITS
324+ ARGUMENTS": the table; a whole program, with its output, that counts
325+ the words of its input in a `Map([]U8, INT)` with a `Buf`, using
326+ `var`, `keep` and `ret`, each said in a sentence where it stands;
327+ then "What the compiler stops", the four mistakes of the messages
328+ above, each with its message. Section 16, "FROM PASCAL AND C TO RAZ",
329+ gains: Pascal's `var` is unmarked at the call, raz's is marked, as C#'s
330+ `ref` is; C's `&x` becomes `var x` when the callee only changes it,
331+ and stays `ADR(x)` when it keeps it. Every other example in the
332+ tutorial is marked. Each runs under `make check`, as now.
333+- **Volume 2, `doc/raz-lib.md`, and raz-lib(3)**: every signature of
334+ part 2, each module's example marked, and a note in each changed
335+ module of what changed.
336+- **`STANDARDS.md`**: "Parameters: a value, `var`, or a pointer" says
337+ that `ADR` no longer marks what is kept, `keep` does; "The library
338+ until a thaw" goes.
339+- **`doc/compiler.md`**: where the marks are read and checked, the
340+ checks of part 3 and the set each value carries, with the code
341+ before and after, and the table of lines.
342+- **`README.md`**: its examples marked.
343+- **Tests.** `conform/`: a program for each rule, run and error;
344+ `test/err` and `test/msg`: each message above; `test/pretty`: the
345+ marks formatted; the vim and emacs support indent and colour `var`
346+ and `keep` at a call.
347+- **`sbcl/`**, the reference compiler from the report alone, follows
348+ the report.
349+
350+### What it breaks, and the order of the work
351+
352+Every call to a procedure with a `var` or `keep` parameter, and every
353+call to the library's procedures of part 2: in `src/`, `lib/`, the
354+tools, the examples, the tests, and rmacs, ravite, rat, rlX11, rloc,
355+rpg and rsqlite, each when it moves its pin. The mending is mechanical,
356+and the compiler names it at each place.
357+
358+**The seed first.** The seed, frozen, can't read a marked call, and it
359+builds `src/` and reads `Sys` and `CAbi`. So this thaw needs the new
360+seed a release makes (CLAUDE.md, "The compiler and the seed") before
361+`src/` and that part of the library can be marked: the owner's call,
362+and the first step.
363+
364+Then, one commit each:
365+
366+1. The report, `report-history.md`, raz(7): for review.
367+2. The compiler reads the marks and checks rules 1 to 7.
368+3. The library's signatures (part 2), and every caller in this
369+ repository mended.
370+4. `ret` and `keep` in headings and their checks (part 3), and the
371+ library marked (`Str.trim`, `Map.put`, `List.push`, `Opt.init`, ...).
372+5. The tutorial, volume 2, raz-lib(3), `STANDARDS.md`,
373+ `doc/compiler.md`, `README.md`.
374+6. The tests: `conform/`, `test/err`, `test/msg`, `test/pretty`, the
375+ editors.
376+
377+The examples of parts 2 and 3 are written as the whole thaw makes
378+them, with the marks of part 1.
379+
380+### Part 2: the library's parameters by `var`, not by pointer
381+
382+Most of the library's procedures take what they work on as `^T`:
383+`Buf.str(ADR(b), s)`, `Vec.push(ADR(v), x)`, `Lock.take(ADR(l))`. None
384+keeps it past the call; each reads or changes it and returns. That is
385+what `var` is for (8), and with it the call is `Buf.str(var b, s)`. As it
386+is, `ADR` stands at nearly every call a program makes to the library,
387+and says nothing: a reader can't tell the few places where a pointer
388+is kept from the many where it isn't. `STANDARDS.md` says how our own
389+code chooses; this would bring the library to it.
390+
391+The proposal, procedure by procedure, from reading each one:
392+
393+- **To `var`** (it changes what it is given): `Arena.init`, `reset`,
394+ `done`; `Buf.init`, `room`, `add`, `str`, `spare`, `cut`, `reset`,
395+ `done`, and the `Buf` that `Files.load`, `Files.read`, `Fmt.write`,
396+ `Str.join` and `Str.replace` write into; every procedure of `List`;
397+ `Lock.take`, `give`; `Map.init`, `put`, `remove`, `done`; every
398+ procedure of `Opt` (a `Parser` is many words, so even those that
399+ only read it); `Sys.openDir`, `nextEntry`, `closeDir`;
400+ `Thread.lock`, `unlock`; `Vec.init`, `room`, `push`, `pop`, `add`,
401+ `tryPush`, `tryAdd`, `resize`, `reset`, `done`.
402+- **To a value** (it only reads a record of a few words): `Buf.bytes`,
403+ `Vec.all`, `Map.get`, `Map.next`, `Sys.entryKind`.
404+- **Staying pointers**: an allocator, `^Mem.Allocator`, everywhere
405+ (`NEW` takes one, and `^Arena(a)` needs one); `Mem.pages`, `unmap`,
406+ `Sys.mmap`, `munmap`, `mapFile`, which are bytes, not variables;
407+ `Thread.spawn`'s `arg`, which is kept, and the `^Thread` it gives,
408+ which `join` and `signal` take as the program holds it; `Rt.fail`,
409+ which the compiler calls.
410+
411+What it costs: it breaks every program that calls these, `src/`,
412+rmacs, ravite, the tools and the library's own tests among them. The
413+mending is mechanical, `ADR(x)` to `var x` at each such call, and `p`
414+to `var p^` where a program holds the thing by pointer, and the
415+compiler's message for a `^T` given to a `var` names it. The seed
416+knows `var` parameters but not the marks of part 1, so with them
417+`src/` and the library it imports wait for the new seed. Alone, this
418+part changes volume 2 and `raz-lib.3`, not the report.
419+
420+Met in rpg and rsqlite, whose `Query`, `Result` and `Stmt` went from
421+`^T` to `var` and to values, and whose own calls to `Buf` couldn't.
422+
423+### Part 3: marking what a procedure does with what it is given: `ret` and `keep`
424+
425+#### The hole
426+
102427 A procedure's locals, its value parameters and the array literals it
103428 passes as slices live in its frame, which is gone once it returns. A
104429 slice that still points there afterwards reads whatever the next call
@@ -135,7 +460,7 @@
135460 var buf: [16]U8;
136461 begin
137462 ...
138- _ := Names.put(ADR(table), buf[0..n], 1); (* the map, on the heap, keeps a slice of buf *)
463+ _ := Names.put(var table, keep buf[0..n], 1); (* the map, on the heap, keeps a slice of buf *)
139464 end;
140465 ```
141466
@@ -145,7 +470,7 @@
145470 heading without seeing the body. Each check needs only the procedure
146471 it is in, so a one-pass compiler can make it.
147472
148-### Words
473+#### Words
149474
150475 **Carrying a slice.** A type carries a slice if it is a slice, or an
151476 array, record or variant with one in it: `[]U8`, `[4][]U8`,
@@ -180,7 +505,7 @@
180505 `var` parameter of this procedure. A value stored there, or returned,
181506 leaves the frame.
182507
183-### No mark
508+#### No mark
184509
185510 A parameter without a mark is only used while the call runs. The
186511 procedure may read it, copy its bytes (`COPY`, `Buf.str`), keep it in
@@ -189,7 +514,7 @@
189514 the call. This is what most procedures do: `Str.eq`, `Str.find`,
190515 `Buf.str`, `Sys.write`, `Fmt.print`, `Map.get`'s key.
191516
192-### `ret`
517+#### `ret`
193518
194519 ```
195520 proc trim(ret s: []U8): []U8;
@@ -235,7 +560,7 @@
235560 and the caller takes the result as pointing where all of their
236561 arguments do.
237562
238-### `keep`
563+#### `keep`
239564
240565 ```
241566 proc put(m: ^Map; keep k: K; keep v: V): BOOL;
@@ -253,10 +578,10 @@
253578 proc count(name: []U8);
254579 var key: [32]U8;
255580 begin
256- _ := Names.put(ADR(table), name, 1); (* an error: name isn't keep here *)
257- _ := Names.put(ADR(table), key[0..4], 1); (* an error: frame-bound to a keep *)
258- _ := Names.put(ADR(table), "main", 1); (* fine: a literal never ends *)
259- _ := Names.put(ADR(table), Str.copy(ADR(heap), name), 1); (* fine: copied to the heap *)
581+ _ := Names.put(var table, keep name, 1); (* an error: name isn't keep here *)
582+ _ := Names.put(var table, keep key[0..4], 1); (* an error: frame-bound to a keep *)
583+ _ := Names.put(var table, keep "main", 1); (* fine: a literal never ends *)
584+ _ := Names.put(var table, keep Str.copy(heap, name), 1); (* fine: copied to the heap *)
260585 end;
261586 ```
262587
@@ -269,7 +594,7 @@
269594 `s := s[1..LEN(s)]` with `var s: []U8` needs no `keep`. The caller's
270595 variable is no worse off.
271596
272-### Both, and the order
597+#### Both, and the order
273598
274599 `ret keep v: T` is both. The marks come before `var`, and each
275600 mark covers the group it starts, so `(ret s, t: []U8)` marks both;
@@ -278,7 +603,7 @@
278603 `static` is a mark only in `link static;`. `proc f(ret: INT)` is a
279604 parameter named `ret`.
280605
281-### What the compiler checks, all of it
606+#### What the compiler checks, all of it
282607
283608 In each procedure:
284609
@@ -295,7 +620,7 @@
295620 Checks 1 to 3 on a local variable's value wait for the procedure's
296621 `end`, when everything assigned to it is known.
297622
298-### Procedure values, generic modules, and the rest
623+#### Procedure values, generic modules, and the rest
299624
300625 **Procedure types** carry the marks, `proc(ret s: []U8): []U8`. A
301626 procedure may be given where its type has the same marks or more,
@@ -317,7 +642,7 @@
317642 a slice reached through such a pointer, and a thread given a pointer
318643 to its spawner's locals.
319644
320-### False alarms
645+#### False alarms
321646
322647 The rules are sure, not exact: they reject some programs that were
323648 right.
@@ -326,7 +651,7 @@
326651 `examples/Sh.raz` does
327652
328653 ```
329- Opt.init(ADR(p), "Sh", [Opt.usage(...), Opt.option(...)]);
654+ Opt.init(var p, "Sh", [Opt.usage(...), Opt.option(...)]);
330655 ```
331656
332657 `Opt.init` keeps its table in `p^`, so it needs `keep table`, and
@@ -340,7 +665,7 @@
340665 - **Several `ret` parameters.** A result is taken to point at all of
341666 them, though it may point at one.
342667
343-### What it breaks
668+#### What it breaks
344669
345670 Every procedure, in the library or a program, that returns or keeps
346671 what a parameter points at stops compiling until it is marked: a
@@ -352,7 +677,7 @@
352677 `Fmt.write` (they copy), `Map.get`. In sauce, `Svec.push` and `add`
353678 (`keep`), and what returns a slice of its argument.
354679
355-### What it costs
680+#### What it costs
356681
357682 Reports: 5.9 (procedure types), 7.3, 8 (parameters), 10 (the promise,
358683 whose exceptions shrink to `ADR`, C, code procedures and `check
@@ -368,7 +693,98 @@
368693 `ret` does. D's experience is that the corners (fields, generics,
369694 procedure values) take years; the narrow form here leaves record
370695 lifetimes out, and so the first false alarm above, on purpose.
696+
697+## A `const` parameter
371698
699+`proc what(const x: Item): []U8;`: the callee may not change `x` (nor
700+take its address), so the caller may pass the address of a record or
701+array without a copy, and the program means the same.
702+
703+Met in porting the compiler to raz, where the seed's `const x: TItem`
704+(about 130 bytes) became a value parameter, copied at every call.
705+Measured once the port built: not what stands out, the compiler's
706+profile is flat (`Scan.tokPos` is 9%).
707+
708+## A minus sign after an operator
709+
710+`x & -8` and `n * -1` are errors: a unary minus belongs to the start
711+of a `SimpleExpr` (6), not to a factor, so after an operator it needs
712+parentheses, `x & (-8)`, or another spelling, `x & ~7`. This is the
713+report's grammar, Oberon's, and the compiler is right to refuse it;
714+but nearly every other language takes it, and the error ("expected an
715+expression, found -") surprises.
716+
717+The proposal: a factor that follows an operator may be `"-" Factor`.
718+The minus at the start of a `SimpleExpr` stays as it is, applying to
719+the first term, so no program changes meaning: `-a & b` is still
720+`-(a & b)`, which isn't `(-a) & b`, nor is `-a shr n` the same as
721+`(-a) shr n`. Only text that is an error today, `x & -8`, gets one.
722+The cost is that a minus means two things by where it stands, which
723+the report would say in a line.
724+
725+Met in tery's `aligned`, `(a + 7) & -8`.
726+
727+## Dividing 128 bits by 64
728+
729+`Math.mul` gives the 128-bit product of two `U64`s; there is nothing
730+to divide such a number back down. Forth's `UM/MOD` and `M*/`, and
731+any arithmetic on numbers wider than a word (a bignum, a fixed-point
732+product scaled back), need "this 128-bit number, given as its high
733+and low words, divided by a 64-bit one: the quotient and the
734+remainder". x86-64 does it in one instruction, `div`; a raz program
735+does it by a loop of 64 shifts and subtractions, or a code procedure
736+per target.
737+
738+The proposal, in the library:
739+
740+```
741+proc div*(hi, lo, d: U64): (U64, U64); (* hi, lo divided by d: the quotient and the remainder; hi < d *)
742+```
743+
744+in `Math`, beside `mul`; `hi >= d`, whose quotient doesn't fit a word,
745+and `d = 0` stop the program as a division by zero does. It breaks no
746+program, so it could come with any release, not only a thaw.
747+
748+Met in tery's `udivmod`, a loop of 64 rounds.
749+
750+## Inlining a procedure that only returns
751+
752+Not the language's but the compiler's: a call of a procedure whose
753+body is `return` and an expression, as `Grep.zeros` and
754+`Scan.tokPos` are, made in place of the call. One pass keeps no
755+tree to copy, but it can read source again, as `while` reads its
756+condition again (`saveScan`, `restoreScan`): the arguments go to
757+temporaries, each evaluated once and in order, a scope names the
758+parameters as them, and the expression is read again from where the
759+procedure's begins, in its own module's scope.
760+
761+Measured by hand on the kernels: Grep with `zeros` and `lowest`
762+written in place is 20% faster, 0.88 times FPC `-O2`'s time where it
763+is 1.14 now. The compiler has 78 such procedures and the library 28;
764+the compiler's profile names the calls of small routines as what is
765+left (doc/compiler.md).
766+
767+Kept narrow: scalars passed by value, no recursion, no code
768+procedure (a square root has its own way), a procedure taken as a
769+value still called. What it costs: an inlined procedure is not in
770+the trap report's list of procedures, nor in gdb's backtrace, though
771+a failed check in it is still told at its line; 60 to 100 lines of
772+the budget; and, across modules, the imported module's
773+text kept to be read again, which is to be checked.
774+
775+More cases, from sauce (`~/r/sauce`, RAZ-NOTES.md). Its SHA-256, with
776+the rotation a procedure, `return (x shr n) | (x shl (32 - n))`, and
777+each word read by `Str.be`, ran at 52 MB/s; with both written out in
778+the loop, 115 MB/s, where FPC `-O2` does about 200 and `check off`
779+adds only 9%. Most of the gap is calls. `ror` is the narrow case
780+above; `Str.be` is not, a loop over its bytes, and would want a
781+`Str.be32` of four bytes that only returns, or the loop unrolled where
782+it is. A rotate of the language's own, an operator or a built-in
783+procedure, would serve SHA, the other hashes and checksums without
784+any inlining, but it is a language change, for a thaw. On 100 MB of
785+new files `sauce commit` takes 1.2 s against the Pascal sauce's 0.7,
786+and `verify` 0.95 s against 0.48: the hashing.
787+
372788 ## Less that is undefined
373789
374790 Section 10 lists what isn't defined: a stale or made-up pointer,
@@ -550,45 +966,3 @@
550966
551967 Met in mosaico's `Mconfig`, where `MenuAction.run` and a status block's
552968 kind `run` met; the block's kind was renamed `command`.
553-
554-## The library's parameters by `var`, not by pointer
555-
556-Most of the library's procedures take what they work on as `^T`:
557-`Buf.str(ADR(b), s)`, `Vec.push(ADR(v), x)`, `Lock.take(ADR(l))`. None
558-keeps it past the call; each reads or changes it and returns. That is
559-what `var` is for (8), and with it the call is `Buf.str(b, s)`. As it
560-is, `ADR` stands at nearly every call a program makes to the library,
561-and says nothing: a reader can't tell the few places where a pointer
562-is kept from the many where it isn't. `STANDARDS.md` says how our own
563-code chooses; this would bring the library to it.
564-
565-The proposal, procedure by procedure, from reading each one:
566-
567-- **To `var`** (it changes what it is given): `Arena.init`, `reset`,
568- `done`; `Buf.init`, `room`, `add`, `str`, `spare`, `cut`, `reset`,
569- `done`, and the `Buf` that `Files.load`, `Files.read`, `Fmt.write`,
570- `Str.join` and `Str.replace` write into; every procedure of `List`;
571- `Lock.take`, `give`; `Map.init`, `put`, `remove`, `done`; every
572- procedure of `Opt` (a `Parser` is many words, so even those that
573- only read it); `Sys.openDir`, `nextEntry`, `closeDir`;
574- `Thread.lock`, `unlock`; `Vec.init`, `room`, `push`, `pop`, `add`,
575- `tryPush`, `tryAdd`, `resize`, `reset`, `done`.
576-- **To a value** (it only reads a record of a few words): `Buf.bytes`,
577- `Vec.all`, `Map.get`, `Map.next`, `Sys.entryKind`.
578-- **Staying pointers**: an allocator, `^Mem.Allocator`, everywhere
579- (`NEW` takes one, and `^Arena(a)` needs one); `Mem.pages`, `unmap`,
580- `Sys.mmap`, `munmap`, `mapFile`, which are bytes, not variables;
581- `Thread.spawn`'s `arg`, which is kept, and the `^Thread` it gives,
582- which `join` and `signal` take as the program holds it; `Rt.fail`,
583- which the compiler calls.
584-
585-What it costs: it breaks every program that calls these, `src/`,
586-rmacs, ravite, the tools and the library's own tests among them. The
587-mending is mechanical, `ADR(x)` to `x` at each such call, and `p` to
588-`p^` where a program holds the thing by pointer, and the compiler's
589-message for a `^T` given to a `var` can name it. The seed knows `var`
590-parameters, so `src/` and the library it imports can take them now.
591-The report doesn't change; volume 2 and `raz-lib.3` do.
592-
593-Met in rpg and rsqlite, whose `Query`, `Result` and `Stmt` went from
594-`^T` to `var` and to values, and whose own calls to `Buf` couldn't.