Skip to content

Commit 5cfd018

Browse files
committed
impl argc method
1 parent 5573159 commit 5cfd018

File tree

1 file changed

+42
-16
lines changed

1 file changed

+42
-16
lines changed

crates/compiler-core/src/bytecode.rs

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use itertools::Itertools;
1010
use malachite_bigint::BigInt;
1111
use num_complex::Complex64;
1212
use rustpython_wtf8::{Wtf8, Wtf8Buf};
13-
use std::{collections::BTreeSet, fmt, hash, marker::PhantomData, mem, ops::Deref};
13+
use std::{collections::BTreeSet, fmt, hash, marker::PhantomData, mem, num::NonZeroU8, ops::Deref};
1414

1515
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
1616
#[repr(i8)]
@@ -1150,21 +1150,47 @@ op_arg_enum!(
11501150
}
11511151
);
11521152

1153-
op_arg_enum!(
1154-
/// Specifies if a slice is built with either 2 or 3 arguments.
1155-
#[repr(u8)]
1156-
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
1157-
pub enum BuildSliceArgCount {
1158-
/// ```py
1159-
/// x[5:10]
1160-
/// ```
1161-
Two = 2,
1162-
/// ```py
1163-
/// x[5:10:2]
1164-
/// ```
1165-
Three = 3,
1153+
/// Specifies if a slice is built with either 2 or 3 arguments.
1154+
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
1155+
pub enum BuildSliceArgCount {
1156+
/// ```py
1157+
/// x[5:10]
1158+
/// ```
1159+
Two,
1160+
/// ```py
1161+
/// x[5:10:2]
1162+
/// ```
1163+
Three,
1164+
}
1165+
1166+
impl OpArgType for BuildSliceArgCount {
1167+
#[inline(always)]
1168+
fn from_op_arg(x: u32) -> Option<Self> {
1169+
Some(match x {
1170+
2 => Self::Two,
1171+
3 => Self::Three,
1172+
_ => return None,
1173+
})
11661174
}
1167-
);
1175+
1176+
#[inline(always)]
1177+
fn to_op_arg(self) -> u32 {
1178+
u32::from(self.argc().get())
1179+
}
1180+
}
1181+
1182+
impl BuildSliceArgCount {
1183+
/// Get the numeric value of `Self`.
1184+
#[must_use]
1185+
pub const fn argc(self) -> NonZeroU8 {
1186+
let inner = match self {
1187+
Self::Two => 2,
1188+
Self::Three => 3,
1189+
};
1190+
// Safety: `inner` can be either 2 or 3.
1191+
unsafe { NonZeroU8::new_unchecked(inner) }
1192+
}
1193+
}
11681194

11691195
#[derive(Copy, Clone)]
11701196
pub struct UnpackExArgs {
@@ -1565,7 +1591,7 @@ impl Instruction {
15651591
BuildSlice { argc } => {
15661592
// push 1
15671593
// pops either 2/3
1568-
1 - (argc.get(arg) as i32)
1594+
1 - (argc.get(arg).argc().get() as i32)
15691595
}
15701596
ListAppend { .. } | SetAdd { .. } => -1,
15711597
MapAdd { .. } => -2,

0 commit comments

Comments
 (0)