Skip to content

Commit

Permalink
Restore override behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
markshannon committed Nov 3, 2023
1 parent fde5f18 commit 140a681
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 9 deletions.
45 changes: 43 additions & 2 deletions Lib/test/test_generated_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -666,9 +666,50 @@ def test_macro_push_push(self):
"""
self.run_cases_test(input, output)

def test_override_inst(self):
input = """
inst(OP, (--)) {
spam();
}
override inst(OP, (--)) {
ham();
}
"""
output = """
TARGET(OP) {
frame->instr_ptr = next_instr;
next_instr += 1;
INSTRUCTION_STATS(OP);
ham();
DISPATCH();
}
"""
self.run_cases_test(input, output)

def test_override_op(self):
input = """
op(OP, (--)) {
spam();
}
macro(M) = OP;
override op(OP, (--)) {
ham();
}
"""
output = """
TARGET(M) {
frame->instr_ptr = next_instr;
next_instr += 1;
INSTRUCTION_STATS(M);
ham();
DISPATCH();
}
"""
self.run_cases_test(input, output)

def test_annotated_inst(self):
input = """
annotation inst(OP, (--)) {
guard inst(OP, (--)) {
ham();
}
"""
Expand All @@ -685,7 +726,7 @@ def test_annotated_inst(self):

def test_annotated_op(self):
input = """
annotation op(OP, (--)) {
guard op(OP, (--)) {
spam();
}
macro(M) = OP;
Expand Down
1 change: 1 addition & 0 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@

/* Annotations */
#define guard
#define override
#define specializing

// Dummy variables for stack effects.
Expand Down
14 changes: 11 additions & 3 deletions Tools/cases_generator/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,20 @@ def parse_file(self, filename: str, instrs_idx: dict[str, int]) -> None:
match thing:
case parsing.InstDef(name=name):
macro: parsing.Macro | None = None
if thing.kind == "inst":
if thing.kind == "inst" and not thing.annotation == "override":
macro = parsing.Macro(name, [parsing.OpName(name)])
if name in self.instrs:
if not thing.annotation == "override":
raise psr.make_syntax_error(
f"Duplicate definition of '{name}' @ {thing.context} "
f"previous definition @ {self.instrs[name].inst.context}",
thing_first_token,
)
self.everything[instrs_idx[name]] = thing
if name not in self.instrs and thing.annotation == "override":
raise psr.make_syntax_error(
f"Duplicate definition of '{name}' @ {thing.context} "
f"previous definition @ {self.instrs[name].inst.context}",
f"Definition of '{name}' @ {thing.context} is supposed to be "
"an override but no previous definition exists.",
thing_first_token,
)
self.instrs[name] = Instruction(thing)
Expand Down
8 changes: 4 additions & 4 deletions Tools/cases_generator/lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,19 +206,19 @@ def choice(*opts: str) -> str:
kwds.append(VOLATILE)
WHILE = "WHILE"
kwds.append(WHILE)
#An instruction in the DSL
# An instruction in the DSL
INST = "INST"
kwds.append(INST)
#A micro-op in the DSL
# A micro-op in the DSL
OP = "OP"
kwds.append(OP)
#A macro in the DSL
# A macro in the DSL
MACRO = "MACRO"
kwds.append(MACRO)
keywords = {name.lower(): name for name in kwds}

ANNOTATION = "ANNOTATION"
annotations = {"specializing", "guard"}
annotations = {"specializing", "guard", "override", "register"}

__all__ = []
__all__.extend(kwds)
Expand Down

0 comments on commit 140a681

Please sign in to comment.