greenhorn: fix JZ encoder bug and expand VM test suite#1
Open
SuperInstance wants to merge 1 commit intomainfrom
Open
greenhorn: fix JZ encoder bug and expand VM test suite#1SuperInstance wants to merge 1 commit intomainfrom
SuperInstance wants to merge 1 commit intomainfrom
Conversation
Comment on lines
+51
to
+62
| const idxToByteOffset = new Map<number, number>(); | ||
| let byteOffset = 0; | ||
| for (let i = 0; i < sizes.length; i++) { | ||
| idxToByteOffset.set(i, byteOffset); | ||
| byteOffset += sizes[i]; | ||
| } | ||
|
|
||
| // Build final label map with byte offsets | ||
| const labels = new Map<string, number>(); | ||
| for (const [name, instrIdx] of labelIndices) { | ||
| labels.set(name, idxToByteOffset.get(instrIdx) ?? instrIdx); | ||
| } |
There was a problem hiding this comment.
🔴 Label at end of program resolves to instruction index instead of byte offset
When a label is defined at the very end of the assembly (after all instructions, with no instruction following it), idxToByteOffset.get(instrIdx) returns undefined because the map only contains entries for indices 0 to sizes.length - 1. The fallback ?? instrIdx then incorrectly uses the raw instruction index as the byte offset, instead of the correct total byte size.
Reproduction demonstrating incorrect label resolution
For JMP end\nMOVI R0, 42\nend::
- Label
endmaps to instruction index 2 idxToByteOffsetonly has keys 0 and 1 (for 2 instructions)idxToByteOffset.get(2)→undefined→ falls back to?? 2- Correct byte offset should be 7 (3 bytes for JMP + 4 bytes for MOVI)
- JMP encodes target as byte 2, which points into the middle of its own instruction
Suggested change
| const idxToByteOffset = new Map<number, number>(); | |
| let byteOffset = 0; | |
| for (let i = 0; i < sizes.length; i++) { | |
| idxToByteOffset.set(i, byteOffset); | |
| byteOffset += sizes[i]; | |
| } | |
| // Build final label map with byte offsets | |
| const labels = new Map<string, number>(); | |
| for (const [name, instrIdx] of labelIndices) { | |
| labels.set(name, idxToByteOffset.get(instrIdx) ?? instrIdx); | |
| } | |
| const idxToByteOffset = new Map<number, number>(); | |
| let byteOffset = 0; | |
| for (let i = 0; i < sizes.length; i++) { | |
| idxToByteOffset.set(i, byteOffset); | |
| byteOffset += sizes[i]; | |
| } | |
| // Also map the end-of-program index so labels after the last instruction resolve correctly | |
| idxToByteOffset.set(sizes.length, byteOffset); | |
| // Build final label map with byte offsets | |
| const labels = new Map<string, number>(); | |
| for (const [name, instrIdx] of labelIndices) { | |
| labels.set(name, idxToByteOffset.get(instrIdx) ?? instrIdx); | |
| } |
Was this helpful? React with 👍 or 👎 to provide feedback.
Debug
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.
Bug fix: JZ/JNZ/JE/JNE/JL/JGE label resolution now uses bytecode offsets instead of instruction indices.
Added 108 tests (up from ~20) covering all opcodes, jumps, stack, flags, and complex programs.
Changes
JE labelandJE R0, labelsyntaxBug Details
Labels were resolved to instruction indices (0, 1, 2...) instead of byte offsets (0, 4, 8...). For variable-length instructions (MOVI=4 bytes, INC=2 bytes, HALT=1 byte), this caused jumps to land in the middle of instructions. The fix adds a size-estimation pass to convert instruction indices to byte offsets before final encoding.