Skip to content

Evaluate constant-only compound oam_spr args at compile time#297

Merged
jonathanpeppers merged 2 commits intomainfrom
fix-oamspr-constant-compound
Mar 20, 2026
Merged

Evaluate constant-only compound oam_spr args at compile time#297
jonathanpeppers merged 2 commits intomainfrom
fix-oamspr-constant-compound

Conversation

@jonathanpeppers
Copy link
Copy Markdown
Owner

When the OamSpr backward scan produces a compound expression with no source variable (compoundLocalIdx < 0, compoundStaticFieldName null), the transpiler threw an exception. This happens when oam_spr arguments contain constant-only compound expressions like (byte)(0x40 | 0x80).

Fix

Evaluate the binary op at compile time instead of throwing:

  • 0 << N = 0, 0 >> N = 0, 0 & N = 0, 0 * N = 0
  • 0 | N = N, 0 ^ N = N
  • 0 + N = N, 0 - N = -N

Then add the outer addConst and emit as LDA #result.

Test

RoslynTests.OamSpr_ConstantOnlyCompoundArg verifies that oam_spr(40, 50, 0x10, (byte)(0x40 | 0x80), 0) transpiles correctly with 0xC0 in the attr byte.

Context

Discovered while getting the crypto sample (PR #211) to transpile. All 564 tests pass.

When the backward scan produces a compound expression with no source
variable (compoundLocalIdx < 0, compoundStaticFieldName null), evaluate
the binary op at compile time instead of throwing. For example,
(byte)(0x40 | 0x80) in an oam_spr attr argument is now correctly
resolved to 0xC0.

Add RoslynTests.OamSpr_ConstantOnlyCompoundArg to verify.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings March 20, 2026 21:56
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes a transpiler crash in oam_spr argument handling by attempting to fold constant-only compound expressions produced by the OamSpr backward IL scan, and adds a Roslyn regression test for the scenario.

Changes:

  • Update EmitOamSprDecsp4() to handle compound args with no source local/static field by emitting an immediate LDA for a compile-time-evaluated result.
  • Add RoslynTests.OamSpr_ConstantOnlyCompoundArg to verify transpilation of an oam_spr call containing a constant-only compound expression.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/dotnes.tasks/Utilities/IL2NESWriter.OamSprites.cs Adds a constant-only compound-expression path when emitting oam_spr args via decsp4.
src/dotnes.tests/RoslynTests.cs Adds a regression test for constant-only compound oam_spr attr argument folding.

Comment thread src/dotnes.tasks/Utilities/IL2NESWriter.OamSprites.cs
Comment thread src/dotnes.tests/RoslynTests.cs Outdated
- Evaluate compoundAddConst BINOP compoundBinOpConst directly instead of
  assuming first operand is 0 (which gave wrong results for overlapping bits)
- Throw TranspileException for unsupported ops (Div/Rem) in constant path
- Test uses overlapping bits (0x41 | 0x43 = 0x43) to catch addition errors

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copy link
Copy Markdown
Owner Author

@jonathanpeppers jonathanpeppers left a comment

Choose a reason for hiding this comment

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

Both review comments addressed in 3cac358. Constant folding now correctly applies the binary op to both operands, and the test uses overlapping bits to distinguish OR from addition. All 564 tests pass.

@jonathanpeppers jonathanpeppers merged commit e3d3438 into main Mar 20, 2026
1 check passed
@jonathanpeppers jonathanpeppers deleted the fix-oamspr-constant-compound branch March 20, 2026 22:08
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