gccllvmssa

Calling Conventions and Register Selection


I've been delving into compiler theory, and how do they really compile source code into machine code. Though, there is something that all papers about register selection seem to ignore, and I'm not understanding the reason for that. Most of the papers I've read solely care about the current function they are operating in, but they act like there is no function to be called within.

Compilers generally follow calling conventions, and some registers are calle saved and others caller saved, but they still tend to let arguments be passed through other registers and not only the stack. So, how do compilers ensure that register allocation within a function do not interfere with the designated registers of the calling convention?


Solution

  • Compilers ensure that by following the rules. When compiling a call, the compiler considers each register. If it's a caller-saved register whose value is used later, then the compiler saves it. (If it's caller-saved but the calling function doesn't use it, then saving isn't needed, etc.)

    At this point the compiler doesn't need to care about whether the register is used to pass anything to the called function, the only two things that matter is whether the register is caller-saved and used later.

    Callee-saved register have a similar test: Is the register modified while in the function? Yes? Then it has to be saved. Again, the test is independent of any use to pass parameters.