diff --git a/crates/codegen/src/compile.rs b/crates/codegen/src/compile.rs index 4f7174c8ba..0131b3008e 100644 --- a/crates/codegen/src/compile.rs +++ b/crates/codegen/src/compile.rs @@ -1063,7 +1063,7 @@ impl Compiler { } ); - emit!(self, Instruction::Pop); + emit!(self, Instruction::PopTop); } else { self.compile_statement(statement)?; } @@ -1079,7 +1079,7 @@ impl Compiler { } ); - emit!(self, Instruction::Pop); + emit!(self, Instruction::PopTop); } else { self.compile_statement(last)?; self.emit_load_const(ConstantData::None); @@ -1104,7 +1104,7 @@ impl Compiler { if let Some(last_statement) = body.last() { match last_statement { Stmt::Expr(_) => { - self.current_block().instructions.pop(); // pop Instruction::Pop + self.current_block().instructions.pop(); // pop Instruction::PopTop } Stmt::FunctionDef(_) | Stmt::ClassDef(_) => { let pop_instructions = self.current_block().instructions.pop(); @@ -1401,14 +1401,14 @@ impl Compiler { } // Pop module from stack: - emit!(self, Instruction::Pop); + emit!(self, Instruction::PopTop); } } Stmt::Expr(StmtExpr { value, .. }) => { self.compile_expression(value)?; // Pop result of stack, since we not use it: - emit!(self, Instruction::Pop); + emit!(self, Instruction::PopTop); } Stmt::Global(_) | Stmt::Nonlocal(_) => { // Handled during symbol table construction. @@ -2051,12 +2051,12 @@ impl Compiler { self.store_name(alias.as_str())? } else { // Drop exception from top of stack: - emit!(self, Instruction::Pop); + emit!(self, Instruction::PopTop); } } else { // Catch all! // Drop exception from top of stack: - emit!(self, Instruction::Pop); + emit!(self, Instruction::PopTop); } // Handler code: @@ -2950,7 +2950,7 @@ impl Compiler { self.compile_store(var)?; } None => { - emit!(self, Instruction::Pop); + emit!(self, Instruction::PopTop); } } final_block @@ -3143,7 +3143,7 @@ impl Compiler { for &label in pc.fail_pop.iter().skip(1).rev() { self.switch_to_block(label); // Emit the POP instruction. - emit!(self, Instruction::Pop); + emit!(self, Instruction::PopTop); } // Finally, use the first label. self.switch_to_block(pc.fail_pop[0]); @@ -3187,7 +3187,7 @@ impl Compiler { match n { // If no name is provided, simply pop the top of the stack. None => { - emit!(self, Instruction::Pop); + emit!(self, Instruction::PopTop); Ok(()) } Some(name) => { @@ -3313,7 +3313,7 @@ impl Compiler { } // Pop the subject off the stack. pc.on_top -= 1; - emit!(self, Instruction::Pop); + emit!(self, Instruction::PopTop); Ok(()) } @@ -3497,7 +3497,7 @@ impl Compiler { pc.on_top -= 1; if is_true_wildcard { - emit!(self, Instruction::Pop); + emit!(self, Instruction::PopTop); continue; // Don't compile wildcard patterns } @@ -3548,7 +3548,7 @@ impl Compiler { if size == 0 && star_target.is_none() { // If the pattern is just "{}", we're done! Pop the subject pc.on_top -= 1; - emit!(self, Instruction::Pop); + emit!(self, Instruction::PopTop); return Ok(()); } @@ -3703,8 +3703,8 @@ impl Compiler { // Non-rest pattern: just clean up the stack // Pop them as we're not using them - emit!(self, Instruction::Pop); // Pop keys_tuple - emit!(self, Instruction::Pop); // Pop subject + emit!(self, Instruction::PopTop); // Pop keys_tuple + emit!(self, Instruction::PopTop); // Pop subject } Ok(()) @@ -3792,7 +3792,7 @@ impl Compiler { // In Rust, old_pc is a local clone, so we need not worry about that. // No alternative matched: pop the subject and fail. - emit!(self, Instruction::Pop); + emit!(self, Instruction::PopTop); self.jump_to_fail_pop(pc, JumpOp::Jump)?; // Use the label "end". @@ -3814,7 +3814,7 @@ impl Compiler { // Old context and control will be dropped automatically. // Finally, pop the copy of the subject. - emit!(self, Instruction::Pop); + emit!(self, Instruction::PopTop); Ok(()) } @@ -3888,7 +3888,7 @@ impl Compiler { pc.on_top -= 1; if only_wildcard { // Patterns like: [] / [_] / [_, _] / [*_] / [_, *_] / [_, _, *_] / etc. - emit!(self, Instruction::Pop); + emit!(self, Instruction::PopTop); } else if star_wildcard { self.pattern_helper_sequence_subscr(patterns, star.unwrap(), pc)?; } else { @@ -4012,7 +4012,7 @@ impl Compiler { } if i != case_count - 1 { - emit!(self, Instruction::Pop); + emit!(self, Instruction::PopTop); } self.compile_statements(&m.body)?; @@ -4023,7 +4023,7 @@ impl Compiler { if has_default { let m = &cases[num_cases - 1]; if num_cases == 1 { - emit!(self, Instruction::Pop); + emit!(self, Instruction::PopTop); } else { emit!(self, Instruction::Nop); } @@ -4125,7 +4125,7 @@ impl Compiler { // early exit left us with stack: `rhs, comparison_result`. We need to clean up rhs. self.switch_to_block(break_block); emit!(self, Instruction::Swap { index: 2 }); - emit!(self, Instruction::Pop); + emit!(self, Instruction::PopTop); self.switch_to_block(after_block); } @@ -4192,7 +4192,7 @@ impl Compiler { emit!(self, Instruction::StoreSubscript); } else { // Drop annotation if not assigned to simple identifier. - emit!(self, Instruction::Pop); + emit!(self, Instruction::PopTop); } Ok(()) @@ -4823,7 +4823,7 @@ impl Compiler { arg: bytecode::ResumeType::AfterYield as u32 } ); - emit!(compiler, Instruction::Pop); + emit!(compiler, Instruction::PopTop); Ok(()) }, diff --git a/crates/codegen/src/snapshots/rustpython_codegen__compile__tests__if_ands.snap b/crates/codegen/src/snapshots/rustpython_codegen__compile__tests__if_ands.snap index 8b2907ef6f..4c9c29887e 100644 --- a/crates/codegen/src/snapshots/rustpython_codegen__compile__tests__if_ands.snap +++ b/crates/codegen/src/snapshots/rustpython_codegen__compile__tests__if_ands.snap @@ -1,12 +1,12 @@ --- -source: compiler/codegen/src/compile.rs +source: crates/codegen/src/compile.rs expression: "compile_exec(\"\\\nif True and False and False:\n pass\n\")" --- - 1 0 LoadConst (True) - 1 PopJumpIfFalse (6) - 2 LoadConst (False) - 3 PopJumpIfFalse (6) - 4 LoadConst (False) - 5 PopJumpIfFalse (6) + 1 0 LOAD_CONST (True) + 1 POP_JUMP_IF_FALSE (6) + 2 LOAD_CONST (False) + 3 POP_JUMP_IF_FALSE (6) + 4 LOAD_CONST (False) + 5 POP_JUMP_IF_FALSE (6) - 2 >> 6 ReturnConst (None) + 2 >> 6 RETURN_CONST (None) diff --git a/crates/codegen/src/snapshots/rustpython_codegen__compile__tests__if_mixed.snap b/crates/codegen/src/snapshots/rustpython_codegen__compile__tests__if_mixed.snap index fc91a74283..a93479df96 100644 --- a/crates/codegen/src/snapshots/rustpython_codegen__compile__tests__if_mixed.snap +++ b/crates/codegen/src/snapshots/rustpython_codegen__compile__tests__if_mixed.snap @@ -1,14 +1,14 @@ --- -source: compiler/codegen/src/compile.rs +source: crates/codegen/src/compile.rs expression: "compile_exec(\"\\\nif (True and False) or (False and True):\n pass\n\")" --- - 1 0 LoadConst (True) - 1 PopJumpIfFalse (4) - 2 LoadConst (False) - 3 PopJumpIfTrue (8) - >> 4 LoadConst (False) - 5 PopJumpIfFalse (8) - 6 LoadConst (True) - 7 PopJumpIfFalse (8) + 1 0 LOAD_CONST (True) + 1 POP_JUMP_IF_FALSE (4) + 2 LOAD_CONST (False) + 3 POP_JUMP_IF_TRUE (8) + >> 4 LOAD_CONST (False) + 5 POP_JUMP_IF_FALSE (8) + 6 LOAD_CONST (True) + 7 POP_JUMP_IF_FALSE (8) - 2 >> 8 ReturnConst (None) + 2 >> 8 RETURN_CONST (None) diff --git a/crates/codegen/src/snapshots/rustpython_codegen__compile__tests__if_ors.snap b/crates/codegen/src/snapshots/rustpython_codegen__compile__tests__if_ors.snap index 9be7c2af7b..37a862cc65 100644 --- a/crates/codegen/src/snapshots/rustpython_codegen__compile__tests__if_ors.snap +++ b/crates/codegen/src/snapshots/rustpython_codegen__compile__tests__if_ors.snap @@ -1,12 +1,12 @@ --- -source: compiler/codegen/src/compile.rs +source: crates/codegen/src/compile.rs expression: "compile_exec(\"\\\nif True or False or False:\n pass\n\")" --- - 1 0 LoadConst (True) - 1 PopJumpIfTrue (6) - 2 LoadConst (False) - 3 PopJumpIfTrue (6) - 4 LoadConst (False) - 5 PopJumpIfFalse (6) + 1 0 LOAD_CONST (True) + 1 POP_JUMP_IF_TRUE (6) + 2 LOAD_CONST (False) + 3 POP_JUMP_IF_TRUE (6) + 4 LOAD_CONST (False) + 5 POP_JUMP_IF_FALSE (6) - 2 >> 6 ReturnConst (None) + 2 >> 6 RETURN_CONST (None) diff --git a/crates/codegen/src/snapshots/rustpython_codegen__compile__tests__nested_double_async_with.snap b/crates/codegen/src/snapshots/rustpython_codegen__compile__tests__nested_double_async_with.snap index 435b73a14d..6cd7d4d523 100644 --- a/crates/codegen/src/snapshots/rustpython_codegen__compile__tests__nested_double_async_with.snap +++ b/crates/codegen/src/snapshots/rustpython_codegen__compile__tests__nested_double_async_with.snap @@ -2,85 +2,85 @@ source: crates/codegen/src/compile.rs expression: "compile_exec(\"\\\nfor stop_exc in (StopIteration('spam'), StopAsyncIteration('ham')):\n with self.subTest(type=type(stop_exc)):\n try:\n async with egg():\n raise stop_exc\n except Exception as ex:\n self.assertIs(ex, stop_exc)\n else:\n self.fail(f'{stop_exc} was suppressed')\n\")" --- - 1 0 SetupLoop - 1 LoadNameAny (0, StopIteration) - 2 LoadConst ("spam") - 3 CallFunctionPositional(1) - 4 LoadNameAny (1, StopAsyncIteration) - 5 LoadConst ("ham") - 6 CallFunctionPositional(1) - 7 BuildTuple (2) - 8 GetIter - >> 9 ForIter (71) - 10 StoreLocal (2, stop_exc) + 1 0 SETUP_LOOP + 1 LOAD_NAME_ANY (0, StopIteration) + 2 LOAD_CONST ("spam") + 3 CALL_FUNCTION_POSITIONAL(1) + 4 LOAD_NAME_ANY (1, StopAsyncIteration) + 5 LOAD_CONST ("ham") + 6 CALL_FUNCTION_POSITIONAL(1) + 7 BUILD_TUPLE (2) + 8 GET_ITER + >> 9 FOR_ITER (71) + 10 STORE_LOCAL (2, stop_exc) - 2 11 LoadNameAny (3, self) - 12 LoadMethod (4, subTest) - 13 LoadNameAny (5, type) - 14 LoadNameAny (2, stop_exc) - 15 CallFunctionPositional(1) - 16 LoadConst (("type")) - 17 CallMethodKeyword (1) - 18 SetupWith (68) - 19 Pop + 2 11 LOAD_NAME_ANY (3, self) + 12 LOAD_METHOD (4, subTest) + 13 LOAD_NAME_ANY (5, type) + 14 LOAD_NAME_ANY (2, stop_exc) + 15 CALL_FUNCTION_POSITIONAL(1) + 16 LOAD_CONST (("type")) + 17 CALL_METHOD_KEYWORD (1) + 18 SETUP_WITH (68) + 19 POP_TOP - 3 20 SetupExcept (42) + 3 20 SETUP_EXCEPT (42) - 4 21 LoadNameAny (6, egg) - 22 CallFunctionPositional(0) - 23 BeforeAsyncWith - 24 GetAwaitable - 25 LoadConst (None) - 26 YieldFrom - 27 Resume (3) - 28 SetupAsyncWith (34) - 29 Pop + 4 21 LOAD_NAME_ANY (6, egg) + 22 CALL_FUNCTION_POSITIONAL(0) + 23 BEFORE_ASYNC_WITH + 24 GET_AWAITABLE + 25 LOAD_CONST (None) + 26 YIELD_FROM + 27 RESUME (3) + 28 SETUP_ASYNC_WITH (34) + 29 POP_TOP - 5 30 LoadNameAny (2, stop_exc) - 31 Raise (Raise) + 5 30 LOAD_NAME_ANY (2, stop_exc) + 31 RAISE (Raise) - 4 32 PopBlock - 33 EnterFinally - >> 34 WithCleanupStart - 35 GetAwaitable - 36 LoadConst (None) - 37 YieldFrom - 38 Resume (3) - 39 WithCleanupFinish - 40 PopBlock - 41 Jump (58) - >> 42 CopyItem (1) + 4 32 POP_BLOCK + 33 ENTER_FINALLY + >> 34 WITH_CLEANUP_START + 35 GET_AWAITABLE + 36 LOAD_CONST (None) + 37 YIELD_FROM + 38 RESUME (3) + 39 WITH_CLEANUP_FINISH + 40 POP_BLOCK + 41 JUMP (58) + >> 42 COPY (1) - 6 43 LoadNameAny (7, Exception) + 6 43 LOAD_NAME_ANY (7, Exception) 44 JUMP_IF_NOT_EXC_MATCH(57) - 45 StoreLocal (8, ex) + 45 STORE_LOCAL (8, ex) - 7 46 LoadNameAny (3, self) - 47 LoadMethod (9, assertIs) - 48 LoadNameAny (8, ex) - 49 LoadNameAny (2, stop_exc) - 50 CallMethodPositional (2) - 51 Pop - 52 PopException - 53 LoadConst (None) - 54 StoreLocal (8, ex) - 55 DeleteLocal (8, ex) - 56 Jump (66) - >> 57 Raise (Reraise) + 7 46 LOAD_NAME_ANY (3, self) + 47 LOAD_METHOD (9, assertIs) + 48 LOAD_NAME_ANY (8, ex) + 49 LOAD_NAME_ANY (2, stop_exc) + 50 CALL_METHOD_POSITIONAL(2) + 51 POP_TOP + 52 POP_EXCEPTION + 53 LOAD_CONST (None) + 54 STORE_LOCAL (8, ex) + 55 DELETE_LOCAL (8, ex) + 56 JUMP (66) + >> 57 RAISE (Reraise) - 9 >> 58 LoadNameAny (3, self) - 59 LoadMethod (10, fail) - 60 LoadNameAny (2, stop_exc) + 9 >> 58 LOAD_NAME_ANY (3, self) + 59 LOAD_METHOD (10, fail) + 60 LOAD_NAME_ANY (2, stop_exc) 61 FORMAT_SIMPLE - 62 LoadConst (" was suppressed") - 63 BuildString (2) - 64 CallMethodPositional (1) - 65 Pop + 62 LOAD_CONST (" was suppressed") + 63 BUILD_STRING (2) + 64 CALL_METHOD_POSITIONAL(1) + 65 POP_TOP - 2 >> 66 PopBlock - 67 EnterFinally - >> 68 WithCleanupStart - 69 WithCleanupFinish - 70 Jump (9) - >> 71 PopBlock - 72 ReturnConst (None) + 2 >> 66 POP_BLOCK + 67 ENTER_FINALLY + >> 68 WITH_CLEANUP_START + 69 WITH_CLEANUP_FINISH + 70 JUMP (9) + >> 71 POP_BLOCK + 72 RETURN_CONST (None) diff --git a/crates/compiler-core/src/bytecode.rs b/crates/compiler-core/src/bytecode.rs index 11d2a7b5f1..dd49e679f2 100644 --- a/crates/compiler-core/src/bytecode.rs +++ b/crates/compiler-core/src/bytecode.rs @@ -782,7 +782,6 @@ pub enum Instruction { MatchMapping, MatchSequence, Nop, - Pop, PopBlock, PopException, /// Pop the top of the stack, and jump if this value is false. @@ -793,6 +792,11 @@ pub enum Instruction { PopJumpIfTrue { target: Arg