power_insn: remove the whitespaces properly
[openpower-isa.git] / src / openpower / decoder / power_insn.py
index a3226a813856569ed21a97eb280b077019542497..34ae88957279e3fc05bece07173c71fc8daaf9d2 100644 (file)
@@ -227,7 +227,9 @@ class PPCRecord:
                 flags.add(flag)
         record["flags"] = PPCRecord.Flags(flags)
 
-        return dataclass(cls, record, keymap=PPCRecord.__KEYMAP, typemap=typemap)
+        return dataclass(cls, record,
+            keymap=PPCRecord.__KEYMAP,
+            typemap=typemap)
 
     @cached_property
     def names(self):
@@ -258,7 +260,9 @@ class PPCMultiRecord(tuple):
                 value |= (vstate << bit)
                 mask |= (mstate << bit)
 
-            return _dataclasses.replace(lhs, opcode=Opcode(value=value, mask=mask))
+            opcode = opcode=Opcode(value=value, mask=mask)
+
+            return _dataclasses.replace(lhs, opcode=opcode)
 
         return _functools.reduce(merge, self)
 
@@ -335,7 +339,11 @@ class SVP64Record:
             if value == "0":
                 record[key] = "NONE"
 
-        record["extra"] = cls.ExtraMap(record.pop(f"{index}") for index in range(0, 4))
+        extra = []
+        for idx in range(0, 4):
+            extra.append(record.pop(f"{idx}"))
+
+        record["extra"] = cls.ExtraMap(extra)
 
         return dataclass(cls, record, keymap=cls.__KEYMAP)
 
@@ -443,6 +451,7 @@ class Operand:
     def disassemble(self, value, record, verbose=False):
         raise NotImplementedError
 
+
 @_dataclasses.dataclass(eq=True, frozen=True)
 class DynamicOperand(Operand):
     def disassemble(self, value, record, verbose=False):
@@ -454,6 +463,12 @@ class DynamicOperand(Operand):
         else:
             yield str(int(value))
 
+
+@_dataclasses.dataclass(eq=True, frozen=True)
+class ImmediateOperand(DynamicOperand):
+    pass
+
+
 @_dataclasses.dataclass(eq=True, frozen=True)
 class StaticOperand(Operand):
     value: int
@@ -467,6 +482,7 @@ class StaticOperand(Operand):
         else:
             yield str(int(value))
 
+
 @_dataclasses.dataclass(eq=True, frozen=True)
 class DynamicOperandTargetAddrLI(DynamicOperand):
     @property
@@ -488,6 +504,7 @@ class DynamicOperandTargetAddrLI(DynamicOperand):
             yield hex(int(_selectconcat(value,
                 _SelectableInt(value=0b00, bits=2))))
 
+
 class DynamicOperandTargetAddrBD(DynamicOperand):
     @property
     def name(self):
@@ -508,6 +525,7 @@ class DynamicOperandTargetAddrBD(DynamicOperand):
             yield hex(int(_selectconcat(value,
                 _SelectableInt(value=0b00, bits=2))))
 
+
 @_dataclasses.dataclass(eq=True, frozen=True)
 class DynamicOperandGPR(DynamicOperand):
     def disassemble(self, value, record, verbose=False):
@@ -519,6 +537,7 @@ class DynamicOperandGPR(DynamicOperand):
         else:
             yield f"r{str(int(value))}"
 
+
 @_dataclasses.dataclass(eq=True, frozen=True)
 class DynamicOperandFPR(DynamicOperand):
     def disassemble(self, value, record, verbose=False):
@@ -531,8 +550,8 @@ class DynamicOperandFPR(DynamicOperand):
             yield f"f{str(int(value))}"
 
 
-class Operands:
-    def __init__(self, insn, iterable):
+class Operands(tuple):
+    def __new__(cls, insn, iterable):
         branches = {
             "b": {"target_addr": DynamicOperandTargetAddrLI},
             "ba": {"target_addr": DynamicOperandTargetAddrLI},
@@ -556,39 +575,33 @@ class Operands:
             else:
                 if operand.endswith(")"):
                     operand = operand.replace("(", " ").replace(")", "")
-                    all_operands = operand.split(" ")
+                    (immediate, _, operand) = operand.partition(" ")
                 else:
-                    all_operands = [operand]
+                    immediate = None
 
-                for operand in all_operands:
-                    if insn in branches and operand in branches[insn]:
-                        dynamic_cls = branches[insn][operand]
+                if immediate is not None:
+                    operands.append(ImmediateOperand(name=immediate))
 
-                    if operand in _RegType.__members__:
-                        regtype = _RegType[operand]
-                        if regtype is _RegType.GPR:
-                            dynamic_cls = DynamicOperandGPR
-                        elif regtype is _RegType.FPR:
-                            dynamic_cls = DynamicOperandFPR
+                if insn in branches and operand in branches[insn]:
+                    dynamic_cls = branches[insn][operand]
 
-                    operand = dynamic_cls(name=operand)
-                    operands.append(operand)
+                if operand in _RegType.__members__:
+                    regtype = _RegType[operand]
+                    if regtype is _RegType.GPR:
+                        dynamic_cls = DynamicOperandGPR
+                    elif regtype is _RegType.FPR:
+                        dynamic_cls = DynamicOperandFPR
 
-        self.__operands = operands
-
-        return super().__init__()
-
-    def __repr__(self):
-        return self.__operands.__repr__()
+                operand = dynamic_cls(name=operand)
+                operands.append(operand)
 
-    def __iter__(self):
-        yield from self.__operands
+        return super().__new__(cls, operands)
 
     def __contains__(self, key):
         return self.__getitem__(key) is not None
 
     def __getitem__(self, key):
-        for operand in self.__operands:
+        for operand in self:
             if operand.name == key:
                 return operand
 
@@ -778,17 +791,28 @@ class WordInstruction(Instruction):
         return "".join(map(str, bits))
 
     def spec(self, record):
+        immediate = ""
         dynamic_operands = []
         for operand in record.operands.dynamic:
-            dynamic_operands.append(operand.name)
+            name = operand.name
+            if immediate:
+                name = f"{immediate}({name})"
+                immediate = ""
+            if isinstance(operand, ImmediateOperand):
+                immediate = operand.name
+            if not immediate:
+                dynamic_operands.append(name)
+
         static_operands = []
         for operand in record.operands.static:
             static_operands.append(f"{operand.name}={operand.value}")
+
         operands = ""
         if dynamic_operands:
             operands += f" {','.join(dynamic_operands)}"
         if static_operands:
             operands += f" ({' '.join(static_operands)})"
+
         return f"{record.name}{operands}"
 
     def opcode(self, record):
@@ -1234,9 +1258,12 @@ class SVP64Instruction(PrefixedInstruction):
 
 
 def parse(stream, factory):
+    def match(entry):
+        return ("TODO" not in frozenset(entry.values()))
+
     lines = filter(lambda line: not line.strip().startswith("#"), stream)
     entries = _csv.DictReader(lines)
-    entries = filter(lambda entry: "TODO" not in frozenset(entry.values()), entries)
+    entries = filter(match, entries)
     return tuple(map(factory, entries))
 
 
@@ -1295,7 +1322,8 @@ class PPCDatabase:
                     section.Mode.INTEGER: IntegerOpcode,
                     section.Mode.PATTERN: PatternOpcode,
                 }[section.mode]
-                factory = _functools.partial(PPCRecord.CSV, opcode_cls=opcode_cls)
+                factory = _functools.partial(
+                    PPCRecord.CSV, opcode_cls=opcode_cls)
                 with open(path, "r", encoding="UTF-8") as stream:
                     for insn in parse(stream, factory):
                         records[section][insn.comment].add(insn)