[RFC] Split oparg type from the opcode enum

Đang mở
#6,746 1 bình luận 0 reaction 1 người được giao Xem trên GitHub

@coolreader18 đang làm issue này rồi.

Từ ngày 16/1/2026.

Đánh giá

Issue này chưa được đánh giá.

Mô tả

RFC

Summary

Introduce a new AnyOparg enum & make Instruction + PseudoInstruction enums to not hold the oparg type.

Detailed Explanation

The current Instruction enum couples the opcode and its oparg type together. In practice, this creates friction and many boilerplate code that can be avoided.

Suggested API

New pure Opcode enum that will have only the mapping between the opcode name and it's ID, like this:

enum Opcode {
   Nop = 0,
   IsOp = 1,
   BinaryOp = 2,
   ...
}

A new AnyOparg enum that will have all oparg types as it's variants:

enum AnyOparg {
    NameIdx(oparg::NameIdx),
    IntristicFunction1(oparg::IntristicFunction1),
    Invert(oparg::Invert),
    ...
}

An Instruction struct that is like a typed version of the CodeUnit struct:

struct Instruction {
  opcode: AnyOpcode,
  oparg: AnyOparg,
}

impl Instruction {
  pub fn new<T: Into<AnyOpcode>>(opcode: T, oparg: Option<i32>) -> Result<Self, MarshalError> {
    let oparg = match opcode.into() {
      AnyOpcode::Real(Opcode::Nop) => None,
      AnyOpcode::Real(Opcode::IsOp) => Some(oparg::Invert::try_from(oparg.unwrap())?),
      ...
    };

    Ok(Self { opcode: opcode.into(), oparg: oparg.map(Into::into) })
  }
}

Drawbacks, Rationale, and Alternatives

Drawbacks
  • Will require a serious refactoring to the code
Rational
  • Reduce boilerplate code (use num_enum crate for example).
  • Easier to auto-generate the opcodes (and their IDs) in the future.
  • Simplify opcode -> oparg mapping.

Unresolved Questions

Performance implifications?

  • Will probably increase performance because we will validate the oparg once instead of doing so every time we need to do an operation on the opcode.

Implementation & refactoring efforts?

Ngôn ngữ chính
Rust
Star
22.4k
Fork
1.5k
Merge trung bình
15 giờ 18 phút
Pull request đã merge (30 ngày)
172

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của RustPython/RustPython

Tất cả issue của RustPython/RustPython

Issue tương tự

Thêm issue về Rust

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.