C calling convention

From llvm-mos
Jump to navigation Jump to search

The current calling convention is somewhat simplistic; it will be tuned for performance and size before the initial release of the compiler.

  • A, X, Y, C, N, V, Z and RS1 to RS12 (RC2 to RC28) are caller-saved. A function may freely overwrite any of these, and the function's callers have to just deal with it.
  • PC, S, D, I, RS0 (RC0 and RC1), and RS9 to RS15 (RC18 to RC31) are callee-saved. A function can use them freely, but before it returns it has to put them back exactly the way it found them, and the function's callers can rely on this behavior.
  • The bytes composing numeric arguments are passed individually in A, then X, then RC2 to RC15.
  • Pointers are assigned to imaginary register pairs, functioning as pointer registers (i.e., RS1=(RC1, RC2) to RS7=(RC14, RC15)).
  • If no registers remain available, values are passed through the soft stack.
  • Aggregate types (structs, arrays, etc.) are passed by pointer. The pointer is managed entirely by the caller, and may or may not be on the soft stack. The callee is free to write to the memory; the caller must consider the memory overwritten by the call. This is handled directly by Clang; LLVM itself should never see aggregates.
  • Aggregate types are returned by a pointer passed as an implicit first argument. The resulting function returns void. This is handled directly by Clang; LLVM itself should never see aggregates.
  • Variable arguments (those within the ellipses of the argument list) are passed through the stack. Named arguments before the variable arguments are passed as usual: first in registers, then stack. Note that the variable argument and regular calling convention differ; thus, variable argument functions must only be called if prototyped. The C standard requires this, but many platforms do not; their variable argument and regular calling conventions are identical. A notable exception is Apple ARM64.

For insight into the design of performant calling conventions, see the following work by Davidson and Whalley. By their convention, this plaftorm uses the "smarter hybrid" method, since LLVM performs both shrink wrapping and caller save-restore placement optimizations, while using both callee-saved and caller-saved registers when appropriate.

Our calling convention is roughly based on RISC-V, suggested after a discussion with one of their working group members.

Methods for Saving and Restoring Register Values across Function Calls: Software--Practice and Experience Vol 21(2), 149-165 (February 1991)