3#ifndef __NUITKA_COMPILED_FRAME_H__
4#define __NUITKA_COMPILED_FRAME_H__
8#include "nuitka/prelude.h"
14#if PYTHON_VERSION >= 0x3b0
21#if PYTHON_VERSION < 0x3b0
22typedef PyFrameObject Nuitka_ThreadStateFrameType;
24typedef _PyInterpreterFrame Nuitka_ThreadStateFrameType;
29extern void PRINT_TOP_FRAME(
char const *prefix);
30extern void PRINT_PYTHON_FRAME(
char const *prefix, PyFrameObject *frame);
32extern void PRINT_INTERPRETER_FRAME(
char const *prefix, Nuitka_ThreadStateFrameType *frame);
34#define PRINT_TOP_FRAME(prefix)
35#define PRINT_PYTHON_FRAME(prefix, frame)
36#define PRINT_COMPILED_FRAME(prefix, frame)
37#define PRINT_INTERPRETER_FRAME(prefix, frame)
41extern struct Nuitka_FrameObject *MAKE_MODULE_FRAME(PyCodeObject *code, PyObject *module);
42extern struct Nuitka_FrameObject *MAKE_FUNCTION_FRAME(PyThreadState *tstate, PyCodeObject *code, PyObject *module,
43 Py_ssize_t locals_size);
44extern struct Nuitka_FrameObject *MAKE_CLASS_FRAME(PyThreadState *tstate, PyCodeObject *code, PyObject *module,
45 PyObject *f_locals, Py_ssize_t locals_size);
46extern void Nuitka_Frame_AssignLocals(
struct Nuitka_FrameObject *frame_object, PyObject *locals_value);
51#if PYTHON_VERSION < 0x300
52#define MAKE_CODE_OBJECT(filename, line, flags, function_name, function_qualname, arg_names, free_vars, arg_count, \
53 kw_only_count, pos_only_count) \
54 makeCodeObject(filename, line, flags, function_name, arg_names, free_vars, arg_count)
55extern PyCodeObject *makeCodeObject(PyObject *filename,
int line,
int flags, PyObject *function_name,
56 PyObject *arg_names, PyObject *free_vars,
int arg_count);
57#elif PYTHON_VERSION < 0x380
58#define MAKE_CODE_OBJECT(filename, line, flags, function_name, function_qualname, arg_names, free_vars, arg_count, \
59 kw_only_count, pos_only_count) \
60 makeCodeObject(filename, line, flags, function_name, arg_names, free_vars, arg_count, kw_only_count)
61extern PyCodeObject *makeCodeObject(PyObject *filename,
int line,
int flags, PyObject *function_name,
62 PyObject *arg_names, PyObject *free_vars,
int arg_count,
int kw_only_count);
63#elif PYTHON_VERSION < 0x3b0
64#define MAKE_CODE_OBJECT(filename, line, flags, function_name, function_qualname, arg_names, free_vars, arg_count, \
65 kw_only_count, pos_only_count) \
66 makeCodeObject(filename, line, flags, function_name, arg_names, free_vars, arg_count, kw_only_count, pos_only_count)
67extern PyCodeObject *makeCodeObject(PyObject *filename,
int line,
int flags, PyObject *function_name,
68 PyObject *arg_names, PyObject *free_vars,
int arg_count,
int kw_only_count,
71#define MAKE_CODE_OBJECT(filename, line, flags, function_name, function_qualname, arg_names, free_vars, arg_count, \
72 kw_only_count, pos_only_count) \
73 makeCodeObject(filename, line, flags, function_name, function_qualname, arg_names, free_vars, arg_count, \
74 kw_only_count, pos_only_count)
75extern PyCodeObject *makeCodeObject(PyObject *filename,
int line,
int flags, PyObject *function_name,
76 PyObject *function_qualname, PyObject *arg_names, PyObject *free_vars,
77 int arg_count,
int kw_only_count,
int pos_only_count);
80NUITKA_MAY_BE_UNUSED
static inline bool isFakeCodeObject(PyCodeObject *code) {
81#if PYTHON_VERSION < 0x300
82 return code->co_code == const_str_empty;
83#elif PYTHON_VERSION < 0x3b0
84 return code->co_code == const_bytes_empty;
94extern PyCodeObject *USE_CODE_OBJECT(PyThreadState *tstate, PyObject *code_object, PyObject *module_filename_obj);
96extern PyTypeObject Nuitka_Frame_Type;
98static inline bool Nuitka_Frame_CheckExact(PyObject *
object) {
100 return Py_TYPE(
object) == &Nuitka_Frame_Type;
103static inline bool Nuitka_Frame_Check(PyObject *
object) {
106 if (!_PyObject_GC_IS_TRACKED(
object)) {
110 CHECK_OBJECT(
object);
112 if (Nuitka_Frame_CheckExact(
object)) {
116 return strcmp(Py_TYPE(
object)->tp_name,
"compiled_frame") == 0;
120 PyFrameObject m_frame;
122#if PYTHON_VERSION >= 0x3b0
123 PyObject *m_generator;
124 PyFrameState m_frame_state;
125 _PyInterpreterFrame m_interpreter_frame;
132 Py_ssize_t m_ob_size;
137 char const *m_type_description;
138 char m_locals_storage[1];
141inline static void CHECK_CODE_OBJECT(PyCodeObject *code_object) { CHECK_OBJECT(code_object); }
143NUITKA_MAY_BE_UNUSED
static inline bool isFrameUnusable(
struct Nuitka_FrameObject *frame_object) {
144 CHECK_OBJECT_X(frame_object);
148 frame_object == NULL ||
150 Py_REFCNT(frame_object) > 1 ||
151#if PYTHON_VERSION < 0x300
153 frame_object->m_frame.f_tstate != PyThreadState_GET() ||
156 frame_object->m_frame.f_back != NULL;
159 if (result && frame_object != NULL) {
160 PRINT_COMPILED_FRAME(
"NOT REUSING FRAME:", frame_object);
168extern int count_active_frame_cache_instances;
169extern int count_allocated_frame_cache_instances;
170extern int count_released_frame_cache_instances;
171extern int count_hit_frame_cache_instances;
175extern void dumpFrameStack(
void);
178#if PYTHON_VERSION >= 0x3b0
179inline static PyCodeObject *Nuitka_InterpreterFrame_GetCodeObject(_PyInterpreterFrame *frame) {
180#if PYTHON_VERSION < 0x3d0
181 PyCodeObject *result = frame->f_code;
182#elif PYTHON_VERSION < 0x3e0
183 PyCodeObject *result = (PyCodeObject *)frame->f_executable;
185 assert(!PyStackRef_IsNull(frame->f_executable));
186 PyCodeObject *result = (PyCodeObject *)PyStackRef_AsPyObjectBorrow(frame->f_executable);
187 CHECK_CODE_OBJECT(result);
189 assert(result == (PyCodeObject *)Py_None || PyCode_Check(result));
194inline static PyCodeObject *Nuitka_Frame_GetCodeObject(PyFrameObject *frame) {
195#if PYTHON_VERSION >= 0x3b0
196 assert(frame->f_frame);
197 return Nuitka_InterpreterFrame_GetCodeObject(frame->f_frame);
199 return frame->f_code;
203inline static void assertPythonFrameObject(PyFrameObject *frame_object) {
208 CHECK_OBJECT(frame_object);
210 CHECK_CODE_OBJECT(Nuitka_Frame_GetCodeObject(frame_object));
214 CHECK_OBJECT(frame_object);
220 assertPythonFrameObject(&frame_object->m_frame);
223inline static void assertThreadFrameObject(Nuitka_ThreadStateFrameType *frame) {
224#if PYTHON_VERSION < 0x3b0
225 assertPythonFrameObject(frame);
229 if (frame->frame_obj) {
230 assertPythonFrameObject(frame->frame_obj);
239#if PYTHON_VERSION >= 0x300
241#if PYTHON_VERSION < 0x3b0
243static inline void Nuitka_PythonFrame_MarkAsExecuting(PyFrameObject *frame) {
244#if PYTHON_VERSION >= 0x3a0
245 frame->f_state = FRAME_EXECUTING;
247 frame->f_executing = 1;
255#if PYTHON_VERSION >= 0x3b0
256 frame->m_frame_state = FRAME_EXECUTING;
257#elif PYTHON_VERSION >= 0x3a0
258 frame->m_frame.f_state = FRAME_EXECUTING;
260 frame->m_frame.f_executing = 1;
264#define Nuitka_Frame_MarkAsExecuting(frame) ;
267#if PYTHON_VERSION >= 0x300
270#if PYTHON_VERSION >= 0x3b0
271 frame->m_frame_state = FRAME_SUSPENDED;
272#elif PYTHON_VERSION >= 0x3a0
273 frame->m_frame.f_state = FRAME_SUSPENDED;
275 frame->m_frame.f_executing = 0;
279#define Nuitka_Frame_MarkAsNotExecuting(frame) ;
280#define Nuitka_PythonFrame_MarkAsExecuting(frame) ;
283#if PYTHON_VERSION >= 0x300
286#if PYTHON_VERSION >= 0x3b0
287 return frame->m_frame_state == FRAME_EXECUTING;
288#elif PYTHON_VERSION >= 0x3a0
289 return frame->m_frame.f_state == FRAME_EXECUTING;
291 return frame->m_frame.f_executing == 1;
296#if PYTHON_VERSION >= 0x3d0
299 return frame->m_frame_state == FRAME_SUSPENDED;
303#if PYTHON_VERSION >= 0x3b0
305#if PYTHON_VERSION < 0x3d0
306#define CURRENT_TSTATE_INTERPRETER_FRAME(tstate) tstate->cframe->current_frame
308#define CURRENT_TSTATE_INTERPRETER_FRAME(tstate) tstate->current_frame
311NUITKA_MAY_BE_UNUSED
inline static void pushFrameStackInterpreterFrame(PyThreadState *tstate,
312 _PyInterpreterFrame *interpreter_frame) {
313 _PyInterpreterFrame *old = CURRENT_TSTATE_INTERPRETER_FRAME(tstate);
314 interpreter_frame->previous = old;
315 CURRENT_TSTATE_INTERPRETER_FRAME(tstate) = interpreter_frame;
317 if (old != NULL && !_PyFrame_IsIncomplete(old) && interpreter_frame->frame_obj) {
318 interpreter_frame->frame_obj->f_back = old->frame_obj;
319 CHECK_OBJECT_X(old->frame_obj);
321 Py_XINCREF(old->frame_obj);
326NUITKA_MAY_BE_UNUSED
inline static void pushFrameStackPythonFrame(PyThreadState *tstate, PyFrameObject *frame_object) {
327 PRINT_TOP_FRAME(
"Normal push entry top frame:");
328 PRINT_COMPILED_FRAME(
"Pushing:", frame_object);
331 assertPythonFrameObject(frame_object);
333 PyFrameObject *old = tstate->frame;
337 assertPythonFrameObject(old);
338 CHECK_CODE_OBJECT(Nuitka_Frame_GetCodeObject(old));
342 assert(old != frame_object);
345 tstate->frame = frame_object;
349 frame_object->f_back = old;
352 Nuitka_PythonFrame_MarkAsExecuting(frame_object);
353 Py_INCREF(frame_object);
355 PRINT_TOP_FRAME(
"Normal push exit top frame:");
359NUITKA_MAY_BE_UNUSED
inline static void pushFrameStackCompiledFrame(PyThreadState *tstate,
361#if PYTHON_VERSION < 0x3b0
362 pushFrameStackPythonFrame(tstate, &frame_object->m_frame);
364 pushFrameStackInterpreterFrame(tstate, &frame_object->m_interpreter_frame);
366 Nuitka_Frame_MarkAsExecuting(frame_object);
367 Py_INCREF(frame_object);
371NUITKA_MAY_BE_UNUSED
inline static void popFrameStack(PyThreadState *tstate) {
373 PRINT_TOP_FRAME(
"Normal pop entry top frame:");
376#if PYTHON_VERSION < 0x3b0
378 CHECK_OBJECT(frame_object);
381 printf(
"Taking off frame %s %s\n", Nuitka_String_AsString(PyObject_Str((PyObject *)frame_object)),
382 Nuitka_String_AsString(PyObject_Repr((PyObject *)Nuitka_Frame_GetCodeObject(&frame_object->m_frame))));
386 tstate->frame = frame_object->m_frame.f_back;
387 frame_object->m_frame.f_back = NULL;
389 Nuitka_Frame_MarkAsNotExecuting(frame_object);
390 Py_DECREF(frame_object);
392 assert(CURRENT_TSTATE_INTERPRETER_FRAME(tstate));
396 CHECK_OBJECT(frame_object);
398 CURRENT_TSTATE_INTERPRETER_FRAME(tstate) = CURRENT_TSTATE_INTERPRETER_FRAME(tstate)->previous;
400 Nuitka_Frame_MarkAsNotExecuting(frame_object);
402 CHECK_OBJECT_X(frame_object->m_frame.f_back);
403 Py_CLEAR(frame_object->m_frame.f_back);
405 Py_DECREF(frame_object);
407 frame_object->m_interpreter_frame.previous = NULL;
411 PRINT_TOP_FRAME(
"Normal pop exit top frame:");
415#if PYTHON_VERSION >= 0x300
416NUITKA_MAY_BE_UNUSED
static void Nuitka_SetFrameGenerator(
struct Nuitka_FrameObject *nuitka_frame,
417 PyObject *generator) {
418#if PYTHON_VERSION < 0x3b0
419 nuitka_frame->m_frame.f_gen = generator;
421 nuitka_frame->m_generator = generator;
426 Nuitka_Frame_MarkAsExecuting(nuitka_frame);
431NUITKA_MAY_BE_UNUSED
static PyCodeObject *Nuitka_GetFrameCodeObject(
struct Nuitka_FrameObject *nuitka_frame) {
432#if PYTHON_VERSION < 0x3b0
433 return nuitka_frame->m_frame.f_code;
435 return Nuitka_InterpreterFrame_GetCodeObject(&nuitka_frame->m_interpreter_frame);
439NUITKA_MAY_BE_UNUSED
static int Nuitka_GetFrameLineNumber(
struct Nuitka_FrameObject *nuitka_frame) {
440 return nuitka_frame->m_frame.f_lineno;
443NUITKA_MAY_BE_UNUSED
static PyObject **Nuitka_GetCodeVarNames(PyCodeObject *code_object) {
444#if PYTHON_VERSION < 0x3b0
445 return &PyTuple_GET_ITEM(code_object->co_varnames, 0);
450 return &PyTuple_GET_ITEM(code_object->co_localsplusnames, 0);
455extern void Nuitka_Frame_AttachLocals(
struct Nuitka_FrameObject *frame,
char const *type_description, ...);
457NUITKA_MAY_BE_UNUSED
static Nuitka_ThreadStateFrameType *_Nuitka_GetThreadStateFrame(PyThreadState *tstate) {
458#if PYTHON_VERSION < 0x3b0
459 return tstate->frame;
461 return CURRENT_TSTATE_INTERPRETER_FRAME(tstate);
465NUITKA_MAY_BE_UNUSED
inline static void pushFrameStackGenerator(PyThreadState *tstate,
466 Nuitka_ThreadStateFrameType *frame_object) {
467#if PYTHON_VERSION < 0x3b0
468 Nuitka_ThreadStateFrameType *return_frame = _Nuitka_GetThreadStateFrame(tstate);
470 Py_XINCREF(return_frame);
472 pushFrameStackPythonFrame(tstate, frame_object);
473 Py_DECREF(frame_object);
475 pushFrameStackInterpreterFrame(tstate, frame_object);
479NUITKA_MAY_BE_UNUSED
inline static void pushFrameStackGeneratorCompiledFrame(PyThreadState *tstate,
481#if PYTHON_VERSION < 0x3b0
482 pushFrameStackGenerator(tstate, &frame_object->m_frame);
484 pushFrameStackGenerator(tstate, &frame_object->m_interpreter_frame);
489#define NUITKA_TYPE_DESCRIPTION_NULL 'N'
490#define NUITKA_TYPE_DESCRIPTION_CELL 'c'
491#define NUITKA_TYPE_DESCRIPTION_OBJECT 'o'
492#define NUITKA_TYPE_DESCRIPTION_OBJECT_PTR 'O'
493#define NUITKA_TYPE_DESCRIPTION_BOOL 'b'
494#define NUITKA_TYPE_DESCRIPTION_NILONG 'L'
497extern int count_active_Nuitka_Frame_Type;
498extern int count_allocated_Nuitka_Frame_Type;
499extern int count_released_Nuitka_Frame_Type;
Definition compiled_frame.h:119