Skip to content
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

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Basic instrumented
  • Loading branch information
ShaharNaveh committed May 7, 2026
commit c66eaf695d672ca7f206c8e40b2db6d1a2aecf47
106 changes: 96 additions & 10 deletions crates/compiler-core/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ class OpcodeGen:
name: str
instructions: list
numeric_repr: str
analysis: analyzer.Analysis

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

methods = "\n\n".join(
getattr(self, attr).strip()
Expand Down Expand Up @@ -63,9 +64,7 @@ def gen(self) -> str:

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

@property
def fn_tryfrom_numeric(self) -> str:
arms = ",\n".join(
f"{instr.opcode} => Self::{instr.name}" for instr in self.instructions
)
arms = ",\n".join(f"{instr.opcode} => Self::{instr.name}" for instr in self)
return f"""
#[must_use]
pub const fn from_{self.numeric_repr}(
Expand Down Expand Up @@ -114,10 +111,10 @@ def impl_into_numeric(self) -> str:
}}
"""

def build_has_attr_fn(self, fn_attr: str, prop_attr: str, doc_flag: str):
def build_has_attr_fn(self, fn_attr: str, prop_attr: str, doc_flag: str) -> str:
arms = "|".join(
f"Self::{instr.name}"
for instr in self.instructions
for instr in self
if getattr(instr.properties, prop_attr)
)

Expand Down Expand Up @@ -162,6 +159,91 @@ def build_has_attr_fn(self, fn_attr: str, prop_attr: str, doc_flag: str):
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}
names = {instr.name for instr in self} - inames

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

arms = arms.strip()
if not arms:
return ""

inner = f"""
Some(match self {{
{arms}
_ => return None,

}})
"""

return f"""
#[must_use]
pub const fn to_base(self) -> Option<Self> {{
{inner}
}}
"""

@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 inames:
name = iname.removeprefix("Instrumented")
if name not in names:
continue
arms += f"Self::{name} => Self::{iname},\n"

arms = arms.strip()
if not arms:
return ""

inner = f"""
Some(match self {{
{arms}
_ => return None,

}})
"""

return f"""
#[must_use]
pub const fn to_instrumented(self) -> Option<Self> {{
{inner}
}}
"""

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

for family in self.analysis.families.values():
family_name = to_pascal_case(family.name)
if family_name not in names:
continue
for member in family.members:
if member.name == family.name:
continue

print(family_name, member.name)

return ""

def __iter__(self):
yield from self.instructions


def to_pascal_case(s: str) -> str:
return s.title().replace("_", "")
Expand Down Expand Up @@ -209,8 +291,12 @@ def main():
instr.name = to_pascal_case(instr.name)

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

outfile.write(code)

generated = outfile.getvalue()
Expand Down
52 changes: 52 additions & 0 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.

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.

We can always revert this PR and do that

Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,58 @@ impl Opcode {
)
}

#[must_use]
pub const fn to_base(self) -> Option<Self> {
Some(match self {
Self::InstrumentedForIter => Self::ForIter,
Self::InstrumentedYieldValue => Self::YieldValue,
Self::InstrumentedNotTaken => Self::NotTaken,
Self::InstrumentedEndSend => Self::EndSend,
Self::InstrumentedJumpBackward => Self::JumpBackward,
Self::InstrumentedResume => Self::Resume,
Self::InstrumentedJumpForward => Self::JumpForward,
Self::InstrumentedPopJumpIfTrue => Self::PopJumpIfTrue,
Self::InstrumentedReturnValue => Self::ReturnValue,
Self::InstrumentedCallFunctionEx => Self::CallFunctionEx,
Self::InstrumentedPopJumpIfFalse => Self::PopJumpIfFalse,
Self::InstrumentedPopJumpIfNone => Self::PopJumpIfNone,
Self::InstrumentedEndFor => Self::EndFor,
Self::InstrumentedPopJumpIfNotNone => Self::PopJumpIfNotNone,
Self::InstrumentedCall => Self::Call,
Self::InstrumentedEndAsyncFor => Self::EndAsyncFor,
Self::InstrumentedPopIter => Self::PopIter,
Self::InstrumentedCallKw => Self::CallKw,
Self::InstrumentedLoadSuperAttr => Self::LoadSuperAttr,
_ => return None,
})
}

#[must_use]
pub const fn to_instrumented(self) -> Option<Self> {
Some(match self {
Self::ForIter => Self::InstrumentedForIter,
Self::YieldValue => Self::InstrumentedYieldValue,
Self::NotTaken => Self::InstrumentedNotTaken,
Self::EndSend => Self::InstrumentedEndSend,
Self::JumpBackward => Self::InstrumentedJumpBackward,
Self::Resume => Self::InstrumentedResume,
Self::JumpForward => Self::InstrumentedJumpForward,
Self::PopJumpIfTrue => Self::InstrumentedPopJumpIfTrue,
Self::ReturnValue => Self::InstrumentedReturnValue,
Self::CallFunctionEx => Self::InstrumentedCallFunctionEx,
Self::PopJumpIfFalse => Self::InstrumentedPopJumpIfFalse,
Self::PopJumpIfNone => Self::InstrumentedPopJumpIfNone,
Self::EndFor => Self::InstrumentedEndFor,
Self::PopJumpIfNotNone => Self::InstrumentedPopJumpIfNotNone,
Self::Call => Self::InstrumentedCall,
Self::EndAsyncFor => Self::InstrumentedEndAsyncFor,
Self::PopIter => Self::InstrumentedPopIter,
Self::CallKw => Self::InstrumentedCallKw,
Self::LoadSuperAttr => Self::InstrumentedLoadSuperAttr,
_ => return None,
})
}

#[must_use]
pub const fn from_u8(value: u8) -> Result<Self, crate::marshal::MarshalError> {
Ok(match value {
Expand Down