From 95efc232246c21a0b3ef4f2653b7ae9134ddb885 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Fri, 13 Nov 2026 20:08:11 +0000 Subject: [PATCH] Lower residual breaks to wasm br depths. The carried count stays the source depth. An if does not decrement it, and break 0 is the local exit. The br index adds each intervening if and a loop's restart label, which the residual count does not include. Co-authored-by: David Charlot --- compute/crates/rce-emit-wasm/src/control.rs | 371 ++++++++++++++++++ compute/crates/rce-emit-wasm/src/lib.rs | 7 + spec/checker/src/lib.rs | 1 + spec/checker/src/wasm_break.rs | 211 ++++++++++ spec/checker/tests/emit_wasm_break.rs | 413 ++++++++++++++++++++ 5 files changed, 1003 insertions(+) create mode 100644 compute/crates/rce-emit-wasm/src/control.rs create mode 100644 spec/checker/src/wasm_break.rs create mode 100644 spec/checker/tests/emit_wasm_break.rs diff --git a/compute/crates/rce-emit-wasm/src/control.rs b/compute/crates/rce-emit-wasm/src/control.rs new file mode 100644 index 00000000..10ac6238 --- /dev/null +++ b/compute/crates/rce-emit-wasm/src/control.rs @@ -0,0 +1,371 @@ +//! Structured control in the wasm emitter. +//! +//! A residual break count and a wasm `br` depth are different numbers. +//! ONE-15 sets the carried count `e` to the source depth `d` (`e = d`, not +//! `e = d + 1`). The originating `break 0` is the local exit and does not +//! decrement `e`. Each block or loop that is left decrements once. An `if` +//! does not. That is what `spec/checker/src/one/eval.rs` does (`Block` and +//! `Loop` subtract one; `If` returns the inner control unchanged) and what +//! W2 counts: a `break d` names an enclosing block or loop. +//! +//! WebAssembly counts every `block`, `loop` and `if` as a `br` label. +//! Branching to a `loop` restarts it, so a loop is emitted as an exit +//! `block` around a `loop`. The exit block is the one residual step for +//! that loop. The restart label is wasm structure only, as an `if` is. +//! The `br` immediate is the number of wasm labels from the break out to +//! its residual target, including that target's label. + +/// The carried residual count. At the break it is the source depth. +/// After a block or a loop has been left it is what remains. It is not a +/// wasm label index. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Residual(u32); + +impl Residual { + /// `e = d` at `break d`. + pub const fn at_break(depth: u32) -> Self { + Self(depth) + } + + pub const fn get(self) -> u32 { + self.0 + } +} + +/// A wasm `br` label index. Zero is the innermost structured instruction. +/// Every enclosing `block`, `loop` and `if` counts. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct LabelDepth(u32); + +impl LabelDepth { + const fn zero() -> Self { + Self(0) + } + + pub const fn get(self) -> u32 { + self.0 + } + + fn outward(self) -> Self { + Self(self.0 + 1) + } +} + +/// One wasm structured label that encloses the instruction being emitted. +/// The innermost label is first in the slice [`br_label`] walks. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Label { + /// An RCE block. Residual 0 branches here, and the continuation runs. + Block, + /// The exit block wrapped around an RCE loop. Residual 0 leaves the loop. + /// The loop is this one residual step. + LoopExit, + /// The wasm `loop`, which a branch restarts. Not a residual step. + LoopRestart, + /// An RCE `if`. A wasm branch target. The residual count is unchanged. + If, +} + +/// The `br` depth of `residual` under `innermost_first`. +/// +/// Residual 0 selects the nearest block or loop exit. Each block or loop +/// exit further out consumes one from the count. An `if` and a loop's +/// restart label add one to the wasm depth and leave the count as it is. +pub fn br_label(innermost_first: &[Label], residual: Residual) -> Result { + let mut depth = LabelDepth::zero(); + let mut left = residual; + for label in innermost_first { + match label { + Label::If | Label::LoopRestart => depth = depth.outward(), + Label::Block | Label::LoopExit => { + if left.get() == 0 { + return Ok(depth); + } + left = Residual(left.get() - 1); + depth = depth.outward(); + } + } + } + Err(format!( + "break {} names no enclosing block or loop", + residual.get() + )) +} + +/// An operand. Integer operations are wrapping i64, which is `int.wadd`, +/// `int.wsub` and `int.wmul` at `int[-2^63, 2^63 - 1]`. Comparisons are signed. +#[derive(Clone, Debug, PartialEq)] +pub enum Expr { + Local(u32), + I64(i64), + Bool(bool), + Add(Box, Box), + Sub(Box, Box), + Mul(Box, Box), + Eq(Box, Box), + Lt(Box, Box), + Le(Box, Box), + Not(Box), +} + +/// A statement of the structured backend. `Break` carries a residual count, +/// not a wasm depth. +#[derive(Clone, Debug, PartialEq)] +pub enum Stmt { + Set(u32, Expr), + If(Expr, Vec, Vec), + Block(Vec), + Loop(Vec), + Break(Residual), + Return(Expr), +} + +/// One function. Locals are i64. The first `params` locals are the parameters. +#[derive(Clone, Debug, PartialEq)] +pub struct Func { + pub params: u32, + pub locals: u32, + pub body: Vec, +} + +/// The text module of `f`, exporting `run`. +pub fn emit_func(f: &Func) -> Result { + if f.locals < f.params { + return Err("a function declares fewer locals than parameters".into()); + } + let mut out = Out { + buf: String::new(), + labels: vec![], + indent: 0, + }; + out.line("(module"); + out.indent = 1; + let mut head = String::from("(func (export \"run\")"); + for _ in 0..f.params { + head.push_str(" (param i64)"); + } + head.push_str(" (result i64)"); + let extra = f.locals - f.params; + if extra > 0 { + head.push_str(" (local"); + for _ in 0..extra { + head.push_str(" i64"); + } + head.push(')'); + } + out.line(&head); + out.indent = 2; + out.seq(&f.body)?; + out.line("(unreachable)"); + out.indent = 1; + out.line(")"); + out.indent = 0; + out.line(")"); + Ok(out.buf) +} + +struct Out { + buf: String, + /// Outermost label first. The last label is the innermost. + labels: Vec