add util.py
authorLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Fri, 23 Apr 2021 16:01:56 +0000 (17:01 +0100)
committerLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Fri, 23 Apr 2021 16:01:56 +0000 (17:01 +0100)
src/openpower/util.py [new file with mode: 0644]

diff --git a/src/openpower/util.py b/src/openpower/util.py
new file mode 100644 (file)
index 0000000..e4f2fe0
--- /dev/null
@@ -0,0 +1,41 @@
+from openpower.consts import FastRegsEnum
+from openpower.decoder.power_enums import SPRfull as SPR, spr_dict
+
+# note that we can get away with using SPRfull here because the values
+# (numerical values) are what is used for lookup.
+spr_to_fast = { SPR.CTR: FastRegsEnum.CTR,
+                SPR.LR: FastRegsEnum.LR,
+                SPR.TAR: FastRegsEnum.TAR,
+                SPR.SRR0: FastRegsEnum.SRR0,
+                SPR.SRR1: FastRegsEnum.SRR1,
+                SPR.XER: FastRegsEnum.XER,
+                SPR.DEC: FastRegsEnum.DEC,
+                SPR.TB: FastRegsEnum.TB,
+               }
+
+sprstr_to_fast = {}
+fast_to_spr = {}
+for (k, v) in spr_to_fast.items():
+    sprstr_to_fast[k.name] = v
+    fast_to_spr[v] = k
+
+def fast_reg_to_spr(spr_num):
+    return fast_to_spr[spr_num].value
+
+
+def spr_to_fast_reg(spr_num):
+    if not isinstance(spr_num, str):
+        spr_num = spr_dict[spr_num].SPR
+    return sprstr_to_fast.get(spr_num, None)
+
+
+def slow_reg_to_spr(slow_reg):
+    for i, x in enumerate(SPR):
+        if slow_reg == i:
+            return x.value
+
+
+def spr_to_slow_reg(spr_num):
+    for i, x in enumerate(SPR):
+        if spr_num == x.value:
+            return i