bpo-46409: Make generators in bytecode (GH-30633)

* Add RETURN_GENERATOR and JUMP_NO_INTERRUPT opcodes.

* Trim frame and generator by word each.

* Minor refactor of frame.c

* Update test.test_sys to account for smaller frames.

* Treat generator functions as normal functions when evaluating and specializing.
This commit is contained in:
Mark Shannon 2022-01-20 11:46:39 +00:00 committed by GitHub
parent d05a66339b
commit b04dfbbe4b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 236 additions and 205 deletions

View file

@ -13,7 +13,6 @@ extern "C" {
and coroutine objects. */
#define _PyGenObject_HEAD(prefix) \
PyObject_HEAD \
/* Note: gi_frame can be NULL if the generator is "finished" */ \
/* The code object backing the generator */ \
PyCodeObject *prefix##_code; \
/* List of weak reference. */ \

View file

@ -41,12 +41,12 @@ typedef struct _interpreter_frame {
PyObject *f_locals; /* Strong reference, may be NULL */
PyCodeObject *f_code; /* Strong reference */
PyFrameObject *frame_obj; /* Strong reference, may be NULL */
PyObject *generator; /* Borrowed reference, may be NULL */
struct _interpreter_frame *previous;
int f_lasti; /* Last instruction if called */
int stacktop; /* Offset of TOS from localsplus */
PyFrameState f_state; /* What state the frame is in */
bool is_entry; // Whether this is the "root" frame for the current CFrame.
bool is_generator;
PyObject *localsplus[1];
} InterpreterFrame;
@ -100,10 +100,10 @@ _PyFrame_InitializeSpecials(
frame->f_locals = Py_XNewRef(locals);
frame->stacktop = nlocalsplus;
frame->frame_obj = NULL;
frame->generator = NULL;
frame->f_lasti = -1;
frame->f_state = FRAME_CREATED;
frame->is_entry = false;
frame->is_generator = false;
}
/* Gets the pointer to the locals array

28
Include/opcode.h generated
View file

@ -38,6 +38,7 @@ extern "C" {
#define LOAD_BUILD_CLASS 71
#define GET_AWAITABLE 73
#define LOAD_ASSERTION_ERROR 74
#define RETURN_GENERATOR 75
#define LIST_TO_TUPLE 82
#define RETURN_VALUE 83
#define IMPORT_STAR 84
@ -89,6 +90,7 @@ extern "C" {
#define RAISE_VARARGS 130
#define MAKE_FUNCTION 132
#define BUILD_SLICE 133
#define JUMP_NO_INTERRUPT 134
#define MAKE_CELL 135
#define LOAD_CLOSURE 136
#define LOAD_DEREF 137
@ -157,18 +159,18 @@ extern "C" {
#define LOAD_GLOBAL_BUILTIN 66
#define LOAD_METHOD_ADAPTIVE 67
#define LOAD_METHOD_CACHED 72
#define LOAD_METHOD_CLASS 75
#define LOAD_METHOD_MODULE 76
#define LOAD_METHOD_NO_DICT 77
#define STORE_ATTR_ADAPTIVE 78
#define STORE_ATTR_INSTANCE_VALUE 79
#define STORE_ATTR_SLOT 80
#define STORE_ATTR_WITH_HINT 81
#define LOAD_FAST__LOAD_FAST 87
#define STORE_FAST__LOAD_FAST 131
#define LOAD_FAST__LOAD_CONST 134
#define LOAD_CONST__LOAD_FAST 140
#define STORE_FAST__STORE_FAST 141
#define LOAD_METHOD_CLASS 76
#define LOAD_METHOD_MODULE 77
#define LOAD_METHOD_NO_DICT 78
#define STORE_ATTR_ADAPTIVE 79
#define STORE_ATTR_INSTANCE_VALUE 80
#define STORE_ATTR_SLOT 81
#define STORE_ATTR_WITH_HINT 87
#define LOAD_FAST__LOAD_FAST 131
#define STORE_FAST__LOAD_FAST 140
#define LOAD_FAST__LOAD_CONST 141
#define LOAD_CONST__LOAD_FAST 143
#define STORE_FAST__STORE_FAST 150
#define DO_TRACING 255
#ifdef NEED_OPCODE_JUMP_TABLES
static uint32_t _PyOpcode_RelativeJump[8] = {
@ -186,7 +188,7 @@ static uint32_t _PyOpcode_Jump[8] = {
0U,
536870912U,
2316288000U,
3U,
67U,
0U,
0U,
0U,