Skip to content
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

Unwind stack #1

Closed
wants to merge 85 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
85 commits
Select commit Hold shift + click to select a range
2315076
bpo-17611: Move unwinding of stack from interpreter to compiler
pitrou Jul 23, 2017
b160900
Cleanup
pitrou Jul 23, 2017
9833631
Improve stack size calculation for SETUP_{EXCEPT,FINALLY}
pitrou Jul 23, 2017
9116fb8
Update frozen importlib
pitrou Jul 23, 2017
5b3796e
Remove useless code
pitrou Jul 23, 2017
77caab0
Don't predict FOR_ITER as it breaks tracing without computed gotos
pitrou Jul 23, 2017
9fffb61
Remove dead code and fix tracing tests
pitrou Jul 23, 2017
5fe5e1b
Fix test_dis
pitrou Jul 23, 2017
a93f2e2
Add JUMP_FINALLY to avoid finally block duplication
pitrou Jul 24, 2017
b007b1c
Remove finally block duplication
pitrou Jul 24, 2017
0bcf8a7
More precise computation of code object stack size
pitrou Jul 24, 2017
eecf02f
JUMP_FINALLY pushes 6 values to be consistent with the effect of an e…
pitrou Jul 24, 2017
64225ce
Exact stack size computation by popping stale exception state in RERAISE
pitrou Jul 24, 2017
04e055e
Remove the now pointless POP_MANY
pitrou Jul 24, 2017
a3e1b80
Add comment for RERAISE
pitrou Jul 24, 2017
a049635
Remove JUMP_FINALLY
pitrou Jul 24, 2017
0741d02
Remove last block duplication
pitrou Jul 24, 2017
5493616
Get rid of END_ITER
pitrou Jul 24, 2017
5c68acf
Fix comments in frameobject.c
pitrou Jul 24, 2017
2d41c17
Add stack size tests
pitrou Jul 29, 2017
afc2235
Fix test_dis.
serhiy-storchaka Aug 24, 2017
00a6681
Simplify handling of 'for' loops in frame_setlineno() and fix it for …
serhiy-storchaka Aug 24, 2017
f8cc274
Do not duplicate the finally block.
serhiy-storchaka Dec 3, 2017
160ce37
Refactor the frame unwinding code.
serhiy-storchaka Dec 4, 2017
b3b3250
Refactoring and optimizations in the compiler.
serhiy-storchaka Dec 5, 2017
e3f5099
Fix frame_setlineno().
serhiy-storchaka Dec 23, 2017
d8dd38d
Rename PUSH_NO_EXCEPT to BEGIN_FINALLY.
serhiy-storchaka Dec 23, 2017
f1d0bc9
Restore SETUP_WITH and SETUP_ASYNC_WITH.
serhiy-storchaka Dec 24, 2017
84c6400
Fix stack depth calculation.
serhiy-storchaka Dec 24, 2017
35bd61b
Remove SETUP_EXCEPT.
serhiy-storchaka Dec 24, 2017
191f2da
Fix rebase error.
serhiy-storchaka Dec 24, 2017
f1a478d
Change codes of new instructions.
serhiy-storchaka Dec 24, 2017
f44d8cd
Update the magic number.
serhiy-storchaka Dec 24, 2017
93ca80e
Remove debug print.
serhiy-storchaka Dec 24, 2017
c8b9129
Polishing.
serhiy-storchaka Dec 24, 2017
e7d03f3
Document changes.
serhiy-storchaka Dec 24, 2017
9273f10
Make CALL_FINALLY a jump instruction, but exclude it from stack depth…
serhiy-storchaka Dec 25, 2017
4ffb02f
Merge branch 'master' into unwind_stack
serhiy-storchaka Dec 26, 2017
b0ea014
Fix jump tests: jumping into a while block is possible now.
serhiy-storchaka Dec 26, 2017
bef9131
Polishing.
serhiy-storchaka Dec 28, 2017
7fc9111
Fix unrolling the finally block.
serhiy-storchaka Dec 28, 2017
800df5c
Optimize 'return' with constant value.
serhiy-storchaka Dec 28, 2017
cb806c5
Do not eliminate END_FINALLY in peepholer.
serhiy-storchaka Dec 29, 2017
c0623d6
Minimize changes in PyCompile_OpcodeStackEffect().
serhiy-storchaka Dec 29, 2017
69d6900
Document POP_FINALLY and add tests for 'break' and 'return' in 'final…
serhiy-storchaka Dec 29, 2017
c879d2e
Polishing.
serhiy-storchaka Dec 29, 2017
894a1f7
Minimize changes in WITH_CLEANUP_START and WITH_CLEANUP_START.
serhiy-storchaka Dec 29, 2017
dc6eae4
Address review comments.
serhiy-storchaka Dec 30, 2017
88a9050
Disable FOR_ITER jump optimization.
serhiy-storchaka Jan 1, 2018
ed175f3
Reorder tests.
serhiy-storchaka Jan 1, 2018
4485924
Merge branch 'master' into unwind_stack
serhiy-storchaka Jan 1, 2018
b49d46e
Merge branch 'master' into unwind_stack
serhiy-storchaka Jan 1, 2018
4ca9831
Clarify the use case of POP_FINALLY.
serhiy-storchaka Jan 1, 2018
e34670a
bpo-24340: Fix estimation of the code stack size.
serhiy-storchaka Jan 1, 2018
42b50d1
Add new tests for stack effect (ported from #5076).
serhiy-storchaka Jan 1, 2018
3d04b53
Add comments.
serhiy-storchaka Jan 1, 2018
4b1697d
Simplify the algorithm, add comments and asserts.
serhiy-storchaka Jan 3, 2018
dc25d14
Merge branch 'master' into stack-size
serhiy-storchaka Jan 3, 2018
8a327cb
Merge branch 'master' into unwind_stack
serhiy-storchaka Jan 3, 2018
4afb870
Merge branch 'stack-size' into unwind_stack
serhiy-storchaka Jan 3, 2018
4d81d0d
Fix stack effect computation.
serhiy-storchaka Jan 3, 2018
edde33a
Update comments and docs in responce to the review.
serhiy-storchaka Jan 3, 2018
44c7d39
Add more tests.
serhiy-storchaka Jan 3, 2018
8dcb17f
Merge branch 'stack-size' into unwind_stack_new
serhiy-storchaka Jan 3, 2018
0317c9d
Add more comments.
serhiy-storchaka Jan 3, 2018
7a33146
Define all stack effects in a single place.
serhiy-storchaka Jan 4, 2018
68e35bb
Make stack_effect() static.
serhiy-storchaka Jan 5, 2018
6056bc7
Add braces for PEP 7.
serhiy-storchaka Jan 7, 2018
44e1e1e
Merge branch 'master' into stack-size
serhiy-storchaka Jan 7, 2018
04647d6
Merge branch 'master' into unwind_stack
serhiy-storchaka Jan 7, 2018
6d5c333
Merge branch 'stack-size' into unwind_stack
serhiy-storchaka Jan 7, 2018
660a117
Add braces for PEP 7.
serhiy-storchaka Jan 7, 2018
a03d212
Merge branch 'master' into unwind_stack
serhiy-storchaka Jan 9, 2018
ff0448f
Merge branch 'master' into unwind_stack
serhiy-storchaka Jan 9, 2018
bd39e0a
Simplify WITH_CLEANUP_START. It is never called after CALL_FINALLY.
serhiy-storchaka Jan 9, 2018
156b455
Merge branch 'master' into unwind_stack
serhiy-storchaka Jan 11, 2018
cd56a99
Merge branch 'master' into unwind_stack
serhiy-storchaka Jan 16, 2018
0816c92
Merge branch 'master' into unwind_stack
serhiy-storchaka Feb 1, 2018
c161251
Merge branch 'master' into unwind_stack
serhiy-storchaka Feb 1, 2018
af9ddf0
Update the magic number.
serhiy-storchaka Feb 4, 2018
c566673
Merge branch 'master' into unwind_stack
serhiy-storchaka Feb 4, 2018
9aee1f0
Merge branch 'master' into unwind_stack
serhiy-storchaka Feb 4, 2018
24667a4
Merge branch 'master' into unwind_stack
serhiy-storchaka Feb 9, 2018
47c5067
Merge branch 'master' into unwind_stack
serhiy-storchaka Feb 22, 2018
4a63042
Merge branch 'master' into unwind_stack
serhiy-storchaka Feb 22, 2018
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
120 changes: 77 additions & 43 deletions Doc/library/dis.rst
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,14 @@ The Python compiler currently generates the following bytecode instructions.
three.


.. opcode:: ROT_FOUR

Lifts second, third and forth stack items one position up, moves top down
to position four.

.. versionadded:: 3.8


.. opcode:: DUP_TOP

Duplicates the reference on top of the stack.
Expand Down Expand Up @@ -605,17 +613,6 @@ the original TOS1.
is terminated with :opcode:`POP_TOP`.


.. opcode:: BREAK_LOOP

Terminates a loop due to a :keyword:`break` statement.


.. opcode:: CONTINUE_LOOP (target)

Continues a loop due to a :keyword:`continue` statement. *target* is the
address to jump to (which should be a :opcode:`FOR_ITER` instruction).


.. opcode:: SET_ADD (i)

Calls ``set.add(TOS1[-i], TOS)``. Used to implement set comprehensions.
Expand Down Expand Up @@ -676,7 +673,7 @@ iterations of the loop.
.. opcode:: POP_BLOCK

Removes one block from the block stack. Per frame, there is a stack of
blocks, denoting nested loops, try statements, and such.
blocks, denoting :keyword:`try` statements, and such.


.. opcode:: POP_EXCEPT
Expand All @@ -687,11 +684,50 @@ iterations of the loop.
popped values are used to restore the exception state.


.. opcode:: POP_FINALLY (preserve_tos)

Cleans up the value stack and the block stack. If *preserve_tos* is not
``0`` TOS first is popped from the stack and pushed on the stack after
perfoming other stack operations:

* If TOS is ``NULL`` or an integer (pushed by :opcode:`BEGIN_FINALLY`
or :opcode:`CALL_FINALLY`) it is popped from the stack.
* If TOS is an exception type (pushed when an exception has been raised)
6 values are popped from the stack, the last three popped values are
used to restore the exception state. An exception handler block is
removed from the block stack.

It is similar to :opcode:`END_FINALLY`, but doesn't change the bytecode
counter nor raise an exception. Used for implementing :keyword:`break`
and :keyword:`return` in the :keyword:`finally` block.

.. versionadded:: 3.8


.. opcode:: BEGIN_FINALLY

Pushes ``NULL`` onto the stack for using it in :opcode:`END_FINALLY`,
:opcode:`POP_FINALLY`, :opcode:`WITH_CLEANUP_START` and
:opcode:`WITH_CLEANUP_FINISH`. Starts the :keyword:`finally` block.

.. versionadded:: 3.8


.. opcode:: END_FINALLY

Terminates a :keyword:`finally` clause. The interpreter recalls whether the
exception has to be re-raised, or whether the function returns, and continues
with the outer-next block.
exception has to be re-raised or execution has to be continued depending on
the value of TOS.

* If TOS is ``NULL`` (pushed by :opcode:`BEGIN_FINALLY`) continue from
the next instruction. TOS is popped.
* If TOS is an integer (pushed by :opcode:`CALL_FINALLY`), sets the
bytecode counter to TOS. TOS is popped.
* If TOS is an exception type (pushed when an exception has been raised)
6 values are popped from the stack, the first three popped values are
used to re-raise the exception and the last three popped values are used
to restore the exception state. An exception handler block is removed
from the block stack.


.. opcode:: LOAD_BUILD_CLASS
Expand All @@ -704,9 +740,9 @@ iterations of the loop.

This opcode performs several operations before a with block starts. First,
it loads :meth:`~object.__exit__` from the context manager and pushes it onto
the stack for later use by :opcode:`WITH_CLEANUP`. Then,
the stack for later use by :opcode:`WITH_CLEANUP_START`. Then,
:meth:`~object.__enter__` is called, and a finally block pointing to *delta*
is pushed. Finally, the result of calling the enter method is pushed onto
is pushed. Finally, the result of calling the ``__enter__()`` method is pushed onto
the stack. The next opcode will either ignore it (:opcode:`POP_TOP`), or
store it in (a) variable(s) (:opcode:`STORE_FAST`, :opcode:`STORE_NAME`, or
:opcode:`UNPACK_SEQUENCE`).
Expand All @@ -716,30 +752,31 @@ iterations of the loop.

.. opcode:: WITH_CLEANUP_START

Cleans up the stack when a :keyword:`with` statement block exits. TOS is the
context manager's :meth:`__exit__` bound method. Below TOS are 1--3 values
indicating how/why the finally clause was entered:
Starts cleaning up the stack when a :keyword:`with` statement block exits.

* SECOND = ``None``
* (SECOND, THIRD) = (``WHY_{RETURN,CONTINUE}``), retval
* SECOND = ``WHY_*``; no retval below it
* (SECOND, THIRD, FOURTH) = exc_info()
At the top of the stack are either ``NULL`` (pushed by
:opcode:`BEGIN_FINALLY`) or 6 values pushed if an exception has been
raised in the with block. Below is the context manager's
:meth:`~object.__exit__` or :meth:`~object.__aexit__` bound method.

In the last case, ``TOS(SECOND, THIRD, FOURTH)`` is called, otherwise
``TOS(None, None, None)``. Pushes SECOND and result of the call
to the stack.
If TOS is ``NULL``, calls ``SECOND(None, None, None)``,
removes the function from the stack, leaving TOS, and pushes ``None``
to the stack. Otherwise calls ``SEVENTH(TOP, SECOND, THIRD)``,
shifts the bottom 3 values of the stack down, replaces the empty spot
with ``NULL`` and pushes TOS. Finally pushes the result of the call.


.. opcode:: WITH_CLEANUP_FINISH

Pops exception type and result of 'exit' function call from the stack.
Finishes cleaning up the stack when a :keyword:`with` statement block exits.

If the stack represents an exception, *and* the function call returns a
'true' value, this information is "zapped" and replaced with a single
``WHY_SILENCED`` to prevent :opcode:`END_FINALLY` from re-raising the
exception. (But non-local gotos will still be resumed.)
TOS is result of ``__exit__()`` or ``__aexit__()`` function call pushed
by :opcode:`WITH_CLEANUP_START`. SECOND is ``None`` or an exception type
(pushed when an exception has been raised).

.. XXX explain the WHY stuff!
Pops two values from the stack. If SECOND is not None and TOS is true
unwinds the EXCEPT_HANDLER block which was created when the exception
was caught and pushes ``NULL`` to the stack.


All of the following opcodes use their arguments.
Expand Down Expand Up @@ -987,22 +1024,19 @@ All of the following opcodes use their arguments.
Loads the global named ``co_names[namei]`` onto the stack.


.. opcode:: SETUP_LOOP (delta)

Pushes a block for a loop onto the block stack. The block spans from the
current instruction with a size of *delta* bytes.

.. opcode:: SETUP_FINALLY (delta)

.. opcode:: SETUP_EXCEPT (delta)
Pushes a try block from a try-finally or try-except clause onto the block
stack. *delta* points to the finally block or the first except block.

Pushes a try block from a try-except clause onto the block stack. *delta*
points to the first except block.

.. opcode:: CALL_FINALLY (delta)

.. opcode:: SETUP_FINALLY (delta)
Pushes the address of the next instruction onto the stack and increments
bytecode counter by *delta*. Used for calling the finally block as a
"subroutine".

Pushes a try block from a try-except clause onto the block stack. *delta*
points to the finally block.
.. versionadded:: 3.8


.. opcode:: LOAD_FAST (var_num)
Expand Down
18 changes: 18 additions & 0 deletions Doc/whatsnew/3.8.rst
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,21 @@ Changes in the Python API
:func:`dbm.dumb.open` with flags ``'r'`` and ``'w'`` no longer creates
a database if it does not exist.
(Contributed by Serhiy Storchaka in :issue:`32749`.)


CPython bytecode changes
------------------------

* The interpreter loop has been simplified by moving the logic of unrolling
the stack of blocks into the compiler. The compiler emits now explicit
instructions for adjusting the stack of values and calling the cleaning
up code for :keyword:`break`, :keyword:`continue` and :keyword:`return`.

Removed opcodes :opcode:`BREAK_LOOP`, :opcode:`CONTINUE_LOOP`,
:opcode:`SETUP_LOOP` and :opcode:`SETUP_EXCEPT`. Added new opcodes
:opcode:`ROT_FOUR`, :opcode:`BEGIN_FINALLY`, :opcode:`CALL_FINALLY` and
:opcode:`POP_FINALLY`. Changed the behavior of :opcode:`END_FINALLY`
and :opcode:`WITH_CLEANUP_START`.

(Contributed by Mark Shannon, Antoine Pitrou and Serhiy Storchaka in
:issue:`17611`.)
8 changes: 4 additions & 4 deletions Include/opcode.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ extern "C" {
#define ROT_THREE 3
#define DUP_TOP 4
#define DUP_TOP_TWO 5
#define ROT_FOUR 6
#define NOP 9
#define UNARY_POSITIVE 10
#define UNARY_NEGATIVE 11
Expand All @@ -32,6 +33,7 @@ extern "C" {
#define GET_AITER 50
#define GET_ANEXT 51
#define BEFORE_ASYNC_WITH 52
#define BEGIN_FINALLY 53
#define INPLACE_ADD 55
#define INPLACE_SUBTRACT 56
#define INPLACE_MULTIPLY 57
Expand All @@ -55,7 +57,6 @@ extern "C" {
#define INPLACE_AND 77
#define INPLACE_XOR 78
#define INPLACE_OR 79
#define BREAK_LOOP 80
#define WITH_CLEANUP_START 81
#define WITH_CLEANUP_FINISH 82
#define RETURN_VALUE 83
Expand Down Expand Up @@ -92,9 +93,6 @@ extern "C" {
#define POP_JUMP_IF_FALSE 114
#define POP_JUMP_IF_TRUE 115
#define LOAD_GLOBAL 116
#define CONTINUE_LOOP 119
#define SETUP_LOOP 120
#define SETUP_EXCEPT 121
#define SETUP_FINALLY 122
#define LOAD_FAST 124
#define STORE_FAST 125
Expand Down Expand Up @@ -127,6 +125,8 @@ extern "C" {
#define BUILD_TUPLE_UNPACK_WITH_CALL 158
#define LOAD_METHOD 160
#define CALL_METHOD 161
#define CALL_FINALLY 162
#define POP_FINALLY 163

/* EXCEPT_HANDLER is a special, implicit block type which is created when
entering an except handler. It is not an opcode but we define it here
Expand Down
3 changes: 2 additions & 1 deletion Lib/importlib/_bootstrap_external.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ def _write_atomic(path, data, mode=0o666):
# Python 3.7a2 3391 (update GET_AITER #31709)
# Python 3.7a4 3392 (PEP 552: Deterministic pycs #31650)
# Python 3.7b1 3393 (remove STORE_ANNOTATION opcode #32550)
# Python 3.8a1 3400 (move frame block handling to compiler #17611)
#
# 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 @@ -254,7 +255,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 = (3393).to_bytes(2, 'little') + b'\r\n'
MAGIC_NUMBER = (3400).to_bytes(2, 'little') + b'\r\n'
_RAW_MAGIC_NUMBER = int.from_bytes(MAGIC_NUMBER, 'little') # For import.c

_PYCACHE = '__pycache__'
Expand Down
11 changes: 5 additions & 6 deletions Lib/opcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def jabs_op(name, op):
def_op('ROT_THREE', 3)
def_op('DUP_TOP', 4)
def_op('DUP_TOP_TWO', 5)
def_op('ROT_FOUR', 6)

def_op('NOP', 9)
def_op('UNARY_POSITIVE', 10)
Expand All @@ -86,6 +87,7 @@ def jabs_op(name, op):
def_op('GET_AITER', 50)
def_op('GET_ANEXT', 51)
def_op('BEFORE_ASYNC_WITH', 52)
def_op('BEGIN_FINALLY', 53)

def_op('INPLACE_ADD', 55)
def_op('INPLACE_SUBTRACT', 56)
Expand Down Expand Up @@ -113,10 +115,8 @@ def jabs_op(name, op):
def_op('INPLACE_AND', 77)
def_op('INPLACE_XOR', 78)
def_op('INPLACE_OR', 79)
def_op('BREAK_LOOP', 80)
def_op('WITH_CLEANUP_START', 81)
def_op('WITH_CLEANUP_FINISH', 82)

def_op('RETURN_VALUE', 83)
def_op('IMPORT_STAR', 84)
def_op('SETUP_ANNOTATIONS', 85)
Expand Down Expand Up @@ -158,10 +158,7 @@ def jabs_op(name, op):

name_op('LOAD_GLOBAL', 116) # Index in name list

jabs_op('CONTINUE_LOOP', 119) # Target address
jrel_op('SETUP_LOOP', 120) # Distance to target address
jrel_op('SETUP_EXCEPT', 121) # ""
jrel_op('SETUP_FINALLY', 122) # ""
jrel_op('SETUP_FINALLY', 122) # Distance to target address

def_op('LOAD_FAST', 124) # Local variable number
haslocal.append(124)
Expand Down Expand Up @@ -213,5 +210,7 @@ def jabs_op(name, op):

name_op('LOAD_METHOD', 160)
def_op('CALL_METHOD', 161)
jrel_op('CALL_FINALLY', 162)
def_op('POP_FINALLY', 163)

del def_op, name_op, jrel_op, jabs_op
Loading