From fdca9ae5a48f07ee07eec9b3ba944339e3893757 Mon Sep 17 00:00:00 2001 From: Luke Kenneth Casson Leighton Date: Fri, 23 Apr 2021 17:01:56 +0100 Subject: [PATCH] add util.py --- src/openpower/util.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/openpower/util.py diff --git a/src/openpower/util.py b/src/openpower/util.py new file mode 100644 index 00000000..e4f2fe04 --- /dev/null +++ b/src/openpower/util.py @@ -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 -- 2.30.2