Skip to content

Implement IntPtr.Size and sizeof(nint) for the 6502#91

Merged
jonathanpeppers merged 3 commits intomainfrom
intptr-size
Mar 6, 2026
Merged

Implement IntPtr.Size and sizeof(nint) for the 6502#91
jonathanpeppers merged 3 commits intomainfrom
intptr-size

Conversation

@jonathanpeppers
Copy link
Copy Markdown
Owner

Fixes #33

The 6502 is an 8-bit CPU, so IntPtr.Size returns 1.

Two IL patterns are handled:

  • IntPtr.Size emits call IntPtr::get_Size() → handled in the Call switch as a compile-time constant (LDA #1)
  • sizeof(nint) emits the Sizeof IL opcode → new case in the int operand handler (LDA #1)

Both produce identical output: a single LDA # instruction.

Example usage

byte size = (byte)IntPtr.Size;     // size = 1 on NES
pal_col(0, size);

Tests added

  • IntPtrSize — verifies IntPtr.Size emits LDA #
  • SizeofNint — verifies sizeof(nint) emits LDA #

All 307 tests pass.

Copilot AI review requested due to automatic review settings March 6, 2026 01:03
Fixes #33

The 6502 is an 8-bit CPU, so IntPtr.Size returns 1. Two IL patterns
are handled:

- IntPtr.Size emits 'call IntPtr::get_Size()' -> handled in the Call
  switch as a compile-time constant (LDA #1)
- sizeof(nint) emits the Sizeof IL opcode -> new case in the int
  operand handler (LDA #1)

Both produce identical output: a single LDA #1 instruction.

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

This PR implements support for IntPtr.Size and sizeof(nint) in the dotnes transpiler, resolving issue #33. On the 6502 (an 8-bit CPU), the native integer size is 1 byte, so both expressions produce LDA #$01.

Changes:

  • Added a Sizeof IL opcode handler in IL2NESWriter.Write(ILInstruction, int) that emits LDA #1
  • Added an IntPtr.get_Size case in the Call handler in IL2NESWriter.Write(ILInstruction, string) that emits LDA #1
  • Added two Roslyn-based tests (IntPtrSize and SizeofNint) verifying the output contains A901 (LDA #$01)

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.cs Added Sizeof opcode case (line 1225) and IntPtr.get_Size call case (line 1762), both emitting WriteLdc(1)
src/dotnes.tests/RoslynTests.cs Added two test methods verifying that IntPtr.Size and sizeof(nint) produce the expected LDA #$01 instruction

jonathanpeppers and others added 2 commits March 5, 2026 19:09
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@jonathanpeppers jonathanpeppers merged commit 3038f99 into main Mar 6, 2026
1 check passed
@jonathanpeppers jonathanpeppers deleted the intptr-size branch March 6, 2026 01:11
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.

Feature Request: Implement IntPtr.Size

2 participants