Path: blob/main/Tools/cases_generator/generate_cases.py
12 views
"""Generate the main interpreter switch.12Reads the instruction definitions from bytecodes.c.3Writes the cases to generated_cases.c.h, which is #included in ceval.c.4"""56import argparse7import contextlib8import dataclasses9import os10import posixpath11import re12import sys13import typing1415import lexer as lx16import parser17from parser import StackEffect1819HERE = os.path.dirname(__file__)20ROOT = os.path.join(HERE, "../..")21THIS = os.path.relpath(__file__, ROOT).replace(os.path.sep, posixpath.sep)2223DEFAULT_INPUT = os.path.relpath(os.path.join(ROOT, "Python/bytecodes.c"))24DEFAULT_OUTPUT = os.path.relpath(os.path.join(ROOT, "Python/generated_cases.c.h"))25DEFAULT_METADATA_OUTPUT = os.path.relpath(26os.path.join(ROOT, "Python/opcode_metadata.h")27)28DEFAULT_PYMETADATA_OUTPUT = os.path.relpath(29os.path.join(ROOT, "Lib/_opcode_metadata.py")30)31DEFAULT_EXECUTOR_OUTPUT = os.path.relpath(32os.path.join(ROOT, "Python/executor_cases.c.h")33)34BEGIN_MARKER = "// BEGIN BYTECODES //"35END_MARKER = "// END BYTECODES //"36RE_PREDICTED = (37r"^\s*(?:GO_TO_INSTRUCTION\(|DEOPT_IF\(.*?,\s*)(\w+)\);\s*(?://.*)?$"38)39UNUSED = "unused"40BITS_PER_CODE_UNIT = 164142RESERVED_WORDS = {43"co_consts" : "Use FRAME_CO_CONSTS.",44"co_names": "Use FRAME_CO_NAMES.",45}4647arg_parser = argparse.ArgumentParser(48description="Generate the code for the interpreter switch.",49formatter_class=argparse.ArgumentDefaultsHelpFormatter,50)51arg_parser.add_argument(52"-o", "--output", type=str, help="Generated code", default=DEFAULT_OUTPUT53)54arg_parser.add_argument(55"-m", "--metadata", type=str, help="Generated C metadata", default=DEFAULT_METADATA_OUTPUT56)57arg_parser.add_argument(58"-p", "--pymetadata", type=str, help="Generated Python metadata", default=DEFAULT_PYMETADATA_OUTPUT59)60arg_parser.add_argument(61"-l", "--emit-line-directives", help="Emit #line directives", action="store_true"62)63arg_parser.add_argument(64"input", nargs=argparse.REMAINDER, help="Instruction definition file(s)"65)66arg_parser.add_argument(67"-e",68"--executor-cases",69type=str,70help="Write executor cases to this file",71default=DEFAULT_EXECUTOR_OUTPUT,72)737475def effect_size(effect: StackEffect) -> tuple[int, str]:76"""Return the 'size' impact of a stack effect.7778Returns a tuple (numeric, symbolic) where:7980- numeric is an int giving the statically analyzable size of the effect81- symbolic is a string representing a variable effect (e.g. 'oparg*2')8283At most one of these will be non-zero / non-empty.84"""85if effect.size:86assert not effect.cond, "Array effects cannot have a condition"87return 0, effect.size88elif effect.cond:89return 0, f"{maybe_parenthesize(effect.cond)} ? 1 : 0"90else:91return 1, ""929394def maybe_parenthesize(sym: str) -> str:95"""Add parentheses around a string if it contains an operator.9697An exception is made for '*' which is common and harmless98in the context where the symbolic size is used.99"""100if re.match(r"^[\s\w*]+$", sym):101return sym102else:103return f"({sym})"104105106def list_effect_size(effects: list[StackEffect]) -> tuple[int, str]:107numeric = 0108symbolic: list[str] = []109for effect in effects:110diff, sym = effect_size(effect)111numeric += diff112if sym:113symbolic.append(maybe_parenthesize(sym))114return numeric, " + ".join(symbolic)115116117def string_effect_size(arg: tuple[int, str]) -> str:118numeric, symbolic = arg119if numeric and symbolic:120return f"{numeric} + {symbolic}"121elif symbolic:122return symbolic123else:124return str(numeric)125126127class Formatter:128"""Wraps an output stream with the ability to indent etc."""129130stream: typing.TextIO131prefix: str132emit_line_directives: bool = False133lineno: int # Next line number, 1-based134filename: str # Slightly improved stream.filename135nominal_lineno: int136nominal_filename: str137138def __init__(139self, stream: typing.TextIO, indent: int,140emit_line_directives: bool = False, comment: str = "//",141) -> None:142self.stream = stream143self.prefix = " " * indent144self.emit_line_directives = emit_line_directives145self.comment = comment146self.lineno = 1147filename = os.path.relpath(self.stream.name, ROOT)148# Make filename more user-friendly and less platform-specific149filename = filename.replace("\\", "/")150if filename.startswith("./"):151filename = filename[2:]152if filename.endswith(".new"):153filename = filename[:-4]154self.filename = filename155self.nominal_lineno = 1156self.nominal_filename = filename157158def write_raw(self, s: str) -> None:159self.stream.write(s)160newlines = s.count("\n")161self.lineno += newlines162self.nominal_lineno += newlines163164def emit(self, arg: str) -> None:165if arg:166self.write_raw(f"{self.prefix}{arg}\n")167else:168self.write_raw("\n")169170def set_lineno(self, lineno: int, filename: str) -> None:171if self.emit_line_directives:172if lineno != self.nominal_lineno or filename != self.nominal_filename:173self.emit(f'#line {lineno} "{filename}"')174self.nominal_lineno = lineno175self.nominal_filename = filename176177def reset_lineno(self) -> None:178if self.lineno != self.nominal_lineno or self.filename != self.nominal_filename:179self.set_lineno(self.lineno + 1, self.filename)180181@contextlib.contextmanager182def indent(self):183self.prefix += " "184yield185self.prefix = self.prefix[:-4]186187@contextlib.contextmanager188def block(self, head: str, tail: str = ""):189if head:190self.emit(head + " {")191else:192self.emit("{")193with self.indent():194yield195self.emit("}" + tail)196197def stack_adjust(198self,199input_effects: list[StackEffect],200output_effects: list[StackEffect],201):202shrink, isym = list_effect_size(input_effects)203grow, osym = list_effect_size(output_effects)204diff = grow - shrink205if isym and isym != osym:206self.emit(f"STACK_SHRINK({isym});")207if diff < 0:208self.emit(f"STACK_SHRINK({-diff});")209if diff > 0:210self.emit(f"STACK_GROW({diff});")211if osym and osym != isym:212self.emit(f"STACK_GROW({osym});")213214def declare(self, dst: StackEffect, src: StackEffect | None):215if dst.name == UNUSED:216return217typ = f"{dst.type}" if dst.type else "PyObject *"218if src:219cast = self.cast(dst, src)220init = f" = {cast}{src.name}"221elif dst.cond:222init = " = NULL"223else:224init = ""225sepa = "" if typ.endswith("*") else " "226self.emit(f"{typ}{sepa}{dst.name}{init};")227228def assign(self, dst: StackEffect, src: StackEffect):229if src.name == UNUSED:230return231if src.size:232# Don't write sized arrays -- it's up to the user code.233return234cast = self.cast(dst, src)235if re.match(r"^REG\(oparg(\d+)\)$", dst.name):236self.emit(f"Py_XSETREF({dst.name}, {cast}{src.name});")237else:238stmt = f"{dst.name} = {cast}{src.name};"239if src.cond:240stmt = f"if ({src.cond}) {{ {stmt} }}"241self.emit(stmt)242243def cast(self, dst: StackEffect, src: StackEffect) -> str:244return f"({dst.type or 'PyObject *'})" if src.type != dst.type else ""245246@dataclasses.dataclass247class InstructionFlags:248"""Construct and manipulate instruction flags"""249250HAS_ARG_FLAG: bool251HAS_CONST_FLAG: bool252HAS_NAME_FLAG: bool253HAS_JUMP_FLAG: bool254255def __post_init__(self):256self.bitmask = {257name : (1 << i) for i, name in enumerate(self.names())258}259260@staticmethod261def fromInstruction(instr: "AnyInstruction"):262return InstructionFlags(263HAS_ARG_FLAG=variable_used(instr, "oparg"),264HAS_CONST_FLAG=variable_used(instr, "FRAME_CO_CONSTS"),265HAS_NAME_FLAG=variable_used(instr, "FRAME_CO_NAMES"),266HAS_JUMP_FLAG=variable_used(instr, "JUMPBY"),267)268269@staticmethod270def newEmpty():271return InstructionFlags(False, False, False, False)272273def add(self, other: "InstructionFlags") -> None:274for name, value in dataclasses.asdict(other).items():275if value:276setattr(self, name, value)277278def names(self, value=None):279if value is None:280return dataclasses.asdict(self).keys()281return [n for n, v in dataclasses.asdict(self).items() if v == value]282283def bitmap(self) -> int:284flags = 0285for name in self.names():286if getattr(self, name):287flags |= self.bitmask[name]288return flags289290@classmethod291def emit_macros(cls, out: Formatter):292flags = cls.newEmpty()293for name, value in flags.bitmask.items():294out.emit(f"#define {name} ({value})");295296for name, value in flags.bitmask.items():297out.emit(298f"#define OPCODE_{name[:-len('_FLAG')]}(OP) "299f"(_PyOpcode_opcode_metadata[(OP)].flags & ({name}))")300301302@dataclasses.dataclass303class ActiveCacheEffect:304"""Wraps a CacheEffect that is actually used, in context."""305effect: parser.CacheEffect306offset: int307308309FORBIDDEN_NAMES_IN_UOPS = (310"resume_with_error", # Proxy for "goto", which isn't an IDENTIFIER311"unbound_local_error",312"kwnames",313"next_instr",314"oparg1", # Proxy for super-instructions like LOAD_FAST_LOAD_FAST315"JUMPBY",316"DISPATCH",317"INSTRUMENTED_JUMP",318"throwflag",319"exception_unwind",320"import_from",321"import_name",322"_PyObject_CallNoArgs", # Proxy for BEFORE_WITH323)324325326# Interpreter tiers327TIER_ONE = 1 # Specializing adaptive interpreter (PEP 659)328TIER_TWO = 2 # Experimental tracing interpreter329Tiers: typing.TypeAlias = typing.Literal[1, 2]330331332@dataclasses.dataclass333class Instruction:334"""An instruction with additional data and code."""335336# Parts of the underlying instruction definition337inst: parser.InstDef338kind: typing.Literal["inst", "op"]339name: str340block: parser.Block341block_text: list[str] # Block.text, less curlies, less PREDICT() calls342block_line: int # First line of block in original code343344# Computed by constructor345always_exits: bool346cache_offset: int347cache_effects: list[parser.CacheEffect]348input_effects: list[StackEffect]349output_effects: list[StackEffect]350unmoved_names: frozenset[str]351instr_fmt: str352instr_flags: InstructionFlags353active_caches: list[ActiveCacheEffect]354355# Set later356family: parser.Family | None = None357predicted: bool = False358359def __init__(self, inst: parser.InstDef):360self.inst = inst361self.kind = inst.kind362self.name = inst.name363self.block = inst.block364self.block_text, self.check_eval_breaker, self.block_line = \365extract_block_text(self.block)366self.always_exits = always_exits(self.block_text)367self.cache_effects = [368effect for effect in inst.inputs if isinstance(effect, parser.CacheEffect)369]370self.cache_offset = sum(c.size for c in self.cache_effects)371self.input_effects = [372effect for effect in inst.inputs if isinstance(effect, StackEffect)373]374self.output_effects = inst.outputs # For consistency/completeness375unmoved_names: set[str] = set()376for ieffect, oeffect in zip(self.input_effects, self.output_effects):377if ieffect.name == oeffect.name:378unmoved_names.add(ieffect.name)379else:380break381self.unmoved_names = frozenset(unmoved_names)382383self.instr_flags = InstructionFlags.fromInstruction(inst)384385self.active_caches = []386offset = 0387for effect in self.cache_effects:388if effect.name != UNUSED:389self.active_caches.append(ActiveCacheEffect(effect, offset))390offset += effect.size391392if self.instr_flags.HAS_ARG_FLAG:393fmt = "IB"394else:395fmt = "IX"396if offset:397fmt += "C" + "0"*(offset-1)398self.instr_fmt = fmt399400def is_viable_uop(self) -> bool:401"""Whether this instruction is viable as a uop."""402if self.always_exits:403return False404if self.instr_flags.HAS_ARG_FLAG:405# If the instruction uses oparg, it cannot use any caches406if self.active_caches:407return False408else:409# If it doesn't use oparg, it can have one cache entry410if len(self.active_caches) > 1:411return False412for forbidden in FORBIDDEN_NAMES_IN_UOPS:413# TODO: Don't check in '#ifdef ENABLE_SPECIALIZATION' regions414if variable_used(self.inst, forbidden):415return False416return True417418def write(self, out: Formatter, tier: Tiers = TIER_ONE) -> None:419"""Write one instruction, sans prologue and epilogue."""420# Write a static assertion that a family's cache size is correct421if family := self.family:422if self.name == family.members[0]:423if cache_size := family.size:424out.emit(425f"static_assert({cache_size} == "426f'{self.cache_offset}, "incorrect cache size");'427)428429# Write input stack effect variable declarations and initializations430ieffects = list(reversed(self.input_effects))431for i, ieffect in enumerate(ieffects):432isize = string_effect_size(433list_effect_size([ieff for ieff in ieffects[: i + 1]])434)435if ieffect.size:436src = StackEffect(f"(stack_pointer - {maybe_parenthesize(isize)})", "PyObject **")437elif ieffect.cond:438src = StackEffect(f"({ieffect.cond}) ? stack_pointer[-{maybe_parenthesize(isize)}] : NULL", "")439else:440src = StackEffect(f"stack_pointer[-{maybe_parenthesize(isize)}]", "")441out.declare(ieffect, src)442443# Write output stack effect variable declarations444isize = string_effect_size(list_effect_size(self.input_effects))445input_names = {ieffect.name for ieffect in self.input_effects}446for i, oeffect in enumerate(self.output_effects):447if oeffect.name not in input_names:448if oeffect.size:449osize = string_effect_size(450list_effect_size([oeff for oeff in self.output_effects[:i]])451)452offset = "stack_pointer"453if isize != osize:454if isize != "0":455offset += f" - ({isize})"456if osize != "0":457offset += f" + {osize}"458src = StackEffect(offset, "PyObject **")459out.declare(oeffect, src)460else:461out.declare(oeffect, None)462463# out.emit(f"next_instr += OPSIZE({self.inst.name}) - 1;")464465self.write_body(out, 0, self.active_caches, tier=tier)466467# Skip the rest if the block always exits468if self.always_exits:469return470471# Write net stack growth/shrinkage472out.stack_adjust(473[ieff for ieff in self.input_effects],474[oeff for oeff in self.output_effects],475)476477# Write output stack effect assignments478oeffects = list(reversed(self.output_effects))479for i, oeffect in enumerate(oeffects):480if oeffect.name in self.unmoved_names:481continue482osize = string_effect_size(483list_effect_size([oeff for oeff in oeffects[: i + 1]])484)485if oeffect.size:486dst = StackEffect(f"stack_pointer - {maybe_parenthesize(osize)}", "PyObject **")487else:488dst = StackEffect(f"stack_pointer[-{maybe_parenthesize(osize)}]", "")489out.assign(dst, oeffect)490491# Write cache effect492if tier == TIER_ONE and self.cache_offset:493out.emit(f"next_instr += {self.cache_offset};")494495def write_body(496self,497out: Formatter,498dedent: int,499active_caches: list[ActiveCacheEffect],500tier: Tiers = TIER_ONE,501) -> None:502"""Write the instruction body."""503# Write cache effect variable declarations and initializations504for active in active_caches:505ceffect = active.effect506bits = ceffect.size * BITS_PER_CODE_UNIT507if bits == 64:508# NOTE: We assume that 64-bit data in the cache509# is always an object pointer.510# If this becomes false, we need a way to specify511# syntactically what type the cache data is.512typ = "PyObject *"513func = "read_obj"514else:515typ = f"uint{bits}_t "516func = f"read_u{bits}"517if tier == TIER_ONE:518out.emit(519f"{typ}{ceffect.name} = {func}(&next_instr[{active.offset}].cache);"520)521else:522out.emit(f"{typ}{ceffect.name} = ({typ.strip()})operand;")523524# Write the body, substituting a goto for ERROR_IF() and other stuff525assert dedent <= 0526extra = " " * -dedent527names_to_skip = self.unmoved_names | frozenset({UNUSED, "null"})528offset = 0529context = self.block.context530assert context is not None and context.owner is not None531filename = context.owner.filename532for line in self.block_text:533out.set_lineno(self.block_line + offset, filename)534offset += 1535if m := re.match(r"(\s*)ERROR_IF\((.+), (\w+)\);\s*(?://.*)?$", line):536space, cond, label = m.groups()537space = extra + space538# ERROR_IF() must pop the inputs from the stack.539# The code block is responsible for DECREF()ing them.540# NOTE: If the label doesn't exist, just add it to ceval.c.541542# Don't pop common input/output effects at the bottom!543# These aren't DECREF'ed so they can stay.544ieffs = list(self.input_effects)545oeffs = list(self.output_effects)546while ieffs and oeffs and ieffs[0] == oeffs[0]:547ieffs.pop(0)548oeffs.pop(0)549ninputs, symbolic = list_effect_size(ieffs)550if ninputs:551label = f"pop_{ninputs}_{label}"552if symbolic:553out.write_raw(554f"{space}if ({cond}) {{ STACK_SHRINK({symbolic}); goto {label}; }}\n"555)556else:557out.write_raw(f"{space}if ({cond}) goto {label};\n")558elif m := re.match(r"(\s*)DECREF_INPUTS\(\);\s*(?://.*)?$", line):559out.reset_lineno()560space = extra + m.group(1)561for ieff in self.input_effects:562if ieff.name in names_to_skip:563continue564if ieff.size:565out.write_raw(566f"{space}for (int _i = {ieff.size}; --_i >= 0;) {{\n"567)568out.write_raw(f"{space} Py_DECREF({ieff.name}[_i]);\n")569out.write_raw(f"{space}}}\n")570else:571decref = "XDECREF" if ieff.cond else "DECREF"572out.write_raw(f"{space}Py_{decref}({ieff.name});\n")573else:574out.write_raw(extra + line)575out.reset_lineno()576577578InstructionOrCacheEffect = Instruction | parser.CacheEffect579StackEffectMapping = list[tuple[StackEffect, StackEffect]]580581582@dataclasses.dataclass583class Component:584instr: Instruction585input_mapping: StackEffectMapping586output_mapping: StackEffectMapping587active_caches: list[ActiveCacheEffect]588589def write_body(self, out: Formatter) -> None:590with out.block(""):591input_names = {ieffect.name for _, ieffect in self.input_mapping}592for var, ieffect in self.input_mapping:593out.declare(ieffect, var)594for _, oeffect in self.output_mapping:595if oeffect.name not in input_names:596out.declare(oeffect, None)597598self.instr.write_body(out, -4, self.active_caches)599600for var, oeffect in self.output_mapping:601out.assign(var, oeffect)602603604MacroParts = list[Component | parser.CacheEffect]605606607@dataclasses.dataclass608class MacroInstruction:609"""A macro instruction."""610611name: str612stack: list[StackEffect]613initial_sp: int614final_sp: int615instr_fmt: str616instr_flags: InstructionFlags617macro: parser.Macro618parts: MacroParts619cache_offset: int620predicted: bool = False621622623@dataclasses.dataclass624class PseudoInstruction:625"""A pseudo instruction."""626627name: str628targets: list[Instruction]629instr_fmt: str630instr_flags: InstructionFlags631632633@dataclasses.dataclass634class OverriddenInstructionPlaceHolder:635name: str636637638AnyInstruction = Instruction | MacroInstruction | PseudoInstruction639INSTR_FMT_PREFIX = "INSTR_FMT_"640641642class Analyzer:643"""Parse input, analyze it, and write to output."""644645input_filenames: list[str]646output_filename: str647metadata_filename: str648pymetadata_filename: str649executor_filename: str650errors: int = 0651emit_line_directives: bool = False652653def __init__(654self,655input_filenames: list[str],656output_filename: str,657metadata_filename: str,658pymetadata_filename: str,659executor_filename: str,660):661"""Read the input file."""662self.input_filenames = input_filenames663self.output_filename = output_filename664self.metadata_filename = metadata_filename665self.pymetadata_filename = pymetadata_filename666self.executor_filename = executor_filename667668def error(self, msg: str, node: parser.Node) -> None:669lineno = 0670filename = "<unknown file>"671if context := node.context:672filename = context.owner.filename673# Use line number of first non-comment in the node674for token in context.owner.tokens[context.begin : context.end]:675lineno = token.line676if token.kind != "COMMENT":677break678print(f"{filename}:{lineno}: {msg}", file=sys.stderr)679self.errors += 1680681everything: list[682parser.InstDef | parser.Macro | parser.Pseudo | OverriddenInstructionPlaceHolder683]684instrs: dict[str, Instruction] # Includes ops685macros: dict[str, parser.Macro]686macro_instrs: dict[str, MacroInstruction]687families: dict[str, parser.Family]688pseudos: dict[str, parser.Pseudo]689pseudo_instrs: dict[str, PseudoInstruction]690691def parse(self) -> None:692"""Parse the source text.693694We only want the parser to see the stuff between the695begin and end markers.696"""697698self.everything = []699self.instrs = {}700self.macros = {}701self.families = {}702self.pseudos = {}703704instrs_idx: dict[str, int] = dict()705706for filename in self.input_filenames:707self.parse_file(filename, instrs_idx)708709files = " + ".join(self.input_filenames)710print(711f"Read {len(self.instrs)} instructions/ops, "712f"{len(self.macros)} macros, {len(self.pseudos)} pseudos, "713f"and {len(self.families)} families from {files}",714file=sys.stderr,715)716717def parse_file(self, filename: str, instrs_idx: dict[str, int]) -> None:718with open(filename) as file:719src = file.read()720721filename = os.path.relpath(filename, ROOT)722# Make filename more user-friendly and less platform-specific723filename = filename.replace("\\", "/")724if filename.startswith("./"):725filename = filename[2:]726psr = parser.Parser(src, filename=filename)727728# Skip until begin marker729while tkn := psr.next(raw=True):730if tkn.text == BEGIN_MARKER:731break732else:733raise psr.make_syntax_error(734f"Couldn't find {BEGIN_MARKER!r} in {psr.filename}"735)736start = psr.getpos()737738# Find end marker, then delete everything after it739while tkn := psr.next(raw=True):740if tkn.text == END_MARKER:741break742del psr.tokens[psr.getpos() - 1 :]743744# Parse from start745psr.setpos(start)746thing: parser.InstDef | parser.Macro | parser.Pseudo | parser.Family | None747thing_first_token = psr.peek()748while thing := psr.definition():749if ws := [w for w in RESERVED_WORDS if variable_used(thing, w)]:750self.error(f"'{ws[0]}' is a reserved word. {RESERVED_WORDS[ws[0]]}", thing)751752match thing:753case parser.InstDef(name=name):754if name in self.instrs:755if not thing.override:756raise psr.make_syntax_error(757f"Duplicate definition of '{name}' @ {thing.context} "758f"previous definition @ {self.instrs[name].inst.context}",759thing_first_token,760)761self.everything[instrs_idx[name]] = OverriddenInstructionPlaceHolder(name=name)762if name not in self.instrs and thing.override:763raise psr.make_syntax_error(764f"Definition of '{name}' @ {thing.context} is supposed to be "765"an override but no previous definition exists.",766thing_first_token,767)768self.instrs[name] = Instruction(thing)769instrs_idx[name] = len(self.everything)770self.everything.append(thing)771case parser.Macro(name):772self.macros[name] = thing773self.everything.append(thing)774case parser.Family(name):775self.families[name] = thing776case parser.Pseudo(name):777self.pseudos[name] = thing778self.everything.append(thing)779case _:780typing.assert_never(thing)781if not psr.eof():782raise psr.make_syntax_error(f"Extra stuff at the end of {filename}")783784def analyze(self) -> None:785"""Analyze the inputs.786787Raises SystemExit if there is an error.788"""789self.analyze_macros_and_pseudos()790self.find_predictions()791self.map_families()792self.check_families()793794def find_predictions(self) -> None:795"""Find the instructions that need PREDICTED() labels."""796for instr in self.instrs.values():797targets: set[str] = set()798for line in instr.block_text:799if m := re.match(RE_PREDICTED, line):800targets.add(m.group(1))801for target in targets:802if target_instr := self.instrs.get(target):803target_instr.predicted = True804elif target_macro := self.macro_instrs.get(target):805target_macro.predicted = True806else:807self.error(808f"Unknown instruction {target!r} predicted in {instr.name!r}",809instr.inst, # TODO: Use better location810)811812def map_families(self) -> None:813"""Link instruction names back to their family, if they have one."""814for family in self.families.values():815for member in family.members:816if member_instr := self.instrs.get(member):817if member_instr.family not in (family, None):818self.error(819f"Instruction {member} is a member of multiple families "820f"({member_instr.family.name}, {family.name}).",821family,822)823else:824member_instr.family = family825elif member_macro := self.macro_instrs.get(member):826for part in member_macro.parts:827if isinstance(part, Component):828if part.instr.family not in (family, None):829self.error(830f"Component {part.instr.name} of macro {member} "831f"is a member of multiple families "832f"({part.instr.family.name}, {family.name}).",833family,834)835else:836part.instr.family = family837else:838self.error(839f"Unknown instruction {member!r} referenced in family {family.name!r}",840family,841)842843def check_families(self) -> None:844"""Check each family:845846- Must have at least 2 members847- All members must be known instructions848- All members must have the same cache, input and output effects849"""850for family in self.families.values():851if len(family.members) < 2:852self.error(f"Family {family.name!r} has insufficient members", family)853members = [854member855for member in family.members856if member in self.instrs or member in self.macro_instrs857]858if members != family.members:859unknown = set(family.members) - set(members)860self.error(861f"Family {family.name!r} has unknown members: {unknown}", family862)863if len(members) < 2:864continue865expected_effects = self.effect_counts(members[0])866for member in members[1:]:867member_effects = self.effect_counts(member)868if member_effects != expected_effects:869self.error(870f"Family {family.name!r} has inconsistent "871f"(cache, input, output) effects:\n"872f" {family.members[0]} = {expected_effects}; "873f"{member} = {member_effects}",874family,875)876877def effect_counts(self, name: str) -> tuple[int, int, int]:878if instr := self.instrs.get(name):879cache = instr.cache_offset880input = len(instr.input_effects)881output = len(instr.output_effects)882elif mac := self.macro_instrs.get(name):883cache = mac.cache_offset884input, output = 0, 0885for part in mac.parts:886if isinstance(part, Component):887# A component may pop what the previous component pushed,888# so we offset the input/output counts by that.889delta_i = len(part.instr.input_effects)890delta_o = len(part.instr.output_effects)891offset = min(delta_i, output)892input += delta_i - offset893output += delta_o - offset894else:895assert False, f"Unknown instruction {name!r}"896return cache, input, output897898def analyze_macros_and_pseudos(self) -> None:899"""Analyze each macro and pseudo instruction."""900self.macro_instrs = {}901self.pseudo_instrs = {}902for name, macro in self.macros.items():903self.macro_instrs[name] = self.analyze_macro(macro)904for name, pseudo in self.pseudos.items():905self.pseudo_instrs[name] = self.analyze_pseudo(pseudo)906907def analyze_macro(self, macro: parser.Macro) -> MacroInstruction:908components = self.check_macro_components(macro)909stack, initial_sp = self.stack_analysis(components)910sp = initial_sp911parts: MacroParts = []912flags = InstructionFlags.newEmpty()913offset = 0914for component in components:915match component:916case parser.CacheEffect() as ceffect:917parts.append(ceffect)918offset += ceffect.size919case Instruction() as instr:920part, sp, offset = self.analyze_instruction(instr, stack, sp, offset)921parts.append(part)922flags.add(instr.instr_flags)923case _:924typing.assert_never(component)925final_sp = sp926format = "IB"927if offset:928format += "C" + "0"*(offset-1)929return MacroInstruction(930macro.name, stack, initial_sp, final_sp, format, flags, macro, parts, offset931)932933def analyze_pseudo(self, pseudo: parser.Pseudo) -> PseudoInstruction:934targets = [self.instrs[target] for target in pseudo.targets]935assert targets936# Make sure the targets have the same fmt937fmts = list(set([t.instr_fmt for t in targets]))938assert(len(fmts) == 1)939assert(len(list(set([t.instr_flags.bitmap() for t in targets]))) == 1)940return PseudoInstruction(pseudo.name, targets, fmts[0], targets[0].instr_flags)941942def analyze_instruction(943self, instr: Instruction, stack: list[StackEffect], sp: int, offset: int944) -> tuple[Component, int, int]:945input_mapping: StackEffectMapping = []946for ieffect in reversed(instr.input_effects):947sp -= 1948input_mapping.append((stack[sp], ieffect))949output_mapping: StackEffectMapping = []950for oeffect in instr.output_effects:951output_mapping.append((stack[sp], oeffect))952sp += 1953active_effects: list[ActiveCacheEffect] = []954for ceffect in instr.cache_effects:955if ceffect.name != UNUSED:956active_effects.append(ActiveCacheEffect(ceffect, offset))957offset += ceffect.size958return Component(instr, input_mapping, output_mapping, active_effects), sp, offset959960def check_macro_components(961self, macro: parser.Macro962) -> list[InstructionOrCacheEffect]:963components: list[InstructionOrCacheEffect] = []964for uop in macro.uops:965match uop:966case parser.OpName(name):967if name not in self.instrs:968self.error(f"Unknown instruction {name!r}", macro)969components.append(self.instrs[name])970case parser.CacheEffect():971components.append(uop)972case _:973typing.assert_never(uop)974return components975976def stack_analysis(977self, components: typing.Iterable[InstructionOrCacheEffect]978) -> tuple[list[StackEffect], int]:979"""Analyze a macro.980981Ignore cache effects.982983Return the list of variables (as StackEffects) and the initial stack pointer.984"""985lowest = current = highest = 0986conditions: dict[int, str] = {} # Indexed by 'current'.987last_instr: Instruction | None = None988for thing in components:989if isinstance(thing, Instruction):990last_instr = thing991for thing in components:992match thing:993case Instruction() as instr:994if any(995eff.size for eff in instr.input_effects + instr.output_effects996):997# TODO: Eventually this will be needed, at least for macros.998self.error(999f"Instruction {instr.name!r} has variable-sized stack effect, "1000"which are not supported in macro instructions",1001instr.inst, # TODO: Pass name+location of macro1002)1003if any(eff.cond for eff in instr.input_effects):1004self.error(1005f"Instruction {instr.name!r} has conditional input stack effect, "1006"which are not supported in macro instructions",1007instr.inst, # TODO: Pass name+location of macro1008)1009if any(eff.cond for eff in instr.output_effects) and instr is not last_instr:1010self.error(1011f"Instruction {instr.name!r} has conditional output stack effect, "1012"but is not the last instruction in a macro",1013instr.inst, # TODO: Pass name+location of macro1014)1015current -= len(instr.input_effects)1016lowest = min(lowest, current)1017for eff in instr.output_effects:1018if eff.cond:1019conditions[current] = eff.cond1020current += 11021highest = max(highest, current)1022case parser.CacheEffect():1023pass1024case _:1025typing.assert_never(thing)1026# At this point, 'current' is the net stack effect,1027# and 'lowest' and 'highest' are the extremes.1028# Note that 'lowest' may be negative.1029stack = [1030StackEffect(f"_tmp_{i}", "", conditions.get(highest - i, ""))1031for i in reversed(range(1, highest - lowest + 1))1032]1033return stack, -lowest10341035def get_stack_effect_info(1036self, thing: parser.InstDef | parser.Macro | parser.Pseudo1037) -> tuple[AnyInstruction | None, str | None, str | None]:1038def effect_str(effects: list[StackEffect]) -> str:1039n_effect, sym_effect = list_effect_size(effects)1040if sym_effect:1041return f"{sym_effect} + {n_effect}" if n_effect else sym_effect1042return str(n_effect)10431044instr: AnyInstruction | None1045match thing:1046case parser.InstDef():1047if thing.kind != "op":1048instr = self.instrs[thing.name]1049popped = effect_str(instr.input_effects)1050pushed = effect_str(instr.output_effects)1051else:1052instr = None1053popped = ""1054pushed = ""1055case parser.Macro():1056instr = self.macro_instrs[thing.name]1057parts = [comp for comp in instr.parts if isinstance(comp, Component)]1058# Note: stack_analysis() already verifies that macro components1059# have no variable-sized stack effects.1060low = 01061sp = 01062high = 01063pushed_symbolic: list[str] = []1064for comp in parts:1065for effect in comp.instr.input_effects:1066assert not effect.cond, effect1067assert not effect.size, effect1068sp -= 11069low = min(low, sp)1070for effect in comp.instr.output_effects:1071assert not effect.size, effect1072if effect.cond:1073pushed_symbolic.append(maybe_parenthesize(f"{maybe_parenthesize(effect.cond)} ? 1 : 0"))1074sp += 11075high = max(sp, high)1076if high != max(0, sp):1077# If you get this, intermediate stack growth occurs,1078# and stack size calculations may go awry.1079# E.g. [push, pop]. The fix would be for stack size1080# calculations to use the micro ops.1081self.error("Macro has virtual stack growth", thing)1082popped = str(-low)1083pushed_symbolic.append(str(sp - low - len(pushed_symbolic)))1084pushed = " + ".join(pushed_symbolic)1085case parser.Pseudo():1086instr = self.pseudo_instrs[thing.name]1087popped = pushed = None1088# Calculate stack effect, and check that it's the the same1089# for all targets.1090for target in self.pseudos[thing.name].targets:1091target_instr = self.instrs.get(target)1092# Currently target is always an instr. This could change1093# in the future, e.g., if we have a pseudo targetting a1094# macro instruction.1095assert target_instr1096target_popped = effect_str(target_instr.input_effects)1097target_pushed = effect_str(target_instr.output_effects)1098if popped is None and pushed is None:1099popped, pushed = target_popped, target_pushed1100else:1101assert popped == target_popped1102assert pushed == target_pushed1103case _:1104typing.assert_never(thing)1105return instr, popped, pushed11061107def write_stack_effect_functions(self) -> None:1108popped_data: list[tuple[AnyInstruction, str]] = []1109pushed_data: list[tuple[AnyInstruction, str]] = []1110for thing in self.everything:1111if isinstance(thing, OverriddenInstructionPlaceHolder):1112continue1113instr, popped, pushed = self.get_stack_effect_info(thing)1114if instr is not None:1115assert popped is not None and pushed is not None1116popped_data.append((instr, popped))1117pushed_data.append((instr, pushed))11181119def write_function(1120direction: str, data: list[tuple[AnyInstruction, str]]1121) -> None:1122self.out.emit("")1123self.out.emit("#ifndef NEED_OPCODE_METADATA")1124self.out.emit(f"extern int _PyOpcode_num_{direction}(int opcode, int oparg, bool jump);")1125self.out.emit("#else")1126self.out.emit("int")1127self.out.emit(f"_PyOpcode_num_{direction}(int opcode, int oparg, bool jump) {{")1128self.out.emit(" switch(opcode) {")1129for instr, effect in data:1130self.out.emit(f" case {instr.name}:")1131self.out.emit(f" return {effect};")1132self.out.emit(" default:")1133self.out.emit(" return -1;")1134self.out.emit(" }")1135self.out.emit("}")1136self.out.emit("#endif")11371138write_function("popped", popped_data)1139write_function("pushed", pushed_data)1140self.out.emit("")11411142def from_source_files(self) -> str:1143paths = f"\n{self.out.comment} ".join(1144os.path.relpath(filename, ROOT).replace(os.path.sep, posixpath.sep)1145for filename in self.input_filenames1146)1147return f"{self.out.comment} from:\n{self.out.comment} {paths}\n"11481149def write_provenance_header(self):1150self.out.write_raw(f"{self.out.comment} This file is generated by {THIS}\n")1151self.out.write_raw(self.from_source_files())1152self.out.write_raw(f"{self.out.comment} Do not edit!\n")11531154def write_metadata(self) -> None:1155"""Write instruction metadata to output file."""11561157# Compute the set of all instruction formats.1158all_formats: set[str] = set()1159for thing in self.everything:1160match thing:1161case OverriddenInstructionPlaceHolder():1162continue1163case parser.InstDef():1164format = self.instrs[thing.name].instr_fmt1165case parser.Macro():1166format = self.macro_instrs[thing.name].instr_fmt1167case parser.Pseudo():1168format = None1169for target in self.pseudos[thing.name].targets:1170target_instr = self.instrs.get(target)1171assert target_instr1172if format is None:1173format = target_instr.instr_fmt1174else:1175assert format == target_instr.instr_fmt1176case _:1177typing.assert_never(thing)1178all_formats.add(format)1179# Turn it into a list of enum definitions.1180format_enums = [INSTR_FMT_PREFIX + format for format in sorted(all_formats)]11811182with open(self.metadata_filename, "w") as f:1183# Create formatter1184self.out = Formatter(f, 0)11851186self.write_provenance_header()11871188self.write_pseudo_instrs()11891190self.out.emit("")1191self.write_uop_items(lambda name, counter: f"#define {name} {counter}")11921193self.write_stack_effect_functions()11941195# Write type definitions1196self.out.emit(f"enum InstructionFormat {{ {', '.join(format_enums)} }};")11971198InstructionFlags.emit_macros(self.out)11991200with self.out.block("struct opcode_metadata", ";"):1201self.out.emit("bool valid_entry;")1202self.out.emit("enum InstructionFormat instr_format;")1203self.out.emit("int flags;")1204self.out.emit("")12051206with self.out.block("struct opcode_macro_expansion", ";"):1207self.out.emit("int nuops;")1208self.out.emit("struct { int16_t uop; int8_t size; int8_t offset; } uops[8];")1209self.out.emit("")12101211self.out.emit("")1212self.out.emit("#define OPCODE_METADATA_FMT(OP) "1213"(_PyOpcode_opcode_metadata[(OP)].instr_format)")1214self.out.emit("#define SAME_OPCODE_METADATA(OP1, OP2) \\")1215self.out.emit(" (OPCODE_METADATA_FMT(OP1) == OPCODE_METADATA_FMT(OP2))")1216self.out.emit("")12171218# Write metadata array declaration1219self.out.emit("#ifndef NEED_OPCODE_METADATA")1220self.out.emit("extern const struct opcode_metadata _PyOpcode_opcode_metadata[512];")1221self.out.emit("extern const struct opcode_macro_expansion _PyOpcode_macro_expansion[256];")1222self.out.emit("#ifdef Py_DEBUG")1223self.out.emit("extern const char * const _PyOpcode_uop_name[512];")1224self.out.emit("#endif")1225self.out.emit("#else")12261227self.out.emit("const struct opcode_metadata _PyOpcode_opcode_metadata[512] = {")12281229# Write metadata for each instruction1230for thing in self.everything:1231match thing:1232case OverriddenInstructionPlaceHolder():1233continue1234case parser.InstDef():1235if thing.kind != "op":1236self.write_metadata_for_inst(self.instrs[thing.name])1237case parser.Macro():1238self.write_metadata_for_macro(self.macro_instrs[thing.name])1239case parser.Pseudo():1240self.write_metadata_for_pseudo(self.pseudo_instrs[thing.name])1241case _:1242typing.assert_never(thing)12431244# Write end of array1245self.out.emit("};")12461247with self.out.block(1248"const struct opcode_macro_expansion _PyOpcode_macro_expansion[256] =",1249";",1250):1251# Write macro expansion for each non-pseudo instruction1252for thing in self.everything:1253match thing:1254case OverriddenInstructionPlaceHolder():1255pass1256case parser.InstDef(name=name):1257instr = self.instrs[name]1258# Since an 'op' is not a bytecode, it has no expansion; but 'inst' is1259if instr.kind == "inst" and instr.is_viable_uop():1260# Construct a dummy Component -- input/output mappings are not used1261part = Component(instr, [], [], instr.active_caches)1262self.write_macro_expansions(instr.name, [part])1263case parser.Macro():1264mac = self.macro_instrs[thing.name]1265self.write_macro_expansions(mac.name, mac.parts)1266case parser.Pseudo():1267pass1268case _:1269typing.assert_never(thing)12701271self.out.emit("#ifdef Py_DEBUG")1272with self.out.block("const char * const _PyOpcode_uop_name[512] =", ";"):1273self.write_uop_items(lambda name, counter: f"[{counter}] = \"{name}\",")1274self.out.emit("#endif")12751276self.out.emit("#endif")12771278with open(self.pymetadata_filename, "w") as f:1279# Create formatter1280self.out = Formatter(f, 0, comment = "#")12811282self.write_provenance_header()12831284self.out.emit("")1285self.out.emit("_specializations = {")1286for name, family in self.families.items():1287assert len(family.members) > 11288with self.out.indent():1289self.out.emit(f"\"{family.members[0]}\": [")1290with self.out.indent():1291for m in family.members[1:]:1292self.out.emit(f"\"{m}\",")1293self.out.emit(f"],")1294self.out.emit("}")12951296# Handle special case1297self.out.emit("")1298self.out.emit("# An irregular case:")1299self.out.emit(1300"_specializations[\"BINARY_OP\"].append("1301"\"BINARY_OP_INPLACE_ADD_UNICODE\")")13021303# Make list of specialized instructions1304self.out.emit("")1305self.out.emit(1306"_specialized_instructions = ["1307"opcode for family in _specializations.values() for opcode in family"1308"]")13091310def write_pseudo_instrs(self) -> None:1311"""Write the IS_PSEUDO_INSTR macro"""1312self.out.emit("\n\n#define IS_PSEUDO_INSTR(OP) ( \\")1313for op in self.pseudos:1314self.out.emit(f" ((OP) == {op}) || \\")1315self.out.emit(f" 0)")13161317def write_uop_items(self, make_text: typing.Callable[[str, int], str]) -> None:1318"""Write '#define XXX NNN' for each uop"""1319counter = 300 # TODO: Avoid collision with pseudo instructions1320def add(name: str) -> None:1321nonlocal counter1322self.out.emit(make_text(name, counter))1323counter += 11324add("EXIT_TRACE")1325add("SET_IP")1326for instr in self.instrs.values():1327if instr.kind == "op" and instr.is_viable_uop():1328add(instr.name)13291330def write_macro_expansions(self, name: str, parts: MacroParts) -> None:1331"""Write the macro expansions for a macro-instruction."""1332# TODO: Refactor to share code with write_cody(), is_viaible_uop(), etc.1333offset = 0 # Cache effect offset1334expansions: list[tuple[str, int, int]] = [] # [(name, size, offset), ...]1335for part in parts:1336if isinstance(part, Component):1337# All component instructions must be viable uops1338if not part.instr.is_viable_uop():1339print(f"NOTE: Part {part.instr.name} of {name} is not a viable uop")1340return1341if part.instr.instr_flags.HAS_ARG_FLAG or not part.active_caches:1342size, offset = 0, 01343else:1344# If this assert triggers, is_viable_uops() lied1345assert len(part.active_caches) == 1, (name, part.instr.name)1346cache = part.active_caches[0]1347size, offset = cache.effect.size, cache.offset1348expansions.append((part.instr.name, size, offset))1349assert len(expansions) > 0, f"Macro {name} has empty expansion?!"1350pieces = [f"{{ {name}, {size}, {offset} }}" for name, size, offset in expansions]1351self.out.emit(1352f"[{name}] = "1353f"{{ .nuops = {len(expansions)}, .uops = {{ {', '.join(pieces)} }} }},"1354)13551356def emit_metadata_entry(1357self, name: str, fmt: str, flags: InstructionFlags1358) -> None:1359flag_names = flags.names(value=True)1360if not flag_names:1361flag_names.append("0")1362self.out.emit(1363f" [{name}] = {{ true, {INSTR_FMT_PREFIX}{fmt},"1364f" {' | '.join(flag_names)} }},"1365)13661367def write_metadata_for_inst(self, instr: Instruction) -> None:1368"""Write metadata for a single instruction."""1369self.emit_metadata_entry(instr.name, instr.instr_fmt, instr.instr_flags)13701371def write_metadata_for_macro(self, mac: MacroInstruction) -> None:1372"""Write metadata for a macro-instruction."""1373self.emit_metadata_entry(mac.name, mac.instr_fmt, mac.instr_flags)13741375def write_metadata_for_pseudo(self, ps: PseudoInstruction) -> None:1376"""Write metadata for a macro-instruction."""1377self.emit_metadata_entry(ps.name, ps.instr_fmt, ps.instr_flags)13781379def write_instructions(self) -> None:1380"""Write instructions to output file."""1381with open(self.output_filename, "w") as f:1382# Create formatter1383self.out = Formatter(f, 8, self.emit_line_directives)13841385self.write_provenance_header()13861387# Write and count instructions of all kinds1388n_instrs = 01389n_macros = 01390n_pseudos = 01391for thing in self.everything:1392match thing:1393case OverriddenInstructionPlaceHolder():1394self.write_overridden_instr_place_holder(thing)1395case parser.InstDef():1396if thing.kind != "op":1397n_instrs += 11398self.write_instr(self.instrs[thing.name])1399case parser.Macro():1400n_macros += 11401self.write_macro(self.macro_instrs[thing.name])1402case parser.Pseudo():1403n_pseudos += 11404case _:1405typing.assert_never(thing)14061407print(1408f"Wrote {n_instrs} instructions, {n_macros} macros, "1409f"and {n_pseudos} pseudos to {self.output_filename}",1410file=sys.stderr,1411)14121413def write_executor_instructions(self) -> None:1414"""Generate cases for the Tier 2 interpreter."""1415with open(self.executor_filename, "w") as f:1416self.out = Formatter(f, 8, self.emit_line_directives)1417self.write_provenance_header()1418for thing in self.everything:1419match thing:1420case OverriddenInstructionPlaceHolder():1421# TODO: Is this helpful?1422self.write_overridden_instr_place_holder(thing)1423case parser.InstDef():1424instr = self.instrs[thing.name]1425if instr.is_viable_uop():1426self.out.emit("")1427with self.out.block(f"case {thing.name}:"):1428instr.write(self.out, tier=TIER_TWO)1429self.out.emit("break;")1430case parser.Macro():1431pass1432case parser.Pseudo():1433pass1434case _:1435typing.assert_never(thing)1436print(1437f"Wrote some stuff to {self.executor_filename}",1438file=sys.stderr,1439)14401441def write_overridden_instr_place_holder(self,1442place_holder: OverriddenInstructionPlaceHolder) -> None:1443self.out.emit("")1444self.out.emit(1445f"{self.out.comment} TARGET({place_holder.name}) overridden by later definition")14461447def write_instr(self, instr: Instruction) -> None:1448name = instr.name1449self.out.emit("")1450if instr.inst.override:1451self.out.emit("{self.out.comment} Override")1452with self.out.block(f"TARGET({name})"):1453if instr.predicted:1454self.out.emit(f"PREDICTED({name});")1455instr.write(self.out)1456if not instr.always_exits:1457if instr.check_eval_breaker:1458self.out.emit("CHECK_EVAL_BREAKER();")1459self.out.emit(f"DISPATCH();")14601461def write_macro(self, mac: MacroInstruction) -> None:1462"""Write code for a macro instruction."""1463last_instr: Instruction | None = None1464with self.wrap_macro(mac):1465cache_adjust = 01466for part in mac.parts:1467match part:1468case parser.CacheEffect(size=size):1469cache_adjust += size1470case Component() as comp:1471last_instr = comp.instr1472comp.write_body(self.out)1473cache_adjust += comp.instr.cache_offset14741475if cache_adjust:1476self.out.emit(f"next_instr += {cache_adjust};")14771478if (1479last_instr1480and (family := last_instr.family)1481and mac.name == family.members[0]1482and (cache_size := family.size)1483):1484self.out.emit(1485f"static_assert({cache_size} == "1486f'{cache_adjust}, "incorrect cache size");'1487)14881489@contextlib.contextmanager1490def wrap_macro(self, mac: MacroInstruction):1491"""Boilerplate for macro instructions."""1492# TODO: Somewhere (where?) make it so that if one instruction1493# has an output that is input to another, and the variable names1494# and types match and don't conflict with other instructions,1495# that variable is declared with the right name and type in the1496# outer block, rather than trusting the compiler to optimize it.1497self.out.emit("")1498with self.out.block(f"TARGET({mac.name})"):1499if mac.predicted:1500self.out.emit(f"PREDICTED({mac.name});")15011502# The input effects should have no conditionals.1503# Only the output effects do (for now).1504ieffects = [1505StackEffect(eff.name, eff.type) if eff.cond else eff1506for eff in mac.stack1507]15081509for i, var in reversed(list(enumerate(ieffects))):1510src = None1511if i < mac.initial_sp:1512src = StackEffect(f"stack_pointer[-{mac.initial_sp - i}]", "")1513self.out.declare(var, src)15141515yield15161517self.out.stack_adjust(ieffects[:mac.initial_sp], mac.stack[:mac.final_sp])15181519for i, var in enumerate(reversed(mac.stack[: mac.final_sp]), 1):1520dst = StackEffect(f"stack_pointer[-{i}]", "")1521self.out.assign(dst, var)15221523self.out.emit(f"DISPATCH();")152415251526def extract_block_text(block: parser.Block) -> tuple[list[str], bool, int]:1527# Get lines of text with proper dedent1528blocklines = block.text.splitlines(True)1529first_token: lx.Token = block.tokens[0] # IndexError means the context is broken1530block_line = first_token.begin[0]15311532# Remove blank lines from both ends1533while blocklines and not blocklines[0].strip():1534blocklines.pop(0)1535block_line += 11536while blocklines and not blocklines[-1].strip():1537blocklines.pop()15381539# Remove leading and trailing braces1540assert blocklines and blocklines[0].strip() == "{"1541assert blocklines and blocklines[-1].strip() == "}"1542blocklines.pop()1543blocklines.pop(0)1544block_line += 115451546# Remove trailing blank lines1547while blocklines and not blocklines[-1].strip():1548blocklines.pop()15491550# Separate CHECK_EVAL_BREAKER() macro from end1551check_eval_breaker = \1552blocklines != [] and blocklines[-1].strip() == "CHECK_EVAL_BREAKER();"1553if check_eval_breaker:1554del blocklines[-1]15551556return blocklines, check_eval_breaker, block_line155715581559def always_exits(lines: list[str]) -> bool:1560"""Determine whether a block always ends in a return/goto/etc."""1561if not lines:1562return False1563line = lines[-1].rstrip()1564# Indent must match exactly (TODO: Do something better)1565if line[:12] != " " * 12:1566return False1567line = line[12:]1568return line.startswith(1569(1570"goto ",1571"return ",1572"DISPATCH",1573"GO_TO_",1574"Py_UNREACHABLE()",1575"ERROR_IF(true, ",1576)1577)157815791580def variable_used(node: parser.Node, name: str) -> bool:1581"""Determine whether a variable with a given name is used in a node."""1582return any(1583token.kind == "IDENTIFIER" and token.text == name for token in node.tokens1584)158515861587def main():1588"""Parse command line, parse input, analyze, write output."""1589args = arg_parser.parse_args() # Prints message and sys.exit(2) on error1590if len(args.input) == 0:1591args.input.append(DEFAULT_INPUT)15921593# Raises OSError if input unreadable1594a = Analyzer(args.input, args.output, args.metadata, args.pymetadata, args.executor_cases)15951596if args.emit_line_directives:1597a.emit_line_directives = True1598a.parse() # Raises SyntaxError on failure1599a.analyze() # Prints messages and sets a.errors on failure1600if a.errors:1601sys.exit(f"Found {a.errors} errors")1602a.write_instructions() # Raises OSError if output can't be written1603a.write_metadata()1604a.write_executor_instructions()160516061607if __name__ == "__main__":1608main()160916101611