Skip to content

Riscv debug

riscv_debug

RISCV_Debug = Dialect('riscv_debug', [PrintfOp], []) module-attribute

PrintfOp

Bases: RISCVCustomFormatOperation, RISCVInstruction

An instruction to print the contents of registers when emulating riscv code.

Is not a real instruction in the RISC-V instruction set, but supported by riscemu and xDSL's interpreter.

During assembly emission, the results are printed before the operands:

s0 = riscv.GetRegisterOp(Registers.s0).res
s1 = riscv.GetRegisterOp(Registers.s1).res
op = PrintfOp("s0: {}, s1: {}", (s0, s1))

op.assembly_line()   # 'printf "s0: {}, s1: {}", s0, s1'
Source code in xdsl/dialects/riscv_debug.py
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
@irdl_op_definition
class PrintfOp(riscv.RISCVCustomFormatOperation, riscv.RISCVInstruction):
    """
    An instruction to print the contents of registers when emulating riscv code.

    Is not a real instruction in the RISC-V instruction set, but supported by riscemu
    and xDSL's interpreter.

    During assembly emission, the results are printed before the operands:

    ``` python
    s0 = riscv.GetRegisterOp(Registers.s0).res
    s1 = riscv.GetRegisterOp(Registers.s1).res
    op = PrintfOp("s0: {}, s1: {}", (s0, s1))

    op.assembly_line()   # 'printf "s0: {}, s1: {}", s0, s1'
    ```
    """

    name = "riscv_debug.printf"
    format_str = attr_def(StringAttr)
    inputs = var_operand_def()

    def __init__(
        self,
        format_str: str | StringAttr,
        inputs: Sequence[SSAValue] = (),
        *,
        comment: str | StringAttr | None = None,
    ):
        if isinstance(format_str, str):
            format_str = StringAttr(format_str)
        if isinstance(comment, str):
            comment = StringAttr(comment)

        super().__init__(
            operands=[inputs],
            attributes={
                "format_str": format_str,
                "comment": comment,
            },
        )

    @classmethod
    def custom_parse_attributes(cls, parser: Parser) -> dict[str, Attribute]:
        attributes = dict[str, Attribute]()
        attributes["format_str"] = StringAttr(
            parser.parse_str_literal("format string.")
        )
        return attributes

    def custom_print_attributes(self, printer: Printer) -> AbstractSet[str]:
        printer.print_string(" ")
        printer.print_attribute(self.format_str)
        return {"format_str"}

    def assembly_line_args(self) -> tuple[riscv.AssemblyInstructionArg, ...]:
        return f'"{self.format_str.data}"', *self.operands

name = 'riscv_debug.printf' class-attribute instance-attribute

format_str = attr_def(StringAttr) class-attribute instance-attribute

inputs = var_operand_def() class-attribute instance-attribute

__init__(format_str: str | StringAttr, inputs: Sequence[SSAValue] = (), *, comment: str | StringAttr | None = None)

Source code in xdsl/dialects/riscv_debug.py
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
def __init__(
    self,
    format_str: str | StringAttr,
    inputs: Sequence[SSAValue] = (),
    *,
    comment: str | StringAttr | None = None,
):
    if isinstance(format_str, str):
        format_str = StringAttr(format_str)
    if isinstance(comment, str):
        comment = StringAttr(comment)

    super().__init__(
        operands=[inputs],
        attributes={
            "format_str": format_str,
            "comment": comment,
        },
    )

custom_parse_attributes(parser: Parser) -> dict[str, Attribute] classmethod

Source code in xdsl/dialects/riscv_debug.py
55
56
57
58
59
60
61
@classmethod
def custom_parse_attributes(cls, parser: Parser) -> dict[str, Attribute]:
    attributes = dict[str, Attribute]()
    attributes["format_str"] = StringAttr(
        parser.parse_str_literal("format string.")
    )
    return attributes

custom_print_attributes(printer: Printer) -> AbstractSet[str]

Source code in xdsl/dialects/riscv_debug.py
63
64
65
66
def custom_print_attributes(self, printer: Printer) -> AbstractSet[str]:
    printer.print_string(" ")
    printer.print_attribute(self.format_str)
    return {"format_str"}

assembly_line_args() -> tuple[riscv.AssemblyInstructionArg, ...]

Source code in xdsl/dialects/riscv_debug.py
68
69
def assembly_line_args(self) -> tuple[riscv.AssemblyInstructionArg, ...]:
    return f'"{self.format_str.data}"', *self.operands