Transpiler: support multi-byte static fields (int/ushort)#238
Merged
jonathanpeppers merged 3 commits intomainfrom Mar 17, 2026
Merged
Transpiler: support multi-byte static fields (int/ushort)#238jonathanpeppers merged 3 commits intomainfrom
jonathanpeppers merged 3 commits intomainfrom
Conversation
- BuildStaticFieldSizes() reads field types from metadata signatures - PreAllocateStaticFields() allocates 2 bytes for ushort/short/int fields - HandleLdsfld emits LDA+LDX for word fields (16-bit load to A:X) - HandleStsfld emits STA+STX for word fields (16-bit store from A:X) - WordStaticFields tracked on LocalVariableManager and IL2NESWriter - StaticFieldAddresses setter accounts for word field sizes - Added wordfield test sample with ushort and int static fields Co-authored-by: jonathanpeppers <840039+jonathanpeppers@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix multi-byte static fields allocation in transpiler
Transpiler: support multi-byte static fields (int/ushort)
Mar 16, 2026
jonathanpeppers
approved these changes
Mar 17, 2026
Owner
jonathanpeppers
left a comment
There was a problem hiding this comment.
🤖 Reviewed and verified — this looks great.
What I checked:
- Field size detection:
BuildStaticFieldSizes()correctly reads field signatures viaDecodeFieldSize()and caps at 2 bytes. Skips non-static and RVA fields. - Address allocation:
PreAllocateStaticFieldsallocates sequential addresses with per-field sizes. The tuple return(addresses, wordFields, totalBytes)keeps all three pieces synchronized. - 16-bit emission:
HandleLdsfldemitsLDA addr/LDX addr+1and sets_ushortInAX.HandleStsfldhandles all four state combinations correctly (_ushortInAX,_runtimeValueInA,_immediateInA, fallback). - Zero-extension: Storing a byte into a word field correctly zeros the high byte.
- LocalCount: Main writer uses explicit
staticFieldBytes; method writers get correct offsets viamethodFrameOffsets(which starts frommainLocalCount). - ROM disassembly: Verified the wordfield ROM —
ldc_i4 300→LDX #$01, LDA #$2C, STA $0328, STX $0329✓, byte→word zero-extendSTA $0326, LDA #$00, STA $0327✓,conv_u1correctly discards X ✓. - All 509 tests pass locally.
Owner
|
🤖 CI is green. Ready for human review. |
Remove the opaque wordfield.debug.dll, wordfield.release.dll, and verified.bin in favor of four inline Roslyn tests that compile the C# source at test time: - WordStaticField_StoreImmediate: 16-bit immediate store - WordStaticField_LoadZeroExtend: byte-to-word zero extension - WordStaticField_LoadWord: 16-bit LDA+LDX load - WordStaticField_TwoByteAllocation: no address overlap Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Adds proper 16-bit (word) support for user-defined static fields in the dotnes transpiler so ushort/short/int/uint statics don’t overlap adjacent RAM allocations and can be loaded/stored as 16-bit values.
Changes:
- Allocate static fields by byte-size derived from metadata signatures (with
int/uintcapped to 16-bit for NES use). - Track which static fields are word-sized and emit 16-bit
ldsfld/stsfldsequences (A:low, X:high) including zero-extension when storing byte→word. - Add a
wordfieldtest sample (debug+release inputs) and snapshot to validate correct behavior.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| src/dotnes.tasks/Utilities/Transpiler.cs | Computes per-static-field sizes from metadata and pre-allocates shared addresses + WordStaticFields set. |
| src/dotnes.tasks/Utilities/LocalVariableManager.cs | Adds WordStaticFields tracking and uses it when advancing LocalCount for preallocated statics. |
| src/dotnes.tasks/Utilities/IL2NESWriter.StructFields.cs | Emits 16-bit load/store for word static fields and handles byte→word zero-extension on stores. |
| src/dotnes.tasks/Utilities/IL2NESWriter.cs | Forwards WordStaticFields through the writer API. |
| src/dotnes.tests/TranspilerTests.cs | Adds wordfield to snapshot test matrix (debug + release). |
| src/dotnes.tests/TranspilerTests.Write.wordfield.verified.bin | New ROM snapshot for the wordfield sample. |
| src/dotnes.tests/Data/wordfield.debug.dll | New debug input DLL for wordfield transpilation test. |
| src/dotnes.tests/Data/wordfield.release.dll | New release input DLL for wordfield transpilation test. |
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.
PreAllocateStaticFieldsallocated 1 byte per static field regardless of type, soint/ushortfields overlapped adjacent fields in zero-page memory.Changes
Transpiler.cs—BuildStaticFieldSizes()reads field types from metadata signatures viaDecodeFieldSize().PreAllocateStaticFields()now allocates 2 bytes forushort/short/int/uintfields and returns aWordStaticFieldsset alongside addresses.LocalCountinitialized from total byte count instead of field count.LocalVariableManager.cs—WordStaticFieldsHashSet (parallelingWordLocals).StaticFieldAddressessetter uses per-field size when computingLocalCount.IL2NESWriter.StructFields.cs—HandleLdsfldemitsLDA+LDXfor word fields, sets_ushortInAX.HandleStsfldemitsSTA+STXfrom A:X, or zero-extends byte values into the high byte.IL2NESWriter.cs—WordStaticFieldsforwarding property.Example
Test
Added
wordfieldsample withushortandintstatic fields validating correct allocation, 16-bit load/store, and zero-extension.Original prompt
📱 Kick off Copilot coding agent tasks wherever you are with GitHub Mobile, available on iOS and Android.