Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix aot load import global bug && enhance dead code processing (#203) #85

Merged
merged 1 commit into from
Mar 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/iwasm/aot/aot_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,7 @@ load_import_globals(const uint8 **p_buf, const uint8 *buf_end,

/* Create each import global */
for (i = 0; i < module->import_global_count; i++) {
buf = (uint8*)align_ptr(buf, 2);
read_uint8(buf, buf_end, import_globals[i].type);
read_uint8(buf, buf_end, import_globals[i].is_mutable);
read_string(buf, buf_end, import_globals[i].module_name);
Expand Down
82 changes: 32 additions & 50 deletions core/iwasm/interpreter/wasm_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -2830,7 +2830,7 @@ add_label_patch_to_list(BranchBlock *frame_csp,

static void
apply_label_patch(WASMLoaderContext *ctx, uint8 depth,
uint8 patch_type, uint8 *frame_ip)
uint8 patch_type)
{
BranchBlock *frame_csp = ctx->frame_csp - depth;
BranchBlockPatch *node = frame_csp->patch_list;
Expand Down Expand Up @@ -2882,7 +2882,12 @@ wasm_loader_emit_br_info(WASMLoaderContext *ctx, BranchBlock *frame_csp,
char *error_buf, uint32 error_buf_size)
{
emit_operand(ctx, frame_csp->dynamic_offset);
if (frame_csp->return_type == VALUE_TYPE_I32
if (frame_csp->block_type == BLOCK_TYPE_LOOP ||
frame_csp->return_type == VALUE_TYPE_VOID) {
emit_byte(ctx, 0);
emit_operand(ctx, 0);
}
else if (frame_csp->return_type == VALUE_TYPE_I32
|| frame_csp->return_type == VALUE_TYPE_F32) {
emit_byte(ctx, 1);
emit_operand(ctx, *(int16*)(ctx->frame_offset - 1));
Expand All @@ -2892,10 +2897,7 @@ wasm_loader_emit_br_info(WASMLoaderContext *ctx, BranchBlock *frame_csp,
emit_byte(ctx, 2);
emit_operand(ctx, *(int16*)(ctx->frame_offset - 2));
}
else {
emit_byte(ctx, 0);
emit_operand(ctx, 0);
}

if (frame_csp->block_type == BLOCK_TYPE_LOOP) {
wasm_loader_emit_ptr(ctx, frame_csp->code_compiled);
}
Expand Down Expand Up @@ -3514,10 +3516,17 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
error_buf, error_buf_size))
goto fail;

if ((loader_ctx->frame_csp - 1)->else_addr)
p = (loader_ctx->frame_csp - 1)->else_addr;
else
if ((loader_ctx->frame_csp - 1)->else_addr) {
#if WASM_ENABLE_FAST_INTERP != 0
loader_ctx->frame_offset = loader_ctx->frame_offset_bottom +
(loader_ctx->frame_csp - 1)->stack_cell_num;
apply_label_patch(loader_ctx, 1, PATCH_ELSE);
#endif
p = (loader_ctx->frame_csp - 1)->else_addr + 1;
}
else {
p = (loader_ctx->frame_csp - 1)->end_addr;
}

is_i32_const = false;
continue;
Expand All @@ -3539,7 +3548,7 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
loader_ctx->frame_ref = loader_ctx->frame_ref_bottom +
loader_ctx->stack_cell_num;
#if WASM_ENABLE_FAST_INTERP != 0
// if the result of if branch is in local or const area, add a copy op
/* if the result of if branch is in local or const area, add a copy op */
if ((loader_ctx->frame_csp - 1)->return_type != VALUE_TYPE_VOID) {
uint8 return_cells;
if ((loader_ctx->frame_csp - 1)->return_type == VALUE_TYPE_I32
Expand All @@ -3557,14 +3566,14 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
emit_operand(loader_ctx, *(loader_ctx->frame_offset - return_cells));
emit_operand(loader_ctx, (loader_ctx->frame_csp - 1)->dynamic_offset);
*(loader_ctx->frame_offset - return_cells) =
loader_ctx->frame_csp->dynamic_offset;
(loader_ctx->frame_csp - 1)->dynamic_offset;
emit_label(opcode);
}
}
loader_ctx->frame_offset = loader_ctx->frame_offset_bottom +
loader_ctx->stack_cell_num;
emit_empty_label_addr_and_frame_ip(PATCH_END);
apply_label_patch(loader_ctx, 1, PATCH_ELSE, p);
apply_label_patch(loader_ctx, 1, PATCH_ELSE);
#endif
break;

Expand Down Expand Up @@ -3601,7 +3610,7 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
wasm_loader_emit_backspace(loader_ctx, sizeof(int16));
}

apply_label_patch(loader_ctx, 0, PATCH_END, p);
apply_label_patch(loader_ctx, 0, PATCH_END);
free_label_patch_list(loader_ctx->frame_csp);
if (loader_ctx->frame_csp->block_type == BLOCK_TYPE_FUNCTION) {
emit_label(WASM_OP_RETURN);
Expand Down Expand Up @@ -3670,8 +3679,15 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,

if ((loader_ctx->frame_csp - 1)->block_type == BLOCK_TYPE_IF
&& (loader_ctx->frame_csp - 1)->else_addr != NULL
&& p <= (loader_ctx->frame_csp - 1)->else_addr)
p = (loader_ctx->frame_csp - 1)->else_addr;
&& p <= (loader_ctx->frame_csp - 1)->else_addr) {
#if WASM_ENABLE_FAST_INTERP != 0
loader_ctx->frame_offset = loader_ctx->frame_offset_bottom +
(loader_ctx->frame_csp - 1)->stack_cell_num;
apply_label_patch(loader_ctx, 1, PATCH_ELSE);
#endif
p = (loader_ctx->frame_csp - 1)->else_addr + 1;

}
else {
p = (loader_ctx->frame_csp - 1)->end_addr;
PUSH_TYPE(block_return_type);
Expand Down Expand Up @@ -3736,40 +3752,6 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
POP_TYPE(ret_type);
PUSH_TYPE(ret_type);

cache_index = ((uintptr_t)(loader_ctx->frame_csp - 1)->start_addr)
& (uintptr_t)(BLOCK_ADDR_CACHE_SIZE - 1);
cache_items = block_addr_cache + BLOCK_ADDR_CONFLICT_SIZE * cache_index;
for (item_index = 0; item_index < BLOCK_ADDR_CONFLICT_SIZE;
item_index++) {
if (cache_items[item_index].start_addr ==
(loader_ctx->frame_csp - 1)->start_addr) {
(loader_ctx->frame_csp - 1)->else_addr = cache_items[item_index].else_addr;
(loader_ctx->frame_csp - 1)->end_addr = cache_items[item_index].end_addr;
break;
}
}
if(item_index == BLOCK_ADDR_CONFLICT_SIZE
&& !wasm_loader_find_block_addr(block_addr_cache,
(loader_ctx->frame_csp - 1)->start_addr,
p_end,
(loader_ctx->frame_csp - 1)->block_type,
&(loader_ctx->frame_csp - 1)->else_addr,
&(loader_ctx->frame_csp - 1)->end_addr,
error_buf, error_buf_size))
goto fail;

loader_ctx->stack_cell_num = (loader_ctx->frame_csp - 1)->stack_cell_num;
loader_ctx->frame_ref = loader_ctx->frame_ref_bottom + loader_ctx->stack_cell_num;

if ((loader_ctx->frame_csp - 1)->block_type == BLOCK_TYPE_IF
&& p <= (loader_ctx->frame_csp - 1)->else_addr) {
p = (loader_ctx->frame_csp - 1)->else_addr;
}
else {
p = (loader_ctx->frame_csp - 1)->end_addr;
PUSH_TYPE((loader_ctx->frame_csp - 1)->return_type);
}

#if WASM_ENABLE_FAST_INTERP != 0
// emit the offset after return opcode
POP_OFFSET_TYPE(ret_type);
Expand All @@ -3778,7 +3760,7 @@ wasm_loader_prepare_bytecode(WASMModule *module, WASMFunction *func,
#endif

is_i32_const = false;
continue;
goto handle_next_reachable_block;
}

case WASM_OP_CALL:
Expand Down