Thanks to visit codestin.com
Credit goes to github.com

Skip to content

gh-112519: Make it possible to specify instruction flags for pseudo instructions in bytecodes.c #112520

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 4 additions & 4 deletions Include/internal/pycore_opcode_metadata.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions Lib/test/test_generated_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,44 @@ def test_macro_instruction(self):
"""
self.run_cases_test(input, output)

def test_pseudo_instruction_no_flags(self):
input = """
pseudo(OP) = {
OP1,
};

inst(OP1, (--)) {
}
"""
output = """
TARGET(OP1) {
frame->instr_ptr = next_instr;
next_instr += 1;
INSTRUCTION_STATS(OP1);
DISPATCH();
}
"""
self.run_cases_test(input, output)

def test_pseudo_instruction_with_flags(self):
input = """
pseudo(OP, (HAS_ARG, HAS_JUMP)) = {
OP1,
};

inst(OP1, (--)) {
}
"""
output = """
TARGET(OP1) {
frame->instr_ptr = next_instr;
next_instr += 1;
INSTRUCTION_STATS(OP1);
DISPATCH();
}
"""
self.run_cases_test(input, output)

def test_array_input(self):
input = """
inst(OP, (below, values[oparg*2], above --)) {
Expand Down
6 changes: 3 additions & 3 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -2831,15 +2831,15 @@ dummy_func(
ERROR_IF(res == NULL, error);
}

pseudo(SETUP_FINALLY) = {
pseudo(SETUP_FINALLY, (HAS_ARG)) = {
NOP,
};

pseudo(SETUP_CLEANUP) = {
pseudo(SETUP_CLEANUP, (HAS_ARG)) = {
NOP,
};

pseudo(SETUP_WITH) = {
pseudo(SETUP_WITH, (HAS_ARG)) = {
NOP,
};

Expand Down
2 changes: 1 addition & 1 deletion Python/flowgraph.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ static const jump_target_label NO_LABEL = {-1};
static inline int
is_block_push(cfg_instr *i)
{
assert(OPCODE_HAS_ARG(i->i_opcode) || !IS_BLOCK_PUSH_OPCODE(i->i_opcode));
return IS_BLOCK_PUSH_OPCODE(i->i_opcode);
}

Expand Down Expand Up @@ -2239,7 +2240,6 @@ convert_pseudo_ops(basicblock *entryblock)
for (int i = 0; i < b->b_iused; i++) {
cfg_instr *instr = &b->b_instr[i];
if (is_block_push(instr) || instr->i_opcode == POP_BLOCK) {
assert(SAME_OPCODE_METADATA(instr->i_opcode, NOP));
INSTR_SET_OP0(instr, NOP);
}
else if (instr->i_opcode == LOAD_CLOSURE) {
Expand Down
9 changes: 7 additions & 2 deletions Tools/cases_generator/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,9 +390,14 @@ def analyze_pseudo(self, pseudo: parsing.Pseudo) -> PseudoInstruction:
else:
targets.append(self.macro_instrs[target_name])
assert targets
ignored_flags = {"HAS_EVAL_BREAK_FLAG", "HAS_DEOPT_FLAG", "HAS_ERROR_FLAG", "HAS_ESCAPES_FLAG"}
ignored_flags = {"HAS_EVAL_BREAK_FLAG", "HAS_DEOPT_FLAG", "HAS_ERROR_FLAG",
"HAS_ESCAPES_FLAG"}
assert len({t.instr_flags.bitmap(ignore=ignored_flags) for t in targets}) == 1
return PseudoInstruction(pseudo.name, targets, targets[0].instr_flags)

flags = InstructionFlags(**{f"{f}_FLAG" : True for f in pseudo.flags})
for t in targets:
flags.add(t.instr_flags)
return PseudoInstruction(pseudo.name, targets, flags)

def analyze_instruction(
self, instr: Instruction, offset: int
Expand Down
25 changes: 23 additions & 2 deletions Tools/cases_generator/parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ class Family(Node):
@dataclass
class Pseudo(Node):
name: str
targets: list[str] # opcodes this can be replaced by
flags: list[str] # instr flags to set on the pseudo instruction
targets: list[str] # opcodes this can be replaced by


class Parser(PLexer):
Expand Down Expand Up @@ -374,19 +375,39 @@ def family_def(self) -> Family | None:
)
return None

def flags(self) -> list[str]:
here = self.getpos()
if self.expect(lx.LPAREN):
if tkn := self.expect(lx.IDENTIFIER):
flags = [tkn.text]
while self.expect(lx.COMMA):
if tkn := self.expect(lx.IDENTIFIER):
flags.append(tkn.text)
else:
break
if not self.expect(lx.RPAREN):
raise self.make_syntax_error("Expected comma or right paren")
return flags
self.setpos(here)
return []

@contextual
def pseudo_def(self) -> Pseudo | None:
if (tkn := self.expect(lx.IDENTIFIER)) and tkn.text == "pseudo":
size = None
if self.expect(lx.LPAREN):
if tkn := self.expect(lx.IDENTIFIER):
if self.expect(lx.COMMA):
flags = self.flags()
else:
flags = []
if self.expect(lx.RPAREN):
if self.expect(lx.EQUALS):
if not self.expect(lx.LBRACE):
raise self.make_syntax_error("Expected {")
if members := self.members():
if self.expect(lx.RBRACE) and self.expect(lx.SEMI):
return Pseudo(tkn.text, members)
return Pseudo(tkn.text, flags, members)
return None

def members(self) -> list[str] | None:
Expand Down