Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address some suggestions from Ruff and Pylint #89

Merged
merged 4 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions gen_from_templates.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python3

"""Autogenerate several very similar test cases where we create specific interference graphs"""
import sys
import textwrap

from pathlib import Path
Expand Down Expand Up @@ -124,7 +125,7 @@ def format_string(text: str, fmt: str) -> str:

def gen_assembly(template_file: Path, output_dir: Path) -> None:
if not template_file.name.endswith(".s.jinja"):
exit(f"Expected assembly template, found {template_file}")
sys.exit(f"Expected assembly template, found {template_file}")
templ = env.get_template(str(template_file))
basename = template_file.name.removesuffix(".s.jinja")
for platform in ["linux", "osx"]:
Expand Down Expand Up @@ -297,7 +298,7 @@ def gen_assembly(template_file: Path, output_dir: Path) -> None:
template_files = Path("templates/chapter_20_templates").iterdir()
for t in template_files:
if t.suffix != ".jinja":
exit(f"Found non-template {f} in templates directory")
sys.exit(f"Found non-template {f} in templates directory")
relative_path = t.relative_to("templates")
templ = env.get_template(str(relative_path))
if t.name in configurable_templates:
Expand Down
1 change: 0 additions & 1 deletion generate_expected_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ def cleanup_keys() -> None:
full_path = TEST_DIR / k
if not full_path.exists():
del results[k]
return


def main() -> None:
Expand Down
10 changes: 2 additions & 8 deletions test_framework/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,11 @@


def get_platform() -> str:
if IS_OSX:
return "os_x"
else:
return "linux"
return "os_x" if IS_OSX else "linux"


def get_platform_suffix() -> str:
if IS_OSX:
return MAC_SUFFIX
else:
return LINUX_SUFFIX
return MAC_SUFFIX if IS_OSX else LINUX_SUFFIX


def get_props_key(source_file: Path) -> str:
Expand Down
5 changes: 2 additions & 3 deletions test_framework/parser/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ def parse_immediate(toks: list[Token]) -> Immediate:
if tok_type == TokType.INT:
val = int(next_tok.tok_str, base=0)
return Immediate(val)
elif tok_type in [TokType.PLUS_SIGN, TokType.MINUS_SIGN]:
if tok_type in [TokType.PLUS_SIGN, TokType.MINUS_SIGN]:
# next tok should val
num_tok = toks.pop(0)
if num_tok.tok_type != TokType.INT:
Expand All @@ -261,8 +261,7 @@ def parse_immediate(toks: list[Token]) -> Immediate:
if tok_type == TokType.MINUS_SIGN:
return Immediate(-val)
return Immediate(val)
else:
raise ParseError(f"Bad immediate value: ${next_tok}")
raise ParseError(f"Bad immediate value: ${next_tok}")


def parse_expr(toks: list[Token]) -> Expr:
Expand Down
14 changes: 6 additions & 8 deletions test_framework/regalloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,12 @@ def is_stack(operand: asm.Operand) -> bool:

def get_spilled_operand_count(spill_instructions: List[asm.AsmItem]) -> int:
"""Count number of distinct stack operands in spill instructions"""
spilled_operands = set(
[
str(op) # convert to string b/c Operands themselves are not hashable
for i in spill_instructions
for op in i.operands # type: ignore
if isinstance(op, asm.Memory) and op.base == Register.BP
]
)
spilled_operands = {
str(op) # convert to string b/c Operands themselves are not hashable
for i in spill_instructions
for op in i.operands # type: ignore
if isinstance(op, asm.Memory) and op.base == Register.BP
}
return len(spilled_operands)


Expand Down
23 changes: 23 additions & 0 deletions test_framework/tacky/common.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Base class for TACKY optimization tests"""
from __future__ import annotations

from collections.abc import Callable
from pathlib import Path
from typing import List, Optional, Sequence

Expand Down Expand Up @@ -152,6 +153,28 @@ def ok(i: asm.AsmItem) -> bool:
),
)

def check_instructions(
self,
parsed_asm: dict[str, asm.AssemblyFunction],
program_source_file: Path,
ok: Callable[[asm.AsmItem], bool],
error_string: str
) -> None:
"""Check that all assembly instructions in all `target_*` functions of a parsed program
satisfy a given predicate and raise a unit test failure if not.
"""
for fn_name, fn_body in parsed_asm.items():
if fn_name.startswith("target"):
bad_instructions = [i for i in fn_body.instructions if not ok(i)]
self.assertFalse(
bad_instructions,
msg=build_msg(
error_string,
bad_instructions=bad_instructions,
full_prog=fn_body,
program_path=program_source_file,
),
)

def build_msg(
msg: str,
Expand Down
14 changes: 1 addition & 13 deletions test_framework/tacky/const_fold.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,7 @@ def ok(i: AsmItem) -> bool:
or common.is_zero_instr(i)
)

for fn_name, fn_body in parsed_asm.items():
if fn_name.startswith("target"):
bad_instructions = [i for i in fn_body.instructions if not ok(i)]
self.assertFalse(
bad_instructions,
msg=common.build_msg(
"Found instructions that should have been constant folded",
bad_instructions=bad_instructions,
full_prog=fn_body,
program_path=program,
),
)

self.check_instructions(parsed_asm, program, ok, "Found instructions that should have been constant folded")

def make_constant_fold_test(program: Path) -> Callable[[TestConstantFolding], None]:
"""Generate test method for a single test program."""
Expand Down
13 changes: 1 addition & 12 deletions test_framework/tacky/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,7 @@ def ok(i: AsmItem) -> bool:
# source isn't immediate or RIP-relative
return False

for fn_name, fn_body in parsed_asm.items():
if fn_name.startswith("target"):
bad_instructions = [i for i in fn_body.instructions if not ok(i)]
self.assertFalse(
bad_instructions,
msg=common.build_msg(
"Found instructions that should have been constant folded",
bad_instructions=bad_instructions,
full_prog=fn_body,
program_path=source_file,
),
)
self.check_instructions(parsed_asm, source_file, ok, "Found instructions that should have been constant folded")

def global_var_unused_test(self, *, source_file: Path, unused_var: str) -> None:
"""
Expand Down
Loading