Skip to content

Commit 1f8722a

Browse files
committed
Rename the enums
1 parent 453212e commit 1f8722a

File tree

9 files changed

+694
-699
lines changed

9 files changed

+694
-699
lines changed

crates/codegen/src/compile.rs

Lines changed: 421 additions & 427 deletions
Large diffs are not rendered by default.

crates/codegen/src/ir.rs

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ use crate::{IndexMap, IndexSet, error::InternalError};
55
use rustpython_compiler_core::{
66
OneIndexed, SourceLocation,
77
bytecode::{
8-
Arg, CodeFlags, CodeObject, CodeUnit, CodeUnits, ConstantData, ExceptionTableEntry,
9-
InstrDisplayContext, Instruction, InstructionMetadata, Label, OpArg, PseudoInstruction,
10-
PyCodeLocationInfoKind, RealInstruction, encode_exception_table, encode_load_attr_arg,
8+
AnyInstruction, Arg, CodeFlags, CodeObject, CodeUnit, CodeUnits, ConstantData,
9+
ExceptionTableEntry, InstrDisplayContext, Instruction, InstructionMetadata, Label, OpArg,
10+
PseudoInstruction, PyCodeLocationInfoKind, encode_exception_table, encode_load_attr_arg,
1111
encode_load_super_attr_arg,
1212
},
1313
varint::{write_signed_varint, write_varint},
@@ -87,7 +87,7 @@ impl ops::IndexMut<BlockIdx> for Vec<Block> {
8787

8888
#[derive(Debug, Clone)]
8989
pub struct InstructionInfo {
90-
pub instr: Instruction,
90+
pub instr: AnyInstruction,
9191
pub arg: OpArg,
9292
pub target: BlockIdx,
9393
pub location: SourceLocation,
@@ -198,24 +198,23 @@ impl CodeInfo {
198198
{
199199
for info in &mut block.instructions {
200200
// Special case for:
201-
// - `RealInstruction::LoadAttr`
202-
// - `RealInstruction::LoadSuperAttr`
201+
// - `Instruction::LoadAttr`
202+
// - `Instruction::LoadSuperAttr`
203203

204204
if let Some(instr) = info.instr.real() {
205205
match instr {
206206
// LOAD_ATTR → encode with method flag=0
207-
RealInstruction::LoadAttr { idx } => {
207+
Instruction::LoadAttr { idx } => {
208208
let encoded = encode_load_attr_arg(idx.get(info.arg), false);
209209
info.arg = OpArg(encoded);
210-
info.instr = RealInstruction::LoadAttr { idx: Arg::marker() }.into();
210+
info.instr = Instruction::LoadAttr { idx: Arg::marker() }.into();
211211
}
212212
// LOAD_SUPER_ATTR → encode with flags=0b10 (method=0, class=1)
213-
RealInstruction::LoadSuperAttr { arg: idx } => {
213+
Instruction::LoadSuperAttr { arg: idx } => {
214214
let encoded =
215215
encode_load_super_attr_arg(idx.get(info.arg), false, true);
216216
info.arg = OpArg(encoded);
217-
info.instr =
218-
RealInstruction::LoadSuperAttr { arg: Arg::marker() }.into();
217+
info.instr = Instruction::LoadSuperAttr { arg: Arg::marker() }.into();
219218
}
220219
_ => {}
221220
}
@@ -230,29 +229,29 @@ impl CodeInfo {
230229
PseudoInstruction::LoadAttrMethod { idx } => {
231230
let encoded = encode_load_attr_arg(idx.get(info.arg), true);
232231
info.arg = OpArg(encoded);
233-
info.instr = RealInstruction::LoadAttr { idx: Arg::marker() }.into();
232+
info.instr = Instruction::LoadAttr { idx: Arg::marker() }.into();
234233
}
235234
// POP_BLOCK pseudo → NOP
236235
PseudoInstruction::PopBlock => {
237-
info.instr = RealInstruction::Nop.into();
236+
info.instr = Instruction::Nop.into();
238237
}
239238
// LOAD_SUPER_METHOD pseudo → LOAD_SUPER_ATTR (flags=0b11: method=1, class=1)
240239
PseudoInstruction::LoadSuperMethod { idx } => {
241240
let encoded = encode_load_super_attr_arg(idx.get(info.arg), true, true);
242241
info.arg = OpArg(encoded);
243-
info.instr = RealInstruction::LoadSuperAttr { arg: Arg::marker() }.into();
242+
info.instr = Instruction::LoadSuperAttr { arg: Arg::marker() }.into();
244243
}
245244
// LOAD_ZERO_SUPER_ATTR pseudo → LOAD_SUPER_ATTR (flags=0b00: method=0, class=0)
246245
PseudoInstruction::LoadZeroSuperAttr { idx } => {
247246
let encoded = encode_load_super_attr_arg(idx.get(info.arg), false, false);
248247
info.arg = OpArg(encoded);
249-
info.instr = RealInstruction::LoadSuperAttr { arg: Arg::marker() }.into();
248+
info.instr = Instruction::LoadSuperAttr { arg: Arg::marker() }.into();
250249
}
251250
// LOAD_ZERO_SUPER_METHOD pseudo → LOAD_SUPER_ATTR (flags=0b01: method=1, class=0)
252251
PseudoInstruction::LoadZeroSuperMethod { idx } => {
253252
let encoded = encode_load_super_attr_arg(idx.get(info.arg), true, false);
254253
info.arg = OpArg(encoded);
255-
info.instr = RealInstruction::LoadSuperAttr { arg: Arg::marker() }.into();
254+
info.instr = Instruction::LoadSuperAttr { arg: Arg::marker() }.into();
256255
}
257256
PseudoInstruction::Jump { .. } => {
258257
// PseudoInstruction::Jump instructions are handled later
@@ -305,16 +304,16 @@ impl CodeInfo {
305304

306305
// Convert JUMP pseudo to real instructions (direction depends on offset)
307306
let op = match info.instr {
308-
Instruction::Pseudo(PseudoInstruction::Jump { .. })
307+
AnyInstruction::Pseudo(PseudoInstruction::Jump { .. })
309308
if target != BlockIdx::NULL =>
310309
{
311310
let target_offset = block_to_offset[target.idx()].0;
312311
if target_offset > current_offset {
313-
RealInstruction::JumpForward {
312+
Instruction::JumpForward {
314313
target: Arg::marker(),
315314
}
316315
} else {
317-
RealInstruction::JumpBackward {
316+
Instruction::JumpBackward {
318317
target: Arg::marker(),
319318
}
320319
}
@@ -329,7 +328,7 @@ impl CodeInfo {
329328
));
330329
instructions.extend(
331330
extras
332-
.map(|byte| CodeUnit::new(RealInstruction::ExtendedArg, byte))
331+
.map(|byte| CodeUnit::new(Instruction::ExtendedArg, byte))
333332
.chain([CodeUnit { op, arg: lo_arg }]),
334333
);
335334
current_offset += info.arg.instr_size() as u32;

crates/compiler-core/src/bytecode.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use rustpython_wtf8::{Wtf8, Wtf8Buf};
1616

1717
pub use crate::bytecode::{
1818
instruction::{
19-
Arg, Instruction, InstructionMetadata, PseudoInstruction, RealInstruction,
19+
AnyInstruction, Arg, Instruction, InstructionMetadata, PseudoInstruction,
2020
decode_load_attr_arg, decode_load_super_attr_arg, encode_load_attr_arg,
2121
encode_load_super_attr_arg,
2222
},
@@ -303,14 +303,14 @@ bitflags! {
303303
#[derive(Copy, Clone)]
304304
#[repr(C)]
305305
pub struct CodeUnit {
306-
pub op: RealInstruction,
306+
pub op: Instruction,
307307
pub arg: OpArgByte,
308308
}
309309

310310
const _: () = assert!(mem::size_of::<CodeUnit>() == 2);
311311

312312
impl CodeUnit {
313-
pub const fn new(op: RealInstruction, arg: OpArgByte) -> Self {
313+
pub const fn new(op: Instruction, arg: OpArgByte) -> Self {
314314
Self { op, arg }
315315
}
316316
}

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

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,15 @@ use crate::{
1212
marshal::MarshalError,
1313
};
1414

15-
/// A Single bytecode instruction.
16-
/// All instructions above 44 have an argument.
15+
/// A Single bytecode instruction that are executed by the VM.
16+
///
17+
/// Currently aligned with CPython 3.13.
18+
///
19+
/// ## See also
20+
/// - [CPython opcode IDs](https://github.com/python/cpython/blob/627894459a84be3488a1789919679c997056a03c/Include/opcode_ids.h)
1721
#[derive(Clone, Copy, Debug)]
1822
#[repr(u8)]
19-
pub enum RealInstruction {
23+
pub enum Instruction {
2024
// ==================== No-argument instructions (opcode < 44) ====================
2125
Cache = 0, // Placeholder
2226
BeforeAsyncWith = 1,
@@ -275,20 +279,21 @@ pub enum RealInstruction {
275279
JumpIfNotExcMatch(Arg<Label>) = 131,
276280
SetExcInfo = 134,
277281
Subscript = 135,
282+
// Pseudos (needs to be moved to `PseudoInstruction` enum.
278283
LoadClosure(Arg<NameIdx>) = 253, // TODO: Move to pseudos
279284
}
280285

281-
const _: () = assert!(mem::size_of::<RealInstruction>() == 1);
286+
const _: () = assert!(mem::size_of::<Instruction>() == 1);
282287

283-
impl From<RealInstruction> for u8 {
288+
impl From<Instruction> for u8 {
284289
#[inline]
285-
fn from(ins: RealInstruction) -> Self {
290+
fn from(ins: Instruction) -> Self {
286291
// SAFETY: there's no padding bits
287-
unsafe { core::mem::transmute::<RealInstruction, Self>(ins) }
292+
unsafe { mem::transmute::<Instruction, Self>(ins) }
288293
}
289294
}
290295

291-
impl TryFrom<u8> for RealInstruction {
296+
impl TryFrom<u8> for Instruction {
292297
type Error = MarshalError;
293298

294299
#[inline]
@@ -341,14 +346,14 @@ impl TryFrom<u8> for RealInstruction {
341346
|| value == load_closure
342347
|| custom_ops.contains(&value)
343348
{
344-
Ok(unsafe { core::mem::transmute::<u8, Self>(value) })
349+
Ok(unsafe { mem::transmute::<u8, Self>(value) })
345350
} else {
346351
Err(Self::Error::InvalidBytecode)
347352
}
348353
}
349354
}
350355

351-
impl InstructionMetadata for RealInstruction {
356+
impl InstructionMetadata for Instruction {
352357
#[inline]
353358
fn label_arg(&self) -> Option<Arg<Label>> {
354359
match self {
@@ -783,6 +788,7 @@ impl InstructionMetadata for RealInstruction {
783788
}
784789
}
785790

791+
/// Instructions used by the compiler. They are not executed by the VM.
786792
#[derive(Clone, Copy, Debug)]
787793
#[repr(u16)]
788794
pub enum PseudoInstruction {
@@ -821,7 +827,7 @@ impl From<PseudoInstruction> for u16 {
821827
#[inline]
822828
fn from(ins: PseudoInstruction) -> Self {
823829
// SAFETY: there's no padding bits
824-
unsafe { core::mem::transmute::<PseudoInstruction, Self>(ins) }
830+
unsafe { mem::transmute::<PseudoInstruction, Self>(ins) }
825831
}
826832
}
827833

@@ -836,7 +842,7 @@ impl TryFrom<u16> for PseudoInstruction {
836842
let end = u16::from(Self::StoreFastMaybeNull);
837843

838844
if (start..=end).contains(&value) {
839-
Ok(unsafe { core::mem::transmute::<u16, Self>(value) })
845+
Ok(unsafe { mem::transmute::<u16, Self>(value) })
840846
} else {
841847
Err(Self::Error::InvalidBytecode)
842848
}
@@ -886,32 +892,32 @@ impl InstructionMetadata for PseudoInstruction {
886892
}
887893

888894
#[derive(Clone, Copy, Debug)]
889-
pub enum Instruction {
890-
Real(RealInstruction),
895+
pub enum AnyInstruction {
896+
Real(Instruction),
891897
Pseudo(PseudoInstruction),
892898
}
893899

894-
impl From<RealInstruction> for Instruction {
895-
fn from(value: RealInstruction) -> Self {
900+
impl From<Instruction> for AnyInstruction {
901+
fn from(value: Instruction) -> Self {
896902
Self::Real(value)
897903
}
898904
}
899905

900-
impl From<PseudoInstruction> for Instruction {
906+
impl From<PseudoInstruction> for AnyInstruction {
901907
fn from(value: PseudoInstruction) -> Self {
902908
Self::Pseudo(value)
903909
}
904910
}
905911

906-
impl TryFrom<u8> for Instruction {
912+
impl TryFrom<u8> for AnyInstruction {
907913
type Error = MarshalError;
908914

909915
fn try_from(value: u8) -> Result<Self, Self::Error> {
910-
Ok(RealInstruction::try_from(value)?.into())
916+
Ok(Instruction::try_from(value)?.into())
911917
}
912918
}
913919

914-
impl TryFrom<u16> for Instruction {
920+
impl TryFrom<u16> for AnyInstruction {
915921
type Error = MarshalError;
916922

917923
fn try_from(value: u16) -> Result<Self, Self::Error> {
@@ -933,7 +939,7 @@ macro_rules! inst_either {
933939
};
934940
}
935941

936-
impl InstructionMetadata for Instruction {
942+
impl InstructionMetadata for AnyInstruction {
937943
inst_either!(fn label_arg(&self) -> Option<Arg<Label>>);
938944

939945
inst_either!(fn unconditional_branch(&self) -> bool);
@@ -951,9 +957,9 @@ impl InstructionMetadata for Instruction {
951957
) -> fmt::Result);
952958
}
953959

954-
impl Instruction {
960+
impl AnyInstruction {
955961
/// Gets the inner value of [`Self::Real`].
956-
pub const fn real(self) -> Option<RealInstruction> {
962+
pub const fn real(self) -> Option<Instruction> {
957963
match self {
958964
Self::Real(ins) => Some(ins),
959965
_ => None,
@@ -973,7 +979,7 @@ impl Instruction {
973979
/// # Panics
974980
///
975981
/// If was called on something else other than [`Self::Real`].
976-
pub const fn expect_real(self) -> RealInstruction {
982+
pub const fn expect_real(self) -> Instruction {
977983
self.real()
978984
.expect("Expected Instruction::Real, found Instruction::Pseudo")
979985
}
@@ -998,8 +1004,8 @@ pub trait InstructionMetadata {
9981004
/// # Examples
9991005
///
10001006
/// ```
1001-
/// use rustpython_compiler_core::bytecode::{Arg, RealInstruction, InstructionMetadata};
1002-
/// let jump_inst = RealInstruction::JumpForward { target: Arg::marker() };
1007+
/// use rustpython_compiler_core::bytecode::{Arg, Instruction, InstructionMetadata};
1008+
/// let jump_inst = Instruction::JumpForward { target: Arg::marker() };
10031009
/// assert!(jump_inst.unconditional_branch())
10041010
/// ```
10051011
fn unconditional_branch(&self) -> bool;
@@ -1009,9 +1015,9 @@ pub trait InstructionMetadata {
10091015
/// # Examples
10101016
///
10111017
/// ```
1012-
/// use rustpython_compiler_core::bytecode::{Arg, RealInstruction, Label, InstructionMetadata};
1018+
/// use rustpython_compiler_core::bytecode::{Arg, Instruction, Label, InstructionMetadata};
10131019
/// let (target, jump_arg) = Arg::new(Label(0xF));
1014-
/// let jump_instruction = RealInstruction::JumpForward { target };
1020+
/// let jump_instruction = Instruction::JumpForward { target };
10151021
/// assert_eq!(jump_instruction.stack_effect(jump_arg, true), 0);
10161022
/// ```
10171023
fn stack_effect(&self, arg: OpArg, jump: bool) -> i32;

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use bitflags::bitflags;
22

33
use core::{fmt, num::NonZeroU8};
44

5-
use crate::bytecode::{CodeUnit, instruction::RealInstruction};
5+
use crate::bytecode::{CodeUnit, instruction::Instruction};
66

77
pub trait OpArgType: Copy {
88
fn from_op_arg(x: u32) -> Option<Self>;
@@ -78,9 +78,9 @@ pub struct OpArgState {
7878

7979
impl OpArgState {
8080
#[inline(always)]
81-
pub fn get(&mut self, ins: CodeUnit) -> (RealInstruction, OpArg) {
81+
pub fn get(&mut self, ins: CodeUnit) -> (Instruction, OpArg) {
8282
let arg = self.extend(ins.arg);
83-
if !matches!(ins.op, RealInstruction::ExtendedArg) {
83+
if !matches!(ins.op, Instruction::ExtendedArg) {
8484
self.reset();
8585
}
8686
(ins.op, arg)
@@ -98,7 +98,7 @@ impl OpArgState {
9898
}
9999
}
100100

101-
/// Oparg values for [`RealInstruction::ConvertValue`].
101+
/// Oparg values for [`Instruction::ConvertValue`].
102102
///
103103
/// ## See also
104104
///
@@ -142,7 +142,7 @@ impl fmt::Display for ConvertValueOparg {
142142
Self::Str => "1 (str)",
143143
Self::Repr => "2 (repr)",
144144
Self::Ascii => "3 (ascii)",
145-
// We should never reach this. `FVC_NONE` are being handled by `RealInstruction::FormatSimple`
145+
// We should never reach this. `FVC_NONE` are being handled by `Instruction::FormatSimple`
146146
Self::None => "",
147147
};
148148

@@ -364,9 +364,9 @@ op_arg_enum!(
364364
/// # Examples
365365
///
366366
/// ```rust
367-
/// use rustpython_compiler_core::bytecode::{Arg, BinaryOperator, RealInstruction};
367+
/// use rustpython_compiler_core::bytecode::{Arg, BinaryOperator, Instruction};
368368
/// let (op, _) = Arg::new(BinaryOperator::Add);
369-
/// let instruction = RealInstruction::BinaryOp { op };
369+
/// let instruction = Instruction::BinaryOp { op };
370370
/// ```
371371
///
372372
/// See also:

0 commit comments

Comments
 (0)