Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

gh-91276: Make JUMP_IF_TRUE_OR_POP/JUMP_IF_FALSE_OR_POP relative #32215

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
Apr 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 9 additions & 4 deletions 13 Doc/library/dis.rst
Original file line number Diff line number Diff line change
Expand Up @@ -993,21 +993,26 @@ iterations of the loop.
.. versionadded:: 3.11


.. opcode:: JUMP_IF_TRUE_OR_POP (target)
.. opcode:: JUMP_IF_TRUE_OR_POP (delta)

If TOS is true, sets the bytecode counter to *target* and leaves TOS on the
If TOS is true, increments the bytecode counter by *delta* and leaves TOS on the
stack. Otherwise (TOS is false), TOS is popped.

.. versionadded:: 3.1

.. versionchanged:: 3.11
The oparg is now a relative delta rather than an absolute target.

.. opcode:: JUMP_IF_FALSE_OR_POP (target)
.. opcode:: JUMP_IF_FALSE_OR_POP (delta)

If TOS is false, sets the bytecode counter to *target* and leaves TOS on the
If TOS is false, increments the bytecode counter by *delta* and leaves TOS on the
stack. Otherwise (TOS is true), TOS is popped.

.. versionadded:: 3.1

.. versionchanged:: 3.11
The oparg is now a relative delta rather than an absolute target.


.. opcode:: FOR_ITER (delta)

Expand Down
3 changes: 3 additions & 0 deletions 3 Doc/whatsnew/3.11.rst
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,9 @@ CPython bytecode changes
:opcode:`POP_JUMP_FORWARD_IF_NONE` and :opcode:`POP_JUMP_BACKWARD_IF_NONE`
opcodes to speed up conditional jumps.

* :opcode:`JUMP_IF_TRUE_OR_POP` and :opcode:`JUMP_IF_FALSE_OR_POP` are now
relative rather than absolute.


Deprecated
==========
Expand Down
2 changes: 1 addition & 1 deletion 2 Include/opcode.h

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

4 changes: 3 additions & 1 deletion 4 Lib/importlib/_bootstrap_external.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ def _write_atomic(path, data, mode=0o666):
# Python 3.11a6 3491 (remove JUMP_IF_NOT_EG_MATCH, add CHECK_EG_MATCH,
# add JUMP_BACKWARD_NO_INTERRUPT, make JUMP_NO_INTERRUPT virtual)
# Python 3.11a7 3492 (make POP_JUMP_IF_NONE/NOT_NONE/TRUE/FALSE relative)
# Python 3.11a7 3493 (Make JUMP_IF_TRUE_OR_POP/JUMP_IF_FALSE_OR_POP relative)

# Python 3.12 will start with magic number 3500

Expand All @@ -415,7 +416,8 @@ def _write_atomic(path, data, mode=0o666):
# Whenever MAGIC_NUMBER is changed, the ranges in the magic_values array
# in PC/launcher.c must also be updated.

MAGIC_NUMBER = (3492).to_bytes(2, 'little') + b'\r\n'
MAGIC_NUMBER = (3493).to_bytes(2, 'little') + b'\r\n'

_RAW_MAGIC_NUMBER = int.from_bytes(MAGIC_NUMBER, 'little') # For import.c

_PYCACHE = '__pycache__'
Expand Down
4 changes: 2 additions & 2 deletions 4 Lib/opcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ def jabs_op(name, op, entries=0):
name_op('IMPORT_NAME', 108) # Index in name list
name_op('IMPORT_FROM', 109) # Index in name list
jrel_op('JUMP_FORWARD', 110) # Number of words to skip
jabs_op('JUMP_IF_FALSE_OR_POP', 111) # Target byte offset from beginning of code
jabs_op('JUMP_IF_TRUE_OR_POP', 112) # ""
jrel_op('JUMP_IF_FALSE_OR_POP', 111) # Number of words to skip
jrel_op('JUMP_IF_TRUE_OR_POP', 112) # ""
jrel_op('POP_JUMP_FORWARD_IF_FALSE', 114)
jrel_op('POP_JUMP_FORWARD_IF_TRUE', 115)
name_op('LOAD_GLOBAL', 116, 5) # Index in name list
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Make opcodes :opcode:`JUMP_IF_TRUE_OR_POP` and :opcode:`JUMP_IF_FALSE_OR_POP` relative rather than absolute.
8 changes: 4 additions & 4 deletions 8 Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -4085,7 +4085,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
DISPATCH();
}
if (Py_IsFalse(cond)) {
JUMPTO(oparg);
JUMPBY(oparg);
DISPATCH();
}
err = PyObject_IsTrue(cond);
Expand All @@ -4094,7 +4094,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
Py_DECREF(cond);
}
else if (err == 0)
JUMPTO(oparg);
JUMPBY(oparg);
else
goto error;
DISPATCH();
Expand All @@ -4109,12 +4109,12 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
DISPATCH();
}
if (Py_IsTrue(cond)) {
JUMPTO(oparg);
JUMPBY(oparg);
DISPATCH();
}
err = PyObject_IsTrue(cond);
if (err > 0) {
JUMPTO(oparg);
JUMPBY(oparg);
}
else if (err == 0) {
STACK_SHRINK(1);
Expand Down
15 changes: 15 additions & 0 deletions 15 Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -7672,6 +7672,21 @@ normalize_jumps(struct assembler *a)
last->i_opcode = is_forward ?
POP_JUMP_FORWARD_IF_TRUE : POP_JUMP_BACKWARD_IF_TRUE;
break;
case JUMP_IF_TRUE_OR_POP:
case JUMP_IF_FALSE_OR_POP:
if (!is_forward) {
/* As far as we can tell, the compiler never emits
* these jumps with a backwards target. If/when this
* exception is raised, we have found a use case for
* a backwards version of this jump (or to replace
* it with the sequence (COPY 1, POP_JUMP_IF_T/F, POP)
*/
PyErr_Format(PyExc_SystemError,
"unexpected %s jumping backwards",
last->i_opcode == JUMP_IF_TRUE_OR_POP ?
"JUMP_IF_TRUE_OR_POP" : "JUMP_IF_FALSE_OR_POP");
}
break;
}
}
}
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.