Split Rotate into Rotate2 & Rotate3 · ChJR/RustPython@f93b134 · GitHub
Skip to content

Commit f93b134

Browse files
committed
Split Rotate into Rotate2 & Rotate3
1 parent f04a305 commit f93b134

4 files changed

Lines changed: 26 additions & 42 deletions

File tree

bytecode/src/lib.rs

Lines changed: 5 additions & 5 deletions

compiler/src/compile.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,7 +1122,7 @@ impl Compiler {
11221122

11231123
self.emit(Instruction::Duplicate);
11241124
self.load_docstring(doc_str);
1125-
self.emit(Instruction::Rotate { amount: 2 });
1125+
self.emit(Instruction::Rotate2);
11261126
let doc = self.name("__doc__");
11271127
self.emit(Instruction::StoreAttr { idx: doc });
11281128

@@ -1498,7 +1498,7 @@ impl Compiler {
14981498
self.compile_expression(val)?;
14991499
// store rhs for the next comparison in chain
15001500
self.emit(Instruction::Duplicate);
1501-
self.emit(Instruction::Rotate { amount: 3 });
1501+
self.emit(Instruction::Rotate3);
15021502

15031503
self.emit(Instruction::CompareOperation {
15041504
op: compile_cmpop(op),
@@ -1525,7 +1525,7 @@ impl Compiler {
15251525

15261526
// early exit left us with stack: `rhs, comparison_result`. We need to clean up rhs.
15271527
self.switch_to_block(break_block);
1528-
self.emit(Instruction::Rotate { amount: 2 });
1528+
self.emit(Instruction::Rotate2);
15291529
self.emit(Instruction::Pop);
15301530

15311531
self.switch_to_block(after_block);
@@ -1692,12 +1692,12 @@ impl Compiler {
16921692
}
16931693
AugAssignKind::Subscript => {
16941694
// stack: CONTAINER SLICE RESULT
1695-
self.emit(Instruction::Rotate { amount: 3 });
1695+
self.emit(Instruction::Rotate3);
16961696
self.emit(Instruction::StoreSubscript);
16971697
}
16981698
AugAssignKind::Attr { idx } => {
16991699
// stack: CONTAINER RESULT
1700-
self.emit(Instruction::Rotate { amount: 2 });
1700+
self.emit(Instruction::Rotate2);
17011701
self.emit(Instruction::StoreAttr { idx });
17021702
}
17031703
}

jit/tests/common.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -132,17 +132,13 @@ impl StackMachine {
132132
let value = self.stack.last().unwrap().clone();
133133
self.stack.push(value);
134134
}
135-
Instruction::Rotate { amount } => {
136-
let mut values = Vec::new();
137-
138-
// Pop all values from stack:
139-
values.extend(self.stack.drain(self.stack.len() - amount as usize..));
140-
141-
// Push top of stack back first:
142-
self.stack.push(values.pop().unwrap());
143-
144-
// Push other value back in order:
145-
self.stack.extend(values);
135+
Instruction::Rotate2 => {
136+
let i = self.stack.len() - 2;
137+
self.stack[i..].rotate_right(1);
138+
}
139+
Instruction::Rotate3 => {
140+
let i = self.stack.len() - 3;
141+
self.stack[i..].rotate_right(1);
146142
}
147143
Instruction::ReturnValue => return true,
148144
_ => unimplemented!(

vm/src/frame.rs

Lines changed: 9 additions & 21 deletions

0 commit comments

Comments
 (0)