8000 Cleanup match statement codegen by arihant2math · Pull Request #5700 · RustPython/RustPython · GitHub
[go: up one dir, main page]

Skip to content

Cleanup match statement codegen #5700

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Apr 18, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Remove Instruction::IsOperation
  • Loading branch information
arihant2math committed Apr 15, 2025
commit 49b348cc7e6291c1e982183a8c76294024b26eca
6 changes: 4 additions & 2 deletions compiler/codegen/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2185,13 +2185,13 @@

let nargs = patterns.len();
let n_attrs = kwd_attrs.len();
let nkwd_patterns = kwd_patterns.len();

Check warning on line 2188 in compiler/codegen/src/compile.rs

View workflow job for this annotation

GitHub Actions / Check Rust code with rustfmt and clippy

Unknown word (nkwd)

// Validate that keyword attribute names and patterns match in length.
if n_attrs != nkwd_patterns {

Check warning on line 2191 in compiler/codegen/src/compile.rs

View workflow job for this annotation

GitHub Actions / Check Rust code with rustfmt and clippy

Unknown word (nkwd)
let msg = format!(
"kwd_attrs ({}) / kwd_patterns ({}) length mismatch in class pattern",
n_attrs, nkwd_patterns

Check warning on line 2194 in compiler/codegen/src/compile.rs

View workflow job for this annotation

GitHub Actions / Check Rust code with rustfmt and clippy

Unknown word (nkwd)
);
unreachable!("{}", msg);
}
Expand Down Expand Up @@ -2223,6 +2223,8 @@
});
}

use bytecode::TestOperator::*;

// Emit instructions:
// 1. Load the new tuple of attribute names.
self.emit_load_const(ConstantData::Tuple {
Expand All @@ -2235,7 +2237,7 @@
// 4. Load None.
self.emit_load_const(ConstantData::None);
// 5. Compare with IS_OP 1.
emit!(self, Instruction::IsOperation(true));
emit!(self, Instruction::TestOperation { op: IsNot });

// At this point the TOS is a tuple of (nargs + n_attrs) attributes (or None).
pc.on_top += 1;
Expand Down Expand Up @@ -2351,7 +2353,7 @@
// emit!(self, Instruction::CopyItem { index: 1_u32 });
// self.emit_load_const(ConstantData::None);
// // TODO: should be is
// emit!(self, Instruction::IsOperation(true));
// emit!(self, Instruction::TestOperation::IsNot);
// self.jump_to_fail_pop(pc, JumpOp::PopJumpIfFalse)?;

// // Unpack the tuple of values.
Expand Down Expand Up @@ -2481,10 +2483,10 @@

// Adjust the final captures.
let nstores = control.as_ref().unwrap().len();
let nrots = nstores + 1 + pc.on_top + pc.stores.len();

Check warning on line 2486 in compiler/codegen/src/compile.rs

View workflow job for this annotation

GitHub Actions / Check Rust code with rustfmt and clippy

Unknown word (nrots)
for i in 0..nstores {
// Rotate the capture to its proper place.
self.pattern_helper_rotate(nrots)?;

Check warning on line 2489 in compiler/codegen/src/compile.rs

View workflow job for this annotation

GitHub Actions / Check Rust code with rustfmt and clippy

Unknown word (nrots)
let name = &control.as_ref().unwrap()[i];
// Check for duplicate binding.
if pc.stores.iter().any(|n| n == name) {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 1 addition & 6 deletions compiler/core/src/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,9 +437,6 @@ pub enum Instruction {
TestOperation {
op: Arg<TestOperator>,
},
/// If the argument is true, perform IS NOT. Otherwise perform the IS operation.
// TODO: duplication of TestOperator::{Is,IsNot}. Fix later.
IsOperation(Arg<bool>),
CompareOperation {
op: Arg<ComparisonOperator>,
},
Expand Down Expand Up @@ -1227,8 +1224,7 @@ impl Instruction {
BinaryOperation { .. }
| BinaryOperationInplace { .. }
| TestOperation { .. }
| CompareOperation { .. }
| IsOperation(..) => -1,
| CompareOperation { .. } => -1,
BinarySubscript => -1,
CopyItem { .. } => 1,
Pop => -1,
Expand Down Expand Up @@ -1436,7 +1432,6 @@ impl Instruction {
BinarySubscript => w!(BinarySubscript),
LoadAttr { idx } => w!(LoadAttr, name = idx),
TestOperation { op } => w!(TestOperation, ?op),
IsOperation(neg) => w!(IsOperation, neg),
CompareOperation { op } => w!(CompareOperation, ?op),
CopyItem { index } => w!(CopyItem, index),
Pop => w!(Pop),
Expand Down
8 changes: 0 additions & 8 deletions vm/src/frame.rs
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -851,14 +851,6 @@ impl ExecutingFrame<'_> {
bytecode::Instruction::UnaryOperation { op } => self.execute_unary_op(vm, op.get(arg)),
bytecode::Instruction::TestOperation { op } => self.execute_test(vm, op.get(arg)),
bytecode::Instruction::CompareOperation { op } => self.execute_compare(vm, op.get(arg)),
bytecode::Instruction::IsOperation(neg) => {
let a = self.pop_value();
let b = self.pop_value();
// xor with neg to invert the result if needed
let result = vm.ctx.new_bool(a.is(b.as_ref()) ^ neg.get(arg));
self.push_value(result.into());
Ok(None)
}
bytecode::Instruction::ReturnValue => {
let value = self.pop_value();
self.unwind_blocks(vm, UnwindReason::Returning { value })
Expand Down
Loading
0