Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Remove Instruction::Rotate* instructions
  • Loading branch information
ShaharNaveh committed Nov 28, 2025
commit 0f4f62fee020065d14f913e436f74c7d01bda35a
13 changes: 7 additions & 6 deletions crates/codegen/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2255,7 +2255,7 @@ impl Compiler {
self.emit_load_const(ConstantData::Str {
value: doc.to_string().into(),
});
emit!(self, Instruction::Rotate2);
emit!(self, Instruction::Swap { index: 2 });
let doc_attr = self.name("__doc__");
emit!(self, Instruction::StoreAttr { idx: doc_attr });
}
Expand Down Expand Up @@ -4121,8 +4121,8 @@ impl Compiler {
for (op, val) in mid_ops.iter().zip(mid_exprs) {
self.compile_expression(val)?;
// store rhs for the next comparison in chain
emit!(self, Instruction::CopyItem { index: 1_u32 });
emit!(self, Instruction::Rotate3);
emit!(self, Instruction::Swap { index: 2 });
emit!(self, Instruction::CopyItem { index: 2_u32 });

compile_cmpop(self, op);

Expand Down Expand Up @@ -4151,7 +4151,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::Rotate2);
emit!(self, Instruction::Swap { index: 2 });
emit!(self, Instruction::Pop);

self.switch_to_block(after_block);
Expand Down Expand Up @@ -4351,12 +4351,13 @@ impl Compiler {
}
AugAssignKind::Subscript => {
// stack: CONTAINER SLICE RESULT
emit!(self, Instruction::Rotate3);
emit!(self, Instruction::Swap { index: 3 });
emit!(self, Instruction::Swap { index: 2 });
emit!(self, Instruction::StoreSubscript);
}
AugAssignKind::Attr { idx } => {
// stack: CONTAINER RESULT
emit!(self, Instruction::Rotate2);
emit!(self, Instruction::Swap { index: 2 });
emit!(self, Instruction::StoreAttr { idx });
}
}
Expand Down
5 changes: 0 additions & 5 deletions crates/compiler-core/src/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -614,8 +614,6 @@ pub enum Instruction {
index: Arg<u32>,
},
ToBool,
Rotate2,
Rotate3,
GetIter,
GetLen,
CallIntrinsic1 {
Expand Down Expand Up @@ -1473,7 +1471,6 @@ impl Instruction {
Pop => -1,
Swap { .. } => 0,
ToBool => 0,
Rotate2 | Rotate3 => 0,
GetIter => 0,
GetLen => 1,
CallIntrinsic1 { .. } => 0, // Takes 1, pushes 1
Expand Down Expand Up @@ -1674,8 +1671,6 @@ impl Instruction {
Pop => w!(Pop),
Swap { index } => w!(Swap, index),
ToBool => w!(ToBool),
Rotate2 => w!(Rotate2),
Rotate3 => w!(Rotate3),
GetIter => w!(GetIter),
// GET_LEN
GetLen => w!(GetLen),
Expand Down
12 changes: 0 additions & 12 deletions crates/vm/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -729,11 +729,6 @@ impl ExecutingFrame<'_> {
Ok(None)
}
*/
// splitting the instructions like this offloads the cost of "dynamic" dispatch (on the
// amount to rotate) to the opcode dispatcher, and generates optimized code for the
// concrete cases we actually have
bytecode::Instruction::Rotate2 => self.execute_rotate(2),
bytecode::Instruction::Rotate3 => self.execute_rotate(3),
bytecode::Instruction::BuildString { size } => {
let s = self
.pop_multiple(size.get(arg) as usize)
Expand Down Expand Up @@ -1674,13 +1669,6 @@ impl ExecutingFrame<'_> {
}
}

#[inline(always)]
fn execute_rotate(&mut self, amount: usize) -> FrameResult {
let i = self.state.stack.len() - amount;
self.state.stack[i..].rotate_right(1);
Ok(None)
}

fn execute_subscript(&mut self, vm: &VirtualMachine) -> FrameResult {
let b_ref = self.pop_value();
let a_ref = self.pop_value();
Expand Down
Loading