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

dialect: (riscv) Throw parser error if immediate is not given #1371

Merged
merged 6 commits into from
Aug 1, 2023
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
14 changes: 13 additions & 1 deletion tests/dialects/test_riscv.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
from xdsl.builder import Builder
from xdsl.dialects import riscv
from xdsl.dialects.builtin import IntegerAttr, ModuleOp, i32
from xdsl.utils.exceptions import VerifyException
from xdsl.ir import MLContext
from xdsl.parser import Parser
from xdsl.utils.exceptions import ParseError, VerifyException
from xdsl.utils.test_value import TestSSAValue


Expand Down Expand Up @@ -246,3 +248,13 @@ def test_float_register():
f1 = TestSSAValue(riscv.Registers.FT0)
f2 = TestSSAValue(riscv.Registers.FT1)
riscv.FAddSOp(f1, f2).verify()


def test_riscv_parse_immediate_value():
ctx = MLContext()
ctx.register_dialect(riscv.RISCV)

prog = """riscv.jalr %0, 1.1, !riscv.reg<> : (!riscv.reg<>) -> ()"""
parser = Parser(ctx, prog)
with pytest.raises(ParseError, match="Expected immediate"):
parser.parse_operation()
32 changes: 26 additions & 6 deletions xdsl/dialects/riscv.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from xdsl.dialects.builtin import (
AnyIntegerAttr,
IndexType,
IntegerAttr,
IntegerType,
ModuleOp,
Expand Down Expand Up @@ -759,12 +760,9 @@ def assembly_line_args(self) -> tuple[AssemblyInstructionArg | None, ...]:
@classmethod
def custom_parse_attributes(cls, parser: Parser) -> Mapping[str, Attribute]:
attributes = dict[str, Attribute]()
if immediate := parser.parse_optional_integer(allow_boolean=False):
attributes["immediate"] = IntegerAttr(
immediate, IntegerType(12, Signedness.SIGNED)
)
elif immediate := parser.parse_optional_str_literal():
attributes["immediate"] = LabelAttr(immediate)
attributes["immediate"] = _parse_immediate_value(
parser, IntegerType(12, Signedness.SIGNED)
)
if parser.parse_optional_punctuation(","):
attributes["rd"] = parser.parse_attribute()
return attributes
Expand Down Expand Up @@ -3046,6 +3044,28 @@ class FSwOp(RsRsImmFloatOperation):

# endregion


def _parse_optional_immediate_value(
parser: Parser, integer_type: IntegerType | IndexType
) -> IntegerAttr[IntegerType | IndexType] | LabelAttr | None:
"""
Parse an optional immediate value. If an integer is parsed, an integer attr with the specified type is created.
"""
if (immediate := parser.parse_optional_integer()) is not None:
return IntegerAttr(immediate, integer_type)
if (immediate := parser.parse_optional_str_literal()) is not None:
return LabelAttr(immediate)


def _parse_immediate_value(
parser: Parser, integer_type: IntegerType | IndexType
) -> IntegerAttr[IntegerType | IndexType] | LabelAttr:
return parser.expect(
lambda: _parse_optional_immediate_value(parser, integer_type),
"Expected immediate",
)


RISCV = Dialect(
[
AddiOp,
Expand Down