[libc-commits] [libc] [libc][arm] implement a basic setjmp/longjmp (PR #93220)
Simon Tatham via libc-commits
libc-commits at lists.llvm.org
Mon Jun 3 02:38:25 PDT 2024
================
@@ -0,0 +1,71 @@
+//===-- Implementation of setjmp ------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/__support/common.h"
+#include "src/setjmp/setjmp_impl.h"
+
+namespace LIBC_NAMESPACE {
+
+#if defined(__thumb__) && __ARM_ARCH_ISA_THUMB == 1
+
+[[gnu::naked, gnu::target("thumb")]]
+LLVM_LIBC_FUNCTION(int, setjmp, (__jmp_buf * buf)) {
+ asm(R"(
+ # Store r4, r5, r6, and r7 into buf.
+ stmia r0!, {r4-r7}
+
+ # Store r8, r9, r10, r11, sp, and lr into buf. Thumb(1) doesn't support
+ # the high registers > r7 in stmia, so move them into lower GPRs first.
+ # Thumb(1) also doesn't support using str with sp or lr, move them
+ # together with the rest.
+ mov r1, r8
+ mov r2, r9
+ mov r3, r10
+ mov r4, r11
+ mov r5, sp
+ mov r6, lr
+ stmia r0!, {r1-r6}
+
+ # AAPCS32 states
+ # A subroutine must preserve the contents of the registers r4-r8 ...
+ # so rewind the buf pointer by the number of registers saved (i.e. 10
+ # registers: r4, r5, r6, r7, r8, r9, r10, r11, sp, lr), then restore
+ # r4, r5, and r6. r7 and r8 were not clobbered. These register are 4B, so
+ # 10 registers times 4B gives us 40B to rewind buf by.
+ subs r0, r0, #40
+ ldmia r0!, {r4-r6}
----------------
statham-arm wrote:
I'm confident this is right, but I'm curious why you didn't do it the same way as in `longjmp`, by doing the original store in smaller blocks so that you don't clobber those registers in the first place. Set r1,r2,r3 to r8,r9,r10 and do a three-reg `stmia`; then set them to r11,sp,lr and do another one.
One fewer instruction and less memory access. It's _more_ of a win here to do it like this than it was in `longjmp`, because you have one more low register free, so you can split the store into two 3-reg chunks instead of three 2-reg ones.
https://github.com/llvm/llvm-project/pull/93220
More information about the libc-commits
mailing list