| 1 |
"""Generate uop metadata. |
| 2 |
Reads the instruction definitions from bytecodes.c. |
| 3 |
Writes the metadata to pycore_uop_metadata.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 |
write_header, |
| 16 |
cflags, |
| 17 |
) |
| 18 |
from stack import Stack |
| 19 |
from cwriter import CWriter |
| 20 |
from typing import TextIO |
| 21 |
|
| 22 |
DEFAULT_OUTPUT = ROOT / "Include/internal/pycore_uop_metadata.h" |
| 23 |
|
| 24 |
|
| 25 |
def generate_names_and_flags(analysis: Analysis, out: CWriter) -> None: |
| 26 |
out.emit("extern const uint16_t _PyUop_Flags[MAX_UOP_ID+1];\n") |
| 27 |
out.emit("extern const uint8_t _PyUop_Replication[MAX_UOP_ID+1];\n") |
| 28 |
out.emit("extern const char * const _PyOpcode_uop_name[MAX_UOP_ID+1];\n\n") |
| 29 |
out.emit("extern int _PyUop_num_popped(int opcode, int oparg);\n\n") |
| 30 |
out.emit("#ifdef NEED_OPCODE_METADATA\n") |
| 31 |
out.emit("const uint16_t _PyUop_Flags[MAX_UOP_ID+1] = {\n") |
| 32 |
for uop in analysis.uops.values(): |
| 33 |
if uop.is_viable() and uop.properties.tier != 1: |
| 34 |
out.emit(f"[{uop.name}] = {cflags(uop.properties)},\n") |
| 35 |
|
| 36 |
out.emit("};\n\n") |
| 37 |
out.emit("const uint8_t _PyUop_Replication[MAX_UOP_ID+1] = {\n") |
| 38 |
for uop in analysis.uops.values(): |
| 39 |
if uop.replicated: |
| 40 |
out.emit(f"[{uop.name}] = {uop.replicated},\n") |
| 41 |
|
| 42 |
out.emit("};\n\n") |
| 43 |
out.emit("const char *const _PyOpcode_uop_name[MAX_UOP_ID+1] = {\n") |
| 44 |
for uop in sorted(analysis.uops.values(), key=lambda t: t.name): |
| 45 |
if uop.is_viable() and uop.properties.tier != 1: |
| 46 |
out.emit(f'[{uop.name}] = "{uop.name}",\n') |
| 47 |
out.emit("};\n") |
| 48 |
out.emit("int _PyUop_num_popped(int opcode, int oparg)\n{\n") |
| 49 |
out.emit("switch(opcode) {\n") |
| 50 |
null = CWriter.null() |
| 51 |
for uop in analysis.uops.values(): |
| 52 |
if uop.is_viable() and uop.properties.tier != 1: |
| 53 |
stack = Stack() |
| 54 |
for var in reversed(uop.stack.inputs): |
| 55 |
if var.peek: |
| 56 |
break |
| 57 |
stack.pop(var, null) |
| 58 |
popped = (-stack.base_offset).to_c() |
| 59 |
out.emit(f"case {uop.name}:\n") |
| 60 |
out.emit(f" return {popped};\n") |
| 61 |
out.emit("default:\n") |
| 62 |
out.emit(" return -1;\n") |
| 63 |
out.emit("}\n") |
| 64 |
out.emit("}\n\n") |
| 65 |
out.emit("#endif // NEED_OPCODE_METADATA\n\n") |
| 66 |
|
| 67 |
|
| 68 |
def generate_uop_metadata( |
| 69 |
filenames: list[str], analysis: Analysis, outfile: TextIO |
| 70 |
) -> None: |
| 71 |
write_header(__file__, filenames, outfile) |
| 72 |
out = CWriter(outfile, 0, False) |
| 73 |
with out.header_guard("Py_CORE_UOP_METADATA_H"): |
| 74 |
out.emit("#include <stdint.h>\n") |
| 75 |
out.emit('#include "pycore_uop_ids.h"\n') |
| 76 |
generate_names_and_flags(analysis, out) |
| 77 |
|
| 78 |
|
| 79 |
arg_parser = argparse.ArgumentParser( |
| 80 |
description="Generate the header file with uop metadata.", |
| 81 |
formatter_class=argparse.ArgumentDefaultsHelpFormatter, |
| 82 |
) |
| 83 |
|
| 84 |
arg_parser.add_argument( |
| 85 |
"-o", "--output", type=str, help="Generated code", default=DEFAULT_OUTPUT |
| 86 |
) |
| 87 |
|
| 88 |
arg_parser.add_argument( |
| 89 |
"input", nargs=argparse.REMAINDER, help="Instruction definition file(s)" |
| 90 |
) |
| 91 |
|
| 92 |
if __name__ == "__main__": |
| 93 |
args = arg_parser.parse_args() |
| 94 |
if len(args.input) == 0: |
| 95 |
args.input.append(DEFAULT_INPUT) |
| 96 |
data = analyze_files(args.input) |
| 97 |
with open(args.output, "w") as outfile: |
| 98 |
generate_uop_metadata(args.input, data, outfile) |