Skip to content

Commit ec7b586

Browse files
committed
Address review comments
1 parent 6d1f298 commit ec7b586

File tree

3 files changed

+134
-200
lines changed

3 files changed

+134
-200
lines changed

compiler/core/src/bytecode.rs

Lines changed: 113 additions & 181 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,27 @@ impl OpArgType for bool {
218218
}
219219
}
220220

221+
macro_rules! oparg_enum {
222+
($(#[$attr:meta])* $vis:vis enum $name:ident { $($(#[$var_attr:meta])* $var:ident = $discr:literal,)* }) => {
223+
$(#[$attr])*
224+
$vis enum $name {
225+
$($(#[$var_attr])* $var = $discr,)*
226+
}
227+
228+
impl OpArgType for $name {
229+
fn to_oparg(self) -> u32 {
230+
self as u32
231+
}
232+
fn from_oparg(x: u32) -> Option<Self> {
233+
Some(match u8::try_from(x).ok()? {
234+
$($discr => Self::$var,)*
235+
_ => return None,
236+
})
237+
}
238+
}
239+
};
240+
}
241+
221242
#[derive(Copy, Clone)]
222243
pub struct Arg<T: OpArgType>(PhantomData<T>);
223244

@@ -292,34 +313,21 @@ impl fmt::Display for Label {
292313
}
293314
}
294315

295-
/// Transforms a value prior to formatting it.
296-
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
297-
#[repr(u8)]
298-
pub enum ConversionFlag {
299-
/// No conversion
300-
None = 0, // CPython uses -1 but not pleasure for us
301-
/// Converts by calling `str(<value>)`.
302-
Str = b's',
303-
/// Converts by calling `ascii(<value>)`.
304-
Ascii = b'a',
305-
/// Converts by calling `repr(<value>)`.
306-
Repr = b'r',
307-
}
308-
309-
impl OpArgType for ConversionFlag {
310-
fn to_oparg(self) -> u32 {
311-
self as u32
312-
}
313-
fn from_oparg(x: u32) -> Option<Self> {
314-
Some(match u8::try_from(x).ok()? {
315-
0 => Self::None,
316-
b's' => Self::Str,
317-
b'a' => Self::Ascii,
318-
b'r' => Self::Repr,
319-
_ => return None,
320-
})
321-
}
322-
}
316+
oparg_enum!(
317+
/// Transforms a value prior to formatting it.
318+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
319+
#[repr(u8)]
320+
pub enum ConversionFlag {
321+
/// No conversion
322+
None = 0, // CPython uses -1 but not pleasure for us
323+
/// Converts by calling `str(<value>)`.
324+
Str = b's',
325+
/// Converts by calling `ascii(<value>)`.
326+
Ascii = b'a',
327+
/// Converts by calling `repr(<value>)`.
328+
Repr = b'r',
329+
}
330+
);
323331

324332
impl TryFrom<usize> for ConversionFlag {
325333
type Error = usize;
@@ -328,28 +336,16 @@ impl TryFrom<usize> for ConversionFlag {
328336
}
329337
}
330338

331-
/// The kind of Raise that occurred.
332-
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
333-
#[repr(u8)]
334-
pub enum RaiseKind {
335-
Reraise,
336-
Raise,
337-
RaiseCause,
338-
}
339-
340-
impl OpArgType for RaiseKind {
341-
fn to_oparg(self) -> u32 {
342-
self as u32
339+
oparg_enum!(
340+
/// The kind of Raise that occurred.
341+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
342+
#[repr(u8)]
343+
pub enum RaiseKind {
344+
Reraise = 0,
345+
Raise = 1,
346+
RaiseCause = 2,
343347
}
344-
fn from_oparg(x: u32) -> Option<Self> {
345-
Some(match x {
346-
0 => Self::Reraise,
347-
1 => Self::Raise,
348-
2 => Self::RaiseCause,
349-
_ => return None,
350-
})
351-
}
352-
}
348+
);
353349

354350
pub type NameIdx = u32;
355351

@@ -785,138 +781,74 @@ impl<C: Constant> BorrowedConstant<'_, C> {
785781
}
786782
}
787783

788-
/// The possible comparison operators
789-
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
790-
#[repr(u8)]
791-
pub enum ComparisonOperator {
792-
// be intentional with bits so that we can do eval_ord with just a bitwise and
793-
// bits: | Equal | Greater | Less |
794-
Less = 0b001,
795-
Greater = 0b010,
796-
NotEqual = 0b011,
797-
Equal = 0b100,
798-
LessOrEqual = 0b101,
799-
GreaterOrEqual = 0b110,
800-
}
801-
802-
impl OpArgType for ComparisonOperator {
803-
fn to_oparg(self) -> u32 {
804-
self as u32
805-
}
806-
fn from_oparg(x: u32) -> Option<Self> {
807-
Some(match x {
808-
0b001 => Self::Less,
809-
0b010 => Self::Greater,
810-
0b011 => Self::NotEqual,
811-
0b100 => Self::Equal,
812-
0b101 => Self::LessOrEqual,
813-
0b110 => Self::GreaterOrEqual,
814-
_ => return None,
815-
})
816-
}
817-
}
818-
819-
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
820-
#[repr(u8)]
821-
pub enum TestOperator {
822-
In,
823-
NotIn,
824-
Is,
825-
IsNot,
826-
/// two exceptions that match?
827-
ExceptionMatch,
828-
}
829-
830-
impl OpArgType for TestOperator {
831-
fn to_oparg(self) -> u32 {
832-
self as u32
833-
}
834-
fn from_oparg(x: u32) -> Option<Self> {
835-
Some(match x {
836-
0 => Self::In,
837-
1 => Self::NotIn,
838-
2 => Self::Is,
839-
3 => Self::IsNot,
840-
4 => Self::ExceptionMatch,
841-
_ => return None,
842-
})
843-
}
844-
}
845-
846-
/// The possible Binary operators
847-
/// # Examples
848-
///
849-
/// ```ignore
850-
/// use rustpython_compiler_core::Instruction::BinaryOperation;
851-
/// use rustpython_compiler_core::BinaryOperator::Add;
852-
/// let op = BinaryOperation {op: Add};
853-
/// ```
854-
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
855-
#[repr(u8)]
856-
pub enum BinaryOperator {
857-
Power,
858-
Multiply,
859-
MatrixMultiply,
860-
Divide,
861-
FloorDivide,
862-
Modulo,
863-
Add,
864-
Subtract,
865-
Lshift,
866-
Rshift,
867-
And,
868-
Xor,
869-
Or,
870-
}
871-
872-
impl OpArgType for BinaryOperator {
873-
fn to_oparg(self) -> u32 {
874-
self as u32
875-
}
876-
fn from_oparg(x: u32) -> Option<Self> {
877-
Some(match x {
878-
0 => Self::Power,
879-
1 => Self::Multiply,
880-
2 => Self::MatrixMultiply,
881-
3 => Self::Divide,
882-
4 => Self::FloorDivide,
883-
5 => Self::Modulo,
884-
6 => Self::Add,
885-
7 => Self::Subtract,
886-
8 => Self::Lshift,
887-
9 => Self::Rshift,
888-
10 => Self::And,
889-
11 => Self::Xor,
890-
12 => Self::Or,
891-
_ => return None,
892-
})
893-
}
894-
}
895-
896-
/// The possible unary operators
897-
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
898-
#[repr(u8)]
899-
pub enum UnaryOperator {
900-
Not,
901-
Invert,
902-
Minus,
903-
Plus,
904-
}
905-
906-
impl OpArgType for UnaryOperator {
907-
fn to_oparg(self) -> u32 {
908-
self as u32
909-
}
910-
fn from_oparg(x: u32) -> Option<Self> {
911-
Some(match x {
912-
0 => Self::Not,
913-
1 => Self::Invert,
914-
2 => Self::Minus,
915-
3 => Self::Plus,
916-
_ => return None,
917-
})
918-
}
919-
}
784+
oparg_enum!(
785+
/// The possible comparison operators
786+
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
787+
#[repr(u8)]
788+
pub enum ComparisonOperator {
789+
// be intentional with bits so that we can do eval_ord with just a bitwise and
790+
// bits: | Equal | Greater | Less |
791+
Less = 0b001,
792+
Greater = 0b010,
793+
NotEqual = 0b011,
794+
Equal = 0b100,
795+
LessOrEqual = 0b101,
796+
GreaterOrEqual = 0b110,
797+
}
798+
);
799+
800+
oparg_enum!(
801+
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
802+
#[repr(u8)]
803+
pub enum TestOperator {
804+
In = 0,
805+
NotIn = 1,
806+
Is = 2,
807+
IsNot = 3,
808+
/// two exceptions that match?
809+
ExceptionMatch = 4,
810+
}
811+
);
812+
813+
oparg_enum!(
814+
/// The possible Binary operators
815+
/// # Examples
816+
///
817+
/// ```ignore
818+
/// use rustpython_compiler_core::Instruction::BinaryOperation;
819+
/// use rustpython_compiler_core::BinaryOperator::Add;
820+
/// let op = BinaryOperation {op: Add};
821+
/// ```
822+
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
823+
#[repr(u8)]
824+
pub enum BinaryOperator {
825+
Power = 0,
826+
Multiply = 1,
827+
MatrixMultiply = 2,
828+
Divide = 3,
829+
FloorDivide = 4,
830+
Modulo = 5,
831+
Add = 6,
832+
Subtract = 7,
833+
Lshift = 8,
834+
Rshift = 9,
835+
And = 10,
836+
Xor = 11,
837+
Or = 12,
838+
}
839+
);
840+
841+
oparg_enum!(
842+
/// The possible unary operators
843+
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
844+
#[repr(u8)]
845+
pub enum UnaryOperator {
846+
Not = 0,
847+
Invert = 1,
848+
Minus = 2,
849+
Plus = 3,
850+
}
851+
);
920852

921853
#[derive(Copy, Clone)]
922854
pub struct UnpackExArgs {

vm/src/builtins/code.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ fn borrow_obj_constant(obj: &PyObject) -> BorrowedConstant<Literal> {
7979
}
8080
ref t @ super::tuple::PyTuple => {
8181
let elements = t.as_slice();
82+
// SAFETY: Literal is repr(transparent) over PyObjectRef, and a Literal tuple only ever
83+
// has other literals as elements
8284
let elements = unsafe { &*(elements as *const [PyObjectRef] as *const [Literal]) };
8385
BorrowedConstant::Tuple { elements }
8486
}

0 commit comments

Comments
 (0)