Skip to content

Commit 1f3d64e

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

10 files changed

Lines changed: 322 additions & 158 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 & 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
{
@@ -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
{
@@ -1190,9 +1204,7 @@ destroy_table_init_data_list(AOTTableInitData **data_list, uint32 count)
11901204
}
11911205
#endif
11921206
#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-
}
1207+
destroy_init_expr(&data_list[i]->offset);
11961208
#endif
11971209
wasm_runtime_free(data_list[i]);
11981210
}
@@ -1406,7 +1418,7 @@ load_init_expr(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
14061418
(void)free_if_fail;
14071419
#endif
14081420
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
1409-
destroy_sub_init_expr(expr);
1421+
destroy_init_expr(expr);
14101422
#endif
14111423
return false;
14121424
}
@@ -1624,8 +1636,7 @@ load_table_init_data_list(const uint8 **p_buf, const uint8 *buf_end,
16241636
}
16251637
}
16261638
#endif
1627-
bh_memcpy_s(&data_list[i]->offset, sizeof(InitializerExpression),
1628-
&offset_expr, sizeof(InitializerExpression));
1639+
data_list[i]->offset = offset_expr;
16291640
data_list[i]->value_count = value_count;
16301641
for (j = 0; j < data_list[i]->value_count; j++) {
16311642
if (!load_init_expr(&buf, buf_end, module,
@@ -4516,20 +4527,11 @@ aot_unload(AOTModule *module)
45164527
destroy_import_globals(module->import_globals);
45174528

45184529
if (module->globals) {
4519-
#if WASM_ENABLE_GC != 0
4530+
#if WASM_ENABLE_GC != 0 || WASM_ENABLE_EXTENDED_CONST_EXPR != 0
45204531
uint32 i;
45214532
for (i = 0; i < module->global_count; i++) {
45224533
destroy_init_expr(&module->globals[i].init_expr);
45234534
}
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-
}
45334535
#endif
45344536
destroy_globals(module->globals);
45354537
}

core/iwasm/aot/aot_runtime.c

Lines changed: 25 additions & 45 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) {
@@ -501,14 +501,14 @@ get_init_expr_recursive(AOTModuleInstance *module_inst, AOTModule *module,
501501
case INIT_EXPR_TYPE_I64_MUL:
502502
{
503503
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)) {
504+
if (!get_init_value_recursive(module_inst, module,
505+
expr->u.binary.l_expr, &l_value,
506+
error_buf, error_buf_size)) {
507507
return false;
508508
}
509-
if (!get_init_expr_recursive(module_inst, module,
510-
expr->u.binary.r_expr, &r_value,
511-
error_buf, error_buf_size)) {
509+
if (!get_init_value_recursive(module_inst, module,
510+
expr->u.binary.r_expr, &r_value,
511+
error_buf, error_buf_size)) {
512512
return false;
513513
}
514514

@@ -580,9 +580,9 @@ global_instantiate(AOTModuleInstance *module_inst, AOTModule *module,
580580
#endif
581581
{
582582
WASMValue value;
583-
if (!get_init_expr_recursive(module_inst, module, init_expr,
584-
&value, error_buf,
585-
error_buf_size)) {
583+
if (!get_init_value_recursive(module_inst, module, init_expr,
584+
&value, error_buf,
585+
error_buf_size)) {
586586
return false;
587587
}
588588
init_global_data(p, global->type.val_type, &value);
@@ -845,26 +845,12 @@ tables_instantiate(AOTModuleInstance *module_inst, AOTModule *module,
845845
bh_assert(offset_flag == INIT_EXPR_TYPE_GET_GLOBAL
846846
|| offset_flag == INIT_EXPR_TYPE_FUNCREF_CONST
847847
|| 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)));
848+
|| (tbl_inst->is_table64 ? is_valid_i64_offset(offset_flag)
849+
: is_valid_i32_offset(offset_flag)));
857850
#else
858851
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)));
852+
|| (tbl_inst->is_table64 ? is_valid_i64_offset(offset_flag)
853+
: is_valid_i32_offset(offset_flag)));
868854
#endif
869855

870856
/* Resolve table data base offset */
@@ -891,9 +877,9 @@ tables_instantiate(AOTModuleInstance *module_inst, AOTModule *module,
891877
}
892878
else {
893879
WASMValue offset_value;
894-
if (!get_init_expr_recursive(module_inst, module,
895-
&table_seg->offset, &offset_value,
896-
error_buf, error_buf_size)) {
880+
if (!get_init_value_recursive(module_inst, module,
881+
&table_seg->offset, &offset_value,
882+
error_buf, error_buf_size)) {
897883
return false;
898884
}
899885
base_offset = (uint32)offset_value.i32;
@@ -1274,14 +1260,8 @@ memories_instantiate(AOTModuleInstance *module_inst, AOTModuleInstance *parent,
12741260
offset_flag = data_seg->offset.init_expr_type;
12751261
bh_assert(offset_flag == INIT_EXPR_TYPE_GET_GLOBAL
12761262
|| (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)));
1263+
? is_valid_i64_offset(offset_flag)
1264+
: is_valid_i32_offset(offset_flag)));
12851265

12861266
/* Resolve memory data base offset */
12871267
if (offset_flag == INIT_EXPR_TYPE_GET_GLOBAL) {
@@ -1314,9 +1294,9 @@ memories_instantiate(AOTModuleInstance *module_inst, AOTModuleInstance *parent,
13141294
}
13151295
else {
13161296
WASMValue offset_value;
1317-
if (!get_init_expr_recursive(module_inst, module, &data_seg->offset,
1318-
&offset_value, error_buf,
1319-
error_buf_size)) {
1297+
if (!get_init_value_recursive(module_inst, module,
1298+
&data_seg->offset, &offset_value,
1299+
error_buf, error_buf_size)) {
13201300
return false;
13211301
}
13221302
#if WASM_ENABLE_MEMORY64 != 0
@@ -2238,7 +2218,7 @@ aot_instantiate(AOTModule *module, AOTModuleInstance *parent,
22382218
*(uint32 *)(module_inst->global_data + data_offset);
22392219
}
22402220
else {
2241-
if (!get_init_expr_recursive(
2221+
if (!get_init_value_recursive(
22422222
module_inst, module, &table_init_data->offset,
22432223
&offset_value, error_buf, error_buf_size)) {
22442224
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)