]> git.sesse.net Git - interpreter_trials/commitdiff
use exit flag instead of exception
authorRune Holm <rune.holm@arm.com>
Thu, 17 Jun 2021 11:45:09 +0000 (13:45 +0200)
committerRune Holm <rune.holm@arm.com>
Thu, 17 Jun 2021 11:45:09 +0000 (13:45 +0200)
interpreter1.cpp
interpreter2.cpp
interpreter3.cpp
interpreter4.cpp
interpreter5.cpp
interpreter6.cpp

index 2d118b82f417d82dcbe5a2d33ded3e99a59b1a00..303abf15fce059d696124b92ffad87322b1c7b4d 100644 (file)
@@ -12,17 +12,14 @@ namespace interpreter1
         bool negative_flag = false;
         uint8_t *pc = nullptr;
         uint32_t cycle_count = 0;
+        bool exit = false;
     };
 
     CpuState global_state;
 
-    struct ReturnException
-    {
-    };
-
     void op_return()
     {
-        throw ReturnException();
+        global_state.exit = true;
     }
 
     void op_add()
@@ -104,23 +101,16 @@ namespace interpreter1
         global_state.pc = program;
         global_state.regs[X0] = param;
         global_state.cycle_count = 0;
-        try
-        {
-            while(true)
-            {
-                uint8_t opcode = *global_state.pc++;
-                global_state.cycle_count += cycle_table[opcode];
-                dispatch_table[opcode]();
-
-
-            }
-
+        global_state.exit = false;
 
-        } catch(ReturnException ex)
+        while(!global_state.exit)
         {
-            return std::make_pair(global_state.regs[X0], global_state.cycle_count);
+            uint8_t opcode = *global_state.pc++;
+            global_state.cycle_count += cycle_table[opcode];
+            dispatch_table[opcode]();
         }
 
+        return std::make_pair(global_state.regs[X0], global_state.cycle_count);
     }
 
 
index b5342e70bfd099862eff570025b3c376c047fd14..ab885cc70930605b28130bf56be7c69f11e25ba5 100644 (file)
@@ -12,16 +12,13 @@ namespace interpreter2
         bool negative_flag = false;
         uint8_t *pc = nullptr;
         uint32_t cycle_count = 0;
+        bool exit = false;
     };
 
 
-    struct ReturnException
-    {
-    };
-
     void op_return(CpuState *state)
     {
-        throw ReturnException();
+        state->exit = true;
     }
 
     void op_add(CpuState *state)
@@ -105,23 +102,16 @@ namespace interpreter2
         state->pc = program;
         state->regs[X0] = param;
         state->cycle_count = 0;
-        try
+        state->exit = false;
+        while(!state->exit)
         {
-            while(true)
-            {
-                uint8_t opcode = *state->pc++;
-                state->cycle_count += cycle_table[opcode];
-                dispatch_table[opcode](state);
-
+            uint8_t opcode = *state->pc++;
+            state->cycle_count += cycle_table[opcode];
+            dispatch_table[opcode](state);
 
-            }
 
-
-        } catch(ReturnException ex)
-        {
-            return std::make_pair(state->regs[X0], state->cycle_count);
         }
-
+        return std::make_pair(state->regs[X0], state->cycle_count);
     }
 
 
index 11aca84dfc4c72d361e26cd60b92963eb7253290..4c5dbe26466f9a407577b88125548063f7c9063c 100644 (file)
@@ -12,17 +12,14 @@ namespace interpreter3
         bool negative_flag = false;
         uint8_t *pc = nullptr;
         uint32_t cycle_count = 0;
+        bool exit = false;
     };
 
 
-    struct ReturnException
-    {
-    };
-
     void op_return(CpuState *state)
     {
         state->cycle_count += 1;
-        throw ReturnException();
+        state->exit = true;
     }
 
     void op_add(CpuState *state)
@@ -112,22 +109,16 @@ namespace interpreter3
         state->pc = program;
         state->regs[X0] = param;
         state->cycle_count = 0;
-        try
-        {
-            while(true)
-            {
-                uint8_t opcode = *state->pc++;
-                dispatch_table[opcode](state);
+        state->exit = false;
 
-
-            }
-
-
-        } catch(ReturnException ex)
+        while(!state->exit)
         {
-            return std::make_pair(state->regs[X0], state->cycle_count);
+            uint8_t opcode = *state->pc++;
+            dispatch_table[opcode](state);
         }
 
+        return std::make_pair(state->regs[X0], state->cycle_count);
+
     }
 
 
index c0d95d41fce42578833818f0eaf35459242803f9..56962b3b0937905912236dbc7fb7599c1e423edd 100644 (file)
@@ -11,17 +11,15 @@ namespace interpreter4
         bool zero_flag = false;
         bool negative_flag = false;
         uint32_t cycle_count = 0;
+        bool exit = false;
     };
 
 
-    struct ReturnException
-    {
-    };
-
     uint8_t *op_return(CpuState *state, uint8_t *pc)
     {
         state->cycle_count += 1;
-        throw ReturnException();
+        state->exit = true;
+        return pc;
     }
 
     uint8_t *op_add(CpuState *state, uint8_t *pc)
@@ -116,22 +114,15 @@ namespace interpreter4
         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)
+        state->exit = false;
+        while(!state->exit)
         {
-            return std::make_pair(state->regs[X0], state->cycle_count);
+            uint8_t opcode = *pc++;
+            pc = dispatch_table[opcode](state, pc);
         }
 
+        return std::make_pair(state->regs[X0], state->cycle_count);
+
     }
 
 
index ff400c6206fd6283cff84eae642c637ed8ed817d..c251296e20f6dbeedb1aa99de425bd9b87311ef9 100644 (file)
@@ -15,11 +15,7 @@ namespace interpreter5
     {
         int32_t regs[16] = {0};
         uint32_t cycle_count = 0;
-    };
-
-
-    struct ReturnException
-    {
+        bool exit = false;
     };
 
 
@@ -37,7 +33,8 @@ namespace interpreter5
     ReturnVal op_return(PARAMS)
     {
         state->cycle_count += 1;
-        throw ReturnException();
+        state->exit = true;
+        return RETURN_VAL;
     }
 
     ReturnVal op_add(PARAMS)
@@ -132,23 +129,19 @@ namespace interpreter5
         uint8_t *pc = program;
         state->regs[X0] = param;
         state->cycle_count = 0;
+        state->exit = false;
         Flags flags = {0, 0};
-        try
-        {
-            while(true)
-            {
-                uint8_t opcode = *pc++;
-                ReturnVal ret = dispatch_table[opcode](ARGS);
-                pc = ret.pc;
-                flags = ret.flags;
-            }
 
-
-        } catch(ReturnException ex)
+        while(!state->exit)
         {
-            return std::make_pair(state->regs[X0], state->cycle_count);
+            uint8_t opcode = *pc++;
+            ReturnVal ret = dispatch_table[opcode](ARGS);
+            pc = ret.pc;
+            flags = ret.flags;
         }
 
+        return std::make_pair(state->regs[X0], state->cycle_count);
+
     }
 
 
index e71cfb93916af05499b375957ec23599545d849b..590caa0ca1158e2f497828c83898e8feb1721b3b 100644 (file)
@@ -15,11 +15,7 @@ namespace interpreter6
     {
         int32_t regs[16] = {0};
         uint32_t final_cycle_count = 0;
-    };
-
-
-    struct ReturnException
-    {
+        bool exit = false;
     };
 
 
@@ -39,7 +35,8 @@ namespace interpreter6
     {
         cycle_count += 1;
         state->final_cycle_count = cycle_count;
-        throw ReturnException();
+        state->exit = true;
+        return RETURN_VAL;
     }
 
     ReturnVal op_add(PARAMS)
@@ -133,25 +130,20 @@ namespace interpreter6
         CpuState *state = &local_state;
         uint8_t *pc = program;
         state->regs[X0] = param;
+        state->exit = false;
         uint32_t cycle_count = 0;
         Flags flags = {0, 0};
-        try
+        while(!state->exit)
         {
-            while(true)
-            {
-                uint8_t opcode = *pc++;
-                ReturnVal ret = dispatch_table[opcode](ARGS);
-                pc = ret.pc;
-                flags = ret.flags;
-                cycle_count = ret.cycle_count;
-            }
-
-
-        } catch(ReturnException ex)
-        {
-            return std::make_pair(state->regs[X0], state->final_cycle_count);
+            uint8_t opcode = *pc++;
+            ReturnVal ret = dispatch_table[opcode](ARGS);
+            pc = ret.pc;
+            flags = ret.flags;
+            cycle_count = ret.cycle_count;
         }
 
+        return std::make_pair(state->regs[X0], state->final_cycle_count);
+
     }