Hacker Timesnew | past | comments | ask | show | jobs | submitlogin

> Why shouldn't the caller save all the registers it needs instead of relying on the good behavior of the callee?

You've got to rely on good behavior of the callee in general anyway. Calling conventions are part of the ABI and the caller and callee already need to agree on parameter passing (except for void(void) functions, which usually aren't much fun).

Picking which registers are callee saved and which are caller saved is roughly an optimization problem. Compilers don't have enough information to know what registers the callee (and its callees) actually use, so the caller can't save only the registers it has used that will definitely be trashed. Caller saving registers that the callee doesn't touch wastes code size, stack space, cpu time, and memory/cache bandwidth; same for callee saving registers the caller didn't need to be saved. So somebody puts together a convention that says these registers are super handy and most functions will want to use them --- those will be caller saved if necessesary. The other registers are deemed to be less likely to be used, callees will save them before using, just in case.

Syscalls may often have a different calling convention than library calls, because the needs are different.

There's some docs on syscall ABIs for Linux, but I didn't find one for x86, here's the docs for PPC though: https://www.kernel.org/doc/html/v6.2/powerpc/syscall64-abi.h...



Thank you, that helped me wrap my head around the concept!




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: