Skip to content

Commit 1bcc7e5

Browse files
Feature: get_stack_pointer (#1214)
* Feature: get_stack_pointer - all listed arch have been tested by clang - will be used by linux_clone3_thread - MSVC is not supported * fix test * add precompile fence to stack_pointer.h * add attribute fence during precompile * move stack_pointer.h to thread/ - as an internal dependency of linux clone3 thread * Avoid compile fail, use runtime terminate instead * fix err msg
1 parent df6bab2 commit 1bcc7e5

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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

Comments
 (0)