summaryrefslogtreecommitdiffstats
path: root/Tools
diff options
context:
space:
mode:
authorMark Shannon <mark@hotpy.org>2023-12-20 15:09:12 (GMT)
committerGitHub <noreply@github.com>2023-12-20 15:09:12 (GMT)
commitde8a4e52a5f5e5f0c5057afd4c391afccfca57d3 (patch)
tree8163483e13adc136f633efbd7148b99f0b1bb9de /Tools
parente96f26083bff31e86c068aa22542e91f38293ea3 (diff)
downloadcpython-de8a4e52a5f5e5f0c5057afd4c391afccfca57d3.zip
cpython-de8a4e52a5f5e5f0c5057afd4c391afccfca57d3.tar.gz
cpython-de8a4e52a5f5e5f0c5057afd4c391afccfca57d3.tar.bz2
GH-111485: Generate `TARGET` table for computed goto dispatch. (GH-113319)
Diffstat (limited to 'Tools')
-rw-r--r--Tools/cases_generator/generate_cases.py1
-rw-r--r--Tools/cases_generator/target_generator.py54
2 files changed, 54 insertions, 1 deletions
diff --git a/Tools/cases_generator/generate_cases.py b/Tools/cases_generator/generate_cases.py
index bb027f3..73b5fc2 100644
--- a/Tools/cases_generator/generate_cases.py
+++ b/Tools/cases_generator/generate_cases.py
@@ -839,7 +839,6 @@ def main() -> None:
# These raise OSError if output can't be written
a.assign_opcode_ids()
- a.write_opcode_targets(args.opcode_targets_h)
a.write_abstract_interpreter_instructions(
args.abstract_interpreter_cases, args.emit_line_directives
)
diff --git a/Tools/cases_generator/target_generator.py b/Tools/cases_generator/target_generator.py
new file mode 100644
index 0000000..44a699c
--- /dev/null
+++ b/Tools/cases_generator/target_generator.py
@@ -0,0 +1,54 @@
+"""Generate targets for computed goto dispatch
+Reads the instruction definitions from bytecodes.c.
+Writes the table to opcode_targets.h by default.
+"""
+
+import argparse
+
+from analyzer import (
+ Analysis,
+ analyze_files,
+)
+from generators_common import (
+ DEFAULT_INPUT,
+ ROOT,
+)
+from cwriter import CWriter
+from typing import TextIO
+
+
+DEFAULT_OUTPUT = ROOT / "Python/opcode_targets.h"
+
+
+def write_opcode_targets(analysis: Analysis, out: CWriter) -> None:
+ """Write header file that defines the jump target table"""
+ targets = ["&&_unknown_opcode,\n"] * 256
+ for name, op in analysis.opmap.items():
+ if op < 256:
+ targets[op] = f"&&TARGET_{name},\n"
+ out.emit("static void *opcode_targets[256] = {\n")
+ for target in targets:
+ out.emit(target)
+ out.emit("};\n")
+
+arg_parser = argparse.ArgumentParser(
+ description="Generate the file with dispatch targets.",
+ formatter_class=argparse.ArgumentDefaultsHelpFormatter,
+)
+
+arg_parser.add_argument(
+ "-o", "--output", type=str, help="Generated code", default=DEFAULT_OUTPUT
+)
+
+arg_parser.add_argument(
+ "input", nargs=argparse.REMAINDER, help="Instruction definition file(s)"
+)
+
+if __name__ == "__main__":
+ args = arg_parser.parse_args()
+ if len(args.input) == 0:
+ args.input.append(DEFAULT_INPUT)
+ data = analyze_files(args.input)
+ with open(args.output, "w") as outfile:
+ out = CWriter(outfile, 0, False)
+ write_opcode_targets(data, out)