Debian Codegraph @

Tools/cases_generator/tier1_generator.py

raw
1 """Generate the main interpreter switch.
2 Reads the instruction definitions from bytecodes.c.
3 Writes the cases to generated_cases.c.h, which is #included in ceval.c.
4 """
5
6 import argparse
7
8 from analyzer import (
9 Analysis,
10 Instruction,
11 Uop,
12 Label,
13 CodeSection,
14 Part,
15 analyze_files,
16 Skip,
17 Flush,
18 analysis_error,
19 StackItem,
20 )
21 from generators_common import (
22 DEFAULT_INPUT,
23 ROOT,
24 write_header,
25 type_and_null,
26 Emitter,
27 TokenIterator,
28 always_true,
29 emit_to,
30 )
31 from cwriter import CWriter
32 from typing import TextIO
33 from lexer import Token
34 from stack import Local, Stack, StackError, get_stack_effect, Storage
35
36 DEFAULT_OUTPUT = ROOT / "Python/generated_cases.c.h"
37
38
39 FOOTER = "#undef TIER_ONE\n"
40 INSTRUCTION_START_MARKER = "/* BEGIN INSTRUCTIONS */"
41 INSTRUCTION_END_MARKER = "/* END INSTRUCTIONS */"
42 LABEL_START_MARKER = "/* BEGIN LABELS */"
43 LABEL_END_MARKER = "/* END LABELS */"
44
45
46 def declare_variable(var: StackItem, out: CWriter) -> None:
47 type, null = type_and_null(var)
48 space = " " if type[-1].isalnum() else ""
49 out.emit(f"{type}{space}{var.name};\n")
50
51
52 def declare_variables(inst: Instruction, out: CWriter) -> None:
53 try:
54 stack = get_stack_effect(inst)
55 except StackError as ex:
56 raise analysis_error(ex.args[0], inst.where) from None
57 seen = {"unused"}
58 for part in inst.parts:
59 if not isinstance(part, Uop):
60 continue
61 for var in part.stack.inputs:
62 if var.used and var.name not in seen:
63 seen.add(var.name)
64 declare_variable(var, out)
65 for var in part.stack.outputs:
66 if var.used and var.name not in seen:
67 seen.add(var.name)
68 declare_variable(var, out)
69
70
71 def write_uop(
72 uop: Part,
73 emitter: Emitter,
74 offset: int,
75 stack: Stack,
76 inst: Instruction,
77 braces: bool,
78 ) -> tuple[bool, int, Stack]:
79 # out.emit(stack.as_comment() + "\n")
80 if isinstance(uop, Skip):
81 entries = "entries" if uop.size > 1 else "entry"
82 emitter.emit(f"/* Skip {uop.size} cache {entries} */\n")
83 return True, (offset + uop.size), stack
84 if isinstance(uop, Flush):
85 emitter.emit(f"// flush\n")
86 stack.flush(emitter.out)
87 return True, offset, stack
88 locals: dict[str, Local] = {}
89 emitter.out.start_line()
90 if braces:
91 emitter.out.emit(f"// {uop.name}\n")
92 emitter.emit("{\n")
93 stack._print(emitter.out)
94 storage = Storage.for_uop(stack, uop, emitter.out)
95
96 for cache in uop.caches:
97 if cache.name != "unused":
98 if cache.size == 4:
99 type = "PyObject *"
100 reader = "read_obj"
101 else:
102 type = f"uint{cache.size*16}_t "
103 reader = f"read_u{cache.size*16}"
104 emitter.emit(
105 f"{type}{cache.name} = {reader}(&this_instr[{offset}].cache);\n"
106 )
107 if inst.family is None:
108 emitter.emit(f"(void){cache.name};\n")
109 offset += cache.size
110
111 reachable, storage = emitter.emit_tokens(uop, storage, inst, False)
112 if braces:
113 emitter.out.start_line()
114 emitter.emit("}\n")
115 # emitter.emit(stack.as_comment() + "\n")
116 return reachable, offset, storage.stack
117
118
119 def uses_this(inst: Instruction) -> bool:
120 if inst.properties.needs_this:
121 return True
122 for uop in inst.parts:
123 if not isinstance(uop, Uop):
124 continue
125 for cache in uop.caches:
126 if cache.name != "unused":
127 return True
128 # Can't be merged into the loop above, because
129 # this must strictly be performed at the end.
130 for uop in inst.parts:
131 if not isinstance(uop, Uop):
132 continue
133 for tkn in uop.body.tokens():
134 if (tkn.kind == "IDENTIFIER"
135 and (tkn.text in {"DEOPT_IF", "EXIT_IF"})):
136 return True
137 return False
138
139
140 UNKNOWN_OPCODE_HANDLER ="""\
141 _PyErr_Format(tstate, PyExc_SystemError,
142 "%U:%d: unknown opcode %d",
143 _PyFrame_GetCode(frame)->co_filename,
144 PyUnstable_InterpreterFrame_GetLine(frame),
145 opcode);
146 JUMP_TO_LABEL(error);
147 """
148
149 def generate_tier1(
150 filenames: list[str], analysis: Analysis, outfile: TextIO, lines: bool
151 ) -> None:
152 write_header(__file__, filenames, outfile)
153 outfile.write("""
154 #ifdef TIER_TWO
155 #error "This file is for Tier 1 only"
156 #endif
157 #define TIER_ONE 1
158 """)
159 outfile.write(f"""
160 #if !Py_TAIL_CALL_INTERP
161 #if !USE_COMPUTED_GOTOS
162 dispatch_opcode:
163 switch (opcode)
164 #endif
165 {{
166 #endif /* Py_TAIL_CALL_INTERP */
167 {INSTRUCTION_START_MARKER}
168 """
169 )
170 generate_tier1_cases(analysis, outfile, lines)
171 outfile.write(f"""
172 {INSTRUCTION_END_MARKER}
173 #if !Py_TAIL_CALL_INTERP
174 #if USE_COMPUTED_GOTOS
175 _unknown_opcode:
176 #else
177 EXTRA_CASES // From pycore_opcode_metadata.h, a 'case' for each unused opcode
178 #endif
179 /* Tell C compilers not to hold the opcode variable in the loop.
180 next_instr points the current instruction without TARGET(). */
181 opcode = next_instr->op.code;
182 {UNKNOWN_OPCODE_HANDLER}
183
184 }}
185
186 /* This should never be reached. Every opcode should end with DISPATCH()
187 or goto error. */
188 Py_UNREACHABLE();
189 #endif /* Py_TAIL_CALL_INTERP */
190 {LABEL_START_MARKER}
191 """)
192 out = CWriter(outfile, 2, lines)
193 emitter = Emitter(out, analysis.labels)
194 generate_tier1_labels(analysis, emitter)
195 outfile.write(f"{LABEL_END_MARKER}\n")
196 outfile.write(FOOTER)
197
198
199
200 def generate_tier1_labels(
201 analysis: Analysis, emitter: Emitter
202 ) -> None:
203 emitter.emit("\n")
204 # Emit tail-callable labels as function defintions
205 for name, label in analysis.labels.items():
206 emitter.emit(f"LABEL({name})\n")
207 storage = Storage(Stack(), [], [], 0, False)
208 if label.spilled:
209 storage.spilled = 1
210 emitter.emit_tokens(label, storage, None)
211 emitter.emit("\n\n")
212
213 def get_popped(inst: Instruction, analysis: Analysis) -> str:
214 stack = get_stack_effect(inst)
215 return (-stack.base_offset).to_c()
216
217 def generate_tier1_cases(
218 analysis: Analysis, outfile: TextIO, lines: bool
219 ) -> None:
220 out = CWriter(outfile, 2, lines)
221 emitter = Emitter(out, analysis.labels)
222 out.emit("\n")
223 for name, inst in sorted(analysis.instructions.items()):
224 out.emit("\n")
225 out.emit(f"TARGET({name}) {{\n")
226 popped = get_popped(inst, analysis)
227 # We need to ifdef it because this breaks platforms
228 # without computed gotos/tail calling.
229 out.emit(f"#if Py_TAIL_CALL_INTERP\n")
230 out.emit(f"int opcode = {name};\n")
231 out.emit(f"(void)(opcode);\n")
232 out.emit(f"#endif\n")
233 needs_this = uses_this(inst)
234 unused_guard = "(void)this_instr;\n"
235 if inst.properties.needs_prev:
236 out.emit(f"_Py_CODEUNIT* const prev_instr = frame->instr_ptr;\n")
237
238 if needs_this and not inst.is_target:
239 out.emit(f"_Py_CODEUNIT* const this_instr = next_instr;\n")
240 out.emit(unused_guard)
241 if not inst.properties.no_save_ip:
242 out.emit(f"frame->instr_ptr = next_instr;\n")
243
244 out.emit(f"next_instr += {inst.size};\n")
245 out.emit(f"INSTRUCTION_STATS({name});\n")
246 if inst.is_target:
247 out.emit(f"PREDICTED_{name}:;\n")
248 if needs_this:
249 out.emit(f"_Py_CODEUNIT* const this_instr = next_instr - {inst.size};\n")
250 out.emit(unused_guard)
251 if inst.properties.uses_opcode:
252 out.emit(f"opcode = {name};\n")
253 if inst.family is not None:
254 out.emit(
255 f"static_assert({inst.family.size} == {inst.size-1}"
256 ', "incorrect cache size");\n'
257 )
258 declare_variables(inst, out)
259 offset = 1 # The instruction itself
260 stack = Stack()
261 for part in inst.parts:
262 # Only emit braces if more than one uop
263 insert_braces = len([p for p in inst.parts if isinstance(p, Uop)]) > 1
264 reachable, offset, stack = write_uop(part, emitter, offset, stack, inst, insert_braces)
265 out.start_line()
266 if reachable: # type: ignore[possibly-undefined]
267 stack.flush(out)
268 out.emit("DISPATCH();\n")
269 out.start_line()
270 out.emit("}")
271 out.emit("\n")
272
273
274 arg_parser = argparse.ArgumentParser(
275 description="Generate the code for the interpreter switch.",
276 formatter_class=argparse.ArgumentDefaultsHelpFormatter,
277 )
278
279 arg_parser.add_argument(
280 "-o", "--output", type=str, help="Generated code", default=DEFAULT_OUTPUT
281 )
282
283 arg_parser.add_argument(
284 "-l", "--emit-line-directives", help="Emit #line directives", action="store_true"
285 )
286
287 arg_parser.add_argument(
288 "input", nargs=argparse.REMAINDER, help="Instruction definition file(s)"
289 )
290
291
292 def generate_tier1_from_files(
293 filenames: list[str], outfilename: str, lines: bool
294 ) -> None:
295 data = analyze_files(filenames)
296 with open(outfilename, "w") as outfile:
297 generate_tier1(filenames, data, outfile, lines)
298
299
300 if __name__ == "__main__":
301 args = arg_parser.parse_args()
302 if len(args.input) == 0:
303 args.input.append(DEFAULT_INPUT)
304 data = analyze_files(args.input)
305 with open(args.output, "w") as outfile:
306 generate_tier1(args.input, data, outfile, args.emit_line_directives)