8000 gh-98831: Modernize the FOR_ITER family of instructions by gvanrossum · Pull Request #101626 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-98831: Modernize the FOR_ITER family of instructions #101626

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Modernize FOR_ITER_RANGE (even weirder)
  • Loading branch information
gvanrossum committed Feb 7, 2023
commit a52a79d2fdd17333400b1b64f5586355c7375e5c
11 changes: 7 additions & 4 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -2076,7 +2076,7 @@ dummy_func(
FOR_ITER,
FOR_ITER_LIST,
FOR_ITER_TUPLE,
// FOR_ITER_RANGE,
FOR_ITER_RANGE,
// FOR_ITER_GEN,
};

Expand Down Expand Up @@ -2161,17 +2161,19 @@ dummy_func(
// Common case: no jump, leave it to the code generator
}

// stack effect: ( -- __0)
inst(FOR_ITER_RANGE) {
// This is slightly different, when the loop isn't terminated we
// jump over the immediately following STORE_FAST instruction.
inst(FOR_ITER_RANGE, (unused/1, iter -- iter, unused)) {
assert(cframe.use_tracing == 0);
_PyRangeIterObject *r = (_PyRangeIterObject *)TOP();
_PyRangeIterObject *r = (_PyRangeIterObject *)iter;
DEOPT_IF(Py_TYPE(r) != &PyRangeIter_Type, FOR_ITER);
STAT_INC(FOR_ITER, hit);
_Py_CODEUNIT next = next_instr[INLINE_CACHE_ENTRIES_FOR_ITER];
assert(_PyOpcode_Deopt[_Py_OPCODE(next)] == STORE_FAST);
if (r->len <= 0) {
STACK_SHRINK(1);
Py_DECREF(r);
// Jump over END_FOR instruction.
JUMPBY(INLINE_CACHE_ENTRIES_FOR_ITER + oparg + 1);
}
else {
Expand All @@ -2184,6 +2186,7 @@ dummy_func(
// The STORE_FAST is already done.
JUMPBY(INLINE_CACHE_ENTRIES_FOR_ITER + 1);
}
DISPATCH();
}

inst(FOR_ITER_GEN) {
Expand Down
4 changes: 3 additions & 1 deletion Python/generated_cases.c.h

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

6 changes: 3 additions & 3 deletions Python/opcode_metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ _PyOpcode_num_popped(int opcode, int oparg, bool jump) {
case FOR_ITER_TUPLE:
return 1;
case FOR_ITER_RANGE:
return -1;
return 1;
case FOR_ITER_GEN:
return -1;
case BEFORE_ASYNC_WITH:
Expand Down Expand Up @@ -613,7 +613,7 @@ _PyOpcode_num_pushed(int opcode, int oparg, bool jump) {
case FOR_ITER_TUPLE:
return 2;
case FOR_ITER_RANGE:
return -1;
return 2;
case FOR_ITER_GEN:
return -1;
case BEFORE_ASYNC_WITH:
Expand Down Expand Up @@ -832,7 +832,7 @@ struct opcode_metadata {
[FOR_ITER] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IBC },
[FOR_ITER_LIST] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IBC },
[FOR_ITER_TUPLE] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IBC },
[FOR_ITER_RANGE] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
[FOR_ITER_RANGE] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IBC },
[FOR_ITER_GEN] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
[BEFORE_ASYNC_WITH] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IX },
[BEFORE_WITH] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IX },
Expand Down
0