Debian Codegraph @

mypyc/lib-rt/mypyc_util.h

raw
1 #ifndef MYPYC_UTIL_H
2 #define MYPYC_UTIL_H
3
4 #include <Python.h>
5 #include <frameobject.h>
6 #include <assert.h>
7
8 #if defined(__clang__) || defined(__GNUC__)
9 #define likely(x) __builtin_expect((x),1)
10 #define unlikely(x) __builtin_expect((x),0)
11 #define CPy_Unreachable() __builtin_unreachable()
12 #else
13 #define likely(x) (x)
14 #define unlikely(x) (x)
15 #define CPy_Unreachable() abort()
16 #endif
17
18 #if defined(__clang__) || defined(__GNUC__)
19 #define CPy_NOINLINE __attribute__((noinline))
20 #elif defined(_MSC_VER)
21 #define CPy_NOINLINE __declspec(noinline)
22 #else
23 #define CPy_NOINLINE
24 #endif
25
26 #ifndef Py_GIL_DISABLED
27
28 // Everything is running in the same thread, so no need for thread locals
29 #define CPyThreadLocal
30
31 #else
32
33 // 1. Use C11 standard thread_local storage, if available
34 #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L && !defined(__STDC_NO_THREADS__)
35 #define CPyThreadLocal _Thread_local
36
37 // 2. Microsoft Visual Studio fallback
38 #elif defined(_MSC_VER)
39 #define CPyThreadLocal __declspec(thread)
40
41 // 3. GNU thread local storage for GCC/Clang targets that still need it
42 #elif defined(__GNUC__) || defined(__clang__)
43 #define CPyThreadLocal __thread
44
45 #else
46 #error "Can't define CPyThreadLocal for this compiler/target (consider using a non-free-threaded Python build)"
47 #endif
48
49 #endif // Py_GIL_DISABLED
50
51 // Helper macro for stringification in _Pragma
52 #define CPY_STRINGIFY(x) #x
53
54 #if defined(__clang__)
55 #define CPY_UNROLL_LOOP_IMPL(x) _Pragma(CPY_STRINGIFY(x))
56 #define CPY_UNROLL_LOOP(n) CPY_UNROLL_LOOP_IMPL(unroll n)
57 #elif defined(__GNUC__) && __GNUC__ >= 8
58 #define CPY_UNROLL_LOOP_IMPL(x) _Pragma(CPY_STRINGIFY(x))
59 #define CPY_UNROLL_LOOP(n) CPY_UNROLL_LOOP_IMPL(GCC unroll n)
60 #else
61 #define CPY_UNROLL_LOOP(n)
62 #endif
63
64 // INCREF and DECREF that assert the pointer is not NULL.
65 // asserts are disabled in release builds so there shouldn't be a perf hit.
66 // I'm honestly kind of surprised that this isn't done by default.
67 #define CPy_INCREF(p) do { assert(p); Py_INCREF(p); } while (0)
68 #define CPy_DECREF(p) do { assert(p); Py_DECREF(p); } while (0)
69 // Here just for consistency
70 #define CPy_XDECREF(p) Py_XDECREF(p)
71
72 #ifndef Py_GIL_DISABLED
73
74 // The *_NO_IMM operations below perform refcount manipulation for
75 // non-immortal objects (Python 3.12 and later).
76 //
77 // Py_INCREF and other CPython operations check for immortality. This
78 // can be expensive when we know that an object cannot be immortal.
79 //
80 // This optimization cannot be performed in free-threaded mode so we
81 // fall back to just calling the normal incref/decref operations.
82
83 static inline void CPy_INCREF_NO_IMM(PyObject *op)
84 {
85 op->ob_refcnt++;
86 }
87
88 static inline void CPy_DECREF_NO_IMM(PyObject *op)
89 {
90 if (--op->ob_refcnt == 0) {
91 _Py_Dealloc(op);
92 }
93 }
94
95 static inline void CPy_XDECREF_NO_IMM(PyObject *op)
96 {
97 if (op != NULL && --op->ob_refcnt == 0) {
98 _Py_Dealloc(op);
99 }
100 }
101
102 #define CPy_INCREF_NO_IMM(op) CPy_INCREF_NO_IMM((PyObject *)(op))
103 #define CPy_DECREF_NO_IMM(op) CPy_DECREF_NO_IMM((PyObject *)(op))
104 #define CPy_XDECREF_NO_IMM(op) CPy_XDECREF_NO_IMM((PyObject *)(op))
105
106 #else
107
108 #define CPy_INCREF_NO_IMM(op) CPy_INCREF(op)
109 #define CPy_DECREF_NO_IMM(op) CPy_DECREF(op)
110 #define CPy_XDECREF_NO_IMM(op) CPy_XDECREF(op)
111
112 #endif
113
114 // Tagged integer -- our representation of Python 'int' objects.
115 // Small enough integers are represented as unboxed integers (shifted
116 // left by 1); larger integers (larger than 63 bits on a 64-bit
117 // platform) are stored as a tagged pointer (PyObject *)
118 // representing a Python int object, with the lowest bit set.
119 // Tagged integers are always normalized. A small integer *must not*
120 // have the tag bit set.
121 typedef size_t CPyTagged;
122
123 typedef size_t CPyPtr;
124
125 #define CPY_INT_BITS (CHAR_BIT * sizeof(CPyTagged))
126
127 #define CPY_TAGGED_MAX (((Py_ssize_t)1 << (CPY_INT_BITS - 2)) - 1)
128 #define CPY_TAGGED_MIN (-((Py_ssize_t)1 << (CPY_INT_BITS - 2)))
129 #define CPY_TAGGED_ABS_MIN (0-(size_t)CPY_TAGGED_MIN)
130
131 typedef PyObject CPyModule;
132
133 // Tag bit used for long integers
134 #define CPY_INT_TAG 1
135
136 // Error value for signed fixed-width (low-level) integers
137 #define CPY_LL_INT_ERROR -113
138
139 // Error value for unsigned fixed-width (low-level) integers
140 #define CPY_LL_UINT_ERROR 239
141
142 // Error value for floats
143 #define CPY_FLOAT_ERROR -113.0
144
145 // Value for 'None' primitive type
146 #define CPY_NONE_ERROR 2
147 #define CPY_NONE 1
148
149 typedef void (*CPyVTableItem)(void);
150
151 static inline CPyTagged CPyTagged_ShortFromInt(int x) {
152 return x << 1;
153 }
154
155 static inline CPyTagged CPyTagged_ShortFromSsize_t(Py_ssize_t x) {
156 return x << 1;
157 }
158
159 static inline int CPyTagged_IsNegative(CPyTagged x) {
160 return ((Py_ssize_t)x) < 0;
161 }
162
163 // Are we targeting Python 3.X or newer?
164 #define CPY_3_11_FEATURES (PY_VERSION_HEX >= 0x030b0000)
165 #define CPY_3_12_FEATURES (PY_VERSION_HEX >= 0x030c0000)
166 #define CPY_3_14_FEATURES (PY_VERSION_HEX >= 0x030e0000)
167 #define CPY_3_15_FEATURES (PY_VERSION_HEX >= 0x030f0000)
168
169 #if CPY_3_12_FEATURES
170
171 // Same as macros in CPython internal/pycore_long.h, but with a CPY_ prefix
172 #define CPY_NON_SIZE_BITS 3
173 #define CPY_SIGN_ZERO 1
174 #define CPY_SIGN_NEGATIVE 2
175 #define CPY_SIGN_MASK 3
176
177 #define CPY_LONG_DIGIT(o, n) ((o)->long_value.ob_digit[n])
178
179 // Only available on Python 3.12 and later
180 #define CPY_LONG_TAG(o) ((o)->long_value.lv_tag)
181 #define CPY_LONG_IS_NEGATIVE(o) (((o)->long_value.lv_tag & CPY_SIGN_MASK) == CPY_SIGN_NEGATIVE)
182 // Only available on Python 3.12 and later
183 #define CPY_LONG_SIZE(o) ((o)->long_value.lv_tag >> CPY_NON_SIZE_BITS)
184 // Number of digits; negative for negative ints
185 #define CPY_LONG_SIZE_SIGNED(o) (CPY_LONG_IS_NEGATIVE(o) ? -CPY_LONG_SIZE(o) : CPY_LONG_SIZE(o))
186 // Number of digits, assuming int is non-negative
187 #define CPY_LONG_SIZE_UNSIGNED(o) CPY_LONG_SIZE(o)
188
189 #else
190
191 #define CPY_LONG_DIGIT(o, n) ((o)->ob_digit[n])
192 #define CPY_LONG_IS_NEGATIVE(o) (((o)->ob_base.ob_size < 0)
193 #define CPY_LONG_SIZE_SIGNED(o) ((o)->ob_base.ob_size)
194 #define CPY_LONG_SIZE_UNSIGNED(o) ((o)->ob_base.ob_size)
195
196 #endif
197
198 // Are we targeting Python 3.13 or newer?
199 #define CPY_3_13_FEATURES (PY_VERSION_HEX >= 0x030d0000)
200
201 // Are we targeting Python 3.14 or newer?
202 #define CPY_3_14_FEATURES (PY_VERSION_HEX >= 0x030e0000)
203
204 #endif