Skip to content

bpo-45636: BINARY_OP (third time's the charm) #29482

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 18 commits into from
Nov 11, 2021
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
141 changes: 7 additions & 134 deletions Doc/library/dis.rst
Original file line number Diff line number Diff line change
Expand Up @@ -406,156 +406,29 @@ result back on the stack.
.. versionadded:: 3.5


**Binary operations**
**Binary and in-place operations**

Binary operations remove the top of the stack (TOS) and the second top-most
stack item (TOS1) from the stack. They perform the operation, and put the
result back on the stack.

.. opcode:: BINARY_POWER

Implements ``TOS = TOS1 ** TOS``.


.. opcode:: BINARY_MULTIPLY

Implements ``TOS = TOS1 * TOS``.


.. opcode:: BINARY_MATRIX_MULTIPLY

Implements ``TOS = TOS1 @ TOS``.

.. versionadded:: 3.5


.. opcode:: BINARY_FLOOR_DIVIDE

Implements ``TOS = TOS1 // TOS``.


.. opcode:: BINARY_TRUE_DIVIDE

Implements ``TOS = TOS1 / TOS``.


.. opcode:: BINARY_MODULO

Implements ``TOS = TOS1 % TOS``.


.. opcode:: BINARY_ADD

Implements ``TOS = TOS1 + TOS``.


.. opcode:: BINARY_SUBTRACT

Implements ``TOS = TOS1 - TOS``.


.. opcode:: BINARY_SUBSCR

Implements ``TOS = TOS1[TOS]``.


.. opcode:: BINARY_LSHIFT

Implements ``TOS = TOS1 << TOS``.


.. opcode:: BINARY_RSHIFT

Implements ``TOS = TOS1 >> TOS``.


.. opcode:: BINARY_AND

Implements ``TOS = TOS1 & TOS``.


.. opcode:: BINARY_XOR

Implements ``TOS = TOS1 ^ TOS``.


.. opcode:: BINARY_OR

Implements ``TOS = TOS1 | TOS``.


**In-place operations**

In-place operations are like binary operations, in that they remove TOS and
TOS1, and push the result back on the stack, but the operation is done in-place
when TOS1 supports it, and the resulting TOS may be (but does not have to be)
the original TOS1.

.. opcode:: INPLACE_POWER

Implements in-place ``TOS = TOS1 ** TOS``.


.. opcode:: INPLACE_MULTIPLY

Implements in-place ``TOS = TOS1 * TOS``.


.. opcode:: INPLACE_MATRIX_MULTIPLY

Implements in-place ``TOS = TOS1 @ TOS``.

.. versionadded:: 3.5


.. opcode:: INPLACE_FLOOR_DIVIDE

Implements in-place ``TOS = TOS1 // TOS``.


.. opcode:: INPLACE_TRUE_DIVIDE

Implements in-place ``TOS = TOS1 / TOS``.


.. opcode:: INPLACE_MODULO
.. opcode:: BINARY_OP (op)

Implements in-place ``TOS = TOS1 % TOS``.
Implements the binary and in-place operators (depending on the value of
*op*).


.. opcode:: INPLACE_ADD

Implements in-place ``TOS = TOS1 + TOS``.


.. opcode:: INPLACE_SUBTRACT

Implements in-place ``TOS = TOS1 - TOS``.


.. opcode:: INPLACE_LSHIFT

Implements in-place ``TOS = TOS1 << TOS``.


.. opcode:: INPLACE_RSHIFT

Implements in-place ``TOS = TOS1 >> TOS``.


.. opcode:: INPLACE_AND

Implements in-place ``TOS = TOS1 & TOS``.


.. opcode:: INPLACE_XOR

Implements in-place ``TOS = TOS1 ^ TOS``.
.. versionadded:: 3.11


.. opcode:: INPLACE_OR
.. opcode:: BINARY_SUBSCR

Implements in-place ``TOS = TOS1 | TOS``.
Implements ``TOS = TOS1[TOS]``.


.. opcode:: STORE_SUBSCR
Expand Down
3 changes: 3 additions & 0 deletions Doc/whatsnew/3.11.rst
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,9 @@ Optimizations
CPython bytecode changes
========================

* Replaced all numeric ``BINARY_*`` and ``INPLACE_*`` instructions with a single
:opcode:`BINARY_OP` implementation.

* Added a new :opcode:`CALL_METHOD_KW` opcode. Calls a method in a similar
fashion as :opcode:`CALL_METHOD`, but also supports keyword arguments. Works
in tandem with :opcode:`LOAD_METHOD`.
Expand Down
4 changes: 2 additions & 2 deletions Include/internal/pycore_code.h
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,9 @@ int _Py_Specialize_StoreAttr(PyObject *owner, _Py_CODEUNIT *instr, PyObject *nam
int _Py_Specialize_LoadGlobal(PyObject *globals, PyObject *builtins, _Py_CODEUNIT *instr, PyObject *name, SpecializedCacheEntry *cache);
int _Py_Specialize_LoadMethod(PyObject *owner, _Py_CODEUNIT *instr, PyObject *name, SpecializedCacheEntry *cache);
int _Py_Specialize_BinarySubscr(PyObject *sub, PyObject *container, _Py_CODEUNIT *instr);
int _Py_Specialize_BinaryAdd(PyObject *left, PyObject *right, _Py_CODEUNIT *instr);
int _Py_Specialize_BinaryMultiply(PyObject *left, PyObject *right, _Py_CODEUNIT *instr);
int _Py_Specialize_CallFunction(PyObject *callable, _Py_CODEUNIT *instr, int nargs, SpecializedCacheEntry *cache, PyObject *builtins);
void _Py_Specialize_BinaryOp(PyObject *lhs, PyObject *rhs, _Py_CODEUNIT *instr,
SpecializedCacheEntry *cache);

#define PRINT_SPECIALIZATION_STATS 0
#define PRINT_SPECIALIZATION_STATS_DETAILED 0
Expand Down
Loading