power_insn: refactor immediate operands
authorDmitry Selyutin <ghostmansd@gmail.com>
Sun, 4 Sep 2022 10:06:24 +0000 (13:06 +0300)
committerDmitry Selyutin <ghostmansd@gmail.com>
Sun, 4 Sep 2022 10:08:28 +0000 (13:08 +0300)
src/openpower/decoder/power_insn.py

index 83f1487a742f11d43a96f09e7508d8203b196808..92ebf4d041d98d8262a748f5b78c543186585781 100644 (file)
@@ -456,6 +456,11 @@ class DynamicOperand(Operand):
             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
@@ -562,23 +567,25 @@ class Operands(tuple):
             else:
                 if operand.endswith(")"):
                     operand = operand.replace("(", " ").replace(")", "")
-                    all_operands = operand.split(" ")
+                    (immediate, _, operand) = operand.partition(" ")
                 else:
-                    all_operands = [operand]
+                    immediate = None
+
+                if immediate is not None:
+                    operands.append(ImmediateOperand(name=immediate))
 
-                for operand in all_operands:
-                    if insn in branches and operand in branches[insn]:
-                        dynamic_cls = branches[insn][operand]
+                if insn in branches and operand in branches[insn]:
+                    dynamic_cls = branches[insn][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
+                if operand in _RegType.__members__:
+                    regtype = _RegType[operand]
+                    if regtype is _RegType.GPR:
+                        dynamic_cls = DynamicOperandGPR
+                    elif regtype is _RegType.FPR:
+                        dynamic_cls = DynamicOperandFPR
 
-                    operand = dynamic_cls(name=operand)
-                    operands.append(operand)
+                operand = dynamic_cls(name=operand)
+                operands.append(operand)
 
         return super().__new__(cls, operands)
 
@@ -776,17 +783,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):