From f40a6b74f6ff75f0ab1808d8fc54230b9864818c Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Fri, 16 Oct 2026 23:17:56 +0000 Subject: [PATCH] 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. --- diff --git a/PROPOSALS.md b/PROPOSALS.md index 5b10775..b10e3b3 100644 --- a/PROPOSALS.md +++ b/PROPOSALS.md @@ -4,101 +4,426 @@ None is built while the freeze holds; each waits for a later thaw, by the owner's word, and goes into the report before any code. -## A `const` parameter +## IMMINENT - APPROVED: what a call does with its arguments, written at the call -`proc what(const x: Item): []U8;`: the callee may not change `x` (nor -take its address), so the caller may pass the address of a record or -array without a copy, and the program means the same. +**Status.** Approved by the owner, 2026-10-16, for the next thaw. It +is three parts, made in one thaw so that programs break once: -Met in porting the compiler to raz, where the seed's `const x: TItem` -(about 130 bytes) became a value parameter, copied at every call. -Measured once the port built: not what stands out, the compiler's -profile is flat (`Scan.tokPos` is 9%). +1. **Marks at the call** (this section): an argument for a `var` + parameter is written `var x`, and one for a `keep` parameter + `keep x`. +2. **The library's parameters by `var`, not by pointer** (below): the + `ADR` that stands at nearly every call to the library goes. +3. **`ret` and `keep`** (below): a heading says what a procedure does + with what it is given, and the compiler holds the body to it. -## A minus sign after an operator +Nothing is built until the owner starts the milestone. The report +changes first, as always; the documentation listed below comes with +it. -`x & -8` and `n * -1` are errors: a unary minus belongs to the start -of a `SimpleExpr` (6), not to a factor, so after an operator it needs -parentheses, `x & (-8)`, or another spelling, `x & ~7`. This is the -report's grammar, Oberon's, and the compiler is right to refuse it; -but nearly every other language takes it, and the error ("expected an -expression, found -") surprises. +### The problem: a call hides what it does -The proposal: a factor that follows an operator may be `"-" Factor`. -The minus at the start of a `SimpleExpr` stays as it is, applying to -the first term, so no program changes meaning: `-a & b` is still -`-(a & b)`, which isn't `(-a) & b`, nor is `-a shr n` the same as -`(-a) shr n`. Only text that is an error today, `x & -8`, gets one. -The cost is that a minus means two things by where it stands, which -the report would say in a line. +A heading says whether a parameter is changed, kept or lent; the call +doesn't. This line of `src/Scan.raz` reads a character: -Met in tery's `aligned`, `(a + 7) & -8`. +``` +v := utf8(cur.pos); +``` -## Dividing 128 bits by 64 +It also moves `cur.pos` past it, since the heading is +`proc utf8(var pos: INT): INT`. Nothing at the call says so. Pascal +and Oberon leave it so; C#'s `ref`, Swift's `&` and Rust's `&mut` +write it at the call. -`Math.mul` gives the 128-bit product of two `U64`s; there is nothing -to divide such a number back down. Forth's `UM/MOD` and `M*/`, and -any arithmetic on numbers wider than a word (a bignum, a fixed-point -product scaled back), need "this 128-bit number, given as its high -and low words, divided by a 64-bit one: the quotient and the -remainder". x86-64 does it in one instruction, `div`; a raz program -does it by a loop of 64 shifts and subtractions, or a code procedure -per target. +`keep` would hide its promise in the same way. Here the map keeps the +name it is given, which must outlive the call; a reader of the call +can't tell: + +``` +_ := Names.put(table, name, 1); +``` + +Today `ADR` stands in for a mark, badly. `STANDARDS.md` says `ADR` at a +call means the callee keeps what it is given, but the library takes +`^T` where it keeps nothing, so `ADR` stands at nearly every call +(`Buf.add(ADR(b), c)`), and says nothing. + +### What a procedure does with what it is given + +| Heading | The procedure | At the call | Example | +|---|---|---|---| +| no mark | reads it while the call runs, then lets go | nothing | `Str.eq(a, b)` | +| `var x` | may change the caller's variable | `var x` | `Buf.add(var b, c)` | +| `keep x` | may store it where it outlives the call | `keep x` | `Names.put(var table, keep name, 1)` | +| `ret x` | its results may point into it | nothing | `s := Str.trim(line[0..n])` | +| `^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)))` | + +`var` is in the language now; `keep` and `ret` come with part 3. The +table goes into the report (8), raz(7) and the tutorial as it is here. + +### The rules -The proposal, in the library: +1. **`var` at the call.** An argument for a `var` parameter is written + `var` and a variable: `var b`, `var p^`, `var a[i]`, `var r.f`. + Leaving the mark out is an error; so is writing it for a parameter + that isn't `var`. This holds for every call: of a procedure, of + another module's, of a procedure value, of a generic module's + instance. +2. **`keep` at the call.** An argument for a `keep` parameter is + written `keep` and the argument, when the argument's type carries a + slice (part 3, "Words"). For a type that carries none it isn't + written, and writing it is an error: `Map(INT, INT).put(var m, 5, 7)` + keeps nothing that could end. In a generic module's instance the + argument's type is the instance's. +3. **Both.** An argument for a `keep var` parameter is written + `keep var x`, in the heading's order. +4. **`ret`** is never written at the call. It is about the result, + which the compiler checks where it is returned or stored (part 3); + and the calls it is on, `Str.trim`, `cut` and `field`, are the most + common ones. +5. **Built-in procedures** take no marks: `INC(v)`, `DEC(v)`, + `NEW(p, a)`, `COPY(d, s)`. They are the report's own words, and + their names say what they change. `extern` procedures have no `var` + or `keep` parameters, and so take none. +6. **What follows `keep`.** `keep` is a mark, not a name, when the + token after it is a name, a number, a string or character literal, + or `{`. Otherwise it is the name `keep`: `keep[i]`, `keep(x)` and + `keep^` are what they are today. An array literal is never the + argument of a `keep` parameter, since it lives in the frame + (part 3), so `keep [` is never needed. +7. **Procedure types** say `var` and `keep` as headings do (5.9), so a + call through a procedure value is marked as a call by name. ``` -proc div*(hi, lo, d: U64): (U64, U64); (* hi, lo divided by d: the quotient and the remainder; hi < d *) +Arguments = "(" [ Argument { "," Argument } ] ")" . +Argument = [ "keep" ] [ "var" ] Expr . ``` -in `Math`, beside `mul`; `hi >= d`, whose quotient doesn't fit a word, -and `d = 0` stop the program as a division by zero does. It breaks no -program, so it could come with any release, not only a thaw. +Each rule looks at the call and the heading only, so a one-pass +compiler makes them as it reads the call: perhaps 40 lines, beside the +250 to 400 of part 3. -Met in tery's `udivmod`, a loop of 64 rounds. +### Examples -## Inlining a procedure that only returns +**Swapping**, from the tutorial (4): -Not the language's but the compiler's: a call of a procedure whose -body is `return` and an expression, as `Grep.zeros` and -`Scan.tokPos` are, made in place of the call. One pass keeps no -tree to copy, but it can read source again, as `while` reads its -condition again (`saveScan`, `restoreScan`): the arguments go to -temporaries, each evaluated once and in order, a scope names the -parameters as them, and the expression is read again from where the -procedure's begins, in its own module's scope. +``` +proc swap(var a, b: INT); +var t: INT; +begin + t := a; a := b; b := t; +end; -Measured by hand on the kernels: Grep with `zeros` and `lowest` -written in place is 20% faster, 0.88 times FPC `-O2`'s time where it -is 1.14 now. The compiler has 78 such procedures and the library 28; -the compiler's profile names the calls of small routines as what is -left (doc/compiler.md). +swap(var x, var y); +``` -Kept narrow: scalars passed by value, no recursion, no code -procedure (a square root has its own way), a procedure taken as a -value still called. What it costs: an inlined procedure is not in -the trap report's list of procedures, nor in gdb's backtrace, though -a failed check in it is still told at its line; 60 to 100 lines of -the budget; and, across modules, the imported module's -text kept to be read again, which is to be checked. +**A procedure that changes a place deep in a record**, from +`src/Scan.raz`: -More cases, from sauce (`~/r/sauce`, RAZ-NOTES.md). Its SHA-256, with -the rotation a procedure, `return (x shr n) | (x shl (32 - n))`, and -each word read by `Str.be`, ran at 52 MB/s; with both written out in -the loop, 115 MB/s, where FPC `-O2` does about 200 and `check off` -adds only 9%. Most of the gap is calls. `ror` is the narrow case -above; `Str.be` is not, a loop over its bytes, and would want a -`Str.be32` of four bytes that only returns, or the loop unrolled where -it is. A rotate of the language's own, an operator or a built-in -procedure, would serve SHA, the other hashes and checksums without -any inlining, but it is a language change, for a thaw. On 100 MB of -new files `sauce commit` takes 1.2 s against the Pascal sauce's 0.7, -and `verify` 0.95 s against 0.48: the hashing. +``` +v := utf8(var cur.pos); (* cur.pos moves past the character *) +bigMul(var num, 10, digit); (* num changed; 10 and digit read *) +bigSub(var num, d); (* num changed; d read *) +``` + +**A buffer.** `examples/Sh.raz`'s `readWord`, today: + +``` +Buf.init(ADR(b), ADR(arena.base)); +while (at < LEN(s)) and not special(s[at]) do + ... + | 39: Buf.add(ADR(b), s[at]); INC(at); + | '$': dollar(ADR(b)); + | else: Buf.add(ADR(b), s[at]); INC(at); + end; +end; +text := Buf.bytes(ADR(b)); +``` + +and after the thaw: + +``` +Buf.init(var b, ADR(arena.base)); (* b set up; the arena is an allocator: a pointer *) +while (at < LEN(s)) and not special(s[at]) do + ... + | 39: Buf.add(var b, s[at]); INC(at); + | '$': dollar(var b); + | else: Buf.add(var b, s[at]); INC(at); + end; +end; +text := Buf.bytes(b); (* only read: no mark *) +``` + +The marks are as many as the `ADR`s were, and shorter; now they are +true. `Buf.bytes(b)` has none, and the one pointer left, +`ADR(arena.base)`, stands out as what it is. + +**Passing on what the procedure was given.** A `var` parameter is a +variable, and is marked like any other: + +``` +proc dollar(var b: Buf.Buf); +begin + ... + Buf.str(var b, Sys.env(s[start..at])); +end; +``` + +**What the program holds by pointer** is given as `p^`: + +``` +proc emit(w: ^Writer; c: U8); +begin + Buf.add(var w^.out, c); +end; +``` + +**A map that keeps its keys:** + +``` +(* import Names := Map([]U8, INT, Str.hash, Str.eq); + table: Names.Map and heap: Heap's allocator, module variables *) +proc count(name: []U8); +begin + _ := Names.put(var table, keep Str.copy(heap, name), 1); (* a copy, on the heap *) + _ := Names.put(var table, keep "main", 1); (* a literal never ends *) +end; + +proc lookup(name: []U8): INT; +var n: INT; found: BOOL; +begin + n, found := Names.get(table, name); (* only read: no mark *) + ... +end; +``` + +**A map that keeps nothing that can end:** + +``` +(* import Counts := Map(INT, INT, hashInt, eqInt) *) +_ := Counts.put(var counts, 5, 7); (* INTs carry no slice: no keep *) +``` + +**A list of names a procedure keeps, passed on.** The parameter is +`keep` in turn, so the promise travels up: + +``` +(* import Seen := List([]U8) *) +proc remember(var l: Seen.List; keep name: []U8); +begin + _ := Seen.push(var l, keep name); +end; + +remember(var seen, keep "start"); +``` + +**`keep var`: a reader that keeps a slice of the caller's buffer.** + +``` +proc start(var r: Reader; keep var page: [4096]U8); +begin + r.rest := page[0..0]; + r.page := page[0..LEN(page)]; +end; + +var page: [4096]U8; (* a module variable *) +... +start(var rd, keep var page); +``` + +**A procedure value.** + +``` +type Step = proc(var s: State): BOOL; + +proc run(step: Step; var s: State); +begin + while step(var s) do end; +end; +``` + +**What stays a pointer**, and so keeps `ADR`: + +``` +NEW(node, ADR(arena.base)); (* an allocator *) +t, err := Thread.spawn(work, ^U8(ADR(job))); (* kept by the thread *) +XNextEvent(dpy, ADR(ev)); (* C *) +p := Sys.mmap(size); (* bytes, not a variable *) +``` + +**`ret`, unmarked:** + +``` +s := Str.trim(line[0..n]); (* s points into line *) +key, rest, ok := Str.cut(s, "="); (* both point into s *) +``` -## Marking what a procedure does with what it is given: `ret` and `keep` +### What the compiler says -### The hole +Each message names the mending, at the argument: +``` +Sh.raz:94:21: b: write `var b`: Buf.add may change it +Sh.raz:115:23: b: Buf.bytes only reads it: no `var` +Count.raz:12:27: name: write `keep name`: Names.put keeps it +Count.raz:14:31: 5: INT carries no slice: no `keep` +Count.raz:9:27: key[0..4]: kept by Names.put, but it points into this procedure's frame +Sh.raz:88:14: ADR(b): Buf.init takes `var b`, not a pointer +Sh.raz:151:14: p: Buf.add takes `var p^`, not a pointer +``` + +The last two are part 2's mending, `ADR(x)` to `var x` and `p` to +`var p^`. + +### How much noise + +Measured by a search of `src/`, `lib/`, `tools/` and `examples/`, about +23,300 lines of code (rough: a search, not the compiler): + +- about 630 `ADR`s; most are at calls to the library that part 2 turns + into `var x`: the same count, shorter, and true; +- about 580 calls of our own procedures with `var` parameters, unmarked + today: each gains `var`, about one line in 40; +- `keep`: a few dozen calls (`Map.put`, `List.push`, `Opt.init`, and + procedures that pass a kept slice on). + +So most of the marks stand where an `ADR` stands today. What is new is +the 580, and those are the calls, like `utf8(cur.pos)`, that change a +variable with nothing to say so. + +### `ADR` afterwards + +`ADR` stays, for the pointers `STANDARDS.md` lists: an allocator, what +may be `nil`, what the program holds by pointer already, C and memory +that is bytes, and a pointer a procedure keeps (`Thread.spawn`'s `arg`). +The marks of part 3 don't follow a pointer, so `ADR` at a call still +says "kept, or not a variable", unchecked, as now; with the library on +`var`, it is rare enough in ordinary code to be read so. + +### What the documentation gains + +The thaw is not done until each of these is: + +- **The report, `doc/raz.md`.** 8: the grammar above; "Parameters" + becomes "Parameters and arguments", with the table, rules 1 to 7, and + the swap and map examples; `keep` and `ret` from part 3. 5.9: + procedure types with marks. 7.3: `ret`. 10: the promise, whose + exceptions shrink to `ADR`, C, code procedures and `check off`. + 1, "A first look": its example marks its calls. Every example in the + report is marked. About two pages; the report stays under 40 (31 + now), counted by `make pdf`. +- **`doc/report-history.md`**: the version's entry, with the mending: + `ADR(x)` to `var x`, `p` to `var p^`, `var` and `keep` at each call + the compiler names. +- **raz(7)**, the language at a glance: under PROCEDURES, the table, as + a list, and one line for each of `var`, `keep` and `ret` at the call; + THE GRAMMAR IN BRIEF gains `Argument`; BUILT-IN PROCEDURES says they + take no marks. +- **The tutorial, raz-tutorial(7).** Section 4 marks `swap(var x, var y)` + and says why. A new section after 6, "WHAT A CALL DOES WITH ITS + ARGUMENTS": the table; a whole program, with its output, that counts + the words of its input in a `Map([]U8, INT)` with a `Buf`, using + `var`, `keep` and `ret`, each said in a sentence where it stands; + then "What the compiler stops", the four mistakes of the messages + above, each with its message. Section 16, "FROM PASCAL AND C TO RAZ", + gains: Pascal's `var` is unmarked at the call, raz's is marked, as C#'s + `ref` is; C's `&x` becomes `var x` when the callee only changes it, + and stays `ADR(x)` when it keeps it. Every other example in the + tutorial is marked. Each runs under `make check`, as now. +- **Volume 2, `doc/raz-lib.md`, and raz-lib(3)**: every signature of + part 2, each module's example marked, and a note in each changed + module of what changed. +- **`STANDARDS.md`**: "Parameters: a value, `var`, or a pointer" says + that `ADR` no longer marks what is kept, `keep` does; "The library + until a thaw" goes. +- **`doc/compiler.md`**: where the marks are read and checked, the + checks of part 3 and the set each value carries, with the code + before and after, and the table of lines. +- **`README.md`**: its examples marked. +- **Tests.** `conform/`: a program for each rule, run and error; + `test/err` and `test/msg`: each message above; `test/pretty`: the + marks formatted; the vim and emacs support indent and colour `var` + and `keep` at a call. +- **`sbcl/`**, the reference compiler from the report alone, follows + the report. + +### What it breaks, and the order of the work + +Every call to a procedure with a `var` or `keep` parameter, and every +call to the library's procedures of part 2: in `src/`, `lib/`, the +tools, the examples, the tests, and rmacs, ravite, rat, rlX11, rloc, +rpg and rsqlite, each when it moves its pin. The mending is mechanical, +and the compiler names it at each place. + +**The seed first.** The seed, frozen, can't read a marked call, and it +builds `src/` and reads `Sys` and `CAbi`. So this thaw needs the new +seed a release makes (CLAUDE.md, "The compiler and the seed") before +`src/` and that part of the library can be marked: the owner's call, +and the first step. + +Then, one commit each: + +1. The report, `report-history.md`, raz(7): for review. +2. The compiler reads the marks and checks rules 1 to 7. +3. The library's signatures (part 2), and every caller in this + repository mended. +4. `ret` and `keep` in headings and their checks (part 3), and the + library marked (`Str.trim`, `Map.put`, `List.push`, `Opt.init`, ...). +5. The tutorial, volume 2, raz-lib(3), `STANDARDS.md`, + `doc/compiler.md`, `README.md`. +6. The tests: `conform/`, `test/err`, `test/msg`, `test/pretty`, the + editors. + +The examples of parts 2 and 3 are written as the whole thaw makes +them, with the marks of part 1. + +### Part 2: the library's parameters by `var`, not by pointer + +Most of the library's procedures take what they work on as `^T`: +`Buf.str(ADR(b), s)`, `Vec.push(ADR(v), x)`, `Lock.take(ADR(l))`. None +keeps it past the call; each reads or changes it and returns. That is +what `var` is for (8), and with it the call is `Buf.str(var b, s)`. As it +is, `ADR` stands at nearly every call a program makes to the library, +and says nothing: a reader can't tell the few places where a pointer +is kept from the many where it isn't. `STANDARDS.md` says how our own +code chooses; this would bring the library to it. + +The proposal, procedure by procedure, from reading each one: + +- **To `var`** (it changes what it is given): `Arena.init`, `reset`, + `done`; `Buf.init`, `room`, `add`, `str`, `spare`, `cut`, `reset`, + `done`, and the `Buf` that `Files.load`, `Files.read`, `Fmt.write`, + `Str.join` and `Str.replace` write into; every procedure of `List`; + `Lock.take`, `give`; `Map.init`, `put`, `remove`, `done`; every + procedure of `Opt` (a `Parser` is many words, so even those that + only read it); `Sys.openDir`, `nextEntry`, `closeDir`; + `Thread.lock`, `unlock`; `Vec.init`, `room`, `push`, `pop`, `add`, + `tryPush`, `tryAdd`, `resize`, `reset`, `done`. +- **To a value** (it only reads a record of a few words): `Buf.bytes`, + `Vec.all`, `Map.get`, `Map.next`, `Sys.entryKind`. +- **Staying pointers**: an allocator, `^Mem.Allocator`, everywhere + (`NEW` takes one, and `^Arena(a)` needs one); `Mem.pages`, `unmap`, + `Sys.mmap`, `munmap`, `mapFile`, which are bytes, not variables; + `Thread.spawn`'s `arg`, which is kept, and the `^Thread` it gives, + which `join` and `signal` take as the program holds it; `Rt.fail`, + which the compiler calls. + +What it costs: it breaks every program that calls these, `src/`, +rmacs, ravite, the tools and the library's own tests among them. The +mending is mechanical, `ADR(x)` to `var x` at each such call, and `p` +to `var p^` where a program holds the thing by pointer, and the +compiler's message for a `^T` given to a `var` names it. The seed +knows `var` parameters but not the marks of part 1, so with them +`src/` and the library it imports wait for the new seed. Alone, this +part changes volume 2 and `raz-lib.3`, not the report. + +Met in rpg and rsqlite, whose `Query`, `Result` and `Stmt` went from +`^T` to `var` and to values, and whose own calls to `Buf` couldn't. + +### Part 3: marking what a procedure does with what it is given: `ret` and `keep` + +#### The hole + A procedure's locals, its value parameters and the array literals it passes as slices live in its frame, which is gone once it returns. A slice that still points there afterwards reads whatever the next call @@ -135,7 +460,7 @@ var buf: [16]U8; begin ... - _ := Names.put(ADR(table), buf[0..n], 1); (* the map, on the heap, keeps a slice of buf *) + _ := Names.put(var table, keep buf[0..n], 1); (* the map, on the heap, keeps a slice of buf *) end; ``` @@ -145,7 +470,7 @@ heading without seeing the body. Each check needs only the procedure it is in, so a one-pass compiler can make it. -### Words +#### Words **Carrying a slice.** A type carries a slice if it is a slice, or an array, record or variant with one in it: `[]U8`, `[4][]U8`, @@ -180,7 +505,7 @@ `var` parameter of this procedure. A value stored there, or returned, leaves the frame. -### No mark +#### No mark A parameter without a mark is only used while the call runs. The procedure may read it, copy its bytes (`COPY`, `Buf.str`), keep it in @@ -189,7 +514,7 @@ the call. This is what most procedures do: `Str.eq`, `Str.find`, `Buf.str`, `Sys.write`, `Fmt.print`, `Map.get`'s key. -### `ret` +#### `ret` ``` proc trim(ret s: []U8): []U8; @@ -235,7 +560,7 @@ and the caller takes the result as pointing where all of their arguments do. -### `keep` +#### `keep` ``` proc put(m: ^Map; keep k: K; keep v: V): BOOL; @@ -253,10 +578,10 @@ proc count(name: []U8); var key: [32]U8; begin - _ := Names.put(ADR(table), name, 1); (* an error: name isn't keep here *) - _ := Names.put(ADR(table), key[0..4], 1); (* an error: frame-bound to a keep *) - _ := Names.put(ADR(table), "main", 1); (* fine: a literal never ends *) - _ := Names.put(ADR(table), Str.copy(ADR(heap), name), 1); (* fine: copied to the heap *) + _ := Names.put(var table, keep name, 1); (* an error: name isn't keep here *) + _ := Names.put(var table, keep key[0..4], 1); (* an error: frame-bound to a keep *) + _ := Names.put(var table, keep "main", 1); (* fine: a literal never ends *) + _ := Names.put(var table, keep Str.copy(heap, name), 1); (* fine: copied to the heap *) end; ``` @@ -269,7 +594,7 @@ `s := s[1..LEN(s)]` with `var s: []U8` needs no `keep`. The caller's variable is no worse off. -### Both, and the order +#### Both, and the order `ret keep v: T` is both. The marks come before `var`, and each mark covers the group it starts, so `(ret s, t: []U8)` marks both; @@ -278,7 +603,7 @@ `static` is a mark only in `link static;`. `proc f(ret: INT)` is a parameter named `ret`. -### What the compiler checks, all of it +#### What the compiler checks, all of it In each procedure: @@ -295,7 +620,7 @@ Checks 1 to 3 on a local variable's value wait for the procedure's `end`, when everything assigned to it is known. -### Procedure values, generic modules, and the rest +#### Procedure values, generic modules, and the rest **Procedure types** carry the marks, `proc(ret s: []U8): []U8`. A procedure may be given where its type has the same marks or more, @@ -317,7 +642,7 @@ a slice reached through such a pointer, and a thread given a pointer to its spawner's locals. -### False alarms +#### False alarms The rules are sure, not exact: they reject some programs that were right. @@ -326,7 +651,7 @@ `examples/Sh.raz` does ``` - Opt.init(ADR(p), "Sh", [Opt.usage(...), Opt.option(...)]); + Opt.init(var p, "Sh", [Opt.usage(...), Opt.option(...)]); ``` `Opt.init` keeps its table in `p^`, so it needs `keep table`, and @@ -340,7 +665,7 @@ - **Several `ret` parameters.** A result is taken to point at all of them, though it may point at one. -### What it breaks +#### What it breaks Every procedure, in the library or a program, that returns or keeps what a parameter points at stops compiling until it is marked: a @@ -352,7 +677,7 @@ `Fmt.write` (they copy), `Map.get`. In sauce, `Svec.push` and `add` (`keep`), and what returns a slice of its argument. -### What it costs +#### What it costs Reports: 5.9 (procedure types), 7.3, 8 (parameters), 10 (the promise, whose exceptions shrink to `ADR`, C, code procedures and `check @@ -368,7 +693,98 @@ `ret` does. D's experience is that the corners (fields, generics, procedure values) take years; the narrow form here leaves record lifetimes out, and so the first false alarm above, on purpose. + +## A `const` parameter +`proc what(const x: Item): []U8;`: the callee may not change `x` (nor +take its address), so the caller may pass the address of a record or +array without a copy, and the program means the same. + +Met in porting the compiler to raz, where the seed's `const x: TItem` +(about 130 bytes) became a value parameter, copied at every call. +Measured once the port built: not what stands out, the compiler's +profile is flat (`Scan.tokPos` is 9%). + +## A minus sign after an operator + +`x & -8` and `n * -1` are errors: a unary minus belongs to the start +of a `SimpleExpr` (6), not to a factor, so after an operator it needs +parentheses, `x & (-8)`, or another spelling, `x & ~7`. This is the +report's grammar, Oberon's, and the compiler is right to refuse it; +but nearly every other language takes it, and the error ("expected an +expression, found -") surprises. + +The proposal: a factor that follows an operator may be `"-" Factor`. +The minus at the start of a `SimpleExpr` stays as it is, applying to +the first term, so no program changes meaning: `-a & b` is still +`-(a & b)`, which isn't `(-a) & b`, nor is `-a shr n` the same as +`(-a) shr n`. Only text that is an error today, `x & -8`, gets one. +The cost is that a minus means two things by where it stands, which +the report would say in a line. + +Met in tery's `aligned`, `(a + 7) & -8`. + +## Dividing 128 bits by 64 + +`Math.mul` gives the 128-bit product of two `U64`s; there is nothing +to divide such a number back down. Forth's `UM/MOD` and `M*/`, and +any arithmetic on numbers wider than a word (a bignum, a fixed-point +product scaled back), need "this 128-bit number, given as its high +and low words, divided by a 64-bit one: the quotient and the +remainder". x86-64 does it in one instruction, `div`; a raz program +does it by a loop of 64 shifts and subtractions, or a code procedure +per target. + +The proposal, in the library: + +``` +proc div*(hi, lo, d: U64): (U64, U64); (* hi, lo divided by d: the quotient and the remainder; hi < d *) +``` + +in `Math`, beside `mul`; `hi >= d`, whose quotient doesn't fit a word, +and `d = 0` stop the program as a division by zero does. It breaks no +program, so it could come with any release, not only a thaw. + +Met in tery's `udivmod`, a loop of 64 rounds. + +## Inlining a procedure that only returns + +Not the language's but the compiler's: a call of a procedure whose +body is `return` and an expression, as `Grep.zeros` and +`Scan.tokPos` are, made in place of the call. One pass keeps no +tree to copy, but it can read source again, as `while` reads its +condition again (`saveScan`, `restoreScan`): the arguments go to +temporaries, each evaluated once and in order, a scope names the +parameters as them, and the expression is read again from where the +procedure's begins, in its own module's scope. + +Measured by hand on the kernels: Grep with `zeros` and `lowest` +written in place is 20% faster, 0.88 times FPC `-O2`'s time where it +is 1.14 now. The compiler has 78 such procedures and the library 28; +the compiler's profile names the calls of small routines as what is +left (doc/compiler.md). + +Kept narrow: scalars passed by value, no recursion, no code +procedure (a square root has its own way), a procedure taken as a +value still called. What it costs: an inlined procedure is not in +the trap report's list of procedures, nor in gdb's backtrace, though +a failed check in it is still told at its line; 60 to 100 lines of +the budget; and, across modules, the imported module's +text kept to be read again, which is to be checked. + +More cases, from sauce (`~/r/sauce`, RAZ-NOTES.md). Its SHA-256, with +the rotation a procedure, `return (x shr n) | (x shl (32 - n))`, and +each word read by `Str.be`, ran at 52 MB/s; with both written out in +the loop, 115 MB/s, where FPC `-O2` does about 200 and `check off` +adds only 9%. Most of the gap is calls. `ror` is the narrow case +above; `Str.be` is not, a loop over its bytes, and would want a +`Str.be32` of four bytes that only returns, or the loop unrolled where +it is. A rotate of the language's own, an operator or a built-in +procedure, would serve SHA, the other hashes and checksums without +any inlining, but it is a language change, for a thaw. On 100 MB of +new files `sauce commit` takes 1.2 s against the Pascal sauce's 0.7, +and `verify` 0.95 s against 0.48: the hashing. + ## Less that is undefined Section 10 lists what isn't defined: a stale or made-up pointer, @@ -550,45 +966,3 @@ Met in mosaico's `Mconfig`, where `MenuAction.run` and a status block's kind `run` met; the block's kind was renamed `command`. - -## The library's parameters by `var`, not by pointer - -Most of the library's procedures take what they work on as `^T`: -`Buf.str(ADR(b), s)`, `Vec.push(ADR(v), x)`, `Lock.take(ADR(l))`. None -keeps it past the call; each reads or changes it and returns. That is -what `var` is for (8), and with it the call is `Buf.str(b, s)`. As it -is, `ADR` stands at nearly every call a program makes to the library, -and says nothing: a reader can't tell the few places where a pointer -is kept from the many where it isn't. `STANDARDS.md` says how our own -code chooses; this would bring the library to it. - -The proposal, procedure by procedure, from reading each one: - -- **To `var`** (it changes what it is given): `Arena.init`, `reset`, - `done`; `Buf.init`, `room`, `add`, `str`, `spare`, `cut`, `reset`, - `done`, and the `Buf` that `Files.load`, `Files.read`, `Fmt.write`, - `Str.join` and `Str.replace` write into; every procedure of `List`; - `Lock.take`, `give`; `Map.init`, `put`, `remove`, `done`; every - procedure of `Opt` (a `Parser` is many words, so even those that - only read it); `Sys.openDir`, `nextEntry`, `closeDir`; - `Thread.lock`, `unlock`; `Vec.init`, `room`, `push`, `pop`, `add`, - `tryPush`, `tryAdd`, `resize`, `reset`, `done`. -- **To a value** (it only reads a record of a few words): `Buf.bytes`, - `Vec.all`, `Map.get`, `Map.next`, `Sys.entryKind`. -- **Staying pointers**: an allocator, `^Mem.Allocator`, everywhere - (`NEW` takes one, and `^Arena(a)` needs one); `Mem.pages`, `unmap`, - `Sys.mmap`, `munmap`, `mapFile`, which are bytes, not variables; - `Thread.spawn`'s `arg`, which is kept, and the `^Thread` it gives, - which `join` and `signal` take as the program holds it; `Rt.fail`, - which the compiler calls. - -What it costs: it breaks every program that calls these, `src/`, -rmacs, ravite, the tools and the library's own tests among them. The -mending is mechanical, `ADR(x)` to `x` at each such call, and `p` to -`p^` where a program holds the thing by pointer, and the compiler's -message for a `^T` given to a `var` can name it. The seed knows `var` -parameters, so `src/` and the library it imports can take them now. -The report doesn't change; volume 2 and `raz-lib.3` do. - -Met in rpg and rsqlite, whose `Query`, `Result` and `Stmt` went from -`^T` to `var` and to values, and whose own calls to `Buf` couldn't.