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

Nick Desaulniers via libc-commits libc-commits at lists.llvm.org
Tue Jun 18 14:55:35 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}
----------------
nickdesaulniers wrote:

5dac5006c692

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


More information about the libc-commits mailing list