Why ARM Says "Link Register Supports Quick Calls to Sheet Functions"

Recently, I have come across concepts of relaying links and sheet functions.

I understand that from the previous SO it reads that LR reports where the code was earlier at runtime. I also found out that a worksheet function is a function that ends with a hierarchy of function calls.

ARM wikipedia page says :

The link register supports quick calls to sheet functions.

Why is this statement true? I looked at ARMARM (Architecture Reference Guide) and the link registry information is minimal.

+7
assembly arm cpu-architecture instruction-set
source share
2 answers

In some architectures (for example, x86, x86_64), the return address of the function is always stored on the stack, and calling the function implies access to the main memory:

  • writing to the stack on call

  • reading from the stack on return.

In contrast, if your architecture / ABI can jump / return without using the main memory, and the parameters and returned values ​​of the called party can also be transferred to the registers, the call and return to / from the sheet functions can be performed without including main memory.

If the sheet function is simple enough, it can be performed without touching RAM:

int callee(int a, int b) { return a + b; } int caller(int a, int b, int c, int d) { return callee(a,b) + calle(c,d); } 

gives (each function is compiled separately clang -target arm-eabi -S -o- -O3 ):

 callee: add r0, r1, r0 bx lr caller: .save {r4, r5, r6, r10, r11, lr} push {r4, r5, r6, r10, r11, lr} .setfp r11, sp, #16 add r11, sp, #16 mov r4, r3 mov r5, r2 bl callee mov r6, r0 mov r0, r5 mov r1, r4 bl callee add r0, r0, r6 pop {r4, r5, r6, r10, r11, lr} bx lr 

Note that we can avoid memory access in the caller core and in calee .

+2
source share

The reason is that this means that when calling the sheet function, the return address does not need to be pushed onto the stack (since it is stored in the link registry). It is assumed that this will be faster than pushing the return address onto the stack, as in processors that do not have a link register (but he is not sure if it is actually faster).

However, there are situations where the leaf function must save data on the stack anyway. For example, a sheet function with many variables might need to use a stack to save them or to save a link register to free temporary registers.

+2
source share

All Articles