Skip to content

Commit b310d5e

Browse files
Use i-methods for in-place operations
1 parent fe3f45f commit b310d5e

File tree

4 files changed

+187
-62
lines changed

4 files changed

+187
-62
lines changed

vm/src/bytecode.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ pub enum Instruction {
7171
},
7272
BinaryOperation {
7373
op: BinaryOperator,
74+
inplace: bool,
7475
},
7576
LoadAttr {
7677
name: String,

vm/src/compile.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ impl Compiler {
608608
self.compile_expression(value)?;
609609

610610
// Perform operation:
611-
self.compile_op(op);
611+
self.compile_op(op, true);
612612
self.compile_store(target)?;
613613
}
614614
ast::Statement::Delete { targets } => {
@@ -757,7 +757,7 @@ impl Compiler {
757757
Ok(())
758758
}
759759

760-
fn compile_op(&mut self, op: &ast::Operator) {
760+
fn compile_op(&mut self, op: &ast::Operator, inplace: bool) {
761761
let i = match op {
762762
ast::Operator::Add => bytecode::BinaryOperator::Add,
763763
ast::Operator::Sub => bytecode::BinaryOperator::Subtract,
@@ -773,7 +773,7 @@ impl Compiler {
773773
ast::Operator::BitXor => bytecode::BinaryOperator::Xor,
774774
ast::Operator::BitAnd => bytecode::BinaryOperator::And,
775775
};
776-
self.emit(Instruction::BinaryOperation { op: i });
776+
self.emit(Instruction::BinaryOperation { op: i, inplace });
777777
}
778778

779779
fn compile_test(
@@ -852,13 +852,14 @@ impl Compiler {
852852
self.compile_expression(b)?;
853853

854854
// Perform operation:
855-
self.compile_op(op);
855+
self.compile_op(op, false);
856856
}
857857
ast::Expression::Subscript { a, b } => {
858858
self.compile_expression(a)?;
859859
self.compile_expression(b)?;
860860
self.emit(Instruction::BinaryOperation {
861861
op: bytecode::BinaryOperator::Subscript,
862+
inplace: false,
862863
});
863864
}
864865
ast::Expression::Unop { op, a } => {

vm/src/frame.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,9 @@ impl Frame {
316316
vm.call_method(&dict_obj, "__setitem__", vec![key, value])?;
317317
Ok(None)
318318
}
319-
bytecode::Instruction::BinaryOperation { ref op } => self.execute_binop(vm, op),
319+
bytecode::Instruction::BinaryOperation { ref op, inplace } => {
320+
self.execute_binop(vm, op, *inplace)
321+
}
320322
bytecode::Instruction::LoadAttr { ref name } => self.load_attr(vm, name),
321323
bytecode::Instruction::StoreAttr { ref name } => self.store_attr(vm, name),
322324
bytecode::Instruction::DeleteAttr { ref name } => self.delete_attr(vm, name),
@@ -893,23 +895,39 @@ impl Frame {
893895
&mut self,
894896
vm: &mut VirtualMachine,
895897
op: &bytecode::BinaryOperator,
898+
inplace: bool,
896899
) -> FrameResult {
897900
let b_ref = self.pop_value();
898901
let a_ref = self.pop_value();
899902
let value = match *op {
903+
bytecode::BinaryOperator::Subtract if inplace => vm._isub(a_ref, b_ref),
900904
bytecode::BinaryOperator::Subtract => vm._sub(a_ref, b_ref),
905+
bytecode::BinaryOperator::Add if inplace => vm._iadd(a_ref, b_ref),
901906
bytecode::BinaryOperator::Add => vm._add(a_ref, b_ref),
907+
bytecode::BinaryOperator::Multiply if inplace => vm._imul(a_ref, b_ref),
902908
bytecode::BinaryOperator::Multiply => vm._mul(a_ref, b_ref),
909+
bytecode::BinaryOperator::MatrixMultiply if inplace => vm._imatmul(a_ref, b_ref),
903910
bytecode::BinaryOperator::MatrixMultiply => vm._matmul(a_ref, b_ref),
911+
bytecode::BinaryOperator::Power if inplace => vm._ipow(a_ref, b_ref),
904912
bytecode::BinaryOperator::Power => vm._pow(a_ref, b_ref),
913+
bytecode::BinaryOperator::Divide if inplace => vm._itruediv(a_ref, b_ref),
905914
bytecode::BinaryOperator::Divide => vm._truediv(a_ref, b_ref),
915+
bytecode::BinaryOperator::FloorDivide if inplace => vm._ifloordiv(a_ref, b_ref),
906916
bytecode::BinaryOperator::FloorDivide => vm._floordiv(a_ref, b_ref),
917+
// TODO: Subscript should probably have its own op
918+
bytecode::BinaryOperator::Subscript if inplace => unreachable!(),
907919
bytecode::BinaryOperator::Subscript => self.subscript(vm, a_ref, b_ref),
920+
bytecode::BinaryOperator::Modulo if inplace => vm._imod(a_ref, b_ref),
908921
bytecode::BinaryOperator::Modulo => vm._mod(a_ref, b_ref),
922+
bytecode::BinaryOperator::Lshift if inplace => vm._ilshift(a_ref, b_ref),
909923
bytecode::BinaryOperator::Lshift => vm._lshift(a_ref, b_ref),
924+
bytecode::BinaryOperator::Rshift if inplace => vm._irshift(a_ref, b_ref),
910925
bytecode::BinaryOperator::Rshift => vm._rshift(a_ref, b_ref),
926+
bytecode::BinaryOperator::Xor if inplace => vm._ixor(a_ref, b_ref),
911927
bytecode::BinaryOperator::Xor => vm._xor(a_ref, b_ref),
928+
bytecode::BinaryOperator::Or if inplace => vm._ior(a_ref, b_ref),
912929
bytecode::BinaryOperator::Or => vm._or(a_ref, b_ref),
930+
bytecode::BinaryOperator::And if inplace => vm._iand(a_ref, b_ref),
913931
bytecode::BinaryOperator::And => vm._and(a_ref, b_ref),
914932
}?;
915933

0 commit comments

Comments
 (0)