Skip to content

Commit 7746b25

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

10 files changed

Lines changed: 358 additions & 202 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: 36 additions & 33 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
{
@@ -993,10 +1022,9 @@ destroy_mem_init_data_list(AOTModule *module, AOTMemInitData **data_list,
9931022
/* If the module owns the binary data, free the bytes buffer */
9941023
if (module->is_binary_freeable && data_list[i]->bytes)
9951024
wasm_runtime_free(data_list[i]->bytes);
1025+
9961026
#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-
}
1027+
destroy_init_expr(&data_list[i]->offset);
10001028
#endif
10011029
/* Free the data segment structure itself */
10021030
wasm_runtime_free(data_list[i]);
@@ -1067,8 +1095,7 @@ load_mem_init_data_list(const uint8 **p_buf, const uint8 *buf_end,
10671095
data_list[i]->is_passive = (bool)is_passive;
10681096
data_list[i]->memory_index = memory_index;
10691097
#endif
1070-
bh_memcpy_s(&data_list[i]->offset, sizeof(InitializerExpression),
1071-
&offset_expr, sizeof(InitializerExpression));
1098+
data_list[i]->offset = offset_expr;
10721099
data_list[i]->byte_count = byte_count;
10731100
data_list[i]->bytes = NULL;
10741101
/* If the module owns the binary data, clone the bytes buffer */
@@ -1153,18 +1180,6 @@ load_memory_info(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
11531180
return false;
11541181
}
11551182

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-
11681183
static void
11691184
destroy_import_tables(AOTImportTable *import_tables)
11701185
{
@@ -1190,9 +1205,7 @@ destroy_table_init_data_list(AOTTableInitData **data_list, uint32 count)
11901205
}
11911206
#endif
11921207
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
1193-
if (is_expr_binary_op(data_list[i]->offset.init_expr_type)) {
1194-
destroy_sub_init_expr(&data_list[i]->offset);
1195-
}
1208+
destroy_init_expr(&data_list[i]->offset);
11961209
#endif
11971210
wasm_runtime_free(data_list[i]);
11981211
}
@@ -1406,7 +1419,7 @@ load_init_expr(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
14061419
(void)free_if_fail;
14071420
#endif
14081421
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
1409-
destroy_sub_init_expr(expr);
1422+
destroy_init_expr(expr);
14101423
#endif
14111424
return false;
14121425
}
@@ -1624,8 +1637,7 @@ load_table_init_data_list(const uint8 **p_buf, const uint8 *buf_end,
16241637
}
16251638
}
16261639
#endif
1627-
bh_memcpy_s(&data_list[i]->offset, sizeof(InitializerExpression),
1628-
&offset_expr, sizeof(InitializerExpression));
1640+
data_list[i]->offset = offset_expr;
16291641
data_list[i]->value_count = value_count;
16301642
for (j = 0; j < data_list[i]->value_count; j++) {
16311643
if (!load_init_expr(&buf, buf_end, module,
@@ -4516,20 +4528,11 @@ aot_unload(AOTModule *module)
45164528
destroy_import_globals(module->import_globals);
45174529

45184530
if (module->globals) {
4519-
#if WASM_ENABLE_GC != 0
4531+
#if WASM_ENABLE_GC != 0 || WASM_ENABLE_EXTENDED_CONST_EXPR != 0
45204532
uint32 i;
45214533
for (i = 0; i < module->global_count; i++) {
45224534
destroy_init_expr(&module->globals[i].init_expr);
45234535
}
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-
}
45334536
#endif
45344537
destroy_globals(module->globals);
45354538
}

core/iwasm/aot/aot_runtime.c

Lines changed: 32 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -451,9 +451,9 @@ assign_table_init_value(AOTModuleInstance *module_inst, AOTModule *module,
451451
#endif /* end of WASM_ENABLE_GC != 0 */
452452

453453
static bool
454-
get_init_expr_recursive(AOTModuleInstance *module_inst, AOTModule *module,
455-
InitializerExpression *expr, WASMValue *value,
456-
char *error_buf, uint32 error_buf_size)
454+
get_init_value_recursive(AOTModuleInstance *module_inst, AOTModule *module,
455+
InitializerExpression *expr, WASMValue *value,
456+
char *error_buf, uint32 error_buf_size)
457457
{
458458
uint8 flag = expr->init_expr_type;
459459
switch (flag) {
@@ -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
@@ -501,14 +495,14 @@ get_init_expr_recursive(AOTModuleInstance *module_inst, AOTModule *module,
501495
case INIT_EXPR_TYPE_I64_MUL:
502496
{
503497
WASMValue l_value, r_value;
504-
if (!get_init_expr_recursive(module_inst, module,
505-
expr->u.binary.l_expr, &l_value,
506-
error_buf, error_buf_size)) {
498+
if (!get_init_value_recursive(module_inst, module,
499+
expr->u.binary.l_expr, &l_value,
500+
error_buf, error_buf_size)) {
507501
return false;
508502
}
509-
if (!get_init_expr_recursive(module_inst, module,
510-
expr->u.binary.r_expr, &r_value,
511-
error_buf, error_buf_size)) {
503+
if (!get_init_value_recursive(module_inst, module,
504+
expr->u.binary.r_expr, &r_value,
505+
error_buf, error_buf_size)) {
512506
return false;
513507
}
514508

@@ -580,9 +574,9 @@ global_instantiate(AOTModuleInstance *module_inst, AOTModule *module,
580574
#endif
581575
{
582576
WASMValue value;
583-
if (!get_init_expr_recursive(module_inst, module, init_expr,
584-
&value, error_buf,
585-
error_buf_size)) {
577+
if (!get_init_value_recursive(module_inst, module, init_expr,
578+
&value, error_buf,
579+
error_buf_size)) {
586580
return false;
587581
}
588582
init_global_data(p, global->type.val_type, &value);
@@ -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 */
@@ -891,9 +871,9 @@ tables_instantiate(AOTModuleInstance *module_inst, AOTModule *module,
891871
}
892872
else {
893873
WASMValue offset_value;
894-
if (!get_init_expr_recursive(module_inst, module,
895-
&table_seg->offset, &offset_value,
896-
error_buf, error_buf_size)) {
874+
if (!get_init_value_recursive(module_inst, module,
875+
&table_seg->offset, &offset_value,
876+
error_buf, error_buf_size)) {
897877
return false;
898878
}
899879
base_offset = (uint32)offset_value.i32;
@@ -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) {
@@ -1314,9 +1288,9 @@ memories_instantiate(AOTModuleInstance *module_inst, AOTModuleInstance *parent,
13141288
}
13151289
else {
13161290
WASMValue offset_value;
1317-
if (!get_init_expr_recursive(module_inst, module, &data_seg->offset,
1318-
&offset_value, error_buf,
1319-
error_buf_size)) {
1291+
if (!get_init_value_recursive(module_inst, module,
1292+
&data_seg->offset, &offset_value,
1293+
error_buf, error_buf_size)) {
13201294
return false;
13211295
}
13221296
#if WASM_ENABLE_MEMORY64 != 0
@@ -2238,7 +2212,7 @@ aot_instantiate(AOTModule *module, AOTModuleInstance *parent,
22382212
*(uint32 *)(module_inst->global_data + data_offset);
22392213
}
22402214
else {
2241-
if (!get_init_expr_recursive(
2215+
if (!get_init_value_recursive(
22422216
module_inst, module, &table_init_data->offset,
22432217
&offset_value, error_buf, error_buf_size)) {
22442218
goto fail;

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)