|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#if defined (_MSC_VER) && !defined(__clang__) |
| 4 | +#error "get_stack_pointer is not supported on MSVC" |
| 5 | +#endif |
| 6 | + |
| 7 | +namespace fast_io::details |
| 8 | +{ |
| 9 | + |
| 10 | +/** |
| 11 | + * @brief Get the stack pointer. |
| 12 | + * @return void* The stack pointer. |
| 13 | + * |
| 14 | + * @note MSVC is not supported. |
| 15 | + */ |
| 16 | +[[nodiscard]] |
| 17 | +#if __has_cpp_attribute(__gnu__::__always_inline__) |
| 18 | +[[__gnu__::__always_inline__]] |
| 19 | +#else |
| 20 | +#error "__gnu__::__always_inline__ is required" |
| 21 | +#endif |
| 22 | +#if __has_cpp_attribute(__gnu__::__artificial__) |
| 23 | +[[__gnu__::__artificial__]] |
| 24 | +#endif |
| 25 | +inline void *get_stack_pointer() noexcept |
| 26 | +{ |
| 27 | + void *result |
| 28 | +#if __has_cpp_attribute(indeterminate) |
| 29 | + [[indeterminate]] |
| 30 | +#endif |
| 31 | + ; |
| 32 | + |
| 33 | +#if defined(__x86_64__) && !defined(__arm64ec__) |
| 34 | + __asm__ volatile("movq %%rsp, %0" : "=r"(result)); |
| 35 | +#elif defined(__i386__) |
| 36 | + __asm__ volatile("movl %%esp, %0" : "=r"(result)); |
| 37 | +#elif defined(__aarch64__) || defined(__arm64ec__) |
| 38 | + __asm__ volatile("mov %0, sp" : "=r"(result)); |
| 39 | +#elif defined(__arm__) |
| 40 | + __asm__ volatile("mov %0, sp" : "=r"(result)); |
| 41 | +#elif defined(__loongarch__) |
| 42 | + __asm__ volatile("move %0, $sp" : "=r"(result)); |
| 43 | +#elif defined(__riscv) |
| 44 | + __asm__ volatile("mv %0, sp" : "=r"(result)); |
| 45 | +#elif defined(__mips__) |
| 46 | + __asm__ volatile("move %0, $sp" : "=r"(result)); |
| 47 | +#elif defined(__powerpc__) |
| 48 | + __asm__ volatile("mr %0, 1" : "=r"(result)); |
| 49 | +#else |
| 50 | + ::fast_io::fast_terminate(); |
| 51 | +#endif |
| 52 | + return result; |
| 53 | +} |
| 54 | + |
| 55 | +} // namespace fast_io |
0 commit comments