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 1 commit
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
Prev Previous commit
Next Next commit
Polishing.
  • Loading branch information
serhiy-storchaka committed Dec 28, 2017
commit bef9131d9a320c0db84fb23c4469adb6901bbfda
2 changes: 1 addition & 1 deletion Doc/library/dis.rst
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@ iterations of the loop.
re-raise the exception and restore ``sys.exc_info()``. 9 values are
popped from the stack.

.. versionchanged:: 3.6
.. versionchanged:: 3.7
WITH_CLEANUP_FINISH now includes the functionality of
:opcode:`END_FINALLY`.

Expand Down
4 changes: 2 additions & 2 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -2886,7 +2886,7 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
to update the PyGen_NeedsFinalizing() function.
*/

PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
STACK_LEVEL());
DISPATCH();
}
Expand Down Expand Up @@ -3389,7 +3389,7 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
continue;
}
UNWIND_BLOCK(b);
if ((b->b_type == SETUP_FINALLY)) {
if (b->b_type == SETUP_FINALLY) {
PyObject *exc, *val, *tb;
int handler = b->b_handler;
_PyErr_StackItem *exc_info = tstate->exc_info;
Expand Down
33 changes: 15 additions & 18 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,6 @@ compiler IR.
enum fblocktype { WHILE_LOOP, FOR_LOOP, EXCEPT, FINALLY_TRY, FINALLY_END,
WITH, ASYNC_WITH, HANDLER_CLEANUP };

struct fblockinfo;
struct compiler;

struct fblockinfo {
enum fblocktype fb_type;
basicblock *fb_block;
Expand Down Expand Up @@ -960,22 +957,22 @@ PyCompile_OpcodeStackEffect(int opcode, int oparg)

/* Exception handling */
case SETUP_WITH:
/* Replace TOP with __exit__, reserve 6 cells for an exception,
/* Replace TOP with __exit__, reserve 6 entries for an exception,
* and push the result of __enter__() */
return 7;
case WITH_CLEANUP_START:
return 2;
case WITH_CLEANUP_FINISH:
/* Pop 2 values pushed by WITH_CLEANUP_START, adjust 6 cells
/* Pop 2 values pushed by WITH_CLEANUP_START, adjust 6 entries
* reserved by SETUP_WITH, and pop __exit__ */
return -9;
case POP_EXCEPT:
return -3;
case CALL_FINALLY:
return 0;
case SETUP_FINALLY:
/* Reserve 3 items for the new exception
* + 3 others for the previous exception state */
/* Reserve 3 entries for the new exception
* + 3 entries for the previous exception state */
return 6;
case BEGIN_FINALLY:
/* The stack has been reserved by SETUP_FINALLY */
Expand All @@ -994,7 +991,7 @@ PyCompile_OpcodeStackEffect(int opcode, int oparg)
case YIELD_FROM:
return -1;
case POP_BLOCK:
/* Adjust 6 cells reserved by SETUP_FINALLY */
/* Adjust 6 entries reserved by SETUP_FINALLY */
return -6;
case FOR_ITER:
return 1; /* or -1, at end of iterator */
Expand Down Expand Up @@ -1059,7 +1056,7 @@ PyCompile_OpcodeStackEffect(int opcode, int oparg)
case GET_AWAITABLE:
return 0;
case SETUP_ASYNC_WITH:
/* Reserve 6 cells for an exception */
/* Reserve 6 entries for an exception */
return 6;
case BEFORE_ASYNC_WITH:
return 1;
Expand Down Expand Up @@ -2604,18 +2601,18 @@ compiler_continue(struct compiler *c)
Pushes the current value stack level and the label
onto the block stack.
POP_BLOCK:
Pops en entry from the block stack, and pops the value
stack until its level is the same as indicated on the
block stack. (The label is ignored.)
Pops en entry from the block stack.
BEGIN_FINALLY
Pushes NULL onto the value stack.
END_FINALLY:
Pops 3 entries from the *value* stack and re-raises the exception
they specify.
Pops 1 (NULL or int) or 6 entries from the *value* stack and restore
the raised and the caught exceptions they specify.

The block stack is unwound when an exception is raised:
when a SETUP_FINALLY entry is found, the exception is pushed
onto the value stack (and the exception condition is cleared),
and the interpreter jumps to the label gotten from the block
stack.
when a SETUP_FINALLY entry is found, the raised and the caught
exceptions are pushed onto the value stack (and the exception
condition is cleared), and the interpreter jumps to the label
gotten from the block stack.
*/

static int
Expand Down