Skip to content

Assembly printer

assembly_printer

AssemblyPrinter dataclass

Bases: BasePrinter

Source code in xdsl/backend/assembly_printer.py
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
class AssemblyPrinter(BasePrinter):
    _current_section: str | None = None

    def emit_section(self, new_section: str):
        if self._current_section == new_section:
            return
        self._current_section = new_section
        self.print_string(new_section, indent=0)
        self.print_string("\n", indent=0)

    @staticmethod
    def append_comment(line: str, comment: StringAttr | None) -> str:
        if comment is None:
            return line

        padding = " " * max(0, 48 - len(line))

        return f"{line}{padding} # {comment.data}"

    @staticmethod
    def assembly_line(
        name: str,
        arg_str: str,
        comment: StringAttr | None = None,
        is_indented: bool = True,
    ) -> str:
        code = "    " if is_indented else ""
        code += name
        if arg_str:
            code += f" {arg_str}"
        code = AssemblyPrinter.append_comment(code, comment)
        return code

    def print_module(self, module: ModuleOp) -> None:
        for op in module.body.walk():
            assert isinstance(op, AssemblyPrintable), f"{op}"
            op.print_assembly(self)

emit_section(new_section: str)

Source code in xdsl/backend/assembly_printer.py
14
15
16
17
18
19
def emit_section(self, new_section: str):
    if self._current_section == new_section:
        return
    self._current_section = new_section
    self.print_string(new_section, indent=0)
    self.print_string("\n", indent=0)

append_comment(line: str, comment: StringAttr | None) -> str staticmethod

Source code in xdsl/backend/assembly_printer.py
21
22
23
24
25
26
27
28
@staticmethod
def append_comment(line: str, comment: StringAttr | None) -> str:
    if comment is None:
        return line

    padding = " " * max(0, 48 - len(line))

    return f"{line}{padding} # {comment.data}"

assembly_line(name: str, arg_str: str, comment: StringAttr | None = None, is_indented: bool = True) -> str staticmethod

Source code in xdsl/backend/assembly_printer.py
30
31
32
33
34
35
36
37
38
39
40
41
42
@staticmethod
def assembly_line(
    name: str,
    arg_str: str,
    comment: StringAttr | None = None,
    is_indented: bool = True,
) -> str:
    code = "    " if is_indented else ""
    code += name
    if arg_str:
        code += f" {arg_str}"
    code = AssemblyPrinter.append_comment(code, comment)
    return code

print_module(module: ModuleOp) -> None

Source code in xdsl/backend/assembly_printer.py
44
45
46
47
def print_module(self, module: ModuleOp) -> None:
    for op in module.body.walk():
        assert isinstance(op, AssemblyPrintable), f"{op}"
        op.print_assembly(self)

AssemblyPrintable dataclass

Bases: Operation, ABC

Base class for operations that can be a part of assembly printing.

Source code in xdsl/backend/assembly_printer.py
50
51
52
53
54
55
56
57
class AssemblyPrintable(Operation, ABC):
    """
    Base class for operations that can be a part of assembly printing.
    """

    @abstractmethod
    def print_assembly(self, printer: AssemblyPrinter) -> None:
        raise NotImplementedError()

print_assembly(printer: AssemblyPrinter) -> None abstractmethod

Source code in xdsl/backend/assembly_printer.py
55
56
57
@abstractmethod
def print_assembly(self, printer: AssemblyPrinter) -> None:
    raise NotImplementedError()

OneLineAssemblyPrintable dataclass

Bases: AssemblyPrintable, ABC

Base class for operations that can be printed in one line of assembly, or not printed at all.

Source code in xdsl/backend/assembly_printer.py
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
class OneLineAssemblyPrintable(AssemblyPrintable, ABC):
    """
    Base class for operations that can be printed in one line of assembly, or not
    printed at all.
    """

    @abstractmethod
    def assembly_line(self) -> str | None:
        """
        Returns the line that should be printed in assembly, or `None` to not be
        printed.
        """
        raise NotImplementedError()

    def print_assembly(self, printer: AssemblyPrinter) -> None:
        line = self.assembly_line()
        if line is not None:
            printer.print_string(line)
            printer.print_string("\n")

assembly_line() -> str | None abstractmethod

Returns the line that should be printed in assembly, or None to not be printed.

Source code in xdsl/backend/assembly_printer.py
66
67
68
69
70
71
72
@abstractmethod
def assembly_line(self) -> str | None:
    """
    Returns the line that should be printed in assembly, or `None` to not be
    printed.
    """
    raise NotImplementedError()

print_assembly(printer: AssemblyPrinter) -> None

Source code in xdsl/backend/assembly_printer.py
74
75
76
77
78
def print_assembly(self, printer: AssemblyPrinter) -> None:
    line = self.assembly_line()
    if line is not None:
        printer.print_string(line)
        printer.print_string("\n")

reg(value: SSAValue) -> str

A wrapper around SSAValue to be printed in assembly. Only valid if the type of the value is a RegisterType.

Source code in xdsl/backend/assembly_printer.py
84
85
86
87
88
89
90
91
def reg(value: SSAValue) -> str:
    """
    A wrapper around SSAValue to be printed in assembly.
    Only valid if the type of the value is a RegisterType.
    """

    assert isinstance(value.type, RegisterType)
    return value.type.register_name.data