Skip to content

Commit 2d13bf8

Browse files
save lr, add more comments
1 parent 51464ea commit 2d13bf8

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

libc/src/setjmp/arm/setjmp.cpp

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,23 @@ namespace LIBC_NAMESPACE {
1313

1414
#if defined(__thumb__) && __ARM_ARCH_ISA_THUMB == 1
1515

16-
[[gnu::naked]]
16+
[[gnu::naked, gnu::target("thumb")]]
1717
LLVM_LIBC_FUNCTION(int, setjmp, (__jmp_buf * buf)) {
1818
asm(R"(
1919
# Store r4, r5, r6, and r7 into buf.
2020
stmia r0!, {r4-r7}
2121
22-
# Store r8, r9, r10, and r11 into buf. Thumb(1) doesn't support the high
23-
# registers > r7 in stmia, so move them into lower GPRs first.
24-
mov r4, r8
25-
mov r5, r9
26-
mov r6, r10
27-
mov r7, r11
28-
stmia r0!, {r4-r7}
29-
30-
# Store sp into buf. Thumb(1) doesn't support sp in str, move to GPR
31-
# first.
32-
mov r4, sp
33-
str r4, [r0]
22+
# Store r8, r9, r10, r11, sp, and lr into buf. Thumb(1) doesn't support
23+
# the high registers > r7 in stmia, so move them into lower GPRs first.
24+
# Thumb(1) also doesn't support using str with sp or lr, move them
25+
# 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}
3433
3534
# Return 0.
3635
movs r0, #0
@@ -42,8 +41,15 @@ LLVM_LIBC_FUNCTION(int, setjmp, (__jmp_buf * buf)) {
4241
[[gnu::naked]]
4342
LLVM_LIBC_FUNCTION(int, setjmp, (__jmp_buf * buf)) {
4443
asm(R"(
44+
# While sp may appear in a register list for ARM mode, it may not for
45+
# Thumb2 mode. Just move it into r12 then stm that, so that this code
46+
# is portable between ARM and Thumb2.
4547
mov r12, sp
48+
49+
# Store r4, r5, r6, r7, r8, r9, r10, r11, sp, and lr into buf.
4650
stm r0, {r4-r12, lr}
51+
52+
# Return zero.
4753
mov r0, #0
4854
bx lr)");
4955
}

0 commit comments

Comments
 (0)