gh-98831: Clean up and add cache size static_assert to macro (#101442)

This commit is contained in:
Guido van Rossum 2023-01-30 17:27:51 -08:00 committed by GitHub
parent af7b2db384
commit 04ab767d28
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 4 deletions

View file

@ -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
@ -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
@ -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(
@ -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) {{")
@ -1031,6 +1037,7 @@ 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:
@ -1038,12 +1045,24 @@ def write_macro(self, mac: MacroInstruction) -> None:
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."""

View file

@ -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();