aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/memory/qv4stacklimits_p.h
blob: a87b541ed98a9a2d1b6075827c37d946de9c599f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#ifndef QV4STACKLIMITS_P_H
#define QV4STACKLIMITS_P_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists purely as an
// implementation detail.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

#include <private/qtqmlglobal_p.h>

#ifndef Q_STACK_GROWTH_DIRECTION
#  ifdef Q_PROCESSOR_HPPA
#    define Q_STACK_GROWTH_DIRECTION (1)
#  else
#    define Q_STACK_GROWTH_DIRECTION (-1)
#  endif
#endif

QT_BEGIN_NAMESPACE

namespace QV4 {

// We may not be able to take the negative of the type
// used to represent stack size, but we can always add
// or subtract it to/from a quint8 pointer.

template<typename Size>
static const void *incrementStackPointer(const void *base, Size amount)
{
#if Q_STACK_GROWTH_DIRECTION > 0
    return static_cast<const quint8 *>(base) + amount;
#else
    return static_cast<const quint8 *>(base) - amount;
#endif
}

template<typename Size>
static void *decrementStackPointer(void *base, Size amount)
{
#if Q_STACK_GROWTH_DIRECTION > 0
    return static_cast<quint8 *>(base) - amount;
#else
    return static_cast<quint8 *>(base) + amount;
#endif
}

// Note: This does not return a completely accurate stack pointer.
//       Depending on whether this function is inlined or not, we may get the address of
//       this function's stack frame or the caller's stack frame.
//       Always use a safety margin when determining stack limits.
inline const void *currentStackPointer()
{
    // TODO: How often do we actually need the assembler mess below? Is that worth it?

    void *stackPointer;
#if defined(Q_CC_GNU) || __has_builtin(__builtin_frame_address)
    stackPointer = __builtin_frame_address(0);
#elif defined(Q_CC_MSVC)
    stackPointer = &stackPointer;
#elif defined(Q_PROCESSOR_X86_64)
    __asm__ __volatile__("movq %%rsp, %0" : "=r"(stackPointer) : :);
#elif defined(Q_PROCESSOR_X86)
    __asm__ __volatile__("movl %%esp, %0" : "=r"(stackPointer) : :);
#elif defined(Q_PROCESSOR_ARM_64) && defined(__ILP32__)
    quint64 stackPointerRegister = 0;
    __asm__ __volatile__("mov %0, sp" : "=r"(stackPointerRegister) : :);
    stackPointer = reinterpret_cast<void *>(stackPointerRegister);
#elif defined(Q_PROCESSOR_ARM_64) || defined(Q_PROCESSOR_ARM_32)
    __asm__ __volatile__("mov %0, sp" : "=r"(stackPointer) : :);
#else
    stackPointer = &stackPointer;
#endif
    return stackPointer;
}

struct StackProperties
{
    const void *base = nullptr;
    const void *softLimit = nullptr;
    const void *hardLimit = nullptr;
};

StackProperties stackProperties();

} // namespace QV4

QT_END_NAMESPACE

#endif // QV4STACKLIMITS_P_H