Skip to content

Commit

Permalink
Revert "bpo-46329: Split calls into precall and call instructions. (G…
Browse files Browse the repository at this point in the history
…H-30855)"

This reverts commit 89fd7c3.
  • Loading branch information
pablogsal authored and ambv committed Jan 31, 2022
1 parent 7685693 commit 286bc37
Show file tree
Hide file tree
Showing 16 changed files with 629 additions and 912 deletions.
70 changes: 30 additions & 40 deletions Doc/library/dis.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,10 @@ the following command can be used to display the disassembly of
:func:`myfunc`::

>>> dis.dis(myfunc)
1 0 RESUME 0

2 2 LOAD_GLOBAL 0 (len)
4 LOAD_FAST 0 (alist)
6 PRECALL_FUNCTION 1
8 CALL 0
10 RETURN_VALUE
2 0 LOAD_GLOBAL 0 (len)
2 LOAD_FAST 0 (alist)
4 CALL_NO_KW 1
6 RETURN_VALUE

(The "2" is a line number).

Expand Down Expand Up @@ -105,11 +102,9 @@ Example::
>>> for instr in bytecode:
... print(instr.opname)
...
RESUME
LOAD_GLOBAL
LOAD_FAST
PRECALL_FUNCTION
CALL
CALL_NO_KW
RETURN_VALUE


Expand Down Expand Up @@ -622,7 +617,7 @@ iterations of the loop.
.. opcode:: LOAD_BUILD_CLASS

Pushes :func:`builtins.__build_class__` onto the stack. It is later called
to construct a class.
by :opcode:`CALL_NO_KW` to construct a class.


.. opcode:: BEFORE_WITH (delta)
Expand Down Expand Up @@ -1063,19 +1058,30 @@ iterations of the loop.
with ``__cause__`` set to ``TOS``)


.. opcode:: CALL (named)
.. opcode:: CALL_NO_KW (argc)

Calls a callable object with positional arguments.
*argc* indicates the number of positional arguments.
The top of the stack contains positional arguments, with the right-most
argument on top. Below the arguments is a callable object to call.
``CALL_NO_KW`` pops all arguments and the callable object off the stack,
calls the callable object with those arguments, and pushes the return value
returned by the callable object.

.. versionadded:: 3.11

Calls a callable object with the number of positional arguments specified by
the preceding :opcode:`PRECALL_FUNCTION` or :opcode:`PRECALL_METHOD` and
the named arguments specified by the preceding :opcode:`KW_NAMES`, if any.
*named* indicates the number of named arguments.
On the stack are (in ascending order):

* The callable
* The positional arguments
* The named arguments
.. opcode:: CALL_KW (argc)

``CALL`` pops all arguments and the callable object off the stack,
Calls a callable object with positional (if any) and keyword arguments.
*argc* indicates the total number of positional and keyword arguments.
The top element on the stack contains a tuple with the names of the
keyword arguments, which must be strings.
Below that are the values for the keyword arguments,
in the order corresponding to the tuple.
Below that are positional arguments, with the right-most parameter on
top. Below the arguments is a callable object to call.
``CALL_KW`` pops all arguments and the callable object off the stack,
calls the callable object with those arguments, and pushes the return value
returned by the callable object.

Expand All @@ -1102,7 +1108,7 @@ iterations of the loop.
Loads a method named ``co_names[namei]`` from the TOS object. TOS is popped.
This bytecode distinguishes two cases: if TOS has a method with the correct
name, the bytecode pushes the unbound method and TOS. TOS will be used as
the first argument (``self``) by :opcode:`PRECALL_METHOD` when calling the
the first argument (``self``) by :opcode:`CALL_METHOD` when calling the
unbound method. Otherwise, ``NULL`` and the object return by the attribute
lookup are pushed.

Expand All @@ -1111,30 +1117,14 @@ iterations of the loop.

.. opcode:: PRECALL_METHOD (argc)

Prefixes :opcode:`CALL` (possibly with an intervening ``KW_NAMES``).
Prefixes either :opcode:`CALL_NO_KW` or :opcode:`CALL_KW`.
This opcode is designed to be used with :opcode:`LOAD_METHOD`.
Sets internal variables, so that :opcode:`CALL`
Sets internal variables, so that :opcode:`CALL_NO_KW` or :opcode:`CALL_KW`
clean up after :opcode:`LOAD_METHOD` correctly.

.. versionadded:: 3.11


.. opcode:: PRECALL_FUNCTION (args)

Prefixes :opcode:`CALL` (possibly with an intervening ``KW_NAMES``).
Sets internal variables, so that :opcode:`CALL` can execute correctly.

.. versionadded:: 3.11


.. opcode:: KW_NAMES (i)

Stores a reference to ``co_consts[consti]`` into an internal variable
for use by :opcode:`CALL`. ``co_consts[consti]`` must be a tuple of strings.

.. versionadded:: 3.11


.. opcode:: MAKE_FUNCTION (flags)

Pushes a new function object on the stack. From bottom to top, the consumed
Expand Down
3 changes: 1 addition & 2 deletions Doc/whatsnew/3.11.rst
Original file line number Diff line number Diff line change
Expand Up @@ -400,8 +400,7 @@ CPython bytecode changes

* Replaced the three call instructions: :opcode:`CALL_FUNCTION`,
:opcode:`CALL_FUNCTION_KW` and :opcode:`CALL_METHOD` with
:opcode:`PRECALL_FUNCTION`, :opcode:`PRECALL_METHOD`, :opcode:`CALL`,
and :opcode:`KW_NAMES`.
:opcode:`CALL_NO_KW`, :opcode:`CALL_KW` and :opcode:`PRECALL_METHOD`.
This decouples the argument shifting for methods from the handling of
keyword arguments and allows better specialization of calls.

Expand Down
5 changes: 2 additions & 3 deletions Include/internal/pycore_code.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ typedef struct {

typedef struct {
uint32_t func_version;
uint16_t min_args;
uint16_t defaults_start;
uint16_t defaults_len;
} _PyCallCache;

Expand Down Expand Up @@ -271,8 +271,7 @@ int _Py_Specialize_LoadGlobal(PyObject *globals, PyObject *builtins, _Py_CODEUNI
int _Py_Specialize_LoadMethod(PyObject *owner, _Py_CODEUNIT *instr, PyObject *name, SpecializedCacheEntry *cache);
int _Py_Specialize_BinarySubscr(PyObject *sub, PyObject *container, _Py_CODEUNIT *instr, SpecializedCacheEntry *cache);
int _Py_Specialize_StoreSubscr(PyObject *container, PyObject *sub, _Py_CODEUNIT *instr);
int _Py_Specialize_CallNoKw(PyObject *callable, _Py_CODEUNIT *instr, int nargs,
PyObject *kwnames, SpecializedCacheEntry *cache, PyObject *builtins);
int _Py_Specialize_CallNoKw(PyObject *callable, _Py_CODEUNIT *instr, int nargs, SpecializedCacheEntry *cache, PyObject *builtins);
void _Py_Specialize_BinaryOp(PyObject *lhs, PyObject *rhs, _Py_CODEUNIT *instr,
SpecializedCacheEntry *cache);
void _Py_Specialize_CompareOp(PyObject *lhs, PyObject *rhs, _Py_CODEUNIT *instr, SpecializedCacheEntry *cache);
Expand Down
3 changes: 0 additions & 3 deletions Include/internal/pycore_frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,6 @@ _PyThreadState_BumpFramePointer(PyThreadState *tstate, size_t size)

void _PyThreadState_PopFrame(PyThreadState *tstate, InterpreterFrame *frame);

InterpreterFrame *
_PyFrame_Push(PyThreadState *tstate, PyFunctionObject *func);

#ifdef __cplusplus
}
#endif
Expand Down
79 changes: 36 additions & 43 deletions Include/opcode.h

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

7 changes: 1 addition & 6 deletions Lib/importlib/_bootstrap_external.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,14 +384,9 @@ def _write_atomic(path, data, mode=0o666):
# Python 3.11a5 3476 (Add ASYNC_GEN_WRAP opcode)
# Python 3.11a5 3477 (Replace DUP_TOP/DUP_TOP_TWO with COPY and
# ROT_TWO/ROT_THREE/ROT_FOUR/ROT_N with SWAP)
# Python 3.11a5 3478 (New CALL opcodes)

# Python 3.12 will start with magic number 3500


# Python 3.12 will start with magic number 3500


#
# MAGIC must change whenever the bytecode emitted by the compiler may no
# longer be understood by older implementations of the eval loop (usually
Expand All @@ -402,7 +397,7 @@ 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 = (3478).to_bytes(2, 'little') + b'\r\n'
MAGIC_NUMBER = (3477).to_bytes(2, 'little') + b'\r\n'
_RAW_MAGIC_NUMBER = int.from_bytes(MAGIC_NUMBER, 'little') # For import.c

_PYCACHE = '__pycache__'
Expand Down
18 changes: 5 additions & 13 deletions Lib/opcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,9 @@ def jabs_op(name, op):
def_op('DICT_MERGE', 164)
def_op('DICT_UPDATE', 165)

def_op('PRECALL_FUNCTION', 167)
def_op('PRECALL_METHOD', 168)

def_op('CALL', 171)
def_op('KW_NAMES', 172)
hasconst.append(172)
def_op('CALL_NO_KW', 169)
def_op('CALL_KW', 170)

del def_op, name_op, jrel_op, jabs_op

Expand Down Expand Up @@ -248,21 +245,16 @@ def jabs_op(name, op):
"STORE_SUBSCR_ADAPTIVE",
"STORE_SUBSCR_LIST_INT",
"STORE_SUBSCR_DICT",
"CALL_ADAPTIVE",
"CALL_BUILTIN_CLASS",
"CALL_NO_KW_ADAPTIVE",
"CALL_NO_KW_BUILTIN_O",
"CALL_NO_KW_BUILTIN_FAST",
"CALL_BUILTIN_FAST_WITH_KEYWORDS",
"CALL_NO_KW_LEN",
"CALL_NO_KW_ISINSTANCE",
"CALL_PY_EXACT_ARGS",
"CALL_PY_WITH_DEFAULTS",
"CALL_NO_KW_PY_SIMPLE",
"CALL_NO_KW_LIST_APPEND",
"CALL_NO_KW_METHOD_DESCRIPTOR_O",
"CALL_NO_KW_METHOD_DESCRIPTOR_NOARGS",
"CALL_NO_KW_STR_1",
"CALL_NO_KW_TUPLE_1",
"CALL_NO_KW_TYPE_1",
"CALL_NO_KW_BUILTIN_CLASS_1",
"CALL_NO_KW_METHOD_DESCRIPTOR_FAST",
"JUMP_ABSOLUTE_QUICK",
"LOAD_ATTR_ADAPTIVE",
Expand Down
7 changes: 4 additions & 3 deletions Lib/test/test_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -837,8 +837,9 @@ def foo(x):
opcodes = list(dis.get_instructions(func))
instructions = [opcode.opname for opcode in opcodes]
self.assertNotIn('LOAD_METHOD', instructions)
self.assertNotIn('CALL_METHOD', instructions)
self.assertIn('LOAD_ATTR', instructions)
self.assertIn('PRECALL_FUNCTION', instructions)
self.assertIn('CALL_NO_KW', instructions)

def test_lineno_procedure_call(self):
def call():
Expand Down Expand Up @@ -1095,7 +1096,7 @@ def test_multiline_expression(self):
)
"""
compiled_code, _ = self.check_positions_against_ast(snippet)
self.assertOpcodeSourcePositionIs(compiled_code, 'CALL',
self.assertOpcodeSourcePositionIs(compiled_code, 'CALL_NO_KW',
line=1, end_line=3, column=0, end_column=1)

def test_very_long_line_end_offset(self):
Expand All @@ -1105,7 +1106,7 @@ def test_very_long_line_end_offset(self):
snippet = f"g('{long_string}')"

compiled_code, _ = self.check_positions_against_ast(snippet)
self.assertOpcodeSourcePositionIs(compiled_code, 'CALL',
self.assertOpcodeSourcePositionIs(compiled_code, 'CALL_NO_KW',
line=1, end_line=1, column=None, end_column=None)

def test_complex_single_line_expression(self):
Expand Down
Loading

0 comments on commit 286bc37

Please sign in to comment.