Skip to content

Latest commit

 

History

History
320 lines (274 loc) · 10.5 KB

CallingConventions.md

File metadata and controls

320 lines (274 loc) · 10.5 KB

x86

x86-64

Linux

Register System Linkage Caller Preserved1
RAX 1st int return value ✔️
RBX base pointer (optional)
RCX 4th int argument ✔️
RDX 3rd int argument; 2nd int return value ✔️
RSP stack pointer
RBP frame pointer (optional)
RSI 2nd int argument ✔️
RDI 1st int argument ✔️
R8 5th int argument ✔️
R9 6th int argument ✔️
R10 ✔️
R11 ✔️
R12
R13
R14
R15
XMM0 1st float argument; 1st float return value ✔️
XMM1 2nd float argument; 2nd float return value ✔️
XMM2 3rd float argument ✔️
XMM3 4th float argument ✔️
XMM4 5th float argument ✔️
XMM5 6th float argument ✔️
XMM6 7th float argument ✔️
XMM7 8th float argument ✔️
XMM8 ✔️
XMM9 ✔️
XMM10 ✔️
XMM11 ✔️
XMM12 ✔️
XMM13 ✔️
XMM14 ✔️
XMM15 ✔️

Windows

Register System Linkage Caller Preserved1
RAX return value ✔️
RBX
RCX 1st argument ✔️
RDX 2nd argument ✔️
RSP stack pointer
RBP frame pointer (optional)
RSI
RDI
R8 3rd argument ✔️
R9 4th argument ✔️
R10 ✔️
R11 ✔️
R12
R13
R14
R15
XMM0 1st float argument; return value ✔️
XMM1 2nd float argument ✔️
XMM2 3rd float argument ✔️
XMM3 4th float argument ✔️
XMM4 ✔️
XMM5 ✔️
XMM6
XMM7
XMM8
XMM9
XMM10
XMM11
XMM12
XMM13
XMM14
XMM15

1. The remaining registers are Callee Preserved.

IA32

For historical reasons, there exist many different calling conventions for IA32, the most popular of which are cdecl (which stands for C declaration, caller clean-up call), stdcall (callee clean-up call) and thiscall (either caller or callee clean-up call).

POWER

Scratch registers are not preserved across calls, while non-volatile registers are preserved by called functions.

Register System linkage
R0 Scratch
R1 System Stack Pointer
R2 Library TOC
R3 1st argument/return value
R4 2nd argument/low-order portion of 64-bit return values in 32-bit mode
R5-10 3rd-8th arguments; the 9th and above arguments are passed on the stack
R11 Scratch (frequently used as a temp for call target address)
R12 Scratch
R13 Non-volatile; OS dedicated (64-bit)
R14 Non-volatile
R15 Non-volatile
R16 Non-volatile
R17-31 Non-volatile
IAR Instruction Address Register (a.k.a PC or NIP on Linux)
LR Link Register (used to pass the return address to a caller)
CTR Count Register (used for calling a far/variable target)
VSR32-63 Vector-Scala Register
CR0 Condition Register (used by compare, branch and record-form instructions)
CR1-7 Condition Register (used by compare and branch instructions)
FP0 1st Floating Point argument / Floating point return value
FP1-7 2nd-8th Floating Point argument
FP8-13 9th-14th Floating Point argument
FP14-31 Non-volatile

Useful Links:

Z

zLinux

64-bit

Register System Linkage Callee Preserved
R0
R1
R2 parameter/return value
R3 parameter
R4 parameter
R5 parameter
R6 parameter ✔️
R7 ✔️
R8-R11 ✔️
R12 GOT ✔️
R13 literal pool ✔️
R14 return address
R15 stack pointer ✔️
F0 parameter/return value
F1
F2 parameter/return value
F3
F4 parameter/return value
F5
F6 parameter/return value
F7
F8-F15 ✔️

31-bit

Register System Linkage Callee Preserved
R0
R1
R2 parameter/return value
R3 parameter/return value
R4 parameter
R5 parameter
R6 parameter ✔️
R7 ✔️
R8-R11 ✔️
R12 GOT ✔️
R13 literal pool ✔️
R14 return address
R15 stack pointer ✔️
F0 parameter/return value
F1
F2 parameter/return value
F3
F4 ✔️
F5
F6 ✔️
F7
F8-F15

z/OS

Register System Linkage Callee Preserved
R0
R1 parameter/extended return value1
R2 parameter/extended return value1
R3 parameter/return value1
R4 biased DSA (address of stack frame minus 2048 bytes)
R5 address of callee environment or scope's stack frame (internal functions)
R6 entry point ✔️
R7 return address ✔️
R8-R11 ✔️
R12 CAA ✔️
R13 ✔️
R14 ✔️
R15 ✔️
F0 parameter/return value
F1
F2 parameter/return value
F3
F4 parameter/return value
F5
F6 parameter/return value
F7
F8-F15 ✔️

In XPLINK, GPR4 (the biased stack pointer) is not saved by the callee in storage. It's recalculated on callee return. So from the caller's POV, it's a preserved register.


  1. The XPLINK specification states that, in AMODE31, integral and 32-bit pointer data types are returned in GPR3, and 64-bit integral types are returned in GPR2 (high half) and GPR3 (low half). In AMODE64, all integral and pointer data types are returned in GPR3. Aggregate or packed decimal types are left-adjusted, and returned in GPR1, but can occupy GPR2 and GPR3 as well if the size of the data exceeds 4 bytes (or 8 bytes for AMODE64).

Useful links:

32-bit Arm

Linux

Scratch registers are not preserved across calls, while non-volatile registers are preserved by called functions. Usage of the VFP registers in this table assumes hard-float ABI.

Register System Linkage
R0 1st argument / return value
R1 2nd argument / return value
R2-3 3rd-4th arguments
R4-10 Non-volatile
R11 System Frame Pointer (FP)
R12 Scratch; Intra-Procedure-Call Register (IP)
R13 System Stack Pointer (SP)
R14 Link Register (LR)
R15 Program Counter (PC)
D0 1st floating point argument / floating point return value
D1-7 2nd-8th floating point arguments
D8-15 Non-volatile
D16-D31 Scratch1

  1. Unavailable in some VFP versions

AArch64 (64-bit Arm)

Linux

Scratch registers are not preserved across calls, while non-volatile registers are preserved by called functions.

Register System Linkage
R0 1st argument / return value
R1-7 2nd-8th arguments
R8-15 Scratch
R16-17 Scratch; Intra-Procedure-Call Registers (IP0-1)
R18 Scratch; Platform Register
R19-28 Non-volatile
R29 System Frame Pointer (FP)
R30 Link Register (LR)
SP System Stack Pointer
PC Program Counter (PC)
V0 1st floating point argument / floating point return value
V1-7 2nd-8th floating point arguments
V8-15 Non-volatile
V16-31 Scratch

RISC-V

Register System Linkage Callee Preserved
zero Zero
ra Return address
sp Stack pointer ✔️
gp Global pointer
tp Thread pointer
t0-t2 Temporary register
s0-s1 Callee-saved registers ✔️
a0 Argument / Return reg.
a1 Argument / Return reg.
a2-a7 Argument registers
s2-s11 Callee-saved registers ✔️
t3-t6 Temporary registers
ft0-ft7 Temporary registers
fs0-fs1 Callee-saved registers ✔️
fa0 Argument registers
fa1 Argument registers
fa2-fa7 Argument registers
fs2-fs11 Callee-saved registers ✔️
ft8-ft11 Temporary registers

Useful links: