Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
allendowney
GitHub Repository: allendowney/cpython
Path: blob/main/Include/internal/pycore_frame.h
12 views
1
#ifndef Py_INTERNAL_FRAME_H
2
#define Py_INTERNAL_FRAME_H
3
#ifdef __cplusplus
4
extern "C" {
5
#endif
6
7
#include <stdbool.h>
8
#include <stddef.h>
9
#include "pycore_code.h" // STATS
10
11
/* See Objects/frame_layout.md for an explanation of the frame stack
12
* including explanation of the PyFrameObject and _PyInterpreterFrame
13
* structs. */
14
15
16
struct _frame {
17
PyObject_HEAD
18
PyFrameObject *f_back; /* previous frame, or NULL */
19
struct _PyInterpreterFrame *f_frame; /* points to the frame data */
20
PyObject *f_trace; /* Trace function */
21
int f_lineno; /* Current line number. Only valid if non-zero */
22
char f_trace_lines; /* Emit per-line trace events? */
23
char f_trace_opcodes; /* Emit per-opcode trace events? */
24
char f_fast_as_locals; /* Have the fast locals of this frame been converted to a dict? */
25
/* The frame data, if this frame object owns the frame */
26
PyObject *_f_frame_data[1];
27
};
28
29
extern PyFrameObject* _PyFrame_New_NoTrack(PyCodeObject *code);
30
31
32
/* other API */
33
34
typedef enum _framestate {
35
FRAME_CREATED = -2,
36
FRAME_SUSPENDED = -1,
37
FRAME_EXECUTING = 0,
38
FRAME_COMPLETED = 1,
39
FRAME_CLEARED = 4
40
} PyFrameState;
41
42
enum _frameowner {
43
FRAME_OWNED_BY_THREAD = 0,
44
FRAME_OWNED_BY_GENERATOR = 1,
45
FRAME_OWNED_BY_FRAME_OBJECT = 2,
46
FRAME_OWNED_BY_CSTACK = 3,
47
};
48
49
typedef struct _PyInterpreterFrame {
50
PyObject *f_executable; /* Strong reference */
51
struct _PyInterpreterFrame *previous;
52
PyObject *f_funcobj; /* Strong reference. Only valid if not on C stack */
53
PyObject *f_globals; /* Borrowed reference. Only valid if not on C stack */
54
PyObject *f_builtins; /* Borrowed reference. Only valid if not on C stack */
55
PyObject *f_locals; /* Strong reference, may be NULL. Only valid if not on C stack */
56
PyFrameObject *frame_obj; /* Strong reference, may be NULL. Only valid if not on C stack */
57
// NOTE: This is not necessarily the last instruction started in the given
58
// frame. Rather, it is the code unit *prior to* the *next* instruction. For
59
// example, it may be an inline CACHE entry, an instruction we just jumped
60
// over, or (in the case of a newly-created frame) a totally invalid value:
61
_Py_CODEUNIT *prev_instr;
62
int stacktop; /* Offset of TOS from localsplus */
63
/* The return_offset determines where a `RETURN` should go in the caller,
64
* relative to `prev_instr`.
65
* It is only meaningful to the callee,
66
* so it needs to be set in any CALL (to a Python function)
67
* or SEND (to a coroutine or generator).
68
* If there is no callee, then it is meaningless. */
69
uint16_t return_offset;
70
char owner;
71
/* Locals and stack */
72
PyObject *localsplus[1];
73
} _PyInterpreterFrame;
74
75
#define _PyInterpreterFrame_LASTI(IF) \
76
((int)((IF)->prev_instr - _PyCode_CODE(_PyFrame_GetCode(IF))))
77
78
static inline PyCodeObject *_PyFrame_GetCode(_PyInterpreterFrame *f) {
79
assert(PyCode_Check(f->f_executable));
80
return (PyCodeObject *)f->f_executable;
81
}
82
83
static inline PyObject **_PyFrame_Stackbase(_PyInterpreterFrame *f) {
84
return f->localsplus + _PyFrame_GetCode(f)->co_nlocalsplus;
85
}
86
87
static inline PyObject *_PyFrame_StackPeek(_PyInterpreterFrame *f) {
88
assert(f->stacktop > _PyFrame_GetCode(f)->co_nlocalsplus);
89
assert(f->localsplus[f->stacktop-1] != NULL);
90
return f->localsplus[f->stacktop-1];
91
}
92
93
static inline PyObject *_PyFrame_StackPop(_PyInterpreterFrame *f) {
94
assert(f->stacktop > _PyFrame_GetCode(f)->co_nlocalsplus);
95
f->stacktop--;
96
return f->localsplus[f->stacktop];
97
}
98
99
static inline void _PyFrame_StackPush(_PyInterpreterFrame *f, PyObject *value) {
100
f->localsplus[f->stacktop] = value;
101
f->stacktop++;
102
}
103
104
#define FRAME_SPECIALS_SIZE ((int)((sizeof(_PyInterpreterFrame)-1)/sizeof(PyObject *)))
105
106
static inline int
107
_PyFrame_NumSlotsForCodeObject(PyCodeObject *code)
108
{
109
/* This function needs to remain in sync with the calculation of
110
* co_framesize in Tools/build/deepfreeze.py */
111
assert(code->co_framesize >= FRAME_SPECIALS_SIZE);
112
return code->co_framesize - FRAME_SPECIALS_SIZE;
113
}
114
115
void _PyFrame_Copy(_PyInterpreterFrame *src, _PyInterpreterFrame *dest);
116
117
/* Consumes reference to func and locals.
118
Does not initialize frame->previous, which happens
119
when frame is linked into the frame stack.
120
*/
121
static inline void
122
_PyFrame_Initialize(
123
_PyInterpreterFrame *frame, PyFunctionObject *func,
124
PyObject *locals, PyCodeObject *code, int null_locals_from)
125
{
126
frame->f_funcobj = (PyObject *)func;
127
frame->f_executable = Py_NewRef(code);
128
frame->f_builtins = func->func_builtins;
129
frame->f_globals = func->func_globals;
130
frame->f_locals = locals;
131
frame->stacktop = code->co_nlocalsplus;
132
frame->frame_obj = NULL;
133
frame->prev_instr = _PyCode_CODE(code) - 1;
134
frame->return_offset = 0;
135
frame->owner = FRAME_OWNED_BY_THREAD;
136
137
for (int i = null_locals_from; i < code->co_nlocalsplus; i++) {
138
frame->localsplus[i] = NULL;
139
}
140
}
141
142
/* Gets the pointer to the locals array
143
* that precedes this frame.
144
*/
145
static inline PyObject**
146
_PyFrame_GetLocalsArray(_PyInterpreterFrame *frame)
147
{
148
return frame->localsplus;
149
}
150
151
/* Fetches the stack pointer, and sets stacktop to -1.
152
Having stacktop <= 0 ensures that invalid
153
values are not visible to the cycle GC.
154
We choose -1 rather than 0 to assist debugging. */
155
static inline PyObject**
156
_PyFrame_GetStackPointer(_PyInterpreterFrame *frame)
157
{
158
PyObject **sp = frame->localsplus + frame->stacktop;
159
frame->stacktop = -1;
160
return sp;
161
}
162
163
static inline void
164
_PyFrame_SetStackPointer(_PyInterpreterFrame *frame, PyObject **stack_pointer)
165
{
166
frame->stacktop = (int)(stack_pointer - frame->localsplus);
167
}
168
169
/* Determine whether a frame is incomplete.
170
* A frame is incomplete if it is part way through
171
* creating cell objects or a generator or coroutine.
172
*
173
* Frames on the frame stack are incomplete until the
174
* first RESUME instruction.
175
* Frames owned by a generator are always complete.
176
*/
177
static inline bool
178
_PyFrame_IsIncomplete(_PyInterpreterFrame *frame)
179
{
180
if (frame->owner == FRAME_OWNED_BY_CSTACK) {
181
return true;
182
}
183
return frame->owner != FRAME_OWNED_BY_GENERATOR &&
184
frame->prev_instr < _PyCode_CODE(_PyFrame_GetCode(frame)) + _PyFrame_GetCode(frame)->_co_firsttraceable;
185
}
186
187
static inline _PyInterpreterFrame *
188
_PyFrame_GetFirstComplete(_PyInterpreterFrame *frame)
189
{
190
while (frame && _PyFrame_IsIncomplete(frame)) {
191
frame = frame->previous;
192
}
193
return frame;
194
}
195
196
static inline _PyInterpreterFrame *
197
_PyThreadState_GetFrame(PyThreadState *tstate)
198
{
199
return _PyFrame_GetFirstComplete(tstate->cframe->current_frame);
200
}
201
202
/* For use by _PyFrame_GetFrameObject
203
Do not call directly. */
204
PyFrameObject *
205
_PyFrame_MakeAndSetFrameObject(_PyInterpreterFrame *frame);
206
207
/* Gets the PyFrameObject for this frame, lazily
208
* creating it if necessary.
209
* Returns a borrowed referennce */
210
static inline PyFrameObject *
211
_PyFrame_GetFrameObject(_PyInterpreterFrame *frame)
212
{
213
214
assert(!_PyFrame_IsIncomplete(frame));
215
PyFrameObject *res = frame->frame_obj;
216
if (res != NULL) {
217
return res;
218
}
219
return _PyFrame_MakeAndSetFrameObject(frame);
220
}
221
222
/* Clears all references in the frame.
223
* If take is non-zero, then the _PyInterpreterFrame frame
224
* may be transferred to the frame object it references
225
* instead of being cleared. Either way
226
* the caller no longer owns the references
227
* in the frame.
228
* take should be set to 1 for heap allocated
229
* frames like the ones in generators and coroutines.
230
*/
231
void
232
_PyFrame_ClearExceptCode(_PyInterpreterFrame * frame);
233
234
int
235
_PyFrame_Traverse(_PyInterpreterFrame *frame, visitproc visit, void *arg);
236
237
int
238
_PyFrame_FastToLocalsWithError(_PyInterpreterFrame *frame);
239
240
void
241
_PyFrame_LocalsToFast(_PyInterpreterFrame *frame, int clear);
242
243
static inline bool
244
_PyThreadState_HasStackSpace(PyThreadState *tstate, int size)
245
{
246
assert(
247
(tstate->datastack_top == NULL && tstate->datastack_limit == NULL)
248
||
249
(tstate->datastack_top != NULL && tstate->datastack_limit != NULL)
250
);
251
return tstate->datastack_top != NULL &&
252
size < tstate->datastack_limit - tstate->datastack_top;
253
}
254
255
extern _PyInterpreterFrame *
256
_PyThreadState_PushFrame(PyThreadState *tstate, size_t size);
257
258
void _PyThreadState_PopFrame(PyThreadState *tstate, _PyInterpreterFrame *frame);
259
260
/* Pushes a frame without checking for space.
261
* Must be guarded by _PyThreadState_HasStackSpace()
262
* Consumes reference to func. */
263
static inline _PyInterpreterFrame *
264
_PyFrame_PushUnchecked(PyThreadState *tstate, PyFunctionObject *func, int null_locals_from)
265
{
266
CALL_STAT_INC(frames_pushed);
267
PyCodeObject *code = (PyCodeObject *)func->func_code;
268
_PyInterpreterFrame *new_frame = (_PyInterpreterFrame *)tstate->datastack_top;
269
tstate->datastack_top += code->co_framesize;
270
assert(tstate->datastack_top < tstate->datastack_limit);
271
_PyFrame_Initialize(new_frame, func, NULL, code, null_locals_from);
272
return new_frame;
273
}
274
275
/* Pushes a trampoline frame without checking for space.
276
* Must be guarded by _PyThreadState_HasStackSpace() */
277
static inline _PyInterpreterFrame *
278
_PyFrame_PushTrampolineUnchecked(PyThreadState *tstate, PyCodeObject *code, int stackdepth, int prev_instr)
279
{
280
CALL_STAT_INC(frames_pushed);
281
_PyInterpreterFrame *frame = (_PyInterpreterFrame *)tstate->datastack_top;
282
tstate->datastack_top += code->co_framesize;
283
assert(tstate->datastack_top < tstate->datastack_limit);
284
frame->f_funcobj = Py_None;
285
frame->f_executable = Py_NewRef(code);
286
#ifdef Py_DEBUG
287
frame->f_builtins = NULL;
288
frame->f_globals = NULL;
289
#endif
290
frame->f_locals = NULL;
291
frame->stacktop = code->co_nlocalsplus + stackdepth;
292
frame->frame_obj = NULL;
293
frame->prev_instr = _PyCode_CODE(code) + prev_instr;
294
frame->owner = FRAME_OWNED_BY_THREAD;
295
frame->return_offset = 0;
296
return frame;
297
}
298
299
static inline
300
PyGenObject *_PyFrame_GetGenerator(_PyInterpreterFrame *frame)
301
{
302
assert(frame->owner == FRAME_OWNED_BY_GENERATOR);
303
size_t offset_in_gen = offsetof(PyGenObject, gi_iframe);
304
return (PyGenObject *)(((char *)frame) - offset_in_gen);
305
}
306
307
#define PY_EXECUTABLE_KIND_SKIP 0
308
#define PY_EXECUTABLE_KIND_PY_FUNCTION 1
309
#define PY_EXECUTABLE_KIND_BUILTIN_FUNCTION 3
310
#define PY_EXECUTABLE_KIND_METHOD_DESCRIPTOR 4
311
#define PY_EXECUTABLE_KINDS 5
312
313
PyAPI_DATA(const PyTypeObject *) const PyUnstable_ExecutableKinds[PY_EXECUTABLE_KINDS+1];
314
315
#ifdef __cplusplus
316
}
317
#endif
318
#endif /* !Py_INTERNAL_FRAME_H */
319
320