Transpiler: support complex array index expressions#234
Merged
jonathanpeppers merged 5 commits intomainfrom Mar 18, 2026
Merged
Transpiler: support complex array index expressions#234jonathanpeppers merged 5 commits intomainfrom
jonathanpeppers merged 5 commits intomainfrom
Conversation
Add HandleLdelemU1ComplexIndex method that handles patterns like array[(x >> 3) + ((y >> 3) << 4)] where the index is computed by preceding arithmetic operations (shr, shl, add, sub, and, or, etc.). The method walks backward through IL using stack depth tracking to find the array instruction, removes array-load code from the block buffer, and emits TAX + LDA array,X to access the element using the computed index in the A register. Add two new tests: ComplexArrayIndexExpression and SimpleShiftArrayIndex. Co-authored-by: jonathanpeppers <840039+jonathanpeppers@users.noreply.github.com>
Add LSR, ASL, CLC assertions to verify the full expression is transpiled. Co-authored-by: jonathanpeppers <840039+jonathanpeppers@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Add support for complex array index expressions
Transpiler: support complex array index expressions
Mar 16, 2026
jonathanpeppers
approved these changes
Mar 17, 2026
Owner
jonathanpeppers
left a comment
There was a problem hiding this comment.
🤖 Code review complete — approving.
What I checked:
- Stack depth walk-back in
HandleLdelemU1ComplexIndexcorrectly locates the array instruction by tracking net IL stack contributions backward fromldelem.u1 - Block buffer removal logic properly cleans up dead array-load instructions (including
JSR pusha/pushaxfor ROM arrays), following the same pattern asHandleStelemI1 - State flags (
_runtimeValueInA,_savedRuntimeToTemp,_immediateInA,_lastLoadedLocalIndex) are all correctly set/cleared after emission GetLdcValuerefactor is a clean simplificationConv_u1/Conv_i1-family opcodes correctly treated as net-zero in the depth tracker- Both new tests pass and follow existing
RoslynTestshex-assertion conventions
Ran locally: All 509 tests pass (445 dotnes.tests + 64 analyzer tests).
Looks good to merge.
Owner
|
🤖 CI is green. All 509 tests pass locally. Ready for human review. |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR extends the IL→6502 transpiler to support ldelem.u1 when the array index is produced by a non-trivial expression (e.g., shifts/adds), which is a common pattern for NES tilemap lookups.
Changes:
- Added
HandleLdelemU1ComplexIndex()to locate the array load via backward IL stack-depth scanning and emitTAX+LDA array,X. - Simplified constant-index detection by using the existing
GetLdcValue()helper. - Added two Roslyn-based tests intended to validate opcode patterns for complex and shift-only index expressions.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
src/dotnes.tasks/Utilities/IL2NESWriter.ArrayHandling.cs |
Adds complex-index support for ldelem.u1 and refactors constant index detection. |
src/dotnes.tests/RoslynTests.cs |
Adds tests for complex array index emission patterns. |
- Preserve STA TEMP and JSR pusha/pushax operand-saving instructions during block removal in HandleLdelemU1ComplexIndex, so prior runtime values survive across complex index computation - Remove unconditional _savedRuntimeToTemp = false; the flag now correctly reflects whether a TEMP save exists in the block - Replace single-byte hex assertions (4A, AA, BD) with multi-byte instruction sequences (4A4A4A, AABD, 0A0A0A0A) to eliminate false positives from operand bytes - Add ComplexArrayIndex_PreservesOperandInTemp test verifying that (x - y) + arr[(byte)(x >> 3)] preserves the subtraction result Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Resolve conflict in RoslynTests.cs: keep both complex array index tests and word static field tests from main. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.
HandleLdelemU1only supported simple array indices (local variable or constant). Complex index expressions likearray[(x >> 3) + ((y >> 3) << 4)]threw aTranspileException. This pattern is common in NES tile map lookups.Changes
IL2NESWriter.ArrayHandling.cs: AddedHandleLdelemU1ComplexIndex()— when the index is not a simple ldloc/ldc, walks backward through IL using stack depth tracking to locate the array instruction, removes dead array-load instructions from the block buffer viaBlock.RemoveAt, strips any state-dependentJSR pusha/pushaxleft by ROM array loads, then emitsTAX+LDA array,X.GetLdcValue()helper.RoslynTests.cs: Two new tests —ComplexArrayIndexExpression(full(x >> 3) + ((y >> 3) << 4)pattern) andSimpleShiftArrayIndex(x >> 3) — verifying LSR, ASL, CLC, TAX, and LDA absolute,X opcodes in output.Example
Previously required a workaround:
Now works directly:
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.