|
| 1 | +Calling Convention Summary |
| 2 | +========================== |
| 3 | + |
| 4 | +Below is a summary of the calling conventions used on macOS and iOS. |
| 5 | + |
| 6 | +The `ABI stability manifesto <../ABIStabilityManifesto.md>`_ gives more details |
| 7 | +on the use of the Swift error return and ``self`` registers, while `The Swift |
| 8 | +Calling Convention <CallingConvention.rst>`_ covers the specifics in more |
| 9 | +details. (The Swift ``self`` register is known in other documents as the |
| 10 | +"Context register".) |
| 11 | + |
| 12 | +x86-64 |
| 13 | +------ |
| 14 | + |
| 15 | +See `Apple x86-64 Documentation`_, `System V ABI AMD64 Processor Supplement`_. |
| 16 | + |
| 17 | +.. _Apple x86-64 Documentation: https://developer.apple.com/library/archive/documentation/DeveloperTools/Conceptual/LowLevelABI/140-x86-64_Function_Calling_Conventions/x86_64.html |
| 18 | +.. _System V ABI AMD64 Processor Supplement: https://www.uclibc.org/docs/psABI-x86_64.pdf |
| 19 | + |
| 20 | +Register usage |
| 21 | +^^^^^^^^^^^^^^ |
| 22 | + |
| 23 | ++-----------+----------------------------------+----------+----------+----------+ |
| 24 | +| Register | Purpose | C++ | ObjC | Swift | |
| 25 | ++===========+==================================+==========+==========+==========+ |
| 26 | +| ``rax`` | Return value; also, for varargs, | | | | |
| 27 | +| | number of ``xmm`` registers used | | | | |
| 28 | ++-----------+----------------------------------+----------+----------+----------+ |
| 29 | +| ``rbx`` | Callee-saved register | | | | |
| 30 | ++-----------+----------------------------------+----------+----------+----------+ |
| 31 | +| ``rdi`` | Integer argument 1 | ``this`` | ``self`` | | |
| 32 | ++-----------+----------------------------------+----------+----------+----------+ |
| 33 | +| ``rsi`` | Integer argument 2 | | ``_cmd`` | | |
| 34 | ++-----------+----------------------------------+----------+----------+----------+ |
| 35 | +| ``rdx`` | Integer argument 3 | | | | |
| 36 | +| | (2nd return value) | | | | |
| 37 | ++-----------+----------------------------------+----------+----------+----------+ |
| 38 | +| ``rcx`` | Integer argument 4 | | | | |
| 39 | +| | (3rd return value) | | | | |
| 40 | ++-----------+----------------------------------+----------+----------+----------+ |
| 41 | +| ``r8`` | Integer argument 5 | | | | |
| 42 | +| | (4th return value) | | | | |
| 43 | ++-----------+----------------------------------+----------+----------+----------+ |
| 44 | +| ``r9`` | Integer argument 6 | | | | |
| 45 | ++-----------+----------------------------------+----------+----------+----------+ |
| 46 | +| ``r12`` | Callee-saved register | | | Error | |
| 47 | +| | | | | return | |
| 48 | ++-----------+----------------------------------+----------+----------+----------+ |
| 49 | +| ``r13`` | Callee-saved register | | | ``self`` | |
| 50 | ++-----------+----------------------------------+----------+----------+----------+ |
| 51 | +| ``r14`` | Callee-saved register | | | | |
| 52 | ++-----------+----------------------------------+----------+----------+----------+ |
| 53 | +| ``r15`` | Callee-saved register | | | | |
| 54 | +| | (other platforms use as GOT ptr) | | | | |
| 55 | ++-----------+----------------------------------+----------+----------+----------+ |
| 56 | +| ``st0`` | Used to return ``long double`` | | | | |
| 57 | +| | values | | | | |
| 58 | ++-----------+----------------------------------+----------+----------+----------+ |
| 59 | +| ``st1`` | Used to return ``long double`` | | | | |
| 60 | +| | values | | | | |
| 61 | ++-----------+----------------------------------+----------+----------+----------+ |
| 62 | +| ``xmm0``- | Floating point arguments 1-8 | | | | |
| 63 | +| ``xmm7`` | (``xmm0``-``xmm3`` also used | | | | |
| 64 | +| | for return) | | | | |
| 65 | ++-----------+----------------------------------+----------+----------+----------+ |
| 66 | +| ``rsp`` | Stack pointer | | | | |
| 67 | ++-----------+----------------------------------+----------+----------+----------+ |
| 68 | +| ``rbp`` | Callee-saved register, | | | | |
| 69 | +| | used as frame pointer | | | | |
| 70 | ++-----------+----------------------------------+----------+----------+----------+ |
| 71 | + |
| 72 | +Stack frame |
| 73 | +^^^^^^^^^^^ |
| 74 | + |
| 75 | +On function entry, ``rsp+8`` is **16-byte aligned**, i.e. the start of the memory |
| 76 | +arguments is 16-byte aligned; the initial stack pointer is shown below as "entry |
| 77 | +``rsp``", but a typical non-leaf function will start by doing:: |
| 78 | + |
| 79 | + push %rbp |
| 80 | + mov %rsp, %rbp |
| 81 | + sub <local-size>, %rsp |
| 82 | + |
| 83 | +Frameless leaf functions, however, will often not set up the frame pointer, |
| 84 | +``rbp``, in which case they may refer to arguments relative to ``rsp`` instead. |
| 85 | + |
| 86 | ++---------------+---------------+------------------------+ |
| 87 | +| | ``rbp+8n+16`` | memory argument *n* | |
| 88 | +| | | | |
| 89 | +| | ... | ... | |
| 90 | +| | | | |
| 91 | +| | ``rbp+16`` | memory argument 0 | |
| 92 | ++---------------+-----------+---+------------------------+ |
| 93 | +| ↓ Current Frame | ↑ Previous Frame | |
| 94 | ++---------------+-----------+---+------------------------+ |
| 95 | +| | ``rbp+8`` | return address | |
| 96 | +| | | | |
| 97 | ++---------------+---------------+------------------------+ |
| 98 | +| entry ``rsp`` | ``rbp`` | previous ``rbp`` value | |
| 99 | ++---------------+---------------+------------------------+ |
| 100 | +| | ``rbp-8`` | | |
| 101 | +| | | | |
| 102 | +| | ... | local storage | |
| 103 | +| | | | |
| 104 | +| | ``rsp`` | | |
| 105 | ++---------------+---------------+------------------------+ |
| 106 | +| | ``rsp-8`` | | |
| 107 | +| | | | |
| 108 | +| | ... | red zone | |
| 109 | +| | | | |
| 110 | +| | ``rsp-128`` | | |
| 111 | ++---------------+---------------+------------------------+ |
| 112 | + |
| 113 | + |
| 114 | +ARM64 |
| 115 | +----- |
| 116 | + |
| 117 | +See `Apple ARM64 Documentation`_, `Procedure Call Standard for the Arm 64-bit Architecture`_. |
| 118 | + |
| 119 | +.. _Apple ARM64 Documentation: https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms |
| 120 | +.. _Procedure Call Standard for the Arm 64-bit Architecture: https://github.com/ARM-software/abi-aa/blob/main/aapcs64/aapcs64.rst |
| 121 | + |
| 122 | +Register usage |
| 123 | +^^^^^^^^^^^^^^ |
| 124 | + |
| 125 | ++----------+---------+-------------------------+----------+----------+----------+ |
| 126 | +| Register | Special | Purpose | C++ | ObjC | Swift | |
| 127 | ++==========+=========+=========================+==========+==========+==========+ |
| 128 | +| ``x0`` | | Integer argument 1 | ``this`` | ``self`` | | |
| 129 | +| | | (1st return value) | | | | |
| 130 | ++----------+---------+-------------------------+----------+----------+----------+ |
| 131 | +| ``x1`` | | Integer argument 2 | | ``_cmd`` | | |
| 132 | +| | | (2nd return value) | | | | |
| 133 | ++----------+---------+-------------------------+----------+----------+----------+ |
| 134 | +| ``x2``- | | Integer arguments 3-8 | | | | |
| 135 | +| ``x7`` | | (3rd-8th return values) | | | | |
| 136 | ++----------+---------+-------------------------+----------+----------+----------+ |
| 137 | +| ``x8`` | | Indirect result | | | | |
| 138 | +| | | location register | | | | |
| 139 | ++----------+---------+-------------------------+----------+----------+----------+ |
| 140 | +| ``x16`` | ``ip0`` | Scratch registers (used | | | | |
| 141 | ++----------+---------+ by dyld, can be used | | | | |
| 142 | +| ``x17`` | ``ip1`` | freely otherwise) | | | | |
| 143 | ++----------+---------+-------------------------+----------+----------+----------+ |
| 144 | +| ``x18`` | | RESERVED **DO NOT USE** | | | | |
| 145 | ++----------+---------+-------------------------+----------+----------+----------+ |
| 146 | +| ``x19`` | | Callee-saved register | | | | |
| 147 | ++----------+---------+-------------------------+----------+----------+----------+ |
| 148 | +| ``x20`` | | Callee-saved register | | | ``self`` | |
| 149 | ++----------+---------+-------------------------+----------+----------+----------+ |
| 150 | +| ``x21`` | | Callee-saved register | | | Error | |
| 151 | +| | | | | | return | |
| 152 | ++----------+---------+-------------------------+----------+----------+----------+ |
| 153 | +| ``x22``- | | Callee-saved registers | | | | |
| 154 | +| ``x28`` | | | | | | |
| 155 | ++----------+---------+-------------------------+----------+----------+----------+ |
| 156 | +| ``x29`` | ``fp`` | Frame pointer | | | | |
| 157 | ++----------+---------+-------------------------+----------+----------+----------+ |
| 158 | +| ``x30`` | ``lr`` | Link register | | | | |
| 159 | ++----------+---------+-------------------------+----------+----------+----------+ |
| 160 | +| ``sp`` | | Stack pointer | | | | |
| 161 | ++----------+---------+-------------------------+----------+----------+----------+ |
| 162 | +| ``v0``- | | Floating point/SIMD | | | | |
| 163 | +| ``v7`` | | arguments 1-8 | | | | |
| 164 | +| | | (also for return) | | | | |
| 165 | ++----------+---------+-------------------------+----------+----------+----------+ |
| 166 | +| ``v8``- | | Callee-saved registers | | | | |
| 167 | +| ``v15`` | | (**lower 64-bits only**)| | | | |
| 168 | ++----------+---------+-------------------------+----------+----------+----------+ |
| 169 | + |
| 170 | +Stack frame |
| 171 | +^^^^^^^^^^^ |
| 172 | + |
| 173 | +The stack pointer is **16-byte aligned**; on function entry, ``sp`` points at |
| 174 | +the location shown by "entry ``sp``" below. As with x86, frameless leaf |
| 175 | +functions may not set up ``fp``, in which case they will use ``sp`` relative |
| 176 | +accesses. |
| 177 | + |
| 178 | ++--------------+---------------+------------------------+ |
| 179 | +| | ``fp+8n+16`` | last memory argument | |
| 180 | +| | | | |
| 181 | +| | ... | ... | |
| 182 | +| | | | |
| 183 | +| | ``fp+16`` | memory argument 0 [1]_ | |
| 184 | ++--------------+------------+--+------------------------+ |
| 185 | +| ↓ Current Frame | ↑ Previous Frame | |
| 186 | ++--------------+------------+--+------------------------+ |
| 187 | +| entry ``sp`` | ``fp+8`` | saved ``lr`` | |
| 188 | +| | | (return address) | |
| 189 | ++--------------+---------------+------------------------+ |
| 190 | +| | ``fp`` | previous ``fp`` value | |
| 191 | ++--------------+---------------+------------------------+ |
| 192 | +| | ``fp-8`` | | |
| 193 | +| | | | |
| 194 | +| | ... | local storage | |
| 195 | +| | | | |
| 196 | +| | ``sp`` | | |
| 197 | ++--------------+---------------+------------------------+ |
| 198 | +| | ``sp-8`` | | |
| 199 | +| | | | |
| 200 | +| | ... | red zone | |
| 201 | +| | | | |
| 202 | +| | ``sp-128`` | | |
| 203 | ++--------------+---------------+------------------------+ |
| 204 | + |
| 205 | +.. [1] See Apple documentation, however. Unlike the official ARM64 ABI, we pack |
| 206 | + arguments, so this might also hold argument 1, argument 2 and so on. |
0 commit comments