Skip to content

Commit

Permalink
dialects: (x86) - mem access helper (#2537)
Browse files Browse the repository at this point in the history
As per suggestion, I made a helper function for the memory accesses
  • Loading branch information
KGrykiel authored May 4, 2024
1 parent 8ad4e82 commit 17ca845
Showing 1 changed file with 28 additions and 72 deletions.
100 changes: 28 additions & 72 deletions xdsl/dialects/x86/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,9 @@ def __init__(
)

def assembly_line_args(self) -> tuple[AssemblyInstructionArg | None, ...]:
return self.r1, self.r2
memory_access = _memory_access_str(self.r2, self.offset)
destination = _assembly_arg_str(self.r1)
return (destination, memory_access)

@classmethod
def custom_parse_attributes(cls, parser: Parser) -> dict[str, Attribute]:
Expand All @@ -508,27 +510,6 @@ def custom_print_attributes(self, printer: Printer) -> Set[str]:
_print_immediate_value(printer, self.offset)
return {"offset"}

def assembly_line(self) -> str | None:
instruction_name = self.assembly_instruction_name()
destination = _assembly_arg_str(self.r1)
source = _assembly_arg_str(self.r2)
if self.offset is not None:
offset = _assembly_arg_str(self.offset)
if self.offset.value.data > 0:
return _assembly_line(
instruction_name,
f"{destination}, [{source}+{offset}]",
self.comment,
)
else:
return _assembly_line(
instruction_name, f"{destination}, [{source}{offset}]", self.comment
)
else:
return _assembly_line(
instruction_name, f"{destination}, [{source}]", self.comment
)


@irdl_op_definition
class RM_AddOp(RMOperation[GeneralRegisterType, GeneralRegisterType]):
Expand Down Expand Up @@ -758,16 +739,8 @@ def __init__(
)

def assembly_line_args(self) -> tuple[AssemblyInstructionArg | None, ...]:
destination = _assembly_arg_str(self.r1)
if self.offset is not None:
offset = _assembly_arg_str(self.offset)
if self.offset.value.data > 0:
destination = f"[{destination}+{offset}]"
else:
destination = f"[{destination}{offset}]"
else:
destination = f"[{destination}]"
return destination, self.r2
memory_access = _memory_access_str(self.r1, self.offset)
return memory_access, self.r2

@classmethod
def custom_parse_attributes(cls, parser: Parser) -> dict[str, Attribute]:
Expand Down Expand Up @@ -889,17 +862,9 @@ def __init__(
)

def assembly_line_args(self) -> tuple[AssemblyInstructionArg | None, ...]:
destination = _assembly_arg_str(self.r1)
immediate = _assembly_arg_str(self.immediate)
if self.offset is not None:
offset = _assembly_arg_str(self.offset)
if self.offset.value.data > 0:
destination = f"[{destination}+{offset}]"
else:
destination = f"[{destination}{offset}]"
else:
destination = f"[{destination}]"
return destination, immediate
memory_access = _memory_access_str(self.r1, self.offset)
return memory_access, immediate

@classmethod
def custom_parse_attributes(cls, parser: Parser) -> dict[str, Attribute]:
Expand Down Expand Up @@ -1091,17 +1056,9 @@ def __init__(

def assembly_line_args(self) -> tuple[AssemblyInstructionArg | None, ...]:
destination = _assembly_arg_str(self.r1)
source = _assembly_arg_str(self.r2)
immediate = _assembly_arg_str(self.immediate)
if self.offset is not None:
offset = _assembly_arg_str(self.offset)
if self.offset.value.data > 0:
source = f"[{source}+{offset}]"
else:
source = f"[{source}{offset}]"
else:
source = f"[{source}]"
return destination, source, immediate
memory_access = _memory_access_str(self.r2, self.offset)
return destination, memory_access, immediate

@classmethod
def custom_parse_attributes(cls, parser: Parser) -> dict[str, Attribute]:
Expand Down Expand Up @@ -1170,16 +1127,8 @@ def __init__(
)

def assembly_line_args(self) -> tuple[AssemblyInstructionArg | None, ...]:
source = _assembly_arg_str(self.source)
if self.offset is not None:
offset = _assembly_arg_str(self.offset)
if self.offset.value.data > 0:
source = f"[{source}+{offset}]"
else:
source = f"[{source}{offset}]"
else:
source = f"[{source}]"
return (source,)
memory_access = _memory_access_str(self.source, self.offset)
return (memory_access,)

@classmethod
def custom_parse_attributes(cls, parser: Parser) -> dict[str, Attribute]:
Expand Down Expand Up @@ -1228,16 +1177,8 @@ def __init__(
)

def assembly_line_args(self) -> tuple[AssemblyInstructionArg | None, ...]:
source = _assembly_arg_str(self.source)
if self.offset is not None:
offset = _assembly_arg_str(self.offset)
if self.offset.value.data > 0:
source = f"[{source}+{offset}]"
else:
source = f"[{source}{offset}]"
else:
source = f"[{source}]"
return (source,)
memory_access = _memory_access_str(self.source, self.offset)
return (memory_access,)

@classmethod
def custom_parse_attributes(cls, parser: Parser) -> dict[str, Attribute]:
Expand Down Expand Up @@ -1362,6 +1303,21 @@ def _print_immediate_value(printer: Printer, immediate: AnyIntegerAttr | LabelAt
printer.print_string_literal(immediate.data)


def _memory_access_str(
register: AssemblyInstructionArg, offset: AnyIntegerAttr | None
) -> str:
register_str = _assembly_arg_str(register)
if offset is not None:
offset_str = _assembly_arg_str(offset)
if offset.value.data > 0:
mem_acc_str = f"[{register_str}+{offset_str}]"
else:
mem_acc_str = f"[{register_str}{offset_str}]"
else:
mem_acc_str = f"[{register_str}]"
return mem_acc_str


# endregion


Expand Down

0 comments on commit 17ca845

Please sign in to comment.