[libc-commits] [libc] [libc][arm] implement a basic setjmp/longjmp (PR #93220)

Simon Tatham via libc-commits libc-commits at lists.llvm.org
Tue May 28 03:15:43 PDT 2024


================
@@ -0,0 +1,59 @@
+//===-- 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 r2, r8
+      mov r3, r9
+      mov r4, r10
----------------
statham-arm wrote:

This approach will surely clobber the values that r4-r7 had on entry to `setjmp`?

`setjmp` has to do two things with the values of callee-saved registers. It has to put them in the `jmp_buf` for `longjmp` to restore. But it _also_ has to preserve them itself, for when it returns directly to the caller.

https://github.com/llvm/llvm-project/pull/93220


More information about the libc-commits mailing list