Skip to content

Riscv debug

riscv_debug

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

PrintfOp

Bases: 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 = rv32.GetRegisterOp(Registers.s0).res
s1 = rv32.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
@irdl_op_definition
class PrintfOp(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 = rv32.GetRegisterOp(Registers.s0).res
    s1 = rv32.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()
    traits = traits_def(MemoryWriteEffect())

    assembly_format = "$format_str (` ` `(` $inputs^ `:` type($inputs) `)`)? attr-dict"

    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,
            },
        )

    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

traits = traits_def(MemoryWriteEffect()) class-attribute instance-attribute

assembly_format = '$format_str (` ` `(` $inputs^ `:` type($inputs) `)`)? attr-dict' 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
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,
        },
    )

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

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