Skip to content

jmp_match: account for self-shrink on forward jcc/jmp rel8 check#239

Open
bboe wants to merge 1 commit into
netwide-assembler:masterfrom
bboe:jmp-match-forward-shrink
Open

jmp_match: account for self-shrink on forward jcc/jmp rel8 check#239
bboe wants to merge 1 commit into
netwide-assembler:masterfrom
bboe:jmp-match-forward-shrink

Conversation

@bboe
Copy link
Copy Markdown

@bboe bboe commented May 18, 2026

Forward jcc/jmp self-shrink case in jmp_match()'s borderline rel8 fit check was rejecting valid shrinks because it compared against the current-layout displacement instead of the post-shrink one. Regression test: travis/test/jccshrink.

The borderline rel8 fit test compared the current-layout short
displacement (target - offset - short_size). For a forward jump that
the previous pass emitted as NEAR, shrinking to SHORT moves the target
near_size - short_size bytes closer, so the post-shrink rel8 is that
many bytes smaller. NASM was keeping forward near jcc/jmp instances
that a self-shrink-aware iteration emits as short (a 4-byte shrink for
jcc32, 3-byte for jmp32).

Track per-jump previous-pass encoding in a static side table, swapped
at pass boundaries via _passn. In the borderline branch, when the
current-layout check rejects AND the previous pass was NEAR AND the
jump is forward, re-check against the post-shrink displacement. The
previous-pass guard prevents over-shrinking already-short jumps whose
displacement grew past 127; the forward guard skips backward jumps,
whose target doesn't move when the jump shrinks.

Add travis/test/jccshrink as a regression test: stock NASM emits 605 B
with a 5-byte near jmp at 0x1d9; patched emits 602 B with eb 7f
(rel8 = 127). make travis passes (301/301). Byte-identical on
optimization.asm, jmp64.asm, riprel.asm, a32offs.asm.

Signed-off-by: Bryce Boe <bbzbryce@gmail.com>
@bboe
Copy link
Copy Markdown
Author

bboe commented May 24, 2026

Bump.

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.

1 participant