| @@ -79,7 +79,7 @@ typedef struct tl_ctx { |
| 79 | 79 | - Every function returns a `tl_value`. |
| 80 | 80 | - Parameters are **owned** by default. The callee must consume them: drop them, store them, or pass them on. |
| 81 | 81 | - Results are owned by the caller. |
| 82 | | −- For *direct* calls only, the compiler may make a parameter *borrowed*, as decided by Perceus borrow inference (M5). Both caller and callee agree because the signature is known statically. |
| 82 | +- For *direct* calls only, the compiler may make a parameter *borrowed*, as decided by Perceus borrow inference. Both caller and callee agree because the signature is known statically. See [Borrowed parameters](#borrowed-parameters-v1-proposed) for the rules as implemented. |
| 83 | 83 | |
| 84 | 84 | **Closure calls:** |
| 85 | 85 | - Closures are always called with owned arguments, **including `self`**. |
| @@ -101,7 +101,7 @@ typedef struct tl_ctx { |
| 101 | 101 | |
| 102 | 102 | **Freeing is iterative.** Dead blocks go onto a per-actor worklist, so dropping a million-element list never recurses. This is essential on small fiber stacks. |
| 103 | 103 | |
| 104 | | −**In-place reuse:** when `tl_is_unique(v)` holds, generated code may overwrite a block's fields and keep the block (FBIP reuse). |
| 104 | +**In-place reuse:** when `tl_is_unique(v)` holds, generated code may overwrite a block's fields and keep the block (FBIP reuse). See [Drop specialisation and reuse](#drop-specialisation-and-reuse-v1-proposed) for the generated code. |
| 105 | 105 | |
| 106 | 106 | **Leak oracle:** with the `stats` feature (on by default for now), `tl_stats_live_blocks()` counts live blocks. Tests require it to be 0 at exit. |
| 107 | 107 | |
| @@ -191,7 +191,7 @@ The M0 spike is redone against this revision at the start of M5. |
| 191 | 191 | |
| 192 | 192 | Every tail call is emitted with `musttail`; a self tail call is a jump. |
| 193 | 193 | - **Externs borrow.** Functions declared `@extern`, and the runtime functions generated code calls, borrow their arguments and return owned results. Perceus (`tilc_perceus`) relies on this. `tl_println` changed accordingly. |
| 194 | | −- **Immortal blocks.** Blocks with `rc == 0` are static literals (strings, floats). `tl_dup` and `tl_drop` leave them alone. |
| 194 | +- **Immortal blocks.** Blocks with `rc == 0` are static literals (strings, floats) and constant dictionaries (v1, below). `tl_dup` and `tl_drop` leave them alone. |
| 195 | 195 | - **Regions:** `tl_region_new`, `tl_ref_new`, `tl_ref_get`, `tl_ref_set` and `tl_region_end` (tag `TL_TAG_REGION`). A region holds a count on each of its references until it ends. |
| 196 | 196 | - **Handler capabilities** (ADR 0010) are ordinary blocks holding the clause closures. A `fn` operation calls its clause directly. |
| 197 | 197 | - **Unwinding (part 2).** Unwinding uses yield checks, not tables (ADR 0003, Revision 2, proposed): |
| @@ -245,6 +245,35 @@ The M0 spike is redone against this revision at the start of M5. |
| 245 | 245 | - `tl_string_join` joins a `List<String>` in one allocation. |
| 246 | 246 | - The value layout and the calling convention are unchanged; only these std externs changed. |
| 247 | 247 | |
| 248 | +### Perceus optimisations (v1, issue #34; proposed, needs engineering review) |
| 249 | +These change what generated code does with counts, not the layouts: `TL_ABI_VERSION` stays 1. Rationale and measurements: [ADR 0002, Implementation (v1)](adr/0002-memory-model.md#implementation-v1--2026-10-09). |
| 250 | + |
| 251 | +#### Borrowed parameters (v1, proposed) |
| 252 | +- A function called only directly (never made into a closure) may *borrow* some parameters: `FnDecl::borrowed` in Core, decided by `tilc_perceus` and printed `^v` by `tilc core`. |
| 253 | +- The caller keeps its reference across the call and releases it as usual afterwards. The callee must neither drop nor consume a borrowed parameter: to store it, return it or pass it to an owned position it takes a `dup` first. |
| 254 | +- A borrowed parameter is not one of the frame's references: unwinding (preemption, panics, exit signals, `final ctl`) does not drop it. |
| 255 | +- The C signature is unchanged (`tl_fn`); only the ownership of the argument differs. A self tail call (`goto entry`) passes borrowed arguments in borrowed parameters. |
| 256 | +- Closures, effect clauses, externs and runtime functions keep their conventions (owned for closures and clauses, borrowed for externs). When an entry function (`main`, a test) borrows its root capability, `tl_entry` drops it after the call. |
| 257 | +- **Projections borrow.** In Core after Perceus, `let a = x.i` reads the field without a `dup` (C: `a = tl_as_block(x)->fields[i]`); a `dup a` follows when `a` must be owned. A projection of a borrowed value stays borrowed, so a borrowed structure is walked without counting. |
| 258 | + |
| 259 | +#### Drop specialisation and reuse (v1, proposed) |
| 260 | +`release x [i: a, ...]` (Core `Expr::Release`) ends `x`'s reference after its fields `a`, ... were projected: |
| 261 | + |
| 262 | +```c |
| 263 | +if (tl_is_unique(x)) { |
| 264 | + tl_as_block(x)->fields[i] = TL_UNIT; /* moved out to a, ... */ |
| 265 | + tl_free_moved(ctx, tl_as_block(x), moved); /* tl_free, or tl_drop_slow if other fields remain */ |
| 266 | +} else { |
| 267 | + tl_dup(a); ... |
| 268 | + tl_drop(ctx, x); |
| 269 | +} |
| 270 | +``` |
| 271 | + |
| 272 | +With reuse (`release x [...] reuse as t`), the unique branch keeps the cell instead, `t = tl_reuse_token(ctx, b)`: its remaining fields are dropped and every field is unit, so a token is an ordinary owned value (a unique block of units, or `TL_UNIT`) and dropping it is safe. A later `reuse t in C(...)` (Core `Value::Reuse`) writes the cell in place when `t` is a block with as many fields as `C`, setting its `tag`; otherwise it drops `t` and allocates. Tokens are only taken from values a `switch` matched (user constructor cells: no raw area, flags 0); `tl_reuse_token` frees a runtime block instead. `tl_free_moved` and `tl_reuse_token` are inline helpers in the generated prelude. |
| 273 | + |
| 274 | +#### Constant dictionaries (v1, proposed) |
| 275 | +A trait dictionary without constraints (`Num<Int>`) is emitted as `static const` immortal blocks: a tuple (`tag` 0) of closures without captures (`TL_TAG_CLOSURE`, `scan_fsize` 0, the code pointer in the raw area). They live in read-only memory, so nothing may write to a block with `rc == 0`: `tl_dup`, `tl_drop`, `send` and the runtime already leave them alone. `tl_dup` and `tl_drop` now test `rc != 0` inline instead of calling `tl_dup_shared` / `tl_drop_slow` for immortal blocks. |
| 276 | + |
| 248 | 277 | **Not yet implemented:** |
| 249 | 278 | - several scheduler threads (ADR 0004, Revision 1); |
| 250 | 279 | - accessors for `Bytes` in C bindings (`tl_bytes_ptr`, `tl_bytes_len`, `tl_bytes_from`). |