STANDARDS.md: a parameter by value, var or pointer; PROPOSALS.md: the library's by var A procedure takes what it reads as a value (or var, when large), what it changes and lets go as var, and only what it keeps as a pointer, so that ADR at a call means the callee keeps it. The library takes nearly everything by pointer though it keeps nothing; the proposal lists, for a thaw, what moves to var, what to a value, and what stays.
| @@ -77,7 +77,9 @@ | ||
| 77 | 77 | and after, and in `doc/compiler.md`. |
| 78 | 78 | - Style: clarity over cleverness; each unit opens with what it is, what |
| 79 | 79 | it depends on and what it leaves out; comments say why. Match the |
| 80 | - report's plain, short prose in documentation. | |
| 80 | + report's plain, short prose in documentation. `STANDARDS.md` says | |
| 81 | + how raz is written here: a parameter is a value, `var`, or, only | |
| 82 | + when it is kept, a pointer; no `ADR` where `var` serves. | |
| 81 | 83 | |
| 82 | 84 | ## Where things stand |
| 83 | 85 |
| @@ -550,3 +550,45 @@ | ||
| 550 | 550 | |
| 551 | 551 | Met in mosaico's `Mconfig`, where `MenuAction.run` and a status block's |
| 552 | 552 | 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. |
| @@ -0,0 +1,56 @@ | ||
| 1 | +# raz: standards | |
| 2 | + | |
| 3 | +How raz is written here: in `src/`, `lib/`, `tools/` and `examples/`, | |
| 4 | +and in the programs and libraries that use them (rpg, rsqlite, rmacs, | |
| 5 | +ravite, ...). The report says what a program may do; this says what we | |
| 6 | +do. New code keeps to it; old code comes to it when it is next changed, | |
| 7 | +or, for the library's interfaces, at a thaw (`PROPOSALS.md`). | |
| 8 | + | |
| 9 | +## Parameters: a value, `var`, or a pointer | |
| 10 | + | |
| 11 | +A procedure takes a variable it is given in one of three ways, by what | |
| 12 | +it does with it: | |
| 13 | + | |
| 14 | +- **It reads it**: a value, when the type is a few words at most (a | |
| 15 | + handle, a slice, a small record). A larger record or array that is | |
| 16 | + only read is `var`, since a value parameter is a copy (8; a `const` | |
| 17 | + parameter waits in `PROPOSALS.md`). | |
| 18 | +- **It changes it, and lets go of it when it returns**: `var`. | |
| 19 | +- **It keeps it**, in a record, a module variable or another thread, | |
| 20 | + past the call: a pointer, `^T`. | |
| 21 | + | |
| 22 | +So `ADR` at a call says the callee keeps what it is given. A reader | |
| 23 | +who sees `Pg.start(q, ADR(c), sql)` knows that `q` is changed and `c` | |
| 24 | +is kept, and that `c` must outlive `q`. | |
| 25 | + | |
| 26 | +``` | |
| 27 | +proc start*(var q: Query; c: ^Conn; sql: []U8); (* q changed; c kept in q *) | |
| 28 | +proc run*(q: Query; var r: Result): ERROR; (* q read; r changed *) | |
| 29 | +proc rows*(r: Result): INT; (* r read: one word *) | |
| 30 | +proc clear*(var r: Result); (* r changed *) | |
| 31 | +``` | |
| 32 | + | |
| 33 | +A pointer is right also where `var` can't serve: | |
| 34 | + | |
| 35 | +- **An allocator**, `^Mem.Allocator`: `NEW` takes one, and an | |
| 36 | + allocator such as `Arena` is reached through its first field, | |
| 37 | + `^Arena(a)`, which a `var` of exactly one type can't do. | |
| 38 | +- **What may be absent**: a pointer may be `nil`; a `var` can't. | |
| 39 | +- **What the program already holds by pointer**: what `NEW` or the | |
| 40 | + library hands out, such as `Thread.spawn`'s `^Thread`, is passed on | |
| 41 | + as it is, not as `p^`. | |
| 42 | +- **C**, and memory that is bytes, not a variable: `extern` | |
| 43 | + procedures, `Mem.pages`, `Sys.mmap`, a buffer's address. | |
| 44 | +- **A procedure value's argument** that holds what the procedure will | |
| 45 | + need, as `Thread.spawn`'s `arg: ^U8`, which is kept. | |
| 46 | + | |
| 47 | +Anything else that takes `^T` and neither keeps it nor needs one of the | |
| 48 | +above is a mistake, and its callers' `ADR`s are noise. | |
| 49 | + | |
| 50 | +## The library until a thaw | |
| 51 | + | |
| 52 | +The library's interfaces mostly take `^T` (`Buf`, `Vec`, `Map`, | |
| 53 | +`List`, `Lock`, `Opt`, ...), though none keeps what it is given. They | |
| 54 | +can't change while the freeze holds, since every caller would break; | |
| 55 | +`PROPOSALS.md` lists them for the next thaw. Until then a program | |
| 56 | +calls them with `ADR`, and its own procedures keep to this file. |