diff options
Diffstat (limited to 'src/binary-reader-interpreter.cc')
-rw-r--r-- | src/binary-reader-interpreter.cc | 340 |
1 files changed, 168 insertions, 172 deletions
diff --git a/src/binary-reader-interpreter.cc b/src/binary-reader-interpreter.cc index 9c5c40d1..ca28d412 100644 --- a/src/binary-reader-interpreter.cc +++ b/src/binary-reader-interpreter.cc @@ -26,10 +26,10 @@ #include "type-checker.h" #include "writer.h" -#define CHECK_RESULT(expr) \ - do { \ - if (WABT_FAILED(expr)) \ - return WABT_ERROR; \ +#define CHECK_RESULT(expr) \ + do { \ + if (WABT_FAILED(expr)) \ + return WabtResult::Error; \ } while (0) #define CHECK_LOCAL(ctx, local_index) \ @@ -39,7 +39,7 @@ if ((local_index) >= max_local_index) { \ print_error((ctx), "invalid local_index: %d (max %d)", (local_index), \ max_local_index); \ - return WABT_ERROR; \ + return WabtResult::Error; \ } \ } while (0) @@ -49,7 +49,7 @@ if ((global_index) >= max_global_index) { \ print_error((ctx), "invalid global_index: %d (max %d)", (global_index), \ max_global_index); \ - return WABT_ERROR; \ + return WabtResult::Error; \ } \ } while (0) @@ -207,13 +207,17 @@ static WabtResult emit_data_at(Context* ctx, static WabtResult emit_data(Context* ctx, const void* data, size_t size) { CHECK_RESULT(emit_data_at(ctx, ctx->istream_offset, data, size)); ctx->istream_offset += size; - return WABT_OK; + return WabtResult::Ok; } static WabtResult emit_opcode(Context* ctx, WabtOpcode opcode) { return emit_data(ctx, &opcode, sizeof(uint8_t)); } +static WabtResult emit_opcode(Context* ctx, WabtInterpreterOpcode opcode) { + return emit_data(ctx, &opcode, sizeof(uint8_t)); +} + static WabtResult emit_i8(Context* ctx, uint8_t value) { return emit_data(ctx, &value, sizeof(value)); } @@ -235,15 +239,14 @@ static WabtResult emit_drop_keep(Context* ctx, uint32_t drop, uint8_t keep) { assert(keep <= 1); if (drop > 0) { if (drop == 1 && keep == 0) { - CHECK_RESULT(emit_opcode(ctx, WABT_OPCODE_DROP)); + CHECK_RESULT(emit_opcode(ctx, WabtInterpreterOpcode::Drop)); } else { - CHECK_RESULT( - emit_opcode(ctx, static_cast<WabtOpcode>(WABT_OPCODE_DROP_KEEP))); + CHECK_RESULT(emit_opcode(ctx, WabtInterpreterOpcode::DropKeep)); CHECK_RESULT(emit_i32(ctx, drop)); CHECK_RESULT(emit_i8(ctx, keep)); } } - return WABT_OK; + return WabtResult::Ok; } static WabtResult append_fixup(Context* ctx, @@ -254,7 +257,7 @@ static WabtResult append_fixup(Context* ctx, Uint32Vector* fixups = &fixups_vector->data[index]; uint32_t offset = get_istream_offset(ctx); wabt_append_uint32_value(fixups, &offset); - return WABT_OK; + return WabtResult::Ok; } static WabtResult emit_br_offset(Context* ctx, @@ -268,7 +271,7 @@ static WabtResult emit_br_offset(Context* ctx, append_fixup(ctx, &ctx->depth_fixups, depth)); } CHECK_RESULT(emit_i32(ctx, offset)); - return WABT_OK; + return WabtResult::Ok; } static WabtResult get_br_drop_keep_count(Context* ctx, @@ -278,7 +281,7 @@ static WabtResult get_br_drop_keep_count(Context* ctx, WabtTypeCheckerLabel* label; CHECK_RESULT(wabt_typechecker_get_label(&ctx->typechecker, depth, &label)); *out_keep_count = - label->label_type != WABT_LABEL_TYPE_LOOP ? label->sig.size : 0; + label->label_type != WabtLabelType::Loop ? label->sig.size : 0; if (wabt_typechecker_is_unreachable(&ctx->typechecker)) { *out_drop_count = 0; } else { @@ -286,7 +289,7 @@ static WabtResult get_br_drop_keep_count(Context* ctx, (ctx->typechecker.type_stack.size - label->type_stack_limit) - *out_keep_count; } - return WABT_OK; + return WabtResult::Ok; } static WabtResult get_return_drop_keep_count(Context* ctx, @@ -294,11 +297,11 @@ static WabtResult get_return_drop_keep_count(Context* ctx, uint32_t* out_keep_count) { if (WABT_FAILED(get_br_drop_keep_count(ctx, ctx->label_stack.size - 1, out_drop_count, out_keep_count))) { - return WABT_ERROR; + return WabtResult::Error; } *out_drop_count += ctx->current_func->defined.param_and_local_types.size; - return WABT_OK; + return WabtResult::Ok; } static WabtResult emit_br(Context* ctx, @@ -306,9 +309,9 @@ static WabtResult emit_br(Context* ctx, uint32_t drop_count, uint32_t keep_count) { CHECK_RESULT(emit_drop_keep(ctx, drop_count, keep_count)); - CHECK_RESULT(emit_opcode(ctx, WABT_OPCODE_BR)); + CHECK_RESULT(emit_opcode(ctx, WabtInterpreterOpcode::Br)); CHECK_RESULT(emit_br_offset(ctx, depth, get_label(ctx, depth)->offset)); - return WABT_OK; + return WabtResult::Ok; } static WabtResult emit_br_table_offset(Context* ctx, uint32_t depth) { @@ -317,7 +320,7 @@ static WabtResult emit_br_table_offset(Context* ctx, uint32_t depth) { CHECK_RESULT(emit_br_offset(ctx, depth, get_label(ctx, depth)->offset)); CHECK_RESULT(emit_i32(ctx, drop_count)); CHECK_RESULT(emit_i8(ctx, keep_count)); - return WABT_OK; + return WabtResult::Ok; } static WabtResult fixup_top_label(Context* ctx) { @@ -325,7 +328,7 @@ static WabtResult fixup_top_label(Context* ctx) { uint32_t top = ctx->label_stack.size - 1; if (top >= ctx->depth_fixups.size) { /* nothing to fixup */ - return WABT_OK; + return WabtResult::Ok; } Uint32Vector* fixups = &ctx->depth_fixups.data[top]; @@ -335,7 +338,7 @@ static WabtResult fixup_top_label(Context* ctx) { /* reduce the size to 0 in case this gets reused. Keep the allocations for * later use */ fixups->size = 0; - return WABT_OK; + return WabtResult::Ok; } static WabtResult emit_func_offset(Context* ctx, @@ -347,7 +350,7 @@ static WabtResult emit_func_offset(Context* ctx, CHECK_RESULT(append_fixup(ctx, &ctx->func_fixups, defined_index)); } CHECK_RESULT(emit_i32(ctx, func->defined.offset)); - return WABT_OK; + return WabtResult::Ok; } static void on_error(WabtBinaryReaderContext* ctx, const char* message) { @@ -362,7 +365,7 @@ static WabtResult on_signature_count(uint32_t count, void* user_data) { ctx->sig_index_mapping.data[i] = ctx->env->sigs.size + i; wabt_resize_interpreter_func_signature_vector(&ctx->env->sigs, ctx->env->sigs.size + count); - return WABT_OK; + return WabtResult::Ok; } static WabtResult on_signature(uint32_t index, @@ -381,13 +384,13 @@ static WabtResult on_signature(uint32_t index, wabt_reserve_types(&sig->result_types, result_count); sig->result_types.size = result_count; memcpy(sig->result_types.data, result_types, result_count * sizeof(WabtType)); - return WABT_OK; + return WabtResult::Ok; } static WabtResult on_import_count(uint32_t count, void* user_data) { Context* ctx = static_cast<Context*>(user_data); wabt_new_interpreter_import_array(&ctx->module->defined.imports, count); - return WABT_OK; + return WabtResult::Ok; } static WabtResult on_import(uint32_t index, @@ -404,7 +407,7 @@ static WabtResult on_import(uint32_t index, if (module_index < 0) { print_error(ctx, "unknown import module \"" PRIstringslice "\"", WABT_PRINTF_STRING_SLICE_ARG(import->module_name)); - return WABT_ERROR; + return WabtResult::Error; } assert(static_cast<size_t>(module_index) < ctx->env->modules.size); @@ -421,14 +424,14 @@ static WabtResult on_import(uint32_t index, if (!export_) { print_error(ctx, "unknown module field \"" PRIstringslice "\"", WABT_PRINTF_STRING_SLICE_ARG(import->field_name)); - return WABT_ERROR; + return WabtResult::Error; } import->kind = export_->kind; ctx->is_host_import = false; ctx->import_env_index = export_->index; } - return WABT_OK; + return WabtResult::Ok; } static WabtResult check_import_kind(Context* ctx, @@ -441,9 +444,9 @@ static WabtResult check_import_kind(Context* ctx, WABT_PRINTF_STRING_SLICE_ARG(import->field_name), wabt_get_kind_name(expected_kind), wabt_get_kind_name(import->kind)); - return WABT_ERROR; + return WabtResult::Error; } - return WABT_OK; + return WabtResult::Ok; } static WabtResult check_import_limits(Context* ctx, @@ -453,7 +456,7 @@ static WabtResult check_import_limits(Context* ctx, print_error(ctx, "actual size (%" PRIu64 ") smaller than declared (%" PRIu64 ")", actual_limits->initial, declared_limits->initial); - return WABT_ERROR; + return WabtResult::Error; } if (declared_limits->has_max) { @@ -461,16 +464,16 @@ static WabtResult check_import_limits(Context* ctx, print_error(ctx, "max size (unspecified) larger than declared (%" PRIu64 ")", declared_limits->max); - return WABT_ERROR; + return WabtResult::Error; } else if (actual_limits->max > declared_limits->max) { print_error(ctx, "max size (%" PRIu64 ") larger than declared (%" PRIu64 ")", actual_limits->max, declared_limits->max); - return WABT_ERROR; + return WabtResult::Error; } } - return WABT_OK; + return WabtResult::Ok; } static WabtResult append_export(Context* ctx, @@ -481,7 +484,7 @@ static WabtResult append_export(Context* ctx, if (wabt_find_binding_index_by_name(&module->export_bindings, &name) != -1) { print_error(ctx, "duplicate export \"" PRIstringslice "\"", WABT_PRINTF_STRING_SLICE_ARG(name)); - return WABT_ERROR; + return WabtResult::Error; } WabtInterpreterExport* export_ = @@ -493,7 +496,7 @@ static WabtResult append_export(Context* ctx, WabtBinding* binding = wabt_insert_binding(&module->export_bindings, &export_->name); binding->index = module->exports.size - 1; - return WABT_OK; + return WabtResult::Ok; } static void on_host_import_print_error(const char* msg, void* user_data) { @@ -536,23 +539,23 @@ static WabtResult on_import_func(uint32_t import_index, assert(func->host.callback); func_env_index = ctx->env->funcs.size - 1; - append_export(ctx, ctx->host_import_module, WABT_EXTERNAL_KIND_FUNC, + append_export(ctx, ctx->host_import_module, WabtExternalKind::Func, func_env_index, import->field_name); } else { - CHECK_RESULT(check_import_kind(ctx, import, WABT_EXTERNAL_KIND_FUNC)); + CHECK_RESULT(check_import_kind(ctx, import, WabtExternalKind::Func)); assert(ctx->import_env_index < ctx->env->funcs.size); WabtInterpreterFunc* func = &ctx->env->funcs.data[ctx->import_env_index]; if (!wabt_func_signatures_are_equal(ctx->env, import->func.sig_index, func->sig_index)) { print_error(ctx, "import signature mismatch"); - return WABT_ERROR; + return WabtResult::Error; } func_env_index = ctx->import_env_index; } wabt_append_uint32_value(&ctx->func_index_mapping, &func_env_index); ctx->num_func_imports++; - return WABT_OK; + return WabtResult::Ok; } static void init_table_func_indexes(Context* ctx, WabtInterpreterTable* table) { @@ -570,7 +573,7 @@ static WabtResult on_import_table(uint32_t import_index, Context* ctx = static_cast<Context*>(user_data); if (ctx->module->table_index != WABT_INVALID_INDEX) { print_error(ctx, "only one table allowed"); - return WABT_ERROR; + return WabtResult::Error; } assert(import_index < ctx->module->defined.imports.size); @@ -592,10 +595,10 @@ static WabtResult on_import_table(uint32_t import_index, CHECK_RESULT(check_import_limits(ctx, elem_limits, &table->limits)); ctx->module->table_index = ctx->env->tables.size - 1; - append_export(ctx, ctx->host_import_module, WABT_EXTERNAL_KIND_TABLE, + append_export(ctx, ctx->host_import_module, WabtExternalKind::Table, ctx->module->table_index, import->field_name); } else { - CHECK_RESULT(check_import_kind(ctx, import, WABT_EXTERNAL_KIND_TABLE)); + CHECK_RESULT(check_import_kind(ctx, import, WabtExternalKind::Table)); assert(ctx->import_env_index < ctx->env->tables.size); WabtInterpreterTable* table = &ctx->env->tables.data[ctx->import_env_index]; CHECK_RESULT(check_import_limits(ctx, elem_limits, &table->limits)); @@ -603,7 +606,7 @@ static WabtResult on_import_table(uint32_t import_index, import->table.limits = *elem_limits; ctx->module->table_index = ctx->import_env_index; } - return WABT_OK; + return WabtResult::Ok; } static WabtResult on_import_memory(uint32_t import_index, @@ -613,7 +616,7 @@ static WabtResult on_import_memory(uint32_t import_index, Context* ctx = static_cast<Context*>(user_data); if (ctx->module->memory_index != WABT_INVALID_INDEX) { print_error(ctx, "only one memory allowed"); - return WABT_ERROR; + return WabtResult::Error; } assert(import_index < ctx->module->defined.imports.size); @@ -634,10 +637,10 @@ static WabtResult on_import_memory(uint32_t import_index, CHECK_RESULT(check_import_limits(ctx, page_limits, &memory->page_limits)); ctx->module->memory_index = ctx->env->memories.size - 1; - append_export(ctx, ctx->host_import_module, WABT_EXTERNAL_KIND_MEMORY, + append_export(ctx, ctx->host_import_module, WabtExternalKind::Memory, ctx->module->memory_index, import->field_name); } else { - CHECK_RESULT(check_import_kind(ctx, import, WABT_EXTERNAL_KIND_MEMORY)); + CHECK_RESULT(check_import_kind(ctx, import, WabtExternalKind::Memory)); assert(ctx->import_env_index < ctx->env->memories.size); WabtInterpreterMemory* memory = &ctx->env->memories.data[ctx->import_env_index]; @@ -646,7 +649,7 @@ static WabtResult on_import_memory(uint32_t import_index, import->memory.limits = *page_limits; ctx->module->memory_index = ctx->import_env_index; } - return WABT_OK; + return WabtResult::Ok; } static WabtResult on_import_global(uint32_t import_index, @@ -673,10 +676,10 @@ static WabtResult on_import_global(uint32_t import_index, host_delegate->user_data)); global_env_index = ctx->env->globals.size - 1; - append_export(ctx, ctx->host_import_module, WABT_EXTERNAL_KIND_GLOBAL, + append_export(ctx, ctx->host_import_module, WabtExternalKind::Global, global_env_index, import->field_name); } else { - CHECK_RESULT(check_import_kind(ctx, import, WABT_EXTERNAL_KIND_GLOBAL)); + CHECK_RESULT(check_import_kind(ctx, import, WabtExternalKind::Global)); // TODO: check type and mutability import->global.type = type; import->global.mutable_ = mutable_; @@ -684,7 +687,7 @@ static WabtResult on_import_global(uint32_t import_index, } wabt_append_uint32_value(&ctx->global_index_mapping, &global_env_index); ctx->num_global_imports++; - return WABT_OK; + return WabtResult::Ok; } static WabtResult on_function_signatures_count(uint32_t count, @@ -698,7 +701,7 @@ static WabtResult on_function_signatures_count(uint32_t count, wabt_resize_interpreter_func_vector(&ctx->env->funcs, ctx->env->funcs.size + count); wabt_resize_uint32_vector_vector(&ctx->func_fixups, count); - return WABT_OK; + return WabtResult::Ok; } static WabtResult on_function_signature(uint32_t index, @@ -708,7 +711,7 @@ static WabtResult on_function_signature(uint32_t index, WabtInterpreterFunc* func = get_func_by_module_index(ctx, index); func->defined.offset = WABT_INVALID_OFFSET; func->sig_index = translate_sig_index_to_env(ctx, sig_index); - return WABT_OK; + return WabtResult::Ok; } static WabtResult on_table(uint32_t index, @@ -718,14 +721,14 @@ static WabtResult on_table(uint32_t index, Context* ctx = static_cast<Context*>(user_data); if (ctx->module->table_index != WABT_INVALID_INDEX) { print_error(ctx, "only one table allowed"); - return WABT_ERROR; + return WabtResult::Error; } WabtInterpreterTable* table = wabt_append_interpreter_table(&ctx->env->tables); table->limits = *elem_limits; init_table_func_indexes(ctx, table); ctx->module->table_index = ctx->env->tables.size - 1; - return WABT_OK; + return WabtResult::Ok; } static WabtResult on_memory(uint32_t index, @@ -734,7 +737,7 @@ static WabtResult on_memory(uint32_t index, Context* ctx = static_cast<Context*>(user_data); if (ctx->module->memory_index != WABT_INVALID_INDEX) { print_error(ctx, "only one memory allowed"); - return WABT_ERROR; + return WabtResult::Error; } WabtInterpreterMemory* memory = wabt_append_interpreter_memory(&ctx->env->memories); @@ -742,7 +745,7 @@ static WabtResult on_memory(uint32_t index, memory->byte_size = page_limits->initial * WABT_PAGE_SIZE; memory->data = wabt_alloc_zero(memory->byte_size); ctx->module->memory_index = ctx->env->memories.size - 1; - return WABT_OK; + return WabtResult::Ok; } static WabtResult on_global_count(uint32_t count, void* user_data) { @@ -754,7 +757,7 @@ static WabtResult on_global_count(uint32_t count, void* user_data) { ctx->global_index_mapping.data[old_size + i] = ctx->env->globals.size + i; wabt_resize_interpreter_global_vector(&ctx->env->globals, ctx->env->globals.size + count); - return WABT_OK; + return WabtResult::Ok; } static WabtResult begin_global(uint32_t index, @@ -765,7 +768,7 @@ static WabtResult begin_global(uint32_t index, WabtInterpreterGlobal* global = get_global_by_module_index(ctx, index); global->typed_value.type = type; global->mutable_ = mutable_; - return WABT_OK; + return WabtResult::Ok; } static WabtResult end_global_init_expr(uint32_t index, void* user_data) { @@ -775,28 +778,28 @@ static WabtResult end_global_init_expr(uint32_t index, void* user_data) { print_error(ctx, "type mismatch in global, expected %s but got %s.", wabt_get_type_name(global->typed_value.type), wabt_get_type_name(ctx->init_expr_value.type)); - return WABT_ERROR; + return WabtResult::Error; } global->typed_value = ctx->init_expr_value; - return WABT_OK; + return WabtResult::Ok; } static WabtResult on_init_expr_f32_const_expr(uint32_t index, uint32_t value_bits, void* user_data) { Context* ctx = static_cast<Context*>(user_data); - ctx->init_expr_value.type = WABT_TYPE_F32; + ctx->init_expr_value.type = WabtType::F32; ctx->init_expr_value.value.f32_bits = value_bits; - return WABT_OK; + return WabtResult::Ok; } static WabtResult on_init_expr_f64_const_expr(uint32_t index, uint64_t value_bits, void* user_data) { Context* ctx = static_cast<Context*>(user_data); - ctx->init_expr_value.type = WABT_TYPE_F64; + ctx->init_expr_value.type = WabtType::F64; ctx->init_expr_value.value.f64_bits = value_bits; - return WABT_OK; + return WabtResult::Ok; } static WabtResult on_init_expr_get_global_expr(uint32_t index, @@ -806,35 +809,35 @@ static WabtResult on_init_expr_get_global_expr(uint32_t index, if (global_index >= ctx->num_global_imports) { print_error(ctx, "initializer expression can only reference an imported global"); - return WABT_ERROR; + return WabtResult::Error; } WabtInterpreterGlobal* ref_global = get_global_by_module_index(ctx, global_index); if (ref_global->mutable_) { print_error(ctx, "initializer expression cannot reference a mutable global"); - return WABT_ERROR; + return WabtResult::Error; } ctx->init_expr_value = ref_global->typed_value; - return WABT_OK; + return WabtResult::Ok; } static WabtResult on_init_expr_i32_const_expr(uint32_t index, uint32_t value, void* user_data) { Context* ctx = static_cast<Context*>(user_data); - ctx->init_expr_value.type = WABT_TYPE_I32; + ctx->init_expr_value.type = WabtType::I32; ctx->init_expr_value.value.i32 = value; - return WABT_OK; + return WabtResult::Ok; } static WabtResult on_init_expr_i64_const_expr(uint32_t index, uint64_t value, void* user_data) { Context* ctx = static_cast<Context*>(user_data); - ctx->init_expr_value.type = WABT_TYPE_I64; + ctx->init_expr_value.type = WabtType::I64; ctx->init_expr_value.value.i64 = value; - return WABT_OK; + return WabtResult::Ok; } static WabtResult on_export(uint32_t index, @@ -844,31 +847,27 @@ static WabtResult on_export(uint32_t index, void* user_data) { Context* ctx = static_cast<Context*>(user_data); switch (kind) { - case WABT_EXTERNAL_KIND_FUNC: + case WabtExternalKind::Func: item_index = translate_func_index_to_env(ctx, item_index); break; - case WABT_EXTERNAL_KIND_TABLE: + case WabtExternalKind::Table: item_index = ctx->module->table_index; break; - case WABT_EXTERNAL_KIND_MEMORY: + case WabtExternalKind::Memory: item_index = ctx->module->memory_index; break; - case WABT_EXTERNAL_KIND_GLOBAL: { + case WabtExternalKind::Global: { item_index = translate_global_index_to_env(ctx, item_index); WabtInterpreterGlobal* global = &ctx->env->globals.data[item_index]; if (global->mutable_) { print_error(ctx, "mutable globals cannot be exported"); - return WABT_ERROR; + return WabtResult::Error; } break; } - - case WABT_NUM_EXTERNAL_KINDS: - assert(0); - break; } return append_export(ctx, ctx->module, kind, item_index, name); } @@ -882,25 +881,25 @@ static WabtResult on_start_function(uint32_t func_index, void* user_data) { get_signature_by_env_index(ctx, start_func->sig_index); if (sig->param_types.size != 0) { print_error(ctx, "start function must be nullary"); - return WABT_ERROR; + return WabtResult::Error; } if (sig->result_types.size != 0) { print_error(ctx, "start function must not return anything"); - return WABT_ERROR; + return WabtResult::Error; } ctx->module->defined.start_func_index = start_func_index; - return WABT_OK; + return WabtResult::Ok; } static WabtResult end_elem_segment_init_expr(uint32_t index, void* user_data) { Context* ctx = static_cast<Context*>(user_data); - if (ctx->init_expr_value.type != WABT_TYPE_I32) { + if (ctx->init_expr_value.type != WabtType::I32) { print_error(ctx, "type mismatch in elem segment, expected i32 but got %s", wabt_get_type_name(ctx->init_expr_value.type)); - return WABT_ERROR; + return WabtResult::Error; } ctx->table_offset = ctx->init_expr_value.value.i32; - return WABT_OK; + return WabtResult::Ok; } static WabtResult on_elem_segment_function_index_check(uint32_t index, @@ -914,19 +913,19 @@ static WabtResult on_elem_segment_function_index_check(uint32_t index, print_error(ctx, "elem segment offset is out of bounds: %u >= max value %" PRIzd, ctx->table_offset, table->func_indexes.size); - return WABT_ERROR; + return WabtResult::Error; } uint32_t max_func_index = ctx->func_index_mapping.size; if (func_index >= max_func_index) { print_error(ctx, "invalid func_index: %d (max %d)", func_index, max_func_index); - return WABT_ERROR; + return WabtResult::Error; } table->func_indexes.data[ctx->table_offset++] = translate_func_index_to_env(ctx, func_index); - return WABT_OK; + return WabtResult::Ok; } static WabtResult on_elem_segment_function_index(uint32_t index, @@ -938,7 +937,7 @@ static WabtResult on_elem_segment_function_index(uint32_t index, &ctx->env->tables.data[ctx->module->table_index]; table->func_indexes.data[ctx->table_offset++] = translate_func_index_to_env(ctx, func_index); - return WABT_OK; + return WabtResult::Ok; } static WabtResult on_data_segment_data_check(uint32_t index, @@ -949,10 +948,10 @@ static WabtResult on_data_segment_data_check(uint32_t index, assert(ctx->module->memory_index != WABT_INVALID_INDEX); WabtInterpreterMemory* memory = &ctx->env->memories.data[ctx->module->memory_index]; - if (ctx->init_expr_value.type != WABT_TYPE_I32) { + if (ctx->init_expr_value.type != WabtType::I32) { print_error(ctx, "type mismatch in data segment, expected i32 but got %s", wabt_get_type_name(ctx->init_expr_value.type)); - return WABT_ERROR; + return WabtResult::Error; } uint32_t address = ctx->init_expr_value.value.i32; uint64_t end_address = @@ -961,9 +960,9 @@ static WabtResult on_data_segment_data_check(uint32_t index, print_error(ctx, "data segment is out of bounds: [%u, %" PRIu64 ") >= max value %u", address, end_address, memory->byte_size); - return WABT_ERROR; + return WabtResult::Error; } - return WABT_OK; + return WabtResult::Ok; } static WabtResult on_data_segment_data(uint32_t index, @@ -977,7 +976,7 @@ static WabtResult on_data_segment_data(uint32_t index, uint32_t address = ctx->init_expr_value.value.i32; uint8_t* dst_data = static_cast<uint8_t*>(memory->data); memcpy(&dst_data[address], src_data, size); - return WABT_OK; + return WabtResult::Ok; } static void push_label(Context* ctx, uint32_t offset, uint32_t fixup_offset) { @@ -1034,7 +1033,7 @@ static WabtResult begin_function_body(WabtBinaryReaderContext* context, /* push implicit func label (equivalent to return) */ push_label(ctx, WABT_INVALID_OFFSET, WABT_INVALID_OFFSET); - return WABT_OK; + return WabtResult::Ok; } static WabtResult end_function_body(uint32_t index, void* user_data) { @@ -1044,17 +1043,17 @@ static WabtResult end_function_body(uint32_t index, void* user_data) { CHECK_RESULT(get_return_drop_keep_count(ctx, &drop_count, &keep_count)); CHECK_RESULT(wabt_typechecker_end_function(&ctx->typechecker)); CHECK_RESULT(emit_drop_keep(ctx, drop_count, keep_count)); - CHECK_RESULT(emit_opcode(ctx, WABT_OPCODE_RETURN)); + CHECK_RESULT(emit_opcode(ctx, WabtInterpreterOpcode::Return)); pop_label(ctx); ctx->current_func = nullptr; - return WABT_OK; + return WabtResult::Ok; } static WabtResult on_local_decl_count(uint32_t count, void* user_data) { Context* ctx = static_cast<Context*>(user_data); WabtInterpreterFunc* func = ctx->current_func; func->defined.local_decl_count = count; - return WABT_OK; + return WabtResult::Ok; } static WabtResult on_local_decl(uint32_t decl_index, @@ -1072,19 +1071,19 @@ static WabtResult on_local_decl(uint32_t decl_index, if (decl_index == func->defined.local_decl_count - 1) { /* last local declaration, allocate space for all locals. */ - CHECK_RESULT(emit_opcode(ctx, static_cast<WabtOpcode>(WABT_OPCODE_ALLOCA))); + CHECK_RESULT(emit_opcode(ctx, WabtInterpreterOpcode::Alloca)); CHECK_RESULT(emit_i32(ctx, func->defined.local_count)); } - return WABT_OK; + return WabtResult::Ok; } static WabtResult check_has_memory(Context* ctx, WabtOpcode opcode) { if (ctx->module->memory_index == WABT_INVALID_INDEX) { print_error(ctx, "%s requires an imported or defined memory.", wabt_get_opcode_name(opcode)); - return WABT_ERROR; + return WabtResult::Error; } - return WABT_OK; + return WabtResult::Ok; } static WabtResult check_align(Context* ctx, @@ -1093,23 +1092,23 @@ static WabtResult check_align(Context* ctx, if (alignment_log2 >= 32 || (1U << alignment_log2) > natural_alignment) { print_error(ctx, "alignment must not be larger than natural alignment (%u)", natural_alignment); - return WABT_ERROR; + return WabtResult::Error; } - return WABT_OK; + return WabtResult::Ok; } static WabtResult on_unary_expr(WabtOpcode opcode, void* user_data) { Context* ctx = static_cast<Context*>(user_data); CHECK_RESULT(wabt_typechecker_on_unary(&ctx->typechecker, opcode)); CHECK_RESULT(emit_opcode(ctx, opcode)); - return WABT_OK; + return WabtResult::Ok; } static WabtResult on_binary_expr(WabtOpcode opcode, void* user_data) { Context* ctx = static_cast<Context*>(user_data); CHECK_RESULT(wabt_typechecker_on_binary(&ctx->typechecker, opcode)); CHECK_RESULT(emit_opcode(ctx, opcode)); - return WABT_OK; + return WabtResult::Ok; } static WabtResult on_block_expr(uint32_t num_types, @@ -1121,7 +1120,7 @@ static WabtResult on_block_expr(uint32_t num_types, sig.data = sig_types; CHECK_RESULT(wabt_typechecker_on_block(&ctx->typechecker, &sig)); push_label(ctx, WABT_INVALID_OFFSET, WABT_INVALID_OFFSET); - return WABT_OK; + return WabtResult::Ok; } static WabtResult on_loop_expr(uint32_t num_types, @@ -1133,7 +1132,7 @@ static WabtResult on_loop_expr(uint32_t num_types, sig.data = sig_types; CHECK_RESULT(wabt_typechecker_on_loop(&ctx->typechecker, &sig)); push_label(ctx, get_istream_offset(ctx), WABT_INVALID_OFFSET); - return WABT_OK; + return WabtResult::Ok; } static WabtResult on_if_expr(uint32_t num_types, @@ -1144,12 +1143,11 @@ static WabtResult on_if_expr(uint32_t num_types, sig.size = num_types; sig.data = sig_types; CHECK_RESULT(wabt_typechecker_on_if(&ctx->typechecker, &sig)); - CHECK_RESULT( - emit_opcode(ctx, static_cast<WabtOpcode>(WABT_OPCODE_BR_UNLESS))); + CHECK_RESULT(emit_opcode(ctx, WabtInterpreterOpcode::BrUnless)); uint32_t fixup_offset = get_istream_offset(ctx); CHECK_RESULT(emit_i32(ctx, WABT_INVALID_OFFSET)); push_label(ctx, WABT_INVALID_OFFSET, fixup_offset); - return WABT_OK; + return WabtResult::Ok; } static WabtResult on_else_expr(void* user_data) { @@ -1157,11 +1155,11 @@ static WabtResult on_else_expr(void* user_data) { CHECK_RESULT(wabt_typechecker_on_else(&ctx->typechecker)); Label* label = top_label(ctx); uint32_t fixup_cond_offset = label->fixup_offset; - CHECK_RESULT(emit_opcode(ctx, WABT_OPCODE_BR)); + CHECK_RESULT(emit_opcode(ctx, WabtInterpreterOpcode::Br)); label->fixup_offset = get_istream_offset(ctx); CHECK_RESULT(emit_i32(ctx, WABT_INVALID_OFFSET)); CHECK_RESULT(emit_i32_at(ctx, fixup_cond_offset, get_istream_offset(ctx))); - return WABT_OK; + return WabtResult::Ok; } static WabtResult on_end_expr(void* user_data) { @@ -1170,13 +1168,13 @@ static WabtResult on_end_expr(void* user_data) { CHECK_RESULT(wabt_typechecker_get_label(&ctx->typechecker, 0, &label)); WabtLabelType label_type = label->label_type; CHECK_RESULT(wabt_typechecker_on_end(&ctx->typechecker)); - if (label_type == WABT_LABEL_TYPE_IF || label_type == WABT_LABEL_TYPE_ELSE) { + if (label_type == WabtLabelType::If || label_type == WabtLabelType::Else) { CHECK_RESULT(emit_i32_at(ctx, top_label(ctx)->fixup_offset, get_istream_offset(ctx))); } fixup_top_label(ctx); pop_label(ctx); - return WABT_OK; + return WabtResult::Ok; } static WabtResult on_br_expr(uint32_t depth, void* user_data) { @@ -1185,7 +1183,7 @@ static WabtResult on_br_expr(uint32_t depth, void* user_data) { CHECK_RESULT(get_br_drop_keep_count(ctx, depth, &drop_count, &keep_count)); CHECK_RESULT(wabt_typechecker_on_br(&ctx->typechecker, depth)); CHECK_RESULT(emit_br(ctx, depth, drop_count, keep_count)); - return WABT_OK; + return WabtResult::Ok; } static WabtResult on_br_if_expr(uint32_t depth, void* user_data) { @@ -1194,13 +1192,12 @@ static WabtResult on_br_if_expr(uint32_t depth, void* user_data) { CHECK_RESULT(wabt_typechecker_on_br_if(&ctx->typechecker, depth)); CHECK_RESULT(get_br_drop_keep_count(ctx, depth, &drop_count, &keep_count)); /* flip the br_if so if <cond> is true it can drop values from the stack */ - CHECK_RESULT( - emit_opcode(ctx, static_cast<WabtOpcode>(WABT_OPCODE_BR_UNLESS))); + CHECK_RESULT(emit_opcode(ctx, WabtInterpreterOpcode::BrUnless)); uint32_t fixup_br_offset = get_istream_offset(ctx); CHECK_RESULT(emit_i32(ctx, WABT_INVALID_OFFSET)); CHECK_RESULT(emit_br(ctx, depth, drop_count, keep_count)); CHECK_RESULT(emit_i32_at(ctx, fixup_br_offset, get_istream_offset(ctx))); - return WABT_OK; + return WabtResult::Ok; } static WabtResult on_br_table_expr(WabtBinaryReaderContext* context, @@ -1209,13 +1206,13 @@ static WabtResult on_br_table_expr(WabtBinaryReaderContext* context, uint32_t default_target_depth) { Context* ctx = static_cast<Context*>(context->user_data); CHECK_RESULT(wabt_typechecker_begin_br_table(&ctx->typechecker)); - CHECK_RESULT(emit_opcode(ctx, WABT_OPCODE_BR_TABLE)); + CHECK_RESULT(emit_opcode(ctx, WabtInterpreterOpcode::BrTable)); CHECK_RESULT(emit_i32(ctx, num_targets)); uint32_t fixup_table_offset = get_istream_offset(ctx); CHECK_RESULT(emit_i32(ctx, WABT_INVALID_OFFSET)); /* not necessary for the interpreter, but it makes it easier to disassemble. * This opcode specifies how many bytes of data follow. */ - CHECK_RESULT(emit_opcode(ctx, static_cast<WabtOpcode>(WABT_OPCODE_DATA))); + CHECK_RESULT(emit_opcode(ctx, WabtInterpreterOpcode::Data)); CHECK_RESULT(emit_i32(ctx, (num_targets + 1) * WABT_TABLE_ENTRY_SIZE)); CHECK_RESULT(emit_i32_at(ctx, fixup_table_offset, get_istream_offset(ctx))); @@ -1227,7 +1224,7 @@ static WabtResult on_br_table_expr(WabtBinaryReaderContext* context, } CHECK_RESULT(wabt_typechecker_end_br_table(&ctx->typechecker)); - return WABT_OK; + return WabtResult::Ok; } static WabtResult on_call_expr(uint32_t func_index, void* user_data) { @@ -1239,71 +1236,70 @@ static WabtResult on_call_expr(uint32_t func_index, void* user_data) { &sig->result_types)); if (func->is_host) { - CHECK_RESULT( - emit_opcode(ctx, static_cast<WabtOpcode>(WABT_OPCODE_CALL_HOST))); + CHECK_RESULT(emit_opcode(ctx, WabtInterpreterOpcode::CallHost)); CHECK_RESULT(emit_i32(ctx, translate_func_index_to_env(ctx, func_index))); } else { - CHECK_RESULT(emit_opcode(ctx, WABT_OPCODE_CALL)); + CHECK_RESULT(emit_opcode(ctx, WabtInterpreterOpcode::Call)); CHECK_RESULT(emit_func_offset(ctx, func, func_index)); } - return WABT_OK; + return WabtResult::Ok; } static WabtResult on_call_indirect_expr(uint32_t sig_index, void* user_data) { Context* ctx = static_cast<Context*>(user_data); if (ctx->module->table_index == WABT_INVALID_INDEX) { print_error(ctx, "found call_indirect operator, but no table"); - return WABT_ERROR; + return WabtResult::Error; } WabtInterpreterFuncSignature* sig = get_signature_by_module_index(ctx, sig_index); CHECK_RESULT(wabt_typechecker_on_call_indirect( &ctx->typechecker, &sig->param_types, &sig->result_types)); - CHECK_RESULT(emit_opcode(ctx, WABT_OPCODE_CALL_INDIRECT)); + CHECK_RESULT(emit_opcode(ctx, WabtInterpreterOpcode::CallIndirect)); CHECK_RESULT(emit_i32(ctx, ctx->module->table_index)); CHECK_RESULT(emit_i32(ctx, translate_sig_index_to_env(ctx, sig_index))); - return WABT_OK; + return WabtResult::Ok; } static WabtResult on_drop_expr(void* user_data) { Context* ctx = static_cast<Context*>(user_data); CHECK_RESULT(wabt_typechecker_on_drop(&ctx->typechecker)); - CHECK_RESULT(emit_opcode(ctx, WABT_OPCODE_DROP)); - return WABT_OK; + CHECK_RESULT(emit_opcode(ctx, WabtInterpreterOpcode::Drop)); + return WabtResult::Ok; } static WabtResult on_i32_const_expr(uint32_t value, void* user_data) { Context* ctx = static_cast<Context*>(user_data); - CHECK_RESULT(wabt_typechecker_on_const(&ctx->typechecker, WABT_TYPE_I32)); - CHECK_RESULT(emit_opcode(ctx, WABT_OPCODE_I32_CONST)); + CHECK_RESULT(wabt_typechecker_on_const(&ctx->typechecker, WabtType::I32)); + CHECK_RESULT(emit_opcode(ctx, WabtInterpreterOpcode::I32Const)); CHECK_RESULT(emit_i32(ctx, value)); - return WABT_OK; + return WabtResult::Ok; } static WabtResult on_i64_const_expr(uint64_t value, void* user_data) { Context* ctx = static_cast<Context*>(user_data); - CHECK_RESULT(wabt_typechecker_on_const(&ctx->typechecker, WABT_TYPE_I64)); - CHECK_RESULT(emit_opcode(ctx, WABT_OPCODE_I64_CONST)); + CHECK_RESULT(wabt_typechecker_on_const(&ctx->typechecker, WabtType::I64)); + CHECK_RESULT(emit_opcode(ctx, WabtInterpreterOpcode::I64Const)); CHECK_RESULT(emit_i64(ctx, value)); - return WABT_OK; + return WabtResult::Ok; } static WabtResult on_f32_const_expr(uint32_t value_bits, void* user_data) { Context* ctx = static_cast<Context*>(user_data); - CHECK_RESULT(wabt_typechecker_on_const(&ctx->typechecker, WABT_TYPE_F32)); - CHECK_RESULT(emit_opcode(ctx, WABT_OPCODE_F32_CONST)); + CHECK_RESULT(wabt_typechecker_on_const(&ctx->typechecker, WabtType::F32)); + CHECK_RESULT(emit_opcode(ctx, WabtInterpreterOpcode::F32Const)); CHECK_RESULT(emit_i32(ctx, value_bits)); - return WABT_OK; + return WabtResult::Ok; } static WabtResult on_f64_const_expr(uint64_t value_bits, void* user_data) { Context* ctx = static_cast<Context*>(user_data); - CHECK_RESULT(wabt_typechecker_on_const(&ctx->typechecker, WABT_TYPE_F64)); - CHECK_RESULT(emit_opcode(ctx, WABT_OPCODE_F64_CONST)); + CHECK_RESULT(wabt_typechecker_on_const(&ctx->typechecker, WabtType::F64)); + CHECK_RESULT(emit_opcode(ctx, WabtInterpreterOpcode::F64Const)); CHECK_RESULT(emit_i64(ctx, value_bits)); - return WABT_OK; + return WabtResult::Ok; } static WabtResult on_get_global_expr(uint32_t global_index, void* user_data) { @@ -1311,9 +1307,9 @@ static WabtResult on_get_global_expr(uint32_t global_index, void* user_data) { CHECK_GLOBAL(ctx, global_index); WabtType type = get_global_type_by_module_index(ctx, global_index); CHECK_RESULT(wabt_typechecker_on_get_global(&ctx->typechecker, type)); - CHECK_RESULT(emit_opcode(ctx, WABT_OPCODE_GET_GLOBAL)); + CHECK_RESULT(emit_opcode(ctx, WabtInterpreterOpcode::GetGlobal)); CHECK_RESULT(emit_i32(ctx, translate_global_index_to_env(ctx, global_index))); - return WABT_OK; + return WabtResult::Ok; } static WabtResult on_set_global_expr(uint32_t global_index, void* user_data) { @@ -1323,13 +1319,13 @@ static WabtResult on_set_global_expr(uint32_t global_index, void* user_data) { if (!global->mutable_) { print_error(ctx, "can't set_global on immutable global at index %u.", global_index); - return WABT_ERROR; + return WabtResult::Error; } CHECK_RESULT(wabt_typechecker_on_set_global(&ctx->typechecker, global->typed_value.type)); - CHECK_RESULT(emit_opcode(ctx, WABT_OPCODE_SET_GLOBAL)); + CHECK_RESULT(emit_opcode(ctx, WabtInterpreterOpcode::SetGlobal)); CHECK_RESULT(emit_i32(ctx, translate_global_index_to_env(ctx, global_index))); - return WABT_OK; + return WabtResult::Ok; } static uint32_t translate_local_index(Context* ctx, uint32_t local_index) { @@ -1346,9 +1342,9 @@ static WabtResult on_get_local_expr(uint32_t local_index, void* user_data) { * relative to the old stack size. */ uint32_t translated_local_index = translate_local_index(ctx, local_index); CHECK_RESULT(wabt_typechecker_on_get_local(&ctx->typechecker, type)); - CHECK_RESULT(emit_opcode(ctx, WABT_OPCODE_GET_LOCAL)); + CHECK_RESULT(emit_opcode(ctx, WabtInterpreterOpcode::GetLocal)); CHECK_RESULT(emit_i32(ctx, translated_local_index)); - return WABT_OK; + return WabtResult::Ok; } static WabtResult on_set_local_expr(uint32_t local_index, void* user_data) { @@ -1356,9 +1352,9 @@ static WabtResult on_set_local_expr(uint32_t local_index, void* user_data) { CHECK_LOCAL(ctx, local_index); WabtType type = get_local_type_by_index(ctx->current_func, local_index); CHECK_RESULT(wabt_typechecker_on_set_local(&ctx->typechecker, type)); - CHECK_RESULT(emit_opcode(ctx, WABT_OPCODE_SET_LOCAL)); + CHECK_RESULT(emit_opcode(ctx, WabtInterpreterOpcode::SetLocal)); CHECK_RESULT(emit_i32(ctx, translate_local_index(ctx, local_index))); - return WABT_OK; + return WabtResult::Ok; } static WabtResult on_tee_local_expr(uint32_t local_index, void* user_data) { @@ -1366,18 +1362,18 @@ static WabtResult on_tee_local_expr(uint32_t local_index, void* user_data) { CHECK_LOCAL(ctx, local_index); WabtType type = get_local_type_by_index(ctx->current_func, local_index); CHECK_RESULT(wabt_typechecker_on_tee_local(&ctx->typechecker, type)); - CHECK_RESULT(emit_opcode(ctx, WABT_OPCODE_TEE_LOCAL)); + CHECK_RESULT(emit_opcode(ctx, WabtInterpreterOpcode::TeeLocal)); CHECK_RESULT(emit_i32(ctx, translate_local_index(ctx, local_index))); - return WABT_OK; + return WabtResult::Ok; } static WabtResult on_grow_memory_expr(void* user_data) { Context* ctx = static_cast<Context*>(user_data); - CHECK_RESULT(check_has_memory(ctx, WABT_OPCODE_GROW_MEMORY)); + CHECK_RESULT(check_has_memory(ctx, WabtOpcode::GrowMemory)); CHECK_RESULT(wabt_typechecker_on_grow_memory(&ctx->typechecker)); - CHECK_RESULT(emit_opcode(ctx, WABT_OPCODE_GROW_MEMORY)); + CHECK_RESULT(emit_opcode(ctx, WabtInterpreterOpcode::GrowMemory)); CHECK_RESULT(emit_i32(ctx, ctx->module->memory_index)); - return WABT_OK; + return WabtResult::Ok; } static WabtResult on_load_expr(WabtOpcode opcode, @@ -1392,7 +1388,7 @@ static WabtResult on_load_expr(WabtOpcode opcode, CHECK_RESULT(emit_opcode(ctx, opcode)); CHECK_RESULT(emit_i32(ctx, ctx->module->memory_index)); CHECK_RESULT(emit_i32(ctx, offset)); - return WABT_OK; + return WabtResult::Ok; } static WabtResult on_store_expr(WabtOpcode opcode, @@ -1407,20 +1403,20 @@ static WabtResult on_store_expr(WabtOpcode opcode, CHECK_RESULT(emit_opcode(ctx, opcode)); CHECK_RESULT(emit_i32(ctx, ctx->module->memory_index)); CHECK_RESULT(emit_i32(ctx, offset)); - return WABT_OK; + return WabtResult::Ok; } static WabtResult on_current_memory_expr(void* user_data) { Context* ctx = static_cast<Context*>(user_data); - CHECK_RESULT(check_has_memory(ctx, WABT_OPCODE_CURRENT_MEMORY)); + CHECK_RESULT(check_has_memory(ctx, WabtOpcode::CurrentMemory)); CHECK_RESULT(wabt_typechecker_on_current_memory(&ctx->typechecker)); - CHECK_RESULT(emit_opcode(ctx, WABT_OPCODE_CURRENT_MEMORY)); + CHECK_RESULT(emit_opcode(ctx, WabtInterpreterOpcode::CurrentMemory)); CHECK_RESULT(emit_i32(ctx, ctx->module->memory_index)); - return WABT_OK; + return WabtResult::Ok; } static WabtResult on_nop_expr(void* user_data) { - return WABT_OK; + return WabtResult::Ok; } static WabtResult on_return_expr(void* user_data) { @@ -1429,22 +1425,22 @@ static WabtResult on_return_expr(void* user_data) { CHECK_RESULT(get_return_drop_keep_count(ctx, &drop_count, &keep_count)); CHECK_RESULT(wabt_typechecker_on_return(&ctx->typechecker)); CHECK_RESULT(emit_drop_keep(ctx, drop_count, keep_count)); - CHECK_RESULT(emit_opcode(ctx, WABT_OPCODE_RETURN)); - return WABT_OK; + CHECK_RESULT(emit_opcode(ctx, WabtInterpreterOpcode::Return)); + return WabtResult::Ok; } static WabtResult on_select_expr(void* user_data) { Context* ctx = static_cast<Context*>(user_data); CHECK_RESULT(wabt_typechecker_on_select(&ctx->typechecker)); - CHECK_RESULT(emit_opcode(ctx, WABT_OPCODE_SELECT)); - return WABT_OK; + CHECK_RESULT(emit_opcode(ctx, WabtInterpreterOpcode::Select)); + return WabtResult::Ok; } static WabtResult on_unreachable_expr(void* user_data) { Context* ctx = static_cast<Context*>(user_data); CHECK_RESULT(wabt_typechecker_on_unreachable(&ctx->typechecker)); - CHECK_RESULT(emit_opcode(ctx, WABT_OPCODE_UNREACHABLE)); - return WABT_OK; + CHECK_RESULT(emit_opcode(ctx, WabtInterpreterOpcode::Unreachable)); + return WabtResult::Ok; } static void destroy_context(Context* ctx) { |