Skip to content

Rv32

rv32

RISC-V 32-bit (RV32) dialect operations and types.

This module defines the RV32-specific variant of RISC-V operations, using 5-bit immediates for 32-bit architectures.

RV32 = Dialect('rv32', [LiOp, GetRegisterOp], []) module-attribute

LiOp

Bases: RISCVCustomFormatOperation, RISCVInstruction, HasFolderInterface, ABC

Loads a 32-bit immediate into rd.

This is an assembler pseudo-instruction.

See external documentation.

Source code in xdsl/dialects/rv32.py
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
@irdl_op_definition
class LiOp(RISCVCustomFormatOperation, RISCVInstruction, HasFolderInterface, ABC):
    """
    Loads a 32-bit immediate into rd.

    This is an assembler pseudo-instruction.

    See external [documentation](https://github.com/riscv-non-isa/riscv-asm-manual/blob/main/src/asm-manual.adoc).
    """

    name = "rv32.li"

    rd = result_def(IntRegisterType)
    immediate = attr_def(IntegerAttr[I32] | LabelAttr)

    traits = traits_def(Pure(), LiOpHasCanonicalizationPatternTrait(), ConstantLike())

    def __init__(
        self,
        immediate: int | IntegerAttr[I32] | str | LabelAttr,
        *,
        rd: IntRegisterType = Registers.UNALLOCATED_INT,
        comment: str | StringAttr | None = None,
    ):
        if isinstance(immediate, int):
            immediate = IntegerAttr(immediate, i32)
        elif isinstance(immediate, str):
            immediate = LabelAttr(immediate)
        if isinstance(comment, str):
            comment = StringAttr(comment)

        super().__init__(
            result_types=[rd],
            attributes={
                "immediate": immediate,
                "comment": comment,
            },
        )

    def assembly_line_args(self) -> tuple[AssemblyInstructionArg, ...]:
        return self.rd, self.immediate

    def fold(self) -> tuple[IntegerAttr[I32] | LabelAttr]:
        return (self.immediate,)

    @classmethod
    def custom_parse_attributes(cls, parser: Parser) -> dict[str, Attribute]:
        attributes = dict[str, Attribute]()
        attributes["immediate"] = parse_immediate_value(parser, i32)
        return attributes

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

    @classmethod
    def parse_op_type(
        cls, parser: Parser
    ) -> tuple[Sequence[Attribute], Sequence[Attribute]]:
        parser.parse_punctuation(":")
        res_type = parser.parse_attribute()
        return (), (res_type,)

    def print_op_type(self, printer: Printer) -> None:
        printer.print_string(" : ")
        printer.print_attribute(self.rd.type)

name = 'rv32.li' class-attribute instance-attribute

rd = result_def(IntRegisterType) class-attribute instance-attribute

immediate = attr_def(IntegerAttr[I32] | LabelAttr) class-attribute instance-attribute

traits = traits_def(Pure(), LiOpHasCanonicalizationPatternTrait(), ConstantLike()) class-attribute instance-attribute

__init__(immediate: int | IntegerAttr[I32] | str | LabelAttr, *, rd: IntRegisterType = Registers.UNALLOCATED_INT, comment: str | StringAttr | None = None)

Source code in xdsl/dialects/rv32.py
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
def __init__(
    self,
    immediate: int | IntegerAttr[I32] | str | LabelAttr,
    *,
    rd: IntRegisterType = Registers.UNALLOCATED_INT,
    comment: str | StringAttr | None = None,
):
    if isinstance(immediate, int):
        immediate = IntegerAttr(immediate, i32)
    elif isinstance(immediate, str):
        immediate = LabelAttr(immediate)
    if isinstance(comment, str):
        comment = StringAttr(comment)

    super().__init__(
        result_types=[rd],
        attributes={
            "immediate": immediate,
            "comment": comment,
        },
    )

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

Source code in xdsl/dialects/rv32.py
85
86
def assembly_line_args(self) -> tuple[AssemblyInstructionArg, ...]:
    return self.rd, self.immediate

fold() -> tuple[IntegerAttr[I32] | LabelAttr]

Source code in xdsl/dialects/rv32.py
88
89
def fold(self) -> tuple[IntegerAttr[I32] | LabelAttr]:
    return (self.immediate,)

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

Source code in xdsl/dialects/rv32.py
91
92
93
94
95
@classmethod
def custom_parse_attributes(cls, parser: Parser) -> dict[str, Attribute]:
    attributes = dict[str, Attribute]()
    attributes["immediate"] = parse_immediate_value(parser, i32)
    return attributes

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

Source code in xdsl/dialects/rv32.py
 97
 98
 99
100
def custom_print_attributes(self, printer: Printer) -> AbstractSet[str]:
    printer.print_string(" ")
    print_immediate_value(printer, self.immediate)
    return {"immediate", "fastmath"}

parse_op_type(parser: Parser) -> tuple[Sequence[Attribute], Sequence[Attribute]] classmethod

Source code in xdsl/dialects/rv32.py
102
103
104
105
106
107
108
@classmethod
def parse_op_type(
    cls, parser: Parser
) -> tuple[Sequence[Attribute], Sequence[Attribute]]:
    parser.parse_punctuation(":")
    res_type = parser.parse_attribute()
    return (), (res_type,)

print_op_type(printer: Printer) -> None

Source code in xdsl/dialects/rv32.py
110
111
112
def print_op_type(self, printer: Printer) -> None:
    printer.print_string(" : ")
    printer.print_attribute(self.rd.type)

GetRegisterOp dataclass

Bases: GetAnyRegisterOperation[IntRegisterType]

Source code in xdsl/dialects/rv32.py
115
116
117
@irdl_op_definition
class GetRegisterOp(GetAnyRegisterOperation[IntRegisterType]):
    name = "rv32.get_register"

name = 'rv32.get_register' class-attribute instance-attribute