@@ -968,6 +968,35 @@ load_custom_section(const uint8 *buf, const uint8 *buf_end, AOTModule *module,
968
968
return false;
969
969
}
970
970
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
+
971
1000
static void
972
1001
destroy_import_memories (AOTImportMemory * import_memories )
973
1002
{
@@ -993,6 +1022,10 @@ destroy_mem_init_data_list(AOTModule *module, AOTMemInitData **data_list,
993
1022
/* If the module owns the binary data, free the bytes buffer */
994
1023
if (module -> is_binary_freeable && data_list [i ]-> bytes )
995
1024
wasm_runtime_free (data_list [i ]-> bytes );
1025
+
1026
+ #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
1027
+ destroy_init_expr (& data_list [i ]-> offset );
1028
+ #endif
996
1029
/* Free the data segment structure itself */
997
1030
wasm_runtime_free (data_list [i ]);
998
1031
}
@@ -1043,11 +1076,11 @@ load_mem_init_data_list(const uint8 **p_buf, const uint8 *buf_end,
1043
1076
uint32 byte_count ;
1044
1077
uint32 is_passive ;
1045
1078
uint32 memory_index ;
1046
- InitializerExpression init_value ;
1079
+ InitializerExpression offset_expr ;
1047
1080
1048
1081
read_uint32 (buf , buf_end , is_passive );
1049
1082
read_uint32 (buf , buf_end , memory_index );
1050
- if (!load_init_expr (& buf , buf_end , module , & init_value , error_buf ,
1083
+ if (!load_init_expr (& buf , buf_end , module , & offset_expr , error_buf ,
1051
1084
error_buf_size )) {
1052
1085
return false;
1053
1086
}
@@ -1062,8 +1095,7 @@ load_mem_init_data_list(const uint8 **p_buf, const uint8 *buf_end,
1062
1095
data_list [i ]-> is_passive = (bool )is_passive ;
1063
1096
data_list [i ]-> memory_index = memory_index ;
1064
1097
#endif
1065
- data_list [i ]-> offset .init_expr_type = init_value .init_expr_type ;
1066
- data_list [i ]-> offset .u = init_value .u ;
1098
+ data_list [i ]-> offset = offset_expr ;
1067
1099
data_list [i ]-> byte_count = byte_count ;
1068
1100
data_list [i ]-> bytes = NULL ;
1069
1101
/* If the module owns the binary data, clone the bytes buffer */
@@ -1148,18 +1180,6 @@ load_memory_info(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
1148
1180
return false;
1149
1181
}
1150
1182
1151
- #if WASM_ENABLE_GC != 0
1152
- static void
1153
- destroy_init_expr (InitializerExpression * expr )
1154
- {
1155
- if (expr -> init_expr_type == INIT_EXPR_TYPE_STRUCT_NEW
1156
- || expr -> init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW
1157
- || expr -> init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW_FIXED ) {
1158
- wasm_runtime_free (expr -> u .data );
1159
- }
1160
- }
1161
- #endif /* end of WASM_ENABLE_GC != 0 */
1162
-
1163
1183
static void
1164
1184
destroy_import_tables (AOTImportTable * import_tables )
1165
1185
{
@@ -1183,6 +1203,9 @@ destroy_table_init_data_list(AOTTableInitData **data_list, uint32 count)
1183
1203
for (j = 0 ; j < data_list [i ]-> value_count ; j ++ ) {
1184
1204
destroy_init_expr (& data_list [i ]-> init_values [j ]);
1185
1205
}
1206
+ #endif
1207
+ #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
1208
+ destroy_init_expr (& data_list [i ]-> offset );
1186
1209
#endif
1187
1210
wasm_runtime_free (data_list [i ]);
1188
1211
}
@@ -1208,34 +1231,34 @@ load_init_expr(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
1208
1231
break ;
1209
1232
case INIT_EXPR_TYPE_I32_CONST :
1210
1233
case INIT_EXPR_TYPE_F32_CONST :
1211
- read_uint32 (buf , buf_end , expr -> u .i32 );
1234
+ read_uint32 (buf , buf_end , expr -> u .unary . v . i32 );
1212
1235
break ;
1213
1236
case INIT_EXPR_TYPE_I64_CONST :
1214
1237
case INIT_EXPR_TYPE_F64_CONST :
1215
- read_uint64 (buf , buf_end , expr -> u .i64 );
1238
+ read_uint64 (buf , buf_end , expr -> u .unary . v . i64 );
1216
1239
break ;
1217
1240
case INIT_EXPR_TYPE_V128_CONST :
1218
- i64x2 = (uint64 * )expr -> u .v128 .i64x2 ;
1241
+ i64x2 = (uint64 * )expr -> u .unary . v . v128 .i64x2 ;
1219
1242
CHECK_BUF (buf , buf_end , sizeof (uint64 ) * 2 );
1220
1243
wasm_runtime_read_v128 (buf , & i64x2 [0 ], & i64x2 [1 ]);
1221
1244
buf += sizeof (uint64 ) * 2 ;
1222
1245
break ;
1223
1246
case INIT_EXPR_TYPE_GET_GLOBAL :
1224
- read_uint32 (buf , buf_end , expr -> u .global_index );
1247
+ read_uint32 (buf , buf_end , expr -> u .unary . v . global_index );
1225
1248
break ;
1226
1249
/* INIT_EXPR_TYPE_FUNCREF_CONST can be used when
1227
1250
both reference types and GC are disabled */
1228
1251
case INIT_EXPR_TYPE_FUNCREF_CONST :
1229
- read_uint32 (buf , buf_end , expr -> u .ref_index );
1252
+ read_uint32 (buf , buf_end , expr -> u .unary . v . ref_index );
1230
1253
break ;
1231
1254
#if WASM_ENABLE_GC != 0 || WASM_ENABLE_REF_TYPES != 0
1232
1255
case INIT_EXPR_TYPE_REFNULL_CONST :
1233
- read_uint32 (buf , buf_end , expr -> u .ref_index );
1256
+ read_uint32 (buf , buf_end , expr -> u .unary . v . ref_index );
1234
1257
break ;
1235
1258
#endif /* end of WASM_ENABLE_GC != 0 || WASM_ENABLE_REF_TYPES != 0 */
1236
1259
#if WASM_ENABLE_GC != 0
1237
1260
case INIT_EXPR_TYPE_I31_NEW :
1238
- read_uint32 (buf , buf_end , expr -> u .i32 );
1261
+ read_uint32 (buf , buf_end , expr -> u .unary . v . i32 );
1239
1262
break ;
1240
1263
case INIT_EXPR_TYPE_STRUCT_NEW :
1241
1264
{
@@ -1256,7 +1279,7 @@ load_init_expr(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
1256
1279
free_if_fail = true;
1257
1280
init_values -> count = field_count ;
1258
1281
init_values -> type_idx = type_idx ;
1259
- expr -> u .data = init_values ;
1282
+ expr -> u .unary . v . data = init_values ;
1260
1283
1261
1284
if (type_idx >= module -> type_count ) {
1262
1285
set_error_buf (error_buf , error_buf_size ,
@@ -1294,7 +1317,7 @@ load_init_expr(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
1294
1317
break ;
1295
1318
}
1296
1319
case INIT_EXPR_TYPE_STRUCT_NEW_DEFAULT :
1297
- read_uint32 (buf , buf_end , expr -> u .type_index );
1320
+ read_uint32 (buf , buf_end , expr -> u .unary . v . type_index );
1298
1321
break ;
1299
1322
case INIT_EXPR_TYPE_ARRAY_NEW :
1300
1323
case INIT_EXPR_TYPE_ARRAY_NEW_DEFAULT :
@@ -1317,8 +1340,8 @@ load_init_expr(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
1317
1340
}
1318
1341
1319
1342
if (init_expr_type == INIT_EXPR_TYPE_ARRAY_NEW_DEFAULT ) {
1320
- expr -> u .array_new_default .type_index = type_idx ;
1321
- expr -> u .array_new_default .length = length ;
1343
+ expr -> u .unary . v . array_new_default .type_index = type_idx ;
1344
+ expr -> u .unary . v . array_new_default .length = length ;
1322
1345
}
1323
1346
else {
1324
1347
uint32 i , elem_size , elem_data_count ;
@@ -1329,7 +1352,7 @@ load_init_expr(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
1329
1352
return false;
1330
1353
}
1331
1354
free_if_fail = true;
1332
- expr -> u .data = init_values ;
1355
+ expr -> u .unary . v . data = init_values ;
1333
1356
1334
1357
init_values -> type_idx = type_idx ;
1335
1358
init_values -> length = length ;
@@ -1357,6 +1380,34 @@ load_init_expr(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
1357
1380
break ;
1358
1381
}
1359
1382
#endif /* end of WASM_ENABLE_GC != 0 */
1383
+ #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
1384
+ case INIT_EXPR_TYPE_I32_ADD :
1385
+ case INIT_EXPR_TYPE_I32_SUB :
1386
+ case INIT_EXPR_TYPE_I32_MUL :
1387
+ case INIT_EXPR_TYPE_I64_ADD :
1388
+ case INIT_EXPR_TYPE_I64_SUB :
1389
+ case INIT_EXPR_TYPE_I64_MUL :
1390
+ {
1391
+ expr -> u .binary .l_expr = expr -> u .binary .r_expr = NULL ;
1392
+ if (!(expr -> u .binary .l_expr =
1393
+ loader_malloc (sizeof (InitializerExpression ), error_buf ,
1394
+ error_buf_size ))) {
1395
+ goto fail ;
1396
+ }
1397
+ if (!load_init_expr (& buf , buf_end , module , expr -> u .binary .l_expr ,
1398
+ error_buf , error_buf_size ))
1399
+ goto fail ;
1400
+ if (!(expr -> u .binary .r_expr =
1401
+ loader_malloc (sizeof (InitializerExpression ), error_buf ,
1402
+ error_buf_size ))) {
1403
+ goto fail ;
1404
+ }
1405
+ if (!load_init_expr (& buf , buf_end , module , expr -> u .binary .r_expr ,
1406
+ error_buf , error_buf_size ))
1407
+ goto fail ;
1408
+ break ;
1409
+ }
1410
+ #endif /* end of WASM_ENABLE_EXTENDED_CONST_EXPR != 0 */
1360
1411
default :
1361
1412
set_error_buf (error_buf , error_buf_size , "invalid init expr type." );
1362
1413
return false;
@@ -1369,10 +1420,13 @@ load_init_expr(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
1369
1420
fail :
1370
1421
#if WASM_ENABLE_GC != 0
1371
1422
if (free_if_fail ) {
1372
- wasm_runtime_free (expr -> u .data );
1423
+ wasm_runtime_free (expr -> u .unary . v . data );
1373
1424
}
1374
1425
#else
1375
1426
(void )free_if_fail ;
1427
+ #endif
1428
+ #if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
1429
+ destroy_init_expr (expr );
1376
1430
#endif
1377
1431
return false;
1378
1432
}
@@ -1535,14 +1589,16 @@ load_table_init_data_list(const uint8 **p_buf, const uint8 *buf_end,
1535
1589
/* Create each table data segment */
1536
1590
for (i = 0 ; i < module -> table_init_data_count ; i ++ ) {
1537
1591
uint32 mode , elem_type ;
1538
- uint32 table_index , init_expr_type , value_count ;
1539
- uint64 init_expr_value , size1 ;
1592
+ uint32 table_index , value_count ;
1593
+ uint64 size1 ;
1594
+ InitializerExpression offset_expr ;
1540
1595
1541
1596
read_uint32 (buf , buf_end , mode );
1542
1597
read_uint32 (buf , buf_end , elem_type );
1543
1598
read_uint32 (buf , buf_end , table_index );
1544
- read_uint32 (buf , buf_end , init_expr_type );
1545
- read_uint64 (buf , buf_end , init_expr_value );
1599
+ if (!load_init_expr (& buf , buf_end , module , & offset_expr , error_buf ,
1600
+ error_buf_size ))
1601
+ return false;
1546
1602
#if WASM_ENABLE_GC != 0
1547
1603
if (wasm_is_type_multi_byte_type (elem_type )) {
1548
1604
uint16 ref_type , nullable ;
@@ -1588,8 +1644,7 @@ load_table_init_data_list(const uint8 **p_buf, const uint8 *buf_end,
1588
1644
}
1589
1645
}
1590
1646
#endif
1591
- data_list [i ]-> offset .init_expr_type = (uint8 )init_expr_type ;
1592
- data_list [i ]-> offset .u .i64 = (int64 )init_expr_value ;
1647
+ data_list [i ]-> offset = offset_expr ;
1593
1648
data_list [i ]-> value_count = value_count ;
1594
1649
for (j = 0 ; j < data_list [i ]-> value_count ; j ++ ) {
1595
1650
if (!load_init_expr (& buf , buf_end , module ,
@@ -4500,7 +4555,7 @@ aot_unload(AOTModule *module)
4500
4555
destroy_import_globals (module -> import_globals );
4501
4556
4502
4557
if (module -> globals ) {
4503
- #if WASM_ENABLE_GC != 0
4558
+ #if WASM_ENABLE_GC != 0 || WASM_ENABLE_EXTENDED_CONST_EXPR != 0
4504
4559
uint32 i ;
4505
4560
for (i = 0 ; i < module -> global_count ; i ++ ) {
4506
4561
destroy_init_expr (& module -> globals [i ].init_expr );
0 commit comments