Skip to content

Commit

Permalink
pythongh-98831: Clean up and add cache size static_assert to macro (p…
Browse files Browse the repository at this point in the history
  • Loading branch information
gvanrossum authored and mdboom committed Jan 31, 2023
1 parent e30789d commit f8fef21
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
27 changes: 23 additions & 4 deletions Tools/cases_generator/generate_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ def map_families(self) -> None:
self.error(
f"Instruction {member} is a member of multiple families "
f"({member_instr.family.name}, {family.name}).",
family
family,
)
else:
member_instr.family = family
Expand All @@ -609,7 +609,7 @@ def map_families(self) -> None:
f"Component {part.instr.name} of macro {member} "
f"is a member of multiple families "
f"({part.instr.family.name}, {family.name}).",
family
family,
)
else:
part.instr.family = family
Expand All @@ -629,7 +629,11 @@ def check_families(self) -> None:
for family in self.families.values():
if len(family.members) < 2:
self.error(f"Family {family.name!r} has insufficient members", family)
members = [member for member in family.members if member in self.instrs or member in self.macro_instrs]
members = [
member
for member in family.members
if member in self.instrs or member in self.macro_instrs
]
if members != family.members:
unknown = set(family.members) - set(members)
self.error(
Expand Down Expand Up @@ -859,7 +863,9 @@ def write_stack_effect_functions(self) -> None:
popped_data.append((instr, popped))
pushed_data.append((instr, pushed))

def write_function(direction: str, data: list[tuple[AnyInstruction, str]]) -> None:
def write_function(
direction: str, data: list[tuple[AnyInstruction, str]]
) -> None:
self.out.emit("\n#ifndef NDEBUG")
self.out.emit("static int")
self.out.emit(f"_PyOpcode_num_{direction}(int opcode, int oparg) {{")
Expand Down Expand Up @@ -1031,19 +1037,32 @@ def write_super(self, sup: SuperInstruction) -> None:

def write_macro(self, mac: MacroInstruction) -> None:
"""Write code for a macro instruction."""
last_instr: Instruction | None = None
with self.wrap_super_or_macro(mac):
cache_adjust = 0
for part in mac.parts:
match part:
case parser.CacheEffect(size=size):
cache_adjust += size
case Component() as comp:
last_instr = comp.instr
comp.write_body(self.out, cache_adjust)
cache_adjust += comp.instr.cache_offset

if cache_adjust:
self.out.emit(f"JUMPBY({cache_adjust});")

if (
last_instr
and (family := last_instr.family)
and mac.name == family.members[0]
and (cache_size := family.size)
):
self.out.emit(
f"static_assert({cache_size} == "
f'{cache_adjust}, "incorrect cache size");'
)

@contextlib.contextmanager
def wrap_super_or_macro(self, up: SuperOrMacroInstruction):
"""Shared boilerplate for super- and macro instructions."""
Expand Down
1 change: 1 addition & 0 deletions Tools/cases_generator/test_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@ def test_macro_instruction():
_tmp_3 = res;
}
JUMPBY(5);
static_assert(INLINE_CACHE_ENTRIES_OP == 5, "incorrect cache size");
STACK_SHRINK(2);
POKE(1, _tmp_3);
DISPATCH();
Expand Down

0 comments on commit f8fef21

Please sign in to comment.