Skip to content

Commit

Permalink
Refactor table element retrieval to use temporary variable for improv…
Browse files Browse the repository at this point in the history
…ed clarity and consistency; restore aot_compile_get_tbl_inst function
  • Loading branch information
lum1n0us committed Nov 10, 2024
1 parent ad3541c commit 5e9abda
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 30 deletions.
10 changes: 6 additions & 4 deletions core/iwasm/common/wasm_runtime_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -468,11 +468,12 @@ WASMTableInstance_to_wasm_table_inst_t(const WASMModule *module,
out->cur_size = table_rt->cur_size;

/* table_index < 0, it means it is created by host */
uintptr_t tmp = (uintptr_t)table_rt->elems;
if (table_index > 0 && table_index < (int64)module->import_table_count) {
out->elems = *(void **)(uintptr_t)table_rt->elems;
out->elems = *(void **)tmp;
}
else {
out->elems = (void *)table_rt->elems;
out->elems = (void *)tmp;
}

bh_assert(out->elems);
Expand All @@ -490,11 +491,12 @@ AOTTableInstance_to_wasm_table_inst_t(const AOTModule *module,
out->cur_size = table_rt->cur_size;

/* table_index < 0, it means it is created by host */
uintptr_t tmp = (uintptr_t)table_rt->elems;
if (table_index > 0 && table_index < (int64)module->import_table_count) {
out->elems = *(void **)(uintptr_t)table_rt->elems;
out->elems = *(void **)tmp;
}
else {
out->elems = (void *)table_rt->elems;
out->elems = (void *)tmp;
}

bh_assert(out->elems);
Expand Down
52 changes: 26 additions & 26 deletions core/iwasm/compilation/aot_emit_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,32 +135,6 @@ get_module_inst_extra_offset(AOTCompContext *comp_ctx)
return offset_32;
}

#if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0

LLVMValueRef
aot_compile_get_tbl_inst(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
uint32 tbl_idx)
{
LLVMValueRef offset, tbl_inst;

if (!(offset =
I64_CONST(get_tbl_inst_offset(comp_ctx, func_ctx, tbl_idx)))) {
HANDLE_FAILURE("LLVMConstInt");
goto fail;
}

if (!(tbl_inst = LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE,
func_ctx->aot_inst, &offset, 1,
"tbl_inst"))) {
HANDLE_FAILURE("LLVMBuildInBoundsGEP");
goto fail;
}

return tbl_inst;
fail:
return NULL;
}

LLVMValueRef
aot_compile_get_table_elem_base(AOTCompContext *comp_ctx,
AOTFuncContext *func_ctx, uint32 table_index)
Expand Down Expand Up @@ -221,6 +195,32 @@ aot_compile_get_table_elem_base(AOTCompContext *comp_ctx,
return NULL;
}

#if WASM_ENABLE_REF_TYPES != 0 || WASM_ENABLE_GC != 0

LLVMValueRef
aot_compile_get_tbl_inst(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
uint32 tbl_idx)
{
LLVMValueRef offset, tbl_inst;

if (!(offset =
I64_CONST(get_tbl_inst_offset(comp_ctx, func_ctx, tbl_idx)))) {
HANDLE_FAILURE("LLVMConstInt");
goto fail;
}

if (!(tbl_inst = LLVMBuildInBoundsGEP2(comp_ctx->builder, INT8_TYPE,
func_ctx->aot_inst, &offset, 1,
"tbl_inst"))) {
HANDLE_FAILURE("LLVMBuildInBoundsGEP");
goto fail;
}

return tbl_inst;
fail:
return NULL;
}

bool
aot_compile_op_elem_drop(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
uint32 tbl_seg_idx)
Expand Down

0 comments on commit 5e9abda

Please sign in to comment.