state->value_number_offset = 0;
state->numbered_values = NULL;
- state->params = NULL;
state->stack_frames = NULL;
state->registers = NULL;
bh_arr_new(store->heap_allocator, state->numbered_values, 128);
- bh_arr_new(store->heap_allocator, state->params, 16);
bh_arr_new(store->heap_allocator, state->stack_frames, 32);
bh_arr_new(store->heap_allocator, state->registers, program->register_count);
bh_arr_insert_end(state->registers, program->register_count);
+ state->param_buf = bh_alloc_array(store->arena_allocator, ovm_value_t, OVM_MAX_PARAM_COUNT);
+ state->param_count = 0;
+
state->external_funcs = NULL;
bh_arr_new(store->heap_allocator, state->external_funcs, 8);
ovm_store_t *store = state->store;
bh_arr_free(state->numbered_values);
- bh_arr_free(state->params);
bh_arr_free(state->stack_frames);
bh_arr_free(state->registers);
bh_arr_free(state->external_funcs);
#define OVMI_FUNC_NAME(n) ovmi_exec_##n
#define OVMI_DISPATCH_NAME ovmi_dispatch
#define OVMI_DEBUG_HOOK ((void)0)
+#define OVMI_EXCEPTION_HOOK ((void)0)
#include "./vm_instrs.h"
#define OVMI_FUNC_NAME(n) ovmi_exec_debug_##n
#define OVMI_DISPATCH_NAME ovmi_debug_dispatch
#define OVMI_DEBUG_HOOK if (state->debug) __ovm_debug_hook(state->engine, state)
+#define OVMI_EXCEPTION_HOOK __ovm_trigger_exception(state)
#include "./vm_instrs.h"
ovm_value_t ovm_run_code(ovm_engine_t *engine, ovm_state_t *state, ovm_program_t *program) {
OVMI_INSTR_EXEC(load_##otype) { \
ovm_assert(VAL(instr->a).type == OVM_TYPE_I32); \
u32 dest = VAL(instr->a).u32 + (u32) instr->b; \
- if (dest == 0) __ovm_trigger_exception(state); \
+ if (dest == 0) OVMI_EXCEPTION_HOOK; \
VAL(instr->r).stype = * (stype *) &memory[dest]; \
VAL(instr->r).type = type_; \
NEXT_OP; \
OVMI_INSTR_EXEC(store_##otype) { \
ovm_assert(VAL(instr->r).type == OVM_TYPE_I32); \
u32 dest = VAL(instr->r).u32 + (u32) instr->b; \
- if (dest == 0) __ovm_trigger_exception(state); \
+ if (dest == 0) OVMI_EXCEPTION_HOOK; \
*(stype *) &memory[dest] = VAL(instr->a).stype; \
NEXT_OP; \
}
u32 src = VAL(instr->a).u32;
u32 count = VAL(instr->b).u32;
- if (!dest || !src) __ovm_trigger_exception(state);
+ if (!dest || !src) OVMI_EXCEPTION_HOOK;
memmove(&memory[dest], &memory[src], count);
u8 byte = VAL(instr->a).u8;
i32 count = VAL(instr->b).i32;
- if (!dest) __ovm_trigger_exception(state);
+ if (!dest) OVMI_EXCEPTION_HOOK;
memset(&memory[dest], byte, count);
//
OVMI_INSTR_EXEC(param) {
- bh_arr_push(state->params, VAL(instr->a));
+ // bh_arr_push(state->params, VAL(instr->a));
+ ovm_assert((state->param_count <= OVM_MAX_PARAM_COUNT));
+ state->param_buf[state->param_count++] = VAL(instr->a);
NEXT_OP;
}
#define OVM_CALL_CODE(func_idx) \
i32 fidx = func_idx; \
ovm_func_t *func = &state->program->funcs[fidx]; \
- i32 extra_params = bh_arr_length(state->params) - func->param_count; \
+ i32 extra_params = state->param_count - func->param_count; \
ovm_assert(extra_params >= 0); \
ovm__func_setup_stack_frame(state, func, instr->r); \
- bh_arr_fastdeleten(state->params, func->param_count); \
+ state->param_count -= func->param_count; \
if (func->kind == OVM_FUNC_INTERNAL) { \
values = state->__frame_values; \
- memcpy(&VAL(0), &state->params[extra_params], func->param_count * sizeof(ovm_value_t)); \
+ memcpy(&VAL(0), &state->param_buf[extra_params], func->param_count * sizeof(ovm_value_t)); \
state->pc = func->start_instr; \
} else { \
ovm_external_func_t external_func = state->external_funcs[func->external_func_idx]; \
- external_func.native_func(external_func.userdata, &state->params[extra_params], &state->__tmp_value); \
+ external_func.native_func(external_func.userdata, &state->param_buf[extra_params], &state->__tmp_value); \
\
ovm__func_teardown_stack_frame(state); \
\
#define CMPXCHG(otype, ctype) \
OVMI_INSTR_EXEC(cmpxchg_##ctype) { \
pthread_mutex_lock(&state->engine->atomic_mutex); \
- if (VAL(instr->r).u32 == 0) __ovm_trigger_exception(state); \
+ if (VAL(instr->r).u32 == 0) OVMI_EXCEPTION_HOOK; \
ctype *addr = (ctype *) &memory[VAL(instr->r).u32]; \
\
VAL(instr->r).u64 = 0; \
OVMI_INSTR_EXEC(illegal) {
- __ovm_trigger_exception(state);
+ OVMI_EXCEPTION_HOOK;
return ((ovm_value_t) {0});
}
#undef OVMI_FUNC_NAME
#undef OVMI_DISPATCH_NAME
#undef OVMI_DEBUG_HOOK
+#undef OVMI_EXCEPTION_HOOK