MCPcopy Index your code
hub / github.com/RustPython/RustPython / execute_bin_op

Method execute_bin_op

crates/vm/src/frame.rs:7021–7081  ·  view source on GitHub ↗
(&mut self, vm: &VirtualMachine, op: bytecode::BinaryOperator)

Source from the content-addressed store, hash-verified

7019
7020 #[cfg_attr(feature = "flame-it", flame("Frame"))]
7021 fn execute_bin_op(&mut self, vm: &VirtualMachine, op: bytecode::BinaryOperator) -> FrameResult {
7022 let b_ref = &self.pop_value();
7023 let a_ref = &self.pop_value();
7024 let value = match op {
7025 // BINARY_OP_ADD_INT / BINARY_OP_SUBTRACT_INT fast paths:
7026 // bypass binary_op1 dispatch for exact int types, use i64 arithmetic
7027 // when possible to avoid BigInt heap allocation.
7028 bytecode::BinaryOperator::Add | bytecode::BinaryOperator::InplaceAdd => {
7029 if let (Some(a), Some(b)) = (
7030 a_ref.downcast_ref_if_exact::<PyInt>(vm),
7031 b_ref.downcast_ref_if_exact::<PyInt>(vm),
7032 ) {
7033 Ok(self.int_add(a.as_bigint(), b.as_bigint(), vm))
7034 } else if matches!(op, bytecode::BinaryOperator::Add) {
7035 vm._add(a_ref, b_ref)
7036 } else {
7037 vm._iadd(a_ref, b_ref)
7038 }
7039 }
7040 bytecode::BinaryOperator::Subtract | bytecode::BinaryOperator::InplaceSubtract => {
7041 if let (Some(a), Some(b)) = (
7042 a_ref.downcast_ref_if_exact::<PyInt>(vm),
7043 b_ref.downcast_ref_if_exact::<PyInt>(vm),
7044 ) {
7045 Ok(self.int_sub(a.as_bigint(), b.as_bigint(), vm))
7046 } else if matches!(op, bytecode::BinaryOperator::Subtract) {
7047 vm._sub(a_ref, b_ref)
7048 } else {
7049 vm._isub(a_ref, b_ref)
7050 }
7051 }
7052 bytecode::BinaryOperator::Multiply => vm._mul(a_ref, b_ref),
7053 bytecode::BinaryOperator::MatrixMultiply => vm._matmul(a_ref, b_ref),
7054 bytecode::BinaryOperator::Power => vm._pow(a_ref, b_ref, vm.ctx.none.as_object()),
7055 bytecode::BinaryOperator::TrueDivide => vm._truediv(a_ref, b_ref),
7056 bytecode::BinaryOperator::FloorDivide => vm._floordiv(a_ref, b_ref),
7057 bytecode::BinaryOperator::Remainder => vm._mod(a_ref, b_ref),
7058 bytecode::BinaryOperator::Lshift => vm._lshift(a_ref, b_ref),
7059 bytecode::BinaryOperator::Rshift => vm._rshift(a_ref, b_ref),
7060 bytecode::BinaryOperator::Xor => vm._xor(a_ref, b_ref),
7061 bytecode::BinaryOperator::Or => vm._or(a_ref, b_ref),
7062 bytecode::BinaryOperator::And => vm._and(a_ref, b_ref),
7063 bytecode::BinaryOperator::InplaceMultiply => vm._imul(a_ref, b_ref),
7064 bytecode::BinaryOperator::InplaceMatrixMultiply => vm._imatmul(a_ref, b_ref),
7065 bytecode::BinaryOperator::InplacePower => {
7066 vm._ipow(a_ref, b_ref, vm.ctx.none.as_object())
7067 }
7068 bytecode::BinaryOperator::InplaceTrueDivide => vm._itruediv(a_ref, b_ref),
7069 bytecode::BinaryOperator::InplaceFloorDivide => vm._ifloordiv(a_ref, b_ref),
7070 bytecode::BinaryOperator::InplaceRemainder => vm._imod(a_ref, b_ref),
7071 bytecode::BinaryOperator::InplaceLshift => vm._ilshift(a_ref, b_ref),
7072 bytecode::BinaryOperator::InplaceRshift => vm._irshift(a_ref, b_ref),
7073 bytecode::BinaryOperator::InplaceXor => vm._ixor(a_ref, b_ref),
7074 bytecode::BinaryOperator::InplaceOr => vm._ior(a_ref, b_ref),
7075 bytecode::BinaryOperator::InplaceAnd => vm._iand(a_ref, b_ref),
7076 bytecode::BinaryOperator::Subscr => a_ref.get_item(b_ref.as_object(), vm),
7077 }?;
7078

Callers 3

execute_instructionMethod · 0.80
execute_binary_op_intMethod · 0.80

Calls 14

pop_valueMethod · 0.80
int_addMethod · 0.80
as_bigintMethod · 0.80
_iaddMethod · 0.80
int_subMethod · 0.80
_subMethod · 0.80
_floordivMethod · 0.80
_modMethod · 0.80
_imulMethod · 0.80
push_valueMethod · 0.80
_addMethod · 0.45
_mulMethod · 0.45

Tested by

no test coverage detected