]> git.sesse.net Git - interpreter_trials/commitdiff
interpreter 4: pass pc as parameter
authorRune Holm <rune.holm@arm.com>
Wed, 16 Jun 2021 21:46:13 +0000 (23:46 +0200)
committerRune Holm <rune.holm@arm.com>
Wed, 16 Jun 2021 21:46:13 +0000 (23:46 +0200)
interpreter4.cpp [new file with mode: 0644]
interpreter_trials.cpp
meson.build

diff --git a/interpreter4.cpp b/interpreter4.cpp
new file mode 100644 (file)
index 0000000..c0d95d4
--- /dev/null
@@ -0,0 +1,138 @@
+#include "bytecode.h"
+#include <utility>
+#include "interpreter.h"
+
+namespace interpreter4
+{
+
+    struct CpuState
+    {
+        int32_t regs[16] = {0};
+        bool zero_flag = false;
+        bool negative_flag = false;
+        uint32_t cycle_count = 0;
+    };
+
+
+    struct ReturnException
+    {
+    };
+
+    uint8_t *op_return(CpuState *state, uint8_t *pc)
+    {
+        state->cycle_count += 1;
+        throw ReturnException();
+    }
+
+    uint8_t *op_add(CpuState *state, uint8_t *pc)
+    {
+        uint8_t reg = *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;
+        return pc;
+    }
+
+    uint8_t *op_sub(CpuState *state, uint8_t *pc)
+    {
+        uint8_t reg = *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;
+        return pc;
+    }
+
+    uint8_t *op_mov(CpuState *state, uint8_t *pc)
+    {
+        uint8_t reg = *pc++;
+        uint8_t dest = reg>>4, src = reg & 0xf;
+
+        state->regs[dest] = state->regs[src];
+        state->cycle_count += 2;
+        return pc;
+
+    }
+
+    uint8_t *op_movi(CpuState *state, uint8_t *pc)
+    {
+        uint8_t reg = *pc++;
+        uint8_t dest = reg>>4;
+
+        int32_t imm = *(int32_t *)pc;
+        pc += 4;
+
+        state->regs[dest] = imm;
+        state->cycle_count += 6;
+        return pc;
+
+    }
+
+    uint8_t *op_b(CpuState *state, uint8_t *pc)
+    {
+        int8_t rel = *pc++;
+        pc += rel;
+        state->cycle_count += 2;
+        return pc;
+    }
+
+    uint8_t *op_bnz(CpuState *state, uint8_t *pc)
+    {
+        int8_t rel = *pc++;
+        if(!state->zero_flag)
+        {
+            pc += rel;
+        }
+        state->cycle_count += 2;
+        return pc;
+    }
+
+    uint8_t *(*dispatch_table[])(CpuState *state, uint8_t *pc) =
+    {
+        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;
+        uint8_t *pc = program;
+        state->regs[X0] = param;
+        state->cycle_count = 0;
+        try
+        {
+            while(true)
+            {
+                uint8_t opcode = *pc++;
+                pc = dispatch_table[opcode](state, pc);
+
+
+            }
+
+
+        } catch(ReturnException ex)
+        {
+            return std::make_pair(state->regs[X0], state->cycle_count);
+        }
+
+    }
+
+
+}
index 353bfa74ce48cd5aa782a224c6db14dd14454a99..00dc05d4bd3c12b6b90fcf50fca805dd540cabd3 100644 (file)
@@ -44,4 +44,12 @@ static void interpreter3_fibonacci(benchmark::State& state) {
 }
 BENCHMARK(interpreter3_fibonacci);
 
+static void interpreter4_fibonacci(benchmark::State& state) {
+    for (auto _ : state)
+    {
+        run(interpreter4::interpreter_run);
+    }
+}
+BENCHMARK(interpreter4_fibonacci);
+
 BENCHMARK_MAIN();
index 40190ece4af70c5ccec70bbcc1f47de6397bc199..308f090fe20155a05002c2d9c928927e46f38aa7 100644 (file)
@@ -10,4 +10,5 @@ executable('interpreter_trials',
        'interpreter1.cpp',
        'interpreter2.cpp',
        'interpreter3.cpp',
+       'interpreter4.cpp',
                                        dependencies : benchmark_dep)