Skip to content

Commit 8f08062

Browse files
committed
implement extended const expr for aot
1 parent 1026241 commit 8f08062

5 files changed

Lines changed: 276 additions & 71 deletions

File tree

core/config.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,4 +720,8 @@ unless used elsewhere */
720720
#define WASM_ENABLE_INSTRUCTION_METERING 0
721721
#endif
722722

723+
#ifndef WASM_ENABLE_EXTENDED_CONST_EXPR
724+
#define WASM_ENABLE_EXTENDED_CONST_EXPR 0
725+
#endif
726+
723727
#endif /* end of _CONFIG_H_ */

core/iwasm/aot/aot_loader.c

Lines changed: 60 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -993,6 +993,11 @@ destroy_mem_init_data_list(AOTModule *module, AOTMemInitData **data_list,
993993
/* If the module owns the binary data, free the bytes buffer */
994994
if (module->is_binary_freeable && data_list[i]->bytes)
995995
wasm_runtime_free(data_list[i]->bytes);
996+
#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+
}
1000+
#endif
9961001
/* Free the data segment structure itself */
9971002
wasm_runtime_free(data_list[i]);
9981003
}
@@ -1043,11 +1048,11 @@ load_mem_init_data_list(const uint8 **p_buf, const uint8 *buf_end,
10431048
uint32 byte_count;
10441049
uint32 is_passive;
10451050
uint32 memory_index;
1046-
InitializerExpression init_value;
1051+
InitializerExpression offset_expr;
10471052

10481053
read_uint32(buf, buf_end, is_passive);
10491054
read_uint32(buf, buf_end, memory_index);
1050-
if (!load_init_expr(&buf, buf_end, module, &init_value, error_buf,
1055+
if (!load_init_expr(&buf, buf_end, module, &offset_expr, error_buf,
10511056
error_buf_size)) {
10521057
return false;
10531058
}
@@ -1062,8 +1067,8 @@ load_mem_init_data_list(const uint8 **p_buf, const uint8 *buf_end,
10621067
data_list[i]->is_passive = (bool)is_passive;
10631068
data_list[i]->memory_index = memory_index;
10641069
#endif
1065-
data_list[i]->offset.init_expr_type = init_value.init_expr_type;
1066-
data_list[i]->offset.u = init_value.u;
1070+
bh_memcpy_s(&data_list[i]->offset, sizeof(InitializerExpression),
1071+
&offset_expr, sizeof(InitializerExpression));
10671072
data_list[i]->byte_count = byte_count;
10681073
data_list[i]->bytes = NULL;
10691074
/* If the module owns the binary data, clone the bytes buffer */
@@ -1183,6 +1188,11 @@ destroy_table_init_data_list(AOTTableInitData **data_list, uint32 count)
11831188
for (j = 0; j < data_list[i]->value_count; j++) {
11841189
destroy_init_expr(&data_list[i]->init_values[j]);
11851190
}
1191+
#endif
1192+
#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+
}
11861196
#endif
11871197
wasm_runtime_free(data_list[i]);
11881198
}
@@ -1350,6 +1360,32 @@ load_init_expr(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
13501360
break;
13511361
}
13521362
#endif /* end of WASM_ENABLE_GC != 0 */
1363+
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
1364+
case INIT_EXPR_TYPE_I32_ADD:
1365+
case INIT_EXPR_TYPE_I32_SUB:
1366+
case INIT_EXPR_TYPE_I32_MUL:
1367+
case INIT_EXPR_TYPE_I64_ADD:
1368+
case INIT_EXPR_TYPE_I64_SUB:
1369+
case INIT_EXPR_TYPE_I64_MUL:
1370+
{
1371+
expr->l_expr = expr->r_expr = NULL;
1372+
if (!(expr->l_expr = loader_malloc(sizeof(InitializerExpression),
1373+
error_buf, error_buf_size))) {
1374+
goto fail;
1375+
}
1376+
if (!load_init_expr(&buf, buf_end, module, expr->l_expr, error_buf,
1377+
error_buf_size))
1378+
goto fail;
1379+
if (!(expr->r_expr = loader_malloc(sizeof(InitializerExpression),
1380+
error_buf, error_buf_size))) {
1381+
goto fail;
1382+
}
1383+
if (!load_init_expr(&buf, buf_end, module, expr->r_expr, error_buf,
1384+
error_buf_size))
1385+
goto fail;
1386+
break;
1387+
}
1388+
#endif /* end of WASM_ENABLE_EXTENDED_CONST_EXPR != 0 */
13531389
default:
13541390
set_error_buf(error_buf, error_buf_size, "invalid init expr type.");
13551391
return false;
@@ -1366,6 +1402,9 @@ load_init_expr(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
13661402
}
13671403
#else
13681404
(void)free_if_fail;
1405+
#endif
1406+
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
1407+
destroy_sub_init_expr(expr);
13691408
#endif
13701409
return false;
13711410
}
@@ -1528,14 +1567,16 @@ load_table_init_data_list(const uint8 **p_buf, const uint8 *buf_end,
15281567
/* Create each table data segment */
15291568
for (i = 0; i < module->table_init_data_count; i++) {
15301569
uint32 mode, elem_type;
1531-
uint32 table_index, init_expr_type, value_count;
1532-
uint64 init_expr_value, size1;
1570+
uint32 table_index, value_count;
1571+
uint64 size1;
1572+
InitializerExpression offset_expr;
15331573

15341574
read_uint32(buf, buf_end, mode);
15351575
read_uint32(buf, buf_end, elem_type);
15361576
read_uint32(buf, buf_end, table_index);
1537-
read_uint32(buf, buf_end, init_expr_type);
1538-
read_uint64(buf, buf_end, init_expr_value);
1577+
if (!load_init_expr(&buf, buf_end, module, &offset_expr, error_buf,
1578+
error_buf_size))
1579+
return false;
15391580
#if WASM_ENABLE_GC != 0
15401581
if (wasm_is_type_multi_byte_type(elem_type)) {
15411582
uint16 ref_type, nullable;
@@ -1581,8 +1622,8 @@ load_table_init_data_list(const uint8 **p_buf, const uint8 *buf_end,
15811622
}
15821623
}
15831624
#endif
1584-
data_list[i]->offset.init_expr_type = (uint8)init_expr_type;
1585-
data_list[i]->offset.u.i64 = (int64)init_expr_value;
1625+
bh_memcpy_s(&data_list[i]->offset, sizeof(InitializerExpression),
1626+
&offset_expr, sizeof(InitializerExpression));
15861627
data_list[i]->value_count = value_count;
15871628
for (j = 0; j < data_list[i]->value_count; j++) {
15881629
if (!load_init_expr(&buf, buf_end, module,
@@ -4478,6 +4519,15 @@ aot_unload(AOTModule *module)
44784519
for (i = 0; i < module->global_count; i++) {
44794520
destroy_init_expr(&module->globals[i].init_expr);
44804521
}
4522+
#endif
4523+
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
4524+
uint32 j;
4525+
for (j = 0; j < module->global_count; j++) {
4526+
if (is_expr_binary_op(
4527+
module->globals[j].init_expr.init_expr_type)) {
4528+
destroy_sub_init_expr(&module->globals[j].init_expr);
4529+
}
4530+
}
44814531
#endif
44824532
destroy_globals(module->globals);
44834533
}

0 commit comments

Comments
 (0)