| 1 |
"""Generate the cases for the tier 2 interpreter. |
| 2 |
Reads the instruction definitions from bytecodes.c. |
| 3 |
Writes the cases to executor_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 |
analyze_files, |
| 15 |
StackItem, |
| 16 |
analysis_error, |
| 17 |
) |
| 18 |
from generators_common import ( |
| 19 |
DEFAULT_INPUT, |
| 20 |
ROOT, |
| 21 |
emit_to, |
| 22 |
write_header, |
| 23 |
type_and_null, |
| 24 |
Emitter, |
| 25 |
TokenIterator, |
| 26 |
always_true, |
| 27 |
) |
| 28 |
from cwriter import CWriter |
| 29 |
from typing import TextIO |
| 30 |
from lexer import Token |
| 31 |
from stack import Local, Stack, StackError, Storage |
| 32 |
|
| 33 |
DEFAULT_OUTPUT = ROOT / "Python/executor_cases.c.h" |
| 34 |
|
| 35 |
|
| 36 |
def declare_variable( |
| 37 |
var: StackItem, uop: Uop, seen: set[str], out: CWriter |
| 38 |
) -> None: |
| 39 |
if not var.used or var.name in seen: |
| 40 |
return |
| 41 |
seen.add(var.name) |
| 42 |
type, null = type_and_null(var) |
| 43 |
space = " " if type[-1].isalnum() else "" |
| 44 |
out.emit(f"{type}{space}{var.name};\n") |
| 45 |
|
| 46 |
|
| 47 |
def declare_variables(uop: Uop, out: CWriter) -> None: |
| 48 |
stack = Stack() |
| 49 |
null = CWriter.null() |
| 50 |
for var in reversed(uop.stack.inputs): |
| 51 |
stack.pop(var, null) |
| 52 |
for var in uop.stack.outputs: |
| 53 |
stack.push(Local.undefined(var)) |
| 54 |
seen = {"unused"} |
| 55 |
for var in reversed(uop.stack.inputs): |
| 56 |
declare_variable(var, uop, seen, out) |
| 57 |
for var in uop.stack.outputs: |
| 58 |
declare_variable(var, uop, seen, out) |
| 59 |
|
| 60 |
|
| 61 |
class Tier2Emitter(Emitter): |
| 62 |
|
| 63 |
def __init__(self, out: CWriter, labels: dict[str, Label]): |
| 64 |
super().__init__(out, labels) |
| 65 |
self._replacers["oparg"] = self.oparg |
| 66 |
|
| 67 |
def goto_error(self, offset: int, storage: Storage) -> str: |
| 68 |
# To do: Add jump targets for popping values. |
| 69 |
if offset != 0: |
| 70 |
storage.copy().flush(self.out) |
| 71 |
return f"JUMP_TO_ERROR();" |
| 72 |
|
| 73 |
def deopt_if( |
| 74 |
self, |
| 75 |
tkn: Token, |
| 76 |
tkn_iter: TokenIterator, |
| 77 |
uop: CodeSection, |
| 78 |
storage: Storage, |
| 79 |
inst: Instruction | None, |
| 80 |
) -> bool: |
| 81 |
self.out.emit_at("if ", tkn) |
| 82 |
lparen = next(tkn_iter) |
| 83 |
self.emit(lparen) |
| 84 |
assert lparen.kind == "LPAREN" |
| 85 |
first_tkn = tkn_iter.peek() |
| 86 |
emit_to(self.out, tkn_iter, "RPAREN") |
| 87 |
next(tkn_iter) # Semi colon |
| 88 |
self.emit(") {\n") |
| 89 |
self.emit("UOP_STAT_INC(uopcode, miss);\n") |
| 90 |
self.emit("JUMP_TO_JUMP_TARGET();\n") |
| 91 |
self.emit("}\n") |
| 92 |
return not always_true(first_tkn) |
| 93 |
|
| 94 |
def exit_if( |
| 95 |
self, |
| 96 |
tkn: Token, |
| 97 |
tkn_iter: TokenIterator, |
| 98 |
uop: CodeSection, |
| 99 |
storage: Storage, |
| 100 |
inst: Instruction | None, |
| 101 |
) -> bool: |
| 102 |
self.out.emit_at("if ", tkn) |
| 103 |
lparen = next(tkn_iter) |
| 104 |
self.emit(lparen) |
| 105 |
first_tkn = tkn_iter.peek() |
| 106 |
emit_to(self.out, tkn_iter, "RPAREN") |
| 107 |
next(tkn_iter) # Semi colon |
| 108 |
self.emit(") {\n") |
| 109 |
self.emit("UOP_STAT_INC(uopcode, miss);\n") |
| 110 |
self.emit("JUMP_TO_JUMP_TARGET();\n") |
| 111 |
self.emit("}\n") |
| 112 |
return not always_true(first_tkn) |
| 113 |
|
| 114 |
def oparg( |
| 115 |
self, |
| 116 |
tkn: Token, |
| 117 |
tkn_iter: TokenIterator, |
| 118 |
uop: CodeSection, |
| 119 |
storage: Storage, |
| 120 |
inst: Instruction | None, |
| 121 |
) -> bool: |
| 122 |
if not uop.name.endswith("_0") and not uop.name.endswith("_1"): |
| 123 |
self.emit(tkn) |
| 124 |
return True |
| 125 |
amp = next(tkn_iter) |
| 126 |
if amp.text != "&": |
| 127 |
self.emit(tkn) |
| 128 |
self.emit(amp) |
| 129 |
return True |
| 130 |
one = next(tkn_iter) |
| 131 |
assert one.text == "1" |
| 132 |
self.out.emit_at(uop.name[-1], tkn) |
| 133 |
return True |
| 134 |
|
| 135 |
|
| 136 |
def write_uop(uop: Uop, emitter: Emitter, stack: Stack) -> Stack: |
| 137 |
locals: dict[str, Local] = {} |
| 138 |
try: |
| 139 |
emitter.out.start_line() |
| 140 |
if uop.properties.oparg: |
| 141 |
emitter.emit("oparg = CURRENT_OPARG();\n") |
| 142 |
assert uop.properties.const_oparg < 0 |
| 143 |
elif uop.properties.const_oparg >= 0: |
| 144 |
emitter.emit(f"oparg = {uop.properties.const_oparg};\n") |
| 145 |
emitter.emit(f"assert(oparg == CURRENT_OPARG());\n") |
| 146 |
storage = Storage.for_uop(stack, uop, emitter.out) |
| 147 |
idx = 0 |
| 148 |
for cache in uop.caches: |
| 149 |
if cache.name != "unused": |
| 150 |
if cache.size == 4: |
| 151 |
type = cast = "PyObject *" |
| 152 |
else: |
| 153 |
type = f"uint{cache.size*16}_t " |
| 154 |
cast = f"uint{cache.size*16}_t" |
| 155 |
emitter.emit(f"{type}{cache.name} = ({cast})CURRENT_OPERAND{idx}();\n") |
| 156 |
idx += 1 |
| 157 |
_, storage = emitter.emit_tokens(uop, storage, None, False) |
| 158 |
storage.flush(emitter.out) |
| 159 |
except StackError as ex: |
| 160 |
raise analysis_error(ex.args[0], uop.body.open) from None |
| 161 |
return storage.stack |
| 162 |
|
| 163 |
SKIPS = ("_EXTENDED_ARG",) |
| 164 |
|
| 165 |
|
| 166 |
def generate_tier2( |
| 167 |
filenames: list[str], analysis: Analysis, outfile: TextIO, lines: bool |
| 168 |
) -> None: |
| 169 |
write_header(__file__, filenames, outfile) |
| 170 |
outfile.write( |
| 171 |
""" |
| 172 |
#ifdef TIER_ONE |
| 173 |
#error "This file is for Tier 2 only" |
| 174 |
#endif |
| 175 |
#define TIER_TWO 2 |
| 176 |
""" |
| 177 |
) |
| 178 |
out = CWriter(outfile, 2, lines) |
| 179 |
emitter = Tier2Emitter(out, analysis.labels) |
| 180 |
out.emit("\n") |
| 181 |
for name, uop in analysis.uops.items(): |
| 182 |
if uop.properties.tier == 1: |
| 183 |
continue |
| 184 |
if uop.is_super(): |
| 185 |
continue |
| 186 |
why_not_viable = uop.why_not_viable() |
| 187 |
if why_not_viable is not None: |
| 188 |
out.emit( |
| 189 |
f"/* {uop.name} is not a viable micro-op for tier 2 because it {why_not_viable} */\n\n" |
| 190 |
) |
| 191 |
continue |
| 192 |
out.emit(f"case {uop.name}: {{\n") |
| 193 |
declare_variables(uop, out) |
| 194 |
stack = Stack() |
| 195 |
stack = write_uop(uop, emitter, stack) |
| 196 |
out.start_line() |
| 197 |
if not uop.properties.always_exits: |
| 198 |
out.emit("break;\n") |
| 199 |
out.start_line() |
| 200 |
out.emit("}") |
| 201 |
out.emit("\n\n") |
| 202 |
outfile.write("#undef TIER_TWO\n") |
| 203 |
|
| 204 |
|
| 205 |
arg_parser = argparse.ArgumentParser( |
| 206 |
description="Generate the code for the tier 2 interpreter.", |
| 207 |
formatter_class=argparse.ArgumentDefaultsHelpFormatter, |
| 208 |
) |
| 209 |
|
| 210 |
arg_parser.add_argument( |
| 211 |
"-o", "--output", type=str, help="Generated code", default=DEFAULT_OUTPUT |
| 212 |
) |
| 213 |
|
| 214 |
arg_parser.add_argument( |
| 215 |
"-l", "--emit-line-directives", help="Emit #line directives", action="store_true" |
| 216 |
) |
| 217 |
|
| 218 |
arg_parser.add_argument( |
| 219 |
"input", nargs=argparse.REMAINDER, help="Instruction definition file(s)" |
| 220 |
) |
| 221 |
|
| 222 |
if __name__ == "__main__": |
| 223 |
args = arg_parser.parse_args() |
| 224 |
if len(args.input) == 0: |
| 225 |
args.input.append(DEFAULT_INPUT) |
| 226 |
data = analyze_files(args.input) |
| 227 |
with open(args.output, "w") as outfile: |
| 228 |
generate_tier2(args.input, data, outfile, args.emit_line_directives) |