xref: /aosp_15_r20/external/llvm-libc/src/setjmp/riscv/longjmp.cpp (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1 //===-- Implementation of longjmp -----------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "src/setjmp/longjmp.h"
10 #include "src/__support/common.h"
11 #include "src/__support/macros/config.h"
12 #include "src/__support/macros/properties/architectures.h"
13 
14 #if !defined(LIBC_TARGET_ARCH_IS_ANY_RISCV)
15 #error "Invalid file include"
16 #endif
17 
18 #define LOAD_IMPL(insns, reg, val)                                             \
19   LIBC_INLINE_ASM(#insns " " #reg ", %0\n\t" : : "m"(val) :)
20 
21 #ifdef LIBC_TARGET_ARCH_IS_RISCV32
22 #define LOAD(reg, val) LOAD_IMPL(lw, reg, val)
23 #define LOAD_FP(reg, val) LOAD_IMPL(flw, reg, val)
24 #else
25 #define LOAD(reg, val) LOAD_IMPL(ld, reg, val)
26 #define LOAD_FP(reg, val) LOAD_IMPL(fld, reg, val)
27 #endif
28 
29 namespace LIBC_NAMESPACE_DECL {
30 
31 LLVM_LIBC_FUNCTION(void, longjmp, (jmp_buf buf, int val)) {
32   LOAD(ra, buf->__pc);
33   LOAD(s0, buf->__regs[0]);
34   LOAD(s1, buf->__regs[1]);
35   LOAD(s2, buf->__regs[2]);
36   LOAD(s3, buf->__regs[3]);
37   LOAD(s4, buf->__regs[4]);
38   LOAD(s5, buf->__regs[5]);
39   LOAD(s6, buf->__regs[6]);
40   LOAD(s7, buf->__regs[7]);
41   LOAD(s8, buf->__regs[8]);
42   LOAD(s9, buf->__regs[9]);
43   LOAD(s10, buf->__regs[10]);
44   LOAD(s11, buf->__regs[11]);
45   LOAD(sp, buf->__sp);
46 
47 #if __riscv_float_abi_double
48   LOAD_FP(fs0, buf->__fpregs[0]);
49   LOAD_FP(fs1, buf->__fpregs[1]);
50   LOAD_FP(fs2, buf->__fpregs[2]);
51   LOAD_FP(fs3, buf->__fpregs[3]);
52   LOAD_FP(fs4, buf->__fpregs[4]);
53   LOAD_FP(fs5, buf->__fpregs[5]);
54   LOAD_FP(fs6, buf->__fpregs[6]);
55   LOAD_FP(fs7, buf->__fpregs[7]);
56   LOAD_FP(fs8, buf->__fpregs[8]);
57   LOAD_FP(fs9, buf->__fpregs[9]);
58   LOAD_FP(fs10, buf->__fpregs[10]);
59   LOAD_FP(fs11, buf->__fpregs[11]);
60 #elif defined(__riscv_float_abi_single)
61 #error "longjmp implementation not available for the target architecture."
62 #endif
63 
64   val = val == 0 ? 1 : val;
65   LIBC_INLINE_ASM("add a0, %0, zero\n\t" : : "r"(val) :);
66 }
67 
68 } // namespace LIBC_NAMESPACE_DECL
69