Skip to content

Commit 6706827

Browse files
committed
Rip out parser and generator support for super()
1 parent 96872f6 commit 6706827

File tree

2 files changed

+13
-93
lines changed

2 files changed

+13
-93
lines changed

Tools/cases_generator/generate_cases.py

+12-72
Original file line numberDiff line numberDiff line change
@@ -260,27 +260,13 @@ def write_body(self, out: Formatter, cache_adjust: int) -> None:
260260

261261

262262
@dataclasses.dataclass
263-
class SuperOrMacroInstruction:
264-
"""Common fields for super- and macro instructions."""
263+
class MacroInstruction:
264+
"""A macro instruction."""
265265

266266
name: str
267267
stack: list[StackEffect]
268268
initial_sp: int
269269
final_sp: int
270-
271-
272-
@dataclasses.dataclass
273-
class SuperInstruction(SuperOrMacroInstruction):
274-
"""A super-instruction."""
275-
276-
super: parser.Super
277-
parts: list[Component]
278-
279-
280-
@dataclasses.dataclass
281-
class MacroInstruction(SuperOrMacroInstruction):
282-
"""A macro instruction."""
283-
284270
macro: parser.Macro
285271
parts: list[Component | parser.CacheEffect]
286272

@@ -311,10 +297,8 @@ def error(self, msg: str, node: parser.Node) -> None:
311297
print(f"{self.filename}:{lineno}: {msg}", file=sys.stderr)
312298
self.errors += 1
313299

314-
everything: list[parser.InstDef | parser.Super | parser.Macro]
300+
everything: list[parser.InstDef | parser.Macro]
315301
instrs: dict[str, Instruction] # Includes ops
316-
supers: dict[str, parser.Super]
317-
super_instrs: dict[str, SuperInstruction]
318302
macros: dict[str, parser.Macro]
319303
macro_instrs: dict[str, MacroInstruction]
320304
families: dict[str, parser.Family]
@@ -347,17 +331,13 @@ def parse(self) -> None:
347331
psr.setpos(start)
348332
self.everything = []
349333
self.instrs = {}
350-
self.supers = {}
351334
self.macros = {}
352335
self.families = {}
353336
while thing := psr.definition():
354337
match thing:
355338
case parser.InstDef(name=name):
356339
self.instrs[name] = Instruction(thing)
357340
self.everything.append(thing)
358-
case parser.Super(name):
359-
self.supers[name] = thing
360-
self.everything.append(thing)
361341
case parser.Macro(name):
362342
self.macros[name] = thing
363343
self.everything.append(thing)
@@ -370,7 +350,7 @@ def parse(self) -> None:
370350

371351
print(
372352
f"Read {len(self.instrs)} instructions/ops, "
373-
f"{len(self.supers)} supers, {len(self.macros)} macros, "
353+
f"{len(self.macros)} macros, "
374354
f"and {len(self.families)} families from {self.filename}",
375355
file=sys.stderr,
376356
)
@@ -383,7 +363,7 @@ def analyze(self) -> None:
383363
self.find_predictions()
384364
self.map_families()
385365
self.check_families()
386-
self.analyze_supers_and_macros()
366+
self.analyze_macros()
387367

388368
def find_predictions(self) -> None:
389369
"""Find the instructions that need PREDICTED() labels."""
@@ -449,26 +429,12 @@ def check_families(self) -> None:
449429
family,
450430
)
451431

452-
def analyze_supers_and_macros(self) -> None:
453-
"""Analyze each super- and macro instruction."""
454-
self.super_instrs = {}
432+
def analyze_macros(self) -> None:
433+
"""Analyze each macro instruction."""
455434
self.macro_instrs = {}
456-
for name, super in self.supers.items():
457-
self.super_instrs[name] = self.analyze_super(super)
458435
for name, macro in self.macros.items():
459436
self.macro_instrs[name] = self.analyze_macro(macro)
460437

461-
def analyze_super(self, super: parser.Super) -> SuperInstruction:
462-
components = self.check_super_components(super)
463-
stack, initial_sp = self.stack_analysis(components)
464-
sp = initial_sp
465-
parts: list[Component] = []
466-
for instr in components:
467-
part, sp = self.analyze_instruction(instr, stack, sp)
468-
parts.append(part)
469-
final_sp = sp
470-
return SuperInstruction(super.name, stack, initial_sp, final_sp, super, parts)
471-
472438
def analyze_macro(self, macro: parser.Macro) -> MacroInstruction:
473439
components = self.check_macro_components(macro)
474440
stack, initial_sp = self.stack_analysis(components)
@@ -499,15 +465,6 @@ def analyze_instruction(
499465
sp += 1
500466
return Component(instr, input_mapping, output_mapping), sp
501467

502-
def check_super_components(self, super: parser.Super) -> list[Instruction]:
503-
components: list[Instruction] = []
504-
for op in super.ops:
505-
if op.name not in self.instrs:
506-
self.error(f"Unknown instruction {op.name!r}", super)
507-
else:
508-
components.append(self.instrs[op.name])
509-
return components
510-
511468
def check_macro_components(
512469
self, macro: parser.Macro
513470
) -> list[InstructionOrCacheEffect]:
@@ -527,7 +484,7 @@ def check_macro_components(
527484
def stack_analysis(
528485
self, components: typing.Iterable[InstructionOrCacheEffect]
529486
) -> tuple[list[StackEffect], int]:
530-
"""Analyze a super-instruction or macro.
487+
"""Analyze a macro instruction.
531488
532489
Print an error if there's a cache effect (which we don't support yet).
533490
@@ -567,25 +524,21 @@ def write_instructions(self) -> None:
567524

568525
# Write and count instructions of all kinds
569526
n_instrs = 0
570-
n_supers = 0
571527
n_macros = 0
572528
for thing in self.everything:
573529
match thing:
574530
case parser.InstDef():
575531
if thing.kind == "inst":
576532
n_instrs += 1
577533
self.write_instr(self.instrs[thing.name])
578-
case parser.Super():
579-
n_supers += 1
580-
self.write_super(self.super_instrs[thing.name])
581534
case parser.Macro():
582535
n_macros += 1
583536
self.write_macro(self.macro_instrs[thing.name])
584537
case _:
585538
typing.assert_never(thing)
586539

587540
print(
588-
f"Wrote {n_instrs} instructions, {n_supers} supers, "
541+
f"Wrote {n_instrs} instructions, "
589542
f"and {n_macros} macros to {self.output_filename}",
590543
file=sys.stderr,
591544
)
@@ -602,22 +555,9 @@ def write_instr(self, instr: Instruction) -> None:
602555
self.out.emit(f"PREDICT({prediction});")
603556
self.out.emit(f"DISPATCH();")
604557

605-
def write_super(self, sup: SuperInstruction) -> None:
606-
"""Write code for a super-instruction."""
607-
with self.wrap_super_or_macro(sup):
608-
first = True
609-
for comp in sup.parts:
610-
if not first:
611-
self.out.emit("NEXTOPARG();")
612-
self.out.emit("JUMPBY(1);")
613-
first = False
614-
comp.write_body(self.out, 0)
615-
if comp.instr.cache_offset:
616-
self.out.emit(f"JUMPBY({comp.instr.cache_offset});")
617-
618558
def write_macro(self, mac: MacroInstruction) -> None:
619559
"""Write code for a macro instruction."""
620-
with self.wrap_super_or_macro(mac):
560+
with self.wrap_macro(mac):
621561
cache_adjust = 0
622562
for part in mac.parts:
623563
match part:
@@ -631,8 +571,8 @@ def write_macro(self, mac: MacroInstruction) -> None:
631571
self.out.emit(f"JUMPBY({cache_adjust});")
632572

633573
@contextlib.contextmanager
634-
def wrap_super_or_macro(self, up: SuperOrMacroInstruction):
635-
"""Shared boilerplate for super- and macro instructions."""
574+
def wrap_macro(self, up: MacroInstruction):
575+
"""Boilerplate for macro instructions."""
636576
# TODO: Somewhere (where?) make it so that if one instruction
637577
# has an output that is input to another, and the variable names
638578
# and types match and don't conflict with other instructions,

Tools/cases_generator/parser.py

+1-21
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,6 @@ class InstDef(Node):
9999
block: Block
100100

101101

102-
@dataclass
103-
class Super(Node):
104-
name: str
105-
ops: list[OpName]
106-
107-
108102
@dataclass
109103
class Macro(Node):
110104
name: str
@@ -120,11 +114,9 @@ class Family(Node):
120114

121115
class Parser(PLexer):
122116
@contextual
123-
def definition(self) -> InstDef | Super | Macro | Family | None:
117+
def definition(self) -> InstDef | Macro | Family | None:
124118
if inst := self.inst_def():
125119
return inst
126-
if super := self.super_def():
127-
return super
128120
if macro := self.macro_def():
129121
return macro
130122
if family := self.family_def():
@@ -224,18 +216,6 @@ def stack_effect(self) -> StackEffect | None:
224216
type = self.require(lx.IDENTIFIER).text
225217
return StackEffect(tkn.text, type)
226218

227-
@contextual
228-
def super_def(self) -> Super | None:
229-
if (tkn := self.expect(lx.IDENTIFIER)) and tkn.text == "super":
230-
if self.expect(lx.LPAREN):
231-
if tkn := self.expect(lx.IDENTIFIER):
232-
if self.expect(lx.RPAREN):
233-
if self.expect(lx.EQUALS):
234-
if ops := self.ops():
235-
self.require(lx.SEMI)
236-
res = Super(tkn.text, ops)
237-
return res
238-
239219
def ops(self) -> list[OpName] | None:
240220
if op := self.op():
241221
ops = [op]

0 commit comments

Comments
 (0)