|
| 1 | +"""Generate the list of opcode IDs. |
| 2 | +Reads the instruction definitions from bytecodes.c. |
| 3 | +Writes the IDs to opcode._ids.h by default. |
| 4 | +""" |
| 5 | + |
| 6 | +import argparse |
| 7 | +import os.path |
| 8 | +import sys |
| 9 | + |
| 10 | +from analyzer import ( |
| 11 | + Analysis, |
| 12 | + Instruction, |
| 13 | + analyze_files, |
| 14 | +) |
| 15 | +from generators_common import ( |
| 16 | + DEFAULT_INPUT, |
| 17 | + ROOT, |
| 18 | + write_header, |
| 19 | +) |
| 20 | +from cwriter import CWriter |
| 21 | +from typing import TextIO |
| 22 | + |
| 23 | + |
| 24 | +DEFAULT_OUTPUT = ROOT / "Include/opcode_ids.h" |
| 25 | + |
| 26 | + |
| 27 | +def generate_opcode_header(filenames: str, analysis: Analysis, outfile: TextIO) -> None: |
| 28 | + write_header(__file__, filenames, outfile) |
| 29 | + out = CWriter(outfile, 0, False) |
| 30 | + out.emit("\n") |
| 31 | + instmap: dict[str, int] = {} |
| 32 | + |
| 33 | + # 0 is reserved for cache entries. This helps debugging. |
| 34 | + instmap["CACHE"] = 0 |
| 35 | + |
| 36 | + # 17 is reserved as it is the initial value for the specializing counter. |
| 37 | + # This helps catch cases where we attempt to execute a cache. |
| 38 | + instmap["RESERVED"] = 17 |
| 39 | + |
| 40 | + # 149 is RESUME - it is hard coded as such in Tools/build/deepfreeze.py |
| 41 | + instmap["RESUME"] = 149 |
| 42 | + instmap["INSTRUMENTED_LINE"] = 254 |
| 43 | + |
| 44 | + instrumented = [ |
| 45 | + name for name in analysis.instructions if name.startswith("INSTRUMENTED") |
| 46 | + ] |
| 47 | + |
| 48 | + # Special case: this instruction is implemented in ceval.c |
| 49 | + # rather than bytecodes.c, so we need to add it explicitly |
| 50 | + # here (at least until we add something to bytecodes.c to |
| 51 | + # declare external instructions). |
| 52 | + instrumented.append("INSTRUMENTED_LINE") |
| 53 | + |
| 54 | + specialized: set[str] = set() |
| 55 | + no_arg: list[str] = [] |
| 56 | + has_arg: list[str] = [] |
| 57 | + |
| 58 | + for family in analysis.families.values(): |
| 59 | + specialized.update(inst.name for inst in family.members) |
| 60 | + |
| 61 | + for inst in analysis.instructions.values(): |
| 62 | + name = inst.name |
| 63 | + if name in specialized: |
| 64 | + continue |
| 65 | + if name in instrumented: |
| 66 | + continue |
| 67 | + if inst.properties.oparg: |
| 68 | + has_arg.append(name) |
| 69 | + else: |
| 70 | + no_arg.append(name) |
| 71 | + |
| 72 | + # Specialized ops appear in their own section |
| 73 | + # Instrumented opcodes are at the end of the valid range |
| 74 | + min_internal = 150 |
| 75 | + min_instrumented = 254 - (len(instrumented) - 1) |
| 76 | + assert min_internal + len(specialized) < min_instrumented |
| 77 | + |
| 78 | + next_opcode = 1 |
| 79 | + |
| 80 | + def add_instruction(name: str) -> None: |
| 81 | + nonlocal next_opcode |
| 82 | + if name in instmap: |
| 83 | + return # Pre-defined name |
| 84 | + while next_opcode in instmap.values(): |
| 85 | + next_opcode += 1 |
| 86 | + instmap[name] = next_opcode |
| 87 | + next_opcode += 1 |
| 88 | + |
| 89 | + for name in sorted(no_arg): |
| 90 | + add_instruction(name) |
| 91 | + for name in sorted(has_arg): |
| 92 | + add_instruction(name) |
| 93 | + # For compatibility |
| 94 | + next_opcode = min_internal |
| 95 | + for name in sorted(specialized): |
| 96 | + add_instruction(name) |
| 97 | + next_opcode = min_instrumented |
| 98 | + for name in instrumented: |
| 99 | + add_instruction(name) |
| 100 | + |
| 101 | + for op, name in enumerate(sorted(analysis.pseudos), 256): |
| 102 | + instmap[name] = op |
| 103 | + |
| 104 | + assert 255 not in instmap.values() |
| 105 | + |
| 106 | + out.emit( |
| 107 | + """#ifndef Py_OPCODE_IDS_H |
| 108 | +#define Py_OPCODE_IDS_H |
| 109 | +#ifdef __cplusplus |
| 110 | +extern "C" { |
| 111 | +#endif |
| 112 | +
|
| 113 | +/* Instruction opcodes for compiled code */ |
| 114 | +""" |
| 115 | + ) |
| 116 | + |
| 117 | + def write_define(name: str, op: int) -> None: |
| 118 | + out.emit(f"#define {name:<38} {op:>3}\n") |
| 119 | + |
| 120 | + for op, name in sorted([(op, name) for (name, op) in instmap.items()]): |
| 121 | + write_define(name, op) |
| 122 | + |
| 123 | + out.emit("\n") |
| 124 | + write_define("HAVE_ARGUMENT", len(no_arg)) |
| 125 | + write_define("MIN_INSTRUMENTED_OPCODE", min_instrumented) |
| 126 | + |
| 127 | + out.emit("\n") |
| 128 | + out.emit("#ifdef __cplusplus\n") |
| 129 | + out.emit("}\n") |
| 130 | + out.emit("#endif\n") |
| 131 | + out.emit("#endif /* !Py_OPCODE_IDS_H */\n") |
| 132 | + |
| 133 | + |
| 134 | +arg_parser = argparse.ArgumentParser( |
| 135 | + description="Generate the header file with all opcode IDs.", |
| 136 | + formatter_class=argparse.ArgumentDefaultsHelpFormatter, |
| 137 | +) |
| 138 | + |
| 139 | +arg_parser.add_argument( |
| 140 | + "-o", "--output", type=str, help="Generated code", default=DEFAULT_OUTPUT |
| 141 | +) |
| 142 | + |
| 143 | +arg_parser.add_argument( |
| 144 | + "input", nargs=argparse.REMAINDER, help="Instruction definition file(s)" |
| 145 | +) |
| 146 | + |
| 147 | +if __name__ == "__main__": |
| 148 | + args = arg_parser.parse_args() |
| 149 | + if len(args.input) == 0: |
| 150 | + args.input.append(DEFAULT_INPUT) |
| 151 | + data = analyze_files(args.input) |
| 152 | + with open(args.output, "w") as outfile: |
| 153 | + generate_opcode_header(args.input, data, outfile) |
0 commit comments