Skip to content

Commit a7a0797

Browse files
authored
gh-109287: Desugar inst(X) to op(X); macro(X) = X (#109294)
This makes the internal representation in the code generator simpler: there's a list of ops, and a list of macros, and there's no special-casing needed for ops that aren't macros. (There's now special-casing for ops that are also macros, but that's simpler.)
1 parent 47af188 commit a7a0797

File tree

5 files changed

+110
-163
lines changed

5 files changed

+110
-163
lines changed

Include/internal/pycore_opcode_metadata.h

Lines changed: 18 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Tools/cases_generator/analysis.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,8 @@ def parse(self) -> None:
9494
self.parse_file(filename, instrs_idx)
9595

9696
files = " + ".join(self.input_filenames)
97-
n_instrs = 0
98-
n_ops = 0
99-
for instr in self.instrs.values():
100-
if instr.kind == "op":
101-
n_ops += 1
102-
else:
103-
n_instrs += 1
97+
n_instrs = len(set(self.instrs) & set(self.macros))
98+
n_ops = len(self.instrs) - n_instrs
10499
print(
105100
f"Read {n_instrs} instructions, {n_ops} ops, "
106101
f"{len(self.macros)} macros, {len(self.pseudos)} pseudos, "
@@ -145,16 +140,22 @@ def parse_file(self, filename: str, instrs_idx: dict[str, int]) -> None:
145140

146141
match thing:
147142
case parsing.InstDef(name=name):
143+
macro: parsing.Macro | None = None
144+
if thing.kind == "inst":
145+
macro = parsing.Macro(name, [parsing.OpName(name)])
148146
if name in self.instrs:
149147
if not thing.override:
150148
raise psr.make_syntax_error(
151149
f"Duplicate definition of '{name}' @ {thing.context} "
152150
f"previous definition @ {self.instrs[name].inst.context}",
153151
thing_first_token,
154152
)
155-
self.everything[
156-
instrs_idx[name]
157-
] = OverriddenInstructionPlaceHolder(name=name)
153+
placeholder = OverriddenInstructionPlaceHolder(name=name)
154+
self.everything[instrs_idx[name]] = placeholder
155+
if macro is not None:
156+
self.warning(
157+
f"Overriding desugared {macro.name} may not work", thing
158+
)
158159
if name not in self.instrs and thing.override:
159160
raise psr.make_syntax_error(
160161
f"Definition of '{name}' @ {thing.context} is supposed to be "
@@ -164,6 +165,9 @@ def parse_file(self, filename: str, instrs_idx: dict[str, int]) -> None:
164165
self.instrs[name] = Instruction(thing)
165166
instrs_idx[name] = len(self.everything)
166167
self.everything.append(thing)
168+
if macro is not None:
169+
self.macros[macro.name] = macro
170+
self.everything.append(macro)
167171
case parsing.Macro(name):
168172
self.macros[name] = thing
169173
self.everything.append(thing)
@@ -197,9 +201,9 @@ def find_predictions(self) -> None:
197201
for target in targets:
198202
if target_instr := self.instrs.get(target):
199203
target_instr.predicted = True
200-
elif target_macro := self.macro_instrs.get(target):
204+
if target_macro := self.macro_instrs.get(target):
201205
target_macro.predicted = True
202-
else:
206+
if not target_instr and not target_macro:
203207
self.error(
204208
f"Unknown instruction {target!r} predicted in {instr.name!r}",
205209
instr.inst, # TODO: Use better location
@@ -263,11 +267,7 @@ def check_families(self) -> None:
263267
)
264268

265269
def effect_counts(self, name: str) -> tuple[int, int, int]:
266-
if instr := self.instrs.get(name):
267-
cache = instr.cache_offset
268-
input = len(instr.input_effects)
269-
output = len(instr.output_effects)
270-
elif mac := self.macro_instrs.get(name):
270+
if mac := self.macro_instrs.get(name):
271271
cache = mac.cache_offset
272272
input, output = 0, 0
273273
for part in mac.parts:
@@ -407,7 +407,8 @@ def check_macro_components(
407407
case parsing.OpName(name):
408408
if name not in self.instrs:
409409
self.error(f"Unknown instruction {name!r}", macro)
410-
components.append(self.instrs[name])
410+
else:
411+
components.append(self.instrs[name])
411412
case parsing.CacheEffect():
412413
components.append(uop)
413414
case _:

0 commit comments

Comments
 (0)