Skip to content

Commit 1e197e6

Browse files
authored
GH-96421: Insert shim frame on entry to interpreter (GH-96319)
* Adds EXIT_INTERPRETER instruction to exit PyEval_EvalDefault() * Simplifies RETURN_VALUE, YIELD_VALUE and RETURN_GENERATOR instructions as they no longer need to check for entry frames.
1 parent dbf2faf commit 1e197e6

24 files changed

+448
-344
lines changed

Include/internal/pycore_code.h

+10
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,16 @@ _PyCode_LineNumberFromArray(PyCodeObject *co, int index)
456456
}
457457
}
458458

459+
typedef struct _PyShimCodeDef {
460+
const uint8_t *code;
461+
int codelen;
462+
int stacksize;
463+
const char *cname;
464+
} _PyShimCodeDef;
465+
466+
extern PyCodeObject *
467+
_Py_MakeShimCode(const _PyShimCodeDef *code);
468+
459469

460470
#ifdef __cplusplus
461471
}

Include/internal/pycore_frame.h

+8-9
Original file line numberDiff line numberDiff line change
@@ -42,27 +42,27 @@ typedef enum _framestate {
4242
enum _frameowner {
4343
FRAME_OWNED_BY_THREAD = 0,
4444
FRAME_OWNED_BY_GENERATOR = 1,
45-
FRAME_OWNED_BY_FRAME_OBJECT = 2
45+
FRAME_OWNED_BY_FRAME_OBJECT = 2,
46+
FRAME_OWNED_BY_CSTACK = 3,
4647
};
4748

4849
typedef struct _PyInterpreterFrame {
4950
/* "Specials" section */
50-
PyObject *f_funcobj; /* Strong reference */
51-
PyObject *f_globals; /* Borrowed reference */
52-
PyObject *f_builtins; /* Borrowed reference */
53-
PyObject *f_locals; /* Strong reference, may be NULL */
51+
PyObject *f_funcobj; /* Strong reference. Only valid if not on C stack */
52+
PyObject *f_globals; /* Borrowed reference. Only valid if not on C stack */
53+
PyObject *f_builtins; /* Borrowed reference. Only valid if not on C stack */
54+
PyObject *f_locals; /* Strong reference, may be NULL. Only valid if not on C stack */
5455
PyCodeObject *f_code; /* Strong reference */
55-
PyFrameObject *frame_obj; /* Strong reference, may be NULL */
56+
PyFrameObject *frame_obj; /* Strong reference, may be NULL. Only valid if not on C stack */
5657
/* Linkage section */
5758
struct _PyInterpreterFrame *previous;
5859
// NOTE: This is not necessarily the last instruction started in the given
5960
// frame. Rather, it is the code unit *prior to* the *next* instruction. For
6061
// example, it may be an inline CACHE entry, an instruction we just jumped
6162
// over, or (in the case of a newly-created frame) a totally invalid value:
6263
_Py_CODEUNIT *prev_instr;
63-
int stacktop; /* Offset of TOS from localsplus */
64+
int stacktop; /* Offset of TOS from localsplus */
6465
uint16_t yield_offset;
65-
bool is_entry; // Whether this is the "root" frame for the current _PyCFrame.
6666
char owner;
6767
/* Locals and stack */
6868
PyObject *localsplus[1];
@@ -110,7 +110,6 @@ _PyFrame_InitializeSpecials(
110110
frame->stacktop = code->co_nlocalsplus;
111111
frame->frame_obj = NULL;
112112
frame->prev_instr = _PyCode_CODE(code) - 1;
113-
frame->is_entry = false;
114113
frame->yield_offset = 0;
115114
frame->owner = FRAME_OWNED_BY_THREAD;
116115
}

Include/internal/pycore_global_objects_fini_generated.h

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_global_strings.h

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ struct _Py_global_strings {
4848
STRUCT_FOR_STR(newline, "\n")
4949
STRUCT_FOR_STR(open_br, "{")
5050
STRUCT_FOR_STR(percent, "%")
51+
STRUCT_FOR_STR(shim_name, "<shim>")
5152
STRUCT_FOR_STR(utf_8, "utf-8")
5253
} literals;
5354

Include/internal/pycore_interp.h

+1
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ struct _is {
186186
struct ast_state ast;
187187
struct types_state types;
188188
struct callable_cache callable_cache;
189+
PyCodeObject *interpreter_trampoline;
189190

190191
/* The following fields are here to avoid allocation during init.
191192
The data is exposed through PyInterpreterState pointer fields.

Include/internal/pycore_opcode.h

+22-22
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_runtime_init_generated.h

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)