diff --git a/crates/compiler-core/src/bytecode/oparg.rs b/crates/compiler-core/src/bytecode/oparg.rs index 27330ed46cd..d3d54750797 100644 --- a/crates/compiler-core/src/bytecode/oparg.rs +++ b/crates/compiler-core/src/bytecode/oparg.rs @@ -242,6 +242,11 @@ macro_rules! impl_oparg_enum { // We already validated this is a lossles cast. Self::try_from_u8(value as u8) } + + /// Iterate over the variants. + $vis fn iter() -> impl Iterator { + [$(Self::$variant),*].iter().copied() + } } impl TryFrom for $name { @@ -366,7 +371,7 @@ oparg_enum!( /// Intrinsic function for CALL_INTRINSIC_1 #[derive(Copy, Clone, Debug, PartialEq, Eq)] pub enum IntrinsicFunction1 { - // Invalid = 0, + Invalid = 0, Print = 1, /// Import * operation ImportStar = 2, @@ -386,10 +391,32 @@ oparg_enum!( } ); +impl IntrinsicFunction1 { + /// https://github.com/python/cpython/blob/v3.14.4/Include/internal/pycore_intrinsics.h#L9-L20 + #[must_use] + pub const fn desc(&self) -> &str { + match self { + Self::Invalid => "INTRINSIC_1_INVALID", + Self::Print => "INTRINSIC_PRINT", + Self::ImportStar => "INTRINSIC_IMPORT_STAR", + Self::StopIterationError => "INTRINSIC_STOPITERATION_ERROR", + Self::AsyncGenWrap => "INTRINSIC_ASYNC_GEN_WRAP", + Self::UnaryPositive => "INTRINSIC_UNARY_POSITIVE", + Self::ListToTuple => "INTRINSIC_LIST_TO_TUPLE", + Self::TypeVar => "INTRINSIC_TYPEVAR", + Self::ParamSpec => "INTRINSIC_PARAMSPEC", + Self::TypeVarTuple => "INTRINSIC_TYPEVARTUPLE", + Self::SubscriptGeneric => "INTRINSIC_SUBSCRIPT_GENERIC", + Self::TypeAlias => "INTRINSIC_TYPEALIAS", + } + } +} + oparg_enum!( /// Intrinsic function for CALL_INTRINSIC_2 #[derive(Copy, Clone, Debug, PartialEq, Eq)] pub enum IntrinsicFunction2 { + Invalid = 0, PrepReraiseStar = 1, TypeVarWithBound = 2, TypeVarWithConstraint = 3, @@ -399,6 +426,21 @@ oparg_enum!( } ); +impl IntrinsicFunction2 { + /// https://github.com/python/cpython/blob/v3.14.4/Include/internal/pycore_intrinsics.h#L26-L31 + #[must_use] + pub const fn desc(&self) -> &str { + match self { + Self::Invalid => "INTRINSIC_2_INVALID", + Self::PrepReraiseStar => "INTRINSIC_PREP_RERAISE_STAR", + Self::TypeVarWithBound => "INTRINSIC_TYPEVAR_WITH_BOUND", + Self::TypeVarWithConstraint => "INTRINSIC_TYPEVAR_WITH_CONSTRAINTS", + Self::SetFunctionTypeParams => "INTRINSIC_SET_FUNCTION_TYPE_PARAMS", + Self::SetTypeparamDefault => "INTRINSIC_SET_TYPEPARAM_DEFAULT", + } + } +} + bitflagset::bitflag! { /// `SET_FUNCTION_ATTRIBUTE` flags. /// Bitmask: Defaults=0x01, KwOnly=0x02, Annotations=0x04, @@ -626,6 +668,40 @@ impl BinaryOperator { _ => self, } } + + /// https://github.com/python/cpython/blob/v3.14.4/Include/opcode.h#L10-L36 + #[must_use] + pub const fn desc(&self) -> &str { + match self { + Self::Add => "NB_ADD", + Self::And => "NB_AND", + Self::FloorDivide => "NB_FLOOR_DIVIDE", + Self::Lshift => "NB_LSHIFT", + Self::MatrixMultiply => "NB_MATRIX_MULTIPLY", + Self::Multiply => "NB_MULTIPLY", + Self::Remainder => "NB_REMAINDER", + Self::Or => "NB_OR", + Self::Power => "NB_POWER", + Self::Rshift => "NB_RSHIFT", + Self::Subtract => "NB_SUBTRACT", + Self::TrueDivide => "NB_TRUE_DIVIDE", + Self::Xor => "NB_XOR", + Self::InplaceAdd => "NB_INPLACE_ADD", + Self::InplaceAnd => "NB_INPLACE_AND", + Self::InplaceFloorDivide => "NB_INPLACE_FLOOR_DIVIDE", + Self::InplaceLshift => "NB_INPLACE_LSHIFT", + Self::InplaceMatrixMultiply => "NB_INPLACE_MATRIX_MULTIPLY", + Self::InplaceMultiply => "NB_INPLACE_MULTIPLY", + Self::InplaceRemainder => "NB_INPLACE_REMAINDER", + Self::InplaceOr => "NB_INPLACE_OR", + Self::InplacePower => "NB_INPLACE_POWER", + Self::InplaceRshift => "NB_INPLACE_RSHIFT", + Self::InplaceSubtract => "NB_INPLACE_SUBTRACT", + Self::InplaceTrueDivide => "NB_INPLACE_TRUE_DIVIDE", + Self::InplaceXor => "NB_INPLACE_XOR", + Self::Subscr => "NB_SUBSCR", + } + } } oparg_enum!( diff --git a/crates/stdlib/src/_opcode.rs b/crates/stdlib/src/_opcode.rs index 6c19a29620a..ca857af7f19 100644 --- a/crates/stdlib/src/_opcode.rs +++ b/crates/stdlib/src/_opcode.rs @@ -5,7 +5,7 @@ mod _opcode { use crate::vm::{ AsObject, PyObjectRef, PyResult, VirtualMachine, builtins::{PyInt, PyIntRef}, - bytecode::{AnyInstruction, AnyOpcode, InstructionMetadata, Opcode, PseudoOpcode}, + bytecode::{AnyInstruction, AnyOpcode, InstructionMetadata, Opcode, PseudoOpcode, oparg}, }; fn try_from_i32(raw: i32) -> Result { @@ -177,85 +177,36 @@ mod _opcode { #[pyfunction] fn get_intrinsic1_descs(vm: &VirtualMachine) -> Vec { - [ - "INTRINSIC_1_INVALID", - "INTRINSIC_PRINT", - "INTRINSIC_IMPORT_STAR", - "INTRINSIC_STOPITERATION_ERROR", - "INTRINSIC_ASYNC_GEN_WRAP", - "INTRINSIC_UNARY_POSITIVE", - "INTRINSIC_LIST_TO_TUPLE", - "INTRINSIC_TYPEVAR", - "INTRINSIC_PARAMSPEC", - "INTRINSIC_TYPEVARTUPLE", - "INTRINSIC_SUBSCRIPT_GENERIC", - "INTRINSIC_TYPEALIAS", - ] - .into_iter() - .map(|x| vm.ctx.new_str(x).into()) - .collect() + oparg::IntrinsicFunction1::iter() + .map(|x| vm.ctx.new_str(x.desc()).into()) + .collect() } #[pyfunction] fn get_intrinsic2_descs(vm: &VirtualMachine) -> Vec { - [ - "INTRINSIC_2_INVALID", - "INTRINSIC_PREP_RERAISE_STAR", - "INTRINSIC_TYPEVAR_WITH_BOUND", - "INTRINSIC_TYPEVAR_WITH_CONSTRAINTS", - "INTRINSIC_SET_FUNCTION_TYPE_PARAMS", - "INTRINSIC_SET_TYPEPARAM_DEFAULT", - ] - .into_iter() - .map(|x| vm.ctx.new_str(x).into()) - .collect() + oparg::IntrinsicFunction2::iter() + .map(|x| vm.ctx.new_str(x.desc()).into()) + .collect() } #[pyfunction] fn get_nb_ops(vm: &VirtualMachine) -> Vec { - [ - ("NB_ADD", "+"), - ("NB_AND", "&"), - ("NB_FLOOR_DIVIDE", "//"), - ("NB_LSHIFT", "<<"), - ("NB_MATRIX_MULTIPLY", "@"), - ("NB_MULTIPLY", "*"), - ("NB_REMAINDER", "%"), - ("NB_OR", "|"), - ("NB_POWER", "**"), - ("NB_RSHIFT", ">>"), - ("NB_SUBTRACT", "-"), - ("NB_TRUE_DIVIDE", "/"), - ("NB_XOR", "^"), - ("NB_INPLACE_ADD", "+="), - ("NB_INPLACE_AND", "&="), - ("NB_INPLACE_FLOOR_DIVIDE", "//="), - ("NB_INPLACE_LSHIFT", "<<="), - ("NB_INPLACE_MATRIX_MULTIPLY", "@="), - ("NB_INPLACE_MULTIPLY", "*="), - ("NB_INPLACE_REMAINDER", "%="), - ("NB_INPLACE_OR", "|="), - ("NB_INPLACE_POWER", "**="), - ("NB_INPLACE_RSHIFT", ">>="), - ("NB_INPLACE_SUBTRACT", "-="), - ("NB_INPLACE_TRUE_DIVIDE", "/="), - ("NB_INPLACE_XOR", "^="), - ("NB_SUBSCR", "[]"), - ] - .into_iter() - .map(|(a, b)| { - vm.ctx - .new_tuple(vec![vm.ctx.new_str(a).into(), vm.ctx.new_str(b).into()]) - .into() - }) - .collect() + oparg::BinaryOperator::iter() + .map(|x| { + vm.ctx + .new_tuple(vec![ + vm.ctx.new_str(x.desc()).into(), + vm.ctx.new_str(x.to_string()).into(), + ]) + .into() + }) + .collect() } #[pyfunction] fn get_special_method_names(vm: &VirtualMachine) -> Vec { - ["__enter__", "__exit__", "__aenter__", "__aexit__"] - .into_iter() - .map(|x| vm.ctx.new_str(x).into()) + oparg::SpecialMethod::iter() + .map(|x| vm.ctx.new_str(x.to_string()).into()) .collect() } diff --git a/crates/vm/src/frame.rs b/crates/vm/src/frame.rs index 93783b1e5cc..739de7a054f 100644 --- a/crates/vm/src/frame.rs +++ b/crates/vm/src/frame.rs @@ -9458,6 +9458,9 @@ impl ExecutingFrame<'_> { vm: &VirtualMachine, ) -> PyResult { match func { + bytecode::IntrinsicFunction1::Invalid => { + unreachable!("This is a bug in RustPython compiler") + } bytecode::IntrinsicFunction1::Print => { let displayhook = vm .sys_module @@ -9573,6 +9576,9 @@ impl ExecutingFrame<'_> { vm: &VirtualMachine, ) -> PyResult { match func { + bytecode::IntrinsicFunction2::Invalid => { + unreachable!("This is a bug in RustPython compiler") + } bytecode::IntrinsicFunction2::SetTypeparamDefault => { crate::stdlib::_typing::set_typeparam_default(arg1, arg2, vm) }