Skip to content

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

Open
jonathanpeppers wants to merge 1 commit intomainfrom
ushort-local-variables
Open

Support ushort[] local variables (auto lo/hi split)#442
jonathanpeppers wants to merge 1 commit 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.

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