| 1 | // Copyright (C) 2022 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef QV4STACKLIMITS_P_H |
| 5 | #define QV4STACKLIMITS_P_H |
| 6 | |
| 7 | // |
| 8 | // W A R N I N G |
| 9 | // ------------- |
| 10 | // |
| 11 | // This file is not part of the Qt API. It exists purely as an |
| 12 | // implementation detail. This header file may change from version to |
| 13 | // version without notice, or even be removed. |
| 14 | // |
| 15 | // We mean it. |
| 16 | // |
| 17 | |
| 18 | #include <private/qtqmlglobal_p.h> |
| 19 | |
| 20 | #ifndef Q_STACK_GROWTH_DIRECTION |
| 21 | # ifdef Q_PROCESSOR_HPPA |
| 22 | # define Q_STACK_GROWTH_DIRECTION (1) |
| 23 | # else |
| 24 | # define Q_STACK_GROWTH_DIRECTION (-1) |
| 25 | # endif |
| 26 | #endif |
| 27 | |
| 28 | QT_BEGIN_NAMESPACE |
| 29 | |
| 30 | namespace QV4 { |
| 31 | |
| 32 | // Note: This does not return a completely accurate stack pointer. |
| 33 | // Depending on whether this function is inlined or not, we may get the address of |
| 34 | // this function's stack frame or the caller's stack frame. |
| 35 | // Always use a safety margin when determining stack limits. |
| 36 | inline const void *currentStackPointer() |
| 37 | { |
| 38 | // TODO: How often do we actually need the assembler mess below? Is that worth it? |
| 39 | |
| 40 | void *stackPointer; |
| 41 | #if defined(Q_CC_GNU) || __has_builtin(__builtin_frame_address) |
| 42 | stackPointer = __builtin_frame_address(0); |
| 43 | #elif defined(Q_CC_MSVC) |
| 44 | stackPointer = &stackPointer; |
| 45 | #elif defined(Q_PROCESSOR_X86_64) |
| 46 | __asm__ __volatile__("movq %%rsp, %0" : "=r" (stackPointer) : :); |
| 47 | #elif defined(Q_PROCESSOR_X86) |
| 48 | __asm__ __volatile__("movl %%esp, %0" : "=r" (stackPointer) : :); |
| 49 | #elif defined(Q_PROCESSOR_ARM_64) && defined(__ILP32__) |
| 50 | quint64 stackPointerRegister = 0; |
| 51 | __asm__ __volatile__("mov %0, sp" : "=r" (stackPointerRegister) : :); |
| 52 | stackPointer = reinterpret_cast<void *>(stackPointerRegister); |
| 53 | #elif defined(Q_PROCESSOR_ARM_64) || defined(Q_PROCESSOR_ARM_32) |
| 54 | __asm__ __volatile__("mov %0, sp" : "=r" (stackPointer) : :); |
| 55 | #else |
| 56 | stackPointer = &stackPointer; |
| 57 | #endif |
| 58 | return stackPointer; |
| 59 | } |
| 60 | |
| 61 | struct StackProperties |
| 62 | { |
| 63 | const void *base = nullptr; |
| 64 | const void *softLimit = nullptr; |
| 65 | const void *hardLimit = nullptr; |
| 66 | }; |
| 67 | |
| 68 | StackProperties stackProperties(); |
| 69 | |
| 70 | } // namespace QV4 |
| 71 | |
| 72 | QT_END_NAMESPACE |
| 73 | |
| 74 | #endif // QV4STACKLIMITS_P_H |
| 75 | |