Skip to content

Support ushort[] local variables (auto lo/hi split)#442

Merged
jonathanpeppers merged 5 commits intomainfrom
ushort-local-variables
Apr 14, 2026
Merged

Support ushort[] local variables (auto lo/hi split)#442
jonathanpeppers merged 5 commits intomainfrom
ushort-local-variables

Conversation

@jonathanpeppers
Copy link
Copy Markdown
Owner

Add transpiler support for ushort[] arrays, handling the IL opcodes C# emits for ushort array operations. This allows users to write ushort[] arr = new ushort[N] instead of manually managing separate byte[] lo and byte[] hi arrays with carry propagation.

New IL opcodes handled

  • newarr System.UInt16: pre-allocates count*2 bytes in zero page
  • stelem.i2: stores 16-bit values (constant/variable index, constant/runtime values)
  • ldelem.u2: loads 16-bit values into A:X (constant/variable index via AbsoluteY)
  • ldelema System.UInt16 + ldind.u2 + stind.i2: compound assignment (arr[i] += val)

6502 addressing strategy

  • Constant index: compile-time address computation (LDA base+i*2)
  • Variable index: uses Y register with AbsoluteY addressing (ASL; TAY; LDA base,Y)

Key details

  • Music note tables (newarr; dup; ldtoken; InitializeArray) are detected via look-ahead and excluded from the new handling
  • Pending array state tracks arrays during initialization (before stloc)
  • Includes ushortarray sample and test DLLs exercising newarr, constant-index stelem.i2, variable-index ldelem.u2, and variable-index stelem.i2
  • All 615 tests pass (551 dotnes.tests + 64 analyzers)

Fixes #422

Add transpiler support for ushort[] arrays, handling the IL opcodes C# emits
for ushort array operations. This allows users to write ushort[] arr = new
ushort[N] instead of manually managing separate byte[] lo and byte[] hi
arrays with carry propagation.

New IL opcodes handled:
- newarr System.UInt16: pre-allocates count*2 bytes in zero page
- stelem.i2: stores 16-bit values (constant/variable index, constant/runtime values)
- ldelem.u2: loads 16-bit values into A:X (constant/variable index via AbsoluteY)
- ldelema System.UInt16 + ldind.u2 + stind.i2: compound assignment (arr[i] += val)

Key implementation details:
- Constant index: compile-time address computation (LDA base+i*2)
- Variable index: uses Y register with AbsoluteY addressing (ASL; TAY; LDA base,Y)
- Music note tables (newarr; dup; ldtoken; InitializeArray) are detected via
  look-ahead and excluded from the new handling
- Pending array state tracks arrays during initialization (before stloc)

Includes ushortarray sample and test DLLs exercising newarr, constant-index
stelem.i2, variable-index ldelem.u2, and variable-index stelem.i2.

Fixes #422

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings April 3, 2026 21:50
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.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

…ng.cs

Keep both ushort array handling (ldelem.u2/i2) and new main features
(Endfinally handler, HandleMetaSpr2x2).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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

Copilot reviewed 8 out of 11 changed files in this pull request and generated 4 comments.

Comment thread src/dotnes.tasks/Utilities/IL2NESWriter.ILDispatch.cs Outdated
Comment thread src/dotnes.tasks/Utilities/IL2NESWriter.LocalVariables.cs
Comment thread src/dotnes.tasks/Utilities/IL2NESWriter.ArrayHandling.cs Outdated
Comment thread samples/ushortarray/Program.cs Outdated
jonathanpeppers and others added 3 commits April 13, 2026 17:46
- Delete samples/ushortarray/ and its pre-compiled test DLLs
- Remove TranspilerTests.Write ushortarray InlineData entries
- Add 4 RoslynTests: NewarrAndConstantStore, VariableIndexLoad,
  VariableIndexStore, LoadStoresIn16BitLocal
- Fix HandleLdelemU2 to handle implicit array ref on eval stack
  (Release compiler may never store array to a local variable)
- Update copilot-instructions.md to prefer RoslynTests over samples

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
… preserve value code

- Always route stelem.i2 to HandleStelemI2 (not byte fallback)
- Clear _pendingArrayType after HandleStlocAfterNewarr for both
  struct and ushort paths to prevent ldtoken misclassification
- Fix non-pending path in HandleStelemI2 to preserve runtime
  value-expression code (only remove array+index instructions)
- Merge main: resolve RoslynTests conflict (keep both ushort
  and oam_begin test methods)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Change frame_count from byte to ushort with a 360-frame threshold
(exceeds byte range of 255), demonstrating transparent ushort local
variable support. Update test DLLs, verified snapshot, and expected
local count (3 → 4 bytes).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@jonathanpeppers jonathanpeppers merged commit f6b1193 into main Apr 14, 2026
3 checks passed
@jonathanpeppers jonathanpeppers deleted the ushort-local-variables branch April 14, 2026 01:10
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.

Support ushort local variables transparently (auto lo/hi split)

2 participants