]> git.sesse.net Git - interpreter_trials/commitdiff
second interpreter with state passed in pointers
authorRune Holm <rune.holm@arm.com>
Wed, 16 Jun 2021 21:31:56 +0000 (23:31 +0200)
committerRune Holm <rune.holm@arm.com>
Wed, 16 Jun 2021 21:34:40 +0000 (23:34 +0200)
interpreter.h
interpreter1.cpp
interpreter2.cpp [new file with mode: 0644]
interpreter_trials.cpp
meson.build

index 27fc43beafe3df4c6bd28d4f824bc9b3751970bb..91c1fcd3f7c8d556bbde277e50df7d9f37387602 100644 (file)
@@ -6,7 +6,27 @@
 
 namespace interpreter1
 {
-    std::pair<int32_t, uint32_t> interpreter1_run(uint8_t *program, int32_t param);
+    std::pair<int32_t, uint32_t> interpreter_run(uint8_t *program, int32_t param);
+}
+
+namespace interpreter2
+{
+    std::pair<int32_t, uint32_t> interpreter_run(uint8_t *program, int32_t param);
+}
+
+namespace interpreter3
+{
+    std::pair<int32_t, uint32_t> interpreter_run(uint8_t *program, int32_t param);
+}
+
+namespace interpreter4
+{
+    std::pair<int32_t, uint32_t> interpreter_run(uint8_t *program, int32_t param);
+}
+
+namespace interpreter5
+{
+    std::pair<int32_t, uint32_t> interpreter_run(uint8_t *program, int32_t param);
 }
 
 #endif
index 77924d14771f7a054dd02cc39b9847ae25306384..59f58d6f4453174f88353b2d3354c1acb50821aa 100644 (file)
@@ -99,7 +99,7 @@ namespace interpreter1
         op_bnz
     };
 
-    std::pair<int32_t, uint32_t> interpreter1_run(uint8_t *program, int32_t param)
+    std::pair<int32_t, uint32_t> interpreter_run(uint8_t *program, int32_t param)
     {
         global_state.pc = program;
         global_state.regs[X0] = param;
diff --git a/interpreter2.cpp b/interpreter2.cpp
new file mode 100644 (file)
index 0000000..bef286e
--- /dev/null
@@ -0,0 +1,128 @@
+#include "bytecode.h"
+#include <utility>
+#include "interpreter.h"
+
+namespace interpreter2
+{
+
+    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)
+    {
+        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;
+
+    }
+
+    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;
+    }
+
+    void op_mov(CpuState *state)
+    {
+        uint8_t reg = *state->pc++;
+        uint8_t dest = reg>>4, src = reg & 0xf;
+
+        state->regs[dest] = state->regs[src];
+
+    }
+
+    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;
+
+    }
+
+    void op_b(CpuState *state)
+    {
+        int8_t rel = *state->pc++;
+        state->pc += rel;
+    }
+
+    void op_bnz(CpuState *state)
+    {
+        int8_t rel = *state->pc++;
+        if(!state->zero_flag)
+        {
+            state->pc += rel;
+        }
+    }
+
+    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);
+                state->cycle_count += cycle_table[opcode];
+
+
+            }
+
+
+        } catch(ReturnException ex)
+        {
+            return std::make_pair(state->regs[X0], state->cycle_count);
+        }
+
+    }
+
+
+}
index 992a77597f2cb41d00e6c3e57397423effa1fa71..58cc436b44f316d2201bac481c21b431f1e5ef20 100644 (file)
@@ -20,13 +20,20 @@ void run(RunLoop run_loop)
     }
 }
 
-// Define another benchmark
 static void interpreter1_fibonacci(benchmark::State& state) {
     for (auto _ : state)
     {
-        run(interpreter1::interpreter1_run);
+        run(interpreter1::interpreter_run);
     }
 }
 BENCHMARK(interpreter1_fibonacci);
 
+static void interpreter2_fibonacci(benchmark::State& state) {
+    for (auto _ : state)
+    {
+        run(interpreter2::interpreter_run);
+    }
+}
+BENCHMARK(interpreter2_fibonacci);
+
 BENCHMARK_MAIN();
index a82b33053800699a35ca88dfd294be805bc8ffbf..0665ce88363d987405566236a94789f42867bd44 100644 (file)
@@ -8,4 +8,5 @@ executable('interpreter_trials',
        'interpreter_trials.cpp',
        'bytecode.cpp',
        'interpreter1.cpp',
+       'interpreter2.cpp',
                                        dependencies : benchmark_dep)