← Back to blog

AArch64 Calling Convention

This is a personal study note — I write these to solidify my own understanding. If you spot anything wrong or have thoughts, feel free to reach out. Layout, formatting, and grammar assisted by Claude.


Passing arguments

Returning values

integer
vector
memory
1stx0
2ndx1
1std0
2ndd1
caller's buffer, address inx8
long
value
double
value
__int128
low
high
struct { long a; long b; }
a
b
struct { double a; double b; }
a
b
struct { long a; double b; }
a
b
struct { double a; long b; }
a
b
struct { long a; long b; long c; }
a
b
c

Structs

void f(struct S s);
layout
integer
vector · HFA only
memory
struct S
bytes0–7
bytes8–15
1stx0
2ndx1
1stv0
2ndv1
3rdv2
4thv3
caller's copyat [x0]
{ long a; long b; }
a
b
a
b
{ double a; double b; }
a
b
a
b
{ long a; double b; }
a
b
a
b
{ int a; double b; }
a
b
a
b
{ int a; int b; int c; }
ab
c
ab
c
{ float a, b, c, d; }
ab
cd
a
b
c
d
{ int a; float b; }
ab
ab
{ long a; long b; long c; }
24 bytes
pointer
a b c

The stack: alignment, frame records, variadic calls

long caller(void) { return callee(1, 2, 3, 4, 5, 6, 7, 8); } long callee(long a, long b, long c, long d, long e, long f, long g, long h) { long local[3] = { a, b, c }; return leaf(local, d) + e + g + h; } long leaf(long *p, long i) { long t[4] = { p[0], p[1], p[2], p[0] + p[1] }; return t[i & 3]; }
saved x30 [sp+8] in caller saved x29 [sp] saved x19 [sp+72] in callee saved x20 [sp+64] padding: saved area to 16n [sp+56] saved x21 [sp+48] saved x30, return address [sp+40] saved x29 [sp+32] local[2] [sp+24] local[1] [sp+16] local[0] [sp+8] padding: sp to 16n [sp] t[3] [sp+24] in leaf t[2] [sp+16] t[1] [sp+8] t[0] [sp] HIGHER ADDRESSES LOWER ADDRESSES sp in caller · 16n x29 in callee sp in callee · 16n sp in leaf · 16n caller callee leaf
int printf(const char *fmt, ...);
long a;
double x, y;
x0
x1
d0
d1
printf("hi\n");
fmt
printf("%ld\n", a);
fmt
a
printf("%f\n", x);
fmt
x
printf("%ld %f %f\n", a, x, y);
fmt
a
x
y
struct __va_list {
    void *__stack;     // next argument on the stack
    void *__gr_top;    // end of the saved general registers
    void *__vr_top;    // end of the saved vector registers
    int   __gr_offs;   // offset from __gr_top to the next integer argument
    int   __vr_offs;   // offset from __vr_top to the next floating-point argument
};
// what va_arg(ap, long) does
if (ap.__gr_offs < 0) {
    value = *(long *)(ap.__gr_top + ap.__gr_offs);
    ap.__gr_offs += 8;
} else {
    value = *(long *)ap.__stack;
    ap.__stack += 8;
}

// what va_arg(ap, double) does
if (ap.__vr_offs < 0) {
    value = *(double *)(ap.__vr_top + ap.__vr_offs);
    ap.__vr_offs += 16;
} else {
    value = *(double *)ap.__stack;
    ap.__stack += 8;
}

registers · at the call

Who saves what

callee-savedcaller-saved
x0–x15
x16–x30, sp
v0–v15 · vector
v16–v31 · vector