replaced assert with ovm_assert
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 25 Jul 2022 13:21:50 +0000 (08:21 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 25 Jul 2022 13:21:50 +0000 (08:21 -0500)
src/vm/vm.c

index 03bf165c7c20d2c08d889c58e2af4ee01fbc6931..155d2e401a5677af041b5b2ef61e240bd40932a5 100644 (file)
@@ -5,6 +5,12 @@
 #include <math.h> // REMOVE THIS!!!  only needed for sqrt
 #include <pthread.h>
 
+#ifdef OVM_DEBUG
+#define ovm_assert(c) assert((c))
+#else
+#define ovm_assert(c)
+#endif
+
 
 static inline void ovm_print_val(ovm_value_t val) {
     switch (val.type) {
@@ -450,10 +456,10 @@ void ovm_engine_delete(ovm_engine_t *engine) {
 }
 
 void ovm_engine_memory_copy(ovm_engine_t *engine, i64 target, void *data, i64 size) {
-    assert(engine);
-    assert(engine->memory);
-    assert(data);
-    assert(size + target < engine->memory_size);
+    ovm_assert(engine);
+    ovm_assert(engine->memory);
+    ovm_assert(data);
+    ovm_assert(size + target < engine->memory_size);
     memcpy(((u8 *) engine->memory) + target, data, size);
 }
 
@@ -511,7 +517,7 @@ void ovm_state_register_external_func(ovm_state_t *state, i32 idx, void (*func)(
 }
 
 ovm_value_t ovm_state_register_get(ovm_state_t *state, i32 idx) {
-    assert(idx < bh_arr_length(state->registers));
+    ovm_assert(idx < bh_arr_length(state->registers));
 
     return state->registers[idx];
 }
@@ -562,7 +568,7 @@ static inline ovm_stack_frame_t ovm__func_teardown_stack_frame(ovm_engine_t *eng
 
 ovm_value_t ovm_func_call(ovm_engine_t *engine, ovm_state_t *state, ovm_program_t *program, i32 func_idx, i32 param_count, ovm_value_t *params) {
     ovm_func_t func = program->funcs[func_idx];
-    assert(func.value_number_count >= func.param_count);
+    ovm_assert(func.value_number_count >= func.param_count);
 
     switch (func.kind) {
         case OVM_FUNC_INTERNAL: {
@@ -637,9 +643,9 @@ static inline double __ovm_copysign(a, b) double a, b; {
 
 
 ovm_value_t ovm_run_code(ovm_engine_t *engine, ovm_state_t *state, ovm_program_t *program) {
-    assert(engine);
-    assert(state);
-    assert(program);
+    ovm_assert(engine);
+    ovm_assert(state);
+    ovm_assert(program);
 
 #define VAL(loc) state->numbered_values[(u32) (loc + state->value_number_offset)]
 
@@ -656,6 +662,8 @@ ovm_value_t ovm_run_code(ovm_engine_t *engine, ovm_state_t *state, ovm_program_t
         tmp_val.type = OVM_TYPE_NONE;
         tmp_val.u64  = 0;
 
+        // Check if breakpoints are hit
+
         //
         // Incrementing the program counter here.
         // All instructions that compute something relative
@@ -676,7 +684,7 @@ ovm_value_t ovm_run_code(ovm_engine_t *engine, ovm_state_t *state, ovm_program_t
 
 #define OVM_OP(i, t, op, ctype) \
             case OVM_TYPED_INSTR(i, t): \
-                assert(VAL(instr.a).type == t && VAL(instr.b).type == t); \
+                ovm_assert(VAL(instr.a).type == t && VAL(instr.b).type == t); \
                 tmp_val.type = t; \
                 tmp_val.ctype = VAL(instr.a).ctype op VAL(instr.b).ctype; \
                 VAL(instr.r) = tmp_val; \
@@ -761,7 +769,7 @@ ovm_value_t ovm_run_code(ovm_engine_t *engine, ovm_state_t *state, ovm_program_t
 
 #define OVM_OP(i, t, func, ctype) \
             case OVM_TYPED_INSTR(i, t): \
-                assert(VAL(instr.a).type == t && VAL(instr.b).type == t); \
+                ovm_assert(VAL(instr.a).type == t && VAL(instr.b).type == t); \
                 tmp_val.type = t; \
                 tmp_val.ctype = func( VAL(instr.a).ctype, VAL(instr.b).ctype ); \
                 VAL(instr.r) = tmp_val; \
@@ -787,7 +795,7 @@ ovm_value_t ovm_run_code(ovm_engine_t *engine, ovm_state_t *state, ovm_program_t
 
 #define OVM_OP(i, t, op, ctype) \
             case OVM_TYPED_INSTR(i, t): \
-                assert(VAL(instr.a).type == t); \
+                ovm_assert(VAL(instr.a).type == t); \
                 tmp_val.type = t; \
                 tmp_val.ctype = (ctype) op (VAL(instr.a).ctype); \
                 VAL(instr.r) = tmp_val; \
@@ -828,7 +836,7 @@ ovm_value_t ovm_run_code(ovm_engine_t *engine, ovm_state_t *state, ovm_program_t
 
 #define OVM_OP(i, t, op, ctype, cast_type) \
             case OVM_TYPED_INSTR(i, t): \
-                assert(VAL(instr.a).type == t && VAL(instr.b).type == t); \
+                ovm_assert(VAL(instr.a).type == t && VAL(instr.b).type == t); \
                 tmp_val.type = OVM_TYPE_I32; \
                 tmp_val.i32 = ((VAL(instr.a).ctype op VAL(instr.b).ctype)) ? 1 : 0; \
                 VAL(instr.r) = tmp_val; \
@@ -933,7 +941,7 @@ ovm_value_t ovm_run_code(ovm_engine_t *engine, ovm_state_t *state, ovm_program_t
 
 #define OVM_LOAD(type_, stype) \
             case OVM_TYPED_INSTR(OVMI_LOAD, type_): {\
-                assert(VAL(instr.a).type == OVM_TYPE_I32); \
+                ovm_assert(VAL(instr.a).type == OVM_TYPE_I32); \
                 tmp_val.type = type_; \
                 tmp_val.stype = * (stype *) &((u8 *) engine->memory)[VAL(instr.a).u32 + (u32) instr.b]; \
                 VAL(instr.r) = tmp_val; \
@@ -951,7 +959,7 @@ ovm_value_t ovm_run_code(ovm_engine_t *engine, ovm_state_t *state, ovm_program_t
 
 #define OVM_STORE(type_, stype) \
             case OVM_TYPED_INSTR(OVMI_STORE, type_): \
-                assert(VAL(instr.r).type == OVM_TYPE_I32); \
+                ovm_assert(VAL(instr.r).type == OVM_TYPE_I32); \
                 *(stype *) &((u8 *) engine->memory)[VAL(instr.r).u32 + (u32) instr.b] = VAL(instr.a).stype; \
                 break;
 
@@ -996,7 +1004,7 @@ ovm_value_t ovm_run_code(ovm_engine_t *engine, ovm_state_t *state, ovm_program_t
 
             case OVMI_IDX_ARR: {
                 ovm_static_integer_array_t data_elem = program->static_data[instr.a];
-                assert(VAL(instr.b).u32 < (u32) data_elem.len);
+                ovm_assert(VAL(instr.b).u32 < (u32) data_elem.len);
 
                 tmp_val.type = OVM_TYPE_I32;
                 tmp_val.i32 = program->static_integers[data_elem.start_idx + VAL(instr.b).u32];
@@ -1039,7 +1047,7 @@ ovm_value_t ovm_run_code(ovm_engine_t *engine, ovm_state_t *state, ovm_program_t
             i32 fidx = func_idx; \
             ovm_func_t *func = &program->funcs[fidx]; \
             i32 extra_params = bh_arr_length(state->params) - func->param_count; \
-            assert(extra_params >= 0); \
+            ovm_assert(extra_params >= 0); \
             if (func->kind == OVM_FUNC_INTERNAL) { \
                 ovm__func_setup_stack_frame(engine, state, program, fidx, instr.r); \
  \
@@ -1183,7 +1191,7 @@ ovm_value_t ovm_run_code(ovm_engine_t *engine, ovm_state_t *state, ovm_program_t
                 printf("ERROR:\n");
                 ovm_program_print_instructions(program, state->pc - 1, 1);
                 fflush(stdout);
-                assert(("ILLEGAL INSTRUCTION", 0));
+                ovm_assert(("ILLEGAL INSTRUCTION", 0));
                 break;
         }