Skip to content

Commit 349e874

Browse files
restore callee-saved r4, r5, r6 for thumb1, use r1-r6 as scratch rather than r2-r7
Link: https://github.com/ARM-software/abi-aa/blob/main/aapcs32/aapcs32.rst#core-registers
1 parent 16619b2 commit 349e874

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

libc/src/setjmp/arm/setjmp.cpp

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,22 @@ LLVM_LIBC_FUNCTION(int, setjmp, (__jmp_buf * buf)) {
2323
# the high registers > r7 in stmia, so move them into lower GPRs first.
2424
# Thumb(1) also doesn't support using str with sp or lr, move them
2525
# together with the rest.
26-
mov r2, r8
27-
mov r3, r9
28-
mov r4, r10
29-
mov r5, r11
30-
mov r6, sp
31-
mov r7, lr
32-
stmia r0!, {r2-r7}
26+
mov r1, r8
27+
mov r2, r9
28+
mov r3, r10
29+
mov r4, r11
30+
mov r5, sp
31+
mov r6, lr
32+
stmia r0!, {r1-r6}
33+
34+
# AAPCS32 states
35+
# A subroutine must preserve the contents of the registers r4-r8 ...
36+
# so rewind the buf pointer by the number of registers saved (i.e. 10
37+
# registers: r4, r5, r6, r7, r8, r9, r10, r11, sp, lr), then restore
38+
# r4, r5, and r6. r7 and r8 were not clobbered. These register are 4B, so
39+
# 10 registers times 4B gives us 40B to rewind buf by.
40+
subs r0, r0, #40
41+
ldmia r0!, {r4-r6}
3342
3443
# Return 0.
3544
movs r0, #0

0 commit comments

Comments
 (0)