Skip to content

Commit 1026241

Browse files
committed
implement extended const expr for interp
1 parent ea5757f commit 1026241

10 files changed

Lines changed: 1032 additions & 113 deletions

File tree

build-scripts/config_common.cmake

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,10 @@ if (NOT DEFINED WAMR_BUILD_TAIL_CALL)
211211
set (WAMR_BUILD_TAIL_CALL 0)
212212
endif ()
213213

214+
if (NOT DEFINED WAMR_BUILD_EXTENDED_CONST_EXPR)
215+
set (WAMR_BUILD_EXTENDED_CONST_EXPR 0)
216+
endif ()
217+
214218
########################################
215219
# Compilation options to marco
216220
########################################
@@ -675,7 +679,13 @@ if (WAMR_BUILD_INSTRUCTION_METERING EQUAL 1)
675679
message (" Instruction metering enabled")
676680
add_definitions (-DWASM_ENABLE_INSTRUCTION_METERING=1)
677681
endif ()
678-
682+
if (WAMR_BUILD_EXTENDED_CONST_EXPR EQUAL 1)
683+
message (" Extended constant expression enabled")
684+
add_definitions(-DWASM_ENABLE_EXTENDED_CONST_EXPR=1)
685+
else()
686+
message (" Extended constant expression disabled")
687+
add_definitions(-DWASM_ENABLE_EXTENDED_CONST_EXPR=0)
688+
endif ()
679689
########################################
680690
# Show Phase4 Wasm proposals status.
681691
########################################
@@ -699,11 +709,11 @@ message (
699709
" \"Tail call\" via WAMR_BUILD_TAIL_CALL: ${WAMR_BUILD_TAIL_CALL}\n"
700710
" \"Threads\" via WAMR_BUILD_SHARED_MEMORY: ${WAMR_BUILD_SHARED_MEMORY}\n"
701711
" \"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"
702713
" Unsupported (>= Phase4):\n"
703714
" \"Branch Hinting\"\n"
704715
" \"Custom Annotation Syntax in the Text Format\"\n"
705716
" \"Exception handling\"\n"
706-
" \"Extended Constant Expressions\"\n"
707717
" \"Import/Export of Mutable Globals\"\n"
708718
" \"JS String Builtins\"\n"
709719
" \"Relaxed SIMD\"\n"

core/iwasm/common/wasm_loader_common.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,3 +225,32 @@ read_leb(uint8 **p_buf, const uint8 *buf_end, uint32 maxbits, bool sign,
225225
return false;
226226
}
227227
}
228+
229+
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
230+
void
231+
destroy_init_expr_recursive(InitializerExpression *expr)
232+
{
233+
if (expr == NULL) {
234+
return;
235+
}
236+
if (expr->l_expr) {
237+
destroy_init_expr_recursive(expr->l_expr);
238+
}
239+
if (expr->r_expr) {
240+
destroy_init_expr_recursive(expr->r_expr);
241+
}
242+
wasm_runtime_free(expr);
243+
}
244+
245+
void
246+
destroy_sub_init_expr(InitializerExpression *expr)
247+
{
248+
if (expr->l_expr) {
249+
destroy_init_expr_recursive(expr->l_expr);
250+
}
251+
if (expr->r_expr) {
252+
destroy_init_expr_recursive(expr->r_expr);
253+
}
254+
expr->l_expr = expr->r_expr = NULL;
255+
}
256+
#endif /* end of WASM_ENABLE_EXTENDED_CONST_EXPR != 0 */

core/iwasm/interpreter/wasm.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,12 @@ typedef void *table_elem_type_t;
135135
#define INIT_EXPR_TYPE_F64_CONST 0x44
136136
#define INIT_EXPR_TYPE_V128_CONST 0xFD
137137
#define INIT_EXPR_TYPE_GET_GLOBAL 0x23
138+
#define INIT_EXPR_TYPE_I32_ADD 0x6A
139+
#define INIT_EXPR_TYPE_I32_SUB 0x6B
140+
#define INIT_EXPR_TYPE_I32_MUL 0x6C
141+
#define INIT_EXPR_TYPE_I64_ADD 0x7C
142+
#define INIT_EXPR_TYPE_I64_SUB 0x7D
143+
#define INIT_EXPR_TYPE_I64_MUL 0x7E
138144
#define INIT_EXPR_TYPE_REFNULL_CONST 0xD0
139145
#define INIT_EXPR_TYPE_FUNCREF_CONST 0xD2
140146
#define INIT_EXPR_TYPE_STRUCT_NEW 0xD3
@@ -278,8 +284,28 @@ typedef struct InitializerExpression {
278284
constant expression */
279285
uint8 init_expr_type;
280286
WASMValue u;
287+
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
288+
struct InitializerExpression *l_expr;
289+
struct InitializerExpression *r_expr;
290+
#endif
281291
} InitializerExpression;
282292

293+
static inline bool
294+
is_expr_binary_op(uint8 flag)
295+
{
296+
return flag == INIT_EXPR_TYPE_I32_ADD || flag == INIT_EXPR_TYPE_I32_SUB
297+
|| flag == INIT_EXPR_TYPE_I32_MUL || flag == INIT_EXPR_TYPE_I64_ADD
298+
|| flag == INIT_EXPR_TYPE_I64_SUB || flag == INIT_EXPR_TYPE_I64_MUL;
299+
}
300+
301+
#if WASM_ENABLE_EXTENDED_CONST_EXPR != 0
302+
void
303+
destroy_init_expr_recursive(InitializerExpression *expr);
304+
305+
void
306+
destroy_sub_init_expr(InitializerExpression *expr);
307+
#endif
308+
283309
#if WASM_ENABLE_GC != 0
284310
/**
285311
* Reference type of (ref null ht) or (ref ht),

0 commit comments

Comments
 (0)