Skip to content

Remove unused flag updates - #6

Merged
nathsou merged 7 commits into
mainfrom
remove-unused-flag-updates
Aug 24, 2026
Merged

nathsou merged 7 commits into
mainfrom
remove-unused-flag-updates

Conversation

@nathsou

@nathsou nathsou commented Aug 21, 2026

Copy link
Copy Markdown
Owner

Summary

  • add conservative flag liveness analysis over the existing basic-block CFG
  • emit flag-free instruction helpers when all written flags are dead
  • remove dead immediate comparisons and unused CLC/SEC updates
  • regenerate codegen output and mark the README checklist item complete

Validation

  • moon check src/main
  • make codegen
  • make hash (build succeeds; runtime hash requires the missing local smb.nes)
  • focused no-flag helper semantics test passes
  • native make build is blocked by the missing raylib static library in this checkout

@nathsou nathsou self-assigned this Aug 21, 2026
@nathsou nathsou closed this Aug 24, 2026
@nathsou nathsou reopened this Aug 24, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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 #imm and dead CLC/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_zpx has the same zero-page,X wraparound issue as inc_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.

Comment thread codegen/lib/instructions.c Outdated
Comment thread codegen/lib/instructions.h
Comment thread codegen/lib/instructions.c

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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

Comment thread src/lower/lower.mbt
@nathsou
nathsou merged commit 2143a91 into main Aug 24, 2026
2 checks passed
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.

2 participants