rustpython-compiler-core 0.5.0

RustPython specific bytecode.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
use core::fmt;

use crate::{
    bytecode::{CodeUnit, instruction::Instruction},
    marshal::MarshalError,
};

pub trait OpArgType: Copy + Into<u32> + TryFrom<u32> {}

/// Opcode argument that may be extended by a prior ExtendedArg.
#[derive(Copy, Clone, PartialEq, Eq)]
#[repr(transparent)]
pub struct OpArgByte(u8);

impl OpArgByte {
    pub const NULL: Self = Self::new(0);

    #[must_use]
    pub const fn new(value: u8) -> Self {
        Self(value)
    }
}

impl From<u8> for OpArgByte {
    fn from(raw: u8) -> Self {
        Self::new(raw)
    }
}

impl From<OpArgByte> for u8 {
    fn from(value: OpArgByte) -> Self {
        value.0
    }
}

impl fmt::Debug for OpArgByte {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.fmt(f)
    }
}

/// Full 32-bit op_arg, including any possible ExtendedArg extension.
#[derive(Copy, Clone, Debug)]
#[repr(transparent)]
pub struct OpArg(u32);

impl OpArg {
    pub const NULL: Self = Self::new(0);

    #[must_use]
    pub const fn new(value: u32) -> Self {
        Self(value)
    }

    /// Returns how many CodeUnits a instruction with this op_arg will be encoded as
    #[inline]
    pub const fn instr_size(self) -> usize {
        (self.0 > 0xff) as usize + (self.0 > 0xff_ff) as usize + (self.0 > 0xff_ff_ff) as usize + 1
    }

    /// returns the arg split into any necessary ExtendedArg components (in big-endian order) and
    /// the arg for the real opcode itself
    #[inline(always)]
    pub fn split(self) -> (impl ExactSizeIterator<Item = OpArgByte>, OpArgByte) {
        let mut it = self
            .0
            .to_le_bytes()
            .map(OpArgByte)
            .into_iter()
            .take(self.instr_size());
        let lo = it.next().unwrap();
        (it.rev(), lo)
    }
}

impl From<u32> for OpArg {
    fn from(raw: u32) -> Self {
        Self::new(raw)
    }
}

impl From<OpArg> for u32 {
    fn from(value: OpArg) -> Self {
        value.0
    }
}

#[derive(Default, Copy, Clone)]
#[repr(transparent)]
pub struct OpArgState {
    state: u32,
}

impl OpArgState {
    #[inline(always)]
    pub fn get(&mut self, ins: CodeUnit) -> (Instruction, OpArg) {
        let arg = self.extend(ins.arg);
        if !matches!(ins.op, Instruction::ExtendedArg) {
            self.reset();
        }
        (ins.op, arg)
    }

    #[inline(always)]
    pub fn extend(&mut self, arg: OpArgByte) -> OpArg {
        self.state = (self.state << 8) | u32::from(arg.0);
        self.state.into()
    }

    #[inline(always)]
    pub const fn reset(&mut self) {
        self.state = 0
    }
}

/// Helper macro for defining oparg enums in an optimal way.
///
/// Will generate the following:
///
/// - Enum which variant's aren't assigned any value (for optimizations).
/// - impl [`TryFrom<u8>`]
/// - impl [`TryFrom<u32>`]
/// - impl [`Into<u8>`]
/// - impl [`Into<u32>`]
/// - impl [`OpArgType`]
///
/// # Note
/// If an enum variant has "alternative" values (i.e. `Foo = 0 | 1`), the first value will be the
/// result of converting to a number.
///
/// # Examples
///
/// ```ignore
/// oparg_enum!(
///     /// Oparg for the `X` opcode.
///     #[derive(Clone, Copy)]
///     pub enum MyOpArg {
///         /// Some doc.
///         Foo = 4,
///         Bar = 8,
///         Baz = 15 | 16,
///         Qux = 23 | 42
///     }
/// );
/// ```
macro_rules! oparg_enum {
    (
        $(#[$enum_meta:meta])*
        $vis:vis enum $name:ident {
            $(
                $(#[$variant_meta:meta])*
                $variant:ident = $value:literal $(| $alternatives:expr)*
            ),* $(,)?
        }
    ) => {
        $(#[$enum_meta])*
        $vis enum $name {
            $(
                $(#[$variant_meta])*
                $variant, // Do assign value to variant.
            )*
        }

        impl_oparg_enum!(
            enum $name {
                $(
                    $variant = $value $(| $alternatives)*,
                )*
            }
        );
    };
}

macro_rules! impl_oparg_enum {
    (
        enum $name:ident {
            $(
                $variant:ident = $value:literal $(| $alternatives:expr)*
            ),* $(,)?
        }
    ) => {
        impl TryFrom<u8> for $name {
            type Error = $crate::marshal::MarshalError;

            fn try_from(value: u8) -> Result<Self, Self::Error> {
                Ok(match value {
                    $(
                        $value $(| $alternatives)* => Self::$variant,
                    )*
                    _ => return Err(Self::Error::InvalidBytecode),
                })
            }
        }

        impl TryFrom<u32> for $name {
            type Error = $crate::marshal::MarshalError;

            fn try_from(value: u32) -> Result<Self, Self::Error> {
                u8::try_from(value)
                    .map_err(|_| Self::Error::InvalidBytecode)
                    .map(TryInto::try_into)?
            }
        }

        impl From<$name> for u8 {
            fn from(value: $name) -> Self {
                match value {
                    $(
                        $name::$variant => $value,
                    )*
                }
            }
        }

        impl From<$name> for u32 {
            fn from(value: $name) -> Self {
                Self::from(u8::from(value))
            }
        }

        impl OpArgType for $name {}
    };
}

oparg_enum!(
    /// Oparg values for [`Instruction::ConvertValue`].
    ///
    /// ## See also
    ///
    /// - [CPython FVC_* flags](https://github.com/python/cpython/blob/8183fa5e3f78ca6ab862de7fb8b14f3d929421e0/Include/ceval.h#L129-L132)
    #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
    pub enum ConvertValueOparg {
        /// No conversion.
        ///
        /// ```python
        /// f"{x}"
        /// f"{x:4}"
        /// ```
        // Ruff `ConversionFlag::None` is `-1i8`, when its converted to `u8` its value is `u8::MAX`.
        None = 0 | 255,
        /// Converts by calling `str(<value>)`.
        ///
        /// ```python
        /// f"{x!s}"
        /// f"{x!s:2}"
        /// ```
        Str = 1,
        /// Converts by calling `repr(<value>)`.
        ///
        /// ```python
        /// f"{x!r}"
        /// f"{x!r:2}"
        /// ```
        Repr = 2,
        /// Converts by calling `ascii(<value>)`.
        ///
        /// ```python
        /// f"{x!a}"
        /// f"{x!a:2}"
        /// ```
        Ascii = 3,
    }
);

impl fmt::Display for ConvertValueOparg {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let out = match self {
            Self::Str => "1 (str)",
            Self::Repr => "2 (repr)",
            Self::Ascii => "3 (ascii)",
            // We should never reach this. `FVC_NONE` are being handled by `Instruction::FormatSimple`
            Self::None => "",
        };

        write!(f, "{out}")
    }
}

pub type NameIdx = u32;

impl OpArgType for u32 {}

oparg_enum!(
    /// The kind of Raise that occurred.
    #[derive(Copy, Clone, Debug, PartialEq, Eq)]
    pub enum RaiseKind {
        /// Bare `raise` statement with no arguments.
        /// Gets the current exception from VM state (topmost_exception).
        /// Maps to RAISE_VARARGS with oparg=0.
        BareRaise = 0,
        /// `raise exc` - exception is on the stack.
        /// Maps to RAISE_VARARGS with oparg=1.
        Raise = 1,
        /// `raise exc from cause` - exception and cause are on the stack.
        /// Maps to RAISE_VARARGS with oparg=2.
        RaiseCause = 2,
        /// Reraise exception from the stack top.
        /// Used in exception handler cleanup blocks (finally, except).
        /// Gets exception from stack, not from VM state.
        /// Maps to the RERAISE opcode.
        ReraiseFromStack = 3,
    }
);

oparg_enum!(
    /// Intrinsic function for CALL_INTRINSIC_1
    #[derive(Copy, Clone, Debug, PartialEq, Eq)]
    pub enum IntrinsicFunction1 {
        // Invalid = 0,
        Print = 1,
        /// Import * operation
        ImportStar = 2,
        /// Convert StopIteration to RuntimeError in async context
        StopIterationError = 3,
        AsyncGenWrap = 4,
        UnaryPositive = 5,
        /// Convert list to tuple
        ListToTuple = 6,
        /// Type parameter related
        TypeVar = 7,
        ParamSpec = 8,
        TypeVarTuple = 9,
        /// Generic subscript for PEP 695
        SubscriptGeneric = 10,
        TypeAlias = 11,
    }
);

oparg_enum!(
    /// Intrinsic function for CALL_INTRINSIC_2
    #[derive(Copy, Clone, Debug, PartialEq, Eq)]
    pub enum IntrinsicFunction2 {
        PrepReraiseStar = 1,
        TypeVarWithBound = 2,
        TypeVarWithConstraint = 3,
        SetFunctionTypeParams = 4,
        /// Set default value for type parameter (PEP 695)
        SetTypeparamDefault = 5,
    }
);

bitflagset::bitflag! {
    /// `SET_FUNCTION_ATTRIBUTE` flags.
    /// Bitmask: Defaults=0x01, KwOnly=0x02, Annotations=0x04,
    /// Closure=0x08, TypeParams=0x10, Annotate=0x20.
    /// Stored as bit position (0-5) by `bitflag!` macro.
    #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
    #[repr(u8)]
    pub enum MakeFunctionFlag {
        Defaults = 0,
        KwOnlyDefaults = 1,
        Annotations = 2,
        Closure = 3,
        /// PEP 649: __annotate__ function closure (instead of __annotations__ dict)
        Annotate = 4,
        TypeParams = 5,
    }
}

bitflagset::bitflagset! {
    #[derive(Copy, Clone, PartialEq, Eq)]
    pub struct MakeFunctionFlags(u8): MakeFunctionFlag
}

impl TryFrom<u32> for MakeFunctionFlag {
    type Error = MarshalError;

    /// Decode from CPython-compatible power-of-two value
    fn try_from(value: u32) -> Result<Self, Self::Error> {
        match value {
            0x01 => Ok(Self::Defaults),
            0x02 => Ok(Self::KwOnlyDefaults),
            0x04 => Ok(Self::Annotations),
            0x08 => Ok(Self::Closure),
            0x10 => Ok(Self::Annotate),
            0x20 => Ok(Self::TypeParams),
            _ => Err(MarshalError::InvalidBytecode),
        }
    }
}

impl From<MakeFunctionFlag> for u32 {
    /// Encode as CPython-compatible power-of-two value
    fn from(flag: MakeFunctionFlag) -> Self {
        1u32 << (flag as u32)
    }
}

impl OpArgType for MakeFunctionFlag {}

/// `COMPARE_OP` arg is `(cmp_index << 5) | mask`.  Only the upper
/// 3 bits identify the comparison; the lower 5 bits are an inline
/// cache mask for adaptive specialization.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum ComparisonOperator {
    Less,
    LessOrEqual,
    Equal,
    NotEqual,
    Greater,
    GreaterOrEqual,
}

impl TryFrom<u8> for ComparisonOperator {
    type Error = MarshalError;
    fn try_from(value: u8) -> Result<Self, Self::Error> {
        Self::try_from(value as u32)
    }
}

impl TryFrom<u32> for ComparisonOperator {
    type Error = MarshalError;
    /// Decode from `COMPARE_OP` arg: `(cmp_index << 5) | mask`.
    fn try_from(value: u32) -> Result<Self, Self::Error> {
        match value >> 5 {
            0 => Ok(Self::Less),
            1 => Ok(Self::LessOrEqual),
            2 => Ok(Self::Equal),
            3 => Ok(Self::NotEqual),
            4 => Ok(Self::Greater),
            5 => Ok(Self::GreaterOrEqual),
            _ => Err(MarshalError::InvalidBytecode),
        }
    }
}

impl From<ComparisonOperator> for u8 {
    /// Encode as `cmp_index << 5` (mask bits zero).
    fn from(value: ComparisonOperator) -> Self {
        match value {
            ComparisonOperator::Less => 0,
            ComparisonOperator::LessOrEqual => 1 << 5,
            ComparisonOperator::Equal => 2 << 5,
            ComparisonOperator::NotEqual => 3 << 5,
            ComparisonOperator::Greater => 4 << 5,
            ComparisonOperator::GreaterOrEqual => 5 << 5,
        }
    }
}

impl From<ComparisonOperator> for u32 {
    fn from(value: ComparisonOperator) -> Self {
        Self::from(u8::from(value))
    }
}

impl OpArgType for ComparisonOperator {}

oparg_enum!(
    /// The possible Binary operators
    ///
    /// # Examples
    ///
    /// ```rust
    /// use rustpython_compiler_core::bytecode::{Arg, BinaryOperator, Instruction};
    /// let (op, _) = Arg::new(BinaryOperator::Add);
    /// let instruction = Instruction::BinaryOp { op };
    /// ```
    ///
    /// See also:
    /// - [_PyEval_BinaryOps](https://github.com/python/cpython/blob/8183fa5e3f78ca6ab862de7fb8b14f3d929421e0/Python/ceval.c#L316-L343)
    #[derive(Clone, Copy, Debug, Eq, PartialEq)]
    pub enum BinaryOperator {
        /// `+`
        Add = 0,
        /// `&`
        And = 1,
        /// `//`
        FloorDivide = 2,
        /// `<<`
        Lshift = 3,
        /// `@`
        MatrixMultiply = 4,
        /// `*`
        Multiply = 5,
        /// `%`
        Remainder = 6,
        /// `|`
        Or = 7,
        /// `**`
        Power = 8,
        /// `>>`
        Rshift = 9,
        /// `-`
        Subtract = 10,
        /// `/`
        TrueDivide = 11,
        /// `^`
        Xor = 12,
        /// `+=`
        InplaceAdd = 13,
        /// `&=`
        InplaceAnd = 14,
        /// `//=`
        InplaceFloorDivide = 15,
        /// `<<=`
        InplaceLshift = 16,
        /// `@=`
        InplaceMatrixMultiply = 17,
        /// `*=`
        InplaceMultiply = 18,
        /// `%=`
        InplaceRemainder = 19,
        /// `|=`
        InplaceOr = 20,
        /// `**=`
        InplacePower = 21,
        /// `>>=`
        InplaceRshift = 22,
        /// `-=`
        InplaceSubtract = 23,
        /// `/=`
        InplaceTrueDivide = 24,
        /// `^=`
        InplaceXor = 25,
        /// `[]` subscript
        Subscr = 26,
    }
);

impl BinaryOperator {
    /// Get the "inplace" version of the operator.
    /// This has no effect if `self` is already an "inplace" operator.
    ///
    /// # Example
    /// ```rust
    /// use rustpython_compiler_core::bytecode::BinaryOperator;
    ///
    /// assert_eq!(BinaryOperator::Power.as_inplace(), BinaryOperator::InplacePower);
    ///
    /// assert_eq!(BinaryOperator::InplaceSubtract.as_inplace(), BinaryOperator::InplaceSubtract);
    /// ```
    #[must_use]
    pub const fn as_inplace(self) -> Self {
        match self {
            Self::Add => Self::InplaceAdd,
            Self::And => Self::InplaceAnd,
            Self::FloorDivide => Self::InplaceFloorDivide,
            Self::Lshift => Self::InplaceLshift,
            Self::MatrixMultiply => Self::InplaceMatrixMultiply,
            Self::Multiply => Self::InplaceMultiply,
            Self::Remainder => Self::InplaceRemainder,
            Self::Or => Self::InplaceOr,
            Self::Power => Self::InplacePower,
            Self::Rshift => Self::InplaceRshift,
            Self::Subtract => Self::InplaceSubtract,
            Self::TrueDivide => Self::InplaceTrueDivide,
            Self::Xor => Self::InplaceXor,
            _ => self,
        }
    }
}

impl fmt::Display for BinaryOperator {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let op = match self {
            Self::Add => "+",
            Self::And => "&",
            Self::FloorDivide => "//",
            Self::Lshift => "<<",
            Self::MatrixMultiply => "@",
            Self::Multiply => "*",
            Self::Remainder => "%",
            Self::Or => "|",
            Self::Power => "**",
            Self::Rshift => ">>",
            Self::Subtract => "-",
            Self::TrueDivide => "/",
            Self::Xor => "^",
            Self::InplaceAdd => "+=",
            Self::InplaceAnd => "&=",
            Self::InplaceFloorDivide => "//=",
            Self::InplaceLshift => "<<=",
            Self::InplaceMatrixMultiply => "@=",
            Self::InplaceMultiply => "*=",
            Self::InplaceRemainder => "%=",
            Self::InplaceOr => "|=",
            Self::InplacePower => "**=",
            Self::InplaceRshift => ">>=",
            Self::InplaceSubtract => "-=",
            Self::InplaceTrueDivide => "/=",
            Self::InplaceXor => "^=",
            Self::Subscr => "[]",
        };
        write!(f, "{op}")
    }
}

oparg_enum!(
    /// Whether or not to invert the operation.
    #[derive(Debug, Copy, Clone, PartialEq, Eq)]
    pub enum Invert {
        /// ```py
        /// foo is bar
        /// x in lst
        /// ```
        No = 0,
        /// ```py
        /// foo is not bar
        /// x not in lst
        /// ```
        Yes = 1,
    }
);

oparg_enum!(
    /// Special method for LOAD_SPECIAL opcode (context managers).
    #[derive(Debug, Copy, Clone, PartialEq, Eq)]
    pub enum SpecialMethod {
        /// `__enter__` for sync context manager
        Enter = 0,
        /// `__exit__` for sync context manager
        Exit = 1,
        /// `__aenter__` for async context manager
        AEnter = 2,
        /// `__aexit__` for async context manager
        AExit = 3,
    }
);

impl fmt::Display for SpecialMethod {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let method_name = match self {
            Self::Enter => "__enter__",
            Self::Exit => "__exit__",
            Self::AEnter => "__aenter__",
            Self::AExit => "__aexit__",
        };
        write!(f, "{method_name}")
    }
}

oparg_enum!(
    /// Common constants for LOAD_COMMON_CONSTANT opcode.
    /// pycore_opcode_utils.h CONSTANT_*
    #[derive(Debug, Copy, Clone, PartialEq, Eq)]
    pub enum CommonConstant {
        /// `AssertionError` exception type
        AssertionError = 0,
        /// `NotImplementedError` exception type
        NotImplementedError = 1,
        /// Built-in `tuple` type
        BuiltinTuple = 2,
        /// Built-in `all` function
        BuiltinAll = 3,
        /// Built-in `any` function
        BuiltinAny = 4,
        /// Built-in `list` type
        BuiltinList = 5,
        /// Built-in `set` type
        BuiltinSet = 6,
    }
);

impl fmt::Display for CommonConstant {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let name = match self {
            Self::AssertionError => "AssertionError",
            Self::NotImplementedError => "NotImplementedError",
            Self::BuiltinTuple => "tuple",
            Self::BuiltinAll => "all",
            Self::BuiltinAny => "any",
            Self::BuiltinList => "list",
            Self::BuiltinSet => "set",
        };
        write!(f, "{name}")
    }
}

oparg_enum!(
    /// Specifies if a slice is built with either 2 or 3 arguments.
    #[derive(Clone, Copy, Debug, Eq, PartialEq)]
    pub enum BuildSliceArgCount {
        /// ```py
        /// x[5:10]
        /// ```
        Two = 2,
        /// ```py
        /// x[5:10:2]
        /// ```
        Three = 3,
    }
);

#[derive(Copy, Clone)]
pub struct UnpackExArgs {
    pub before: u8,
    pub after: u8,
}

impl From<u32> for UnpackExArgs {
    fn from(value: u32) -> Self {
        let [before, after, ..] = value.to_le_bytes();
        Self { before, after }
    }
}

impl From<UnpackExArgs> for u32 {
    fn from(value: UnpackExArgs) -> Self {
        Self::from_le_bytes([value.before, value.after, 0, 0])
    }
}

impl OpArgType for UnpackExArgs {}

impl fmt::Display for UnpackExArgs {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "before: {}, after: {}", self.before, self.after)
    }
}

macro_rules! newtype_oparg {
    (
      $(#[$oparg_meta:meta])*
      $vis:vis struct $name:ident(u32)
    ) => {
        $(#[$oparg_meta])*
        $vis struct $name(u32);

        impl $name {
            /// Creates a new [`$name`] instance.
            #[must_use]
            pub const fn from_u32(value: u32) -> Self {
                Self(value)
            }

            /// Returns the oparg as a `u32` value.
            #[must_use]
            pub const fn as_u32(self) -> u32 {
                self.0
            }

            /// Returns the oparg as a `usize` value.
            #[must_use]
            pub const fn as_usize(self) -> usize {
              self.0 as usize
            }
        }

        impl From<u32> for $name {
            fn from(value: u32) -> Self {
                Self::from_u32(value)
            }
        }

        impl From<$name> for u32 {
            fn from(value: $name) -> Self {
                value.as_u32()
            }
        }

        impl From<$name> for usize {
            fn from(value: $name) -> Self {
                value.as_usize()
            }
        }

        impl ::core::fmt::Display for $name {
            fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
                self.0.fmt(f)
            }
        }

        impl OpArgType for $name {}
    }
}

newtype_oparg!(
    #[derive(Clone, Copy)]
    #[repr(transparent)]
    pub struct ConstIdx(u32)
);

newtype_oparg!(
    #[derive(Clone, Copy)]
    #[repr(transparent)]
    pub struct VarNum(u32)
);

newtype_oparg!(
    #[derive(Clone, Copy)]
    #[repr(transparent)]
    pub struct VarNums(u32)
);

newtype_oparg!(
    #[derive(Clone, Copy)]
    #[repr(transparent)]
    pub struct LoadAttr(u32)
);

newtype_oparg!(
    #[derive(Clone, Copy)]
    #[repr(transparent)]
    pub struct LoadSuperAttr(u32)
);

newtype_oparg!(
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
    #[repr(transparent)]
    pub struct Label(u32)
);

newtype_oparg!(
    /// Context for [`Instruction::Resume`].
    ///
    /// The oparg consists of two parts:
    /// 1. [`ResumeContext::location`]: Indicates where the instruction occurs.
    /// 2. [`ResumeContext::is_exception_depth1`]: Is the instruction is at except-depth 1.
    #[derive(Clone, Copy)]
    #[repr(transparent)]
    pub struct ResumeContext(u32)
);

impl ResumeContext {
    /// [CPython `RESUME_OPARG_LOCATION_MASK`](https://github.com/python/cpython/blob/v3.14.3/Include/internal/pycore_opcode_utils.h#L84)
    pub const LOCATION_MASK: u32 = 0x3;

    /// [CPython `RESUME_OPARG_DEPTH1_MASK`](https://github.com/python/cpython/blob/v3.14.3/Include/internal/pycore_opcode_utils.h#L85)
    pub const DEPTH1_MASK: u32 = 0x4;

    #[must_use]
    pub const fn new(location: ResumeLocation, is_exception_depth1: bool) -> Self {
        let value = if is_exception_depth1 {
            Self::DEPTH1_MASK
        } else {
            0
        };

        Self::from_u32(location.as_u32() | value)
    }

    /// Resume location is determined by [`Self::LOCATION_MASK`].
    #[must_use]
    pub fn location(&self) -> ResumeLocation {
        // SAFETY: The mask should return a value that is in range.
        unsafe { ResumeLocation::try_from(self.as_u32() & Self::LOCATION_MASK).unwrap_unchecked() }
    }

    /// True if the bit at [`Self::DEPTH1_MASK`] is on.
    #[must_use]
    pub const fn is_exception_depth1(&self) -> bool {
        (self.as_u32() & Self::DEPTH1_MASK) != 0
    }
}

#[derive(Copy, Clone)]
pub enum ResumeLocation {
    /// At the start of a function, which is neither a generator, coroutine nor an async generator.
    AtFuncStart,
    /// After a `yield` expression.
    AfterYield,
    /// After a `yield from` expression.
    AfterYieldFrom,
    /// After an `await` expression.
    AfterAwait,
}

impl From<ResumeLocation> for ResumeContext {
    fn from(location: ResumeLocation) -> Self {
        Self::new(location, false)
    }
}

impl TryFrom<u32> for ResumeLocation {
    type Error = MarshalError;

    fn try_from(value: u32) -> Result<Self, Self::Error> {
        Ok(match value {
            0 => Self::AtFuncStart,
            1 => Self::AfterYield,
            2 => Self::AfterYieldFrom,
            3 => Self::AfterAwait,
            _ => return Err(Self::Error::InvalidBytecode),
        })
    }
}

impl ResumeLocation {
    #[must_use]
    pub const fn as_u8(&self) -> u8 {
        match self {
            Self::AtFuncStart => 0,
            Self::AfterYield => 1,
            Self::AfterYieldFrom => 2,
            Self::AfterAwait => 3,
        }
    }

    #[must_use]
    pub const fn as_u32(&self) -> u32 {
        self.as_u8() as u32
    }
}

impl From<ResumeLocation> for u8 {
    fn from(location: ResumeLocation) -> Self {
        location.as_u8()
    }
}

impl From<ResumeLocation> for u32 {
    fn from(location: ResumeLocation) -> Self {
        location.as_u32()
    }
}

impl VarNums {
    #[must_use]
    pub const fn idx_1(self) -> VarNum {
        VarNum::from_u32(self.0 >> 4)
    }

    #[must_use]
    pub const fn idx_2(self) -> VarNum {
        VarNum::from_u32(self.0 & 15)
    }

    #[must_use]
    pub const fn indexes(self) -> (VarNum, VarNum) {
        (self.idx_1(), self.idx_2())
    }
}

impl LoadAttr {
    #[must_use]
    pub const fn new(name_idx: u32, is_method: bool) -> Self {
        Self::from_u32((name_idx << 1) | (is_method as u32))
    }

    #[must_use]
    pub const fn name_idx(self) -> u32 {
        self.0 >> 1
    }

    #[must_use]
    pub const fn is_method(self) -> bool {
        (self.0 & 1) == 1
    }
}

impl LoadSuperAttr {
    #[must_use]
    pub const fn new(name_idx: u32, is_load_method: bool, has_class: bool) -> Self {
        Self::from_u32((name_idx << 2) | (is_load_method as u32) | ((has_class as u32) << 1))
    }

    #[must_use]
    pub const fn name_idx(self) -> u32 {
        self.0 >> 2
    }

    #[must_use]
    pub const fn is_load_method(self) -> bool {
        (self.0 & 1) == 1
    }

    #[must_use]
    pub const fn has_class(self) -> bool {
        (self.0 & 2) == 2
    }
}