WASM32 Support

This commit is contained in:
Rob Blanckaert 2025-08-30 12:54:51 -07:00
parent 95f506a6e7
commit 2e272ebdd5
15 changed files with 127 additions and 3 deletions

View File

@ -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 ()

View File

@ -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

View File

@ -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 ()

View File

@ -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)) {
@ -572,7 +574,9 @@ fail3:
os_mutex_destroy(&registered_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 @@ fail:
|| 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

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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)) {

View File

@ -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

View File

@ -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 <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

View File

@ -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
}

View File

@ -0,0 +1 @@
#include "../linux/platform_internal.h"

View File

@ -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})