Skip to content

Commit 45c71ed

Browse files
committed
resolve comments and enable extended const for mini loader
1 parent 0f475a5 commit 45c71ed

10 files changed

Lines changed: 312 additions & 157 deletions

File tree

build-scripts/config_common.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,7 @@ message (
699699
" \"WebAssembly C and C++ API\"\n"
700700
" Configurable. 0 is OFF. 1 is ON:\n"
701701
" \"Bulk Memory Operation\" via WAMR_BUILD_BULK_MEMORY: ${WAMR_BUILD_BULK_MEMORY}\n"
702+
" \"Extended Constant Expressions\" via WAMR_BUILD_EXTENDED_CONST_EXPR: ${WAMR_BUILD_EXTENDED_CONST_EXPR}\n"
702703
" \"Fixed-width SIMD\" via WAMR_BUILD_SIMD: ${WAMR_BUILD_SIMD}\n"
703704
" \"Garbage collection\" via WAMR_BUILD_GC: ${WAMR_BUILD_GC}\n"
704705
" \"Legacy Exception handling\" via WAMR_BUILD_EXCE_HANDLING: ${WAMR_BUILD_EXCE_HANDLING}\n"
@@ -709,7 +710,6 @@ message (
709710
" \"Tail call\" via WAMR_BUILD_TAIL_CALL: ${WAMR_BUILD_TAIL_CALL}\n"
710711
" \"Threads\" via WAMR_BUILD_SHARED_MEMORY: ${WAMR_BUILD_SHARED_MEMORY}\n"
711712
" \"Typed Function References\" via WAMR_BUILD_GC: ${WAMR_BUILD_GC}\n"
712-
" \"Extended Constant Expressions\" via WAMR_BUILD_EXTENDED_CONST_EXPR: ${WAMR_BUILD_EXTENDED_CONST_EXPR}\n"
713713
" Unsupported (>= Phase4):\n"
714714
" \"Branch Hinting\"\n"
715715
" \"Custom Annotation Syntax in the Text Format\"\n"

core/iwasm/aot/aot_loader.c

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -968,6 +968,35 @@ load_custom_section(const uint8 *buf, const uint8 *buf_end, AOTModule *module,
968968
return false;
969969
}
970970

971+
#if WASM_ENABLE_GC != 0 || WASM_ENABLE_EXTENDED_CONST_EXPR != 0
972+
static void
973+
destroy_init_expr(InitializerExpression *expr)
974+
{
975+
#if WASM_ENABLE_GC != 0
976+
if (expr->init_expr_type == INIT_EXPR_TYPE_STRUCT_NEW
977+
|| expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW
978+
|| expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
979+
wasm_runtime_free(expr->u.unary.v.data);
980+
}
981+
#endif
982+
983+
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
984+
// free left expr and right expr for binary oprand
985+
if (is_expr_binary_op(expr->init_expr_type)) {
986+
return;
987+
}
988+
if (expr->u.binary.l_expr) {
989+
destroy_init_expr_recursive(expr->u.binary.l_expr);
990+
}
991+
if (expr->u.binary.r_expr) {
992+
destroy_init_expr_recursive(expr->u.binary.r_expr);
993+
}
994+
expr->u.binary.l_expr = expr->u.binary.r_expr = NULL;
995+
#endif
996+
}
997+
#endif /* end of WASM_ENABLE_GC != 0 || WASM_ENABLE_EXTENDED_CONST_EXPR != 0 \
998+
*/
999+
9711000
static void
9721001
destroy_import_memories(AOTImportMemory *import_memories)
9731002
{
@@ -994,9 +1023,7 @@ destroy_mem_init_data_list(AOTModule *module, AOTMemInitData **data_list,
9941023
if (module->is_binary_freeable && data_list[i]->bytes)
9951024
wasm_runtime_free(data_list[i]->bytes);
9961025
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
997-
if (is_expr_binary_op(data_list[i]->offset.init_expr_type)) {
998-
destroy_sub_init_expr(&data_list[i]->offset);
999-
}
1026+
destroy_init_expr(&data_list[i]->offset);
10001027
#endif
10011028
/* Free the data segment structure itself */
10021029
wasm_runtime_free(data_list[i]);
@@ -1067,8 +1094,7 @@ load_mem_init_data_list(const uint8 **p_buf, const uint8 *buf_end,
10671094
data_list[i]->is_passive = (bool)is_passive;
10681095
data_list[i]->memory_index = memory_index;
10691096
#endif
1070-
bh_memcpy_s(&data_list[i]->offset, sizeof(InitializerExpression),
1071-
&offset_expr, sizeof(InitializerExpression));
1097+
data_list[i]->offset = offset_expr;
10721098
data_list[i]->byte_count = byte_count;
10731099
data_list[i]->bytes = NULL;
10741100
/* If the module owns the binary data, clone the bytes buffer */
@@ -1153,18 +1179,6 @@ load_memory_info(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
11531179
return false;
11541180
}
11551181

1156-
#if WASM_ENABLE_GC != 0
1157-
static void
1158-
destroy_init_expr(InitializerExpression *expr)
1159-
{
1160-
if (expr->init_expr_type == INIT_EXPR_TYPE_STRUCT_NEW
1161-
|| expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW
1162-
|| expr->init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW_FIXED) {
1163-
wasm_runtime_free(expr->u.unary.v.data);
1164-
}
1165-
}
1166-
#endif /* end of WASM_ENABLE_GC != 0 */
1167-
11681182
static void
11691183
destroy_import_tables(AOTImportTable *import_tables)
11701184
{
@@ -1191,7 +1205,7 @@ destroy_table_init_data_list(AOTTableInitData **data_list, uint32 count)
11911205
#endif
11921206
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
11931207
if (is_expr_binary_op(data_list[i]->offset.init_expr_type)) {
1194-
destroy_sub_init_expr(&data_list[i]->offset);
1208+
destroy_init_expr(&data_list[i]->offset);
11951209
}
11961210
#endif
11971211
wasm_runtime_free(data_list[i]);
@@ -1406,7 +1420,7 @@ load_init_expr(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
14061420
(void)free_if_fail;
14071421
#endif
14081422
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
1409-
destroy_sub_init_expr(expr);
1423+
destroy_init_expr(expr);
14101424
#endif
14111425
return false;
14121426
}
@@ -1624,8 +1638,7 @@ load_table_init_data_list(const uint8 **p_buf, const uint8 *buf_end,
16241638
}
16251639
}
16261640
#endif
1627-
bh_memcpy_s(&data_list[i]->offset, sizeof(InitializerExpression),
1628-
&offset_expr, sizeof(InitializerExpression));
1641+
data_list[i]->offset = offset_expr;
16291642
data_list[i]->value_count = value_count;
16301643
for (j = 0; j < data_list[i]->value_count; j++) {
16311644
if (!load_init_expr(&buf, buf_end, module,
@@ -4516,20 +4529,11 @@ aot_unload(AOTModule *module)
45164529
destroy_import_globals(module->import_globals);
45174530

45184531
if (module->globals) {
4519-
#if WASM_ENABLE_GC != 0
4532+
#if WASM_ENABLE_GC != 0 || WASM_ENABLE_EXTENDED_CONST_EXPR != 0
45204533
uint32 i;
45214534
for (i = 0; i < module->global_count; i++) {
45224535
destroy_init_expr(&module->globals[i].init_expr);
45234536
}
4524-
#endif
4525-
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
4526-
uint32 j;
4527-
for (j = 0; j < module->global_count; j++) {
4528-
if (is_expr_binary_op(
4529-
module->globals[j].init_expr.init_expr_type)) {
4530-
destroy_sub_init_expr(&module->globals[j].init_expr);
4531-
}
4532-
}
45334537
#endif
45344538
destroy_globals(module->globals);
45354539
}

core/iwasm/aot/aot_runtime.c

Lines changed: 13 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -468,28 +468,22 @@ get_init_expr_recursive(AOTModuleInstance *module_inst, AOTModule *module,
468468
.global_data_linked;
469469
#else
470470
if (expr->u.unary.v.global_index < module->import_global_count) {
471-
bh_memcpy_s(
472-
value, sizeof(WASMValue),
473-
&module->import_globals[expr->u.unary.v.global_index]
474-
.global_data_linked,
475-
sizeof(WASMValue));
471+
*value = module->import_globals[expr->u.unary.v.global_index]
472+
.global_data_linked;
476473
}
477474
else {
478-
bh_memcpy_s(value, sizeof(WASMValue),
479-
&module
480-
->globals[expr->u.unary.v.global_index
481-
- module->import_global_count]
482-
.init_expr.u.unary.v,
483-
sizeof(WASMValue));
475+
*value = module
476+
->globals[expr->u.unary.v.global_index
477+
- module->import_global_count]
478+
.init_expr.u.unary.v;
484479
}
485480
#endif
486481
break;
487482
}
488483
case INIT_EXPR_TYPE_I32_CONST:
489484
case INIT_EXPR_TYPE_I64_CONST:
490485
{
491-
bh_memcpy_s(value, sizeof(WASMValue), &expr->u.unary.v,
492-
sizeof(WASMValue));
486+
*value = expr->u.unary.v;
493487
break;
494488
}
495489
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
@@ -845,26 +839,12 @@ tables_instantiate(AOTModuleInstance *module_inst, AOTModule *module,
845839
bh_assert(offset_flag == INIT_EXPR_TYPE_GET_GLOBAL
846840
|| offset_flag == INIT_EXPR_TYPE_FUNCREF_CONST
847841
|| offset_flag == INIT_EXPR_TYPE_REFNULL_CONST
848-
|| (tbl_inst->is_table64
849-
? (offset_flag == INIT_EXPR_TYPE_I64_CONST
850-
|| offset_flag == INIT_EXPR_TYPE_I64_ADD
851-
|| offset_flag == INIT_EXPR_TYPE_I64_SUB
852-
|| offset_flag == INIT_EXPR_TYPE_I64_MUL)
853-
: (offset_flag == INIT_EXPR_TYPE_I32_CONST
854-
|| offset_flag == INIT_EXPR_TYPE_I32_ADD
855-
|| offset_flag == INIT_EXPR_TYPE_I32_SUB
856-
|| offset_flag == INIT_EXPR_TYPE_I32_MUL)));
842+
|| (tbl_inst->is_table64 ? is_valid_i64_offset(offset_flag)
843+
: is_valid_i32_offset(offset_flag)));
857844
#else
858845
bh_assert(offset_flag == INIT_EXPR_TYPE_GET_GLOBAL
859-
|| (tbl_inst->is_table64
860-
? (offset_flag == INIT_EXPR_TYPE_I64_CONST
861-
|| offset_flag == INIT_EXPR_TYPE_I64_ADD
862-
|| offset_flag == INIT_EXPR_TYPE_I64_SUB
863-
|| offset_flag == INIT_EXPR_TYPE_I64_MUL)
864-
: (offset_flag == INIT_EXPR_TYPE_I32_CONST
865-
|| offset_flag == INIT_EXPR_TYPE_I32_ADD
866-
|| offset_flag == INIT_EXPR_TYPE_I32_SUB
867-
|| offset_flag == INIT_EXPR_TYPE_I32_MUL)));
846+
|| (tbl_inst->is_table64 ? is_valid_i64_offset(offset_flag)
847+
: is_valid_i32_offset(offset_flag)));
868848
#endif
869849

870850
/* Resolve table data base offset */
@@ -1274,14 +1254,8 @@ memories_instantiate(AOTModuleInstance *module_inst, AOTModuleInstance *parent,
12741254
offset_flag = data_seg->offset.init_expr_type;
12751255
bh_assert(offset_flag == INIT_EXPR_TYPE_GET_GLOBAL
12761256
|| (memory_inst->is_memory64
1277-
? (offset_flag == INIT_EXPR_TYPE_I64_CONST
1278-
|| offset_flag == INIT_EXPR_TYPE_I64_ADD
1279-
|| offset_flag == INIT_EXPR_TYPE_I64_SUB
1280-
|| offset_flag == INIT_EXPR_TYPE_I64_MUL)
1281-
: (offset_flag == INIT_EXPR_TYPE_I32_CONST
1282-
|| offset_flag == INIT_EXPR_TYPE_I32_ADD
1283-
|| offset_flag == INIT_EXPR_TYPE_I32_SUB
1284-
|| offset_flag == INIT_EXPR_TYPE_I32_MUL)));
1257+
? is_valid_i64_offset(offset_flag)
1258+
: is_valid_i32_offset(offset_flag)));
12851259

12861260
/* Resolve memory data base offset */
12871261
if (offset_flag == INIT_EXPR_TYPE_GET_GLOBAL) {

core/iwasm/common/wasm_loader_common.c

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -239,19 +239,4 @@ destroy_init_expr_recursive(InitializerExpression *expr)
239239
}
240240
wasm_runtime_free(expr);
241241
}
242-
243-
void
244-
destroy_sub_init_expr(InitializerExpression *expr)
245-
{
246-
if (is_expr_binary_op(expr->init_expr_type)) {
247-
return;
248-
}
249-
if (expr->u.binary.l_expr) {
250-
destroy_init_expr_recursive(expr->u.binary.l_expr);
251-
}
252-
if (expr->u.binary.r_expr) {
253-
destroy_init_expr_recursive(expr->u.binary.r_expr);
254-
}
255-
expr->u.binary.l_expr = expr->u.binary.r_expr = NULL;
256-
}
257242
#endif /* end of WASM_ENABLE_EXTENDED_CONST_EXPR != 0 */

core/iwasm/common/wasm_loader_common.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ void
5050
wasm_loader_set_error_buf(char *error_buf, uint32 error_buf_size,
5151
const char *string, bool is_aot);
5252

53+
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
54+
void
55+
destroy_init_expr_recursive(InitializerExpression *expr);
56+
#endif
57+
5358
#ifdef __cplusplus
5459
}
5560
#endif

core/iwasm/interpreter/wasm.h

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -302,13 +302,21 @@ is_expr_binary_op(uint8 flag)
302302
|| flag == INIT_EXPR_TYPE_I64_SUB || flag == INIT_EXPR_TYPE_I64_MUL;
303303
}
304304

305-
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
306-
void
307-
destroy_init_expr_recursive(InitializerExpression *expr);
305+
/* check if table or data offset is valid for i32 offset */
306+
static inline bool
307+
is_valid_i32_offset(uint8 flag)
308+
{
309+
return flag == INIT_EXPR_TYPE_I32_CONST || flag == INIT_EXPR_TYPE_I32_ADD
310+
|| flag == INIT_EXPR_TYPE_I32_SUB || flag == INIT_EXPR_TYPE_I32_MUL;
311+
}
308312

309-
void
310-
destroy_sub_init_expr(InitializerExpression *expr);
311-
#endif
313+
/* check if table or data offset is valid for i64 offset */
314+
static inline bool
315+
is_valid_i64_offset(uint8 flag)
316+
{
317+
return flag == INIT_EXPR_TYPE_I64_CONST || flag == INIT_EXPR_TYPE_I64_ADD
318+
|| flag == INIT_EXPR_TYPE_I64_SUB || flag == INIT_EXPR_TYPE_I64_MUL;
319+
}
312320

313321
#if WASM_ENABLE_GC != 0
314322
/**

0 commit comments

Comments
 (0)