Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions build-scripts/config_common.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()
Expand Down
16 changes: 14 additions & 2 deletions core/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions core/iwasm/common/iwasm_common.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()
Expand Down
15 changes: 15 additions & 0 deletions core/iwasm/common/wasm_runtime_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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(&registered_module_list_lock)) {
Expand Down Expand Up @@ -572,7 +574,9 @@ wasm_runtime_env_init(void)
os_mutex_destroy(&registered_module_list_lock);
fail2:
#endif
#if WASM_ENABLE_INVOKE_NATIVE != 0
wasm_native_destroy();
#endif
fail1:
bh_platform_destroy();

Expand Down Expand Up @@ -694,7 +698,9 @@ wasm_runtime_destroy_internal(void)
thread_manager_destroy();
#endif

#if WASM_ENABLE_INVOKE_NATIVE != 0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC WASM_ENABLE_INVOKE_NATIVE is designed to prevent the involvement of runtime built-in functions. In that case, -DWAMR_BUILD_LIBC_BUILTIN=0 -DWAMR_BUILD_LIBC_WASI=0 can achieve the same effect, along with -DWAMR_BUILD_QUICK_AOT_ENTRY=0.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i guess it's a good opportunity to replace the heavy ifdef conditions with something easier to maintain.

wasm_native_destroy();
#endif
bh_platform_destroy();

wasm_runtime_memory_destroy();
Expand Down Expand Up @@ -791,13 +797,15 @@ 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,
init_args->n_native_symbols)) {
wasm_runtime_destroy();
return false;
}
#endif

#if WASM_ENABLE_THREAD_MGR != 0
wasm_cluster_set_max_thread_num(init_args->max_thread_num);
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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[])
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions core/iwasm/compilation/aot.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions core/iwasm/interpreter/wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions core/iwasm/interpreter/wasm_interp_classic.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion core/iwasm/interpreter/wasm_interp_fast.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions core/iwasm/interpreter/wasm_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions core/iwasm/interpreter/wasm_runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,16 @@ 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);

if (function->func_ptr_linked) {
return true;
}
#endif

#if WASM_ENABLE_MULTI_MODULE != 0
if (!wasm_runtime_is_built_in_module(function->module_name)) {
Expand Down
4 changes: 4 additions & 0 deletions core/shared/platform/common/libc-util/libc_errno.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
#ifndef WASI_ERRNO_H
#define WASI_ERRNO_H

#if !defined(__wasm32__) && !defined(__EMSCRIPTEN__)
#include "platform_wasi_types.h"
#else
#include <wasi/api.h>
#endif

// Converts an errno error code to a WASI error code.
__wasi_errno_t
Expand Down
12 changes: 12 additions & 0 deletions core/shared/platform/include/platform_api_extension.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,19 @@
#define PLATFORM_API_EXTENSION_H

#include "platform_common.h"

#if !defined(__wasm32__) && !defined(__EMSCRIPTEN__)
#include "platform_wasi_types.h"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you avoid using platform_wasi_types.h?

#else
#include <wasi/api.h>
#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
Expand Down
43 changes: 43 additions & 0 deletions core/shared/platform/wasm32/platform_init.c
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions core/shared/platform/wasm32/platform_internal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "../linux/platform_internal.h"
19 changes: 19 additions & 0 deletions core/shared/platform/wasm32/shared_platform.cmake
Original file line number Diff line number Diff line change
@@ -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})
Loading