gh-128563: A new tail-calling interpreter (GH-128718)

Co-authored-by: Garrett Gu <garrettgu777@gmail.com>
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
This commit is contained in:
Ken Jin 2025-02-06 23:21:57 +08:00 committed by GitHub
parent 555dc50c81
commit cb640b659e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
16 changed files with 3883 additions and 623 deletions

View file

@ -13,6 +13,7 @@ from generators_common import (
DEFAULT_INPUT,
ROOT,
)
from tier1_generator import UNKNOWN_OPCODE_HANDLER
from cwriter import CWriter
@ -25,11 +26,49 @@ def write_opcode_targets(analysis: Analysis, out: CWriter) -> None:
for name, op in analysis.opmap.items():
if op < 256:
targets[op] = f"&&TARGET_{name},\n"
out.emit("#ifndef Py_TAIL_CALL_INTERP\n")
out.emit("static void *opcode_targets[256] = {\n")
for target in targets:
out.emit(target)
out.emit("};\n")
out.emit("#else /* Py_TAIL_CALL_INTERP */\n")
def function_proto(name: str) -> str:
return f"Py_PRESERVE_NONE_CC static PyObject *_TAIL_CALL_{name}(TAIL_CALL_PARAMS)"
def write_tailcall_dispatch_table(analysis: Analysis, out: CWriter) -> None:
out.emit("static py_tail_call_funcptr INSTRUCTION_TABLE[256];\n")
out.emit("\n")
# Emit function prototypes for labels.
for name in analysis.labels:
out.emit(f"{function_proto(name)};\n")
out.emit("\n")
# Emit function prototypes for opcode handlers.
for name in sorted(analysis.instructions.keys()):
out.emit(f"{function_proto(name)};\n")
out.emit("\n")
# Emit unknown opcode handler.
out.emit(function_proto("UNKNOWN_OPCODE"))
out.emit(" {\n")
out.emit("int opcode = next_instr->op.code;\n")
out.emit(UNKNOWN_OPCODE_HANDLER)
out.emit("}\n")
out.emit("\n")
# Emit the dispatch table.
out.emit("static py_tail_call_funcptr INSTRUCTION_TABLE[256] = {\n")
for name in sorted(analysis.instructions.keys()):
out.emit(f"[{name}] = _TAIL_CALL_{name},\n")
named_values = analysis.opmap.values()
for rest in range(256):
if rest not in named_values:
out.emit(f"[{rest}] = _TAIL_CALL_UNKNOWN_OPCODE,\n")
out.emit("};\n")
outfile.write("#endif /* Py_TAIL_CALL_INTERP */\n")
arg_parser = argparse.ArgumentParser(
description="Generate the file with dispatch targets.",
@ -52,3 +91,4 @@ if __name__ == "__main__":
with open(args.output, "w") as outfile:
out = CWriter(outfile, 0, False)
write_opcode_targets(data, out)
write_tailcall_dispatch_table(data, out)