|
| 1 | +#!/usr/bin/env python |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +import enum |
| 5 | +import functools |
| 6 | +import io |
| 7 | +import pathlib |
| 8 | +import subprocess |
| 9 | +import sys |
| 10 | +import typing |
| 11 | + |
| 12 | +import tomllib |
| 13 | + |
| 14 | +CPYTHON_VERSION = "v3.13.9" |
| 15 | + |
| 16 | + |
| 17 | +CRATE_ROOT = pathlib.Path(__file__).parent |
| 18 | +CONF_FILE = CRATE_ROOT / "instructions.toml" |
| 19 | +OUT_FILE = CRATE_ROOT / "src" / "bytecode" / "instruction.rs" |
| 20 | + |
| 21 | +ROOT = CRATE_ROOT.parents[1] |
| 22 | +SUBMODULES = ROOT / "submodules" |
| 23 | +CPYTHON_DIR = SUBMODULES / f"cpython-{CPYTHON_VERSION}" |
| 24 | +CPYTHON_TOOLS_DIR = CPYTHON_DIR / "Tools" / "cases_generator" |
| 25 | +DIS_DOC = CPYTHON_DIR / "Doc" / "library" / "dis.rst" |
| 26 | + |
| 27 | +sys.path.append(CPYTHON_TOOLS_DIR.as_posix()) |
| 28 | + |
| 29 | +import analyzer |
| 30 | +from generators_common import DEFAULT_INPUT |
| 31 | + |
| 32 | + |
| 33 | +class Inst: |
| 34 | + def __init__( |
| 35 | + self, cpython_name: str, override: dict, analysis: analyzer.Analysis |
| 36 | + ) -> None: |
| 37 | + inst = analysis.instructions[cpython_name] |
| 38 | + properties = inst.properties |
| 39 | + |
| 40 | + self.name = override.get("name", snake_case_to_pascal_case(cpython_name)) |
| 41 | + self.id = analysis.opmap[cpython_name] |
| 42 | + self.has_oparg = override.get("has_oparg", properties.oparg) |
| 43 | + |
| 44 | + if (oparg_typ := override.get("oparg_typ")) is not None: |
| 45 | + self.oparg_typ = getattr(Oparg, oparg_typ) |
| 46 | + elif self.has_oparg: |
| 47 | + self.oparg_typ = Oparg.from_properties(properties) |
| 48 | + |
| 49 | + if (oparg_name := override.get("oparg_name")) is not None: |
| 50 | + self.oparg_name = oparg_name |
| 51 | + elif self.has_oparg: |
| 52 | + oparg_map = build_oparg_name_map() |
| 53 | + self.oparg_name = oparg_map.get(cpython_name, self.oparg_typ.field_name) |
| 54 | + |
| 55 | + @property |
| 56 | + def variant(self) -> str: |
| 57 | + if self.has_oparg: |
| 58 | + fields = f"{{ {self.oparg_name}: Arg<{self.oparg_typ.name}> }}" |
| 59 | + else: |
| 60 | + fields = "" |
| 61 | + |
| 62 | + return f"{self.name} {fields} = {self.id}" |
| 63 | + |
| 64 | + def __lt__(self, other) -> bool: |
| 65 | + return self.name < other.name |
| 66 | + |
| 67 | + |
| 68 | +@enum.unique |
| 69 | +class Oparg(enum.StrEnum): |
| 70 | + IntrinsicFunction1 = enum.auto() |
| 71 | + IntrinsicFunction2 = enum.auto() |
| 72 | + ResumeKind = enum.auto() |
| 73 | + Label = enum.auto() |
| 74 | + NameIdx = enum.auto() |
| 75 | + u32 = enum.auto() # TODO: Remove this; Everything needs to be a newtype |
| 76 | + |
| 77 | + @property |
| 78 | + def field_name(self) -> str: |
| 79 | + match self: |
| 80 | + case self.Label: |
| 81 | + return "target" |
| 82 | + case self.NameIdx: |
| 83 | + return "namei" |
| 84 | + case _: |
| 85 | + return "idx" # Fallback to `idx` |
| 86 | + |
| 87 | + @classmethod |
| 88 | + def from_properties(cls, properties: analyzer.Properties) -> typing.Self: |
| 89 | + if properties.uses_co_names: |
| 90 | + return cls.NameIdx |
| 91 | + elif properties.jumps: |
| 92 | + return cls.Label |
| 93 | + elif properties.uses_co_consts: |
| 94 | + return cls.u32 # TODO: Needs to be `ConstIdx` |
| 95 | + elif properties.uses_locals: |
| 96 | + return cls.u32 # TODO: Needs to be `ConstIdx` |
| 97 | + else: |
| 98 | + # TODO: Raise here. |
| 99 | + return cls.u32 # Fallback to something generic |
| 100 | + |
| 101 | + |
| 102 | +@functools.cache |
| 103 | +def build_oparg_name_map() -> dict[str, str]: |
| 104 | + doc = DIS_DOC.read_text() |
| 105 | + |
| 106 | + out = {} |
| 107 | + for line in doc.splitlines(): |
| 108 | + if not line.startswith(".. opcode:: "): |
| 109 | + continue |
| 110 | + |
| 111 | + # At this point `line` would look something like: |
| 112 | + # |
| 113 | + # `.. opcode:: OPCODE_NAME` |
| 114 | + # or |
| 115 | + # `.. opcode:: OPCODE_NAME (oparg_name)` |
| 116 | + # |
| 117 | + # We only care about the later. |
| 118 | + |
| 119 | + parts = line.split() |
| 120 | + if len(parts) != 4: |
| 121 | + continue |
| 122 | + |
| 123 | + _, _, cpython_name, oparg = parts |
| 124 | + out[cpython_name] = oparg.removeprefix("(").removesuffix(")") |
| 125 | + |
| 126 | + return out |
| 127 | + |
| 128 | + |
| 129 | +def snake_case_to_pascal_case(name: str) -> str: |
| 130 | + return name.title().replace("_", "") |
| 131 | + |
| 132 | + |
| 133 | +def rustfmt(code: str) -> str: |
| 134 | + return subprocess.check_output(["rustfmt", "--emit=stdout"], input=code, text=True) |
| 135 | + |
| 136 | + |
| 137 | +def get_analysis() -> analyser.Analysis: |
| 138 | + analysis = analyzer.analyze_files([DEFAULT_INPUT]) |
| 139 | + |
| 140 | + # We don't differentiate between real and pseudos yet |
| 141 | + analysis.instructions |= analysis.pseudos |
| 142 | + return analysis |
| 143 | + |
| 144 | + |
| 145 | +def write_enum(outfile: typing.IO, instructions: list[Inst]) -> None: |
| 146 | + variants = ",\n".join(inst.variant for inst in instructions) |
| 147 | + outfile.write( |
| 148 | + f""" |
| 149 | + /// A Single bytecode instruction. |
| 150 | + #[repr(u8)] |
| 151 | + #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 152 | + pub enum Instruction {{ |
| 153 | + {variants} |
| 154 | + }} |
| 155 | + """ |
| 156 | + ) |
| 157 | + |
| 158 | + |
| 159 | +def main(): |
| 160 | + conf = tomllib.loads(CONF_FILE.read_text()) |
| 161 | + opcodes = conf["opcodes"] |
| 162 | + analysis = get_analysis() |
| 163 | + instructions = sorted( |
| 164 | + Inst(name, opcodes.get(name, {}), analysis) |
| 165 | + for name in analysis.instructions |
| 166 | + if opcodes.get(name, {}).get("enabled", True) |
| 167 | + ) |
| 168 | + |
| 169 | + outfile = io.StringIO() |
| 170 | + |
| 171 | + write_enum(outfile, instructions) |
| 172 | + |
| 173 | + script_path = pathlib.Path(__file__).resolve().relative_to(ROOT).as_posix() |
| 174 | + |
| 175 | + generated = outfile.getvalue() |
| 176 | + output = rustfmt( |
| 177 | + f""" |
| 178 | + // This file is generated by {script_path} |
| 179 | + // Do not edit! |
| 180 | +
|
| 181 | + use crate::bytecode::{{Arg, Label, NameIdx}}; |
| 182 | +
|
| 183 | + {generated} |
| 184 | + """ |
| 185 | + ) |
| 186 | + print(output) |
| 187 | + OUT_FILE.write_text(output) |
| 188 | + |
| 189 | + |
| 190 | +if __name__ == "__main__": |
| 191 | + main() |
0 commit comments