Newtype oparg for StoreFastLoadFast (#7191)

This commit is contained in:
Shahar Naveh authored and GitHub committed 2026-02-20 08:19:12 +09:00
1 parent 550cc44fbd
commit 1ac52b9187
3 files changed
+52 -14

No files matched your search

@@ -7,7 +7,7 @@ use crate::{
BinaryOperator, BuildSliceArgCount, CommonConstant, ComparisonOperator,
ConvertValueOparg, IntrinsicFunction1, IntrinsicFunction2, Invert, Label, LoadAttr,
LoadSuperAttr, MakeFunctionFlags, NameIdx, OpArg, OpArgByte, OpArgType, RaiseKind,
SpecialMethod, UnpackExArgs,
SpecialMethod, StoreFastLoadFast, UnpackExArgs,
},
},
marshal::MarshalError,
@@ -241,8 +241,7 @@ pub enum Instruction {
StoreDeref(Arg<NameIdx>) = 111,
StoreFast(Arg<NameIdx>) = 112,
StoreFastLoadFast {
store_idx: Arg<NameIdx>,
load_idx: Arg<NameIdx>,
var_nums: Arg<StoreFastLoadFast>,
} = 113,
StoreFastStoreFast {
arg: Arg<u32>,
@@ -901,12 +900,12 @@ impl InstructionMetadata for Instruction {
Self::StoreAttr { idx } => w!(STORE_ATTR, name = idx),
Self::StoreDeref(idx) => w!(STORE_DEREF, cell_name = idx),
Self::StoreFast(idx) => w!(STORE_FAST, varname = idx),
Self::StoreFastLoadFast {
store_idx,
load_idx,
} => {
Self::StoreFastLoadFast { var_nums } => {
let oparg = var_nums.get(arg);
let store_idx = oparg.store_idx();
let load_idx = oparg.load_idx();
write!(f, "STORE_FAST_LOAD_FAST")?;
write!(f, " ({}, {})", store_idx.get(arg), load_idx.get(arg))
write!(f, " ({}, {})", store_idx, load_idx)
}
Self::StoreGlobal(idx) => w!(STORE_GLOBAL, name = idx),
Self::StoreName(idx) => w!(STORE_NAME, name = idx),
@@ -323,6 +323,47 @@ impl fmt::Display for Label {
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
#[repr(transparent)]
pub struct StoreFastLoadFast(u32);
impl StoreFastLoadFast {
#[must_use]
pub const fn new(value: u32) -> Self {
Self(value)
}
#[must_use]
pub const fn store_idx(self) -> NameIdx {
self.0 >> 4
}
#[must_use]
pub const fn load_idx(self) -> NameIdx {
self.0 & 15
}
}
impl From<u32> for StoreFastLoadFast {
fn from(value: u32) -> Self {
Self::new(value)
}
}
impl From<StoreFastLoadFast> for u32 {
fn from(value: StoreFastLoadFast) -> Self {
value.0
}
}
impl OpArgType for StoreFastLoadFast {}
impl fmt::Display for StoreFastLoadFast {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
oparg_enum!(
/// The kind of Raise that occurred.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
+4 -6
View File
@@ -2091,15 +2091,13 @@ impl ExecutingFrame<'_> {
self.fastlocals.lock()[idx.get(arg) as usize] = Some(value);
Ok(None)
}
Instruction::StoreFastLoadFast {
store_idx,
load_idx,
} => {
Instruction::StoreFastLoadFast { var_nums } => {
// Store to one slot and load from another (often the same) - for inlined comprehensions
let value = self.pop_value();
let mut locals = self.fastlocals.lock();
locals[store_idx.get(arg) as usize] = Some(value);
let load_value = locals[load_idx.get(arg) as usize]
let oparg = var_nums.get(arg);
locals[oparg.store_idx() as usize] = Some(value);
let load_value = locals[oparg.load_idx() as usize]
.clone()
.expect("StoreFastLoadFast: load slot should have value after store");
drop(locals);