diff --git a/build-scripts/config_common.cmake b/build-scripts/config_common.cmake index 52c6d94afa..0a9635c6d8 100644 --- a/build-scripts/config_common.cmake +++ b/build-scripts/config_common.cmake @@ -45,6 +45,8 @@ elseif (WAMR_BUILD_TARGET STREQUAL "RISCV32_ILP32") add_definitions(-DBUILD_TARGET_RISCV32_ILP32) elseif (WAMR_BUILD_TARGET STREQUAL "ARC") add_definitions(-DBUILD_TARGET_ARC) +elseif (WAMR_BUILD_TARGET STREQUAL "WASM32") + add_definitions(-DBUILD_TARGET_WASM32) else () message (FATAL_ERROR "-- WAMR build target isn't set") endif () diff --git a/core/config.h b/core/config.h index cb5db1d0cf..700cae637d 100644 --- a/core/config.h +++ b/core/config.h @@ -22,7 +22,8 @@ && !defined(BUILD_TARGET_RISCV32_ILP32D) \ && !defined(BUILD_TARGET_RISCV32_ILP32F) \ && !defined(BUILD_TARGET_RISCV32_ILP32) \ - && !defined(BUILD_TARGET_ARC) + && !defined(BUILD_TARGET_ARC) \ + && !defined(BUILD_TARGET_WASM32) /* clang-format on */ #if defined(__x86_64__) || defined(__x86_64) #define BUILD_TARGET_X86_64 @@ -52,6 +53,8 @@ #define BUILD_TARGET_RISCV32_ILP32D #elif defined(__arc__) #define BUILD_TARGET_ARC +#elif defined(__wasm32__) || defined(__EMSCRIPTEN__) +#define BUILD_TARGET_WASM32 #else #error "Build target isn't set" #endif @@ -294,6 +297,15 @@ #define WASM_DEBUG_PREPROCESSOR 0 #endif +/* Enable Invoke Native by default */ +#ifndef WASM_ENABLE_INVOKE_NATIVE +#if defined(BUILD_TARGET_WASM32) +#define WASM_ENABLE_INVOKE_NATIVE 0 +#else +#define WASM_ENABLE_INVOKE_NATIVE 1 +#endif +#endif + /* Enable opcode counter or not */ #ifndef WASM_ENABLE_OPCODE_COUNTER #define WASM_ENABLE_OPCODE_COUNTER 0 @@ -676,7 +688,7 @@ unless used elsewhere */ to speed up the calling process of invoking the AOT/JIT functions of these types from the host embedder */ #ifndef WASM_ENABLE_QUICK_AOT_ENTRY -#define WASM_ENABLE_QUICK_AOT_ENTRY 1 +#define WASM_ENABLE_QUICK_AOT_ENTRY WASM_ENABLE_INVOKE_NATIVE #endif /* Support AOT intrinsic functions which can be called from the AOT code diff --git a/core/iwasm/common/iwasm_common.cmake b/core/iwasm/common/iwasm_common.cmake index 15895b8e5e..6bab3589eb 100644 --- a/core/iwasm/common/iwasm_common.cmake +++ b/core/iwasm/common/iwasm_common.cmake @@ -91,6 +91,8 @@ elseif (WAMR_BUILD_TARGET MATCHES "RISCV*") set (source_all ${c_source_all} ${IWASM_COMMON_DIR}/arch/invokeNative_riscv.S) elseif (WAMR_BUILD_TARGET STREQUAL "ARC") set (source_all ${c_source_all} ${IWASM_COMMON_DIR}/arch/invokeNative_arc.s) +elseif (WAMR_BUILD_TARGET STREQUAL "WASM32") + set (source_all ${c_source_all}) else () message (FATAL_ERROR "Build target isn't set") endif () diff --git a/core/iwasm/common/wasm_runtime_common.c b/core/iwasm/common/wasm_runtime_common.c index 26283b3f8b..9031120527 100644 --- a/core/iwasm/common/wasm_runtime_common.c +++ b/core/iwasm/common/wasm_runtime_common.c @@ -460,9 +460,11 @@ wasm_runtime_env_init(void) if (bh_platform_init() != 0) return false; +#if WASM_ENABLE_INVOKE_NATIVE != 0 if (wasm_native_init() == false) { goto fail1; } +#endif #if WASM_ENABLE_MULTI_MODULE if (BHT_OK != os_mutex_init(®istered_module_list_lock)) { @@ -572,7 +574,9 @@ wasm_runtime_env_init(void) os_mutex_destroy(®istered_module_list_lock); fail2: #endif +#if WASM_ENABLE_INVOKE_NATIVE != 0 wasm_native_destroy(); +#endif fail1: bh_platform_destroy(); @@ -694,7 +698,9 @@ wasm_runtime_destroy_internal(void) thread_manager_destroy(); #endif +#if WASM_ENABLE_INVOKE_NATIVE != 0 wasm_native_destroy(); +#endif bh_platform_destroy(); wasm_runtime_memory_destroy(); @@ -791,6 +797,7 @@ wasm_runtime_full_init_internal(RuntimeInitArgs *init_args) } #endif +#if WASM_ENABLE_INVOKE_NATIVE != 0 if (init_args->n_native_symbols > 0 && !wasm_runtime_register_natives(init_args->native_module_name, init_args->native_symbols, @@ -798,6 +805,7 @@ wasm_runtime_full_init_internal(RuntimeInitArgs *init_args) wasm_runtime_destroy(); return false; } +#endif #if WASM_ENABLE_THREAD_MGR != 0 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) return table_type->max_size; } +#if WASM_ENABLE_INVOKE_NATIVE != 0 bool wasm_runtime_register_natives(const char *module_name, NativeSymbol *native_symbols, @@ -6249,6 +6258,8 @@ wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr, || defined(BUILD_TARGET_RISCV64_LP64D) \ || defined(BUILD_TARGET_RISCV64_LP64) */ +#endif /* end of WASM_ENABLE_INVOKE_NATIVE != 0 */ + bool wasm_runtime_call_indirect(WASMExecEnv *exec_env, uint32 element_index, uint32 argc, uint32 argv[]) @@ -7426,8 +7437,12 @@ bool wasm_runtime_is_import_func_linked(const char *module_name, const char *func_name) { +#if WASM_EANBLE_INVOKE_NATIVE != 0 return wasm_native_resolve_symbol(module_name, func_name, NULL, NULL, NULL, NULL); +#else + return false; +#endif } bool diff --git a/core/iwasm/compilation/aot.h b/core/iwasm/compilation/aot.h index f1ecccfb37..2cc0264bba 100644 --- a/core/iwasm/compilation/aot.h +++ b/core/iwasm/compilation/aot.h @@ -196,7 +196,9 @@ typedef struct AOTImportFunc { const char *signature; /* attachment */ void *attachment; +#if WASM_ENABLE_INVOKE_NATIVE != 0 bool call_conv_raw; +#endif bool call_conv_wasm_c_api; bool wasm_c_api_with_env; } AOTImportFunc; diff --git a/core/iwasm/interpreter/wasm.h b/core/iwasm/interpreter/wasm.h index 87552455e6..12e955f53e 100644 --- a/core/iwasm/interpreter/wasm.h +++ b/core/iwasm/interpreter/wasm.h @@ -611,7 +611,9 @@ typedef struct WASMFunctionImport { /* the type index of this function's func_type */ uint32 type_idx; #endif +#if WASM_ENABLE_INVOKE_NATIVE != 0 bool call_conv_raw; +#endif bool call_conv_wasm_c_api; #if WASM_ENABLE_MULTI_MODULE != 0 WASMModule *import_module; diff --git a/core/iwasm/interpreter/wasm_interp_classic.c b/core/iwasm/interpreter/wasm_interp_classic.c index 9ba3a5f4f0..c151ae92c1 100644 --- a/core/iwasm/interpreter/wasm_interp_classic.c +++ b/core/iwasm/interpreter/wasm_interp_classic.c @@ -1272,6 +1272,7 @@ wasm_interp_call_func_native(WASMModuleInstance *module_inst, argv_ret[1] = frame->lp[1]; } } +#if WASM_ENABLE_INVOKE_NATIVE != 0 else if (!func_import->call_conv_raw) { ret = wasm_runtime_invoke_native( exec_env, native_func_pointer, func_import->func_type, @@ -1284,6 +1285,7 @@ wasm_interp_call_func_native(WASMModuleInstance *module_inst, func_import->signature, func_import->attachment, frame->lp, cur_func->param_cell_num, argv_ret); } +#endif if (!ret) return; diff --git a/core/iwasm/interpreter/wasm_interp_fast.c b/core/iwasm/interpreter/wasm_interp_fast.c index a326018efc..878ec53db6 100644 --- a/core/iwasm/interpreter/wasm_interp_fast.c +++ b/core/iwasm/interpreter/wasm_interp_fast.c @@ -1203,7 +1203,7 @@ wasm_interp_call_func_native(WASMModuleInstance *module_inst, WASMInterpFrame *frame; uint32 argv_ret[2], cur_func_index; void *native_func_pointer = NULL; - bool ret; + bool ret = false; #if WASM_ENABLE_GC != 0 WASMFuncType *func_type; uint8 *frame_ref; @@ -1262,6 +1262,7 @@ wasm_interp_call_func_native(WASMModuleInstance *module_inst, argv_ret[1] = frame->lp[1]; } } +#if WASM_ENABLE_INVOKE_NATIVE != 0 else if (!func_import->call_conv_raw) { ret = wasm_runtime_invoke_native( exec_env, native_func_pointer, func_import->func_type, @@ -1274,6 +1275,7 @@ wasm_interp_call_func_native(WASMModuleInstance *module_inst, func_import->signature, func_import->attachment, frame->lp, cur_func->param_cell_num, argv_ret); } +#endif if (!ret) return; diff --git a/core/iwasm/interpreter/wasm_loader.c b/core/iwasm/interpreter/wasm_loader.c index 19ca249496..075c8e5c7d 100644 --- a/core/iwasm/interpreter/wasm_loader.c +++ b/core/iwasm/interpreter/wasm_loader.c @@ -2802,12 +2802,15 @@ load_function_import(const uint8 **p_buf, const uint8 *buf_end, function->field_name = (char *)function_name; function->attachment = NULL; function->signature = NULL; +#if WASM_ENABLE_INVOKE_NATIVE != 0 function->call_conv_raw = false; /* lookup registered native symbols first */ + if (!no_resolve) { wasm_resolve_import_func(parent_module, function); } +#endif return true; fail: return false; diff --git a/core/iwasm/interpreter/wasm_runtime.c b/core/iwasm/interpreter/wasm_runtime.c index 3642adf9b0..054b3b51c3 100644 --- a/core/iwasm/interpreter/wasm_runtime.c +++ b/core/iwasm/interpreter/wasm_runtime.c @@ -166,6 +166,8 @@ wasm_resolve_import_func(const WASMModule *module, WASMFunctionImport *function) char error_buf[128]; WASMModule *sub_module = NULL; #endif + +#if WASM_ENABLE_INVOKE_NATIVE != 0 function->func_ptr_linked = wasm_native_resolve_symbol( function->module_name, function->field_name, function->func_type, &function->signature, &function->attachment, &function->call_conv_raw); @@ -173,6 +175,7 @@ wasm_resolve_import_func(const WASMModule *module, WASMFunctionImport *function) if (function->func_ptr_linked) { return true; } +#endif #if WASM_ENABLE_MULTI_MODULE != 0 if (!wasm_runtime_is_built_in_module(function->module_name)) { diff --git a/core/shared/platform/common/libc-util/libc_errno.h b/core/shared/platform/common/libc-util/libc_errno.h index b0a8b78c6a..d4c92df5ce 100644 --- a/core/shared/platform/common/libc-util/libc_errno.h +++ b/core/shared/platform/common/libc-util/libc_errno.h @@ -6,7 +6,11 @@ #ifndef WASI_ERRNO_H #define WASI_ERRNO_H +#if !defined(__wasm32__) && !defined(__EMSCRIPTEN__) #include "platform_wasi_types.h" +#else +#include +#endif // Converts an errno error code to a WASI error code. __wasi_errno_t diff --git a/core/shared/platform/include/platform_api_extension.h b/core/shared/platform/include/platform_api_extension.h index 41ec5742bc..5755f52cc4 100644 --- a/core/shared/platform/include/platform_api_extension.h +++ b/core/shared/platform/include/platform_api_extension.h @@ -7,7 +7,19 @@ #define PLATFORM_API_EXTENSION_H #include "platform_common.h" + +#if !defined(__wasm32__) && !defined(__EMSCRIPTEN__) #include "platform_wasi_types.h" +#else +#include +#define __WASI_ESUCCESS (0) +#define __WASI_EINVAL (28) +#define __WASI_CLOCK_REALTIME (0) +#define __WASI_CLOCK_MONOTONIC (1) +#define __WASI_CLOCK_PROCESS_CPUTIME_ID (2) +#define __WASI_CLOCK_THREAD_CPUTIME_ID (3) +#endif + /** * The related data structures should be defined * in platform_internal.h diff --git a/core/shared/platform/wasm32/platform_init.c b/core/shared/platform/wasm32/platform_init.c new file mode 100644 index 0000000000..2aae13fa14 --- /dev/null +++ b/core/shared/platform/wasm32/platform_init.c @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2019 Intel Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + */ + +#include "platform_api_vmcore.h" + +int +bh_platform_init() +{ + return 0; +} + +void +bh_platform_destroy() +{} + +int +os_printf(const char *format, ...) +{ + int ret = 0; + va_list ap; + + va_start(ap, format); +#ifndef BH_VPRINTF + ret += vprintf(format, ap); +#else + ret += BH_VPRINTF(format, ap); +#endif + va_end(ap); + + return ret; +} + +int +os_vprintf(const char *format, va_list ap) +{ +#ifndef BH_VPRINTF + return vprintf(format, ap); +#else + return BH_VPRINTF(format, ap); +#endif +} diff --git a/core/shared/platform/wasm32/platform_internal.h b/core/shared/platform/wasm32/platform_internal.h new file mode 100644 index 0000000000..0f651bb810 --- /dev/null +++ b/core/shared/platform/wasm32/platform_internal.h @@ -0,0 +1 @@ +#include "../linux/platform_internal.h" \ No newline at end of file diff --git a/core/shared/platform/wasm32/shared_platform.cmake b/core/shared/platform/wasm32/shared_platform.cmake new file mode 100644 index 0000000000..dc3bb926cd --- /dev/null +++ b/core/shared/platform/wasm32/shared_platform.cmake @@ -0,0 +1,19 @@ + +# Copyright (C) 2019 Intel Corporation. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +set (PLATFORM_SHARED_DIR ${CMAKE_CURRENT_LIST_DIR}) + +add_definitions(-DBH_PLATFORM_WASM32) + +include_directories(${PLATFORM_SHARED_DIR}) +include_directories(${PLATFORM_SHARED_DIR}/../include) + +include (${CMAKE_CURRENT_LIST_DIR}/../common/posix/platform_api_posix.cmake) + +file (GLOB_RECURSE source_all ${PLATFORM_SHARED_DIR}/*.c) + +set (PLATFORM_SHARED_SOURCE ${source_all} ${PLATFORM_COMMON_POSIX_SOURCE}) + +file (GLOB header ${PLATFORM_SHARED_DIR}/../include/*.h) +LIST (APPEND RUNTIME_LIB_HEADER_LIST ${header})