| 1 |
"""Generate targets for computed goto dispatch |
| 2 |
Reads the instruction definitions from bytecodes.c. |
| 3 |
Writes the table to opcode_targets.h by default. |
| 4 |
""" |
| 5 |
|
| 6 |
import argparse |
| 7 |
|
| 8 |
from analyzer import ( |
| 9 |
Analysis, |
| 10 |
analyze_files, |
| 11 |
) |
| 12 |
from generators_common import ( |
| 13 |
DEFAULT_INPUT, |
| 14 |
ROOT, |
| 15 |
) |
| 16 |
from tier1_generator import UNKNOWN_OPCODE_HANDLER |
| 17 |
from cwriter import CWriter |
| 18 |
|
| 19 |
|
| 20 |
DEFAULT_OUTPUT = ROOT / "Python/opcode_targets.h" |
| 21 |
|
| 22 |
|
| 23 |
def write_opcode_targets(analysis: Analysis, out: CWriter) -> None: |
| 24 |
"""Write header file that defines the jump target table""" |
| 25 |
targets = ["&&_unknown_opcode,\n"] * 256 |
| 26 |
for name, op in analysis.opmap.items(): |
| 27 |
if op < 256: |
| 28 |
targets[op] = f"&&TARGET_{name},\n" |
| 29 |
out.emit("#if !Py_TAIL_CALL_INTERP\n") |
| 30 |
out.emit("static void *opcode_targets[256] = {\n") |
| 31 |
for target in targets: |
| 32 |
out.emit(target) |
| 33 |
out.emit("};\n") |
| 34 |
out.emit("#else /* Py_TAIL_CALL_INTERP */\n") |
| 35 |
|
| 36 |
def function_proto(name: str) -> str: |
| 37 |
return f"Py_PRESERVE_NONE_CC static PyObject *_TAIL_CALL_{name}(TAIL_CALL_PARAMS)" |
| 38 |
|
| 39 |
|
| 40 |
def write_tailcall_dispatch_table(analysis: Analysis, out: CWriter) -> None: |
| 41 |
out.emit("static py_tail_call_funcptr INSTRUCTION_TABLE[256];\n") |
| 42 |
out.emit("\n") |
| 43 |
|
| 44 |
# Emit function prototypes for labels. |
| 45 |
for name in analysis.labels: |
| 46 |
out.emit(f"{function_proto(name)};\n") |
| 47 |
out.emit("\n") |
| 48 |
|
| 49 |
# Emit function prototypes for opcode handlers. |
| 50 |
for name in sorted(analysis.instructions.keys()): |
| 51 |
out.emit(f"{function_proto(name)};\n") |
| 52 |
out.emit("\n") |
| 53 |
|
| 54 |
# Emit unknown opcode handler. |
| 55 |
out.emit(function_proto("UNKNOWN_OPCODE")) |
| 56 |
out.emit(" {\n") |
| 57 |
out.emit("int opcode = next_instr->op.code;\n") |
| 58 |
out.emit(UNKNOWN_OPCODE_HANDLER) |
| 59 |
out.emit("}\n") |
| 60 |
out.emit("\n") |
| 61 |
|
| 62 |
# Emit the dispatch table. |
| 63 |
out.emit("static py_tail_call_funcptr INSTRUCTION_TABLE[256] = {\n") |
| 64 |
for name in sorted(analysis.instructions.keys()): |
| 65 |
out.emit(f"[{name}] = _TAIL_CALL_{name},\n") |
| 66 |
named_values = analysis.opmap.values() |
| 67 |
for rest in range(256): |
| 68 |
if rest not in named_values: |
| 69 |
out.emit(f"[{rest}] = _TAIL_CALL_UNKNOWN_OPCODE,\n") |
| 70 |
out.emit("};\n") |
| 71 |
outfile.write("#endif /* Py_TAIL_CALL_INTERP */\n") |
| 72 |
|
| 73 |
arg_parser = argparse.ArgumentParser( |
| 74 |
description="Generate the file with dispatch targets.", |
| 75 |
formatter_class=argparse.ArgumentDefaultsHelpFormatter, |
| 76 |
) |
| 77 |
|
| 78 |
arg_parser.add_argument( |
| 79 |
"-o", "--output", type=str, help="Generated code", default=DEFAULT_OUTPUT |
| 80 |
) |
| 81 |
|
| 82 |
arg_parser.add_argument( |
| 83 |
"input", nargs=argparse.REMAINDER, help="Instruction definition file(s)" |
| 84 |
) |
| 85 |
|
| 86 |
if __name__ == "__main__": |
| 87 |
args = arg_parser.parse_args() |
| 88 |
if len(args.input) == 0: |
| 89 |
args.input.append(DEFAULT_INPUT) |
| 90 |
data = analyze_files(args.input) |
| 91 |
with open(args.output, "w") as outfile: |
| 92 |
out = CWriter(outfile, 0, False) |
| 93 |
write_opcode_targets(data, out) |
| 94 |
write_tailcall_dispatch_table(data, out) |