Skip to content

Commit 64f4d7f

Browse files
committed
Revert "Remove currently unused Nop,Swap op"
This reverts commit 4beef2c.
1 parent 4beef2c commit 64f4d7f

3 files changed

Lines changed: 20 additions & 1 deletion

File tree

compiler/codegen/src/compile.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1880,7 +1880,7 @@ impl Compiler {
18801880
emit!(self, Instruction::Pop);
18811881
} else {
18821882
// Show line coverage for default case (it doesn't create bytecode)
1883-
// emit!(self, Instruction::Nop);
1883+
emit!(self, Instruction::Nop);
18841884
}
18851885
self.compile_statements(&m.body)?;
18861886
}

compiler/core/src/bytecode.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,10 @@ pub type NameIdx = u32;
370370
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
371371
#[repr(u8)]
372372
pub enum Instruction {
373+
/// No-op, don't do anything at all
374+
///
375+
/// Equivalent to `NOP` in cpython bytecode
376+
Nop,
373377
/// Importing by name
374378
ImportName {
375379
idx: Arg<NameIdx>,
@@ -428,6 +432,9 @@ pub enum Instruction {
428432
op: Arg<ComparisonOperator>,
429433
},
430434
Pop,
435+
Swap {
436+
index: Arg<u32>,
437+
},
431438
Rotate2,
432439
Rotate3,
433440
Duplicate,
@@ -1177,6 +1184,7 @@ impl Instruction {
11771184
///
11781185
pub fn stack_effect(&self, arg: OpArg, jump: bool) -> i32 {
11791186
match self {
1187+
Nop => 0,
11801188
ImportName { .. } | ImportNameless => -1,
11811189
ImportStar => -1,
11821190
ImportFrom { .. } => 1,
@@ -1197,6 +1205,7 @@ impl Instruction {
11971205
| TestOperation { .. }
11981206
| CompareOperation { .. } => -1,
11991207
Pop => -1,
1208+
Swap { .. } => 0,
12001209
Rotate2 | Rotate3 => 0,
12011210
Duplicate => 1,
12021211
Duplicate2 => 2,
@@ -1361,6 +1370,7 @@ impl Instruction {
13611370
};
13621371

13631372
match self {
1373+
Nop => w!(Nop),
13641374
ImportName { idx } => w!(ImportName, name = idx),
13651375
ImportNameless => w!(ImportNameless),
13661376
ImportStar => w!(ImportStar),
@@ -1392,6 +1402,7 @@ impl Instruction {
13921402
TestOperation { op } => w!(TestOperation, ?op),
13931403
CompareOperation { op } => w!(CompareOperation, ?op),
13941404
Pop => w!(Pop),
1405+
Swap { index } => w!(Swap, index),
13951406
Rotate2 => w!(Rotate2),
13961407
Rotate3 => w!(Rotate3),
13971408
Duplicate => w!(Duplicate),

vm/src/frame.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,7 @@ impl ExecutingFrame<'_> {
517517
}
518518

519519
match instruction {
520+
bytecode::Instruction::Nop => Ok(None),
520521
bytecode::Instruction::LoadConst { idx } => {
521522
self.push_value(self.code.constants[idx.get(arg) as usize].clone().into());
522523
Ok(None)
@@ -672,6 +673,13 @@ impl ExecutingFrame<'_> {
672673
self.pop_value();
673674
Ok(None)
674675
}
676+
bytecode::Instruction::Swap { index } => {
677+
let len = self.state.stack.len();
678+
self.state
679+
.stack
680+
.swap(len - 1, len - 1 - index.get(arg) as usize);
681+
Ok(None)
682+
}
675683
bytecode::Instruction::Duplicate => {
676684
// Duplicate top of stack
677685
let value = self.top_value();

0 commit comments

Comments
 (0)