Link register

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by 217.205.218.246 (talk) at 14:22, 20 October 2016 (Thunks not using a link register do not necessarily put a return address on the stack (e.g. x86 jmp)). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

A link register is a special-purpose register which holds the address to return to when a function call completes. This is more efficient than the more traditional scheme of storing return addresses on a call stack, sometimes called a machine stack. The link register does not require the writes and reads of the memory containing the stack which can save a considerable percentage of execution time with repeated calls of small subroutines.

A link register is used in many instruction set architectures, such as the PowerPC, ARM, RISC-V and the PA-RISC. Other architectures (such as SPARC) have a register with the same purpose but another name (in this case, "output register 7" or o7).[1]

The usage of a link register allows for faster calls to leaf subroutines. When the subroutine is non-leaf, passing the return address in a register can still result in generation of more efficient code for thunks, e.g. for a function whose sole purpose is to call another function with arguments rearranged in some way. Other subroutines can benefit from the use of link register because it can be saved in a batch with other callee-used registers—e.g. an ARM subroutine pushes registers 4-7 along with the link register, LR, by the single instruction STMDB SP!, {R4-R7, LR} pipelining all memory writes required.

References

  1. ^ "RTEMS SPARC Applications Supplement" (PDF). May 2000. Retrieved 2013-04-19.