Remove unused flag updates - #6
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
There are confirmed correctness issues in the new instruction helpers (zero-page indexed wraparound for INC/DEC) and an inconsistent negative-flag definition between the new masked updater and the existing CPU helper that should be reconciled.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
This PR reduces unnecessary CPU-flag work in the generated C by adding a conservative carry/zero/negative liveness analysis over the existing basic-block CFG, then using that to (a) elide dead flag-only ops and (b) select flag-updating instruction helper variants only when needed. It also updates build/link settings to allow section GC (particularly helpful with the expanded helper surface area) and marks the corresponding README checklist item complete.
Changes:
- Add per-instruction CZ/N flag liveness tracking in lowering and expose
flag_live_after(id)for consumers. - Update transpilation to (1) omit dead
CMP/CPX/CPY #immand deadCLC/SEC, and (2) call flag-suffixed instruction helpers only for live written flags. - Regenerate / refactor instruction helpers into a mask-driven core with many exported entry points, plus build flags to allow GC of unused helpers.
File summaries
| File | Description |
|---|---|
| src/transpile/transpile.mbt | Adds helper-suffix selection based on liveness; elides dead flag-only instructions/updates during emission. |
| src/lower/lower.mbt | Assigns stable instruction IDs and computes conservative flag liveness over the BB CFG. |
| src/lower/asm_ast.mbt | Extends AsmItem::Inst to include an instruction ID; adds FlagLiveness utilities. |
| README.md | Marks “Remove unused flag updates” checklist item complete. |
| Makefile | Enables function/data sections and linker GC to strip unused helper variants. |
| codegen/lib/instructions.h | Hides helper symbol visibility (for wasm --export-all) and declares flag-suffixed helper variants. |
| codegen/lib/instructions.c | Refactors helpers into shared cores + flag-mask-driven variants and adds compare variants that preserve memory reads. |
Review details
Suppressed comments (1)
codegen/lib/instructions.c:284
dec_zpxhas the same zero-page,X wraparound issue asinc_zpx:(arg + x)should be truncated to 8 bits to model 6502 zero-page indexed addressing correctly.
DEFINE_READ_VARIANTS(dec_zp, uint8_t, (uint16_t)arg, dec_memory, FLAGS_NZ)
DEFINE_READ_VARIANTS(dec_zpx, uint8_t, (uint16_t)(arg + x), dec_memory, FLAGS_NZ)
DEFINE_READ_VARIANTS(dec_abs, uint16_t, arg, dec_memory, FLAGS_NZ)
DEFINE_READ_VARIANTS(dec_absx, uint16_t, arg + x, dec_memory, FLAGS_NZ)
- Files reviewed: 7/8 changed files
- Comments generated: 3
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
There was a problem hiding this comment.
🟡 Changes recommended
Flag liveness is computed before later lowering passes that can insert flag-relevant instructions, which can make flag_live_after(id) stale and lead to incorrect flag-update elision.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Review details
- Files reviewed: 7/8 changed files
- Comments generated: 1
- Review effort level: Lite
Summary
Validation
moon check src/mainmake codegenmake hash(build succeeds; runtime hash requires the missing localsmb.nes)make buildis blocked by the missing raylib static library in this checkout