forked from python/cpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5eb67cf
commit d67c834
Showing
4 changed files
with
58 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |