Skip to content

Commit 6ea6985

Browse files
committed
Auto-generate has_eval_break and route AnyInstruction has_arg/has_const via macro
Add fn_has_eval_break to generate_rs_opcode_metadata.py using CPython's Properties.eval_breaker, removing the hand-written matches! body for Opcode::has_eval_break and PseudoOpcode::has_eval_break. Forward has_arg/has_const from Instruction and PseudoInstruction to their opcode, so AnyInstruction can use either_real_pseudo! like the other has_* accessors instead of an open-coded match.
1 parent bd9c09c commit 6ea6985

3 files changed

Lines changed: 60 additions & 50 deletions

File tree

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

Lines changed: 18 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,16 @@ macro_rules! define_opcodes {
171171
self.as_opcode().has_jump()
172172
}
173173

174+
#[must_use]
175+
$instr_vis const fn has_arg(&self) -> bool {
176+
self.as_opcode().has_arg()
177+
}
178+
179+
#[must_use]
180+
$instr_vis const fn has_const(&self) -> bool {
181+
self.as_opcode().has_const()
182+
}
183+
174184
#[must_use]
175185
$instr_vis const fn has_eval_break(&self) -> bool {
176186
self.as_opcode().has_eval_break()
@@ -730,36 +740,6 @@ impl Opcode {
730740
self.has_jump() || self.is_block_push()
731741
}
732742

733-
/// Does this opcode have CPython's `HAS_EVAL_BREAK_FLAG` set.
734-
#[must_use]
735-
pub const fn has_eval_break(&self) -> bool {
736-
matches!(
737-
self,
738-
Self::Call
739-
| Self::CallBuiltinClass
740-
| Self::CallBuiltinFast
741-
| Self::CallBuiltinFastWithKeywords
742-
| Self::CallBuiltinO
743-
| Self::CallFunctionEx
744-
| Self::CallKwNonPy
745-
| Self::CallMethodDescriptorFast
746-
| Self::CallMethodDescriptorFastWithKeywords
747-
| Self::CallMethodDescriptorNoargs
748-
| Self::CallMethodDescriptorO
749-
| Self::CallNonPyGeneral
750-
| Self::CallStr1
751-
| Self::CallTuple1
752-
| Self::InstrumentedCall
753-
| Self::InstrumentedCallFunctionEx
754-
| Self::InstrumentedJumpBackward
755-
| Self::InstrumentedResume
756-
| Self::JumpBackward
757-
| Self::JumpBackwardJit
758-
| Self::JumpBackwardNoJit
759-
| Self::Resume
760-
)
761-
}
762-
763743
#[must_use]
764744
pub const fn is_block_push(&self) -> bool {
765745
false
@@ -802,12 +782,6 @@ impl PseudoOpcode {
802782
matches!(self, Self::Jump | Self::JumpNoInterrupt)
803783
}
804784

805-
/// Does this pseudo opcode have CPython's `HAS_EVAL_BREAK_FLAG` set.
806-
#[must_use]
807-
pub const fn has_eval_break(&self) -> bool {
808-
matches!(self, Self::Jump)
809-
}
810-
811785
#[must_use]
812786
pub const fn is_assembler(&self) -> bool {
813787
false
@@ -925,21 +899,15 @@ impl AnyInstruction {
925899
pub const fn has_jump(&self) -> bool
926900
);
927901

928-
#[must_use]
929-
pub const fn has_arg(&self) -> bool {
930-
match self {
931-
Self::Real(instr) => instr.as_opcode().has_arg(),
932-
Self::Pseudo(instr) => instr.as_opcode().has_arg(),
933-
}
934-
}
902+
either_real_pseudo!(
903+
#[must_use]
904+
pub const fn has_arg(&self) -> bool
905+
);
935906

936-
#[must_use]
937-
pub const fn has_const(&self) -> bool {
938-
match self {
939-
Self::Real(instr) => instr.as_opcode().has_const(),
940-
Self::Pseudo(instr) => instr.as_opcode().has_const(),
941-
}
942-
}
907+
either_real_pseudo!(
908+
#[must_use]
909+
pub const fn has_const(&self) -> bool
910+
);
943911

944912
either_real_pseudo!(
945913
#[must_use]

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

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tools/opcode_metadata/generate_rs_opcode_metadata.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ def fn_has_free(self) -> str:
6666
def fn_has_local(self) -> str:
6767
return self.gen_fn_has_attr("has_local", "uses_locals", "HAS_LOCAL_FLAG")
6868

69+
@property
70+
def fn_has_eval_break(self) -> str:
71+
return self.gen_fn_has_attr(
72+
"has_eval_break", "eval_breaker", "HAS_EVAL_BREAK_FLAG"
73+
)
74+
6975
@property
7076
def fn_is_instrumented(self) -> str:
7177
arms = "|".join(

0 commit comments

Comments
 (0)