Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does IA-32 have a non-intuitive caller and callee register saving convention?

The common calling conventions for IA-32 say:

• Callee-save registers
%ebx, %esi, %edi, %ebp, %esp
Callee must not change these.  (Or restore the caller's values before returning.)

• Caller-save registers
%eax, %edx, %ecx, condition flags
Caller saves these if it wants to preserve them.  Callee can freely clobber.

Why does this strange convention exist? Why not save all the registers before calling another function? Or have the callee save and restore everything with pusha/popa?

like image 269
Bruce Avatar asked Dec 01 '11 01:12

Bruce


People also ask

What is the advantage of using callee and caller saved registers?

Caller saved registers Callee saving has the advantage of keeping the total size of your code small (each method only contains one set of instructions to save registers). Note: You have to generate the register saving instructions before you know what registers need to be saved.

What are caller saved and callee-saved registers?

A caller-save register must be saved and restored around any call to a subprogram. In contrast, for a callee-save register, a caller need do no extra work at a call site (the callee saves and restores the register if it is used). Minimum stack size for a standard function?

What is the purpose of a caller saved register?

The callee-saved registers are used for local state of the caller that needs to preserved across further function calls.

Why do we have calling conventions?

A calling convention governs how functions on a particular architecture and operating system interact. This includes rules about includes how function arguments are placed, where return values go, what registers functions may use, how they may allocate local variables, and so forth.


2 Answers

Why would you want to write code to save registers in every function that you might not need? That would add extra code and extra memory writes to every single function call. It may not seem significant now, but back in the 80's when this convention was created it probably did matter.

And note that ia-32 doesn't have a fixed calling convention - what you list is only an external convention - ia-32 doesn't enforce it. If you're writing your own code you use the registers however you wish.

Also see the discussion History of Calling Conventions at the Old New Thing Blog.

When deciding which registers should be preserved by a calling convention, you need to balance the needs of the caller against the needs of the callee. The caller would prefer that all registers be preserved, since that removes the need for the caller to worry about saving/restoring the value across a call. The callee would prefer that no registers be preserved, since that removes the need to save the value on entry and restore it on exit.

If you require too few registers to be preserved, then callers become filled with register save/restore code. But if you require too many registers to be preserved, then callees become obligated to save and restore registers that the caller might not have really cared about. This is particularly important for leaf functions (functions that do not call any other functions).

like image 180
shf301 Avatar answered Oct 26 '22 10:10

shf301


A guess:

If the caller saves all registers it will still need after a function call, it wastes time when the called function doesn't modify all those registers.

If the callee saves all registers it changes, it wastes time when the caller didn't need the values in those registers again.

When some registers are saved by caller and some by callee, the compiler (or assembly programmer) can choose which kind to use depending on if the value is needed after the next function call.

like image 42
Baffe Boyois Avatar answered Oct 26 '22 12:10

Baffe Boyois