Skip to content

Commit 2bad703

Browse files
Phase 1: real codegen — layout engine + typed-access lowering (#146)
Real codegen, replacing representative stub bodies, harvested from the Zig twasmc reference (PR #136, closed superseded with credit): - layout engine (field offsets, element stride, alignment, inline embedded regions — Players slot 48B, nested .pos.x into @vec2 at offset 16) - typed-access lowering (Body::Typed/Stmt/Access): real loads/stores at base + index*stride (+ offsets), via a base-pointer local read once per handle (keeps &mut/own clean under the verifier) - example 01 now carries &/&mut ownership; example 03 reads via base locals ADR-0004 ratified (Proposed -> Accepted) recording the Rust-vs-Zig decision + harvest; CHANGELOG + crate README updated. Verified locally (cargo test --workspace --locked: codegen 22 + verifier 80; fmt + clippy clean; no new deps) since Actions credit is exhausted. Closes the representative-bodies gap from #127 (front-end -> IR seam still deferred). Part of Phase 1 (#49). https://claude.ai/code/session_01Rq4da8i2uGnDUfanSB1Hx4
1 parent 8de427e commit 2bad703

6 files changed

Lines changed: 585 additions & 308 deletions

File tree

CHANGELOG.md

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,23 @@ self-verifies in-process — by verifying a `.wasm` from **any** producer,
2222
including external / third-party modules. Validated against `tw build`
2323
output (`structural ✓ · L7/L10/L13 ✓ · L2 ✓`).
2424

25+
### Real codegen: layout engine + typed-access lowering, harvested from Zig `twasmc` (#136) (2026-05-30)
26+
27+
The Rust producer's function bodies are now **real codegen**, not representative
28+
stubs. A layout engine computes field offsets, element strides, and alignment —
29+
including inline embedded regions (`Players` slot stride 48 B; `.pos.x` reaches
30+
into the embedded `@Vec2` at offset 16) — and typed accesses emit real
31+
loads/stores at computed offsets addressed as `base + index*stride (+ offsets)`.
32+
Each region handle is read once into a base-pointer local, so `&mut`/`own` params
33+
stay clean under the verifier; example 01 now also carries `&`/`&mut` ownership.
34+
35+
After a head-to-head Rust-vs-Zig comparison, the host-language choice was
36+
ratified (**ADR-0004 → Accepted**) and the approach + base-local convention were
37+
harvested from the parallel Zig producer (`twasmc`, PR #136), which was closed as
38+
superseded with credit. Closes the representative-bodies gap noted in #127; the
39+
front-end → IR parser seam remains (#127) and `if`/`region.scan` control flow is
40+
still simplified.
41+
2542
### In-tree producer: `typed-wasm-codegen` (codegen v0 → multi-module, debug info, human errors) (2026-05-30)
2643

2744
Adds the first in-tree `.twasm``.wasm` **producer** as a Rust crate
@@ -51,9 +68,10 @@ boundary, all verifier-backed. Deferred (tracked): the front-end → IR seam +
5168
remaining examples (#127), source → line maps (#129), region-imports /
5269
L13-positive (#140), ECHIDNA round-trip corpus (#130).
5370

54-
> Note: an alternative **Zig** producer (`twasmc`, `src/codegen/`) was
55-
> proposed in parallel in PR #136 (draft, open). Host-language reconciliation
56-
> against ADR-0004 (Rust) is an open owner decision.
71+
> Note: an alternative **Zig** producer (`twasmc`, `src/codegen/`) was proposed
72+
> in parallel in PR #136; after a Rust-vs-Zig comparison the Rust track was
73+
> retained (ADR-0004 **Accepted**) and #136 was closed as superseded, its layout
74+
> approach harvested — see the "Real codegen" entry above.
5775
5876
### Multi-producer carrier ABI: proposals 0001 + 0002 accepted, promoted to ADRs (2026-05-30)
5977

crates/typed-wasm-codegen/README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ seeds Phase 1 (issue #49, deliverable 1).
1212

1313
Lowers a typed region IR ([`Module`](src/lib.rs)) to:
1414

15-
- a **well-formed wasm module** (linear memory + type-correct function
16-
bodies), and
15+
- a **well-formed wasm module** with **real typed loads/stores at
16+
layout-computed offsets** (element `base + index*stride`, nested `.pos.x`
17+
into inline embedded regions), and
1718
- the L2–L6 carrier sections **`typedwasm.regions`** (ADR-0002) and
1819
**`typedwasm.access-sites`** (ADR-0003),
1920

@@ -57,7 +58,7 @@ cargo test -p typed-wasm-codegen
5758
| `typedwasm.ownership` (L7/L10) | **emitted** — Linear/ExclBorrow/SharedBorrow (example 03 + multi-module callee), checked by `verify_from_module` |
5859
| Multi-module (linear boundary) | **emitted** — callee export + caller import round-trip through `verify_cross_module` (#128) |
5960
| Debug symbols (`name` section) | **emitted** — function names for debuggers (#129, first increment) |
60-
| Function-body lowering | representative type-correct bodies, not full `region.scan`/indexing semantics |
61+
| Function-body lowering | **real** — layout engine (offsets/stride/align, inline embedded regions) + typed loads/stores via a base-local (harvested from Zig `twasmc` #136); `if`/`region.scan` control flow still simplified |
6162

6263
## Example coverage (#127)
6364

0 commit comments

Comments
 (0)