Skip to content

Commit

Permalink
add and use constant NEXT_INSTR_OFFSET_FOR_YIELD
Browse files Browse the repository at this point in the history
  • Loading branch information
iritkatriel committed Oct 13, 2023
1 parent 19da0c0 commit cbee4f8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
18 changes: 10 additions & 8 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
}
Expand Down
4 changes: 4 additions & 0 deletions Python/ceval_macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -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); \
Expand Down Expand Up @@ -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); \
Expand Down
18 changes: 10 additions & 8 deletions Python/generated_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cbee4f8

Please sign in to comment.