Debian Codegraph @

Tools/cases_generator/uop_id_generator.py

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