test/ldst: add fixedsync tests for b/h/w/d ll/sc, but not quadword
[openpower-isa.git] / src / openpower / test / common.py
index 2ab1fe02a25f46cf1c83c9663c354c1d4df74aa3..b3b89876398b49e3bbdbd5d388d21d58b1eb8623 100644 (file)
@@ -3,13 +3,16 @@ Bugreports:
 * https://bugs.libre-soc.org/show_bug.cgi?id=361
 """
 
-import inspect
+from contextlib import contextmanager
+import sys
 import functools
 import types
+import os
 
 from openpower.decoder.power_enums import XER_bits, CryIn, spr_dict
-from openpower.util import fast_reg_to_spr, slow_reg_to_spr  # HACK!
-from openpower.consts import XERRegsEnum
+from openpower.util import LogType, log, \
+    fast_reg_to_spr, slow_reg_to_spr  # HACK!
+from openpower.consts import XERRegsEnum, DEFAULT_MSR
 
 
 # TODO: make this a util routine (somewhere)
@@ -36,7 +39,7 @@ def _id(obj):
     return obj
 
 
-def skip_case(reason):
+def skip_case(reason, *, __condition=True):
     """
     Unconditionally skip a test case.
 
@@ -51,13 +54,19 @@ def skip_case(reason):
 
     For use with TestAccumulatorBase
     """
+    if not callable(__condition):
+        def __condition(ta, *, __retval=bool(__condition)):
+            return __retval
     def decorator(item):
         assert not isinstance(item, type), \
             "can't use skip_case to decorate types"
 
         @functools.wraps(item)
-        def wrapper(*args, **kwargs):
-            raise SkipCase(reason)
+        def wrapper(self, *args, **kwargs):
+            if __condition(self):
+                raise SkipCase(reason)
+            else:
+                return item(self, *args, **kwargs)
         return wrapper
     if isinstance(reason, types.FunctionType):
         item = reason
@@ -75,16 +84,54 @@ def skip_case_if(condition, reason):
         def case_abc(self):
             ...
 
+    Or:
+        # ta is the TestAccumulatorBase instance
+        @skip_case_if(lambda ta: ta.has_case_abc(), "my reason for skipping")
+        def case_abc(self):
+            ...
+
+    For use with TestAccumulatorBase
+    """
+    return skip_case(reason, __condition=condition)
+
+
+def skip_case_if_flag(flag_name):
+    """
+    Skip a test case if `flag_name in TestAccumulatorBase.flags`.
+
+    Use like:
+        @skip_if_flag("foo")
+        def case_not_on_foo(self):
+            ...
+
     For use with TestAccumulatorBase
     """
-    if condition:
-        return skip_case(reason)
-    return _id
+    return skip_case_if(lambda ta: flag_name in ta.flags,
+                        flag_name + " is in flags")
+
+
+def skip_case_if_not_flag(flag_name):
+    """
+    Skip a test case if `flag_name not in TestAccumulatorBase.flags`.
+
+    Use like:
+        @skip_if_not_flag("foo")
+        def case_only_on_foo(self):
+            ...
+
+    For use with TestAccumulatorBase
+    """
+    return skip_case_if(lambda ta: flag_name not in ta.flags,
+                        flag_name + " isn't in flags")
 
 
 class TestAccumulatorBase:
+    __test__ = False  # pytest should ignore this class
+
+    def __init__(self, flags=()):
+        self.__subtest_args = {}
+        self.flags = frozenset(flags)
 
-    def __init__(self):
         self.test_data = []
         # automatically identifies anything starting with "case_" and
         # runs it.  very similar to unittest auto-identification except
@@ -97,29 +144,62 @@ class TestAccumulatorBase:
                     # TODO(programmerjake): translate to final test sending
                     # skip signal to unittest. for now, just print the skipped
                     # reason and ignore
-                    print(f"SKIPPED({n}):", str(e))
+                    log(f"SKIPPED({n}):", str(e), kind=LogType.SkipCase)
+
+    @contextmanager
+    def subTest(self, **kwargs):
+        old_subtest_args = self.__subtest_args
+        try:
+            self.__subtest_args = old_subtest_args.copy()
+            self.__subtest_args.update(**kwargs)
+            yield
+        finally:
+            self.__subtest_args = old_subtest_args
 
     def add_case(self, prog, initial_regs=None, initial_sprs=None,
-                 initial_cr=0, initial_msr=0,
+                 initial_cr=0, initial_msr=None,
                  initial_mem=None,
-                 initial_svstate=0):
-
-        test_name = inspect.stack()[1][3]  # name of caller of this function
+                 initial_svstate=0,
+                 expected=None,
+                 stop_at_pc=None,
+                 fpregs=None,
+                 initial_fpscr=None,
+                 src_loc_at=0):
+
+        c = sys._getframe(1 + src_loc_at).f_code
+        # name of caller of this function
+        test_name = c.co_name
+        # name of file containing test case
+        test_file = os.path.splitext(os.path.basename(c.co_filename))[0]
         tc = TestCase(prog, test_name,
                       regs=initial_regs, sprs=initial_sprs, cr=initial_cr,
                       msr=initial_msr,
                       mem=initial_mem,
-                      svstate=initial_svstate)
+                      svstate=initial_svstate,
+                      expected=expected,
+                      stop_at_pc=stop_at_pc,
+                      test_file=test_file,
+                      subtest_args=self.__subtest_args.copy(),
+                      fpregs=fpregs,
+                      initial_fpscr=initial_fpscr)
 
         self.test_data.append(tc)
 
 
 class TestCase:
+    __test__ = False  # pytest should ignore this class
+
     def __init__(self, program, name, regs=None, sprs=None, cr=0, mem=None,
-                 msr=0,
+                 msr=None,
                  do_sim=True,
                  extra_break_addr=None,
-                 svstate=0):
+                 svstate=None,
+                 expected=None,
+                 stop_at_pc=None,
+                 test_file=None,
+                 subtest_args=None,
+                 fpregs=None,
+                 initial_fpscr=None):
 
         self.program = program
         self.name = name
@@ -130,7 +210,10 @@ class TestCase:
             sprs = {}
         if mem is None:
             mem = {}
+        if fpregs is None:
+            fpregs = [0] * 32
         self.regs = regs
+        self.fpregs = fpregs
         self.sprs = sprs
         self.cr = cr
         self.mem = mem
@@ -138,6 +221,13 @@ class TestCase:
         self.do_sim = do_sim
         self.extra_break_addr = extra_break_addr
         self.svstate = svstate
+        self.expected = expected # expected results from the test
+        self.stop_at_pc = stop_at_pc # hard-stop address (do not attempt to run)
+        self.test_file = test_file
+        self.subtest_args = {} if subtest_args is None else dict(subtest_args)
+        if initial_fpscr is None:
+            initial_fpscr = 0
+        self.initial_fpscr = initial_fpscr
 
 
 class ALUHelpers:
@@ -237,173 +327,173 @@ class ALUHelpers:
     def set_int_ra(alu, dec2, inp):
         # TODO: immediate RA zero.
         if 'ra' in inp:
-            yield alu.p.data_i.ra.eq(inp['ra'])
+            yield alu.p.i_data.ra.eq(inp['ra'])
         else:
-            yield alu.p.data_i.ra.eq(0)
+            yield alu.p.i_data.ra.eq(0)
 
     def set_int_rb(alu, dec2, inp):
-        yield alu.p.data_i.rb.eq(0)
+        yield alu.p.i_data.rb.eq(0)
         if 'rb' in inp:
-            yield alu.p.data_i.rb.eq(inp['rb'])
+            yield alu.p.i_data.rb.eq(inp['rb'])
         if not hasattr(dec2.e.do, "imm_data"):
             return
         # If there's an immediate, set the B operand to that
         imm_ok = yield dec2.e.do.imm_data.ok
         if imm_ok:
             data2 = yield dec2.e.do.imm_data.data
-            yield alu.p.data_i.rb.eq(data2)
+            yield alu.p.i_data.rb.eq(data2)
 
     def set_int_rc(alu, dec2, inp):
         if 'rc' in inp:
-            yield alu.p.data_i.rc.eq(inp['rc'])
+            yield alu.p.i_data.rc.eq(inp['rc'])
         else:
-            yield alu.p.data_i.rc.eq(0)
+            yield alu.p.i_data.rc.eq(0)
 
     def set_xer_ca(alu, dec2, inp):
         if 'xer_ca' in inp:
-            yield alu.p.data_i.xer_ca.eq(inp['xer_ca'])
+            yield alu.p.i_data.xer_ca.eq(inp['xer_ca'])
             print("extra inputs: CA/32", bin(inp['xer_ca']))
 
     def set_xer_ov(alu, dec2, inp):
         if 'xer_ov' in inp:
-            yield alu.p.data_i.xer_ov.eq(inp['xer_ov'])
+            yield alu.p.i_data.xer_ov.eq(inp['xer_ov'])
             print("extra inputs: OV/32", bin(inp['xer_ov']))
 
     def set_xer_so(alu, dec2, inp):
         if 'xer_so' in inp:
             so = inp['xer_so']
             print("extra inputs: so", so)
-            yield alu.p.data_i.xer_so.eq(so)
+            yield alu.p.i_data.xer_so.eq(so)
 
     def set_msr(alu, dec2, inp):
         print("TODO: deprecate set_msr")
         if 'msr' in inp:
-            yield alu.p.data_i.msr.eq(inp['msr'])
+            yield alu.p.i_data.msr.eq(inp['msr'])
 
     def set_cia(alu, dec2, inp):
         print("TODO: deprecate set_cia")
         if 'cia' in inp:
-            yield alu.p.data_i.cia.eq(inp['cia'])
+            yield alu.p.i_data.cia.eq(inp['cia'])
 
     def set_slow_spr1(alu, dec2, inp):
         if 'spr1' in inp:
-            yield alu.p.data_i.spr1.eq(inp['spr1'])
+            yield alu.p.i_data.spr1.eq(inp['spr1'])
 
     def set_slow_spr2(alu, dec2, inp):
         if 'spr2' in inp:
-            yield alu.p.data_i.spr2.eq(inp['spr2'])
+            yield alu.p.i_data.spr2.eq(inp['spr2'])
 
     def set_fast_spr1(alu, dec2, inp):
         if 'fast1' in inp:
-            yield alu.p.data_i.fast1.eq(inp['fast1'])
+            yield alu.p.i_data.fast1.eq(inp['fast1'])
 
     def set_fast_spr2(alu, dec2, inp):
         if 'fast2' in inp:
-            yield alu.p.data_i.fast2.eq(inp['fast2'])
+            yield alu.p.i_data.fast2.eq(inp['fast2'])
 
     def set_fast_spr3(alu, dec2, inp):
         if 'fast3' in inp:
-            yield alu.p.data_i.fast3.eq(inp['fast3'])
+            yield alu.p.i_data.fast3.eq(inp['fast3'])
 
     def set_cr_a(alu, dec2, inp):
         if 'cr_a' in inp:
-            yield alu.p.data_i.cr_a.eq(inp['cr_a'])
+            yield alu.p.i_data.cr_a.eq(inp['cr_a'])
 
     def set_cr_b(alu, dec2, inp):
         if 'cr_b' in inp:
-            yield alu.p.data_i.cr_b.eq(inp['cr_b'])
+            yield alu.p.i_data.cr_b.eq(inp['cr_b'])
 
     def set_cr_c(alu, dec2, inp):
         if 'cr_c' in inp:
-            yield alu.p.data_i.cr_c.eq(inp['cr_c'])
+            yield alu.p.i_data.cr_c.eq(inp['cr_c'])
 
     def set_full_cr(alu, dec2, inp):
         if 'full_cr' in inp:
             full_reg = yield dec2.dec_cr_in.whole_reg.data
             full_reg_ok = yield dec2.dec_cr_in.whole_reg.ok
             full_cr_mask = mask_extend(full_reg, 8, 4)
-            yield alu.p.data_i.full_cr.eq(inp['full_cr'] & full_cr_mask)
+            yield alu.p.i_data.full_cr.eq(inp['full_cr'] & full_cr_mask)
         else:
-            yield alu.p.data_i.full_cr.eq(0)
+            yield alu.p.i_data.full_cr.eq(0)
 
     def get_slow_spr1(res, alu, dec2):
-        spr1_valid = yield alu.n.data_o.spr1.ok
+        spr1_valid = yield alu.n.o_data.spr1.ok
         if spr1_valid:
-            res['spr1'] = yield alu.n.data_o.spr1.data
+            res['spr1'] = yield alu.n.o_data.spr1.data
 
     def get_slow_spr2(res, alu, dec2):
-        spr2_valid = yield alu.n.data_o.spr2.ok
+        spr2_valid = yield alu.n.o_data.spr2.ok
         if spr2_valid:
-            res['spr2'] = yield alu.n.data_o.spr2.data
+            res['spr2'] = yield alu.n.o_data.spr2.data
 
     def get_fast_spr1(res, alu, dec2):
-        spr1_valid = yield alu.n.data_o.fast1.ok
+        spr1_valid = yield alu.n.o_data.fast1.ok
         if spr1_valid:
-            res['fast1'] = yield alu.n.data_o.fast1.data
+            res['fast1'] = yield alu.n.o_data.fast1.data
 
     def get_fast_spr2(res, alu, dec2):
-        spr2_valid = yield alu.n.data_o.fast2.ok
+        spr2_valid = yield alu.n.o_data.fast2.ok
         if spr2_valid:
-            res['fast2'] = yield alu.n.data_o.fast2.data
+            res['fast2'] = yield alu.n.o_data.fast2.data
 
     def get_fast_spr3(res, alu, dec2):
-        spr3_valid = yield alu.n.data_o.fast3.ok
+        spr3_valid = yield alu.n.o_data.fast3.ok
         if spr3_valid:
-            res['fast3'] = yield alu.n.data_o.fast3.data
+            res['fast3'] = yield alu.n.o_data.fast3.data
 
     def get_cia(res, alu, dec2):
-        res['cia'] = yield alu.p.data_i.cia
+        res['cia'] = yield alu.p.i_data.cia
 
     def get_nia(res, alu, dec2):
-        nia_valid = yield alu.n.data_o.nia.ok
+        nia_valid = yield alu.n.o_data.nia.ok
         if nia_valid:
-            res['nia'] = yield alu.n.data_o.nia.data
+            res['nia'] = yield alu.n.o_data.nia.data
 
     def get_msr(res, alu, dec2):
-        msr_valid = yield alu.n.data_o.msr.ok
+        msr_valid = yield alu.n.o_data.msr.ok
         if msr_valid:
-            res['msr'] = yield alu.n.data_o.msr.data
+            res['msr'] = yield alu.n.o_data.msr.data
 
     def get_int_o1(res, alu, dec2):
         out_reg_valid = yield dec2.e.write_ea.ok
         if out_reg_valid:
-            res['o1'] = yield alu.n.data_o.o1.data
+            res['o1'] = yield alu.n.o_data.o1.data
 
     def get_int_o(res, alu, dec2):
         out_reg_valid = yield dec2.e.write_reg.ok
         if out_reg_valid:
-            res['o'] = yield alu.n.data_o.o.data
+            res['o'] = yield alu.n.o_data.o.data
 
     def get_cr_a(res, alu, dec2):
         cridx_ok = yield dec2.e.write_cr.ok
         if cridx_ok:
-            res['cr_a'] = yield alu.n.data_o.cr0.data
+            res['cr_a'] = yield alu.n.o_data.cr0.data
 
     def get_xer_so(res, alu, dec2):
         oe = yield dec2.e.do.oe.oe
         oe_ok = yield dec2.e.do.oe.ok
         xer_out = yield dec2.e.xer_out
-        if not (yield alu.n.data_o.xer_so.ok):
+        if not (yield alu.n.o_data.xer_so.ok):
             return
         if xer_out or (oe and oe_ok):
-            res['xer_so'] = yield alu.n.data_o.xer_so.data[0]
+            res['xer_so'] = yield alu.n.o_data.xer_so.data[0]
 
     def get_xer_ov(res, alu, dec2):
         oe = yield dec2.e.do.oe.oe
         oe_ok = yield dec2.e.do.oe.ok
         xer_out = yield dec2.e.xer_out
-        if not (yield alu.n.data_o.xer_ov.ok):
+        if not (yield alu.n.o_data.xer_ov.ok):
             return
         if xer_out or (oe and oe_ok):
-            res['xer_ov'] = yield alu.n.data_o.xer_ov.data
+            res['xer_ov'] = yield alu.n.o_data.xer_ov.data
 
     def get_xer_ca(res, alu, dec2):
         cry_out = yield dec2.e.do.output_carry
         xer_out = yield dec2.e.xer_out
-        if not (yield alu.n.data_o.xer_ca.ok):
+        if not (yield alu.n.o_data.xer_ca.ok):
             return
         if xer_out or (cry_out):
-            res['xer_ca'] = yield alu.n.data_o.xer_ca.data
+            res['xer_ca'] = yield alu.n.o_data.xer_ca.data
 
     def get_sim_int_o(res, sim, dec2):
         out_reg_valid = yield dec2.e.write_reg.ok
@@ -456,7 +546,7 @@ class ALUHelpers:
             res['spr1'] = sim.spr[spr_name].value
 
     def get_wr_sim_xer_ca(res, sim, dec2):
-        # if not (yield alu.n.data_o.xer_ca.ok):
+        # if not (yield alu.n.o_data.xer_ca.ok):
         #    return
         cry_out = yield dec2.e.do.output_carry
         xer_out = yield dec2.e.xer_out
@@ -470,7 +560,7 @@ class ALUHelpers:
         oe_ok = yield dec2.e.do.oe.ok
         xer_out = yield dec2.e.xer_out
         print("get_wr_sim_xer_ov", xer_out)
-        if not (yield alu.n.data_o.xer_ov.ok):
+        if not (yield alu.n.o_data.xer_ov.ok):
             return
         if xer_out or (oe and oe_ok):
             expected_ov = 1 if sim.spr['XER'][XER_bits['OV']] else 0
@@ -481,7 +571,7 @@ class ALUHelpers:
         oe = yield dec2.e.do.oe.oe
         oe_ok = yield dec2.e.do.oe.ok
         xer_out = yield dec2.e.xer_out
-        if not (yield alu.n.data_o.xer_so.ok):
+        if not (yield alu.n.o_data.xer_so.ok):
             return
         if xer_out or (oe and oe_ok):
             res['xer_so'] = 1 if sim.spr['XER'][XER_bits['SO']] else 0