Skip to content

Commit eddb8f2

Browse files
authored
Newtype oparg for StoreFastLoadFast (RustPython#7191)
1 parent 41ea698 commit eddb8f2

3 files changed

Lines changed: 52 additions & 14 deletions

File tree

crates/compiler-core/src/bytecode/instruction.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::{
77
BinaryOperator, BuildSliceArgCount, CommonConstant, ComparisonOperator,
88
ConvertValueOparg, IntrinsicFunction1, IntrinsicFunction2, Invert, Label, LoadAttr,
99
LoadSuperAttr, MakeFunctionFlags, NameIdx, OpArg, OpArgByte, OpArgType, RaiseKind,
10-
SpecialMethod, UnpackExArgs,
10+
SpecialMethod, StoreFastLoadFast, UnpackExArgs,
1111
},
1212
},
1313
marshal::MarshalError,
@@ -241,8 +241,7 @@ pub enum Instruction {
241241
StoreDeref(Arg<NameIdx>) = 111,
242242
StoreFast(Arg<NameIdx>) = 112,
243243
StoreFastLoadFast {
244-
store_idx: Arg<NameIdx>,
245-
load_idx: Arg<NameIdx>,
244+
var_nums: Arg<StoreFastLoadFast>,
246245
} = 113,
247246
StoreFastStoreFast {
248247
arg: Arg<u32>,
@@ -901,12 +900,12 @@ impl InstructionMetadata for Instruction {
901900
Self::StoreAttr { idx } => w!(STORE_ATTR, name = idx),
902901
Self::StoreDeref(idx) => w!(STORE_DEREF, cell_name = idx),
903902
Self::StoreFast(idx) => w!(STORE_FAST, varname = idx),
904-
Self::StoreFastLoadFast {
905-
store_idx,
906-
load_idx,
907-
} => {
903+
Self::StoreFastLoadFast { var_nums } => {
904+
let oparg = var_nums.get(arg);
905+
let store_idx = oparg.store_idx();
906+
let load_idx = oparg.load_idx();
908907
write!(f, "STORE_FAST_LOAD_FAST")?;
909-
write!(f, " ({}, {})", store_idx.get(arg), load_idx.get(arg))
908+
write!(f, " ({}, {})", store_idx, load_idx)
910909
}
911910
Self::StoreGlobal(idx) => w!(STORE_GLOBAL, name = idx),
912911
Self::StoreName(idx) => w!(STORE_NAME, name = idx),

crates/compiler-core/src/bytecode/oparg.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,47 @@ impl fmt::Display for Label {
323323
}
324324
}
325325

326+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
327+
#[repr(transparent)]
328+
pub struct StoreFastLoadFast(u32);
329+
330+
impl StoreFastLoadFast {
331+
#[must_use]
332+
pub const fn new(value: u32) -> Self {
333+
Self(value)
334+
}
335+
336+
#[must_use]
337+
pub const fn store_idx(self) -> NameIdx {
338+
self.0 >> 4
339+
}
340+
341+
#[must_use]
342+
pub const fn load_idx(self) -> NameIdx {
343+
self.0 & 15
344+
}
345+
}
346+
347+
impl From<u32> for StoreFastLoadFast {
348+
fn from(value: u32) -> Self {
349+
Self::new(value)
350+
}
351+
}
352+
353+
impl From<StoreFastLoadFast> for u32 {
354+
fn from(value: StoreFastLoadFast) -> Self {
355+
value.0
356+
}
357+
}
358+
359+
impl OpArgType for StoreFastLoadFast {}
360+
361+
impl fmt::Display for StoreFastLoadFast {
362+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
363+
self.0.fmt(f)
364+
}
365+
}
366+
326367
oparg_enum!(
327368
/// The kind of Raise that occurred.
328369
#[derive(Copy, Clone, Debug, PartialEq, Eq)]

crates/vm/src/frame.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2091,15 +2091,13 @@ impl ExecutingFrame<'_> {
20912091
self.fastlocals.lock()[idx.get(arg) as usize] = Some(value);
20922092
Ok(None)
20932093
}
2094-
Instruction::StoreFastLoadFast {
2095-
store_idx,
2096-
load_idx,
2097-
} => {
2094+
Instruction::StoreFastLoadFast { var_nums } => {
20982095
// Store to one slot and load from another (often the same) - for inlined comprehensions
20992096
let value = self.pop_value();
21002097
let mut locals = self.fastlocals.lock();
2101-
locals[store_idx.get(arg) as usize] = Some(value);
2102-
let load_value = locals[load_idx.get(arg) as usize]
2098+
let oparg = var_nums.get(arg);
2099+
locals[oparg.store_idx() as usize] = Some(value);
2100+
let load_value = locals[oparg.load_idx() as usize]
21032101
.clone()
21042102
.expect("StoreFastLoadFast: load slot should have value after store");
21052103
drop(locals);

0 commit comments

Comments
 (0)