]> git.sesse.net Git - interpreter_trials/commitdiff
added tail-calling interpreter
authorRune Holm <rune.holm@arm.com>
Wed, 16 Jun 2021 22:24:12 +0000 (00:24 +0200)
committerRune Holm <rune.holm@arm.com>
Wed, 16 Jun 2021 22:24:12 +0000 (00:24 +0200)
interpreter7.cpp [new file with mode: 0644]
interpreter_trials.cpp
meson.build

diff --git a/interpreter7.cpp b/interpreter7.cpp
new file mode 100644 (file)
index 0000000..c68ba02
--- /dev/null
@@ -0,0 +1,159 @@
+#include "bytecode.h"
+#include <utility>
+#include "interpreter.h"
+
+namespace interpreter7
+{
+
+    struct Flags
+    {
+        unsigned int zero:16;
+        unsigned int negative:16;
+    };
+
+    struct CpuState
+    {
+        int32_t regs[16] = {0};
+        uint32_t final_cycle_count = 0;
+    };
+
+
+    struct ReturnException
+    {
+    };
+
+
+
+#define PARAMS CpuState *state, uint8_t *pc, Flags flags, uint32_t cycle_count
+#define ARGS state, pc, flags, cycle_count
+
+    extern void (*dispatch_table[])(PARAMS);
+
+    void op_return(PARAMS)
+    {
+        cycle_count += 1;
+        state->final_cycle_count = cycle_count;
+        throw ReturnException();
+    }
+
+    void op_add(PARAMS)
+    {
+        uint8_t reg = *pc++;
+        uint8_t dest = reg>>4, src = reg & 0xf;
+
+        int32_t v = state->regs[dest];
+        v += state->regs[src];
+
+        flags.zero = (v == 0);
+        flags.negative = (v < 0);
+        state->regs[dest] = v;
+        cycle_count += 2;
+
+        uint8_t opcode = *pc++;
+        dispatch_table[opcode](ARGS);
+    }
+
+    void op_sub(PARAMS)
+    {
+        uint8_t reg = *pc++;
+        uint8_t dest = reg>>4, src = reg & 0xf;
+
+        int32_t v = state->regs[dest];
+        v -= state->regs[src];
+
+        flags.zero = (v == 0);
+        flags.negative = (v < 0);
+        state->regs[dest] = v;
+        cycle_count += 2;
+
+        uint8_t opcode = *pc++;
+        dispatch_table[opcode](ARGS);
+    }
+
+    void op_mov(PARAMS)
+    {
+        uint8_t reg = *pc++;
+        uint8_t dest = reg>>4, src = reg & 0xf;
+
+        state->regs[dest] = state->regs[src];
+        cycle_count += 2;
+
+        uint8_t opcode = *pc++;
+        dispatch_table[opcode](ARGS);
+    }
+
+    void op_movi(PARAMS)
+    {
+        uint8_t reg = *pc++;
+        uint8_t dest = reg>>4;
+
+        int32_t imm = *(int32_t *)pc;
+        pc += 4;
+
+        state->regs[dest] = imm;
+        cycle_count += 6;
+
+        uint8_t opcode = *pc++;
+        dispatch_table[opcode](ARGS);
+    }
+
+    void op_b(PARAMS)
+    {
+        int8_t rel = *pc++;
+        pc += rel;
+        cycle_count += 2;
+
+        uint8_t opcode = *pc++;
+        dispatch_table[opcode](ARGS);
+    }
+
+    void op_bnz(PARAMS)
+    {
+        int8_t rel = *pc++;
+        if(!flags.zero)
+        {
+            pc += rel;
+        }
+        cycle_count += 2;
+
+        uint8_t opcode = *pc++;
+        dispatch_table[opcode](ARGS);
+    }
+
+    void (*dispatch_table[])(PARAMS) =
+    {
+        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;
+        uint32_t cycle_count = 0;
+        Flags flags = {0, 0};
+        try
+        {
+            while(true)
+            {
+                uint8_t opcode = *pc++;
+                dispatch_table[opcode](ARGS);
+            }
+
+
+        } catch(ReturnException ex)
+        {
+            return std::make_pair(state->regs[X0], state->final_cycle_count);
+        }
+
+    }
+
+
+}
index 31ed90628dd089622c9b8166d4e614315765609e..602b6d82d02039e91ba2f18eeb81be72be7429c2 100644 (file)
@@ -68,4 +68,12 @@ static void interpreter6_pc_flags_cycle_count_in_registers(benchmark::State& sta
 }
 BENCHMARK(interpreter6_pc_flags_cycle_count_in_registers);
 
+static void interpreter7_tail_calls(benchmark::State& state) {
+    for (auto _ : state)
+    {
+        run(interpreter7::interpreter_run);
+    }
+}
+BENCHMARK(interpreter7_tail_calls);
+
 BENCHMARK_MAIN();
index f53cca78d4f534f6cfbdc416736c5148a7c3587d..7c2ca51116131799d6bd73ab07bf2e5257363048 100644 (file)
@@ -13,4 +13,5 @@ executable('interpreter_trials',
        'interpreter4.cpp',
        'interpreter5.cpp',
        'interpreter6.cpp',
+       'interpreter7.cpp',
                                        dependencies : benchmark_dep)