Repository navigation
Fix #8389 - Make MIPS branch offsets relative to the instruction address ##asm - #27332
Merged
Merged
Conversation
…tion address ##asm The assembler encoded a branch target as if the branch were at address 0, so `beq t0, zero, 0x100` at 0x100 jumped forward and branches back to an earlier label could not be assembled. Encode the offset from the next instruction, and fail when it is misaligned or out of range. While labels are sized, a label defined further down is not known yet, so let encode report 4 bytes for a branch that cannot be resolved then.
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.
Description
Fixes #8389.
mips_iencoded a branch target astarget / 4 - 1(and0for targets up to 4), which is the offset only for a branch at address 0.mips_assemblealready getspcbut never used it for branches. So:beq t0, zero, 0x100at 0x100 jumped forward instead of to itself;The offset is now
(target - (pc + 4)) / 4, counted from the instruction after the branch and computed in 32 bits. A misaligned or out-of-range target now fails with the usual "Cannot assemble" error instead of being rounded.While labels are sized, a label defined further down is still unknown, so
encodereports the 4 bytes of every MIPS instruction in that pass instead of failing; otherwise every later label would be placed 4 bytes short.The issue's loop now assembles to
ffff0810feff0814, which disassembles to the same two branches back to 0.A numeric target is now an address, like the ones
pdprints and GNU as takes. Before, at any address other than 0 it acted as a distance from the branch:b 0x8at 0x100 went to 0x108.Testing
test/db/asm/mips_32(new) has assemble lines at different addresses forbeq,bne,bandbal. Four of them fail on master; the one for a branch at address 0 passes on master too.test/db/tools/rasm2covers the issue's backward loop, a forward label, and two targets that do not fit.db/tools/rasm2anddb/cmd/archs.sys/lint.py --allis clean.