Skip to content

Commit 2e272eb

Browse files
committed
WASM32 Support
1 parent 95f506a commit 2e272eb

File tree

15 files changed

+127
-3
lines changed

15 files changed

+127
-3
lines changed

build-scripts/config_common.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ elseif (WAMR_BUILD_TARGET STREQUAL "RISCV32_ILP32")
4545
add_definitions(-DBUILD_TARGET_RISCV32_ILP32)
4646
elseif (WAMR_BUILD_TARGET STREQUAL "ARC")
4747
add_definitions(-DBUILD_TARGET_ARC)
48+
elseif (WAMR_BUILD_TARGET STREQUAL "WASM32")
49+
add_definitions(-DBUILD_TARGET_WASM32)
4850
else ()
4951
message (FATAL_ERROR "-- WAMR build target isn't set")
5052
endif ()

core/config.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
&& !defined(BUILD_TARGET_RISCV32_ILP32D) \
2323
&& !defined(BUILD_TARGET_RISCV32_ILP32F) \
2424
&& !defined(BUILD_TARGET_RISCV32_ILP32) \
25-
&& !defined(BUILD_TARGET_ARC)
25+
&& !defined(BUILD_TARGET_ARC) \
26+
&& !defined(BUILD_TARGET_WASM32)
2627
/* clang-format on */
2728
#if defined(__x86_64__) || defined(__x86_64)
2829
#define BUILD_TARGET_X86_64
@@ -52,6 +53,8 @@
5253
#define BUILD_TARGET_RISCV32_ILP32D
5354
#elif defined(__arc__)
5455
#define BUILD_TARGET_ARC
56+
#elif defined(__wasm32__) || defined(__EMSCRIPTEN__)
57+
#define BUILD_TARGET_WASM32
5558
#else
5659
#error "Build target isn't set"
5760
#endif
@@ -294,6 +297,15 @@
294297
#define WASM_DEBUG_PREPROCESSOR 0
295298
#endif
296299

300+
/* Enable Invoke Native by default */
301+
#ifndef WASM_ENABLE_INVOKE_NATIVE
302+
#if defined(BUILD_TARGET_WASM32)
303+
#define WASM_ENABLE_INVOKE_NATIVE 0
304+
#else
305+
#define WASM_ENABLE_INVOKE_NATIVE 1
306+
#endif
307+
#endif
308+
297309
/* Enable opcode counter or not */
298310
#ifndef WASM_ENABLE_OPCODE_COUNTER
299311
#define WASM_ENABLE_OPCODE_COUNTER 0
@@ -676,7 +688,7 @@ unless used elsewhere */
676688
to speed up the calling process of invoking the AOT/JIT functions of
677689
these types from the host embedder */
678690
#ifndef WASM_ENABLE_QUICK_AOT_ENTRY
679-
#define WASM_ENABLE_QUICK_AOT_ENTRY 1
691+
#define WASM_ENABLE_QUICK_AOT_ENTRY WASM_ENABLE_INVOKE_NATIVE
680692
#endif
681693

682694
/* Support AOT intrinsic functions which can be called from the AOT code

core/iwasm/common/iwasm_common.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ elseif (WAMR_BUILD_TARGET MATCHES "RISCV*")
9191
set (source_all ${c_source_all} ${IWASM_COMMON_DIR}/arch/invokeNative_riscv.S)
9292
elseif (WAMR_BUILD_TARGET STREQUAL "ARC")
9393
set (source_all ${c_source_all} ${IWASM_COMMON_DIR}/arch/invokeNative_arc.s)
94+
elseif (WAMR_BUILD_TARGET STREQUAL "WASM32")
95+
set (source_all ${c_source_all})
9496
else ()
9597
message (FATAL_ERROR "Build target isn't set")
9698
endif ()

core/iwasm/common/wasm_runtime_common.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,9 +460,11 @@ wasm_runtime_env_init(void)
460460
if (bh_platform_init() != 0)
461461
return false;
462462

463+
#if WASM_ENABLE_INVOKE_NATIVE != 0
463464
if (wasm_native_init() == false) {
464465
goto fail1;
465466
}
467+
#endif
466468

467469
#if WASM_ENABLE_MULTI_MODULE
468470
if (BHT_OK != os_mutex_init(&registered_module_list_lock)) {
@@ -572,7 +574,9 @@ wasm_runtime_env_init(void)
572574
os_mutex_destroy(&registered_module_list_lock);
573575
fail2:
574576
#endif
577+
#if WASM_ENABLE_INVOKE_NATIVE != 0
575578
wasm_native_destroy();
579+
#endif
576580
fail1:
577581
bh_platform_destroy();
578582

@@ -694,7 +698,9 @@ wasm_runtime_destroy_internal(void)
694698
thread_manager_destroy();
695699
#endif
696700

701+
#if WASM_ENABLE_INVOKE_NATIVE != 0
697702
wasm_native_destroy();
703+
#endif
698704
bh_platform_destroy();
699705

700706
wasm_runtime_memory_destroy();
@@ -791,13 +797,15 @@ wasm_runtime_full_init_internal(RuntimeInitArgs *init_args)
791797
}
792798
#endif
793799

800+
#if WASM_ENABLE_INVOKE_NATIVE != 0
794801
if (init_args->n_native_symbols > 0
795802
&& !wasm_runtime_register_natives(init_args->native_module_name,
796803
init_args->native_symbols,
797804
init_args->n_native_symbols)) {
798805
wasm_runtime_destroy();
799806
return false;
800807
}
808+
#endif
801809

802810
#if WASM_ENABLE_THREAD_MGR != 0
803811
wasm_cluster_set_max_thread_num(init_args->max_thread_num);
@@ -4721,6 +4729,7 @@ wasm_table_type_get_max_size(WASMTableType *const table_type)
47214729
return table_type->max_size;
47224730
}
47234731

4732+
#if WASM_ENABLE_INVOKE_NATIVE != 0
47244733
bool
47254734
wasm_runtime_register_natives(const char *module_name,
47264735
NativeSymbol *native_symbols,
@@ -6249,6 +6258,8 @@ wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
62496258
|| defined(BUILD_TARGET_RISCV64_LP64D) \
62506259
|| defined(BUILD_TARGET_RISCV64_LP64) */
62516260

6261+
#endif /* end of WASM_ENABLE_INVOKE_NATIVE != 0 */
6262+
62526263
bool
62536264
wasm_runtime_call_indirect(WASMExecEnv *exec_env, uint32 element_index,
62546265
uint32 argc, uint32 argv[])
@@ -7426,8 +7437,12 @@ bool
74267437
wasm_runtime_is_import_func_linked(const char *module_name,
74277438
const char *func_name)
74287439
{
7440+
#if WASM_EANBLE_INVOKE_NATIVE != 0
74297441
return wasm_native_resolve_symbol(module_name, func_name, NULL, NULL, NULL,
74307442
NULL);
7443+
#else
7444+
return false;
7445+
#endif
74317446
}
74327447

74337448
bool

core/iwasm/compilation/aot.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,9 @@ typedef struct AOTImportFunc {
196196
const char *signature;
197197
/* attachment */
198198
void *attachment;
199+
#if WASM_ENABLE_INVOKE_NATIVE != 0
199200
bool call_conv_raw;
201+
#endif
200202
bool call_conv_wasm_c_api;
201203
bool wasm_c_api_with_env;
202204
} AOTImportFunc;

core/iwasm/interpreter/wasm.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,9 @@ typedef struct WASMFunctionImport {
611611
/* the type index of this function's func_type */
612612
uint32 type_idx;
613613
#endif
614+
#if WASM_ENABLE_INVOKE_NATIVE != 0
614615
bool call_conv_raw;
616+
#endif
615617
bool call_conv_wasm_c_api;
616618
#if WASM_ENABLE_MULTI_MODULE != 0
617619
WASMModule *import_module;

core/iwasm/interpreter/wasm_interp_classic.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,6 +1272,7 @@ wasm_interp_call_func_native(WASMModuleInstance *module_inst,
12721272
argv_ret[1] = frame->lp[1];
12731273
}
12741274
}
1275+
#if WASM_ENABLE_INVOKE_NATIVE != 0
12751276
else if (!func_import->call_conv_raw) {
12761277
ret = wasm_runtime_invoke_native(
12771278
exec_env, native_func_pointer, func_import->func_type,
@@ -1284,6 +1285,7 @@ wasm_interp_call_func_native(WASMModuleInstance *module_inst,
12841285
func_import->signature, func_import->attachment, frame->lp,
12851286
cur_func->param_cell_num, argv_ret);
12861287
}
1288+
#endif
12871289

12881290
if (!ret)
12891291
return;

core/iwasm/interpreter/wasm_interp_fast.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1203,7 +1203,7 @@ wasm_interp_call_func_native(WASMModuleInstance *module_inst,
12031203
WASMInterpFrame *frame;
12041204
uint32 argv_ret[2], cur_func_index;
12051205
void *native_func_pointer = NULL;
1206-
bool ret;
1206+
bool ret = false;
12071207
#if WASM_ENABLE_GC != 0
12081208
WASMFuncType *func_type;
12091209
uint8 *frame_ref;
@@ -1262,6 +1262,7 @@ wasm_interp_call_func_native(WASMModuleInstance *module_inst,
12621262
argv_ret[1] = frame->lp[1];
12631263
}
12641264
}
1265+
#if WASM_ENABLE_INVOKE_NATIVE != 0
12651266
else if (!func_import->call_conv_raw) {
12661267
ret = wasm_runtime_invoke_native(
12671268
exec_env, native_func_pointer, func_import->func_type,
@@ -1274,6 +1275,7 @@ wasm_interp_call_func_native(WASMModuleInstance *module_inst,
12741275
func_import->signature, func_import->attachment, frame->lp,
12751276
cur_func->param_cell_num, argv_ret);
12761277
}
1278+
#endif
12771279

12781280
if (!ret)
12791281
return;

core/iwasm/interpreter/wasm_loader.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2802,12 +2802,15 @@ load_function_import(const uint8 **p_buf, const uint8 *buf_end,
28022802
function->field_name = (char *)function_name;
28032803
function->attachment = NULL;
28042804
function->signature = NULL;
2805+
#if WASM_ENABLE_INVOKE_NATIVE != 0
28052806
function->call_conv_raw = false;
28062807

28072808
/* lookup registered native symbols first */
2809+
28082810
if (!no_resolve) {
28092811
wasm_resolve_import_func(parent_module, function);
28102812
}
2813+
#endif
28112814
return true;
28122815
fail:
28132816
return false;

core/iwasm/interpreter/wasm_runtime.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,13 +166,16 @@ wasm_resolve_import_func(const WASMModule *module, WASMFunctionImport *function)
166166
char error_buf[128];
167167
WASMModule *sub_module = NULL;
168168
#endif
169+
170+
#if WASM_ENABLE_INVOKE_NATIVE != 0
169171
function->func_ptr_linked = wasm_native_resolve_symbol(
170172
function->module_name, function->field_name, function->func_type,
171173
&function->signature, &function->attachment, &function->call_conv_raw);
172174

173175
if (function->func_ptr_linked) {
174176
return true;
175177
}
178+
#endif
176179

177180
#if WASM_ENABLE_MULTI_MODULE != 0
178181
if (!wasm_runtime_is_built_in_module(function->module_name)) {

0 commit comments

Comments
 (0)