| 1 |
"""Generate the list of uop IDs. |
| 2 |
Reads the instruction definitions from bytecodes.c. |
| 3 |
Writes the IDs to pycore_uop_ids.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 |
) |
| 17 |
from cwriter import CWriter |
| 18 |
from typing import TextIO |
| 19 |
|
| 20 |
|
| 21 |
DEFAULT_OUTPUT = ROOT / "Include/internal/pycore_uop_ids.h" |
| 22 |
|
| 23 |
|
| 24 |
def generate_uop_ids( |
| 25 |
filenames: list[str], analysis: Analysis, outfile: TextIO, distinct_namespace: bool |
| 26 |
) -> None: |
| 27 |
write_header(__file__, filenames, outfile) |
| 28 |
out = CWriter(outfile, 0, False) |
| 29 |
with out.header_guard("Py_CORE_UOP_IDS_H"): |
| 30 |
next_id = 1 if distinct_namespace else 300 |
| 31 |
# These two are first by convention |
| 32 |
out.emit(f"#define _EXIT_TRACE {next_id}\n") |
| 33 |
next_id += 1 |
| 34 |
out.emit(f"#define _SET_IP {next_id}\n") |
| 35 |
next_id += 1 |
| 36 |
PRE_DEFINED = {"_EXIT_TRACE", "_SET_IP"} |
| 37 |
|
| 38 |
uops = [(uop.name, uop) for uop in analysis.uops.values()] |
| 39 |
# Sort so that _BASE comes immediately before _BASE_0, etc. |
| 40 |
for name, uop in sorted(uops): |
| 41 |
if name in PRE_DEFINED: |
| 42 |
continue |
| 43 |
if uop.properties.tier == 1: |
| 44 |
continue |
| 45 |
if uop.implicitly_created and not distinct_namespace and not uop.replicated: |
| 46 |
out.emit(f"#define {name} {name[1:]}\n") |
| 47 |
else: |
| 48 |
out.emit(f"#define {name} {next_id}\n") |
| 49 |
next_id += 1 |
| 50 |
|
| 51 |
out.emit(f"#define MAX_UOP_ID {next_id-1}\n") |
| 52 |
|
| 53 |
|
| 54 |
arg_parser = argparse.ArgumentParser( |
| 55 |
description="Generate the header file with all uop IDs.", |
| 56 |
formatter_class=argparse.ArgumentDefaultsHelpFormatter, |
| 57 |
) |
| 58 |
|
| 59 |
arg_parser.add_argument( |
| 60 |
"-o", "--output", type=str, help="Generated code", default=DEFAULT_OUTPUT |
| 61 |
) |
| 62 |
arg_parser.add_argument( |
| 63 |
"-n", |
| 64 |
"--namespace", |
| 65 |
help="Give uops a distinct namespace", |
| 66 |
action="store_true", |
| 67 |
) |
| 68 |
|
| 69 |
arg_parser.add_argument( |
| 70 |
"input", nargs=argparse.REMAINDER, help="Instruction definition file(s)" |
| 71 |
) |
| 72 |
|
| 73 |
if __name__ == "__main__": |
| 74 |
args = arg_parser.parse_args() |
| 75 |
if len(args.input) == 0: |
| 76 |
args.input.append(DEFAULT_INPUT) |
| 77 |
data = analyze_files(args.input) |
| 78 |
with open(args.output, "w") as outfile: |
| 79 |
generate_uop_ids(args.input, data, outfile, args.namespace) |