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/properties/architectures.h" |
12 | |
13 | #include <setjmp.h> |
14 | |
15 | #if !defined(LIBC_TARGET_ARCH_IS_ANY_RISCV) |
16 | #error "Invalid file include" |
17 | #endif |
18 | |
19 | #define LOAD_IMPL(insns, reg, val) \ |
20 | LIBC_INLINE_ASM(#insns " " #reg ", %0\n\t" : : "m"(val) :) |
21 | |
22 | #ifdef LIBC_TARGET_ARCH_IS_RISCV32 |
23 | #define LOAD(reg, val) LOAD_IMPL(lw, reg, val) |
24 | #define LOAD_FP(reg, val) LOAD_IMPL(flw, reg, val) |
25 | #else |
26 | #define LOAD(reg, val) LOAD_IMPL(ld, reg, val) |
27 | #define LOAD_FP(reg, val) LOAD_IMPL(fld, reg, val) |
28 | #endif |
29 | |
30 | namespace LIBC_NAMESPACE { |
31 | |
32 | LLVM_LIBC_FUNCTION(void, longjmp, (__jmp_buf * buf, int val)) { |
33 | LOAD(ra, buf->__pc); |
34 | LOAD(s0, buf->__regs[0]); |
35 | LOAD(s1, buf->__regs[1]); |
36 | LOAD(s2, buf->__regs[2]); |
37 | LOAD(s3, buf->__regs[3]); |
38 | LOAD(s4, buf->__regs[4]); |
39 | LOAD(s5, buf->__regs[5]); |
40 | LOAD(s6, buf->__regs[6]); |
41 | LOAD(s7, buf->__regs[7]); |
42 | LOAD(s8, buf->__regs[8]); |
43 | LOAD(s9, buf->__regs[9]); |
44 | LOAD(s10, buf->__regs[10]); |
45 | LOAD(s11, buf->__regs[11]); |
46 | LOAD(sp, buf->__sp); |
47 | |
48 | #if __riscv_float_abi_double |
49 | LOAD_FP(fs0, buf->__fpregs[0]); |
50 | LOAD_FP(fs1, buf->__fpregs[1]); |
51 | LOAD_FP(fs2, buf->__fpregs[2]); |
52 | LOAD_FP(fs3, buf->__fpregs[3]); |
53 | LOAD_FP(fs4, buf->__fpregs[4]); |
54 | LOAD_FP(fs5, buf->__fpregs[5]); |
55 | LOAD_FP(fs6, buf->__fpregs[6]); |
56 | LOAD_FP(fs7, buf->__fpregs[7]); |
57 | LOAD_FP(fs8, buf->__fpregs[8]); |
58 | LOAD_FP(fs9, buf->__fpregs[9]); |
59 | LOAD_FP(fs10, buf->__fpregs[10]); |
60 | LOAD_FP(fs11, buf->__fpregs[11]); |
61 | #elif defined(__riscv_float_abi_single) |
62 | #error "longjmp implementation not available for the target architecture." |
63 | #endif |
64 | |
65 | val = val == 0 ? 1 : val; |
66 | LIBC_INLINE_ASM("add a0, %0, zero\n\t" : : "r" (val) :); |
67 | } |
68 | |
69 | } // namespace LIBC_NAMESPACE |
70 | |