about summary refs log tree commit diff stats
path: root/test/arch/mep/jit
diff options
context:
space:
mode:
Diffstat (limited to 'test/arch/mep/jit')
-rw-r--r--test/arch/mep/jit/launch.py7
-rw-r--r--test/arch/mep/jit/test_jit_branchjump.py23
-rw-r--r--test/arch/mep/jit/test_jit_repeat.py99
-rw-r--r--test/arch/mep/jit/ut_helpers_jit.py49
4 files changed, 178 insertions, 0 deletions
diff --git a/test/arch/mep/jit/launch.py b/test/arch/mep/jit/launch.py
new file mode 100644
index 00000000..8c67e072
--- /dev/null
+++ b/test/arch/mep/jit/launch.py
@@ -0,0 +1,7 @@
+# Toshiba MeP-c4 - pytest unit tests wrapper
+# Guillaume Valadon <guillaume@valadon.net>
+
+from ut_helpers_jit import launch_tests
+
+from test_jit_branchjump import TestBranchJump; launch_tests(TestBranchJump())
+from test_jit_repeat import TestRepeat; launch_tests(TestRepeat())
diff --git a/test/arch/mep/jit/test_jit_branchjump.py b/test/arch/mep/jit/test_jit_branchjump.py
new file mode 100644
index 00000000..baf602d8
--- /dev/null
+++ b/test/arch/mep/jit/test_jit_branchjump.py
@@ -0,0 +1,23 @@
+# Toshiba MeP-c4 - Branch/Jump instructions JIT unit tests
+# Guillaume Valadon <guillaume@valadon.net>
+
+from ut_helpers_jit import jit_instructions
+
+
+class TestBranchJump:
+
+    def test_blti(self):
+        """Test BLTI jit"""
+
+        # Instructions that will be jitted
+        instructions = "MOV R0, 1\n"
+        instructions += "BLTI R0, 0x2, 0x6\n"
+        instructions += "MOV R0, 0\n"
+        instructions += "MOV R1, 1"
+
+        # Jit
+        jitter = jit_instructions(instructions)
+
+        # Check expected results
+        assert(jitter.cpu.R0 == 1)
+        assert(jitter.cpu.R1 == 1)
diff --git a/test/arch/mep/jit/test_jit_repeat.py b/test/arch/mep/jit/test_jit_repeat.py
new file mode 100644
index 00000000..9fa64fa5
--- /dev/null
+++ b/test/arch/mep/jit/test_jit_repeat.py
@@ -0,0 +1,99 @@
+# Toshiba MeP-c4 - *REPEAT instructions JIT unit tests
+# Guillaume Valadon <guillaume@valadon.net>
+
+from ut_helpers_jit import jit_instructions
+
+
+class TestRepeat:
+    def test_repeat(self):
+        """Test REPEAT jit"""
+
+        # Instructions that will be jitted
+        instructions = "MOV R0, 8\n"
+        instructions += "REPEAT R0, 0x6\n"
+        instructions += "ADD R1, 1\n"
+        instructions += "ADD R2, 1\n"  # <-RPE
+        instructions += "ADD R3, 1"
+
+        # Jit
+        jitter = jit_instructions(instructions)
+
+        # Check expected results
+        assert(jitter.cpu.R0 == 8)
+        assert(jitter.cpu.R1 == 8)
+        assert(jitter.cpu.R2 == 8)
+        assert(jitter.cpu.R3 == 8)
+
+    def test_erepeat_0(self):
+        """Test EREPEAT jit"""
+
+        # Instructions that will be jitted
+        instructions = "EREPEAT 0xA\n"
+        instructions += "ADD R1, 1\n"
+        instructions += "BEQI R1, 0x6, 0x8\n"
+        instructions += "ADD R2, 1\n"
+        instructions += "ADD R3, 1"  # <- RPE
+
+        # Jit
+        jitter = jit_instructions(instructions)
+
+        # Check expected results
+        assert(jitter.cpu.R1 == 6)
+        assert(jitter.cpu.R2 == 5)
+        assert(jitter.cpu.R3 == 5)
+
+    def test_erepeat_1(self):
+        """Test EREPEAT jit"""
+
+        # Instructions that will be jitted
+        instructions = "EREPEAT 0x8\n"
+        instructions += "ADD R1, 1\n"
+        instructions += "ADD R2, 1\n"
+        instructions += "ADD R3, 1\n"
+        instructions += "BEQI R1, 0x6, 0x4\n"  # <- RPE
+        instructions += "ADD R2, 1\n"
+        instructions += "ADD R3, 1"
+
+        # Jit
+        jitter = jit_instructions(instructions)
+
+        # Check expected results
+        assert(jitter.cpu.R1 == 6)
+        assert(jitter.cpu.R2 == 7)
+        assert(jitter.cpu.R3 == 7)
+
+    def test_erepeat_2(self):
+        """Test EREPEAT jit"""
+
+        # Instructions that will be jitted
+        instructions = "EREPEAT 0x8\n"
+        instructions += "ADD R1, 1\n"
+        instructions += "ADD R2, 1\n"
+        instructions += "ADD R3, 1\n"  # <- RPE
+        instructions += "BEQI R3, 0x6, 0x4"
+
+        # Jit
+        jitter = jit_instructions(instructions)
+
+        # Check expected results
+        assert(jitter.cpu.R1 == 6)
+        assert(jitter.cpu.R2 == 6)
+        assert(jitter.cpu.R3 == 6)
+
+    def test_erepeat_3(self):
+        """Test EREPEAT jit"""
+
+        # Instructions that will be jitted
+        instructions = "EREPEAT 0x8\n"
+        instructions += "ADD R1, 1\n"
+        instructions += "ADD R2, 1\n"
+        instructions += "BEQI R1, 0x6, 0x6\n"  # <- RPE
+        instructions += "ADD R3, 1"
+
+        # Jit
+        jitter = jit_instructions(instructions)
+
+        # Check expected results
+        assert(jitter.cpu.R1 == 6)
+        assert(jitter.cpu.R2 == 6)
+        assert(jitter.cpu.R3 == 5)
diff --git a/test/arch/mep/jit/ut_helpers_jit.py b/test/arch/mep/jit/ut_helpers_jit.py
new file mode 100644
index 00000000..590c534f
--- /dev/null
+++ b/test/arch/mep/jit/ut_helpers_jit.py
@@ -0,0 +1,49 @@
+# Toshiba MeP-c4 - unit tests helpers
+# Guillaume Valadon <guillaume@valadon.net>
+
+from miasm2.analysis.machine import Machine
+from miasm2.jitter.csts import PAGE_READ, PAGE_WRITE
+
+
+def jit_instructions(mn_str):
+    """JIT instructions and return the jitter object."""
+
+    # Get the miasm2 Machine
+    machine = Machine("mepb")
+    mn_mep = machine.mn()
+
+    # Assemble the instructions
+    asm = ""
+    for instr_str in mn_str.split("\n"):
+        instr = mn_mep.fromstring(instr_str, "b")
+        instr.mode = "b"
+        asm += mn_mep.asm(instr)[0]
+
+    # Init the jitter and add the assembled instructions to memory
+    jitter = machine.jitter(jit_type="gcc")
+    jitter.vm.add_memory_page(0, PAGE_READ | PAGE_WRITE, asm)
+
+    # Set the breakpoint
+    jitter.add_breakpoint(len(asm), lambda x: False)
+
+    # Jit the instructions
+    #jitter.init_stack()
+    jitter.init_run(0)
+    jitter.continue_run()
+
+    return jitter
+
+
+def launch_tests(obj):
+    """Call test methods by name"""
+
+    test_methods = [name for name in dir(obj) if name.startswith("test")]
+
+    for method in test_methods:
+        print method
+        try:
+            getattr(obj, method)()
+        except AttributeError as e:
+            print "Method not found: %s" % method
+            assert(False)
+        print '-' * 42