FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Autogen instructions & opcodes by ShaharNaveh · Pull Request #7797 · RustPython/RustPython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
fef345f
Base conf
ShaharNaveh May 5, 2026
09eb0b0
Basic layout
ShaharNaveh May 5, 2026
1ddf444
to from numeric
ShaharNaveh May 5, 2026
1e0c01b
generate has attr
ShaharNaveh May 5, 2026
c66eaf6
Basic instrumented
ShaharNaveh May 5, 2026
d1f0982
sort instrumeneted
ShaharNaveh May 5, 2026
5f07a17
deopt
ShaharNaveh May 5, 2026
bea434e
Cache entries
ShaharNaveh May 5, 2026
8768c9f
simplify
ShaharNaveh May 5, 2026
b9f1941
stack effect
ShaharNaveh May 5, 2026
566c0cf
Simplify conf structure
ShaharNaveh May 6, 2026
0b93378
Switch to numeric_repr
ShaharNaveh May 6, 2026
9ff0a7a
Add oparg conf
ShaharNaveh May 6, 2026
9c74715
`as_opcode()`
ShaharNaveh May 6, 2026
9e6382a
as_{opcode,instruction}
ShaharNaveh May 7, 2026
9f88f88
save progress
ShaharNaveh May 7, 2026
7669f0c
label_arg
ShaharNaveh May 7, 2026
e6d07b1
Compiler core compiles
ShaharNaveh May 7, 2026
99ba1db
More things to compile
ShaharNaveh May 7, 2026
9ce43ac
codegen compiles
ShaharNaveh May 7, 2026
d662a99
add must_use
ShaharNaveh May 7, 2026
0e35818
Overridable stack_effect
ShaharNaveh May 7, 2026
b58d736
fix stack_effect
ShaharNaveh May 7, 2026
74addfd
fix more in frame.rs
ShaharNaveh May 7, 2026
606a5aa
vm compiles
ShaharNaveh May 8, 2026
4ed7659
stdlib compile
ShaharNaveh May 8, 2026
0bc0d9b
_opcode `has_X` ignore deopt
ShaharNaveh May 8, 2026
a42b005
Align methods
ShaharNaveh May 8, 2026
09925bb
save
ShaharNaveh May 8, 2026
428ad29
fix `has_exc`
ShaharNaveh May 8, 2026
92e43e3
clippy
ShaharNaveh May 8, 2026
34268ce
Align script
ShaharNaveh May 8, 2026
9d909a3
clippy
ShaharNaveh May 8, 2026
f9358bf
Simplify code
ShaharNaveh May 9, 2026
d90b993
Fix compiles
ShaharNaveh May 9, 2026
File filter

Filter by extension

Filter by extension .py  (1) .rs  (3) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
Prev Previous commit
Next Next commit
Simplify code
  • Loading branch information
ShaharNaveh committed May 9, 2026
commit f9358bf0cd82063ce5a7c0ff160d01450fe94dfe
49 changes: 23 additions & 26 deletions crates/compiler-core/generate.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -158,33 +158,35 @@ def build_has_attr_fn(self, fn_attr: str, prop_attr: str, doc_flag: str) -> str:
lambda self: self.build_has_attr_fn("local", "uses_locals", "HAS_LOCAL_FLAG")
)

fn_has_exc = property(
lambda self: self.build_has_attr_fn("exc", "pure", "HAS_PURE_FLAG")
)

@property
def instrumented(self) -> list:
return [instr for instr in self if instr.name.startswith("Instrumented")]

@property
def fn_to_base(self) -> str:
inames = {instr.name for instr in self.instrumented}
def instrumented_mapping(self) -> dict[str, str]:
inames = {instr.name for instr in self if instr.name.startswith("Instrumented")}
names = {instr.name for instr in self} - inames

arms = ""
res = {}
for iname in sorted(inames):
name = iname.removeprefix("Instrumented")
if name not in names:
continue
arms += f"Self::{iname} => Self::{name},\n"

res[name] = iname

return res

@property
def fn_to_base(self) -> str:
arms = ",\n".join(
f"Self::{iname} => Self::{name}"
for name, iname in self.instrumented_mapping.items()
)

arms = arms.strip()
if not arms:
inner = "None"
else:
inner = f"""
Some(match self {{
{arms}
{arms},
_ => return None,

}})
Expand All @@ -199,23 +201,18 @@ def fn_to_base(self) -> str:

@property
def fn_to_instrumented(self) -> str:
inames = {instr.name for instr in self.instrumented}
names = {instr.name for instr in self} - inames

arms = ""
for iname in sorted(inames):
name = iname.removeprefix("Instrumented")
if name not in names:
continue
arms += f"Self::{name} => Self::{iname},\n"
arms = ",\n".join(
f"Self::{name} => Self::{iname}"
for name, iname in self.instrumented_mapping.items()
)

arms = arms.strip()
if not arms:
inner = "None"
else:
inner = f"""
Some(match self {{
{arms}
{arms},
_ => return None,

}})
Expand Down Expand Up @@ -334,7 +331,7 @@ def fn_stack_effect_info(self) -> str:
arms += f"""
Self::{name} => (
{pushed}, {pushed_comment}
{popped} {popped_comment}
{popped}, {popped_comment}
),
""".strip()

Expand Down Expand Up @@ -413,7 +410,7 @@ def __iter__(self):


@dataclasses.dataclass(frozen=True, kw_only=True, slots=True)
class InstructioneGen:
class InstructionGen:
name: str
opcode_enum: str
instructions: list
Expand Down Expand Up @@ -686,7 +683,7 @@ def main():

outfile.write(opcode_code)

instruction_code = InstructioneGen(
instruction_code = InstructionGen(
name=instruction_enum,
opcode_enum=opcode_enum,
instructions=instructions,
Expand Down
5 changes: 0 additions & 5 deletions crates/compiler-core/src/bytecode/instruction.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -447,11 +447,6 @@ impl AnyOpcode {
pub const fn has_const(&self) -> bool
);

either_real_pseudo!(
#[must_use]
pub const fn has_exc(&self) -> bool
);

either_real_pseudo!(
#[must_use]
pub const fn is_instrumented(&self) -> bool
Expand Down
33 changes: 0 additions & 33 deletions crates/compiler-core/src/bytecode/instructions.rs

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

feeling like this could be written using macro_rules and embed opcode.toml in the file. not confident though

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

We can always revert this PR and do that

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1094,25 +1094,6 @@ impl Opcode {
)
}

/// Does this opcode have 'HAS_PURE_FLAG' set.
#[must_use]
pub const fn has_exc(self) -> bool {
matches!(
self,
Self::EndSend
| Self::Nop
| Self::NotTaken
| Self::PopIter
| Self::PopTop
| Self::PushNull
| Self::UnaryNot
| Self::Copy
| Self::LoadFast
| Self::LoadFastBorrow
| Self::Swap
)
}

/// Does this opcode have 'HAS_FREE_FLAG' set.
#[must_use]
pub const fn has_free(self) -> bool {
Expand Down Expand Up @@ -2561,20 +2542,6 @@ impl PseudoOpcode {
false
}

/// Does this opcode have 'HAS_PURE_FLAG' set.
#[must_use]
pub const fn has_exc(self) -> bool {
matches!(
self,
Self::AnnotationsPlaceholder
| Self::LoadClosure
| Self::PopBlock
| Self::SetupCleanup
| Self::SetupFinally
| Self::SetupWith
)
}

/// Does this opcode have 'HAS_FREE_FLAG' set.
#[must_use]
pub const fn has_free(self) -> bool {
Expand Down
14 changes: 7 additions & 7 deletions crates/stdlib/src/_opcode.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mod _opcode {
use crate::vm::{
AsObject, PyObjectRef, PyResult, VirtualMachine,
builtins::{PyInt, PyIntRef},
bytecode::{AnyInstruction, AnyOpcode, oparg},
bytecode::{AnyOpcode, oparg},
};

fn try_from_i32(raw: i32) -> Result<AnyOpcode, ()> {
Expand Down Expand Up @@ -71,7 +71,7 @@ mod _opcode {
.opcode
.try_to_primitive::<u16>(vm)
.and_then(|v| {
AnyInstruction::try_from(v)
AnyOpcode::try_from(v)
.map_err(|_| vm.new_exception_empty(vm.ctx.exceptions.value_error.to_owned()))
})
.map_err(|_| vm.new_value_error("invalid opcode or oparg"))?;
Expand Down Expand Up @@ -105,35 +105,35 @@ mod _opcode {
#[pyfunction]
fn has_const(opcode: i32) -> bool {
try_from_i32(opcode)
.map(|op| op.deopt().is_none() && op.has_const())
.map(|op| op.has_const())
.unwrap_or(false)
}

#[pyfunction]
fn has_name(opcode: i32) -> bool {
try_from_i32(opcode)
.map(|op| op.deopt().is_none() && op.has_name())
.map(|op| &&op.has_name())
.unwrap_or(false)
}

#[pyfunction]
fn has_jump(opcode: i32) -> bool {
try_from_i32(opcode)
.map(|op| op.deopt().is_none() && op.has_jump())
.map(|op| op.has_jump())
.unwrap_or(false)
}

#[pyfunction]
fn has_free(opcode: i32) -> bool {
try_from_i32(opcode)
.map(|op| op.deopt().is_none() && op.has_free())
.map(|op| op.has_free())
.unwrap_or(false)
}

#[pyfunction]
fn has_local(opcode: i32) -> bool {
try_from_i32(opcode)
.map(|op| op.deopt().is_none() && op.has_local())
.map(|op| op.has_local())
.unwrap_or(false)
}

Expand Down
Loading

Back | FazBrowse Home | New Git URL