From cbee4f8a5357ece28acbb2f15629a10cda978af5 Mon Sep 17 00:00:00 2001 From: Irit Katriel Date: Fri, 13 Oct 2023 11:01:43 +0200 Subject: [PATCH] add and use constant NEXT_INSTR_OFFSET_FOR_YIELD --- Python/bytecodes.c | 18 ++++++++++-------- Python/ceval_macros.h | 4 ++++ Python/generated_cases.c.h | 18 ++++++++++-------- 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/Python/bytecodes.c b/Python/bytecodes.c index 5bcbe0a8738987..a9078b7fc8acc4 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -984,8 +984,8 @@ dummy_func( gen->gi_exc_state.previous_item = tstate->exc_info; tstate->exc_info = &gen->gi_exc_state; SKIP_OVER(INLINE_CACHE_ENTRIES_SEND); - assert(1 + INLINE_CACHE_ENTRIES_SEND == next_instr - frame->instr_ptr); - frame->yield_offset = 1 + INLINE_CACHE_ENTRIES_SEND; + assert(NEXT_INSTR_OFFSET_FOR_YIELD == next_instr - frame->instr_ptr); + frame->yield_offset = NEXT_INSTR_OFFSET_FOR_YIELD; frame->next_instr_offset = 1 + INLINE_CACHE_ENTRIES_SEND + oparg; DISPATCH_INLINED(gen_frame); } @@ -1024,8 +1024,8 @@ dummy_func( gen->gi_exc_state.previous_item = tstate->exc_info; tstate->exc_info = &gen->gi_exc_state; SKIP_OVER(INLINE_CACHE_ENTRIES_SEND); - assert(1 + INLINE_CACHE_ENTRIES_SEND == next_instr - frame->instr_ptr); - frame->yield_offset = 1 + INLINE_CACHE_ENTRIES_SEND; + assert(NEXT_INSTR_OFFSET_FOR_YIELD == next_instr - frame->instr_ptr); + frame->yield_offset = NEXT_INSTR_OFFSET_FOR_YIELD; frame->next_instr_offset = 1 + INLINE_CACHE_ENTRIES_SEND + oparg; DISPATCH_INLINED(gen_frame); } @@ -1048,7 +1048,8 @@ dummy_func( frame = tstate->current_frame = frame->previous; gen_frame->previous = NULL; _PyFrame_StackPush(frame, retval); - frame->next_instr_offset = frame->yield_offset; + assert(frame->yield_offset == NEXT_INSTR_OFFSET_FOR_YIELD || frame->owner == FRAME_OWNED_BY_CSTACK); + frame->next_instr_offset = frame->owner == FRAME_OWNED_BY_CSTACK ? 0 : NEXT_INSTR_OFFSET_FOR_YIELD; frame->yield_offset = 0; goto resume_frame; } @@ -1070,7 +1071,8 @@ dummy_func( frame = tstate->current_frame = frame->previous; gen_frame->previous = NULL; _PyFrame_StackPush(frame, retval); - frame->next_instr_offset = frame->yield_offset; + assert(frame->yield_offset == NEXT_INSTR_OFFSET_FOR_YIELD || frame->owner == FRAME_OWNED_BY_CSTACK); + frame->next_instr_offset = frame->owner == FRAME_OWNED_BY_CSTACK ? 0 : NEXT_INSTR_OFFSET_FOR_YIELD; frame->yield_offset = 0; goto resume_frame; } @@ -2688,8 +2690,8 @@ dummy_func( SKIP_OVER(INLINE_CACHE_ENTRIES_FOR_ITER); assert(next_instr[oparg].op.code == END_FOR || next_instr[oparg].op.code == INSTRUMENTED_END_FOR); - assert(1 + INLINE_CACHE_ENTRIES_FOR_ITER == next_instr - frame->instr_ptr); - frame->yield_offset = 1 + INLINE_CACHE_ENTRIES_FOR_ITER; + assert(NEXT_INSTR_OFFSET_FOR_YIELD == next_instr - frame->instr_ptr); + frame->yield_offset = NEXT_INSTR_OFFSET_FOR_YIELD; frame->next_instr_offset = 1 + INLINE_CACHE_ENTRIES_FOR_ITER + oparg; DISPATCH_INLINED(gen_frame); } diff --git a/Python/ceval_macros.h b/Python/ceval_macros.h index f9f8eee7a94fdc..b75af2b6bb2380 100644 --- a/Python/ceval_macros.h +++ b/Python/ceval_macros.h @@ -70,6 +70,7 @@ #else #define INSTRUCTION_START(op) \ do { \ +if (0) fprintf(stderr, "-- %s frame=%p\n", _PyOpcode_OpName[op], frame); \ frame->instr_ptr = next_instr++; \ frame->next_instr_offset = 0; \ assert(frame->yield_offset == 0); \ @@ -291,6 +292,9 @@ GETITEM(PyObject *v, Py_ssize_t i) { #define CONSTS() _PyFrame_GetCode(frame)->co_consts #define NAMES() _PyFrame_GetCode(frame)->co_names +/* 1 + the cache size of SEND/FOR_ITER_GEN (they must be same) */ +#define NEXT_INSTR_OFFSET_FOR_YIELD 2 + #define DTRACE_FUNCTION_ENTRY() \ if (PyDTrace_FUNCTION_ENTRY_ENABLED()) { \ dtrace_function_entry(frame); \ diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index c7cc06ab42843e..38001cafbd193f 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -1264,8 +1264,8 @@ gen->gi_exc_state.previous_item = tstate->exc_info; tstate->exc_info = &gen->gi_exc_state; SKIP_OVER(INLINE_CACHE_ENTRIES_SEND); - assert(1 + INLINE_CACHE_ENTRIES_SEND == next_instr - frame->instr_ptr); - frame->yield_offset = 1 + INLINE_CACHE_ENTRIES_SEND; + assert(NEXT_INSTR_OFFSET_FOR_YIELD == next_instr - frame->instr_ptr); + frame->yield_offset = NEXT_INSTR_OFFSET_FOR_YIELD; frame->next_instr_offset = 1 + INLINE_CACHE_ENTRIES_SEND + oparg; DISPATCH_INLINED(gen_frame); } @@ -1311,8 +1311,8 @@ gen->gi_exc_state.previous_item = tstate->exc_info; tstate->exc_info = &gen->gi_exc_state; SKIP_OVER(INLINE_CACHE_ENTRIES_SEND); - assert(1 + INLINE_CACHE_ENTRIES_SEND == next_instr - frame->instr_ptr); - frame->yield_offset = 1 + INLINE_CACHE_ENTRIES_SEND; + assert(NEXT_INSTR_OFFSET_FOR_YIELD == next_instr - frame->instr_ptr); + frame->yield_offset = NEXT_INSTR_OFFSET_FOR_YIELD; frame->next_instr_offset = 1 + INLINE_CACHE_ENTRIES_SEND + oparg; DISPATCH_INLINED(gen_frame); } @@ -1337,7 +1337,8 @@ frame = tstate->current_frame = frame->previous; gen_frame->previous = NULL; _PyFrame_StackPush(frame, retval); - frame->next_instr_offset = frame->yield_offset; + assert(frame->yield_offset == NEXT_INSTR_OFFSET_FOR_YIELD || frame->owner == FRAME_OWNED_BY_CSTACK); + frame->next_instr_offset = frame->owner == FRAME_OWNED_BY_CSTACK ? 0 : NEXT_INSTR_OFFSET_FOR_YIELD; frame->yield_offset = 0; goto resume_frame; } @@ -1361,7 +1362,8 @@ frame = tstate->current_frame = frame->previous; gen_frame->previous = NULL; _PyFrame_StackPush(frame, retval); - frame->next_instr_offset = frame->yield_offset; + assert(frame->yield_offset == NEXT_INSTR_OFFSET_FOR_YIELD || frame->owner == FRAME_OWNED_BY_CSTACK); + frame->next_instr_offset = frame->owner == FRAME_OWNED_BY_CSTACK ? 0 : NEXT_INSTR_OFFSET_FOR_YIELD; frame->yield_offset = 0; goto resume_frame; } @@ -3464,8 +3466,8 @@ SKIP_OVER(INLINE_CACHE_ENTRIES_FOR_ITER); assert(next_instr[oparg].op.code == END_FOR || next_instr[oparg].op.code == INSTRUMENTED_END_FOR); - assert(1 + INLINE_CACHE_ENTRIES_FOR_ITER == next_instr - frame->instr_ptr); - frame->yield_offset = 1 + INLINE_CACHE_ENTRIES_FOR_ITER; + assert(NEXT_INSTR_OFFSET_FOR_YIELD == next_instr - frame->instr_ptr); + frame->yield_offset = NEXT_INSTR_OFFSET_FOR_YIELD; frame->next_instr_offset = 1 + INLINE_CACHE_ENTRIES_FOR_ITER + oparg; DISPATCH_INLINED(gen_frame); }