Skip to content

Commit 59e63d4

Browse files
committed
Tweak the structure of Instruction and {Super,Macro}Instruction
1 parent 4687fba commit 59e63d4

File tree

1 file changed

+30
-29
lines changed

1 file changed

+30
-29
lines changed

Tools/cases_generator/generate_cases.py

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,16 @@ def block(self, head: str):
7474
self.emit("}")
7575

7676

77-
# This is not a data class
78-
class Instruction(parser.InstDef):
77+
@dataclasses.dataclass
78+
class Instruction:
7979
"""An instruction with additional data and code."""
8080

81+
# Parts of the underlying instruction definition
82+
inst: parser.InstDef
83+
kind: typing.Literal["inst", "op"]
84+
name: str
85+
block: parser.Block
86+
8187
# Computed by constructor
8288
always_exits: bool
8389
cache_offset: int
@@ -90,17 +96,19 @@ class Instruction(parser.InstDef):
9096
predicted: bool = False
9197

9298
def __init__(self, inst: parser.InstDef):
93-
super().__init__(inst.kind, inst.name, inst.inputs, inst.outputs, inst.block)
94-
self.context = inst.context
99+
self.inst = inst
100+
self.kind = inst.kind
101+
self.name = inst.name
102+
self.block = inst.block
95103
self.always_exits = always_exits(self.block)
96104
self.cache_effects = [
97-
effect for effect in self.inputs if isinstance(effect, parser.CacheEffect)
105+
effect for effect in inst.inputs if isinstance(effect, parser.CacheEffect)
98106
]
99107
self.cache_offset = sum(c.size for c in self.cache_effects)
100108
self.input_effects = [
101-
effect for effect in self.inputs if isinstance(effect, parser.StackEffect)
109+
effect for effect in inst.inputs if isinstance(effect, parser.StackEffect)
102110
]
103-
self.output_effects = self.outputs # For consistency/completeness
111+
self.output_effects = inst.outputs # For consistency/completeness
104112

105113
def write(self, out: Formatter) -> None:
106114
"""Write one instruction, sans prologue and epilogue."""
@@ -235,35 +243,32 @@ def write_body(self, out: Formatter, cache_adjust: int) -> None:
235243

236244

237245
# TODO: Use a common base class for {Super,Macro}Instruction
246+
238247
@dataclasses.dataclass
239-
class SuperInstruction:
240-
"""A super-instruction."""
248+
class SuperOrMacroInstruction:
249+
"""Common fields for super- and macro instructions."""
241250

242-
super: parser.Super
251+
name: str
243252
stack: list[str]
244253
initial_sp: int
245254
final_sp: int
246-
parts: list[Component]
247255

248-
@property
249-
def name(self) -> str:
250-
return self.super.name
256+
257+
@dataclasses.dataclass
258+
class SuperInstruction(SuperOrMacroInstruction):
259+
"""A super-instruction."""
260+
261+
super: parser.Super
262+
parts: list[Component]
251263

252264

253265
@dataclasses.dataclass
254-
class MacroInstruction:
266+
class MacroInstruction(SuperOrMacroInstruction):
255267
"""A macro instruction."""
256268

257269
macro: parser.Macro
258-
stack: list[str]
259-
initial_sp: int
260-
final_sp: int
261270
parts: list[Component | parser.CacheEffect]
262271

263-
@property
264-
def name(self) -> str:
265-
return self.macro.name
266-
267272

268273
class Analyzer:
269274
"""Parse input, analyze it, and write to output."""
@@ -369,7 +374,7 @@ def find_predictions(self) -> None:
369374
else:
370375
self.error(
371376
f"Unknown instruction {target!r} predicted in {instr.name!r}",
372-
instr, # TODO: Use better location
377+
instr.inst, # TODO: Use better location
373378
)
374379

375380
def map_families(self) -> None:
@@ -453,7 +458,7 @@ def analyze_super(self, super: parser.Super) -> SuperInstruction:
453458
case _:
454459
typing.assert_never(component)
455460
final_sp = sp
456-
return SuperInstruction(super, stack, initial_sp, final_sp, parts)
461+
return SuperInstruction(super.name, stack, initial_sp, final_sp, super, parts)
457462

458463
def analyze_macro(self, macro: parser.Macro) -> MacroInstruction:
459464
components = self.check_macro_components(macro)
@@ -479,12 +484,10 @@ def analyze_macro(self, macro: parser.Macro) -> MacroInstruction:
479484
case _:
480485
typing.assert_never(component)
481486
final_sp = sp
482-
return MacroInstruction(macro, stack, initial_sp, final_sp, parts)
487+
return MacroInstruction(macro.name, stack, initial_sp, final_sp, macro, parts)
483488

484489
def check_super_components(self, super: parser.Super) -> list[Instruction]:
485490
components: list[Instruction] = []
486-
if not super.ops:
487-
self.error(f"Super-instruction has no operands", super)
488491
for op in super.ops:
489492
if op.name not in self.instrs:
490493
self.error(f"Unknown instruction {op.name!r}", super)
@@ -496,8 +499,6 @@ def check_macro_components(
496499
self, macro: parser.Macro
497500
) -> list[InstructionOrCacheEffect]:
498501
components: list[InstructionOrCacheEffect] = []
499-
if not macro.uops:
500-
self.error(f"Macro instruction has no operands", macro)
501502
for uop in macro.uops:
502503
match uop:
503504
case parser.OpName(name):

0 commit comments

Comments
 (0)