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  (1) 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
to from numeric
  • Loading branch information
ShaharNaveh committed May 7, 2026
commit 1ddf4440e060e89bcceee8dd6331443b81589d06
125 changes: 122 additions & 3 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
@@ -1,4 +1,5 @@
#!/usr/bin/env python
import dataclasses
import io
import os
import pathlib
Expand Down Expand Up @@ -26,6 +27,98 @@
from generators_common import DEFAULT_INPUT


@dataclasses.dataclass(frozen=True, kw_only=True, slots=True)
class OpcodeGen:
name: str
instructions: list
numeric_repr: str

def gen(self) -> str:
variants = ",\n".join(instr.name for instr in self.instructions)

methods = "\n\n".join(
getattr(self, attr).strip()
for attr in sorted(dir(self))
if attr.startswith("fn_")
)

impls = "\n\n".join(
getattr(self, attr).strip()
for attr in sorted(dir(self))
if attr.startswith("impl_")
)

return f"""
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum {self.name} {{
{variants}
}}

impl {self.name} {{
{methods}
}}

{impls}
"""

@property
def fn_as_numeric(self) -> str:
arms = ",\n".join(
f"Self::{instr.name} => {instr.opcode}" for instr in self.instructions
)
return f"""
#[must_use]
pub const fn as_{self.numeric_repr}(self) -> {self.numeric_repr} {{
match self {{
{arms},
}}
}}
"""

@property
def fn_tryfrom_numeric(self) -> str:
arms = ",\n".join(
f"{instr.opcode} => Self::{instr.name}" for instr in self.instructions
)
return f"""
#[must_use]
pub const fn from_{self.numeric_repr}(
value: {self.numeric_repr}
) -> Result<Self, crate::marshal::MarshalError> {{
Ok(match value {{
{arms},
_ => return Err(crate::MarshalError::InvalidBytecode),
}})
}}
"""

@property
def impl_tryfrom_numeric(self) -> str:
return f"""
impl TryFrom<{self.numeric_repr}> for {self.name} {{
type Error = crate::marshal::MarshalError;

fn try_from(value: {self.numeric_repr}) -> Result<Self, Self::Error> {{
Self::from_{self.numeric_repr}(value)
}}
}}
"""

@property
def impl_into_numeric(self) -> str:
return f"""
impl From<{self.name}> for {self.numeric_repr}{{
fn from(opcode: {self.name}) -> Self {{
opcode.as_{self.numeric_repr}()
}}
}}
"""


def to_pascal_case(s: str) -> str:
return s.title().replace("_", "")


def get_analysis() -> analyzer.Analysis:
analysis = analyzer.analyze_files([DEFAULT_INPUT])

Expand All @@ -43,10 +136,36 @@ def main():

analysis = get_analysis()

outfile = io.StringIO()
opcode_conf = CONF["Opcodes"]
opcodes_conf = CONF["Opcodes"]
instructions_conf = CONF["Instructions"]

generated = ""
outfile = io.StringIO()
for key, conf in opcodes_conf.items():
opcode_enum_name = conf["opcode_enum_name"]
numeric_repr = conf["numeric_repr"]

opcode_range = conf["range"]
lower, upper = map(int, (opcode_range["min"], opcode_range["max"]))
bounds = range(lower, upper + 1)

instructions = sorted(
(
instr
for instr in analysis.instructions.values()
if instr.opcode in bounds
),
key=lambda x: x.opcode,
)

for instr in instructions:
instr.name = to_pascal_case(instr.name)

code = OpcodeGen(
name=opcode_enum_name, instructions=instructions, numeric_repr=numeric_repr
).gen()
outfile.write(code)

generated = outfile.getvalue()

script_path = pathlib.Path(__file__).resolve().relative_to(ROOT).as_posix()

Expand Down
Loading

Back | FazBrowse Home | New Git URL