Skip to content

Commit 4687fba

Browse files
committed
Flatten InstDef; improve CLI help output
I tried to split it into InstDef and OpDef, removing kind, but that caused problems because the Instruction class inherits from InstHeader.
1 parent a3af889 commit 4687fba

File tree

2 files changed

+23
-27
lines changed

2 files changed

+23
-27
lines changed

Tools/cases_generator/generate_cases.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,28 @@
1414

1515
import parser
1616

17-
DEFAULT_INPUT = "Python/bytecodes.c"
18-
DEFAULT_OUTPUT = "Python/generated_cases.c.h"
17+
DEFAULT_INPUT = os.path.relpath(
18+
os.path.join(os.path.dirname(__file__), "../../Python/bytecodes.c")
19+
)
20+
DEFAULT_OUTPUT = os.path.relpath(
21+
os.path.join(os.path.dirname(__file__), "../../Python/generated_cases.c.h")
22+
)
1923
BEGIN_MARKER = "// BEGIN BYTECODES //"
2024
END_MARKER = "// END BYTECODES //"
2125
RE_PREDICTED = r"(?s)(?:PREDICT\(|GO_TO_INSTRUCTION\(|DEOPT_IF\(.*?,\s*)(\w+)\);"
2226
UNUSED = "unused"
2327
BITS_PER_CODE_UNIT = 16
2428

25-
arg_parser = argparse.ArgumentParser()
26-
arg_parser.add_argument("-i", "--input", type=str, default=DEFAULT_INPUT)
27-
arg_parser.add_argument("-o", "--output", type=str, default=DEFAULT_OUTPUT)
29+
arg_parser = argparse.ArgumentParser(
30+
description="Generate the code for the interpreter switch.",
31+
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
32+
)
33+
arg_parser.add_argument(
34+
"-i", "--input", type=str, help="Instruction definitions", default=DEFAULT_INPUT
35+
)
36+
arg_parser.add_argument(
37+
"-o", "--output", type=str, help="Generated code", default=DEFAULT_OUTPUT
38+
)
2839

2940

3041
class Formatter:
@@ -79,7 +90,7 @@ class Instruction(parser.InstDef):
7990
predicted: bool = False
8091

8192
def __init__(self, inst: parser.InstDef):
82-
super().__init__(inst.header, inst.block)
93+
super().__init__(inst.kind, inst.name, inst.inputs, inst.outputs, inst.block)
8394
self.context = inst.context
8495
self.always_exits = always_exits(self.block)
8596
self.cache_effects = [

Tools/cases_generator/parser.py

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -91,26 +91,12 @@ class InstHeader(Node):
9191

9292
@dataclass
9393
class InstDef(Node):
94-
# TODO: Merge InstHeader and InstDef
95-
header: InstHeader
94+
kind: Literal["inst", "op"]
95+
name: str
96+
inputs: list[InputEffect]
97+
outputs: list[OutputEffect]
9698
block: Block
9799

98-
@property
99-
def kind(self) -> Literal["inst", "op"]:
100-
return self.header.kind
101-
102-
@property
103-
def name(self) -> str:
104-
return self.header.name
105-
106-
@property
107-
def inputs(self) -> list[InputEffect]:
108-
return self.header.inputs
109-
110-
@property
111-
def outputs(self) -> list[OutputEffect]:
112-
return self.header.outputs
113-
114100

115101
@dataclass
116102
class Super(Node):
@@ -145,9 +131,9 @@ def definition(self) -> InstDef | Super | Macro | Family | None:
145131

146132
@contextual
147133
def inst_def(self) -> InstDef | None:
148-
if header := self.inst_header():
134+
if hdr := self.inst_header():
149135
if block := self.block():
150-
return InstDef(header, block)
136+
return InstDef(hdr.kind, hdr.name, hdr.inputs, hdr.outputs, block)
151137
raise self.make_syntax_error("Expected block")
152138
return None
153139

@@ -156,7 +142,6 @@ def inst_header(self) -> InstHeader | None:
156142
# inst(NAME)
157143
# | inst(NAME, (inputs -- outputs))
158144
# | op(NAME, (inputs -- outputs))
159-
# TODO: Error out when there is something unexpected.
160145
# TODO: Make INST a keyword in the lexer.
161146
if (tkn := self.expect(lx.IDENTIFIER)) and (kind := tkn.text) in ("inst", "op"):
162147
if self.expect(lx.LPAREN) and (tkn := self.expect(lx.IDENTIFIER)):

0 commit comments

Comments
 (0)