78 lines · cpp
1//===-- Implementation of setjmp ------------------------------------------===//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-exception6//7//===----------------------------------------------------------------------===//8 9// We use naked functions to avoid compiler-generated prologue and epilogue.10// Despite GCC documentation listing this as an unsupported case for extended11// asm, the generated code is not wrong as we only pass in constant operands12// to extended asm.13// See https://github.com/llvm/llvm-project/issues/137055 for related remarks.14 15#include "hdr/offsetof_macros.h"16#include "src/__support/common.h"17#include "src/__support/macros/config.h"18#include "src/setjmp/setjmp_impl.h"19 20#if !defined(LIBC_TARGET_ARCH_IS_X86)21#error "Invalid file include"22#endif23 24namespace LIBC_NAMESPACE_DECL {25 26#ifdef __i386__27[[gnu::naked]]28LLVM_LIBC_FUNCTION(int, setjmp, (jmp_buf buf)) {29 asm(R"(30 mov 4(%%esp), %%eax31 32 mov %%ebx, %c[ebx](%%eax)33 mov %%esi, %c[esi](%%eax)34 mov %%edi, %c[edi](%%eax)35 mov %%ebp, %c[ebp](%%eax)36 37 lea 4(%%esp), %%ecx38 mov %%ecx, %c[esp](%%eax)39 40 mov (%%esp), %%ecx41 mov %%ecx, %c[eip](%%eax)42 43 xorl %%eax, %%eax44 retl)" ::[ebx] "i"(offsetof(__jmp_buf, ebx)),45 [esi] "i"(offsetof(__jmp_buf, esi)), [edi] "i"(offsetof(__jmp_buf, edi)),46 [ebp] "i"(offsetof(__jmp_buf, ebp)), [esp] "i"(offsetof(__jmp_buf, esp)),47 [eip] "i"(offsetof(__jmp_buf, eip))48 : "eax", "ecx");49}50#else51[[gnu::naked]]52LLVM_LIBC_FUNCTION(int, setjmp, (jmp_buf buf)) {53 asm(R"(54 mov %%rbx, %c[rbx](%%rdi)55 mov %%rbp, %c[rbp](%%rdi)56 mov %%r12, %c[r12](%%rdi)57 mov %%r13, %c[r13](%%rdi)58 mov %%r14, %c[r14](%%rdi)59 mov %%r15, %c[r15](%%rdi)60 61 lea 8(%%rsp), %%rax62 mov %%rax, %c[rsp](%%rdi)63 64 mov (%%rsp), %%rax65 mov %%rax, %c[rip](%%rdi)66 67 xorl %%eax, %%eax68 retq)" ::[rbx] "i"(offsetof(__jmp_buf, rbx)),69 [rbp] "i"(offsetof(__jmp_buf, rbp)), [r12] "i"(offsetof(__jmp_buf, r12)),70 [r13] "i"(offsetof(__jmp_buf, r13)), [r14] "i"(offsetof(__jmp_buf, r14)),71 [r15] "i"(offsetof(__jmp_buf, r15)), [rsp] "i"(offsetof(__jmp_buf, rsp)),72 [rip] "i"(offsetof(__jmp_buf, rip))73 : "rax");74}75#endif76 77} // namespace LIBC_NAMESPACE_DECL78