Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32.4k
gh-105481: refactor instr flag related code into a new InstructionFlags class#105950
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes from1 commit
Commits
Show all changes
5 commits Select commitHold shift + click to select a range
bb43ee8
gh-105481: refactor instr flag related code into a new InstructionFla…
iritkatriela08ac66
code review comments
iritkatriel8fcd65f
make InstructionFlags a dataclass
iritkatrield383eaa
remove __hash__ and __eq__
iritkatrielf34174d
hash the bitmap instead of the InstructionFlags object
iritkatrielFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
code review comments
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
commita08ac664c227730f1512b2397983dbb51b97cf21
There are no files selected for viewing
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 |
---|---|---|
@@ -238,38 +238,40 @@ def cast(self, dst: StackEffect, src: StackEffect) -> str: | ||
class InstructionFlags: | ||
iritkatriel marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
"""Construct and manipulate instruction flags""" | ||
INSTRUCTION_FLAGS = ["HAS_ARG", "HAS_CONST", "HAS_NAME", "HAS_JUMP"] | ||
INSTR_FLAG_SUFFIX = "_FLAG" | ||
def __init__( | ||
self, has_arg: bool, has_const: bool, has_name: bool, has_jump: bool | ||
): | ||
self.data = { | ||
"HAS_ARG": has_arg, | ||
"HAS_CONST": has_const, | ||
"HAS_NAME": has_name, | ||
"HAS_JUMP": has_jump, | ||
} | ||
assert set(self.data.keys()) == set(self.INSTRUCTION_FLAGS) | ||
@staticmethod | ||
def fromInstruction(instr: "AnyInstruction"): | ||
return InstructionFlags( | ||
has_arg=variable_used(instr, "oparg"), | ||
has_const=variable_used(instr, "FRAME_CO_CONSTS"), | ||
has_name=variable_used(instr, "FRAME_CO_NAMES"), | ||
has_jump=variable_used(instr, "JUMPBY"), | ||
) | ||
@staticmethod | ||
def newEmpty(): | ||
return InstructionFlags(False, False, False, False) | ||
def names(self) -> list[str]: | ||
return [ | ||
f"{name}{self.INSTR_FLAG_SUFFIX}" | ||
for name in self.INSTRUCTION_FLAGS | ||
if self.is_set(name) | ||
] | ||
def __eq__(self, other: "InstructionFlags") -> False: | ||
return self.bitmap() == other.bitmap() | ||
@@ -291,14 +293,14 @@ def is_set(self, name: str) -> bool: | ||
return self.data[name] | ||
@classmethod | ||
def emit_macros(cls,out: Formatter): | ||
for i, flag in enumerate(cls.INSTRUCTION_FLAGS): | ||
flag_name = f"{flag}{cls.INSTR_FLAG_SUFFIX}" | ||
out.emit(f"#define {flag_name} ({1 << i})"); | ||
for flag in cls.INSTRUCTION_FLAGS: | ||
flag_name = f"{flag}{cls.INSTR_FLAG_SUFFIX}" | ||
out.emit( | ||
f"#define OPCODE_{flag}(OP) " | ||
f"(_PyOpcode_opcode_metadata[(OP)].flags & ({flag_name}))") | ||
@@ -1206,9 +1208,13 @@ def write_pseudo_instrs(self) -> None: | ||
self.out.emit(f" ((OP) == {op}) || \\") | ||
self.out.emit(f" 0") | ||
def emit_metadata_entry( | ||
self, name: str, fmt: str, flags: InstructionFlags | ||
) -> None: | ||
if not (names := flags.names()): | ||
names.append("0") | ||
self.out.emit( | ||
f" [{name}] = {{ true, {INSTR_FMT_PREFIX}{fmt}, {' | '.join(names)} }}," | ||
) | ||
def write_metadata_for_inst(self, instr: Instruction) -> None: | ||
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.