Skip to content

Latest commit

 

History

History
212 lines (189 loc) · 12.8 KB

CallingConventionSummary.rst

File metadata and controls

212 lines (189 loc) · 12.8 KB
orphan:

Calling Convention Summary

Below is a summary of the calling conventions used on macOS and iOS.

The ABI stability manifesto gives more details on the use of the Swift error return and self registers, while The Swift Calling Convention covers the specifics in more details. (The Swift self register is known in other documents as the "Context register".)

x86-64

See Apple x86-64 Documentation, System V ABI AMD64 Processor Supplement.

Register usage

RegisterPurposeC++ObjCSwift
raxReturn value; also, for varargs, number of xmm registers used   
rbxCallee-saved register   
rdiInteger argument 1thisself 
rsiInteger argument 2 _cmd 
rdxInteger argument 3 (2nd return value)   
rcxInteger argument 4 (3rd return value)   
r8Integer argument 5 (4th return value)   
r9Integer argument 6   
r12Callee-saved register  Error return
r13Callee-saved register  self
r14Callee-saved register  Async context
r15Callee-saved register (other platforms use as GOT ptr)   
st0Used to return long double values   
st1Used to return long double values   
xmm0- xmm7Floating point arguments 1-8 (xmm0-xmm3 also used for return)   
rspStack pointer   
rbpCallee-saved register, used as frame pointer   

Stack frame

On function entry, rsp+8 is 16-byte aligned, i.e. the start of the memory arguments is 16-byte aligned; the initial stack pointer is shown below as "entry rsp", but a typical non-leaf function will start by doing:

push %rbp mov %rsp, %rbp sub <local-size>, %rsp 

Frameless leaf functions, however, will often not set up the frame pointer, rbp, in which case they may refer to arguments relative to rsp instead.

 

rbp+8n+16

...

rbp+16

memory argument n

...

memory argument 0

↓ Current Frame↑ Previous Frame
 rbp+8return address
entry rsprbpprevious rbp value
 

rbp-8

...

rsp

local storage
 

rsp-8

...

rsp-128

red zone

ARM64

See Apple ARM64 Documentation, Procedure Call Standard for the Arm 64-bit Architecture.

Register usage

RegisterSpecialPurposeC++ObjCSwift
x0 Integer argument 1 (1st return value)thisself 
x1 Integer argument 2 (2nd return value) _cmd 
x2- x7 Integer arguments 3-8 (3rd-8th return values)   
x8 Indirect result location register   
x16, x17ip0, ip1Scratch registers (used by dyld, can be used freely otherwise)   
x18 RESERVED DO NOT USE   
x19 Callee-saved register   
x20 Callee-saved register  self
x21 Callee-saved register  Error return
x22 Callee-saved register  Async context
x23- x28 Callee-saved registers   
x29fpFrame pointer   
x30lrLink register   
sp Stack pointer   
v0- v7 Floating point/SIMD arguments 1-8 (also for return)   
v8- v15 Callee-saved registers (lower 64-bits only)   

Stack frame

The stack pointer is 16-byte aligned; on function entry, sp points at the location shown by "entry sp" below. As with x86, frameless leaf functions may not set up fp, in which case they will use sp relative accesses.

 

fp+8n+16

...

fp+16

last memory argument

...

memory argument 0 [1]

↓ Current Frame↑ Previous Frame
entry spfp+8saved lr (return address)
 fpprevious fp value
 

fp-8

...

sp

local storage
 

sp-8

...

sp-128

red zone
[1]See Apple documentation, however. Unlike the official ARM64 ABI, we pack arguments, so this might also hold argument 1, argument 2 and so on.
close