Calling conventions
A calling convention is the contract that governs how a caller and a callee transfer control and data. Which registers hold arguments, which registers survive a call, who cleans the stack, where the return value lands. Every platform we target specifies one, and our generated code has to respect it at every bl or call boundary.
The conventions for the three targets: AAPCS64 on macOS and Linux ARM64, System V AMD64 on Linux x86-64. The chapter walks through where each one surfaces in our code.
Why calling conventions matter to this compiler
The emitted code for an INTERCAL statement usually ends with one or more subroutine calls into the runtime: bl _rt_mingle, bl _rt_write_roman, bl _rt_read_out_array. Every one of those is a place where the calling convention applies. A codegen mistake that violates the convention, passing the wrong argument in the wrong register, clobbering a callee-saved register, imbalancing the stack, produces a runtime crash whose cause is not visible in the source program.
The runtime itself is also a collection of callees. Each _rt_* routine has to preserve the registers the convention says it must preserve, restore the stack pointer on exit, and return values in the right place.
Finally, the main entry point of a compiled program is itself a callee of the C runtime startup code that calls it. Getting main's prologue and epilogue wrong breaks every program.
AAPCS64. ARM64 on macOS and Linux
AAPCS64 is the ARM Architecture Procedure Call Standard for 64-bit. Both macOS and Linux follow it, with small platform-specific addenda.
Register roles
| Register | Role |
|---|---|
x0–x7 | Integer argument and return registers. Up to 8 arguments fit in registers; further arguments spill to the stack. x0 holds the primary return value; x0–x1 hold 128-bit returns. |
x8 | Indirect result location register, also used on Linux as the syscall number. |
x9–x15 | Caller-saved temporaries. The callee can clobber these freely. |
x16, x17 | Intra-procedure-call scratch, also the syscall number register on macOS (x16). |
x18 | Platform-reserved. Apple uses it; Linux user code generally does not. |
x19–x28 | Callee-saved. If a callee uses any of these it must restore them before returning. |
x29 | Frame pointer. |
x30 | Link register. bl writes the return address here. ret reads it. |
sp | Stack pointer. Must be 16-byte aligned at every call site. |
On macOS, our generated _main follows the convention: at entry, argc is in x0 and argv is in x1. On Linux, the _start / main contract is slightly different, libc wraps main: but we observe the same register contents.
What the codegen has to respect
- Pass INTERCAL-level expression results through
x0/w0. Everybl _rt_*that takes one argument places it inx0. - The NEXT stack helper
_rt_resume_1is called with the count inw0and does its own branch viabr x3(the target address popped from_next_stack). The helper is aware it is not a normal callee and bypasses the usual return convention. - The
_rt_mingle,_rt_selectand unary routines preservex0–x7only as documented; the codegen must save any value inx1–x7across them if needed. spmust be 16-byte aligned before everybl. The emitted code does not allocate per-statement stack frames, so this invariant is maintained by the runtime entry prologue, not by codegen.
Small AAPCS64 traps
x16changes role between macOS (syscall number) and Linux (intra-procedure scratch). Thesedpipeline inintercalc.shrewritesmov x16, #Nintomov x8, #Mwhen translating for Linux, along with the syscall number itself.stp/ldpare the load-pair and store-pair instructions; they move two registers (16 bytes) in one cycle. Forgetting to use these instead of pairs ofstr/ldris a common cost-of-prologue inefficiency. They also maintain 16-byte stack alignment implicitly: pushing FP+LR viastp x29, x30, [sp, #-16]!decrementsspby exactly 16 in one instruction, leaving the stack correctly aligned without a separatesub sp, sp, #16.- Floating-point arguments go in
v0–v7. Our compiler emits no floating-point code, so we never touch those registers. If we ever do, AAPCS64's full rules for mixed integer/float argument passing become relevant.
The canonical AAPCS64 prologue and epilogue
Most leaf-and-non-leaf functions in our runtime follow a small set of templates. The non-leaf canonical prologue is:
stp x29, x30, [sp, #-16]! ; push frame pointer and link register, sp -= 16
mov x29, sp ; set new frame pointer to current sp
And the matching epilogue:
ldp x29, x30, [sp], #16 ; pop FP and LR, sp += 16
ret ; return via x30
Two important properties:
- FP (
x29) and LR (x30) are always saved next to each other. This is what stack walkers (debuggers, profilers,__builtin_frame_address) depend on: walking the linked list of saved frame pointers and reading the previous LR at offset 8. - Callee-saved registers (
x19–x28,d8–d15) are pushed in pairs at higher offsets if the routine uses any of them. Our_rt_*routines mostly do not need callee-saved registers, so most prologues are just the FP/LR pair.
If a routine needs local stack space beyond the frame pointer save, it allocates after pushing FP+LR:
stp x29, x30, [sp, #-16]!
mov x29, sp
sub sp, sp, #N ; N must be a multiple of 16
; ... routine body ...
add sp, sp, #N
ldp x29, x30, [sp], #16
ret
Or, more compactly with a wider initial allocation:
stp x29, x30, [sp, #-(16+N)]! ; allocate FP/LR slot plus N locals in one go
mov x29, sp
; ... access locals at [sp, #16] through [sp, #(16+N-8)] ...
ldp x29, x30, [sp], #(16+N)
ret
Our _rt_write_roman uses the second form because it has a small stack-local buffer for the roman-numeral string. Most other _rt_* routines use the first form with N=0.
System V AMD64. Linux x86-64
The System V AMD64 ABI governs Linux, FreeBSD, macOS (for user code), and every x86-64 Unix. We target it directly for Linux x86-64; macOS on x86-64 is not a platform we support today.
Register roles
| Register | Role |
|---|---|
rdi, rsi, rdx, rcx, r8, r9 | Integer argument registers, in that order. |
rax, rdx | Return value registers (rax for 64-bit, rax:rdx for 128-bit). |
r10 | Used by the ABI as a static chain for nested functions; in our code, a caller-saved scratch. |
r11 | Caller-saved scratch. |
rbx, rbp, r12–r15 | Callee-saved. |
rsp | Stack pointer. Must be 16-byte aligned before a call instruction is executed; on entry to a function it is 8-byte-aligned-past-16 because call pushed an 8-byte return address. |
The alignment rule is a frequent source of bugs. The idiomatic prologue is push rbp; mov rbp, rsp; sub rsp, N for some 16-byte-multiple N, which restores 16-byte alignment at the first nested call.
What the codegen has to respect
codegen_x86_64.shplaces the first integer argument inrdi, notrax. This is the most common source of bugs when translating from ARM64 codegen (where the first argument isx0): the direct register correspondence isrdi↔x0.- Call sites must align the stack. The
codegen_*functions in the x86-64 backend allocate 16-byte frames for every statement that makes a call, to be safe. r12–r15are callee-saved. The runtime's_rt_sys666_openand_rt_sys666_writeuser14to hold a buffer size across a syscall, because a caller-saved register would not survive the syscall's internal register clobbering.- Syscalls on Linux x86-64 use the
syscallinstruction, with the syscall number inraxand arguments inrdi,rsi,rdx,r10,r8,r9(note:r10replacesrcx, becausesyscallclobbersrcx). This is almost the System V AMD64 convention but not identical,rcxis replaced byr10for syscalls specifically.
Small System V AMD64 traps
- The red zone: the 128 bytes below
rspare reserved by the ABI for leaf functions to use without adjusting the stack pointer. The reservation says signal and interrupt handlers must not modify this region, so leaf functions can store temporaries there safely. Our runtime uses this in a few places to avoid prologue overhead. Windows x64 does not have a red zone; if we ever target Windows, the optimisation has to be reverted. syscallvscall:syscalldoes not touch the stack. No return address is pushed. The kernel arranges the return via a privileged instruction.- Intel syntax destination-first, AT&T syntax source-first. Our emitted code is uniformly Intel. Mixing the two in the same
.sfile confuses GNUas.
Variadic functions
The System V AMD64 ABI requires that, when calling a variadic function (e.g. printf), the caller load %al (the low byte of %rax) with the number of vector registers used to pass arguments, typically zero for our generated code, since INTERCAL has no floating-point. The value need not be exact, only an upper bound. We never emit calls to variadic functions in our compiled output; the runtime calls libc write and read, which are not variadic. So %al is never set explicitly, and the convention does not bite us today.
Vector registers
%xmm0 through %xmm7 carry the first eight floating-point arguments. %xmm0 and %xmm1 carry floating-point return values. AVX2 widens these to %ymm0–%ymm15 (256-bit), AVX-512 to %zmm0–%zmm31 (512-bit). INTERCAL has no floats, so we touch none of these. They are mentioned only because future bridges to C libraries that take double arguments would have to follow the convention.
macOS ARM64 specifics
The macOS ABI is AAPCS64 with a few deviations:
_mainis the entry point symbol, notmain. The underscore is mandatory for every external symbol (this is a Mach-O convention, not strictly AAPCS64).- The Apple linker (
ld) is more lenient about.globaldeclarations than GNUld. We still emit.globalfor every top-level symbol; this is the intersection of what both linkers accept. - The syscall mechanism uses
svc #0x80with the syscall number inx16. No other ABI uses0x80.
These are all tabulated alongside the Linux equivalents in platforms.md.
Linux ARM64 specifics
Standard AAPCS64 with these corners:
- Syscalls via
svc #0(not#0x80), with the number inx8(notx16). This is a Linux ABI convention, not AAPCS64. openatis used instead ofopenbecause the kernel no longer exposesopen. The first argument isAT_FDCWD(-100), which shifts every subsequent argument's register by one.- Negative return values indicate errors. The check is
cmp x0, #0; b.lt error_handler, notb.cs.
Our callee discipline
Every _rt_* routine in the runtime files follows the same discipline, which is the intersection of AAPCS64 and System V AMD64:
- Preserve all callee-saved registers. If the routine needs any of
x19–x28/r12–r15, it saves them in the prologue. - Preserve the frame pointer and restore the stack pointer on exit. The prologue is
stp x29, x30, [sp, #-16]!on ARM64,push rbp; mov rbp, rspon x86-64. - Return via
ret(ARM64) orret(x86-64), which reads the return address from the link register or the stack respectively.
Several routines that use the syscall instruction internally save and restore rcx and r11 on x86-64, because those registers are clobbered by syscall. A bug that predated the current runtime assumed the compiler's caller code could rely on them; the fix was to make the syscall wrappers preserve them.
Worked example: bl _rt_write_roman
The generated code for DO READ OUT .1, after stripping the abstain check and label framing:
; ARM64
adrp x0, _spot_1@PAGE
add x0, x0, _spot_1@PAGEOFF
ldr w0, [x0] ; value now in w0 (zero-extended to x0)
bl _rt_write_roman ; call runtime
The bl is the instruction that enforces the convention. At this point, x0 holds the argument (the scalar value), x30 will be overwritten with the return address, and sp is 16-byte aligned.
Inside _rt_write_roman:
_rt_write_roman:
stp x29, x30, [sp, #-32]!
mov x29, sp
; ... routine body ...
ldp x29, x30, [sp], #32
ret
The prologue saves the frame pointer and link register, allocating 32 bytes of frame (enough for the stack-local roman-numeral buffer the routine uses). The epilogue restores them and returns.
The x86-64 equivalent looks similar structurally, with different mnemonics:
; x86-64
lea rdi, [rip + _spot_1] ; address in rdi
mov edi, dword ptr [rdi] ; value now in edi/rdi
call _rt_write_roman
_rt_write_roman:
push rbp
mov rbp, rsp
sub rsp, 32
; ... routine body ...
mov rsp, rbp
pop rbp
ret
The same structure, save frame pointer, allocate local frame, restore, return, with different instruction names.
Why we do not reinvent the wheel
Writing a compiler that targets a custom calling convention is possible, but not a good idea. Every such compiler ends up needing to interoperate with the system linker, the C runtime, and the kernel, all of which already agree on a convention. The right answer is to match the host platform's convention so that:
main,_main, and every_rt_*symbol can be linked bycc.- The
write,read,open,close,mmap,exitsyscalls can be invoked correctly. - Future interoperability (calling C from INTERCAL, calling INTERCAL from a test harness written in another language) remains straightforward.
This is the same reasoning behind the --pure-syslib design: we match the native side's calling convention even in pure-INTERCAL code, because the alternative would fork the interface.
Exercises
- Trace
DO READ OUT .5from the emitted assembly through_rt_write_romanto thewritesyscall. How many times does a value move between registers? How many times is a value on the stack? - Suppose a future runtime routine needs to preserve
x19across a call to a helper. Write the minimal prologue/epilogue pair that correctly follows AAPCS64. - The System V AMD64 ABI reserves
r10as a static chain register for nested functions. Our compiler has no nested functions. Does the runtime ever touchr10? If so, why? - On Linux x86-64, the
syscallinstruction clobbersrcxandr11. Find every place insrc/runtime/linux_x86_64.swhere this matters, and describe what the surrounding code does to cope. - Why does the AAPCS64 calling convention require 16-byte stack alignment rather than 8-byte? Give a concrete reason tied to the instruction set.