Skip to content

Commit 1453726

Browse files
committed
Simplify match arms
1 parent 8ae98b2 commit 1453726

File tree

2 files changed

+70
-20
lines changed

2 files changed

+70
-20
lines changed

crates/codegen/src/ir.rs

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -197,48 +197,74 @@ impl CodeInfo {
197197
.filter(|b| b.next != BlockIdx::NULL || !b.instructions.is_empty())
198198
{
199199
for info in &mut block.instructions {
200-
match info.instr {
200+
// Special case for:
201+
// - `RealInstruction::LoadAttr`
202+
// - `RealInstruction::LoadSuperAttr`
203+
204+
if let Some(instr) = info.instr.real() {
205+
match instr {
206+
// LOAD_ATTR → encode with method flag=0
207+
RealInstruction::LoadAttr { idx } => {
208+
let encoded = encode_load_attr_arg(idx.get(info.arg), false);
209+
info.arg = OpArg(encoded);
210+
info.instr = RealInstruction::LoadAttr { idx: Arg::marker() }.into();
211+
}
212+
// LOAD_SUPER_ATTR → encode with flags=0b10 (method=0, class=1)
213+
RealInstruction::LoadSuperAttr { arg: idx } => {
214+
let encoded =
215+
encode_load_super_attr_arg(idx.get(info.arg), false, true);
216+
info.arg = OpArg(encoded);
217+
info.instr =
218+
RealInstruction::LoadSuperAttr { arg: Arg::marker() }.into();
219+
}
220+
_ => {}
221+
}
222+
223+
continue;
224+
}
225+
226+
let instr = info.instr.expect_pseudo();
227+
228+
match instr {
201229
// LOAD_ATTR_METHOD pseudo → LOAD_ATTR (with method flag=1)
202-
Instruction::Pseudo(PseudoInstruction::LoadAttrMethod { idx }) => {
230+
PseudoInstruction::LoadAttrMethod { idx } => {
203231
let encoded = encode_load_attr_arg(idx.get(info.arg), true);
204232
info.arg = OpArg(encoded);
205233
info.instr = RealInstruction::LoadAttr { idx: Arg::marker() }.into();
206234
}
207-
// LOAD_ATTR → encode with method flag=0
208-
Instruction::Real(RealInstruction::LoadAttr { idx }) => {
209-
let encoded = encode_load_attr_arg(idx.get(info.arg), false);
210-
info.arg = OpArg(encoded);
211-
info.instr = RealInstruction::LoadAttr { idx: Arg::marker() }.into();
212-
}
213235
// POP_BLOCK pseudo → NOP
214-
Instruction::Pseudo(PseudoInstruction::PopBlock) => {
236+
PseudoInstruction::PopBlock => {
215237
info.instr = RealInstruction::Nop.into();
216238
}
217239
// LOAD_SUPER_METHOD pseudo → LOAD_SUPER_ATTR (flags=0b11: method=1, class=1)
218-
Instruction::Pseudo(PseudoInstruction::LoadSuperMethod { idx }) => {
240+
PseudoInstruction::LoadSuperMethod { idx } => {
219241
let encoded = encode_load_super_attr_arg(idx.get(info.arg), true, true);
220242
info.arg = OpArg(encoded);
221243
info.instr = RealInstruction::LoadSuperAttr { arg: Arg::marker() }.into();
222244
}
223245
// LOAD_ZERO_SUPER_ATTR pseudo → LOAD_SUPER_ATTR (flags=0b00: method=0, class=0)
224-
Instruction::Pseudo(PseudoInstruction::LoadZeroSuperAttr { idx }) => {
246+
PseudoInstruction::LoadZeroSuperAttr { idx } => {
225247
let encoded = encode_load_super_attr_arg(idx.get(info.arg), false, false);
226248
info.arg = OpArg(encoded);
227249
info.instr = RealInstruction::LoadSuperAttr { arg: Arg::marker() }.into();
228250
}
229251
// LOAD_ZERO_SUPER_METHOD pseudo → LOAD_SUPER_ATTR (flags=0b01: method=1, class=0)
230-
Instruction::Pseudo(PseudoInstruction::LoadZeroSuperMethod { idx }) => {
252+
PseudoInstruction::LoadZeroSuperMethod { idx } => {
231253
let encoded = encode_load_super_attr_arg(idx.get(info.arg), true, false);
232254
info.arg = OpArg(encoded);
233255
info.instr = RealInstruction::LoadSuperAttr { arg: Arg::marker() }.into();
234256
}
235-
// LOAD_SUPER_ATTR → encode with flags=0b10 (method=0, class=1)
236-
Instruction::Real(RealInstruction::LoadSuperAttr { arg: idx }) => {
237-
let encoded = encode_load_super_attr_arg(idx.get(info.arg), false, true);
238-
info.arg = OpArg(encoded);
239-
info.instr = RealInstruction::LoadSuperAttr { arg: Arg::marker() }.into();
257+
PseudoInstruction::Jump { .. } => {
258+
// PseudoInstruction::Jump instructions are handled later
259+
}
260+
PseudoInstruction::JumpNoInterrupt { .. }
261+
| PseudoInstruction::Reserved258
262+
| PseudoInstruction::SetupCleanup
263+
| PseudoInstruction::SetupFinally
264+
| PseudoInstruction::SetupWith
265+
| PseudoInstruction::StoreFastMaybeNull => {
266+
unimplemented!("Got a placeholder pseudo instruction ({instr:?})")
240267
}
241-
_ => {}
242268
}
243269
}
244270
}

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

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -954,17 +954,41 @@ impl InstructionMetadata for Instruction {
954954
}
955955

956956
impl Instruction {
957-
pub const fn as_real(self) -> Option<RealInstruction> {
957+
/// Gets the inner value of [`Self::Real`].
958+
pub const fn real(self) -> Option<RealInstruction> {
958959
match self {
959960
Self::Real(ins) => Some(ins),
960961
_ => None,
961962
}
962963
}
963964

965+
/// Gets the inner value of [`Self::Pseudo`].
966+
pub const fn pseudo(self) -> Option<PseudoInstruction> {
967+
match self {
968+
Self::Pseudo(ins) => Some(ins),
969+
_ => None,
970+
}
971+
}
972+
973+
/// Same as [`Self::real`] but panics if wasn't called on [`Self::Real`].
974+
///
975+
/// # Panics
976+
///
977+
/// If was called on something else other than [`Self::Real`].
964978
pub const fn expect_real(self) -> RealInstruction {
965-
self.as_real()
979+
self.real()
966980
.expect("Expected Instruction::Real, found Instruction::Pseudo")
967981
}
982+
983+
/// Same as [`Self::pseudo`] but panics if wasn't called on [`Self::Pseudo`].
984+
///
985+
/// # Panics
986+
///
987+
/// If was called on something else other than [`Self::Pseudo`].
988+
pub const fn expect_pseudo(self) -> PseudoInstruction {
989+
self.pseudo()
990+
.expect("Expected Instruction::Pseudo, found Instruction::Real")
991+
}
968992
}
969993

970994
pub trait InstructionMetadata {

0 commit comments

Comments
 (0)