docs: add WASM backend design document#22
Draft
Proxy-alt wants to merge 2 commits into
Draft
Conversation
GloriousTacoo
requested changes
Apr 28, 2026
GloriousTacoo
requested changes
Apr 28, 2026
f0b2b31 to
7512fab
Compare
Adds a Tier 1 design document for a WebAssembly backend covering the opcodes currently implemented and tested in the IR layer. Branching, control flow, host calls, and Tier 2 optimization are out of scope per direction from @GloriousTacoo and will be added when the IR side stabilizes for them. The design follows PROGRAMMING_RULES.md: compile-time backend selection via static-inline emit functions, cursor-based iteration over the instruction array, single-pass emission with locals-count patching to avoid a second cache pass, and read-only fields on the IR block accessed directly rather than hoisted into locals. Six open questions are collected in §16 for resolution in review before the §12 instruction mapping table is finalized. Signed-off-by: Proxy Alt <proxy-alt@proxy-alt.dev>
7512fab to
9f24005
Compare
Author
|
Force-pushed addressing all 16 review comments: Errors corrected:
Removed:
Added:
Open question changes:
|
Comment on lines
+656
to
+658
| **All locals declared as i64.** `ssa_bit_widths[]` exists but using narrower | ||
| types is deferred. Costs roughly 1 byte per local in the locals section, | ||
| trivial. |
Collaborator
There was a problem hiding this comment.
This is flawed thinking. If the locals are all 64-bits and an ARM instruction overflows you will produce incorrect values.
Comment on lines
+362
to
+380
| static inline void wasm_emit_local_get (BalBackendCtx* ctx, uint32_t idx); | ||
| static inline void wasm_emit_local_set (BalBackendCtx* ctx, uint32_t idx); | ||
| static inline void wasm_emit_i64_const (BalBackendCtx* ctx, int64_t v); | ||
| static inline void wasm_emit_i64_load (BalBackendCtx* ctx, uint32_t align, uint32_t off); | ||
| static inline void wasm_emit_i64_store (BalBackendCtx* ctx, uint32_t align, uint32_t off); | ||
| static inline void wasm_emit_i64_add (BalBackendCtx* ctx); | ||
| static inline void wasm_emit_i64_sub (BalBackendCtx* ctx); | ||
| static inline void wasm_emit_i64_mul (BalBackendCtx* ctx); | ||
| static inline void wasm_emit_i64_div_s (BalBackendCtx* ctx); | ||
| static inline void wasm_emit_i64_div_u (BalBackendCtx* ctx); | ||
| static inline void wasm_emit_i64_and (BalBackendCtx* ctx); | ||
| static inline void wasm_emit_i64_xor (BalBackendCtx* ctx); | ||
| static inline void wasm_emit_i64_or (BalBackendCtx* ctx); | ||
| static inline void wasm_emit_i64_shl (BalBackendCtx* ctx); | ||
| static inline void wasm_emit_i64_shr_s (BalBackendCtx* ctx); | ||
| static inline void wasm_emit_i64_shr_u (BalBackendCtx* ctx); | ||
| static inline void wasm_emit_select (BalBackendCtx* ctx); | ||
| static inline void wasm_emit_return (BalBackendCtx* ctx); | ||
| static inline void wasm_emit_unreachable(BalBackendCtx* ctx); |
Collaborator
There was a problem hiding this comment.
Rename BalBackendCtx to bal_backend_context_t
Author
|
Will deal with updating the branch (with atomic commits too since I accidentally clicked the update button) and finishing PR after API has stabilized |
f06eb11 to
c6f5cac
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Adds
docs/WASM_BACKEND_DESIGN.md, a Tier 1 design document for a WebAssembly backend for Ballistic. The doc covers a single-pass dumb emitter scoped strictly to the IR opcodes currently implemented and tested in the engine.Motivation: a WASM backend is the path to running Ballistic in the browser, on iOS, and on other sandboxed targets where native JIT is not permitted. The design respects the IR's structured-SSA model and PROGRAMMING_RULES.md (compile-time backend selection, no function pointer dispatch in the translation loop, single-pass emission, cursor iteration, read-only fields accessed directly through
block).The doc was written after a Discord thread with @GloriousTacoo (4/19–4/23) clarifying IR encoding, register-file placement, MERGE destination semantics, and Tier 1 scope. Per his direction, branching, control flow, host calls, and Tier 2 are explicitly out of scope; they are listed in §14 with the understanding that the doc gets extended when the IR side stabilizes for them.
Open questions that could not be closed in chat are collected in §16 (Q1-Q6). They block finalizing the §12 instruction mapping table for five opcodes (
OPCODE_MOVoperand order,OPCODE_DIVsign split,OPCODE_SHIFTdirection encoding,OPCODE_CMPresult type, theOPCODE_TRAPhost-callback ABI, andOPCODE_RETURNunder inlining). None of them block starting work on the assembler or module-builder layers.No code changes. No dependencies.
Resolves: (no issue, design doc submitted ahead of implementation per @GloriousTacoo's CONTRIBUTING guidance: "create [a] design doc similar to IR_DESIGN_DOC.md and create a pull request").
Type of Change
Code Review Process
Acknowledged. I expect this draft to change substantially in review, the IR doc went through ~50 commits over two months before settling, and a backend design touching every opcode will need similar iteration. Six open questions in §16 are explicitly flagged for resolution before the table in §12 can be considered final. I am prepared to defend each section, rework the operand mapping if any of Q1-Q6 resolves differently than assumed, and rewrite the doc to match whatever direction @GloriousTacoo gives.
Breaking Changes
None. Documentation-only addition. No source files, build files, tests, or public headers are modified.
Checklist