| @@ -41,3 +41,52 @@ Lowering is chosen per operation kind, which is known statically from the effect |
| 41 | 41 | - **A fiber per handler for `ctl`** (OCaml 5 style). Captures cheaply, but with corosensei's asymmetric coroutines, preemption inside a nested fiber returns to the parent handler rather than to the scheduler. It also needs a separate wasm story. Deferred. |
| 42 | 42 | - **`setjmp`/`longjmp` or forced unwinding with `__attribute__((cleanup))`.** Zero cost on the happy path, but it runs drops at scope end instead of last use, fights Perceus's ownership tracking, and is undefined behaviour across Rust frames. |
| 43 | 43 | - **A full CPS transform.** Simple semantics, but it allocates on every effectful call and makes debugging the generated C painful. |
| 44 | + |
| 45 | +## Revision 1 — 2026-10-09: native `ctl`, zero-overhead unwinding, guaranteed tail calls |
| 46 | +**Status:** Proposed, decided by the project owner; needs engineering review (#2). This supersedes "general `ctl` is interpreter-only in v0", rule 3 (a yield check after every non-leaf call), and the tail-call rule. |
| 47 | + |
| 48 | +### `ctl` operations compile natively in v0: stack switching with one-shot continuations |
| 49 | +This follows Lexa (Ma et al., OOPSLA 2024), OCaml 5 and WasmFX. |
| 50 | + |
| 51 | +- **Handlers on fibers:** a `handle` for an effect that has `ctl` operations runs its body on a fiber taken from a per-thread pool. Fibers have large, lazily committed stacks with a guard page; C stacks cannot move. |
| 52 | +- **Operations:** performing a `ctl` operation switches to the handler's stack, and `resume` switches back. A continuation is the suspended fiber, so it is one-shot and needs no copying. |
| 53 | +- **A continuation that is never resumed** is resumed once in *cancel* mode, which unwinds it (see below), so Perceus stays exact. |
| 54 | +- **Unchanged paths:** |
| 55 | + - `fn` operations remain direct calls through the evidence vector; |
| 56 | + - `final ctl` uses the unwinding below. |
| 57 | + |
| 58 | + Programs that do not use `ctl` pay nothing. |
| 59 | +- **The context switch:** the runtime's own symmetric switch, a few dozen lines of assembly for x86_64 and aarch64. It replaces corosensei, and is shared with actor scheduling (ADR 0004). |
| 60 | + |
| 61 | +### Unwinding is zero-overhead and table-driven |
| 62 | +This follows the zero-overhead principle and mechanism of Ma et al., *Zero-Overhead Lexical Effect Handlers* (OOPSLA 2025). |
| 63 | + |
| 64 | +- **Normal paths carry nothing:** no flag checks after calls, and no extra arguments. |
| 65 | +- **The data:** the compiler emits per-call-site metadata keyed by return address, recording which owned values must be released if the stack is unwound through that call. |
| 66 | +- **At raise time,** for a panic, an exit signal or a `final ctl` operation, a runtime stack walker visits frames from the raise point and runs their cleanups. It stops at: |
| 67 | + - the handler frame, recorded (stack address and resume point) when the handler was installed: a cost per `handle`, not per call; |
| 68 | + - or the actor's root. |
| 69 | +- **In the C backend,** the call-site tables are the platform unwind tables. The compiler emits C compiled with `-fexceptions`, marks owned locals with `__attribute__((cleanup))`, and sets moved values to null. |
| 70 | + - The runtime walks the tables with `_Unwind_ForcedUnwind` and a stop function that recognizes the target frame. |
| 71 | + - The runtime is entered through `extern "C-unwind"`, so unwinding through it is defined. |
| 72 | +- **The trade-off, measured in the paper:** raising becomes more expensive (a stack walk), while code that does not raise runs faster. Panics and exit signals are rare by nature. |
| 73 | + - For `final ctl` effects that are raised frequently, an effect can opt into the previous early-return (bubbling) strategy, as Zero Lexa does per effect. |
| 74 | + - The default and the opt-in syntax are settled with the M5 benchmarks. |
| 75 | +- **Handlers stay dynamically scoped (Koka).** Zero Lexa targets *lexically* scoped handlers; tilcayo adopts its zero-overhead unwinding, not lexical scoping. Handler lookup is already constant-time through the evidence vector. Moving to lexical handlers would be a separate language decision. |
| 76 | + |
| 77 | +### Tail calls are guaranteed |
| 78 | +**Rule:** every call in tail position runs in constant stack, whether the callee is known, mutually recursive, or a closure. This is part of the semantics, not an optimization. |
| 79 | + |
| 80 | +- **Self tail calls** become loops. |
| 81 | +- **Every other tail call** is emitted with `musttail`. `musttail` requires the caller's and callee's C signatures to match, so all generated functions share **one** signature: |
| 82 | + - the context, then 6 value arguments (passed in registers); |
| 83 | + - unused slots are never read; |
| 84 | + - further arguments go through an area in the context. |
| 85 | +- **Toolchain:** clang ≥ 13 or GCC ≥ 15. `tilc build` rejects older C compilers instead of silently losing the guarantee. |
| 86 | +- **Interactions:** |
| 87 | + - **Reference counting:** nothing remains to release after a tail call. Perceus releases before it, and borrowed parameters passed on are first given an owned copy. |
| 88 | + - **Unwinding:** there are no unwinding metadata at tail calls. |
| 89 | + - **Preemption:** the reduction check stays in the callee's entry. |
| 90 | + - **Handlers:** calls inside a `handle` body are not in tail position for the enclosing function, because the handler frame remains. These are Koka's semantics. |
| 91 | +- **Tail recursion modulo cons** (Leijen & Lorenzen, POPL 2023) is a Core transformation. `[f(x), ..map(rest, f)]` writes into a hole of an already-allocated cell and becomes a real tail call, so `map`, `filter` and `append` run in constant stack. It benefits both the interpreter and the native backend. |
| 92 | +- **Tests:** native runs of `tests/run` use a deliberately small stack (`ulimit -s`). |