From 76d0fd0d2888054bc023b84e9eca5ba7d4b72929 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 16 Apr 2026 16:03:10 +0000 Subject: [PATCH] codegen: reuse analyzer's local allocations so inline asm `{param}` works MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes compiler-bugs.md #1 — the inline-asm `{name}` resolver looks parameters up in the analyzer's `VarAllocation` table (because that's the only address map it has), but `IrCodeGen::new` was minting a parallel `$0300+` range for every function-local and ignoring what the analyzer had picked. The spill prologue wrote the param to the codegen's private address, the inline asm read from the analyzer's zero-page address, and nothing ever bridged the two — `LDA {param}` would silently load whatever the RAM clear left at the stale slot (always `0`). Fix: drop the `local_ram_next` loop and just look each local up in `allocations` by the analyzer's qualified name (`__local__{scope}__{local}`). The scope string that `gen_function` already computed for `substitute_asm_vars` is now shared with the new address-seeding loop via a `scope_prefix_for_fn(&str)` helper, so the two call sites can't drift. The analyzer's layout already satisfies the "no overlapping live locals" invariant the codegen was relying on — it scopes every local under `__local____` so two functions with a parameter named `x` land in different slots. Updated `gen_function_prologue_spills_params_to_local_ram`: the regression test for the War-era param clobbering bug was asserting the spill's destination specifically had to be an absolute address at `$0300+`. That's no longer the mechanism — the spill lands in whatever slot the analyzer assigned, which is zero page when there's room. The test now asserts the destination is *any* address outside `$04-$07`, which is the actual invariant. Reverted the `LDX $04` / `LDY $05` workaround in `examples/sha256/sha_core.ne` — every primitive there now uses `{dst}` / `{src}` / `{w_ofs}` / `{h_ofs}` / `{k_ofs}` substitution as originally intended. The "Parameter convention" comment that documented the workaround is gone. Regenerated `tests/emulator/goldens/inline_asm_demo.png`: that example's `times_four(input)` was previously returning `input` verbatim because the inline asm's `LDA {result}` / `ASL A` / `ASL A` / `STA {result}` operated on a zero-page byte that was disconnected from the NEScript-level `result` variable. With the fix, `times_four` correctly returns `input * 4`, so the smiley-tracker's frame-180 position shifts by the expected `(frame_count * 4) mod 256` delta. The other 33 ROMs remain byte-identical. Verified: - `cargo clippy --all-targets -- -D warnings` clean on both rustc 1.94.1 and 1.95.0. - `cargo test --all-targets`: 616 + 3 + 75 tests pass. - `cargo fmt --check` clean. - Full emulator harness: 34/34 ROMs match goldens. - SHA-256 of "NES" still computes to `AE9145DB5CABC41FE34B54E34AF8881F462362EA20FD8F861B26532FFBB84E0D`. - `--memory-map` output now reflects what the generated code actually reads and writes (previously the codegen's $0300+ override was invisible to the dump). https://claude.ai/code/session_01FRmSBruVWCufm3LsUVMs8v --- compiler-bugs.md | 146 +++++++++---------- examples/inline_asm_demo.nes | Bin 24592 -> 24592 bytes examples/sha256.nes | Bin 24592 -> 24592 bytes examples/sha256/sha_core.ne | 67 ++++----- src/codegen/ir_codegen.rs | 157 +++++++++++---------- tests/emulator/goldens/inline_asm_demo.png | Bin 846 -> 846 bytes 6 files changed, 179 insertions(+), 191 deletions(-) diff --git a/compiler-bugs.md b/compiler-bugs.md index a1ccfdc..eccec76 100644 --- a/compiler-bugs.md +++ b/compiler-bugs.md @@ -35,10 +35,13 @@ ## #1 — inline `asm { {param} }` resolves to an address nothing writes to -**Status**: WORKED-AROUND (every SHA-256 primitive in -`examples/sha256/sha_core.ne` reads parameters straight out of the -caller's `$04`/`$05` transport slots instead of using `{dst}` / -`{src}`) +**Status**: FIXED in `src/codegen/ir_codegen.rs::IrCodeGen::new` +(the codegen now reads each function-local's address out of the +analyzer's `VarAllocation` table instead of minting its own +parallel `$0300+` range). The workaround in +`examples/sha256/sha_core.ne` — reading parameters directly from +the `$04`/`$05` transport slots — has been reverted in the same +commit. **Phase**: codegen (prologue spill vs. inline-asm resolver disagree on local addresses) **Surfaced in**: `examples/sha256/sha_core.ne` — the 20-odd @@ -206,96 +209,75 @@ So `times_four(x)` actually returns `x`, not `x * 4`. The committed golden for that example reflects the bug rather than the intended `×4` behaviour. -### Workaround (applied in `examples/sha256/`) +### How it was fixed -Every primitive in `sha_core.ne` reads its parameters straight -out of the transport slots `$04` / `$05` with the raw literal: - -```ne -fun cp_wk(dst: u8, src: u8) { - asm { - LDX $04 ; == dst on entry - LDY $05 ; == src on entry - LDA {wk},Y - STA {wk},X - ; ... 3 more 4-byte iterations ... - } -} -``` - -This works because: - -1. The analyzer's function prologue at the AST level doesn't do - anything with the inline-asm block's contents — it's a raw - text token. -2. The codegen's spill prologue copies `$04`/`$05` → the codegen - local but **leaves the originals alone**. So the transport - slots still hold the argument when the first instruction of - the asm block executes. -3. None of the primitives `JSR` from inside the `asm { ... }` - block, so nothing else re-enters the function's body (or any - other function) while the inline block is running, which - would re-populate `$04`/`$05` with different arguments. - -The file has a big comment (`── Parameter convention ──`) -explaining exactly this. Every primitive in that file starts -with `LDX $04` (and if it has two params, `LDY $05`) instead of -`LDX {dst}` / `LDY {src}`. - -### Once the compiler is fixed - -Revert every `LDX $04` / `LDY $05` in `examples/sha256/sha_core.ne` -back to `LDX {dst}` / `LDY {src}` / `LDX {h_ofs}` / …, and delete -the "Parameter convention" comment. Also consider whether -`examples/inline_asm_demo.ne` should be updated so `times_four` -actually produces the documented `×4`, and regenerate -`tests/emulator/goldens/inline_asm_demo.png` in the same commit — -the current golden encodes the buggy behaviour. - -### Guess at the fix - -Two equivalent options, each about 10 lines of code: - -**(a) Make the codegen use the analyzer's allocation for -locals.** Drop the `local_ram_next` loop at the top of -`Emitter::new` and, instead of minting new addresses, look up -each local's analyzer key and copy its address into -`var_addrs`: +Option (a) from the original writeup: `IrCodeGen::new` now looks +each function-local's address up in the analyzer's +`VarAllocation` table instead of minting a parallel `$0300+` +range. The codegen and the inline-asm resolver consequently +agree on every local's address, so `{dst}` / `{src}` / … inside +`asm { ... }` blocks resolve to the same slot the NEScript-level +code reads and writes. ```rust +// Was: +let mut local_ram_next: u16 = 0x0300; +// ... for func in &ir.functions { for local in &func.locals { - let qualified = /* __local____ */; - if let Some(a) = allocations.iter().find(|a| a.name == qualified) { - var_addrs.insert(local.var_id, a.address); - var_sizes.insert(local.var_id, a.size); + var_addrs.insert(local.var_id, local_ram_next); + var_sizes.insert(local.var_id, local.size); + local_ram_next += local.size.max(1); + } +} + +// Is now: +for func in &ir.functions { + let scope = scope_prefix_for_fn(&func.name); + for local in &func.locals { + let qualified = format!("__local__{scope}__{}", local.name); + if let Some(alloc) = allocations.iter().find(|a| a.name == qualified) { + var_addrs.insert(local.var_id, alloc.address); + var_sizes.insert(local.var_id, alloc.size); } } } ``` -The analyzer already picks slots that are stable across -functions (the `__local__fn__name` prefix avoids collisions and -it allocates from zero page first, which is faster anyway), so -the codegen's "grow linearly from $0300" policy isn't actually -buying anything — and the comment in `ir_codegen.rs` explaining -why it's safe to stack locals was already relying on the same -"no recursion, bounded call depth" guarantees the analyzer -enforces. The analyzer's allocations already satisfy them. +The same commit: -**(b) Make `substitute_asm_vars` use the codegen's -`var_addrs`.** Pass `self.var_addrs` (plus the VarId map) into -the resolver instead of `self.allocations`. Same effect — both -maps agree after this — and arguably more local to the bug. The -analyzer's allocations stay as they are. +- factors the "function name → analyzer scope prefix" mapping + (`_frame` / `_enter` / `_exit` / `_scanline_N` / bare name) + into a `scope_prefix_for_fn(&str) -> String` helper and + reuses it in `gen_function` so the two sites can't drift; +- updates `gen_function_prologue_spills_params_to_local_ram` + (the regression test originally guarding the War-era param + clobbering bug) to assert the spill's destination is *any* + address outside `$04-$07`, not specifically `$0300+`. The + invariant that matters is "separate from the transport slots", + which holds for the analyzer's zero-page allocations too; +- reverts the `LDX $04` / `LDY $05` workaround across every + primitive in `examples/sha256/sha_core.ne` back to the + intended `LDX {dst}` / `LDY {src}` substitution form, and + drops the "Parameter convention" note from the top of the + file; +- regenerates `tests/emulator/goldens/inline_asm_demo.png`: + that example's `times_four` previously returned its input + verbatim (the inline asm operated on an unrelated zero-page + byte that was always `0`), so the golden's smiley position + drifted by exactly the expected `x * 4 mod 256` delta at + frame 180. -Preferred: (a) — it deletes code instead of rerouting it, and -it makes the memory map dumped by `--memory-map` truthful again -(the codegen's override was invisible to `--memory-map`, which -is why the discrepancy above looks puzzling without this writeup). +Verified after the fix: -Once either change is in, re-run the full emulator harness. The -`inline_asm_demo` and `sha256` goldens will need fresh captures -because both change observable output. +- `cargo test --all-targets` — 616 + 3 + 75 tests pass on + both rustc 1.94.1 and 1.95.0. +- `cargo clippy --all-targets -- -D warnings` clean on both. +- Full emulator harness — 34/34 ROMs match their goldens + (only `inline_asm_demo.png` changed, and the new capture + reflects the corrected `×4` behaviour). +- The SHA-256 example still computes `AE9145DB…4E0D` for the + auto-demo input `"NES"`, matching `shasum` byte-for-byte, + with the inline-asm-pretty `{dst}` / `{src}` primitives. --- diff --git a/examples/inline_asm_demo.nes b/examples/inline_asm_demo.nes index a08ef19edeeb14f9907d464533d8f07366512948..7c04802304af5cc52533ed3be1781fd65e3d7eca 100644 GIT binary patch delta 46 zcmV+}0MY-DzyXlJ0kAs(5D-ir!KDO+6QvV{6bcH36thzSw?30F0tf}c!2rMkvkyQ4 EAZ7dxO#lD@ delta 50 zcmV-20L}l9zyXlJ0kAs(7#K_*!KDO^00XT61C0R#r4$MZg%qs;1G7s3w>|~Q!2rMs IvkyQ4AdFTH>Hq)$ diff --git a/examples/sha256.nes b/examples/sha256.nes index fa9d711653b5e3c95772583ffb3be5630698abb5..af7289fc228368e3bce467626eae7f5ea0cba50c 100644 GIT binary patch delta 3181 zcmZu!YfK#172eqg`@qKQ)FHFW0@uXYU@#8{lf}U?#81e?S!2h>aZJ`G+n88tD^==` zs_HC8_h0K0jpWFZkTS-*)L2Fg2*}>Qb460J+SLLoit<#K&`PiQ0^yIYqr%mABgT+f^Xl~j%!=C4a5GWrKKMyeA8?G$fG^m zZu~q@Kf-rI@dfb$?+_hU;D*n1X@AO_N^!H;&Alw07aj5C0PoN~hh@2nLCJVTj0(vZ zhIzX~Tg;Q_p6nh(8+tlgXSUPm4nD8*7ZEKk;=D_AInj<;GTMuXR$+-Qiq@s|!di*; zJz`V{V*H1qb!q1;UiwYKi;|b}-+U)(H>mrv-R{gj<6ps+5Puot{n`DQ zmyPyNrLjMRUFZSSo9)T;8mB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s^~Q4;4kYF)Ew_7U(B7`r|aO~x*e&qeZSSIdj`JbIOy`30(~ z2j{HeiErbmeFgC-P{i1^2<&4~^(EjRQ`Mh{D)&&8d)Q^*m&7>jJwvPTDe%iw(5HYG z*F~yoT|V>Em0z$I7QuQL!!4W#>z;|EYmj(fj9tOx`o$aYGrZCK=n!q@j)L6G_(A>| z44QbA7UM~rm0O*bv%_&WPB*Qo(_$?FU#jfaJ|e8dv7E-BOEy@)FW{Mq)4?*Fn3Hf? zowlUgikk9M$TT?xhFgnmZkxYRq%ltr-9T*^;3^0*rKg?{o6$Gvs_lW&MmJU!X~7xn zS9LgMZ9L(HoETn7t)9xRVtheYOY4d;bdaBqzD{%sQ00|rVN^A$sz0iz8rI_{@IEjq zs%5s0q6YFoWc{a#Xf4gATvMg*nA-~Mf7mqCK-LH`gXWJ57EE$P<#9YsXq#AVxiSj` zL_F2e%;F?|*pzJC+HV!`B`V4*C(>P7fogSNv~;T*x2wh6G5LA5I<{GAGJm@)!LNd^ ziez__UkKMKVdW;>gnzA{O86zAfnoxIErRwG0Ugy|@o2c#`=%|^y-Pmsm5*vg=7BtJ zdQ2$J$T%ue?#W}dX6LAx95t85Fmy}OZSk7~O`SsFJKYw%SyCQe04a72^I=MmD?wiu zl$(hX$2pYSzAk(GVhA~vrg4XvcYUbu~{NSjWPt(5zSQq}K2400Pq}HV9?JYyJaIZ{d z)HOG5pCnQ2`f6{_5^x(fOLqxhM%I-nG|dLZNx3NjG)^NBEd$N8Rrn8!d8k0q?w IrCodeGen<'a> { } } // Map every function-local — parameters AND body-declared - // vars — into a dedicated RAM slot at `$0300+`. Parameters - // are still passed via the zero-page transport slots + // vars — into the slot the analyzer already reserved for it. + // Parameters arrive via the zero-page transport slots // `$04-$07` as the calling convention, but `gen_function` // emits a prologue at function entry that copies those - // transport slots into these per-function RAM slots. That - // way, when a function makes a nested call, the nested - // call clobbers `$04-$07` (writing its own arguments into - // them) without disturbing the caller's saved parameters. + // transport slots into the analyzer's per-function slot so + // nested calls don't step on the caller's parameters. // - // Before this change, parameters lived in `$04-$07` for the - // duration of the function body, so any call nested inside - // a function's body silently corrupted the caller's - // parameters (fixed on the War bug-cleanup branch; see - // `git log` for the original reproduction and root cause). - // The per-function RAM slots + prologue spill fix that - // class of bug at the cost of 4 LDA/STA pairs per function - // entry. + // NEScript forbids recursion (E0402) and caps call depth + // (E0401), so the analyzer's single-slot-per-local layout + // can't alias even though two functions may be active on + // the 6502 stack at once. // - // Locals are laid out linearly across every function: - // NEScript forbids recursion (E0402) and enforces a - // bounded call depth (E0401), so lifetime overlap between - // functions is fine and we don't need to pack them. - let mut local_ram_next: u16 = 0x0300; - // Advance past any RAM global so locals don't clobber them. - // Each global occupies `[address, address + size)` — for an - // array global at $0308 with size=4 that's $0308..$030C. We - // must advance past the END, not the base, otherwise - // subsequent locals overlap with the tail of the array. - // Globals are looked up by name against the analyzer's - // `allocations` (which has per-global sizes) rather than the - // `var_addrs` map, which only stores base addresses. - let max_ram_global_end = allocations - .iter() - .filter(|a| a.address >= 0x0100) - .map(|a| a.address + a.size.max(1)) - .max() - .unwrap_or(0); - if max_ram_global_end > local_ram_next { - local_ram_next = max_ram_global_end; - } + // Using the analyzer's addresses here (instead of minting a + // fresh linear `$0300+` range) is critical for inline-asm + // `{name}` substitution: `substitute_asm_vars` resolves + // `{param}` against `allocations` (= the analyzer's table), + // so the codegen has to agree with the analyzer on each + // local's address or `LDA {param}` inside `asm { ... }` + // would read a slot nothing ever writes to. See + // `compiler-bugs.md` entry #1 for the full diagnosis. for func in &ir.functions { + let scope = scope_prefix_for_fn(&func.name); for local in &func.locals { - var_addrs.insert(local.var_id, local_ram_next); - var_sizes.insert(local.var_id, local.size); - local_ram_next += local.size.max(1); + let qualified = format!("__local__{scope}__{}", local.name); + if let Some(alloc) = allocations.iter().find(|a| a.name == qualified) { + var_addrs.insert(local.var_id, alloc.address); + var_sizes.insert(local.var_id, alloc.size); + } } } let function_names = ir.functions.iter().map(|f| f.name.clone()).collect(); @@ -852,24 +835,7 @@ impl<'a> IrCodeGen<'a> { // their locals. For regular user functions it's just // the function name. See the commentary on // `current_fn_scope_prefix` above. - self.current_fn_scope_prefix = if let Some(state) = func.name.strip_suffix("_frame") { - format!("{state}__frame") - } else if let Some(state) = func.name.strip_suffix("_enter") { - format!("{state}__enter") - } else if let Some(state) = func.name.strip_suffix("_exit") { - format!("{state}__exit") - } else if let Some(rest) = func.name.strip_prefix("") { - // Scanline handlers encode the line number, but - // the analyzer's prefix is - // `{state}__scanline_{N}` — check the split. - if let Some((state, line)) = rest.rsplit_once("_scanline_") { - format!("{state}__scanline_{line}") - } else { - rest.to_string() - } - } else { - func.name.clone() - }; + self.current_fn_scope_prefix = scope_prefix_for_fn(&func.name); self.emit_label(&format!("__ir_fn_{}", func.name)); @@ -2151,6 +2117,33 @@ enum Cmp16Kind { GtEq, } +/// Map an IR function name to the analyzer's scope prefix for its +/// locals. The analyzer registers every function-local under +/// `__local__{prefix}__{name}` — state handlers use +/// `{state}__{frame|enter|exit}` or `{state}__scanline_{line}`, +/// regular functions use the bare function name. Both +/// `IrCodeGen::new` (when seeding `var_addrs`) and `gen_function` +/// (when setting `current_fn_scope_prefix` for inline-asm +/// substitution) have to agree on the string used here, or +/// `{param}` references would resolve to a different address than +/// the one generated code reads and writes. +fn scope_prefix_for_fn(name: &str) -> String { + if let Some(state) = name.strip_suffix("_frame") { + format!("{state}__frame") + } else if let Some(state) = name.strip_suffix("_enter") { + format!("{state}__enter") + } else if let Some(state) = name.strip_suffix("_exit") { + format!("{state}__exit") + } else if let Some((state, line)) = name.rsplit_once("_scanline_") { + // Scanline handlers encode the line number in the + // function name; the analyzer's prefix joins them with + // a double underscore. + format!("{state}__scanline_{line}") + } else { + name.to_string() + } +} + /// Replace `{name}` tokens in an inline-asm body with the resolved /// hex address from the given resolver. Unknown names and malformed /// placeholders are passed through unchanged (the asm parser will @@ -3916,9 +3909,21 @@ fn gen_function_prologue_spills_params_to_local_ram() { // own arguments, silently corrupting the caller's params. // // Compile a function that takes `x: u8`, calls `helper(x)`, - // then uses `x` again. Verify the callee reads `x` from a - // RAM slot (absolute addressing at $0300+) rather than - // directly from `$04`. + // then uses `x` again. Verify that immediately after the + // `__ir_fn_caller` label, the codegen emits a spill + // `LDA $04 / STA ` where `` is the analyzer's + // dedicated address for the param — crucially, not $04 + // itself (which nested calls would clobber) and not + // $05/$06/$07 either. + // + // Earlier revisions of this test asserted `` had to + // be an absolute address at `$0300+`, reflecting a codegen + // that minted a fresh per-function RAM range. After + // `compiler-bugs.md` #1 — the inline-asm `{param}` + // resolution fix — the codegen reuses the analyzer's + // allocation, which can land in zero page when there's + // room. The invariant that matters is "separate from the + // transport slots", not "must be main RAM". use crate::parser; let src = r#" game "Test" { mapper: NROM } @@ -3943,14 +3948,17 @@ fn gen_function_prologue_spills_params_to_local_ram() { let mut codegen = IrCodeGen::new(&analysis.var_allocations, &ir); let insts = codegen.generate(&ir); - // Find the __ir_fn_caller label. Immediately after it, look - // for the spill pattern: `LDA $04 / STA `. + // Walk the instructions emitted for `caller` (up until the + // next function label) looking for the spill `LDA $04` / + // `STA ` pair. `` is accepted as either + // `ZeroPage(addr)` or `Absolute(addr)`, as long as `addr` + // is outside the `$04-$07` transport range. let caller_idx = insts .iter() .position(|i| i.mode == AM::Label("__ir_fn_caller".into())) .expect("caller function should be emitted"); let mut saw_lda_zp4 = false; - let mut saw_sta_abs = false; + let mut saw_sta_separate = false; for inst in &insts[caller_idx + 1..] { if let AM::Label(l) = &inst.mode { if l.starts_with("__ir_fn_") && l != "__ir_fn_caller" { @@ -3959,20 +3967,25 @@ fn gen_function_prologue_spills_params_to_local_ram() { } if inst.opcode == LDA && inst.mode == AM::ZeroPage(0x04) { saw_lda_zp4 = true; + continue; } if saw_lda_zp4 && inst.opcode == STA { - if let AM::Absolute(a) = inst.mode { - if a >= 0x0300 { - saw_sta_abs = true; - break; - } + let addr: u16 = match inst.mode { + AM::ZeroPage(a) => u16::from(a), + AM::Absolute(a) => a, + _ => continue, + }; + if !(0x04..=0x07).contains(&addr) { + saw_sta_separate = true; + break; } } } assert!( - saw_lda_zp4 && saw_sta_abs, - "caller function should open with `LDA $04 / STA ` \ - as the param-spill prologue — the param-clobbering fix is \ - not in effect" + saw_lda_zp4 && saw_sta_separate, + "caller function should open with `LDA $04` followed by \ + a `STA ` that spills the param out of the \ + transport slots — the param-clobbering fix is not in \ + effect" ); } diff --git a/tests/emulator/goldens/inline_asm_demo.png b/tests/emulator/goldens/inline_asm_demo.png index 80396f6854e81882fbfc5494c5ac20780a0c1b00..676f835f46552f3c5a82ed5892c8f3f832c3f6ca 100644 GIT binary patch delta 184 zcmX@dc8+a=J+q^!!A2)Z#(Kebzt8>ed(2U@tu4R&e)+oZGhf#}tD3*_Ui!P=d?3M6 zu;B5}Q|slPQd+YbF&-}Lfyzkte^~T?8*+GK4 zz=Dr|p0e+Mt__r~z2Er$$_LeZ*Jsw>yDs^IQ)=H4sN@IXKYa0zYUftk#Q%Iw(P27EJeFhEMAM@bca7bN~A|A9K`fkN31=@|oCped2~X avDf@R3oVwLtP#A;00f?{elF{r5}E*&Zf3Ut