Skip to content

Commit eece3bf

Browse files
committed
Make use of Unary opcodes
1 parent 6cc103b commit eece3bf

File tree

3 files changed

+41
-59
lines changed

3 files changed

+41
-59
lines changed

crates/codegen/src/compile.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5656,13 +5656,20 @@ impl Compiler {
56565656
self.compile_expression(operand)?;
56575657

56585658
// Perform operation:
5659-
let op = match op {
5660-
UnaryOp::UAdd => bytecode::UnaryOperator::Plus,
5661-
UnaryOp::USub => bytecode::UnaryOperator::Minus,
5662-
UnaryOp::Not => bytecode::UnaryOperator::Not,
5663-
UnaryOp::Invert => bytecode::UnaryOperator::Invert,
5659+
match op {
5660+
UnaryOp::UAdd => emit!(
5661+
self,
5662+
Instruction::CallIntrinsic1 {
5663+
func: bytecode::IntrinsicFunction1::UnaryPositive
5664+
}
5665+
),
5666+
UnaryOp::USub => emit!(self, Instruction::UnaryNegative),
5667+
UnaryOp::Not => {
5668+
emit!(self, Instruction::ToBool);
5669+
emit!(self, Instruction::UnaryNot);
5670+
}
5671+
UnaryOp::Invert => emit!(self, Instruction::UnaryInvert),
56645672
};
5665-
emit!(self, Instruction::UnaryOperation { op });
56665673
}
56675674
Expr::Attribute(ExprAttribute { value, attr, .. }) => {
56685675
self.compile_expression(value)?;

crates/compiler-core/src/bytecode.rs

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ op_arg_enum!(
640640
ImportStar = 2,
641641
// StopIterationError = 3,
642642
// AsyncGenWrap = 4,
643-
// UnaryPositive = 5,
643+
UnaryPositive = 5,
644644
/// Convert list to tuple
645645
ListToTuple = 6,
646646
/// Type parameter related
@@ -758,11 +758,11 @@ pub enum Instruction {
758758
StoreSubscript,
759759
// 40: TO_BOOL
760760
ToBool,
761-
// 41: UNARY_INVERT - placeholder (RustPython uses UnaryOperation)
761+
// 41: UNARY_INVERT
762762
UnaryInvert,
763-
// 42: UNARY_NEGATIVE - placeholder
763+
// 42: UNARY_NEGATIVE
764764
UnaryNegative,
765-
// 43: UNARY_NOT - placeholder
765+
// 43: UNARY_NOT
766766
UnaryNot,
767767
// ==================== With-argument instructions (opcode >= 44) ====================
768768
// 44: WITH_EXCEPT_START
@@ -1091,10 +1091,6 @@ pub enum Instruction {
10911091
SetExcInfo,
10921092
// 139: SUBSCRIPT
10931093
Subscript,
1094-
// 140: UNARY_OP (combines UNARY_*)
1095-
UnaryOperation {
1096-
op: Arg<UnaryOperator>,
1097-
},
10981094
// 141-148: Reserved (padding to keep RESUME at 149)
10991095
Reserved141,
11001096
Reserved142,
@@ -1538,18 +1534,6 @@ impl fmt::Display for BinaryOperator {
15381534
}
15391535
}
15401536

1541-
op_arg_enum!(
1542-
/// The possible unary operators
1543-
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
1544-
#[repr(u8)]
1545-
pub enum UnaryOperator {
1546-
Not = 0,
1547-
Invert = 1,
1548-
Minus = 2,
1549-
Plus = 3,
1550-
}
1551-
);
1552-
15531537
op_arg_enum!(
15541538
/// Whether or not to invert the operation.
15551539
#[repr(u8)]
@@ -1910,13 +1894,10 @@ impl Instruction {
19101894
/// # Examples
19111895
///
19121896
/// ```
1913-
/// use rustpython_compiler_core::bytecode::{Arg, Instruction, Label, UnaryOperator};
1897+
/// use rustpython_compiler_core::bytecode::{Arg, Instruction, Label};
19141898
/// let (target, jump_arg) = Arg::new(Label(0xF));
19151899
/// let jump_instruction = Instruction::Jump { target };
1916-
/// let (op, invert_arg) = Arg::new(UnaryOperator::Invert);
1917-
/// let invert_instruction = Instruction::UnaryOperation { op };
19181900
/// assert_eq!(jump_instruction.stack_effect(jump_arg, true), 0);
1919-
/// assert_eq!(invert_instruction.stack_effect(invert_arg, false), 0);
19201901
/// ```
19211902
///
19221903
pub fn stack_effect(&self, arg: OpArg, jump: bool) -> i32 {
@@ -1925,8 +1906,7 @@ impl Instruction {
19251906
Cache | Reserved3 | Reserved17 | Reserved141 | Reserved142 | Reserved143
19261907
| Reserved144 | Reserved145 | Reserved146 | Reserved147 | Reserved148 => 0,
19271908
BinarySlice | EndFor | ExitInitCheck | GetYieldFromIter | InterpreterExit
1928-
| LoadAssertionError | LoadLocals | PushNull | ReturnGenerator | StoreSlice
1929-
| UnaryInvert | UnaryNegative | UnaryNot => 0,
1909+
| LoadAssertionError | LoadLocals | PushNull | ReturnGenerator | StoreSlice => 0,
19301910
BuildConstKeyMap { .. }
19311911
| CopyFreeVars { .. }
19321912
| DictMerge { .. }
@@ -1959,7 +1939,6 @@ impl Instruction {
19591939
StoreAttr { .. } => -2,
19601940
DeleteAttr { .. } => -1,
19611941
LoadConst { .. } => 1,
1962-
UnaryOperation { .. } => 0,
19631942
BinaryOp { .. } | CompareOperation { .. } => -1,
19641943
BinarySubscript => -1,
19651944
CopyItem { .. } => 1,
@@ -2086,6 +2065,9 @@ impl Instruction {
20862065
MatchKeys => 1, // Pop 2 (subject, keys), push 3 (subject, keys_or_none, values_or_none)
20872066
MatchClass(_) => -2,
20882067
ExtendedArg => 0,
2068+
UnaryInvert => 0,
2069+
UnaryNegative => 0,
2070+
UnaryNot => 0,
20892071
}
20902072
}
20912073

@@ -2302,7 +2284,6 @@ impl Instruction {
23022284
Subscript => w!(SUBSCRIPT),
23032285
Swap { index } => w!(SWAP, index),
23042286
ToBool => w!(TO_BOOL),
2305-
UnaryOperation { op } => w!(UNARY_OP, ?op),
23062287
UnpackEx { args } => w!(UNPACK_EX, args),
23072288
UnpackSequence { size } => w!(UNPACK_SEQUENCE, size),
23082289
WithExceptStart => w!(WITH_EXCEPT_START),

crates/vm/src/frame.rs

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1652,7 +1652,6 @@ impl ExecutingFrame<'_> {
16521652
self.push_value(vm.ctx.new_bool(bool_val).into());
16531653
Ok(None)
16541654
}
1655-
bytecode::Instruction::UnaryOperation { op } => self.execute_unary_op(vm, op.get(arg)),
16561655
bytecode::Instruction::UnpackEx { args } => {
16571656
let args = args.get(arg);
16581657
self.execute_unpack_ex(vm, args.before, args.after)
@@ -1762,7 +1761,24 @@ impl ExecutingFrame<'_> {
17621761
.map_err(|_| vm.new_type_error("exception expected".to_owned()))?;
17631762
Err(exc)
17641763
}
1765-
1764+
bytecode::Instruction::UnaryInvert => {
1765+
let a = self.pop_value();
1766+
let value = vm._invert(&a)?;
1767+
self.push_value(value);
1768+
Ok(None)
1769+
}
1770+
bytecode::Instruction::UnaryNegative => {
1771+
let a = self.pop_value();
1772+
let value = vm._neg(&a)?;
1773+
self.push_value(value);
1774+
Ok(None)
1775+
}
1776+
bytecode::Instruction::UnaryNot => {
1777+
let obj = self.pop_value();
1778+
let value = obj.try_to_bool(vm)?;
1779+
self.push_value(vm.ctx.new_bool(!value).into());
1780+
Ok(None)
1781+
}
17661782
// Placeholder/dummy instructions - these should never be executed
17671783
bytecode::Instruction::Cache
17681784
| bytecode::Instruction::Reserved3
@@ -1776,9 +1792,6 @@ impl ExecutingFrame<'_> {
17761792
| bytecode::Instruction::PushNull
17771793
| bytecode::Instruction::ReturnGenerator
17781794
| bytecode::Instruction::StoreSlice
1779-
| bytecode::Instruction::UnaryInvert
1780-
| bytecode::Instruction::UnaryNegative
1781-
| bytecode::Instruction::UnaryNot
17821795
| bytecode::Instruction::BuildConstKeyMap { .. }
17831796
| bytecode::Instruction::CopyFreeVars { .. }
17841797
| bytecode::Instruction::DictMerge { .. }
@@ -2440,26 +2453,6 @@ impl ExecutingFrame<'_> {
24402453
Ok(None)
24412454
}
24422455

2443-
#[cfg_attr(feature = "flame-it", flame("Frame"))]
2444-
fn execute_unary_op(
2445-
&mut self,
2446-
vm: &VirtualMachine,
2447-
op: bytecode::UnaryOperator,
2448-
) -> FrameResult {
2449-
let a = self.pop_value();
2450-
let value = match op {
2451-
bytecode::UnaryOperator::Minus => vm._neg(&a)?,
2452-
bytecode::UnaryOperator::Plus => vm._pos(&a)?,
2453-
bytecode::UnaryOperator::Invert => vm._invert(&a)?,
2454-
bytecode::UnaryOperator::Not => {
2455-
let value = a.try_to_bool(vm)?;
2456-
vm.ctx.new_bool(!value).into()
2457-
}
2458-
};
2459-
self.push_value(value);
2460-
Ok(None)
2461-
}
2462-
24632456
#[cold]
24642457
fn setup_annotations(&mut self, vm: &VirtualMachine) -> FrameResult {
24652458
let __annotations__ = identifier!(vm, __annotations__);
@@ -2634,6 +2627,7 @@ impl ExecutingFrame<'_> {
26342627
self.import_star(vm)?;
26352628
Ok(vm.ctx.none())
26362629
}
2630+
bytecode::IntrinsicFunction1::UnaryPositive => vm._pos(&arg),
26372631
bytecode::IntrinsicFunction1::SubscriptGeneric => {
26382632
// Used for PEP 695: Generic[*type_params]
26392633
crate::builtins::genericalias::subscript_generic(arg, vm)

0 commit comments

Comments
 (0)