Sfoglia il codice sorgente

GC: Change table_elem_type_t to pointer width to simplify the related handlings (#2178)

Huang Qi 2 anni fa
parent
commit
02f0156475

+ 1 - 0
.gitignore

@@ -8,6 +8,7 @@
 *.obj
 *.obj
 *.a
 *.a
 *.so
 *.so
+.clangd
 .DS_Store
 .DS_Store
 
 
 core/deps/**
 core/deps/**

+ 1 - 1
core/iwasm/aot/aot_loader.c

@@ -1037,7 +1037,7 @@ load_table_init_data_list(const uint8 **p_buf, const uint8 *buf_end,
         read_uint64(buf, buf_end, init_expr_value);
         read_uint64(buf, buf_end, init_expr_value);
         read_uint32(buf, buf_end, func_index_count);
         read_uint32(buf, buf_end, func_index_count);
 
 
-        size1 = sizeof(uint32) * (uint64)func_index_count;
+        size1 = sizeof(uintptr_t) * (uint64)func_index_count;
         size = offsetof(AOTTableInitData, func_indexes) + size1;
         size = offsetof(AOTTableInitData, func_indexes) + size1;
         if (!(data_list[i] = loader_malloc(size, error_buf, error_buf_size))) {
         if (!(data_list[i] = loader_malloc(size, error_buf, error_buf_size))) {
             return false;
             return false;

+ 19 - 14
core/iwasm/aot/aot_runtime.c

@@ -228,12 +228,14 @@ tables_instantiate(AOTModuleInstance *module_inst, AOTModule *module,
         }
         }
 
 
         /* Set all elements to -1 to mark them as uninitialized elements */
         /* Set all elements to -1 to mark them as uninitialized elements */
-        memset(tbl_inst->elems, 0xff, sizeof(uint32) * tbl_inst->max_size);
+        memset(tbl_inst->elems, 0xff,
+               sizeof(table_elem_type_t) * tbl_inst->max_size);
 
 
         module_inst->tables[i] = tbl_inst;
         module_inst->tables[i] = tbl_inst;
         tbl_inst = (AOTTableInstance *)((uint8 *)tbl_inst
         tbl_inst = (AOTTableInstance *)((uint8 *)tbl_inst
                                         + offsetof(AOTTableInstance, elems)
                                         + offsetof(AOTTableInstance, elems)
-                                        + sizeof(uint32) * tbl_inst->max_size);
+                                        + sizeof(table_elem_type_t)
+                                              * tbl_inst->max_size);
     }
     }
 
 
     /* fill table with element segment content */
     /* fill table with element segment content */
@@ -316,9 +318,10 @@ tables_instantiate(AOTModuleInstance *module_inst, AOTModule *module,
          * Check function index in the current module inst for now.
          * Check function index in the current module inst for now.
          * will check the linked table inst owner in future
          * will check the linked table inst owner in future
          */
          */
-        bh_memcpy_s(tbl_inst->elems + base_offset,
-                    (tbl_inst->max_size - base_offset) * sizeof(uint32),
-                    table_seg->func_indexes, length * sizeof(uint32));
+        bh_memcpy_s(
+            tbl_inst->elems + base_offset,
+            (tbl_inst->max_size - base_offset) * sizeof(table_elem_type_t),
+            table_seg->func_indexes, length * sizeof(table_elem_type_t));
     }
     }
 
 
     return true;
     return true;
@@ -1112,7 +1115,7 @@ aot_instantiate(AOTModule *module, bool is_sub_inst, WASMExecEnv *exec_env_main,
      */
      */
     for (i = 0; i != module->import_table_count; ++i) {
     for (i = 0; i != module->import_table_count; ++i) {
         table_size += offsetof(AOTTableInstance, elems);
         table_size += offsetof(AOTTableInstance, elems);
-        table_size += (uint64)sizeof(uint32)
+        table_size += (uint64)sizeof(table_elem_type_t)
                       * (uint64)aot_get_imp_tbl_data_slots(
                       * (uint64)aot_get_imp_tbl_data_slots(
                           module->import_tables + i, false);
                           module->import_tables + i, false);
     }
     }
@@ -1120,7 +1123,7 @@ aot_instantiate(AOTModule *module, bool is_sub_inst, WASMExecEnv *exec_env_main,
     for (i = 0; i != module->table_count; ++i) {
     for (i = 0; i != module->table_count; ++i) {
         table_size += offsetof(AOTTableInstance, elems);
         table_size += offsetof(AOTTableInstance, elems);
         table_size +=
         table_size +=
-            (uint64)sizeof(uint32)
+            (uint64)sizeof(table_elem_type_t)
             * (uint64)aot_get_tbl_data_slots(module->tables + i, false);
             * (uint64)aot_get_tbl_data_slots(module->tables + i, false);
     }
     }
     total_size += table_size;
     total_size += table_size;
@@ -2507,9 +2510,10 @@ aot_table_init(AOTModuleInstance *module_inst, uint32 tbl_idx,
     }
     }
 
 
     bh_memcpy_s((uint8 *)tbl_inst + offsetof(AOTTableInstance, elems)
     bh_memcpy_s((uint8 *)tbl_inst + offsetof(AOTTableInstance, elems)
-                    + dst_offset * sizeof(uint32),
-                (tbl_inst->cur_size - dst_offset) * sizeof(uint32),
-                tbl_seg->func_indexes + src_offset, length * sizeof(uint32));
+                    + dst_offset * sizeof(table_elem_type_t),
+                (tbl_inst->cur_size - dst_offset) * sizeof(table_elem_type_t),
+                tbl_seg->func_indexes + src_offset,
+                length * sizeof(table_elem_type_t));
 }
 }
 
 
 void
 void
@@ -2535,11 +2539,12 @@ aot_table_copy(AOTModuleInstance *module_inst, uint32 src_tbl_idx,
     /* if src_offset < dst_offset, copy from back to front */
     /* if src_offset < dst_offset, copy from back to front */
     /* merge all together */
     /* merge all together */
     bh_memmove_s((uint8 *)dst_tbl_inst + offsetof(AOTTableInstance, elems)
     bh_memmove_s((uint8 *)dst_tbl_inst + offsetof(AOTTableInstance, elems)
-                     + dst_offset * sizeof(uint32),
-                 (dst_tbl_inst->cur_size - dst_offset) * sizeof(uint32),
+                     + dst_offset * sizeof(table_elem_type_t),
+                 (dst_tbl_inst->cur_size - dst_offset)
+                     * sizeof(table_elem_type_t),
                  (uint8 *)src_tbl_inst + offsetof(AOTTableInstance, elems)
                  (uint8 *)src_tbl_inst + offsetof(AOTTableInstance, elems)
-                     + src_offset * sizeof(uint32),
-                 length * sizeof(uint32));
+                     + src_offset * sizeof(table_elem_type_t),
+                 length * sizeof(table_elem_type_t));
 }
 }
 
 
 void
 void

+ 2 - 2
core/iwasm/common/wasm_c_api.c

@@ -3984,7 +3984,7 @@ wasm_table_set(wasm_table_t *table, wasm_table_size_t index,
             return false;
             return false;
         }
         }
 
 
-        p_ref_idx = table_interp->elems + index;
+        p_ref_idx = (uint32 *)(table_interp->elems + index);
         function_count =
         function_count =
             ((WASMModuleInstance *)table->inst_comm_rt)->e->function_count;
             ((WASMModuleInstance *)table->inst_comm_rt)->e->function_count;
     }
     }
@@ -4000,7 +4000,7 @@ wasm_table_set(wasm_table_t *table, wasm_table_size_t index,
             return false;
             return false;
         }
         }
 
 
-        p_ref_idx = table_aot->elems + index;
+        p_ref_idx = (uint32 *)(table_aot->elems + index);
         function_count = module_aot->func_count;
         function_count = module_aot->func_count;
     }
     }
 #endif
 #endif

+ 4 - 2
core/iwasm/common/wasm_runtime_common.c

@@ -4935,7 +4935,8 @@ mark_externref(uint32 externref_idx)
 static void
 static void
 interp_mark_all_externrefs(WASMModuleInstance *module_inst)
 interp_mark_all_externrefs(WASMModuleInstance *module_inst)
 {
 {
-    uint32 i, j, externref_idx, *table_data;
+    uint32 i, j, externref_idx;
+    table_elem_type_t *table_data;
     uint8 *global_data = module_inst->global_data;
     uint8 *global_data = module_inst->global_data;
     WASMGlobalInstance *global;
     WASMGlobalInstance *global;
     WASMTableInstance *table;
     WASMTableInstance *table;
@@ -5455,7 +5456,8 @@ results_to_argv(WASMModuleInstanceCommon *module_inst, uint32 *out_argv,
 #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
 #if WASM_ENABLE_GC == 0 && WASM_ENABLE_REF_TYPES != 0
             case VALUE_TYPE_EXTERNREF:
             case VALUE_TYPE_EXTERNREF:
                 if (!wasm_externref_obj2ref(module_inst,
                 if (!wasm_externref_obj2ref(module_inst,
-                                            (void *)result->of.foreign, argv)) {
+                                            (void *)result->of.foreign,
+                                            (uint32 *)argv)) {
                     return false;
                     return false;
                 }
                 }
                 argv++;
                 argv++;

+ 8 - 7
core/iwasm/compilation/aot.c

@@ -114,9 +114,9 @@ aot_create_table_init_data_list(const WASMModule *module)
 
 
     /* Create each table data segment */
     /* Create each table data segment */
     for (i = 0; i < module->table_seg_count; i++) {
     for (i = 0; i < module->table_seg_count; i++) {
-        size =
-            offsetof(AOTTableInitData, func_indexes)
-            + sizeof(uint32) * (uint64)module->table_segments[i].function_count;
+        size = offsetof(AOTTableInitData, func_indexes)
+               + sizeof(uintptr_t)
+                     * (uint64)module->table_segments[i].function_count;
         if (size >= UINT32_MAX
         if (size >= UINT32_MAX
             || !(data_list[i] = wasm_runtime_malloc((uint32)size))) {
             || !(data_list[i] = wasm_runtime_malloc((uint32)size))) {
             aot_set_last_error("allocate memory failed.");
             aot_set_last_error("allocate memory failed.");
@@ -136,10 +136,11 @@ aot_create_table_init_data_list(const WASMModule *module)
                     sizeof(AOTInitExpr));
                     sizeof(AOTInitExpr));
         data_list[i]->func_index_count =
         data_list[i]->func_index_count =
             module->table_segments[i].function_count;
             module->table_segments[i].function_count;
-        bh_memcpy_s(data_list[i]->func_indexes,
-                    sizeof(uint32) * module->table_segments[i].function_count,
-                    module->table_segments[i].func_indexes,
-                    sizeof(uint32) * module->table_segments[i].function_count);
+        bh_memcpy_s(
+            data_list[i]->func_indexes,
+            sizeof(uintptr_t) * module->table_segments[i].function_count,
+            module->table_segments[i].func_indexes,
+            sizeof(uintptr_t) * module->table_segments[i].function_count);
     }
     }
 
 
     return data_list;
     return data_list;

+ 1 - 1
core/iwasm/compilation/aot.h

@@ -132,7 +132,7 @@ typedef struct AOTTableInitData {
     /* Function index count */
     /* Function index count */
     uint32 func_index_count;
     uint32 func_index_count;
     /* Function index array */
     /* Function index array */
-    uint32 func_indexes[1];
+    uintptr_t func_indexes[1];
 } AOTTableInitData;
 } AOTTableInitData;
 
 
 /**
 /**

+ 2 - 0
core/iwasm/compilation/aot_compiler.h

@@ -229,11 +229,13 @@ check_type_compatible(uint8 src_type, uint8 dst_type)
 #define INT1_TYPE comp_ctx->basic_types.int1_type
 #define INT1_TYPE comp_ctx->basic_types.int1_type
 #define INT8_TYPE comp_ctx->basic_types.int8_type
 #define INT8_TYPE comp_ctx->basic_types.int8_type
 #define INT16_TYPE comp_ctx->basic_types.int16_type
 #define INT16_TYPE comp_ctx->basic_types.int16_type
+#define INTPTR_TYPE comp_ctx->basic_types.intptr_type
 #define MD_TYPE comp_ctx->basic_types.meta_data_type
 #define MD_TYPE comp_ctx->basic_types.meta_data_type
 #define INT8_PTR_TYPE comp_ctx->basic_types.int8_ptr_type
 #define INT8_PTR_TYPE comp_ctx->basic_types.int8_ptr_type
 #define INT16_PTR_TYPE comp_ctx->basic_types.int16_ptr_type
 #define INT16_PTR_TYPE comp_ctx->basic_types.int16_ptr_type
 #define INT32_PTR_TYPE comp_ctx->basic_types.int32_ptr_type
 #define INT32_PTR_TYPE comp_ctx->basic_types.int32_ptr_type
 #define INT64_PTR_TYPE comp_ctx->basic_types.int64_ptr_type
 #define INT64_PTR_TYPE comp_ctx->basic_types.int64_ptr_type
+#define INTPTR_PTR_TYPE comp_ctx->basic_types.intptr_ptr_type
 #define F32_PTR_TYPE comp_ctx->basic_types.float32_ptr_type
 #define F32_PTR_TYPE comp_ctx->basic_types.float32_ptr_type
 #define F64_PTR_TYPE comp_ctx->basic_types.float64_ptr_type
 #define F64_PTR_TYPE comp_ctx->basic_types.float64_ptr_type
 #define FUNC_REF_TYPE comp_ctx->basic_types.funcref_type
 #define FUNC_REF_TYPE comp_ctx->basic_types.funcref_type

+ 22 - 11
core/iwasm/compilation/aot_emit_aot_file.c

@@ -251,7 +251,8 @@ get_mem_info_size(AOTCompData *comp_data)
 }
 }
 
 
 static uint32
 static uint32
-get_table_init_data_size(AOTTableInitData *table_init_data)
+get_table_init_data_size(AOTCompContext *comp_ctx,
+                         AOTTableInitData *table_init_data)
 {
 {
     /*
     /*
      * mode (4 bytes), elem_type (4 bytes), do not need is_dropped field
      * mode (4 bytes), elem_type (4 bytes), do not need is_dropped field
@@ -262,11 +263,13 @@ get_table_init_data_size(AOTTableInitData *table_init_data)
      */
      */
     return (uint32)(sizeof(uint32) * 2 + sizeof(uint32) + sizeof(uint32)
     return (uint32)(sizeof(uint32) * 2 + sizeof(uint32) + sizeof(uint32)
                     + sizeof(uint64) + sizeof(uint32)
                     + sizeof(uint64) + sizeof(uint32)
-                    + sizeof(uint32) * table_init_data->func_index_count);
+                    + comp_ctx->pointer_size
+                          * table_init_data->func_index_count);
 }
 }
 
 
 static uint32
 static uint32
-get_table_init_data_list_size(AOTTableInitData **table_init_data_list,
+get_table_init_data_list_size(AOTCompContext *comp_ctx,
+                              AOTTableInitData **table_init_data_list,
                               uint32 table_init_data_count)
                               uint32 table_init_data_count)
 {
 {
     /*
     /*
@@ -279,7 +282,7 @@ get_table_init_data_list_size(AOTTableInitData **table_init_data_list,
      * |                     | U32 offset.init_expr_type
      * |                     | U32 offset.init_expr_type
      * |                     | U64 offset.u.i64
      * |                     | U64 offset.u.i64
      * |                     | U32 func_index_count
      * |                     | U32 func_index_count
-     * |                     | U32[func_index_count]
+     * |                     | U32/U64 [func_index_count]
      * ------------------------------
      * ------------------------------
      */
      */
     AOTTableInitData **table_init_data = table_init_data_list;
     AOTTableInitData **table_init_data = table_init_data_list;
@@ -289,7 +292,7 @@ get_table_init_data_list_size(AOTTableInitData **table_init_data_list,
 
 
     for (i = 0; i < table_init_data_count; i++, table_init_data++) {
     for (i = 0; i < table_init_data_count; i++, table_init_data++) {
         size = align_uint(size, 4);
         size = align_uint(size, 4);
-        size += get_table_init_data_size(*table_init_data);
+        size += get_table_init_data_size(comp_ctx, *table_init_data);
     }
     }
     return size;
     return size;
 }
 }
@@ -331,7 +334,7 @@ get_table_size(AOTCompData *comp_data)
 }
 }
 
 
 static uint32
 static uint32
-get_table_info_size(AOTCompData *comp_data)
+get_table_info_size(AOTCompContext *comp_ctx, AOTCompData *comp_data)
 {
 {
     /*
     /*
      * ------------------------------
      * ------------------------------
@@ -355,7 +358,8 @@ get_table_info_size(AOTCompData *comp_data)
      * ------------------------------
      * ------------------------------
      */
      */
     return get_import_table_size(comp_data) + get_table_size(comp_data)
     return get_import_table_size(comp_data) + get_table_size(comp_data)
-           + get_table_init_data_list_size(comp_data->table_init_data_list,
+           + get_table_init_data_list_size(comp_ctx,
+                                           comp_data->table_init_data_list,
                                            comp_data->table_init_data_count);
                                            comp_data->table_init_data_count);
 }
 }
 
 
@@ -530,7 +534,7 @@ get_init_data_section_size(AOTCompContext *comp_ctx, AOTCompData *comp_data,
     size += get_mem_info_size(comp_data);
     size += get_mem_info_size(comp_data);
 
 
     size = align_uint(size, 4);
     size = align_uint(size, 4);
-    size += get_table_info_size(comp_data);
+    size += get_table_info_size(comp_ctx, comp_data);
 
 
     size = align_uint(size, 4);
     size = align_uint(size, 4);
     size += get_func_type_info_size(comp_data);
     size += get_func_type_info_size(comp_data);
@@ -1470,11 +1474,18 @@ aot_emit_table_info(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
         EMIT_U32(init_datas[i]->offset.init_expr_type);
         EMIT_U32(init_datas[i]->offset.init_expr_type);
         EMIT_U64(init_datas[i]->offset.u.i64);
         EMIT_U64(init_datas[i]->offset.u.i64);
         EMIT_U32(init_datas[i]->func_index_count);
         EMIT_U32(init_datas[i]->func_index_count);
-        for (j = 0; j < init_datas[i]->func_index_count; j++)
-            EMIT_U32(init_datas[i]->func_indexes[j]);
+        for (j = 0; j < init_datas[i]->func_index_count; j++) {
+
+            if (comp_ctx->pointer_size == 4) {
+                EMIT_U32(init_datas[i]->func_indexes[j]);
+            }
+            else {
+                EMIT_U64(init_datas[i]->func_indexes[j]);
+            }
+        }
     }
     }
 
 
-    if (offset - *p_offset != get_table_info_size(comp_data)) {
+    if (offset - *p_offset != get_table_info_size(comp_ctx, comp_data)) {
         aot_set_last_error("emit table info failed.");
         aot_set_last_error("emit table info failed.");
         return false;
         return false;
     }
     }

+ 12 - 6
core/iwasm/compilation/aot_emit_function.c

@@ -1210,9 +1210,9 @@ aot_compile_op_call_indirect(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
        are equal (the type index of call_indirect opcode and callee func),
        are equal (the type index of call_indirect opcode and callee func),
        we don't need to check whether the whole function types are equal,
        we don't need to check whether the whole function types are equal,
        including param types and result types. */
        including param types and result types. */
-    type_idx = wasm_get_smallest_type_idx(comp_ctx->comp_data->func_types,
-                                          comp_ctx->comp_data->func_type_count,
-                                          type_idx);
+    type_idx = wasm_get_smallest_type_idx(
+        (WASMTypePtr *)comp_ctx->comp_data->func_types,
+        comp_ctx->comp_data->func_type_count, type_idx);
     ftype_idx_const = I32_CONST(type_idx);
     ftype_idx_const = I32_CONST(type_idx);
     CHECK_LLVM_CONST(ftype_idx_const);
     CHECK_LLVM_CONST(ftype_idx_const);
 
 
@@ -1285,25 +1285,31 @@ aot_compile_op_call_indirect(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
     }
     }
 
 
     if (!(table_elem = LLVMBuildBitCast(comp_ctx->builder, table_elem,
     if (!(table_elem = LLVMBuildBitCast(comp_ctx->builder, table_elem,
-                                        INT32_PTR_TYPE, "table_elem_i32p"))) {
+                                        INTPTR_PTR_TYPE, "table_elem_ptr"))) {
         HANDLE_FAILURE("LLVMBuildBitCast");
         HANDLE_FAILURE("LLVMBuildBitCast");
         goto fail;
         goto fail;
     }
     }
 
 
     /* Load function index */
     /* Load function index */
     if (!(table_elem =
     if (!(table_elem =
-              LLVMBuildInBoundsGEP2(comp_ctx->builder, I32_TYPE, table_elem,
+              LLVMBuildInBoundsGEP2(comp_ctx->builder, INTPTR_TYPE, table_elem,
                                     &elem_idx, 1, "table_elem"))) {
                                     &elem_idx, 1, "table_elem"))) {
         HANDLE_FAILURE("LLVMBuildNUWAdd");
         HANDLE_FAILURE("LLVMBuildNUWAdd");
         goto fail;
         goto fail;
     }
     }
 
 
-    if (!(func_idx = LLVMBuildLoad2(comp_ctx->builder, I32_TYPE, table_elem,
+    if (!(func_idx = LLVMBuildLoad2(comp_ctx->builder, INTPTR_TYPE, table_elem,
                                     "func_idx"))) {
                                     "func_idx"))) {
         aot_set_last_error("llvm build load failed.");
         aot_set_last_error("llvm build load failed.");
         goto fail;
         goto fail;
     }
     }
 
 
+    if (!(func_idx = LLVMBuildIntCast2(comp_ctx->builder, func_idx, I32_TYPE,
+                                       true, "func_idx_i32"))) {
+        aot_set_last_error("llvm build int cast failed.");
+        goto fail;
+    }
+
     /* Check if func_idx == -1 */
     /* Check if func_idx == -1 */
     if (!(cmp_func_idx = LLVMBuildICmp(comp_ctx->builder, LLVMIntEQ, func_idx,
     if (!(cmp_func_idx = LLVMBuildICmp(comp_ctx->builder, LLVMIntEQ, func_idx,
                                        I32_NEG_ONE, "cmp_func_idx"))) {
                                        I32_NEG_ONE, "cmp_func_idx"))) {

+ 13 - 7
core/iwasm/compilation/aot_emit_table.c

@@ -24,7 +24,7 @@ get_tbl_inst_offset(const AOTCompContext *comp_ctx,
         offset += offsetof(AOTTableInstance, elems);
         offset += offsetof(AOTTableInstance, elems);
         /* avoid loading from current AOTTableInstance */
         /* avoid loading from current AOTTableInstance */
         offset +=
         offset +=
-            sizeof(uint32)
+            comp_ctx->pointer_size
             * aot_get_imp_tbl_data_slots(imp_tbls + i, comp_ctx->is_jit_mode);
             * aot_get_imp_tbl_data_slots(imp_tbls + i, comp_ctx->is_jit_mode);
         ++i;
         ++i;
     }
     }
@@ -38,7 +38,7 @@ get_tbl_inst_offset(const AOTCompContext *comp_ctx,
     while (i < tbl_idx && i < comp_ctx->comp_data->table_count) {
     while (i < tbl_idx && i < comp_ctx->comp_data->table_count) {
         offset += offsetof(AOTTableInstance, elems);
         offset += offsetof(AOTTableInstance, elems);
         /* avoid loading from current AOTTableInstance */
         /* avoid loading from current AOTTableInstance */
-        offset += sizeof(uint32)
+        offset += comp_ctx->pointer_size
                   * aot_get_tbl_data_slots(tbls + i, comp_ctx->is_jit_mode);
                   * aot_get_tbl_data_slots(tbls + i, comp_ctx->is_jit_mode);
         ++i;
         ++i;
     }
     }
@@ -194,25 +194,31 @@ aot_compile_op_table_get(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
     }
     }
 
 
     if (!(table_elem = LLVMBuildBitCast(comp_ctx->builder, table_elem,
     if (!(table_elem = LLVMBuildBitCast(comp_ctx->builder, table_elem,
-                                        INT32_PTR_TYPE, "table_elem_i32p"))) {
+                                        INTPTR_PTR_TYPE, "table_elem_i32p"))) {
         HANDLE_FAILURE("LLVMBuildBitCast");
         HANDLE_FAILURE("LLVMBuildBitCast");
         goto fail;
         goto fail;
     }
     }
 
 
     /* Load function index */
     /* Load function index */
     if (!(table_elem =
     if (!(table_elem =
-              LLVMBuildInBoundsGEP2(comp_ctx->builder, I32_TYPE, table_elem,
+              LLVMBuildInBoundsGEP2(comp_ctx->builder, INTPTR_TYPE, table_elem,
                                     &elem_idx, 1, "table_elem"))) {
                                     &elem_idx, 1, "table_elem"))) {
         HANDLE_FAILURE("LLVMBuildNUWAdd");
         HANDLE_FAILURE("LLVMBuildNUWAdd");
         goto fail;
         goto fail;
     }
     }
 
 
-    if (!(func_idx = LLVMBuildLoad2(comp_ctx->builder, I32_TYPE, table_elem,
+    if (!(func_idx = LLVMBuildLoad2(comp_ctx->builder, INTPTR_TYPE, table_elem,
                                     "func_idx"))) {
                                     "func_idx"))) {
         HANDLE_FAILURE("LLVMBuildLoad");
         HANDLE_FAILURE("LLVMBuildLoad");
         goto fail;
         goto fail;
     }
     }
 
 
+    if (!(func_idx = LLVMBuildIntCast2(comp_ctx->builder, func_idx, I32_TYPE,
+                                       true, "func_idx_i32"))) {
+        HANDLE_FAILURE("LLVMBuildIntCast");
+        goto fail;
+    }
+
     PUSH_I32(func_idx);
     PUSH_I32(func_idx);
 
 
     return true;
     return true;
@@ -248,14 +254,14 @@ aot_compile_op_table_set(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
     }
     }
 
 
     if (!(table_elem = LLVMBuildBitCast(comp_ctx->builder, table_elem,
     if (!(table_elem = LLVMBuildBitCast(comp_ctx->builder, table_elem,
-                                        INT32_PTR_TYPE, "table_elem_i32p"))) {
+                                        INTPTR_PTR_TYPE, "table_elem_i32p"))) {
         HANDLE_FAILURE("LLVMBuildBitCast");
         HANDLE_FAILURE("LLVMBuildBitCast");
         goto fail;
         goto fail;
     }
     }
 
 
     /* Load function index */
     /* Load function index */
     if (!(table_elem =
     if (!(table_elem =
-              LLVMBuildInBoundsGEP2(comp_ctx->builder, I32_TYPE, table_elem,
+              LLVMBuildInBoundsGEP2(comp_ctx->builder, INTPTR_TYPE, table_elem,
                                     &elem_idx, 1, "table_elem"))) {
                                     &elem_idx, 1, "table_elem"))) {
         HANDLE_FAILURE("LLVMBuildInBoundsGEP");
         HANDLE_FAILURE("LLVMBuildInBoundsGEP");
         goto fail;
         goto fail;

+ 15 - 3
core/iwasm/compilation/aot_llvm.c

@@ -1088,7 +1088,8 @@ aot_create_func_contexts(AOTCompData *comp_data, AOTCompContext *comp_ctx)
 }
 }
 
 
 static bool
 static bool
-aot_set_llvm_basic_types(AOTLLVMTypes *basic_types, LLVMContextRef context)
+aot_set_llvm_basic_types(AOTLLVMTypes *basic_types, LLVMContextRef context,
+                         int pointer_size)
 {
 {
     basic_types->int1_type = LLVMInt1TypeInContext(context);
     basic_types->int1_type = LLVMInt1TypeInContext(context);
     basic_types->int8_type = LLVMInt8TypeInContext(context);
     basic_types->int8_type = LLVMInt8TypeInContext(context);
@@ -1131,9 +1132,19 @@ aot_set_llvm_basic_types(AOTLLVMTypes *basic_types, LLVMContextRef context)
     basic_types->funcref_type = LLVMInt32TypeInContext(context);
     basic_types->funcref_type = LLVMInt32TypeInContext(context);
     basic_types->externref_type = LLVMInt32TypeInContext(context);
     basic_types->externref_type = LLVMInt32TypeInContext(context);
 
 
+    if (pointer_size == 4) {
+        basic_types->intptr_type = basic_types->int32_type;
+        basic_types->intptr_ptr_type = basic_types->int32_ptr_type;
+    }
+    else {
+        basic_types->intptr_type = basic_types->int64_type;
+        basic_types->intptr_ptr_type = basic_types->int64_ptr_type;
+    }
+
     return (basic_types->int8_ptr_type && basic_types->int8_pptr_type
     return (basic_types->int8_ptr_type && basic_types->int8_pptr_type
             && basic_types->int16_ptr_type && basic_types->int32_ptr_type
             && basic_types->int16_ptr_type && basic_types->int32_ptr_type
-            && basic_types->int64_ptr_type && basic_types->float32_ptr_type
+            && basic_types->int64_ptr_type && basic_types->intptr_type
+            && basic_types->intptr_ptr_type && basic_types->float32_ptr_type
             && basic_types->float64_ptr_type && basic_types->i8x16_vec_type
             && basic_types->float64_ptr_type && basic_types->i8x16_vec_type
             && basic_types->i16x8_vec_type && basic_types->i32x4_vec_type
             && basic_types->i16x8_vec_type && basic_types->i32x4_vec_type
             && basic_types->i64x2_vec_type && basic_types->f32x4_vec_type
             && basic_types->i64x2_vec_type && basic_types->f32x4_vec_type
@@ -2112,7 +2123,8 @@ aot_create_comp_context(AOTCompData *comp_data, aot_comp_option_t option)
         goto fail;
         goto fail;
     }
     }
 
 
-    if (!aot_set_llvm_basic_types(&comp_ctx->basic_types, comp_ctx->context)) {
+    if (!aot_set_llvm_basic_types(&comp_ctx->basic_types, comp_ctx->context,
+                                  comp_ctx->pointer_size)) {
         aot_set_last_error("create LLVM basic types failed.");
         aot_set_last_error("create LLVM basic types failed.");
         goto fail;
         goto fail;
     }
     }

+ 2 - 0
core/iwasm/compilation/aot_llvm.h

@@ -193,6 +193,7 @@ typedef struct AOTLLVMTypes {
     LLVMTypeRef int16_type;
     LLVMTypeRef int16_type;
     LLVMTypeRef int32_type;
     LLVMTypeRef int32_type;
     LLVMTypeRef int64_type;
     LLVMTypeRef int64_type;
+    LLVMTypeRef intptr_type;
     LLVMTypeRef float32_type;
     LLVMTypeRef float32_type;
     LLVMTypeRef float64_type;
     LLVMTypeRef float64_type;
     LLVMTypeRef void_type;
     LLVMTypeRef void_type;
@@ -202,6 +203,7 @@ typedef struct AOTLLVMTypes {
     LLVMTypeRef int16_ptr_type;
     LLVMTypeRef int16_ptr_type;
     LLVMTypeRef int32_ptr_type;
     LLVMTypeRef int32_ptr_type;
     LLVMTypeRef int64_ptr_type;
     LLVMTypeRef int64_ptr_type;
+    LLVMTypeRef intptr_ptr_type;
     LLVMTypeRef float32_ptr_type;
     LLVMTypeRef float32_ptr_type;
     LLVMTypeRef float64_ptr_type;
     LLVMTypeRef float64_ptr_type;
 
 

+ 3 - 1
core/iwasm/fast-jit/fe/jit_emit_function.c

@@ -480,7 +480,9 @@ jit_compile_op_call_indirect(JitCompContext *cc, uint32 type_idx,
     if (UINTPTR_MAX == UINT64_MAX) {
     if (UINTPTR_MAX == UINT64_MAX) {
         offset_i32 = jit_cc_new_reg_I32(cc);
         offset_i32 = jit_cc_new_reg_I32(cc);
         offset = jit_cc_new_reg_I64(cc);
         offset = jit_cc_new_reg_I64(cc);
-        GEN_INSN(SHL, offset_i32, elem_idx, NEW_CONST(I32, 2));
+        /* Calculate offset by pointer size (elem_idx *
+         * sizeof(table_elem_type_t)) */
+        GEN_INSN(SHL, offset_i32, elem_idx, NEW_CONST(I32, 3));
         GEN_INSN(I32TOI64, offset, offset_i32);
         GEN_INSN(I32TOI64, offset, offset_i32);
     }
     }
     else {
     else {

+ 16 - 12
core/iwasm/fast-jit/fe/jit_emit_table.c

@@ -45,7 +45,8 @@ jit_compile_op_table_get(JitCompContext *cc, uint32 tbl_idx)
     GEN_INSN(I32TOI64, elem_idx_long, elem_idx);
     GEN_INSN(I32TOI64, elem_idx_long, elem_idx);
 
 
     offset = jit_cc_new_reg_I64(cc);
     offset = jit_cc_new_reg_I64(cc);
-    GEN_INSN(MUL, offset, elem_idx_long, NEW_CONST(I64, sizeof(uint32)));
+    GEN_INSN(MUL, offset, elem_idx_long,
+             NEW_CONST(I64, sizeof(table_elem_type_t)));
 
 
     res = jit_cc_new_reg_I32(cc);
     res = jit_cc_new_reg_I32(cc);
     tbl_elems = get_table_elems_reg(cc->jit_frame, tbl_idx);
     tbl_elems = get_table_elems_reg(cc->jit_frame, tbl_idx);
@@ -76,7 +77,8 @@ jit_compile_op_table_set(JitCompContext *cc, uint32 tbl_idx)
     GEN_INSN(I32TOI64, elem_idx_long, elem_idx);
     GEN_INSN(I32TOI64, elem_idx_long, elem_idx);
 
 
     offset = jit_cc_new_reg_I64(cc);
     offset = jit_cc_new_reg_I64(cc);
-    GEN_INSN(MUL, offset, elem_idx_long, NEW_CONST(I64, sizeof(uint32)));
+    GEN_INSN(MUL, offset, elem_idx_long,
+             NEW_CONST(I64, sizeof(table_elem_type_t)));
 
 
     tbl_elems = get_table_elems_reg(cc->jit_frame, tbl_idx);
     tbl_elems = get_table_elems_reg(cc->jit_frame, tbl_idx);
     GEN_INSN(STI32, elem_val, tbl_elems, offset);
     GEN_INSN(STI32, elem_val, tbl_elems, offset);
@@ -106,9 +108,10 @@ wasm_init_table(WASMModuleInstance *inst, uint32 tbl_idx, uint32 elem_idx,
         goto out_of_bounds;
         goto out_of_bounds;
 
 
     bh_memcpy_s((uint8 *)tbl + offsetof(WASMTableInstance, elems)
     bh_memcpy_s((uint8 *)tbl + offsetof(WASMTableInstance, elems)
-                    + dst * sizeof(uint32),
-                (uint32)((tbl_sz - dst) * sizeof(uint32)),
-                elem->func_indexes + src, (uint32)(len * sizeof(uint32)));
+                    + dst * sizeof(table_elem_type_t),
+                (uint32)((tbl_sz - dst) * sizeof(table_elem_type_t)),
+                elem->func_indexes + src,
+                (uint32)(len * sizeof(table_elem_type_t)));
 
 
     return 0;
     return 0;
 out_of_bounds:
 out_of_bounds:
@@ -167,12 +170,13 @@ wasm_copy_table(WASMModuleInstance *inst, uint32 src_tbl_idx,
     if (dst_offset > dst_tbl_sz || dst_tbl_sz - dst_offset < len)
     if (dst_offset > dst_tbl_sz || dst_tbl_sz - dst_offset < len)
         goto out_of_bounds;
         goto out_of_bounds;
 
 
-    bh_memmove_s((uint8 *)dst_tbl + offsetof(WASMTableInstance, elems)
-                     + dst_offset * sizeof(uint32),
-                 (uint32)((dst_tbl_sz - dst_offset) * sizeof(uint32)),
-                 (uint8 *)src_tbl + offsetof(WASMTableInstance, elems)
-                     + src_offset * sizeof(uint32),
-                 (uint32)(len * sizeof(uint32)));
+    bh_memmove_s(
+        (uint8 *)dst_tbl + offsetof(WASMTableInstance, elems)
+            + dst_offset * sizeof(table_elem_type_t),
+        (uint32)((dst_tbl_sz - dst_offset) * sizeof(table_elem_type_t)),
+        (uint8 *)src_tbl + offsetof(WASMTableInstance, elems)
+            + src_offset * sizeof(table_elem_type_t),
+        (uint32)(len * sizeof(table_elem_type_t)));
 
 
     return 0;
     return 0;
 out_of_bounds:
 out_of_bounds:
@@ -264,7 +268,7 @@ fail:
 
 
 static int
 static int
 wasm_fill_table(WASMModuleInstance *inst, uint32 tbl_idx, uint32 dst,
 wasm_fill_table(WASMModuleInstance *inst, uint32 tbl_idx, uint32 dst,
-                uint32 val, uint32 len)
+                uintptr_t val, uint32 len)
 {
 {
     WASMTableInstance *tbl;
     WASMTableInstance *tbl;
     uint32 tbl_sz;
     uint32 tbl_sz;

+ 2 - 2
core/iwasm/fast-jit/jit_frontend.c

@@ -97,9 +97,9 @@ jit_frontend_get_table_inst_offset(const WASMModule *module, uint32 tbl_idx)
 
 
         offset += (uint32)offsetof(WASMTableInstance, elems);
         offset += (uint32)offsetof(WASMTableInstance, elems);
 #if WASM_ENABLE_MULTI_MODULE != 0
 #if WASM_ENABLE_MULTI_MODULE != 0
-        offset += (uint32)sizeof(uint32) * table->max_size;
+        offset += (uint32)sizeof(table_elem_type_t) * table->max_size;
 #else
 #else
-        offset += (uint32)sizeof(uint32)
+        offset += (uint32)sizeof(table_elem_type_t)
                   * (table->possible_grow ? table->max_size : table->init_size);
                   * (table->possible_grow ? table->max_size : table->init_size);
 #endif
 #endif
 
 

+ 2 - 2
core/iwasm/interpreter/wasm.h

@@ -79,7 +79,7 @@ extern "C" {
 #define DEFAULT_MAX_PAGES 65536
 #define DEFAULT_MAX_PAGES 65536
 
 
 #if WASM_ENABLE_GC == 0
 #if WASM_ENABLE_GC == 0
-typedef uint32 table_elem_type_t;
+typedef uintptr_t table_elem_type_t;
 #define NULL_REF (0xFFFFFFFF)
 #define NULL_REF (0xFFFFFFFF)
 #else
 #else
 typedef void *table_elem_type_t;
 typedef void *table_elem_type_t;
@@ -610,7 +610,7 @@ typedef struct WASMTableSeg {
     uint32 table_index;
     uint32 table_index;
     InitializerExpression base_offset;
     InitializerExpression base_offset;
     uint32 function_count;
     uint32 function_count;
-    uint32 *func_indexes;
+    uintptr_t *func_indexes;
 } WASMTableSeg;
 } WASMTableSeg;
 
 
 typedef struct WASMDataSeg {
 typedef struct WASMDataSeg {

+ 10 - 10
core/iwasm/interpreter/wasm_interp_classic.c

@@ -4222,7 +4222,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
                         WASMTableInstance *tbl_inst;
                         WASMTableInstance *tbl_inst;
 #if WASM_ENABLE_GC != 0
 #if WASM_ENABLE_GC != 0
                         void **table_elems;
                         void **table_elems;
-                        uint32 *func_indexes;
+                        uintptr_t *func_indexes;
 #endif
 #endif
 
 
                         read_leb_uint32(frame_ip, frame_ip_end, elem_idx);
                         read_leb_uint32(frame_ip, frame_ip_end, elem_idx);
@@ -4267,15 +4267,15 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
                         }
                         }
 
 
 #if WASM_ENABLE_GC == 0
 #if WASM_ENABLE_GC == 0
-                        bh_memcpy_s(
-                            (uint8 *)tbl_inst
-                                + offsetof(WASMTableInstance, elems)
-                                + d * sizeof(uint32),
-                            (uint32)((tbl_inst->cur_size - d) * sizeof(uint32)),
-                            module->module->table_segments[elem_idx]
-                                    .func_indexes
-                                + s,
-                            (uint32)(n * sizeof(uint32)));
+                        bh_memcpy_s((uint8 *)tbl_inst
+                                        + offsetof(WASMTableInstance, elems)
+                                        + d * sizeof(table_elem_type_t),
+                                    (uint32)((tbl_inst->cur_size - d)
+                                             * sizeof(table_elem_type_t)),
+                                    module->module->table_segments[elem_idx]
+                                            .func_indexes
+                                        + s,
+                                    (uint32)(n * sizeof(table_elem_type_t)));
 #else
 #else
                         SYNC_ALL_TO_FRAME();
                         SYNC_ALL_TO_FRAME();
                         table_elems = tbl_inst->elems + d;
                         table_elems = tbl_inst->elems + d;

+ 10 - 10
core/iwasm/interpreter/wasm_interp_fast.c

@@ -4026,7 +4026,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
                         WASMTableInstance *tbl_inst;
                         WASMTableInstance *tbl_inst;
 #if WASM_ENABLE_GC != 0
 #if WASM_ENABLE_GC != 0
                         void **table_elems;
                         void **table_elems;
-                        uint32 *func_indexes;
+                        uintptr_t *func_indexes;
                         uint64 i;
                         uint64 i;
 #endif
 #endif
 
 
@@ -4070,15 +4070,15 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
                         }
                         }
 
 
 #if WASM_ENABLE_GC == 0
 #if WASM_ENABLE_GC == 0
-                        bh_memcpy_s(
-                            (uint8 *)tbl_inst
-                                + offsetof(WASMTableInstance, elems)
-                                + d * sizeof(uint32),
-                            (uint32)((tbl_inst->cur_size - d) * sizeof(uint32)),
-                            module->module->table_segments[elem_idx]
-                                    .func_indexes
-                                + s,
-                            (uint32)(n * sizeof(uint32)));
+                        bh_memcpy_s((uint8 *)tbl_inst
+                                        + offsetof(WASMTableInstance, elems)
+                                        + d * sizeof(table_elem_type_t),
+                                    (uint32)((tbl_inst->cur_size - d)
+                                             * sizeof(table_elem_type_t)),
+                                    module->module->table_segments[elem_idx]
+                                            .func_indexes
+                                        + s,
+                                    (uint32)(n * sizeof(table_elem_type_t)));
 #else
 #else
                         SYNC_ALL_TO_FRAME();
                         SYNC_ALL_TO_FRAME();
                         table_elems = tbl_inst->elems + d;
                         table_elems = tbl_inst->elems + d;

+ 2 - 2
core/iwasm/interpreter/wasm_loader.c

@@ -3468,9 +3468,9 @@ load_func_index_vec(const uint8 **p_buf, const uint8 *buf_end,
 
 
     read_leb_uint32(p, p_end, function_count);
     read_leb_uint32(p, p_end, function_count);
     table_segment->function_count = function_count;
     table_segment->function_count = function_count;
-    total_size = sizeof(uint32) * (uint64)function_count;
+    total_size = sizeof(uintptr_t) * (uint64)function_count;
     if (total_size > 0
     if (total_size > 0
-        && !(table_segment->func_indexes = (uint32 *)loader_malloc(
+        && !(table_segment->func_indexes = (uintptr_t *)loader_malloc(
                  total_size, error_buf, error_buf_size))) {
                  total_size, error_buf, error_buf_size))) {
         return false;
         return false;
     }
     }

+ 2 - 2
core/iwasm/interpreter/wasm_mini_loader.c

@@ -1345,9 +1345,9 @@ load_func_index_vec(const uint8 **p_buf, const uint8 *buf_end,
 
 
     read_leb_uint32(p, p_end, function_count);
     read_leb_uint32(p, p_end, function_count);
     table_segment->function_count = function_count;
     table_segment->function_count = function_count;
-    total_size = sizeof(uint32) * (uint64)function_count;
+    total_size = sizeof(uintptr_t) * (uint64)function_count;
     if (total_size > 0
     if (total_size > 0
-        && !(table_segment->func_indexes = (uint32 *)loader_malloc(
+        && !(table_segment->func_indexes = (uintptr_t *)loader_malloc(
                  total_size, error_buf, error_buf_size))) {
                  total_size, error_buf, error_buf_size))) {
         return false;
         return false;
     }
     }

+ 12 - 10
core/iwasm/interpreter/wasm_runtime.c

@@ -640,7 +640,7 @@ tables_instantiate(const WASMModule *module, WASMModuleInstance *module_inst,
 #endif
 #endif
 #if WASM_ENABLE_GC == 0
 #if WASM_ENABLE_GC == 0
         /* Store function indexes */
         /* Store function indexes */
-        total_size += sizeof(uint32) * (uint64)max_size_fixed;
+        total_size += sizeof(uintptr_t) * (uint64)max_size_fixed;
 #else
 #else
         /* Store object pointers */
         /* Store object pointers */
         total_size += sizeof(uintptr_t) * (uint64)max_size_fixed;
         total_size += sizeof(uintptr_t) * (uint64)max_size_fixed;
@@ -2313,8 +2313,8 @@ wasm_instantiate(WASMModule *module, bool is_sub_inst,
         bh_memcpy_s(
         bh_memcpy_s(
             table_data + table_seg->base_offset.u.i32,
             table_data + table_seg->base_offset.u.i32,
             (uint32)((table->cur_size - (uint32)table_seg->base_offset.u.i32)
             (uint32)((table->cur_size - (uint32)table_seg->base_offset.u.i32)
-                     * sizeof(uint32)),
-            table_seg->func_indexes, (uint32)(length * sizeof(uint32)));
+                     * sizeof(table_elem_type_t)),
+            table_seg->func_indexes, (uint32)(length * sizeof(uintptr_t)));
 #else
 #else
         for (j = 0; j < length; j++) {
         for (j = 0; j < length; j++) {
             WASMFuncObjectRef func_obj;
             WASMFuncObjectRef func_obj;
@@ -3628,10 +3628,11 @@ llvm_jit_table_init(WASMModuleInstance *module_inst, uint32 tbl_idx,
     }
     }
 
 
     bh_memcpy_s((uint8 *)tbl_inst + offsetof(WASMTableInstance, elems)
     bh_memcpy_s((uint8 *)tbl_inst + offsetof(WASMTableInstance, elems)
-                    + dst_offset * sizeof(uint32),
-                (uint32)sizeof(uint32) * (tbl_inst->cur_size - dst_offset),
+                    + dst_offset * sizeof(table_elem_type_t),
+                (uint32)sizeof(table_elem_type_t)
+                    * (tbl_inst->cur_size - dst_offset),
                 tbl_seg->func_indexes + src_offset,
                 tbl_seg->func_indexes + src_offset,
-                (uint32)(length * sizeof(uint32)));
+                (uint32)(length * sizeof(table_elem_type_t)));
 }
 }
 
 
 void
 void
@@ -3665,11 +3666,12 @@ llvm_jit_table_copy(WASMModuleInstance *module_inst, uint32 src_tbl_idx,
     /* if src_offset < dst_offset, copy from back to front */
     /* if src_offset < dst_offset, copy from back to front */
     /* merge all together */
     /* merge all together */
     bh_memmove_s((uint8 *)dst_tbl_inst + offsetof(WASMTableInstance, elems)
     bh_memmove_s((uint8 *)dst_tbl_inst + offsetof(WASMTableInstance, elems)
-                     + sizeof(uint32) * dst_offset,
-                 (uint32)sizeof(uint32) * (dst_tbl_inst->cur_size - dst_offset),
+                     + sizeof(table_elem_type_t) * dst_offset,
+                 (uint32)sizeof(table_elem_type_t)
+                     * (dst_tbl_inst->cur_size - dst_offset),
                  (uint8 *)src_tbl_inst + offsetof(WASMTableInstance, elems)
                  (uint8 *)src_tbl_inst + offsetof(WASMTableInstance, elems)
-                     + sizeof(uint32) * src_offset,
-                 (uint32)sizeof(uint32) * length);
+                     + sizeof(table_elem_type_t) * src_offset,
+                 (uint32)sizeof(table_elem_type_t) * length);
 }
 }
 
 
 void
 void