Evaluate constant-only compound oam_spr args at compile time#297
Merged
jonathanpeppers merged 2 commits intomainfrom Mar 20, 2026
Merged
Evaluate constant-only compound oam_spr args at compile time#297jonathanpeppers merged 2 commits intomainfrom
jonathanpeppers merged 2 commits intomainfrom
Conversation
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>
Contributor
There was a problem hiding this comment.
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 immediateLDAfor a compile-time-evaluated result. - Add
RoslynTests.OamSpr_ConstantOnlyCompoundArgto verify transpilation of anoam_sprcall 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. |
- 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>
jonathanpeppers
commented
Mar 20, 2026
Owner
Author
jonathanpeppers
left a comment
There was a problem hiding this comment.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When the OamSpr backward scan produces a compound expression with no source variable (
compoundLocalIdx < 0,compoundStaticFieldNamenull), 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 = 00 | N = N,0 ^ N = N0 + N = N,0 - N = -NThen add the outer
addConstand emit asLDA #result.Test
RoslynTests.OamSpr_ConstantOnlyCompoundArgverifies thatoam_spr(40, 50, 0x10, (byte)(0x40 | 0x80), 0)transpiles correctly with0xC0in the attr byte.Context
Discovered while getting the crypto sample (PR #211) to transpile. All 564 tests pass.