Skip to content

Lower residual breaks to wasm br depths - #177

Open
dcharlot65-openie wants to merge 1 commit into
mainfrom
cursor/wasm-residual-break-1ec8
Open

dcharlot65-openie wants to merge 1 commit into
mainfrom
cursor/wasm-residual-break-1ec8

Conversation

@dcharlot65-openie

Copy link
Copy Markdown
Contributor

Fixes #173

rce emit wasm for a graph term still returns from a kernel status (if ...). That if is not a break target. A structured control backend now lowers a residual break to br N.

The carried count is the source depth: ONE-15 writes set e = d; break 0. break 0 is the local exit and does not decrement e. A block or a loop decrements once. An if does not. That is one/eval.rs (Block and Loop subtract one; If returns the inner control unchanged) and W2, which counts enclosing blocks and loops. The ledger row of 2026-11-09 and Break.lean stepExit say the same. ONE-15 says "construct" and does not say "not an if". The evaluator and W2 are the clause that decides, and they agree with that reading, so the e = d + 1 initialisation is not used.

WebAssembly counts every block, loop and if as a br label, and a branch to a loop restarts it. A loop is therefore an exit block around a loop. The exit block is the one residual step. The restart label is wasm structure only, as an if is. Residual and LabelDepth are separate types. The br immediate is how many wasm labels lie between the break and its residual target, including that target.

The integer carrier is wrapping i64, which is int.wadd, int.wsub and int.wmul at int[-2^63, 2^63 - 1]. Comparisons are signed. The gate and the specification are unchanged.

Tests: 10 core programs (nested blocks and loops, intervening ifs, depths 0, 1 and 2, break 0 as a local exit, and breaks that cross several ifs and loops). Each module is assembled with wat, checked with wasmparser, and instantiated with wasmi. The export run matches the core evaluator. A further table checks br depths on label stacks directly.

Base 86b8efcf6f9af6d46a04c0998d105037d0c1002b. Not merged. Not deployed.

Open in Web Open in Cursor 

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 <dcharlot65-openie@users.noreply.github.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

emit wasm: structured control-flow backend for residual breaks (if counts as a br label)

2 participants