Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
38 changes: 18 additions & 20 deletions crates/codegen/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3301,7 +3301,7 @@ impl Compiler {
// Subtract to compute the correct index.
emit!(
self,
Instruction::BinaryOperation {
Instruction::BinaryOp {
op: BinaryOperator::Subtract
}
);
Expand Down Expand Up @@ -4339,26 +4339,24 @@ impl Compiler {
}

fn compile_op(&mut self, op: &Operator, inplace: bool) {
let op = match op {
Operator::Add => bytecode::BinaryOperator::Add,
Operator::Sub => bytecode::BinaryOperator::Subtract,
Operator::Mult => bytecode::BinaryOperator::Multiply,
Operator::MatMult => bytecode::BinaryOperator::MatrixMultiply,
Operator::Div => bytecode::BinaryOperator::Divide,
Operator::FloorDiv => bytecode::BinaryOperator::FloorDivide,
Operator::Mod => bytecode::BinaryOperator::Modulo,
Operator::Pow => bytecode::BinaryOperator::Power,
Operator::LShift => bytecode::BinaryOperator::Lshift,
Operator::RShift => bytecode::BinaryOperator::Rshift,
Operator::BitOr => bytecode::BinaryOperator::Or,
Operator::BitXor => bytecode::BinaryOperator::Xor,
Operator::BitAnd => bytecode::BinaryOperator::And,
let bin_op = match op {
Operator::Add => BinaryOperator::Add,
Operator::Sub => BinaryOperator::Subtract,
Operator::Mult => BinaryOperator::Multiply,
Operator::MatMult => BinaryOperator::MatrixMultiply,
Operator::Div => BinaryOperator::TrueDivide,
Operator::FloorDiv => BinaryOperator::FloorDivide,
Operator::Mod => BinaryOperator::Remainder,
Operator::Pow => BinaryOperator::Power,
Operator::LShift => BinaryOperator::Lshift,
Operator::RShift => BinaryOperator::Rshift,
Operator::BitOr => BinaryOperator::Or,
Operator::BitXor => BinaryOperator::Xor,
Operator::BitAnd => BinaryOperator::And,
};
if inplace {
emit!(self, Instruction::BinaryOperationInplace { op })
} else {
emit!(self, Instruction::BinaryOperation { op })
}

let op = if inplace { bin_op.as_inplace() } else { bin_op };
emit!(self, Instruction::BinaryOp { op })
}

/// Implement boolean short circuit evaluation logic.
Expand Down
152 changes: 128 additions & 24 deletions crates/compiler-core/src/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -594,10 +594,7 @@ pub enum Instruction {
UnaryOperation {
op: Arg<UnaryOperator>,
},
BinaryOperation {
op: Arg<BinaryOperator>,
},
BinaryOperationInplace {
BinaryOp {
op: Arg<BinaryOperator>,
},
BinarySubscript,
Expand Down Expand Up @@ -1094,32 +1091,140 @@ op_arg_enum!(

op_arg_enum!(
/// The possible Binary operators
///
/// # Examples
///
/// ```ignore
/// use rustpython_compiler_core::Instruction::BinaryOperation;
/// use rustpython_compiler_core::BinaryOperator::Add;
/// let op = BinaryOperation {op: Add};
/// ```rust
/// use rustpython_compiler_core::bytecode::{Arg, BinaryOperator, Instruction};
/// let (op, _) = Arg::new(BinaryOperator::Add);
/// let instruction = Instruction::BinaryOp { op };
/// ```
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
///
/// See also:
/// - [_PyEval_BinaryOps](https://github.com/python/cpython/blob/8183fa5e3f78ca6ab862de7fb8b14f3d929421e0/Python/ceval.c#L316-L343)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
#[repr(u8)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum BinaryOperator {
Power = 0,
Multiply = 1,
MatrixMultiply = 2,
Divide = 3,
FloorDivide = 4,
Modulo = 5,
Add = 6,
Subtract = 7,
Lshift = 8,
/// `+`
Add = 0,
/// `&`
And = 1,
/// `//`
FloorDivide = 2,
/// `<<`
Lshift = 3,
/// `@`
MatrixMultiply = 4,
/// `*`
Multiply = 5,
/// `%`
Remainder = 6,
/// `|`
Or = 7,
/// `**`
Power = 8,
/// `>>`
Rshift = 9,
And = 10,
Xor = 11,
Or = 12,
/// `-`
Subtract = 10,
/// `/`
TrueDivide = 11,
/// `^`
Xor = 12,
/// `+=`
InplaceAdd = 13,
/// `&=`
InplaceAnd = 14,
/// `//=`
InplaceFloorDivide = 15,
/// `<<=`
InplaceLshift = 16,
/// `@=`
InplaceMatrixMultiply = 17,
/// `*=`
InplaceMultiply = 18,
/// `%=`
InplaceRemainder = 19,
/// `|=`
InplaceOr = 20,
/// `**=`
InplacePower = 21,
/// `>>=`
InplaceRshift = 22,
/// `-=`
InplaceSubtract = 23,
/// `/=`
InplaceTrueDivide = 24,
/// `^=`
InplaceXor = 25,
}
);

impl BinaryOperator {
/// Get the "inplace" version of the operator.
/// This has no effect if `self` is already an "inplace" operator.
///
/// # Example
/// ```rust
/// assert_eq!(BinaryOperator::Power.as_inplace(), BinaryOperator::InplacePower);
///
/// assert_eq!(BinaryOperator::InplaceSubtract.as_inplace(), BinaryOperator::InplaceSubtract);
/// ```
Comment thread
coderabbitai[bot] marked this conversation as resolved.
#[must_use]
pub const fn as_inplace(self) -> Self {
match self {
Self::Add => Self::InplaceAdd,
Self::And => Self::InplaceAnd,
Self::FloorDivide => Self::InplaceFloorDivide,
Self::Lshift => Self::InplaceLshift,
Self::MatrixMultiply => Self::InplaceMatrixMultiply,
Self::Multiply => Self::InplaceMultiply,
Self::Remainder => Self::InplaceRemainder,
Self::Or => Self::InplaceOr,
Self::Power => Self::InplacePower,
Self::Rshift => Self::InplaceRshift,
Self::Subtract => Self::InplaceSubtract,
Self::TrueDivide => Self::InplaceTrueDivide,
Self::Xor => Self::InplaceXor,
_ => self,
}
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}

impl fmt::Display for BinaryOperator {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let op = match self {
Self::Add => "+",
Self::And => "&",
Self::FloorDivide => "//",
Self::Lshift => "<<",
Self::MatrixMultiply => "@",
Self::Multiply => "*",
Self::Remainder => "%",
Self::Or => "|",
Self::Power => "**",
Self::Rshift => ">>",
Self::Subtract => "-",
Self::TrueDivide => "/",
Self::Xor => "^",
Self::InplaceAdd => "+=",
Self::InplaceAnd => "&=",
Self::InplaceFloorDivide => "//=",
Self::InplaceLshift => "<<=",
Self::InplaceMatrixMultiply => "@=",
Self::InplaceMultiply => "*=",
Self::InplaceRemainder => "%=",
Self::InplaceOr => "|=",
Self::InplacePower => "**=",
Self::InplaceRshift => ">>=",
Self::InplaceSubtract => "-=",
Self::InplaceTrueDivide => "/=",
Self::InplaceXor => "^=",
};
write!(f, "{op}")
}
}

op_arg_enum!(
/// The possible unary operators
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -1514,7 +1619,7 @@ impl Instruction {
DeleteAttr { .. } => -1,
LoadConst { .. } => 1,
UnaryOperation { .. } => 0,
BinaryOperation { .. } | BinaryOperationInplace { .. } | CompareOperation { .. } => -1,
BinaryOp { .. } | CompareOperation { .. } => -1,
BinarySubscript => -1,
CopyItem { .. } => 1,
Pop => -1,
Expand Down Expand Up @@ -1719,8 +1824,7 @@ impl Instruction {
DeleteAttr { idx } => w!(DeleteAttr, name = idx),
LoadConst { idx } => fmt_const("LoadConst", arg, f, idx),
UnaryOperation { op } => w!(UnaryOperation, ?op),
BinaryOperation { op } => w!(BinaryOperation, ?op),
BinaryOperationInplace { op } => w!(BinaryOperationInplace, ?op),
BinaryOp { op } => write!(f, "BINARY_OP {}", op.get(arg)),
BinarySubscript => w!(BinarySubscript),
LoadAttr { idx } => w!(LoadAttr, name = idx),
CompareOperation { op } => w!(CompareOperation, ?op),
Expand Down
50 changes: 18 additions & 32 deletions crates/vm/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -868,10 +868,7 @@ impl ExecutingFrame<'_> {
dict.set_item(&*key, value, vm)?;
Ok(None)
}
bytecode::Instruction::BinaryOperation { op } => self.execute_bin_op(vm, op.get(arg)),
bytecode::Instruction::BinaryOperationInplace { op } => {
self.execute_bin_op_inplace(vm, op.get(arg))
}
bytecode::Instruction::BinaryOp { op } => self.execute_bin_op(vm, op.get(arg)),
bytecode::Instruction::BinarySubscript => {
let key = self.pop_value();
let container = self.pop_value();
Expand Down Expand Up @@ -2126,40 +2123,29 @@ impl ExecutingFrame<'_> {
bytecode::BinaryOperator::Multiply => vm._mul(a_ref, b_ref),
bytecode::BinaryOperator::MatrixMultiply => vm._matmul(a_ref, b_ref),
bytecode::BinaryOperator::Power => vm._pow(a_ref, b_ref, vm.ctx.none.as_object()),
bytecode::BinaryOperator::Divide => vm._truediv(a_ref, b_ref),
bytecode::BinaryOperator::TrueDivide => vm._truediv(a_ref, b_ref),
bytecode::BinaryOperator::FloorDivide => vm._floordiv(a_ref, b_ref),
bytecode::BinaryOperator::Modulo => vm._mod(a_ref, b_ref),
bytecode::BinaryOperator::Remainder => vm._mod(a_ref, b_ref),
bytecode::BinaryOperator::Lshift => vm._lshift(a_ref, b_ref),
bytecode::BinaryOperator::Rshift => vm._rshift(a_ref, b_ref),
bytecode::BinaryOperator::Xor => vm._xor(a_ref, b_ref),
bytecode::BinaryOperator::Or => vm._or(a_ref, b_ref),
bytecode::BinaryOperator::And => vm._and(a_ref, b_ref),
}?;

self.push_value(value);
Ok(None)
}
fn execute_bin_op_inplace(
&mut self,
vm: &VirtualMachine,
op: bytecode::BinaryOperator,
) -> FrameResult {
let b_ref = &self.pop_value();
let a_ref = &self.pop_value();
let value = match op {
bytecode::BinaryOperator::Subtract => vm._isub(a_ref, b_ref),
bytecode::BinaryOperator::Add => vm._iadd(a_ref, b_ref),
bytecode::BinaryOperator::Multiply => vm._imul(a_ref, b_ref),
bytecode::BinaryOperator::MatrixMultiply => vm._imatmul(a_ref, b_ref),
bytecode::BinaryOperator::Power => vm._ipow(a_ref, b_ref, vm.ctx.none.as_object()),
bytecode::BinaryOperator::Divide => vm._itruediv(a_ref, b_ref),
bytecode::BinaryOperator::FloorDivide => vm._ifloordiv(a_ref, b_ref),
bytecode::BinaryOperator::Modulo => vm._imod(a_ref, b_ref),
bytecode::BinaryOperator::Lshift => vm._ilshift(a_ref, b_ref),
bytecode::BinaryOperator::Rshift => vm._irshift(a_ref, b_ref),
bytecode::BinaryOperator::Xor => vm._ixor(a_ref, b_ref),
bytecode::BinaryOperator::Or => vm._ior(a_ref, b_ref),
bytecode::BinaryOperator::And => vm._iand(a_ref, b_ref),
bytecode::BinaryOperator::InplaceSubtract => vm._isub(a_ref, b_ref),
bytecode::BinaryOperator::InplaceAdd => vm._iadd(a_ref, b_ref),
bytecode::BinaryOperator::InplaceMultiply => vm._imul(a_ref, b_ref),
bytecode::BinaryOperator::InplaceMatrixMultiply => vm._imatmul(a_ref, b_ref),
bytecode::BinaryOperator::InplacePower => {
vm._ipow(a_ref, b_ref, vm.ctx.none.as_object())
}
bytecode::BinaryOperator::InplaceTrueDivide => vm._itruediv(a_ref, b_ref),
bytecode::BinaryOperator::InplaceFloorDivide => vm._ifloordiv(a_ref, b_ref),
bytecode::BinaryOperator::InplaceRemainder => vm._imod(a_ref, b_ref),
bytecode::BinaryOperator::InplaceLshift => vm._ilshift(a_ref, b_ref),
bytecode::BinaryOperator::InplaceRshift => vm._irshift(a_ref, b_ref),
bytecode::BinaryOperator::InplaceXor => vm._ixor(a_ref, b_ref),
bytecode::BinaryOperator::InplaceOr => vm._ior(a_ref, b_ref),
bytecode::BinaryOperator::InplaceAnd => vm._iand(a_ref, b_ref),
}?;

self.push_value(value);
Expand Down
Loading