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);
49#if PYTHON_VERSION < 0x300
50#define MAKE_CODE_OBJECT(filename, line, flags, function_name, function_qualname, arg_names, free_vars, arg_count, \
51 kw_only_count, pos_only_count) \
52 makeCodeObject(filename, line, flags, function_name, arg_names, free_vars, arg_count)
53extern PyCodeObject *makeCodeObject(PyObject *filename,
int line,
int flags, PyObject *function_name,
54 PyObject *arg_names, PyObject *free_vars,
int arg_count);
55#elif PYTHON_VERSION < 0x380
56#define MAKE_CODE_OBJECT(filename, line, flags, function_name, function_qualname, arg_names, free_vars, arg_count, \
57 kw_only_count, pos_only_count) \
58 makeCodeObject(filename, line, flags, function_name, arg_names, free_vars, arg_count, kw_only_count)
59extern PyCodeObject *makeCodeObject(PyObject *filename,
int line,
int flags, PyObject *function_name,
60 PyObject *arg_names, PyObject *free_vars,
int arg_count,
int kw_only_count);
61#elif PYTHON_VERSION < 0x3b0
62#define MAKE_CODE_OBJECT(filename, line, flags, function_name, function_qualname, arg_names, free_vars, arg_count, \
63 kw_only_count, pos_only_count) \
64 makeCodeObject(filename, line, flags, function_name, arg_names, free_vars, arg_count, kw_only_count, pos_only_count)
65extern PyCodeObject *makeCodeObject(PyObject *filename,
int line,
int flags, PyObject *function_name,
66 PyObject *arg_names, PyObject *free_vars,
int arg_count,
int kw_only_count,
69#define MAKE_CODE_OBJECT(filename, line, flags, function_name, function_qualname, arg_names, free_vars, arg_count, \
70 kw_only_count, pos_only_count) \
71 makeCodeObject(filename, line, flags, function_name, function_qualname, arg_names, free_vars, arg_count, \
72 kw_only_count, pos_only_count)
73extern PyCodeObject *makeCodeObject(PyObject *filename,
int line,
int flags, PyObject *function_name,
74 PyObject *function_qualname, PyObject *arg_names, PyObject *free_vars,
75 int arg_count,
int kw_only_count,
int pos_only_count);
78NUITKA_MAY_BE_UNUSED
static inline bool isFakeCodeObject(PyCodeObject *code) {
79#if PYTHON_VERSION < 0x300
80 return code->co_code == const_str_empty;
81#elif PYTHON_VERSION < 0x3b0
82 return code->co_code == const_bytes_empty;
92extern PyCodeObject *USE_CODE_OBJECT(PyThreadState *tstate, PyObject *code_object, PyObject *module_filename_obj);
94extern PyTypeObject Nuitka_Frame_Type;
96static inline bool Nuitka_Frame_CheckExact(PyObject *
object) {
98 return Py_TYPE(
object) == &Nuitka_Frame_Type;
101static inline bool Nuitka_Frame_Check(PyObject *
object) {
104 if (!_PyObject_GC_IS_TRACKED(
object)) {
108 CHECK_OBJECT(
object);
110 if (Nuitka_Frame_CheckExact(
object)) {
114 return strcmp(Py_TYPE(
object)->tp_name,
"compiled_frame") == 0;
118 PyFrameObject m_frame;
120#if PYTHON_VERSION >= 0x3b0
121 PyObject *m_generator;
122 PyFrameState m_frame_state;
123 _PyInterpreterFrame m_interpreter_frame;
130 Py_ssize_t m_ob_size;
135 char const *m_type_description;
136 char m_locals_storage[1];
139inline static void CHECK_CODE_OBJECT(PyCodeObject *code_object) { CHECK_OBJECT(code_object); }
141NUITKA_MAY_BE_UNUSED
static inline bool isFrameUnusable(
struct Nuitka_FrameObject *frame_object) {
142 CHECK_OBJECT_X(frame_object);
146 frame_object == NULL ||
148 Py_REFCNT(frame_object) > 1 ||
149#if PYTHON_VERSION < 0x300
151 frame_object->m_frame.f_tstate != PyThreadState_GET() ||
154 frame_object->m_frame.f_back != NULL;
157 if (result && frame_object != NULL) {
158 PRINT_COMPILED_FRAME(
"NOT REUSING FRAME:", frame_object);
166extern int count_active_frame_cache_instances;
167extern int count_allocated_frame_cache_instances;
168extern int count_released_frame_cache_instances;
169extern int count_hit_frame_cache_instances;
173extern void dumpFrameStack(
void);
176#if PYTHON_VERSION >= 0x3b0
177inline static PyCodeObject *Nuitka_InterpreterFrame_GetCodeObject(_PyInterpreterFrame *frame) {
178#if PYTHON_VERSION < 0x3d0
179 PyCodeObject *result = frame->f_code;
180#elif PYTHON_VERSION < 0x3e0
181 PyCodeObject *result = (PyCodeObject *)frame->f_executable;
183 assert(!PyStackRef_IsNull(frame->f_executable));
184 PyCodeObject *result = (PyCodeObject *)PyStackRef_AsPyObjectBorrow(frame->f_executable);
186 assert(PyCode_Check(result));
191inline static PyCodeObject *Nuitka_Frame_GetCodeObject(PyFrameObject *frame) {
192#if PYTHON_VERSION >= 0x3b0
193 assert(frame->f_frame);
194 return Nuitka_InterpreterFrame_GetCodeObject(frame->f_frame);
196 return frame->f_code;
200inline static void assertPythonFrameObject(PyFrameObject *frame_object) {
205 CHECK_OBJECT(frame_object);
207 CHECK_CODE_OBJECT(Nuitka_Frame_GetCodeObject(frame_object));
211 CHECK_OBJECT(frame_object);
217 assertPythonFrameObject(&frame_object->m_frame);
220inline static void assertThreadFrameObject(Nuitka_ThreadStateFrameType *frame) {
221#if PYTHON_VERSION < 0x3b0
222 assertPythonFrameObject(frame);
226 if (frame->frame_obj) {
227 assertPythonFrameObject(frame->frame_obj);
236#if PYTHON_VERSION >= 0x300
238#if PYTHON_VERSION < 0x3b0
240static inline void Nuitka_PythonFrame_MarkAsExecuting(PyFrameObject *frame) {
241#if PYTHON_VERSION >= 0x3a0
242 frame->f_state = FRAME_EXECUTING;
244 frame->f_executing = 1;
252#if PYTHON_VERSION >= 0x3b0
253 frame->m_frame_state = FRAME_EXECUTING;
254#elif PYTHON_VERSION >= 0x3a0
255 frame->m_frame.f_state = FRAME_EXECUTING;
257 frame->m_frame.f_executing = 1;
261#define Nuitka_Frame_MarkAsExecuting(frame) ;
264#if PYTHON_VERSION >= 0x300
267#if PYTHON_VERSION >= 0x3b0
268 frame->m_frame_state = FRAME_SUSPENDED;
269#elif PYTHON_VERSION >= 0x3a0
270 frame->m_frame.f_state = FRAME_SUSPENDED;
272 frame->m_frame.f_executing = 0;
276#define Nuitka_Frame_MarkAsNotExecuting(frame) ;
277#define Nuitka_PythonFrame_MarkAsExecuting(frame) ;
280#if PYTHON_VERSION >= 0x300
283#if PYTHON_VERSION >= 0x3b0
284 return frame->m_frame_state == FRAME_EXECUTING;
285#elif PYTHON_VERSION >= 0x3a0
286 return frame->m_frame.f_state == FRAME_EXECUTING;
288 return frame->m_frame.f_executing == 1;
293#if PYTHON_VERSION >= 0x3b0
295#if PYTHON_VERSION < 0x3d0
296#define CURRENT_TSTATE_INTERPRETER_FRAME(tstate) tstate->cframe->current_frame
298#define CURRENT_TSTATE_INTERPRETER_FRAME(tstate) tstate->current_frame
301NUITKA_MAY_BE_UNUSED
inline static void pushFrameStackInterpreterFrame(PyThreadState *tstate,
302 _PyInterpreterFrame *interpreter_frame) {
303 _PyInterpreterFrame *old = CURRENT_TSTATE_INTERPRETER_FRAME(tstate);
304 interpreter_frame->previous = old;
305 CURRENT_TSTATE_INTERPRETER_FRAME(tstate) = interpreter_frame;
307 if (old != NULL && !_PyFrame_IsIncomplete(old) && interpreter_frame->frame_obj) {
308 interpreter_frame->frame_obj->f_back = old->frame_obj;
309 CHECK_OBJECT_X(old->frame_obj);
311 Py_XINCREF(old->frame_obj);
316NUITKA_MAY_BE_UNUSED
inline static void pushFrameStackPythonFrame(PyThreadState *tstate, PyFrameObject *frame_object) {
317 PRINT_TOP_FRAME(
"Normal push entry top frame:");
318 PRINT_COMPILED_FRAME(
"Pushing:", frame_object);
321 assertPythonFrameObject(frame_object);
323 PyFrameObject *old = tstate->frame;
327 assertPythonFrameObject(old);
328 CHECK_CODE_OBJECT(Nuitka_Frame_GetCodeObject(old));
332 assert(old != frame_object);
335 tstate->frame = frame_object;
339 frame_object->f_back = old;
342 Nuitka_PythonFrame_MarkAsExecuting(frame_object);
343 Py_INCREF(frame_object);
345 PRINT_TOP_FRAME(
"Normal push exit top frame:");
349NUITKA_MAY_BE_UNUSED
inline static void pushFrameStackCompiledFrame(PyThreadState *tstate,
351#if PYTHON_VERSION < 0x3b0
352 pushFrameStackPythonFrame(tstate, &frame_object->m_frame);
354 pushFrameStackInterpreterFrame(tstate, &frame_object->m_interpreter_frame);
356 Nuitka_Frame_MarkAsExecuting(frame_object);
357 Py_INCREF(frame_object);
361NUITKA_MAY_BE_UNUSED
inline static void popFrameStack(PyThreadState *tstate) {
363 PRINT_TOP_FRAME(
"Normal pop entry top frame:");
366#if PYTHON_VERSION < 0x3b0
368 CHECK_OBJECT(frame_object);
371 printf(
"Taking off frame %s %s\n", Nuitka_String_AsString(PyObject_Str((PyObject *)frame_object)),
372 Nuitka_String_AsString(PyObject_Repr((PyObject *)Nuitka_Frame_GetCodeObject(&frame_object->m_frame))));
376 tstate->frame = frame_object->m_frame.f_back;
377 frame_object->m_frame.f_back = NULL;
379 Nuitka_Frame_MarkAsNotExecuting(frame_object);
380 Py_DECREF(frame_object);
382 assert(CURRENT_TSTATE_INTERPRETER_FRAME(tstate));
386 CHECK_OBJECT(frame_object);
388 CURRENT_TSTATE_INTERPRETER_FRAME(tstate) = CURRENT_TSTATE_INTERPRETER_FRAME(tstate)->previous;
390 Nuitka_Frame_MarkAsNotExecuting(frame_object);
392 CHECK_OBJECT_X(frame_object->m_frame.f_back);
393 Py_CLEAR(frame_object->m_frame.f_back);
395 Py_DECREF(frame_object);
397 frame_object->m_interpreter_frame.previous = NULL;
401 PRINT_TOP_FRAME(
"Normal pop exit top frame:");
405#if PYTHON_VERSION >= 0x300
406NUITKA_MAY_BE_UNUSED
static void Nuitka_SetFrameGenerator(
struct Nuitka_FrameObject *nuitka_frame,
407 PyObject *generator) {
408#if PYTHON_VERSION < 0x3b0
409 nuitka_frame->m_frame.f_gen = generator;
411 nuitka_frame->m_generator = generator;
416 Nuitka_Frame_MarkAsExecuting(nuitka_frame);
421NUITKA_MAY_BE_UNUSED
static PyCodeObject *Nuitka_GetFrameCodeObject(
struct Nuitka_FrameObject *nuitka_frame) {
422#if PYTHON_VERSION < 0x3b0
423 return nuitka_frame->m_frame.f_code;
425 return Nuitka_InterpreterFrame_GetCodeObject(&nuitka_frame->m_interpreter_frame);
429NUITKA_MAY_BE_UNUSED
static int Nuitka_GetFrameLineNumber(
struct Nuitka_FrameObject *nuitka_frame) {
430 return nuitka_frame->m_frame.f_lineno;
433NUITKA_MAY_BE_UNUSED
static PyObject **Nuitka_GetCodeVarNames(PyCodeObject *code_object) {
434#if PYTHON_VERSION < 0x3b0
435 return &PyTuple_GET_ITEM(code_object->co_varnames, 0);
440 return &PyTuple_GET_ITEM(code_object->co_localsplusnames, 0);
445extern void Nuitka_Frame_AttachLocals(
struct Nuitka_FrameObject *frame,
char const *type_description, ...);
447NUITKA_MAY_BE_UNUSED
static Nuitka_ThreadStateFrameType *_Nuitka_GetThreadStateFrame(PyThreadState *tstate) {
448#if PYTHON_VERSION < 0x3b0
449 return tstate->frame;
451 return CURRENT_TSTATE_INTERPRETER_FRAME(tstate);
455NUITKA_MAY_BE_UNUSED
inline static void pushFrameStackGenerator(PyThreadState *tstate,
456 Nuitka_ThreadStateFrameType *frame_object) {
457#if PYTHON_VERSION < 0x3b0
458 Nuitka_ThreadStateFrameType *return_frame = _Nuitka_GetThreadStateFrame(tstate);
460 Py_XINCREF(return_frame);
462 pushFrameStackPythonFrame(tstate, frame_object);
463 Py_DECREF(frame_object);
465 pushFrameStackInterpreterFrame(tstate, frame_object);
469NUITKA_MAY_BE_UNUSED
inline static void pushFrameStackGeneratorCompiledFrame(PyThreadState *tstate,
471#if PYTHON_VERSION < 0x3b0
472 pushFrameStackGenerator(tstate, &frame_object->m_frame);
474 pushFrameStackGenerator(tstate, &frame_object->m_interpreter_frame);
479#define NUITKA_TYPE_DESCRIPTION_NULL 'N'
480#define NUITKA_TYPE_DESCRIPTION_CELL 'c'
481#define NUITKA_TYPE_DESCRIPTION_OBJECT 'o'
482#define NUITKA_TYPE_DESCRIPTION_OBJECT_PTR 'O'
483#define NUITKA_TYPE_DESCRIPTION_BOOL 'b'
484#define NUITKA_TYPE_DESCRIPTION_NILONG 'L'
487extern int count_active_Nuitka_Frame_Type;
488extern int count_allocated_Nuitka_Frame_Type;
489extern int count_released_Nuitka_Frame_Type;
Definition compiled_frame.h:117