HNNewShowAskJobs
Built with Tanstack Start
Handling Long Branches(maskray.me)
34 points by ingve 4 days ago | 6 comments
  • netule3 days ago

    It looks like the author has renamed their blog post, and thus the link has changed: https://maskray.me/blog/2026-01-25-long-branches-in-compiler...

    • zdimension3 days ago |parent

      Related: https://www.w3.org/Provider/Style/URI

    • MaskRay2 days ago |parent

      I accidentally published handling-long-branches.html . I actually removed it quickly, but Google Search already got it....

      https://news.ycombinator.com/item?id=46759921 (Long branches in compilers, assemblers, and linkers), 2 days ago

  • stevekemp3 days ago

    It was only a couple of years ago since I wrote an assembly language program of my own and got hit by branching-limits on the Z80 processor.

    I did exactly the same things that were suggested in this article, either inverted conditionals, or had a thunk - essentially "jmp nextJump", where that jumped to the actual location.

    I sometimes spent a few hours shuffling code around to remove the longer jumps and re-order code in groups closer together to save individual bytes.

  • kccqzy3 days ago

    This is really useful! I only knew about -mcmodel during compilation but I didn’t know the linker would insert thunks for faraway branches. I would’ve thought that the linker would simply complain and ask you to recompile with a larger code model. Also interesting that different linkers (or the same linker on different architectures!) chose different thunk creation algorithms.

  • dmitrygr2 days ago

    long relative branches are even more fun. Not all PIC has a GOT. Sometimes you just need a chunk of code that can run anywhere in memory. For MIPS this can get hard beyond +/-32KB... Here is approximately the minimum thunk to allow branching to anywhere in 32 bit address space relatively to current location, where "offset" is addr of dst minus addr of this thunk minus 8

        BGEZAL  $zero, . + 8
        ADDIU   $at, $ra, 0
        LUI     $t7, hi16(offset)
        ORI     $t7, $t7, lo16(offset)
        ADDU    $t7, $ra, $t7
        JR      $t7
        ADDIU   $ra, $at, 0