4#ifndef NUITKA_PYTHON_INTERNALS_ACCESS_H
5#define NUITKA_PYTHON_INTERNALS_ACCESS_H
11#if _NUITKA_MODULE_MODE && PYTHON_VERSION >= 0x3e0 && PYTHON_VERSION < 0x3f0 && !defined(Py_GIL_DISABLED)
12static inline struct _Py_interp_cached_objects *Nuitka_PyInterpreterState_GetCachedObjects(PyInterpreterState *interp) {
13 return (
struct _Py_interp_cached_objects *)Nuitka_PyInterpreterState_AdjustPostQsbrPointer(&interp->cached_objects);
16static inline struct _Py_interp_static_objects *Nuitka_PyInterpreterState_GetStaticObjects(PyInterpreterState *interp) {
17 return (
struct _Py_interp_static_objects *)Nuitka_PyInterpreterState_AdjustPostQsbrPointer(&interp->static_objects);
21#if PYTHON_VERSION >= 0x3c0
22static inline bool Nuitka_RuntimeHasPerfTrampoline(
void) {
23 static int cached_result = -1;
24 if (cached_result != -1) {
25 return cached_result != 0;
27 PyObject *func = PySys_GetObject(
"activate_stack_trampoline");
32 NUITKA_MAY_BE_UNUSED PyObject *res = PyObject_CallFunction(func,
"s",
"s");
36 PyThreadState *tstate = PyThreadState_GET();
37 PyObject *exception_value = tstate->current_exception;
38 assert(exception_value != NULL);
39 assert((PyObject *)Py_TYPE(exception_value) == PyExc_ValueError);
41 PyObject *args = ((PyBaseExceptionObject *)exception_value)->args;
42 assert(PyTuple_CheckExact(args) && PyTuple_GET_SIZE(args) > 0);
43 PyObject *arg = PyTuple_GET_ITEM(args, 0);
44 assert(PyUnicode_CheckExact(arg));
46 const char *msg = PyUnicode_AsUTF8(arg);
48 if (strcmp(msg,
"perf trampoline not available") == 0) {
52 tstate->current_exception = NULL;
53 Py_DECREF(exception_value);
55 cached_result = has_perf ? 1 : 0;
60#if PYTHON_VERSION >= 0x3d0 && PYTHON_VERSION < 0x3e0 && !defined(Py_GIL_DISABLED)
61#if defined(_WIN32) && (defined(__i386__) || defined(_M_IX86))
63#define NUITKA_PYRUNTIME_OFFSET__PyRuntimeState_ceval 1272
64#define NUITKA_PYRUNTIME_OFFSET__PyRuntimeState_imports 1252
65#define NUITKA_PYRUNTIME_OFFSET__PyRuntimeState_static_objects 8612
66#define NUITKA_PYRUNTIME_OFFSET__PyRuntimeState_stoptheworld 5140
72#if PYTHON_VERSION >= 0x3d0 && PYTHON_VERSION < 0x3e0 && !defined(Py_GIL_DISABLED)
73#if defined(_WIN32) && (defined(__x86_64__) || defined(_M_X64))
75#define NUITKA_PYRUNTIME_OFFSET__PyRuntimeState_ceval 1864
76#define NUITKA_PYRUNTIME_OFFSET__PyRuntimeState_imports 1824
77#define NUITKA_PYRUNTIME_OFFSET__PyRuntimeState_static_objects 13032
78#define NUITKA_PYRUNTIME_OFFSET__PyRuntimeState_stoptheworld 9520
84#if PYTHON_VERSION >= 0x3e0 && PYTHON_VERSION < 0x3f0 && !defined(Py_GIL_DISABLED)
85#if defined(_WIN32) && (defined(__i386__) || defined(_M_IX86))
87#define NUITKA_PYRUNTIME_OFFSET__PyRuntimeState_ceval 1452
88#define NUITKA_PYRUNTIME_OFFSET__PyRuntimeState_imports 1432
89#define NUITKA_PYRUNTIME_OFFSET__PyRuntimeState_static_objects 8812
90#define NUITKA_PYRUNTIME_OFFSET__PyRuntimeState_stoptheworld 5324
96#if PYTHON_VERSION >= 0x3e0 && PYTHON_VERSION < 0x3f0 && !defined(Py_GIL_DISABLED)
97#if defined(_WIN32) && (defined(__x86_64__) || defined(_M_X64))
99#define NUITKA_PYRUNTIME_OFFSET__PyRuntimeState_ceval 2048
100#define NUITKA_PYRUNTIME_OFFSET__PyRuntimeState_imports 2008
101#define NUITKA_PYRUNTIME_OFFSET__PyRuntimeState_static_objects 13240
102#define NUITKA_PYRUNTIME_OFFSET__PyRuntimeState_stoptheworld 9712
108#if defined(__linux__) && _NUITKA_MODULE_MODE
110#if PYTHON_VERSION >= 0x3c0 && PYTHON_VERSION < 0x3d0
111struct _Nuitka_perf_trampoline_missing_delta {
113 Py_ssize_t extra_code_index;
115 void *trampoline_api[5];
118#elif PYTHON_VERSION >= 0x3d0
119struct _Nuitka_perf_trampoline_missing_delta_early {
121 int perf_trampoline_type;
122 Py_ssize_t extra_code_index;
124 void *trampoline_api[5];
126 Py_ssize_t persist_after_fork;
128struct _Nuitka_perf_trampoline_missing_delta_late {
130 int perf_trampoline_type;
131 Py_ssize_t extra_code_index;
133 void *trampoline_api[5];
135 Py_ssize_t persist_after_fork;
136 void *prev_eval_frame;
137 Py_ssize_t trampoline_refcount;
142#if PYTHON_VERSION >= 0x3c0
143static inline size_t _Nuitka_perf_trampoline_get_deduction(
void) {
144#if PYTHON_VERSION < 0x3d0 || PYTHON_VERSION >= 0x3f0
145 return sizeof(
struct _Nuitka_perf_trampoline_missing_delta) - sizeof(int);
147 int ver = Nuitka_GetRuntimeVersion();
150 if ((ver >= 0x3d0 && ver < 0x3e0 && ver >= 0x3dc) || (ver >= 0x3e0 && ver < 0x3f0 && ver >= 0x3e3)) {
151 return sizeof(
struct _Nuitka_perf_trampoline_missing_delta_late) - sizeof(int);
153 return sizeof(
struct _Nuitka_perf_trampoline_missing_delta_early) - sizeof(int);
158static inline size_t _Nuitka_perf_trampoline_get_deduction_compile_time(
void) {
159#if PYTHON_VERSION < 0x3d0 || PYTHON_VERSION >= 0x3f0
160 return sizeof(
struct _Nuitka_perf_trampoline_missing_delta) - sizeof(int);
163#if (PYTHON_VERSION >= 0x3d0 && PYTHON_VERSION < 0x3e0 && PYTHON_VERSION >= 0x3dc) || \
164 (PYTHON_VERSION >= 0x3e0 && PYTHON_VERSION < 0x3f0 && PYTHON_VERSION >= 0x3e3)
165 return sizeof(
struct _Nuitka_perf_trampoline_missing_delta_late) - sizeof(int);
167 return sizeof(
struct _Nuitka_perf_trampoline_missing_delta_early) - sizeof(int);
173static inline size_t Nuitka_PyRuntime_GetOffset__PyRuntimeState_imports(
void) {
174 size_t c_offset = offsetof(_PyRuntimeState, imports);
175#if PYTHON_VERSION >= 0x3c0
176 if (c_offset > offsetof(_PyRuntimeState, ceval)) {
177#if defined(PY_HAVE_PERF_TRAMPOLINE)
178 c_offset -= _Nuitka_perf_trampoline_get_deduction_compile_time();
180 if (Nuitka_RuntimeHasPerfTrampoline()) {
181 c_offset += _Nuitka_perf_trampoline_get_deduction();
187#define NUITKA_PYRUNTIME_OFFSET__PyRuntimeState_imports Nuitka_PyRuntime_GetOffset__PyRuntimeState_imports()
188static inline size_t Nuitka_PyRuntime_GetOffset__PyRuntimeState_static_objects(
void) {
189 size_t c_offset = offsetof(_PyRuntimeState, static_objects);
190#if PYTHON_VERSION >= 0x3c0
191 if (c_offset > offsetof(_PyRuntimeState, ceval)) {
192#if defined(PY_HAVE_PERF_TRAMPOLINE)
193 c_offset -= _Nuitka_perf_trampoline_get_deduction_compile_time();
195 if (Nuitka_RuntimeHasPerfTrampoline()) {
196 c_offset += _Nuitka_perf_trampoline_get_deduction();
202#define NUITKA_PYRUNTIME_OFFSET__PyRuntimeState_static_objects \
203 Nuitka_PyRuntime_GetOffset__PyRuntimeState_static_objects()
204static inline size_t Nuitka_PyRuntime_GetOffset__PyRuntimeState_ceval(
void) {
205 size_t c_offset = offsetof(_PyRuntimeState, ceval);
206#if PYTHON_VERSION >= 0x3c0
207 if (c_offset > offsetof(_PyRuntimeState, ceval)) {
208#if defined(PY_HAVE_PERF_TRAMPOLINE)
209 c_offset -= _Nuitka_perf_trampoline_get_deduction_compile_time();
211 if (Nuitka_RuntimeHasPerfTrampoline()) {
212 c_offset += _Nuitka_perf_trampoline_get_deduction();
218#define NUITKA_PYRUNTIME_OFFSET__PyRuntimeState_ceval Nuitka_PyRuntime_GetOffset__PyRuntimeState_ceval()
219#if PYTHON_VERSION >= 0x3d0
220static inline size_t Nuitka_PyRuntime_GetOffset__PyRuntimeState_stoptheworld(
void) {
221 size_t c_offset = offsetof(_PyRuntimeState, stoptheworld);
222#if PYTHON_VERSION >= 0x3c0
223 if (c_offset > offsetof(_PyRuntimeState, ceval)) {
224#if defined(PY_HAVE_PERF_TRAMPOLINE)
225 c_offset -= _Nuitka_perf_trampoline_get_deduction_compile_time();
227 if (Nuitka_RuntimeHasPerfTrampoline()) {
228 c_offset += _Nuitka_perf_trampoline_get_deduction();
234#define NUITKA_PYRUNTIME_OFFSET__PyRuntimeState_stoptheworld Nuitka_PyRuntime_GetOffset__PyRuntimeState_stoptheworld()
237#if (!_NUITKA_MODULE_MODE && !defined(__MINGW64__)) || (!defined(__linux__) && !defined(_WIN32))
238#define NUITKA_PYRUNTIME_PTR(struct_start, comp) ((char *)(struct_start) + offsetof(_PyRuntimeState, comp))
240#define NUITKA_PYRUNTIME_PTR(struct_start, comp) \
241 ((char *)(struct_start) + NUITKA_PYRUNTIME_OFFSET__PyRuntimeState_##comp)
248#if defined(__GNUC__) || defined(__clang__)
249#define NUITKA_PYRUNTIME_DYNAMIC(var, comp, type) \
251 __typeof__(((_PyRuntimeState *)0)->comp) *_type_check = (type *)0; \
253 (type *)NUITKA_PYRUNTIME_PTR(var, comp); \
256#define NUITKA_PYRUNTIME_DYNAMIC(var, comp, type) ((type *)NUITKA_PYRUNTIME_PTR(var, comp))
264PyAPI_DATA(_PyRuntimeState) _PyRuntime;
266static inline _PyRuntimeState *_Nuitka_Get_PyRuntime_PTR(
void) {
return &_PyRuntime; }
267#define _Nuitka_PyRuntime_PTR (_Nuitka_Get_PyRuntime_PTR())
270#if PYTHON_VERSION >= 0x3c0
271#define Nuitka_PyRuntime__imports NUITKA_PYRUNTIME_DYNAMIC(_Nuitka_PyRuntime_PTR, imports, struct _import_runtime_state)
272#define Nuitka_PyRuntime__static_objects \
273 NUITKA_PYRUNTIME_DYNAMIC(_Nuitka_PyRuntime_PTR, static_objects, struct _Py_static_objects)
276#if PYTHON_VERSION >= 0x3d0
277#define _PyRuntime (*((struct NUITKA_ERROR_Do_not_use_PyRuntime_directly_use_NUITKA_PYRUNTIME_PTR *)0))