Skip to content

GH-125837: Rework the instructions for loading constants and returning values. #125878

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

Closed
24 changes: 18 additions & 6 deletions Doc/library/dis.rst
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ the following command can be used to display the disassembly of
3 LOAD_GLOBAL 1 (len + NULL)
LOAD_FAST 0 (alist)
CALL 1
RETURN_VALUE
RETURN_VALUE_FUNC

(The "2" is a line number).

Expand Down Expand Up @@ -203,7 +203,7 @@ Example:
LOAD_GLOBAL
LOAD_FAST
CALL
RETURN_VALUE
RETURN_VALUE_FUNC


Analysis functions
Expand Down Expand Up @@ -857,16 +857,20 @@ container object remains on the stack so that it is available for further
iterations of the loop.


.. opcode:: RETURN_VALUE
.. opcode:: RETURN_VALUE_FUNC

Returns with ``STACK[-1]`` to the caller of the function.
Used in normal functions.

.. versionadded:: 3.14

.. opcode:: RETURN_CONST (consti)

Returns with ``co_consts[consti]`` to the caller of the function.
.. opcode:: RETURN_VALUE_GEN

.. versionadded:: 3.12
Returns with ``STACK[-1]`` to the caller of the generator.
Used in generator functions.

.. versionadded:: 3.14


.. opcode:: YIELD_VALUE
Expand Down Expand Up @@ -1086,6 +1090,14 @@ iterations of the loop.
Pushes ``co_consts[consti]`` onto the stack.


.. opcode:: LOAD_CONST_IMMORTAL (consti)

Pushes ``co_consts[consti]`` onto the stack.
Can be used when the constant value is known to be immortal.

.. versionadded:: 3.14


.. opcode:: LOAD_NAME (namei)

Pushes the value associated with ``co_names[namei]`` onto the stack.
Expand Down
2 changes: 1 addition & 1 deletion Doc/whatsnew/3.12.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1123,7 +1123,7 @@ CPython bytecode changes
* Add the :opcode:`LOAD_SUPER_ATTR` instruction. (Contributed by Carl Meyer and
Vladimir Matveev in :gh:`103497`.)

* Add the :opcode:`RETURN_CONST` instruction. (Contributed by Wenyang Wang in :gh:`101632`.)
* Add the ``RETURN_CONST`` instruction. (Contributed by Wenyang Wang in :gh:`101632`.)

Demos and Tools
===============
Expand Down
2 changes: 2 additions & 0 deletions Include/internal/pycore_ceval.h
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,8 @@ PyAPI_FUNC(PyObject *)_PyEval_MatchKeys(PyThreadState *tstate, PyObject *map, Py
PyAPI_FUNC(void) _PyEval_MonitorRaise(PyThreadState *tstate, _PyInterpreterFrame *frame, _Py_CODEUNIT *instr);
PyAPI_FUNC(int) _PyEval_UnpackIterableStackRef(PyThreadState *tstate, _PyStackRef v, int argcnt, int argcntafter, _PyStackRef *sp);
PyAPI_FUNC(void) _PyEval_FrameClearAndPop(PyThreadState *tstate, _PyInterpreterFrame *frame);
PyAPI_FUNC(void) _PyEval_ClearThreadFrame(PyThreadState *tstate, _PyInterpreterFrame *frame);
PyAPI_FUNC(void) _PyEval_ClearGenFrame(PyThreadState *tstate, _PyInterpreterFrame *frame);
PyAPI_FUNC(PyObject **) _PyObjectArray_FromStackRefArray(_PyStackRef *input, Py_ssize_t nargs, PyObject **scratch);

PyAPI_FUNC(void) _PyObjectArray_Free(PyObject **array, PyObject **scratch);
Expand Down
3 changes: 2 additions & 1 deletion Include/internal/pycore_magic_number.h
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ Known values:
Python 3.14a1 3606 (Specialize CALL_KW)
Python 3.14a1 3607 (Add pseudo instructions JUMP_IF_TRUE/FALSE)
Python 3.14a1 3608 (Add support for slices)
Python 3.14a2 3609 (Break up RETURN_VALUE)

Python 3.15 will start with 3650

Expand All @@ -273,7 +274,7 @@ PC/launcher.c must also be updated.

*/

#define PYC_MAGIC_NUMBER 3608
#define PYC_MAGIC_NUMBER 3609
/* This is equivalent to converting PYC_MAGIC_NUMBER to 2 bytes
(little-endian) and then appending b'\r\n'. */
#define PYC_MAGIC_NUMBER_TOKEN \
Expand Down
77 changes: 46 additions & 31 deletions Include/internal/pycore_opcode_metadata.h

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

3 changes: 2 additions & 1 deletion Include/internal/pycore_opcode_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ extern "C" {

#define IS_SCOPE_EXIT_OPCODE(opcode) \
((opcode) == RETURN_VALUE || \
(opcode) == RETURN_CONST || \
(opcode) == RETURN_VALUE_FUNC || \
(opcode) == RETURN_VALUE_GEN || \
(opcode) == RAISE_VARARGS || \
(opcode) == RERAISE)

Expand Down
4 changes: 3 additions & 1 deletion Include/internal/pycore_uop_ids.h

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

Loading
Loading