Skip to content

Commit

Permalink
Rip out support for 'super' from the generator
Browse files Browse the repository at this point in the history
  • Loading branch information
gvanrossum committed Dec 7, 2022
1 parent 28e45f9 commit 248ecfe
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 95 deletions.
85 changes: 11 additions & 74 deletions Tools/cases_generator/generate_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,27 +260,13 @@ def write_body(self, out: Formatter, cache_adjust: int) -> None:


@dataclasses.dataclass
class SuperOrMacroInstruction:
"""Common fields for super- and macro instructions."""
class MacroInstruction:
"""A macro instruction."""

name: str
stack: list[StackEffect]
initial_sp: int
final_sp: int


@dataclasses.dataclass
class SuperInstruction(SuperOrMacroInstruction):
"""A super-instruction."""

super: parser.Super
parts: list[Component]


@dataclasses.dataclass
class MacroInstruction(SuperOrMacroInstruction):
"""A macro instruction."""

macro: parser.Macro
parts: list[Component | parser.CacheEffect]

Expand Down Expand Up @@ -312,8 +298,6 @@ def error(self, msg: str, node: parser.Node) -> None:
self.errors += 1

instrs: dict[str, Instruction] # Includes ops
supers: dict[str, parser.Super]
super_instrs: dict[str, SuperInstruction]
macros: dict[str, parser.Macro]
macro_instrs: dict[str, MacroInstruction]
families: dict[str, parser.Family]
Expand Down Expand Up @@ -345,15 +329,12 @@ def parse(self) -> None:
# Parse from start
psr.setpos(start)
self.instrs = {}
self.supers = {}
self.macros = {}
self.families = {}
while thing := psr.definition():
match thing:
case parser.InstDef(name=name):
self.instrs[name] = Instruction(thing)
case parser.Super(name):
self.supers[name] = thing
case parser.Macro(name):
self.macros[name] = thing
case parser.Family(name):
Expand All @@ -365,7 +346,7 @@ def parse(self) -> None:

print(
f"Read {len(self.instrs)} instructions/ops, "
f"{len(self.supers)} supers, {len(self.macros)} macros, "
f"{len(self.macros)} macros, "
f"and {len(self.families)} families from {self.filename}",
file=sys.stderr,
)
Expand All @@ -378,7 +359,7 @@ def analyze(self) -> None:
self.find_predictions()
self.map_families()
self.check_families()
self.analyze_supers_and_macros()
self.analyze_macros()

def find_predictions(self) -> None:
"""Find the instructions that need PREDICTED() labels."""
Expand Down Expand Up @@ -444,26 +425,12 @@ def check_families(self) -> None:
family,
)

def analyze_supers_and_macros(self) -> None:
"""Analyze each super- and macro instruction."""
self.super_instrs = {}
def analyze_macros(self) -> None:
"""Analyze each macro instruction."""
self.macro_instrs = {}
for name, super in self.supers.items():
self.super_instrs[name] = self.analyze_super(super)
for name, macro in self.macros.items():
self.macro_instrs[name] = self.analyze_macro(macro)

def analyze_super(self, super: parser.Super) -> SuperInstruction:
components = self.check_super_components(super)
stack, initial_sp = self.stack_analysis(components)
sp = initial_sp
parts: list[Component] = []
for instr in components:
part, sp = self.analyze_instruction(instr, stack, sp)
parts.append(part)
final_sp = sp
return SuperInstruction(super.name, stack, initial_sp, final_sp, super, parts)

def analyze_macro(self, macro: parser.Macro) -> MacroInstruction:
components = self.check_macro_components(macro)
stack, initial_sp = self.stack_analysis(components)
Expand Down Expand Up @@ -494,15 +461,6 @@ def analyze_instruction(
sp += 1
return Component(instr, input_mapping, output_mapping), sp

def check_super_components(self, super: parser.Super) -> list[Instruction]:
components: list[Instruction] = []
for op in super.ops:
if op.name not in self.instrs:
self.error(f"Unknown instruction {op.name!r}", super)
else:
components.append(self.instrs[op.name])
return components

def check_macro_components(
self, macro: parser.Macro
) -> list[InstructionOrCacheEffect]:
Expand All @@ -522,9 +480,7 @@ def check_macro_components(
def stack_analysis(
self, components: typing.Iterable[InstructionOrCacheEffect]
) -> tuple[list[StackEffect], int]:
"""Analyze a super-instruction or macro.
Print an error if there's a cache effect (which we don't support yet).
"""Analyze a macro.
Return the list of variable names and the initial stack pointer.
"""
Expand Down Expand Up @@ -576,40 +532,21 @@ def write_instructions(self) -> None:
self.out.emit(f"PREDICT({prediction});")
self.out.emit(f"DISPATCH();")

# Write and count super-instructions
n_supers = 0
for sup in self.super_instrs.values():
n_supers += 1
self.write_super(sup)

# Write and count macro instructions
n_macros = 0
for macro in self.macro_instrs.values():
n_macros += 1
self.write_macro(macro)

print(
f"Wrote {n_instrs} instructions, {n_supers} supers, "
f"Wrote {n_instrs} instructions "
f"and {n_macros} macros to {self.output_filename}",
file=sys.stderr,
)

def write_super(self, sup: SuperInstruction) -> None:
"""Write code for a super-instruction."""
with self.wrap_super_or_macro(sup):
first = True
for comp in sup.parts:
if not first:
self.out.emit("NEXTOPARG();")
self.out.emit("next_instr++;")
first = False
comp.write_body(self.out, 0)
if comp.instr.cache_offset:
self.out.emit(f"next_instr += {comp.instr.cache_offset};")

def write_macro(self, mac: MacroInstruction) -> None:
"""Write code for a macro instruction."""
with self.wrap_super_or_macro(mac):
with self.wrap_macro(mac):
cache_adjust = 0
for part in mac.parts:
match part:
Expand All @@ -623,8 +560,8 @@ def write_macro(self, mac: MacroInstruction) -> None:
self.out.emit(f"next_instr += {cache_adjust};")

@contextlib.contextmanager
def wrap_super_or_macro(self, up: SuperOrMacroInstruction):
"""Shared boilerplate for super- and macro instructions."""
def wrap_macro(self, up: MacroInstruction):
"""Boilerplate for macro instructions."""
# TODO: Somewhere (where?) make it so that if one instruction
# has an output that is input to another, and the variable names
# and types match and don't conflict with other instructions,
Expand Down
22 changes: 1 addition & 21 deletions Tools/cases_generator/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,6 @@ class InstDef(Node):
block: Block


@dataclass
class Super(Node):
name: str
ops: list[OpName]


@dataclass
class Macro(Node):
name: str
Expand All @@ -120,11 +114,9 @@ class Family(Node):

class Parser(PLexer):
@contextual
def definition(self) -> InstDef | Super | Macro | Family | None:
def definition(self) -> InstDef | Macro | Family | None:
if inst := self.inst_def():
return inst
if super := self.super_def():
return super
if macro := self.macro_def():
return macro
if family := self.family_def():
Expand Down Expand Up @@ -224,18 +216,6 @@ def stack_effect(self) -> StackEffect | None:
type = self.require(lx.IDENTIFIER).text
return StackEffect(tkn.text, type)

@contextual
def super_def(self) -> Super | None:
if (tkn := self.expect(lx.IDENTIFIER)) and tkn.text == "super":
if self.expect(lx.LPAREN):
if tkn := self.expect(lx.IDENTIFIER):
if self.expect(lx.RPAREN):
if self.expect(lx.EQUALS):
if ops := self.ops():
self.require(lx.SEMI)
res = Super(tkn.text, ops)
return res

def ops(self) -> list[OpName] | None:
if op := self.op():
ops = [op]
Expand Down

0 comments on commit 248ecfe

Please sign in to comment.