]> git.sesse.net Git - interpreter_trials/commitdiff
interpreter 3: inline cycle update
authorRune Holm <rune.holm@arm.com>
Wed, 16 Jun 2021 21:38:57 +0000 (23:38 +0200)
committerRune Holm <rune.holm@arm.com>
Wed, 16 Jun 2021 21:38:57 +0000 (23:38 +0200)
interpreter1.cpp
interpreter2.cpp
interpreter3.cpp [new file with mode: 0644]
interpreter_trials.cpp
meson.build

index 59f58d6f4453174f88353b2d3354c1acb50821aa..2d118b82f417d82dcbe5a2d33ded3e99a59b1a00 100644 (file)
@@ -109,8 +109,8 @@ namespace interpreter1
             while(true)
             {
                 uint8_t opcode = *global_state.pc++;
-                dispatch_table[opcode]();
                 global_state.cycle_count += cycle_table[opcode];
+                dispatch_table[opcode]();
 
 
             }
index bef286e289e107c74afffd3780c4069ef137c76e..b5342e70bfd099862eff570025b3c376c047fd14 100644 (file)
@@ -110,8 +110,8 @@ namespace interpreter2
             while(true)
             {
                 uint8_t opcode = *state->pc++;
-                dispatch_table[opcode](state);
                 state->cycle_count += cycle_table[opcode];
+                dispatch_table[opcode](state);
 
 
             }
diff --git a/interpreter3.cpp b/interpreter3.cpp
new file mode 100644 (file)
index 0000000..11aca84
--- /dev/null
@@ -0,0 +1,134 @@
+#include "bytecode.h"
+#include <utility>
+#include "interpreter.h"
+
+namespace interpreter3
+{
+
+    struct CpuState
+    {
+        int32_t regs[16] = {0};
+        bool zero_flag = false;
+        bool negative_flag = false;
+        uint8_t *pc = nullptr;
+        uint32_t cycle_count = 0;
+    };
+
+
+    struct ReturnException
+    {
+    };
+
+    void op_return(CpuState *state)
+    {
+        state->cycle_count += 1;
+        throw ReturnException();
+    }
+
+    void op_add(CpuState *state)
+    {
+        uint8_t reg = *state->pc++;
+        uint8_t dest = reg>>4, src = reg & 0xf;
+
+        int32_t v = state->regs[dest];
+        v += state->regs[src];
+
+        state->zero_flag = (v == 0);
+        state->negative_flag = (v < 0);
+        state->regs[dest] = v;
+        state->cycle_count += 2;
+
+    }
+
+    void op_sub(CpuState *state)
+    {
+        uint8_t reg = *state->pc++;
+        uint8_t dest = reg>>4, src = reg & 0xf;
+
+        int32_t v = state->regs[dest];
+        v -= state->regs[src];
+
+        state->zero_flag = (v == 0);
+        state->negative_flag = (v < 0);
+        state->regs[dest] = v;
+        state->cycle_count += 2;
+    }
+
+    void op_mov(CpuState *state)
+    {
+        uint8_t reg = *state->pc++;
+        uint8_t dest = reg>>4, src = reg & 0xf;
+
+        state->regs[dest] = state->regs[src];
+        state->cycle_count += 2;
+
+    }
+
+    void op_movi(CpuState *state)
+    {
+        uint8_t reg = *state->pc++;
+        uint8_t dest = reg>>4;
+
+        int32_t imm = *(int32_t *)state->pc;
+        state->pc += 4;
+
+        state->regs[dest] = imm;
+        state->cycle_count += 6;
+
+    }
+
+    void op_b(CpuState *state)
+    {
+        int8_t rel = *state->pc++;
+        state->pc += rel;
+        state->cycle_count += 2;
+    }
+
+    void op_bnz(CpuState *state)
+    {
+        int8_t rel = *state->pc++;
+        if(!state->zero_flag)
+        {
+            state->pc += rel;
+        }
+        state->cycle_count += 2;
+    }
+
+    void (*dispatch_table[])(CpuState *) =
+    {
+        op_return,
+        op_add,
+        op_sub,
+        op_mov,
+        op_movi,
+        op_b,
+        op_bnz
+    };
+
+    std::pair<int32_t, uint32_t> interpreter_run(uint8_t *program, int32_t param)
+    {
+        CpuState local_state;
+        CpuState *state = &local_state;
+        state->pc = program;
+        state->regs[X0] = param;
+        state->cycle_count = 0;
+        try
+        {
+            while(true)
+            {
+                uint8_t opcode = *state->pc++;
+                dispatch_table[opcode](state);
+
+
+            }
+
+
+        } catch(ReturnException ex)
+        {
+            return std::make_pair(state->regs[X0], state->cycle_count);
+        }
+
+    }
+
+
+}
index 58cc436b44f316d2201bac481c21b431f1e5ef20..353bfa74ce48cd5aa782a224c6db14dd14454a99 100644 (file)
@@ -13,7 +13,7 @@ void run(RunLoop run_loop)
     {
         throw std::runtime_error("Invalid result " + std::to_string(res));
     }
-    if(cycles != 480)
+    if(cycles != 481)
     {
         throw std::runtime_error("Invalid cycle count " + std::to_string(cycles));
 
@@ -36,4 +36,12 @@ static void interpreter2_fibonacci(benchmark::State& state) {
 }
 BENCHMARK(interpreter2_fibonacci);
 
+static void interpreter3_fibonacci(benchmark::State& state) {
+    for (auto _ : state)
+    {
+        run(interpreter3::interpreter_run);
+    }
+}
+BENCHMARK(interpreter3_fibonacci);
+
 BENCHMARK_MAIN();
index 0665ce88363d987405566236a94789f42867bd44..40190ece4af70c5ccec70bbcc1f47de6397bc199 100644 (file)
@@ -9,4 +9,5 @@ executable('interpreter_trials',
        'bytecode.cpp',
        'interpreter1.cpp',
        'interpreter2.cpp',
+       'interpreter3.cpp',
                                        dependencies : benchmark_dep)