#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) {
}
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);
}
}
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];
}
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: {
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)]
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
#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; \
#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; \
#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; \
#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; \
#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; \
#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;
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];
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); \
\
printf("ERROR:\n");
ovm_program_print_instructions(program, state->pc - 1, 1);
fflush(stdout);
- assert(("ILLEGAL INSTRUCTION", 0));
+ ovm_assert(("ILLEGAL INSTRUCTION", 0));
break;
}